Repository navigation
Expand file tree
/
Copy pathfactor.cpp
More file actions
302 lines (293 loc) · 13.7 KB
/
Copy pathfactor.cpp
File metadata and controls
302 lines (293 loc) · 13.7 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <gdla/gdla.hpp>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <vector>
namespace {
using namespace gdla;
template<class T> T value(double re, double im = 0) {
if constexpr (std::is_arithmetic_v<T>) return T(re);
else return T(re, im);
}
template<class T> T adj(T x) {
if constexpr (std::is_arithmetic_v<T>) return x;
else return std::conj(x);
}
void check(bool ok, const char* message) {
if (!ok) throw std::runtime_error(message);
}
// A small independent serial oracle. Only test fixtures store full matrices.
template<class T, class Fn>
void fill(Matrix<T>& a, Fn f) {
for (int i=0; i<a.rows().blocks(); ++i)
for (int j=0; j<a.cols().blocks(); ++j) {
if (!a.stored_side(i,j) || a.owner(i,j)!=a.grid().rank()) continue;
auto tile = make_tile<T>(a.rows().width(i),a.cols().width(j));
bool nonzero = false;
for (int r=0; r<a.rows().width(i); ++r)
for (int c=0; c<a.cols().width(j); ++c) {
tile(r,c) = f(a.rows().offset(i)+r, a.cols().offset(j)+c);
nonzero |= tile(r,c)!=T{};
}
if (nonzero) a.set_tile(i,j,tile);
}
}
template<class T, class Fn>
void compare(const Matrix<T>& a, Fn ref, const char* name) {
double err=0;
for (int i=0; i<a.rows().blocks(); ++i)
for (int j=0; j<a.cols().blocks(); ++j) {
if (!a.stored_side(i,j) || a.owner(i,j)!=a.grid().rank()) continue;
auto tile=a.local_tile(i,j);
for (int r=0; r<a.physical_rows(i); ++r)
for (int c=0; c<a.physical_cols(j); ++c) {
const T got = tile.empty() ? T{} : tile(r,c);
if (r>=a.rows().width(i) || c>=a.cols().width(j)) {
if (got!=T{}) err=std::numeric_limits<double>::infinity();
continue;
}
const auto expected=ref(a.rows().offset(i)+r,a.cols().offset(j)+c);
const double local=std::abs(got-expected)/(1.+std::abs(expected));
err=std::isfinite(local) ? std::max(err,local) : std::numeric_limits<double>::infinity();
}
}
MPI_Allreduce(MPI_IN_PLACE,&err,1,MPI_DOUBLE,MPI_MAX,a.grid().comm());
using Real=decltype(std::real(T{}));
if (err>128*std::numeric_limits<Real>::epsilon())
throw std::runtime_error(std::string(name)+" relative error "+std::to_string(err));
}
// A published result keeps the address of storage it retains.
template<class T> std::map<std::pair<int,int>,const T*> addresses(const Matrix<T>& a) {
std::map<std::pair<int,int>,const T*> out;
if (a.format()==Format::Dense) out[{-1,-1}]=a.dense_data();
else a.for_each_local_tile([&](int i,int j){out[{i,j}]=a.tile_data(i,j);});
return out;
}
template<class T>
void check_addresses(const Matrix<T>& a,const std::map<std::pair<int,int>,const T*>& before,const char* name) {
int ok=1;
for (const auto& [key,ptr] : addresses(a)) {
const auto it=before.find(key);
if (it!=before.end() && it->second!=ptr) ok=0;
}
MPI_Allreduce(MPI_IN_PLACE,&ok,1,MPI_INT,MPI_MIN,a.grid().comm());
check(ok,name);
}
template<class T> T spd(int r,int c) {
if (r==c) return value<T>(12+r);
if (r>c) return c==0 ? value<T>((r+1)*0.15,0.1*r) : T{};
return adj(spd<T>(c,r));
}
template<class T> std::vector<T> cholesky_reference(int n) {
std::vector<T> l(n*n);
for (int j=0;j<n;++j)
for (int i=j;i<n;++i) {
T x=spd<T>(i,j);
for (int k=0;k<j;++k) x-=l[i*n+k]*adj(l[j*n+k]);
l[i*n+j]=i==j ? T(std::sqrt(std::real(x))) : x/l[j*n+j];
}
return l;
}
template<class T> void chol(const Grid& grid) {
constexpr int n=7;
auto l=cholesky_reference<T>(n);
for (auto fmt : {Format::Dense,Format::Tiled,Format::AtomPair}) {
auto ax=fmt==Format::AtomPair ? Axis::atom({2,1,4},{19,5,8}) : Axis::fixed(n,3,{19,5,8});
for (Uplo u : {Uplo::Lower,Uplo::Upper}) {
const bool lower=u==Uplo::Lower;
auto selected=[&](int r,int c){return lower ? r>=c : r<=c;};
auto initial=[&](int r,int c){return selected(r,c) ? spd<T>(r,c) : value<T>(-91-r,c+1);};
for (auto structure : {Structure::General,Structure::Hermitian}) {
if (fmt==Format::Dense && structure!=Structure::General) continue;
Matrix<T> a(grid,fmt,ax,ax,structure,u);
fill(a,initial);
const auto stored=addresses(a);
check(potrf(u,a)==0,"potrf failed on SPD input");
check_addresses(a,stored,"potrf moved retained storage");
check(a.format()==fmt && a.rows()==ax && a.cols()==ax,"potrf changed format or axes");
check(a.structure()==(structure==Structure::General ? structure : Structure::Triangular),
"Cholesky result structure");
compare(a,[&](int r,int c) {
if (!selected(r,c)) return structure==Structure::General ? initial(r,c) : T{};
return lower ? l[r*n+c] : adj(l[c*n+r]);
},"potrf");
for (auto bf : {Format::Dense,Format::Tiled,Format::AtomPair}) {
// RHS rows must be cut like the factor; fixed blocks cannot
// reproduce the {2,1,4} atom partition.
auto rows = bf!=Format::AtomPair ? Axis::fixed(n,3) :
fmt==Format::AtomPair ? Axis::atom({2,1,4},{19,5,8}) : Axis::atom({3,3,1},{19,5,8});
auto cols=bf==Format::AtomPair ? Axis::atom({1,2},{99,3}) : Axis::fixed(3,2);
Matrix<T> b(grid,bf,rows,cols);
auto x=[](int r,int c){return value<T>(0.75+r-2*c,0.25*(r+c));};
auto rhs=[&](int r,int c) {
T sum{};
for(int k=0;k<n;++k) sum+=spd<T>(r,k)*x(k,c);
return sum;
};
fill(b,rhs);
if (fmt==Format::AtomPair && bf!=Format::AtomPair) {
bool rejected=false;
try { potrs(u,a,b); } catch(const std::invalid_argument&) {rejected=true;}
check(rejected,"potrs accepted a differently partitioned RHS");
compare(b,rhs,"rejected potrs preserves RHS");
continue;
}
const auto rhs_storage=addresses(b);
check(potrs(u,a,b)==0,"potrs failed");
check_addresses(b,rhs_storage,"potrs moved retained storage");
check(b.format()==bf && b.rows()==rows && b.cols()==cols,"potrs changed RHS layout");
compare(b,x,"potrs");
}
}
}
}
}
template<class T> T lu_entry(int r,int c) {
if (r==c) return value<T>(10+r,0.25);
if (r!=0 && c!=0 && (3*r+c)%5) return T{};
return value<T>(0.125*(r+c+1),0.0625*(r-c));
}
template<class T> void lu(const Grid& grid, int m, int n, bool aligned = false) {
std::vector<T> ref(m*n);
for(int r=0;r<m;++r) for(int c=0;c<n;++c) ref[r*n+c]=lu_entry<T>(r,c);
for(int k=0;k<std::min(m,n);++k) for(int i=k+1;i<m;++i) {
ref[i*n+k]/=ref[k*n+k];
for(int j=k+1;j<n;++j) ref[i*n+j]-=ref[i*n+k]*ref[k*n+j];
}
for(auto fmt : {Format::Dense,Format::Tiled,Format::AtomPair}) {
Axis rows=Axis::fixed(m,3), cols=Axis::fixed(n,m==n || aligned ? 3 : 2);
if (fmt==Format::AtomPair) {
rows=Axis::atom({2,1,m-3},{21,18,7});
cols=m==n ? rows : Axis::atom({1,2,n-3},{8,4,9});
if (aligned && m!=n) {
// The last pivot block is rectangular, with an additional
// off-diagonal panel on the longer axis.
rows=m>n ? Axis::atom({2,4,2},{21,18,7}) : Axis::atom({2,3},{21,18});
cols=n>m ? Axis::atom({2,4,2},{8,4,9}) : Axis::atom({2,3},{8,4});
}
}
Matrix<T> a(grid,fmt,rows,cols);
fill(a,lu_entry<T>);
if (m!=n && !aligned) {
// Pivot blocks would not be square; LibGDLA does not reblock.
bool rejected=false;
try { pgetrf_nopiv(a); } catch(const std::invalid_argument&) {rejected=true;}
check(rejected,"LU accepted non-square pivot blocks");
compare(a,lu_entry<T>,"rejected LU preserves input");
continue;
}
const auto stored=addresses(a);
check(pgetrf_nopiv(a)==0,"no-pivot LU failed");
check_addresses(a,stored,"pgetrf_nopiv moved retained storage");
check(a.rows()==rows && a.cols()==cols && a.format()==fmt,"LU changed layout");
compare(a,[&](int i,int j){return ref[i*n+j];},"pgetrf_nopiv");
}
}
void dense_atom_factor(const Grid& grid) {
auto axis = Axis::atom({2,1,4},{15,-8,3});
auto cols = Axis::atom({2,1},{22,9});
Matrix<double> a(grid,Storage::Dense,Partition::AtomPair,axis,axis);
Matrix<double> b(grid,Storage::Dense,Partition::AtomPair,axis,cols);
fill(a,spd<double>);
auto solution = [](int i,int j){ return 1.0+i-2*j; };
fill(b,[&](int i,int j) {
double v = 0.;
for (int k=0;k<axis.size();++k) v += spd<double>(i,k)*solution(k,j);
return v;
});
auto original = addresses(a);
check(potrf(Uplo::Lower,a)==0 && potrs(Uplo::Lower,a,b)==0,"dense atom-pair Cholesky solve");
check_addresses(a,original,"dense atom-pair Cholesky moved storage");
compare(b,solution,"dense atom-pair solution");
Matrix<double> lu_matrix(grid,Storage::Dense,Partition::AtomPair,axis,axis);
fill(lu_matrix,[](int i,int j) { return i == j ? 5.+i : (i>j ? 0.25*j : 0.); });
check(pgetrf_nopiv(lu_matrix)==0,"dense atom-pair LU");
compare(lu_matrix,[](int i,int j){ return i==j ? 5.+i : (i>j ? 0.25*j/(5.+j) : 0.); },
"dense atom-pair LU");
}
void failures(const Grid& grid) {
for (auto fmt : {Format::Dense,Format::Tiled,Format::AtomPair}) {
auto rows=fmt==Format::AtomPair ? Axis::atom({2,1,4}) : Axis::fixed(7,3);
auto cols=fmt==Format::AtomPair ? Axis::atom({1,2,4}) : Axis::fixed(7,2);
Matrix<double> a(grid,fmt,rows,cols);
auto diag=[](int i,int j){return i==j ? 4.:0.;};
fill(a,diag);
for (bool cholesky : {true,false}) {
bool rejected=false;
try { cholesky ? potrf(Uplo::Lower,a) : pgetrf_nopiv(a); }
catch(const std::invalid_argument&) {rejected=true;}
check(rejected,"Factorization accepted non-square diagonal blocks");
compare(a,diag,"rejected factorization preserves input");
}
}
for (auto fmt : {Format::Dense,Format::Tiled,Format::AtomPair}) {
auto ax=fmt==Format::AtomPair ? Axis::atom({2,1,4}) : Axis::fixed(7,3);
for (bool cholesky : {false,true}) {
Matrix<double> a(grid,fmt,ax,ax);
auto bad=[&](int i,int j) {return i==j ? (i==4 ? (cholesky ? -1.:0.) : 4.) : 0.;};
fill(a,bad);
const int info=cholesky ? potrf(Uplo::Lower,a) : pgetrf_nopiv(a);
check(info==5,"Incorrect global 1-based failing pivot");
compare(a,bad,"factor failure preserves input");
}
Matrix<double> a(grid,fmt,ax,ax);
auto diag=[](int i,int j){return i==j && i!=4 ? 2.:0.;};
fill(a,diag);
Matrix<double> bad_rhs(grid,fmt,ax,Axis::fixed(2,2));
auto payload=[](int i,int j){return 2.+i-j;};
fill(bad_rhs,payload);
check(potrs(Uplo::Lower,a,bad_rhs)==5,"POTRS zero diagonal INFO");
compare(bad_rhs,payload,"POTRS failure preserves RHS");
auto empty_axis=Axis::fixed(0,3);
Matrix<double> empty(grid,fmt,empty_axis,empty_axis);
check(potrf(Uplo::Upper,empty)==0,"Empty POTRF");
Matrix<double> short_rows(grid,fmt,empty_axis,ax), short_cols(grid,fmt,ax,empty_axis);
check(pgetrf_nopiv(short_rows)==0 && pgetrf_nopiv(short_cols)==0,"Empty rectangular LU");
check(potrs(Uplo::Upper,empty,short_rows)==0,"Empty POTRS");
bool rejected=false;
try {
Matrix<double> rhs(grid,fmt,Axis::fixed(6,3),Axis::fixed(2,2));
potrs(Uplo::Lower,a,rhs);
} catch(const std::invalid_argument&) {rejected=true;}
check(rejected,"POTRS accepted incompatible RHS");
}
}
template<class T> void suite(const Grid& grid) {
chol<T>(grid);
lu<T>(grid,7,7);
lu<T>(grid,8,5);
lu<T>(grid,5,8);
lu<T>(grid,8,5,true);
lu<T>(grid,5,8,true);
}
} // namespace
int main(int argc,char** argv) {
int provided=0, ranks=0, rank=0;
MPI_Init_thread(&argc,&argv,MPI_THREAD_FUNNELED,&provided);
MPI_Comm_size(MPI_COMM_WORLD,&ranks);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
try {
const bool gpu=argc==2 && std::string(argv[1])=="--gpu";
check(argc==1 || gpu,"usage: factor_test [--gpu]");
check(provided>=MPI_THREAD_FUNNELED,"MPI thread support insufficient");
const int pr=ranks%2==0 ? 2 : 1;
auto ctx=gdla::init(MPI_COMM_WORLD,gpu ? gdla::Backend::GPU : gdla::Backend::CPU,
pr,ranks/pr,'C',gpu ? gdla::Communication::HostStagedMPI : gdla::Communication::MPI);
const auto g=ctx.grid();
suite<float>(g);
suite<double>(g);
suite<std::complex<float>>(g);
suite<std::complex<double>>(g);
dense_atom_factor(g);
failures(g);
if (!rank) std::cout<<"Factorization and solve checks passed on "<<ranks<<" ranks\n";
} catch(const std::exception& e) {
std::cerr<<"rank "<<rank<<": "<<e.what()<<"\n";
MPI_Abort(MPI_COMM_WORLD,1);
}
MPI_Finalize();
}