You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_zgemm.c 9.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*****************************************************************************
  2. Copyright (c) 2023, The OpenBLAS Project
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. 3. Neither the name of the OpenBLAS project nor the names of
  14. its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written
  16. permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  26. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. **********************************************************************************/
  28. #include "utest/openblas_utest.h"
  29. #include <cblas.h>
  30. #include "common.h"
  31. #define DATASIZE 100
  32. #define INCREMENT 2
  33. struct DATA_ZGEMM {
  34. double a_test[DATASIZE * DATASIZE * 2];
  35. double a_verify[DATASIZE * DATASIZE * 2];
  36. double b_test[DATASIZE * DATASIZE * 2];
  37. double b_verify[DATASIZE * DATASIZE * 2];
  38. double c_test[DATASIZE * DATASIZE * 2];
  39. double c_verify[DATASIZE * DATASIZE * 2];
  40. };
  41. #ifdef BUILD_COMPLEX16
  42. static struct DATA_ZGEMM data_zgemm;
  43. /**
  44. * Test zgemm with the conjugate matrices by conjugating and not transposed matrices
  45. * and comparing it with the non-conjugate zgemm.
  46. *
  47. * param transa specifies op(A), the transposition (conjugation) operation applied to A
  48. * param transb specifies op(B), the transposition (conjugation) operation applied to B
  49. * param m specifies the number of rows of the matrix op(A) and of the matrix C
  50. * param n specifies the number of columns of the matrix op(B) and the number of columns of the matrix C
  51. * param k specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B)
  52. * param alpha - scaling factor for the matrix-matrix product
  53. * param lda - leading dimension of matrix A
  54. * param ldb - leading dimension of matrix B
  55. * param beta - scaling factor for matrix C
  56. * param ldc - leading dimension of matrix C
  57. * return norm of difference
  58. */
  59. static double check_zgemm(char transa, char transb, blasint m, blasint n, blasint k,
  60. double *alpha, blasint lda, blasint ldb, double *beta, blasint ldc)
  61. {
  62. blasint i;
  63. double alpha_conj[] = {1.0, 0.0};
  64. char transa_verify = transa;
  65. char transb_verify = transb;
  66. char cc[2]="C", cr[2]="R";
  67. blasint arows = k, acols = m;
  68. blasint brows = n, bcols = k;
  69. if (transa == 'T' || transa == 'C'){
  70. arows = m; acols = k;
  71. }
  72. if (transb == 'T' || transb == 'C'){
  73. brows = k; bcols = n;
  74. }
  75. drand_generate(data_zgemm.a_test, arows * lda * 2);
  76. drand_generate(data_zgemm.b_test, brows * ldb * 2);
  77. drand_generate(data_zgemm.c_test, n * ldc * 2);
  78. for (i = 0; i < arows * lda * 2; i++)
  79. data_zgemm.a_verify[i] = data_zgemm.a_test[i];
  80. for (i = 0; i < brows * ldb * 2; i++)
  81. data_zgemm.b_verify[i] = data_zgemm.b_test[i];
  82. for (i = 0; i < n * ldc * 2; i++)
  83. data_zgemm.c_verify[i] = data_zgemm.c_test[i];
  84. if (transa == 'R'){
  85. BLASFUNC(zimatcopy)(cc, cr, &arows, &acols, alpha_conj, data_zgemm.a_verify, &lda, &lda);
  86. transa_verify = 'N';
  87. }
  88. if (transb == 'R'){
  89. BLASFUNC(zimatcopy)(cc, cr, &brows, &bcols, alpha_conj, data_zgemm.b_verify, &ldb, &ldb);
  90. transb_verify = 'N';
  91. }
  92. BLASFUNC(zgemm)(&transa_verify, &transb_verify, &m, &n, &k, alpha, data_zgemm.a_verify, &lda,
  93. data_zgemm.b_verify, &ldb, beta, data_zgemm.c_verify, &ldc);
  94. BLASFUNC(zgemm)(&transa, &transb, &m, &n, &k, alpha, data_zgemm.a_test, &lda,
  95. data_zgemm.b_test, &ldb, beta, data_zgemm.c_test, &ldc);
  96. return dmatrix_difference(data_zgemm.c_test, data_zgemm.c_verify, m, n, ldc*2);
  97. }
  98. /**
  99. * Test zgemm with the conjugate matrices by conjugating and not transposed matrices
  100. * and comparing it with the non-conjugate zgemm.
  101. * Test with the following options:
  102. *
  103. * matrix A is conjugate and transposed
  104. * matrix B is conjugate and not transposed
  105. */
  106. CTEST(zgemm, conjtransa_conjnotransb)
  107. {
  108. blasint n = DATASIZE, m = DATASIZE, k = DATASIZE;
  109. blasint lda = DATASIZE, ldb = DATASIZE, ldc = DATASIZE;
  110. char transa = 'C';
  111. char transb = 'R';
  112. double alpha[] = {-2.0, 1.0};
  113. double beta[] = {1.0, -1.0};
  114. double norm = check_zgemm(transa, transb, m, n, k, alpha, lda, ldb, beta, ldc);
  115. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_EPS);
  116. }
  117. /**
  118. * Test zgemm with the conjugate matrices by conjugating and not transposed matrices
  119. * and comparing it with the non-conjugate zgemm.
  120. * Test with the following options:
  121. *
  122. * matrix A is not conjugate and not transposed
  123. * matrix B is conjugate and not transposed
  124. */
  125. CTEST(zgemm, notransa_conjnotransb)
  126. {
  127. blasint n = DATASIZE, m = DATASIZE, k = DATASIZE;
  128. blasint lda = DATASIZE, ldb = DATASIZE, ldc = DATASIZE;
  129. char transa = 'N';
  130. char transb = 'R';
  131. double alpha[] = {-2.0, 1.0};
  132. double beta[] = {1.0, -1.0};
  133. double norm = check_zgemm(transa, transb, m, n, k, alpha, lda, ldb, beta, ldc);
  134. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_EPS);
  135. }
  136. /**
  137. * Test zgemm with the conjugate matrices by conjugating and not transposed matrices
  138. * and comparing it with the non-conjugate zgemm.
  139. * Test with the following options:
  140. *
  141. * matrix A is conjugate and not transposed
  142. * matrix B is conjugate and transposed
  143. */
  144. CTEST(zgemm, conjnotransa_conjtransb)
  145. {
  146. blasint n = DATASIZE, m = DATASIZE, k = DATASIZE;
  147. blasint lda = DATASIZE, ldb = DATASIZE, ldc = DATASIZE;
  148. char transa = 'R';
  149. char transb = 'C';
  150. double alpha[] = {-2.0, 1.0};
  151. double beta[] = {1.0, -1.0};
  152. double norm = check_zgemm(transa, transb, m, n, k, alpha, lda, ldb, beta, ldc);
  153. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_EPS);
  154. }
  155. /**
  156. * Test zgemm with the conjugate matrices by conjugating and not transposed matrices
  157. * and comparing it with the non-conjugate zgemm.
  158. * Test with the following options:
  159. *
  160. * matrix A is conjugate and not transposed
  161. * matrix B is not conjugate and not transposed
  162. */
  163. CTEST(zgemm, conjnotransa_notransb)
  164. {
  165. blasint n = DATASIZE, m = DATASIZE, k = DATASIZE;
  166. blasint lda = DATASIZE, ldb = DATASIZE, ldc = DATASIZE;
  167. char transa = 'R';
  168. char transb = 'N';
  169. double alpha[] = {-2.0, 1.0};
  170. double beta[] = {1.0, -1.0};
  171. double norm = check_zgemm(transa, transb, m, n, k, alpha, lda, ldb, beta, ldc);
  172. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_EPS);
  173. }
  174. /**
  175. * Test zgemm with the conjugate matrices by conjugating and not transposed matrices
  176. * and comparing it with the non-conjugate zgemm.
  177. * Test with the following options:
  178. *
  179. * matrix A is conjugate and not transposed
  180. * matrix B is conjugate and not transposed
  181. */
  182. CTEST(zgemm, conjnotransa_conjnotransb)
  183. {
  184. blasint n = DATASIZE, m = DATASIZE, k = DATASIZE;
  185. blasint lda = DATASIZE, ldb = DATASIZE, ldc = DATASIZE;
  186. char transa = 'R';
  187. char transb = 'R';
  188. double alpha[] = {-2.0, 1.0};
  189. double beta[] = {1.0, -1.0};
  190. double norm = check_zgemm(transa, transb, m, n, k, alpha, lda, ldb, beta, ldc);
  191. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_EPS);
  192. }
  193. /**
  194. * Test zgemm with the conjugate matrices by conjugating and not transposed matrices
  195. * and comparing it with the non-conjugate zgemm.
  196. * Test with the following options:
  197. *
  198. * matrix A is conjugate and not transposed
  199. * matrix B is transposed
  200. */
  201. CTEST(zgemm, conjnotransa_transb)
  202. {
  203. blasint n = DATASIZE, m = DATASIZE, k = DATASIZE;
  204. blasint lda = DATASIZE, ldb = DATASIZE, ldc = DATASIZE;
  205. char transa = 'R';
  206. char transb = 'T';
  207. double alpha[] = {-2.0, 1.0};
  208. double beta[] = {1.0, -1.0};
  209. double norm = check_zgemm(transa, transb, m, n, k, alpha, lda, ldb, beta, ldc);
  210. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_EPS);
  211. }
  212. /**
  213. * Test zgemm with the conjugate matrices by conjugating and not transposed matrices
  214. * and comparing it with the non-conjugate zgemm.
  215. * Test with the following options:
  216. *
  217. * matrix A is transposed
  218. * matrix B is conjugate and not transposed
  219. */
  220. CTEST(zgemm, transa_conjnotransb)
  221. {
  222. blasint n = DATASIZE, m = DATASIZE, k = DATASIZE;
  223. blasint lda = DATASIZE, ldb = DATASIZE, ldc = DATASIZE;
  224. char transa = 'T';
  225. char transb = 'R';
  226. double alpha[] = {-2.0, 1.0};
  227. double beta[] = {1.0, -1.0};
  228. double norm = check_zgemm(transa, transb, m, n, k, alpha, lda, ldb, beta, ldc);
  229. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_EPS);
  230. }
  231. #endif