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_cgbmv.c 8.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 1
  32. struct DATA_CGBMV {
  33. float a_test[DATASIZE * DATASIZE * 2];
  34. float a_band_storage[DATASIZE * DATASIZE * 2];
  35. float matrix[DATASIZE * DATASIZE * 2];
  36. float b_test[DATASIZE * 2 * INCREMENT];
  37. float c_test[DATASIZE * 2 * INCREMENT];
  38. float c_verify[DATASIZE * 2 * INCREMENT];
  39. };
  40. #ifdef BUILD_COMPLEX
  41. static struct DATA_CGBMV data_cgbmv;
  42. /**
  43. * Transform full-storage band matrix A to band-packed storage mode.
  44. *
  45. * param m - number of rows of A
  46. * param n - number of columns of A
  47. * param kl - number of sub-diagonals of the matrix A
  48. * param ku - number of super-diagonals of the matrix A
  49. * output param a - buffer for holding band-packed matrix
  50. * param lda - specifies the leading dimension of a
  51. * param matrix - buffer holding full-storage band matrix A
  52. * param ldm - specifies the leading full-storage band matrix A
  53. */
  54. static void transform_to_band_storage(blasint m, blasint n, blasint kl,
  55. blasint ku, float* a, blasint lda,
  56. float* matrix, blasint ldm)
  57. {
  58. blasint i, j, k;
  59. for (j = 0; j < n; j++)
  60. {
  61. k = 2 * (ku - j);
  62. for (i = MAX(0, 2*(j - ku)); i < MIN(m, j + kl + 1) * 2; i+=2)
  63. {
  64. a[(k + i) + j * lda * 2] = matrix[i + j * ldm * 2];
  65. a[(k + i) + j * lda * 2 + 1] = matrix[i + j * ldm * 2 + 1];
  66. }
  67. }
  68. }
  69. /**
  70. * Generate full-storage band matrix A with kl sub-diagonals and ku super-diagonals
  71. *
  72. * param m - number of rows of A
  73. * param n - number of columns of A
  74. * param kl - number of sub-diagonals of the matrix A
  75. * param ku - number of super-diagonals of the matrix A
  76. * output param band_matrix - buffer for full-storage band matrix.
  77. * param matrix - buffer holding input general matrix
  78. * param ldm - specifies the leading of input general matrix
  79. */
  80. static void get_band_matrix(blasint m, blasint n, blasint kl, blasint ku,
  81. float *band_matrix, float *matrix, blasint ldm)
  82. {
  83. blasint i, j;
  84. blasint k = 0;
  85. for (i = 0; i < n; i++)
  86. {
  87. for (j = 0; j < m * 2; j += 2)
  88. {
  89. if ((blasint)(j/2) > kl + i || i > ku + (blasint)(j/2))
  90. {
  91. band_matrix[i * ldm * 2 + j] = 0.0f;
  92. band_matrix[i * ldm * 2 + j + 1] = 0.0f;
  93. continue;
  94. }
  95. band_matrix[i * ldm * 2 + j] = matrix[k++];
  96. band_matrix[i * ldm * 2 + j + 1] = matrix[k++];
  97. }
  98. }
  99. }
  100. /**
  101. * Comapare results computed by cgbmv and cgemv
  102. * since gbmv is gemv for band matrix
  103. *
  104. * param trans specifies op(A), the transposition operation applied to A
  105. * param m - number of rows of A
  106. * param n - number of columns of A
  107. * param kl - number of sub-diagonals of the matrix A
  108. * param ku - number of super-diagonals of the matrix A
  109. * param alpha - scaling factor for the matrix-vector product
  110. * param lda - specifies the leading dimension of a
  111. * param inc_b - stride of vector b
  112. * param beta - scaling factor for vector c
  113. * param inc_c - stride of vector c
  114. * return norm of differences
  115. */
  116. static float check_cgbmv(char trans, blasint m, blasint n, blasint kl, blasint ku,
  117. float *alpha, blasint lda, blasint inc_b, float *beta, blasint inc_c)
  118. {
  119. blasint i;
  120. blasint lenb, lenc;
  121. if(trans == 'T' || trans == 'C' || trans == 'D' || trans == 'U'){
  122. lenb = m;
  123. lenc = n;
  124. } else {
  125. lenb = n;
  126. lenc = m;
  127. }
  128. srand_generate(data_cgbmv.matrix, m * n * 2);
  129. srand_generate(data_cgbmv.b_test, 2 * (1 + (lenb - 1) * inc_b));
  130. srand_generate(data_cgbmv.c_test, 2 * (1 + (lenc - 1) * inc_c));
  131. for (i = 0; i < 2 * (1 + (lenc - 1) * inc_c); i++)
  132. data_cgbmv.c_verify[i] = data_cgbmv.c_test[i];
  133. get_band_matrix(m, n, kl, ku, data_cgbmv.a_test, data_cgbmv.matrix, m);
  134. transform_to_band_storage(m, n, kl, ku, data_cgbmv.a_band_storage, lda, data_cgbmv.a_test, m);
  135. BLASFUNC(cgemv)(&trans, &m, &n, alpha, data_cgbmv.a_test, &m, data_cgbmv.b_test,
  136. &inc_b, beta, data_cgbmv.c_verify, &inc_c);
  137. BLASFUNC(cgbmv)(&trans, &m, &n, &kl, &ku, alpha, data_cgbmv.a_band_storage, &lda, data_cgbmv.b_test,
  138. &inc_b, beta, data_cgbmv.c_test, &inc_c);
  139. for (i = 0; i < 2 * (1 + (lenc - 1) * inc_c); i++)
  140. data_cgbmv.c_verify[i] -= data_cgbmv.c_test[i];
  141. return BLASFUNC(scnrm2)(&lenc, data_cgbmv.c_verify, &inc_c);
  142. }
  143. /**
  144. * Test cgbmv by comparing it against cgemv
  145. * with param trans is D
  146. */
  147. CTEST(cgbmv, trans_D)
  148. {
  149. blasint m = 50, n = 25;
  150. blasint inc_b = 1, inc_c = 1;
  151. blasint kl = 20, ku = 11;
  152. blasint lda = 50;
  153. char trans = 'D';
  154. float alpha[] = {7.0f, 1.0f};
  155. float beta[] = {1.5f, -1.5f};
  156. float norm = check_cgbmv(trans, m, n, kl, ku, alpha, lda, inc_b, beta, inc_c);
  157. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_TOL);
  158. }
  159. /**
  160. * Test cgbmv by comparing it against cgemv
  161. * with param trans is O
  162. */
  163. CTEST(cgbmv, trans_O)
  164. {
  165. blasint m = 50, n = 25;
  166. blasint inc_b = 1, inc_c = 1;
  167. blasint kl = 20, ku = 10;
  168. blasint lda = 50;
  169. char trans = 'O';
  170. float alpha[] = {7.0f, 1.0f};
  171. float beta[] = {1.5f, -1.5f};
  172. float norm = check_cgbmv(trans, m, n, kl, ku, alpha, lda, inc_b, beta, inc_c);
  173. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_TOL);
  174. }
  175. /**
  176. * Test cgbmv by comparing it against cgemv
  177. * with param trans is S
  178. */
  179. CTEST(cgbmv, trans_S)
  180. {
  181. blasint m = 50, n = 25;
  182. blasint inc_b = 1, inc_c = 1;
  183. blasint kl = 6, ku = 9;
  184. blasint lda = 50;
  185. char trans = 'S';
  186. float alpha[] = {7.0f, 1.0f};
  187. float beta[] = {1.5f, -1.5f};
  188. float norm = check_cgbmv(trans, m, n, kl, ku, alpha, lda, inc_b, beta, inc_c);
  189. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_TOL);
  190. }
  191. /**
  192. * Test cgbmv by comparing it against cgemv
  193. * with param trans is U
  194. */
  195. CTEST(cgbmv, trans_U)
  196. {
  197. blasint m = 25, n = 50;
  198. blasint inc_b = 1, inc_c = 1;
  199. blasint kl = 7, ku = 11;
  200. blasint lda = kl + ku + 1;
  201. char trans = 'U';
  202. float alpha[] = {7.0f, 1.0f};
  203. float beta[] = {1.5f, -1.5f};
  204. float norm = check_cgbmv(trans, m, n, kl, ku, alpha, lda, inc_b, beta, inc_c);
  205. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_TOL);
  206. }
  207. /**
  208. * Test cgbmv by comparing it against cgemv
  209. * with param trans is C
  210. */
  211. CTEST(cgbmv, trans_C)
  212. {
  213. blasint m = 50, n = 25;
  214. blasint inc_b = 1, inc_c = 1;
  215. blasint kl = 20, ku = 11;
  216. blasint lda = 50;
  217. char trans = 'C';
  218. float alpha[] = {7.0f, 1.0f};
  219. float beta[] = {1.5f, -1.5f};
  220. float norm = check_cgbmv(trans, m, n, kl, ku, alpha, lda, inc_b, beta, inc_c);
  221. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_TOL);
  222. }
  223. /**
  224. * Test cgbmv by comparing it against cgemv
  225. * with param trans is R
  226. */
  227. CTEST(cgbmv, trans_R)
  228. {
  229. blasint m = 50, n = 100;
  230. blasint inc_b = 1, inc_c = 1;
  231. blasint kl = 20, ku = 11;
  232. blasint lda = 50;
  233. char trans = 'R';
  234. float alpha[] = {7.0f, 1.0f};
  235. float beta[] = {1.5f, -1.5f};
  236. float norm = check_cgbmv(trans, m, n, kl, ku, alpha, lda, inc_b, beta, inc_c);
  237. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_TOL);
  238. }
  239. #endif