-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_dot_prod.py
More file actions
67 lines (55 loc) · 1.65 KB
/
Copy pathmain_dot_prod.py
File metadata and controls
67 lines (55 loc) · 1.65 KB
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
# main.py — Punto de entrada y demo del GPU simulado
#
# TAREAS:
# 1. Configurar el GPU (num_sms, cores_per_sm, etc.)
# 2. Crear arrays de prueba A y B de tamaño N
# 3. Cargarlos en memoria global
# 4. Ejecutar el kernel vector_add con grid_dim y block_dim apropiados
# 5. Verificar que C[i] == A[i] + B[i] para todo i
# 6. Imprimir estadisticas de la simulacion
#
# EJEMPLO DE USO ESPERADO (cuando todo este implementado):
#
# gpu = GPU(num_sms=4, cores_per_sm=32)
# runner = SimRunner(gpu)
#
# N = 128
# A = list(range(N))
# B = list(range(N, 2*N))
#
# runner.load_data(A, B, N)
#
# with open("kernels/vector_add.asm") as f:
# program = f.read()
#
# stats = runner.run_kernel(program, grid_dim=4, block_dim=32)
# runner.print_stats(stats)
#
# assert stats["result"] == [A[i] + B[i] for i in range(N)], "ERROR"
# print("Resultado correcto!")
from random import randint
from sim.runner import SimRunner
# Resultado esperado calculado en CPU (ground truth)
N = 128
A = [randint(0, N) for _ in range(N)]
B = [randint(0, N) for _ in range(N)]
mul = [a * b for a, b in zip(A, B)] # [11, 22, 33, 44]
expected = sum(i for i in mul)
sim = SimRunner()
program = open('kernels/dot_product.asm')
instructions = program.read()
program.close()
print("-------")
sim.reset()
sim.load_data(A, B, N)
stats = sim.run_kernel(instructions, 4, 32, registers={
"R7": N, "R9": 1}, result_count=1)
sim.print_stats(stats)
print("-------")
# sim.print_stats(stats)
# Resultado del GPU simulado
gpu_result = stats["results"]
print(gpu_result)
# Verificación
assert gpu_result == expected, f"Error: {gpu_result} != {expected}"
print("OK")