1 2 /* 3 Provides an interface to the SuperLU_DIST sparse solver 4 */ 5 6 #include <../src/mat/impls/aij/seq/aij.h> 7 #include <../src/mat/impls/aij/mpi/mpiaij.h> 8 #if defined(PETSC_HAVE_STDLIB_H) /* This is to get around weird problem with SuperLU on cray */ 9 #include <stdlib.h> 10 #endif 11 12 #if defined(PETSC_USE_64BIT_INDICES) 13 /* ugly SuperLU_Dist variable telling it to use long long int */ 14 #define _LONGINT 15 #endif 16 17 EXTERN_C_BEGIN 18 #if defined(PETSC_USE_COMPLEX) 19 #include <superlu_zdefs.h> 20 #else 21 #include <superlu_ddefs.h> 22 #endif 23 EXTERN_C_END 24 25 /* 26 GLOBAL - The sparse matrix and right hand side are all stored initially on process 0. Should be called centralized 27 DISTRIBUTED - The sparse matrix and right hand size are initially stored across the entire MPI communicator. 28 */ 29 typedef enum {GLOBAL,DISTRIBUTED} SuperLU_MatInputMode; 30 const char *SuperLU_MatInputModes[] = {"GLOBAL","DISTRIBUTED","SuperLU_MatInputMode","PETSC_",0}; 31 32 typedef struct { 33 int_t nprow,npcol,*row,*col; 34 gridinfo_t grid; 35 superlu_dist_options_t options; 36 SuperMatrix A_sup; 37 ScalePermstruct_t ScalePermstruct; 38 LUstruct_t LUstruct; 39 int StatPrint; 40 SuperLU_MatInputMode MatInputMode; 41 SOLVEstruct_t SOLVEstruct; 42 fact_t FactPattern; 43 MPI_Comm comm_superlu; 44 #if defined(PETSC_USE_COMPLEX) 45 doublecomplex *val; 46 #else 47 double *val; 48 #endif 49 PetscBool matsolve_iscalled,matmatsolve_iscalled; 50 PetscBool CleanUpSuperLU_Dist; /* Flag to clean up (non-global) SuperLU objects during Destroy */ 51 } Mat_SuperLU_DIST; 52 53 54 PetscErrorCode MatSuperluDistGetDiagU_SuperLU_DIST(Mat F,PetscScalar *diagU) 55 { 56 Mat_SuperLU_DIST *lu= (Mat_SuperLU_DIST*)F->data; 57 58 PetscFunctionBegin; 59 #if defined(PETSC_USE_COMPLEX) 60 PetscStackCall("SuperLU_DIST:pzGetDiagU",pzGetDiagU(F->rmap->N,&lu->LUstruct,&lu->grid,(doublecomplex*)diagU)); 61 #else 62 PetscStackCall("SuperLU_DIST:pdGetDiagU",pdGetDiagU(F->rmap->N,&lu->LUstruct,&lu->grid,diagU)); 63 #endif 64 PetscFunctionReturn(0); 65 } 66 67 PetscErrorCode MatSuperluDistGetDiagU(Mat F,PetscScalar *diagU) 68 { 69 PetscErrorCode ierr; 70 71 PetscFunctionBegin; 72 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 73 ierr = PetscTryMethod(F,"MatSuperluDistGetDiagU_C",(Mat,PetscScalar*),(F,diagU));CHKERRQ(ierr); 74 PetscFunctionReturn(0); 75 } 76 77 static PetscErrorCode MatDestroy_SuperLU_DIST(Mat A) 78 { 79 PetscErrorCode ierr; 80 Mat_SuperLU_DIST *lu = (Mat_SuperLU_DIST*)A->data; 81 82 PetscFunctionBegin; 83 if (lu->CleanUpSuperLU_Dist) { 84 /* Deallocate SuperLU_DIST storage */ 85 if (lu->MatInputMode == GLOBAL) { 86 PetscStackCall("SuperLU_DIST:Destroy_CompCol_Matrix_dist",Destroy_CompCol_Matrix_dist(&lu->A_sup)); 87 } else { 88 PetscStackCall("SuperLU_DIST:Destroy_CompRowLoc_Matrix_dist",Destroy_CompRowLoc_Matrix_dist(&lu->A_sup)); 89 if (lu->options.SolveInitialized) { 90 #if defined(PETSC_USE_COMPLEX) 91 PetscStackCall("SuperLU_DIST:zSolveFinalize",zSolveFinalize(&lu->options, &lu->SOLVEstruct)); 92 #else 93 PetscStackCall("SuperLU_DIST:dSolveFinalize",dSolveFinalize(&lu->options, &lu->SOLVEstruct)); 94 #endif 95 } 96 } 97 PetscStackCall("SuperLU_DIST:Destroy_LU",Destroy_LU(A->cmap->N, &lu->grid, &lu->LUstruct)); 98 PetscStackCall("SuperLU_DIST:ScalePermstructFree",ScalePermstructFree(&lu->ScalePermstruct)); 99 PetscStackCall("SuperLU_DIST:LUstructFree",LUstructFree(&lu->LUstruct)); 100 101 /* Release the SuperLU_DIST process grid. */ 102 PetscStackCall("SuperLU_DIST:superlu_gridexit",superlu_gridexit(&lu->grid)); 103 ierr = MPI_Comm_free(&(lu->comm_superlu));CHKERRQ(ierr); 104 } 105 ierr = PetscFree(A->data);CHKERRQ(ierr); 106 /* clear composed functions */ 107 ierr = PetscObjectComposeFunction((PetscObject)A,"MatFactorGetSolverPackage_C",NULL);CHKERRQ(ierr); 108 ierr = PetscObjectComposeFunction((PetscObject)A,"MatSuperluDistGetDiagU_C",NULL);CHKERRQ(ierr); 109 110 PetscFunctionReturn(0); 111 } 112 113 static PetscErrorCode MatSolve_SuperLU_DIST(Mat A,Vec b_mpi,Vec x) 114 { 115 Mat_SuperLU_DIST *lu = (Mat_SuperLU_DIST*)A->data; 116 PetscErrorCode ierr; 117 PetscMPIInt size; 118 PetscInt m=A->rmap->n,M=A->rmap->N,N=A->cmap->N; 119 SuperLUStat_t stat; 120 double berr[1]; 121 PetscScalar *bptr; 122 PetscInt nrhs=1; 123 Vec x_seq; 124 IS iden; 125 VecScatter scat; 126 int info; /* SuperLU_Dist info code is ALWAYS an int, even with long long indices */ 127 static PetscBool cite = PETSC_FALSE; 128 129 PetscFunctionBegin; 130 ierr = PetscCitationsRegister("@article{lidemmel03,\n author = {Xiaoye S. Li and James W. Demmel},\n title = {{SuperLU_DIST}: A Scalable Distributed-Memory Sparse Direct\n Solver for Unsymmetric Linear Systems},\n journal = {ACM Trans. Mathematical Software},\n volume = {29},\n number = {2},\n pages = {110-140},\n year = 2003\n}\n",&cite);CHKERRQ(ierr); 131 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);CHKERRQ(ierr); 132 if (size > 1 && lu->MatInputMode == GLOBAL) { 133 /* global mat input, convert b to x_seq */ 134 ierr = VecCreateSeq(PETSC_COMM_SELF,N,&x_seq);CHKERRQ(ierr); 135 ierr = ISCreateStride(PETSC_COMM_SELF,N,0,1,&iden);CHKERRQ(ierr); 136 ierr = VecScatterCreate(b_mpi,iden,x_seq,iden,&scat);CHKERRQ(ierr); 137 ierr = ISDestroy(&iden);CHKERRQ(ierr); 138 139 ierr = VecScatterBegin(scat,b_mpi,x_seq,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 140 ierr = VecScatterEnd(scat,b_mpi,x_seq,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 141 ierr = VecGetArray(x_seq,&bptr);CHKERRQ(ierr); 142 } else { /* size==1 || distributed mat input */ 143 if (lu->options.SolveInitialized && !lu->matsolve_iscalled) { 144 /* see comments in MatMatSolve() */ 145 #if defined(PETSC_USE_COMPLEX) 146 PetscStackCall("SuperLU_DIST:zSolveFinalize",zSolveFinalize(&lu->options, &lu->SOLVEstruct)); 147 #else 148 PetscStackCall("SuperLU_DIST:dSolveFinalize",dSolveFinalize(&lu->options, &lu->SOLVEstruct)); 149 #endif 150 lu->options.SolveInitialized = NO; 151 } 152 ierr = VecCopy(b_mpi,x);CHKERRQ(ierr); 153 ierr = VecGetArray(x,&bptr);CHKERRQ(ierr); 154 } 155 156 if (lu->options.Fact != FACTORED) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"SuperLU_DIST options.Fact mush equal FACTORED"); 157 158 PetscStackCall("SuperLU_DIST:PStatInit",PStatInit(&stat)); /* Initialize the statistics variables. */ 159 if (lu->MatInputMode == GLOBAL) { 160 #if defined(PETSC_USE_COMPLEX) 161 PetscStackCall("SuperLU_DIST:pzgssvx_ABglobal",pzgssvx_ABglobal(&lu->options,&lu->A_sup,&lu->ScalePermstruct,(doublecomplex*)bptr,M,nrhs,&lu->grid,&lu->LUstruct,berr,&stat,&info)); 162 #else 163 PetscStackCall("SuperLU_DIST:pdgssvx_ABglobal",pdgssvx_ABglobal(&lu->options, &lu->A_sup, &lu->ScalePermstruct,bptr,M,nrhs,&lu->grid,&lu->LUstruct,berr,&stat,&info)); 164 #endif 165 } else { /* distributed mat input */ 166 #if defined(PETSC_USE_COMPLEX) 167 PetscStackCall("SuperLU_DIST:pzgssvx",pzgssvx(&lu->options,&lu->A_sup,&lu->ScalePermstruct,(doublecomplex*)bptr,m,nrhs,&lu->grid,&lu->LUstruct,&lu->SOLVEstruct,berr,&stat,&info)); 168 #else 169 PetscStackCall("SuperLU_DIST:pdgssvx",pdgssvx(&lu->options,&lu->A_sup,&lu->ScalePermstruct,bptr,m,nrhs,&lu->grid,&lu->LUstruct,&lu->SOLVEstruct,berr,&stat,&info)); 170 #endif 171 } 172 if (info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"pdgssvx fails, info: %d\n",info); 173 174 if (lu->options.PrintStat) PStatPrint(&lu->options, &stat, &lu->grid); /* Print the statistics. */ 175 PetscStackCall("SuperLU_DIST:PStatFree",PStatFree(&stat)); 176 177 if (size > 1 && lu->MatInputMode == GLOBAL) { 178 /* convert seq x to mpi x */ 179 ierr = VecRestoreArray(x_seq,&bptr);CHKERRQ(ierr); 180 ierr = VecScatterBegin(scat,x_seq,x,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 181 ierr = VecScatterEnd(scat,x_seq,x,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 182 ierr = VecScatterDestroy(&scat);CHKERRQ(ierr); 183 ierr = VecDestroy(&x_seq);CHKERRQ(ierr); 184 } else { 185 ierr = VecRestoreArray(x,&bptr);CHKERRQ(ierr); 186 187 lu->matsolve_iscalled = PETSC_TRUE; 188 lu->matmatsolve_iscalled = PETSC_FALSE; 189 } 190 PetscFunctionReturn(0); 191 } 192 193 static PetscErrorCode MatMatSolve_SuperLU_DIST(Mat A,Mat B_mpi,Mat X) 194 { 195 Mat_SuperLU_DIST *lu = (Mat_SuperLU_DIST*)A->data; 196 PetscErrorCode ierr; 197 PetscMPIInt size; 198 PetscInt M=A->rmap->N,m=A->rmap->n,nrhs; 199 SuperLUStat_t stat; 200 double berr[1]; 201 PetscScalar *bptr; 202 int info; /* SuperLU_Dist info code is ALWAYS an int, even with long long indices */ 203 PetscBool flg; 204 205 PetscFunctionBegin; 206 if (lu->options.Fact != FACTORED) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"SuperLU_DIST options.Fact mush equal FACTORED"); 207 ierr = PetscObjectTypeCompareAny((PetscObject)B_mpi,&flg,MATSEQDENSE,MATMPIDENSE,NULL);CHKERRQ(ierr); 208 if (!flg) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Matrix B must be MATDENSE matrix"); 209 ierr = PetscObjectTypeCompareAny((PetscObject)X,&flg,MATSEQDENSE,MATMPIDENSE,NULL);CHKERRQ(ierr); 210 if (!flg) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Matrix X must be MATDENSE matrix"); 211 212 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);CHKERRQ(ierr); 213 if (size > 1 && lu->MatInputMode == GLOBAL) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatInputMode=GLOBAL for nproc %d>1 is not supported",size); 214 /* size==1 or distributed mat input */ 215 if (lu->options.SolveInitialized && !lu->matmatsolve_iscalled) { 216 /* communication pattern of SOLVEstruct is unlikely created for matmatsolve, 217 thus destroy it and create a new SOLVEstruct. 218 Otherwise it may result in memory corruption or incorrect solution 219 See src/mat/examples/tests/ex125.c */ 220 #if defined(PETSC_USE_COMPLEX) 221 PetscStackCall("SuperLU_DIST:zSolveFinalize",zSolveFinalize(&lu->options, &lu->SOLVEstruct)); 222 #else 223 PetscStackCall("SuperLU_DIST:dSolveFinalize",dSolveFinalize(&lu->options, &lu->SOLVEstruct)); 224 #endif 225 lu->options.SolveInitialized = NO; 226 } 227 ierr = MatCopy(B_mpi,X,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 228 229 ierr = MatGetSize(B_mpi,NULL,&nrhs);CHKERRQ(ierr); 230 231 PetscStackCall("SuperLU_DIST:PStatInit",PStatInit(&stat)); /* Initialize the statistics variables. */ 232 ierr = MatDenseGetArray(X,&bptr);CHKERRQ(ierr); 233 if (lu->MatInputMode == GLOBAL) { /* size == 1 */ 234 #if defined(PETSC_USE_COMPLEX) 235 PetscStackCall("SuperLU_DIST:pzgssvx_ABglobal",pzgssvx_ABglobal(&lu->options, &lu->A_sup, &lu->ScalePermstruct,(doublecomplex*)bptr, M, nrhs,&lu->grid, &lu->LUstruct, berr, &stat, &info)); 236 #else 237 PetscStackCall("SuperLU_DIST:pdgssvx_ABglobal",pdgssvx_ABglobal(&lu->options, &lu->A_sup, &lu->ScalePermstruct,bptr, M, nrhs, &lu->grid, &lu->LUstruct, berr, &stat, &info)); 238 #endif 239 } else { /* distributed mat input */ 240 #if defined(PETSC_USE_COMPLEX) 241 PetscStackCall("SuperLU_DIST:pzgssvx",pzgssvx(&lu->options,&lu->A_sup,&lu->ScalePermstruct,(doublecomplex*)bptr,m,nrhs,&lu->grid, &lu->LUstruct,&lu->SOLVEstruct,berr,&stat,&info)); 242 #else 243 PetscStackCall("SuperLU_DIST:pdgssvx",pdgssvx(&lu->options,&lu->A_sup,&lu->ScalePermstruct,bptr,m,nrhs,&lu->grid,&lu->LUstruct,&lu->SOLVEstruct,berr,&stat,&info)); 244 #endif 245 } 246 if (info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"pdgssvx fails, info: %d\n",info); 247 ierr = MatDenseRestoreArray(X,&bptr);CHKERRQ(ierr); 248 249 if (lu->options.PrintStat) PStatPrint(&lu->options, &stat, &lu->grid); /* Print the statistics. */ 250 PetscStackCall("SuperLU_DIST:PStatFree",PStatFree(&stat)); 251 lu->matsolve_iscalled = PETSC_FALSE; 252 lu->matmatsolve_iscalled = PETSC_TRUE; 253 PetscFunctionReturn(0); 254 } 255 256 257 static PetscErrorCode MatLUFactorNumeric_SuperLU_DIST(Mat F,Mat A,const MatFactorInfo *info) 258 { 259 Mat *tseq,A_seq = NULL; 260 Mat_SeqAIJ *aa,*bb; 261 Mat_SuperLU_DIST *lu = (Mat_SuperLU_DIST*)(F)->data; 262 PetscErrorCode ierr; 263 PetscInt M=A->rmap->N,N=A->cmap->N,i,*ai,*aj,*bi,*bj,nz,rstart,*garray, 264 m=A->rmap->n, colA_start,j,jcol,jB,countA,countB,*bjj,*ajj; 265 int sinfo; /* SuperLU_Dist info flag is always an int even with long long indices */ 266 PetscMPIInt size; 267 SuperLUStat_t stat; 268 double *berr=0; 269 IS isrow; 270 #if defined(PETSC_USE_COMPLEX) 271 doublecomplex *av, *bv; 272 #else 273 double *av, *bv; 274 #endif 275 276 PetscFunctionBegin; 277 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);CHKERRQ(ierr); 278 279 if (lu->MatInputMode == GLOBAL) { /* global mat input */ 280 if (size > 1) { /* convert mpi A to seq mat A */ 281 ierr = ISCreateStride(PETSC_COMM_SELF,M,0,1,&isrow);CHKERRQ(ierr); 282 ierr = MatGetSubMatrices(A,1,&isrow,&isrow,MAT_INITIAL_MATRIX,&tseq);CHKERRQ(ierr); 283 ierr = ISDestroy(&isrow);CHKERRQ(ierr); 284 285 A_seq = *tseq; 286 ierr = PetscFree(tseq);CHKERRQ(ierr); 287 aa = (Mat_SeqAIJ*)A_seq->data; 288 } else { 289 PetscBool flg; 290 ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&flg);CHKERRQ(ierr); 291 if (flg) { 292 Mat_MPIAIJ *At = (Mat_MPIAIJ*)A->data; 293 A = At->A; 294 } 295 aa = (Mat_SeqAIJ*)A->data; 296 } 297 298 /* Convert Petsc NR matrix to SuperLU_DIST NC. 299 Note: memories of lu->val, col and row are allocated by CompRow_to_CompCol_dist()! */ 300 if (lu->options.Fact != DOFACT) {/* successive numeric factorization, sparsity pattern is reused. */ 301 PetscStackCall("SuperLU_DIST:Destroy_CompCol_Matrix_dist",Destroy_CompCol_Matrix_dist(&lu->A_sup)); 302 if (lu->FactPattern == SamePattern_SameRowPerm) { 303 lu->options.Fact = SamePattern_SameRowPerm; /* matrix has similar numerical values */ 304 } else { /* lu->FactPattern == SamePattern */ 305 PetscStackCall("SuperLU_DIST:Destroy_LU",Destroy_LU(N, &lu->grid, &lu->LUstruct)); 306 lu->options.Fact = SamePattern; 307 } 308 } 309 #if defined(PETSC_USE_COMPLEX) 310 PetscStackCall("SuperLU_DIST:zCompRow_to_CompCol_dist",zCompRow_to_CompCol_dist(M,N,aa->nz,(doublecomplex*)aa->a,(int_t*)aa->j,(int_t*)aa->i,&lu->val,&lu->col, &lu->row)); 311 #else 312 PetscStackCall("SuperLU_DIST:dCompRow_to_CompCol_dist",dCompRow_to_CompCol_dist(M,N,aa->nz,aa->a,(int_t*)aa->j,(int_t*)aa->i,&lu->val, &lu->col, &lu->row)); 313 #endif 314 315 /* Create compressed column matrix A_sup. */ 316 #if defined(PETSC_USE_COMPLEX) 317 PetscStackCall("SuperLU_DIST:zCreate_CompCol_Matrix_dist",zCreate_CompCol_Matrix_dist(&lu->A_sup, M, N, aa->nz, lu->val, lu->col, lu->row, SLU_NC, SLU_Z, SLU_GE)); 318 #else 319 PetscStackCall("SuperLU_DIST:dCreate_CompCol_Matrix_dist",dCreate_CompCol_Matrix_dist(&lu->A_sup, M, N, aa->nz, lu->val, lu->col, lu->row, SLU_NC, SLU_D, SLU_GE)); 320 #endif 321 } else { /* distributed mat input */ 322 Mat_MPIAIJ *mat = (Mat_MPIAIJ*)A->data; 323 aa=(Mat_SeqAIJ*)(mat->A)->data; 324 bb=(Mat_SeqAIJ*)(mat->B)->data; 325 ai=aa->i; aj=aa->j; 326 bi=bb->i; bj=bb->j; 327 #if defined(PETSC_USE_COMPLEX) 328 av=(doublecomplex*)aa->a; 329 bv=(doublecomplex*)bb->a; 330 #else 331 av=aa->a; 332 bv=bb->a; 333 #endif 334 rstart = A->rmap->rstart; 335 nz = aa->nz + bb->nz; 336 garray = mat->garray; 337 338 if (lu->options.Fact == DOFACT) { /* first numeric factorization */ 339 #if defined(PETSC_USE_COMPLEX) 340 PetscStackCall("SuperLU_DIST:zallocateA_dist",zallocateA_dist(m, nz, &lu->val, &lu->col, &lu->row)); 341 #else 342 PetscStackCall("SuperLU_DIST:dallocateA_dist",dallocateA_dist(m, nz, &lu->val, &lu->col, &lu->row)); 343 #endif 344 } else { /* successive numeric factorization, sparsity pattern and perm_c are reused. */ 345 if (lu->FactPattern == SamePattern_SameRowPerm) { 346 lu->options.Fact = SamePattern_SameRowPerm; /* matrix has similar numerical values */ 347 } else if (lu->FactPattern == SamePattern) { 348 PetscStackCall("SuperLU_DIST:Destroy_LU",Destroy_LU(N, &lu->grid, &lu->LUstruct)); /* Deallocate storage associated with the L and U matrices. */ 349 lu->options.Fact = SamePattern; 350 } else { 351 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"options.Fact must be one of SamePattern SamePattern_SameRowPerm"); 352 } 353 } 354 nz = 0; 355 for (i=0; i<m; i++) { 356 lu->row[i] = nz; 357 countA = ai[i+1] - ai[i]; 358 countB = bi[i+1] - bi[i]; 359 if (aj) { 360 ajj = aj + ai[i]; /* ptr to the beginning of this row */ 361 } else { 362 ajj = NULL; 363 } 364 bjj = bj + bi[i]; 365 366 /* B part, smaller col index */ 367 if (aj) { 368 colA_start = rstart + ajj[0]; /* the smallest global col index of A */ 369 } else { /* superlu_dist does not require matrix has diagonal entries, thus aj=NULL would work */ 370 colA_start = rstart; 371 } 372 jB = 0; 373 for (j=0; j<countB; j++) { 374 jcol = garray[bjj[j]]; 375 if (jcol > colA_start) { 376 jB = j; 377 break; 378 } 379 lu->col[nz] = jcol; 380 lu->val[nz++] = *bv++; 381 if (j==countB-1) jB = countB; 382 } 383 384 /* A part */ 385 for (j=0; j<countA; j++) { 386 lu->col[nz] = rstart + ajj[j]; 387 lu->val[nz++] = *av++; 388 } 389 390 /* B part, larger col index */ 391 for (j=jB; j<countB; j++) { 392 lu->col[nz] = garray[bjj[j]]; 393 lu->val[nz++] = *bv++; 394 } 395 } 396 lu->row[m] = nz; 397 398 if (lu->options.Fact == DOFACT) { 399 #if defined(PETSC_USE_COMPLEX) 400 PetscStackCall("SuperLU_DIST:zCreate_CompRowLoc_Matrix_dist",zCreate_CompRowLoc_Matrix_dist(&lu->A_sup, M, N, nz, m, rstart,lu->val, lu->col, lu->row, SLU_NR_loc, SLU_Z, SLU_GE)); 401 #else 402 PetscStackCall("SuperLU_DIST:dCreate_CompRowLoc_Matrix_dist",dCreate_CompRowLoc_Matrix_dist(&lu->A_sup, M, N, nz, m, rstart,lu->val, lu->col, lu->row, SLU_NR_loc, SLU_D, SLU_GE)); 403 #endif 404 } 405 } 406 407 /* Factor the matrix. */ 408 PetscStackCall("SuperLU_DIST:PStatInit",PStatInit(&stat)); /* Initialize the statistics variables. */ 409 if (lu->MatInputMode == GLOBAL) { /* global mat input */ 410 #if defined(PETSC_USE_COMPLEX) 411 PetscStackCall("SuperLU_DIST:pzgssvx_ABglobal",pzgssvx_ABglobal(&lu->options, &lu->A_sup, &lu->ScalePermstruct, 0, M, 0,&lu->grid, &lu->LUstruct, berr, &stat, &sinfo)); 412 #else 413 PetscStackCall("SuperLU_DIST:pdgssvx_ABglobal",pdgssvx_ABglobal(&lu->options, &lu->A_sup, &lu->ScalePermstruct, 0, M, 0,&lu->grid, &lu->LUstruct, berr, &stat, &sinfo)); 414 #endif 415 } else { /* distributed mat input */ 416 #if defined(PETSC_USE_COMPLEX) 417 PetscStackCall("SuperLU_DIST:pzgssvx",pzgssvx(&lu->options, &lu->A_sup, &lu->ScalePermstruct, 0, m, 0, &lu->grid,&lu->LUstruct, &lu->SOLVEstruct, berr, &stat, &sinfo)); 418 #else 419 PetscStackCall("SuperLU_DIST:pdgssvx",pdgssvx(&lu->options, &lu->A_sup, &lu->ScalePermstruct, 0, m, 0, &lu->grid,&lu->LUstruct, &lu->SOLVEstruct, berr, &stat, &sinfo)); 420 #endif 421 } 422 423 if (sinfo > 0) { 424 if (A->erroriffailure) { 425 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot in row %D",sinfo); 426 } else { 427 if (sinfo <= lu->A_sup.ncol) { 428 F->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 429 ierr = PetscInfo1(F,"U(i,i) is exactly zero, i= %D\n",sinfo);CHKERRQ(ierr); 430 } else if (sinfo > lu->A_sup.ncol) { 431 /* 432 number of bytes allocated when memory allocation 433 failure occurred, plus A->ncol. 434 */ 435 F->factorerrortype = MAT_FACTOR_OUTMEMORY; 436 ierr = PetscInfo1(F,"Number of bytes allocated when memory allocation fails %D\n",sinfo);CHKERRQ(ierr); 437 } 438 } 439 } else if (sinfo < 0) { 440 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB, "info = %D, argument in p*gssvx() had an illegal value", sinfo); 441 } 442 443 if (lu->MatInputMode == GLOBAL && size > 1) { 444 ierr = MatDestroy(&A_seq);CHKERRQ(ierr); 445 } 446 447 if (lu->options.PrintStat) { 448 PStatPrint(&lu->options, &stat, &lu->grid); /* Print the statistics. */ 449 } 450 PetscStackCall("SuperLU_DIST:PStatFree",PStatFree(&stat)); 451 (F)->assembled = PETSC_TRUE; 452 (F)->preallocated = PETSC_TRUE; 453 lu->options.Fact = FACTORED; /* The factored form of A is supplied. Local option used by this func. only */ 454 PetscFunctionReturn(0); 455 } 456 457 /* Note the Petsc r and c permutations are ignored */ 458 static PetscErrorCode MatLUFactorSymbolic_SuperLU_DIST(Mat F,Mat A,IS r,IS c,const MatFactorInfo *info) 459 { 460 Mat_SuperLU_DIST *lu = (Mat_SuperLU_DIST*)F->data; 461 PetscInt M = A->rmap->N,N=A->cmap->N; 462 463 PetscFunctionBegin; 464 /* Initialize the SuperLU process grid. */ 465 PetscStackCall("SuperLU_DIST:superlu_gridinit",superlu_gridinit(lu->comm_superlu, lu->nprow, lu->npcol, &lu->grid)); 466 467 /* Initialize ScalePermstruct and LUstruct. */ 468 PetscStackCall("SuperLU_DIST:ScalePermstructInit",ScalePermstructInit(M, N, &lu->ScalePermstruct)); 469 PetscStackCall("SuperLU_DIST:LUstructInit",LUstructInit(N, &lu->LUstruct)); 470 F->ops->lufactornumeric = MatLUFactorNumeric_SuperLU_DIST; 471 F->ops->solve = MatSolve_SuperLU_DIST; 472 F->ops->matsolve = MatMatSolve_SuperLU_DIST; 473 lu->CleanUpSuperLU_Dist = PETSC_TRUE; 474 PetscFunctionReturn(0); 475 } 476 477 static PetscErrorCode MatFactorGetSolverPackage_aij_superlu_dist(Mat A,const MatSolverPackage *type) 478 { 479 PetscFunctionBegin; 480 *type = MATSOLVERSUPERLU_DIST; 481 PetscFunctionReturn(0); 482 } 483 484 static PetscErrorCode MatFactorInfo_SuperLU_DIST(Mat A,PetscViewer viewer) 485 { 486 Mat_SuperLU_DIST *lu=(Mat_SuperLU_DIST*)A->data; 487 superlu_dist_options_t options; 488 PetscErrorCode ierr; 489 490 PetscFunctionBegin; 491 /* check if matrix is superlu_dist type */ 492 if (A->ops->solve != MatSolve_SuperLU_DIST) PetscFunctionReturn(0); 493 494 options = lu->options; 495 ierr = PetscViewerASCIIPrintf(viewer,"SuperLU_DIST run parameters:\n");CHKERRQ(ierr); 496 ierr = PetscViewerASCIIPrintf(viewer," Process grid nprow %D x npcol %D \n",lu->nprow,lu->npcol);CHKERRQ(ierr); 497 ierr = PetscViewerASCIIPrintf(viewer," Equilibrate matrix %s \n",PetscBools[options.Equil != NO]);CHKERRQ(ierr); 498 ierr = PetscViewerASCIIPrintf(viewer," Matrix input mode %d \n",lu->MatInputMode);CHKERRQ(ierr); 499 ierr = PetscViewerASCIIPrintf(viewer," Replace tiny pivots %s \n",PetscBools[options.ReplaceTinyPivot != NO]);CHKERRQ(ierr); 500 ierr = PetscViewerASCIIPrintf(viewer," Use iterative refinement %s \n",PetscBools[options.IterRefine == SLU_DOUBLE]);CHKERRQ(ierr); 501 ierr = PetscViewerASCIIPrintf(viewer," Processors in row %d col partition %d \n",lu->nprow,lu->npcol);CHKERRQ(ierr); 502 ierr = PetscViewerASCIIPrintf(viewer," Row permutation %s \n",(options.RowPerm == NOROWPERM) ? "NATURAL" : "LargeDiag");CHKERRQ(ierr); 503 504 switch (options.ColPerm) { 505 case NATURAL: 506 ierr = PetscViewerASCIIPrintf(viewer," Column permutation NATURAL\n");CHKERRQ(ierr); 507 break; 508 case MMD_AT_PLUS_A: 509 ierr = PetscViewerASCIIPrintf(viewer," Column permutation MMD_AT_PLUS_A\n");CHKERRQ(ierr); 510 break; 511 case MMD_ATA: 512 ierr = PetscViewerASCIIPrintf(viewer," Column permutation MMD_ATA\n");CHKERRQ(ierr); 513 break; 514 case METIS_AT_PLUS_A: 515 ierr = PetscViewerASCIIPrintf(viewer," Column permutation METIS_AT_PLUS_A\n");CHKERRQ(ierr); 516 break; 517 case PARMETIS: 518 ierr = PetscViewerASCIIPrintf(viewer," Column permutation PARMETIS\n");CHKERRQ(ierr); 519 break; 520 default: 521 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown column permutation"); 522 } 523 524 ierr = PetscViewerASCIIPrintf(viewer," Parallel symbolic factorization %s \n",PetscBools[options.ParSymbFact != NO]);CHKERRQ(ierr); 525 526 if (lu->FactPattern == SamePattern) { 527 ierr = PetscViewerASCIIPrintf(viewer," Repeated factorization SamePattern\n");CHKERRQ(ierr); 528 } else { 529 ierr = PetscViewerASCIIPrintf(viewer," Repeated factorization SamePattern_SameRowPerm\n");CHKERRQ(ierr); 530 } 531 PetscFunctionReturn(0); 532 } 533 534 static PetscErrorCode MatView_SuperLU_DIST(Mat A,PetscViewer viewer) 535 { 536 PetscErrorCode ierr; 537 PetscBool iascii; 538 PetscViewerFormat format; 539 540 PetscFunctionBegin; 541 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 542 if (iascii) { 543 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 544 if (format == PETSC_VIEWER_ASCII_INFO) { 545 ierr = MatFactorInfo_SuperLU_DIST(A,viewer);CHKERRQ(ierr); 546 } 547 } 548 PetscFunctionReturn(0); 549 } 550 551 static PetscErrorCode MatGetFactor_aij_superlu_dist(Mat A,MatFactorType ftype,Mat *F) 552 { 553 Mat B; 554 Mat_SuperLU_DIST *lu; 555 PetscErrorCode ierr; 556 PetscInt M=A->rmap->N,N=A->cmap->N,indx; 557 PetscMPIInt size; 558 superlu_dist_options_t options; 559 PetscBool flg; 560 const char *colperm[] = {"NATURAL","MMD_AT_PLUS_A","MMD_ATA","METIS_AT_PLUS_A","PARMETIS"}; 561 const char *rowperm[] = {"LargeDiag","NATURAL"}; 562 const char *factPattern[] = {"SamePattern","SamePattern_SameRowPerm"}; 563 PetscBool set; 564 565 PetscFunctionBegin; 566 /* Create the factorization matrix */ 567 ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr); 568 ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,M,N);CHKERRQ(ierr); 569 ierr = PetscStrallocpy("superlu_dist",&((PetscObject)B)->type_name);CHKERRQ(ierr); 570 ierr = MatSetUp(B);CHKERRQ(ierr); 571 B->ops->getinfo = MatGetInfo_External; 572 B->ops->lufactorsymbolic = MatLUFactorSymbolic_SuperLU_DIST; 573 B->ops->view = MatView_SuperLU_DIST; 574 B->ops->destroy = MatDestroy_SuperLU_DIST; 575 576 B->factortype = MAT_FACTOR_LU; 577 578 /* set solvertype */ 579 ierr = PetscFree(B->solvertype);CHKERRQ(ierr); 580 ierr = PetscStrallocpy(MATSOLVERSUPERLU_DIST,&B->solvertype);CHKERRQ(ierr); 581 582 ierr = PetscNewLog(B,&lu);CHKERRQ(ierr); 583 B->data = lu; 584 585 /* Set the default input options: 586 options.Fact = DOFACT; 587 options.Equil = YES; 588 options.ParSymbFact = NO; 589 options.ColPerm = METIS_AT_PLUS_A; 590 options.RowPerm = LargeDiag; 591 options.ReplaceTinyPivot = YES; 592 options.IterRefine = DOUBLE; 593 options.Trans = NOTRANS; 594 options.SolveInitialized = NO; -hold the communication pattern used MatSolve() and MatMatSolve() 595 options.RefineInitialized = NO; 596 options.PrintStat = YES; 597 */ 598 set_default_options_dist(&options); 599 600 ierr = MPI_Comm_dup(PetscObjectComm((PetscObject)A),&(lu->comm_superlu));CHKERRQ(ierr); 601 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);CHKERRQ(ierr); 602 /* Default num of process columns and rows */ 603 lu->npcol = (int_t) (0.5 + PetscSqrtReal((PetscReal)size)); 604 if (!lu->npcol) lu->npcol = 1; 605 while (lu->npcol > 0) { 606 lu->nprow = (int_t) (size/lu->npcol); 607 if (size == lu->nprow * lu->npcol) break; 608 lu->npcol--; 609 } 610 611 ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"SuperLU_Dist Options","Mat");CHKERRQ(ierr); 612 ierr = PetscOptionsInt("-mat_superlu_dist_r","Number rows in processor partition","None",lu->nprow,(PetscInt*)&lu->nprow,NULL);CHKERRQ(ierr); 613 ierr = PetscOptionsInt("-mat_superlu_dist_c","Number columns in processor partition","None",lu->npcol,(PetscInt*)&lu->npcol,NULL);CHKERRQ(ierr); 614 if (size != lu->nprow * lu->npcol) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number of processes %d must equal to nprow %d * npcol %d",size,lu->nprow,lu->npcol); 615 616 lu->MatInputMode = DISTRIBUTED; 617 618 ierr = PetscOptionsEnum("-mat_superlu_dist_matinput","Matrix input mode (global or distributed)","None",SuperLU_MatInputModes,(PetscEnum)lu->MatInputMode,(PetscEnum*)&lu->MatInputMode,NULL);CHKERRQ(ierr); 619 if (lu->MatInputMode == DISTRIBUTED && size == 1) lu->MatInputMode = GLOBAL; 620 621 ierr = PetscOptionsBool("-mat_superlu_dist_equil","Equilibrate matrix","None",options.Equil ? PETSC_TRUE : PETSC_FALSE,&flg,&set);CHKERRQ(ierr); 622 if (set && !flg) options.Equil = NO; 623 624 ierr = PetscOptionsEList("-mat_superlu_dist_rowperm","Row permutation","None",rowperm,2,rowperm[0],&indx,&flg);CHKERRQ(ierr); 625 if (flg) { 626 switch (indx) { 627 case 0: 628 options.RowPerm = LargeDiag; 629 break; 630 case 1: 631 options.RowPerm = NOROWPERM; 632 break; 633 } 634 } 635 636 ierr = PetscOptionsEList("-mat_superlu_dist_colperm","Column permutation","None",colperm,5,colperm[3],&indx,&flg);CHKERRQ(ierr); 637 if (flg) { 638 switch (indx) { 639 case 0: 640 options.ColPerm = NATURAL; 641 break; 642 case 1: 643 options.ColPerm = MMD_AT_PLUS_A; 644 break; 645 case 2: 646 options.ColPerm = MMD_ATA; 647 break; 648 case 3: 649 options.ColPerm = METIS_AT_PLUS_A; 650 break; 651 case 4: 652 options.ColPerm = PARMETIS; /* only works for np>1 */ 653 break; 654 default: 655 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown column permutation"); 656 } 657 } 658 659 options.ReplaceTinyPivot = NO; 660 ierr = PetscOptionsBool("-mat_superlu_dist_replacetinypivot","Replace tiny pivots","None",options.ReplaceTinyPivot ? PETSC_TRUE : PETSC_FALSE,&flg,&set);CHKERRQ(ierr); 661 if (set && flg) options.ReplaceTinyPivot = YES; 662 663 options.ParSymbFact = NO; 664 ierr = PetscOptionsBool("-mat_superlu_dist_parsymbfact","Parallel symbolic factorization","None",PETSC_FALSE,&flg,&set);CHKERRQ(ierr); 665 if (set && flg && size>1) { 666 if (lu->MatInputMode == GLOBAL) { 667 #if defined(PETSC_USE_INFO) 668 ierr = PetscInfo(A,"Warning: '-mat_superlu_dist_parsymbfact' is ignored because MatInputMode=GLOBAL\n");CHKERRQ(ierr); 669 #endif 670 } else { 671 #if defined(PETSC_HAVE_PARMETIS) 672 options.ParSymbFact = YES; 673 options.ColPerm = PARMETIS; /* in v2.2, PARMETIS is forced for ParSymbFact regardless of user ordering setting */ 674 #else 675 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"parsymbfact needs PARMETIS"); 676 #endif 677 } 678 } 679 680 lu->FactPattern = SamePattern_SameRowPerm; 681 ierr = PetscOptionsEList("-mat_superlu_dist_fact","Sparsity pattern for repeated matrix factorization","None",factPattern,2,factPattern[0],&indx,&flg);CHKERRQ(ierr); 682 if (flg) { 683 switch (indx) { 684 case 0: 685 lu->FactPattern = SamePattern; 686 break; 687 case 1: 688 lu->FactPattern = SamePattern_SameRowPerm; 689 break; 690 } 691 } 692 693 options.IterRefine = NOREFINE; 694 ierr = PetscOptionsBool("-mat_superlu_dist_iterrefine","Use iterative refinement","None",options.IterRefine == NOREFINE ? PETSC_FALSE : PETSC_TRUE ,&flg,&set);CHKERRQ(ierr); 695 if (set) { 696 if (flg) options.IterRefine = SLU_DOUBLE; 697 else options.IterRefine = NOREFINE; 698 } 699 700 if (PetscLogPrintInfo) options.PrintStat = YES; 701 else options.PrintStat = NO; 702 ierr = PetscOptionsBool("-mat_superlu_dist_statprint","Print factorization information","None",(PetscBool)options.PrintStat,(PetscBool*)&options.PrintStat,NULL);CHKERRQ(ierr); 703 ierr = PetscOptionsEnd();CHKERRQ(ierr); 704 705 lu->options = options; 706 lu->options.Fact = DOFACT; 707 lu->matsolve_iscalled = PETSC_FALSE; 708 lu->matmatsolve_iscalled = PETSC_FALSE; 709 710 ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverPackage_C",MatFactorGetSolverPackage_aij_superlu_dist);CHKERRQ(ierr); 711 ierr = PetscObjectComposeFunction((PetscObject)B,"MatSuperluDistGetDiagU_C",MatSuperluDistGetDiagU_SuperLU_DIST);CHKERRQ(ierr); 712 713 *F = B; 714 PetscFunctionReturn(0); 715 } 716 717 PETSC_EXTERN PetscErrorCode MatSolverPackageRegister_SuperLU_DIST(void) 718 { 719 PetscErrorCode ierr; 720 PetscFunctionBegin; 721 ierr = MatSolverPackageRegister(MATSOLVERSUPERLU_DIST,MATMPIAIJ, MAT_FACTOR_LU,MatGetFactor_aij_superlu_dist);CHKERRQ(ierr); 722 ierr = MatSolverPackageRegister(MATSOLVERSUPERLU_DIST,MATSEQAIJ, MAT_FACTOR_LU,MatGetFactor_aij_superlu_dist);CHKERRQ(ierr); 723 PetscFunctionReturn(0); 724 } 725 726 /*MC 727 MATSOLVERSUPERLU_DIST - Parallel direct solver package for LU factorization 728 729 Use ./configure --download-superlu_dist --download-parmetis --download-metis --download-ptscotch to have PETSc installed with SuperLU_DIST 730 731 Use -pc_type lu -pc_factor_mat_solver_package superlu_dist to us this direct solver 732 733 Works with AIJ matrices 734 735 Options Database Keys: 736 + -mat_superlu_dist_r <n> - number of rows in processor partition 737 . -mat_superlu_dist_c <n> - number of columns in processor partition 738 . -mat_superlu_dist_matinput <0,1> - matrix input mode; 0=global, 1=distributed 739 . -mat_superlu_dist_equil - equilibrate the matrix 740 . -mat_superlu_dist_rowperm <LargeDiag,NATURAL> - row permutation 741 . -mat_superlu_dist_colperm <MMD_AT_PLUS_A,MMD_ATA,NATURAL> - column permutation 742 . -mat_superlu_dist_replacetinypivot - replace tiny pivots 743 . -mat_superlu_dist_fact <SamePattern> - (choose one of) SamePattern SamePattern_SameRowPerm 744 . -mat_superlu_dist_iterrefine - use iterative refinement 745 - -mat_superlu_dist_statprint - print factorization information 746 747 Level: beginner 748 749 .seealso: PCLU 750 751 .seealso: PCFactorSetMatSolverPackage(), MatSolverPackage 752 753 M*/ 754 755