|
| 1 | + |
| 2 | +import numpy as np |
| 3 | +from tinygpu.gpu import TinyGPU |
| 4 | + |
| 5 | +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,) |
| 14 | + |
| 15 | +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) |
0 commit comments