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.

common.c 7.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "common.h"
  29. /**
  30. * Generate random array
  31. */
  32. void srand_generate(float *alpha, blasint n)
  33. {
  34. blasint i;
  35. for (i = 0; i < n; i++)
  36. alpha[i] = (float)rand() / (float)RAND_MAX;
  37. }
  38. void drand_generate(double *alpha, blasint n)
  39. {
  40. blasint i;
  41. for (i = 0; i < n; i++)
  42. alpha[i] = (double)rand() / (double)RAND_MAX;
  43. }
  44. /**
  45. * Find difference between two rectangle matrix
  46. * return norm of differences
  47. */
  48. float smatrix_difference(float *a, float *b, blasint cols, blasint rows, blasint ld)
  49. {
  50. blasint i = 0;
  51. blasint j = 0;
  52. blasint inc = 1;
  53. float norm = 0.0f;
  54. float *a_ptr = a;
  55. float *b_ptr = b;
  56. for(i = 0; i < rows; i++)
  57. {
  58. for (j = 0; j < cols; j++) {
  59. a_ptr[j] -= b_ptr[j];
  60. }
  61. norm += BLASFUNC(snrm2)(&cols, a_ptr, &inc);
  62. a_ptr += ld;
  63. b_ptr += ld;
  64. }
  65. return norm/(float)(rows);
  66. }
  67. double dmatrix_difference(double *a, double *b, blasint cols, blasint rows, blasint ld)
  68. {
  69. blasint i = 0;
  70. blasint j = 0;
  71. blasint inc = 1;
  72. double norm = 0.0;
  73. double *a_ptr = a;
  74. double *b_ptr = b;
  75. for(i = 0; i < rows; i++)
  76. {
  77. for (j = 0; j < cols; j++) {
  78. a_ptr[j] -= b_ptr[j];
  79. }
  80. norm += BLASFUNC(dnrm2)(&cols, a_ptr, &inc);
  81. a_ptr += ld;
  82. b_ptr += ld;
  83. }
  84. return norm/(double)(rows);
  85. }
  86. /**
  87. * Complex conjugate operation for vector
  88. *
  89. * param n specifies number of elements in vector x
  90. * param inc_x specifies increment of vector x
  91. * param x_ptr specifies buffer holding vector x
  92. */
  93. void cconjugate_vector(blasint n, blasint inc_x, float *x_ptr)
  94. {
  95. blasint i;
  96. inc_x *= 2;
  97. for (i = 0; i < n; i++)
  98. {
  99. x_ptr[1] *= (-1.0f);
  100. x_ptr += inc_x;
  101. }
  102. }
  103. void zconjugate_vector(blasint n, blasint inc_x, double *x_ptr)
  104. {
  105. blasint i;
  106. inc_x *= 2;
  107. for (i = 0; i < n; i++)
  108. {
  109. x_ptr[1] *= (-1.0);
  110. x_ptr += inc_x;
  111. }
  112. }
  113. /**
  114. * Transpose matrix
  115. *
  116. * param rows specifies number of rows of A
  117. * param cols specifies number of columns of A
  118. * param alpha specifies scaling factor for matrix A
  119. * param a_src - buffer holding input matrix A
  120. * param lda_src - leading dimension of the matrix A
  121. * param a_dst - buffer holding output matrix A
  122. * param lda_dst - leading dimension of output matrix A
  123. */
  124. void stranspose(blasint rows, blasint cols, float alpha, float *a_src, int lda_src,
  125. float *a_dst, blasint lda_dst)
  126. {
  127. blasint i, j;
  128. for (i = 0; i != cols; i++)
  129. {
  130. for (j = 0; j != rows; j++)
  131. a_dst[i*lda_dst+j] = alpha*a_src[j*lda_src+i];
  132. }
  133. }
  134. void dtranspose(blasint rows, blasint cols, double alpha, double *a_src, int lda_src,
  135. double *a_dst, blasint lda_dst)
  136. {
  137. blasint i, j;
  138. for (i = 0; i != cols; i++)
  139. {
  140. for (j = 0; j != rows; j++)
  141. a_dst[i*lda_dst+j] = alpha*a_src[j*lda_src+i];
  142. }
  143. }
  144. void ctranspose(blasint rows, blasint cols, float *alpha, float *a_src, int lda_src,
  145. float *a_dst, blasint lda_dst, int conj)
  146. {
  147. blasint i, j;
  148. lda_dst *= 2;
  149. lda_src *= 2;
  150. for (i = 0; i != cols*2; i+=2)
  151. {
  152. for (j = 0; j != rows*2; j+=2){
  153. a_dst[(i/2)*lda_dst+j] = alpha[0] * a_src[(j/2)*lda_src+i] + conj * alpha[1] * a_src[(j/2)*lda_src+i+1];
  154. a_dst[(i/2)*lda_dst+j+1] = (-1.0f) * conj * alpha[0] * a_src[(j/2)*lda_src+i+1] + alpha[1] * a_src[(j/2)*lda_src+i];
  155. }
  156. }
  157. }
  158. void ztranspose(blasint rows, blasint cols, double *alpha, double *a_src, int lda_src,
  159. double *a_dst, blasint lda_dst, int conj)
  160. {
  161. blasint i, j;
  162. lda_dst *= 2;
  163. lda_src *= 2;
  164. for (i = 0; i != cols*2; i+=2)
  165. {
  166. for (j = 0; j != rows*2; j+=2){
  167. a_dst[(i/2)*lda_dst+j] = alpha[0] * a_src[(j/2)*lda_src+i] + conj * alpha[1] * a_src[(j/2)*lda_src+i+1];
  168. a_dst[(i/2)*lda_dst+j+1] = (-1.0) * conj * alpha[0] * a_src[(j/2)*lda_src+i+1] + alpha[1] * a_src[(j/2)*lda_src+i];
  169. }
  170. }
  171. }
  172. /**
  173. * Copy matrix from source A to destination A
  174. *
  175. * param rows specifies number of rows of A
  176. * param cols specifies number of columns of A
  177. * param alpha specifies scaling factor for matrix A
  178. * param a_src - buffer holding input matrix A
  179. * param lda_src - leading dimension of the matrix A
  180. * param a_dst - buffer holding output matrix A
  181. * param lda_dst - leading dimension of output matrix A
  182. * param conj specifies conjugation
  183. */
  184. void my_scopy(blasint rows, blasint cols, float alpha, float *a_src, int lda_src,
  185. float *a_dst, blasint lda_dst)
  186. {
  187. blasint i, j;
  188. for (i = 0; i != rows; i++)
  189. {
  190. for (j = 0; j != cols; j++)
  191. a_dst[i*lda_dst+j] = alpha*a_src[i*lda_src+j];
  192. }
  193. }
  194. void my_dcopy(blasint rows, blasint cols, double alpha, double *a_src, int lda_src,
  195. double *a_dst, blasint lda_dst)
  196. {
  197. blasint i, j;
  198. for (i = 0; i != rows; i++)
  199. {
  200. for (j = 0; j != cols; j++)
  201. a_dst[i*lda_dst+j] = alpha*a_src[i*lda_src+j];
  202. }
  203. }
  204. void my_ccopy(blasint rows, blasint cols, float *alpha, float *a_src, int lda_src,
  205. float *a_dst, blasint lda_dst, int conj)
  206. {
  207. blasint i, j;
  208. lda_dst *= 2;
  209. lda_src *= 2;
  210. for (i = 0; i != rows; i++)
  211. {
  212. for (j = 0; j != cols*2; j+=2){
  213. a_dst[i*lda_dst+j] = alpha[0] * a_src[i*lda_src+j] + conj * alpha[1] * a_src[i*lda_src+j+1];
  214. a_dst[i*lda_dst+j+1] = (-1.0f) * conj *alpha[0] * a_src[i*lda_src+j+1] + alpha[1] * a_src[i*lda_src+j];
  215. }
  216. }
  217. }
  218. void my_zcopy(blasint rows, blasint cols, double *alpha, double *a_src, int lda_src,
  219. double *a_dst, blasint lda_dst, int conj)
  220. {
  221. blasint i, j;
  222. lda_dst *= 2;
  223. lda_src *= 2;
  224. for (i = 0; i != rows; i++)
  225. {
  226. for (j = 0; j != cols*2; j+=2){
  227. a_dst[i*lda_dst+j] = alpha[0] * a_src[i*lda_src+j] + conj * alpha[1] * a_src[i*lda_src+j+1];
  228. a_dst[i*lda_dst+j+1] = (-1.0) * conj *alpha[0] * a_src[i*lda_src+j+1] + alpha[1] * a_src[i*lda_src+j];
  229. }
  230. }
  231. }