xref: /petsc/src/mat/impls/aij/mpi/pastix/pastix.c (revision 95452b02e12c0ee11232c7ff2b24b568a8e07e43)
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 #else
215ec454cfSSatish Balay #define PASTIX_CALL z_pastix
225ec454cfSSatish Balay #endif
23d41469e0Sxavier lacoste 
24d41469e0Sxavier lacoste #else /* PETSC_USE_COMPLEX */
25d41469e0Sxavier lacoste 
26519f805aSKarl Rupp #if defined(PETSC_USE_REAL_SINGLE)
275ec454cfSSatish Balay #define PASTIX_CALL s_pastix
285ec454cfSSatish Balay #else
295ec454cfSSatish Balay #define PASTIX_CALL d_pastix
305ec454cfSSatish Balay #endif
31d41469e0Sxavier lacoste 
32d41469e0Sxavier lacoste #endif /* PETSC_USE_COMPLEX */
33d41469e0Sxavier lacoste 
34dbbbd53dSSatish Balay typedef PetscScalar PastixScalar;
35dbbbd53dSSatish Balay 
363bf14a46SMatthew Knepley typedef struct Mat_Pastix_ {
373bf14a46SMatthew Knepley   pastix_data_t *pastix_data;    /* Pastix data storage structure                        */
383bf14a46SMatthew Knepley   MatStructure  matstruc;
393bf14a46SMatthew Knepley   PetscInt      n;               /* Number of columns in the matrix                      */
403bf14a46SMatthew Knepley   PetscInt      *colptr;         /* Index of first element of each column in row and val */
413bf14a46SMatthew Knepley   PetscInt      *row;            /* Row of each element of the matrix                    */
423bf14a46SMatthew Knepley   PetscScalar   *val;            /* Value of each element of the matrix                  */
433bf14a46SMatthew Knepley   PetscInt      *perm;           /* Permutation tabular                                  */
443bf14a46SMatthew Knepley   PetscInt      *invp;           /* Reverse permutation tabular                          */
453bf14a46SMatthew Knepley   PetscScalar   *rhs;            /* Rhight-hand-side member                              */
463bf14a46SMatthew Knepley   PetscInt      rhsnbr;          /* Rhight-hand-side number (must be 1)                  */
47baed9decSBarry Smith   PetscInt      iparm[IPARM_SIZE];       /* Integer parameters                                   */
48baed9decSBarry Smith   double        dparm[DPARM_SIZE];       /* Floating point parameters                            */
493bf14a46SMatthew Knepley   MPI_Comm      pastix_comm;     /* PaStiX MPI communicator                              */
503bf14a46SMatthew Knepley   PetscMPIInt   commRank;        /* MPI rank                                             */
513bf14a46SMatthew Knepley   PetscMPIInt   commSize;        /* MPI communicator size                                */
52ace3abfcSBarry Smith   PetscBool     CleanUpPastix;   /* Boolean indicating if we call PaStiX clean step      */
533bf14a46SMatthew Knepley   VecScatter    scat_rhs;
543bf14a46SMatthew Knepley   VecScatter    scat_sol;
55f31ce8a6SBarry Smith   Vec           b_seq;
563bf14a46SMatthew Knepley } Mat_Pastix;
573bf14a46SMatthew Knepley 
5809573ac7SBarry Smith extern PetscErrorCode MatDuplicate_Pastix(Mat,MatDuplicateOption,Mat*);
593bf14a46SMatthew Knepley 
603bf14a46SMatthew Knepley /*
613bf14a46SMatthew Knepley    convert Petsc seqaij matrix to CSC: colptr[n], row[nz], val[nz]
623bf14a46SMatthew Knepley 
633bf14a46SMatthew Knepley   input:
643bf14a46SMatthew Knepley     A       - matrix in seqaij or mpisbaij (bs=1) format
653bf14a46SMatthew Knepley     valOnly - FALSE: spaces are allocated and values are set for the CSC
663bf14a46SMatthew Knepley               TRUE:  Only fill values
673bf14a46SMatthew Knepley   output:
683bf14a46SMatthew Knepley     n       - Size of the matrix
693bf14a46SMatthew Knepley     colptr  - Index of first element of each column in row and val
703bf14a46SMatthew Knepley     row     - Row of each element of the matrix
713bf14a46SMatthew Knepley     values  - Value of each element of the matrix
723bf14a46SMatthew Knepley  */
73ace3abfcSBarry Smith PetscErrorCode MatConvertToCSC(Mat A,PetscBool valOnly,PetscInt *n,PetscInt **colptr,PetscInt **row,PetscScalar **values)
7441c8de11SBarry Smith {
753bf14a46SMatthew Knepley   Mat_SeqAIJ     *aa      = (Mat_SeqAIJ*)A->data;
763bf14a46SMatthew Knepley   PetscInt       *rowptr  = aa->i;
773bf14a46SMatthew Knepley   PetscInt       *col     = aa->j;
783bf14a46SMatthew Knepley   PetscScalar    *rvalues = aa->a;
793bf14a46SMatthew Knepley   PetscInt       m        = A->rmap->N;
80745c78f7SBarry Smith   PetscInt       nnz;
813bf14a46SMatthew Knepley   PetscInt       i,j, k;
823bf14a46SMatthew Knepley   PetscInt       base = 1;
833bf14a46SMatthew Knepley   PetscInt       idx;
843bf14a46SMatthew Knepley   PetscErrorCode ierr;
853bf14a46SMatthew Knepley   PetscInt       colidx;
863bf14a46SMatthew Knepley   PetscInt       *colcount;
87ace3abfcSBarry Smith   PetscBool      isSBAIJ;
88ace3abfcSBarry Smith   PetscBool      isSeqSBAIJ;
89ace3abfcSBarry Smith   PetscBool      isMpiSBAIJ;
90ace3abfcSBarry Smith   PetscBool      isSym;
913bf14a46SMatthew Knepley 
923bf14a46SMatthew Knepley   PetscFunctionBegin;
9341c8de11SBarry Smith   ierr = MatIsSymmetric(A,0.0,&isSym);CHKERRQ(ierr);
94251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATSBAIJ,&isSBAIJ);CHKERRQ(ierr);
95251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATSEQSBAIJ,&isSeqSBAIJ);CHKERRQ(ierr);
96251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATMPISBAIJ,&isMpiSBAIJ);CHKERRQ(ierr);
973bf14a46SMatthew Knepley 
98745c78f7SBarry Smith   *n = A->cmap->N;
99745c78f7SBarry Smith 
100745c78f7SBarry Smith   /* PaStiX only needs triangular matrix if matrix is symmetric
101745c78f7SBarry Smith    */
1022205254eSKarl Rupp   if (isSym && !(isSBAIJ || isSeqSBAIJ || isMpiSBAIJ)) nnz = (aa->nz - *n)/2 + *n;
1032205254eSKarl Rupp   else nnz = aa->nz;
1043bf14a46SMatthew Knepley 
1053bf14a46SMatthew Knepley   if (!valOnly) {
106854ce69bSBarry Smith     ierr = PetscMalloc1((*n)+1,colptr);CHKERRQ(ierr);
107854ce69bSBarry Smith     ierr = PetscMalloc1(nnz,row);CHKERRQ(ierr);
108785e854fSJed Brown     ierr = PetscMalloc1(nnz,values);CHKERRQ(ierr);
1093bf14a46SMatthew Knepley 
11041c8de11SBarry Smith     if (isSBAIJ || isSeqSBAIJ || isMpiSBAIJ) {
11141c8de11SBarry Smith       ierr = PetscMemcpy (*colptr, rowptr, ((*n)+1)*sizeof(PetscInt));CHKERRQ(ierr);
1122205254eSKarl Rupp       for (i = 0; i < *n+1; i++) (*colptr)[i] += base;
11341c8de11SBarry Smith       ierr = PetscMemcpy (*row, col, (nnz)*sizeof(PetscInt));CHKERRQ(ierr);
1142205254eSKarl Rupp       for (i = 0; i < nnz; i++) (*row)[i] += base;
11541c8de11SBarry Smith       ierr = PetscMemcpy (*values, rvalues, (nnz)*sizeof(PetscScalar));CHKERRQ(ierr);
11641c8de11SBarry Smith     } else {
117854ce69bSBarry Smith       ierr = PetscMalloc1(*n,&colcount);CHKERRQ(ierr);
11841c8de11SBarry Smith 
119f31ce8a6SBarry Smith       for (i = 0; i < m; i++) colcount[i] = 0;
1203bf14a46SMatthew Knepley       /* Fill-in colptr */
121f31ce8a6SBarry Smith       for (i = 0; i < m; i++) {
122f31ce8a6SBarry Smith         for (j = rowptr[i]; j < rowptr[i+1]; j++) {
123f31ce8a6SBarry Smith           if (!isSym || col[j] <= i)  colcount[col[j]]++;
124f31ce8a6SBarry Smith         }
125f31ce8a6SBarry Smith       }
126745c78f7SBarry Smith 
1273bf14a46SMatthew Knepley       (*colptr)[0] = base;
1283bf14a46SMatthew Knepley       for (j = 0; j < *n; j++) {
1293bf14a46SMatthew Knepley         (*colptr)[j+1] = (*colptr)[j] + colcount[j];
130745c78f7SBarry Smith         /* in next loop we fill starting from (*colptr)[colidx] - base */
1313bf14a46SMatthew Knepley         colcount[j] = -base;
1323bf14a46SMatthew Knepley       }
1333bf14a46SMatthew Knepley 
1343bf14a46SMatthew Knepley       /* Fill-in rows and values */
1353bf14a46SMatthew Knepley       for (i = 0; i < m; i++) {
1363bf14a46SMatthew Knepley         for (j = rowptr[i]; j < rowptr[i+1]; j++) {
13741c8de11SBarry Smith           if (!isSym || col[j] <= i) {
1383bf14a46SMatthew Knepley             colidx         = col[j];
1393bf14a46SMatthew Knepley             idx            = (*colptr)[colidx] + colcount[colidx];
1403bf14a46SMatthew Knepley             (*row)[idx]    = i + base;
1413bf14a46SMatthew Knepley             (*values)[idx] = rvalues[j];
1423bf14a46SMatthew Knepley             colcount[colidx]++;
1433bf14a46SMatthew Knepley           }
1443bf14a46SMatthew Knepley         }
1453bf14a46SMatthew Knepley       }
14641c8de11SBarry Smith       ierr = PetscFree(colcount);CHKERRQ(ierr);
147745c78f7SBarry Smith     }
14841c8de11SBarry Smith   } else {
149745c78f7SBarry Smith     /* Fill-in only values */
1503bf14a46SMatthew Knepley     for (i = 0; i < m; i++) {
1513bf14a46SMatthew Knepley       for (j = rowptr[i]; j < rowptr[i+1]; j++) {
1523bf14a46SMatthew Knepley         colidx = col[j];
1532205254eSKarl Rupp         if ((isSBAIJ || isSeqSBAIJ || isMpiSBAIJ) ||!isSym || col[j] <= i) {
154745c78f7SBarry Smith           /* look for the value to fill */
155f31ce8a6SBarry Smith           for (k = (*colptr)[colidx] - base; k < (*colptr)[colidx + 1] - base; k++) {
156eb1f6c34SBarry Smith             if (((*row)[k]-base) == i) {
1573bf14a46SMatthew Knepley               (*values)[k] = rvalues[j];
1583bf14a46SMatthew Knepley               break;
1593bf14a46SMatthew Knepley             }
1603bf14a46SMatthew Knepley           }
161f31ce8a6SBarry Smith           /* data structure of sparse matrix has changed */
162e32f2f54SBarry Smith           if (k == (*colptr)[colidx + 1] - base) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"overflow on k %D",k);
1633bf14a46SMatthew Knepley         }
1643bf14a46SMatthew Knepley       }
1653bf14a46SMatthew Knepley     }
166745c78f7SBarry Smith   }
1673bf14a46SMatthew Knepley   PetscFunctionReturn(0);
1683bf14a46SMatthew Knepley }
1693bf14a46SMatthew Knepley 
1703bf14a46SMatthew Knepley /*
1713bf14a46SMatthew Knepley   Call clean step of PaStiX if lu->CleanUpPastix == true.
1723bf14a46SMatthew Knepley   Free the CSC matrix.
1733bf14a46SMatthew Knepley  */
1743bf14a46SMatthew Knepley PetscErrorCode MatDestroy_Pastix(Mat A)
1753bf14a46SMatthew Knepley {
17658c95f1bSBarry Smith   Mat_Pastix     *lu=(Mat_Pastix*)A->data;
1773bf14a46SMatthew Knepley   PetscErrorCode ierr;
178745c78f7SBarry Smith 
1793bf14a46SMatthew Knepley   PetscFunctionBegin;
18058c95f1bSBarry Smith   if (lu->CleanUpPastix) {
1813bf14a46SMatthew Knepley     /* Terminate instance, deallocate memories */
1826bf464f9SBarry Smith     ierr = VecScatterDestroy(&lu->scat_rhs);CHKERRQ(ierr);
1836bf464f9SBarry Smith     ierr = VecDestroy(&lu->b_seq);CHKERRQ(ierr);
1846bf464f9SBarry Smith     ierr = VecScatterDestroy(&lu->scat_sol);CHKERRQ(ierr);
1853bf14a46SMatthew Knepley 
1863bf14a46SMatthew Knepley     lu->iparm[IPARM_START_TASK]=API_TASK_CLEAN;
1873bf14a46SMatthew Knepley     lu->iparm[IPARM_END_TASK]  =API_TASK_CLEAN;
1883bf14a46SMatthew Knepley 
189d41469e0Sxavier lacoste     PASTIX_CALL(&(lu->pastix_data),
1903bf14a46SMatthew Knepley                 lu->pastix_comm,
191d41469e0Sxavier lacoste                 lu->n,
192d41469e0Sxavier lacoste                 lu->colptr,
193d41469e0Sxavier lacoste                 lu->row,
1945ec454cfSSatish Balay                 (PastixScalar*)lu->val,
195d41469e0Sxavier lacoste                 lu->perm,
196d41469e0Sxavier lacoste                 lu->invp,
1975ec454cfSSatish Balay                 (PastixScalar*)lu->rhs,
198d41469e0Sxavier lacoste                 lu->rhsnbr,
199d41469e0Sxavier lacoste                 lu->iparm,
2003bf14a46SMatthew Knepley                 lu->dparm);
201baed9decSBarry Smith     if (lu->iparm[IPARM_ERROR_NUMBER] != 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by PaStiX in destroy: iparm(IPARM_ERROR_NUMBER)=%d\n",lu->iparm[IPARM_ERROR_NUMBER]);
2023bf14a46SMatthew Knepley     ierr = PetscFree(lu->colptr);CHKERRQ(ierr);
2033bf14a46SMatthew Knepley     ierr = PetscFree(lu->row);CHKERRQ(ierr);
2043bf14a46SMatthew Knepley     ierr = PetscFree(lu->val);CHKERRQ(ierr);
2053bf14a46SMatthew Knepley     ierr = PetscFree(lu->perm);CHKERRQ(ierr);
2063bf14a46SMatthew Knepley     ierr = PetscFree(lu->invp);CHKERRQ(ierr);
2073bf14a46SMatthew Knepley     ierr = MPI_Comm_free(&(lu->pastix_comm));CHKERRQ(ierr);
2083bf14a46SMatthew Knepley   }
20958c95f1bSBarry Smith   ierr = PetscFree(A->data);CHKERRQ(ierr);
2103bf14a46SMatthew Knepley   PetscFunctionReturn(0);
2113bf14a46SMatthew Knepley }
2123bf14a46SMatthew Knepley 
2133bf14a46SMatthew Knepley /*
2143bf14a46SMatthew Knepley   Gather right-hand-side.
2153bf14a46SMatthew Knepley   Call for Solve step.
2163bf14a46SMatthew Knepley   Scatter solution.
2173bf14a46SMatthew Knepley  */
2183bf14a46SMatthew Knepley PetscErrorCode MatSolve_PaStiX(Mat A,Vec b,Vec x)
2193bf14a46SMatthew Knepley {
22058c95f1bSBarry Smith   Mat_Pastix     *lu=(Mat_Pastix*)A->data;
2213bf14a46SMatthew Knepley   PetscScalar    *array;
2223bf14a46SMatthew Knepley   Vec            x_seq;
2233bf14a46SMatthew Knepley   PetscErrorCode ierr;
2243bf14a46SMatthew Knepley 
2253bf14a46SMatthew Knepley   PetscFunctionBegin;
2263bf14a46SMatthew Knepley   lu->rhsnbr = 1;
2273bf14a46SMatthew Knepley   x_seq      = lu->b_seq;
2283bf14a46SMatthew Knepley   if (lu->commSize > 1) {
2293bf14a46SMatthew Knepley     /* PaStiX only supports centralized rhs. Scatter b into a seqential rhs vector */
2303bf14a46SMatthew Knepley     ierr = VecScatterBegin(lu->scat_rhs,b,x_seq,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2313bf14a46SMatthew Knepley     ierr = VecScatterEnd(lu->scat_rhs,b,x_seq,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
232b5e56a35SBarry Smith     ierr = VecGetArray(x_seq,&array);CHKERRQ(ierr);
23341c8de11SBarry Smith   } else {  /* size == 1 */
2343bf14a46SMatthew Knepley     ierr = VecCopy(b,x);CHKERRQ(ierr);
2353bf14a46SMatthew Knepley     ierr = VecGetArray(x,&array);CHKERRQ(ierr);
2363bf14a46SMatthew Knepley   }
2373bf14a46SMatthew Knepley   lu->rhs = array;
2383bf14a46SMatthew Knepley   if (lu->commSize == 1) {
2393bf14a46SMatthew Knepley     ierr = VecRestoreArray(x,&array);CHKERRQ(ierr);
2403bf14a46SMatthew Knepley   } else {
2413bf14a46SMatthew Knepley     ierr = VecRestoreArray(x_seq,&array);CHKERRQ(ierr);
2423bf14a46SMatthew Knepley   }
2433bf14a46SMatthew Knepley 
2443bf14a46SMatthew Knepley   /* solve phase */
2453bf14a46SMatthew Knepley   /*-------------*/
2463bf14a46SMatthew Knepley   lu->iparm[IPARM_START_TASK] = API_TASK_SOLVE;
2473bf14a46SMatthew Knepley   lu->iparm[IPARM_END_TASK]   = API_TASK_REFINE;
248745c78f7SBarry Smith   lu->iparm[IPARM_RHS_MAKING] = API_RHS_B;
2493bf14a46SMatthew Knepley 
250d41469e0Sxavier lacoste   PASTIX_CALL(&(lu->pastix_data),
251d41469e0Sxavier lacoste               lu->pastix_comm,
252d41469e0Sxavier lacoste               lu->n,
253d41469e0Sxavier lacoste               lu->colptr,
254d41469e0Sxavier lacoste               lu->row,
2555ec454cfSSatish Balay               (PastixScalar*)lu->val,
256d41469e0Sxavier lacoste               lu->perm,
257d41469e0Sxavier lacoste               lu->invp,
2585ec454cfSSatish Balay               (PastixScalar*)lu->rhs,
259d41469e0Sxavier lacoste               lu->rhsnbr,
260d41469e0Sxavier lacoste               lu->iparm,
261d41469e0Sxavier lacoste               lu->dparm);
262baed9decSBarry 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]);
2633bf14a46SMatthew Knepley 
2643bf14a46SMatthew Knepley   if (lu->commSize == 1) {
2653bf14a46SMatthew Knepley     ierr = VecRestoreArray(x,&(lu->rhs));CHKERRQ(ierr);
2663bf14a46SMatthew Knepley   } else {
2673bf14a46SMatthew Knepley     ierr = VecRestoreArray(x_seq,&(lu->rhs));CHKERRQ(ierr);
2683bf14a46SMatthew Knepley   }
2693bf14a46SMatthew Knepley 
2703bf14a46SMatthew Knepley   if (lu->commSize > 1) { /* convert PaStiX centralized solution to petsc mpi x */
2713bf14a46SMatthew Knepley     ierr = VecScatterBegin(lu->scat_sol,x_seq,x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2723bf14a46SMatthew Knepley     ierr = VecScatterEnd(lu->scat_sol,x_seq,x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2733bf14a46SMatthew Knepley   }
2743bf14a46SMatthew Knepley   PetscFunctionReturn(0);
2753bf14a46SMatthew Knepley }
2763bf14a46SMatthew Knepley 
2773bf14a46SMatthew Knepley /*
2783bf14a46SMatthew Knepley   Numeric factorisation using PaStiX solver.
2793bf14a46SMatthew Knepley 
2803bf14a46SMatthew Knepley  */
2813bf14a46SMatthew Knepley PetscErrorCode MatFactorNumeric_PaStiX(Mat F,Mat A,const MatFactorInfo *info)
2823bf14a46SMatthew Knepley {
28358c95f1bSBarry Smith   Mat_Pastix     *lu =(Mat_Pastix*)(F)->data;
28441c8de11SBarry Smith   Mat            *tseq;
2853bf14a46SMatthew Knepley   PetscErrorCode ierr = 0;
286b5e56a35SBarry Smith   PetscInt       icntl;
287b5e56a35SBarry Smith   PetscInt       M=A->rmap->N;
288ace3abfcSBarry Smith   PetscBool      valOnly,flg, isSym;
2893bf14a46SMatthew Knepley   IS             is_iden;
2903bf14a46SMatthew Knepley   Vec            b;
2913bf14a46SMatthew Knepley   IS             isrow;
29251a30905SBarry Smith   PetscBool      isSeqAIJ,isSeqSBAIJ,isMPIAIJ;
2933bf14a46SMatthew Knepley 
2943bf14a46SMatthew Knepley   PetscFunctionBegin;
295251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATSEQAIJ,&isSeqAIJ);CHKERRQ(ierr);
296251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&isMPIAIJ);CHKERRQ(ierr);
297251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATSEQSBAIJ,&isSeqSBAIJ);CHKERRQ(ierr);
2983bf14a46SMatthew Knepley   if (lu->matstruc == DIFFERENT_NONZERO_PATTERN) {
2993bf14a46SMatthew Knepley     (F)->ops->solve = MatSolve_PaStiX;
3003bf14a46SMatthew Knepley 
3013bf14a46SMatthew Knepley     /* Initialize a PASTIX instance */
302ce94432eSBarry Smith     ierr = MPI_Comm_dup(PetscObjectComm((PetscObject)A),&(lu->pastix_comm));CHKERRQ(ierr);
3033bf14a46SMatthew Knepley     ierr = MPI_Comm_rank(lu->pastix_comm, &lu->commRank);CHKERRQ(ierr);
3043bf14a46SMatthew Knepley     ierr = MPI_Comm_size(lu->pastix_comm, &lu->commSize);CHKERRQ(ierr);
3053bf14a46SMatthew Knepley 
3063bf14a46SMatthew Knepley     /* Set pastix options */
3073bf14a46SMatthew Knepley     lu->iparm[IPARM_MODIFY_PARAMETER] = API_NO;
3083bf14a46SMatthew Knepley     lu->iparm[IPARM_START_TASK]       = API_TASK_INIT;
3093bf14a46SMatthew Knepley     lu->iparm[IPARM_END_TASK]         = API_TASK_INIT;
3102205254eSKarl Rupp 
3113bf14a46SMatthew Knepley     lu->rhsnbr = 1;
3123bf14a46SMatthew Knepley 
3133bf14a46SMatthew Knepley     /* Call to set default pastix options */
314d41469e0Sxavier lacoste     PASTIX_CALL(&(lu->pastix_data),
315d41469e0Sxavier lacoste                 lu->pastix_comm,
316d41469e0Sxavier lacoste                 lu->n,
317d41469e0Sxavier lacoste                 lu->colptr,
318d41469e0Sxavier lacoste                 lu->row,
3195ec454cfSSatish Balay                 (PastixScalar*)lu->val,
320d41469e0Sxavier lacoste                 lu->perm,
321d41469e0Sxavier lacoste                 lu->invp,
3225ec454cfSSatish Balay                 (PastixScalar*)lu->rhs,
323d41469e0Sxavier lacoste                 lu->rhsnbr,
324d41469e0Sxavier lacoste                 lu->iparm,
325d41469e0Sxavier lacoste                 lu->dparm);
326baed9decSBarry Smith     if (lu->iparm[IPARM_ERROR_NUMBER] != 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error reported by PaStiX in MatFactorNumeric: iparm(IPARM_ERROR_NUMBER)=%d\n",lu->iparm[IPARM_ERROR_NUMBER]);
3273bf14a46SMatthew Knepley 
328ce94432eSBarry Smith     ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"PaStiX Options","Mat");CHKERRQ(ierr);
3293bf14a46SMatthew Knepley     icntl = -1;
33041c8de11SBarry Smith     lu->iparm[IPARM_VERBOSE] = API_VERBOSE_NOT;
33141c8de11SBarry Smith     ierr = PetscOptionsInt("-mat_pastix_verbose","iparm[IPARM_VERBOSE] : level of printing (0 to 2)","None",lu->iparm[IPARM_VERBOSE],&icntl,&flg);CHKERRQ(ierr);
332d41469e0Sxavier lacoste     if ((flg && icntl >= 0) || PetscLogPrintInfo) {
3333bf14a46SMatthew Knepley       lu->iparm[IPARM_VERBOSE] =  icntl;
3343bf14a46SMatthew Knepley     }
3353bf14a46SMatthew Knepley     icntl=-1;
336e4e47003SBarry 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);
3373bf14a46SMatthew Knepley     if ((flg && icntl > 0)) {
3383bf14a46SMatthew Knepley       lu->iparm[IPARM_THREAD_NBR] = icntl;
3393bf14a46SMatthew Knepley     }
3403bf14a46SMatthew Knepley     PetscOptionsEnd();
3413bf14a46SMatthew Knepley     valOnly = PETSC_FALSE;
34241c8de11SBarry Smith   } else {
3435d6241c8SBarry Smith     if (isSeqAIJ || isMPIAIJ) {
3445d6241c8SBarry Smith       ierr    = PetscFree(lu->colptr);CHKERRQ(ierr);
3455d6241c8SBarry Smith       ierr    = PetscFree(lu->row);CHKERRQ(ierr);
3465d6241c8SBarry Smith       ierr    = PetscFree(lu->val);CHKERRQ(ierr);
3475d6241c8SBarry Smith       valOnly = PETSC_FALSE;
3485d6241c8SBarry Smith     } else valOnly = PETSC_TRUE;
3493bf14a46SMatthew Knepley   }
3503bf14a46SMatthew Knepley 
3513bf14a46SMatthew Knepley   lu->iparm[IPARM_MATRIX_VERIFICATION] = API_YES;
3523bf14a46SMatthew Knepley 
3533bf14a46SMatthew Knepley   /* convert mpi A to seq mat A */
3543bf14a46SMatthew Knepley   ierr = ISCreateStride(PETSC_COMM_SELF,M,0,1,&isrow);CHKERRQ(ierr);
3557dae84e0SHong Zhang   ierr = MatCreateSubMatrices(A,1,&isrow,&isrow,MAT_INITIAL_MATRIX,&tseq);CHKERRQ(ierr);
3566bf464f9SBarry Smith   ierr = ISDestroy(&isrow);CHKERRQ(ierr);
3573bf14a46SMatthew Knepley 
35841c8de11SBarry Smith   ierr = MatConvertToCSC(*tseq,valOnly, &lu->n, &lu->colptr, &lu->row, &lu->val);CHKERRQ(ierr);
35941c8de11SBarry Smith   ierr = MatIsSymmetric(*tseq,0.0,&isSym);CHKERRQ(ierr);
36041c8de11SBarry Smith   ierr = MatDestroyMatrices(1,&tseq);CHKERRQ(ierr);
36141c8de11SBarry Smith 
3625d6241c8SBarry Smith   if (!lu->perm) {
363854ce69bSBarry Smith     ierr = PetscMalloc1(lu->n,&(lu->perm));CHKERRQ(ierr);
364854ce69bSBarry Smith     ierr = PetscMalloc1(lu->n,&(lu->invp));CHKERRQ(ierr);
3655d6241c8SBarry Smith   }
3663bf14a46SMatthew Knepley 
3673bf14a46SMatthew Knepley   if (isSym) {
368745c78f7SBarry Smith     /* On symmetric matrix, LLT */
3693bf14a46SMatthew Knepley     lu->iparm[IPARM_SYM]           = API_SYM_YES;
37041c8de11SBarry Smith     lu->iparm[IPARM_FACTORIZATION] = API_FACT_LDLT;
371f31ce8a6SBarry Smith   } else {
372745c78f7SBarry Smith     /* On unsymmetric matrix, LU */
3733bf14a46SMatthew Knepley     lu->iparm[IPARM_SYM]           = API_SYM_NO;
3743bf14a46SMatthew Knepley     lu->iparm[IPARM_FACTORIZATION] = API_FACT_LU;
3753bf14a46SMatthew Knepley   }
3763bf14a46SMatthew Knepley 
3773bf14a46SMatthew Knepley   /*----------------*/
3783bf14a46SMatthew Knepley   if (lu->matstruc == DIFFERENT_NONZERO_PATTERN) {
37958c95f1bSBarry Smith     if (!(isSeqAIJ || isSeqSBAIJ) && !lu->b_seq) {
3803bf14a46SMatthew Knepley       /* PaStiX only supports centralized rhs. Create scatter scat_rhs for repeated use in MatSolve() */
3813bf14a46SMatthew Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF,A->cmap->N,&lu->b_seq);CHKERRQ(ierr);
3823bf14a46SMatthew Knepley       ierr = ISCreateStride(PETSC_COMM_SELF,A->cmap->N,0,1,&is_iden);CHKERRQ(ierr);
3832a7a6963SBarry Smith       ierr = MatCreateVecs(A,NULL,&b);CHKERRQ(ierr);
3843bf14a46SMatthew Knepley       ierr = VecScatterCreate(b,is_iden,lu->b_seq,is_iden,&lu->scat_rhs);CHKERRQ(ierr);
3853bf14a46SMatthew Knepley       ierr = VecScatterCreate(lu->b_seq,is_iden,b,is_iden,&lu->scat_sol);CHKERRQ(ierr);
3866bf464f9SBarry Smith       ierr = ISDestroy(&is_iden);CHKERRQ(ierr);
3876bf464f9SBarry Smith       ierr = VecDestroy(&b);CHKERRQ(ierr);
3883bf14a46SMatthew Knepley     }
3893bf14a46SMatthew Knepley     lu->iparm[IPARM_START_TASK] = API_TASK_ORDERING;
3903bf14a46SMatthew Knepley     lu->iparm[IPARM_END_TASK]   = API_TASK_NUMFACT;
3913bf14a46SMatthew Knepley 
392d41469e0Sxavier lacoste     PASTIX_CALL(&(lu->pastix_data),
393d41469e0Sxavier lacoste                 lu->pastix_comm,
394d41469e0Sxavier lacoste                 lu->n,
395d41469e0Sxavier lacoste                 lu->colptr,
396d41469e0Sxavier lacoste                 lu->row,
3975ec454cfSSatish Balay                 (PastixScalar*)lu->val,
398d41469e0Sxavier lacoste                 lu->perm,
399d41469e0Sxavier lacoste                 lu->invp,
4005ec454cfSSatish Balay                 (PastixScalar*)lu->rhs,
401d41469e0Sxavier lacoste                 lu->rhsnbr,
402d41469e0Sxavier lacoste                 lu->iparm,
403d41469e0Sxavier lacoste                 lu->dparm);
404baed9decSBarry 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]);
40541c8de11SBarry Smith   } else {
4063bf14a46SMatthew Knepley     lu->iparm[IPARM_START_TASK] = API_TASK_NUMFACT;
4073bf14a46SMatthew Knepley     lu->iparm[IPARM_END_TASK]   = API_TASK_NUMFACT;
408d41469e0Sxavier lacoste     PASTIX_CALL(&(lu->pastix_data),
409d41469e0Sxavier lacoste                 lu->pastix_comm,
410d41469e0Sxavier lacoste                 lu->n,
411d41469e0Sxavier lacoste                 lu->colptr,
412d41469e0Sxavier lacoste                 lu->row,
4135ec454cfSSatish Balay                 (PastixScalar*)lu->val,
414d41469e0Sxavier lacoste                 lu->perm,
415d41469e0Sxavier lacoste                 lu->invp,
4165ec454cfSSatish Balay                 (PastixScalar*)lu->rhs,
417d41469e0Sxavier lacoste                 lu->rhsnbr,
418d41469e0Sxavier lacoste                 lu->iparm,
419d41469e0Sxavier lacoste                 lu->dparm);
420baed9decSBarry 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]);
4213bf14a46SMatthew Knepley   }
4223bf14a46SMatthew Knepley 
4233bf14a46SMatthew Knepley   (F)->assembled    = PETSC_TRUE;
4243bf14a46SMatthew Knepley   lu->matstruc      = SAME_NONZERO_PATTERN;
4253bf14a46SMatthew Knepley   lu->CleanUpPastix = PETSC_TRUE;
4263bf14a46SMatthew Knepley   PetscFunctionReturn(0);
4273bf14a46SMatthew Knepley }
4283bf14a46SMatthew Knepley 
4293bf14a46SMatthew Knepley /* Note the Petsc r and c permutations are ignored */
4303bf14a46SMatthew Knepley PetscErrorCode MatLUFactorSymbolic_AIJPASTIX(Mat F,Mat A,IS r,IS c,const MatFactorInfo *info)
4313bf14a46SMatthew Knepley {
43258c95f1bSBarry Smith   Mat_Pastix *lu = (Mat_Pastix*)F->data;
4333bf14a46SMatthew Knepley 
4343bf14a46SMatthew Knepley   PetscFunctionBegin;
4353bf14a46SMatthew Knepley   lu->iparm[IPARM_FACTORIZATION] = API_FACT_LU;
4363bf14a46SMatthew Knepley   lu->iparm[IPARM_SYM]           = API_SYM_YES;
4373bf14a46SMatthew Knepley   lu->matstruc                   = DIFFERENT_NONZERO_PATTERN;
4383bf14a46SMatthew Knepley   F->ops->lufactornumeric        = MatFactorNumeric_PaStiX;
4393bf14a46SMatthew Knepley   PetscFunctionReturn(0);
4403bf14a46SMatthew Knepley }
4413bf14a46SMatthew Knepley 
4423bf14a46SMatthew Knepley 
4433bf14a46SMatthew Knepley /* Note the Petsc r permutation is ignored */
4443bf14a46SMatthew Knepley PetscErrorCode MatCholeskyFactorSymbolic_SBAIJPASTIX(Mat F,Mat A,IS r,const MatFactorInfo *info)
4453bf14a46SMatthew Knepley {
44658c95f1bSBarry Smith   Mat_Pastix *lu = (Mat_Pastix*)(F)->data;
4473bf14a46SMatthew Knepley 
4483bf14a46SMatthew Knepley   PetscFunctionBegin;
4493bf14a46SMatthew Knepley   lu->iparm[IPARM_FACTORIZATION]  = API_FACT_LLT;
4503bf14a46SMatthew Knepley   lu->iparm[IPARM_SYM]            = API_SYM_NO;
4513bf14a46SMatthew Knepley   lu->matstruc                    = DIFFERENT_NONZERO_PATTERN;
4523bf14a46SMatthew Knepley   (F)->ops->choleskyfactornumeric = MatFactorNumeric_PaStiX;
4533bf14a46SMatthew Knepley   PetscFunctionReturn(0);
4543bf14a46SMatthew Knepley }
4553bf14a46SMatthew Knepley 
4563bf14a46SMatthew Knepley PetscErrorCode MatView_PaStiX(Mat A,PetscViewer viewer)
4573bf14a46SMatthew Knepley {
4583bf14a46SMatthew Knepley   PetscErrorCode    ierr;
459ace3abfcSBarry Smith   PetscBool         iascii;
4603bf14a46SMatthew Knepley   PetscViewerFormat format;
4613bf14a46SMatthew Knepley 
4623bf14a46SMatthew Knepley   PetscFunctionBegin;
463251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
4643bf14a46SMatthew Knepley   if (iascii) {
4653bf14a46SMatthew Knepley     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
4663bf14a46SMatthew Knepley     if (format == PETSC_VIEWER_ASCII_INFO) {
46758c95f1bSBarry Smith       Mat_Pastix *lu=(Mat_Pastix*)A->data;
468b5e56a35SBarry Smith 
469b5e56a35SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"PaStiX run parameters:\n");CHKERRQ(ierr);
470b5e56a35SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Matrix type :                      %s \n",((lu->iparm[IPARM_SYM] == API_SYM_YES) ? "Symmetric" : "Unsymmetric"));CHKERRQ(ierr);
471b5e56a35SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Level of printing (0,1,2):         %d \n",lu->iparm[IPARM_VERBOSE]);CHKERRQ(ierr);
472b5e56a35SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Number of refinements iterations : %d \n",lu->iparm[IPARM_NBITER]);CHKERRQ(ierr);
473b5e56a35SBarry Smith       ierr = PetscPrintf(PETSC_COMM_SELF,"  Error :                        %g \n",lu->dparm[DPARM_RELATIVE_ERROR]);CHKERRQ(ierr);
4743bf14a46SMatthew Knepley     }
4753bf14a46SMatthew Knepley   }
4763bf14a46SMatthew Knepley   PetscFunctionReturn(0);
4773bf14a46SMatthew Knepley }
4783bf14a46SMatthew Knepley 
4793bf14a46SMatthew Knepley 
4803bf14a46SMatthew Knepley /*MC
4812692d6eeSBarry Smith      MATSOLVERPASTIX  - A solver package providing direct solvers (LU) for distributed
4823bf14a46SMatthew Knepley   and sequential matrices via the external package PaStiX.
4833bf14a46SMatthew Knepley 
48490b3b921SSatish Balay   Use ./configure --download-pastix --download-ptscotch  to have PETSc installed with PasTiX
485c2b89b5dSBarry Smith 
4863ca39a21SBarry Smith   Use -pc_type lu -pc_factor_mat_solver_type pastix to use this direct solver
4873bf14a46SMatthew Knepley 
4883bf14a46SMatthew Knepley   Options Database Keys:
489b5e56a35SBarry Smith + -mat_pastix_verbose   <0,1,2>   - print level
490b5e56a35SBarry Smith - -mat_pastix_threadnbr <integer> - Set the thread number by MPI task.
4913bf14a46SMatthew Knepley 
492*95452b02SPatrick Sanan   Notes:
493*95452b02SPatrick Sanan     This only works for matrices with symmetric nonzero structure, if you pass it a matrix with
494baed9decSBarry Smith    nonsymmetric structure PasTiX and hence PETSc return with an error.
495baed9decSBarry Smith 
4963bf14a46SMatthew Knepley   Level: beginner
4973bf14a46SMatthew Knepley 
4983ca39a21SBarry Smith .seealso: PCFactorSetMatSolverType(), MatSolverType
49941c8de11SBarry Smith 
5003bf14a46SMatthew Knepley M*/
5013bf14a46SMatthew Knepley 
5023bf14a46SMatthew Knepley 
5033bf14a46SMatthew Knepley PetscErrorCode MatGetInfo_PaStiX(Mat A,MatInfoType flag,MatInfo *info)
5043bf14a46SMatthew Knepley {
50558c95f1bSBarry Smith   Mat_Pastix *lu =(Mat_Pastix*)A->data;
5063bf14a46SMatthew Knepley 
5073bf14a46SMatthew Knepley   PetscFunctionBegin;
5083bf14a46SMatthew Knepley   info->block_size        = 1.0;
5093bf14a46SMatthew Knepley   info->nz_allocated      = lu->iparm[IPARM_NNZEROS];
5103bf14a46SMatthew Knepley   info->nz_used           = lu->iparm[IPARM_NNZEROS];
5113bf14a46SMatthew Knepley   info->nz_unneeded       = 0.0;
5123bf14a46SMatthew Knepley   info->assemblies        = 0.0;
5133bf14a46SMatthew Knepley   info->mallocs           = 0.0;
5143bf14a46SMatthew Knepley   info->memory            = 0.0;
5153bf14a46SMatthew Knepley   info->fill_ratio_given  = 0;
5163bf14a46SMatthew Knepley   info->fill_ratio_needed = 0;
5173bf14a46SMatthew Knepley   info->factor_mallocs    = 0;
5183bf14a46SMatthew Knepley   PetscFunctionReturn(0);
5193bf14a46SMatthew Knepley }
5203bf14a46SMatthew Knepley 
521ea799195SBarry Smith static PetscErrorCode MatFactorGetSolverType_pastix(Mat A,MatSolverType *type)
5223bf14a46SMatthew Knepley {
5233bf14a46SMatthew Knepley   PetscFunctionBegin;
5242692d6eeSBarry Smith   *type = MATSOLVERPASTIX;
5253bf14a46SMatthew Knepley   PetscFunctionReturn(0);
5263bf14a46SMatthew Knepley }
5273bf14a46SMatthew Knepley 
5283bf14a46SMatthew Knepley /*
5293bf14a46SMatthew Knepley     The seq and mpi versions of this function are the same
5303bf14a46SMatthew Knepley */
531cc2e6a90SBarry Smith static PetscErrorCode MatGetFactor_seqaij_pastix(Mat A,MatFactorType ftype,Mat *F)
5323bf14a46SMatthew Knepley {
5333bf14a46SMatthew Knepley   Mat            B;
5343bf14a46SMatthew Knepley   PetscErrorCode ierr;
5353bf14a46SMatthew Knepley   Mat_Pastix     *pastix;
5363bf14a46SMatthew Knepley 
5373bf14a46SMatthew Knepley   PetscFunctionBegin;
538e7e72b3dSBarry Smith   if (ftype != MAT_FACTOR_LU) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot use PETSc AIJ matrices with PaStiX Cholesky, use SBAIJ matrix");
5393bf14a46SMatthew Knepley   /* Create the factorization matrix */
540ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
5413bf14a46SMatthew Knepley   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
54258c95f1bSBarry Smith   ierr = PetscStrallocpy("pastix",&((PetscObject)B)->type_name);CHKERRQ(ierr);
54358c95f1bSBarry Smith   ierr = MatSetUp(B);CHKERRQ(ierr);
5443bf14a46SMatthew Knepley 
5453bf14a46SMatthew Knepley   B->ops->lufactorsymbolic = MatLUFactorSymbolic_AIJPASTIX;
5463bf14a46SMatthew Knepley   B->ops->view             = MatView_PaStiX;
5473bf14a46SMatthew Knepley   B->ops->getinfo          = MatGetInfo_PaStiX;
5482205254eSKarl Rupp 
5493ca39a21SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverType_C",MatFactorGetSolverType_pastix);CHKERRQ(ierr);
5502205254eSKarl Rupp 
551d5f3da31SBarry Smith   B->factortype = MAT_FACTOR_LU;
5523bf14a46SMatthew Knepley 
55300c67f3bSHong Zhang   /* set solvertype */
55400c67f3bSHong Zhang   ierr = PetscFree(B->solvertype);CHKERRQ(ierr);
55500c67f3bSHong Zhang   ierr = PetscStrallocpy(MATSOLVERPASTIX,&B->solvertype);CHKERRQ(ierr);
55600c67f3bSHong Zhang 
557b00a9115SJed Brown   ierr = PetscNewLog(B,&pastix);CHKERRQ(ierr);
5582205254eSKarl Rupp 
5593bf14a46SMatthew Knepley   pastix->CleanUpPastix = PETSC_FALSE;
5600298fd71SBarry Smith   pastix->scat_rhs      = NULL;
5610298fd71SBarry Smith   pastix->scat_sol      = NULL;
56258c95f1bSBarry Smith   B->ops->getinfo       = MatGetInfo_External;
5633bf14a46SMatthew Knepley   B->ops->destroy       = MatDestroy_Pastix;
56458c95f1bSBarry Smith   B->data               = (void*)pastix;
5653bf14a46SMatthew Knepley 
5663bf14a46SMatthew Knepley   *F = B;
5673bf14a46SMatthew Knepley   PetscFunctionReturn(0);
5683bf14a46SMatthew Knepley }
5693bf14a46SMatthew Knepley 
570cc2e6a90SBarry Smith static PetscErrorCode MatGetFactor_mpiaij_pastix(Mat A,MatFactorType ftype,Mat *F)
5713bf14a46SMatthew Knepley {
5723bf14a46SMatthew Knepley   Mat            B;
5733bf14a46SMatthew Knepley   PetscErrorCode ierr;
5743bf14a46SMatthew Knepley   Mat_Pastix     *pastix;
5753bf14a46SMatthew Knepley 
5763bf14a46SMatthew Knepley   PetscFunctionBegin;
577e32f2f54SBarry Smith   if (ftype != MAT_FACTOR_LU) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot use PETSc AIJ matrices with PaStiX Cholesky, use SBAIJ matrix");
5783bf14a46SMatthew Knepley   /* Create the factorization matrix */
579ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
5803bf14a46SMatthew Knepley   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
58158c95f1bSBarry Smith   ierr = PetscStrallocpy("pastix",&((PetscObject)B)->type_name);CHKERRQ(ierr);
58258c95f1bSBarry Smith   ierr = MatSetUp(B);CHKERRQ(ierr);
5833bf14a46SMatthew Knepley 
5843bf14a46SMatthew Knepley   B->ops->lufactorsymbolic = MatLUFactorSymbolic_AIJPASTIX;
5853bf14a46SMatthew Knepley   B->ops->view             = MatView_PaStiX;
58658c95f1bSBarry Smith   B->ops->getinfo          = MatGetInfo_PaStiX;
5873ca39a21SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverType_C",MatFactorGetSolverType_pastix);CHKERRQ(ierr);
5882205254eSKarl Rupp 
589d5f3da31SBarry Smith   B->factortype = MAT_FACTOR_LU;
5903bf14a46SMatthew Knepley 
59100c67f3bSHong Zhang   /* set solvertype */
59200c67f3bSHong Zhang   ierr = PetscFree(B->solvertype);CHKERRQ(ierr);
59300c67f3bSHong Zhang   ierr = PetscStrallocpy(MATSOLVERPASTIX,&B->solvertype);CHKERRQ(ierr);
59400c67f3bSHong Zhang 
595b00a9115SJed Brown   ierr = PetscNewLog(B,&pastix);CHKERRQ(ierr);
5962205254eSKarl Rupp 
5973bf14a46SMatthew Knepley   pastix->CleanUpPastix = PETSC_FALSE;
5980298fd71SBarry Smith   pastix->scat_rhs      = NULL;
5990298fd71SBarry Smith   pastix->scat_sol      = NULL;
60058c95f1bSBarry Smith   B->ops->getinfo       = MatGetInfo_External;
6013bf14a46SMatthew Knepley   B->ops->destroy       = MatDestroy_Pastix;
60258c95f1bSBarry Smith   B->data               = (void*)pastix;
6033bf14a46SMatthew Knepley 
6043bf14a46SMatthew Knepley   *F = B;
6053bf14a46SMatthew Knepley   PetscFunctionReturn(0);
6063bf14a46SMatthew Knepley }
6073bf14a46SMatthew Knepley 
608cc2e6a90SBarry Smith static PetscErrorCode MatGetFactor_seqsbaij_pastix(Mat A,MatFactorType ftype,Mat *F)
6093bf14a46SMatthew Knepley {
6103bf14a46SMatthew Knepley   Mat            B;
6113bf14a46SMatthew Knepley   PetscErrorCode ierr;
6123bf14a46SMatthew Knepley   Mat_Pastix     *pastix;
6133bf14a46SMatthew Knepley 
6143bf14a46SMatthew Knepley   PetscFunctionBegin;
615e7e72b3dSBarry Smith   if (ftype != MAT_FACTOR_CHOLESKY) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot use PETSc SBAIJ matrices with PaStiX LU, use AIJ matrix");
6163bf14a46SMatthew Knepley   /* Create the factorization matrix */
617ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
6183bf14a46SMatthew Knepley   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
61958c95f1bSBarry Smith   ierr = PetscStrallocpy("pastix",&((PetscObject)B)->type_name);CHKERRQ(ierr);
62058c95f1bSBarry Smith   ierr = MatSetUp(B);CHKERRQ(ierr);
6213bf14a46SMatthew Knepley 
6223bf14a46SMatthew Knepley   B->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SBAIJPASTIX;
6233bf14a46SMatthew Knepley   B->ops->view                   = MatView_PaStiX;
62458c95f1bSBarry Smith   B->ops->getinfo                = MatGetInfo_PaStiX;
6253ca39a21SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverType_C",MatFactorGetSolverType_pastix);CHKERRQ(ierr);
6262205254eSKarl Rupp 
627d5f3da31SBarry Smith   B->factortype = MAT_FACTOR_CHOLESKY;
6283bf14a46SMatthew Knepley 
62900c67f3bSHong Zhang   /* set solvertype */
63000c67f3bSHong Zhang   ierr = PetscFree(B->solvertype);CHKERRQ(ierr);
63100c67f3bSHong Zhang   ierr = PetscStrallocpy(MATSOLVERPASTIX,&B->solvertype);CHKERRQ(ierr);
63200c67f3bSHong Zhang 
633b00a9115SJed Brown   ierr = PetscNewLog(B,&pastix);CHKERRQ(ierr);
6342205254eSKarl Rupp 
6353bf14a46SMatthew Knepley   pastix->CleanUpPastix = PETSC_FALSE;
6360298fd71SBarry Smith   pastix->scat_rhs      = NULL;
6370298fd71SBarry Smith   pastix->scat_sol      = NULL;
63858c95f1bSBarry Smith   B->ops->getinfo       = MatGetInfo_External;
6393bf14a46SMatthew Knepley   B->ops->destroy       = MatDestroy_Pastix;
64058c95f1bSBarry Smith   B->data               = (void*)pastix;
6413bf14a46SMatthew Knepley 
6423bf14a46SMatthew Knepley   *F = B;
6433bf14a46SMatthew Knepley   PetscFunctionReturn(0);
6443bf14a46SMatthew Knepley }
6453bf14a46SMatthew Knepley 
646cc2e6a90SBarry Smith static PetscErrorCode MatGetFactor_mpisbaij_pastix(Mat A,MatFactorType ftype,Mat *F)
6473bf14a46SMatthew Knepley {
6483bf14a46SMatthew Knepley   Mat            B;
6493bf14a46SMatthew Knepley   PetscErrorCode ierr;
6503bf14a46SMatthew Knepley   Mat_Pastix     *pastix;
6513bf14a46SMatthew Knepley 
6523bf14a46SMatthew Knepley   PetscFunctionBegin;
653e32f2f54SBarry Smith   if (ftype != MAT_FACTOR_CHOLESKY) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot use PETSc SBAIJ matrices with PaStiX LU, use AIJ matrix");
65441c8de11SBarry Smith 
6553bf14a46SMatthew Knepley   /* Create the factorization matrix */
656ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
6573bf14a46SMatthew Knepley   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
65858c95f1bSBarry Smith   ierr = PetscStrallocpy("pastix",&((PetscObject)B)->type_name);CHKERRQ(ierr);
65958c95f1bSBarry Smith   ierr = MatSetUp(B);CHKERRQ(ierr);
6603bf14a46SMatthew Knepley 
6613bf14a46SMatthew Knepley   B->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SBAIJPASTIX;
6623bf14a46SMatthew Knepley   B->ops->view                   = MatView_PaStiX;
66358c95f1bSBarry Smith   B->ops->getinfo                = MatGetInfo_PaStiX;
66458c95f1bSBarry Smith   B->ops->destroy                = MatDestroy_Pastix;
6653ca39a21SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverType_C",MatFactorGetSolverType_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->data               = (void*)pastix;
6793bf14a46SMatthew Knepley 
6803bf14a46SMatthew Knepley   *F = B;
6813bf14a46SMatthew Knepley   PetscFunctionReturn(0);
6823bf14a46SMatthew Knepley }
683f7a08781SBarry Smith 
6843ca39a21SBarry Smith PETSC_EXTERN PetscErrorCode MatSolverTypeRegister_Pastix(void)
68542c9c57cSBarry Smith {
68642c9c57cSBarry Smith   PetscErrorCode ierr;
68742c9c57cSBarry Smith 
68842c9c57cSBarry Smith   PetscFunctionBegin;
6893ca39a21SBarry Smith   ierr = MatSolverTypeRegister(MATSOLVERPASTIX,MATMPIAIJ,        MAT_FACTOR_LU,MatGetFactor_mpiaij_pastix);CHKERRQ(ierr);
6903ca39a21SBarry Smith   ierr = MatSolverTypeRegister(MATSOLVERPASTIX,MATSEQAIJ,        MAT_FACTOR_LU,MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
6913ca39a21SBarry Smith   ierr = MatSolverTypeRegister(MATSOLVERPASTIX,MATMPISBAIJ,      MAT_FACTOR_CHOLESKY,MatGetFactor_mpisbaij_pastix);CHKERRQ(ierr);
6923ca39a21SBarry Smith   ierr = MatSolverTypeRegister(MATSOLVERPASTIX,MATSEQSBAIJ,      MAT_FACTOR_CHOLESKY,MatGetFactor_seqsbaij_pastix);CHKERRQ(ierr);
69342c9c57cSBarry Smith   PetscFunctionReturn(0);
69442c9c57cSBarry Smith }
695