-
Notifications
You must be signed in to change notification settings - Fork 0
/
Launcher.py
327 lines (283 loc) · 13.3 KB
/
Launcher.py
1
2
3
4
5
6
7
8
9
10
11
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import os
import sys
import time
import ctypes
import numpy as np
from numpy.ctypeslib import ndpointer
from numpy import linalg as LA
import mandel
import output
from mandel import mandelPy, mediaPy, binarizaPy
def read_options(argv):
options = {
'debug': "debug" in argv,
'binarizar': "bin" in argv,
'diffs': "diffs" in argv,
'times': "times" in argv,
'onlytimes': "onlytimes" in argv,
'mode': 'cuda' if 'tpb' in argv else 'omp',
'cuda': 'tpb' in argv,
'noheader': "noheader" in argv,
'csv': "csv" in argv,
}
params = {
'xmin': float(argv[1]),
'xmax': float(argv[2]),
'ymin': float(argv[3]),
'maxiter': int(argv[4])
}
if os.environ.get('OMP_NUM_THREADS') is not None: options['threads'] = int(os.environ.get('OMP_NUM_THREADS'))
else: options['threads'] = os.cpu_count()
if options['cuda']: # Detección de modo CUDA
try: tpb = int(argv[argv.index("tpb") + 1])
except Exception:
tpb = 32
print("Error al obtener el número de hilos por bloque, se utiliza valor por defecto (32)")
params['tpb'] = tpb
params['ymax'] = params['xmax'] - params['xmin'] + params['ymin']
return options, params
def read_calls(argv, mode):
valid_functions = { # Diccionario de funciones válidas y sus alias en parámetros.
'omp': {
'mandel': {
'normal': 'mandel_normal',
'collapse': 'mandel_collapse',
'tasks': 'mandel_tasks',
'schedule_auto': 'mandel_schedule_auto',
'schedule_static': 'mandel_schedule_static',
'schedule_guided': 'mandel_schedule_guided',
'schedule_dynamic': 'mandel_schedule_dynamic'
},
'promedio': {
'normal': 'promedio_normal',
'int': 'promedio_int',
'schedule': 'promedio_schedule',
'atomic': 'promedio_atomic',
'critical': 'promedio_critical',
'vect': 'promedio_vect'
}
},
'cuda': {
'mandel': {
'normal': 'mandelGPU_normal',
'heter': 'mandelGPU_heter',
'unified': 'mandelGPU_unified',
'pinned': 'mandelGPU_pinned',
'1D': 'mandelGPU_1D'
},
'promedio': {
'api': 'promedioGPU_api',
'shared': 'promedioGPU_shared',
'param': 'promedioGPU_param',
'atomic': 'promedioGPU_atomic',
}
}
}
valid_calls = {
'prof': {
'function': 'mandelProf',
'name': 'fractalProf',
'average': 'mediaProf',
'binary': 'binarizaProf'
},
'py': {
'function': 'mandelPy',
'name': 'fractalPy',
'average': 'mediaPy',
'binary': 'binarizaPy'
},
'own': {}
}
calls = []
sizes = []
for key in list(valid_calls.keys()):
if key in argv:
if key == 'own':
averages = [next(iter(valid_functions[mode]['promedio'].values()))]
if "averages" in sys.argv:
if "all" == sys.argv[sys.argv.index("averages") + 1]:
averages = list(valid_functions[mode]['promedio'].values())
else:
averages = []
for i in range(sys.argv.index("averages") + 1, len(sys.argv)):
if sys.argv[i] in valid_functions[mode]['promedio']:
averages.append(valid_functions[mode]['promedio'][sys.argv[i]])
else: break
if "methods" in sys.argv:
if "all" == sys.argv[sys.argv.index("methods") + 1]:
for key, value in valid_functions[mode]['mandel'].items():
for average in averages:
calls.append({
'function': value,
'name': f'fractalAlumnx{key.capitalize()}',
'average': average,
'binary': 'binarizaAlumnx'
})
else:
for i in range(sys.argv.index("methods") + 1, len(sys.argv)):
if sys.argv[i] in valid_functions[mode]['mandel']:
for average in averages:
calls.append({
'function': valid_functions[mode]['mandel'][sys.argv[i]],
'name': f'fractalAlumnx{sys.argv[i].capitalize()}',
'average': average,
'binary': 'binarizaAlumnx'
})
else: break
else:
for average in averages:
calls.append({
'function': next(iter(valid_functions[mode]['mandel'].values())),
'name': 'fractalAlumnx',
'average': average,
'binary': 'binarizaAlumnx'
})
else: calls.append(valid_calls.get(key))
elif f"-{key}" in sys.argv and key in valid_calls:
calls.remove(valid_calls.get(key))
if "sizes" in sys.argv:
for i in range(sys.argv.index("sizes") + 1, len(sys.argv)):
try: sizes.append(int(sys.argv[i]))
except Exception: break
if len(sizes) == 0: sizes.append(4) # marcar error si no se detectan tamaños
return calls, sizes
def load_libraries(calls, cuda):
functions = {
'mandel': {
'name': 'mandel',
'restype': None,
'argtypes': [ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_int, ctypes.c_int, ctypes.c_int, ndpointer(ctypes.c_double, flags="C_CONTIGUOUS")]
},
'media': {
'name': 'promedio',
'restype': ctypes.c_double,
'argtypes': [ctypes.c_int, ctypes.c_int, ndpointer(ctypes.c_double, flags="C_CONTIGUOUS")]
},
'binariza': {
'name': 'binariza',
'restype': None,
'argtypes': [ctypes.c_int, ctypes.c_int, ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), ctypes.c_double]
}
}
owners = ['Prof', 'Alumnx']
for owner in owners:
lib = ctypes.cdll.LoadLibrary(f"./{'cuda' if cuda else 'openmp'}/mandel{owner}{'GPU' if cuda else ''}.so")
for key, value in functions.items():
if owner == 'Alumnx' and (key == 'mandel' or key == 'media'): continue
globals()[f"{key}{owner}"] = getattr(lib, f"{value['name']}{'GPU' if cuda else ''}")
globals()[f"{key}{owner}"].restype = value['restype']
globals()[f"{key}{owner}"].argtypes = value['argtypes']
if owner == "Alumnx":
for call in calls:
if "Prof" in call['function'] or "Py" in call['function']: continue
globals()[f"{call['function']}"] = getattr(lib, call['function'])
globals()[f"{call['function']}"].restype = functions['mandel']['restype']
globals()[f"{call['function']}"].argtypes = functions['mandel']['argtypes']
globals()[f"{call['average']}"] = getattr(lib, call['average'])
globals()[f"{call['average']}"].restype = functions['media']['restype']
globals()[f"{call['average']}"].argtypes = functions['media']['argtypes']
def execute(calls, sizes, options, params):
times = options['times']
onlytimes = options['onlytimes']
cuda = options['cuda']
binarizar = options['binarizar']
mode = options['mode']
debug = options['debug']
diffs = options['diffs']
csv = options['csv']
if cuda: tpb = params['tpb']
xmin = params['xmin']
ymin = params['ymin']
xmax = params['xmax']
ymax = params['ymax']
maxiter = params['maxiter']
objectives = output.print_header(calls, sizes, options, params) if not csv else []
prev = {}
if options['cuda']: # heat up cache
for i in range(0, 3):
size = next(iter(sizes))
for call in calls:
locals()[call['name']] = np.zeros(size * size).astype(np.double)
globals()[call['function']](xmin, ymin, xmax, ymax, maxiter, size, size, locals()[call['name']], tpb)
globals()[call['average']](size, size, locals()[call['name']], tpb)
for size in sizes:
yres = size
xres = size
for call in calls:
function = call['function']
name = call['name']
average_function = call['average']
binary_function = call['binary']
original = calls[0]['name']
check_cuda = cuda and "Py" not in function
# Como indicado en clase, tamaños superiores a 2048 suponen un calculo
# demasiado largo y no son útiles para la práctica.
# Para poder enviar todos los tamaños en una sola ejecución, se comprueba
# el tamaño aquí.
if "Py" in function and size > 2048: continue
locals()[name] = np.zeros(yres * xres).astype(np.double) # reservar memoria
results = {
'Function': output.alias(function),
'Size': size,
'Time': '...',
'Error': '...'
}
if not onlytimes and not csv:
results['Average Function'] = output.alias(average_function)
results['Average'] = '...'
if times: results['Average Time'] = '...'
if binarizar:
results['Binary Function'] = output.alias(binary_function)
results['Binary Error'] = '...'
if times: results['Binary Time'] = '...'
if times: results['Total Time'] = '...'
output.print_execution(objectives, results, options, prev, True)
# ejecutar función
calc_time = time.time()
if check_cuda: globals()[function](xmin, ymin, xmax, ymax, maxiter, xres, yres, locals()[name], tpb)
else: globals()[function](xmin, ymin, xmax, ymax, maxiter, xres, yres, locals()[name])
calc_time = time.time() - calc_time
results['Time'] = calc_time
# calcular error
try: error = "-" if original == name else '{:.2f}%'.format(sum(abs(locals()[name] - locals()[original])) / (xres * yres)) # calcular error
except Exception: error = "NaN"
results['Error'] = error
if not onlytimes: # calcular promedio
results['Average Function'] = output.alias(average_function)
average_time = time.time()
if check_cuda: average = globals()[average_function](xres, yres, locals()[name], tpb)
else: average = globals()[average_function](xres, yres, locals()[name]) # calcular promedio
average_time = time.time() - average_time
results['Average'] = average
if times: results['Average Time'] = average_time
# guardar imágenes
if debug:
mandel.grabar(locals()[name], xres, yres, f"{name}_{size}.bmp") # guardar archivo
if diffs and i > 0: mandel.grabar(mandel.diffImage(locals()[name], globals()[original]), xres, yres, f"diff_{name}_{size}.bmp")
# binarizar
if binarizar and not onlytimes:
binary_name = f"bin_{name}"
binary_original = f"bin_{original}"
globals()[binary_name] = np.copy(locals()[name]) # copiar imagen para evitar sobreescribirla
# calcular binarización
binary_time = time.time()
if check_cuda: globals()[binary_function](xres, yres, globals()[binary_name], average, tpb)
else: globals()[binary_function](yres, xres, globals()[binary_name], average)
binary_time = time.time() - binary_time
# calcular e imprimir error
error = "-" if binary_name == binary_original else LA.norm(globals()[binary_name] - globals()[binary_original])
results['Binary Error'] = error
if times: results['Binary Time'] = binary_time
# guardar binarizado
if debug: mandel.grabar(globals()[binary_name], xres, yres, f"{binary_name}_{size}.bmp")
if times and not onlytimes:
total = calc_time + average_time
if binarizar: total += binary_time
results['Total Time'] = total
prev = output.print_execution(objectives, results, options, prev, False)
if __name__ == '__main__':
options, params = read_options(sys.argv) # Leer parámetros de entrada
os.system(f"make {options['mode']} >/dev/null") # Compilar librerías necesarias
calls, sizes = read_calls(sys.argv, options['mode']) # Leer funciones y tamaños a ejecutar
load_libraries(calls, options['cuda']) # Cargar librerías mediante ctypes
execute(calls, sizes, options, params) # Ejecutar funciones y mostrar resultados