SNABSuite  0.x
Spiking Neural Architecture Benchmark Suite
generate_bench_json.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 
4 # SNABSuite -- Spiking Neural Architecture Benchmark Suite
5 # Copyright (C) 2018 Christoph Ostrau
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>
19 
20 """
21 Generate benchmarks.json for the web frontend
22 @author Michael Thies, Christoph Ostrau
23 """
24 from __future__ import print_function
25 from builtins import str
26 from builtins import range
27 import sys
28 import CppHeaderParser
29 import re
30 import os
31 import json
32 
33 simulators = ["nest", "spinnaker", "nmpm1", "spikey"] # SNABSuite simulators
34 web_simulators = ["NEST", "SpiNNaker", "BrainScaleS",
35  "Spikey"] # Web frontend simulators
36 bench_index = ["Single Core/Smallest Network", "Single Chip",
37  "Small System", "Large System"] # Benchmark index and their description
38 
39 
40 def get_key_arrays(dictionary):
41  """
42  Iterates through the dictionary, gathers all entries with lists
43 
44  dictionary -- dict to iteratively search for lists
45  """
46 
47  temp_dict = dict()
48  length = 0
49  for i in dictionary:
50  if type(dictionary[i]) == list:
51  temp_dict[i] = dictionary[i]
52  if(length < len(dictionary[i])):
53  length = len(dictionary[i])
54  else:
55  if type(dictionary[i]) == dict:
56  a, length2 = get_key_arrays(dictionary[i])
57  if len(a) != 0:
58  temp_dict[i] = a
59  if(length < length2):
60  length = length2
61  return temp_dict, length
62 
63 
64 def get_available_simulators(dictionary):
65  """
66  Checks the dictionary for lists of every simulator to gather information
67  about which simulator is available at which network size
68 
69  dictionary -- config file to search for dictionaries
70  """
71  avail_list = []
72  global_length = 0
73  for simulator in simulators:
74  try:
75  a, length = get_key_arrays(dictionary[simulator])
76  if length > global_length:
77  global_length = length
78  avail_list.append(length)
79  except:
80  avail_list.append(0)
81 
82  if global_length == 0: # Not a scalable benchmark
83  avail_list = [1 for i in avail_list]
84 
85  # Check for invalid flag
86  for i, simulator in enumerate(simulators):
87  if "invalid" in dictionary[simulator]:
88  if dictionary[simulator]["invalid"]:
89  avail_list[i] = 0
90 
91  return avail_list
92 
93 
94 global_json = [] # This will be written to file
95 list_files = []
96 for root, dirs, files in os.walk(os.path.abspath("../source/SNABs/")):
97  for file in files:
98  list_files.append(os.path.join(root, file))
99 
100 for headername in list_files:
101  if not headername.endswith(".hpp"):
102  continue # Check if it is a header
103  try:
104  cppHeader = CppHeaderParser.CppHeader(headername)
105  except CppHeaderParser.CppParseError as e:
106  print(e)
107  sys.exit(1) # Exit with error so that travis will report an issue
108 
109  #print("CppHeaderParser view of %s"%cppHeader)
110 
111  print(headername)
112  for classname in cppHeader.classes:
113  # Every class represents a SNAB/benchmark
114  clazz = cppHeader.classes[classname]
115  if not os.path.exists("../config/" + clazz['name'] + ".json"):
116  continue
117  print('class', clazz['name'])
118  comment = ""
119  if 'doxygen' in clazz:
120  comment = clazz['doxygen']
121  # strip start of comment marker
122  comment = re.sub(r'^/\*+\s*\n', '', comment)
123  # strip end of comment marker
124  comment = re.sub(r'\n\s*\*/', '', comment)
125  # strip leading * at start of each line in comment
126  comment = re.sub(r'^\s*\*\s*', '', comment, 0, re.MULTILINE)
127  # unwrap the text into a single line
128  comment = comment.replace('\n', ' ')
129 
130  # One dict per SNAB
131  snab_dict = {}
132  snab_dict["model"] = {"name": clazz['name'],
133  "description": comment}
134 
135  # Open corresponding config file to generate tasks
136  with open("../config/" + clazz['name'] + ".json") as temp:
137  json_temp = json.load(temp)
138  json_temp2, length = get_key_arrays(json_temp)
139  avail_list = get_available_simulators(json_temp)
140 
141  tasks = [] # Generate task list
142  if length == 0: # Special case, not a scalable benchmark
143  length = 1
144  set_snab = False # Is there any viable benchmark
145  for i in range(0, length):
146  set_task = False # Is there any viable task
147  task_dict = {}
148  task_dict["name"] = bench_index[i]
149  task_dict["command"] = "benchmark.py {system} "
150  task_dict["target"] = []
151  for j in range(0, len(simulators)):
152  if i < avail_list[j]:
153  task_dict["target"].append(web_simulators[j])
154  set_task = True
155  if not set_task:
156  continue
157  task_dict["command"] += clazz['name'] + " " + str(i)
158  task_dict["config"] = {}
159  if i < avail_list[2]:
160  task_dict["config"] [web_simulators[2]]= {}
161  task_dict["config"] [web_simulators[2]]["WAFER_MODULE"] = 33
162  if "wafer" in json_temp[simulators[2]]["setup"]:
163  task_dict["config"] [web_simulators[2]]["WAFER_MODULE"] = json_temp[simulators[2]]["setup"]["wafer"]
164  if not task_dict["config"]:
165  task_dict.pop('config', None)
166  tasks.append(task_dict)
167  set_snab = True
168  if not set_snab:
169  continue
170 
171  snab_dict["tasks"] = tasks
172  global_json.append(snab_dict)
173  print('--------')
174  print('========')
175 
176 # Dump the global list of benchmark description in pretty
177 with open('benchmarks.json', 'w') as file:
178  json.dump(global_json, file, indent=4, sort_keys=True)
def get_available_simulators(dictionary)
def get_key_arrays(dictionary)