-
Notifications
You must be signed in to change notification settings - Fork 5
/
cholesky.cpp
187 lines (165 loc) · 5.38 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Copyright 2019-2024 Michael Sippel, Tapish Narwal
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <redGrapes/redGrapes.hpp>
#include <redGrapes/resource/ioresource.hpp>
#include <cblas.h>
#include <lapacke.h>
#include <iostream>
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;
}
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]);
auto rg = redGrapes::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<redGrapes::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::debug("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](auto a, auto c)
{
spdlog::debug("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(
[blksz](auto a)
{
spdlog::debug("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](auto a, auto b)
{
spdlog::debug("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());
}
}
print_matrix(A, nblks, blksz);
return 0;
}