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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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_ZGBMV {
  33. double a_test[DATASIZE * DATASIZE * 2];
  34. double a_band_storage[DATASIZE * DATASIZE * 2];
  35. double matrix[DATASIZE * DATASIZE * 2];
  36. double b_test[DATASIZE * 2 * INCREMENT];
  37. double c_test[DATASIZE * 2 * INCREMENT];
  38. double c_verify[DATASIZE * 2 * INCREMENT];
  39. };
  40. #ifdef BUILD_COMPLEX16
  41. static struct DATA_ZGBMV data_zgbmv;
  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, double* a, blasint lda,
  56. double* 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. double *band_matrix, double *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.0;
  92. band_matrix[i * ldm * 2 + j + 1] = 0.0;
  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 zgbmv and zgemv
  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 double check_zgbmv(char trans, blasint m, blasint n, blasint kl, blasint ku,
  117. double *alpha, blasint lda, blasint inc_b, double *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. drand_generate(data_zgbmv.matrix, m * n * 2);
  129. drand_generate(data_zgbmv.b_test, 2 * (1 + (lenb - 1) * inc_b));
  130. drand_generate(data_zgbmv.c_test, 2 * (1 + (lenc - 1) * inc_c));
  131. for (i = 0; i < 2 * (1 + (lenc - 1) * inc_c); i++)
  132. data_zgbmv.c_verify[i] = data_zgbmv.c_test[i];
  133. get_band_matrix(m, n, kl, ku, data_zgbmv.a_test, data_zgbmv.matrix, m);
  134. transform_to_band_storage(m, n, kl, ku, data_zgbmv.a_band_storage, lda, data_zgbmv.a_test, m);
  135. BLASFUNC(zgemv)(&trans, &m, &n, alpha, data_zgbmv.a_test, &m, data_zgbmv.b_test,
  136. &inc_b, beta, data_zgbmv.c_verify, &inc_c);
  137. BLASFUNC(zgbmv)(&trans, &m, &n, &kl, &ku, alpha, data_zgbmv.a_band_storage, &lda, data_zgbmv.b_test,
  138. &inc_b, beta, data_zgbmv.c_test, &inc_c);
  139. for (i = 0; i < 2 * (1 + (lenc - 1) * inc_c); i++)
  140. data_zgbmv.c_verify[i] -= data_zgbmv.c_test[i];
  141. return BLASFUNC(dznrm2)(&lenc, data_zgbmv.c_verify, &inc_c);
  142. }
  143. /**
  144. * Test zgbmv by comparing it against zgemv
  145. * with param trans is D
  146. */
  147. CTEST(zgbmv, 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. double alpha[] = {7.0, 1.0};
  155. double beta[] = {1.5, -1.5};
  156. double norm = check_zgbmv(trans, m, n, kl, ku, alpha, lda, inc_b, beta, inc_c);
  157. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_TOL);
  158. }
  159. /**
  160. * Test zgbmv by comparing it against zgemv
  161. * with param trans is O
  162. */
  163. CTEST(zgbmv, 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. double alpha[] = {7.0, 1.0};
  171. double beta[] = {1.5, -1.5};
  172. double norm = check_zgbmv(trans, m, n, kl, ku, alpha, lda, inc_b, beta, inc_c);
  173. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_TOL);
  174. }
  175. /**
  176. * Test zgbmv by comparing it against zgemv
  177. * with param trans is S
  178. */
  179. CTEST(zgbmv, 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. double alpha[] = {7.0, 1.0};
  187. double beta[] = {1.5, -1.5};
  188. double norm = check_zgbmv(trans, m, n, kl, ku, alpha, lda, inc_b, beta, inc_c);
  189. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_TOL);
  190. }
  191. /**
  192. * Test zgbmv by comparing it against zgemv
  193. * with param trans is U
  194. */
  195. CTEST(zgbmv, 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. double alpha[] = {7.0, 1.0};
  203. double beta[] = {1.5, -1.5};
  204. double norm = check_zgbmv(trans, m, n, kl, ku, alpha, lda, inc_b, beta, inc_c);
  205. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_TOL);
  206. }
  207. /**
  208. * Test zgbmv by comparing it against zgemv
  209. * with param trans is C
  210. */
  211. CTEST(zgbmv, 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. double alpha[] = {7.0, 1.0};
  219. double beta[] = {1.5, -1.5};
  220. double norm = check_zgbmv(trans, m, n, kl, ku, alpha, lda, inc_b, beta, inc_c);
  221. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_TOL);
  222. }
  223. /**
  224. * Test zgbmv by comparing it against zgemv
  225. * with param trans is R
  226. */
  227. CTEST(zgbmv, 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. double alpha[] = {7.0, 1.0};
  235. double beta[] = {1.5, -1.5};
  236. double norm = check_zgbmv(trans, m, n, kl, ku, alpha, lda, inc_b, beta, inc_c);
  237. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_TOL);
  238. }
  239. #endif