forked from ComputationalRadiationPhysics/redGrapes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcholesky.cpp
165 lines (139 loc) · 4.89 KB
/
cholesky.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
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
#include <iostream>
#include <cblas.h>
// work-around, see
// https://github.com/xianyi/OpenBLAS/issues/1992#issuecomment-459474791
// https://github.com/xianyi/OpenBLAS/pull/1998
#include <complex>
#define lapack_complex_float std::complex<float>
#define lapack_complex_double std::complex<double>
// end work-around
#include <lapacke.h>
#include <redGrapes/task/property/inherit.hpp>
#include <redGrapes/task/property/label.hpp>
#define REDGRAPES_TASK_PROPERTIES redGrapes::LabelProperty
#include <redGrapes/redGrapes.hpp>
#include <redGrapes/resource/ioresource.hpp>
namespace rg = redGrapes;
void print_matrix(std::vector<redGrapes::IOResource<double*>> A, int n_blocks, int blocksize);
int main(int argc, char* argv[])
{
size_t nblks;
size_t blksz;
unsigned n_threads = 1;
if(argc >= 3)
{
nblks = (long) atoi(argv[1]);
blksz = (long) atoi(argv[2]);
}
else
{
printf("usage: %s nblks blksz [n_threads]\n\n", argv[0]);
printf(" example: %s 256 16\n", argv[0]);
exit(0);
}
if(argc >= 4)
n_threads = atoi(argv[3]);
rg::init(n_threads);
size_t N = nblks * blksz;
// allocate input matrix
double* Alin = (double*) malloc(N * N * sizeof(double));
// fill the matrix with random values
for(size_t i = 0; i < N * N; i++)
Alin[i] = ((double) rand()) / ((double) RAND_MAX);
// make it positive definite
for(size_t i = 0; i < N; i++)
Alin[i * N + i] += N;
// initialize tiled matrix in column-major layout
std::vector<rg::IOResource<double*>> A(nblks * nblks);
// allocate each tile (also in column-major layout)
for(size_t j = 0; j < nblks; ++j)
for(size_t i = 0; i < nblks; ++i)
A[j * nblks + i] = new double[blksz * blksz];
/* ia: row of outer matrix
ib: row of inner matrix
ja: col of outer matrix
jb: col of inner matrix */
for(size_t ia = 0; ia < nblks; ++ia)
for(size_t ib = 0; ib < blksz; ++ib)
for(size_t ja = 0; ja < nblks; ++ja)
for(size_t jb = 0; jb < blksz; ++jb)
(*A[ja * nblks + ia])[jb * blksz + ib] = Alin[(ia * blksz + ib) + (ja * blksz + jb) * N];
print_matrix(A, nblks, blksz);
// calculate cholesky decomposition
for(size_t j = 0; j < nblks; j++)
{
for(size_t k = 0; k < j; k++)
{
for(size_t i = j + 1; i < nblks; i++)
{
// A[i,j] = A[i,j] - A[i,k] * (A[j,k])^t
rg::emplace_task(
[blksz](auto a, auto b, auto c)
{
spdlog::info("dgemm");
cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans,
blksz, blksz, blksz, -1.0, *a, blksz, *b, blksz, 1.0, *c, blksz);
},
A[k * nblks + i].read(),
A[k * nblks + j].read(),
A[j * nblks + i].write());
}
}
for(size_t i = 0; i < j; i++)
{
// A[j,j] = A[j,j] - A[j,i] * (A[j,i])^t
rg::emplace_task(
[blksz, nblks](auto a, auto c)
{
spdlog::info("dsyrk");
cblas_dsyrk(CblasColMajor, CblasLower, CblasNoTrans,
blksz, blksz, -1.0, *a, blksz, 1.0, *c, blksz);
},
A[i * nblks + j].read(),
A[j * nblks + j].write());
}
// Cholesky Factorization of A[j,j]
rg::emplace_task(
[j, blksz, nblks](auto a)
{
spdlog::info("dpotrf");
LAPACKE_dpotrf(LAPACK_COL_MAJOR, 'L', blksz, *a, blksz);
},
A[j * nblks + j].write());
for(size_t i = j + 1; i < nblks; i++)
{
// A[i,j] <- A[i,j] = X * (A[j,j])^t
rg::emplace_task(
[blksz, nblks](auto a, auto b)
{
spdlog::info("dtrsm");
cblas_dtrsm(CblasColMajor,
CblasRight, CblasLower, CblasTrans, CblasNonUnit,
blksz, blksz, 1.0, *a, blksz, *b, blksz);
},
A[j * nblks + j].read(),
A[j * nblks + i].write());
}
}
rg::finalize();
print_matrix(A, nblks, blksz);
return 0;
}
void print_matrix(std::vector<redGrapes::IOResource<double*>> A, int nblks, int blocksize)
{
for(int ia = 0; ia < nblks; ++ia)
{
for(int ib = 0; ib < blocksize; ++ib)
{
for(int ja = 0; ja < nblks; ++ja)
{
for(int jb = 0; jb < blocksize; ++jb)
{
std::cout << (*A[ja * nblks + ia])[jb * blocksize + ib] << "; ";
}
}
std::cout << std::endl;
}
}
std::cout << std::endl;
}