COMPASS  5.0.0
End-to-end AO simulation tool using GPU acceleration
test_cusolver.py
1 import numpy as np
2 import carmaWrap as ch
3 import numpy.testing as npt
4 import time
5 
6 m = 1024
7 n = 1024
8 min_mn = min(m, n)
9 
10 dec = 3
11 prec = 10**(-dec)
12 
13 c = ch.context.get_instance()
14 
15 print("precision: ", prec)
16 
17 # def test_float_svd():
18 
19 # mat = np.random.rand(m, n).astype(np.float32)
20 
21 # h_mat = ch.host_obj_float(mat, ch.MA_PAGELOCK)
22 # h_eig = ch.host_obj_float(np.random.randn([min_mn]), ch.MA_PAGELOCK)
23 # h_U = ch.host_obj_float(np.random.randn((m, m)), ch.MA_PAGELOCK)
24 # h_VT = ch.host_obj_float(np.random.randn((n, n)), ch.MA_PAGELOCK)
25 
26 # npt.assert_array_equal(mat, np.array(h_mat))
27 
28 # ch.svd_cpu_float(h_mat, h_eig, h_U, h_VT)
29 
30 # # expected: U.S.V=mat
31 # # U = np.array(h_U)
32 # # S = np.random.randn((m, n))
33 # # S = np.diag(h_eig)
34 # # VT = np.array(h_VT)
35 
36 # # res = np.dot(U, S)
37 # # res = np.dot(res, VT.T)
38 
39 # # iErr = np.argmax(np.abs(res - mat))
40 # # err = np.abs(np.abs(res.item(iErr) - mat.item(iErr)))
41 # # if (mat.item(iErr) != 0):
42 # # err = err / mat.item(iErr)
43 
44 # # npt.assert_almost_equal(err, 0., decimal=dec)
45 # # print("")
46 # # print(err)
47 
48 # p_U, p_S, p_V = np.linalg.svd(mat)
49 # npt.assert_array_almost_equal(np.array(h_eig), p_S, decimal=dec)
50 
51 # def test_float_getri_cpu():
52 
53 # a = np.random.rand(m, m).astype(np.float32)
54 # a = np.dot(a, a.T)
55 
56 # a += np.identity(m)
57 
58 # h_mat = ch.host_obj_float(a, ch.MA_PAGELOCK)
59 # h_mat2 = ch.host_obj_float(a, ch.MA_PAGELOCK)
60 
61 # ch.getri_cpu_float(h_mat2)
62 
63 # a_1 = np.array(h_mat2)
64 # #expected: a.a_1=Id
65 # res = np.dot(a, a_1)
66 
67 # err = np.amax(np.abs(res - np.identity(m)))
68 
69 # npt.assert_almost_equal(err, 0., decimal=dec)
70 
71 # def test_float_potri_cpu():
72 
73 # a = np.random.rand(m, m).astype(np.float32)
74 # a = np.dot(a, a.T)
75 # a += np.identity(m)
76 
77 # h_mat = ch.host_obj_float(a, ch.MA_PAGELOCK)
78 # h_mat2 = ch.host_obj_float(a, ch.MA_PAGELOCK)
79 
80 # ch.potri_cpu_float(h_mat2)
81 
82 # a_1 = np.array(h_mat2)
83 # #expected: a.a_1=Id
84 # res = np.dot(a, a_1)
85 
86 # err = np.amax(np.abs(res - np.identity(m)))
87 # print("")
88 # print(err)
89 
90 # npt.assert_almost_equal(err, 0., decimal=dec)
91 
92 # def test_float_getri_gpu():
93 
94 # d_mat = ch.obj_float(c, np.random.randn([m, m]))
95 # d_mat.random(np.int32(time.perf_counter() * 1e3))
96 # a = np.array(d_mat)
97 # a = a.T
98 # a.reshape(a.T.shape)
99 
100 # identity = ch.obj_float(c, np.identity(m))
101 
102 # d_res = d_mat.gemm(d_mat, 'n', 't', 1, identity, 1)
103 
104 # b = np.dot(a, a.T)
105 # mat = np.array(d_res)
106 
107 # d_mat = ch.obj_float(c, d_res)
108 # ch.getri_float(d_res)
109 
110 # d_id = d_mat.gemm(d_res)
111 # res_id = np.array(d_id)
112 
113 # err = np.amax(np.abs(res_id - np.identity(m)))
114 
115 # npt.assert_almost_equal(err, 0., decimal=dec)
116 
117 # def test_float_potri_gpu():
118 
119 # d_mat = ch.obj_float(c, np.random.randn([m, m))
120 # d_mat.random(np.int32(time.perf_counter() * 1e3))
121 # a = np.array(d_mat)
122 # a = a.T
123 # a.reshape(a.T.shape)
124 
125 # identity = ch.obj_float(c, np.identity(m))
126 
127 # d_res = d_mat.gemm(d_mat, op_a='n', op_b='t', beta=1, matC=identity)
128 
129 # b = np.dot(a, a.T)
130 # mat = np.array(d_res)
131 
132 # d_mat = ch.obj_float(c, d_res)
133 # ch.potri_float(d_res)
134 
135 # d_id = d_mat.gemm(d_res)
136 # res_id = np.array(d_id)
137 
138 # err = np.amax(np.abs(res_id - np.identity(m)))
139 # print("")
140 # print(err)
141 
142 # npt.assert_almost_equal(err, 0, decimal=dec)
143 
144 
146 
147  d_mat = ch.obj_float(c, np.random.randn(m, m))
148  d_U = ch.obj_float(c, np.random.randn(m, m))
149  d_EV = ch.obj_float(c, np.random.randn(m))
150  d_EV2 = ch.obj_float(c, np.random.randn(m))
151 
152  d_res = d_mat.gemm(d_mat, op_a='n', op_b='t')
153 
154  ch.syevd_float(d_res, d_EV, d_U)
155 
156  U = np.array(d_U)
157  Mat = np.array(d_res)
158  EV = np.diag(d_EV)
159 
160  npt.assert_almost_equal(np.dot(np.dot(U, EV), U.T), Mat, decimal=dec - 1)
161 
162  err = np.amax(np.abs(Mat - np.dot(np.dot(U, EV), U.T)))
163 
164  print("")
165 
166  print("out of place, compute U")
167  print(err)
168 
169  npt.assert_almost_equal(err, 0., decimal=dec)
170 
171  d_res = d_mat.gemm(d_mat, op_a='n', op_b='t')
172  ch.syevd_float(d_res, d_EV2, computeU=False)
173 
174  err = np.amax(np.abs(np.array(d_EV) - np.array(d_EV2)))
175 
176  print("in place, U not computed")
177  print(err)
178  npt.assert_almost_equal(np.array(d_EV), np.array(d_EV2), decimal=dec - 2)
179 
180 
182 
183  d_mat = ch.obj_double(c, np.random.randn(m, m))
184  d_U = ch.obj_double(c, np.random.randn(m, m))
185  d_EV = ch.obj_double(c, np.random.randn(m))
186  d_EV2 = ch.obj_double(c, np.random.randn(m))
187 
188  d_res = d_mat.gemm(d_mat, op_a='n', op_b='t')
189 
190  ch.syevd_double(d_res, d_EV, d_U)
191 
192  U = np.array(d_U)
193  Mat = np.array(d_res)
194  EV = np.diag(d_EV)
195 
196  npt.assert_almost_equal(np.dot(np.dot(U, EV), U.T), Mat, decimal=2 * dec - 1)
197 
198  err = np.amax(np.abs(Mat - np.dot(np.dot(U, EV), U.T)))
199 
200  print("")
201 
202  print("out of place, compute U")
203  print(err)
204 
205  npt.assert_almost_equal(err, 0., decimal=dec)
206 
207  d_res = d_mat.gemm(d_mat, op_a='n', op_b='t')
208  ch.syevd_double(d_res, d_EV2, computeU=False)
209 
210  err = np.amax(np.abs(np.array(d_EV) - np.array(d_EV2)))
211 
212  print("in place, U not computed")
213  print(err)
214  npt.assert_almost_equal(np.array(d_EV), np.array(d_EV2), decimal=2 * dec - 2)
test_cusolver.test_double_syevd
def test_double_syevd()
Definition: test_cusolver.py:181
test_cusolver.test_float_syevd
def test_float_syevd()
Definition: test_cusolver.py:145