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_zgemv_n.c 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 "common.h"
  30. #define DATASIZE 100
  31. #define INCREMENT 2
  32. struct DATA_ZSPMV_N {
  33. double a_test[DATASIZE * DATASIZE * 2];
  34. double b_test[DATASIZE * 2 * INCREMENT];
  35. double c_test[DATASIZE * 2 * INCREMENT];
  36. double c_verify[DATASIZE * 2 * INCREMENT];
  37. };
  38. #ifdef BUILD_COMPLEX16
  39. static struct DATA_ZSPMV_N data_zgemv_n;
  40. /**
  41. * zgemv not transposed reference code
  42. *
  43. * param trans specifies whether matris A is conj or/and xconj
  44. * param m - number of rows of A
  45. * param n - number of columns of A
  46. * param alpha - scaling factor for the matrib-vector product
  47. * param a - buffer holding input matrib A
  48. * param lda - leading dimension of matrix A
  49. * param b - Buffer holding input vector b
  50. * param inc_b - stride of vector b
  51. * param beta - scaling factor for vector c
  52. * param c - buffer holding input/output vector c
  53. * param inc_c - stride of vector c
  54. */
  55. static void zgemv_n_trusted(char trans, blasint m, blasint n, double *alpha, double *a,
  56. blasint lda, double *b, blasint inc_b, double *beta, double *c,
  57. blasint inc_c)
  58. {
  59. blasint i, j;
  60. blasint i2 = 0;
  61. blasint ib = 0, ic = 0;
  62. double temp_r, temp_i;
  63. double *a_ptr = a;
  64. blasint lda2 = 2*lda;
  65. blasint inc_b2 = 2 * inc_b;
  66. blasint inc_c2 = 2 * inc_c;
  67. BLASFUNC(zscal)(&m, beta, c, &inc_c);
  68. for (j = 0; j < n; j++)
  69. {
  70. if (trans == 'N' || trans == 'R') {
  71. temp_r = alpha[0] * b[ib] - alpha[1] * b[ib+1];
  72. temp_i = alpha[0] * b[ib+1] + alpha[1] * b[ib];
  73. } else {
  74. temp_r = alpha[0] * b[ib] + alpha[1] * b[ib+1];
  75. temp_i = alpha[0] * b[ib+1] - alpha[1] * b[ib];
  76. }
  77. ic = 0;
  78. i2 = 0;
  79. for (i = 0; i < m; i++)
  80. {
  81. if (trans == 'N') {
  82. c[ic] += temp_r * a_ptr[i2] - temp_i * a_ptr[i2+1];
  83. c[ic+1] += temp_r * a_ptr[i2+1] + temp_i * a_ptr[i2];
  84. }
  85. if (trans == 'O') {
  86. c[ic] += temp_r * a_ptr[i2] + temp_i * a_ptr[i2+1];
  87. c[ic+1] += temp_r * a_ptr[i2+1] - temp_i * a_ptr[i2];
  88. }
  89. if (trans == 'R') {
  90. c[ic] += temp_r * a_ptr[i2] + temp_i * a_ptr[i2+1];
  91. c[ic+1] -= temp_r * a_ptr[i2+1] - temp_i * a_ptr[i2];
  92. }
  93. if (trans == 'S') {
  94. c[ic] += temp_r * a_ptr[i2] - temp_i * a_ptr[i2+1];
  95. c[ic+1] -= temp_r * a_ptr[i2+1] + temp_i * a_ptr[i2];
  96. }
  97. i2 += 2;
  98. ic += inc_c2;
  99. }
  100. a_ptr += lda2;
  101. ib += inc_b2;
  102. }
  103. }
  104. /**
  105. * Comapare results computed by zgemv and zgemv_n_trusted
  106. *
  107. * param trans specifies whether matris A is conj or/and xconj
  108. * param m - number of rows of A
  109. * param n - number of columns of A
  110. * param alpha - scaling factor for the matrib-vector product
  111. * param lda - leading dimension of matrix A
  112. * param inc_b - stride of vector b
  113. * param beta - scaling factor for vector c
  114. * param inc_c - stride of vector c
  115. * return norm of differences
  116. */
  117. static double check_zgemv_n(char trans, blasint m, blasint n, double *alpha, blasint lda,
  118. blasint inc_b, double *beta, blasint inc_c)
  119. {
  120. blasint i;
  121. drand_generate(data_zgemv_n.a_test, n * lda);
  122. drand_generate(data_zgemv_n.b_test, 2 * n * inc_b);
  123. drand_generate(data_zgemv_n.c_test, 2 * m * inc_c);
  124. for (i = 0; i < m * 2 * inc_c; i++)
  125. data_zgemv_n.c_verify[i] = data_zgemv_n.c_test[i];
  126. zgemv_n_trusted(trans, m, n, alpha, data_zgemv_n.a_test, lda, data_zgemv_n.b_test,
  127. inc_b, beta, data_zgemv_n.c_test, inc_c);
  128. BLASFUNC(zgemv)(&trans, &m, &n, alpha, data_zgemv_n.a_test, &lda, data_zgemv_n.b_test,
  129. &inc_b, beta, data_zgemv_n.c_verify, &inc_c);
  130. for (i = 0; i < m * 2 * inc_c; i++)
  131. data_zgemv_n.c_verify[i] -= data_zgemv_n.c_test[i];
  132. return BLASFUNC(dznrm2)(&n, data_zgemv_n.c_verify, &inc_c);
  133. }
  134. /**
  135. * Test zgemv by comparing it against reference
  136. * with the following options:
  137. *
  138. * A is xconj
  139. * Number of rows and columns of A is 100
  140. * Stride of vector b is 1
  141. * Stride of vector c is 1
  142. */
  143. CTEST(zgemv, trans_o_square_matrix)
  144. {
  145. blasint n = 100, m = 100, lda = 100;
  146. blasint inc_b = 1, inc_c = 1;
  147. char trans = 'O';
  148. double alpha[] = {2.0, -1.0};
  149. double beta[] = {1.4, 5.0};
  150. double norm = check_zgemv_n(trans, m, n, alpha, lda, inc_b, beta, inc_c);
  151. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_TOL);
  152. }
  153. /**
  154. * Test zgemv by comparing it against reference
  155. * with the following options:
  156. *
  157. * A is xconj
  158. * Number of rows of A is 50
  159. * Number of colums of A is 100
  160. * Stride of vector b is 1
  161. * Stride of vector c is 1
  162. */
  163. CTEST(zgemv, trans_o_rectangular_matrix_rows_less_then_cols)
  164. {
  165. blasint n = 100, m = 50, lda = 50;
  166. blasint inc_b = 1, inc_c = 1;
  167. char trans = 'O';
  168. double alpha[] = {2.0, -1.0};
  169. double beta[] = {1.4, 5.0};
  170. double norm = check_zgemv_n(trans, m, n, alpha, lda, inc_b, beta, inc_c);
  171. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_TOL);
  172. }
  173. /**
  174. * Test zgemv by comparing it against reference
  175. * with the following options:
  176. *
  177. * A is xconj
  178. * Number of rows of A is 100
  179. * Number of colums of A is 50
  180. * Stride of vector b is 1
  181. * Stride of vector c is 1
  182. */
  183. CTEST(zgemv, trans_o_rectangular_matrix_cols_less_then_rows)
  184. {
  185. blasint n = 50, m = 100, lda = 100;
  186. blasint inc_b = 1, inc_c = 1;
  187. char trans = 'O';
  188. double alpha[] = {2.0, -1.0};
  189. double beta[] = {1.4, 5.0};
  190. double norm = check_zgemv_n(trans, m, n, alpha, lda, inc_b, beta, inc_c);
  191. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_TOL);
  192. }
  193. /**
  194. * Test zgemv by comparing it against reference
  195. * with the following options:
  196. *
  197. * A is xconj
  198. * Number of rows and columns of A is 100
  199. * Stride of vector b is 2
  200. * Stride of vector c is 2
  201. */
  202. CTEST(zgemv, trans_o_double_strides)
  203. {
  204. blasint n = 100, m = 100, lda = 100;
  205. blasint inc_b = 2, inc_c = 2;
  206. char trans = 'O';
  207. double alpha[] = {2.0, -1.0};
  208. double beta[] = {1.4, 5.0};
  209. double norm = check_zgemv_n(trans, m, n, alpha, lda, inc_b, beta, inc_c);
  210. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_TOL);
  211. }
  212. /**
  213. * Test zgemv by comparing it against reference
  214. * with the following options:
  215. *
  216. * A is xconj and conj
  217. * Number of rows and columns of A is 100
  218. * Stride of vector b is 1
  219. * Stride of vector c is 1
  220. */
  221. CTEST(zgemv, trans_s_square_matrix)
  222. {
  223. blasint n = 100, m = 100, lda = 100;
  224. blasint inc_b = 1, inc_c = 1;
  225. char trans = 'S';
  226. double alpha[] = {1.0, 1.0};
  227. double beta[] = {1.4, 5.0};
  228. double norm = check_zgemv_n(trans, m, n, alpha, lda, inc_b, beta, inc_c);
  229. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_TOL);
  230. }
  231. /**
  232. * Test zgemv by comparing it against reference
  233. * with the following options:
  234. *
  235. * A is xconj and conj
  236. * Number of rows of A is 50
  237. * Number of colums of A is 100
  238. * Stride of vector b is 1
  239. * Stride of vector c is 1
  240. */
  241. CTEST(zgemv, trans_s_rectangular_matrix_rows_less_then_cols)
  242. {
  243. blasint n = 100, m = 50, lda = 50;
  244. blasint inc_b = 1, inc_c = 1;
  245. char trans = 'S';
  246. double alpha[] = {2.0, -1.0};
  247. double beta[] = {1.4, 5.0};
  248. double norm = check_zgemv_n(trans, m, n, alpha, lda, inc_b, beta, inc_c);
  249. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_TOL);
  250. }
  251. /**
  252. * Test zgemv by comparing it against reference
  253. * with the following options:
  254. *
  255. * A is xconj and conj
  256. * Number of rows of A is 100
  257. * Number of colums of A is 50
  258. * Stride of vector b is 1
  259. * Stride of vector c is 1
  260. */
  261. CTEST(zgemv, trans_s_rectangular_matrix_cols_less_then_rows)
  262. {
  263. blasint n = 50, m = 100, lda = 100;
  264. blasint inc_b = 1, inc_c = 1;
  265. char trans = 'S';
  266. double alpha[] = {2.0, -1.0};
  267. double beta[] = {1.4, 0.0};
  268. double norm = check_zgemv_n(trans, m, n, alpha, lda, inc_b, beta, inc_c);
  269. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_TOL);
  270. }
  271. /**
  272. * Test zgemv by comparing it against reference
  273. * with the following options:
  274. *
  275. * A is xconj and conj
  276. * Number of rows and columns of A is 100
  277. * Stride of vector b is 2
  278. * Stride of vector c is 2
  279. */
  280. CTEST(zgemv, trans_s_double_strides)
  281. {
  282. blasint n = 100, m = 100, lda = 100;
  283. blasint inc_b = 2, inc_c = 2;
  284. char trans = 'S';
  285. double alpha[] = {2.0, -1.0};
  286. double beta[] = {1.0, 5.0};
  287. double norm = check_zgemv_n(trans, m, n, alpha, lda, inc_b, beta, inc_c);
  288. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_TOL);
  289. }
  290. #endif