sum_serial.cpp 773 Bytes
Newer Older
Damian S. Steiger's avatar
Damian S. Steiger committed
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
#include <vector>
#include <numeric>
#include <iostream>
#include "timer.hpp"
#include "aligned_allocator.hpp"


int main( int argc, char** argv )
{
    // repetitions
    const int nrep = 10000;
    // vector size
    const int n = 1<<18;

    // initialize 64 byte aligned vectors
    std::vector< float, hpcse::aligned_allocator<float,64> > x(n,-1.2), y(n,3.4), z(n);

    hpcse::timer<> tim;
    tim.start();
    for (int k = 0; k < nrep; ++k)
    {
        for( int i = 0; i < n; ++i )
        {
            z[i] = x[i]*x[i] + y[i]*y[i] + 2.*x[i]*y[i];
        }
    }
    tim.stop();

    // print result checksum
    std::cout << std::accumulate(z.begin(), z.end(), 0.) << std::endl;
    std::cout << "Task took " << tim.get_timing() << " seconds." << std::endl;
}