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.

dgemv_n.c 8.1 kB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /***************************************************************************
  2. Copyright (c) 2013-2016, 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. /**************************************************************************************
  28. * 2016/03/30 Werner Saar (wernsaar@googlemail.com)
  29. * BLASTEST : OK
  30. * CTEST : OK
  31. * TEST : OK
  32. * LAPACK-TEST : OK
  33. **************************************************************************************/
  34. #include "common.h"
  35. #if defined(POWER8) || defined(POWER9) || defined(POWER10)
  36. #if defined(__VEC__) || defined(__ALTIVEC__)
  37. #include "dgemv_n_microk_power8.c"
  38. #endif
  39. #endif
  40. #define NBMAX 4096
  41. #ifndef HAVE_KERNEL_4x4
  42. static void dgemv_kernel_4x4(BLASLONG n, FLOAT *a_ptr, BLASLONG lda, FLOAT *xo, FLOAT *y, FLOAT alpha)
  43. {
  44. BLASLONG i;
  45. FLOAT x[4] __attribute__ ((aligned (16)));;
  46. FLOAT *a0 = a_ptr;
  47. FLOAT *a1 = a0 + lda;
  48. FLOAT *a2 = a1 + lda;
  49. FLOAT *a3 = a2 + lda;
  50. for ( i=0; i<4; i++)
  51. x[i] = xo[i] * alpha;
  52. for ( i=0; i< n; i+=4 )
  53. {
  54. y[i] += a0[i]*x[0] + a1[i]*x[1] + a2[i]*x[2] + a3[i]*x[3];
  55. y[i+1] += a0[i+1]*x[0] + a1[i+1]*x[1] + a2[i+1]*x[2] + a3[i+1]*x[3];
  56. y[i+2] += a0[i+2]*x[0] + a1[i+2]*x[1] + a2[i+2]*x[2] + a3[i+2]*x[3];
  57. y[i+3] += a0[i+3]*x[0] + a1[i+3]*x[1] + a2[i+3]*x[2] + a3[i+3]*x[3];
  58. }
  59. }
  60. #endif
  61. #ifndef HAVE_KERNEL_4x2
  62. static void dgemv_kernel_4x2(BLASLONG n, FLOAT *a0, FLOAT *a1, FLOAT *xo, FLOAT *y, FLOAT alpha)
  63. {
  64. BLASLONG i;
  65. FLOAT x[4] __attribute__ ((aligned (16)));;
  66. for ( i=0; i<2; i++)
  67. x[i] = xo[i] * alpha;
  68. for ( i=0; i< n; i+=4 )
  69. {
  70. y[i] += a0[i]*x[0] + a1[i]*x[1];
  71. y[i+1] += a0[i+1]*x[0] + a1[i+1]*x[1];
  72. y[i+2] += a0[i+2]*x[0] + a1[i+2]*x[1];
  73. y[i+3] += a0[i+3]*x[0] + a1[i+3]*x[1];
  74. }
  75. }
  76. #endif
  77. #ifndef HAVE_KERNEL_4x1
  78. static void dgemv_kernel_4x1(BLASLONG n, FLOAT *a0, FLOAT *xo, FLOAT *y, FLOAT alpha)
  79. {
  80. BLASLONG i;
  81. FLOAT x[4] __attribute__ ((aligned (16)));;
  82. for ( i=0; i<1; i++)
  83. x[i] = xo[i] * alpha;
  84. for ( i=0; i< n; i+=4 )
  85. {
  86. y[i] += a0[i]*x[0];
  87. y[i+1] += a0[i+1]*x[0];
  88. y[i+2] += a0[i+2]*x[0];
  89. y[i+3] += a0[i+3]*x[0];
  90. }
  91. }
  92. #endif
  93. static void add_y(BLASLONG n, FLOAT *src, FLOAT *dest, BLASLONG inc_dest)
  94. {
  95. BLASLONG i;
  96. if ( inc_dest != 1 )
  97. {
  98. for ( i=0; i<n; i++ )
  99. {
  100. *dest += *src;
  101. src++;
  102. dest += inc_dest;
  103. }
  104. return;
  105. }
  106. }
  107. int CNAME(BLASLONG m, BLASLONG n, BLASLONG dummy1, FLOAT alpha, FLOAT *a, BLASLONG lda, FLOAT *x, BLASLONG inc_x, FLOAT *y, BLASLONG inc_y, FLOAT *buffer)
  108. {
  109. BLASLONG i;
  110. FLOAT *a_ptr;
  111. FLOAT *x_ptr;
  112. FLOAT *y_ptr;
  113. BLASLONG n1;
  114. BLASLONG m1;
  115. BLASLONG m2;
  116. BLASLONG m3;
  117. BLASLONG n2;
  118. BLASLONG lda4 = lda << 2;
  119. FLOAT xbuffer[8] __attribute__ ((aligned (16)));
  120. FLOAT *ybuffer;
  121. if ( m < 1 ) return(0);
  122. if ( n < 1 ) return(0);
  123. ybuffer = buffer;
  124. n1 = n >> 2 ;
  125. n2 = n & 3 ;
  126. m3 = m & 3 ;
  127. m1 = m & -4 ;
  128. m2 = (m & (NBMAX-1)) - m3 ;
  129. y_ptr = y;
  130. BLASLONG NB = NBMAX;
  131. while ( NB == NBMAX )
  132. {
  133. m1 -= NB;
  134. if ( m1 < 0)
  135. {
  136. if ( m2 == 0 ) break;
  137. NB = m2;
  138. }
  139. a_ptr = a;
  140. x_ptr = x;
  141. if ( inc_y != 1 )
  142. memset(ybuffer,0,NB*8);
  143. else
  144. ybuffer = y_ptr;
  145. if ( inc_x == 1 )
  146. {
  147. for( i = 0; i < n1 ; i++)
  148. {
  149. dgemv_kernel_4x4(NB,a_ptr,lda,x_ptr,ybuffer,alpha);
  150. a_ptr += lda4;
  151. x_ptr += 4;
  152. }
  153. if ( n2 & 2 )
  154. {
  155. dgemv_kernel_4x2(NB,a_ptr,a_ptr+lda,x_ptr,ybuffer,alpha);
  156. a_ptr += lda*2;
  157. x_ptr += 2;
  158. }
  159. if ( n2 & 1 )
  160. {
  161. dgemv_kernel_4x1(NB,a_ptr,x_ptr,ybuffer,alpha);
  162. a_ptr += lda;
  163. x_ptr += 1;
  164. }
  165. }
  166. else
  167. {
  168. for( i = 0; i < n1 ; i++)
  169. {
  170. xbuffer[0] = x_ptr[0];
  171. x_ptr += inc_x;
  172. xbuffer[1] = x_ptr[0];
  173. x_ptr += inc_x;
  174. xbuffer[2] = x_ptr[0];
  175. x_ptr += inc_x;
  176. xbuffer[3] = x_ptr[0];
  177. x_ptr += inc_x;
  178. dgemv_kernel_4x4(NB,a_ptr,lda,xbuffer,ybuffer,alpha);
  179. a_ptr += lda4;
  180. }
  181. for( i = 0; i < n2 ; i++)
  182. {
  183. xbuffer[0] = x_ptr[0];
  184. x_ptr += inc_x;
  185. dgemv_kernel_4x1(NB,a_ptr,xbuffer,ybuffer,alpha);
  186. a_ptr += lda;
  187. }
  188. }
  189. a += NB;
  190. if ( inc_y != 1 )
  191. {
  192. add_y(NB,ybuffer,y_ptr,inc_y);
  193. y_ptr += NB * inc_y;
  194. }
  195. else
  196. y_ptr += NB ;
  197. }
  198. if ( m3 == 0 ) return(0);
  199. if ( m3 == 3 )
  200. {
  201. a_ptr = a;
  202. x_ptr = x;
  203. FLOAT temp0 = 0.0;
  204. FLOAT temp1 = 0.0;
  205. FLOAT temp2 = 0.0;
  206. if ( lda == 3 && inc_x ==1 )
  207. {
  208. for( i = 0; i < ( n & -4 ); i+=4 )
  209. {
  210. temp0 += a_ptr[0] * x_ptr[0] + a_ptr[3] * x_ptr[1];
  211. temp1 += a_ptr[1] * x_ptr[0] + a_ptr[4] * x_ptr[1];
  212. temp2 += a_ptr[2] * x_ptr[0] + a_ptr[5] * x_ptr[1];
  213. temp0 += a_ptr[6] * x_ptr[2] + a_ptr[9] * x_ptr[3];
  214. temp1 += a_ptr[7] * x_ptr[2] + a_ptr[10] * x_ptr[3];
  215. temp2 += a_ptr[8] * x_ptr[2] + a_ptr[11] * x_ptr[3];
  216. a_ptr += 12;
  217. x_ptr += 4;
  218. }
  219. for( ; i < n; i++ )
  220. {
  221. temp0 += a_ptr[0] * x_ptr[0];
  222. temp1 += a_ptr[1] * x_ptr[0];
  223. temp2 += a_ptr[2] * x_ptr[0];
  224. a_ptr += 3;
  225. x_ptr ++;
  226. }
  227. }
  228. else
  229. {
  230. for( i = 0; i < n; i++ )
  231. {
  232. temp0 += a_ptr[0] * x_ptr[0];
  233. temp1 += a_ptr[1] * x_ptr[0];
  234. temp2 += a_ptr[2] * x_ptr[0];
  235. a_ptr += lda;
  236. x_ptr += inc_x;
  237. }
  238. }
  239. y_ptr[0] += alpha * temp0;
  240. y_ptr += inc_y;
  241. y_ptr[0] += alpha * temp1;
  242. y_ptr += inc_y;
  243. y_ptr[0] += alpha * temp2;
  244. return(0);
  245. }
  246. if ( m3 == 2 )
  247. {
  248. a_ptr = a;
  249. x_ptr = x;
  250. FLOAT temp0 = 0.0;
  251. FLOAT temp1 = 0.0;
  252. if ( lda == 2 && inc_x ==1 )
  253. {
  254. for( i = 0; i < (n & -4) ; i+=4 )
  255. {
  256. temp0 += a_ptr[0] * x_ptr[0] + a_ptr[2] * x_ptr[1];
  257. temp1 += a_ptr[1] * x_ptr[0] + a_ptr[3] * x_ptr[1];
  258. temp0 += a_ptr[4] * x_ptr[2] + a_ptr[6] * x_ptr[3];
  259. temp1 += a_ptr[5] * x_ptr[2] + a_ptr[7] * x_ptr[3];
  260. a_ptr += 8;
  261. x_ptr += 4;
  262. }
  263. for( ; i < n; i++ )
  264. {
  265. temp0 += a_ptr[0] * x_ptr[0];
  266. temp1 += a_ptr[1] * x_ptr[0];
  267. a_ptr += 2;
  268. x_ptr ++;
  269. }
  270. }
  271. else
  272. {
  273. for( i = 0; i < n; i++ )
  274. {
  275. temp0 += a_ptr[0] * x_ptr[0];
  276. temp1 += a_ptr[1] * x_ptr[0];
  277. a_ptr += lda;
  278. x_ptr += inc_x;
  279. }
  280. }
  281. y_ptr[0] += alpha * temp0;
  282. y_ptr += inc_y;
  283. y_ptr[0] += alpha * temp1;
  284. return(0);
  285. }
  286. if ( m3 == 1 )
  287. {
  288. a_ptr = a;
  289. x_ptr = x;
  290. FLOAT temp = 0.0;
  291. if ( lda == 1 && inc_x ==1 )
  292. {
  293. for( i = 0; i < (n & -4); i+=4 )
  294. {
  295. temp += a_ptr[i] * x_ptr[i] + a_ptr[i+1] * x_ptr[i+1] + a_ptr[i+2] * x_ptr[i+2] + a_ptr[i+3] * x_ptr[i+3];
  296. }
  297. for( ; i < n; i++ )
  298. {
  299. temp += a_ptr[i] * x_ptr[i];
  300. }
  301. }
  302. else
  303. {
  304. for( i = 0; i < n; i++ )
  305. {
  306. temp += a_ptr[0] * x_ptr[0];
  307. a_ptr += lda;
  308. x_ptr += inc_x;
  309. }
  310. }
  311. y_ptr[0] += alpha * temp;
  312. return(0);
  313. }
  314. return(0);
  315. }