|
2 | 2 | import numpy as np |
3 | 3 | from tinygpu.gpu import TinyGPU |
4 | 4 |
|
| 5 | + |
5 | 6 | def test_tinygpu_init(): |
6 | | - gpu = TinyGPU(num_threads=4, num_registers=8, mem_size=16) |
7 | | - assert gpu.num_threads == 4 |
8 | | - assert gpu.num_registers == 8 |
9 | | - assert gpu.mem_size == 16 |
10 | | - assert gpu.registers.shape == (4, 8) |
11 | | - assert gpu.memory.shape == (16,) |
12 | | - assert gpu.pc.shape == (4,) |
13 | | - assert gpu.active.shape == (4,) |
| 7 | + gpu = TinyGPU(num_threads=4, num_registers=8, mem_size=16) |
| 8 | + assert gpu.num_threads == 4 |
| 9 | + assert gpu.num_registers == 8 |
| 10 | + assert gpu.mem_size == 16 |
| 11 | + assert gpu.registers.shape == (4, 8) |
| 12 | + assert gpu.memory.shape == (16,) |
| 13 | + assert gpu.pc.shape == (4,) |
| 14 | + assert gpu.active.shape == (4,) |
| 15 | + |
14 | 16 |
|
15 | 17 | def test_tinygpu_step_and_run(): |
16 | | - gpu = TinyGPU(num_threads=2, num_registers=4, mem_size=8) |
17 | | - # Fake program: increment R0, halt after 2 steps |
18 | | - gpu.program = [ |
19 | | - ("LOAD", [("R", 0), 1]), |
20 | | - ("ADD", [("R", 0), ("R", 0), 1]), |
21 | | - ] |
22 | | - gpu.pc[:] = 0 |
23 | | - gpu.active[:] = True |
24 | | - gpu.sync_waiting[:] = False |
25 | | - gpu.history_registers = [] |
26 | | - gpu.history_memory = [] |
27 | | - gpu.history_pc = [] |
28 | | - # Patch INSTRUCTIONS for test |
29 | | - from tinygpu import instructions |
30 | | - def fake_load(self, tid, reg, val): |
31 | | - self.registers[tid, reg[1]] = val |
32 | | - def fake_add(self, tid, reg, reg2, val): |
33 | | - self.registers[tid, reg[1]] = self.registers[tid, reg2[1]] + val |
34 | | - instructions.INSTRUCTIONS["LOAD"] = fake_load |
35 | | - instructions.INSTRUCTIONS["ADD"] = fake_add |
36 | | - # After first step, both instructions are executed for both threads |
37 | | - gpu.step() |
38 | | - # LOAD sets R0=1, then ADD sets R0=2 in the same step |
39 | | - assert np.all(gpu.registers[:,0] == 2) |
| 18 | + gpu = TinyGPU(num_threads=2, num_registers=4, mem_size=8) |
| 19 | + # Fake program: increment R0, halt after 2 steps |
| 20 | + gpu.program = [ |
| 21 | + ("LOAD", [("R", 0), 1]), |
| 22 | + ("ADD", [("R", 0), ("R", 0), 1]), |
| 23 | + ] |
| 24 | + gpu.pc[:] = 0 |
| 25 | + gpu.active[:] = True |
| 26 | + gpu.sync_waiting[:] = False |
| 27 | + gpu.history_registers = [] |
| 28 | + gpu.history_memory = [] |
| 29 | + gpu.history_pc = [] |
| 30 | + # Patch INSTRUCTIONS for test |
| 31 | + from tinygpu import instructions |
| 32 | + |
| 33 | + def fake_load(self, tid, reg, val): |
| 34 | + self.registers[tid, reg[1]] = val |
| 35 | + |
| 36 | + def fake_add(self, tid, reg, reg2, val): |
| 37 | + self.registers[tid, reg[1]] = self.registers[tid, reg2[1]] + val |
| 38 | + |
| 39 | + instructions.INSTRUCTIONS["LOAD"] = fake_load |
| 40 | + instructions.INSTRUCTIONS["ADD"] = fake_add |
| 41 | + # After first step, both instructions are executed for both threads |
| 42 | + gpu.step() |
| 43 | + # LOAD sets R0=1, then ADD sets R0=2 in the same step |
| 44 | + assert np.all(gpu.registers[:, 0] == 2) |
0 commit comments