|
| 1 | +/*************************************************************************** |
| 2 | +Copyright (c) 2014, 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 | +/* need a new enough GCC for avx512 support */ |
| 29 | +#if (( defined(__GNUC__) && __GNUC__ > 6 && defined(__AVX2__)) || (defined(__clang__) && __clang_major__ >= 6)) |
| 30 | + |
| 31 | +#define HAVE_KERNEL_16 1 |
| 32 | + |
| 33 | +#include <immintrin.h> |
| 34 | + |
| 35 | +static void sdot_kernel_16( BLASLONG n, FLOAT *x, FLOAT *y, FLOAT *dot) |
| 36 | + |
| 37 | +{ |
| 38 | + int i = 0; |
| 39 | + __m256 accum_0, accum_1, accum_2, accum_3; |
| 40 | + |
| 41 | + accum_0 = _mm256_setzero_ps(); |
| 42 | + accum_1 = _mm256_setzero_ps(); |
| 43 | + accum_2 = _mm256_setzero_ps(); |
| 44 | + accum_3 = _mm256_setzero_ps(); |
| 45 | + |
| 46 | +#ifdef __AVX512CD__ |
| 47 | + __m512 accum_05, accum_15, accum_25, accum_35; |
| 48 | + int n64; |
| 49 | + n64 = n & (~63); |
| 50 | + |
| 51 | + accum_05 = _mm512_setzero_ps(); |
| 52 | + accum_15 = _mm512_setzero_ps(); |
| 53 | + accum_25 = _mm512_setzero_ps(); |
| 54 | + accum_35 = _mm512_setzero_ps(); |
| 55 | + |
| 56 | + for (; i < n64; i += 64) { |
| 57 | + accum_05 += _mm512_loadu_ps(&x[i+ 0]) * _mm512_loadu_ps(&y[i+ 0]); |
| 58 | + accum_15 += _mm512_loadu_ps(&x[i+16]) * _mm512_loadu_ps(&y[i+16]); |
| 59 | + accum_25 += _mm512_loadu_ps(&x[i+32]) * _mm512_loadu_ps(&y[i+32]); |
| 60 | + accum_35 += _mm512_loadu_ps(&x[i+48]) * _mm512_loadu_ps(&y[i+48]); |
| 61 | + } |
| 62 | + |
| 63 | + /* |
| 64 | + * we need to fold our 512 bit wide accumulator vectors into 256 bit wide vectors so that the AVX2 code |
| 65 | + * below can continue using the intermediate results in its loop |
| 66 | + */ |
| 67 | + accum_0 = _mm512_extractf32x8_ps(accum_05, 0) + _mm512_extractf32x8_ps(accum_05, 1); |
| 68 | + accum_1 = _mm512_extractf32x8_ps(accum_15, 0) + _mm512_extractf32x8_ps(accum_15, 1); |
| 69 | + accum_2 = _mm512_extractf32x8_ps(accum_25, 0) + _mm512_extractf32x8_ps(accum_25, 1); |
| 70 | + accum_3 = _mm512_extractf32x8_ps(accum_35, 0) + _mm512_extractf32x8_ps(accum_35, 1)) |
| 71 | + |
| 72 | +#endif |
| 73 | + for (; i < n; i += 32) { |
| 74 | + accum_0 += _mm256_loadu_ps(&x[i+ 0]) * _mm256_loadu_ps(&y[i+ 0]); |
| 75 | + accum_1 += _mm256_loadu_ps(&x[i+ 8]) * _mm256_loadu_ps(&y[i+ 8]); |
| 76 | + accum_2 += _mm256_loadu_ps(&x[i+16]) * _mm256_loadu_ps(&y[i+16]); |
| 77 | + accum_3 += _mm256_loadu_ps(&x[i+24]) * _mm256_loadu_ps(&y[i+24]); |
| 78 | + } |
| 79 | + |
| 80 | + /* we now have the partial sums of the dot product in the 4 accumulation vectors, time to consolidate */ |
| 81 | + |
| 82 | + accum_0 = accum_0 + accum_1 + accum_2 + accum_3; |
| 83 | + |
| 84 | + __m128 half_accum0; |
| 85 | + |
| 86 | + /* Add upper half to lower half of each of the 256 bit vector to get a 128 bit vector */ |
| 87 | + half_accum0 = _mm256_extractf128_ps(accum_0, 0) + _mm256_extractf128_ps(accum_0, 1); |
| 88 | + |
| 89 | + /* in 128 bit land there is a hadd operation to do the rest of the element-wise sum in one go */ |
| 90 | + half_accum0 = _mm_hadd_ps(half_accum0, half_accum0); |
| 91 | + half_accum0 = _mm_hadd_ps(half_accum0, half_accum0); |
| 92 | + |
| 93 | + *dot = half_accum0[0]; |
| 94 | +} |
| 95 | + |
| 96 | +#else |
| 97 | +#include "sdot_microk_haswell-2.c" |
| 98 | +#endif |
0 commit comments