-
Notifications
You must be signed in to change notification settings - Fork 1
/
local_lu.cpp
130 lines (110 loc) · 3.4 KB
/
local_lu.cpp
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
// std dependencies
#include <chrono>
#include <cmath>
#include <iostream>
#include <random>
#include <string>
#include <tuple>
#include <vector>
// mpi
#include <mpi.h>
// mkl
#include <mkl.h>
#include <mkl_scalapack.h>
#include <mkl_cblas.h>
#include "utils.hpp"
bool get_parameters(int argc, char** argv, int& N, int& n_rep) {
if (argc < 2) {
std::cout << "Not enough arguments!" << std::endl;
std::cout << "Call it like: " << std::endl;
std::cout << "./local_lu <global matrix size> <n_repetitions>" << std::endl;
return false;
}
// global matrix size
N = std::atoi(argv[1]);
// number of repetitions
n_rep = 2;
if (argc >= 3) {
n_rep = std::atoi(argv[2]);
}
if (argc > 3) {
std::cout << "Too many parameters given, ignoring some of them." << std::endl;
}
return true;
}
// prints the matrix in row-major format
template <typename T>
void print_matrix(T* mat, int N) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
std::cout << mat[i * N + j] << ", ";
}
std::cout << "\n";
}
std::cout << std::endl;
}
int main(int argc, char ** argv)
{
//******************************
// INPUT PARAMETERS
//******************************
int N; // global matrix size
int n_rep; // number of repetitions
// get the input parameters
bool status = get_parameters(argc, argv, N, nb, n_rep);
if (status != true) {
return 0;
}
//******************************
// GENERATING MATRIX DATA
//******************************
std::vector<double> a(N * N);
int seed = 1234 + rank;
std::mt19937 gen (seed);
std::uniform_real_distribution<double> dist(0.0, 10.0);
// set all elements to some random value
for (auto& el : a) {
el = dist(gen);
}
//******************************
// PIVOTING
//******************************
std::vector<int> pivots;
pivots.reserve(N);
// it's 1-based
for (int i = 0; i < N; ++i) {
pivots.push_back(i+1);
}
std::vector<long> timings;
timings.reserve(n_rep);
//******************************
// LU FACTORIZATION + TIMING
//******************************
for (int i = 0; i < n_rep; ++i) {
auto start = std::chrono::high_resolution_clock::now();
LAPACKE_dgetrf(LAPACK_ROW_MAJOR, N, N, a.data(), N, pivots.data());
auto end = std::chrono::high_resolution_clock::now();
// total time
auto time = std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count();
timings.push_back(time);
}
//******************************
// OUTPUT TIMINGS
//******************************
std::cout << "==========================" << std::endl;
std::cout << " PROBLEM PARAMETERS:" << std::endl;
std::cout << "==========================" << std::endl;
std::cout << "Matrix size: " << N << std::endl;
std::cout << "Number of repetitions: " << n_rep << std::endl;
std::cout << "--------------------------" << std::endl;
std::cout << "Input Matrix: " << std::endl;
print_matrix(a.data(), N);
std::cout << "--------------------------" << std::endl;
std::cout << "TIMINGS [ms] = ";
for (auto &time : timings) {
std::cout << time << " ";
}
std::cout << std::endl;
std::cout << "==========================" << std::endl;
return 0;
}