Actual source code: ispai.c


  2: /*
  3:    3/99 Modified by Stephen Barnard to support SPAI version 3.0
  4: */

  6: /*
  7:       Provides an interface to the SPAI Sparse Approximate Inverse Preconditioner
  8:    Code written by Stephen Barnard.

 10:       Note: there is some BAD memory bleeding below!

 12:       This code needs work

 14:    1) get rid of all memory bleeding
 15:    2) fix PETSc/interface so that it gets if the matrix is symmetric from the matrix
 16:       rather than having the sp flag for PC_SPAI
 17:    3) fix to set the block size based on the matrix block size

 19: */
 20: #define PETSC_SKIP_COMPLEX /* since spai uses I which conflicts with some complex implementations */

 22: #include <petsc/private/pcimpl.h>
 23: #include <../src/ksp/pc/impls/spai/petscspai.h>

 25: /*
 26:     These are the SPAI include files
 27: */
 28: EXTERN_C_BEGIN
 29: #define SPAI_USE_MPI /* required for setting SPAI_Comm correctly in basics.h */
 30: #include <spai.h>
 31: #include <matrix.h>
 32: EXTERN_C_END

 34: extern PetscErrorCode ConvertMatToMatrix(MPI_Comm,Mat,Mat,matrix**);
 35: extern PetscErrorCode ConvertMatrixToMat(MPI_Comm,matrix*,Mat*);
 36: extern PetscErrorCode ConvertVectorToVec(MPI_Comm,vector *v,Vec *Pv);
 37: extern PetscErrorCode MM_to_PETSC(char*,char*,char*);

 39: typedef struct {

 41:   matrix *B;                /* matrix in SPAI format */
 42:   matrix *BT;               /* transpose of matrix in SPAI format */
 43:   matrix *M;                /* the approximate inverse in SPAI format */

 45:   Mat PM;                   /* the approximate inverse PETSc format */

 47:   double epsilon;           /* tolerance */
 48:   int    nbsteps;           /* max number of "improvement" steps per line */
 49:   int    max;               /* max dimensions of is_I, q, etc. */
 50:   int    maxnew;            /* max number of new entries per step */
 51:   int    block_size;        /* constant block size */
 52:   int    cache_size;        /* one of (1,2,3,4,5,6) indicting size of cache */
 53:   int    verbose;           /* SPAI prints timing and statistics */

 55:   int      sp;              /* symmetric nonzero pattern */
 56:   MPI_Comm comm_spai;     /* communicator to be used with spai */
 57: } PC_SPAI;

 59: /**********************************************************************/

 61: static PetscErrorCode PCSetUp_SPAI(PC pc)
 62: {
 63:   PC_SPAI        *ispai = (PC_SPAI*)pc->data;
 65:   Mat            AT;

 68:   init_SPAI();

 70:   if (ispai->sp) {
 71:     ConvertMatToMatrix(ispai->comm_spai,pc->pmat,pc->pmat,&ispai->B);
 72:   } else {
 73:     /* Use the transpose to get the column nonzero structure. */
 74:     MatTranspose(pc->pmat,MAT_INITIAL_MATRIX,&AT);
 75:     ConvertMatToMatrix(ispai->comm_spai,pc->pmat,AT,&ispai->B);
 76:     MatDestroy(&AT);
 77:   }

 79:   /* Destroy the transpose */
 80:   /* Don't know how to do it. PETSc developers? */

 82:   /* construct SPAI preconditioner */
 83:   /* FILE *messages */     /* file for warning messages */
 84:   /* double epsilon */     /* tolerance */
 85:   /* int nbsteps */        /* max number of "improvement" steps per line */
 86:   /* int max */            /* max dimensions of is_I, q, etc. */
 87:   /* int maxnew */         /* max number of new entries per step */
 88:   /* int block_size */     /* block_size == 1 specifies scalar elments
 89:                               block_size == n specifies nxn constant-block elements
 90:                               block_size == 0 specifies variable-block elements */
 91:   /* int cache_size */     /* one of (1,2,3,4,5,6) indicting size of cache. cache_size == 0 indicates no caching */
 92:   /* int    verbose    */  /* verbose == 0 specifies that SPAI is silent
 93:                               verbose == 1 prints timing and matrix statistics */

 95:   bspai(ispai->B,&ispai->M,
 96:                stdout,
 97:                ispai->epsilon,
 98:                ispai->nbsteps,
 99:                ispai->max,
100:                ispai->maxnew,
101:                ispai->block_size,
102:                ispai->cache_size,
103:                ispai->verbose);

105:   ConvertMatrixToMat(PetscObjectComm((PetscObject)pc),ispai->M,&ispai->PM);

107:   /* free the SPAI matrices */
108:   sp_free_matrix(ispai->B);
109:   sp_free_matrix(ispai->M);
110:   return(0);
111: }

113: /**********************************************************************/

115: static PetscErrorCode PCApply_SPAI(PC pc,Vec xx,Vec y)
116: {
117:   PC_SPAI        *ispai = (PC_SPAI*)pc->data;

121:   /* Now using PETSc's multiply */
122:   MatMult(ispai->PM,xx,y);
123:   return(0);
124: }

126: static PetscErrorCode PCMatApply_SPAI(PC pc,Mat X,Mat Y)
127: {
128:   PC_SPAI        *ispai = (PC_SPAI*)pc->data;

132:   /* Now using PETSc's multiply */
133:   MatMatMult(ispai->PM,X,MAT_REUSE_MATRIX,PETSC_DEFAULT,&Y);
134:   return(0);
135: }

137: /**********************************************************************/

139: static PetscErrorCode PCDestroy_SPAI(PC pc)
140: {
142:   PC_SPAI        *ispai = (PC_SPAI*)pc->data;

145:   MatDestroy(&ispai->PM);
146:   MPI_Comm_free(&(ispai->comm_spai));
147:   PetscFree(pc->data);
148:   return(0);
149: }

151: /**********************************************************************/

153: static PetscErrorCode PCView_SPAI(PC pc,PetscViewer viewer)
154: {
155:   PC_SPAI        *ispai = (PC_SPAI*)pc->data;
157:   PetscBool      iascii;

160:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
161:   if (iascii) {
162:     PetscViewerASCIIPrintf(viewer,"    epsilon %g\n",   (double)ispai->epsilon);
163:     PetscViewerASCIIPrintf(viewer,"    nbsteps %d\n",   ispai->nbsteps);
164:     PetscViewerASCIIPrintf(viewer,"    max %d\n",       ispai->max);
165:     PetscViewerASCIIPrintf(viewer,"    maxnew %d\n",    ispai->maxnew);
166:     PetscViewerASCIIPrintf(viewer,"    block_size %d\n",ispai->block_size);
167:     PetscViewerASCIIPrintf(viewer,"    cache_size %d\n",ispai->cache_size);
168:     PetscViewerASCIIPrintf(viewer,"    verbose %d\n",   ispai->verbose);
169:     PetscViewerASCIIPrintf(viewer,"    sp %d\n",        ispai->sp);
170:   }
171:   return(0);
172: }

174: static PetscErrorCode  PCSPAISetEpsilon_SPAI(PC pc,double epsilon1)
175: {
176:   PC_SPAI *ispai = (PC_SPAI*)pc->data;

179:   ispai->epsilon = epsilon1;
180:   return(0);
181: }

183: /**********************************************************************/

185: static PetscErrorCode  PCSPAISetNBSteps_SPAI(PC pc,int nbsteps1)
186: {
187:   PC_SPAI *ispai = (PC_SPAI*)pc->data;

190:   ispai->nbsteps = nbsteps1;
191:   return(0);
192: }

194: /**********************************************************************/

196: /* added 1/7/99 g.h. */
197: static PetscErrorCode  PCSPAISetMax_SPAI(PC pc,int max1)
198: {
199:   PC_SPAI *ispai = (PC_SPAI*)pc->data;

202:   ispai->max = max1;
203:   return(0);
204: }

206: /**********************************************************************/

208: static PetscErrorCode  PCSPAISetMaxNew_SPAI(PC pc,int maxnew1)
209: {
210:   PC_SPAI *ispai = (PC_SPAI*)pc->data;

213:   ispai->maxnew = maxnew1;
214:   return(0);
215: }

217: /**********************************************************************/

219: static PetscErrorCode  PCSPAISetBlockSize_SPAI(PC pc,int block_size1)
220: {
221:   PC_SPAI *ispai = (PC_SPAI*)pc->data;

224:   ispai->block_size = block_size1;
225:   return(0);
226: }

228: /**********************************************************************/

230: static PetscErrorCode  PCSPAISetCacheSize_SPAI(PC pc,int cache_size)
231: {
232:   PC_SPAI *ispai = (PC_SPAI*)pc->data;

235:   ispai->cache_size = cache_size;
236:   return(0);
237: }

239: /**********************************************************************/

241: static PetscErrorCode  PCSPAISetVerbose_SPAI(PC pc,int verbose)
242: {
243:   PC_SPAI *ispai = (PC_SPAI*)pc->data;

246:   ispai->verbose = verbose;
247:   return(0);
248: }

250: /**********************************************************************/

252: static PetscErrorCode  PCSPAISetSp_SPAI(PC pc,int sp)
253: {
254:   PC_SPAI *ispai = (PC_SPAI*)pc->data;

257:   ispai->sp = sp;
258:   return(0);
259: }

261: /* -------------------------------------------------------------------*/

263: /*@
264:   PCSPAISetEpsilon -- Set the tolerance for the SPAI preconditioner

266:   Input Parameters:
267: + pc - the preconditioner
268: - eps - epsilon (default .4)

270:   Notes:
271:     Espilon must be between 0 and 1. It controls the
272:                  quality of the approximation of M to the inverse of
273:                  A. Higher values of epsilon lead to more work, more
274:                  fill, and usually better preconditioners. In many
275:                  cases the best choice of epsilon is the one that
276:                  divides the total solution time equally between the
277:                  preconditioner and the solver.

279:   Level: intermediate

281: .seealso: PCSPAI, PCSetType()
282:   @*/
283: PetscErrorCode  PCSPAISetEpsilon(PC pc,double epsilon1)
284: {

288:   PetscTryMethod(pc,"PCSPAISetEpsilon_C",(PC,double),(pc,epsilon1));
289:   return(0);
290: }

292: /**********************************************************************/

294: /*@
295:   PCSPAISetNBSteps - set maximum number of improvement steps per row in
296:         the SPAI preconditioner

298:   Input Parameters:
299: + pc - the preconditioner
300: - n - number of steps (default 5)

302:   Notes:
303:     SPAI constructs to approximation to every column of
304:                  the exact inverse of A in a series of improvement
305:                  steps. The quality of the approximation is determined
306:                  by epsilon. If an approximation achieving an accuracy
307:                  of epsilon is not obtained after ns steps, SPAI simply
308:                  uses the best approximation constructed so far.

310:   Level: intermediate

312: .seealso: PCSPAI, PCSetType(), PCSPAISetMaxNew()
313: @*/
314: PetscErrorCode  PCSPAISetNBSteps(PC pc,int nbsteps1)
315: {

319:   PetscTryMethod(pc,"PCSPAISetNBSteps_C",(PC,int),(pc,nbsteps1));
320:   return(0);
321: }

323: /**********************************************************************/

325: /* added 1/7/99 g.h. */
326: /*@
327:   PCSPAISetMax - set the size of various working buffers in
328:         the SPAI preconditioner

330:   Input Parameters:
331: + pc - the preconditioner
332: - n - size (default is 5000)

334:   Level: intermediate

336: .seealso: PCSPAI, PCSetType()
337: @*/
338: PetscErrorCode  PCSPAISetMax(PC pc,int max1)
339: {

343:   PetscTryMethod(pc,"PCSPAISetMax_C",(PC,int),(pc,max1));
344:   return(0);
345: }

347: /**********************************************************************/

349: /*@
350:   PCSPAISetMaxNew - set maximum number of new nonzero candidates per step
351:    in SPAI preconditioner

353:   Input Parameters:
354: + pc - the preconditioner
355: - n - maximum number (default 5)

357:   Level: intermediate

359: .seealso: PCSPAI, PCSetType(), PCSPAISetNBSteps()
360: @*/
361: PetscErrorCode  PCSPAISetMaxNew(PC pc,int maxnew1)
362: {

366:   PetscTryMethod(pc,"PCSPAISetMaxNew_C",(PC,int),(pc,maxnew1));
367:   return(0);
368: }

370: /**********************************************************************/

372: /*@
373:   PCSPAISetBlockSize - set the block size for the SPAI preconditioner

375:   Input Parameters:
376: + pc - the preconditioner
377: - n - block size (default 1)

379:   Notes:
380:     A block
381:                  size of 1 treats A as a matrix of scalar elements. A
382:                  block size of s > 1 treats A as a matrix of sxs
383:                  blocks. A block size of 0 treats A as a matrix with
384:                  variable sized blocks, which are determined by
385:                  searching for dense square diagonal blocks in A.
386:                  This can be very effective for finite-element
387:                  matrices.

389:                  SPAI will convert A to block form, use a block
390:                  version of the preconditioner algorithm, and then
391:                  convert the result back to scalar form.

393:                  In many cases the a block-size parameter other than 1
394:                  can lead to very significant improvement in
395:                  performance.

397:   Level: intermediate

399: .seealso: PCSPAI, PCSetType()
400: @*/
401: PetscErrorCode  PCSPAISetBlockSize(PC pc,int block_size1)
402: {

406:   PetscTryMethod(pc,"PCSPAISetBlockSize_C",(PC,int),(pc,block_size1));
407:   return(0);
408: }

410: /**********************************************************************/

412: /*@
413:   PCSPAISetCacheSize - specify cache size in the SPAI preconditioner

415:   Input Parameters:
416: + pc - the preconditioner
417: - n -  cache size {0,1,2,3,4,5} (default 5)

419:   Notes:
420:     SPAI uses a hash table to cache messages and avoid
421:                  redundant communication. If suggest always using
422:                  5. This parameter is irrelevant in the serial
423:                  version.

425:   Level: intermediate

427: .seealso: PCSPAI, PCSetType()
428: @*/
429: PetscErrorCode  PCSPAISetCacheSize(PC pc,int cache_size)
430: {

434:   PetscTryMethod(pc,"PCSPAISetCacheSize_C",(PC,int),(pc,cache_size));
435:   return(0);
436: }

438: /**********************************************************************/

440: /*@
441:   PCSPAISetVerbose - verbosity level for the SPAI preconditioner

443:   Input Parameters:
444: + pc - the preconditioner
445: - n - level (default 1)

447:   Notes:
448:     print parameters, timings and matrix statistics

450:   Level: intermediate

452: .seealso: PCSPAI, PCSetType()
453: @*/
454: PetscErrorCode  PCSPAISetVerbose(PC pc,int verbose)
455: {

459:   PetscTryMethod(pc,"PCSPAISetVerbose_C",(PC,int),(pc,verbose));
460:   return(0);
461: }

463: /**********************************************************************/

465: /*@
466:   PCSPAISetSp - specify a symmetric matrix sparsity pattern in the SPAI preconditioner

468:   Input Parameters:
469: + pc - the preconditioner
470: - n - 0 or 1

472:   Notes:
473:     If A has a symmetric nonzero pattern use -sp 1 to
474:                  improve performance by eliminating some communication
475:                  in the parallel version. Even if A does not have a
476:                  symmetric nonzero pattern -sp 1 may well lead to good
477:                  results, but the code will not follow the published
478:                  SPAI algorithm exactly.

480:   Level: intermediate

482: .seealso: PCSPAI, PCSetType()
483: @*/
484: PetscErrorCode  PCSPAISetSp(PC pc,int sp)
485: {

489:   PetscTryMethod(pc,"PCSPAISetSp_C",(PC,int),(pc,sp));
490:   return(0);
491: }

493: /**********************************************************************/

495: /**********************************************************************/

497: static PetscErrorCode PCSetFromOptions_SPAI(PetscOptionItems *PetscOptionsObject,PC pc)
498: {
499:   PC_SPAI        *ispai = (PC_SPAI*)pc->data;
501:   int            nbsteps1,max1,maxnew1,block_size1,cache_size,verbose,sp;
502:   double         epsilon1;
503:   PetscBool      flg;

506:   PetscOptionsHead(PetscOptionsObject,"SPAI options");
507:   PetscOptionsReal("-pc_spai_epsilon","","PCSPAISetEpsilon",ispai->epsilon,&epsilon1,&flg);
508:   if (flg) {
509:     PCSPAISetEpsilon(pc,epsilon1);
510:   }
511:   PetscOptionsInt("-pc_spai_nbsteps","","PCSPAISetNBSteps",ispai->nbsteps,&nbsteps1,&flg);
512:   if (flg) {
513:     PCSPAISetNBSteps(pc,nbsteps1);
514:   }
515:   /* added 1/7/99 g.h. */
516:   PetscOptionsInt("-pc_spai_max","","PCSPAISetMax",ispai->max,&max1,&flg);
517:   if (flg) {
518:     PCSPAISetMax(pc,max1);
519:   }
520:   PetscOptionsInt("-pc_spai_maxnew","","PCSPAISetMaxNew",ispai->maxnew,&maxnew1,&flg);
521:   if (flg) {
522:     PCSPAISetMaxNew(pc,maxnew1);
523:   }
524:   PetscOptionsInt("-pc_spai_block_size","","PCSPAISetBlockSize",ispai->block_size,&block_size1,&flg);
525:   if (flg) {
526:     PCSPAISetBlockSize(pc,block_size1);
527:   }
528:   PetscOptionsInt("-pc_spai_cache_size","","PCSPAISetCacheSize",ispai->cache_size,&cache_size,&flg);
529:   if (flg) {
530:     PCSPAISetCacheSize(pc,cache_size);
531:   }
532:   PetscOptionsInt("-pc_spai_verbose","","PCSPAISetVerbose",ispai->verbose,&verbose,&flg);
533:   if (flg) {
534:     PCSPAISetVerbose(pc,verbose);
535:   }
536:   PetscOptionsInt("-pc_spai_sp","","PCSPAISetSp",ispai->sp,&sp,&flg);
537:   if (flg) {
538:     PCSPAISetSp(pc,sp);
539:   }
540:   PetscOptionsTail();
541:   return(0);
542: }

544: /**********************************************************************/

546: /*MC
547:    PCSPAI - Use the Sparse Approximate Inverse method of Grote and Barnard
548:      as a preconditioner (SIAM J. Sci. Comput.; vol 18, nr 3)

550:    Options Database Keys:
551: +  -pc_spai_epsilon <eps> - set tolerance
552: .  -pc_spai_nbstep <n> - set nbsteps
553: .  -pc_spai_max <m> - set max
554: .  -pc_spai_max_new <m> - set maxnew
555: .  -pc_spai_block_size <n> - set block size
556: .  -pc_spai_cache_size <n> - set cache size
557: .  -pc_spai_sp <m> - set sp
558: -  -pc_spai_set_verbose <true,false> - verbose output

560:    Notes:
561:     This only works with AIJ matrices.

563:    Level: beginner

565: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC,
566:     PCSPAISetEpsilon(), PCSPAISetMax(), PCSPAISetMaxNew(), PCSPAISetBlockSize(),
567:     PCSPAISetVerbose(), PCSPAISetSp()
568: M*/

570: PETSC_EXTERN PetscErrorCode PCCreate_SPAI(PC pc)
571: {
572:   PC_SPAI        *ispai;

576:   PetscNewLog(pc,&ispai);
577:   pc->data = ispai;

579:   pc->ops->destroy         = PCDestroy_SPAI;
580:   pc->ops->apply           = PCApply_SPAI;
581:   pc->ops->matapply        = PCMatApply_SPAI;
582:   pc->ops->applyrichardson = 0;
583:   pc->ops->setup           = PCSetUp_SPAI;
584:   pc->ops->view            = PCView_SPAI;
585:   pc->ops->setfromoptions  = PCSetFromOptions_SPAI;

587:   ispai->epsilon    = .4;
588:   ispai->nbsteps    = 5;
589:   ispai->max        = 5000;
590:   ispai->maxnew     = 5;
591:   ispai->block_size = 1;
592:   ispai->cache_size = 5;
593:   ispai->verbose    = 0;

595:   ispai->sp = 1;
596:   MPI_Comm_dup(PetscObjectComm((PetscObject)pc),&(ispai->comm_spai));

598:   PetscObjectComposeFunction((PetscObject)pc,"PCSPAISetEpsilon_C",PCSPAISetEpsilon_SPAI);
599:   PetscObjectComposeFunction((PetscObject)pc,"PCSPAISetNBSteps_C",PCSPAISetNBSteps_SPAI);
600:   PetscObjectComposeFunction((PetscObject)pc,"PCSPAISetMax_C",PCSPAISetMax_SPAI);
601:   PetscObjectComposeFunction((PetscObject)pc,"PCSPAISetMaxNew_C",PCSPAISetMaxNew_SPAI);
602:   PetscObjectComposeFunction((PetscObject)pc,"PCSPAISetBlockSize_C",PCSPAISetBlockSize_SPAI);
603:   PetscObjectComposeFunction((PetscObject)pc,"PCSPAISetCacheSize_C",PCSPAISetCacheSize_SPAI);
604:   PetscObjectComposeFunction((PetscObject)pc,"PCSPAISetVerbose_C",PCSPAISetVerbose_SPAI);
605:   PetscObjectComposeFunction((PetscObject)pc,"PCSPAISetSp_C",PCSPAISetSp_SPAI);
606:   return(0);
607: }

609: /**********************************************************************/

611: /*
612:    Converts from a PETSc matrix to an SPAI matrix
613: */
614: PetscErrorCode ConvertMatToMatrix(MPI_Comm comm, Mat A,Mat AT,matrix **B)
615: {
616:   matrix                  *M;
617:   int                     i,j,col;
618:   int                     row_indx;
619:   int                     len,pe,local_indx,start_indx;
620:   int                     *mapping;
621:   PetscErrorCode          ierr;
622:   const int               *cols;
623:   const double            *vals;
624:   int                     n,mnl,nnl,nz,rstart,rend;
625:   PetscMPIInt             size,rank;
626:   struct compressed_lines *rows;

629:   MPI_Comm_size(comm,&size);
630:   MPI_Comm_rank(comm,&rank);
631:   MatGetSize(A,&n,&n);
632:   MatGetLocalSize(A,&mnl,&nnl);

634:   /*
635:     not sure why a barrier is required. commenting out
636:   MPI_Barrier(comm);
637:   */

639:   M = new_matrix((SPAI_Comm)comm);

641:   M->n              = n;
642:   M->bs             = 1;
643:   M->max_block_size = 1;

645:   M->mnls          = (int*)malloc(sizeof(int)*size);
646:   M->start_indices = (int*)malloc(sizeof(int)*size);
647:   M->pe            = (int*)malloc(sizeof(int)*n);
648:   M->block_sizes   = (int*)malloc(sizeof(int)*n);
649:   for (i=0; i<n; i++) M->block_sizes[i] = 1;

651:   MPI_Allgather(&mnl,1,MPI_INT,M->mnls,1,MPI_INT,comm);

653:   M->start_indices[0] = 0;
654:   for (i=1; i<size; i++) M->start_indices[i] = M->start_indices[i-1] + M->mnls[i-1];

656:   M->mnl            = M->mnls[M->myid];
657:   M->my_start_index = M->start_indices[M->myid];

659:   for (i=0; i<size; i++) {
660:     start_indx = M->start_indices[i];
661:     for (j=0; j<M->mnls[i]; j++) M->pe[start_indx+j] = i;
662:   }

664:   if (AT) {
665:     M->lines = new_compressed_lines(M->mnls[rank],1);
666:   } else {
667:     M->lines = new_compressed_lines(M->mnls[rank],0);
668:   }

670:   rows = M->lines;

672:   /* Determine the mapping from global indices to pointers */
673:   PetscMalloc1(M->n,&mapping);
674:   pe         = 0;
675:   local_indx = 0;
676:   for (i=0; i<M->n; i++) {
677:     if (local_indx >= M->mnls[pe]) {
678:       pe++;
679:       local_indx = 0;
680:     }
681:     mapping[i] = local_indx + M->start_indices[pe];
682:     local_indx++;
683:   }

685:   /*********************************************************/
686:   /************** Set up the row structure *****************/
687:   /*********************************************************/

689:   MatGetOwnershipRange(A,&rstart,&rend);
690:   for (i=rstart; i<rend; i++) {
691:     row_indx = i - rstart;
692:     MatGetRow(A,i,&nz,&cols,&vals);
693:     /* allocate buffers */
694:     rows->ptrs[row_indx] = (int*)malloc(nz*sizeof(int));
695:     rows->A[row_indx]    = (double*)malloc(nz*sizeof(double));
696:     /* copy the matrix */
697:     for (j=0; j<nz; j++) {
698:       col = cols[j];
699:       len = rows->len[row_indx]++;

701:       rows->ptrs[row_indx][len] = mapping[col];
702:       rows->A[row_indx][len]    = vals[j];
703:     }
704:     rows->slen[row_indx] = rows->len[row_indx];

706:     MatRestoreRow(A,i,&nz,&cols,&vals);
707:   }

709:   /************************************************************/
710:   /************** Set up the column structure *****************/
711:   /*********************************************************/

713:   if (AT) {

715:     for (i=rstart; i<rend; i++) {
716:       row_indx = i - rstart;
717:       MatGetRow(AT,i,&nz,&cols,&vals);
718:       /* allocate buffers */
719:       rows->rptrs[row_indx] = (int*)malloc(nz*sizeof(int));
720:       /* copy the matrix (i.e., the structure) */
721:       for (j=0; j<nz; j++) {
722:         col = cols[j];
723:         len = rows->rlen[row_indx]++;

725:         rows->rptrs[row_indx][len] = mapping[col];
726:       }
727:       MatRestoreRow(AT,i,&nz,&cols,&vals);
728:     }
729:   }

731:   PetscFree(mapping);

733:   order_pointers(M);
734:   M->maxnz = calc_maxnz(M);
735:   *B       = M;
736:   return(0);
737: }

739: /**********************************************************************/

741: /*
742:    Converts from an SPAI matrix B  to a PETSc matrix PB.
743:    This assumes that the SPAI matrix B is stored in
744:    COMPRESSED-ROW format.
745: */
746: PetscErrorCode ConvertMatrixToMat(MPI_Comm comm,matrix *B,Mat *PB)
747: {
748:   PetscMPIInt    size,rank;
750:   int            m,n,M,N;
751:   int            d_nz,o_nz;
752:   int            *d_nnz,*o_nnz;
753:   int            i,k,global_row,global_col,first_diag_col,last_diag_col;
754:   PetscScalar    val;

757:   MPI_Comm_size(comm,&size);
758:   MPI_Comm_rank(comm,&rank);

760:   m    = n = B->mnls[rank];
761:   d_nz = o_nz = 0;

763:   /* Determine preallocation for MatCreateAIJ */
764:   PetscMalloc1(m,&d_nnz);
765:   PetscMalloc1(m,&o_nnz);
766:   for (i=0; i<m; i++) d_nnz[i] = o_nnz[i] = 0;
767:   first_diag_col = B->start_indices[rank];
768:   last_diag_col  = first_diag_col + B->mnls[rank];
769:   for (i=0; i<B->mnls[rank]; i++) {
770:     for (k=0; k<B->lines->len[i]; k++) {
771:       global_col = B->lines->ptrs[i][k];
772:       if ((global_col >= first_diag_col) && (global_col < last_diag_col)) d_nnz[i]++;
773:       else o_nnz[i]++;
774:     }
775:   }

777:   M = N = B->n;
778:   /* Here we only know how to create AIJ format */
779:   MatCreate(comm,PB);
780:   MatSetSizes(*PB,m,n,M,N);
781:   MatSetType(*PB,MATAIJ);
782:   MatSeqAIJSetPreallocation(*PB,d_nz,d_nnz);
783:   MatMPIAIJSetPreallocation(*PB,d_nz,d_nnz,o_nz,o_nnz);

785:   for (i=0; i<B->mnls[rank]; i++) {
786:     global_row = B->start_indices[rank]+i;
787:     for (k=0; k<B->lines->len[i]; k++) {
788:       global_col = B->lines->ptrs[i][k];

790:       val  = B->lines->A[i][k];
791:       MatSetValues(*PB,1,&global_row,1,&global_col,&val,ADD_VALUES);
792:     }
793:   }

795:   PetscFree(d_nnz);
796:   PetscFree(o_nnz);

798:   MatAssemblyBegin(*PB,MAT_FINAL_ASSEMBLY);
799:   MatAssemblyEnd(*PB,MAT_FINAL_ASSEMBLY);
800:   return(0);
801: }

803: /**********************************************************************/

805: /*
806:    Converts from an SPAI vector v  to a PETSc vec Pv.
807: */
808: PetscErrorCode ConvertVectorToVec(MPI_Comm comm,vector *v,Vec *Pv)
809: {
811:   PetscMPIInt    size,rank;
812:   int            m,M,i,*mnls,*start_indices,*global_indices;

815:   MPI_Comm_size(comm,&size);
816:   MPI_Comm_rank(comm,&rank);

818:   m = v->mnl;
819:   M = v->n;

821:   VecCreateMPI(comm,m,M,Pv);

823:   PetscMalloc1(size,&mnls);
824:   MPI_Allgather(&v->mnl,1,MPI_INT,mnls,1,MPI_INT,comm);

826:   PetscMalloc1(size,&start_indices);

828:   start_indices[0] = 0;
829:   for (i=1; i<size; i++) start_indices[i] = start_indices[i-1] +mnls[i-1];

831:   PetscMalloc1(v->mnl,&global_indices);
832:   for (i=0; i<v->mnl; i++) global_indices[i] = start_indices[rank] + i;

834:   PetscFree(mnls);
835:   PetscFree(start_indices);

837:   VecSetValues(*Pv,v->mnl,global_indices,v->v,INSERT_VALUES);
838:   VecAssemblyBegin(*Pv);
839:   VecAssemblyEnd(*Pv);

841:   PetscFree(global_indices);
842:   return(0);
843: }