|
#include <iostream> |
|
#include <iomanip> |
|
#include <vector> |
|
#include <chrono> |
|
|
|
// External linkage without header file goes here. |
|
// The symbols here are defined in *.o (Object files) |
|
// or shared libraries. (*.so - UNIX) or (*.dll - Windows) |
|
extern "C" { |
|
// WARNING: This is the GFortran ABI which encodes symbols with underscore (_) |
|
// at the end. It may be necessary to use #ifdef contional compilation to |
|
// abstract other compiler's ABIs. |
|
void daxpy_(int* n, double* alpha, |
|
double* x, int *incx, |
|
double* y, int* incy |
|
); |
|
} |
|
|
|
void tableHead(const std::vector<std::vector<double>>& columns, int n = 5){ |
|
int ncols = columns.size(); |
|
for(int i = 0; i < n; i++){ |
|
std::cout << std::setw(10) << i; |
|
for(int j = 0; j < ncols; j++ ){ |
|
std::cout << std::setw(10) << std::fixed << std::setprecision(3) << columns[j][i]; |
|
} |
|
std::cout << std::endl; |
|
} |
|
} |
|
|
|
void tableTail(const std::vector<std::vector<double>>& columns, int n = 5){ |
|
int ncols = columns.size(); |
|
// Assume that the table has at least one column |
|
int columnSize = columns.at(0).size(); |
|
int tailSize = n <= columnSize ? n : columnSize; |
|
for(int i = columnSize - tailSize; i < columnSize; i++){ |
|
std::cout << std::setw(15) << i; |
|
for(int j = 0; j < ncols; j++ ){ |
|
std::cout << std::setw(15) << std::fixed << std::setprecision(3) << columns[j][i]; |
|
} |
|
std::cout << std::endl; |
|
} |
|
} |
|
|
|
int main(){ |
|
using std::cout; |
|
using std::endl; |
|
|
|
const int N = 1000000; |
|
cout << "Number of vector elements N = " << N << endl; |
|
|
|
std::vector<double> X; |
|
std::vector<double> Y; |
|
for (int i = 0; i < N; i++){ |
|
// cout << "i = " << i << endl; |
|
X.push_back(i); |
|
Y.push_back(4 * i); |
|
} |
|
|
|
double a = 0.2; |
|
int incx = 1; |
|
int incy = 1; |
|
int n = N; |
|
|
|
cout << "Vector X size " << X.size() << " - Vector Y size = " << Y.size() << endl; |
|
cout << "Vectors X and Y before operation: daxpy" << endl; |
|
tableHead({X, Y}); |
|
|
|
// Blas Level Subroutine - vector to vector operations |
|
// Operation: Y <- a.X + Y |
|
// Computes : Y <- 0.2 * X + Y |
|
auto start = std::chrono::system_clock::now(); |
|
daxpy_(&n, &a, X.data(), &incx, Y.data(), &incy); |
|
auto end = std::chrono::system_clock::now(); |
|
std::chrono::duration<double> elapsed_time = end - start; |
|
|
|
cout << "[Head] Vectors X and Y after operation: daxpy" << endl; |
|
tableHead({X, Y}); |
|
cout << "[Tail] Vectors X and Y after operation: daxpy" << endl; |
|
tableTail({X, Y}); |
|
|
|
cout << "Finish execution - elapsed time in milliseconds = " |
|
<< 1000.0 * elapsed_time.count() |
|
<< endl; |
|
|
|
return 0; |
|
} |