def test_NpTypes_ToList(self): np_c = NpTypes() lists_out = np_c.ToList() list_0 = [key for key in self.known_c_types] list_1 = [key for key in self.known_np_types] self.assertTrue( AllTrue([list_0 == lists_out[0], list_1 == lists_out[1]]))
def test_IdpyKernelCU(self): cu = CUDA() cu.SetDevice() tenet = cu.GetTenet() grid, block = ((self.n + self.block_size - 1) // self.block_size, 1, 1), (self.block_size, 1, 1) myTypes = CustomTypes({'SpinType': 'unsigned int'}) np_c = NpTypes() SumOne = self.K_SumOne(custom_types=myTypes.Push(), constants={'DATA_N': self.n}) SumOne_Idea = SumOne(tenet=tenet, grid=grid, block=block) A = IdpyMemory.Const(self.n, dtype=np_c.C[myTypes['SpinType']], const=0, tenet=tenet) print() print("A: ", A.D2H(), A.dtype) print("SumOne_Idea.Deploy([A])") SumOne_Idea.Deploy([A]) print("A: ", A.D2H(), A.dtype) check_array = np.zeros(self.n, dtype=np_c.C[myTypes['SpinType']]) check_array.fill(1) checks = [AllTrue(A.D2H() == check_array)] tenet.End() self.assertTrue(AllTrue(checks))
def test_IdpyKernelLoopConstCL(self): ocl = OpenCL() ocl.SetDevice() tenet = ocl.GetTenet() grid, block = ((self.n + self.block_size - 1) // self.block_size, 1, 1), (self.block_size, 1, 1) myTypes = CustomTypes({'SpinType': 'unsigned int'}) np_c = NpTypes() SumConst = self.K_SumConst(custom_types=myTypes.Push(), constants={'DATA_N': self.n}) A = IdpyMemory.Const(self.n, dtype=np_c.C[myTypes['SpinType']], const=0, tenet=tenet) # https://stackoverflow.com/questions/5710690/pycuda-passing-variable-by-value-to-kernel mem_dict = {'A': A, 'const': np.int32(self.in_const)} SumOne_Loop = IdpyLoop([mem_dict], [[(SumConst( tenet=tenet, grid=grid, block=block), ['A', 'const'])]]) print() print("A: ", A.D2H(), A.dtype) print("SumOne_Loop.Run(range(1))") SumOne_Loop.Run(range(1)) print("A: ", A.D2H(), A.dtype) print("SumOne_Loop.Run(range(8))") SumOne_Loop.Run(range(8)) print("A: ", A.D2H(), A.dtype) check_array = np.zeros(self.n, dtype=np_c.C[myTypes['SpinType']]) check_array.fill(self.in_const * 9) checks = [] checks += [AllTrue(A.D2H() == check_array)] tenet.End() self.assertTrue(AllTrue(checks))
def test_IdpyKernelLoopCU(self): cu = CUDA() cu.SetDevice() tenet = cu.GetTenet() grid, block = ((self.n + self.block_size - 1) // self.block_size, 1, 1), (self.block_size, 1, 1) myTypes = CustomTypes({'SpinType': 'unsigned int'}) np_c = NpTypes() SumOne = self.K_SumOne(custom_types=myTypes.Push(), constants={'DATA_N': self.n}) A = IdpyMemory.Const(self.n, dtype=np_c.C[myTypes['SpinType']], const=0, tenet=tenet) mem_dict = {'A': A} SumOne_Loop = IdpyLoop( [mem_dict], [[(SumOne(tenet=tenet, grid=grid, block=block), ['A'])]]) print() print("A: ", A.D2H(), A.dtype) print("SumOne_Loop.Run(range(1))") SumOne_Loop.Run(range(1)) print("A: ", A.D2H(), A.dtype) print("SumOne_Loop.Run(range(8))") SumOne_Loop.Run(range(8)) print("A: ", A.D2H(), A.dtype) check_array = np.zeros(self.n, dtype=np_c.C[myTypes['SpinType']]) check_array.fill(9) checks = [] checks += [AllTrue(A.D2H() == check_array)] tenet.End() self.assertTrue(AllTrue(checks))
def test_IdpyKernelFuncLoopMultStreamCL(self): ocl = OpenCL() ocl.SetDevice() tenet = ocl.GetTenet() grid, block = ((self.n + self.block_size - 1) // self.block_size, 1, 1), (self.block_size, 1, 1) myTypes = CustomTypes({'SpinType': 'unsigned int'}) np_c = NpTypes() SumTwoArrConst = self.K_SumTwoArrays(custom_types=myTypes.Push(), constants={'DATA_N': self.n}, f_classes=[ self.F_SumTwoArraysPtr, self.F_SumTwoArraysRet, self.F_SumTwoArraysVal ]) SumConst = self.K_SumConst(custom_types=myTypes.Push(), constants={'DATA_N': self.n}) A = IdpyMemory.Const(self.n, dtype=np_c.C[myTypes['SpinType']], const=0, tenet=tenet) B = IdpyMemory.Const(self.n, dtype=np_c.C[myTypes['SpinType']], const=1, tenet=tenet) C = IdpyMemory.Const(self.n, dtype=np_c.C[myTypes['SpinType']], const=2, tenet=tenet) D = IdpyMemory.Const(self.n, dtype=np_c.C[myTypes['SpinType']], const=3, tenet=tenet) print() print("A: ", A.D2H(), A.dtype) print("B: ", B.D2H(), B.dtype) print("C: ", C.D2H(), C.dtype) print("D: ", D.D2H(), D.dtype) ''' Checking result ''' a, b, c, cc = A.D2H()[0], B.D2H()[0], C.D2H()[0], self.in_const d = D.D2H()[0] for i in range(2): '''first stream''' c += cc c += a + b c += a + b c += a + b + cc a, c = c, a '''second stream''' d += cc # https://stackoverflow.com/questions/5710690/pycuda-passing-variable-by-value-to-kernel ## mem_dict_0 = { 'A': A, 'B': B, 'C': C, 'const': np_c.C[myTypes['SpinType']](self.in_const) } mem_dict_1 = { 'D': D, 'const': np_c.C[myTypes['SpinType']](self.in_const) } SumTwoArrConst_Loop = IdpyLoop( [mem_dict_0, mem_dict_1], [[(SumTwoArrConst(tenet=tenet, grid=grid, block=block), ['A', 'B', 'C', 'const']), (self.M_SwapArrays(tenet), ['A', 'C'])], [ (SumConst(tenet=tenet, grid=grid, block=block), ['D', 'const']), ]]) print() print("SumTwoArrConst_Loop.Run(range(2))") SumTwoArrConst_Loop.Run(range(2)) print("A: ", A.D2H(), A.dtype, a) print("B: ", B.D2H(), B.dtype, b) print("C: ", C.D2H(), C.dtype, c) print("D: ", D.D2H(), D.dtype, d) checks = [] check_array = np.full(self.n, a, dtype=np_c.C[myTypes['SpinType']]) checks += [AllTrue(A.D2H() == check_array)] check_array = np.full(self.n, b, dtype=np_c.C[myTypes['SpinType']]) checks += [AllTrue(B.D2H() == check_array)] check_array = np.full(self.n, c, dtype=np_c.C[myTypes['SpinType']]) checks += [AllTrue(C.D2H() == check_array)] check_array = np.full(self.n, d, dtype=np_c.C[myTypes['SpinType']]) checks += [AllTrue(D.D2H() == check_array)] tenet.End() self.assertTrue(AllTrue(checks))