Actual source code: sbaijfact.c
petsc-3.7.3 2016-08-01
2: #include <../src/mat/impls/baij/seq/baij.h>
3: #include <../src/mat/impls/sbaij/seq/sbaij.h>
4: #include <petsc/private/kernels/blockinvert.h>
5: #include <petscis.h>
7: /*
8: input:
9: F -- numeric factor
10: output:
11: nneg, nzero, npos: matrix inertia
12: */
16: PetscErrorCode MatGetInertia_SeqSBAIJ(Mat F,PetscInt *nneig,PetscInt *nzero,PetscInt *npos)
17: {
18: Mat_SeqSBAIJ *fact_ptr = (Mat_SeqSBAIJ*)F->data;
19: MatScalar *dd = fact_ptr->a;
20: PetscInt mbs =fact_ptr->mbs,bs=F->rmap->bs,i,nneig_tmp,npos_tmp,*fi = fact_ptr->diag;
23: if (bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for bs: %D >1 yet",bs);
24: nneig_tmp = 0; npos_tmp = 0;
25: for (i=0; i<mbs; i++) {
26: if (PetscRealPart(dd[*fi]) > 0.0) npos_tmp++;
27: else if (PetscRealPart(dd[*fi]) < 0.0) nneig_tmp++;
28: fi++;
29: }
30: if (nneig) *nneig = nneig_tmp;
31: if (npos) *npos = npos_tmp;
32: if (nzero) *nzero = mbs - nneig_tmp - npos_tmp;
33: return(0);
34: }
36: /*
37: Symbolic U^T*D*U factorization for SBAIJ format. Modified from SSF of YSMP.
38: Use Modified Sparse Row (MSR) storage for u and ju. See page 85, "Iterative Methods ..." by Saad.
39: */
42: PetscErrorCode MatCholeskyFactorSymbolic_SeqSBAIJ_MSR(Mat F,Mat A,IS perm,const MatFactorInfo *info)
43: {
44: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data,*b;
46: const PetscInt *rip,*ai,*aj;
47: PetscInt i,mbs = a->mbs,*jutmp,bs = A->rmap->bs,bs2=a->bs2;
48: PetscInt m,reallocs = 0,prow;
49: PetscInt *jl,*q,jmin,jmax,juidx,nzk,qm,*iu,*ju,k,j,vj,umax,maxadd;
50: PetscReal f = info->fill;
51: PetscBool perm_identity;
54: /* check whether perm is the identity mapping */
55: ISIdentity(perm,&perm_identity);
56: ISGetIndices(perm,&rip);
58: if (perm_identity) { /* without permutation */
59: a->permute = PETSC_FALSE;
61: ai = a->i; aj = a->j;
62: } else { /* non-trivial permutation */
63: a->permute = PETSC_TRUE;
65: MatReorderingSeqSBAIJ(A,perm);
67: ai = a->inew; aj = a->jnew;
68: }
70: /* initialization */
71: PetscMalloc1(mbs+1,&iu);
72: umax = (PetscInt)(f*ai[mbs] + 1); umax += mbs + 1;
73: PetscMalloc1(umax,&ju);
74: iu[0] = mbs+1;
75: juidx = mbs + 1; /* index for ju */
76: /* jl linked list for pivot row -- linked list for col index */
77: PetscMalloc2(mbs,&jl,mbs,&q);
78: for (i=0; i<mbs; i++) {
79: jl[i] = mbs;
80: q[i] = 0;
81: }
83: /* for each row k */
84: for (k=0; k<mbs; k++) {
85: for (i=0; i<mbs; i++) q[i] = 0; /* to be removed! */
86: nzk = 0; /* num. of nz blocks in k-th block row with diagonal block excluded */
87: q[k] = mbs;
88: /* initialize nonzero structure of k-th row to row rip[k] of A */
89: jmin = ai[rip[k]] +1; /* exclude diag[k] */
90: jmax = ai[rip[k]+1];
91: for (j=jmin; j<jmax; j++) {
92: vj = rip[aj[j]]; /* col. value */
93: if (vj > k) {
94: qm = k;
95: do {
96: m = qm; qm = q[m];
97: } while (qm < vj);
98: if (qm == vj) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Duplicate entry in A\n");
99: nzk++;
100: q[m] = vj;
101: q[vj] = qm;
102: } /* if (vj > k) */
103: } /* for (j=jmin; j<jmax; j++) */
105: /* modify nonzero structure of k-th row by computing fill-in
106: for each row i to be merged in */
107: prow = k;
108: prow = jl[prow]; /* next pivot row (== mbs for symbolic factorization) */
110: while (prow < k) {
111: /* merge row prow into k-th row */
112: jmin = iu[prow] + 1; jmax = iu[prow+1];
113: qm = k;
114: for (j=jmin; j<jmax; j++) {
115: vj = ju[j];
116: do {
117: m = qm; qm = q[m];
118: } while (qm < vj);
119: if (qm != vj) {
120: nzk++; q[m] = vj; q[vj] = qm; qm = vj;
121: }
122: }
123: prow = jl[prow]; /* next pivot row */
124: }
126: /* add k to row list for first nonzero element in k-th row */
127: if (nzk > 0) {
128: i = q[k]; /* col value of first nonzero element in U(k, k+1:mbs-1) */
129: jl[k] = jl[i]; jl[i] = k;
130: }
131: iu[k+1] = iu[k] + nzk;
133: /* allocate more space to ju if needed */
134: if (iu[k+1] > umax) {
135: /* estimate how much additional space we will need */
136: /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */
137: /* just double the memory each time */
138: maxadd = umax;
139: if (maxadd < nzk) maxadd = (mbs-k)*(nzk+1)/2;
140: umax += maxadd;
142: /* allocate a longer ju */
143: PetscMalloc1(umax,&jutmp);
144: PetscMemcpy(jutmp,ju,iu[k]*sizeof(PetscInt));
145: PetscFree(ju);
146: ju = jutmp;
147: reallocs++; /* count how many times we realloc */
148: }
150: /* save nonzero structure of k-th row in ju */
151: i=k;
152: while (nzk--) {
153: i = q[i];
154: ju[juidx++] = i;
155: }
156: }
158: #if defined(PETSC_USE_INFO)
159: if (ai[mbs] != 0) {
160: PetscReal af = ((PetscReal)iu[mbs])/((PetscReal)ai[mbs]);
161: PetscInfo3(A,"Reallocs %D Fill ratio:given %g needed %g\n",reallocs,(double)f,(double)af);
162: PetscInfo1(A,"Run with -pc_factor_fill %g or use \n",(double)af);
163: PetscInfo1(A,"PCFactorSetFill(pc,%g);\n",(double)af);
164: PetscInfo(A,"for best performance.\n");
165: } else {
166: PetscInfo(A,"Empty matrix.\n");
167: }
168: #endif
170: ISRestoreIndices(perm,&rip);
171: PetscFree2(jl,q);
173: /* put together the new matrix */
174: MatSeqSBAIJSetPreallocation_SeqSBAIJ(F,bs,MAT_SKIP_ALLOCATION,NULL);
176: /* PetscLogObjectParent((PetscObject)B,(PetscObject)iperm); */
177: b = (Mat_SeqSBAIJ*)(F)->data;
178: b->singlemalloc = PETSC_FALSE;
179: b->free_a = PETSC_TRUE;
180: b->free_ij = PETSC_TRUE;
182: PetscMalloc1((iu[mbs]+1)*bs2,&b->a);
183: b->j = ju;
184: b->i = iu;
185: b->diag = 0;
186: b->ilen = 0;
187: b->imax = 0;
188: b->row = perm;
190: b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */
192: PetscObjectReference((PetscObject)perm);
194: b->icol = perm;
195: PetscObjectReference((PetscObject)perm);
196: PetscMalloc1(bs*mbs+bs,&b->solve_work);
197: /* In b structure: Free imax, ilen, old a, old j.
198: Allocate idnew, solve_work, new a, new j */
199: PetscLogObjectMemory((PetscObject)F,(iu[mbs]-mbs)*(sizeof(PetscInt)+sizeof(MatScalar)));
200: b->maxnz = b->nz = iu[mbs];
202: (F)->info.factor_mallocs = reallocs;
203: (F)->info.fill_ratio_given = f;
204: if (ai[mbs] != 0) {
205: (F)->info.fill_ratio_needed = ((PetscReal)iu[mbs])/((PetscReal)ai[mbs]);
206: } else {
207: (F)->info.fill_ratio_needed = 0.0;
208: }
209: MatSeqSBAIJSetNumericFactorization_inplace(F,perm_identity);
210: return(0);
211: }
212: /*
213: Symbolic U^T*D*U factorization for SBAIJ format.
214: See MatICCFactorSymbolic_SeqAIJ() for description of its data structure.
215: */
216: #include <petscbt.h>
217: #include <../src/mat/utils/freespace.h>
220: PetscErrorCode MatCholeskyFactorSymbolic_SeqSBAIJ(Mat fact,Mat A,IS perm,const MatFactorInfo *info)
221: {
222: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
223: Mat_SeqSBAIJ *b;
224: PetscErrorCode ierr;
225: PetscBool perm_identity,missing;
226: PetscReal fill = info->fill;
227: const PetscInt *rip,*ai=a->i,*aj=a->j;
228: PetscInt i,mbs=a->mbs,bs=A->rmap->bs,reallocs=0,prow;
229: PetscInt *jl,jmin,jmax,nzk,*ui,k,j,*il,nextprow;
230: PetscInt nlnk,*lnk,ncols,*cols,*uj,**ui_ptr,*uj_ptr,*udiag;
231: PetscFreeSpaceList free_space=NULL,current_space=NULL;
232: PetscBT lnkbt;
235: if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n);
236: MatMissingDiagonal(A,&missing,&i);
237: if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",i);
238: if (bs > 1) {
239: MatCholeskyFactorSymbolic_SeqSBAIJ_inplace(fact,A,perm,info);
240: return(0);
241: }
243: /* check whether perm is the identity mapping */
244: ISIdentity(perm,&perm_identity);
245: if (!perm_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix reordering is not supported for sbaij matrix. Use aij format");
246: a->permute = PETSC_FALSE;
247: ISGetIndices(perm,&rip);
249: /* initialization */
250: PetscMalloc1(mbs+1,&ui);
251: PetscMalloc1(mbs+1,&udiag);
252: ui[0] = 0;
254: /* jl: linked list for storing indices of the pivot rows
255: il: il[i] points to the 1st nonzero entry of U(i,k:mbs-1) */
256: PetscMalloc4(mbs,&ui_ptr,mbs,&il,mbs,&jl,mbs,&cols);
257: for (i=0; i<mbs; i++) {
258: jl[i] = mbs; il[i] = 0;
259: }
261: /* create and initialize a linked list for storing column indices of the active row k */
262: nlnk = mbs + 1;
263: PetscLLCreate(mbs,mbs,nlnk,lnk,lnkbt);
265: /* initial FreeSpace size is fill*(ai[mbs]+1) */
266: PetscFreeSpaceGet(PetscRealIntMultTruncate(fill,ai[mbs]+1),&free_space);
267: current_space = free_space;
269: for (k=0; k<mbs; k++) { /* for each active row k */
270: /* initialize lnk by the column indices of row rip[k] of A */
271: nzk = 0;
272: ncols = ai[k+1] - ai[k];
273: if (!ncols) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MAT_CH_ZRPVT,"Empty row %D in matrix ",k);
274: for (j=0; j<ncols; j++) {
275: i = *(aj + ai[k] + j);
276: cols[j] = i;
277: }
278: PetscLLAdd(ncols,cols,mbs,nlnk,lnk,lnkbt);
279: nzk += nlnk;
281: /* update lnk by computing fill-in for each pivot row to be merged in */
282: prow = jl[k]; /* 1st pivot row */
284: while (prow < k) {
285: nextprow = jl[prow];
286: /* merge prow into k-th row */
287: jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:mbs-1) */
288: jmax = ui[prow+1];
289: ncols = jmax-jmin;
290: uj_ptr = ui_ptr[prow] + jmin - ui[prow]; /* points to the 2nd nzero entry in U(prow,k:mbs-1) */
291: PetscLLAddSorted(ncols,uj_ptr,mbs,nlnk,lnk,lnkbt);
292: nzk += nlnk;
294: /* update il and jl for prow */
295: if (jmin < jmax) {
296: il[prow] = jmin;
297: j = *uj_ptr; jl[prow] = jl[j]; jl[j] = prow;
298: }
299: prow = nextprow;
300: }
302: /* if free space is not available, make more free space */
303: if (current_space->local_remaining<nzk) {
304: i = mbs - k + 1; /* num of unfactored rows */
305: i = PetscIntMultTruncate(i,PetscMin(nzk, i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */
306: PetscFreeSpaceGet(i,¤t_space);
307: reallocs++;
308: }
310: /* copy data into free space, then initialize lnk */
311: PetscLLClean(mbs,mbs,nzk,lnk,current_space->array,lnkbt);
313: /* add the k-th row into il and jl */
314: if (nzk > 1) {
315: i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:mbs-1) */
316: jl[k] = jl[i]; jl[i] = k;
317: il[k] = ui[k] + 1;
318: }
319: ui_ptr[k] = current_space->array;
321: current_space->array += nzk;
322: current_space->local_used += nzk;
323: current_space->local_remaining -= nzk;
325: ui[k+1] = ui[k] + nzk;
326: }
328: ISRestoreIndices(perm,&rip);
329: PetscFree4(ui_ptr,il,jl,cols);
331: /* destroy list of free space and other temporary array(s) */
332: PetscMalloc1(ui[mbs]+1,&uj);
333: PetscFreeSpaceContiguous_Cholesky(&free_space,uj,mbs,ui,udiag); /* store matrix factor */
334: PetscLLDestroy(lnk,lnkbt);
336: /* put together the new matrix in MATSEQSBAIJ format */
337: MatSeqSBAIJSetPreallocation_SeqSBAIJ(fact,bs,MAT_SKIP_ALLOCATION,NULL);
339: b = (Mat_SeqSBAIJ*)fact->data;
340: b->singlemalloc = PETSC_FALSE;
341: b->free_a = PETSC_TRUE;
342: b->free_ij = PETSC_TRUE;
344: PetscMalloc1(ui[mbs]+1,&b->a);
346: b->j = uj;
347: b->i = ui;
348: b->diag = udiag;
349: b->free_diag = PETSC_TRUE;
350: b->ilen = 0;
351: b->imax = 0;
352: b->row = perm;
353: b->icol = perm;
355: PetscObjectReference((PetscObject)perm);
356: PetscObjectReference((PetscObject)perm);
358: b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */
360: PetscMalloc1(mbs+1,&b->solve_work);
361: PetscLogObjectMemory((PetscObject)fact,ui[mbs]*(sizeof(PetscInt)+sizeof(MatScalar)));
363: b->maxnz = b->nz = ui[mbs];
365: fact->info.factor_mallocs = reallocs;
366: fact->info.fill_ratio_given = fill;
367: if (ai[mbs] != 0) {
368: fact->info.fill_ratio_needed = ((PetscReal)ui[mbs])/ai[mbs];
369: } else {
370: fact->info.fill_ratio_needed = 0.0;
371: }
372: #if defined(PETSC_USE_INFO)
373: if (ai[mbs] != 0) {
374: PetscReal af = fact->info.fill_ratio_needed;
375: PetscInfo3(A,"Reallocs %D Fill ratio:given %g needed %g\n",reallocs,(double)fill,(double)af);
376: PetscInfo1(A,"Run with -pc_factor_fill %g or use \n",(double)af);
377: PetscInfo1(A,"PCFactorSetFill(pc,%g) for best performance.\n",(double)af);
378: } else {
379: PetscInfo(A,"Empty matrix.\n");
380: }
381: #endif
382: fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering;
383: return(0);
384: }
388: PetscErrorCode MatCholeskyFactorSymbolic_SeqSBAIJ_inplace(Mat fact,Mat A,IS perm,const MatFactorInfo *info)
389: {
390: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
391: Mat_SeqSBAIJ *b;
392: PetscErrorCode ierr;
393: PetscBool perm_identity,missing;
394: PetscReal fill = info->fill;
395: const PetscInt *rip,*ai,*aj;
396: PetscInt i,mbs=a->mbs,bs=A->rmap->bs,reallocs=0,prow,d;
397: PetscInt *jl,jmin,jmax,nzk,*ui,k,j,*il,nextprow;
398: PetscInt nlnk,*lnk,ncols,*cols,*uj,**ui_ptr,*uj_ptr;
399: PetscFreeSpaceList free_space=NULL,current_space=NULL;
400: PetscBT lnkbt;
403: MatMissingDiagonal(A,&missing,&d);
404: if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d);
406: /*
407: This code originally uses Modified Sparse Row (MSR) storage
408: (see page 85, "Iterative Methods ..." by Saad) for the output matrix B - bad choise!
409: Then it is rewritten so the factor B takes seqsbaij format. However the associated
410: MatCholeskyFactorNumeric_() have not been modified for the cases of bs>1 or !perm_identity,
411: thus the original code in MSR format is still used for these cases.
412: The code below should replace MatCholeskyFactorSymbolic_SeqSBAIJ_MSR() whenever
413: MatCholeskyFactorNumeric_() is modified for using sbaij symbolic factor.
414: */
415: if (bs > 1) {
416: MatCholeskyFactorSymbolic_SeqSBAIJ_MSR(fact,A,perm,info);
417: return(0);
418: }
420: /* check whether perm is the identity mapping */
421: ISIdentity(perm,&perm_identity);
423: if (perm_identity) {
424: a->permute = PETSC_FALSE;
426: ai = a->i; aj = a->j;
427: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix reordering is not supported for sbaij matrix. Use aij format");
428: ISGetIndices(perm,&rip);
430: /* initialization */
431: PetscMalloc1(mbs+1,&ui);
432: ui[0] = 0;
434: /* jl: linked list for storing indices of the pivot rows
435: il: il[i] points to the 1st nonzero entry of U(i,k:mbs-1) */
436: PetscMalloc4(mbs,&ui_ptr,mbs,&il,mbs,&jl,mbs,&cols);
437: for (i=0; i<mbs; i++) {
438: jl[i] = mbs; il[i] = 0;
439: }
441: /* create and initialize a linked list for storing column indices of the active row k */
442: nlnk = mbs + 1;
443: PetscLLCreate(mbs,mbs,nlnk,lnk,lnkbt);
445: /* initial FreeSpace size is fill*(ai[mbs]+1) */
446: PetscFreeSpaceGet(PetscRealIntMultTruncate(fill,ai[mbs]+1),&free_space);
447: current_space = free_space;
449: for (k=0; k<mbs; k++) { /* for each active row k */
450: /* initialize lnk by the column indices of row rip[k] of A */
451: nzk = 0;
452: ncols = ai[rip[k]+1] - ai[rip[k]];
453: for (j=0; j<ncols; j++) {
454: i = *(aj + ai[rip[k]] + j);
455: cols[j] = rip[i];
456: }
457: PetscLLAdd(ncols,cols,mbs,nlnk,lnk,lnkbt);
458: nzk += nlnk;
460: /* update lnk by computing fill-in for each pivot row to be merged in */
461: prow = jl[k]; /* 1st pivot row */
463: while (prow < k) {
464: nextprow = jl[prow];
465: /* merge prow into k-th row */
466: jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:mbs-1) */
467: jmax = ui[prow+1];
468: ncols = jmax-jmin;
469: uj_ptr = ui_ptr[prow] + jmin - ui[prow]; /* points to the 2nd nzero entry in U(prow,k:mbs-1) */
470: PetscLLAddSorted(ncols,uj_ptr,mbs,nlnk,lnk,lnkbt);
471: nzk += nlnk;
473: /* update il and jl for prow */
474: if (jmin < jmax) {
475: il[prow] = jmin;
477: j = *uj_ptr; jl[prow] = jl[j]; jl[j] = prow;
478: }
479: prow = nextprow;
480: }
482: /* if free space is not available, make more free space */
483: if (current_space->local_remaining<nzk) {
484: i = mbs - k + 1; /* num of unfactored rows */
485: i = PetscMin(PetscIntMultTruncate(i,nzk), PetscIntMultTruncate(i,i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */
486: PetscFreeSpaceGet(i,¤t_space);
487: reallocs++;
488: }
490: /* copy data into free space, then initialize lnk */
491: PetscLLClean(mbs,mbs,nzk,lnk,current_space->array,lnkbt);
493: /* add the k-th row into il and jl */
494: if (nzk-1 > 0) {
495: i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:mbs-1) */
496: jl[k] = jl[i]; jl[i] = k;
497: il[k] = ui[k] + 1;
498: }
499: ui_ptr[k] = current_space->array;
501: current_space->array += nzk;
502: current_space->local_used += nzk;
503: current_space->local_remaining -= nzk;
505: ui[k+1] = ui[k] + nzk;
506: }
508: ISRestoreIndices(perm,&rip);
509: PetscFree4(ui_ptr,il,jl,cols);
511: /* destroy list of free space and other temporary array(s) */
512: PetscMalloc1(ui[mbs]+1,&uj);
513: PetscFreeSpaceContiguous(&free_space,uj);
514: PetscLLDestroy(lnk,lnkbt);
516: /* put together the new matrix in MATSEQSBAIJ format */
517: MatSeqSBAIJSetPreallocation_SeqSBAIJ(fact,bs,MAT_SKIP_ALLOCATION,NULL);
519: b = (Mat_SeqSBAIJ*)fact->data;
520: b->singlemalloc = PETSC_FALSE;
521: b->free_a = PETSC_TRUE;
522: b->free_ij = PETSC_TRUE;
524: PetscMalloc1(ui[mbs]+1,&b->a);
526: b->j = uj;
527: b->i = ui;
528: b->diag = 0;
529: b->ilen = 0;
530: b->imax = 0;
531: b->row = perm;
533: b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */
535: PetscObjectReference((PetscObject)perm);
536: b->icol = perm;
537: PetscObjectReference((PetscObject)perm);
538: PetscMalloc1(mbs+1,&b->solve_work);
539: PetscLogObjectMemory((PetscObject)fact,(ui[mbs]-mbs)*(sizeof(PetscInt)+sizeof(MatScalar)));
540: b->maxnz = b->nz = ui[mbs];
542: fact->info.factor_mallocs = reallocs;
543: fact->info.fill_ratio_given = fill;
544: if (ai[mbs] != 0) {
545: fact->info.fill_ratio_needed = ((PetscReal)ui[mbs])/ai[mbs];
546: } else {
547: fact->info.fill_ratio_needed = 0.0;
548: }
549: #if defined(PETSC_USE_INFO)
550: if (ai[mbs] != 0) {
551: PetscReal af = fact->info.fill_ratio_needed;
552: PetscInfo3(A,"Reallocs %D Fill ratio:given %g needed %g\n",reallocs,(double)fill,(double)af);
553: PetscInfo1(A,"Run with -pc_factor_fill %g or use \n",(double)af);
554: PetscInfo1(A,"PCFactorSetFill(pc,%g) for best performance.\n",(double)af);
555: } else {
556: PetscInfo(A,"Empty matrix.\n");
557: }
558: #endif
559: MatSeqSBAIJSetNumericFactorization_inplace(fact,perm_identity);
560: return(0);
561: }
565: PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_N(Mat C,Mat A,const MatFactorInfo *info)
566: {
567: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data,*b = (Mat_SeqSBAIJ*)C->data;
568: IS perm = b->row;
570: const PetscInt *ai,*aj,*perm_ptr,mbs=a->mbs,*bi=b->i,*bj=b->j;
571: PetscInt i,j;
572: PetscInt *a2anew,k,k1,jmin,jmax,*jl,*il,vj,nexti,ili;
573: PetscInt bs =A->rmap->bs,bs2 = a->bs2;
574: MatScalar *ba = b->a,*aa,*ap,*dk,*uik;
575: MatScalar *u,*diag,*rtmp,*rtmp_ptr;
576: MatScalar *work;
577: PetscInt *pivots;
578: PetscBool allowzeropivot,zeropivotdetected;
581: /* initialization */
582: PetscCalloc1(bs2*mbs,&rtmp);
583: PetscMalloc2(mbs,&il,mbs,&jl);
584: allowzeropivot = PetscNot(A->erroriffailure);
586: il[0] = 0;
587: for (i=0; i<mbs; i++) jl[i] = mbs;
588:
589: PetscMalloc3(bs2,&dk,bs2,&uik,bs,&work);
590: PetscMalloc1(bs,&pivots);
592: ISGetIndices(perm,&perm_ptr);
594: /* check permutation */
595: if (!a->permute) {
596: ai = a->i; aj = a->j; aa = a->a;
597: } else {
598: ai = a->inew; aj = a->jnew;
599: PetscMalloc1(bs2*ai[mbs],&aa);
600: PetscMemcpy(aa,a->a,bs2*ai[mbs]*sizeof(MatScalar));
601: PetscMalloc1(ai[mbs],&a2anew);
602: PetscMemcpy(a2anew,a->a2anew,(ai[mbs])*sizeof(PetscInt));
604: for (i=0; i<mbs; i++) {
605: jmin = ai[i]; jmax = ai[i+1];
606: for (j=jmin; j<jmax; j++) {
607: while (a2anew[j] != j) {
608: k = a2anew[j]; a2anew[j] = a2anew[k]; a2anew[k] = k;
609: for (k1=0; k1<bs2; k1++) {
610: dk[k1] = aa[k*bs2+k1];
611: aa[k*bs2+k1] = aa[j*bs2+k1];
612: aa[j*bs2+k1] = dk[k1];
613: }
614: }
615: /* transform columnoriented blocks that lie in the lower triangle to roworiented blocks */
616: if (i > aj[j]) {
617: /* printf("change orientation, row: %d, col: %d\n",i,aj[j]); */
618: ap = aa + j*bs2; /* ptr to the beginning of j-th block of aa */
619: for (k=0; k<bs2; k++) dk[k] = ap[k]; /* dk <- j-th block of aa */
620: for (k=0; k<bs; k++) { /* j-th block of aa <- dk^T */
621: for (k1=0; k1<bs; k1++) *ap++ = dk[k + bs*k1];
622: }
623: }
624: }
625: }
626: PetscFree(a2anew);
627: }
629: /* for each row k */
630: for (k = 0; k<mbs; k++) {
632: /*initialize k-th row with elements nonzero in row perm(k) of A */
633: jmin = ai[perm_ptr[k]]; jmax = ai[perm_ptr[k]+1];
635: ap = aa + jmin*bs2;
636: for (j = jmin; j < jmax; j++) {
637: vj = perm_ptr[aj[j]]; /* block col. index */
638: rtmp_ptr = rtmp + vj*bs2;
639: for (i=0; i<bs2; i++) *rtmp_ptr++ = *ap++;
640: }
642: /* modify k-th row by adding in those rows i with U(i,k) != 0 */
643: PetscMemcpy(dk,rtmp+k*bs2,bs2*sizeof(MatScalar));
644: i = jl[k]; /* first row to be added to k_th row */
646: while (i < k) {
647: nexti = jl[i]; /* next row to be added to k_th row */
649: /* compute multiplier */
650: ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */
652: /* uik = -inv(Di)*U_bar(i,k) */
653: diag = ba + i*bs2;
654: u = ba + ili*bs2;
655: PetscMemzero(uik,bs2*sizeof(MatScalar));
656: PetscKernel_A_gets_A_minus_B_times_C(bs,uik,diag,u);
658: /* update D(k) += -U(i,k)^T * U_bar(i,k) */
659: PetscKernel_A_gets_A_plus_Btranspose_times_C(bs,dk,uik,u);
660: PetscLogFlops(4.0*bs*bs2);
662: /* update -U(i,k) */
663: PetscMemcpy(ba+ili*bs2,uik,bs2*sizeof(MatScalar));
665: /* add multiple of row i to k-th row ... */
666: jmin = ili + 1; jmax = bi[i+1];
667: if (jmin < jmax) {
668: for (j=jmin; j<jmax; j++) {
669: /* rtmp += -U(i,k)^T * U_bar(i,j) */
670: rtmp_ptr = rtmp + bj[j]*bs2;
671: u = ba + j*bs2;
672: PetscKernel_A_gets_A_plus_Btranspose_times_C(bs,rtmp_ptr,uik,u);
673: }
674: PetscLogFlops(2.0*bs*bs2*(jmax-jmin));
676: /* ... add i to row list for next nonzero entry */
677: il[i] = jmin; /* update il(i) in column k+1, ... mbs-1 */
678: j = bj[jmin];
679: jl[i] = jl[j]; jl[j] = i; /* update jl */
680: }
681: i = nexti;
682: }
684: /* save nonzero entries in k-th row of U ... */
686: /* invert diagonal block */
687: diag = ba+k*bs2;
688: PetscMemcpy(diag,dk,bs2*sizeof(MatScalar));
690: PetscKernel_A_gets_inverse_A(bs,diag,pivots,work,allowzeropivot,&zeropivotdetected);
691: if (zeropivotdetected) C->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
693: jmin = bi[k]; jmax = bi[k+1];
694: if (jmin < jmax) {
695: for (j=jmin; j<jmax; j++) {
696: vj = bj[j]; /* block col. index of U */
697: u = ba + j*bs2;
698: rtmp_ptr = rtmp + vj*bs2;
699: for (k1=0; k1<bs2; k1++) {
700: *u++ = *rtmp_ptr;
701: *rtmp_ptr++ = 0.0;
702: }
703: }
705: /* ... add k to row list for first nonzero entry in k-th row */
706: il[k] = jmin;
707: i = bj[jmin];
708: jl[k] = jl[i]; jl[i] = k;
709: }
710: }
712: PetscFree(rtmp);
713: PetscFree2(il,jl);
714: PetscFree3(dk,uik,work);
715: PetscFree(pivots);
716: if (a->permute) {
717: PetscFree(aa);
718: }
720: ISRestoreIndices(perm,&perm_ptr);
722: C->ops->solve = MatSolve_SeqSBAIJ_N_inplace;
723: C->ops->solvetranspose = MatSolve_SeqSBAIJ_N_inplace;
724: C->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_N_inplace;
725: C->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_N_inplace;
727: C->assembled = PETSC_TRUE;
728: C->preallocated = PETSC_TRUE;
730: PetscLogFlops(1.3333*bs*bs2*b->mbs); /* from inverting diagonal blocks */
731: return(0);
732: }
736: PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering(Mat C,Mat A,const MatFactorInfo *info)
737: {
738: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data,*b = (Mat_SeqSBAIJ*)C->data;
740: PetscInt i,j,mbs=a->mbs,*bi=b->i,*bj=b->j;
741: PetscInt *ai,*aj,k,k1,jmin,jmax,*jl,*il,vj,nexti,ili;
742: PetscInt bs =A->rmap->bs,bs2 = a->bs2;
743: MatScalar *ba = b->a,*aa,*ap,*dk,*uik;
744: MatScalar *u,*diag,*rtmp,*rtmp_ptr;
745: MatScalar *work;
746: PetscInt *pivots;
747: PetscBool allowzeropivot,zeropivotdetected;
750: PetscCalloc1(bs2*mbs,&rtmp);
751: PetscMalloc2(mbs,&il,mbs,&jl);
752: il[0] = 0;
753: for (i=0; i<mbs; i++) jl[i] = mbs;
754:
755: PetscMalloc3(bs2,&dk,bs2,&uik,bs,&work);
756: PetscMalloc1(bs,&pivots);
757: allowzeropivot = PetscNot(A->erroriffailure);
759: ai = a->i; aj = a->j; aa = a->a;
761: /* for each row k */
762: for (k = 0; k<mbs; k++) {
764: /*initialize k-th row with elements nonzero in row k of A */
765: jmin = ai[k]; jmax = ai[k+1];
766: ap = aa + jmin*bs2;
767: for (j = jmin; j < jmax; j++) {
768: vj = aj[j]; /* block col. index */
769: rtmp_ptr = rtmp + vj*bs2;
770: for (i=0; i<bs2; i++) *rtmp_ptr++ = *ap++;
771: }
773: /* modify k-th row by adding in those rows i with U(i,k) != 0 */
774: PetscMemcpy(dk,rtmp+k*bs2,bs2*sizeof(MatScalar));
775: i = jl[k]; /* first row to be added to k_th row */
777: while (i < k) {
778: nexti = jl[i]; /* next row to be added to k_th row */
780: /* compute multiplier */
781: ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */
783: /* uik = -inv(Di)*U_bar(i,k) */
784: diag = ba + i*bs2;
785: u = ba + ili*bs2;
786: PetscMemzero(uik,bs2*sizeof(MatScalar));
787: PetscKernel_A_gets_A_minus_B_times_C(bs,uik,diag,u);
789: /* update D(k) += -U(i,k)^T * U_bar(i,k) */
790: PetscKernel_A_gets_A_plus_Btranspose_times_C(bs,dk,uik,u);
791: PetscLogFlops(2.0*bs*bs2);
793: /* update -U(i,k) */
794: PetscMemcpy(ba+ili*bs2,uik,bs2*sizeof(MatScalar));
796: /* add multiple of row i to k-th row ... */
797: jmin = ili + 1; jmax = bi[i+1];
798: if (jmin < jmax) {
799: for (j=jmin; j<jmax; j++) {
800: /* rtmp += -U(i,k)^T * U_bar(i,j) */
801: rtmp_ptr = rtmp + bj[j]*bs2;
802: u = ba + j*bs2;
803: PetscKernel_A_gets_A_plus_Btranspose_times_C(bs,rtmp_ptr,uik,u);
804: }
805: PetscLogFlops(2.0*bs*bs2*(jmax-jmin));
807: /* ... add i to row list for next nonzero entry */
808: il[i] = jmin; /* update il(i) in column k+1, ... mbs-1 */
809: j = bj[jmin];
810: jl[i] = jl[j]; jl[j] = i; /* update jl */
811: }
812: i = nexti;
813: }
815: /* save nonzero entries in k-th row of U ... */
817: /* invert diagonal block */
818: diag = ba+k*bs2;
819: PetscMemcpy(diag,dk,bs2*sizeof(MatScalar));
821: PetscKernel_A_gets_inverse_A(bs,diag,pivots,work,allowzeropivot,&zeropivotdetected);
822: if (zeropivotdetected) C->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
824: jmin = bi[k]; jmax = bi[k+1];
825: if (jmin < jmax) {
826: for (j=jmin; j<jmax; j++) {
827: vj = bj[j]; /* block col. index of U */
828: u = ba + j*bs2;
829: rtmp_ptr = rtmp + vj*bs2;
830: for (k1=0; k1<bs2; k1++) {
831: *u++ = *rtmp_ptr;
832: *rtmp_ptr++ = 0.0;
833: }
834: }
836: /* ... add k to row list for first nonzero entry in k-th row */
837: il[k] = jmin;
838: i = bj[jmin];
839: jl[k] = jl[i]; jl[i] = k;
840: }
841: }
843: PetscFree(rtmp);
844: PetscFree2(il,jl);
845: PetscFree3(dk,uik,work);
846: PetscFree(pivots);
848: C->ops->solve = MatSolve_SeqSBAIJ_N_NaturalOrdering_inplace;
849: C->ops->solvetranspose = MatSolve_SeqSBAIJ_N_NaturalOrdering_inplace;
850: C->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_N_NaturalOrdering_inplace;
851: C->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_N_NaturalOrdering_inplace;
852: C->assembled = PETSC_TRUE;
853: C->preallocated = PETSC_TRUE;
855: PetscLogFlops(1.3333*bs*bs2*b->mbs); /* from inverting diagonal blocks */
856: return(0);
857: }
859: /*
860: Numeric U^T*D*U factorization for SBAIJ format. Modified from SNF of YSMP.
861: Version for blocks 2 by 2.
862: */
865: PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_2(Mat C,Mat A,const MatFactorInfo *info)
866: {
867: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data,*b = (Mat_SeqSBAIJ*)C->data;
868: IS perm = b->row;
870: const PetscInt *ai,*aj,*perm_ptr;
871: PetscInt i,j,mbs=a->mbs,*bi=b->i,*bj=b->j;
872: PetscInt *a2anew,k,k1,jmin,jmax,*jl,*il,vj,nexti,ili;
873: MatScalar *ba = b->a,*aa,*ap;
874: MatScalar *u,*diag,*rtmp,*rtmp_ptr,dk[4],uik[4];
875: PetscReal shift = info->shiftamount;
876: PetscBool allowzeropivot,zeropivotdetected;
879: allowzeropivot = PetscNot(A->erroriffailure);
881: /* initialization */
882: /* il and jl record the first nonzero element in each row of the accessing
883: window U(0:k, k:mbs-1).
884: jl: list of rows to be added to uneliminated rows
885: i>= k: jl(i) is the first row to be added to row i
886: i< k: jl(i) is the row following row i in some list of rows
887: jl(i) = mbs indicates the end of a list
888: il(i): points to the first nonzero element in columns k,...,mbs-1 of
889: row i of U */
890: PetscCalloc1(4*mbs,&rtmp);
891: PetscMalloc2(mbs,&il,mbs,&jl);
892: il[0] = 0;
893: for (i=0; i<mbs; i++) jl[i] = mbs;
894:
895: ISGetIndices(perm,&perm_ptr);
897: /* check permutation */
898: if (!a->permute) {
899: ai = a->i; aj = a->j; aa = a->a;
900: } else {
901: ai = a->inew; aj = a->jnew;
902: PetscMalloc1(4*ai[mbs],&aa);
903: PetscMemcpy(aa,a->a,4*ai[mbs]*sizeof(MatScalar));
904: PetscMalloc1(ai[mbs],&a2anew);
905: PetscMemcpy(a2anew,a->a2anew,(ai[mbs])*sizeof(PetscInt));
907: for (i=0; i<mbs; i++) {
908: jmin = ai[i]; jmax = ai[i+1];
909: for (j=jmin; j<jmax; j++) {
910: while (a2anew[j] != j) {
911: k = a2anew[j]; a2anew[j] = a2anew[k]; a2anew[k] = k;
912: for (k1=0; k1<4; k1++) {
913: dk[k1] = aa[k*4+k1];
914: aa[k*4+k1] = aa[j*4+k1];
915: aa[j*4+k1] = dk[k1];
916: }
917: }
918: /* transform columnoriented blocks that lie in the lower triangle to roworiented blocks */
919: if (i > aj[j]) {
920: /* printf("change orientation, row: %d, col: %d\n",i,aj[j]); */
921: ap = aa + j*4; /* ptr to the beginning of the block */
922: dk[1] = ap[1]; /* swap ap[1] and ap[2] */
923: ap[1] = ap[2];
924: ap[2] = dk[1];
925: }
926: }
927: }
928: PetscFree(a2anew);
929: }
931: /* for each row k */
932: for (k = 0; k<mbs; k++) {
934: /*initialize k-th row with elements nonzero in row perm(k) of A */
935: jmin = ai[perm_ptr[k]]; jmax = ai[perm_ptr[k]+1];
936: ap = aa + jmin*4;
937: for (j = jmin; j < jmax; j++) {
938: vj = perm_ptr[aj[j]]; /* block col. index */
939: rtmp_ptr = rtmp + vj*4;
940: for (i=0; i<4; i++) *rtmp_ptr++ = *ap++;
941: }
943: /* modify k-th row by adding in those rows i with U(i,k) != 0 */
944: PetscMemcpy(dk,rtmp+k*4,4*sizeof(MatScalar));
945: i = jl[k]; /* first row to be added to k_th row */
947: while (i < k) {
948: nexti = jl[i]; /* next row to be added to k_th row */
950: /* compute multiplier */
951: ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */
953: /* uik = -inv(Di)*U_bar(i,k): - ba[ili]*ba[i] */
954: diag = ba + i*4;
955: u = ba + ili*4;
956: uik[0] = -(diag[0]*u[0] + diag[2]*u[1]);
957: uik[1] = -(diag[1]*u[0] + diag[3]*u[1]);
958: uik[2] = -(diag[0]*u[2] + diag[2]*u[3]);
959: uik[3] = -(diag[1]*u[2] + diag[3]*u[3]);
961: /* update D(k) += -U(i,k)^T * U_bar(i,k): dk += uik*ba[ili] */
962: dk[0] += uik[0]*u[0] + uik[1]*u[1];
963: dk[1] += uik[2]*u[0] + uik[3]*u[1];
964: dk[2] += uik[0]*u[2] + uik[1]*u[3];
965: dk[3] += uik[2]*u[2] + uik[3]*u[3];
967: PetscLogFlops(16.0*2.0);
969: /* update -U(i,k): ba[ili] = uik */
970: PetscMemcpy(ba+ili*4,uik,4*sizeof(MatScalar));
972: /* add multiple of row i to k-th row ... */
973: jmin = ili + 1; jmax = bi[i+1];
974: if (jmin < jmax) {
975: for (j=jmin; j<jmax; j++) {
976: /* rtmp += -U(i,k)^T * U_bar(i,j): rtmp[bj[j]] += uik*ba[j]; */
977: rtmp_ptr = rtmp + bj[j]*4;
978: u = ba + j*4;
979: rtmp_ptr[0] += uik[0]*u[0] + uik[1]*u[1];
980: rtmp_ptr[1] += uik[2]*u[0] + uik[3]*u[1];
981: rtmp_ptr[2] += uik[0]*u[2] + uik[1]*u[3];
982: rtmp_ptr[3] += uik[2]*u[2] + uik[3]*u[3];
983: }
984: PetscLogFlops(16.0*(jmax-jmin));
986: /* ... add i to row list for next nonzero entry */
987: il[i] = jmin; /* update il(i) in column k+1, ... mbs-1 */
988: j = bj[jmin];
989: jl[i] = jl[j]; jl[j] = i; /* update jl */
990: }
991: i = nexti;
992: }
994: /* save nonzero entries in k-th row of U ... */
996: /* invert diagonal block */
997: diag = ba+k*4;
998: PetscMemcpy(diag,dk,4*sizeof(MatScalar));
999: PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);
1000: if (zeropivotdetected) C->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1002: jmin = bi[k]; jmax = bi[k+1];
1003: if (jmin < jmax) {
1004: for (j=jmin; j<jmax; j++) {
1005: vj = bj[j]; /* block col. index of U */
1006: u = ba + j*4;
1007: rtmp_ptr = rtmp + vj*4;
1008: for (k1=0; k1<4; k1++) {
1009: *u++ = *rtmp_ptr;
1010: *rtmp_ptr++ = 0.0;
1011: }
1012: }
1014: /* ... add k to row list for first nonzero entry in k-th row */
1015: il[k] = jmin;
1016: i = bj[jmin];
1017: jl[k] = jl[i]; jl[i] = k;
1018: }
1019: }
1021: PetscFree(rtmp);
1022: PetscFree2(il,jl);
1023: if (a->permute) {
1024: PetscFree(aa);
1025: }
1026: ISRestoreIndices(perm,&perm_ptr);
1028: C->ops->solve = MatSolve_SeqSBAIJ_2_inplace;
1029: C->ops->solvetranspose = MatSolve_SeqSBAIJ_2_inplace;
1030: C->assembled = PETSC_TRUE;
1031: C->preallocated = PETSC_TRUE;
1033: PetscLogFlops(1.3333*8*b->mbs); /* from inverting diagonal blocks */
1034: return(0);
1035: }
1037: /*
1038: Version for when blocks are 2 by 2 Using natural ordering
1039: */
1042: PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering(Mat C,Mat A,const MatFactorInfo *info)
1043: {
1044: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data,*b = (Mat_SeqSBAIJ*)C->data;
1046: PetscInt i,j,mbs=a->mbs,*bi=b->i,*bj=b->j;
1047: PetscInt *ai,*aj,k,k1,jmin,jmax,*jl,*il,vj,nexti,ili;
1048: MatScalar *ba = b->a,*aa,*ap,dk[8],uik[8];
1049: MatScalar *u,*diag,*rtmp,*rtmp_ptr;
1050: PetscReal shift = info->shiftamount;
1051: PetscBool allowzeropivot,zeropivotdetected;
1054: allowzeropivot = PetscNot(A->erroriffailure);
1056: /* initialization */
1057: /* il and jl record the first nonzero element in each row of the accessing
1058: window U(0:k, k:mbs-1).
1059: jl: list of rows to be added to uneliminated rows
1060: i>= k: jl(i) is the first row to be added to row i
1061: i< k: jl(i) is the row following row i in some list of rows
1062: jl(i) = mbs indicates the end of a list
1063: il(i): points to the first nonzero element in columns k,...,mbs-1 of
1064: row i of U */
1065: PetscCalloc1(4*mbs,&rtmp);
1066: PetscMalloc2(mbs,&il,mbs,&jl);
1067: il[0] = 0;
1068: for (i=0; i<mbs; i++) jl[i] = mbs;
1069:
1070: ai = a->i; aj = a->j; aa = a->a;
1072: /* for each row k */
1073: for (k = 0; k<mbs; k++) {
1075: /*initialize k-th row with elements nonzero in row k of A */
1076: jmin = ai[k]; jmax = ai[k+1];
1077: ap = aa + jmin*4;
1078: for (j = jmin; j < jmax; j++) {
1079: vj = aj[j]; /* block col. index */
1080: rtmp_ptr = rtmp + vj*4;
1081: for (i=0; i<4; i++) *rtmp_ptr++ = *ap++;
1082: }
1084: /* modify k-th row by adding in those rows i with U(i,k) != 0 */
1085: PetscMemcpy(dk,rtmp+k*4,4*sizeof(MatScalar));
1086: i = jl[k]; /* first row to be added to k_th row */
1088: while (i < k) {
1089: nexti = jl[i]; /* next row to be added to k_th row */
1091: /* compute multiplier */
1092: ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */
1094: /* uik = -inv(Di)*U_bar(i,k): - ba[ili]*ba[i] */
1095: diag = ba + i*4;
1096: u = ba + ili*4;
1097: uik[0] = -(diag[0]*u[0] + diag[2]*u[1]);
1098: uik[1] = -(diag[1]*u[0] + diag[3]*u[1]);
1099: uik[2] = -(diag[0]*u[2] + diag[2]*u[3]);
1100: uik[3] = -(diag[1]*u[2] + diag[3]*u[3]);
1102: /* update D(k) += -U(i,k)^T * U_bar(i,k): dk += uik*ba[ili] */
1103: dk[0] += uik[0]*u[0] + uik[1]*u[1];
1104: dk[1] += uik[2]*u[0] + uik[3]*u[1];
1105: dk[2] += uik[0]*u[2] + uik[1]*u[3];
1106: dk[3] += uik[2]*u[2] + uik[3]*u[3];
1108: PetscLogFlops(16.0*2.0);
1110: /* update -U(i,k): ba[ili] = uik */
1111: PetscMemcpy(ba+ili*4,uik,4*sizeof(MatScalar));
1113: /* add multiple of row i to k-th row ... */
1114: jmin = ili + 1; jmax = bi[i+1];
1115: if (jmin < jmax) {
1116: for (j=jmin; j<jmax; j++) {
1117: /* rtmp += -U(i,k)^T * U_bar(i,j): rtmp[bj[j]] += uik*ba[j]; */
1118: rtmp_ptr = rtmp + bj[j]*4;
1119: u = ba + j*4;
1120: rtmp_ptr[0] += uik[0]*u[0] + uik[1]*u[1];
1121: rtmp_ptr[1] += uik[2]*u[0] + uik[3]*u[1];
1122: rtmp_ptr[2] += uik[0]*u[2] + uik[1]*u[3];
1123: rtmp_ptr[3] += uik[2]*u[2] + uik[3]*u[3];
1124: }
1125: PetscLogFlops(16.0*(jmax-jmin));
1127: /* ... add i to row list for next nonzero entry */
1128: il[i] = jmin; /* update il(i) in column k+1, ... mbs-1 */
1129: j = bj[jmin];
1130: jl[i] = jl[j]; jl[j] = i; /* update jl */
1131: }
1132: i = nexti;
1133: }
1135: /* save nonzero entries in k-th row of U ... */
1137: /* invert diagonal block */
1138: diag = ba+k*4;
1139: PetscMemcpy(diag,dk,4*sizeof(MatScalar));
1140: PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);
1141: if (zeropivotdetected) C->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1143: jmin = bi[k]; jmax = bi[k+1];
1144: if (jmin < jmax) {
1145: for (j=jmin; j<jmax; j++) {
1146: vj = bj[j]; /* block col. index of U */
1147: u = ba + j*4;
1148: rtmp_ptr = rtmp + vj*4;
1149: for (k1=0; k1<4; k1++) {
1150: *u++ = *rtmp_ptr;
1151: *rtmp_ptr++ = 0.0;
1152: }
1153: }
1155: /* ... add k to row list for first nonzero entry in k-th row */
1156: il[k] = jmin;
1157: i = bj[jmin];
1158: jl[k] = jl[i]; jl[i] = k;
1159: }
1160: }
1162: PetscFree(rtmp);
1163: PetscFree2(il,jl);
1165: C->ops->solve = MatSolve_SeqSBAIJ_2_NaturalOrdering_inplace;
1166: C->ops->solvetranspose = MatSolve_SeqSBAIJ_2_NaturalOrdering_inplace;
1167: C->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_2_NaturalOrdering_inplace;
1168: C->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_2_NaturalOrdering_inplace;
1169: C->assembled = PETSC_TRUE;
1170: C->preallocated = PETSC_TRUE;
1172: PetscLogFlops(1.3333*8*b->mbs); /* from inverting diagonal blocks */
1173: return(0);
1174: }
1176: /*
1177: Numeric U^T*D*U factorization for SBAIJ format.
1178: Version for blocks are 1 by 1.
1179: */
1182: PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_1_inplace(Mat C,Mat A,const MatFactorInfo *info)
1183: {
1184: Mat_SeqSBAIJ *a=(Mat_SeqSBAIJ*)A->data,*b=(Mat_SeqSBAIJ*)C->data;
1185: IS ip=b->row;
1187: const PetscInt *ai,*aj,*rip;
1188: PetscInt *a2anew,i,j,mbs=a->mbs,*bi=b->i,*bj=b->j,*bcol;
1189: PetscInt k,jmin,jmax,*jl,*il,col,nexti,ili,nz;
1190: MatScalar *rtmp,*ba=b->a,*bval,*aa,dk,uikdi;
1191: PetscReal rs;
1192: FactorShiftCtx sctx;
1195: /* MatPivotSetUp(): initialize shift context sctx */
1196: PetscMemzero(&sctx,sizeof(FactorShiftCtx));
1198: ISGetIndices(ip,&rip);
1199: if (!a->permute) {
1200: ai = a->i; aj = a->j; aa = a->a;
1201: } else {
1202: ai = a->inew; aj = a->jnew;
1203: nz = ai[mbs];
1204: PetscMalloc1(nz,&aa);
1205: a2anew = a->a2anew;
1206: bval = a->a;
1207: for (j=0; j<nz; j++) {
1208: aa[a2anew[j]] = *(bval++);
1209: }
1210: }
1212: /* initialization */
1213: /* il and jl record the first nonzero element in each row of the accessing
1214: window U(0:k, k:mbs-1).
1215: jl: list of rows to be added to uneliminated rows
1216: i>= k: jl(i) is the first row to be added to row i
1217: i< k: jl(i) is the row following row i in some list of rows
1218: jl(i) = mbs indicates the end of a list
1219: il(i): points to the first nonzero element in columns k,...,mbs-1 of
1220: row i of U */
1221: PetscMalloc3(mbs,&rtmp,mbs,&il,mbs,&jl);
1223: do {
1224: sctx.newshift = PETSC_FALSE;
1225: il[0] = 0;
1226: for (i=0; i<mbs; i++) {
1227: rtmp[i] = 0.0; jl[i] = mbs;
1228: }
1230: for (k = 0; k<mbs; k++) {
1231: /*initialize k-th row by the perm[k]-th row of A */
1232: jmin = ai[rip[k]]; jmax = ai[rip[k]+1];
1233: bval = ba + bi[k];
1234: for (j = jmin; j < jmax; j++) {
1235: col = rip[aj[j]];
1236: rtmp[col] = aa[j];
1237: *bval++ = 0.0; /* for in-place factorization */
1238: }
1240: /* shift the diagonal of the matrix */
1241: if (sctx.nshift) rtmp[k] += sctx.shift_amount;
1243: /* modify k-th row by adding in those rows i with U(i,k)!=0 */
1244: dk = rtmp[k];
1245: i = jl[k]; /* first row to be added to k_th row */
1247: while (i < k) {
1248: nexti = jl[i]; /* next row to be added to k_th row */
1250: /* compute multiplier, update diag(k) and U(i,k) */
1251: ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */
1252: uikdi = -ba[ili]*ba[bi[i]]; /* diagonal(k) */
1253: dk += uikdi*ba[ili];
1254: ba[ili] = uikdi; /* -U(i,k) */
1256: /* add multiple of row i to k-th row */
1257: jmin = ili + 1; jmax = bi[i+1];
1258: if (jmin < jmax) {
1259: for (j=jmin; j<jmax; j++) rtmp[bj[j]] += uikdi*ba[j];
1260: PetscLogFlops(2.0*(jmax-jmin));
1262: /* update il and jl for row i */
1263: il[i] = jmin;
1264: j = bj[jmin]; jl[i] = jl[j]; jl[j] = i;
1265: }
1266: i = nexti;
1267: }
1269: /* shift the diagonals when zero pivot is detected */
1270: /* compute rs=sum of abs(off-diagonal) */
1271: rs = 0.0;
1272: jmin = bi[k]+1;
1273: nz = bi[k+1] - jmin;
1274: if (nz) {
1275: bcol = bj + jmin;
1276: while (nz--) {
1277: rs += PetscAbsScalar(rtmp[*bcol]);
1278: bcol++;
1279: }
1280: }
1282: sctx.rs = rs;
1283: sctx.pv = dk;
1284: MatPivotCheck(C,A,info,&sctx,k);
1285: if (sctx.newshift) break; /* sctx.shift_amount is updated */
1286: dk = sctx.pv;
1288: /* copy data into U(k,:) */
1289: ba[bi[k]] = 1.0/dk; /* U(k,k) */
1290: jmin = bi[k]+1; jmax = bi[k+1];
1291: if (jmin < jmax) {
1292: for (j=jmin; j<jmax; j++) {
1293: col = bj[j]; ba[j] = rtmp[col]; rtmp[col] = 0.0;
1294: }
1295: /* add the k-th row into il and jl */
1296: il[k] = jmin;
1297: i = bj[jmin]; jl[k] = jl[i]; jl[i] = k;
1298: }
1299: }
1300: } while (sctx.newshift);
1301: PetscFree3(rtmp,il,jl);
1302: if (a->permute) {PetscFree(aa);}
1304: ISRestoreIndices(ip,&rip);
1306: C->ops->solve = MatSolve_SeqSBAIJ_1_inplace;
1307: C->ops->solves = MatSolves_SeqSBAIJ_1_inplace;
1308: C->ops->solvetranspose = MatSolve_SeqSBAIJ_1_inplace;
1309: C->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_1_inplace;
1310: C->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_1_inplace;
1311: C->assembled = PETSC_TRUE;
1312: C->preallocated = PETSC_TRUE;
1314: PetscLogFlops(C->rmap->N);
1315: if (sctx.nshift) {
1316: if (info->shifttype == (PetscReal)MAT_SHIFT_NONZERO) {
1317: PetscInfo2(A,"number of shiftnz tries %D, shift_amount %g\n",sctx.nshift,(double)sctx.shift_amount);
1318: } else if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) {
1319: PetscInfo2(A,"number of shiftpd tries %D, shift_amount %g\n",sctx.nshift,(double)sctx.shift_amount);
1320: }
1321: }
1322: return(0);
1323: }
1325: /*
1326: Version for when blocks are 1 by 1 Using natural ordering under new datastructure
1327: Modified from MatCholeskyFactorNumeric_SeqAIJ()
1328: */
1331: PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering(Mat B,Mat A,const MatFactorInfo *info)
1332: {
1333: Mat_SeqSBAIJ *a=(Mat_SeqSBAIJ*)A->data;
1334: Mat_SeqSBAIJ *b=(Mat_SeqSBAIJ*)B->data;
1336: PetscInt i,j,mbs=A->rmap->n,*bi=b->i,*bj=b->j,*bdiag=b->diag,*bjtmp;
1337: PetscInt *ai=a->i,*aj=a->j,*ajtmp;
1338: PetscInt k,jmin,jmax,*c2r,*il,col,nexti,ili,nz;
1339: MatScalar *rtmp,*ba=b->a,*bval,*aa=a->a,dk,uikdi;
1340: FactorShiftCtx sctx;
1341: PetscReal rs;
1342: MatScalar d,*v;
1345: PetscMalloc3(mbs,&rtmp,mbs,&il,mbs,&c2r);
1347: /* MatPivotSetUp(): initialize shift context sctx */
1348: PetscMemzero(&sctx,sizeof(FactorShiftCtx));
1350: if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) { /* set sctx.shift_top=max{rs} */
1351: sctx.shift_top = info->zeropivot;
1353: PetscMemzero(rtmp,mbs*sizeof(MatScalar));
1355: for (i=0; i<mbs; i++) {
1356: /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */
1357: d = (aa)[a->diag[i]];
1358: rtmp[i] += -PetscRealPart(d); /* diagonal entry */
1359: ajtmp = aj + ai[i] + 1; /* exclude diagonal */
1360: v = aa + ai[i] + 1;
1361: nz = ai[i+1] - ai[i] - 1;
1362: for (j=0; j<nz; j++) {
1363: rtmp[i] += PetscAbsScalar(v[j]);
1364: rtmp[ajtmp[j]] += PetscAbsScalar(v[j]);
1365: }
1366: if (PetscRealPart(rtmp[i]) > sctx.shift_top) sctx.shift_top = PetscRealPart(rtmp[i]);
1367: }
1368: sctx.shift_top *= 1.1;
1369: sctx.nshift_max = 5;
1370: sctx.shift_lo = 0.;
1371: sctx.shift_hi = 1.;
1372: }
1374: /* allocate working arrays
1375: c2r: linked list, keep track of pivot rows for a given column. c2r[col]: head of the list for a given col
1376: il: for active k row, il[i] gives the index of the 1st nonzero entry in U[i,k:n-1] in bj and ba arrays
1377: */
1378: do {
1379: sctx.newshift = PETSC_FALSE;
1381: for (i=0; i<mbs; i++) c2r[i] = mbs;
1382: if (mbs) il[0] = 0;
1384: for (k = 0; k<mbs; k++) {
1385: /* zero rtmp */
1386: nz = bi[k+1] - bi[k];
1387: bjtmp = bj + bi[k];
1388: for (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0;
1390: /* load in initial unfactored row */
1391: bval = ba + bi[k];
1392: jmin = ai[k]; jmax = ai[k+1];
1393: for (j = jmin; j < jmax; j++) {
1394: col = aj[j];
1395: rtmp[col] = aa[j];
1396: *bval++ = 0.0; /* for in-place factorization */
1397: }
1398: /* shift the diagonal of the matrix: ZeropivotApply() */
1399: rtmp[k] += sctx.shift_amount; /* shift the diagonal of the matrix */
1401: /* modify k-th row by adding in those rows i with U(i,k)!=0 */
1402: dk = rtmp[k];
1403: i = c2r[k]; /* first row to be added to k_th row */
1405: while (i < k) {
1406: nexti = c2r[i]; /* next row to be added to k_th row */
1408: /* compute multiplier, update diag(k) and U(i,k) */
1409: ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */
1410: uikdi = -ba[ili]*ba[bdiag[i]]; /* diagonal(k) */
1411: dk += uikdi*ba[ili]; /* update diag[k] */
1412: ba[ili] = uikdi; /* -U(i,k) */
1414: /* add multiple of row i to k-th row */
1415: jmin = ili + 1; jmax = bi[i+1];
1416: if (jmin < jmax) {
1417: for (j=jmin; j<jmax; j++) rtmp[bj[j]] += uikdi*ba[j];
1418: /* update il and c2r for row i */
1419: il[i] = jmin;
1420: j = bj[jmin]; c2r[i] = c2r[j]; c2r[j] = i;
1421: }
1422: i = nexti;
1423: }
1425: /* copy data into U(k,:) */
1426: rs = 0.0;
1427: jmin = bi[k]; jmax = bi[k+1]-1;
1428: if (jmin < jmax) {
1429: for (j=jmin; j<jmax; j++) {
1430: col = bj[j]; ba[j] = rtmp[col]; rs += PetscAbsScalar(ba[j]);
1431: }
1432: /* add the k-th row into il and c2r */
1433: il[k] = jmin;
1434: i = bj[jmin]; c2r[k] = c2r[i]; c2r[i] = k;
1435: }
1437: sctx.rs = rs;
1438: sctx.pv = dk;
1439: MatPivotCheck(B,A,info,&sctx,k);
1440: if (sctx.newshift) break;
1441: dk = sctx.pv;
1443: ba[bdiag[k]] = 1.0/dk; /* U(k,k) */
1444: }
1445: } while (sctx.newshift);
1447: PetscFree3(rtmp,il,c2r);
1449: B->ops->solve = MatSolve_SeqSBAIJ_1_NaturalOrdering;
1450: B->ops->solves = MatSolves_SeqSBAIJ_1;
1451: B->ops->solvetranspose = MatSolve_SeqSBAIJ_1_NaturalOrdering;
1452: B->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_1_NaturalOrdering;
1453: B->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_1_NaturalOrdering;
1455: B->assembled = PETSC_TRUE;
1456: B->preallocated = PETSC_TRUE;
1458: PetscLogFlops(B->rmap->n);
1460: /* MatPivotView() */
1461: if (sctx.nshift) {
1462: if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) {
1463: PetscInfo4(A,"number of shift_pd tries %D, shift_amount %g, diagonal shifted up by %e fraction top_value %e\n",sctx.nshift,(double)sctx.shift_amount,(double)sctx.shift_fraction,(double)sctx.shift_top);
1464: } else if (info->shifttype == (PetscReal)MAT_SHIFT_NONZERO) {
1465: PetscInfo2(A,"number of shift_nz tries %D, shift_amount %g\n",sctx.nshift,(double)sctx.shift_amount);
1466: } else if (info->shifttype == (PetscReal)MAT_SHIFT_INBLOCKS) {
1467: PetscInfo2(A,"number of shift_inblocks applied %D, each shift_amount %g\n",sctx.nshift,(double)info->shiftamount);
1468: }
1469: }
1470: return(0);
1471: }
1475: PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering_inplace(Mat C,Mat A,const MatFactorInfo *info)
1476: {
1477: Mat_SeqSBAIJ *a=(Mat_SeqSBAIJ*)A->data,*b=(Mat_SeqSBAIJ*)C->data;
1479: PetscInt i,j,mbs = a->mbs;
1480: PetscInt *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
1481: PetscInt k,jmin,*jl,*il,nexti,ili,*acol,*bcol,nz;
1482: MatScalar *rtmp,*ba=b->a,*aa=a->a,dk,uikdi,*aval,*bval;
1483: PetscReal rs;
1484: FactorShiftCtx sctx;
1487: /* MatPivotSetUp(): initialize shift context sctx */
1488: PetscMemzero(&sctx,sizeof(FactorShiftCtx));
1490: /* initialization */
1491: /* il and jl record the first nonzero element in each row of the accessing
1492: window U(0:k, k:mbs-1).
1493: jl: list of rows to be added to uneliminated rows
1494: i>= k: jl(i) is the first row to be added to row i
1495: i< k: jl(i) is the row following row i in some list of rows
1496: jl(i) = mbs indicates the end of a list
1497: il(i): points to the first nonzero element in U(i,k:mbs-1)
1498: */
1499: PetscMalloc1(mbs,&rtmp);
1500: PetscMalloc2(mbs,&il,mbs,&jl);
1502: do {
1503: sctx.newshift = PETSC_FALSE;
1504: il[0] = 0;
1505: for (i=0; i<mbs; i++) {
1506: rtmp[i] = 0.0; jl[i] = mbs;
1507: }
1509: for (k = 0; k<mbs; k++) {
1510: /*initialize k-th row with elements nonzero in row perm(k) of A */
1511: nz = ai[k+1] - ai[k];
1512: acol = aj + ai[k];
1513: aval = aa + ai[k];
1514: bval = ba + bi[k];
1515: while (nz--) {
1516: rtmp[*acol++] = *aval++;
1517: *bval++ = 0.0; /* for in-place factorization */
1518: }
1520: /* shift the diagonal of the matrix */
1521: if (sctx.nshift) rtmp[k] += sctx.shift_amount;
1523: /* modify k-th row by adding in those rows i with U(i,k)!=0 */
1524: dk = rtmp[k];
1525: i = jl[k]; /* first row to be added to k_th row */
1527: while (i < k) {
1528: nexti = jl[i]; /* next row to be added to k_th row */
1529: /* compute multiplier, update D(k) and U(i,k) */
1530: ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */
1531: uikdi = -ba[ili]*ba[bi[i]];
1532: dk += uikdi*ba[ili];
1533: ba[ili] = uikdi; /* -U(i,k) */
1535: /* add multiple of row i to k-th row ... */
1536: jmin = ili + 1;
1537: nz = bi[i+1] - jmin;
1538: if (nz > 0) {
1539: bcol = bj + jmin;
1540: bval = ba + jmin;
1541: PetscLogFlops(2.0*nz);
1542: while (nz--) rtmp[*bcol++] += uikdi*(*bval++);
1544: /* update il and jl for i-th row */
1545: il[i] = jmin;
1546: j = bj[jmin]; jl[i] = jl[j]; jl[j] = i;
1547: }
1548: i = nexti;
1549: }
1551: /* shift the diagonals when zero pivot is detected */
1552: /* compute rs=sum of abs(off-diagonal) */
1553: rs = 0.0;
1554: jmin = bi[k]+1;
1555: nz = bi[k+1] - jmin;
1556: if (nz) {
1557: bcol = bj + jmin;
1558: while (nz--) {
1559: rs += PetscAbsScalar(rtmp[*bcol]);
1560: bcol++;
1561: }
1562: }
1564: sctx.rs = rs;
1565: sctx.pv = dk;
1566: MatPivotCheck(C,A,info,&sctx,k);
1567: if (sctx.newshift) break; /* sctx.shift_amount is updated */
1568: dk = sctx.pv;
1570: /* copy data into U(k,:) */
1571: ba[bi[k]] = 1.0/dk;
1572: jmin = bi[k]+1;
1573: nz = bi[k+1] - jmin;
1574: if (nz) {
1575: bcol = bj + jmin;
1576: bval = ba + jmin;
1577: while (nz--) {
1578: *bval++ = rtmp[*bcol];
1579: rtmp[*bcol++] = 0.0;
1580: }
1581: /* add k-th row into il and jl */
1582: il[k] = jmin;
1583: i = bj[jmin]; jl[k] = jl[i]; jl[i] = k;
1584: }
1585: } /* end of for (k = 0; k<mbs; k++) */
1586: } while (sctx.newshift);
1587: PetscFree(rtmp);
1588: PetscFree2(il,jl);
1590: C->ops->solve = MatSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
1591: C->ops->solves = MatSolves_SeqSBAIJ_1_inplace;
1592: C->ops->solvetranspose = MatSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
1593: C->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
1594: C->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
1596: C->assembled = PETSC_TRUE;
1597: C->preallocated = PETSC_TRUE;
1599: PetscLogFlops(C->rmap->N);
1600: if (sctx.nshift) {
1601: if (info->shifttype == (PetscReal)MAT_SHIFT_NONZERO) {
1602: PetscInfo2(A,"number of shiftnz tries %D, shift_amount %g\n",sctx.nshift,(double)sctx.shift_amount);
1603: } else if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) {
1604: PetscInfo2(A,"number of shiftpd tries %D, shift_amount %g\n",sctx.nshift,(double)sctx.shift_amount);
1605: }
1606: }
1607: return(0);
1608: }
1612: PetscErrorCode MatCholeskyFactor_SeqSBAIJ(Mat A,IS perm,const MatFactorInfo *info)
1613: {
1615: Mat C;
1618: MatGetFactor(A,"petsc",MAT_FACTOR_CHOLESKY,&C);
1619: MatCholeskyFactorSymbolic(C,A,perm,info);
1620: MatCholeskyFactorNumeric(C,A,info);
1622: A->ops->solve = C->ops->solve;
1623: A->ops->solvetranspose = C->ops->solvetranspose;
1625: MatHeaderMerge(A,&C);
1626: return(0);
1627: }