-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcilupack.c
327 lines (274 loc) · 10.4 KB
/
pcilupack.c
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
#include "ilupack.h" /* has to be FIRST to avoid warnings about _Complex */
#include "pcilupack.h"
#include <petsc/private/pcimpl.h>
/* Hard-coded - this will only work in double */
#define FLOAT double
/*
#define PRINT_INFO
*/
#define PCILUPACK_FILL_INFO
typedef struct {
ILUPACKparam param;
SPARSEmat B;
AMGlevelmat PRE;
integer *ia, *ja;
FLOAT *a;
PetscReal droptol;
PetscReal droptolS;
PetscReal condest;
PetscBool ilupackinit;
} PC_ILUPACK;
/* A local function with the ILUpack setup calls */
static PetscErrorCode ILUPACKSetUp(PC_ILUPACK *ilupack,PetscInt nA,PetscScalar val[],PetscInt row_ptr[],PetscInt col_ind[])
{
integer i,j,k,l,nnz=0;
int ilupackerr;
integer *ia,*ja,nB;
FLOAT *a; /* convenience */
PetscFunctionBegin;
/* We assume that these types are the same. We have lazily left code referring to all types, but have removed conversions
that would be required to use different types */
if (sizeof(FLOAT) != sizeof(PetscScalar)) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"ILUPACK must use the same floating point type as PETSc!");
if (sizeof(double) != sizeof(PetscScalar)) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"PCILUPACK only supports double for now!");
if (sizeof(integer) != sizeof(PetscInt)) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"ILUPACK must use the same integer type as PETSc!");
nB = (integer)nA;
#if defined(PRINT_INFO)
for (i=0; i<nB; i++) {
printf("%d : ",i);
for (j=row_ptr[i]; j<row_ptr[i+1]; j++) {
k=col_ind[j];
printf("(%d,%f) ",k,val[j]);
}
printf("\n");
}
#endif
// logical pointer array, initialized with 0
ilupack->ia=(integer *)calloc((nB+1),sizeof(integer));
ia=ilupack->ia;
// count number of nonzeros in the upper triangular part
for (i=0; i<nB; i++) {
for (j=row_ptr[i]; j<row_ptr[i+1]; j++) {
k=col_ind[j];
// upper triangular part only
if (k>=i) {
// count nnz by row, do not use ia[0] (see below, why)
ia[i+1]++;
nnz++;
}
}
}
// switch from nz to pointer. Now this refers to the physical beginning of each (empty) row
// this is why we used ia[1],...,ia[nB] previously and left ia[0] untouched
for (i=0; i<nB; i++)
ia[i+1]+=ia[i];
// array of column indices
ilupack->ja=(integer *)malloc(nnz*sizeof(integer));
ja=ilupack->ja;
// array of numerical values
ilupack->a =(FLOAT *) malloc(nnz*sizeof(FLOAT));
a=ilupack->a;
// extract upper triangular part
for (i=0; i<nB; i++) {
for (j=row_ptr[i]; j<row_ptr[i+1]; j++) {
k=col_ind[j];
// upper triangular part only
if (k>=i) {
// current start of the unoccupied part of row i
l=ia[i];
// FORTRAN starts indexing with 1
ja[l]=k+1;
// copy numerical value
a[l]=val[j];
// free part of row i incremented
ia[i]=l+1;
} // end if
} // end for j
} // end for i
// now the entries ia[0],...,ia[nB-1] of the pointer array must have those values
// that were previously stored as pointers in ia[1],...,ia[nB]
// shift pointers back, FORTRAN starts indexing with 1
for (i=nB; i>0; i--) ia[i]=ia[i-1]+1;
ia[0]=1;
// now ia,ja,a contain the upper triangular part of the matrix in FORTRAN format
/* Hard-coded! */
ilupack->B.isreal=1; /* real matrix */
ilupack->B.issymmetric=1; /* symmetric matrix */
ilupack->B.isdefinite=0; /* not positive definite */
ilupack->B.issingle=0; /* use double precision */
ilupack->B.nr = ilupack->B.nc=nB;
ilupack->B.nnz = nnz;
ilupack->B.ia = ilupack->ia;
ilupack->B.ja = ilupack->ja;
ilupack->B.a = (void*) ilupack->a;
#if defined(PRINT_INFO)
printf("nr : %d\n",ilupack->B.nr);
printf("nc : %d\n",ilupack->B.nc);
printf("nnz : %d\n",ilupack->B.nnz);
printf("ia : ");
for (i=0; i<ilupack->B.nr+1; i++){
printf("%d ",ilupack->B.ia[i]);
}
printf("\n");
printf("ja : ");
for (i=0; i<ilupack->B.nnz; i++){
printf("%d ",ilupack->B.ja[i]);
}
printf("\n");
printf("a : ");
for (i=0; i<ilupack->B.nnz; i++){
printf("%f ",((FLOAT*)ilupack->B.a)[i]);
}
printf("\n");
#endif
/* initialize ILUPACK options structure to its default options */
AMGinit(&ilupack->B,&ilupack->param);
/* thresholds for ILU */
ilupack->param.droptol = (FLOAT) ilupack->droptol;
ilupack->param.droptolS= (FLOAT) ilupack->droptolS;
/* turn on matching */
ilupack->param.matching = 1;
/* METIS-by-nodes ordering */
ilupack->param.ordering = "metisn";
/* Parameter which controls how many levels.
Low (~5)-->many levels (probably what you want) */
ilupack->param.condest=(FLOAT) ilupack->condest;
/* Call ILUPack Setup routine */
#if 1
ilupackerr=AMGfactor(&ilupack->B, &ilupack->PRE, &ilupack->param);
if (ilupackerr) {
SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_LIB,"ILUPACK encountered an error during factorization: %d",ilupackerr);
} else {
#if defined(PCILUPACK_FILL_INFO)
PetscErrorCode ierr;
ierr=PetscPrintf(PETSC_COMM_WORLD,"factorization successful with %d levels completed\n", ilupack->PRE.nlev);CHKERRQ(ierr);
ierr=PetscPrintf(PETSC_COMM_WORLD,"final elbow space factor=%8.2f\n",ilupack->param.elbow+0.005);CHKERRQ(ierr);
#endif
}
ilupack->ilupackinit=PETSC_TRUE;
#endif
PetscFunctionReturn(0);
}
#undef __FUNCT__
#define __FUNCT__ "PCSetUp_ILUPACK"
static PetscErrorCode PCSetUp_ILUPACK(PC pc)
{
PC_ILUPACK *ilupack = (PC_ILUPACK*)pc->data;
PetscErrorCode ierr;
Mat A;
PetscBool issame;
PetscFunctionBegin;
ierr = PCGetOperators(pc,&A,NULL);CHKERRQ(ierr);
ierr = PetscObjectTypeCompare((PetscObject)A,MATSEQAIJ,&issame);CHKERRQ(ierr);
if (issame) {
PetscInt nA;
const PetscInt *col_ind,*row_ptr;
PetscScalar *val;
const PetscBool shift=PETSC_FALSE,symmetric=PETSC_FALSE,inodecompressed=PETSC_FALSE;
PetscBool done;
/* Get Raw CSR data (Note that we are being wasteful and not using sym types) */
ierr = MatSeqAIJGetArray(A,&val);CHKERRQ(ierr); /* PETSc doesn't have a "Read" version of this, hence val isn't marked const */
ierr = MatGetRowIJ(A,shift,symmetric,inodecompressed,&nA,&row_ptr,&col_ind,&done);CHKERRQ(ierr);
/* ILUPACK setup (local function) */
ierr = ILUPACKSetUp(ilupack,nA,(PetscScalar*)val,(PetscInt*)row_ptr,(PetscInt*)col_ind);CHKERRQ(ierr);
ierr = MatRestoreRowIJ(A,shift,symmetric,inodecompressed,&nA,&row_ptr,&col_ind,&done);CHKERRQ(ierr);
ierr = MatSeqAIJRestoreArray(A,&val);CHKERRQ(ierr);
} else SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Only valid for MATSEQAIJ"); /* Yes, this sucks - should be SBAIJ but more work */
PetscFunctionReturn(0);
}
#undef __FUNCT__
#define __FUNCT__ "PCApply_ILUPACK"
static PetscErrorCode PCApply_ILUPACK(PC pc,Vec vecB,Vec vecX)
{
PC_ILUPACK *ilupack = (PC_ILUPACK*)pc->data;
PetscInt nB;
FLOAT *x,*b;
PetscErrorCode ierr;
PetscFunctionBegin;
ierr = VecGetSize(vecB,&nB);CHKERRQ(ierr); /* note this is uniprocessor only */
ierr = VecGetArrayRead(vecB,&b);CHKERRQ(ierr); /* Not read-only, assumes type, a bit ugly */
ierr = VecGetArray(vecX,&x);CHKERRQ(ierr);
AMGsol(&ilupack->PRE,&ilupack->param,b,x);
ierr = VecRestoreArray(vecX,&x);CHKERRQ(ierr);
ierr = VecRestoreArrayRead(vecB,&b);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
#undef __FUNCT__
#define __FUNCT__ "PCReset_ILUPACK"
static PetscErrorCode PCReset_ILUPACK(PC pc)
{
PC_ILUPACK *ilupack = (PC_ILUPACK*)pc->data;
PetscFunctionBegin;
if (ilupack->ilupackinit){
ilupack->ilupackinit=PETSC_FALSE;
AMGdelete(&ilupack->B,&ilupack->PRE,&ilupack->param);
}
if(ilupack->ia) {free(ilupack->ia); ilupack->ia=NULL;}
if(ilupack->ja) {free(ilupack->ja); ilupack->ja=NULL;}
if(ilupack->a) {free(ilupack->a); ilupack->a=NULL;}
PetscFunctionReturn(0);
}
#undef __FUNCT__
#define __FUNCT__ "PCDestroy_ILUPACK"
static PetscErrorCode PCDestroy_ILUPACK(PC pc)
{
PetscErrorCode ierr;
PetscFunctionBegin;
ierr = PCReset_ILUPACK(pc);CHKERRQ(ierr);
ierr = PetscFree(pc->data);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
#undef __FUNCT__
#define __FUNCT__ "PCSetFromOptions_ILUPACK"
static PetscErrorCode PCSetFromOptions_ILUPACK(PetscOptionItems *PetscOptionsObject,PC pc)
{
PC_ILUPACK *ilupack = (PC_ILUPACK*)pc->data;
PetscErrorCode ierr;
PetscFunctionBegin;
ierr = PetscOptionsHead(PetscOptionsObject,"ILUPACK options");CHKERRQ(ierr);
ierr = PetscOptionsReal("-pc_ilupack_droptol","Set drop tol",NULL,ilupack->droptol,&ilupack->droptol,NULL);CHKERRQ(ierr);
ierr = PetscOptionsReal("-pc_ilupack_droptolS","Set Schur complement drop tol",NULL,ilupack->droptolS,&ilupack->droptolS,NULL);CHKERRQ(ierr);
ierr = PetscOptionsReal("-pc_ilupack_condest","Set condition number used to choose level structure",NULL,ilupack->condest,&ilupack->condest,NULL);CHKERRQ(ierr);
ierr = PetscOptionsTail();CHKERRQ(ierr);
PetscFunctionReturn(0);
}
#undef __FUNCT__
#define __FUNCT__ "PCView_ILUPACK"
static PetscErrorCode PCView_ILUPACK(PC pc,PetscViewer viewer)
{
PC_ILUPACK *ilupack = (PC_ILUPACK*)pc->data;
PetscErrorCode ierr;
PetscBool iascii;
PetscFunctionBegin;
ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
if (iascii) {
ierr = PetscViewerASCIIPrintf(viewer," ILUPACK: droptol : %g\n",ilupack->droptol );CHKERRQ(ierr);
ierr = PetscViewerASCIIPrintf(viewer," ILUPACK: droptolS : %g\n",ilupack->droptolS);CHKERRQ(ierr);
ierr = PetscViewerASCIIPrintf(viewer," ILUPACK: condest : %g\n",ilupack->condest );CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
#undef __FUNCT__
#define __FUNCT__ "PCCreate_ILUPACK"
PETSC_EXTERN PetscErrorCode PCCreate_ILUPACK(PC pc)
{
PC_ILUPACK *ilupack;
PetscErrorCode ierr;
PetscFunctionBegin;
ierr = PetscNewLog(pc,&ilupack);CHKERRQ(ierr);
pc->data = (void*)ilupack;
pc->ops->apply = PCApply_ILUPACK;
pc->ops->applytranspose = 0;
pc->ops->setup = PCSetUp_ILUPACK;
pc->ops->reset = PCReset_ILUPACK;
pc->ops->destroy = PCDestroy_ILUPACK;
pc->ops->setfromoptions = PCSetFromOptions_ILUPACK;
pc->ops->view = PCView_ILUPACK;
pc->ops->applyrichardson = 0;
pc->ops->applysymmetricleft = 0;
pc->ops->applysymmetricright = 0;
ilupack->ilupackinit = PETSC_FALSE;
ilupack->droptol = 0.01;
ilupack->droptolS = 0.01;
ilupack->condest = 10.0;
PetscFunctionReturn(0);
}