-
Notifications
You must be signed in to change notification settings - Fork 1
/
Petsc.cpp
444 lines (377 loc) · 11.1 KB
/
Petsc.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include "Petsc.hpp"
#include <string>
#include <limits>
#include <random>
#include "petscviewer.h"
#include "petscdraw.h"
namespace petsc {
void openViewer(PetscViewer & viewer, std::string filename, VIEWERFORMAT format, MPI_Comm comm)
{
PetscErrorCode ierr = 0;
if (format == ASCII) {
ierr = PetscViewerASCIIOpen(comm, filename.c_str(), &viewer);
CHKERRV(ierr);
}
else if (format == BINARY) {
ierr = PetscViewerBinaryOpen(comm, filename.c_str(), FILE_MODE_WRITE, &viewer);
CHKERRV(ierr);
ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_NATIVE);
CHKERRV(ierr);
}
}
template<class T>
MPI_Comm getCommunicator(T obj)
{
MPI_Comm comm;
PetscObjectGetComm(reinterpret_cast<PetscObject>(obj), &comm);
return comm;
}
template<class T>
void setName(T obj, std::string name)
{
PetscErrorCode ierr = 0;
ierr = PetscObjectSetName(reinterpret_cast<PetscObject>(obj), name.c_str()); CHKERRV(ierr);
}
template<class T>
std::string getName(T obj)
{
const char *cstr;
PetscObjectGetName(reinterpret_cast<PetscObject>(obj), &cstr);
return cstr;
}
/////////////////////////////////////////////////////////////////////////
Vector::Vector(std::string name)
{
int size;
MPI_Comm_size(PETSC_COMM_WORLD, &size);
PetscErrorCode ierr = 0;
ierr = VecCreate(PETSC_COMM_WORLD, &vector); CHKERRV(ierr);
setName(vector, name);
}
Vector::Vector(Vec &v, std::string name)
{
VecCopy(v, vector);
setName(vector, name);
}
Vector::Vector(Vector &v, std::string name)
{
PetscErrorCode ierr = 0;
ierr = VecDuplicate(v.vector, &vector); CHKERRV(ierr);
setName(vector, name);
}
Vector::Vector(Mat &m, std::string name, LEFTRIGHT type)
{
// MatGetVecs is deprecated, we keep it due to the old PETSc version at the SuperMUC.
PetscErrorCode ierr = 0;
if (type == LEFTRIGHT::LEFT) {
ierr = MatCreateVecs(m, nullptr, &vector); CHKERRV(ierr); // a vector with the same number of rows
}
else {
ierr = MatCreateVecs(m, &vector, nullptr); CHKERRV(ierr); // a vector with the same number of cols
}
setName(vector, name);
}
Vector::Vector(Matrix &m, std::string name, LEFTRIGHT type) :
Vector(m.matrix, name, type)
{}
Vector::~Vector()
{
PetscErrorCode ierr = 0;
PetscBool petscIsInitialized;
PetscInitialized(&petscIsInitialized);
if (petscIsInitialized) // If PetscFinalize is called before ~Vector
ierr = VecDestroy(&vector); CHKERRV(ierr);
}
Vector::operator Vec&()
{
return vector;
}
void Vector::init(PetscInt rows)
{
PetscErrorCode ierr = 0;
ierr = VecSetSizes(vector, PETSC_DECIDE, rows); CHKERRV(ierr);
ierr = VecSetFromOptions(vector); CHKERRV(ierr);
}
int Vector::getSize()
{
PetscInt size;
VecGetSize(vector, &size);
return size;
}
int Vector::getLocalSize()
{
PetscInt size;
VecGetLocalSize(vector, &size);
return size;
}
void Vector::setValue(PetscInt row, PetscScalar value)
{
PetscErrorCode ierr = 0;
ierr = VecSetValue(vector, row, value, INSERT_VALUES); CHKERRV(ierr);
}
void Vector::arange(double start, double stop)
{
PetscErrorCode ierr = 0;
PetscScalar *a;
PetscInt range_start, range_end, size;
VecGetSize(vector, &size);
VecGetOwnershipRange(vector, &range_start, &range_end);
double step_size = (stop-start) / size;
ierr = VecGetArray(vector, &a); CHKERRV(ierr);
for (PetscInt i = range_start; i < range_end; i++) {
a[i - range_start] = (i + start) * step_size;
}
VecRestoreArray(vector, &a);
}
void Vector::fillWithRandoms()
{
PetscErrorCode ierr = 0;
PetscRandom rctx;
std::random_device rd;
std::uniform_real_distribution<double> dist(0, 1);
PetscRandomCreate(getCommunicator(vector), &rctx);
PetscRandomSetType(rctx, PETSCRAND48);
PetscRandomSetSeed(rctx, dist(rd));
PetscRandomSeed(rctx);
ierr = VecSetRandom(vector, rctx); CHKERRV(ierr);
PetscRandomDestroy(&rctx);
}
void Vector::sort()
{
PetscErrorCode ierr = 0;
PetscInt size;
PetscReal *a;
ierr = VecGetArray(vector, &a); CHKERRV(ierr);
ierr = VecGetSize(vector, &size);
ierr = PetscSortReal(size, a); CHKERRV(ierr);
ierr = VecRestoreArray(vector, &a); CHKERRV(ierr);
}
void Vector::assemble()
{
PetscErrorCode ierr = 0;
ierr = VecAssemblyBegin(vector); CHKERRV(ierr);
ierr = VecAssemblyEnd(vector); CHKERRV(ierr);
}
std::pair<PetscInt, PetscInt> Vector::ownerRange()
{
PetscInt range_start, range_end;
VecGetOwnershipRange(vector, &range_start, &range_end);
return std::make_pair(range_start, range_end);
}
void Vector::write(std::string filename, VIEWERFORMAT format)
{
PetscErrorCode ierr = 0;
PetscViewer viewer;
openViewer(viewer, filename, format, getCommunicator(vector));
VecView(vector, viewer); CHKERRV(ierr);
PetscViewerDestroy(&viewer);
}
void Vector::read(std::string filename, VIEWERFORMAT format)
{
PetscErrorCode ierr = 0;
PetscViewer viewer;
openViewer(viewer, filename, format, getCommunicator(vector));
VecLoad(vector, viewer); CHKERRV(ierr); CHKERRV(ierr);
PetscViewerDestroy(&viewer);
}
void Vector::view()
{
PetscErrorCode ierr;
ierr = VecView(vector, PETSC_VIEWER_STDOUT_WORLD); CHKERRV(ierr);
}
/////////////////////////////////////////////////////////////////////////
Matrix::Matrix(std::string name)
{
PetscErrorCode ierr = 0;
ierr = MatCreate(PETSC_COMM_WORLD, &matrix); CHKERRV(ierr);
setName(matrix, name);
}
Matrix::~Matrix()
{
PetscErrorCode ierr = 0;
PetscBool petscIsInitialized;
PetscInitialized(&petscIsInitialized);
if (petscIsInitialized) // If PetscFinalize is called before ~Matrix
ierr = MatDestroy(&matrix); CHKERRV(ierr);
}
Matrix::operator Mat&()
{
return matrix;
}
void Matrix::assemble(MatAssemblyType type)
{
PetscErrorCode ierr = 0;
ierr = MatAssemblyBegin(matrix, type); CHKERRV(ierr);
ierr = MatAssemblyEnd(matrix, type); CHKERRV(ierr);
}
void Matrix::init(PetscInt localRows, PetscInt localCols, PetscInt globalRows, PetscInt globalCols,
MatType type, bool doSetup)
{
PetscErrorCode ierr = 0;
if (type != nullptr) {
ierr = MatSetType(matrix, type); CHKERRV(ierr);
}
ierr = MatSetSizes(matrix, localRows, localCols, globalRows, globalCols); CHKERRV(ierr);
ierr = MatSetFromOptions(matrix); CHKERRV(ierr);
if (doSetup)
ierr = MatSetUp(matrix); CHKERRV(ierr);
}
void Matrix::reset()
{
PetscErrorCode ierr = 0;
std::string name = getName(matrix);
ierr = MatDestroy(&matrix); CHKERRV(ierr);
ierr = MatCreate(PETSC_COMM_WORLD, &matrix); CHKERRV(ierr);
setName(matrix, name);
}
MatInfo Matrix::getInfo(MatInfoType flag)
{
MatInfo info;
MatGetInfo(matrix, flag, &info);
return info;
}
void Matrix::setValue(PetscInt row, PetscInt col, PetscScalar value)
{
PetscErrorCode ierr = 0;
ierr = MatSetValue(matrix, row, col, value, INSERT_VALUES); CHKERRV(ierr);
}
void Matrix::fillWithRandoms()
{
PetscErrorCode ierr = 0;
PetscRandom rctx;
std::random_device rd;
std::uniform_real_distribution<double> dist(0, 1);
PetscRandomCreate(getCommunicator(matrix), &rctx);
PetscRandomSetType(rctx, PETSCRAND48);
PetscRandomSetSeed(rctx, dist(rd));
PetscRandomSeed(rctx);
ierr = MatSetRandom(matrix, rctx); CHKERRV(ierr);
PetscRandomDestroy(&rctx);
}
void Matrix::setColumn(Vector &v, int col)
{
PetscErrorCode ierr = 0;
const PetscScalar *vec;
PetscInt range_start, range_end;
VecGetOwnershipRange(v.vector, &range_start, &range_end);
std::vector<PetscInt> irow(range_end - range_start);
std::iota(irow.begin(), irow.end(), range_start);
ierr = VecGetArrayRead(v.vector, &vec); CHKERRV(ierr);
ierr = MatSetValues(matrix, range_end - range_start, irow.data(), 1, &col, vec, INSERT_VALUES); CHKERRV(ierr);
ierr = VecRestoreArrayRead(v.vector, &vec); CHKERRV(ierr);
ierr = MatAssemblyBegin(matrix, MAT_FINAL_ASSEMBLY); CHKERRV(ierr);
ierr = MatAssemblyEnd(matrix, MAT_FINAL_ASSEMBLY); CHKERRV(ierr);
}
std::pair<PetscInt, PetscInt> Matrix::getSize()
{
PetscInt m, n;
MatGetSize(matrix, &m, &n);
return std::make_pair(m, n);
}
std::pair<PetscInt, PetscInt> Matrix::getLocalSize()
{
PetscInt m, n;
MatGetLocalSize(matrix, &m, &n);
return std::make_pair(m, n);
}
std::pair<PetscInt, PetscInt> Matrix::ownerRange()
{
PetscInt range_start, range_end;
MatGetOwnershipRange(matrix, &range_start, &range_end);
return std::make_pair(range_start, range_end);
}
std::pair<PetscInt, PetscInt> Matrix::ownerRangeColumn()
{
PetscInt range_start, range_end;
MatGetOwnershipRangeColumn(matrix, &range_start, &range_end);
return std::make_pair(range_start, range_end);
}
PetscInt Matrix::blockSize() const
{
PetscErrorCode ierr = 0;
PetscInt bs;
ierr = MatGetBlockSize(matrix, &bs); CHKERRQ(ierr);
return bs;
}
void Matrix::write(std::string filename, VIEWERFORMAT format)
{
PetscErrorCode ierr = 0;
PetscViewer viewer;
openViewer(viewer, filename, format, getCommunicator(matrix));
ierr = MatView(matrix, viewer); CHKERRV(ierr);
PetscViewerDestroy(&viewer);
}
void Matrix::read(std::string filename)
{
PetscErrorCode ierr = 0;
PetscViewer viewer;
openViewer(viewer, filename, BINARY, getCommunicator(matrix));
ierr = MatLoad(matrix, viewer); CHKERRV(ierr);
PetscViewerDestroy(&viewer);
}
void Matrix::view()
{
PetscErrorCode ierr = 0;
PetscViewer viewer;
ierr = PetscViewerCreate(getCommunicator(matrix), &viewer); CHKERRV(ierr);
ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII); CHKERRV(ierr);
ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_DENSE); CHKERRV(ierr);
ierr = MatView(matrix, viewer); CHKERRV(ierr);
ierr = PetscViewerPopFormat(viewer); CHKERRV(ierr);
ierr = PetscViewerDestroy(&viewer); CHKERRV(ierr);
}
void Matrix::viewDraw()
{
PetscErrorCode ierr = 0;
PetscViewer viewer;
PetscDraw draw;
ierr = PetscViewerCreate(getCommunicator(matrix), &viewer); CHKERRV(ierr);
ierr = PetscViewerSetType(viewer, PETSCVIEWERDRAW); CHKERRV(ierr);
ierr = MatView(matrix, viewer); CHKERRV(ierr);
ierr = PetscViewerDrawGetDraw(viewer, 0, &draw); CHKERRV(ierr);
ierr = PetscDrawSetPause(draw, -1); CHKERRV(ierr); // Wait for user
ierr = PetscViewerDestroy(&viewer); CHKERRV(ierr);
}
/////////////////////////////////////////////////////////////////////////
KSPSolver::KSPSolver(std::string name)
{
PetscErrorCode ierr = 0;
ierr = KSPCreate(PETSC_COMM_WORLD, &ksp); CHKERRV(ierr);
setName(ksp, name);
}
KSPSolver::~KSPSolver()
{
PetscErrorCode ierr = 0;
PetscBool petscIsInitialized;
PetscInitialized(&petscIsInitialized);
if (petscIsInitialized) // If PetscFinalize is called before ~KSPSolver
ierr = KSPDestroy(&ksp); CHKERRV(ierr);
}
KSPSolver::operator KSP&()
{
return ksp;
}
void KSPSolver::reset()
{
PetscErrorCode ierr = 0;
ierr = KSPReset(ksp); CHKERRV(ierr);
}
bool KSPSolver::solve(Vector &b, Vector &x)
{
PetscErrorCode ierr = 0;
KSPConvergedReason convReason;
KSPSolve(ksp, b, x);
ierr = KSPGetConvergedReason(ksp, &convReason); CHKERRQ(ierr);
return (convReason > 0);
}
/////////////////////////////////////////////////////////////////////////
void destroy(ISLocalToGlobalMapping * IS)
{
PetscErrorCode ierr = 0;
PetscBool petscIsInitialized;
PetscInitialized(&petscIsInitialized);
if (IS and petscIsInitialized) {
ierr = ISLocalToGlobalMappingDestroy(IS); CHKERRV(ierr);
}
}
}