3 import numpy.testing
as npt
13 c = ch.context.get_instance()
15 print(
"precision: ", prec)
27 Mat = ch.obj_float(c, np.random.randn(m, n))
28 MatT = ch.obj_float(c, np.random.randn(n, m))
32 Vectx = ch.obj_float(c, np.random.randn(n))
33 Vecty = ch.obj_float(c, np.random.randn(m))
42 y = alpha * A.dot(x) + beta * y
44 y3 = alpha * AT.T.dot(x)
48 Mat.magma_gemv(Vectx, alpha,
'N', Vecty, beta)
49 Vecty_2 = Mat.magma_gemv(Vectx, alpha,
'N')
50 Vecty_3 = MatT.magma_gemv(Vectx, alpha,
'T')
52 npt.assert_array_almost_equal(x, np.array(Vectx), decimal=dec - 1)
53 npt.assert_array_almost_equal(y, np.array(Vecty), decimal=dec - 1)
54 npt.assert_array_almost_equal(y2, np.array(Vecty_2), decimal=dec - 1)
55 npt.assert_array_almost_equal(y3, np.array(Vecty_3), decimal=dec - 1)
67 Mat = ch.obj_double(c, np.random.randn(m, n))
68 MatT = ch.obj_double(c, np.random.randn(n, m))
72 Vectx = ch.obj_double(c, np.random.randn(n))
73 Vecty = ch.obj_double(c, np.random.randn(m))
82 y = alpha * A.dot(x) + beta * y
84 y3 = alpha * AT.T.dot(x)
88 Mat.magma_gemv(Vectx, alpha,
'N', Vecty, beta)
89 Vecty_2 = Mat.magma_gemv(Vectx, alpha,
'N')
90 Vecty_3 = MatT.magma_gemv(Vectx, alpha,
'T')
92 npt.assert_array_almost_equal(x, np.array(Vectx), decimal=dec - 1)
93 npt.assert_array_almost_equal(y, np.array(Vecty), decimal=dec - 1)
94 npt.assert_array_almost_equal(y2, np.array(Vecty_2), decimal=dec - 1)
95 npt.assert_array_almost_equal(y3, np.array(Vecty_3), decimal=dec - 1)
100 mat = np.random.rand(m, n).astype(np.float32)
102 h_mat = ch.host_obj_float(mat, ch.MA_PAGELOCK)
103 h_eig = ch.host_obj_float(np.random.randn(min_mn), ch.MA_PAGELOCK)
104 h_U = ch.host_obj_float(np.random.randn(m, m), ch.MA_PAGELOCK)
105 h_VT = ch.host_obj_float(np.random.randn(n, n), ch.MA_PAGELOCK)
107 npt.assert_array_equal(mat, np.array(h_mat))
109 ch.magma_svd_cpu_float(h_mat, h_eig, h_U, h_VT)
129 p_U, p_S, p_V = np.linalg.svd(mat)
130 npt.assert_array_almost_equal(np.array(h_eig), p_S, decimal=dec)
135 a = np.random.rand(m, m).astype(np.float32)
138 a += np.identity(m, dtype=np.float32)
140 h_mat = ch.host_obj_float(a, ch.MA_PAGELOCK)
141 h_mat2 = ch.host_obj_float(a, ch.MA_PAGELOCK)
143 ch.magma_getri_cpu_float(h_mat2)
145 a_1 = np.array(h_mat2)
149 err = np.amax(np.abs(res - np.identity(m)))
151 npt.assert_almost_equal(err, 0., decimal=dec)
156 a = np.random.rand(m, m).astype(np.float32)
158 a += np.identity(m, dtype=np.float32)
160 h_mat = ch.host_obj_float(a, ch.MA_PAGELOCK)
161 h_mat2 = ch.host_obj_float(a, ch.MA_PAGELOCK)
163 ch.magma_potri_cpu_float(h_mat2)
165 a_1 = np.array(h_mat2)
169 err = np.amax(np.abs(res - np.identity(m)))
173 npt.assert_almost_equal(err, 0., decimal=dec)
178 d_mat = ch.obj_float(c, np.random.randn(m, m))
179 d_mat.random(np.int32(time.perf_counter() * 1e3))
184 identity = ch.obj_float(c, np.identity(m))
186 d_res = d_mat.gemm(d_mat,
'n',
't', 1, identity, 1)
189 mat = np.array(d_res)
191 d_mat = ch.obj_float(c, d_res)
192 ch.magma_getri_float(d_res)
194 d_id = d_mat.gemm(d_res)
195 res_id = np.array(d_id)
197 err = np.amax(np.abs(res_id - np.identity(m)))
199 npt.assert_almost_equal(err, 0., decimal=dec)
204 d_mat = ch.obj_float(c, np.random.randn(m, m))
205 d_mat.random(np.int32(time.perf_counter() * 1e3))
210 identity = ch.obj_float(c, np.identity(m))
212 d_res = d_mat.gemm(d_mat, op_a=
'n', op_b=
't', beta=1, matC=identity)
215 mat = np.array(d_res)
217 d_mat = ch.obj_float(c, d_res)
218 ch.magma_potri_float(d_res)
220 d_id = d_mat.gemm(d_res)
221 res_id = np.array(d_id)
223 err = np.amax(np.abs(res_id - np.identity(m)))
227 npt.assert_almost_equal(err, 0, decimal=dec)
232 d_mat = ch.obj_float(c, np.random.randn(m, m))
233 d_U = ch.obj_float(c, np.random.randn(m, m))
234 h_EV = ch.host_obj_float(np.random.randn(m))
235 h_EV2 = ch.host_obj_float(np.random.randn(m))
237 d_res = d_mat.gemm(d_mat, op_a=
'n', op_b=
't')
239 ch.magma_syevd_float(d_res, h_EV, d_U)
242 Mat = np.array(d_res)
245 npt.assert_almost_equal(
246 np.dot(np.dot(U, EV), U.T).astype(np.float32), Mat, decimal=dec - 1)
248 err = np.amax(np.abs(Mat - np.dot(np.dot(U, EV), U.T)))
252 print(
"out of place, compute U")
255 npt.assert_almost_equal(err, 0., decimal=dec - 1)
257 d_res = d_mat.gemm(d_mat, op_a=
'n', op_b=
't')
258 ch.magma_syevd_float(d_res, h_EV2, computeU=
False)
260 err = np.amax(np.abs(np.array(h_EV) - np.array(h_EV2)))
262 print(
"in place, U not computed")
264 npt.assert_almost_equal(np.array(h_EV), np.array(h_EV2), decimal=dec - 2)
269 mat = np.random.rand(m, n).astype(np.float32)
271 h_mat = ch.host_obj_double(mat, ch.MA_PAGELOCK)
272 h_eig = ch.host_obj_double(np.random.randn(min_mn), ch.MA_PAGELOCK)
273 h_U = ch.host_obj_double(np.random.randn(m, m), ch.MA_PAGELOCK)
274 h_VT = ch.host_obj_double(np.random.randn(n, n), ch.MA_PAGELOCK)
276 npt.assert_array_equal(mat, np.array(h_mat))
278 ch.magma_svd_cpu_double(h_mat, h_eig, h_U, h_VT)
298 p_U, p_S, p_V = np.linalg.svd(mat)
299 npt.assert_array_almost_equal(np.array(h_eig), p_S, decimal=2 * dec - 1)
304 a = np.random.rand(m, m).astype(np.float32)
307 a += np.identity(m, dtype=np.float32)
309 h_mat = ch.host_obj_double(a, ch.MA_PAGELOCK)
310 h_mat2 = ch.host_obj_double(a, ch.MA_PAGELOCK)
312 ch.magma_getri_cpu_double(h_mat2)
314 a_1 = np.array(h_mat2)
318 err = np.amax(np.abs(res - np.identity(m)))
320 npt.assert_almost_equal(err, 0., decimal=2 * dec)
325 a = np.random.rand(m, m).astype(np.float32)
327 a += np.identity(m, dtype=np.float32)
329 h_mat = ch.host_obj_double(a, ch.MA_PAGELOCK)
330 h_mat2 = ch.host_obj_double(a, ch.MA_PAGELOCK)
332 ch.magma_potri_cpu_double(h_mat2)
334 a_1 = np.array(h_mat2)
338 err = np.amax(np.abs(res - np.identity(m)))
342 npt.assert_almost_equal(err, 0., decimal=2 * dec)
347 d_mat = ch.obj_double(c, np.random.randn(m, m))
348 d_mat.random(np.int32(time.perf_counter() * 1e3))
353 identity = ch.obj_double(c, np.identity(m))
355 d_res = d_mat.gemm(d_mat,
'n',
't', 1, identity, 1)
358 mat = np.array(d_res)
360 d_mat = ch.obj_double(c, d_res)
361 ch.magma_getri_double(d_res)
363 d_id = d_mat.gemm(d_res)
364 res_id = np.array(d_id)
366 err = np.amax(np.abs(res_id - np.identity(m)))
368 npt.assert_almost_equal(err, 0., decimal=2 * dec)
373 d_mat = ch.obj_double(c, np.random.randn(m, m))
374 d_mat.random(np.int32(time.perf_counter() * 1e3))
379 identity = ch.obj_double(c, np.identity(m))
381 d_res = d_mat.gemm(d_mat, op_a=
'n', op_b=
't', beta=1, matC=identity)
384 mat = np.array(d_res)
386 d_mat = ch.obj_double(c, d_res)
387 ch.magma_potri_double(d_res)
389 d_id = d_mat.gemm(d_res)
390 res_id = np.array(d_id)
392 err = np.amax(np.abs(res_id - np.identity(m)))
396 npt.assert_almost_equal(err, 0, decimal=2 * dec)
401 d_mat = ch.obj_double(c, np.random.randn(m, m))
402 d_U = ch.obj_double(c, np.random.randn(m, m))
403 h_EV = ch.host_obj_double(np.random.randn(m))
404 h_EV2 = ch.host_obj_double(np.random.randn(m))
406 d_res = d_mat.gemm(d_mat, op_a=
'n', op_b=
't')
408 ch.magma_syevd_double(d_res, h_EV, d_U)
411 Mat = np.array(d_res)
414 npt.assert_almost_equal(
415 np.dot(np.dot(U, EV), U.T).astype(np.float32), Mat, decimal=2 * dec - 2)
417 err = np.amax(np.abs(Mat - np.dot(np.dot(U, EV), U.T)))
421 print(
"out of place, compute U")
424 npt.assert_almost_equal(err, 0., decimal=2 * dec - 1)
426 d_res = d_mat.gemm(d_mat, op_a=
'n', op_b=
't')
427 ch.magma_syevd_double(d_res, h_EV2, computeU=
False)
429 err = np.amax(np.abs(np.array(h_EV) - np.array(h_EV2)))
431 print(
"in place, U not computed")
433 npt.assert_almost_equal(np.array(h_EV), np.array(h_EV2), decimal=2 * dec - 2)