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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_ZSCAL {
  34. double x_test[DATASIZE * 2 * INCREMENT];
  35. double x_verify[DATASIZE * 2 * INCREMENT];
  36. };
  37. #ifdef BUILD_COMPLEX16
  38. static struct DATA_ZSCAL data_zscal;
  39. /**
  40. * zscal 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 zscal_trusted(blasint n, double *alpha, double* x, blasint inc){
  48. blasint i, ip = 0;
  49. blasint inc_x2 = 2 * inc;
  50. double 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 zscal and zscal_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 double check_zscal(char api, blasint n, double *alpha, blasint inc)
  69. {
  70. blasint i;
  71. // Fill vectors x
  72. drand_generate(data_zscal.x_test, n * inc * 2);
  73. // Copy vector x for zscal_trusted
  74. for (i = 0; i < n * 2 * inc; i++)
  75. data_zscal.x_verify[i] = data_zscal.x_test[i];
  76. zscal_trusted(n, alpha, data_zscal.x_verify, inc);
  77. if(api == 'F')
  78. BLASFUNC(zscal)(&n, alpha, data_zscal.x_test, &inc);
  79. else
  80. cblas_zscal(n, alpha, data_zscal.x_test, inc);
  81. // Find the differences between output vector computed by zscal and zscal_trusted
  82. for (i = 0; i < n * 2 * inc; i++)
  83. data_zscal.x_verify[i] -= data_zscal.x_test[i];
  84. // Find the norm of differences
  85. return BLASFUNC(dznrm2)(&n, data_zscal.x_verify, &inc);
  86. }
  87. /**
  88. * Fortran API specific test
  89. * Test zscal by comparing it against reference
  90. */
  91. CTEST(zscal, alpha_r_zero_alpha_i_not_zero)
  92. {
  93. blasint N = DATASIZE;
  94. blasint inc = 1;
  95. double alpha[2] = {0.0, 1.0};
  96. double norm = check_zscal('F', N, alpha, inc);
  97. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_EPS);
  98. }
  99. /**
  100. * Fortran API specific test
  101. * Test zscal by comparing it against reference
  102. */
  103. CTEST(zscal, alpha_r_zero_alpha_i_zero_inc_2)
  104. {
  105. blasint N = DATASIZE;
  106. blasint inc = 2;
  107. double alpha[2] = {0.0, 0.0};
  108. double norm = check_zscal('F', N, alpha, inc);
  109. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_EPS);
  110. }
  111. /**
  112. * C API specific test
  113. * Test zscal by comparing it against reference
  114. */
  115. CTEST(zscal, c_api_alpha_r_zero_alpha_i_not_zero)
  116. {
  117. blasint N = DATASIZE;
  118. blasint inc = 1;
  119. double alpha[2] = {0.0, 1.0};
  120. double norm = check_zscal('C', N, alpha, inc);
  121. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_EPS);
  122. }
  123. /**
  124. * C API specific test
  125. * Test zscal by comparing it against reference
  126. */
  127. CTEST(zscal, c_api_alpha_r_zero_alpha_i_zero_inc_2)
  128. {
  129. blasint N = DATASIZE;
  130. blasint inc = 2;
  131. double alpha[2] = {0.0, 0.0};
  132. double norm = check_zscal('C', N, alpha, inc);
  133. ASSERT_DBL_NEAR_TOL(0.0, norm, DOUBLE_EPS);
  134. }
  135. #endif