xref: /petsc/src/mat/impls/aij/mpi/pastix/pastix.c (revision d41469e0dc440bd369b0dda8085217f5158dab4b)
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 
943801a69SSatish Balay #if defined(PETSC_HAVE_STDLIB_H)
1043801a69SSatish Balay #include <stdlib.h>
1143801a69SSatish Balay #endif
1243801a69SSatish Balay #if defined(PETSC_HAVE_STRING_H)
1343801a69SSatish Balay #include <string.h>
1443801a69SSatish Balay #endif
1543801a69SSatish Balay 
163bf14a46SMatthew Knepley EXTERN_C_BEGIN
17c6db04a5SJed Brown #include <pastix.h>
183bf14a46SMatthew Knepley EXTERN_C_END
193bf14a46SMatthew Knepley 
20*d41469e0Sxavier lacoste #ifdef PETSC_USE_COMPLEX
21*d41469e0Sxavier lacoste #define PASTIX_CALL(pastix_data, pastix_comm,             \
22*d41469e0Sxavier lacoste                     n, colptr, row, val,                  \
23*d41469e0Sxavier lacoste                     perm, invp,                           \
24*d41469e0Sxavier lacoste                     rhs, rhsnbr,                          \
25*d41469e0Sxavier lacoste                     iparm, dparm)                         \
26*d41469e0Sxavier lacoste   if (sizeof(PetscScalar) == sizeof(double complex)) {    \
27*d41469e0Sxavier lacoste     z_pastix(pastix_data, pastix_comm,                    \
28*d41469e0Sxavier lacoste              n, colptr, row, (double complex*)val,        \
29*d41469e0Sxavier lacoste              perm, invp,                                  \
30*d41469e0Sxavier lacoste              (double complex *)rhs, rhsnbr,               \
31*d41469e0Sxavier lacoste              iparm, dparm);                               \
32*d41469e0Sxavier lacoste   } else {                                                \
33*d41469e0Sxavier lacoste     c_pastix(pastix_data, pastix_comm,                    \
34*d41469e0Sxavier lacoste              n, colptr, row, (complex*)val,               \
35*d41469e0Sxavier lacoste              perm, invp,                                  \
36*d41469e0Sxavier lacoste              (complex*)rhs, rhsnbr,                       \
37*d41469e0Sxavier lacoste              iparm, dparm);                               \
38*d41469e0Sxavier lacoste   }
39*d41469e0Sxavier lacoste 
40*d41469e0Sxavier lacoste #define PASTIX_CHECKMATRIX(comm,                                        \
41*d41469e0Sxavier lacoste                            verb,sym, realloc,                           \
42*d41469e0Sxavier lacoste                            n, colptr, rows, values,                     \
43*d41469e0Sxavier lacoste                            l2g, dof)                                    \
44*d41469e0Sxavier lacoste   if (sizeof(PetscScalar) == sizeof(double complex)) {                  \
45*d41469e0Sxavier lacoste     z_pastix_checkMatrix(comm,                                          \
46*d41469e0Sxavier lacoste                          verb,sym, realloc,                             \
47*d41469e0Sxavier lacoste                          n, colptr, rows, (double complex **)values,    \
48*d41469e0Sxavier lacoste                          l2g, dof);                                     \
49*d41469e0Sxavier lacoste   } else {                                                              \
50*d41469e0Sxavier lacoste     c_pastix_checkMatrix(comm,                                          \
51*d41469e0Sxavier lacoste                          verb,sym, realloc,                             \
52*d41469e0Sxavier lacoste                          n, colptr, rows, (complex**)values,            \
53*d41469e0Sxavier lacoste                          l2g, dof);                                     \
54*d41469e0Sxavier lacoste   }
55*d41469e0Sxavier lacoste 
56*d41469e0Sxavier lacoste #else /* PETSC_USE_COMPLEX */
57*d41469e0Sxavier lacoste 
58*d41469e0Sxavier lacoste #define PASTIX_CALL(pastix_data, pastix_comm,           \
59*d41469e0Sxavier lacoste                     n, colptr, row, val,                \
60*d41469e0Sxavier lacoste                     perm, invp,                         \
61*d41469e0Sxavier lacoste                     rhs, rhsnbr,                        \
62*d41469e0Sxavier lacoste                     iparm, dparm)                       \
63*d41469e0Sxavier lacoste   if (sizeof(PetscScalar) == sizeof(double)) {          \
64*d41469e0Sxavier lacoste     d_pastix(pastix_data, pastix_comm,                  \
65*d41469e0Sxavier lacoste              n, colptr, row, (double *)val,             \
66*d41469e0Sxavier lacoste              perm, invp,                                \
67*d41469e0Sxavier lacoste              (double *)rhs, rhsnbr,                     \
68*d41469e0Sxavier lacoste              iparm, dparm);                             \
69*d41469e0Sxavier lacoste   } else {                                              \
70*d41469e0Sxavier lacoste     s_pastix(pastix_data, pastix_comm,                  \
71*d41469e0Sxavier lacoste              n, colptr, row, (float*)val,               \
72*d41469e0Sxavier lacoste              perm, invp,                                \
73*d41469e0Sxavier lacoste              (float *)rhs, rhsnbr,                      \
74*d41469e0Sxavier lacoste              iparm, dparm);                             \
75*d41469e0Sxavier lacoste   }
76*d41469e0Sxavier lacoste 
77*d41469e0Sxavier lacoste #define PASTIX_CHECKMATRIX(comm,                                        \
78*d41469e0Sxavier lacoste                            verb,sym, realloc,                           \
79*d41469e0Sxavier lacoste                            n, colptr, rows, values,                     \
80*d41469e0Sxavier lacoste                            l2g, dof)                                    \
81*d41469e0Sxavier lacoste   if (sizeof(PetscScalar) == sizeof(double)) {                          \
82*d41469e0Sxavier lacoste     d_pastix_checkMatrix(comm,                                          \
83*d41469e0Sxavier lacoste                          verb,sym, realloc,                             \
84*d41469e0Sxavier lacoste                          n, colptr, rows, (double **)values,            \
85*d41469e0Sxavier lacoste                          l2g, dof);                                     \
86*d41469e0Sxavier lacoste   } else {                                                              \
87*d41469e0Sxavier lacoste     s_pastix_checkMatrix(comm,                                          \
88*d41469e0Sxavier lacoste                          verb,sym, realloc,                             \
89*d41469e0Sxavier lacoste                          n, colptr, rows, (float **)values,             \
90*d41469e0Sxavier lacoste                          l2g, dof);                                     \
91*d41469e0Sxavier lacoste   }
92*d41469e0Sxavier lacoste #endif /* PETSC_USE_COMPLEX */
93*d41469e0Sxavier lacoste 
943bf14a46SMatthew Knepley typedef struct Mat_Pastix_ {
953bf14a46SMatthew Knepley   pastix_data_t *pastix_data;    /* Pastix data storage structure                        */
963bf14a46SMatthew Knepley   MatStructure   matstruc;
973bf14a46SMatthew Knepley   PetscInt       n;              /* Number of columns in the matrix                      */
983bf14a46SMatthew Knepley   PetscInt       *colptr;        /* Index of first element of each column in row and val */
993bf14a46SMatthew Knepley   PetscInt       *row;           /* Row of each element of the matrix                    */
1003bf14a46SMatthew Knepley   PetscScalar    *val;           /* Value of each element of the matrix                  */
1013bf14a46SMatthew Knepley   PetscInt       *perm;          /* Permutation tabular                                  */
1023bf14a46SMatthew Knepley   PetscInt       *invp;          /* Reverse permutation tabular                          */
1033bf14a46SMatthew Knepley   PetscScalar    *rhs;           /* Rhight-hand-side member                              */
1043bf14a46SMatthew Knepley   PetscInt       rhsnbr;         /* Rhight-hand-side number (must be 1)                  */
1053bf14a46SMatthew Knepley   PetscInt       iparm[64];      /* Integer parameters                                   */
1063bf14a46SMatthew Knepley   double         dparm[64];      /* Floating point parameters                            */
1073bf14a46SMatthew Knepley   MPI_Comm       pastix_comm;    /* PaStiX MPI communicator                              */
1083bf14a46SMatthew Knepley   PetscMPIInt    commRank;       /* MPI rank                                             */
1093bf14a46SMatthew Knepley   PetscMPIInt    commSize;       /* MPI communicator size                                */
110ace3abfcSBarry Smith   PetscBool      CleanUpPastix;  /* Boolean indicating if we call PaStiX clean step      */
1113bf14a46SMatthew Knepley   VecScatter     scat_rhs;
1123bf14a46SMatthew Knepley   VecScatter     scat_sol;
113f31ce8a6SBarry Smith   Vec            b_seq;
114ace3abfcSBarry Smith   PetscBool      isAIJ;
115bf0cc555SLisandro Dalcin   PetscErrorCode (*Destroy)(Mat);
1163bf14a46SMatthew Knepley } Mat_Pastix;
1173bf14a46SMatthew Knepley 
11809573ac7SBarry Smith extern PetscErrorCode MatDuplicate_Pastix(Mat,MatDuplicateOption,Mat*);
1193bf14a46SMatthew Knepley 
120eb1f6c34SBarry Smith #undef __FUNCT__
121eb1f6c34SBarry Smith #define __FUNCT__ "MatConvertToCSC"
1223bf14a46SMatthew Knepley /*
1233bf14a46SMatthew Knepley    convert Petsc seqaij matrix to CSC: colptr[n], row[nz], val[nz]
1243bf14a46SMatthew Knepley 
1253bf14a46SMatthew Knepley   input:
1263bf14a46SMatthew Knepley     A       - matrix in seqaij or mpisbaij (bs=1) format
1273bf14a46SMatthew Knepley     valOnly - FALSE: spaces are allocated and values are set for the CSC
1283bf14a46SMatthew Knepley               TRUE:  Only fill values
1293bf14a46SMatthew Knepley   output:
1303bf14a46SMatthew Knepley     n       - Size of the matrix
1313bf14a46SMatthew Knepley     colptr  - Index of first element of each column in row and val
1323bf14a46SMatthew Knepley     row     - Row of each element of the matrix
1333bf14a46SMatthew Knepley     values  - Value of each element of the matrix
1343bf14a46SMatthew Knepley  */
135ace3abfcSBarry Smith PetscErrorCode MatConvertToCSC(Mat A,PetscBool  valOnly,PetscInt *n,PetscInt **colptr,PetscInt **row,PetscScalar **values)
13641c8de11SBarry Smith {
1373bf14a46SMatthew Knepley   Mat_SeqAIJ     *aa      = (Mat_SeqAIJ*)A->data;
1383bf14a46SMatthew Knepley   PetscInt       *rowptr  = aa->i;
1393bf14a46SMatthew Knepley   PetscInt       *col     = aa->j;
1403bf14a46SMatthew Knepley   PetscScalar    *rvalues = aa->a;
1413bf14a46SMatthew Knepley   PetscInt        m       = A->rmap->N;
142745c78f7SBarry Smith   PetscInt        nnz;
1433bf14a46SMatthew Knepley   PetscInt        i,j, k;
1443bf14a46SMatthew Knepley   PetscInt        base = 1;
1453bf14a46SMatthew Knepley   PetscInt        idx;
1463bf14a46SMatthew Knepley   PetscErrorCode  ierr;
1473bf14a46SMatthew Knepley   PetscInt        colidx;
1483bf14a46SMatthew Knepley   PetscInt       *colcount;
149ace3abfcSBarry Smith   PetscBool       isSBAIJ;
150ace3abfcSBarry Smith   PetscBool       isSeqSBAIJ;
151ace3abfcSBarry Smith   PetscBool       isMpiSBAIJ;
152ace3abfcSBarry Smith   PetscBool       isSym;
153*d41469e0Sxavier lacoste   PetscBool       flg;
154*d41469e0Sxavier lacoste   PetscInt        icntl;
155*d41469e0Sxavier lacoste   PetscInt        verb;
156*d41469e0Sxavier lacoste   PetscInt        check;
1573bf14a46SMatthew Knepley 
1583bf14a46SMatthew Knepley   PetscFunctionBegin;
15941c8de11SBarry Smith   ierr = MatIsSymmetric(A,0.0,&isSym);CHKERRQ(ierr);
16041c8de11SBarry Smith   ierr = PetscTypeCompare((PetscObject)A,MATSBAIJ,&isSBAIJ);CHKERRQ(ierr);
16141c8de11SBarry Smith   ierr = PetscTypeCompare((PetscObject)A,MATSEQSBAIJ,&isSeqSBAIJ);CHKERRQ(ierr);
16241c8de11SBarry Smith   ierr = PetscTypeCompare((PetscObject)A,MATMPISBAIJ,&isMpiSBAIJ);CHKERRQ(ierr);
1633bf14a46SMatthew Knepley 
164745c78f7SBarry Smith   *n = A->cmap->N;
165745c78f7SBarry Smith 
166745c78f7SBarry Smith   /* PaStiX only needs triangular matrix if matrix is symmetric
167745c78f7SBarry Smith    */
16841c8de11SBarry Smith   if (isSym && !(isSBAIJ || isSeqSBAIJ || isMpiSBAIJ)) {
169745c78f7SBarry Smith     nnz = (aa->nz - *n)/2 + *n;
170745c78f7SBarry Smith   }
17141c8de11SBarry Smith   else {
172745c78f7SBarry Smith     nnz     = aa->nz;
1733bf14a46SMatthew Knepley   }
1743bf14a46SMatthew Knepley 
1753bf14a46SMatthew Knepley   if (!valOnly){
1763bf14a46SMatthew Knepley     ierr = PetscMalloc(((*n)+1) *sizeof(PetscInt)   ,colptr );CHKERRQ(ierr);
1773bf14a46SMatthew Knepley     ierr = PetscMalloc( nnz     *sizeof(PetscInt)   ,row);CHKERRQ(ierr);
1783bf14a46SMatthew Knepley     ierr = PetscMalloc( nnz     *sizeof(PetscScalar),values);CHKERRQ(ierr);
1793bf14a46SMatthew Knepley 
18041c8de11SBarry Smith     if (isSBAIJ || isSeqSBAIJ || isMpiSBAIJ) {
18141c8de11SBarry Smith         ierr = PetscMemcpy (*colptr, rowptr, ((*n)+1)*sizeof(PetscInt));CHKERRQ(ierr);
18241c8de11SBarry Smith         for (i = 0; i < *n+1; i++)
18341c8de11SBarry Smith           (*colptr)[i] += base;
18441c8de11SBarry Smith         ierr = PetscMemcpy (*row, col, (nnz)*sizeof(PetscInt));CHKERRQ(ierr);
18541c8de11SBarry Smith         for (i = 0; i < nnz; i++)
18641c8de11SBarry Smith           (*row)[i] += base;
18741c8de11SBarry Smith         ierr = PetscMemcpy (*values, rvalues, (nnz)*sizeof(PetscScalar));CHKERRQ(ierr);
18841c8de11SBarry Smith     } else {
18941c8de11SBarry Smith       ierr = PetscMalloc((*n)*sizeof(PetscInt)   ,&colcount);CHKERRQ(ierr);
19041c8de11SBarry Smith 
191f31ce8a6SBarry Smith       for (i = 0; i < m; i++) colcount[i] = 0;
1923bf14a46SMatthew Knepley       /* Fill-in colptr */
193f31ce8a6SBarry Smith       for (i = 0; i < m; i++) {
194f31ce8a6SBarry Smith         for (j = rowptr[i]; j < rowptr[i+1]; j++) {
195f31ce8a6SBarry Smith           if (!isSym || col[j] <= i)  colcount[col[j]]++;
196f31ce8a6SBarry Smith         }
197f31ce8a6SBarry Smith       }
198745c78f7SBarry Smith 
1993bf14a46SMatthew Knepley       (*colptr)[0] = base;
2003bf14a46SMatthew Knepley       for (j = 0; j < *n; j++) {
2013bf14a46SMatthew Knepley         (*colptr)[j+1] = (*colptr)[j] + colcount[j];
202745c78f7SBarry Smith         /* in next loop we fill starting from (*colptr)[colidx] - base */
2033bf14a46SMatthew Knepley         colcount[j] = -base;
2043bf14a46SMatthew Knepley       }
2053bf14a46SMatthew Knepley 
2063bf14a46SMatthew Knepley       /* Fill-in rows and values */
2073bf14a46SMatthew Knepley       for (i = 0; i < m; i++) {
2083bf14a46SMatthew Knepley         for (j = rowptr[i]; j < rowptr[i+1]; j++) {
20941c8de11SBarry Smith           if (!isSym || col[j] <= i) {
2103bf14a46SMatthew Knepley             colidx = col[j];
2113bf14a46SMatthew Knepley             idx    = (*colptr)[colidx] + colcount[colidx];
2123bf14a46SMatthew Knepley             (*row)[idx]    = i + base;
2133bf14a46SMatthew Knepley             (*values)[idx] = rvalues[j];
2143bf14a46SMatthew Knepley             colcount[colidx]++;
2153bf14a46SMatthew Knepley           }
2163bf14a46SMatthew Knepley         }
2173bf14a46SMatthew Knepley       }
21841c8de11SBarry Smith       ierr = PetscFree(colcount);CHKERRQ(ierr);
219745c78f7SBarry Smith     }
22041c8de11SBarry Smith   } else {
221745c78f7SBarry Smith     /* Fill-in only values */
2223bf14a46SMatthew Knepley     for (i = 0; i < m; i++) {
2233bf14a46SMatthew Knepley       for (j = rowptr[i]; j < rowptr[i+1]; j++) {
2243bf14a46SMatthew Knepley         colidx = col[j];
22541c8de11SBarry Smith         if ((isSBAIJ || isSeqSBAIJ || isMpiSBAIJ) ||!isSym || col[j] <= i)
226745c78f7SBarry Smith           {
227745c78f7SBarry Smith             /* look for the value to fill */
228f31ce8a6SBarry Smith             for (k = (*colptr)[colidx] - base; k < (*colptr)[colidx + 1] - base; k++) {
229eb1f6c34SBarry Smith               if (((*row)[k]-base) == i) {
2303bf14a46SMatthew Knepley                 (*values)[k] = rvalues[j];
2313bf14a46SMatthew Knepley                 break;
2323bf14a46SMatthew Knepley               }
2333bf14a46SMatthew Knepley             }
234f31ce8a6SBarry Smith             /* data structure of sparse matrix has changed */
235e32f2f54SBarry Smith             if (k == (*colptr)[colidx + 1] - base) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"overflow on k %D",k);
2363bf14a46SMatthew Knepley           }
2373bf14a46SMatthew Knepley       }
2383bf14a46SMatthew Knepley     }
239745c78f7SBarry Smith   }
240*d41469e0Sxavier lacoste 
241*d41469e0Sxavier lacoste   icntl=-1;
242*d41469e0Sxavier lacoste   check = 0;
243*d41469e0Sxavier lacoste   ierr = PetscOptionsInt("-mat_pastix_check","Check the matrix 0 : no, 1 : yes)","None",check,&icntl,&flg);CHKERRQ(ierr);
244*d41469e0Sxavier lacoste   if ((flg && icntl >= 0) || PetscLogPrintInfo) {
245*d41469e0Sxavier lacoste     check =  icntl;
246*d41469e0Sxavier lacoste   }
247*d41469e0Sxavier lacoste   if (check == 1) {
24870fe17b1SSatish Balay     PetscScalar *tmpvalues;
24970fe17b1SSatish Balay     PetscInt    *tmprows,*tmpcolptr;
2500f11a792SBarry Smith     tmpvalues = (PetscScalar*)malloc(nnz*sizeof(PetscScalar));    if (!tmpvalues) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Unable to allocate memory");
2510f11a792SBarry Smith     tmprows   = (PetscInt*)   malloc(nnz*sizeof(PetscInt));       if (!tmprows)   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Unable to allocate memory");
2520f11a792SBarry Smith     tmpcolptr = (PetscInt*)   malloc((*n+1)*sizeof(PetscInt));    if (!tmpcolptr) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Unable to allocate memory");
25343801a69SSatish Balay 
25470fe17b1SSatish Balay     ierr = PetscMemcpy(tmpcolptr,*colptr,(*n+1)*sizeof(PetscInt));CHKERRQ(ierr);
25570fe17b1SSatish Balay     ierr = PetscMemcpy(tmprows,*row,nnz*sizeof(PetscInt));CHKERRQ(ierr);
25670fe17b1SSatish Balay     ierr = PetscMemcpy(tmpvalues,*values,nnz*sizeof(PetscScalar));CHKERRQ(ierr);
25743801a69SSatish Balay     ierr = PetscFree(*row);CHKERRQ(ierr);
25843801a69SSatish Balay     ierr = PetscFree(*values);CHKERRQ(ierr);
25943801a69SSatish Balay 
260*d41469e0Sxavier lacoste     icntl=-1;
261*d41469e0Sxavier lacoste     verb = API_VERBOSE_NOT;
262*d41469e0Sxavier lacoste     ierr = PetscOptionsInt("-mat_pastix_verbose","iparm[IPARM_VERBOSE] : level of printing (0 to 2)","None",verb,&icntl,&flg);CHKERRQ(ierr);
263*d41469e0Sxavier lacoste     if ((flg && icntl >= 0) || PetscLogPrintInfo) {
264*d41469e0Sxavier lacoste       verb =  icntl;
265*d41469e0Sxavier lacoste     }
266*d41469e0Sxavier lacoste     PASTIX_CHECKMATRIX(MPI_COMM_WORLD,verb,((isSym != 0) ? API_SYM_YES : API_SYM_NO),API_YES,*n,&tmpcolptr,&tmprows,&tmpvalues,NULL,1);
26743801a69SSatish Balay 
26870fe17b1SSatish Balay     ierr = PetscMemcpy(*colptr,tmpcolptr,(*n+1)*sizeof(PetscInt));CHKERRQ(ierr);
26943801a69SSatish Balay     ierr = PetscMalloc(((*colptr)[*n]-1)*sizeof(PetscInt),row);CHKERRQ(ierr);
27070fe17b1SSatish Balay     ierr = PetscMemcpy(*row,tmprows,((*colptr)[*n]-1)*sizeof(PetscInt));CHKERRQ(ierr);
27143801a69SSatish Balay     ierr = PetscMalloc(((*colptr)[*n]-1)*sizeof(PetscScalar),values);CHKERRQ(ierr);
27270fe17b1SSatish Balay     ierr = PetscMemcpy(*values,tmpvalues,((*colptr)[*n]-1)*sizeof(PetscScalar));CHKERRQ(ierr);
273be76a908SBarry Smith     free(tmpvalues);
274be76a908SBarry Smith     free(tmprows);
275be76a908SBarry Smith     free(tmpcolptr);
276be76a908SBarry Smith 
27743801a69SSatish Balay   }
2783bf14a46SMatthew Knepley   PetscFunctionReturn(0);
2793bf14a46SMatthew Knepley }
2803bf14a46SMatthew Knepley 
2813bf14a46SMatthew Knepley 
2823bf14a46SMatthew Knepley 
2833bf14a46SMatthew Knepley #undef __FUNCT__
2843bf14a46SMatthew Knepley #define __FUNCT__ "MatDestroy_Pastix"
2853bf14a46SMatthew Knepley /*
2863bf14a46SMatthew Knepley   Call clean step of PaStiX if lu->CleanUpPastix == true.
2873bf14a46SMatthew Knepley   Free the CSC matrix.
2883bf14a46SMatthew Knepley  */
2893bf14a46SMatthew Knepley PetscErrorCode MatDestroy_Pastix(Mat A)
2903bf14a46SMatthew Knepley {
2913bf14a46SMatthew Knepley   Mat_Pastix      *lu=(Mat_Pastix*)A->spptr;
2923bf14a46SMatthew Knepley   PetscErrorCode   ierr;
2933bf14a46SMatthew Knepley   PetscMPIInt      size=lu->commSize;
294745c78f7SBarry Smith 
2953bf14a46SMatthew Knepley   PetscFunctionBegin;
296bf0cc555SLisandro Dalcin   if (lu && lu->CleanUpPastix) {
2973bf14a46SMatthew Knepley     /* Terminate instance, deallocate memories */
2983bf14a46SMatthew Knepley     if (size > 1){
2996bf464f9SBarry Smith       ierr = VecScatterDestroy(&lu->scat_rhs);CHKERRQ(ierr);
3006bf464f9SBarry Smith       ierr = VecDestroy(&lu->b_seq);CHKERRQ(ierr);
3016bf464f9SBarry Smith       ierr = VecScatterDestroy(&lu->scat_sol);CHKERRQ(ierr);
3023bf14a46SMatthew Knepley     }
3033bf14a46SMatthew Knepley 
3043bf14a46SMatthew Knepley     lu->iparm[IPARM_START_TASK]=API_TASK_CLEAN;
3053bf14a46SMatthew Knepley     lu->iparm[IPARM_END_TASK]=API_TASK_CLEAN;
3063bf14a46SMatthew Knepley 
307*d41469e0Sxavier lacoste     PASTIX_CALL(&(lu->pastix_data),
3083bf14a46SMatthew Knepley                 lu->pastix_comm,
309*d41469e0Sxavier lacoste                 lu->n,
310*d41469e0Sxavier lacoste                 lu->colptr,
311*d41469e0Sxavier lacoste                 lu->row,
312*d41469e0Sxavier lacoste                 lu->val,
313*d41469e0Sxavier lacoste                 lu->perm,
314*d41469e0Sxavier lacoste                 lu->invp,
315*d41469e0Sxavier lacoste                 lu->rhs,
316*d41469e0Sxavier lacoste                 lu->rhsnbr,
317*d41469e0Sxavier lacoste                 lu->iparm,
3183bf14a46SMatthew Knepley                 lu->dparm);
3193bf14a46SMatthew Knepley 
3203bf14a46SMatthew Knepley     ierr = PetscFree(lu->colptr);CHKERRQ(ierr);
3213bf14a46SMatthew Knepley     ierr = PetscFree(lu->row);  CHKERRQ(ierr);
3223bf14a46SMatthew Knepley     ierr = PetscFree(lu->val);  CHKERRQ(ierr);
3233bf14a46SMatthew Knepley     ierr = PetscFree(lu->perm); CHKERRQ(ierr);
3243bf14a46SMatthew Knepley     ierr = PetscFree(lu->invp); CHKERRQ(ierr);
3253bf14a46SMatthew Knepley     ierr = MPI_Comm_free(&(lu->pastix_comm));CHKERRQ(ierr);
3263bf14a46SMatthew Knepley   }
327bf0cc555SLisandro Dalcin   if (lu && lu->Destroy) {
328bf0cc555SLisandro Dalcin     ierr = (lu->Destroy)(A);CHKERRQ(ierr);
329bf0cc555SLisandro Dalcin   }
330bf0cc555SLisandro Dalcin   ierr = PetscFree(A->spptr);CHKERRQ(ierr);
3313bf14a46SMatthew Knepley   PetscFunctionReturn(0);
3323bf14a46SMatthew Knepley }
3333bf14a46SMatthew Knepley 
3343bf14a46SMatthew Knepley #undef __FUNCT__
3353bf14a46SMatthew Knepley #define __FUNCT__ "MatSolve_PaStiX"
3363bf14a46SMatthew Knepley /*
3373bf14a46SMatthew Knepley   Gather right-hand-side.
3383bf14a46SMatthew Knepley   Call for Solve step.
3393bf14a46SMatthew Knepley   Scatter solution.
3403bf14a46SMatthew Knepley  */
3413bf14a46SMatthew Knepley PetscErrorCode MatSolve_PaStiX(Mat A,Vec b,Vec x)
3423bf14a46SMatthew Knepley {
3433bf14a46SMatthew Knepley   Mat_Pastix     *lu=(Mat_Pastix*)A->spptr;
3443bf14a46SMatthew Knepley   PetscScalar    *array;
3453bf14a46SMatthew Knepley   Vec             x_seq;
3463bf14a46SMatthew Knepley   PetscErrorCode  ierr;
3473bf14a46SMatthew Knepley 
3483bf14a46SMatthew Knepley   PetscFunctionBegin;
3493bf14a46SMatthew Knepley   lu->rhsnbr = 1;
3503bf14a46SMatthew Knepley   x_seq = lu->b_seq;
3513bf14a46SMatthew Knepley   if (lu->commSize > 1){
3523bf14a46SMatthew Knepley     /* PaStiX only supports centralized rhs. Scatter b into a seqential rhs vector */
3533bf14a46SMatthew Knepley     ierr = VecScatterBegin(lu->scat_rhs,b,x_seq,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3543bf14a46SMatthew Knepley     ierr = VecScatterEnd(lu->scat_rhs,b,x_seq,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
355b5e56a35SBarry Smith     ierr = VecGetArray(x_seq,&array);CHKERRQ(ierr);
35641c8de11SBarry Smith   } else {  /* size == 1 */
3573bf14a46SMatthew Knepley     ierr = VecCopy(b,x);CHKERRQ(ierr);
3583bf14a46SMatthew Knepley     ierr = VecGetArray(x,&array);CHKERRQ(ierr);
3593bf14a46SMatthew Knepley   }
3603bf14a46SMatthew Knepley   lu->rhs = array;
3613bf14a46SMatthew Knepley   if (lu->commSize == 1){
3623bf14a46SMatthew Knepley     ierr = VecRestoreArray(x,&array);CHKERRQ(ierr);
3633bf14a46SMatthew Knepley   } else {
3643bf14a46SMatthew Knepley     ierr = VecRestoreArray(x_seq,&array);CHKERRQ(ierr);
3653bf14a46SMatthew Knepley   }
3663bf14a46SMatthew Knepley 
3673bf14a46SMatthew Knepley   /* solve phase */
3683bf14a46SMatthew Knepley   /*-------------*/
3693bf14a46SMatthew Knepley   lu->iparm[IPARM_START_TASK] = API_TASK_SOLVE;
3703bf14a46SMatthew Knepley   lu->iparm[IPARM_END_TASK]   = API_TASK_REFINE;
371745c78f7SBarry Smith   lu->iparm[IPARM_RHS_MAKING] = API_RHS_B;
3723bf14a46SMatthew Knepley 
373*d41469e0Sxavier lacoste   PASTIX_CALL(&(lu->pastix_data),
374*d41469e0Sxavier lacoste               lu->pastix_comm,
375*d41469e0Sxavier lacoste               lu->n,
376*d41469e0Sxavier lacoste               lu->colptr,
377*d41469e0Sxavier lacoste               lu->row,
378*d41469e0Sxavier lacoste               lu->val,
379*d41469e0Sxavier lacoste               lu->perm,
380*d41469e0Sxavier lacoste               lu->invp,
381*d41469e0Sxavier lacoste               lu->rhs,
382*d41469e0Sxavier lacoste               lu->rhsnbr,
383*d41469e0Sxavier lacoste               lu->iparm,
384*d41469e0Sxavier lacoste               lu->dparm);
3853bf14a46SMatthew Knepley 
38665e19b50SBarry 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] );
3873bf14a46SMatthew Knepley 
3883bf14a46SMatthew Knepley   if (lu->commSize == 1){
3893bf14a46SMatthew Knepley     ierr = VecRestoreArray(x,&(lu->rhs));CHKERRQ(ierr);
3903bf14a46SMatthew Knepley   } else {
3913bf14a46SMatthew Knepley     ierr = VecRestoreArray(x_seq,&(lu->rhs));CHKERRQ(ierr);
3923bf14a46SMatthew Knepley   }
3933bf14a46SMatthew Knepley 
3943bf14a46SMatthew Knepley   if (lu->commSize > 1) { /* convert PaStiX centralized solution to petsc mpi x */
3953bf14a46SMatthew Knepley     ierr = VecScatterBegin(lu->scat_sol,x_seq,x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3963bf14a46SMatthew Knepley     ierr = VecScatterEnd(lu->scat_sol,x_seq,x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3973bf14a46SMatthew Knepley   }
3983bf14a46SMatthew Knepley   PetscFunctionReturn(0);
3993bf14a46SMatthew Knepley }
4003bf14a46SMatthew Knepley 
4013bf14a46SMatthew Knepley /*
4023bf14a46SMatthew Knepley   Numeric factorisation using PaStiX solver.
4033bf14a46SMatthew Knepley 
4043bf14a46SMatthew Knepley  */
4053bf14a46SMatthew Knepley #undef __FUNCT__
40653c77d0aSJed Brown #define __FUNCT__ "MatFactorNumeric_PaStiX"
4073bf14a46SMatthew Knepley PetscErrorCode MatFactorNumeric_PaStiX(Mat F,Mat A,const MatFactorInfo *info)
4083bf14a46SMatthew Knepley {
4093bf14a46SMatthew Knepley   Mat_Pastix    *lu =(Mat_Pastix*)(F)->spptr;
41041c8de11SBarry Smith   Mat           *tseq;
4113bf14a46SMatthew Knepley   PetscErrorCode ierr = 0;
412b5e56a35SBarry Smith   PetscInt       icntl;
413b5e56a35SBarry Smith   PetscInt       M=A->rmap->N;
414ace3abfcSBarry Smith   PetscBool      valOnly,flg, isSym;
4153bf14a46SMatthew Knepley   Mat            F_diag;
4163bf14a46SMatthew Knepley   IS             is_iden;
4173bf14a46SMatthew Knepley   Vec            b;
4183bf14a46SMatthew Knepley   IS             isrow;
41951a30905SBarry Smith   PetscBool      isSeqAIJ,isSeqSBAIJ,isMPIAIJ;
4203bf14a46SMatthew Knepley 
4213bf14a46SMatthew Knepley   PetscFunctionBegin;
42241c8de11SBarry Smith 
4233bf14a46SMatthew Knepley   ierr = PetscTypeCompare((PetscObject)A,MATSEQAIJ,&isSeqAIJ);CHKERRQ(ierr);
42451a30905SBarry Smith   ierr = PetscTypeCompare((PetscObject)A,MATMPIAIJ,&isMPIAIJ);CHKERRQ(ierr);
4253bf14a46SMatthew Knepley   ierr = PetscTypeCompare((PetscObject)A,MATSEQSBAIJ,&isSeqSBAIJ);CHKERRQ(ierr);
4263bf14a46SMatthew Knepley   if (lu->matstruc == DIFFERENT_NONZERO_PATTERN){
4273bf14a46SMatthew Knepley     (F)->ops->solve   = MatSolve_PaStiX;
4283bf14a46SMatthew Knepley 
4293bf14a46SMatthew Knepley     /* Initialize a PASTIX instance */
4303bf14a46SMatthew Knepley     ierr = MPI_Comm_dup(((PetscObject)A)->comm,&(lu->pastix_comm));CHKERRQ(ierr);
4313bf14a46SMatthew Knepley     ierr = MPI_Comm_rank(lu->pastix_comm, &lu->commRank);         CHKERRQ(ierr);
4323bf14a46SMatthew Knepley     ierr = MPI_Comm_size(lu->pastix_comm, &lu->commSize);         CHKERRQ(ierr);
4333bf14a46SMatthew Knepley 
4343bf14a46SMatthew Knepley     /* Set pastix options */
4353bf14a46SMatthew Knepley     lu->iparm[IPARM_MODIFY_PARAMETER] = API_NO;
4363bf14a46SMatthew Knepley     lu->iparm[IPARM_START_TASK]       = API_TASK_INIT;
4373bf14a46SMatthew Knepley     lu->iparm[IPARM_END_TASK]         = API_TASK_INIT;
4383bf14a46SMatthew Knepley     lu->rhsnbr = 1;
4393bf14a46SMatthew Knepley 
4403bf14a46SMatthew Knepley     /* Call to set default pastix options */
441*d41469e0Sxavier lacoste     PASTIX_CALL(&(lu->pastix_data),
442*d41469e0Sxavier lacoste                 lu->pastix_comm,
443*d41469e0Sxavier lacoste                 lu->n,
444*d41469e0Sxavier lacoste                 lu->colptr,
445*d41469e0Sxavier lacoste                 lu->row,
446*d41469e0Sxavier lacoste                 lu->val,
447*d41469e0Sxavier lacoste                 lu->perm,
448*d41469e0Sxavier lacoste                 lu->invp,
449*d41469e0Sxavier lacoste                 lu->rhs,
450*d41469e0Sxavier lacoste                 lu->rhsnbr,
451*d41469e0Sxavier lacoste                 lu->iparm,
452*d41469e0Sxavier lacoste                 lu->dparm);
4533bf14a46SMatthew Knepley 
4543bf14a46SMatthew Knepley     ierr = PetscOptionsBegin(((PetscObject)A)->comm,((PetscObject)A)->prefix,"PaStiX Options","Mat");CHKERRQ(ierr);
4553bf14a46SMatthew Knepley 
4563bf14a46SMatthew Knepley     icntl=-1;
45741c8de11SBarry Smith     lu->iparm[IPARM_VERBOSE] = API_VERBOSE_NOT;
45841c8de11SBarry Smith     ierr = PetscOptionsInt("-mat_pastix_verbose","iparm[IPARM_VERBOSE] : level of printing (0 to 2)","None",lu->iparm[IPARM_VERBOSE],&icntl,&flg);CHKERRQ(ierr);
459*d41469e0Sxavier lacoste     if ((flg && icntl >= 0) || PetscLogPrintInfo) {
4603bf14a46SMatthew Knepley       lu->iparm[IPARM_VERBOSE] =  icntl;
4613bf14a46SMatthew Knepley     }
4623bf14a46SMatthew Knepley     icntl=-1;
46341c8de11SBarry Smith     ierr = PetscOptionsInt("-mat_pastix_threadnbr","iparm[IPARM_THREAD_NBR] : Number of thread by MPI node","None",lu->iparm[IPARM_THREAD_NBR],&icntl,PETSC_NULL);CHKERRQ(ierr);
4643bf14a46SMatthew Knepley     if ((flg && icntl > 0)) {
4653bf14a46SMatthew Knepley       lu->iparm[IPARM_THREAD_NBR] = icntl;
4663bf14a46SMatthew Knepley     }
4673bf14a46SMatthew Knepley     PetscOptionsEnd();
4683bf14a46SMatthew Knepley     valOnly = PETSC_FALSE;
46941c8de11SBarry Smith   }  else {
4705d6241c8SBarry Smith     if (isSeqAIJ || isMPIAIJ)  {
4715d6241c8SBarry Smith       ierr = PetscFree(lu->colptr);CHKERRQ(ierr);
4725d6241c8SBarry Smith       ierr = PetscFree(lu->row);CHKERRQ(ierr);
4735d6241c8SBarry Smith       ierr = PetscFree(lu->val);CHKERRQ(ierr);
4745d6241c8SBarry Smith       valOnly = PETSC_FALSE;
4755d6241c8SBarry Smith     } else valOnly = PETSC_TRUE;
4763bf14a46SMatthew Knepley   }
4773bf14a46SMatthew Knepley 
4783bf14a46SMatthew Knepley   lu->iparm[IPARM_MATRIX_VERIFICATION] = API_YES;
4793bf14a46SMatthew Knepley 
4803bf14a46SMatthew Knepley   /* convert mpi A to seq mat A */
4813bf14a46SMatthew Knepley   ierr = ISCreateStride(PETSC_COMM_SELF,M,0,1,&isrow);CHKERRQ(ierr);
4823bf14a46SMatthew Knepley   ierr = MatGetSubMatrices(A,1,&isrow,&isrow,MAT_INITIAL_MATRIX,&tseq);CHKERRQ(ierr);
4836bf464f9SBarry Smith   ierr = ISDestroy(&isrow);CHKERRQ(ierr);
4843bf14a46SMatthew Knepley 
48541c8de11SBarry Smith   ierr = MatConvertToCSC(*tseq,valOnly, &lu->n, &lu->colptr, &lu->row, &lu->val);CHKERRQ(ierr);
48641c8de11SBarry Smith   ierr = MatIsSymmetric(*tseq,0.0,&isSym);CHKERRQ(ierr);
48741c8de11SBarry Smith   ierr = MatDestroyMatrices(1,&tseq);CHKERRQ(ierr);
48841c8de11SBarry Smith 
4895d6241c8SBarry Smith   if (!lu->perm) {
4903bf14a46SMatthew Knepley     ierr = PetscMalloc((lu->n)*sizeof(PetscInt)   ,&(lu->perm));CHKERRQ(ierr);
4913bf14a46SMatthew Knepley     ierr = PetscMalloc((lu->n)*sizeof(PetscInt)   ,&(lu->invp));CHKERRQ(ierr);
4925d6241c8SBarry Smith   }
4933bf14a46SMatthew Knepley 
4943bf14a46SMatthew Knepley   if (isSym) {
495745c78f7SBarry Smith     /* On symmetric matrix, LLT */
4963bf14a46SMatthew Knepley     lu->iparm[IPARM_SYM] = API_SYM_YES;
49741c8de11SBarry Smith     lu->iparm[IPARM_FACTORIZATION] = API_FACT_LDLT;
498f31ce8a6SBarry Smith   } else {
499745c78f7SBarry Smith     /* On unsymmetric matrix, LU */
5003bf14a46SMatthew Knepley     lu->iparm[IPARM_SYM] = API_SYM_NO;
5013bf14a46SMatthew Knepley     lu->iparm[IPARM_FACTORIZATION] = API_FACT_LU;
5023bf14a46SMatthew Knepley   }
5033bf14a46SMatthew Knepley 
5043bf14a46SMatthew Knepley   /*----------------*/
5053bf14a46SMatthew Knepley   if (lu->matstruc == DIFFERENT_NONZERO_PATTERN){
5063bf14a46SMatthew Knepley     if (!(isSeqAIJ || isSeqSBAIJ)) {
5073bf14a46SMatthew Knepley       /* PaStiX only supports centralized rhs. Create scatter scat_rhs for repeated use in MatSolve() */
5083bf14a46SMatthew Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF,A->cmap->N,&lu->b_seq);CHKERRQ(ierr);
5093bf14a46SMatthew Knepley         ierr = ISCreateStride(PETSC_COMM_SELF,A->cmap->N,0,1,&is_iden);CHKERRQ(ierr);
5103bf14a46SMatthew Knepley         ierr = VecCreate(((PetscObject)A)->comm,&b);CHKERRQ(ierr);
5113bf14a46SMatthew Knepley         ierr = VecSetSizes(b,A->rmap->n,PETSC_DECIDE);CHKERRQ(ierr);
5123bf14a46SMatthew Knepley         ierr = VecSetFromOptions(b);CHKERRQ(ierr);
5133bf14a46SMatthew Knepley 
5143bf14a46SMatthew Knepley         ierr = VecScatterCreate(b,is_iden,lu->b_seq,is_iden,&lu->scat_rhs);CHKERRQ(ierr);
5153bf14a46SMatthew Knepley         ierr = VecScatterCreate(lu->b_seq,is_iden,b,is_iden,&lu->scat_sol);CHKERRQ(ierr);
5166bf464f9SBarry Smith         ierr = ISDestroy(&is_iden);CHKERRQ(ierr);
5176bf464f9SBarry Smith         ierr = VecDestroy(&b);CHKERRQ(ierr);
5183bf14a46SMatthew Knepley     }
5193bf14a46SMatthew Knepley     lu->iparm[IPARM_START_TASK] = API_TASK_ORDERING;
5203bf14a46SMatthew Knepley     lu->iparm[IPARM_END_TASK]   = API_TASK_NUMFACT;
5213bf14a46SMatthew Knepley 
522*d41469e0Sxavier lacoste     PASTIX_CALL(&(lu->pastix_data),
523*d41469e0Sxavier lacoste                 lu->pastix_comm,
524*d41469e0Sxavier lacoste                 lu->n,
525*d41469e0Sxavier lacoste                 lu->colptr,
526*d41469e0Sxavier lacoste                 lu->row,
527*d41469e0Sxavier lacoste                 lu->val,
528*d41469e0Sxavier lacoste                 lu->perm,
529*d41469e0Sxavier lacoste                 lu->invp,
530*d41469e0Sxavier lacoste                 lu->rhs,
531*d41469e0Sxavier lacoste                 lu->rhsnbr,
532*d41469e0Sxavier lacoste                 lu->iparm,
533*d41469e0Sxavier lacoste                 lu->dparm);
53465e19b50SBarry 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]);
53541c8de11SBarry Smith   } else {
5363bf14a46SMatthew Knepley     lu->iparm[IPARM_START_TASK] = API_TASK_NUMFACT;
5373bf14a46SMatthew Knepley     lu->iparm[IPARM_END_TASK]   = API_TASK_NUMFACT;
538*d41469e0Sxavier lacoste     PASTIX_CALL(&(lu->pastix_data),
539*d41469e0Sxavier lacoste                 lu->pastix_comm,
540*d41469e0Sxavier lacoste                 lu->n,
541*d41469e0Sxavier lacoste                 lu->colptr,
542*d41469e0Sxavier lacoste                 lu->row,
543*d41469e0Sxavier lacoste                 lu->val,
544*d41469e0Sxavier lacoste                 lu->perm,
545*d41469e0Sxavier lacoste                 lu->invp,
546*d41469e0Sxavier lacoste                 lu->rhs,
547*d41469e0Sxavier lacoste                 lu->rhsnbr,
548*d41469e0Sxavier lacoste                 lu->iparm,
549*d41469e0Sxavier lacoste                 lu->dparm);
5503bf14a46SMatthew Knepley 
55165e19b50SBarry 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]);
5523bf14a46SMatthew Knepley   }
5533bf14a46SMatthew Knepley 
5543bf14a46SMatthew Knepley   if (lu->commSize > 1){
555d5f3da31SBarry Smith     if ((F)->factortype == MAT_FACTOR_LU){
5563bf14a46SMatthew Knepley       F_diag = ((Mat_MPIAIJ *)(F)->data)->A;
5573bf14a46SMatthew Knepley     } else {
5583bf14a46SMatthew Knepley       F_diag = ((Mat_MPISBAIJ *)(F)->data)->A;
5593bf14a46SMatthew Knepley     }
5603bf14a46SMatthew Knepley     F_diag->assembled = PETSC_TRUE;
5613bf14a46SMatthew Knepley   }
5623bf14a46SMatthew Knepley   (F)->assembled     = PETSC_TRUE;
5633bf14a46SMatthew Knepley   lu->matstruc       = SAME_NONZERO_PATTERN;
5643bf14a46SMatthew Knepley   lu->CleanUpPastix  = PETSC_TRUE;
5653bf14a46SMatthew Knepley   PetscFunctionReturn(0);
5663bf14a46SMatthew Knepley }
5673bf14a46SMatthew Knepley 
5683bf14a46SMatthew Knepley /* Note the Petsc r and c permutations are ignored */
5693bf14a46SMatthew Knepley #undef __FUNCT__
5703bf14a46SMatthew Knepley #define __FUNCT__ "MatLUFactorSymbolic_AIJPASTIX"
5713bf14a46SMatthew Knepley PetscErrorCode MatLUFactorSymbolic_AIJPASTIX(Mat F,Mat A,IS r,IS c,const MatFactorInfo *info)
5723bf14a46SMatthew Knepley {
5733bf14a46SMatthew Knepley   Mat_Pastix      *lu = (Mat_Pastix*)F->spptr;
5743bf14a46SMatthew Knepley 
5753bf14a46SMatthew Knepley   PetscFunctionBegin;
5763bf14a46SMatthew Knepley   lu->iparm[IPARM_FACTORIZATION] = API_FACT_LU;
5773bf14a46SMatthew Knepley   lu->iparm[IPARM_SYM]           = API_SYM_YES;
5783bf14a46SMatthew Knepley   lu->matstruc                   = DIFFERENT_NONZERO_PATTERN;
5793bf14a46SMatthew Knepley   F->ops->lufactornumeric        = MatFactorNumeric_PaStiX;
5803bf14a46SMatthew Knepley   PetscFunctionReturn(0);
5813bf14a46SMatthew Knepley }
5823bf14a46SMatthew Knepley 
5833bf14a46SMatthew Knepley 
5843bf14a46SMatthew Knepley /* Note the Petsc r permutation is ignored */
5853bf14a46SMatthew Knepley #undef __FUNCT__
5863bf14a46SMatthew Knepley #define __FUNCT__ "MatCholeskyFactorSymbolic_SBAIJPASTIX"
5873bf14a46SMatthew Knepley PetscErrorCode MatCholeskyFactorSymbolic_SBAIJPASTIX(Mat F,Mat A,IS r,const MatFactorInfo *info)
5883bf14a46SMatthew Knepley {
5893bf14a46SMatthew Knepley   Mat_Pastix      *lu = (Mat_Pastix*)(F)->spptr;
5903bf14a46SMatthew Knepley 
5913bf14a46SMatthew Knepley   PetscFunctionBegin;
5923bf14a46SMatthew Knepley   lu->iparm[IPARM_FACTORIZATION]  = API_FACT_LLT;
5933bf14a46SMatthew Knepley   lu->iparm[IPARM_SYM]            = API_SYM_NO;
5943bf14a46SMatthew Knepley   lu->matstruc                    = DIFFERENT_NONZERO_PATTERN;
5953bf14a46SMatthew Knepley   (F)->ops->choleskyfactornumeric = MatFactorNumeric_PaStiX;
5963bf14a46SMatthew Knepley   PetscFunctionReturn(0);
5973bf14a46SMatthew Knepley }
5983bf14a46SMatthew Knepley 
5993bf14a46SMatthew Knepley #undef __FUNCT__
6003bf14a46SMatthew Knepley #define __FUNCT__ "MatView_PaStiX"
6013bf14a46SMatthew Knepley PetscErrorCode MatView_PaStiX(Mat A,PetscViewer viewer)
6023bf14a46SMatthew Knepley {
6033bf14a46SMatthew Knepley   PetscErrorCode    ierr;
604ace3abfcSBarry Smith   PetscBool         iascii;
6053bf14a46SMatthew Knepley   PetscViewerFormat format;
6063bf14a46SMatthew Knepley 
6073bf14a46SMatthew Knepley   PetscFunctionBegin;
6082692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
6093bf14a46SMatthew Knepley   if (iascii) {
6103bf14a46SMatthew Knepley     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
6113bf14a46SMatthew Knepley     if (format == PETSC_VIEWER_ASCII_INFO){
612b5e56a35SBarry Smith       Mat_Pastix      *lu=(Mat_Pastix*)A->spptr;
613b5e56a35SBarry Smith 
614b5e56a35SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"PaStiX run parameters:\n");CHKERRQ(ierr);
615b5e56a35SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Matrix type :                      %s \n",((lu->iparm[IPARM_SYM] == API_SYM_YES)?"Symmetric":"Unsymmetric"));CHKERRQ(ierr);
616b5e56a35SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Level of printing (0,1,2):         %d \n",lu->iparm[IPARM_VERBOSE]);CHKERRQ(ierr);
617b5e56a35SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Number of refinements iterations : %d \n",lu->iparm[IPARM_NBITER]);CHKERRQ(ierr);
618b5e56a35SBarry Smith       ierr = PetscPrintf(PETSC_COMM_SELF,"  Error :                        %g \n",lu->dparm[DPARM_RELATIVE_ERROR]);CHKERRQ(ierr);
6193bf14a46SMatthew Knepley     }
6203bf14a46SMatthew Knepley   }
6213bf14a46SMatthew Knepley   PetscFunctionReturn(0);
6223bf14a46SMatthew Knepley }
6233bf14a46SMatthew Knepley 
6243bf14a46SMatthew Knepley 
6253bf14a46SMatthew Knepley /*MC
6262692d6eeSBarry Smith      MATSOLVERPASTIX  - A solver package providing direct solvers (LU) for distributed
6273bf14a46SMatthew Knepley   and sequential matrices via the external package PaStiX.
6283bf14a46SMatthew Knepley 
629e2e64c6bSBarry Smith   Use ./configure --download-pastix to have PETSc installed with PaStiX
6303bf14a46SMatthew Knepley 
6313bf14a46SMatthew Knepley   Options Database Keys:
632b5e56a35SBarry Smith + -mat_pastix_verbose   <0,1,2>   - print level
633b5e56a35SBarry Smith - -mat_pastix_threadnbr <integer> - Set the thread number by MPI task.
6343bf14a46SMatthew Knepley 
6353bf14a46SMatthew Knepley   Level: beginner
6363bf14a46SMatthew Knepley 
63741c8de11SBarry Smith .seealso: PCFactorSetMatSolverPackage(), MatSolverPackage
63841c8de11SBarry Smith 
6393bf14a46SMatthew Knepley M*/
6403bf14a46SMatthew Knepley 
6413bf14a46SMatthew Knepley 
6423bf14a46SMatthew Knepley #undef __FUNCT__
6433bf14a46SMatthew Knepley #define __FUNCT__ "MatGetInfo_PaStiX"
6443bf14a46SMatthew Knepley PetscErrorCode MatGetInfo_PaStiX(Mat A,MatInfoType flag,MatInfo *info)
6453bf14a46SMatthew Knepley {
6463bf14a46SMatthew Knepley     Mat_Pastix  *lu =(Mat_Pastix*)A->spptr;
6473bf14a46SMatthew Knepley 
6483bf14a46SMatthew Knepley     PetscFunctionBegin;
6493bf14a46SMatthew Knepley     info->block_size        = 1.0;
6503bf14a46SMatthew Knepley     info->nz_allocated      = lu->iparm[IPARM_NNZEROS];
6513bf14a46SMatthew Knepley     info->nz_used           = lu->iparm[IPARM_NNZEROS];
6523bf14a46SMatthew Knepley     info->nz_unneeded       = 0.0;
6533bf14a46SMatthew Knepley     info->assemblies        = 0.0;
6543bf14a46SMatthew Knepley     info->mallocs           = 0.0;
6553bf14a46SMatthew Knepley     info->memory            = 0.0;
6563bf14a46SMatthew Knepley     info->fill_ratio_given  = 0;
6573bf14a46SMatthew Knepley     info->fill_ratio_needed = 0;
6583bf14a46SMatthew Knepley     info->factor_mallocs    = 0;
6593bf14a46SMatthew Knepley     PetscFunctionReturn(0);
6603bf14a46SMatthew Knepley }
6613bf14a46SMatthew Knepley 
6623bf14a46SMatthew Knepley EXTERN_C_BEGIN
6633bf14a46SMatthew Knepley #undef __FUNCT__
6643bf14a46SMatthew Knepley #define __FUNCT__ "MatFactorGetSolverPackage_pastix"
6653bf14a46SMatthew Knepley PetscErrorCode MatFactorGetSolverPackage_pastix(Mat A,const MatSolverPackage *type)
6663bf14a46SMatthew Knepley {
6673bf14a46SMatthew Knepley   PetscFunctionBegin;
6682692d6eeSBarry Smith   *type = MATSOLVERPASTIX;
6693bf14a46SMatthew Knepley   PetscFunctionReturn(0);
6703bf14a46SMatthew Knepley }
6713bf14a46SMatthew Knepley EXTERN_C_END
6723bf14a46SMatthew Knepley 
6733bf14a46SMatthew Knepley EXTERN_C_BEGIN
6743bf14a46SMatthew Knepley /*
6753bf14a46SMatthew Knepley     The seq and mpi versions of this function are the same
6763bf14a46SMatthew Knepley */
6773bf14a46SMatthew Knepley #undef __FUNCT__
6783bf14a46SMatthew Knepley #define __FUNCT__ "MatGetFactor_seqaij_pastix"
6793bf14a46SMatthew Knepley PetscErrorCode MatGetFactor_seqaij_pastix(Mat A,MatFactorType ftype,Mat *F)
6803bf14a46SMatthew Knepley {
6813bf14a46SMatthew Knepley   Mat            B;
6823bf14a46SMatthew Knepley   PetscErrorCode ierr;
6833bf14a46SMatthew Knepley   Mat_Pastix    *pastix;
6843bf14a46SMatthew Knepley 
6853bf14a46SMatthew Knepley   PetscFunctionBegin;
686e7e72b3dSBarry Smith   if (ftype != MAT_FACTOR_LU) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot use PETSc AIJ matrices with PaStiX Cholesky, use SBAIJ matrix");
6873bf14a46SMatthew Knepley   /* Create the factorization matrix */
6883bf14a46SMatthew Knepley   ierr = MatCreate(((PetscObject)A)->comm,&B);CHKERRQ(ierr);
6893bf14a46SMatthew Knepley   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
6903bf14a46SMatthew Knepley   ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr);
6913bf14a46SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B,0,PETSC_NULL);CHKERRQ(ierr);
6923bf14a46SMatthew Knepley 
6933bf14a46SMatthew Knepley   B->ops->lufactorsymbolic = MatLUFactorSymbolic_AIJPASTIX;
6943bf14a46SMatthew Knepley   B->ops->view             = MatView_PaStiX;
6953bf14a46SMatthew Knepley   B->ops->getinfo          = MatGetInfo_PaStiX;
69631e762f5SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatFactorGetSolverPackage_C","MatFactorGetSolverPackage_pastix", MatFactorGetSolverPackage_pastix);CHKERRQ(ierr);
697d5f3da31SBarry Smith   B->factortype            = MAT_FACTOR_LU;
6983bf14a46SMatthew Knepley 
6993bf14a46SMatthew Knepley   ierr = PetscNewLog(B,Mat_Pastix,&pastix);CHKERRQ(ierr);
7003bf14a46SMatthew Knepley   pastix->CleanUpPastix             = PETSC_FALSE;
7013bf14a46SMatthew Knepley   pastix->isAIJ                     = PETSC_TRUE;
7023bf14a46SMatthew Knepley   pastix->scat_rhs                  = PETSC_NULL;
7033bf14a46SMatthew Knepley   pastix->scat_sol                  = PETSC_NULL;
704bf0cc555SLisandro Dalcin   pastix->Destroy                   = B->ops->destroy;
7053bf14a46SMatthew Knepley   B->ops->destroy                   = MatDestroy_Pastix;
7063bf14a46SMatthew Knepley   B->spptr                          = (void*)pastix;
7073bf14a46SMatthew Knepley 
7083bf14a46SMatthew Knepley   *F = B;
7093bf14a46SMatthew Knepley   PetscFunctionReturn(0);
7103bf14a46SMatthew Knepley }
7113bf14a46SMatthew Knepley EXTERN_C_END
7123bf14a46SMatthew Knepley 
713b5e56a35SBarry Smith 
7143bf14a46SMatthew Knepley EXTERN_C_BEGIN
7153bf14a46SMatthew Knepley #undef __FUNCT__
7163bf14a46SMatthew Knepley #define __FUNCT__ "MatGetFactor_mpiaij_pastix"
7173bf14a46SMatthew Knepley PetscErrorCode MatGetFactor_mpiaij_pastix(Mat A,MatFactorType ftype,Mat *F)
7183bf14a46SMatthew Knepley {
7193bf14a46SMatthew Knepley   Mat            B;
7203bf14a46SMatthew Knepley   PetscErrorCode ierr;
7213bf14a46SMatthew Knepley   Mat_Pastix    *pastix;
7223bf14a46SMatthew Knepley 
7233bf14a46SMatthew Knepley   PetscFunctionBegin;
724e32f2f54SBarry Smith   if (ftype != MAT_FACTOR_LU) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot use PETSc AIJ matrices with PaStiX Cholesky, use SBAIJ matrix");
7253bf14a46SMatthew Knepley   /* Create the factorization matrix */
7263bf14a46SMatthew Knepley   ierr = MatCreate(((PetscObject)A)->comm,&B);CHKERRQ(ierr);
7273bf14a46SMatthew Knepley   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
7283bf14a46SMatthew Knepley   ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr);
7293bf14a46SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B,0,PETSC_NULL);CHKERRQ(ierr);
7303bf14a46SMatthew Knepley   ierr = MatMPIAIJSetPreallocation(B,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
7313bf14a46SMatthew Knepley 
7323bf14a46SMatthew Knepley   B->ops->lufactorsymbolic = MatLUFactorSymbolic_AIJPASTIX;
7333bf14a46SMatthew Knepley   B->ops->view             = MatView_PaStiX;
73431e762f5SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatFactorGetSolverPackage_C","MatFactorGetSolverPackage_pastix",MatFactorGetSolverPackage_pastix);CHKERRQ(ierr);
735d5f3da31SBarry Smith   B->factortype            = MAT_FACTOR_LU;
7363bf14a46SMatthew Knepley 
7373bf14a46SMatthew Knepley   ierr = PetscNewLog(B,Mat_Pastix,&pastix);CHKERRQ(ierr);
7383bf14a46SMatthew Knepley   pastix->CleanUpPastix             = PETSC_FALSE;
7393bf14a46SMatthew Knepley   pastix->isAIJ                     = PETSC_TRUE;
7403bf14a46SMatthew Knepley   pastix->scat_rhs                  = PETSC_NULL;
7413bf14a46SMatthew Knepley   pastix->scat_sol                  = PETSC_NULL;
742bf0cc555SLisandro Dalcin   pastix->Destroy                   = B->ops->destroy;
7433bf14a46SMatthew Knepley   B->ops->destroy                   = MatDestroy_Pastix;
7443bf14a46SMatthew Knepley   B->spptr                          = (void*)pastix;
7453bf14a46SMatthew Knepley 
7463bf14a46SMatthew Knepley   *F = B;
7473bf14a46SMatthew Knepley   PetscFunctionReturn(0);
7483bf14a46SMatthew Knepley }
7493bf14a46SMatthew Knepley EXTERN_C_END
7503bf14a46SMatthew Knepley 
7513bf14a46SMatthew Knepley EXTERN_C_BEGIN
7523bf14a46SMatthew Knepley #undef __FUNCT__
7533bf14a46SMatthew Knepley #define __FUNCT__ "MatGetFactor_seqsbaij_pastix"
7543bf14a46SMatthew Knepley PetscErrorCode MatGetFactor_seqsbaij_pastix(Mat A,MatFactorType ftype,Mat *F)
7553bf14a46SMatthew Knepley {
7563bf14a46SMatthew Knepley   Mat            B;
7573bf14a46SMatthew Knepley   PetscErrorCode ierr;
7583bf14a46SMatthew Knepley   Mat_Pastix    *pastix;
7593bf14a46SMatthew Knepley 
7603bf14a46SMatthew Knepley   PetscFunctionBegin;
761e7e72b3dSBarry Smith   if (ftype != MAT_FACTOR_CHOLESKY) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot use PETSc SBAIJ matrices with PaStiX LU, use AIJ matrix");
7623bf14a46SMatthew Knepley   /* Create the factorization matrix */
7633bf14a46SMatthew Knepley   ierr = MatCreate(((PetscObject)A)->comm,&B);CHKERRQ(ierr);
7643bf14a46SMatthew Knepley   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
7653bf14a46SMatthew Knepley   ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr);
7663bf14a46SMatthew Knepley   ierr = MatSeqSBAIJSetPreallocation(B,1,0,PETSC_NULL);CHKERRQ(ierr);
7673bf14a46SMatthew Knepley   ierr = MatMPISBAIJSetPreallocation(B,1,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
7683bf14a46SMatthew Knepley 
7693bf14a46SMatthew Knepley   B->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SBAIJPASTIX;
7703bf14a46SMatthew Knepley   B->ops->view                   = MatView_PaStiX;
77131e762f5SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatFactorGetSolverPackage_C","MatFactorGetSolverPackage_pastix",MatFactorGetSolverPackage_pastix);CHKERRQ(ierr);
772d5f3da31SBarry Smith   B->factortype                  = MAT_FACTOR_CHOLESKY;
7733bf14a46SMatthew Knepley 
7743bf14a46SMatthew Knepley   ierr = PetscNewLog(B,Mat_Pastix,&pastix);CHKERRQ(ierr);
7753bf14a46SMatthew Knepley   pastix->CleanUpPastix             = PETSC_FALSE;
7763bf14a46SMatthew Knepley   pastix->isAIJ                     = PETSC_TRUE;
7773bf14a46SMatthew Knepley   pastix->scat_rhs                  = PETSC_NULL;
7783bf14a46SMatthew Knepley   pastix->scat_sol                  = PETSC_NULL;
779bf0cc555SLisandro Dalcin   pastix->Destroy                   = B->ops->destroy;
7803bf14a46SMatthew Knepley   B->ops->destroy                   = MatDestroy_Pastix;
7813bf14a46SMatthew Knepley   B->spptr                          = (void*)pastix;
7823bf14a46SMatthew Knepley 
7833bf14a46SMatthew Knepley   *F = B;
7843bf14a46SMatthew Knepley   PetscFunctionReturn(0);
7853bf14a46SMatthew Knepley }
7863bf14a46SMatthew Knepley EXTERN_C_END
7873bf14a46SMatthew Knepley 
7883bf14a46SMatthew Knepley EXTERN_C_BEGIN
7893bf14a46SMatthew Knepley #undef __FUNCT__
7903bf14a46SMatthew Knepley #define __FUNCT__ "MatGetFactor_mpisbaij_pastix"
7913bf14a46SMatthew Knepley PetscErrorCode MatGetFactor_mpisbaij_pastix(Mat A,MatFactorType ftype,Mat *F)
7923bf14a46SMatthew Knepley {
7933bf14a46SMatthew Knepley   Mat            B;
7943bf14a46SMatthew Knepley   PetscErrorCode ierr;
7953bf14a46SMatthew Knepley   Mat_Pastix    *pastix;
7963bf14a46SMatthew Knepley 
7973bf14a46SMatthew Knepley   PetscFunctionBegin;
798e32f2f54SBarry Smith   if (ftype != MAT_FACTOR_CHOLESKY) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot use PETSc SBAIJ matrices with PaStiX LU, use AIJ matrix");
79941c8de11SBarry Smith 
8003bf14a46SMatthew Knepley   /* Create the factorization matrix */
8013bf14a46SMatthew Knepley   ierr = MatCreate(((PetscObject)A)->comm,&B);CHKERRQ(ierr);
8023bf14a46SMatthew Knepley   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
8033bf14a46SMatthew Knepley   ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr);
8043bf14a46SMatthew Knepley   ierr = MatSeqSBAIJSetPreallocation(B,1,0,PETSC_NULL);CHKERRQ(ierr);
8053bf14a46SMatthew Knepley   ierr = MatMPISBAIJSetPreallocation(B,1,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
8063bf14a46SMatthew Knepley 
8073bf14a46SMatthew Knepley   B->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SBAIJPASTIX;
8083bf14a46SMatthew Knepley   B->ops->view                   = MatView_PaStiX;
80931e762f5SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatFactorGetSolverPackage_C","MatFactorGetSolverPackage_pastix",MatFactorGetSolverPackage_pastix);CHKERRQ(ierr);
810d5f3da31SBarry Smith   B->factortype                  = MAT_FACTOR_CHOLESKY;
8113bf14a46SMatthew Knepley 
8123bf14a46SMatthew Knepley   ierr = PetscNewLog(B,Mat_Pastix,&pastix);CHKERRQ(ierr);
8133bf14a46SMatthew Knepley   pastix->CleanUpPastix             = PETSC_FALSE;
8143bf14a46SMatthew Knepley   pastix->isAIJ                     = PETSC_TRUE;
8153bf14a46SMatthew Knepley   pastix->scat_rhs                  = PETSC_NULL;
8163bf14a46SMatthew Knepley   pastix->scat_sol                  = PETSC_NULL;
817bf0cc555SLisandro Dalcin   pastix->Destroy                   = B->ops->destroy;
8183bf14a46SMatthew Knepley   B->ops->destroy                   = MatDestroy_Pastix;
8193bf14a46SMatthew Knepley   B->spptr                          = (void*)pastix;
8203bf14a46SMatthew Knepley 
8213bf14a46SMatthew Knepley   *F = B;
8223bf14a46SMatthew Knepley   PetscFunctionReturn(0);
8233bf14a46SMatthew Knepley }
8243bf14a46SMatthew Knepley EXTERN_C_END
825