Actual source code: mpispooles.c

  1: #define PETSCMAT_DLL

  3: /* 
  4:    Provides an interface to the Spooles parallel sparse solver (MPI SPOOLES)
  5: */

 7:  #include ../src/mat/impls/aij/seq/aij.h
 8:  #include ../src/mat/impls/sbaij/seq/sbaij.h
 9:  #include ../src/mat/impls/baij/seq/baij.h
 10:  #include ../src/mat/impls/aij/mpi/mpiaij.h
 11:  #include ../src/mat/impls/sbaij/mpi/mpisbaij.h
 12:  #include ../src/mat/impls/aij/seq/spooles/spooles.h

 14: EXTERN int SetSpoolesOptions(Mat, Spooles_options *);
 15: EXTERN PetscErrorCode MatDestroy_MPIAIJ(Mat);

 19: PetscErrorCode MatDestroy_MPIAIJSpooles(Mat A)
 20: {
 21:   Mat_Spooles   *lu = (Mat_Spooles*)A->spptr;
 23: 
 25:   if (lu->CleanUpSpooles) {
 26:     FrontMtx_free(lu->frontmtx);
 27:     IV_free(lu->newToOldIV);
 28:     IV_free(lu->oldToNewIV);
 29:     IV_free(lu->vtxmapIV);
 30:     InpMtx_free(lu->mtxA);
 31:     ETree_free(lu->frontETree);
 32:     IVL_free(lu->symbfacIVL);
 33:     SubMtxManager_free(lu->mtxmanager);
 34:     DenseMtx_free(lu->mtxX);
 35:     DenseMtx_free(lu->mtxY);
 36:     MPI_Comm_free(&(lu->comm_spooles));
 37:     if ( lu->scat ){
 38:       VecDestroy(lu->vec_spooles);
 39:       ISDestroy(lu->iden);
 40:       ISDestroy(lu->is_petsc);
 41:       VecScatterDestroy(lu->scat);
 42:     }
 43:   }
 44:   MatDestroy_MPIAIJ(A);

 46:   return(0);
 47: }

 51: PetscErrorCode MatSolve_MPISpooles(Mat A,Vec b,Vec x)
 52: {
 53:   Mat_Spooles   *lu = (Mat_Spooles*)A->spptr;
 55:   int           size,rank,m=A->rmap->n,irow,*rowindY;
 56:   PetscScalar   *array;
 57:   DenseMtx      *newY ;
 58:   SubMtxManager *solvemanager ;
 59: #if defined(PETSC_USE_COMPLEX)
 60:   double x_real,x_imag;
 61: #endif

 64:   MPI_Comm_size(((PetscObject)A)->comm,&size);
 65:   MPI_Comm_rank(((PetscObject)A)->comm,&rank);
 66: 
 67:   /* copy b into spooles' rhs mtxY */
 68:   DenseMtx_init(lu->mtxY, lu->options.typeflag, 0, 0, m, 1, 1, m);
 69:   VecGetArray(b,&array);

 71:   DenseMtx_rowIndices(lu->mtxY, &m, &rowindY);  /* get m, rowind */
 72:   for ( irow = 0 ; irow < m ; irow++ ) {
 73:     rowindY[irow] = irow + lu->rstart;           /* global rowind */
 74: #if !defined(PETSC_USE_COMPLEX)
 75:     DenseMtx_setRealEntry(lu->mtxY, irow, 0, *array++);
 76: #else
 77:     DenseMtx_setComplexEntry(lu->mtxY,irow,0,PetscRealPart(*array),PetscImaginaryPart(*array));
 78:     array++;
 79: #endif
 80:   }
 81:   VecRestoreArray(b,&array);
 82: 
 83:   if ( lu->options.msglvl > 2 ) {
 84:     int err;
 85:     PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n\n 1 matrix in original ordering");
 86:     DenseMtx_writeForHumanEye(lu->mtxY, lu->options.msgFile);
 87:     err = fflush(lu->options.msgFile);
 88:     if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
 89:   }
 90: 
 91:   /* permute and redistribute Y if necessary */
 92:   DenseMtx_permuteRows(lu->mtxY, lu->oldToNewIV);
 93:   if ( lu->options.msglvl > 2 ) {
 94:     int err;
 95:     PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n\n rhs matrix in new ordering");
 96:     DenseMtx_writeForHumanEye(lu->mtxY, lu->options.msgFile);
 97:     err = fflush(lu->options.msgFile);
 98:     if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
 99:   }

101:   MPI_Barrier(((PetscObject)A)->comm); /* for initializing firsttag, because the num. of tags used
102:                                    by FrontMtx_MPI_split() is unknown */
103:   lu->firsttag = 0;
104:   newY = DenseMtx_MPI_splitByRows(lu->mtxY, lu->vtxmapIV, lu->stats, lu->options.msglvl,
105:                                 lu->options.msgFile, lu->firsttag, lu->comm_spooles);
106:   DenseMtx_free(lu->mtxY);
107:   lu->mtxY = newY ;
108:   lu->firsttag += size ;
109:   if ( lu->options.msglvl > 2 ) {
110:     int err;
111:     PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n\n split DenseMtx Y");
112:     DenseMtx_writeForHumanEye(lu->mtxY, lu->options.msgFile);
113:     err = fflush(lu->options.msgFile);
114:     if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
115:   }

117:   if ( FRONTMTX_IS_PIVOTING(lu->frontmtx) ) {
118:     /*   pivoting has taken place, redistribute the right hand side
119:          to match the final rows and columns in the fronts             */
120:     IV *rowmapIV ;
121:     rowmapIV = FrontMtx_MPI_rowmapIV(lu->frontmtx, lu->ownersIV, lu->options.msglvl,
122:                                     lu->options.msgFile, lu->comm_spooles);
123:     newY = DenseMtx_MPI_splitByRows(lu->mtxY, rowmapIV, lu->stats, lu->options.msglvl,
124:                                    lu->options.msgFile, lu->firsttag, lu->comm_spooles);
125:     DenseMtx_free(lu->mtxY);
126:     lu->mtxY = newY ;
127:     IV_free(rowmapIV);
128:     lu->firsttag += size;
129:   }
130:   if ( lu->options.msglvl > 2 ) {
131:     int err;
132:     PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n\n rhs matrix after split");
133:     DenseMtx_writeForHumanEye(lu->mtxY, lu->options.msgFile);
134:     err = fflush(lu->options.msgFile);
135:     if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
136:   }

138:   if ( lu->nmycol > 0 ) IVcopy(lu->nmycol,lu->rowindX,IV_entries(lu->ownedColumnsIV)); /* must do for each solve */
139: 
140:   /* solve the linear system */
141:   solvemanager = SubMtxManager_new();
142:   SubMtxManager_init(solvemanager, NO_LOCK, 0);
143:   FrontMtx_MPI_solve(lu->frontmtx, lu->mtxX, lu->mtxY, solvemanager, lu->solvemap, lu->cpus,
144:                    lu->stats, lu->options.msglvl, lu->options.msgFile, lu->firsttag, lu->comm_spooles);
145:   SubMtxManager_free(solvemanager);
146:   if ( lu->options.msglvl > 2 ) {
147:     PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n solution in new ordering");
148:     DenseMtx_writeForHumanEye(lu->mtxX, lu->options.msgFile);
149:   }

151:   /* permute the solution into the original ordering */
152:   DenseMtx_permuteRows(lu->mtxX, lu->newToOldIV);
153:   if ( lu->options.msglvl > 2 ) {
154:     int err;
155:     PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n\n solution in old ordering");
156:     DenseMtx_writeForHumanEye(lu->mtxX, lu->options.msgFile);
157:     err = fflush(lu->options.msgFile);
158:     if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
159:   }
160: 
161:   /* scatter local solution mtxX into mpi vector x */
162:   if( !lu->scat ){ /* create followings once for each numfactorization */
163:     /* vec_spooles <- mtxX */
164: #if !defined(PETSC_USE_COMPLEX) 
165:     VecCreateSeqWithArray(PETSC_COMM_SELF,lu->nmycol,lu->entX,&lu->vec_spooles);
166: #else    
167:     VecCreateSeq(PETSC_COMM_SELF,lu->nmycol,&lu->vec_spooles);
168: #endif 
169:     ISCreateStride(PETSC_COMM_SELF,lu->nmycol,0,1,&lu->iden);
170:     ISCreateGeneral(PETSC_COMM_SELF,lu->nmycol,lu->rowindX,&lu->is_petsc);
171:     VecScatterCreate(lu->vec_spooles,lu->iden,x,lu->is_petsc,&lu->scat);
172:   }
173: #if defined(PETSC_USE_COMPLEX)
174:     VecGetArray(lu->vec_spooles,&array);
175:     for (irow = 0; irow < lu->nmycol; irow++){
176:       DenseMtx_complexEntry(lu->mtxX,irow,0,&x_real,&x_imag);
177:       array[irow] = x_real+x_imag*PETSC_i;
178:     }
179:     VecRestoreArray(lu->vec_spooles,&array);
180: #endif 
181:   VecScatterBegin(lu->scat,lu->vec_spooles,x,INSERT_VALUES,SCATTER_FORWARD);
182:   VecScatterEnd(lu->scat,lu->vec_spooles,x,INSERT_VALUES,SCATTER_FORWARD);
183:   return(0);
184: }

188: PetscErrorCode MatFactorNumeric_MPISpooles(Mat F,Mat A,const MatFactorInfo *info)
189: {
190:   Mat_Spooles     *lu = (Mat_Spooles*)(F)->spptr;
191:   PetscErrorCode  ierr;
192:   int             rank,size,lookahead=0,sierr;
193:   ChvManager      *chvmanager ;
194:   Chv             *rootchv ;
195:   Graph           *graph ;
196:   IVL             *adjIVL;
197:   DV              *cumopsDV ;
198:   double          droptol=0.0,*opcounts,minops,cutoff;
199: #if !defined(PETSC_USE_COMPLEX)
200:   double          *val;
201: #endif
202:   InpMtx          *newA ;
203:   PetscScalar     *av, *bv;
204:   PetscInt        *ai, *aj, *bi,*bj, nz, *ajj, *bjj, *garray,
205:                   i,j,irow,jcol,countA,countB,jB,*row,*col,colA_start,jj;
206:   PetscInt        M=A->rmap->N,m=A->rmap->n,root,nedges,tagbound,lasttag;
207:   Mat             F_diag;
208: 
210:   MPI_Comm_size(((PetscObject)A)->comm,&size);
211:   MPI_Comm_rank(((PetscObject)A)->comm,&rank);

213:   if (lu->flg == DIFFERENT_NONZERO_PATTERN) { /* first numeric factorization */
214:     /* get input parameters */
215:     SetSpoolesOptions(A, &lu->options);

217:     (F)->assembled    = PETSC_TRUE;
218:     if ((F)->factor == MAT_FACTOR_LU){
219:       F_diag = ((Mat_MPIAIJ *)(F)->data)->A;
220:     } else {
221:       F_diag = ((Mat_MPISBAIJ *)(F)->data)->A;
222:     }
223:     F_diag->assembled  = PETSC_TRUE;

225:     /* to be used by MatSolve() */
226:     lu->mtxY = DenseMtx_new();
227:     lu->mtxX = DenseMtx_new();
228:     lu->scat = PETSC_NULL;

230:     IVzero(20, lu->stats);
231:     DVzero(20, lu->cpus);

233:     lu->mtxA = InpMtx_new();
234:   }
235: 
236:   /* copy A to Spooles' InpMtx object */
237:   if ( lu->options.symflag == SPOOLES_NONSYMMETRIC ) {
238:     Mat_MPIAIJ  *mat =  (Mat_MPIAIJ*)A->data;
239:     Mat_SeqAIJ  *aa=(Mat_SeqAIJ*)(mat->A)->data;
240:     Mat_SeqAIJ  *bb=(Mat_SeqAIJ*)(mat->B)->data;
241:     ai=aa->i; aj=aa->j; av=aa->a;
242:     bi=bb->i; bj=bb->j; bv=bb->a;
243:     lu->rstart = A->rmap->rstart;
244:     nz         = aa->nz + bb->nz;
245:     garray     = mat->garray;
246:   } else {         /* SPOOLES_SYMMETRIC  */
247:     Mat_MPISBAIJ  *mat = (Mat_MPISBAIJ*)A->data;
248:     Mat_SeqSBAIJ  *aa=(Mat_SeqSBAIJ*)(mat->A)->data;
249:     Mat_SeqBAIJ    *bb=(Mat_SeqBAIJ*)(mat->B)->data;
250:     ai=aa->i; aj=aa->j; av=aa->a;
251:     bi=bb->i; bj=bb->j; bv=bb->a;
252:     lu->rstart = A->rmap->rstart;
253:     nz         = aa->nz + bb->nz;
254:     garray     = mat->garray;
255:   }
256: 
257:   InpMtx_init(lu->mtxA, INPMTX_BY_ROWS, lu->options.typeflag, nz, 0);
258:   row   = InpMtx_ivec1(lu->mtxA);
259:   col   = InpMtx_ivec2(lu->mtxA);
260: #if !defined(PETSC_USE_COMPLEX)
261:   val   = InpMtx_dvec(lu->mtxA);
262: #endif

264:   jj = 0; irow = lu->rstart;
265:   for ( i=0; i<m; i++ ) {
266:     ajj = aj + ai[i];                 /* ptr to the beginning of this row */
267:     countA = ai[i+1] - ai[i];
268:     countB = bi[i+1] - bi[i];
269:     bjj = bj + bi[i];
270:     jB = 0;
271: 
272:     if (lu->options.symflag == SPOOLES_NONSYMMETRIC ){
273:       /* B part, smaller col index */
274:       colA_start = lu->rstart + ajj[0]; /* the smallest col index for A */
275:       for (j=0; j<countB; j++){
276:         jcol = garray[bjj[j]];
277:         if (jcol > colA_start) {
278:           jB = j;
279:           break;
280:         }
281:         row[jj] = irow; col[jj] = jcol;
282: #if !defined(PETSC_USE_COMPLEX)
283:         val[jj++] = *bv++;
284: #else
285:         InpMtx_inputComplexEntry(lu->mtxA,irow,jcol,PetscRealPart(*bv),PetscImaginaryPart(*bv));
286:         bv++; jj++;
287: #endif
288:         if (j==countB-1) jB = countB;
289:       }
290:     }
291:     /* A part */
292:     for (j=0; j<countA; j++){
293:       row[jj] = irow; col[jj] = lu->rstart + ajj[j];
294: #if !defined(PETSC_USE_COMPLEX)
295:       val[jj++] = *av++;
296: #else
297:       InpMtx_inputComplexEntry(lu->mtxA,irow,col[jj],PetscRealPart(*av),PetscImaginaryPart(*av));
298:       av++; jj++;
299: #endif
300:     }
301:     /* B part, larger col index */
302:     for (j=jB; j<countB; j++){
303:       row[jj] = irow; col[jj] = garray[bjj[j]];
304: #if !defined(PETSC_USE_COMPLEX)
305:       val[jj++] = *bv++;
306: #else
307:      InpMtx_inputComplexEntry(lu->mtxA,irow,col[jj],PetscRealPart(*bv),PetscImaginaryPart(*bv));
308:      bv++; jj++;
309: #endif
310:     }
311:     irow++;
312:   }
313: #if !defined(PETSC_USE_COMPLEX)
314:   InpMtx_inputRealTriples(lu->mtxA, nz, row, col, val);
315: #endif
316:   InpMtx_changeStorageMode(lu->mtxA, INPMTX_BY_VECTORS);
317:   if ( lu->options.msglvl > 0 ) {
318:     int err;
319:     printf("[%d] input matrix\n",rank);
320:     PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n\n [%d] input matrix\n",rank);
321:     InpMtx_writeForHumanEye(lu->mtxA, lu->options.msgFile);
322:     err = fflush(lu->options.msgFile);
323:     if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
324:   }

326:   if ( lu->flg == DIFFERENT_NONZERO_PATTERN){ /* first numeric factorization */
327:     /*
328:       find a low-fill ordering
329:       (1) create the Graph object
330:       (2) order the graph using multiple minimum degree
331:       (3) find out who has the best ordering w.r.t. op count,
332:           and broadcast that front tree object
333:     */
334:     graph = Graph_new();
335:     adjIVL = InpMtx_MPI_fullAdjacency(lu->mtxA, lu->stats,
336:               lu->options.msglvl, lu->options.msgFile, lu->comm_spooles);
337:     nedges = IVL_tsize(adjIVL);
338:     Graph_init2(graph, 0, M, 0, nedges, M, nedges, adjIVL, NULL, NULL);
339:     if ( lu->options.msglvl > 2 ) {
340:       int err;
341:       err = PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n\n graph of the input matrix");
342:       Graph_writeForHumanEye(graph, lu->options.msgFile);
343:       fflush(lu->options.msgFile);
344:       if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
345:     }

347:     switch (lu->options.ordering) {
348:     case 0:
349:       lu->frontETree = orderViaBestOfNDandMS(graph,
350:                      lu->options.maxdomainsize, lu->options.maxzeros, lu->options.maxsize,
351:                      lu->options.seed + rank, lu->options.msglvl, lu->options.msgFile); break;
352:     case 1:
353:       lu->frontETree = orderViaMMD(graph,lu->options.seed + rank,lu->options.msglvl,lu->options.msgFile); break;
354:     case 2:
355:       lu->frontETree = orderViaMS(graph, lu->options.maxdomainsize,
356:                      lu->options.seed + rank,lu->options.msglvl,lu->options.msgFile); break;
357:     case 3:
358:       lu->frontETree = orderViaND(graph, lu->options.maxdomainsize,
359:                      lu->options.seed + rank,lu->options.msglvl,lu->options.msgFile); break;
360:     default:
361:       SETERRQ(PETSC_ERR_ARG_WRONG,"Unknown Spooles's ordering");
362:     }

364:     Graph_free(graph);
365:     if ( lu->options.msglvl > 2 ) {
366:       int err;
367:       PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n\n front tree from ordering");
368:       ETree_writeForHumanEye(lu->frontETree, lu->options.msgFile);
369:       err = fflush(lu->options.msgFile);
370:       if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
371:     }

373:     opcounts = DVinit(size, 0.0);
374:     opcounts[rank] = ETree_nFactorOps(lu->frontETree, lu->options.typeflag, lu->options.symflag);
375:     MPI_Allgather((void*) &opcounts[rank], 1, MPI_DOUBLE,
376:               (void*) opcounts, 1, MPI_DOUBLE, ((PetscObject)A)->comm);
377:     minops = DVmin(size, opcounts, &root);
378:     DVfree(opcounts);
379: 
380:     lu->frontETree = ETree_MPI_Bcast(lu->frontETree, root,
381:                              lu->options.msglvl, lu->options.msgFile, lu->comm_spooles);
382:     if ( lu->options.msglvl > 2 ) {
383:       int err;
384:       PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n\n best front tree");
385:       ETree_writeForHumanEye(lu->frontETree, lu->options.msgFile);
386:       err = fflush(lu->options.msgFile);
387:       if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
388:     }
389: 
390:     /* get the permutations, permute the front tree, permute the matrix */
391:     lu->oldToNewIV = ETree_oldToNewVtxPerm(lu->frontETree);
392:     lu->newToOldIV = ETree_newToOldVtxPerm(lu->frontETree);

394:     ETree_permuteVertices(lu->frontETree, lu->oldToNewIV);

396:     InpMtx_permute(lu->mtxA, IV_entries(lu->oldToNewIV), IV_entries(lu->oldToNewIV));
397: 
398:     if (  lu->options.symflag == SPOOLES_SYMMETRIC ) InpMtx_mapToUpperTriangle(lu->mtxA);

400:     InpMtx_changeCoordType(lu->mtxA, INPMTX_BY_CHEVRONS);
401:     InpMtx_changeStorageMode(lu->mtxA, INPMTX_BY_VECTORS);

403:     /* generate the owners map IV object and the map from vertices to owners */
404:     cutoff   = 1./(2*size);
405:     cumopsDV = DV_new();
406:     DV_init(cumopsDV, size, NULL);
407:     lu->ownersIV = ETree_ddMap(lu->frontETree,
408:                        lu->options.typeflag, lu->options.symflag, cumopsDV, cutoff);
409:     DV_free(cumopsDV);
410:     lu->vtxmapIV = IV_new();
411:     IV_init(lu->vtxmapIV, M, NULL);
412:     IVgather(M, IV_entries(lu->vtxmapIV),
413:              IV_entries(lu->ownersIV), ETree_vtxToFront(lu->frontETree));
414:     if ( lu->options.msglvl > 2 ) {
415:       int err;

417:       PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n\n map from fronts to owning processes");
418:       IV_writeForHumanEye(lu->ownersIV, lu->options.msgFile);
419:       PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n\n map from vertices to owning processes");
420:       IV_writeForHumanEye(lu->vtxmapIV, lu->options.msgFile);
421:       err = fflush(lu->options.msgFile);
422:       if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
423:     }

425:     /* redistribute the matrix */
426:     lu->firsttag = 0 ;
427:     newA = InpMtx_MPI_split(lu->mtxA, lu->vtxmapIV, lu->stats,
428:                         lu->options.msglvl, lu->options.msgFile, lu->firsttag, lu->comm_spooles);
429:     lu->firsttag += size ;

431:     InpMtx_free(lu->mtxA);
432:     lu->mtxA = newA ;
433:     InpMtx_changeStorageMode(lu->mtxA, INPMTX_BY_VECTORS);
434:     if ( lu->options.msglvl > 2 ) {
435:       int err;
436:       PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n\n split InpMtx");
437:       InpMtx_writeForHumanEye(lu->mtxA, lu->options.msgFile);
438:       err = fflush(lu->options.msgFile);
439:       if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
440:     }
441: 
442:     /* compute the symbolic factorization */
443:     lu->symbfacIVL = SymbFac_MPI_initFromInpMtx(lu->frontETree, lu->ownersIV, lu->mtxA,
444:                      lu->stats, lu->options.msglvl, lu->options.msgFile, lu->firsttag, lu->comm_spooles);
445:     lu->firsttag += lu->frontETree->nfront ;
446:     if ( lu->options.msglvl > 2 ) {
447:       int err;
448:       PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n\n local symbolic factorization");
449:       IVL_writeForHumanEye(lu->symbfacIVL, lu->options.msgFile);
450:       err = fflush(lu->options.msgFile);
451:       if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
452:     }

454:     lu->mtxmanager = SubMtxManager_new();
455:     SubMtxManager_init(lu->mtxmanager, NO_LOCK, 0);
456:     lu->frontmtx = FrontMtx_new();

458:   } else { /* new num factorization using previously computed symbolic factor */
459:     /* different FrontMtx is required */
460:     FrontMtx_free(lu->frontmtx);
461:     lu->frontmtx   = FrontMtx_new();

463:     SubMtxManager_free(lu->mtxmanager);
464:     lu->mtxmanager = SubMtxManager_new();
465:     SubMtxManager_init(lu->mtxmanager, NO_LOCK, 0);

467:     /* permute mtxA */
468:     InpMtx_permute(lu->mtxA, IV_entries(lu->oldToNewIV), IV_entries(lu->oldToNewIV));
469:     if ( lu->options.symflag == SPOOLES_SYMMETRIC ) InpMtx_mapToUpperTriangle(lu->mtxA);
470: 
471:     InpMtx_changeCoordType(lu->mtxA, INPMTX_BY_CHEVRONS);
472:     InpMtx_changeStorageMode(lu->mtxA, INPMTX_BY_VECTORS);

474:     /* redistribute the matrix */
475:     MPI_Barrier(((PetscObject)A)->comm);
476:     lu->firsttag = 0;
477:     newA = InpMtx_MPI_split(lu->mtxA, lu->vtxmapIV, lu->stats,
478:                         lu->options.msglvl, lu->options.msgFile, lu->firsttag,lu->comm_spooles);
479:     lu->firsttag += size ;

481:     InpMtx_free(lu->mtxA);
482:     lu->mtxA = newA ;
483:     InpMtx_changeStorageMode(lu->mtxA, INPMTX_BY_VECTORS);
484:     if ( lu->options.msglvl > 2 ) {
485:       int err;
486:       PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n\n split InpMtx");
487:       InpMtx_writeForHumanEye(lu->mtxA, lu->options.msgFile);
488:       err = fflush(lu->options.msgFile);
489:       if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
490:     }
491:   } /* end of if ( lu->flg == DIFFERENT_NONZERO_PATTERN) */

493:   FrontMtx_init(lu->frontmtx, lu->frontETree, lu->symbfacIVL, lu->options.typeflag, lu->options.symflag,
494:               FRONTMTX_DENSE_FRONTS, lu->options.pivotingflag, NO_LOCK, rank,
495:               lu->ownersIV, lu->mtxmanager, lu->options.msglvl, lu->options.msgFile);

497:   if ( lu->options.symflag == SPOOLES_SYMMETRIC ) {
498:     if (lu->options.patchAndGoFlag == 1 || lu->options.patchAndGoFlag == 2 ){
499:       if (lu->frontmtx->patchinfo) PatchAndGoInfo_free(lu->frontmtx->patchinfo);
500:       lu->frontmtx->patchinfo = PatchAndGoInfo_new();
501:       PatchAndGoInfo_init(lu->frontmtx->patchinfo, lu->options.patchAndGoFlag, lu->options.toosmall, lu->options.fudge,
502:                        lu->options.storeids, lu->options.storevalues);
503:     }
504:   }

506:   /* numerical factorization */
507:   chvmanager = ChvManager_new();
508:   ChvManager_init(chvmanager, NO_LOCK, 0);

510:   tagbound = maxTagMPI(lu->comm_spooles);
511:   lasttag  = lu->firsttag + 3*lu->frontETree->nfront + 2;
512:   if ( lasttag > tagbound ) {
513:       SETERRQ3(PETSC_ERR_LIB,"fatal error in FrontMtx_MPI_factorInpMtx(), tag range is [%d,%d], tag_bound = %d",\
514:                lu->firsttag, lasttag, tagbound);
515:   }
516:   rootchv = FrontMtx_MPI_factorInpMtx(lu->frontmtx, lu->mtxA, lu->options.tau, droptol,
517:                      chvmanager, lu->ownersIV, lookahead, &sierr, lu->cpus,
518:                      lu->stats, lu->options.msglvl, lu->options.msgFile, lu->firsttag,lu->comm_spooles);
519:   ChvManager_free(chvmanager);
520:   lu->firsttag = lasttag;
521:   if ( lu->options.msglvl > 2 ) {
522:     int err;
523:     PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n\n numeric factorization");
524:     FrontMtx_writeForHumanEye(lu->frontmtx, lu->options.msgFile);
525:     err = fflush(lu->options.msgFile);
526:     if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
527:   }

529:   if ( lu->options.symflag == SPOOLES_SYMMETRIC ) {
530:     if ( lu->options.patchAndGoFlag == 1 ) {
531:       if ( lu->frontmtx->patchinfo->fudgeIV != NULL ) {
532:         if (lu->options.msglvl > 0 ){
533:           PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n small pivots found at these locations");
534:           IV_writeForHumanEye(lu->frontmtx->patchinfo->fudgeIV, lu->options.msgFile);
535:         }
536:       }
537:       PatchAndGoInfo_free(lu->frontmtx->patchinfo);
538:     } else if ( lu->options.patchAndGoFlag == 2 ) {
539:       if (lu->options.msglvl > 0 ){
540:         if ( lu->frontmtx->patchinfo->fudgeIV != NULL ) {
541:           PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n small pivots found at these locations");
542:           IV_writeForHumanEye(lu->frontmtx->patchinfo->fudgeIV, lu->options.msgFile);
543:         }
544:         if ( lu->frontmtx->patchinfo->fudgeDV != NULL ) {
545:           PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n perturbations");
546:           DV_writeForHumanEye(lu->frontmtx->patchinfo->fudgeDV, lu->options.msgFile);
547:         }
548:       }
549:       PatchAndGoInfo_free(lu->frontmtx->patchinfo);
550:     }
551:   }
552:   if ( sierr >= 0 ) SETERRQ2(PETSC_ERR_LIB,"\n proc %d : factorization error at front %d", rank, sierr);
553: 
554:   /*  post-process the factorization and split 
555:       the factor matrices into submatrices */
556:   lasttag  = lu->firsttag + 5*size;
557:   if ( lasttag > tagbound ) {
558:       SETERRQ3(PETSC_ERR_LIB,"fatal error in FrontMtx_MPI_postProcess(), tag range is [%d,%d], tag_bound = %d",\
559:                lu->firsttag, lasttag, tagbound);
560:   }
561:   FrontMtx_MPI_postProcess(lu->frontmtx, lu->ownersIV, lu->stats, lu->options.msglvl,
562:                          lu->options.msgFile, lu->firsttag, lu->comm_spooles);
563:   lu->firsttag += 5*size ;
564:   if ( lu->options.msglvl > 2 ) {
565:     int err;
566:     PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n\n numeric factorization after post-processing");
567:     FrontMtx_writeForHumanEye(lu->frontmtx, lu->options.msgFile);
568:     err = fflush(lu->options.msgFile);
569:     if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
570:   }
571: 
572:   /* create the solve map object */
573:   if (lu->solvemap) SolveMap_free(lu->solvemap);
574:   lu->solvemap = SolveMap_new();
575:   SolveMap_ddMap(lu->solvemap, lu->frontmtx->symmetryflag,
576:                FrontMtx_upperBlockIVL(lu->frontmtx),
577:                FrontMtx_lowerBlockIVL(lu->frontmtx),
578:                size, lu->ownersIV, FrontMtx_frontTree(lu->frontmtx),
579:                lu->options.seed, lu->options.msglvl, lu->options.msgFile);
580:   if ( lu->options.msglvl > 2 ) {
581:     int err;
582:     SolveMap_writeForHumanEye(lu->solvemap, lu->options.msgFile);
583:     err = fflush(lu->options.msgFile);
584:     if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
585:   }

587:   /* redistribute the submatrices of the factors */
588:   FrontMtx_MPI_split(lu->frontmtx, lu->solvemap,
589:                    lu->stats, lu->options.msglvl, lu->options.msgFile, lu->firsttag, lu->comm_spooles);
590:   if ( lu->options.msglvl > 2 ) {
591:     int err;
592:     PetscFPrintf(PETSC_COMM_SELF,lu->options.msgFile, "\n\n numeric factorization after split");
593:     FrontMtx_writeForHumanEye(lu->frontmtx, lu->options.msgFile);
594:     err = fflush(lu->options.msgFile);
595:     if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
596:   }

598:   /* create a solution DenseMtx object */
599:   lu->ownedColumnsIV = FrontMtx_ownedColumnsIV(lu->frontmtx, rank, lu->ownersIV,
600:                                          lu->options.msglvl, lu->options.msgFile);
601:   lu->nmycol = IV_size(lu->ownedColumnsIV);
602:   if ( lu->nmycol > 0) {
603:     DenseMtx_init(lu->mtxX, lu->options.typeflag, 0, 0, lu->nmycol, 1, 1, lu->nmycol);
604:     /* get pointers rowindX and entX */
605:     DenseMtx_rowIndices(lu->mtxX, &lu->nmycol, &lu->rowindX);
606:     lu->entX = DenseMtx_entries(lu->mtxX);
607:   } else { /* lu->nmycol == 0 */
608:     lu->entX    = 0;
609:     lu->rowindX = 0;
610:   }

612:   if ( lu->scat ){
613:     VecDestroy(lu->vec_spooles);
614:     ISDestroy(lu->iden);
615:     ISDestroy(lu->is_petsc);
616:     VecScatterDestroy(lu->scat);
617:   }
618:   lu->scat      = PETSC_NULL;
619:   lu->flg       = SAME_NONZERO_PATTERN;
620:   F->ops->solve = MatSolve_MPISpooles;

622:   lu->CleanUpSpooles = PETSC_TRUE;
623:   return(0);
624: }