| 1 | #!/dsk/l1/misc/meunier/tools/bin/python3 |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | |
|---|
| 4 | # Note: |
|---|
| 5 | # This code requires python 3, and I haven't found |
|---|
| 6 | # a proper way of mixing python 2 and 3 without using |
|---|
| 7 | # the she/bang line at the top, which includes a local |
|---|
| 8 | # path... |
|---|
| 9 | |
|---|
| 10 | from __future__ import print_function |
|---|
| 11 | import os |
|---|
| 12 | import sys |
|---|
| 13 | import re |
|---|
| 14 | import subprocess |
|---|
| 15 | import shutil |
|---|
| 16 | import filecmp |
|---|
| 17 | import random |
|---|
| 18 | import math |
|---|
| 19 | |
|---|
| 20 | from stack import Stack |
|---|
| 21 | |
|---|
| 22 | from common import * |
|---|
| 23 | |
|---|
| 24 | # Creates graphes for random images with a density varying from 0 to 100% |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | with_features = True |
|---|
| 28 | |
|---|
| 29 | threads = [1, 4, 16, 64] |
|---|
| 30 | use_dsk = True |
|---|
| 31 | img_size = 2048 |
|---|
| 32 | granularities = [1, 4, 16] |
|---|
| 33 | rand_seed = 7 |
|---|
| 34 | |
|---|
| 35 | top_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) |
|---|
| 36 | scripts_path = os.path.join(top_path, 'scripts') |
|---|
| 37 | pyconf_file = os.path.join(scripts_path, "config.py") |
|---|
| 38 | |
|---|
| 39 | base_data_dir = "data_2016_09_21" # For timing information extracted |
|---|
| 40 | base_graph_dir = "graphes" # For storing generated graphes |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | # if with_features, each of these configuration must have been run with both features activated and deactivated |
|---|
| 44 | configs = [ |
|---|
| 45 | #{'SLOW':'1', 'FAST':'0', 'PARMERGE':'0', 'ARSP':'0'}, |
|---|
| 46 | {'SLOW':'0', 'FAST':'1', 'PARMERGE':'0', 'ARSP':'0'}, |
|---|
| 47 | {'SLOW':'0', 'FAST':'1', 'PARMERGE':'1', 'ARSP':'0'}, |
|---|
| 48 | {'SLOW':'0', 'FAST':'1', 'PARMERGE':'1', 'ARSP':'1'}, |
|---|
| 49 | ] |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | # Loading config file |
|---|
| 53 | with open(pyconf_file) as f: |
|---|
| 54 | code = compile(f.read(), pyconf_file, 'exec') |
|---|
| 55 | exec(code) |
|---|
| 56 | |
|---|
| 57 | nb_step = 0 |
|---|
| 58 | |
|---|
| 59 | if use_dsk: |
|---|
| 60 | try: |
|---|
| 61 | dsk_dir |
|---|
| 62 | if not os.path.exists(dsk_dir): |
|---|
| 63 | print("*** Warning: variable dsk_dir is not defined in %s file; using current directory for reading data files" % (short_path(pyconf_file))) |
|---|
| 64 | use_dsk = False |
|---|
| 65 | except OSError: |
|---|
| 66 | print("*** Warning: directory %s does not exist; using current directory for reading data files" % (dsk_dir)) |
|---|
| 67 | use_dsk = False |
|---|
| 68 | |
|---|
| 69 | # Updating output directories |
|---|
| 70 | if use_dsk: |
|---|
| 71 | data_dir = os.path.join(dsk_dir, base_data_dir) |
|---|
| 72 | graph_dir = os.path.join(dsk_dir, base_graph_dir) |
|---|
| 73 | else: |
|---|
| 74 | data_dir = os.path.join(scripts_path, base_data_dir) |
|---|
| 75 | graph_dir = os.path.join(scripts_path, base_graph_dir) |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | if not os.path.exists(graph_dir): |
|---|
| 80 | my_mkdir(graph_dir) |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | if with_features: |
|---|
| 85 | features = ["FT", "NF"] # Features and No-Features |
|---|
| 86 | else: |
|---|
| 87 | features = ["NF"] # No features only |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | # Phase 1: Extracting execution times |
|---|
| 91 | exec_time = {} |
|---|
| 92 | for config in configs: |
|---|
| 93 | fconfig = frozenset(config.items()) |
|---|
| 94 | exec_time[fconfig] = {} |
|---|
| 95 | for thread in threads: |
|---|
| 96 | exec_time[fconfig][thread] = {} |
|---|
| 97 | for granularity in granularities: |
|---|
| 98 | exec_time[fconfig][thread][granularity] = {} |
|---|
| 99 | for ftrs in features: # Features or No features |
|---|
| 100 | exec_time[fconfig][thread][granularity][ftrs] = {} |
|---|
| 101 | for density in range(0, 101): |
|---|
| 102 | exec_time[fconfig][thread][granularity][ftrs][density] = {} |
|---|
| 103 | random_img_file = get_random_img_file(density, img_size, img_size, granularity, rand_seed) |
|---|
| 104 | #random_img_file = get_short_random_img_file(density, granularity) |
|---|
| 105 | img_basename = os.path.splitext(random_img_file)[0] |
|---|
| 106 | log_file = get_filename(data_dir, thread, config, ftrs == "FT", img_basename) |
|---|
| 107 | lines = open(log_file, 'r') |
|---|
| 108 | for line in lines: |
|---|
| 109 | tokens = line.split() |
|---|
| 110 | if len(tokens) == 0: |
|---|
| 111 | continue |
|---|
| 112 | tag = tokens[0] |
|---|
| 113 | pattern = re.compile('\[STEP_([0-9]+)\]') |
|---|
| 114 | match = pattern.match(tag) |
|---|
| 115 | if match: |
|---|
| 116 | step = int(match.group(1)) |
|---|
| 117 | nb_step = max(int(step) + 1, nb_step) |
|---|
| 118 | value = tokens[len(tokens) - 1] |
|---|
| 119 | exec_time[fconfig][thread][granularity][ftrs][density][step] = value |
|---|
| 120 | #print("exec_time[fconfig][%d][%d][%s][%d][%s] = %s" % (thread, granularity, ftrs, density, step, exec_time[fconfig][thread][granularity][ftrs][density][step])) |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | # Phase 2: Creating plots |
|---|
| 125 | for config in configs: |
|---|
| 126 | fconfig = frozenset(config.items()) |
|---|
| 127 | for thread in threads: |
|---|
| 128 | for granularity in granularities: |
|---|
| 129 | plotter = Stack(["red", "blue", "green", "orange", "pink", "purple"], "Légende", "X", "Y") |
|---|
| 130 | X = [x for x in range(0, 101)] |
|---|
| 131 | plotter.add_x(X) |
|---|
| 132 | assert(nb_step == 5) |
|---|
| 133 | # Parallel Labeling |
|---|
| 134 | YPAR1 = [float(int(exec_time[fconfig][thread][granularity]["NF"][density][0]) / math.pow(img_size, 2)) for density in range(0, 101)] |
|---|
| 135 | # Merging (Parallel or Pyramidal) |
|---|
| 136 | YPAR2 = [float(int(exec_time[fconfig][thread][granularity]["NF"][density][1]) / math.pow(img_size, 2)) for density in range(0, 101)] |
|---|
| 137 | # Parallel Transitive Closure |
|---|
| 138 | YPTC2 = [float(int(exec_time[fconfig][thread][granularity]["NF"][density][2]) / math.pow(img_size, 2)) for density in range(0, 101)] |
|---|
| 139 | # Propagate Features if Features and ARSP, nothing otherwise |
|---|
| 140 | YPRF2 = [float(int(exec_time[fconfig][thread][granularity]["NF"][density][3]) / math.pow(img_size, 2)) for density in range(0, 101)] |
|---|
| 141 | # Parallel Relabelling |
|---|
| 142 | YPAR3 = [float(int(exec_time[fconfig][thread][granularity]["NF"][density][4]) / math.pow(img_size, 2)) for density in range(0, 101)] |
|---|
| 143 | |
|---|
| 144 | plotter.add_y(YPAR1) |
|---|
| 145 | plotter.add_y(YPAR2) |
|---|
| 146 | plotter.add_y(YPTC2) |
|---|
| 147 | plotter.add_y(YPRF2) |
|---|
| 148 | # displaying PAR3 |
|---|
| 149 | plotter.add_y(YPAR3) |
|---|
| 150 | |
|---|
| 151 | if with_features: |
|---|
| 152 | YPAR1F = [float(int(exec_time[fconfig][thread][granularity]["FT"][density][0]) / math.pow(img_size, 2)) for density in range(0, 101)] |
|---|
| 153 | YPAR2F = [float(int(exec_time[fconfig][thread][granularity]["FT"][density][1]) / math.pow(img_size, 2)) for density in range(0, 101)] |
|---|
| 154 | YPTC2F = [float(int(exec_time[fconfig][thread][granularity]["FT"][density][2]) / math.pow(img_size, 2)) for density in range(0, 101)] |
|---|
| 155 | YPRF2F = [float(int(exec_time[fconfig][thread][granularity]["FT"][density][3]) / math.pow(img_size, 2)) for density in range(0, 101)] |
|---|
| 156 | YPAR3F = [float(int(exec_time[fconfig][thread][granularity]["FT"][density][4]) / math.pow(img_size, 2)) for density in range(0, 101)] |
|---|
| 157 | TotalNF = [x + y + z + t + u for x, y, z, t, u in zip(YPAR1, YPAR2, YPTC2, YPRF2, YPAR3)] # Total No Features |
|---|
| 158 | TotalFT = [x + y + z + t + u for x, y, z, t, u in zip(YPAR1F, YPAR2F, YPTC2F, YPRF2F, YPAR3F)] # Total with Features |
|---|
| 159 | YFCT = [max(0, x - y) for x, y in zip(TotalFT, TotalNF)] # Difference = Features Computation Time |
|---|
| 160 | plotter.add_y(YFCT) |
|---|
| 161 | |
|---|
| 162 | plotter.plot() |
|---|
| 163 | graph_name = get_graph_filename(graph_dir, thread, granularity, config, ".pdf") |
|---|
| 164 | print("# Creating graph %s" % short_path(graph_name)) |
|---|
| 165 | plotter.save(graph_name) |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|