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_cspmv.c 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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_CSPMV {
  33. float a_verify[DATASIZE * DATASIZE * 2];
  34. float a_test[DATASIZE * (DATASIZE + 1)];
  35. float b_test[DATASIZE * 2 * INCREMENT];
  36. float c_test[DATASIZE * 2 * INCREMENT];
  37. float c_verify[DATASIZE * 2 * INCREMENT];
  38. };
  39. #ifdef BUILD_COMPLEX
  40. static struct DATA_CSPMV data_cspmv;
  41. /**
  42. * Compute spmv via gemv since spmv is gemv for symmetric packed matrix
  43. *
  44. * param uplo specifies whether matrix A is upper or lower triangular
  45. * param n - number of rows and columns of A
  46. * param alpha - scaling factor for the matrix-vector product
  47. * param a - buffer holding input matrix A
  48. * param b - Buffer holding input vector b
  49. * param inc_b - stride of vector b
  50. * param beta - scaling factor for vector c
  51. * param c - buffer holding input/output vector c
  52. * param inc_c - stride of vector c
  53. * output param data_cspmv.c_verify - matrix computed by gemv
  54. */
  55. static void cspmv_trusted(char uplo, blasint n, float *alpha, float *a,
  56. float *b, blasint inc_b, float *beta, float *c,
  57. blasint inc_c)
  58. {
  59. blasint k;
  60. blasint i, j;
  61. // param for gemv (can use any, since the input matrix is symmetric)
  62. char trans = 'N';
  63. // Unpack the input symmetric packed matrix
  64. if (uplo == 'L')
  65. {
  66. k = 0;
  67. for (i = 0; i < n; i++)
  68. {
  69. for (j = 0; j < n * 2; j += 2)
  70. {
  71. if (j / 2 < i)
  72. {
  73. data_cspmv.a_verify[i * n * 2 + j] =
  74. data_cspmv.a_verify[j * n + i * 2];
  75. data_cspmv.a_verify[i * n * 2 + j + 1] =
  76. data_cspmv.a_verify[j * n + i * 2 + 1];
  77. }
  78. else
  79. {
  80. data_cspmv.a_verify[i * n * 2 + j] = a[k++];
  81. data_cspmv.a_verify[i * n * 2 + j + 1] = a[k++];
  82. }
  83. }
  84. }
  85. }
  86. else
  87. {
  88. k = n * (n + 1) - 1;
  89. for (j = 2 * n - 1; j >= 0; j -= 2)
  90. {
  91. for (i = n - 1; i >= 0; i--)
  92. {
  93. if (j / 2 < i)
  94. {
  95. data_cspmv.a_verify[i * n * 2 + j] =
  96. data_cspmv.a_verify[(j - 1) * n + i * 2 + 1];
  97. data_cspmv.a_verify[i * n * 2 + j - 1] =
  98. data_cspmv.a_verify[(j - 1) * n + i * 2];
  99. }
  100. else
  101. {
  102. data_cspmv.a_verify[i * n * 2 + j] = a[k--];
  103. data_cspmv.a_verify[i * n * 2 + j - 1] = a[k--];
  104. }
  105. }
  106. }
  107. }
  108. // Run gemv with the unpacked matrix
  109. BLASFUNC(cgemv)(&trans, &n, &n, alpha, data_cspmv.a_verify, &n, b,
  110. &inc_b, beta, data_cspmv.c_verify, &inc_c);
  111. }
  112. /**
  113. * Comapare results computed by cspmv and cspmv_trusted
  114. *
  115. * param uplo specifies whether matrix A is upper or lower triangular
  116. * param n - number of rows and columns of A
  117. * param alpha - scaling factor for the matrix-vector product
  118. * param inc_b - stride of vector b
  119. * param beta - scaling factor for vector c
  120. * param inc_c - stride of vector c
  121. * return norm of differences
  122. */
  123. static float check_cspmv(char uplo, blasint n, float *alpha, blasint inc_b,
  124. float *beta, blasint inc_c)
  125. {
  126. blasint i;
  127. // Fill symmetric packed maxtix a, vectors b and c
  128. srand_generate(data_cspmv.a_test, n * (n + 1));
  129. srand_generate(data_cspmv.b_test, 2 * n * inc_b);
  130. srand_generate(data_cspmv.c_test, 2 * n * inc_c);
  131. // Copy vector c for cspmv_trusted
  132. for (i = 0; i < n * 2 * inc_c; i++)
  133. data_cspmv.c_verify[i] = data_cspmv.c_test[i];
  134. cspmv_trusted(uplo, n, alpha, data_cspmv.a_test, data_cspmv.b_test,
  135. inc_b, beta, data_cspmv.c_verify, inc_c);
  136. BLASFUNC(cspmv)(&uplo, &n, alpha, data_cspmv.a_test, data_cspmv.b_test,
  137. &inc_b, beta, data_cspmv.c_test, &inc_c);
  138. // Find the differences between output vector computed by cspmv and cspmv_trusted
  139. for (i = 0; i < n * 2 * inc_c; i++)
  140. data_cspmv.c_test[i] -= data_cspmv.c_verify[i];
  141. // Find the norm of differences
  142. return BLASFUNC(scnrm2)(&n, data_cspmv.c_test, &inc_c);
  143. }
  144. /**
  145. * Check if error function was called with expected function name
  146. * and param info
  147. *
  148. * param uplo specifies whether matrix A is upper or lower triangular
  149. * param n - number of rows and columns of A
  150. * param inc_b - stride of vector b
  151. * param inc_c - stride of vector c
  152. * param expected_info - expected invalid parameter number in cspmv
  153. * return TRUE if everything is ok, otherwise FALSE
  154. */
  155. static int check_badargs(char uplo, blasint n, blasint inc_b,
  156. blasint inc_c, int expected_info)
  157. {
  158. float alpha[] = {1.0, 1.0};
  159. float beta[] = {0.0, 0.0};
  160. set_xerbla("CSPMV ", expected_info);
  161. BLASFUNC(cspmv)(&uplo, &n, alpha, data_cspmv.a_test, data_cspmv.b_test,
  162. &inc_b, beta, data_cspmv.c_test, &inc_c);
  163. return check_error();
  164. }
  165. /**
  166. * Test cspmv by comparing it against cgemv
  167. * with the following options:
  168. *
  169. * A is upper triangular
  170. * Number of rows and columns of A is 100
  171. * Stride of vector b is 1
  172. * Stride of vector c is 1
  173. */
  174. CTEST(cspmv, upper_inc_b_1_inc_c_1_N_100)
  175. {
  176. blasint N = DATASIZE, inc_b = 1, inc_c = 1;
  177. char uplo = 'U';
  178. float alpha[] = {1.0f, 1.0f};
  179. float beta[] = {0.0f, 0.0f};
  180. float norm = check_cspmv(uplo, N, alpha, inc_b, beta, inc_c);
  181. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_TOL);
  182. }
  183. /**
  184. * Test cspmv by comparing it against cgemv
  185. * with the following options:
  186. *
  187. * A is upper triangular
  188. * Number of rows and columns of A is 100
  189. * Stride of vector b is 1
  190. * Stride of vector c is 2
  191. */
  192. CTEST(cspmv, upper_inc_b_1_inc_c_2_N_100)
  193. {
  194. blasint N = DATASIZE, inc_b = 1, inc_c = 2;
  195. char uplo = 'U';
  196. float alpha[] = {1.0f, 1.0f};
  197. float beta[] = {0.0f, 0.0f};
  198. float norm = check_cspmv(uplo, N, alpha, inc_b, beta, inc_c);
  199. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_TOL);
  200. }
  201. /**
  202. * Test cspmv by comparing it against cgemv
  203. * with the following options:
  204. *
  205. * A is upper triangular
  206. * Number of rows and columns of A is 100
  207. * Stride of vector b is 2
  208. * Stride of vector c is 1
  209. */
  210. CTEST(cspmv, upper_inc_b_2_inc_c_1_N_100)
  211. {
  212. blasint N = DATASIZE, inc_b = 2, inc_c = 1;
  213. char uplo = 'U';
  214. float alpha[] = {1.0f, 0.0f};
  215. float beta[] = {1.0f, 0.0f};
  216. float norm = check_cspmv(uplo, N, alpha, inc_b, beta, inc_c);
  217. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_TOL);
  218. }
  219. /**
  220. * Test cspmv by comparing it against cgemv
  221. * with the following options:
  222. *
  223. * A is upper triangular
  224. * Number of rows and columns of A is 100
  225. * Stride of vector b is 2
  226. * Stride of vector c is 2
  227. */
  228. CTEST(cspmv, upper_inc_b_2_inc_c_2_N_100)
  229. {
  230. blasint N = DATASIZE, inc_b = 2, inc_c = 2;
  231. char uplo = 'U';
  232. float alpha[] = {2.5, -2.1};
  233. float beta[] = {0.0f, 1.0f};
  234. float norm = check_cspmv(uplo, N, alpha, inc_b, beta, inc_c);
  235. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_TOL);
  236. }
  237. /**
  238. * Test cspmv by comparing it against cgemv
  239. * with the following options:
  240. *
  241. * A is lower triangular
  242. * Number of rows and columns of A is 100
  243. * Stride of vector b is 1
  244. * Stride of vector c is 1
  245. */
  246. CTEST(cspmv, lower_inc_b_1_inc_c_1_N_100)
  247. {
  248. blasint N = DATASIZE, inc_b = 1, inc_c = 1;
  249. char uplo = 'L';
  250. float alpha[] = {1.0f, 1.0f};
  251. float beta[] = {0.0f, 0.0f};
  252. float norm = check_cspmv(uplo, N, alpha, inc_b, beta, inc_c);
  253. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_TOL);
  254. }
  255. /**
  256. * Test cspmv by comparing it against cgemv
  257. * with the following options:
  258. *
  259. * A is lower triangular
  260. * Number of rows and columns of A is 100
  261. * Stride of vector b is 1
  262. * Stride of vector c is 2
  263. */
  264. CTEST(cspmv, lower_inc_b_1_inc_c_2_N_100)
  265. {
  266. blasint N = DATASIZE, inc_b = 1, inc_c = 2;
  267. char uplo = 'L';
  268. float alpha[] = {1.0f, 1.0f};
  269. float beta[] = {0.0f, 0.0f};
  270. float norm = check_cspmv(uplo, N, alpha, inc_b, beta, inc_c);
  271. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_TOL);
  272. }
  273. /**
  274. * Test cspmv by comparing it against cgemv
  275. * with the following options:
  276. *
  277. * A is lower triangular
  278. * Number of rows and columns of A is 100
  279. * Stride of vector b is 2
  280. * Stride of vector c is 1
  281. */
  282. CTEST(cspmv, lower_inc_b_2_inc_c_1_N_100)
  283. {
  284. blasint N = DATASIZE, inc_b = 2, inc_c = 1;
  285. char uplo = 'L';
  286. float alpha[] = {1.0f, 0.0f};
  287. float beta[] = {1.0f, 0.0f};
  288. float norm = check_cspmv(uplo, N, alpha, inc_b, beta, inc_c);
  289. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_TOL);
  290. }
  291. /**
  292. * Test cspmv by comparing it against cgemv
  293. * with the following options:
  294. *
  295. * A is lower triangular
  296. * Number of rows and columns of A is 100
  297. * Stride of vector b is 2
  298. * Stride of vector c is 2
  299. */
  300. CTEST(cspmv, lower_inc_b_2_inc_c_2_N_100)
  301. {
  302. blasint N = DATASIZE, inc_b = 2, inc_c = 2;
  303. char uplo = 'L';
  304. float alpha[] = {2.5, -2.1};
  305. float beta[] = {0.0f, 1.0f};
  306. float norm = check_cspmv(uplo, N, alpha, inc_b, beta, inc_c);
  307. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_TOL);
  308. }
  309. /**
  310. * Check if output matrix A contains any NaNs
  311. */
  312. CTEST(cspmv, check_for_NaN)
  313. {
  314. blasint N = DATASIZE, inc_b = 1, inc_c = 1;
  315. char uplo = 'U';
  316. float alpha[] = {1.0f, 1.0f};
  317. float beta[] = {0.0f, 0.0f};
  318. float norm = check_cspmv(uplo, N, alpha, inc_b, beta, inc_c);
  319. ASSERT_TRUE(norm == norm); /* NaN == NaN is false */
  320. }
  321. /**
  322. * Test error function for an invalid param uplo.
  323. * uplo specifies whether A is upper or lower triangular.
  324. */
  325. CTEST(cspmv, xerbla_uplo_invalid)
  326. {
  327. blasint N = DATASIZE, inc_b = 1, inc_c = 1;
  328. char uplo = 'O';
  329. int expected_info = 1;
  330. int passed = check_badargs(uplo, N, inc_b, inc_c, expected_info);
  331. ASSERT_EQUAL(TRUE, passed);
  332. }
  333. /**
  334. * Test error function for an invalid param N -
  335. * number of rows and columns of A. Must be at least zero.
  336. */
  337. CTEST(cspmv, xerbla_N_invalid)
  338. {
  339. blasint N = INVALID, inc_b = 1, inc_c = 1;
  340. char uplo = 'U';
  341. int expected_info = 2;
  342. int passed = check_badargs(uplo, N, inc_b, inc_c, expected_info);
  343. ASSERT_EQUAL(TRUE, passed);
  344. }
  345. /**
  346. * Test error function for an invalid param inc_b -
  347. * stride of vector b. Can't be zero.
  348. */
  349. CTEST(cspmv, xerbla_inc_b_zero)
  350. {
  351. blasint N = DATASIZE, inc_b = 0, inc_c = 1;
  352. char uplo = 'U';
  353. int expected_info = 6;
  354. int passed = check_badargs(uplo, N, inc_b, inc_c, expected_info);
  355. ASSERT_EQUAL(TRUE, passed);
  356. }
  357. /**
  358. * Test error function for an invalid param inc_c -
  359. * stride of vector c. Can't be zero.
  360. */
  361. CTEST(cspmv, xerbla_inc_c_zero)
  362. {
  363. blasint N = DATASIZE, inc_b = 1, inc_c = 0;
  364. char uplo = 'U';
  365. int expected_info = 9;
  366. int passed = check_badargs(uplo, N, inc_b, inc_c, expected_info);
  367. ASSERT_EQUAL(TRUE, passed);
  368. }
  369. #endif