1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
     100
     101
     102
     103
     104
     105
     106
     107
     108
     109
     110
     111
     112
     113
     114
     115
     116
     117
     118
     119
     120
     121
     122
     123
     124
     125
     126
     127
     128
     129
     130
     131
     132
     133
     134
     135
     136
     137
     138
     139
     140
     141
     142
     143
     144
     145
     146
     147
     148
     149
     150
     151
     152
     153
     154
     155
     156
     157
     158
     159
     160
     161
     162
     163
     164
     165
     166
     167
     168
     169
     170
     171
     172
     173
     174
     175
     176
     177
     178
     179
     180
     181
     182
     183
     184
     185
     186
     187
     188
     189
     190
     191
     192
     193
     194
     195
     196
     197
     198
     199
     200
     201
     202
     203
     204
     205
     206
     207
     208
     209
     210
     211
     212
     213
     214
     215
     216
     217
     218
     219
     220
     221
     222
     223
     224
     225
     226
     227
     228
     229
     230
     231
     232
     233
     234
     235
     236
     237
     238
     239
     240
     241
     242
     243
     244
     245
     246
     247
     248
     249
     250
     251
     252
     253
     254
     255
     256
     257
     258
     259
     260
     261
     262
     263
     264
     265
     266
     267
     268
     269
     270
     271
     272
     273
     274
     275
     276
     277
     278
     279
     280
     281
     282
     283
     284
     285
     286
     287
     288
     289
     290
     291
     292
     293
     294
     295
     296
     297
     298
     299
     300
     301
     302
     303
     304
     305
     306
     307
     308
     309
     310
     311
     312
     313
     314
     315
     316
     317
     318
     319
     320
     321
     322
     323
     324
     325
     326
     327
     328
     329
     330
     331
     332
     333
     334
     335
     336
     337
     338
     339
     340
     341
     342
     343
     344
     345
     346
     347
     348
     349
     350
     351
     352
     353
     354
     355
     356
     357
     358
     359
     360
     361
     362
     363
     364
     365
     366
     367
     368
     369
     370
     371
     372
     373
     374
     375
#include <cxxstd/iostream.h>
#include <flens/flens.h>

#ifdef VECSUM

#define NB 8

//------------------------------------------------------------------------------

namespace flens {

template <typename ALPHA, typename TX, typename TY>
void
copy_NB(ALPHA alpha, const TX *x, TY *y)
{
    /*
    for (int i=0; i<NB; ++i) {
        y[i] = alpha*x[i];
    }
    */
    y[ 0] = x[ 0];
    y[ 1] = x[ 1];
    y[ 2] = x[ 2];
    y[ 3] = x[ 3];
    y[ 4] = x[ 4];
    y[ 5] = x[ 5];
    y[ 6] = x[ 6];
    y[ 7] = x[ 7];

    y[ 0] *= alpha;
    y[ 1] *= alpha;
    y[ 2] *= alpha;
    y[ 3] *= alpha;
    y[ 4] *= alpha;
    y[ 5] *= alpha;
    y[ 6] *= alpha;
    y[ 7] *= alpha;
}

template <typename ALPHA, typename TX, typename TY>
void
add_NB(ALPHA alpha, const TX *x, TY *y)
{
    /*
    for (int i=0; i<NB; ++i) {
        y[i] += alpha*x[i];
    }
    */

#   ifndef HAVE_SSE
    y[ 0] += alpha*x[ 0];
    y[ 1] += alpha*x[ 1];
    y[ 2] += alpha*x[ 2];
    y[ 3] += alpha*x[ 3];
    y[ 4] += alpha*x[ 4];
    y[ 5] += alpha*x[ 5];
    y[ 6] += alpha*x[ 6];
    y[ 7] += alpha*x[ 7];
#   else
#       ifdef HAVE_AVX

        __m256d x1_, x2_, y1_, y2_;
        __m256d alpha_ = mm256_set1_pd_(alpha);

        x1_ = mm256_loadu_pd_(x);
        x2_ = mm256_loadu_pd_(x+4);

        x1_ = mm256_mul_pd_(alpha_, x1_);
        x2_ = mm256_mul_pd_(alpha_, x2_);

        y1_ = mm256_loadu_pd_(y);
        y2_ = mm256_loadu_pd_(y+4);

        y1_ = mm256_add_pd_(y1_, x1_);
        y2_ = mm256_add_pd_(y2_, x2_);

        mm256_storeu_pd_(y  , y1_);
        mm256_storeu_pd_(y+4, y2_);

#       else

        __m128d x1_, x2_, x3_, x4_ , y1_, y2_, y3_, y4_;
        __m128d alpha_ = mm_set1_pd_(alpha);

        x1_ = mm_loadu_pd_(x);
        x2_ = mm_loadu_pd_(x+2);
        x3_ = mm_loadu_pd_(x+4);
        x4_ = mm_loadu_pd_(x+6);

        x1_ = mm_mul_pd_(alpha_, x1_);
        x2_ = mm_mul_pd_(alpha_, x2_);
        x3_ = mm_mul_pd_(alpha_, x3_);
        x4_ = mm_mul_pd_(alpha_, x4_);

        y1_ = mm_loadu_pd_(y);
        y2_ = mm_loadu_pd_(y+2);
        y3_ = mm_loadu_pd_(y+4);
        y4_ = mm_loadu_pd_(y+6);

        y1_ = mm_add_pd_(y1_, x1_);
        y2_ = mm_add_pd_(y2_, x2_);
        y3_ = mm_add_pd_(y3_, x3_);
        y4_ = mm_add_pd_(y4_, x4_);

        mm_storeu_pd_(y  , y1_);
        mm_storeu_pd_(y+2, y2_);
        mm_storeu_pd_(y+4, y3_);
        mm_storeu_pd_(y+6, y4_);

#       endif // HAVE_AVX
#   endif // HAVE_SSE

}

// namespace flens

//------------------------------------------------------------------------------

namespace flens {

template <typename IndexType, typename ALPHA, typename VX, typename VY>
void
copy(IndexType i0, IndexType nb, ALPHA alpha, const DenseVector<VX> &x,
     DenseVector<VY> &y)
{
    typedef typename DenseVector<VX>::ElementType  TX;
    typedef typename DenseVector<VY>::ElementType  TY;

    const IndexType  incX = x.stride();
    const TX         *x_ = x.data() + i0*incX;

    const IndexType  incY = y.stride();
    TY               *y_  = y.data() + i0*incY;

    if (incX==1 && incY==1) {
        if (nb==NB) {
            copy_NB(alpha, x_, y_);
        } else {
            for (IndexType i=0; i<nb; ++i) {
                y_[i] = alpha * x_[i];
            }
        }
    } else {
        for (IndexType i=0; i<nb; ++i, x_+=incX, y_+=incY) {
            *y_ = alpha * (*x_);
        }
    }
}

template <typename IndexType, typename ALPHA, typename VX, typename VY>
void
add(IndexType i0, IndexType nb, ALPHA alpha, const DenseVector<VX> &x,
    DenseVector<VY> &y)
{
    typedef typename DenseVector<VX>::ElementType  TX;
    typedef typename DenseVector<VY>::ElementType  TY;

    const IndexType  incX = x.stride();
    const TX         *x_ = x.data() + i0*incX;

    const IndexType  incY = y.stride();
    TY               *y_  = y.data() + i0*incY;

    if (incX==1 && incY==1) {
        if (nb==NB) {
            add_NB(alpha, x_, y_);
        } else {
            for (IndexType i=0; i<nb; ++i) {
                y_[i] += alpha * x_[i];
            }
        }
    } else {
        for (IndexType i=0; i<nb; ++i, x_+=incX, y_+=incY) {
            *y_ += alpha * (*x_);
        }
    }
}

// namespace flens

//------------------------------------------------------------------------------

namespace flens {

template <typename IndexType, typename ALPHA, typename VX1, typename VX2,
          typename VY>
void
copy(IndexType i0, IndexType nb, ALPHA alpha,
     const VectorClosure<OpAdd, VX1, VX2> &x,
     DenseVector<VY> &y)
{
    copy(i0, nb, alpha, x.left(), y);
    add(i0, nb, alpha, x.right(), y);
}

template <typename IndexType, typename ALPHA, typename SV, typename VX,
          typename VY>
typename RestrictTo<IsScalarValue<SV>::value,
                    void>::Type
copy(IndexType i0, IndexType nb, ALPHA alpha,
     const VectorClosure<OpMult, SV, VX> &x,
     DenseVector<VY> &y)
{
    copy(i0, nb, alpha*x.left().value(), x.right(), y);
}

template <typename IndexType, typename ALPHA, typename VX1, typename VX2,
          typename VY>
void
add(IndexType i0, IndexType nb, ALPHA alpha,
    const VectorClosure<OpAdd, VX1, VX2> &x,
    DenseVector<VY> &y)
{
    add(i0, nb, alpha, x.left(), y);
    add(i0, nb, alpha, x.right(), y);
}

template <typename IndexType, typename ALPHA, typename VX1, typename VX2,
          typename VY>
void
add(IndexType i0, IndexType nb, ALPHA alpha,
    const VectorClosure<OpMult, VX1, VX2> &x,
    DenseVector<VY> &y)
{
    add(i0, nb, alpha*x.left().value(), x.right(), y);
}

// namespace flens

//------------------------------------------------------------------------------

namespace flens { namespace blas {

//
//  IsDenseVectorSum: Trait for vector sums of form alpha1*x1 + alpha2*x2 + ...
//
template <typename V>
struct IsDenseVectorSum
{
    static const bool value = IsDenseVector<V>::value;
};

template <typename L, typename R>
struct IsDenseVectorSum<VectorClosure<OpAdd, L, R> >
{
    static const bool value = IsDenseVectorSum<L>::value
                           && IsDenseVectorSum<R>::value;
};

template <typename SV, typename VX>
struct IsDenseVectorSum<VectorClosure<OpMult, SV, VX> >
{
    static const bool value = IsScalarValue<SV>::value
                           && IsDenseVectorSum<VX>::value;
};

//
//  Use non-default evaluation for vector sums
//
template <typename Op, typename L, typename R>
struct DefaultEval<VectorClosure<Op, L, R> >
{
    typedef VectorClosure<OpAdd, L, R>   Closure;

    static const bool value = !IsDenseVectorSum<Closure>::value;
};

//
//  Entry point for the evaluation of vector sums.
//
template <typename VL, typename VR, typename VY>
typename RestrictTo<IsDenseVectorSum<VectorClosure<OpAdd, VL, VR> >::value,
         void>::Type
copy(const VectorClosure<OpAdd, VL, VR> &x, DenseVector<VY> &y)
{
    typedef typename DenseVector<VY>::IndexType  IndexType;
    typedef typename DenseVector<VY>::ElementType  ElementType;

    const IndexType nb = NB;
    const IndexType n  = y.length();
    const IndexType n1 = (n/nb)*nb;

    const ElementType  One(1);

    for (IndexType i=0; i<n1; i+=nb) {
        copy(i, nb, One, x.left(), y);
        add(i, nb, One, x.right(), y);
    }
    if (n1<n) {
        copy(n1, n-n1, One, x.left(), y);
        add(n1, n-n1, One, x.right(), y);
    }
}

} } // namespace blas, flens

#endif

//------------------------------------------------------------------------------

namespace flens {

template <typename IndexType,
          typename ALPHA1, typename VX1,
          typename ALPHA2, typename VX2,
          typename ALPHA3, typename VX3,
          typename VY>
void
sum(IndexType n,
    const ALPHA1 alpha1, const VX1 *x1, IndexType incX1,
    const ALPHA2 alpha2, const VX2 *x2, IndexType incX2,
    const ALPHA3 alpha3, const VX3 *x3, IndexType incX3,
    VY *y, IndexType incY)
{
    for (IndexType i=0; i<n; ++i, x1+=incX1, x2+=incX2, x3+=incX3, y+=incY) {
        *y = alpha1 * (*x1) + alpha2 * (*x2) + alpha3 * (*x3);
    }
}


template <typename ALPHA1, typename VX1,
          typename ALPHA2, typename VX2,
          typename ALPHA3, typename VX3,
          typename VY>
void
sum(const ALPHA1 alpha1, const DenseVector<VX1> &x1,
    const ALPHA2 alpha2, const DenseVector<VX2> &x2,
    const ALPHA3 alpha3, const DenseVector<VX3> &x3,
    DenseVector<VY> &y)
{
    sum(y.length(),
        alpha1, x1.data(), x1.stride(),
        alpha2, x2.data(), x2.stride(),
        alpha3, x3.data(), x3.stride(),
        y.data(), y.stride());
}

// namespace flens

//------------------------------------------------------------------------------

#include <cxxstd/iostream.h>
#include <flens/flens.cxx>

using namespace flens;
using namespace std;

int
main()
{
    const int n = 150;
    DenseVector<Array<double> >   x1(n), x2(n), x3(n), y(n);

    x1 = 1;
    x2 = 2;
    x3 = 3;

    for (int count=1; count<=500000; ++count) {

        sum(2.0, x1, 4.0, x2, 3.0, x3, y);
        sum(2.0, x1, 4.0, x2, 3.0, x3, y);
        sum(2.0, x1, 4.0, x2, 3.0, x3, y);
        sum(2.0, x1, 4.0, x2, 3.0, x3, y);

        /*
        y = 2.0*x1 + 4.0*x2 + 3.0*x3;
        y = 2.0*x1 + 4.0*x2 + 3.0*x3;
        y = 2.0*x1 + 4.0*x2 + 3.0*x3;
        y = 2.0*x1 + 4.0*x2 + 3.0*x3;
        */

    }

    return y(2);
}