COMPASS  5.0.0
End-to-end AO simulation tool using GPU acceleration
test_cublas3.py
1 import numpy as np
2 import carmaWrap as ch
3 import numpy.testing as npt
4 import time
5 
6 dec = 4
7 prec = 10**-dec
8 
9 sizem = 128
10 sizen = 256
11 sizek = 512
12 
13 seed = np.int32(time.perf_counter() * 1e3)
14 
15 print("")
16 print("Test cublas 3")
17 print("precision: ", prec)
18 
19 c = ch.context.get_instance()
20 
21 
23 
24  # function gemm
25  #testing: C=A.B+C
26  #A,B,C matrices
27 
28  #generating random matrices A,B,C and associated CarmaObj
29 
30  # np.random.seed(seed)
31  # A = A.dot(A.T)
32  # B = B.dot(B.T)
33 
34  matA = ch.obj_float(c, np.random.randn(sizem, sizek))
35  matAT = ch.obj_float(c, np.random.randn(sizek, sizem))
36  matB = ch.obj_float(c, np.random.randn(sizek, sizen))
37  matBT = ch.obj_float(c, np.random.randn(sizen, sizek))
38  matC = ch.obj_float(c, np.random.randn(sizem, sizen))
39  matC2 = ch.obj_float(c, np.random.randn(sizem, sizen))
40  matC3 = ch.obj_float(c, np.random.randn(sizem, sizen))
41 
42  # matA.random_host(seed, 'U')
43  # matAT.random_host(seed, 'U')
44  # matB.random_host(seed + 2, 'U')
45  # matBT.random_host(seed + 2, 'U')
46  # matC.random_host(seed + 3, 'U')
47  # matC2.random_host(seed + 3, 'U')
48  # matC3.random_host(seed + 3, 'U')
49 
50  A = np.array(matA)
51  AT = np.array(matAT)
52  B = np.array(matB)
53  BT = np.array(matBT)
54  C = np.array(matC)
55  C2 = np.array(matC2)
56  C3 = np.array(matC3)
57 
58  alpha = 2
59  beta = 1
60 
61  #matrices product
62  matA.gemm(matB, 'n', 'n', alpha, matC, beta)
63  matAT.gemm(matB, 't', 'n', alpha, matC2, beta)
64  matAT.gemm(matBT, 't', 't', alpha, matC3, beta)
65  matC4 = matA.gemm(matB, 'n', 'n', alpha)
66  matC5 = matAT.gemm(matB, 't', 'n', alpha)
67  matC6 = matAT.gemm(matBT, 't', 't', alpha)
68 
69  C = alpha * A.dot(B) + beta * C
70  C2 = alpha * AT.T.dot(B) + beta * C2
71  C3 = alpha * AT.T.dot(BT.T) + beta * C3
72  C4 = alpha * A.dot(B)
73  C5 = alpha * AT.T.dot(B)
74  C6 = alpha * AT.T.dot(BT.T)
75 
76  npt.assert_array_almost_equal(C, np.array(matC), decimal=dec - 1)
77  npt.assert_array_almost_equal(C2, np.array(matC2), decimal=dec - 1)
78  npt.assert_array_almost_equal(C3, np.array(matC3), decimal=dec - 1)
79  npt.assert_array_almost_equal(C4, np.array(matC4), decimal=dec - 1)
80  npt.assert_array_almost_equal(C5, np.array(matC5), decimal=dec - 1)
81  npt.assert_array_almost_equal(C6, np.array(matC6), decimal=dec - 1)
82 
83 
85  #function symm
86  #testing: C=A.B+C
87  #A ssymetric matrix, B,C matrices
88  A = np.random.randn(sizek, sizek)
89  B = np.random.randn(sizek, sizen)
90  C = np.random.randn(sizek, sizen)
91 
92  #generating random matrices and associated CarmaObj
93  matA = ch.obj_float(c, A) #np.random.randn(sizek, sizek)))
94  matB = ch.obj_float(c, B) #np.random.randn(sizek, sizen)))
95  matC = ch.obj_float(c, C) #np.random.randn(sizek, sizen)))
96 
97  # matA.random_host(seed, 'U')
98  # matB.random_host(seed + 2, 'U')
99  # matC.random_host(seed + 3, 'U')
100 
101  #A symetric
102  A = np.array(matA)
103  A = (A + A.T) / 2
104  matA.host2device(A)
105  B = np.array(matB)
106  C = np.array(matC)
107 
108  #matrices multiplication
109  t1 = time.perf_counter()
110  matA.symm(matB, 1, matC, 1)
111  t2 = time.perf_counter()
112  C = A.dot(B) + C
113  t3 = time.perf_counter()
114 
115  matC2 = matA.symm(matB)
116  C2 = A.dot(B)
117 
118  print("")
119  print("test symm:")
120  print("execution time (s)")
121  print("python: ", t3 - t2)
122  print("carma : ", t2 - t1)
123 
124  #test results
125  res = np.array(matC)
126 
127  npt.assert_almost_equal(C, res, decimal=dec - 1)
128  npt.assert_almost_equal(C2, np.array(matC2), decimal=dec - 1)
129 
130  # M = np.argmax(np.abs(res - C))
131  # d = 5
132  # if (0 < np.abs(C.item(M))):
133 
134 
135 # d = 10**np.ceil(np.log10(np.abs(C.item(M))))
136 
137 # print(res.item(M))
138 # print(C.item(M))
139 
140 # npt.assert_almost_equal(C.item(M) / d, res.item(M) / d, decimal=dec)
141 
142 
144  #function dgmm
145  #testing: C=A.d
146  # C,A matrices, d vector (diagonal matrix as a vector)
147 
148  #generating random matrices and associated CarmaObj
149  matA = ch.obj_float(c, np.random.randn(sizek, sizek))
150  Vectx = ch.obj_float(c, np.random.randn(sizek))
151 
152  # matA.random_host(seed, 'U')
153  # Vectx.random_host(seed + 2, 'U')
154  A = np.array(matA)
155  x = np.array(Vectx)
156 
157  #matrices product
158  t1 = time.perf_counter()
159  matC = matA.dgmm(Vectx)
160  t2 = time.perf_counter()
161  C = A * x
162  t3 = time.perf_counter()
163 
164  print("")
165  print("test dgmm:")
166  print("execution time (s)")
167  print("python: ", t3 - t2)
168  print("carma : ", t2 - t1)
169 
170  #test results
171  res = np.array(matC)
172 
173  npt.assert_almost_equal(C, res, decimal=dec)
174 
175  # M = np.argmax(np.abs(res - C))
176  # d = 5
177  # if (0 < np.abs(C.item(M))):
178  # d = 10**np.ceil(np.log10(np.abs(C.item(M))))
179 
180  # print(res.item(M))
181  # print(C.item(M))
182 
183  # npt.assert_almost_equal(C.item(M) / d, res.item(M) / d, decimal=dec)
184 
185 
187  #function syrk
188  #testing: C=A.transpose(A)+C
189  #A matrix, C symetric matrix
190 
191  #generating random matrices and associated CarmaObj
192  matA = ch.obj_float(c, np.random.randn(sizen, sizek))
193  matC = ch.obj_float(c, np.random.randn(sizen, sizen))
194 
195  # matA.random_host(seed, 'U')
196  # matC.random_host(seed + 2, 'U')
197 
198  A = np.array(matA)
199  C = np.array(matC)
200  #syrk: C matrix is symetric
201  C = (C + C.T) / 2
202 
203  matC.host2device(C)
204 
205  #matrices product
206  t1 = time.perf_counter()
207  matA.syrk(matC=matC, beta=1)
208  t2 = time.perf_counter()
209  C = A.dot(A.T) + C
210  t3 = time.perf_counter()
211 
212  print("")
213  print("test syrk:")
214  print("execution time (s)")
215  print("python: ", t3 - t2)
216  print("carma : ", t2 - t1)
217 
218  #test results
219  res = np.array(matC)
220 
221  #only upper triangle is computed
222  C = np.triu(C).flatten()
223  res = np.triu(res).flatten()
224  npt.assert_almost_equal(C, res, decimal=dec - 1)
225 
226  # M = np.argmax(np.abs(res - C))
227  # d = 5
228  # if (0 < np.abs(C.item(M))):
229  # d = 10**np.ceil(np.log10(np.abs(C.item(M))))
230 
231  # print(res.item(M))
232  # print(C.item(M))
233 
234  # npt.assert_almost_equal(C.item(M) / d, res.item(M) / d, decimal=dec)
235 
236 
238  #function syrkx
239  #testing: C=A.transpose(B)+C
240  #A matrix, C symetric matrix
241 
242  #generating random matrices and associated CarmaObj
243  matA = ch.obj_float(c, np.random.randn(sizen, sizek))
244  matB = ch.obj_float(c, np.random.randn(sizen, sizek))
245  matC = ch.obj_float(c, np.random.randn(sizen, sizen))
246 
247  # matA.random_host(seed, 'U')
248  # matB.random_host(seed + 2, 'U')
249  # matC.random_host(seed + 3, 'U')
250 
251  A = np.array(matA)
252  B = np.array(matB)
253  C = np.array(matC)
254 
255  #C is symetric
256  C = np.dot(C, C.T)
257 
258  matC.host2device(C)
259 
260  #matrices product
261  t1 = time.perf_counter()
262  matA.syrkx(matB, alpha=1, matC=matC, beta=1)
263  t2 = time.perf_counter()
264  C = A.dot(B.T) + C.T
265  t3 = time.perf_counter()
266 
267  print("")
268  print("test syrkx:")
269  print("execution time (s)")
270  print("python: ", t3 - t2)
271  print("carma : ", t2 - t1)
272 
273  #test results
274  res = np.array(matC)
275 
276  #only upper triangle is computed
277  res = np.triu(res)
278  C = np.triu(C)
279  npt.assert_almost_equal(C, res, decimal=dec - 1)
280 
281  # M = np.argmax(np.abs(res - C))
282  # d = 5
283  # if (0 < np.abs(C.item(M))):
284  # d = 10**np.ceil(np.log10(np.abs(C.item(M))))
285 
286  # print(res.item(M))
287  # print(C.item(M))
288 
289  # npt.assert_almost_equal(C.item(M) / d, res.item(M) / d, decimal=dec)
290 
291 
293 
294  #function geam
295  #testing: C=A.B
296  #A,B matrices
297 
298  #generating random matrices and associated CarmaObj
299  matA = ch.obj_float(c, np.random.randn(sizem, sizen))
300  matB = ch.obj_float(c, np.random.randn(sizem, sizen))
301 
302  # matA.random_host(seed, 'U')
303  # matB.random_host(seed + 2, 'U')
304 
305  A = np.array(matA)
306  B = np.array(matB)
307 
308  #matrices product
309  t1 = time.perf_counter()
310  C = A + B
311  t2 = time.perf_counter()
312  matC = matA.geam(matB, beta=1)
313  t3 = time.perf_counter()
314 
315  print("")
316  print("test geam:")
317  print("execution time (s)")
318  print("python: ", t3 - t2)
319  print("carma : ", t2 - t1)
320 
321  #testing result
322 
323  npt.assert_array_almost_equal(C, np.array(matC), decimal=dec)
324 
325 
327 
328  # function gemm
329  #testing: C=A.B+C
330  #A,B,C matrices
331 
332  #generating random matrices A,B,C and associated CarmaObj
333 
334  # np.random.seed(seed)
335  # A = A.dot(A.T)
336  # B = B.dot(B.T)
337 
338  matA = ch.obj_double(c, np.random.randn(sizem, sizek))
339  matAT = ch.obj_double(c, np.random.randn(sizek, sizem))
340  matB = ch.obj_double(c, np.random.randn(sizek, sizen))
341  matBT = ch.obj_double(c, np.random.randn(sizen, sizek))
342  matC = ch.obj_double(c, np.random.randn(sizem, sizen))
343  matC2 = ch.obj_double(c, np.random.randn(sizem, sizen))
344  matC3 = ch.obj_double(c, np.random.randn(sizem, sizen))
345 
346  # matA.random_host(seed, 'U')
347  # matAT.random_host(seed, 'U')
348  # matB.random_host(seed + 2, 'U')
349  # matBT.random_host(seed + 2, 'U')
350  # matC.random_host(seed + 3, 'U')
351  # matC2.random_host(seed + 3, 'U')
352  # matC3.random_host(seed + 3, 'U')
353 
354  A = np.array(matA)
355  AT = np.array(matAT)
356  B = np.array(matB)
357  BT = np.array(matBT)
358  C = np.array(matC)
359  C2 = np.array(matC2)
360  C3 = np.array(matC3)
361 
362  alpha = 2
363  beta = 1
364 
365  #matrices product
366  matA.gemm(matB, 'n', 'n', alpha, matC, beta)
367  matAT.gemm(matB, 't', 'n', alpha, matC2, beta)
368  matAT.gemm(matBT, 't', 't', alpha, matC3, beta)
369  matC4 = matA.gemm(matB, 'n', 'n', alpha)
370  matC5 = matAT.gemm(matB, 't', 'n', alpha)
371  matC6 = matAT.gemm(matBT, 't', 't', alpha)
372 
373  C = alpha * A.dot(B) + beta * C
374  C2 = alpha * AT.T.dot(B) + beta * C2
375  C3 = alpha * AT.T.dot(BT.T) + beta * C3
376  C4 = alpha * A.dot(B)
377  C5 = alpha * AT.T.dot(B)
378  C6 = alpha * AT.T.dot(BT.T)
379 
380  npt.assert_array_almost_equal(C, np.array(matC), decimal=2 * dec - 1)
381  npt.assert_array_almost_equal(C2, np.array(matC2), decimal=2 * dec - 1)
382  npt.assert_array_almost_equal(C3, np.array(matC3), decimal=2 * dec - 1)
383  npt.assert_array_almost_equal(C4, np.array(matC4), decimal=2 * dec - 1)
384  npt.assert_array_almost_equal(C5, np.array(matC5), decimal=2 * dec - 1)
385  npt.assert_array_almost_equal(C6, np.array(matC6), decimal=2 * dec - 1)
386 
387 
389  #function symm
390  #testing: C=A.B+C
391  #A ssymetric matrix, B,C matrices
392  A = np.random.randn(sizek, sizek)
393  B = np.random.randn(sizek, sizen)
394  C = np.random.randn(sizek, sizen)
395 
396  #generating random matrices and associated CarmaObj
397  matA = ch.obj_double(c, A) #np.random.randn(sizek, sizek)))
398  matB = ch.obj_double(c, B) #np.random.randn(sizek, sizen)))
399  matC = ch.obj_double(c, C) #np.random.randn(sizek, sizen)))
400 
401  # matA.random_host(seed, 'U')
402  # matB.random_host(seed + 2, 'U')
403  # matC.random_host(seed + 3, 'U')
404 
405  #A symetric
406  A = np.array(matA)
407  A = (A + A.T) / 2
408  matA.host2device(A)
409  B = np.array(matB)
410  C = np.array(matC)
411 
412  #matrices multiplication
413  t1 = time.perf_counter()
414  matA.symm(matB, 1, matC, 1)
415  t2 = time.perf_counter()
416  C = A.dot(B) + C
417  t3 = time.perf_counter()
418 
419  matC2 = matA.symm(matB)
420  C2 = A.dot(B)
421 
422  print("")
423  print("test symm:")
424  print("execution time (s)")
425  print("python: ", t3 - t2)
426  print("carma : ", t2 - t1)
427 
428  #test results
429  res = np.array(matC)
430 
431  npt.assert_almost_equal(C, res, decimal=2 * dec - 1)
432  npt.assert_almost_equal(C2, np.array(matC2), decimal=2 * dec - 1)
433 
434  # M = np.argmax(np.abs(res - C))
435  # d = 5
436  # if (0 < np.abs(C.item(M))):
437 
438 
439 # d = 10**np.ceil(np.log10(np.abs(C.item(M))))
440 
441 # print(res.item(M))
442 # print(C.item(M))
443 
444 # npt.assert_almost_equal(C.item(M) / d, res.item(M) / d, decimal=2*dec)
445 
446 
448  #function dgmm
449  #testing: C=A.d
450  # C,A matrices, d vector (diagonal matrix as a vector)
451 
452  #generating random matrices and associated CarmaObj
453  matA = ch.obj_double(c, np.random.randn(sizek, sizek))
454  Vectx = ch.obj_double(c, np.random.randn(sizek))
455 
456  # matA.random_host(seed, 'U')
457  # Vectx.random_host(seed + 2, 'U')
458  A = np.array(matA)
459  x = np.array(Vectx)
460 
461  #matrices product
462  t1 = time.perf_counter()
463  matC = matA.dgmm(Vectx)
464  t2 = time.perf_counter()
465  C = A * x
466  t3 = time.perf_counter()
467 
468  print("")
469  print("test dgmm:")
470  print("execution time (s)")
471  print("python: ", t3 - t2)
472  print("carma : ", t2 - t1)
473 
474  #test results
475  res = np.array(matC)
476 
477  npt.assert_almost_equal(C, res, decimal=2 * dec)
478 
479  # M = np.argmax(np.abs(res - C))
480  # d = 5
481  # if (0 < np.abs(C.item(M))):
482  # d = 10**np.ceil(np.log10(np.abs(C.item(M))))
483 
484  # print(res.item(M))
485  # print(C.item(M))
486 
487  # npt.assert_almost_equal(C.item(M) / d, res.item(M) / d, decimal=2*dec)
488 
489 
491  #function syrk
492  #testing: C=A.transpose(A)+C
493  #A matrix, C symetric matrix
494 
495  #generating random matrices and associated CarmaObj
496  matA = ch.obj_double(c, np.random.randn(sizen, sizek))
497  matC = ch.obj_double(c, np.random.randn(sizen, sizen))
498 
499  # matA.random_host(seed, 'U')
500  # matC.random_host(seed + 2, 'U')
501 
502  A = np.array(matA)
503  C = np.array(matC)
504  #syrk: C matrix is symetric
505  C = (C + C.T) / 2
506 
507  matC.host2device(C)
508 
509  #matrices product
510  t1 = time.perf_counter()
511  matA.syrk(matC=matC, beta=1)
512  t2 = time.perf_counter()
513  C = A.dot(A.T) + C
514  t3 = time.perf_counter()
515 
516  print("")
517  print("test syrk:")
518  print("execution time (s)")
519  print("python: ", t3 - t2)
520  print("carma : ", t2 - t1)
521 
522  #test results
523  res = np.array(matC)
524 
525  #only upper triangle is computed
526  C = np.triu(C).flatten()
527  res = np.triu(res).flatten()
528  npt.assert_almost_equal(C, res, decimal=2 * dec - 1)
529 
530  # M = np.argmax(np.abs(res - C))
531  # d = 5
532  # if (0 < np.abs(C.item(M))):
533  # d = 10**np.ceil(np.log10(np.abs(C.item(M))))
534 
535  # print(res.item(M))
536  # print(C.item(M))
537 
538  # npt.assert_almost_equal(C.item(M) / d, res.item(M) / d, decimal=2*dec)
539 
540 
542  #function syrkx
543  #testing: C=A.transpose(B)+C
544  #A matrix, C symetric matrix
545 
546  #generating random matrices and associated CarmaObj
547  matA = ch.obj_double(c, np.random.randn(sizen, sizek))
548  matB = ch.obj_double(c, np.random.randn(sizen, sizek))
549  matC = ch.obj_double(c, np.random.randn(sizen, sizen))
550 
551  # matA.random_host(seed, 'U')
552  # matB.random_host(seed + 2, 'U')
553  # matC.random_host(seed + 3, 'U')
554 
555  A = np.array(matA)
556  B = np.array(matB)
557  C = np.array(matC)
558 
559  #C is symetric
560  C = np.dot(C, C.T)
561 
562  matC.host2device(C)
563 
564  #matrices product
565  t1 = time.perf_counter()
566  matA.syrkx(matB, alpha=1, matC=matC, beta=1)
567  t2 = time.perf_counter()
568  C = A.dot(B.T) + C.T
569  t3 = time.perf_counter()
570 
571  print("")
572  print("test syrkx:")
573  print("execution time (s)")
574  print("python: ", t3 - t2)
575  print("carma : ", t2 - t1)
576 
577  #test results
578  res = np.array(matC)
579 
580  #only upper triangle is computed
581  res = np.triu(res)
582  C = np.triu(C)
583  npt.assert_almost_equal(C, res, decimal=2 * dec - 1)
584 
585  # M = np.argmax(np.abs(res - C))
586  # d = 5
587  # if (0 < np.abs(C.item(M))):
588  # d = 10**np.ceil(np.log10(np.abs(C.item(M))))
589 
590  # print(res.item(M))
591  # print(C.item(M))
592 
593  # npt.assert_almost_equal(C.item(M) / d, res.item(M) / d, decimal=2*dec)
594 
595 
597 
598  #function geam
599  #testing: C=A.B
600  #A,B matrices
601 
602  #generating random matrices and associated CarmaObj
603  matA = ch.obj_double(c, np.random.randn(sizem, sizen))
604  matB = ch.obj_double(c, np.random.randn(sizem, sizen))
605 
606  # matA.random_host(seed, 'U')
607  # matB.random_host(seed + 2, 'U')
608 
609  A = np.array(matA)
610  B = np.array(matB)
611 
612  #matrices product
613  t1 = time.perf_counter()
614  C = A + B
615  t2 = time.perf_counter()
616  matC = matA.geam(matB, beta=1)
617  t3 = time.perf_counter()
618 
619  print("")
620  print("test geam:")
621  print("execution time (s)")
622  print("python: ", t3 - t2)
623  print("carma : ", t2 - t1)
624 
625  #testing result
626 
627  npt.assert_array_almost_equal(C, np.array(matC), decimal=2 * dec)
test_cublas3.test_float_gemm
def test_float_gemm()
Definition: test_cublas3.py:22
test_cublas3.test_double_syrk
def test_double_syrk()
Definition: test_cublas3.py:490
test_cublas3.test_double_symm
def test_double_symm()
Definition: test_cublas3.py:388
test_cublas3.test_double_syrkx
def test_double_syrkx()
Definition: test_cublas3.py:541
test_cublas3.test_float_symm
def test_float_symm()
Definition: test_cublas3.py:84
test_cublas3.test_double_dgmm
def test_double_dgmm()
Definition: test_cublas3.py:447
test_cublas3.test_float_syrkx
def test_float_syrkx()
Definition: test_cublas3.py:237
test_cublas3.test_double_geam
def test_double_geam()
Definition: test_cublas3.py:596
test_cublas3.test_float_syrk
def test_float_syrk()
Definition: test_cublas3.py:186
test_cublas3.test_double_gemm
def test_double_gemm()
Definition: test_cublas3.py:326
test_cublas3.test_float_geam
def test_float_geam()
Definition: test_cublas3.py:292
test_cublas3.test_float_dgmm
def test_float_dgmm()
Definition: test_cublas3.py:143