loutre.py 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/python
import getopt
import sys
import math
import copy
import os
import socket
import subprocess
import re
import types
Martin Khannouz's avatar
Martin Khannouz committed
11
import glob
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

class ScalFMMConfig(object):
    num_threads    = 1
    num_nodes      = 1
    algorithm      = "implicit"
    model          = "cube"
    num_particules = 10000
    height         = 4
    bloc_size      = 100
    order          = 5

    def show(self):
        print ("=== Simulation parameters ===")
        print ("Number of nodes: " + str(self.num_nodes))
        print ("Number of threads: " + str(self.num_threads))
        print ("Model: " + str(self.model))
        print ("Number of particules: " + str(self.num_particules))
        print ("Height: " + str(self.height))
        print ("Bloc size: " + str(self.bloc_size))
        print ("Order: " + str(self.order))

    def gen_header(self):
        columns = [
            "model",
            "algo",
            "nnode",
            "nthreads",
            "npart",
            "height",
            "bsize",
            "global_time",
            "runtime_time",
            "task_time",
            "idle_time",
            "scheduling_time",
            "communication_time",
Martin Khannouz's avatar
Martin Khannouz committed
48
            "communication_vol",
49 50 51 52 53 54 55 56 57 58
            "rmem",
        ]
        header = ""
        for i in range(len(columns)):
            if not i == 0:
                header += ","
            header += "\"" + columns[i] + "\""
        header += "\n"
        return header

Martin Khannouz's avatar
Martin Khannouz committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    def gen_header_gantt(self):
        columns = [
            "model",
            "algo",
            "nnode",
            "nthreads",
            "npart",
            "height",
            "bsize",
            "filename",
            "start_profiling",
            "stop_profiling",
        ]
        header = ""
        for i in range(len(columns)):
            if not i == 0:
                header += ","
            header += "\"" + columns[i] + "\""
        header += "\n"
        return header
79

Martin Khannouz's avatar
Martin Khannouz committed
80
    def gen_record(self, global_time, runtime_time, task_time, idle_time, scheduling_time, communication_time, communication_vol, rmem):
81 82 83 84 85 86 87 88 89 90 91 92 93
        columns = [
            self.model,
            self.algorithm,
            self.num_nodes,
            self.num_threads,
            self.num_particules,
            self.height,
            self.bloc_size,
            global_time,
            runtime_time,
            task_time,
            idle_time,
            scheduling_time,
94
            communication_time,
Martin Khannouz's avatar
Martin Khannouz committed
95
            communication_vol,
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
            rmem,
        ]
        record = ""
        for i in range(len(columns)):
            if not i == 0:
                record += ","
            if (type(columns[i]) is bool or
                type(columns[i]) == str):
                record += "\""
            record += str(columns[i])
            if (type(columns[i]) == bool or
                type(columns[i]) == str):
                record += "\""
        record += "\n"
        return record

Martin Khannouz's avatar
Martin Khannouz committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    def gen_record_gantt(self, filename, start_profiling, stop_profiling):
        columns = [
            self.model,
            self.algorithm,
            self.num_nodes,
            self.num_threads,
            self.num_particules,
            self.height,
            self.bloc_size,
            filename,
            start_profiling,
            stop_profiling,
        ]
        record = ""
        for i in range(len(columns)):
            if not i == 0:
                record += ","
            if (type(columns[i]) is bool or
                type(columns[i]) == str):
                record += "\""
            record += str(columns[i])
            if (type(columns[i]) == bool or
                type(columns[i]) == str):
                record += "\""
        record += "\n"
        return record

139 140 141 142 143 144 145 146 147 148 149
def get_times_from_trace_file(filename):
    cmd = "starpu_trace_state_stats.py " + filename
    proc = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
    stdout, stderr = proc.communicate()
    if not proc.returncode == 0:
        sys.exit("FATAL: Failed to parse trace.rec!")
        return proc.returncode
    task_time = 0.0
    idle_time = 0.0
    runtime_time = 0.0
    scheduling_time = 0.0
150
    communication_time = 0.0
151 152 153 154 155 156 157
    for line in stdout.decode().splitlines():
        arr = line.replace("\"", "").split(",")
        if arr[0] == "Name":
            continue
        if len(arr) >= 4:
            if arr[2] == "Runtime":
                if arr[0] == "Scheduling":
Martin Khannouz's avatar
Martin Khannouz committed
158
                    scheduling_time += float(arr[3])
159
                else:
Martin Khannouz's avatar
Martin Khannouz committed
160
                    runtime_time += float(arr[3])
161 162 163
            elif arr[2] == "Task":
                task_time += float(arr[3])
            elif arr[2] == "Other":
Martin Khannouz's avatar
Martin Khannouz committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177
                idle_time += float(arr[3])
            elif arr[2] == "MPI":
                communication_time += float(arr[3])
            elif arr[2] == "User":
                if arr[0] == "Decoding task for MPI":
                    communication_time += float(arr[3])
                elif arr[0] == "Preparing task for MPI":
                    communication_time += float(arr[3])
                elif arr[0] == "Post-processing task for MPI":
                    communication_time += float(arr[3])
                else:
                    runtime_time += float(arr[3])
            else:
                print("Error type " + arr[2])
178
            # sys.exit("Invalid time!")
179
    return runtime_time, task_time, idle_time, scheduling_time, communication_time
180

Martin Khannouz's avatar
Martin Khannouz committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
def generate_gantt(config, gantt_filename, initial_dir):
    if (os.path.isfile(gantt_filename)):
        gantt_file = open(gantt_filename, "a")
    else:
        gantt_file = open(gantt_filename, "w")
        gantt_file.write(config.gen_header_gantt())
    csv_paje_filename = initial_dir + "/paje.csv"
    simple_paje_filename = initial_dir + "/paje.trace"

    #Get start profiling time
    cmd = "grep start_profiling " + simple_paje_filename
    proc = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
    stdout, stderr = proc.communicate()
    if not proc.returncode == 0:
        sys.exit("FATAL: Failed to parse " + simple_paje_filename + "!")
        return proc.returncode
    start_profiling = float("inf")
    for line in stdout.decode().splitlines():
        arr = line.split()
        start_marker = float(arr[1])
        if(start_marker < start_profiling):
            start_profiling = start_marker
    
    #Get stop profiling time
    cmd = "grep stop_profiling " + simple_paje_filename
    proc = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
    stdout, stderr = proc.communicate()
    if not proc.returncode == 0:
        sys.exit("FATAL: Failed to parse " + simple_paje_filename + "!")
        return proc.returncode
    stop_profiling = 0.0
    for line in stdout.decode().splitlines():
        arr = line.split()
        stop_marker = float(arr[1])
        if(stop_marker > stop_profiling):
            stop_profiling = stop_marker

    gantt_file.write(config.gen_record_gantt(csv_paje_filename, start_profiling, stop_profiling))

220 221 222 223
def main():
    output_trace_file=""
    trace_filename="trace.rec"
    output_filename="loutre.db"
Martin Khannouz's avatar
Martin Khannouz committed
224
    gantt_database=""
225 226 227 228

    long_opts = ["help",
                 "trace-file=",
                 "output-trace-file=",
Martin Khannouz's avatar
Martin Khannouz committed
229
                 "gantt-database=",
230 231
                 "output-file="]

Martin Khannouz's avatar
Martin Khannouz committed
232
    opts, args = getopt.getopt(sys.argv[1:], "ht:i:o:g:", long_opts)
233 234 235 236 237 238 239 240 241 242 243
    for o, a in opts:
        if o in ("-h", "--help"):
            # usage()
            print("No help")
            sys.exit()
        elif o in ("-t", "--trace-file"):
            trace_filename = str(a)
        elif o in ("-i", "--output-trace-file"):
            output_trace_file = str(a)
        elif o in ("-o", "--output-file"):
            output_filename = str(a)
Martin Khannouz's avatar
Martin Khannouz committed
244 245
        elif o in ("-g", "--gantt-database"):
            gantt_database = str(a)
246 247 248 249 250 251 252 253 254 255
        else:
            assert False, "unhandled option"

    config=ScalFMMConfig()
    rmem = 0
    global_time = 0.0
    runtime_time = 0.0
    task_time = 0.0
    idle_time = 0.0
    scheduling_time = 0.0
Martin Khannouz's avatar
Martin Khannouz committed
256 257
    communication_time = 0.0
    communication_vol = 0.0
258 259 260 261 262 263 264 265 266 267 268 269

    if (os.path.isfile(output_filename)): #Time in milli
        output_file = open(output_filename, "a")
    else:
        output_file = open(output_filename, "w")
        output_file.write(config.gen_header())

    with open(output_trace_file, "r") as ins:
        for line in ins:
            if re.search("Average", line):
                a = re.findall("[-+]?\d*\.\d+|\d+", line)
                if len(a) == 1:
270
                    global_time = float(a[0])*1000 # Else it is in sec
271
            elif re.search("Total particles", line):
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
                a = re.findall("[-+]?\d*\.\d+|\d+", line)
                if len(a) == 1:
                    config.num_particules = int(a[0])
            elif re.search("Group size", line):
                a = re.findall("[-+]?\d*\.\d+|\d+", line)
                if len(a) == 1:
                    config.bloc_size = int(a[0])
            elif re.search("Nb node", line):
                a = re.findall("[-+]?\d*\.\d+|\d+", line)
                if len(a) == 1:
                    config.num_nodes = int(a[0])
            elif re.search("Tree height", line):
                a = re.findall("[-+]?\d*\.\d+|\d+", line)
                if len(a) == 1:
                    config.height = int(a[0])
            elif re.search("Nb thread", line):
                a = re.findall("[-+]?\d*\.\d+|\d+", line)
                if len(a) == 1:
                    config.num_threads = int(a[0])
            elif re.search("Model", line):
                config.model = line[line.index(":")+1:].strip()
            elif re.search("Algorithm", line):
                config.algorithm = line[line.index(":")+1:].strip()
Martin Khannouz's avatar
Martin Khannouz committed
295 296 297 298 299 300 301
            elif re.search("TOTAL", line) and re.search("starpu_comm_stats", line):
                a = re.findall("(\d*\.\d+|\d+).MB", line)
                if len(a) == 1:
                    communication_vol += float(a[0])
                
    if(gantt_database != ""):
        generate_gantt(config, gantt_database, os.path.dirname(trace_filename))
302

Martin Khannouz's avatar
Martin Khannouz committed
303
    print("Generating time ...")
304
    if (os.path.isfile(trace_filename)): #Time in milli
305
        runtime_time, task_time, idle_time, scheduling_time, communication_time = get_times_from_trace_file(trace_filename)
306 307
    else:
        print("File doesn't exist " + trace_filename)
Martin Khannouz's avatar
Martin Khannouz committed
308

309 310 311 312 313
    sum_time = (runtime_time + task_time + scheduling_time + communication_time)/(config.num_nodes*config.num_threads)
    diff_time = float('%.2f'%(abs(global_time-sum_time)/global_time))

    if diff_time > 0.01:   
        print('\033[31m/!\\Timing Error of ' + str(diff_time) + '\033[39m')
Martin Khannouz's avatar
Martin Khannouz committed
314
        print('\033[31m Global ' + str(global_time) + ' Sum ' + str(sum_time) + '\033[39m')
315 316

    # Write a record to the output file.
317
    output_file.write(config.gen_record(global_time,
318 319 320 321
                      float(runtime_time),
                      float(task_time),
                      float(idle_time),
                      float(scheduling_time),
322
                      float(communication_time),
Martin Khannouz's avatar
Martin Khannouz committed
323
                      float(communication_vol),
324 325 326
                      int(rmem)))

main()