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.

compare_sgemv_bgemv.c 5.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /***************************************************************************
  2. Copyright (c) 2020,2025 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 permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBLAS PROJECT OR CONTRIBUTORS BE
  20. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  25. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *****************************************************************************/
  27. #include <stdio.h>
  28. #include <stdint.h>
  29. #include "../common.h"
  30. #include "test_helpers.h"
  31. #define SGEMV BLASFUNC(sgemv)
  32. #define BGEMV BLASFUNC(bgemv)
  33. #define BGEMV_LARGEST 256
  34. int main(int argc, char *argv[])
  35. {
  36. blasint k;
  37. int i, j, l;
  38. blasint x, y;
  39. blasint one = 1;
  40. int ret = 0;
  41. int loop = BGEMV_LARGEST;
  42. char transA = 'N';
  43. float alpha = 1.0, beta = 0.0;
  44. bfloat16 alpha_bf16, beta_bf16;
  45. for (beta = 0; beta < 3; beta += 1)
  46. {
  47. for (alpha = 0; alpha < 3; alpha += 1)
  48. {
  49. for (l = 0; l < 2; l++)
  50. { // l = 1 to test inc_x & inc_y not equal to one.
  51. for (x = 1; x <= loop; x++)
  52. {
  53. k = (x == 0) ? 0 : l + 1;
  54. float *A = (float *)malloc_safe(x * x * sizeof(FLOAT));
  55. float *B = (float *)malloc_safe(x * sizeof(FLOAT) << l);
  56. float *C = (float *)malloc_safe(x * sizeof(FLOAT) << l);
  57. bfloat16 *AA = (bfloat16 *)malloc_safe(x * x * sizeof(bfloat16));
  58. bfloat16 *BB = (bfloat16 *)malloc_safe(x * sizeof(bfloat16) << l);
  59. bfloat16 *CC = (bfloat16 *)malloc_safe(x * sizeof(bfloat16) << l);
  60. float *DD = (float *)malloc_safe(x * sizeof(FLOAT));
  61. if ((A == NULL) || (B == NULL) || (C == NULL) || (AA == NULL) || (BB == NULL) ||
  62. (CC == NULL) || (DD == NULL))
  63. return 1;
  64. for (j = 0; j < x; j++)
  65. {
  66. for (i = 0; i < x; i++)
  67. {
  68. A[j * x + i] = ((FLOAT)rand() / (FLOAT)RAND_MAX) + 0.5;
  69. sbstobf16_(&one, &A[j * x + i], &one, &AA[j * x + i], &one);
  70. }
  71. B[j << l] = ((FLOAT)rand() / (FLOAT)RAND_MAX) + 0.5;
  72. sbstobf16_(&one, &B[j << l], &one, &BB[j << l], &one);
  73. C[j << l] = ((FLOAT)rand() / (FLOAT)RAND_MAX) + 0.5;
  74. sbstobf16_(&one, &B[j << l], &one, &CC[j << l], &one);
  75. }
  76. for (y = 0; y < 2; y++)
  77. {
  78. if (y == 0)
  79. {
  80. transA = 'N';
  81. }
  82. else
  83. {
  84. transA = 'T';
  85. }
  86. memset(C, 0, x * sizeof(FLOAT) << l);
  87. memset(CC, 0, x * sizeof(bfloat16) << l);
  88. memset(DD, 0, x * sizeof(FLOAT));
  89. sbstobf16_(&one, &alpha, &one, &alpha_bf16, &one);
  90. sbstobf16_(&one, &beta, &one, &beta_bf16, &one);
  91. SGEMV(&transA, &x, &x, &alpha, A, &x, B, &k, &beta, C, &k);
  92. BGEMV(&transA, &x, &x, &alpha_bf16, AA, &x, BB, &k, &beta_bf16, CC, &k);
  93. for (i = 0; i < x; i++)
  94. DD[i] *= beta;
  95. for (j = 0; j < x; j++)
  96. for (i = 0; i < x; i++)
  97. if (transA == 'N')
  98. {
  99. DD[i] += alpha * float16to32(AA[j * x + i]) * float16to32(BB[j << l]);
  100. }
  101. else if (transA == 'T')
  102. {
  103. DD[j] += alpha * float16to32(AA[j * x + i]) * float16to32(BB[i << l]);
  104. }
  105. for (j = 0; j < x; j++)
  106. {
  107. if (!is_close(float16to32(CC[j << l]), truncate_float32_to_bfloat16(C[j << l]), 0.01, 0.001))
  108. {
  109. #ifdef DEBUG
  110. printf("Mismatch at trans=%c, alpha=%.2f, beta=%.2f, i=%d, j=%d, k=%ld: CC=%.6f, C=%.6f\n",
  111. transA, alpha, beta, i, j, k, float16to32(CC[j << l]), truncate_float32_to_bfloat16(C[j << l]));
  112. #endif
  113. ret++;
  114. }
  115. if (!is_close(float16to32(CC[j << l]), truncate_float32_to_bfloat16(DD[j]), 0.001, 0.0001))
  116. {
  117. #ifdef DEBUG
  118. printf("Mismatch at trans=%c, alpha=%.2f, beta=%.2f, i=%d, j=%d, k=%ld: CC=%.6f, C=%.6f\n",
  119. transA, alpha, beta, i, j, k, float16to32(CC[j << l]), truncate_float32_to_bfloat16(DD[j]));
  120. #endif
  121. ret++;
  122. }
  123. }
  124. }
  125. free(A);
  126. free(B);
  127. free(C);
  128. free(AA);
  129. free(BB);
  130. free(CC);
  131. free(DD);
  132. } // x
  133. } // l
  134. } // alpha
  135. } // beta
  136. if (ret != 0)
  137. fprintf(stderr, "FATAL ERROR BGEMV - Return code: %d\n", ret);
  138. return ret;
  139. }