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_cscal.c 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <cblas.h>
  30. #include "common.h"
  31. #define DATASIZE 100
  32. #define INCREMENT 2
  33. struct DATA_CSCAL {
  34. float x_test[DATASIZE * 2 * INCREMENT];
  35. float x_verify[DATASIZE * 2 * INCREMENT];
  36. };
  37. #ifdef BUILD_COMPLEX
  38. static struct DATA_CSCAL data_cscal;
  39. /**
  40. * cscal reference code
  41. *
  42. * param n - number of elements of vector x
  43. * param alpha - scaling factor for the vector product
  44. * param x - buffer holding input vector x
  45. * param inc - stride of vector x
  46. */
  47. static void cscal_trusted(blasint n, float *alpha, float* x, blasint inc){
  48. blasint i, ip = 0;
  49. blasint inc_x2 = 2 * inc;
  50. float temp;
  51. for (i = 0; i < n; i++)
  52. {
  53. temp = alpha[0] * x[ip] - alpha[1] * x[ip+1];
  54. x[ip+1] = alpha[0] * x[ip+1] + alpha[1] * x[ip];
  55. x[ip] = temp;
  56. ip += inc_x2;
  57. }
  58. }
  59. /**
  60. * Comapare results computed by cscal and cscal_trusted
  61. *
  62. * param api specifies tested api (C or Fortran)
  63. * param n - number of elements of vector x
  64. * param alpha - scaling factor for the vector product
  65. * param inc - stride of vector x
  66. * return norm of differences
  67. */
  68. static float check_cscal(char api, blasint n, float *alpha, blasint inc)
  69. {
  70. blasint i;
  71. // Fill vectors a
  72. srand_generate(data_cscal.x_test, n * inc * 2);
  73. // Copy vector x for cscal_trusted
  74. for (i = 0; i < n * 2 * inc; i++)
  75. data_cscal.x_verify[i] = data_cscal.x_test[i];
  76. cscal_trusted(n, alpha, data_cscal.x_verify, inc);
  77. if(api == 'F')
  78. BLASFUNC(cscal)(&n, alpha, data_cscal.x_test, &inc);
  79. else
  80. cblas_cscal(n, alpha, data_cscal.x_test, inc);
  81. // Find the differences between output vector computed by cscal and cscal_trusted
  82. for (i = 0; i < n * 2 * inc; i++)
  83. data_cscal.x_verify[i] -= data_cscal.x_test[i];
  84. // Find the norm of differences
  85. return BLASFUNC(scnrm2)(&n, data_cscal.x_verify, &inc);
  86. }
  87. /**
  88. * Fortran API specific test
  89. * Test cscal by comparing it against reference
  90. */
  91. CTEST(cscal, alpha_r_zero_alpha_i_not_zero)
  92. {
  93. blasint N = DATASIZE;
  94. blasint inc = 1;
  95. float alpha[2] = {0.0f, 1.0f};
  96. float norm = check_cscal('F', N, alpha, inc);
  97. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  98. }
  99. /**
  100. * Fortran API specific test
  101. * Test cscal by comparing it against reference
  102. */
  103. CTEST(cscal, alpha_r_zero_alpha_i_zero_inc_2)
  104. {
  105. blasint N = DATASIZE;
  106. blasint inc = 2;
  107. float alpha[2] = {0.0f, 0.0f};
  108. float norm = check_cscal('F', N, alpha, inc);
  109. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  110. }
  111. /**
  112. * C API specific test
  113. * Test cscal by comparing it against reference
  114. */
  115. CTEST(cscal, c_api_alpha_r_zero_alpha_i_not_zero)
  116. {
  117. blasint N = DATASIZE;
  118. blasint inc = 1;
  119. float alpha[2] = {0.0f, 1.0f};
  120. float norm = check_cscal('C', N, alpha, inc);
  121. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  122. }
  123. /**
  124. * C API specific test
  125. * Test cscal by comparing it against reference
  126. */
  127. CTEST(cscal, c_api_alpha_r_zero_alpha_i_zero_inc_2)
  128. {
  129. blasint N = DATASIZE;
  130. blasint inc = 2;
  131. float alpha[2] = {0.0f, 0.0f};
  132. float norm = check_cscal('C', N, alpha, inc);
  133. ASSERT_DBL_NEAR_TOL(0.0f, norm, SINGLE_EPS);
  134. }
  135. #endif