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_somatcopy.c 17 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. struct DATA_SOMATCOPY {
  32. float a_test[DATASIZE * DATASIZE];
  33. float b_test[DATASIZE * DATASIZE];
  34. float b_verify[DATASIZE * DATASIZE];
  35. };
  36. #ifdef BUILD_SINGLE
  37. static struct DATA_SOMATCOPY data_somatcopy;
  38. /**
  39. * Comapare results computed by somatcopy and reference func
  40. *
  41. * param api specifies tested api (C or Fortran)
  42. * param order specifies row or column major order
  43. * param trans specifies op(A), the transposition operation
  44. * applied to the matrix A
  45. * param rows - number of rows of A
  46. * param cols - number of columns of A
  47. * param alpha - scaling factor for matrix B
  48. * param lda - leading dimension of the matrix A
  49. * param ldb - leading dimension of the matrix B
  50. * return norm of difference between openblas and reference func
  51. */
  52. static float check_somatcopy(char api, char order, char trans, blasint rows, blasint cols, float alpha,
  53. blasint lda, blasint ldb)
  54. {
  55. blasint b_rows, b_cols;
  56. blasint m, n;
  57. enum CBLAS_ORDER corder;
  58. enum CBLAS_TRANSPOSE ctrans;
  59. if (order == 'C') {
  60. m = cols; n = rows;
  61. }
  62. else {
  63. m = rows; n = cols;
  64. }
  65. if(trans == 'T' || trans == 'C') {
  66. b_rows = n; b_cols = m;
  67. }
  68. else {
  69. b_rows = m; b_cols = n;
  70. }
  71. srand_generate(data_somatcopy.a_test, lda*m);
  72. if (trans == 'T' || trans == 'C') {
  73. stranspose(m, n, alpha, data_somatcopy.a_test, lda, data_somatcopy.b_verify, ldb);
  74. }
  75. else {
  76. scopy(m, n, alpha, data_somatcopy.a_test, lda, data_somatcopy.b_verify, ldb);
  77. }
  78. if (api == 'F') {
  79. BLASFUNC(somatcopy)(&order, &trans, &rows, &cols, &alpha, data_somatcopy.a_test,
  80. &lda, data_somatcopy.b_test, &ldb);
  81. }
  82. #ifndef NO_CBLAS
  83. else {
  84. if (order == 'C') corder = CblasColMajor;
  85. if (order == 'R') corder = CblasRowMajor;
  86. if (trans == 'T') ctrans = CblasTrans;
  87. if (trans == 'N') ctrans = CblasNoTrans;
  88. if (trans == 'C') ctrans = CblasConjTrans;
  89. if (trans == 'R') ctrans = CblasConjNoTrans;
  90. cblas_somatcopy(corder, ctrans, rows, cols, alpha, data_somatcopy.a_test,
  91. lda, data_somatcopy.b_test, ldb);
  92. }
  93. #endif
  94. return smatrix_difference(data_somatcopy.b_test, data_somatcopy.b_verify, b_cols, b_rows, ldb);
  95. }
  96. /**
  97. * Check if error function was called with expected function name
  98. * and param info
  99. *
  100. * param order specifies row or column major order
  101. * param trans specifies op(A), the transposition operation
  102. * applied to the matrix A
  103. * param rows - number of rows of A
  104. * param cols - number of columns of A
  105. * param lda - leading dimension of the matrix A
  106. * param ldb - leading dimension of the matrix B
  107. * param expected_info - expected invalid parameter number
  108. * return TRUE if everything is ok, otherwise FALSE
  109. */
  110. static int check_badargs(char order, char trans, blasint rows, blasint cols,
  111. blasint lda, blasint ldb, int expected_info)
  112. {
  113. float alpha = 1.0;
  114. set_xerbla("SOMATCOPY", expected_info);
  115. BLASFUNC(somatcopy)(&order, &trans, &rows, &cols, &alpha, data_somatcopy.a_test,
  116. &lda, data_somatcopy.b_test, &ldb);
  117. return check_error();
  118. }
  119. /**
  120. * Fortran API specific test
  121. * Test somatcopy by comparing it against refernce
  122. * with the following options:
  123. *
  124. * Column Major
  125. * Transposition
  126. * Square matrix
  127. * alpha = 1.0
  128. */
  129. CTEST(somatcopy, colmajor_trans_col_100_row_100)
  130. {
  131. blasint m = 100, n = 100;
  132. blasint lda = 100, ldb = 100;
  133. char order = 'C';
  134. char trans = 'T';
  135. float alpha = 1.0f;
  136. float norm = check_somatcopy('F', order, trans, m, n, alpha, lda, ldb);
  137. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  138. }
  139. /**
  140. * Fortran API specific test
  141. * Test somatcopy by comparing it against refernce
  142. * with the following options:
  143. *
  144. * Column Major
  145. * Copy only
  146. * Square matrix
  147. * alpha = 1.0
  148. */
  149. CTEST(somatcopy, colmajor_notrans_col_100_row_100)
  150. {
  151. blasint m = 100, n = 100;
  152. blasint lda = 100, ldb = 100;
  153. char order = 'C';
  154. char trans = 'N';
  155. float alpha = 1.0f;
  156. float norm = check_somatcopy('F', order, trans, m, n, alpha, lda, ldb);
  157. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  158. }
  159. /**
  160. * Fortran API specific test
  161. * Test somatcopy by comparing it against refernce
  162. * with the following options:
  163. *
  164. * Column Major
  165. * Transposition
  166. * Rectangular matrix
  167. * alpha = 2.0
  168. */
  169. CTEST(somatcopy, colmajor_trans_col_50_row_100)
  170. {
  171. blasint m = 100, n = 50;
  172. blasint lda = 100, ldb = 50;
  173. char order = 'C';
  174. char trans = 'T';
  175. float alpha = 2.0f;
  176. float norm = check_somatcopy('F', order, trans, m, n, alpha, lda, ldb);
  177. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  178. }
  179. /**
  180. * Fortran API specific test
  181. * Test somatcopy by comparing it against refernce
  182. * with the following options:
  183. *
  184. * Column Major
  185. * Copy only
  186. * Rectangular matrix
  187. * alpha = 2.0
  188. */
  189. CTEST(somatcopy, colmajor_notrans_col_50_row_100)
  190. {
  191. blasint m = 100, n = 50;
  192. blasint lda = 100, ldb = 100;
  193. char order = 'C';
  194. char trans = 'N';
  195. float alpha = 2.0f;
  196. float norm = check_somatcopy('F', order, trans, m, n, alpha, lda, ldb);
  197. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  198. }
  199. /**
  200. * Fortran API specific test
  201. * Test somatcopy by comparing it against refernce
  202. * with the following options:
  203. *
  204. * Column Major
  205. * Transposition
  206. * Rectangular matrix
  207. * alpha = 0.0
  208. */
  209. CTEST(somatcopy, colmajor_trans_col_100_row_50)
  210. {
  211. blasint m = 50, n = 100;
  212. blasint lda = 50, ldb = 100;
  213. char order = 'C';
  214. char trans = 'T';
  215. float alpha = 0.0f;
  216. float norm = check_somatcopy('F', order, trans, m, n, alpha, lda, ldb);
  217. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  218. }
  219. /**
  220. * Fortran API specific test
  221. * Test somatcopy by comparing it against refernce
  222. * with the following options:
  223. *
  224. * Column Major
  225. * Copy only
  226. * Rectangular matrix
  227. * alpha = 0.0
  228. */
  229. CTEST(somatcopy, colmajor_notrans_col_100_row_50)
  230. {
  231. blasint m = 50, n = 100;
  232. blasint lda = 50, ldb = 50;
  233. char order = 'C';
  234. char trans = 'N';
  235. float alpha = 0.0f;
  236. float norm = check_somatcopy('F', order, trans, m, n, alpha, lda, ldb);
  237. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  238. }
  239. /**
  240. * Fortran API specific test
  241. * Test somatcopy by comparing it against refernce
  242. * with the following options:
  243. *
  244. * Row Major
  245. * Transposition
  246. * Square matrix
  247. * alpha = 1.0
  248. */
  249. CTEST(somatcopy, rowmajor_trans_col_100_row_100)
  250. {
  251. blasint m = 100, n = 100;
  252. blasint lda = 100, ldb = 100;
  253. char order = 'R';
  254. char trans = 'T';
  255. float alpha = 1.0f;
  256. float norm = check_somatcopy('F', order, trans, m, n, alpha, lda, ldb);
  257. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  258. }
  259. /**
  260. * Fortran API specific test
  261. * Test somatcopy by comparing it against refernce
  262. * with the following options:
  263. *
  264. * Row Major
  265. * Copy only
  266. * Square matrix
  267. * alpha = 1.0
  268. */
  269. CTEST(somatcopy, rowmajor_notrans_col_100_row_100)
  270. {
  271. blasint m = 100, n = 100;
  272. blasint lda = 100, ldb = 100;
  273. char order = 'R';
  274. char trans = 'N';
  275. float alpha = 1.0f;
  276. float norm = check_somatcopy('F', order, trans, m, n, alpha, lda, ldb);
  277. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  278. }
  279. /**
  280. * Fortran API specific test
  281. * Test somatcopy by comparing it against refernce
  282. * with the following options:
  283. *
  284. * Row Major
  285. * Transposition
  286. * Rectangular matrix
  287. * alpha = 2.0
  288. */
  289. CTEST(somatcopy, rowmajor_conjtrans_col_100_row_50)
  290. {
  291. blasint m = 50, n = 100;
  292. blasint lda = 100, ldb = 50;
  293. char order = 'R';
  294. char trans = 'C'; // same as trans for real matrix
  295. float alpha = 2.0f;
  296. float norm = check_somatcopy('F', order, trans, m, n, alpha, lda, ldb);
  297. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  298. }
  299. /**
  300. * Fortran API specific test
  301. * Test somatcopy by comparing it against refernce
  302. * with the following options:
  303. *
  304. * Row Major
  305. * Copy only
  306. * Rectangular matrix
  307. * alpha = 2.0
  308. */
  309. CTEST(somatcopy, rowmajor_notrans_col_50_row_100)
  310. {
  311. blasint m = 100, n = 50;
  312. blasint lda = 50, ldb = 50;
  313. char order = 'R';
  314. char trans = 'N';
  315. float alpha = 2.0f;
  316. float norm = check_somatcopy('F', order, trans, m, n, alpha, lda, ldb);
  317. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  318. }
  319. /**
  320. * Fortran API specific test
  321. * Test somatcopy by comparing it against refernce
  322. * with the following options:
  323. *
  324. * Row Major
  325. * Transposition
  326. * Matrix dimensions leave residues from 4 and 2 (specialize
  327. * for rt case)
  328. * alpha = 1.5
  329. */
  330. CTEST(somatcopy, rowmajor_trans_col_27_row_27)
  331. {
  332. blasint m = 27, n = 27;
  333. blasint lda = 27, ldb = 27;
  334. char order = 'R';
  335. char trans = 'T';
  336. float alpha = 1.5f;
  337. float norm = check_somatcopy('F', order, trans, m, n, alpha, lda, ldb);
  338. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  339. }
  340. /**
  341. * Fortran API specific test
  342. * Test somatcopy by comparing it against refernce
  343. * with the following options:
  344. *
  345. * Row Major
  346. * Copy only
  347. * Rectangular matrix
  348. * alpha = 0.0
  349. */
  350. CTEST(somatcopy, rowmajor_notrans_col_100_row_50)
  351. {
  352. blasint m = 50, n = 100;
  353. blasint lda = 100, ldb = 100;
  354. char order = 'R';
  355. char trans = 'N';
  356. float alpha = 0.0f;
  357. float norm = check_somatcopy('F', order, trans, m, n, alpha, lda, ldb);
  358. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  359. }
  360. #ifndef NO_CBLAS
  361. /**
  362. * C API specific test
  363. * Test somatcopy by comparing it against refernce
  364. * with the following options:
  365. *
  366. * Column Major
  367. * Transposition
  368. * Square matrix
  369. * alpha = 1.0
  370. */
  371. CTEST(somatcopy, c_api_colmajor_trans_col_100_row_100)
  372. {
  373. blasint m = 100, n = 100;
  374. blasint lda = 100, ldb = 100;
  375. char order = 'C';
  376. char trans = 'T';
  377. float alpha = 1.0f;
  378. float norm = check_somatcopy('C', order, trans, m, n, alpha, lda, ldb);
  379. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  380. }
  381. /**
  382. * C API specific test
  383. * Test somatcopy by comparing it against refernce
  384. * with the following options:
  385. *
  386. * Column Major
  387. * Copy only
  388. * Square matrix
  389. * alpha = 1.0
  390. */
  391. CTEST(somatcopy, c_api_colmajor_notrans_col_100_row_100)
  392. {
  393. blasint m = 100, n = 100;
  394. blasint lda = 100, ldb = 100;
  395. char order = 'C';
  396. char trans = 'N';
  397. float alpha = 1.0f;
  398. float norm = check_somatcopy('C', order, trans, m, n, alpha, lda, ldb);
  399. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  400. }
  401. /**
  402. * C API specific test
  403. * Test somatcopy by comparing it against refernce
  404. * with the following options:
  405. *
  406. * Row Major
  407. * Transposition
  408. * Square matrix
  409. * alpha = 1.0
  410. */
  411. CTEST(somatcopy, c_api_rowmajor_trans_col_100_row_100)
  412. {
  413. blasint m = 100, n = 100;
  414. blasint lda = 100, ldb = 100;
  415. char order = 'R';
  416. char trans = 'T';
  417. float alpha = 1.0f;
  418. float norm = check_somatcopy('C', order, trans, m, n, alpha, lda, ldb);
  419. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  420. }
  421. /**
  422. * C API specific test
  423. * Test somatcopy by comparing it against refernce
  424. * with the following options:
  425. *
  426. * Row Major
  427. * Copy only
  428. * Square matrix
  429. * alpha = 1.0
  430. */
  431. CTEST(somatcopy, c_api_rowmajor_notrans_col_100_row_100)
  432. {
  433. blasint m = 100, n = 100;
  434. blasint lda = 100, ldb = 100;
  435. char order = 'R';
  436. char trans = 'N';
  437. float alpha = 1.0f;
  438. float norm = check_somatcopy('C', order, trans, m, n, alpha, lda, ldb);
  439. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  440. }
  441. #endif
  442. /**
  443. * Test error function for an invalid param order.
  444. * Must be column (C) or row major (R).
  445. */
  446. CTEST(somatcopy, xerbla_invalid_order)
  447. {
  448. blasint m = 100, n = 100;
  449. blasint lda = 100, ldb = 100;
  450. char order = 'O';
  451. char trans = 'T';
  452. int expected_info = 1;
  453. int passed = check_badargs(order, trans, m, n, lda, ldb, expected_info);
  454. ASSERT_EQUAL(TRUE, passed);
  455. }
  456. /**
  457. * Test error function for an invalid param trans.
  458. * Must be trans (T/C) or no-trans (N/R).
  459. */
  460. CTEST(somatcopy, xerbla_invalid_trans)
  461. {
  462. blasint m = 100, n = 100;
  463. blasint lda = 100, ldb = 100;
  464. char order = 'C';
  465. char trans = 'O';
  466. int expected_info = 2;
  467. int passed = check_badargs(order, trans, m, n, lda, ldb, expected_info);
  468. ASSERT_EQUAL(TRUE, passed);
  469. }
  470. /**
  471. * Test error function for an invalid param lda.
  472. * If matrices are stored using row major layout,
  473. * lda must be at least n.
  474. */
  475. CTEST(somatcopy, xerbla_rowmajor_invalid_lda)
  476. {
  477. blasint m = 50, n = 100;
  478. blasint lda = 50, ldb = 100;
  479. char order = 'R';
  480. char trans = 'T';
  481. int expected_info = 7;
  482. int passed = check_badargs(order, trans, m, n, lda, ldb, expected_info);
  483. ASSERT_EQUAL(TRUE, passed);
  484. }
  485. /**
  486. * Test error function for an invalid param lda.
  487. * If matrices are stored using column major layout,
  488. * lda must be at least m.
  489. */
  490. CTEST(somatcopy, xerbla_colmajor_invalid_lda)
  491. {
  492. blasint m = 100, n = 50;
  493. blasint lda = 50, ldb = 100;
  494. char order = 'C';
  495. char trans = 'T';
  496. int expected_info = 7;
  497. int passed = check_badargs(order, trans, m, n, lda, ldb, expected_info);
  498. ASSERT_EQUAL(TRUE, passed);
  499. }
  500. /**
  501. * Test error function for an invalid param ldb.
  502. * If matrices are stored using row major layout and
  503. * there is no transposition, ldb must be at least n.
  504. */
  505. CTEST(somatcopy, xerbla_rowmajor_notrans_invalid_ldb)
  506. {
  507. blasint m = 50, n = 100;
  508. blasint lda = 100, ldb = 50;
  509. char order = 'R';
  510. char trans = 'N';
  511. int expected_info = 9;
  512. int passed = check_badargs(order, trans, m, n, lda, ldb, expected_info);
  513. ASSERT_EQUAL(TRUE, passed);
  514. }
  515. /**
  516. * Test error function for an invalid param ldb.
  517. * If matrices are stored using row major layout and
  518. * there is transposition, ldb must be at least m.
  519. */
  520. CTEST(somatcopy, xerbla_rowmajor_trans_invalid_ldb)
  521. {
  522. blasint m = 100, n = 50;
  523. blasint lda = 100, ldb = 50;
  524. char order = 'R';
  525. char trans = 'T';
  526. int expected_info = 9;
  527. int passed = check_badargs(order, trans, m, n, lda, ldb, expected_info);
  528. ASSERT_EQUAL(TRUE, passed);
  529. }
  530. /**
  531. * Test error function for an invalid param ldb.
  532. * If matrices are stored using column major layout and
  533. * there is no transposition, ldb must be at least m.
  534. */
  535. CTEST(somatcopy, xerbla_colmajor_notrans_invalid_ldb)
  536. {
  537. blasint m = 100, n = 50;
  538. blasint lda = 100, ldb = 50;
  539. char order = 'C';
  540. char trans = 'N';
  541. int expected_info = 9;
  542. int passed = check_badargs(order, trans, m, n, lda, ldb, expected_info);
  543. ASSERT_EQUAL(TRUE, passed);
  544. }
  545. /**
  546. * Test error function for an invalid param ldb.
  547. * If matrices are stored using column major layout and
  548. * there is transposition, ldb must be at least n.
  549. */
  550. CTEST(somatcopy, xerbla_colmajor_trans_invalid_ldb)
  551. {
  552. blasint m = 50, n = 100;
  553. blasint lda = 100, ldb = 50;
  554. char order = 'C';
  555. char trans = 'T';
  556. int expected_info = 9;
  557. int passed = check_badargs(order, trans, m, n, lda, ldb, expected_info);
  558. ASSERT_EQUAL(TRUE, passed);
  559. }
  560. #endif