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_cgemm.c 9.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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_CGEMM {
  34. float a_test[DATASIZE * DATASIZE * 2];
  35. float a_verify[DATASIZE * DATASIZE * 2];
  36. float b_test[DATASIZE * DATASIZE * 2];
  37. float b_verify[DATASIZE * DATASIZE * 2];
  38. float c_test[DATASIZE * DATASIZE * 2];
  39. float c_verify[DATASIZE * DATASIZE * 2];
  40. };
  41. #ifdef BUILD_COMPLEX
  42. static struct DATA_CGEMM data_cgemm;
  43. /**
  44. * Test cgemm with the conjugate matrices by conjugating and not transposed matrices
  45. * and comparing it with the non-conjugate cgemm.
  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 float check_cgemm(char transa, char transb, blasint m, blasint n, blasint k,
  60. float *alpha, blasint lda, blasint ldb, float *beta, blasint ldc)
  61. {
  62. blasint i;
  63. float alpha_conj[] = {1.0f, 0.0f};
  64. char transa_verify = transa;
  65. char transb_verify = transb;
  66. int arows = k, acols = m;
  67. int brows = n, bcols = k;
  68. if (transa == 'T' || transa == 'C'){
  69. arows = m; acols = k;
  70. }
  71. if (transb == 'T' || transb == 'C'){
  72. brows = k; bcols = n;
  73. }
  74. srand_generate(data_cgemm.a_test, arows * lda * 2);
  75. srand_generate(data_cgemm.b_test, brows * ldb * 2);
  76. srand_generate(data_cgemm.c_test, n * ldc * 2);
  77. for (i = 0; i < arows * lda * 2; i++)
  78. data_cgemm.a_verify[i] = data_cgemm.a_test[i];
  79. for (i = 0; i < brows * ldb * 2; i++)
  80. data_cgemm.b_verify[i] = data_cgemm.b_test[i];
  81. for (i = 0; i < n * ldc * 2; i++)
  82. data_cgemm.c_verify[i] = data_cgemm.c_test[i];
  83. if (transa == 'R'){
  84. cblas_cimatcopy(CblasColMajor, CblasConjNoTrans, arows, acols, alpha_conj, data_cgemm.a_verify, lda, lda);
  85. transa_verify = 'N';
  86. }
  87. if (transb == 'R'){
  88. cblas_cimatcopy(CblasColMajor, CblasConjNoTrans, brows, bcols, alpha_conj, data_cgemm.b_verify, ldb, ldb);
  89. transb_verify = 'N';
  90. }
  91. BLASFUNC(cgemm)(&transa_verify, &transb_verify, &m, &n, &k, alpha, data_cgemm.a_verify, &lda,
  92. data_cgemm.b_verify, &ldb, beta, data_cgemm.c_verify, &ldc);
  93. BLASFUNC(cgemm)(&transa, &transb, &m, &n, &k, alpha, data_cgemm.a_test, &lda,
  94. data_cgemm.b_test, &ldb, beta, data_cgemm.c_test, &ldc);
  95. return smatrix_difference(data_cgemm.c_test, data_cgemm.c_verify, m, n, ldc*2);
  96. }
  97. /**
  98. * Test cgemm with the conjugate matrices by conjugating and not transposed matrices
  99. * and comparing it with the non-conjugate cgemm.
  100. * Test with the following options:
  101. *
  102. * matrix A is conjugate and transposed
  103. * matrix B is conjugate and not transposed
  104. */
  105. CTEST(cgemm, conjtransa_conjnotransb)
  106. {
  107. blasint n = DATASIZE, m = DATASIZE, k = DATASIZE;
  108. blasint lda = DATASIZE, ldb = DATASIZE, ldc = DATASIZE;
  109. char transa = 'C';
  110. char transb = 'R';
  111. float alpha[] = {-2.0, 1.0f};
  112. float beta[] = {1.0f, -1.0f};
  113. float norm = check_cgemm(transa, transb, m, n, k, alpha, lda, ldb, beta, ldc);
  114. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  115. }
  116. /**
  117. * Test cgemm with the conjugate matrices by conjugating and not transposed matrices
  118. * and comparing it with the non-conjugate cgemm.
  119. * Test with the following options:
  120. *
  121. * matrix A is not conjugate and not transposed
  122. * matrix B is conjugate and not transposed
  123. */
  124. CTEST(cgemm, notransa_conjnotransb)
  125. {
  126. blasint n = DATASIZE, m = DATASIZE, k = DATASIZE;
  127. blasint lda = DATASIZE, ldb = DATASIZE, ldc = DATASIZE;
  128. char transa = 'N';
  129. char transb = 'R';
  130. float alpha[] = {-2.0, 1.0f};
  131. float beta[] = {1.0f, -1.0f};
  132. float norm = check_cgemm(transa, transb, m, n, k, alpha, lda, ldb, beta, ldc);
  133. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  134. }
  135. /**
  136. * Test cgemm with the conjugate matrices by conjugating and not transposed matrices
  137. * and comparing it with the non-conjugate cgemm.
  138. * Test with the following options:
  139. *
  140. * matrix A is conjugate and not transposed
  141. * matrix B is conjugate and transposed
  142. */
  143. CTEST(cgemm, conjnotransa_conjtransb)
  144. {
  145. blasint n = DATASIZE, m = DATASIZE, k = DATASIZE;
  146. blasint lda = DATASIZE, ldb = DATASIZE, ldc = DATASIZE;
  147. char transa = 'R';
  148. char transb = 'C';
  149. float alpha[] = {-2.0, 1.0f};
  150. float beta[] = {1.0f, -1.0f};
  151. float norm = check_cgemm(transa, transb, m, n, k, alpha, lda, ldb, beta, ldc);
  152. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  153. }
  154. /**
  155. * Test cgemm with the conjugate matrices by conjugating and not transposed matrices
  156. * and comparing it with the non-conjugate cgemm.
  157. * Test with the following options:
  158. *
  159. * matrix A is conjugate and not transposed
  160. * matrix B is not conjugate and not transposed
  161. */
  162. CTEST(cgemm, conjnotransa_notransb)
  163. {
  164. blasint n = DATASIZE, m = DATASIZE, k = DATASIZE;
  165. blasint lda = DATASIZE, ldb = DATASIZE, ldc = DATASIZE;
  166. char transa = 'R';
  167. char transb = 'N';
  168. float alpha[] = {-2.0, 1.0f};
  169. float beta[] = {1.0f, -1.0f};
  170. float norm = check_cgemm(transa, transb, m, n, k, alpha, lda, ldb, beta, ldc);
  171. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  172. }
  173. /**
  174. * Test cgemm with the conjugate matrices by conjugating and not transposed matrices
  175. * and comparing it with the non-conjugate cgemm.
  176. * Test with the following options:
  177. *
  178. * matrix A is conjugate and not transposed
  179. * matrix B is conjugate and not transposed
  180. */
  181. CTEST(cgemm, conjnotransa_conjnotransb)
  182. {
  183. blasint n = DATASIZE, m = DATASIZE, k = DATASIZE;
  184. blasint lda = DATASIZE, ldb = DATASIZE, ldc = DATASIZE;
  185. char transa = 'R';
  186. char transb = 'R';
  187. float alpha[] = {-2.0, 1.0f};
  188. float beta[] = {1.0f, -1.0f};
  189. float norm = check_cgemm(transa, transb, m, n, k, alpha, lda, ldb, beta, ldc);
  190. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  191. }
  192. /**
  193. * Test cgemm with the conjugate matrices by conjugating and not transposed matrices
  194. * and comparing it with the non-conjugate cgemm.
  195. * Test with the following options:
  196. *
  197. * matrix A is conjugate and not transposed
  198. * matrix B is transposed
  199. */
  200. CTEST(cgemm, conjnotransa_transb)
  201. {
  202. blasint n = DATASIZE, m = DATASIZE, k = DATASIZE;
  203. blasint lda = DATASIZE, ldb = DATASIZE, ldc = DATASIZE;
  204. char transa = 'R';
  205. char transb = 'T';
  206. float alpha[] = {-2.0, 1.0f};
  207. float beta[] = {1.0f, -1.0f};
  208. float norm = check_cgemm(transa, transb, m, n, k, alpha, lda, ldb, beta, ldc);
  209. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  210. }
  211. /**
  212. * Test cgemm with the conjugate matrices by conjugating and not transposed matrices
  213. * and comparing it with the non-conjugate cgemm.
  214. * Test with the following options:
  215. *
  216. * matrix A is transposed
  217. * matrix B is conjugate and not transposed
  218. */
  219. CTEST(cgemm, transa_conjnotransb)
  220. {
  221. blasint n = DATASIZE, m = DATASIZE, k = DATASIZE;
  222. blasint lda = DATASIZE, ldb = DATASIZE, ldc = DATASIZE;
  223. char transa = 'T';
  224. char transb = 'R';
  225. float alpha[] = {-2.0, 1.0f};
  226. float beta[] = {1.0f, -1.0f};
  227. float norm = check_cgemm(transa, transb, m, n, k, alpha, lda, ldb, beta, ldc);
  228. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  229. }
  230. #endif