xref: /petsc/src/mat/impls/aij/mpi/pastix/pastix.c (revision 7dae84e064f15683b0c1756735f0ad1de62763f6)
13bf14a46SMatthew Knepley /*
23bf14a46SMatthew Knepley  Provides an interface to the PaStiX sparse solver
33bf14a46SMatthew Knepley  */
4c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/aij.h>
5c6db04a5SJed Brown #include <../src/mat/impls/aij/mpi/mpiaij.h>
6c6db04a5SJed Brown #include <../src/mat/impls/sbaij/seq/sbaij.h>
7c6db04a5SJed Brown #include <../src/mat/impls/sbaij/mpi/mpisbaij.h>
83bf14a46SMatthew Knepley 
9dbbbd53dSSatish Balay #if defined(PETSC_USE_COMPLEX)
105ec454cfSSatish Balay #define _H_COMPLEX
115ec454cfSSatish Balay #endif
125ec454cfSSatish Balay 
133bf14a46SMatthew Knepley EXTERN_C_BEGIN
14c6db04a5SJed Brown #include <pastix.h>
153bf14a46SMatthew Knepley EXTERN_C_END
163bf14a46SMatthew Knepley 
17519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
18519f805aSKarl Rupp #if defined(PETSC_USE_REAL_SINGLE)
195ec454cfSSatish Balay #define PASTIX_CALL c_pastix
205ec454cfSSatish Balay #define PASTIX_CHECKMATRIX c_pastix_checkMatrix
215ec454cfSSatish Balay #else
225ec454cfSSatish Balay #define PASTIX_CALL z_pastix
235ec454cfSSatish Balay #define PASTIX_CHECKMATRIX z_pastix_checkMatrix
245ec454cfSSatish Balay #endif
25d41469e0Sxavier lacoste 
26d41469e0Sxavier lacoste #else /* PETSC_USE_COMPLEX */
27d41469e0Sxavier lacoste 
28519f805aSKarl Rupp #if defined(PETSC_USE_REAL_SINGLE)
295ec454cfSSatish Balay #define PASTIX_CALL s_pastix
305ec454cfSSatish Balay #define PASTIX_CHECKMATRIX s_pastix_checkMatrix
315ec454cfSSatish Balay #else
325ec454cfSSatish Balay #define PASTIX_CALL d_pastix
335ec454cfSSatish Balay #define PASTIX_CHECKMATRIX d_pastix_checkMatrix
345ec454cfSSatish Balay #endif
35d41469e0Sxavier lacoste 
36d41469e0Sxavier lacoste #endif /* PETSC_USE_COMPLEX */
37d41469e0Sxavier lacoste 
38dbbbd53dSSatish Balay typedef PetscScalar PastixScalar;
39dbbbd53dSSatish Balay 
403bf14a46SMatthew Knepley typedef struct Mat_Pastix_ {
413bf14a46SMatthew Knepley   pastix_data_t *pastix_data;    /* Pastix data storage structure                        */
423bf14a46SMatthew Knepley   MatStructure  matstruc;
433bf14a46SMatthew Knepley   PetscInt      n;               /* Number of columns in the matrix                      */
443bf14a46SMatthew Knepley   PetscInt      *colptr;         /* Index of first element of each column in row and val */
453bf14a46SMatthew Knepley   PetscInt      *row;            /* Row of each element of the matrix                    */
463bf14a46SMatthew Knepley   PetscScalar   *val;            /* Value of each element of the matrix                  */
473bf14a46SMatthew Knepley   PetscInt      *perm;           /* Permutation tabular                                  */
483bf14a46SMatthew Knepley   PetscInt      *invp;           /* Reverse permutation tabular                          */
493bf14a46SMatthew Knepley   PetscScalar   *rhs;            /* Rhight-hand-side member                              */
503bf14a46SMatthew Knepley   PetscInt      rhsnbr;          /* Rhight-hand-side number (must be 1)                  */
513bf14a46SMatthew Knepley   PetscInt      iparm[64];       /* Integer parameters                                   */
523bf14a46SMatthew Knepley   double        dparm[64];       /* Floating point parameters                            */
533bf14a46SMatthew Knepley   MPI_Comm      pastix_comm;     /* PaStiX MPI communicator                              */
543bf14a46SMatthew Knepley   PetscMPIInt   commRank;        /* MPI rank                                             */
553bf14a46SMatthew Knepley   PetscMPIInt   commSize;        /* MPI communicator size                                */
56ace3abfcSBarry Smith   PetscBool     CleanUpPastix;   /* Boolean indicating if we call PaStiX clean step      */
573bf14a46SMatthew Knepley   VecScatter    scat_rhs;
583bf14a46SMatthew Knepley   VecScatter    scat_sol;
59f31ce8a6SBarry Smith   Vec           b_seq;
603bf14a46SMatthew Knepley } Mat_Pastix;
613bf14a46SMatthew Knepley 
6209573ac7SBarry Smith extern PetscErrorCode MatDuplicate_Pastix(Mat,MatDuplicateOption,Mat*);
633bf14a46SMatthew Knepley 
643bf14a46SMatthew Knepley /*
653bf14a46SMatthew Knepley    convert Petsc seqaij matrix to CSC: colptr[n], row[nz], val[nz]
663bf14a46SMatthew Knepley 
673bf14a46SMatthew Knepley   input:
683bf14a46SMatthew Knepley     A       - matrix in seqaij or mpisbaij (bs=1) format
693bf14a46SMatthew Knepley     valOnly - FALSE: spaces are allocated and values are set for the CSC
703bf14a46SMatthew Knepley               TRUE:  Only fill values
713bf14a46SMatthew Knepley   output:
723bf14a46SMatthew Knepley     n       - Size of the matrix
733bf14a46SMatthew Knepley     colptr  - Index of first element of each column in row and val
743bf14a46SMatthew Knepley     row     - Row of each element of the matrix
753bf14a46SMatthew Knepley     values  - Value of each element of the matrix
763bf14a46SMatthew Knepley  */
77ace3abfcSBarry Smith PetscErrorCode MatConvertToCSC(Mat A,PetscBool valOnly,PetscInt *n,PetscInt **colptr,PetscInt **row,PetscScalar **values)
7841c8de11SBarry Smith {
793bf14a46SMatthew Knepley   Mat_SeqAIJ     *aa      = (Mat_SeqAIJ*)A->data;
803bf14a46SMatthew Knepley   PetscInt       *rowptr  = aa->i;
813bf14a46SMatthew Knepley   PetscInt       *col     = aa->j;
823bf14a46SMatthew Knepley   PetscScalar    *rvalues = aa->a;
833bf14a46SMatthew Knepley   PetscInt       m        = A->rmap->N;
84745c78f7SBarry Smith   PetscInt       nnz;
853bf14a46SMatthew Knepley   PetscInt       i,j, k;
863bf14a46SMatthew Knepley   PetscInt       base = 1;
873bf14a46SMatthew Knepley   PetscInt       idx;
883bf14a46SMatthew Knepley   PetscErrorCode ierr;
893bf14a46SMatthew Knepley   PetscInt       colidx;
903bf14a46SMatthew Knepley   PetscInt       *colcount;
91ace3abfcSBarry Smith   PetscBool      isSBAIJ;
92ace3abfcSBarry Smith   PetscBool      isSeqSBAIJ;
93ace3abfcSBarry Smith   PetscBool      isMpiSBAIJ;
94ace3abfcSBarry Smith   PetscBool      isSym;
95d41469e0Sxavier lacoste   PetscBool      flg;
96d41469e0Sxavier lacoste   PetscInt       icntl;
97d41469e0Sxavier lacoste   PetscInt       verb;
98d41469e0Sxavier lacoste   PetscInt       check;
993bf14a46SMatthew Knepley 
1003bf14a46SMatthew Knepley   PetscFunctionBegin;
10141c8de11SBarry Smith   ierr = MatIsSymmetric(A,0.0,&isSym);CHKERRQ(ierr);
102251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATSBAIJ,&isSBAIJ);CHKERRQ(ierr);
103251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATSEQSBAIJ,&isSeqSBAIJ);CHKERRQ(ierr);
104251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATMPISBAIJ,&isMpiSBAIJ);CHKERRQ(ierr);
1053bf14a46SMatthew Knepley 
106745c78f7SBarry Smith   *n = A->cmap->N;
107745c78f7SBarry Smith 
108745c78f7SBarry Smith   /* PaStiX only needs triangular matrix if matrix is symmetric
109745c78f7SBarry Smith    */
1102205254eSKarl Rupp   if (isSym && !(isSBAIJ || isSeqSBAIJ || isMpiSBAIJ)) nnz = (aa->nz - *n)/2 + *n;
1112205254eSKarl Rupp   else nnz = aa->nz;
1123bf14a46SMatthew Knepley 
1133bf14a46SMatthew Knepley   if (!valOnly) {
114854ce69bSBarry Smith     ierr = PetscMalloc1((*n)+1,colptr);CHKERRQ(ierr);
115854ce69bSBarry Smith     ierr = PetscMalloc1(nnz,row);CHKERRQ(ierr);
116785e854fSJed Brown     ierr = PetscMalloc1(nnz,values);CHKERRQ(ierr);
1173bf14a46SMatthew Knepley 
11841c8de11SBarry Smith     if (isSBAIJ || isSeqSBAIJ || isMpiSBAIJ) {
11941c8de11SBarry Smith       ierr = PetscMemcpy (*colptr, rowptr, ((*n)+1)*sizeof(PetscInt));CHKERRQ(ierr);
1202205254eSKarl Rupp       for (i = 0; i < *n+1; i++) (*colptr)[i] += base;
12141c8de11SBarry Smith       ierr = PetscMemcpy (*row, col, (nnz)*sizeof(PetscInt));CHKERRQ(ierr);
1222205254eSKarl Rupp       for (i = 0; i < nnz; i++) (*row)[i] += base;
12341c8de11SBarry Smith       ierr = PetscMemcpy (*values, rvalues, (nnz)*sizeof(PetscScalar));CHKERRQ(ierr);
12441c8de11SBarry Smith     } else {
125854ce69bSBarry Smith       ierr = PetscMalloc1(*n,&colcount);CHKERRQ(ierr);
12641c8de11SBarry Smith 
127f31ce8a6SBarry Smith       for (i = 0; i < m; i++) colcount[i] = 0;
1283bf14a46SMatthew Knepley       /* Fill-in colptr */
129f31ce8a6SBarry Smith       for (i = 0; i < m; i++) {
130f31ce8a6SBarry Smith         for (j = rowptr[i]; j < rowptr[i+1]; j++) {
131f31ce8a6SBarry Smith           if (!isSym || col[j] <= i)  colcount[col[j]]++;
132f31ce8a6SBarry Smith         }
133f31ce8a6SBarry Smith       }
134745c78f7SBarry Smith 
1353bf14a46SMatthew Knepley       (*colptr)[0] = base;
1363bf14a46SMatthew Knepley       for (j = 0; j < *n; j++) {
1373bf14a46SMatthew Knepley         (*colptr)[j+1] = (*colptr)[j] + colcount[j];
138745c78f7SBarry Smith         /* in next loop we fill starting from (*colptr)[colidx] - base */
1393bf14a46SMatthew Knepley         colcount[j] = -base;
1403bf14a46SMatthew Knepley       }
1413bf14a46SMatthew Knepley 
1423bf14a46SMatthew Knepley       /* Fill-in rows and values */
1433bf14a46SMatthew Knepley       for (i = 0; i < m; i++) {
1443bf14a46SMatthew Knepley         for (j = rowptr[i]; j < rowptr[i+1]; j++) {
14541c8de11SBarry Smith           if (!isSym || col[j] <= i) {
1463bf14a46SMatthew Knepley             colidx         = col[j];
1473bf14a46SMatthew Knepley             idx            = (*colptr)[colidx] + colcount[colidx];
1483bf14a46SMatthew Knepley             (*row)[idx]    = i + base;
1493bf14a46SMatthew Knepley             (*values)[idx] = rvalues[j];
1503bf14a46SMatthew Knepley             colcount[colidx]++;
1513bf14a46SMatthew Knepley           }
1523bf14a46SMatthew Knepley         }
1533bf14a46SMatthew Knepley       }
15441c8de11SBarry Smith       ierr = PetscFree(colcount);CHKERRQ(ierr);
155745c78f7SBarry Smith     }
15641c8de11SBarry Smith   } else {
157745c78f7SBarry Smith     /* Fill-in only values */
1583bf14a46SMatthew Knepley     for (i = 0; i < m; i++) {
1593bf14a46SMatthew Knepley       for (j = rowptr[i]; j < rowptr[i+1]; j++) {
1603bf14a46SMatthew Knepley         colidx = col[j];
1612205254eSKarl Rupp         if ((isSBAIJ || isSeqSBAIJ || isMpiSBAIJ) ||!isSym || col[j] <= i) {
162745c78f7SBarry Smith           /* look for the value to fill */
163f31ce8a6SBarry Smith           for (k = (*colptr)[colidx] - base; k < (*colptr)[colidx + 1] - base; k++) {
164eb1f6c34SBarry Smith             if (((*row)[k]-base) == i) {
1653bf14a46SMatthew Knepley               (*values)[k] = rvalues[j];
1663bf14a46SMatthew Knepley               break;
1673bf14a46SMatthew Knepley             }
1683bf14a46SMatthew Knepley           }
169f31ce8a6SBarry Smith           /* data structure of sparse matrix has changed */
170e32f2f54SBarry Smith           if (k == (*colptr)[colidx + 1] - base) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"overflow on k %D",k);
1713bf14a46SMatthew Knepley         }
1723bf14a46SMatthew Knepley       }
1733bf14a46SMatthew Knepley     }
174745c78f7SBarry Smith   }
175d41469e0Sxavier lacoste 
176d41469e0Sxavier lacoste   icntl =-1;
177d41469e0Sxavier lacoste   check = 0;
178c5929fdfSBarry Smith   ierr  = PetscOptionsGetInt(NULL,((PetscObject) A)->prefix, "-mat_pastix_check", &icntl, &flg);CHKERRQ(ierr);
1792205254eSKarl Rupp   if ((flg && icntl >= 0) || PetscLogPrintInfo) check =  icntl;
1802205254eSKarl Rupp 
181d41469e0Sxavier lacoste   if (check == 1) {
18270fe17b1SSatish Balay     PetscScalar *tmpvalues;
18370fe17b1SSatish Balay     PetscInt    *tmprows,*tmpcolptr;
18458c95f1bSBarry Smith 
18558c95f1bSBarry Smith     ierr = PetscMalloc3(nnz,&tmpvalues,nnz,&tmprows,*n+1,&tmpcolptr);CHKERRQ(ierr);
18643801a69SSatish Balay 
18770fe17b1SSatish Balay     ierr = PetscMemcpy(tmpcolptr,*colptr,(*n+1)*sizeof(PetscInt));CHKERRQ(ierr);
18870fe17b1SSatish Balay     ierr = PetscMemcpy(tmprows,*row,nnz*sizeof(PetscInt));CHKERRQ(ierr);
18970fe17b1SSatish Balay     ierr = PetscMemcpy(tmpvalues,*values,nnz*sizeof(PetscScalar));CHKERRQ(ierr);
19043801a69SSatish Balay     ierr = PetscFree(*row);CHKERRQ(ierr);
19143801a69SSatish Balay     ierr = PetscFree(*values);CHKERRQ(ierr);
19243801a69SSatish Balay 
193d41469e0Sxavier lacoste     icntl=-1;
194d41469e0Sxavier lacoste     verb = API_VERBOSE_NOT;
195952ee8f4SMatthew G. Knepley     /* "iparm[IPARM_VERBOSE] : level of printing (0 to 2)" */
196c5929fdfSBarry Smith     ierr = PetscOptionsGetInt(NULL,((PetscObject) A)->prefix, "-mat_pastix_verbose", &icntl, &flg);CHKERRQ(ierr);
197952ee8f4SMatthew G. Knepley     if ((flg && icntl >= 0) || PetscLogPrintInfo) verb =  icntl;
1985ec454cfSSatish Balay     PASTIX_CHECKMATRIX(MPI_COMM_WORLD,verb,((isSym != 0) ? API_SYM_YES : API_SYM_NO),API_YES,*n,&tmpcolptr,&tmprows,(PastixScalar**)&tmpvalues,NULL,1);
19943801a69SSatish Balay 
20070fe17b1SSatish Balay     ierr = PetscMemcpy(*colptr,tmpcolptr,(*n+1)*sizeof(PetscInt));CHKERRQ(ierr);
201785e854fSJed Brown     ierr = PetscMalloc1(((*colptr)[*n]-1),row);CHKERRQ(ierr);
20270fe17b1SSatish Balay     ierr = PetscMemcpy(*row,tmprows,((*colptr)[*n]-1)*sizeof(PetscInt));CHKERRQ(ierr);
203785e854fSJed Brown     ierr = PetscMalloc1(((*colptr)[*n]-1),values);CHKERRQ(ierr);
20470fe17b1SSatish Balay     ierr = PetscMemcpy(*values,tmpvalues,((*colptr)[*n]-1)*sizeof(PetscScalar));CHKERRQ(ierr);
20558c95f1bSBarry Smith     ierr = PetscFree3(tmpvalues,tmprows,tmpcolptr);CHKERRQ(ierr);
206be76a908SBarry Smith 
20743801a69SSatish Balay   }
2083bf14a46SMatthew Knepley   PetscFunctionReturn(0);
2093bf14a46SMatthew Knepley }
2103bf14a46SMatthew Knepley 
2113bf14a46SMatthew Knepley /*
2123bf14a46SMatthew Knepley   Call clean step of PaStiX if lu->CleanUpPastix == true.
2133bf14a46SMatthew Knepley   Free the CSC matrix.
2143bf14a46SMatthew Knepley  */
2153bf14a46SMatthew Knepley PetscErrorCode MatDestroy_Pastix(Mat A)
2163bf14a46SMatthew Knepley {
21758c95f1bSBarry Smith   Mat_Pastix     *lu=(Mat_Pastix*)A->data;
2183bf14a46SMatthew Knepley   PetscErrorCode ierr;
219745c78f7SBarry Smith 
2203bf14a46SMatthew Knepley   PetscFunctionBegin;
22158c95f1bSBarry Smith   if (lu->CleanUpPastix) {
2223bf14a46SMatthew Knepley     /* Terminate instance, deallocate memories */
2236bf464f9SBarry Smith     ierr = VecScatterDestroy(&lu->scat_rhs);CHKERRQ(ierr);
2246bf464f9SBarry Smith     ierr = VecDestroy(&lu->b_seq);CHKERRQ(ierr);
2256bf464f9SBarry Smith     ierr = VecScatterDestroy(&lu->scat_sol);CHKERRQ(ierr);
2263bf14a46SMatthew Knepley 
2273bf14a46SMatthew Knepley     lu->iparm[IPARM_START_TASK]=API_TASK_CLEAN;
2283bf14a46SMatthew Knepley     lu->iparm[IPARM_END_TASK]  =API_TASK_CLEAN;
2293bf14a46SMatthew Knepley 
230d41469e0Sxavier lacoste     PASTIX_CALL(&(lu->pastix_data),
2313bf14a46SMatthew Knepley                 lu->pastix_comm,
232d41469e0Sxavier lacoste                 lu->n,
233d41469e0Sxavier lacoste                 lu->colptr,
234d41469e0Sxavier lacoste                 lu->row,
2355ec454cfSSatish Balay                 (PastixScalar*)lu->val,
236d41469e0Sxavier lacoste                 lu->perm,
237d41469e0Sxavier lacoste                 lu->invp,
2385ec454cfSSatish Balay                 (PastixScalar*)lu->rhs,
239d41469e0Sxavier lacoste                 lu->rhsnbr,
240d41469e0Sxavier lacoste                 lu->iparm,
2413bf14a46SMatthew Knepley                 lu->dparm);
2423bf14a46SMatthew Knepley 
2433bf14a46SMatthew Knepley     ierr = PetscFree(lu->colptr);CHKERRQ(ierr);
2443bf14a46SMatthew Knepley     ierr = PetscFree(lu->row);CHKERRQ(ierr);
2453bf14a46SMatthew Knepley     ierr = PetscFree(lu->val);CHKERRQ(ierr);
2463bf14a46SMatthew Knepley     ierr = PetscFree(lu->perm);CHKERRQ(ierr);
2473bf14a46SMatthew Knepley     ierr = PetscFree(lu->invp);CHKERRQ(ierr);
2483bf14a46SMatthew Knepley     ierr = MPI_Comm_free(&(lu->pastix_comm));CHKERRQ(ierr);
2493bf14a46SMatthew Knepley   }
25058c95f1bSBarry Smith   ierr = PetscFree(A->data);CHKERRQ(ierr);
2513bf14a46SMatthew Knepley   PetscFunctionReturn(0);
2523bf14a46SMatthew Knepley }
2533bf14a46SMatthew Knepley 
2543bf14a46SMatthew Knepley /*
2553bf14a46SMatthew Knepley   Gather right-hand-side.
2563bf14a46SMatthew Knepley   Call for Solve step.
2573bf14a46SMatthew Knepley   Scatter solution.
2583bf14a46SMatthew Knepley  */
2593bf14a46SMatthew Knepley PetscErrorCode MatSolve_PaStiX(Mat A,Vec b,Vec x)
2603bf14a46SMatthew Knepley {
26158c95f1bSBarry Smith   Mat_Pastix     *lu=(Mat_Pastix*)A->data;
2623bf14a46SMatthew Knepley   PetscScalar    *array;
2633bf14a46SMatthew Knepley   Vec            x_seq;
2643bf14a46SMatthew Knepley   PetscErrorCode ierr;
2653bf14a46SMatthew Knepley 
2663bf14a46SMatthew Knepley   PetscFunctionBegin;
2673bf14a46SMatthew Knepley   lu->rhsnbr = 1;
2683bf14a46SMatthew Knepley   x_seq      = lu->b_seq;
2693bf14a46SMatthew Knepley   if (lu->commSize > 1) {
2703bf14a46SMatthew Knepley     /* PaStiX only supports centralized rhs. Scatter b into a seqential rhs vector */
2713bf14a46SMatthew Knepley     ierr = VecScatterBegin(lu->scat_rhs,b,x_seq,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2723bf14a46SMatthew Knepley     ierr = VecScatterEnd(lu->scat_rhs,b,x_seq,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
273b5e56a35SBarry Smith     ierr = VecGetArray(x_seq,&array);CHKERRQ(ierr);
27441c8de11SBarry Smith   } else {  /* size == 1 */
2753bf14a46SMatthew Knepley     ierr = VecCopy(b,x);CHKERRQ(ierr);
2763bf14a46SMatthew Knepley     ierr = VecGetArray(x,&array);CHKERRQ(ierr);
2773bf14a46SMatthew Knepley   }
2783bf14a46SMatthew Knepley   lu->rhs = array;
2793bf14a46SMatthew Knepley   if (lu->commSize == 1) {
2803bf14a46SMatthew Knepley     ierr = VecRestoreArray(x,&array);CHKERRQ(ierr);
2813bf14a46SMatthew Knepley   } else {
2823bf14a46SMatthew Knepley     ierr = VecRestoreArray(x_seq,&array);CHKERRQ(ierr);
2833bf14a46SMatthew Knepley   }
2843bf14a46SMatthew Knepley 
2853bf14a46SMatthew Knepley   /* solve phase */
2863bf14a46SMatthew Knepley   /*-------------*/
2873bf14a46SMatthew Knepley   lu->iparm[IPARM_START_TASK] = API_TASK_SOLVE;
2883bf14a46SMatthew Knepley   lu->iparm[IPARM_END_TASK]   = API_TASK_REFINE;
289745c78f7SBarry Smith   lu->iparm[IPARM_RHS_MAKING] = API_RHS_B;
2903bf14a46SMatthew Knepley 
291d41469e0Sxavier lacoste   PASTIX_CALL(&(lu->pastix_data),
292d41469e0Sxavier lacoste               lu->pastix_comm,
293d41469e0Sxavier lacoste               lu->n,
294d41469e0Sxavier lacoste               lu->colptr,
295d41469e0Sxavier lacoste               lu->row,
2965ec454cfSSatish Balay               (PastixScalar*)lu->val,
297d41469e0Sxavier lacoste               lu->perm,
298d41469e0Sxavier lacoste               lu->invp,
2995ec454cfSSatish Balay               (PastixScalar*)lu->rhs,
300d41469e0Sxavier lacoste               lu->rhsnbr,
301d41469e0Sxavier lacoste               lu->iparm,
302d41469e0Sxavier lacoste               lu->dparm);
3033bf14a46SMatthew Knepley 
30465e19b50SBarry Smith   if (lu->iparm[IPARM_ERROR_NUMBER] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by PaStiX in solve phase: lu->iparm[IPARM_ERROR_NUMBER] = %d\n",lu->iparm[IPARM_ERROR_NUMBER]);
3053bf14a46SMatthew Knepley 
3063bf14a46SMatthew Knepley   if (lu->commSize == 1) {
3073bf14a46SMatthew Knepley     ierr = VecRestoreArray(x,&(lu->rhs));CHKERRQ(ierr);
3083bf14a46SMatthew Knepley   } else {
3093bf14a46SMatthew Knepley     ierr = VecRestoreArray(x_seq,&(lu->rhs));CHKERRQ(ierr);
3103bf14a46SMatthew Knepley   }
3113bf14a46SMatthew Knepley 
3123bf14a46SMatthew Knepley   if (lu->commSize > 1) { /* convert PaStiX centralized solution to petsc mpi x */
3133bf14a46SMatthew Knepley     ierr = VecScatterBegin(lu->scat_sol,x_seq,x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3143bf14a46SMatthew Knepley     ierr = VecScatterEnd(lu->scat_sol,x_seq,x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3153bf14a46SMatthew Knepley   }
3163bf14a46SMatthew Knepley   PetscFunctionReturn(0);
3173bf14a46SMatthew Knepley }
3183bf14a46SMatthew Knepley 
3193bf14a46SMatthew Knepley /*
3203bf14a46SMatthew Knepley   Numeric factorisation using PaStiX solver.
3213bf14a46SMatthew Knepley 
3223bf14a46SMatthew Knepley  */
3233bf14a46SMatthew Knepley PetscErrorCode MatFactorNumeric_PaStiX(Mat F,Mat A,const MatFactorInfo *info)
3243bf14a46SMatthew Knepley {
32558c95f1bSBarry Smith   Mat_Pastix     *lu =(Mat_Pastix*)(F)->data;
32641c8de11SBarry Smith   Mat            *tseq;
3273bf14a46SMatthew Knepley   PetscErrorCode ierr = 0;
328b5e56a35SBarry Smith   PetscInt       icntl;
329b5e56a35SBarry Smith   PetscInt       M=A->rmap->N;
330ace3abfcSBarry Smith   PetscBool      valOnly,flg, isSym;
3313bf14a46SMatthew Knepley   IS             is_iden;
3323bf14a46SMatthew Knepley   Vec            b;
3333bf14a46SMatthew Knepley   IS             isrow;
33451a30905SBarry Smith   PetscBool      isSeqAIJ,isSeqSBAIJ,isMPIAIJ;
3353bf14a46SMatthew Knepley 
3363bf14a46SMatthew Knepley   PetscFunctionBegin;
337251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATSEQAIJ,&isSeqAIJ);CHKERRQ(ierr);
338251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&isMPIAIJ);CHKERRQ(ierr);
339251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATSEQSBAIJ,&isSeqSBAIJ);CHKERRQ(ierr);
3403bf14a46SMatthew Knepley   if (lu->matstruc == DIFFERENT_NONZERO_PATTERN) {
3413bf14a46SMatthew Knepley     (F)->ops->solve = MatSolve_PaStiX;
3423bf14a46SMatthew Knepley 
3433bf14a46SMatthew Knepley     /* Initialize a PASTIX instance */
344ce94432eSBarry Smith     ierr = MPI_Comm_dup(PetscObjectComm((PetscObject)A),&(lu->pastix_comm));CHKERRQ(ierr);
3453bf14a46SMatthew Knepley     ierr = MPI_Comm_rank(lu->pastix_comm, &lu->commRank);CHKERRQ(ierr);
3463bf14a46SMatthew Knepley     ierr = MPI_Comm_size(lu->pastix_comm, &lu->commSize);CHKERRQ(ierr);
3473bf14a46SMatthew Knepley 
3483bf14a46SMatthew Knepley     /* Set pastix options */
3493bf14a46SMatthew Knepley     lu->iparm[IPARM_MODIFY_PARAMETER] = API_NO;
3503bf14a46SMatthew Knepley     lu->iparm[IPARM_START_TASK]       = API_TASK_INIT;
3513bf14a46SMatthew Knepley     lu->iparm[IPARM_END_TASK]         = API_TASK_INIT;
3522205254eSKarl Rupp 
3533bf14a46SMatthew Knepley     lu->rhsnbr = 1;
3543bf14a46SMatthew Knepley 
3553bf14a46SMatthew Knepley     /* Call to set default pastix options */
356d41469e0Sxavier lacoste     PASTIX_CALL(&(lu->pastix_data),
357d41469e0Sxavier lacoste                 lu->pastix_comm,
358d41469e0Sxavier lacoste                 lu->n,
359d41469e0Sxavier lacoste                 lu->colptr,
360d41469e0Sxavier lacoste                 lu->row,
3615ec454cfSSatish Balay                 (PastixScalar*)lu->val,
362d41469e0Sxavier lacoste                 lu->perm,
363d41469e0Sxavier lacoste                 lu->invp,
3645ec454cfSSatish Balay                 (PastixScalar*)lu->rhs,
365d41469e0Sxavier lacoste                 lu->rhsnbr,
366d41469e0Sxavier lacoste                 lu->iparm,
367d41469e0Sxavier lacoste                 lu->dparm);
3683bf14a46SMatthew Knepley 
369ce94432eSBarry Smith     ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"PaStiX Options","Mat");CHKERRQ(ierr);
3703bf14a46SMatthew Knepley 
3713bf14a46SMatthew Knepley     icntl = -1;
3722205254eSKarl Rupp 
37341c8de11SBarry Smith     lu->iparm[IPARM_VERBOSE] = API_VERBOSE_NOT;
3742205254eSKarl Rupp 
37541c8de11SBarry Smith     ierr = PetscOptionsInt("-mat_pastix_verbose","iparm[IPARM_VERBOSE] : level of printing (0 to 2)","None",lu->iparm[IPARM_VERBOSE],&icntl,&flg);CHKERRQ(ierr);
376d41469e0Sxavier lacoste     if ((flg && icntl >= 0) || PetscLogPrintInfo) {
3773bf14a46SMatthew Knepley       lu->iparm[IPARM_VERBOSE] =  icntl;
3783bf14a46SMatthew Knepley     }
3793bf14a46SMatthew Knepley     icntl=-1;
380e4e47003SBarry Smith     ierr = PetscOptionsInt("-mat_pastix_threadnbr","iparm[IPARM_THREAD_NBR] : Number of thread by MPI node","None",lu->iparm[IPARM_THREAD_NBR],&icntl,&flg);CHKERRQ(ierr);
3813bf14a46SMatthew Knepley     if ((flg && icntl > 0)) {
3823bf14a46SMatthew Knepley       lu->iparm[IPARM_THREAD_NBR] = icntl;
3833bf14a46SMatthew Knepley     }
3843bf14a46SMatthew Knepley     PetscOptionsEnd();
3853bf14a46SMatthew Knepley     valOnly = PETSC_FALSE;
38641c8de11SBarry Smith   } else {
3875d6241c8SBarry Smith     if (isSeqAIJ || isMPIAIJ) {
3885d6241c8SBarry Smith       ierr    = PetscFree(lu->colptr);CHKERRQ(ierr);
3895d6241c8SBarry Smith       ierr    = PetscFree(lu->row);CHKERRQ(ierr);
3905d6241c8SBarry Smith       ierr    = PetscFree(lu->val);CHKERRQ(ierr);
3915d6241c8SBarry Smith       valOnly = PETSC_FALSE;
3925d6241c8SBarry Smith     } else valOnly = PETSC_TRUE;
3933bf14a46SMatthew Knepley   }
3943bf14a46SMatthew Knepley 
3953bf14a46SMatthew Knepley   lu->iparm[IPARM_MATRIX_VERIFICATION] = API_YES;
3963bf14a46SMatthew Knepley 
3973bf14a46SMatthew Knepley   /* convert mpi A to seq mat A */
3983bf14a46SMatthew Knepley   ierr = ISCreateStride(PETSC_COMM_SELF,M,0,1,&isrow);CHKERRQ(ierr);
399*7dae84e0SHong Zhang   ierr = MatCreateSubMatrices(A,1,&isrow,&isrow,MAT_INITIAL_MATRIX,&tseq);CHKERRQ(ierr);
4006bf464f9SBarry Smith   ierr = ISDestroy(&isrow);CHKERRQ(ierr);
4013bf14a46SMatthew Knepley 
40241c8de11SBarry Smith   ierr = MatConvertToCSC(*tseq,valOnly, &lu->n, &lu->colptr, &lu->row, &lu->val);CHKERRQ(ierr);
40341c8de11SBarry Smith   ierr = MatIsSymmetric(*tseq,0.0,&isSym);CHKERRQ(ierr);
40441c8de11SBarry Smith   ierr = MatDestroyMatrices(1,&tseq);CHKERRQ(ierr);
40541c8de11SBarry Smith 
4065d6241c8SBarry Smith   if (!lu->perm) {
407854ce69bSBarry Smith     ierr = PetscMalloc1(lu->n,&(lu->perm));CHKERRQ(ierr);
408854ce69bSBarry Smith     ierr = PetscMalloc1(lu->n,&(lu->invp));CHKERRQ(ierr);
4095d6241c8SBarry Smith   }
4103bf14a46SMatthew Knepley 
4113bf14a46SMatthew Knepley   if (isSym) {
412745c78f7SBarry Smith     /* On symmetric matrix, LLT */
4133bf14a46SMatthew Knepley     lu->iparm[IPARM_SYM]           = API_SYM_YES;
41441c8de11SBarry Smith     lu->iparm[IPARM_FACTORIZATION] = API_FACT_LDLT;
415f31ce8a6SBarry Smith   } else {
416745c78f7SBarry Smith     /* On unsymmetric matrix, LU */
4173bf14a46SMatthew Knepley     lu->iparm[IPARM_SYM]           = API_SYM_NO;
4183bf14a46SMatthew Knepley     lu->iparm[IPARM_FACTORIZATION] = API_FACT_LU;
4193bf14a46SMatthew Knepley   }
4203bf14a46SMatthew Knepley 
4213bf14a46SMatthew Knepley   /*----------------*/
4223bf14a46SMatthew Knepley   if (lu->matstruc == DIFFERENT_NONZERO_PATTERN) {
42358c95f1bSBarry Smith     if (!(isSeqAIJ || isSeqSBAIJ) && !lu->b_seq) {
4243bf14a46SMatthew Knepley       /* PaStiX only supports centralized rhs. Create scatter scat_rhs for repeated use in MatSolve() */
4253bf14a46SMatthew Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF,A->cmap->N,&lu->b_seq);CHKERRQ(ierr);
4263bf14a46SMatthew Knepley       ierr = ISCreateStride(PETSC_COMM_SELF,A->cmap->N,0,1,&is_iden);CHKERRQ(ierr);
4272a7a6963SBarry Smith       ierr = MatCreateVecs(A,NULL,&b);CHKERRQ(ierr);
4283bf14a46SMatthew Knepley       ierr = VecScatterCreate(b,is_iden,lu->b_seq,is_iden,&lu->scat_rhs);CHKERRQ(ierr);
4293bf14a46SMatthew Knepley       ierr = VecScatterCreate(lu->b_seq,is_iden,b,is_iden,&lu->scat_sol);CHKERRQ(ierr);
4306bf464f9SBarry Smith       ierr = ISDestroy(&is_iden);CHKERRQ(ierr);
4316bf464f9SBarry Smith       ierr = VecDestroy(&b);CHKERRQ(ierr);
4323bf14a46SMatthew Knepley     }
4333bf14a46SMatthew Knepley     lu->iparm[IPARM_START_TASK] = API_TASK_ORDERING;
4343bf14a46SMatthew Knepley     lu->iparm[IPARM_END_TASK]   = API_TASK_NUMFACT;
4353bf14a46SMatthew Knepley 
436d41469e0Sxavier lacoste     PASTIX_CALL(&(lu->pastix_data),
437d41469e0Sxavier lacoste                 lu->pastix_comm,
438d41469e0Sxavier lacoste                 lu->n,
439d41469e0Sxavier lacoste                 lu->colptr,
440d41469e0Sxavier lacoste                 lu->row,
4415ec454cfSSatish Balay                 (PastixScalar*)lu->val,
442d41469e0Sxavier lacoste                 lu->perm,
443d41469e0Sxavier lacoste                 lu->invp,
4445ec454cfSSatish Balay                 (PastixScalar*)lu->rhs,
445d41469e0Sxavier lacoste                 lu->rhsnbr,
446d41469e0Sxavier lacoste                 lu->iparm,
447d41469e0Sxavier lacoste                 lu->dparm);
44865e19b50SBarry Smith     if (lu->iparm[IPARM_ERROR_NUMBER] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by PaStiX in analysis phase: iparm(IPARM_ERROR_NUMBER)=%d\n",lu->iparm[IPARM_ERROR_NUMBER]);
44941c8de11SBarry Smith   } else {
4503bf14a46SMatthew Knepley     lu->iparm[IPARM_START_TASK] = API_TASK_NUMFACT;
4513bf14a46SMatthew Knepley     lu->iparm[IPARM_END_TASK]   = API_TASK_NUMFACT;
452d41469e0Sxavier lacoste     PASTIX_CALL(&(lu->pastix_data),
453d41469e0Sxavier lacoste                 lu->pastix_comm,
454d41469e0Sxavier lacoste                 lu->n,
455d41469e0Sxavier lacoste                 lu->colptr,
456d41469e0Sxavier lacoste                 lu->row,
4575ec454cfSSatish Balay                 (PastixScalar*)lu->val,
458d41469e0Sxavier lacoste                 lu->perm,
459d41469e0Sxavier lacoste                 lu->invp,
4605ec454cfSSatish Balay                 (PastixScalar*)lu->rhs,
461d41469e0Sxavier lacoste                 lu->rhsnbr,
462d41469e0Sxavier lacoste                 lu->iparm,
463d41469e0Sxavier lacoste                 lu->dparm);
46465e19b50SBarry Smith     if (lu->iparm[IPARM_ERROR_NUMBER] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by PaStiX in analysis phase: iparm(IPARM_ERROR_NUMBER)=%d\n",lu->iparm[IPARM_ERROR_NUMBER]);
4653bf14a46SMatthew Knepley   }
4663bf14a46SMatthew Knepley 
4673bf14a46SMatthew Knepley   (F)->assembled    = PETSC_TRUE;
4683bf14a46SMatthew Knepley   lu->matstruc      = SAME_NONZERO_PATTERN;
4693bf14a46SMatthew Knepley   lu->CleanUpPastix = PETSC_TRUE;
4703bf14a46SMatthew Knepley   PetscFunctionReturn(0);
4713bf14a46SMatthew Knepley }
4723bf14a46SMatthew Knepley 
4733bf14a46SMatthew Knepley /* Note the Petsc r and c permutations are ignored */
4743bf14a46SMatthew Knepley PetscErrorCode MatLUFactorSymbolic_AIJPASTIX(Mat F,Mat A,IS r,IS c,const MatFactorInfo *info)
4753bf14a46SMatthew Knepley {
47658c95f1bSBarry Smith   Mat_Pastix *lu = (Mat_Pastix*)F->data;
4773bf14a46SMatthew Knepley 
4783bf14a46SMatthew Knepley   PetscFunctionBegin;
4793bf14a46SMatthew Knepley   lu->iparm[IPARM_FACTORIZATION] = API_FACT_LU;
4803bf14a46SMatthew Knepley   lu->iparm[IPARM_SYM]           = API_SYM_YES;
4813bf14a46SMatthew Knepley   lu->matstruc                   = DIFFERENT_NONZERO_PATTERN;
4823bf14a46SMatthew Knepley   F->ops->lufactornumeric        = MatFactorNumeric_PaStiX;
4833bf14a46SMatthew Knepley   PetscFunctionReturn(0);
4843bf14a46SMatthew Knepley }
4853bf14a46SMatthew Knepley 
4863bf14a46SMatthew Knepley 
4873bf14a46SMatthew Knepley /* Note the Petsc r permutation is ignored */
4883bf14a46SMatthew Knepley PetscErrorCode MatCholeskyFactorSymbolic_SBAIJPASTIX(Mat F,Mat A,IS r,const MatFactorInfo *info)
4893bf14a46SMatthew Knepley {
49058c95f1bSBarry Smith   Mat_Pastix *lu = (Mat_Pastix*)(F)->data;
4913bf14a46SMatthew Knepley 
4923bf14a46SMatthew Knepley   PetscFunctionBegin;
4933bf14a46SMatthew Knepley   lu->iparm[IPARM_FACTORIZATION]  = API_FACT_LLT;
4943bf14a46SMatthew Knepley   lu->iparm[IPARM_SYM]            = API_SYM_NO;
4953bf14a46SMatthew Knepley   lu->matstruc                    = DIFFERENT_NONZERO_PATTERN;
4963bf14a46SMatthew Knepley   (F)->ops->choleskyfactornumeric = MatFactorNumeric_PaStiX;
4973bf14a46SMatthew Knepley   PetscFunctionReturn(0);
4983bf14a46SMatthew Knepley }
4993bf14a46SMatthew Knepley 
5003bf14a46SMatthew Knepley PetscErrorCode MatView_PaStiX(Mat A,PetscViewer viewer)
5013bf14a46SMatthew Knepley {
5023bf14a46SMatthew Knepley   PetscErrorCode    ierr;
503ace3abfcSBarry Smith   PetscBool         iascii;
5043bf14a46SMatthew Knepley   PetscViewerFormat format;
5053bf14a46SMatthew Knepley 
5063bf14a46SMatthew Knepley   PetscFunctionBegin;
507251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
5083bf14a46SMatthew Knepley   if (iascii) {
5093bf14a46SMatthew Knepley     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
5103bf14a46SMatthew Knepley     if (format == PETSC_VIEWER_ASCII_INFO) {
51158c95f1bSBarry Smith       Mat_Pastix *lu=(Mat_Pastix*)A->data;
512b5e56a35SBarry Smith 
513b5e56a35SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"PaStiX run parameters:\n");CHKERRQ(ierr);
514b5e56a35SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Matrix type :                      %s \n",((lu->iparm[IPARM_SYM] == API_SYM_YES) ? "Symmetric" : "Unsymmetric"));CHKERRQ(ierr);
515b5e56a35SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Level of printing (0,1,2):         %d \n",lu->iparm[IPARM_VERBOSE]);CHKERRQ(ierr);
516b5e56a35SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Number of refinements iterations : %d \n",lu->iparm[IPARM_NBITER]);CHKERRQ(ierr);
517b5e56a35SBarry Smith       ierr = PetscPrintf(PETSC_COMM_SELF,"  Error :                        %g \n",lu->dparm[DPARM_RELATIVE_ERROR]);CHKERRQ(ierr);
5183bf14a46SMatthew Knepley     }
5193bf14a46SMatthew Knepley   }
5203bf14a46SMatthew Knepley   PetscFunctionReturn(0);
5213bf14a46SMatthew Knepley }
5223bf14a46SMatthew Knepley 
5233bf14a46SMatthew Knepley 
5243bf14a46SMatthew Knepley /*MC
5252692d6eeSBarry Smith      MATSOLVERPASTIX  - A solver package providing direct solvers (LU) for distributed
5263bf14a46SMatthew Knepley   and sequential matrices via the external package PaStiX.
5273bf14a46SMatthew Knepley 
52890b3b921SSatish Balay   Use ./configure --download-pastix --download-ptscotch  to have PETSc installed with PasTiX
529c2b89b5dSBarry Smith 
530c2b89b5dSBarry Smith   Use -pc_type lu -pc_factor_mat_solver_package pastix to us this direct solver
5313bf14a46SMatthew Knepley 
5323bf14a46SMatthew Knepley   Options Database Keys:
533b5e56a35SBarry Smith + -mat_pastix_verbose   <0,1,2>   - print level
534b5e56a35SBarry Smith - -mat_pastix_threadnbr <integer> - Set the thread number by MPI task.
5353bf14a46SMatthew Knepley 
5363bf14a46SMatthew Knepley   Level: beginner
5373bf14a46SMatthew Knepley 
53841c8de11SBarry Smith .seealso: PCFactorSetMatSolverPackage(), MatSolverPackage
53941c8de11SBarry Smith 
5403bf14a46SMatthew Knepley M*/
5413bf14a46SMatthew Knepley 
5423bf14a46SMatthew Knepley 
5433bf14a46SMatthew Knepley PetscErrorCode MatGetInfo_PaStiX(Mat A,MatInfoType flag,MatInfo *info)
5443bf14a46SMatthew Knepley {
54558c95f1bSBarry Smith   Mat_Pastix *lu =(Mat_Pastix*)A->data;
5463bf14a46SMatthew Knepley 
5473bf14a46SMatthew Knepley   PetscFunctionBegin;
5483bf14a46SMatthew Knepley   info->block_size        = 1.0;
5493bf14a46SMatthew Knepley   info->nz_allocated      = lu->iparm[IPARM_NNZEROS];
5503bf14a46SMatthew Knepley   info->nz_used           = lu->iparm[IPARM_NNZEROS];
5513bf14a46SMatthew Knepley   info->nz_unneeded       = 0.0;
5523bf14a46SMatthew Knepley   info->assemblies        = 0.0;
5533bf14a46SMatthew Knepley   info->mallocs           = 0.0;
5543bf14a46SMatthew Knepley   info->memory            = 0.0;
5553bf14a46SMatthew Knepley   info->fill_ratio_given  = 0;
5563bf14a46SMatthew Knepley   info->fill_ratio_needed = 0;
5573bf14a46SMatthew Knepley   info->factor_mallocs    = 0;
5583bf14a46SMatthew Knepley   PetscFunctionReturn(0);
5593bf14a46SMatthew Knepley }
5603bf14a46SMatthew Knepley 
561cc2e6a90SBarry Smith static PetscErrorCode MatFactorGetSolverPackage_pastix(Mat A,const MatSolverPackage *type)
5623bf14a46SMatthew Knepley {
5633bf14a46SMatthew Knepley   PetscFunctionBegin;
5642692d6eeSBarry Smith   *type = MATSOLVERPASTIX;
5653bf14a46SMatthew Knepley   PetscFunctionReturn(0);
5663bf14a46SMatthew Knepley }
5673bf14a46SMatthew Knepley 
5683bf14a46SMatthew Knepley /*
5693bf14a46SMatthew Knepley     The seq and mpi versions of this function are the same
5703bf14a46SMatthew Knepley */
571cc2e6a90SBarry Smith static PetscErrorCode MatGetFactor_seqaij_pastix(Mat A,MatFactorType ftype,Mat *F)
5723bf14a46SMatthew Knepley {
5733bf14a46SMatthew Knepley   Mat            B;
5743bf14a46SMatthew Knepley   PetscErrorCode ierr;
5753bf14a46SMatthew Knepley   Mat_Pastix     *pastix;
5763bf14a46SMatthew Knepley 
5773bf14a46SMatthew Knepley   PetscFunctionBegin;
578e7e72b3dSBarry Smith   if (ftype != MAT_FACTOR_LU) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot use PETSc AIJ matrices with PaStiX Cholesky, use SBAIJ matrix");
5793bf14a46SMatthew Knepley   /* Create the factorization matrix */
580ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
5813bf14a46SMatthew Knepley   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
58258c95f1bSBarry Smith   ierr = PetscStrallocpy("pastix",&((PetscObject)B)->type_name);CHKERRQ(ierr);
58358c95f1bSBarry Smith   ierr = MatSetUp(B);CHKERRQ(ierr);
5843bf14a46SMatthew Knepley 
5853bf14a46SMatthew Knepley   B->ops->lufactorsymbolic = MatLUFactorSymbolic_AIJPASTIX;
5863bf14a46SMatthew Knepley   B->ops->view             = MatView_PaStiX;
5873bf14a46SMatthew Knepley   B->ops->getinfo          = MatGetInfo_PaStiX;
5882205254eSKarl Rupp 
589bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverPackage_C",MatFactorGetSolverPackage_pastix);CHKERRQ(ierr);
5902205254eSKarl Rupp 
591d5f3da31SBarry Smith   B->factortype = MAT_FACTOR_LU;
5923bf14a46SMatthew Knepley 
59300c67f3bSHong Zhang   /* set solvertype */
59400c67f3bSHong Zhang   ierr = PetscFree(B->solvertype);CHKERRQ(ierr);
59500c67f3bSHong Zhang   ierr = PetscStrallocpy(MATSOLVERPASTIX,&B->solvertype);CHKERRQ(ierr);
59600c67f3bSHong Zhang 
597b00a9115SJed Brown   ierr = PetscNewLog(B,&pastix);CHKERRQ(ierr);
5982205254eSKarl Rupp 
5993bf14a46SMatthew Knepley   pastix->CleanUpPastix = PETSC_FALSE;
6000298fd71SBarry Smith   pastix->scat_rhs      = NULL;
6010298fd71SBarry Smith   pastix->scat_sol      = NULL;
60258c95f1bSBarry Smith   B->ops->getinfo       = MatGetInfo_External;
6033bf14a46SMatthew Knepley   B->ops->destroy       = MatDestroy_Pastix;
60458c95f1bSBarry Smith   B->data               = (void*)pastix;
6053bf14a46SMatthew Knepley 
6063bf14a46SMatthew Knepley   *F = B;
6073bf14a46SMatthew Knepley   PetscFunctionReturn(0);
6083bf14a46SMatthew Knepley }
6093bf14a46SMatthew Knepley 
610cc2e6a90SBarry Smith static PetscErrorCode MatGetFactor_mpiaij_pastix(Mat A,MatFactorType ftype,Mat *F)
6113bf14a46SMatthew Knepley {
6123bf14a46SMatthew Knepley   Mat            B;
6133bf14a46SMatthew Knepley   PetscErrorCode ierr;
6143bf14a46SMatthew Knepley   Mat_Pastix     *pastix;
6153bf14a46SMatthew Knepley 
6163bf14a46SMatthew Knepley   PetscFunctionBegin;
617e32f2f54SBarry Smith   if (ftype != MAT_FACTOR_LU) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot use PETSc AIJ matrices with PaStiX Cholesky, use SBAIJ matrix");
6183bf14a46SMatthew Knepley   /* Create the factorization matrix */
619ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
6203bf14a46SMatthew Knepley   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
62158c95f1bSBarry Smith   ierr = PetscStrallocpy("pastix",&((PetscObject)B)->type_name);CHKERRQ(ierr);
62258c95f1bSBarry Smith   ierr = MatSetUp(B);CHKERRQ(ierr);
6233bf14a46SMatthew Knepley 
6243bf14a46SMatthew Knepley   B->ops->lufactorsymbolic = MatLUFactorSymbolic_AIJPASTIX;
6253bf14a46SMatthew Knepley   B->ops->view             = MatView_PaStiX;
62658c95f1bSBarry Smith   B->ops->getinfo          = MatGetInfo_PaStiX;
627bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverPackage_C",MatFactorGetSolverPackage_pastix);CHKERRQ(ierr);
6282205254eSKarl Rupp 
629d5f3da31SBarry Smith   B->factortype = MAT_FACTOR_LU;
6303bf14a46SMatthew Knepley 
63100c67f3bSHong Zhang   /* set solvertype */
63200c67f3bSHong Zhang   ierr = PetscFree(B->solvertype);CHKERRQ(ierr);
63300c67f3bSHong Zhang   ierr = PetscStrallocpy(MATSOLVERPASTIX,&B->solvertype);CHKERRQ(ierr);
63400c67f3bSHong Zhang 
635b00a9115SJed Brown   ierr = PetscNewLog(B,&pastix);CHKERRQ(ierr);
6362205254eSKarl Rupp 
6373bf14a46SMatthew Knepley   pastix->CleanUpPastix = PETSC_FALSE;
6380298fd71SBarry Smith   pastix->scat_rhs      = NULL;
6390298fd71SBarry Smith   pastix->scat_sol      = NULL;
64058c95f1bSBarry Smith   B->ops->getinfo       = MatGetInfo_External;
6413bf14a46SMatthew Knepley   B->ops->destroy       = MatDestroy_Pastix;
64258c95f1bSBarry Smith   B->data               = (void*)pastix;
6433bf14a46SMatthew Knepley 
6443bf14a46SMatthew Knepley   *F = B;
6453bf14a46SMatthew Knepley   PetscFunctionReturn(0);
6463bf14a46SMatthew Knepley }
6473bf14a46SMatthew Knepley 
648cc2e6a90SBarry Smith static PetscErrorCode MatGetFactor_seqsbaij_pastix(Mat A,MatFactorType ftype,Mat *F)
6493bf14a46SMatthew Knepley {
6503bf14a46SMatthew Knepley   Mat            B;
6513bf14a46SMatthew Knepley   PetscErrorCode ierr;
6523bf14a46SMatthew Knepley   Mat_Pastix     *pastix;
6533bf14a46SMatthew Knepley 
6543bf14a46SMatthew Knepley   PetscFunctionBegin;
655e7e72b3dSBarry Smith   if (ftype != MAT_FACTOR_CHOLESKY) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot use PETSc SBAIJ matrices with PaStiX LU, use AIJ matrix");
6563bf14a46SMatthew Knepley   /* Create the factorization matrix */
657ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
6583bf14a46SMatthew Knepley   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
65958c95f1bSBarry Smith   ierr = PetscStrallocpy("pastix",&((PetscObject)B)->type_name);CHKERRQ(ierr);
66058c95f1bSBarry Smith   ierr = MatSetUp(B);CHKERRQ(ierr);
6613bf14a46SMatthew Knepley 
6623bf14a46SMatthew Knepley   B->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SBAIJPASTIX;
6633bf14a46SMatthew Knepley   B->ops->view                   = MatView_PaStiX;
66458c95f1bSBarry Smith   B->ops->getinfo                = MatGetInfo_PaStiX;
665bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverPackage_C",MatFactorGetSolverPackage_pastix);CHKERRQ(ierr);
6662205254eSKarl Rupp 
667d5f3da31SBarry Smith   B->factortype = MAT_FACTOR_CHOLESKY;
6683bf14a46SMatthew Knepley 
66900c67f3bSHong Zhang   /* set solvertype */
67000c67f3bSHong Zhang   ierr = PetscFree(B->solvertype);CHKERRQ(ierr);
67100c67f3bSHong Zhang   ierr = PetscStrallocpy(MATSOLVERPASTIX,&B->solvertype);CHKERRQ(ierr);
67200c67f3bSHong Zhang 
673b00a9115SJed Brown   ierr = PetscNewLog(B,&pastix);CHKERRQ(ierr);
6742205254eSKarl Rupp 
6753bf14a46SMatthew Knepley   pastix->CleanUpPastix = PETSC_FALSE;
6760298fd71SBarry Smith   pastix->scat_rhs      = NULL;
6770298fd71SBarry Smith   pastix->scat_sol      = NULL;
67858c95f1bSBarry Smith   B->ops->getinfo       = MatGetInfo_External;
6793bf14a46SMatthew Knepley   B->ops->destroy       = MatDestroy_Pastix;
68058c95f1bSBarry Smith   B->data               = (void*)pastix;
6813bf14a46SMatthew Knepley 
6823bf14a46SMatthew Knepley   *F = B;
6833bf14a46SMatthew Knepley   PetscFunctionReturn(0);
6843bf14a46SMatthew Knepley }
6853bf14a46SMatthew Knepley 
686cc2e6a90SBarry Smith static PetscErrorCode MatGetFactor_mpisbaij_pastix(Mat A,MatFactorType ftype,Mat *F)
6873bf14a46SMatthew Knepley {
6883bf14a46SMatthew Knepley   Mat            B;
6893bf14a46SMatthew Knepley   PetscErrorCode ierr;
6903bf14a46SMatthew Knepley   Mat_Pastix     *pastix;
6913bf14a46SMatthew Knepley 
6923bf14a46SMatthew Knepley   PetscFunctionBegin;
693e32f2f54SBarry Smith   if (ftype != MAT_FACTOR_CHOLESKY) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot use PETSc SBAIJ matrices with PaStiX LU, use AIJ matrix");
69441c8de11SBarry Smith 
6953bf14a46SMatthew Knepley   /* Create the factorization matrix */
696ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
6973bf14a46SMatthew Knepley   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
69858c95f1bSBarry Smith   ierr = PetscStrallocpy("pastix",&((PetscObject)B)->type_name);CHKERRQ(ierr);
69958c95f1bSBarry Smith   ierr = MatSetUp(B);CHKERRQ(ierr);
7003bf14a46SMatthew Knepley 
7013bf14a46SMatthew Knepley   B->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SBAIJPASTIX;
7023bf14a46SMatthew Knepley   B->ops->view                   = MatView_PaStiX;
70358c95f1bSBarry Smith   B->ops->getinfo                = MatGetInfo_PaStiX;
70458c95f1bSBarry Smith   B->ops->destroy                = MatDestroy_Pastix;
705bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverPackage_C",MatFactorGetSolverPackage_pastix);CHKERRQ(ierr);
7062205254eSKarl Rupp 
707d5f3da31SBarry Smith   B->factortype = MAT_FACTOR_CHOLESKY;
7083bf14a46SMatthew Knepley 
70900c67f3bSHong Zhang   /* set solvertype */
71000c67f3bSHong Zhang   ierr = PetscFree(B->solvertype);CHKERRQ(ierr);
71100c67f3bSHong Zhang   ierr = PetscStrallocpy(MATSOLVERPASTIX,&B->solvertype);CHKERRQ(ierr);
71200c67f3bSHong Zhang 
713b00a9115SJed Brown   ierr = PetscNewLog(B,&pastix);CHKERRQ(ierr);
7142205254eSKarl Rupp 
7153bf14a46SMatthew Knepley   pastix->CleanUpPastix = PETSC_FALSE;
7160298fd71SBarry Smith   pastix->scat_rhs      = NULL;
7170298fd71SBarry Smith   pastix->scat_sol      = NULL;
71858c95f1bSBarry Smith   B->data               = (void*)pastix;
7193bf14a46SMatthew Knepley 
7203bf14a46SMatthew Knepley   *F = B;
7213bf14a46SMatthew Knepley   PetscFunctionReturn(0);
7223bf14a46SMatthew Knepley }
723f7a08781SBarry Smith 
72429b38603SBarry Smith PETSC_EXTERN PetscErrorCode MatSolverPackageRegister_Pastix(void)
72542c9c57cSBarry Smith {
72642c9c57cSBarry Smith   PetscErrorCode ierr;
72742c9c57cSBarry Smith 
72842c9c57cSBarry Smith   PetscFunctionBegin;
72942c9c57cSBarry Smith   ierr = MatSolverPackageRegister(MATSOLVERPASTIX,MATMPIAIJ,        MAT_FACTOR_LU,MatGetFactor_mpiaij_pastix);CHKERRQ(ierr);
73042c9c57cSBarry Smith   ierr = MatSolverPackageRegister(MATSOLVERPASTIX,MATSEQAIJ,        MAT_FACTOR_LU,MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
73142c9c57cSBarry Smith   ierr = MatSolverPackageRegister(MATSOLVERPASTIX,MATMPISBAIJ,      MAT_FACTOR_CHOLESKY,MatGetFactor_mpisbaij_pastix);CHKERRQ(ierr);
73242c9c57cSBarry Smith   ierr = MatSolverPackageRegister(MATSOLVERPASTIX,MATSEQSBAIJ,      MAT_FACTOR_CHOLESKY,MatGetFactor_seqsbaij_pastix);CHKERRQ(ierr);
73342c9c57cSBarry Smith   PetscFunctionReturn(0);
73442c9c57cSBarry Smith }
735