-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolmult.cpp
80 lines (60 loc) · 1.98 KB
/
colmult.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
#include <Kokkidio.hpp>
#include <iostream>
#include "magic_enum.hpp"
template<typename ViewMapType>
void printFirstN( int nPrint, const ViewMapType& a, const ViewMapType& b );
int main(int argc, char** argv){
Kokkos::ScopeGuard guard(argc, argv);
using namespace Kokkidio;
std::cout << "DefaultTarget: " << magic_enum::enum_name(DefaultTarget) <<'\n';
constexpr Target target {DefaultTarget};
// constexpr Target target {Target::host};
int nRows {4}, nCols {5};
/* create and set the input matrices */
using MatrixView = DualViewMap<Eigen::MatrixXd, target>;
MatrixView a {nRows, nCols}, b {nRows, nCols};
// b.resizeLike(a);
a.map_host().setRandom();
b.map_host().setRandom();
a.copyToTarget();
b.copyToTarget();
double result = 0;
/* perform parallel computation and reduction (2D -> column range) */
Kokkidio::parallel_reduce<target>(
a.cols(),
KOKKOS_LAMBDA(ParallelRange<target> rng, double& sum){
sum += ( rng(a).transpose() * rng(b) ).trace(); // trace = sum of the diagonal
/* equivalent: sum of coefficient-wise products */
// sum += ( rng(a).array() * rng(b).array() ).sum();
},
redux::sum(result)
);
std::cout
<< "Result: " << result
// << ", expected: " << ( a.map_host().transpose() * b.map_host() ).trace()
<< ", expected: " << ( a.map_host().array() * b.map_host().array() ).sum()
<< '\n';
printFirstN(5, a, b);
return 0;
}
template<typename ViewMapType>
void printFirstN( int nPrint, const ViewMapType& a, const ViewMapType& b ){
int nCols { a.cols() };
nPrint = std::min(nPrint, nCols);
/* col buffer for printing */
Eigen::MatrixXd colBuf ( a.rows(), 2 );
int precision {4}, opts {0};
Eigen::IOFormat fmt( precision, opts, " * ", " + \n", "(", ")" );
std::stringstream str;
for (int i{0}; i<nPrint; ++i){
colBuf << a.map_host().col(i), b.map_host().col(i);
str
<< colBuf.format(fmt) << " = "
<< colBuf.col(0).dot(colBuf.col(1))
<< '\n';
}
if (nPrint < nCols){
str << "...\n";
}
std::cout << "Check:\n" << str.str();
}