Index: soft/giet_vm/applications/rosenfeld/scripts/run_simus.py
===================================================================
--- soft/giet_vm/applications/rosenfeld/scripts/run_simus.py	(revision 823)
+++ soft/giet_vm/applications/rosenfeld/scripts/run_simus.py	(revision 826)
@@ -5,6 +5,6 @@
 # python 2 as it will execute on computation servers
 
-# TODO:
-#       Can we do something about assert in perf eval?
+# FIXME?
+#       When deactivating assert for perf eval, exec times are slower...
 
 from __future__ import print_function
@@ -29,5 +29,4 @@
 base_logs_dir     = "logs"   # Log of execution, i.e. what is printed on screen
 base_data_dir     = "data"   # For timing information extracted
-base_rand_img_dir = "random_images" # For storing random images if using dsk
 
 
@@ -43,24 +42,26 @@
 # - With check_results, num_app_runs should be used, so as to have a number of checks equals to the number of runs,
 #     because only one check per application run is performed
-num_app_runs = 1        # Number of times the application is launched per configuration
-num_internal_runs = 20  # Number of times the image is processed inside the application
+num_app_runs = 1       # Number of times the application is launched per configuration
+num_internal_runs = 10 # Number of times the image is processed inside the application
 check_results = False
 eval_perf = True
 use_valgrind = False
 use_rand_images = True
-threads = [1, 2, 4, 8, 16, 32, 64]
-#threads = [1, 2]
+threads = [1, 4, 16, 64]
 use_dsk = True
 # Using dsk will store generated random images, otherwise they are re-generated at each run to save disk space
+granularities = [1, 4, 16]
+img_size = 2048
 
 # Configurations
 configs = [
         #{'SLOW':'1', 'FAST':'0', 'FEATURES':'0', 'PARMERGE':'0', 'ARSP':'0'},
-        #{'SLOW':'0', 'FAST':'1', 'FEATURES':'0', 'PARMERGE':'0', 'ARSP':'0'},
+        {'SLOW':'0', 'FAST':'1', 'FEATURES':'0', 'PARMERGE':'0', 'ARSP':'0'},
         #{'SLOW':'1', 'FAST':'0', 'FEATURES':'1', 'PARMERGE':'0', 'ARSP':'0'},
-        #{'SLOW':'0', 'FAST':'1', 'FEATURES':'1', 'PARMERGE':'0', 'ARSP':'0'},
+        {'SLOW':'0', 'FAST':'1', 'FEATURES':'1', 'PARMERGE':'0', 'ARSP':'0'},
         {'SLOW':'0', 'FAST':'1', 'FEATURES':'0', 'PARMERGE':'1', 'ARSP':'0'},
-        #{'SLOW':'0', 'FAST':'1', 'FEATURES':'1', 'PARMERGE':'1', 'ARSP':'0'},
-        #{'SLOW':'0', 'FAST':'1', 'FEATURES':'0', 'PARMERGE':'1', 'ARSP':'1'},
+        {'SLOW':'0', 'FAST':'1', 'FEATURES':'1', 'PARMERGE':'1', 'ARSP':'0'},
+        {'SLOW':'0', 'FAST':'1', 'FEATURES':'0', 'PARMERGE':'1', 'ARSP':'1'},
+        {'SLOW':'0', 'FAST':'1', 'FEATURES':'1', 'PARMERGE':'1', 'ARSP':'1'},
 ]
 
@@ -68,6 +69,4 @@
 # Other parameters which shouldn't be changed
 rand_seed = 7
-granularity = 1 # constant for now
-img_size = 2048
 
 check_pyconf_file(pyconf_file)
@@ -83,9 +82,22 @@
             os.mkdir(dsk_dir)
     except NameError:
-        print("*** Warning: variable dsk_dir is not defined in %s file; using current directory for storing output files" % (short_path(pyconf_file)))
+        print("*** Warning: variable dsk_dir is not defined in file %s; using current directory for storing output files" % (short_path(pyconf_file)))
         use_dsk = False
     except OSError:
         print("*** Warning: Impossible to create directory %s; using current directory for storing output files" % (dsk_dir))
         use_dsk = False
+
+if use_rand_images:
+    try:
+        rand_img_dir
+        if not os.path.exists(rand_img_dir):
+            print("mkdir %s", rand_img_dir)
+            os.mkdir(rand_img_dir)
+    except NameError:
+        print("*** Error: variable rand_img_dir, containing the path the directory of the random images (either pre-existing or to be generated), is not defined in file %s" % (short_path(pyconf_file)))
+        sys.exit(1)
+    except OSError:
+        print("*** Error: Impossible to create directory %s" % (rand_img_dir))
+        sys.exit(1)
 
 
@@ -95,5 +107,4 @@
     logs_dir     = os.path.join(dsk_dir, base_logs_dir)
     data_dir     = os.path.join(dsk_dir, base_data_dir)
-    rand_img_dir = os.path.join(dsk_dir, base_rand_img_dir)
 else:
     output_dir = os.path.join(scripts_path, base_output_dir)
@@ -110,29 +121,4 @@
 if check_results and num_internal_runs != 1:
     print("*** Warning: using check_results with num_internal_runs != 1\n")
-
-
-
-def gen_random_image(filename, x, y, granularity, density, seed):
-    random.seed(seed)
-    img = [[0 for a in range(x)] for b in range(y)] 
-    for i in range(0, x, granularity):
-        for j in range(0, y, granularity):
-            r = random.random();
-            if r < density:
-                px = 255
-            else:
-                px = 0
-
-            for di in range(0, granularity):
-                for dj in range(0, granularity):
-                    if i + di < x and j + dj < y:
-                        img[i + di][j + dj] = px;
-
-    f = open(filename, 'wb')
-    f.write("P5\n%d %d\n255\n" % (x, y))
-    for j in range(0, y):
-        bimg = bytearray(img[j])
-        f.write(bimg)
-    f.close()
 
 
@@ -186,8 +172,4 @@
     my_mkdir(data_dir)
 
-if use_dsk and not os.path.exists(rand_img_dir):
-    my_mkdir(rand_img_dir)
-
-
 
 
@@ -196,5 +178,4 @@
 
 for config in configs:
-    img_idx = 0
     fconfig = frozenset(config.iteritems())
     perf_array[fconfig] = {}
@@ -210,155 +191,153 @@
     my_chdir(scripts_path)
 
-    while not use_rand_images and img_idx != len(images) or use_rand_images and img_idx != 101:
-        # Compute image and stat filenames
-        if use_rand_images:
-            if use_dsk:
+    for granularity in granularities:
+        perf_array[fconfig][granularity] = {}
+        img_idx = 0
+        while not use_rand_images and img_idx != len(images) or use_rand_images and img_idx != 101:
+            # Compute image and stat filenames
+            if use_rand_images:
                 random_img_file = get_random_img_file(img_idx, img_size, img_size, granularity, rand_seed)
                 random_img_file = os.path.join(rand_img_dir, random_img_file)
+                if not os.path.isfile(random_img_file):
+                    # We generate the random image if it does not exist
+                    print("# Generating random image %s with granularity = %d and density = %d" % (random_img_file, granularity, img_idx))
+                    gen_random_image(random_img_file, img_size, img_size, granularity, float(img_idx) / 100, rand_seed)
+                image = random_img_file
             else:
-                random_img_file = "random.pgm"
-            if not (use_dsk and os.path.isfile(random_img_file)):
-                # We re-generate the random image if not using the dsk or if it does not exist
-                print("# Generating random image %s with density = %d" % (random_img_file, img_idx))
-                gen_random_image(random_img_file, img_size, img_size, 1, float(img_idx) / 100, rand_seed)
-
-            image = random_img_file
-        else:
-            image = images[img_idx]
-        img_basename = os.path.splitext(os.path.basename(image))[0]
-        perf_array[fconfig][img_basename] = {}
-        ref_bmpfile  = os.path.join(output_dir, os.path.splitext(os.path.basename(image))[0] + "_ref.bmp")
-        ref_statfile = os.path.join(output_dir, os.path.splitext(os.path.basename(image))[0] + "_ref.txt")
-
-        for nthreads in threads:
-            perf_array[fconfig][img_basename][nthreads] = {}
-            for run in range(num_app_runs):
-                if not os.path.exists(ref_bmpfile):
-                    bmpfile = ref_bmpfile
-                else:
-                    bmpfile = os.path.join(output_dir, os.path.splitext(os.path.basename(image))[0] + ".bmp")
-                    if os.path.exists(bmpfile):
-                        os.remove(bmpfile)
-    
-                if not os.path.exists(ref_statfile):
-                    statfile = ref_statfile
-                else:
-                    statfile = os.path.join(output_dir, os.path.splitext(os.path.basename(image))[0] + ".txt")
-    
-                cmd = []
-                if use_valgrind:
-                    cmd.append('valgrind')
-    
-                cmd.extend([short_path(binary_file), '-n', str(nthreads), '-i', short_path(image)])
-
-                if num_internal_runs > 1:
-                    cmd.extend(['-r', str(num_internal_runs)])
-                
-                if check_results:
-                    cmd.extend(['-o', short_path(bmpfile), '-g'])
-                
-                if check_results and features:
-                    cmd.append('-d')
-    
-                config_keys = config.keys()
-                logfile = get_filename(logs_dir, nthreads, config, features, img_basename)
-                output = print_and_popen(cmd, logfile)
-                outlines = output.splitlines()
-
-                # if performance evaluation, get timing measurements
-                # Only the last application run is considered
+                image = images[img_idx]
+            img_basename = os.path.splitext(os.path.basename(image))[0]
+            perf_array[fconfig][granularity][img_basename] = {}
+            ref_bmpfile  = os.path.join(output_dir, os.path.splitext(os.path.basename(image))[0] + "_ref.bmp")
+            ref_statfile = os.path.join(output_dir, os.path.splitext(os.path.basename(image))[0] + "_ref.txt")
+    
+            for nthreads in threads:
+                perf_array[fconfig][granularity][img_basename][nthreads] = {}
+                for run in range(num_app_runs):
+                    if not os.path.exists(ref_bmpfile):
+                        # Generating the reference file if it does not exist
+                        bmpfile = ref_bmpfile
+                    else:
+                        bmpfile = os.path.join(output_dir, os.path.splitext(os.path.basename(image))[0] + ".bmp")
+                        if os.path.exists(bmpfile):
+                            os.remove(bmpfile)
+        
+                    if not os.path.exists(ref_statfile):
+                        statfile = ref_statfile
+                    else:
+                        statfile = os.path.join(output_dir, os.path.splitext(os.path.basename(image))[0] + ".txt")
+                        if os.path.exists(statfile):
+                            os.remove(statfile)
+ 
+                    cmd = []
+                    if use_valgrind:
+                        cmd.append('valgrind')
+        
+                    cmd.extend([short_path(binary_file), '-n', str(nthreads), '-i', short_path(image)])
+    
+                    if num_internal_runs > 1:
+                        cmd.extend(['-r', str(num_internal_runs)])
+                    
+                    if check_results:
+                        cmd.extend(['-o', short_path(bmpfile), '-g'])
+                    
+                    if check_results and features:
+                        cmd.append('-d')
+        
+                    config_keys = config.keys()
+                    logfile = get_filename(logs_dir, nthreads, config, features, img_basename)
+                    output = print_and_popen(cmd, logfile)
+                    outlines = output.splitlines()
+    
+                    # if performance evaluation, get timing measurements
+                    # Only the last application run is considered
+                    if eval_perf:
+                        for line in outlines:
+                            tokens = line.split()
+                            if len(tokens) == 0:
+                                continue
+                            tag = tokens[0]
+                            pattern = re.compile('\[THREAD_STEP_([0-9]+)\]');
+                            match = pattern.match(tag)
+                            if match:
+                                step = match.group(1)
+                                value = tokens[len(tokens) - 1]
+                                perf_array[fconfig][granularity][img_basename][nthreads][step] = int(value)
+    
+    
+                    # Checking against reference output image    
+                    if check_results and bmpfile != ref_bmpfile:
+                        print("diff %s %s" % (short_path(bmpfile), short_path(ref_bmpfile)))
+                        if not filecmp.cmp(bmpfile, ref_bmpfile):
+                            print("*** Error: files %s and %s differ" % (short_path(bmpfile), short_path(ref_bmpfile)))
+                            sys.exit(1)
+        
+                    # Checking for valgrind errors
+                    if use_valgrind:
+                        if not "== ERROR SUMMARY: 0 errors from 0 contexts" in output:
+                            print("*** Error: Valgrind error")
+                            sys.exit(1)
+                        if not "== All heap blocks were freed -- no leaks are possible" in output:
+                            print("*** Error: Valgrind detected a memory leak")
+                            sys.exit(1)
+        
+                    # Extracting features for correctness verification
+                    if check_results and features:
+                        stat_array = {}
+                        in_stats = False
+                        index = 0
+                        for line in outlines:
+                            if "[STATS]" in line:
+                                in_stats = True
+                                continue
+                            if "[/STATS]" in line:
+                                in_stats = False
+                                break
+                            if in_stats:
+                                tokens = line.split()
+                                assert(len(tokens) == 8)
+                                stat_array[index] = {}
+                                for j in range(len(tokens)):
+                                    stat_array[index][j] = tokens[j]
+                                index += 1
+        
+                        # Dump stat array in stat file
+                        file = open(statfile, 'w')
+                        for i in range(len(stat_array)):
+                            for j in range(8): # 8 is the number of features per element
+                                file.write("%s " % stat_array[i][j])
+                            file.write("\n");
+                        file.close()
+        
+                        # Comparison to reference
+                        if statfile != ref_statfile:
+                            print("diff %s %s" % (short_path(statfile), short_path(ref_statfile)))
+                            if not filecmp.cmp(statfile, ref_statfile):
+                                print("*** Error: feature files %s and %s differ" % (short_path(statfile), short_path(ref_statfile)))
+                                sys.exit(1)
+    
+                # End of the num_runs simus
                 if eval_perf:
-                    for line in outlines:
-                        tokens = line.split()
-                        if len(tokens) == 0:
-                            continue
-                        tag = tokens[0]
-                        pattern = re.compile('\[THREAD_STEP_([0-9]+)\]');
-                        match = pattern.match(tag)
-                        if match:
-                            step = match.group(1)
-                            value = tokens[len(tokens) - 1]
-                            perf_array[fconfig][img_basename][nthreads][step] = int(value)
-
-
-                # Checking against reference output image    
-                if check_results and bmpfile != ref_bmpfile:
-                    print("diff %s %s" % (short_path(bmpfile), short_path(ref_bmpfile)))
-                    if not filecmp.cmp(bmpfile, ref_bmpfile):
-                        print("*** Error: files %s and %s differ" % (short_path(bmpfile), short_path(ref_bmpfile)))
-                        sys.exit(1)
-    
-                # Checking for valgrind errors
-                if use_valgrind:
-                    if not "== ERROR SUMMARY: 0 errors from 0 contexts" in output:
-                        print("*** Error: Valgrind error")
-                        sys.exit(1)
-                    if not "== All heap blocks were freed -- no leaks are possible" in output:
-                        print("*** Error: Valgrind detected a memory leak")
-                        sys.exit(1)
-    
-                # Extracting features for correctness verification
-                if check_results and features:
-                    stat_array = {}
-                    in_stats = False
-                    index = 0
-                    for line in outlines:
-                        if "[STATS]" in line:
-                            in_stats = True
-                            continue
-                        if "[/STATS]" in line:
-                            in_stats = False
-                            break
-                        if in_stats:
-                            tokens = line.split()
-                            assert(len(tokens) == 8)
-                            stat_array[index] = {}
-                            for j in range(len(tokens)):
-                                stat_array[index][j] = tokens[j]
-                            index += 1
-    
-                    # Dump stat array in stat file
-                    file = open(statfile, 'w')
-                    for i in range(len(stat_array)):
-                        for j in range(8):
-                            file.write("%s " % stat_array[i][j])
-                        file.write("\n");
-                    file.close()
-    
-                    # Comparison to reference
-                    if statfile != ref_statfile:
-                        print("diff %s %s" % (short_path(statfile), short_path(ref_statfile)))
-                        if not filecmp.cmp(statfile, ref_statfile):
-                            print("*** Error: feature files %s and %s differ" % (short_path(statfile), short_path(ref_statfile)))
-                            sys.exit(1)
-
-            # End of the num_runs simus
-            if eval_perf:
-                if use_rand_images:
-                    idx = img_idx
-                else:
-                    idx = None
-                datafile = get_filename(data_dir, nthreads, config, features, img_basename)
-                file = open(datafile, 'w')
-                for step in sorted(perf_array[fconfig][img_basename][nthreads].keys()):
-                    # Average time for each step
-                    file.write("[STEP_%s]   %d\n" % (step, perf_array[fconfig][img_basename][nthreads][step]))
-
-        img_idx += 1
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+                    datafile = get_filename(data_dir, nthreads, config, features, img_basename)
+                    file = open(datafile, 'w')
+                    for step in sorted(perf_array[fconfig][granularity][img_basename][nthreads].keys()):
+                        # Average time for each step
+                        file.write("[STEP_%s]   %d\n" % (step, perf_array[fconfig][granularity][img_basename][nthreads][step]))
+    
+            img_idx += 1
+        #end image list (101 rand or explicit list)
+    #end granularity
+#end config
+
+
+
+
+
+
+
+
+
+
+
+
+
+
