1 #define PETSCMAT_DLL 2 3 4 #include "../src/mat/impls/aij/seq/aij.h" 5 #include "../src/mat/impls/sbaij/seq/sbaij.h" 6 #include "petscbt.h" 7 #include "../src/mat/utils/freespace.h" 8 9 EXTERN_C_BEGIN 10 #undef __FUNCT__ 11 #define __FUNCT__ "MatOrdering_Flow_SeqAIJ" 12 /* 13 Computes an ordering to get most of the large numerical values in the lower triangular part of the matrix 14 */ 15 PetscErrorCode MatOrdering_Flow_SeqAIJ(Mat mat,const MatOrderingType type,IS *irow,IS *icol) 16 { 17 Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->data; 18 PetscErrorCode ierr; 19 PetscInt i,j,jj,k, kk,n = mat->rmap->n, current = 0, newcurrent = 0,*order; 20 const PetscInt *ai = a->i, *aj = a->j; 21 const PetscScalar *aa = a->a; 22 PetscTruth *done; 23 PetscReal best,past = 0,future; 24 25 PetscFunctionBegin; 26 /* pick initial row */ 27 best = -1; 28 for (i=0; i<n; i++) { 29 future = 0.0; 30 for (j=ai[i]; j<ai[i+1]; j++) { 31 if (aj[j] != i) future += PetscAbsScalar(aa[j]); else past = PetscAbsScalar(aa[j]); 32 } 33 if (!future) future = 1.e-10; /* if there is zero in the upper diagonal part want to rank this row high */ 34 if (past/future > best) { 35 best = past/future; 36 current = i; 37 } 38 } 39 40 ierr = PetscMalloc(n*sizeof(PetscTruth),&done);CHKERRQ(ierr); 41 ierr = PetscMemzero(done,n*sizeof(PetscTruth));CHKERRQ(ierr); 42 ierr = PetscMalloc(n*sizeof(PetscInt),&order);CHKERRQ(ierr); 43 order[0] = current; 44 for (i=0; i<n-1; i++) { 45 done[current] = PETSC_TRUE; 46 best = -1; 47 /* loop over all neighbors of current pivot */ 48 for (j=ai[current]; j<ai[current+1]; j++) { 49 jj = aj[j]; 50 if (done[jj]) continue; 51 /* loop over columns of potential next row computing weights for below and above diagonal */ 52 past = future = 0.0; 53 for (k=ai[jj]; k<ai[jj+1]; k++) { 54 kk = aj[k]; 55 if (done[kk]) past += PetscAbsScalar(aa[k]); 56 else if (kk != jj) future += PetscAbsScalar(aa[k]); 57 } 58 if (!future) future = 1.e-10; /* if there is zero in the upper diagonal part want to rank this row high */ 59 if (past/future > best) { 60 best = past/future; 61 newcurrent = jj; 62 } 63 } 64 if (best == -1) { /* no neighbors to select from so select best of all that remain */ 65 best = -1; 66 for (k=0; k<n; k++) { 67 if (done[k]) continue; 68 future = 0.0; 69 past = 0.0; 70 for (j=ai[k]; j<ai[k+1]; j++) { 71 kk = aj[j]; 72 if (done[kk]) past += PetscAbsScalar(aa[j]); 73 else if (kk != k) future += PetscAbsScalar(aa[j]); 74 } 75 if (!future) future = 1.e-10; /* if there is zero in the upper diagonal part want to rank this row high */ 76 if (past/future > best) { 77 best = past/future; 78 newcurrent = k; 79 } 80 } 81 } 82 if (current == newcurrent) SETERRQ(PETSC_ERR_PLIB,"newcurrent cannot be current"); 83 current = newcurrent; 84 order[i+1] = current; 85 } 86 ierr = ISCreateGeneral(PETSC_COMM_SELF,n,order,irow);CHKERRQ(ierr); 87 *icol = *irow; 88 ierr = PetscObjectReference((PetscObject)*irow);CHKERRQ(ierr); 89 ierr = PetscFree(done);CHKERRQ(ierr); 90 ierr = PetscFree(order);CHKERRQ(ierr); 91 PetscFunctionReturn(0); 92 } 93 EXTERN_C_END 94 95 EXTERN_C_BEGIN 96 #undef __FUNCT__ 97 #define __FUNCT__ "MatGetFactorAvailable_seqaij_petsc" 98 PetscErrorCode MatGetFactorAvailable_seqaij_petsc(Mat A,MatFactorType ftype,PetscTruth *flg) 99 { 100 PetscFunctionBegin; 101 *flg = PETSC_TRUE; 102 PetscFunctionReturn(0); 103 } 104 EXTERN_C_END 105 106 EXTERN_C_BEGIN 107 #undef __FUNCT__ 108 #define __FUNCT__ "MatGetFactor_seqaij_petsc" 109 PetscErrorCode MatGetFactor_seqaij_petsc(Mat A,MatFactorType ftype,Mat *B) 110 { 111 PetscInt n = A->rmap->n; 112 PetscErrorCode ierr; 113 114 PetscFunctionBegin; 115 ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr); 116 ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr); 117 if (ftype == MAT_FACTOR_LU || ftype == MAT_FACTOR_ILU || ftype == MAT_FACTOR_ILUDT){ 118 ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr); 119 (*B)->ops->ilufactorsymbolic = MatILUFactorSymbolic_SeqAIJ; 120 (*B)->ops->lufactorsymbolic = MatLUFactorSymbolic_SeqAIJ; 121 } else if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) { 122 ierr = MatSetType(*B,MATSEQSBAIJ);CHKERRQ(ierr); 123 ierr = MatSeqSBAIJSetPreallocation(*B,1,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr); 124 (*B)->ops->iccfactorsymbolic = MatICCFactorSymbolic_SeqAIJ; 125 (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqAIJ; 126 } else SETERRQ(PETSC_ERR_SUP,"Factor type not supported"); 127 (*B)->factor = ftype; 128 PetscFunctionReturn(0); 129 } 130 EXTERN_C_END 131 132 #undef __FUNCT__ 133 #define __FUNCT__ "MatLUFactorSymbolic_SeqAIJ_inplace" 134 PetscErrorCode MatLUFactorSymbolic_SeqAIJ_inplace(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 135 { 136 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b; 137 IS isicol; 138 PetscErrorCode ierr; 139 const PetscInt *r,*ic; 140 PetscInt i,n=A->rmap->n,*ai=a->i,*aj=a->j; 141 PetscInt *bi,*bj,*ajtmp; 142 PetscInt *bdiag,row,nnz,nzi,reallocs=0,nzbd,*im; 143 PetscReal f; 144 PetscInt nlnk,*lnk,k,**bi_ptr; 145 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 146 PetscBT lnkbt; 147 148 PetscFunctionBegin; 149 if (A->rmap->N != A->cmap->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square"); 150 ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 151 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 152 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 153 154 /* get new row pointers */ 155 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 156 bi[0] = 0; 157 158 /* bdiag is location of diagonal in factor */ 159 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr); 160 bdiag[0] = 0; 161 162 /* linked list for storing column indices of the active row */ 163 nlnk = n + 1; 164 ierr = PetscLLCreate(n,n,nlnk,lnk,lnkbt);CHKERRQ(ierr); 165 166 ierr = PetscMalloc2(n+1,PetscInt**,&bi_ptr,n+1,PetscInt,&im);CHKERRQ(ierr); 167 168 /* initial FreeSpace size is f*(ai[n]+1) */ 169 f = info->fill; 170 ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space);CHKERRQ(ierr); 171 current_space = free_space; 172 173 for (i=0; i<n; i++) { 174 /* copy previous fill into linked list */ 175 nzi = 0; 176 nnz = ai[r[i]+1] - ai[r[i]]; 177 if (!nnz) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i); 178 ajtmp = aj + ai[r[i]]; 179 ierr = PetscLLAddPerm(nnz,ajtmp,ic,n,nlnk,lnk,lnkbt);CHKERRQ(ierr); 180 nzi += nlnk; 181 182 /* add pivot rows into linked list */ 183 row = lnk[n]; 184 while (row < i) { 185 nzbd = bdiag[row] - bi[row] + 1; /* num of entries in the row with column index <= row */ 186 ajtmp = bi_ptr[row] + nzbd; /* points to the entry next to the diagonal */ 187 ierr = PetscLLAddSortedLU(ajtmp,row,nlnk,lnk,lnkbt,i,nzbd,im);CHKERRQ(ierr); 188 nzi += nlnk; 189 row = lnk[row]; 190 } 191 bi[i+1] = bi[i] + nzi; 192 im[i] = nzi; 193 194 /* mark bdiag */ 195 nzbd = 0; 196 nnz = nzi; 197 k = lnk[n]; 198 while (nnz-- && k < i){ 199 nzbd++; 200 k = lnk[k]; 201 } 202 bdiag[i] = bi[i] + nzbd; 203 204 /* if free space is not available, make more free space */ 205 if (current_space->local_remaining<nzi) { 206 nnz = (n - i)*nzi; /* estimated and max additional space needed */ 207 ierr = PetscFreeSpaceGet(nnz,¤t_space);CHKERRQ(ierr); 208 reallocs++; 209 } 210 211 /* copy data into free space, then initialize lnk */ 212 ierr = PetscLLClean(n,n,nzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 213 bi_ptr[i] = current_space->array; 214 current_space->array += nzi; 215 current_space->local_used += nzi; 216 current_space->local_remaining -= nzi; 217 } 218 #if defined(PETSC_USE_INFO) 219 if (ai[n] != 0) { 220 PetscReal af = ((PetscReal)bi[n])/((PetscReal)ai[n]); 221 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,f,af);CHKERRQ(ierr); 222 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 223 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G);\n",af);CHKERRQ(ierr); 224 ierr = PetscInfo(A,"for best performance.\n");CHKERRQ(ierr); 225 } else { 226 ierr = PetscInfo(A,"Empty matrix\n");CHKERRQ(ierr); 227 } 228 #endif 229 230 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 231 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 232 233 /* destroy list of free space and other temporary array(s) */ 234 ierr = PetscMalloc((bi[n]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 235 ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr); 236 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 237 ierr = PetscFree2(bi_ptr,im);CHKERRQ(ierr); 238 239 /* put together the new matrix */ 240 ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr); 241 ierr = PetscLogObjectParent(B,isicol);CHKERRQ(ierr); 242 b = (Mat_SeqAIJ*)(B)->data; 243 b->free_a = PETSC_TRUE; 244 b->free_ij = PETSC_TRUE; 245 b->singlemalloc = PETSC_FALSE; 246 ierr = PetscMalloc((bi[n]+1)*sizeof(PetscScalar),&b->a);CHKERRQ(ierr); 247 b->j = bj; 248 b->i = bi; 249 b->diag = bdiag; 250 b->ilen = 0; 251 b->imax = 0; 252 b->row = isrow; 253 b->col = iscol; 254 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 255 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 256 b->icol = isicol; 257 ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 258 259 /* In b structure: Free imax, ilen, old a, old j. Allocate solve_work, new a, new j */ 260 ierr = PetscLogObjectMemory(B,(bi[n]-n)*(sizeof(PetscInt)+sizeof(PetscScalar)));CHKERRQ(ierr); 261 b->maxnz = b->nz = bi[n] ; 262 263 (B)->factor = MAT_FACTOR_LU; 264 (B)->info.factor_mallocs = reallocs; 265 (B)->info.fill_ratio_given = f; 266 267 if (ai[n]) { 268 (B)->info.fill_ratio_needed = ((PetscReal)bi[n])/((PetscReal)ai[n]); 269 } else { 270 (B)->info.fill_ratio_needed = 0.0; 271 } 272 (B)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ_inplace; 273 (B)->ops->solve = MatSolve_SeqAIJ_inplace; 274 (B)->ops->solvetranspose = MatSolveTranspose_SeqAIJ_inplace; 275 /* switch to inodes if appropriate */ 276 ierr = MatLUFactorSymbolic_SeqAIJ_Inode(B,A,isrow,iscol,info);CHKERRQ(ierr); 277 PetscFunctionReturn(0); 278 } 279 280 #undef __FUNCT__ 281 #define __FUNCT__ "MatLUFactorSymbolic_SeqAIJ" 282 PetscErrorCode MatLUFactorSymbolic_SeqAIJ(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 283 { 284 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b; 285 IS isicol; 286 PetscErrorCode ierr; 287 const PetscInt *r,*ic; 288 PetscInt i,n=A->rmap->n,*ai=a->i,*aj=a->j; 289 PetscInt *bi,*bj,*ajtmp; 290 PetscInt *bdiag,row,nnz,nzi,reallocs=0,nzbd,*im; 291 PetscReal f; 292 PetscInt nlnk,*lnk,k,**bi_ptr; 293 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 294 PetscBT lnkbt; 295 296 PetscFunctionBegin; 297 if (A->rmap->N != A->cmap->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square"); 298 ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 299 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 300 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 301 302 /* get new row and diagonal pointers, must be allocated separately because they will be given to the Mat_SeqAIJ and freed separately */ 303 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 304 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr); 305 bi[0] = bdiag[0] = 0; 306 307 /* linked list for storing column indices of the active row */ 308 nlnk = n + 1; 309 ierr = PetscLLCreate(n,n,nlnk,lnk,lnkbt);CHKERRQ(ierr); 310 311 ierr = PetscMalloc2(n+1,PetscInt**,&bi_ptr,n+1,PetscInt,&im);CHKERRQ(ierr); 312 313 /* initial FreeSpace size is f*(ai[n]+1) */ 314 f = info->fill; 315 ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space);CHKERRQ(ierr); 316 current_space = free_space; 317 318 for (i=0; i<n; i++) { 319 /* copy previous fill into linked list */ 320 nzi = 0; 321 nnz = ai[r[i]+1] - ai[r[i]]; 322 if (!nnz) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i); 323 ajtmp = aj + ai[r[i]]; 324 ierr = PetscLLAddPerm(nnz,ajtmp,ic,n,nlnk,lnk,lnkbt);CHKERRQ(ierr); 325 nzi += nlnk; 326 327 /* add pivot rows into linked list */ 328 row = lnk[n]; 329 while (row < i){ 330 nzbd = bdiag[row] + 1; /* num of entries in the row with column index <= row */ 331 ajtmp = bi_ptr[row] + nzbd; /* points to the entry next to the diagonal */ 332 ierr = PetscLLAddSortedLU(ajtmp,row,nlnk,lnk,lnkbt,i,nzbd,im);CHKERRQ(ierr); 333 nzi += nlnk; 334 row = lnk[row]; 335 } 336 bi[i+1] = bi[i] + nzi; 337 im[i] = nzi; 338 339 /* mark bdiag */ 340 nzbd = 0; 341 nnz = nzi; 342 k = lnk[n]; 343 while (nnz-- && k < i){ 344 nzbd++; 345 k = lnk[k]; 346 } 347 bdiag[i] = nzbd; /* note: bdiag[i] = nnzL as input for PetscFreeSpaceContiguous_LU() */ 348 349 /* if free space is not available, make more free space */ 350 if (current_space->local_remaining<nzi) { 351 nnz = 2*(n - i)*nzi; /* estimated and max additional space needed */ 352 ierr = PetscFreeSpaceGet(nnz,¤t_space);CHKERRQ(ierr); 353 reallocs++; 354 } 355 356 /* copy data into free space, then initialize lnk */ 357 ierr = PetscLLClean(n,n,nzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 358 bi_ptr[i] = current_space->array; 359 current_space->array += nzi; 360 current_space->local_used += nzi; 361 current_space->local_remaining -= nzi; 362 } 363 #if defined(PETSC_USE_INFO) 364 if (ai[n] != 0) { 365 PetscReal af = ((PetscReal)bi[n])/((PetscReal)ai[n]); 366 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,f,af);CHKERRQ(ierr); 367 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 368 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G);\n",af);CHKERRQ(ierr); 369 ierr = PetscInfo(A,"for best performance.\n");CHKERRQ(ierr); 370 } else { 371 ierr = PetscInfo(A,"Empty matrix\n");CHKERRQ(ierr); 372 } 373 #endif 374 375 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 376 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 377 378 /* destroy list of free space and other temporary array(s) */ 379 ierr = PetscMalloc((bi[n]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 380 ierr = PetscFreeSpaceContiguous_LU(&free_space,bj,n,bi,bdiag);CHKERRQ(ierr); 381 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 382 ierr = PetscFree2(bi_ptr,im);CHKERRQ(ierr); 383 384 /* put together the new matrix */ 385 ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr); 386 ierr = PetscLogObjectParent(B,isicol);CHKERRQ(ierr); 387 b = (Mat_SeqAIJ*)(B)->data; 388 b->free_a = PETSC_TRUE; 389 b->free_ij = PETSC_TRUE; 390 b->singlemalloc = PETSC_FALSE; 391 ierr = PetscMalloc((bdiag[0]+1)*sizeof(PetscScalar),&b->a);CHKERRQ(ierr); 392 b->j = bj; 393 b->i = bi; 394 b->diag = bdiag; 395 b->ilen = 0; 396 b->imax = 0; 397 b->row = isrow; 398 b->col = iscol; 399 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 400 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 401 b->icol = isicol; 402 ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 403 404 /* In b structure: Free imax, ilen, old a, old j. Allocate solve_work, new a, new j */ 405 ierr = PetscLogObjectMemory(B,(bdiag[0]+1)*(sizeof(PetscInt)+sizeof(PetscScalar)));CHKERRQ(ierr); 406 b->maxnz = b->nz = bdiag[0]+1; 407 B->factor = MAT_FACTOR_LU; 408 B->info.factor_mallocs = reallocs; 409 B->info.fill_ratio_given = f; 410 411 if (ai[n]) { 412 B->info.fill_ratio_needed = ((PetscReal)(bdiag[0]+1))/((PetscReal)ai[n]); 413 } else { 414 B->info.fill_ratio_needed = 0.0; 415 } 416 B->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ; 417 /* switch to inodes if appropriate */ 418 ierr = Mat_CheckInode_FactorLU(B,PETSC_FALSE);CHKERRQ(ierr); 419 PetscFunctionReturn(0); 420 } 421 422 /* 423 Trouble in factorization, should we dump the original matrix? 424 */ 425 #undef __FUNCT__ 426 #define __FUNCT__ "MatFactorDumpMatrix" 427 PetscErrorCode MatFactorDumpMatrix(Mat A) 428 { 429 PetscErrorCode ierr; 430 PetscTruth flg = PETSC_FALSE; 431 432 PetscFunctionBegin; 433 ierr = PetscOptionsGetTruth(PETSC_NULL,"-mat_factor_dump_on_error",&flg,PETSC_NULL);CHKERRQ(ierr); 434 if (flg) { 435 PetscViewer viewer; 436 char filename[PETSC_MAX_PATH_LEN]; 437 438 ierr = PetscSNPrintf(filename,PETSC_MAX_PATH_LEN,"matrix_factor_error.%d",PetscGlobalRank);CHKERRQ(ierr); 439 ierr = PetscViewerBinaryOpen(((PetscObject)A)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 440 ierr = MatView(A,viewer);CHKERRQ(ierr); 441 ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); 442 } 443 PetscFunctionReturn(0); 444 } 445 446 #undef __FUNCT__ 447 #define __FUNCT__ "MatLUFactorNumeric_SeqAIJ" 448 PetscErrorCode MatLUFactorNumeric_SeqAIJ(Mat B,Mat A,const MatFactorInfo *info) 449 { 450 Mat C=B; 451 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ *)C->data; 452 IS isrow = b->row,isicol = b->icol; 453 PetscErrorCode ierr; 454 const PetscInt *r,*ic,*ics; 455 const PetscInt n=A->rmap->n,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*bdiag=b->diag; 456 PetscInt i,j,k,nz,nzL,row,*pj; 457 const PetscInt *ajtmp,*bjtmp; 458 MatScalar *rtmp,*pc,multiplier,*pv; 459 const MatScalar *aa=a->a,*v; 460 PetscTruth row_identity,col_identity; 461 462 FactorShiftCtx sctx; 463 PetscInt *ddiag; 464 PetscReal rs; 465 MatScalar d; 466 467 PetscFunctionBegin; 468 /* MatPivotSetUp(): initialize shift context sctx */ 469 ierr = PetscMemzero(&sctx,sizeof(FactorShiftCtx));CHKERRQ(ierr); 470 471 /* if both shift schemes are chosen by user, only use info->shiftpd */ 472 if (info->shiftpd) { /* set sctx.shift_top=max{rs} */ 473 ddiag = a->diag; 474 sctx.shift_top = info->zeropivot; 475 for (i=0; i<n; i++) { 476 /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */ 477 d = (aa)[ddiag[i]]; 478 rs = -PetscAbsScalar(d) - PetscRealPart(d); 479 v = aa+ai[i]; 480 nz = ai[i+1] - ai[i]; 481 for (j=0; j<nz; j++) 482 rs += PetscAbsScalar(v[j]); 483 if (rs>sctx.shift_top) sctx.shift_top = rs; 484 } 485 sctx.shift_top *= 1.1; 486 sctx.nshift_max = 5; 487 sctx.shift_lo = 0.; 488 sctx.shift_hi = 1.; 489 } 490 491 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 492 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 493 ierr = PetscMalloc((n+1)*sizeof(MatScalar),&rtmp);CHKERRQ(ierr); 494 ics = ic; 495 496 do { 497 sctx.useshift = PETSC_FALSE; 498 for (i=0; i<n; i++){ 499 /* zero rtmp */ 500 /* L part */ 501 nz = bi[i+1] - bi[i]; 502 bjtmp = bj + bi[i]; 503 for (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0; 504 505 /* U part */ 506 nz = bdiag[i]-bdiag[i+1]; 507 bjtmp = bj + bdiag[i+1]+1; 508 for (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0; 509 510 /* load in initial (unfactored row) */ 511 nz = ai[r[i]+1] - ai[r[i]]; 512 ajtmp = aj + ai[r[i]]; 513 v = aa + ai[r[i]]; 514 for (j=0; j<nz; j++) { 515 rtmp[ics[ajtmp[j]]] = v[j]; 516 } 517 /* ZeropivotApply() */ 518 rtmp[i] += sctx.shift_amount; /* shift the diagonal of the matrix */ 519 520 /* elimination */ 521 bjtmp = bj + bi[i]; 522 row = *bjtmp++; 523 nzL = bi[i+1] - bi[i]; 524 for(k=0; k < nzL;k++) { 525 pc = rtmp + row; 526 if (*pc != 0.0) { 527 pv = b->a + bdiag[row]; 528 multiplier = *pc * (*pv); 529 *pc = multiplier; 530 pj = b->j + bdiag[row+1]+1; /* beginning of U(row,:) */ 531 pv = b->a + bdiag[row+1]+1; 532 nz = bdiag[row]-bdiag[row+1]-1; /* num of entries in U(row,:) excluding diag */ 533 for (j=0; j<nz; j++) rtmp[pj[j]] -= multiplier * pv[j]; 534 ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr); 535 } 536 row = *bjtmp++; 537 } 538 539 /* finished row so stick it into b->a */ 540 rs = 0.0; 541 /* L part */ 542 pv = b->a + bi[i] ; 543 pj = b->j + bi[i] ; 544 nz = bi[i+1] - bi[i]; 545 for (j=0; j<nz; j++) { 546 pv[j] = rtmp[pj[j]]; rs += PetscAbsScalar(pv[j]); 547 } 548 549 /* U part */ 550 pv = b->a + bdiag[i+1]+1; 551 pj = b->j + bdiag[i+1]+1; 552 nz = bdiag[i] - bdiag[i+1]-1; 553 for (j=0; j<nz; j++) { 554 pv[j] = rtmp[pj[j]]; rs += PetscAbsScalar(pv[j]); 555 } 556 557 /* MatPivotCheck() */ 558 sctx.rs = rs; 559 sctx.pv = rtmp[i]; 560 if (info->shiftnz){ 561 ierr = MatPivotCheck_nz(info,sctx,i);CHKERRQ(ierr); 562 } else if (info->shiftpd){ 563 ierr = MatPivotCheck_pd(info,sctx,i);CHKERRQ(ierr); 564 } else if (info->shiftinblocks){ 565 ierr = MatPivotCheck_inblocks(info,sctx,i);CHKERRQ(ierr); 566 } else { 567 ierr = MatPivotCheck_none(info,sctx,i);CHKERRQ(ierr); 568 } 569 rtmp[i] = sctx.pv; 570 571 /* Mark diagonal and invert diagonal for simplier triangular solves */ 572 pv = b->a + bdiag[i]; 573 *pv = 1.0/rtmp[i]; 574 575 } /* endof for (i=0; i<n; i++){ */ 576 577 /* MatPivotRefine() */ 578 if (info->shiftpd && !sctx.useshift && sctx.shift_fraction>0 && sctx.nshift<sctx.nshift_max){ 579 /* 580 * if no shift in this attempt & shifting & started shifting & can refine, 581 * then try lower shift 582 */ 583 sctx.shift_hi = sctx.shift_fraction; 584 sctx.shift_fraction = (sctx.shift_hi+sctx.shift_lo)/2.; 585 sctx.shift_amount = sctx.shift_fraction * sctx.shift_top; 586 sctx.useshift = PETSC_TRUE; 587 sctx.nshift++; 588 } 589 } while (sctx.useshift); 590 591 ierr = PetscFree(rtmp);CHKERRQ(ierr); 592 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 593 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 594 if (b->inode.use){ 595 C->ops->solve = MatSolve_SeqAIJ_Inode; 596 } else { 597 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 598 ierr = ISIdentity(isicol,&col_identity);CHKERRQ(ierr); 599 if (row_identity && col_identity) { 600 C->ops->solve = MatSolve_SeqAIJ_NaturalOrdering; 601 } else { 602 C->ops->solve = MatSolve_SeqAIJ; 603 } 604 } 605 C->ops->solveadd = MatSolveAdd_SeqAIJ; 606 C->ops->solvetranspose = MatSolveTranspose_SeqAIJ; 607 C->ops->solvetransposeadd = MatSolveTransposeAdd_SeqAIJ; 608 C->ops->matsolve = MatMatSolve_SeqAIJ; 609 C->assembled = PETSC_TRUE; 610 C->preallocated = PETSC_TRUE; 611 ierr = PetscLogFlops(C->cmap->n);CHKERRQ(ierr); 612 613 /* MatPivotView() */ 614 if (sctx.nshift){ 615 if (info->shiftpd) { 616 ierr = PetscInfo4(A,"number of shift_pd tries %D, shift_amount %G, diagonal shifted up by %e fraction top_value %e\n",sctx.nshift,sctx.shift_amount,sctx.shift_fraction,sctx.shift_top);CHKERRQ(ierr); 617 } else if (info->shiftnz) { 618 ierr = PetscInfo2(A,"number of shift_nz tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr); 619 } else if (info->shiftinblocks){ 620 ierr = PetscInfo2(A,"number of shift_inblocks applied %D, each shift_amount %G\n",sctx.nshift,info->shiftinblocks);CHKERRQ(ierr); 621 } 622 } 623 PetscFunctionReturn(0); 624 } 625 626 #undef __FUNCT__ 627 #define __FUNCT__ "MatLUFactorNumeric_SeqAIJ_inplace" 628 PetscErrorCode MatLUFactorNumeric_SeqAIJ_inplace(Mat B,Mat A,const MatFactorInfo *info) 629 { 630 Mat C=B; 631 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ *)C->data; 632 IS isrow = b->row,isicol = b->icol; 633 PetscErrorCode ierr; 634 const PetscInt *r,*ic,*ics; 635 PetscInt nz,row,i,j,n=A->rmap->n,diag; 636 const PetscInt *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j; 637 const PetscInt *ajtmp,*bjtmp,*diag_offset = b->diag,*pj; 638 MatScalar *pv,*rtmp,*pc,multiplier,d; 639 const MatScalar *v,*aa=a->a; 640 PetscReal rs=0.0; 641 FactorShiftCtx sctx; 642 PetscInt newshift,*ddiag; 643 644 PetscFunctionBegin; 645 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 646 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 647 ierr = PetscMalloc((n+1)*sizeof(MatScalar),&rtmp);CHKERRQ(ierr); 648 ics = ic; 649 650 /* initialize shift context sctx */ 651 sctx.nshift = 0; 652 sctx.nshift_max = 0; 653 sctx.shift_top = 0.0; 654 sctx.shift_lo = 0.0; 655 sctx.shift_hi = 0.0; 656 sctx.shift_fraction = 0.0; 657 sctx.shift_amount = 0.0; 658 659 /* if both shift schemes are chosen by user, only use info->shiftpd */ 660 if (info->shiftpd) { /* set sctx.shift_top=max{rs} */ 661 ddiag = a->diag; 662 sctx.shift_top = info->zeropivot; 663 for (i=0; i<n; i++) { 664 /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */ 665 d = (aa)[ddiag[i]]; 666 rs = -PetscAbsScalar(d) - PetscRealPart(d); 667 v = aa+ai[i]; 668 nz = ai[i+1] - ai[i]; 669 for (j=0; j<nz; j++) 670 rs += PetscAbsScalar(v[j]); 671 if (rs>sctx.shift_top) sctx.shift_top = rs; 672 } 673 sctx.shift_top *= 1.1; 674 sctx.nshift_max = 5; 675 sctx.shift_lo = 0.; 676 sctx.shift_hi = 1.; 677 } 678 679 do { 680 sctx.useshift = PETSC_FALSE; 681 for (i=0; i<n; i++){ 682 nz = bi[i+1] - bi[i]; 683 bjtmp = bj + bi[i]; 684 for (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0; 685 686 /* load in initial (unfactored row) */ 687 nz = ai[r[i]+1] - ai[r[i]]; 688 ajtmp = aj + ai[r[i]]; 689 v = aa + ai[r[i]]; 690 for (j=0; j<nz; j++) { 691 rtmp[ics[ajtmp[j]]] = v[j]; 692 } 693 rtmp[ics[r[i]]] += sctx.shift_amount; /* shift the diagonal of the matrix */ 694 /* if (sctx.shift_amount > 0.0) printf("row %d, shift %g\n",i,sctx.shift_amount); */ 695 696 row = *bjtmp++; 697 while (row < i) { 698 pc = rtmp + row; 699 if (*pc != 0.0) { 700 pv = b->a + diag_offset[row]; 701 pj = b->j + diag_offset[row] + 1; 702 multiplier = *pc / *pv++; 703 *pc = multiplier; 704 nz = bi[row+1] - diag_offset[row] - 1; 705 for (j=0; j<nz; j++) rtmp[pj[j]] -= multiplier * pv[j]; 706 ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr); 707 } 708 row = *bjtmp++; 709 } 710 /* finished row so stick it into b->a */ 711 pv = b->a + bi[i] ; 712 pj = b->j + bi[i] ; 713 nz = bi[i+1] - bi[i]; 714 diag = diag_offset[i] - bi[i]; 715 rs = 0.0; 716 for (j=0; j<nz; j++) { 717 pv[j] = rtmp[pj[j]]; 718 rs += PetscAbsScalar(pv[j]); 719 } 720 rs -= PetscAbsScalar(pv[diag]); 721 722 /* 9/13/02 Victor Eijkhout suggested scaling zeropivot by rs for matrices with funny scalings */ 723 sctx.rs = rs; 724 sctx.pv = pv[diag]; 725 ierr = MatLUCheckShift_inline(info,sctx,i,newshift);CHKERRQ(ierr); 726 if (newshift == 1) break; 727 } 728 729 if (info->shiftpd && !sctx.useshift && sctx.shift_fraction>0 && sctx.nshift<sctx.nshift_max) { 730 /* 731 * if no shift in this attempt & shifting & started shifting & can refine, 732 * then try lower shift 733 */ 734 sctx.shift_hi = sctx.shift_fraction; 735 sctx.shift_fraction = (sctx.shift_hi+sctx.shift_lo)/2.; 736 sctx.shift_amount = sctx.shift_fraction * sctx.shift_top; 737 sctx.useshift = PETSC_TRUE; 738 sctx.nshift++; 739 } 740 } while (sctx.useshift); 741 742 /* invert diagonal entries for simplier triangular solves */ 743 for (i=0; i<n; i++) { 744 b->a[diag_offset[i]] = 1.0/b->a[diag_offset[i]]; 745 } 746 ierr = PetscFree(rtmp);CHKERRQ(ierr); 747 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 748 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 749 if (b->inode.use) { 750 C->ops->solve = MatSolve_SeqAIJ_Inode_inplace; 751 } else { 752 PetscTruth row_identity, col_identity; 753 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 754 ierr = ISIdentity(isicol,&col_identity);CHKERRQ(ierr); 755 if (row_identity && col_identity) { 756 C->ops->solve = MatSolve_SeqAIJ_NaturalOrdering_inplace; 757 } else { 758 C->ops->solve = MatSolve_SeqAIJ_inplace; 759 } 760 } 761 C->ops->solveadd = MatSolveAdd_SeqAIJ_inplace; 762 C->ops->solvetranspose = MatSolveTranspose_SeqAIJ_inplace; 763 C->ops->solvetransposeadd = MatSolveTransposeAdd_SeqAIJ_inplace; 764 C->ops->matsolve = MatMatSolve_SeqAIJ_inplace; 765 C->assembled = PETSC_TRUE; 766 C->preallocated = PETSC_TRUE; 767 ierr = PetscLogFlops(C->cmap->n);CHKERRQ(ierr); 768 if (sctx.nshift){ 769 if (info->shiftpd) { 770 ierr = PetscInfo4(A,"number of shift_pd tries %D, shift_amount %G, diagonal shifted up by %e fraction top_value %e\n",sctx.nshift,sctx.shift_amount,sctx.shift_fraction,sctx.shift_top);CHKERRQ(ierr); 771 } else if (info->shiftnz) { 772 ierr = PetscInfo2(A,"number of shift_nz tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr); 773 } 774 } 775 PetscFunctionReturn(0); 776 } 777 778 /* 779 This routine implements inplace ILU(0) with row or/and column permutations. 780 Input: 781 A - original matrix 782 Output; 783 A - a->i (rowptr) is same as original rowptr, but factored i-the row is stored in rowperm[i] 784 a->j (col index) is permuted by the inverse of colperm, then sorted 785 a->a reordered accordingly with a->j 786 a->diag (ptr to diagonal elements) is updated. 787 */ 788 #undef __FUNCT__ 789 #define __FUNCT__ "MatLUFactorNumeric_SeqAIJ_InplaceWithPerm" 790 PetscErrorCode MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(Mat B,Mat A,const MatFactorInfo *info) 791 { 792 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data; 793 IS isrow = a->row,isicol = a->icol; 794 PetscErrorCode ierr; 795 const PetscInt *r,*ic,*ics; 796 PetscInt i,j,n=A->rmap->n,*ai=a->i,*aj=a->j; 797 PetscInt *ajtmp,nz,row; 798 PetscInt *diag = a->diag,nbdiag,*pj; 799 PetscScalar *rtmp,*pc,multiplier,d; 800 MatScalar *v,*pv; 801 PetscReal rs; 802 FactorShiftCtx sctx; 803 PetscInt newshift; 804 805 PetscFunctionBegin; 806 if (A != B) SETERRQ(PETSC_ERR_ARG_INCOMP,"input and output matrix must have same address"); 807 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 808 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 809 ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&rtmp);CHKERRQ(ierr); 810 ierr = PetscMemzero(rtmp,(n+1)*sizeof(PetscScalar));CHKERRQ(ierr); 811 ics = ic; 812 813 sctx.shift_top = 0.; 814 sctx.nshift_max = 0; 815 sctx.shift_lo = 0.; 816 sctx.shift_hi = 0.; 817 sctx.shift_fraction = 0.; 818 819 /* if both shift schemes are chosen by user, only use info->shiftpd */ 820 if (info->shiftpd) { /* set sctx.shift_top=max{rs} */ 821 sctx.shift_top = 0.; 822 for (i=0; i<n; i++) { 823 /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */ 824 d = (a->a)[diag[i]]; 825 rs = -PetscAbsScalar(d) - PetscRealPart(d); 826 v = a->a+ai[i]; 827 nz = ai[i+1] - ai[i]; 828 for (j=0; j<nz; j++) 829 rs += PetscAbsScalar(v[j]); 830 if (rs>sctx.shift_top) sctx.shift_top = rs; 831 } 832 if (sctx.shift_top < info->zeropivot) sctx.shift_top = info->zeropivot; 833 sctx.shift_top *= 1.1; 834 sctx.nshift_max = 5; 835 sctx.shift_lo = 0.; 836 sctx.shift_hi = 1.; 837 } 838 839 sctx.shift_amount = 0.; 840 sctx.nshift = 0; 841 do { 842 sctx.useshift = PETSC_FALSE; 843 for (i=0; i<n; i++){ 844 /* load in initial unfactored row */ 845 nz = ai[r[i]+1] - ai[r[i]]; 846 ajtmp = aj + ai[r[i]]; 847 v = a->a + ai[r[i]]; 848 /* sort permuted ajtmp and values v accordingly */ 849 for (j=0; j<nz; j++) ajtmp[j] = ics[ajtmp[j]]; 850 ierr = PetscSortIntWithScalarArray(nz,ajtmp,v);CHKERRQ(ierr); 851 852 diag[r[i]] = ai[r[i]]; 853 for (j=0; j<nz; j++) { 854 rtmp[ajtmp[j]] = v[j]; 855 if (ajtmp[j] < i) diag[r[i]]++; /* update a->diag */ 856 } 857 rtmp[r[i]] += sctx.shift_amount; /* shift the diagonal of the matrix */ 858 859 row = *ajtmp++; 860 while (row < i) { 861 pc = rtmp + row; 862 if (*pc != 0.0) { 863 pv = a->a + diag[r[row]]; 864 pj = aj + diag[r[row]] + 1; 865 866 multiplier = *pc / *pv++; 867 *pc = multiplier; 868 nz = ai[r[row]+1] - diag[r[row]] - 1; 869 for (j=0; j<nz; j++) rtmp[pj[j]] -= multiplier * pv[j]; 870 ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr); 871 } 872 row = *ajtmp++; 873 } 874 /* finished row so overwrite it onto a->a */ 875 pv = a->a + ai[r[i]] ; 876 pj = aj + ai[r[i]] ; 877 nz = ai[r[i]+1] - ai[r[i]]; 878 nbdiag = diag[r[i]] - ai[r[i]]; /* num of entries before the diagonal */ 879 880 rs = 0.0; 881 for (j=0; j<nz; j++) { 882 pv[j] = rtmp[pj[j]]; 883 if (j != nbdiag) rs += PetscAbsScalar(pv[j]); 884 } 885 886 /* 9/13/02 Victor Eijkhout suggested scaling zeropivot by rs for matrices with funny scalings */ 887 sctx.rs = rs; 888 sctx.pv = pv[nbdiag]; 889 ierr = MatLUCheckShift_inline(info,sctx,i,newshift);CHKERRQ(ierr); 890 if (newshift == 1) break; 891 } 892 893 if (info->shiftpd && !sctx.useshift && sctx.shift_fraction>0 && sctx.nshift<sctx.nshift_max) { 894 /* 895 * if no shift in this attempt & shifting & started shifting & can refine, 896 * then try lower shift 897 */ 898 sctx.shift_hi = sctx.shift_fraction; 899 sctx.shift_fraction = (sctx.shift_hi+sctx.shift_lo)/2.; 900 sctx.shift_amount = sctx.shift_fraction * sctx.shift_top; 901 sctx.useshift = PETSC_TRUE; 902 sctx.nshift++; 903 } 904 } while (sctx.useshift); 905 906 /* invert diagonal entries for simplier triangular solves */ 907 for (i=0; i<n; i++) { 908 a->a[diag[r[i]]] = 1.0/a->a[diag[r[i]]]; 909 } 910 911 ierr = PetscFree(rtmp);CHKERRQ(ierr); 912 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 913 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 914 A->ops->solve = MatSolve_SeqAIJ_InplaceWithPerm; 915 A->ops->solveadd = MatSolveAdd_SeqAIJ_inplace; 916 A->ops->solvetranspose = MatSolveTranspose_SeqAIJ_inplace; 917 A->ops->solvetransposeadd = MatSolveTransposeAdd_SeqAIJ_inplace; 918 A->assembled = PETSC_TRUE; 919 A->preallocated = PETSC_TRUE; 920 ierr = PetscLogFlops(A->cmap->n);CHKERRQ(ierr); 921 if (sctx.nshift){ 922 if (info->shiftpd) { 923 ierr = PetscInfo4(A,"number of shift_pd tries %D, shift_amount %G, diagonal shifted up by %e fraction top_value %e\n",sctx.nshift,sctx.shift_amount,sctx.shift_fraction,sctx.shift_top);CHKERRQ(ierr); 924 } else if (info->shiftnz) { 925 ierr = PetscInfo2(A,"number of shift_nz tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr); 926 } 927 } 928 PetscFunctionReturn(0); 929 } 930 931 /* ----------------------------------------------------------- */ 932 #undef __FUNCT__ 933 #define __FUNCT__ "MatLUFactor_SeqAIJ" 934 PetscErrorCode MatLUFactor_SeqAIJ(Mat A,IS row,IS col,const MatFactorInfo *info) 935 { 936 PetscErrorCode ierr; 937 Mat C; 938 939 PetscFunctionBegin; 940 ierr = MatGetFactor(A,MAT_SOLVER_PETSC,MAT_FACTOR_LU,&C);CHKERRQ(ierr); 941 ierr = MatLUFactorSymbolic(C,A,row,col,info);CHKERRQ(ierr); 942 ierr = MatLUFactorNumeric(C,A,info);CHKERRQ(ierr); 943 A->ops->solve = C->ops->solve; 944 A->ops->solvetranspose = C->ops->solvetranspose; 945 ierr = MatHeaderCopy(A,C);CHKERRQ(ierr); 946 ierr = PetscLogObjectParent(A,((Mat_SeqAIJ*)(A->data))->icol);CHKERRQ(ierr); 947 PetscFunctionReturn(0); 948 } 949 /* ----------------------------------------------------------- */ 950 951 952 #undef __FUNCT__ 953 #define __FUNCT__ "MatSolve_SeqAIJ_inplace" 954 PetscErrorCode MatSolve_SeqAIJ_inplace(Mat A,Vec bb,Vec xx) 955 { 956 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 957 IS iscol = a->col,isrow = a->row; 958 PetscErrorCode ierr; 959 PetscInt i, n = A->rmap->n,*vi,*ai = a->i,*aj = a->j; 960 PetscInt nz; 961 const PetscInt *rout,*cout,*r,*c; 962 PetscScalar *x,*tmp,*tmps,sum; 963 const PetscScalar *b; 964 const MatScalar *aa = a->a,*v; 965 966 PetscFunctionBegin; 967 if (!n) PetscFunctionReturn(0); 968 969 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 970 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 971 tmp = a->solve_work; 972 973 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 974 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1); 975 976 /* forward solve the lower triangular */ 977 tmp[0] = b[*r++]; 978 tmps = tmp; 979 for (i=1; i<n; i++) { 980 v = aa + ai[i] ; 981 vi = aj + ai[i] ; 982 nz = a->diag[i] - ai[i]; 983 sum = b[*r++]; 984 PetscSparseDenseMinusDot(sum,tmps,v,vi,nz); 985 tmp[i] = sum; 986 } 987 988 /* backward solve the upper triangular */ 989 for (i=n-1; i>=0; i--){ 990 v = aa + a->diag[i] + 1; 991 vi = aj + a->diag[i] + 1; 992 nz = ai[i+1] - a->diag[i] - 1; 993 sum = tmp[i]; 994 PetscSparseDenseMinusDot(sum,tmps,v,vi,nz); 995 x[*c--] = tmp[i] = sum*aa[a->diag[i]]; 996 } 997 998 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 999 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1000 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1001 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1002 ierr = PetscLogFlops(2.0*a->nz - A->cmap->n);CHKERRQ(ierr); 1003 PetscFunctionReturn(0); 1004 } 1005 1006 #undef __FUNCT__ 1007 #define __FUNCT__ "MatMatSolve_SeqAIJ_inplace" 1008 PetscErrorCode MatMatSolve_SeqAIJ_inplace(Mat A,Mat B,Mat X) 1009 { 1010 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1011 IS iscol = a->col,isrow = a->row; 1012 PetscErrorCode ierr; 1013 PetscInt i, n = A->rmap->n,*vi,*ai = a->i,*aj = a->j; 1014 PetscInt nz,neq; 1015 const PetscInt *rout,*cout,*r,*c; 1016 PetscScalar *x,*b,*tmp,*tmps,sum; 1017 const MatScalar *aa = a->a,*v; 1018 PetscTruth bisdense,xisdense; 1019 1020 PetscFunctionBegin; 1021 if (!n) PetscFunctionReturn(0); 1022 1023 ierr = PetscTypeCompare((PetscObject)B,MATSEQDENSE,&bisdense);CHKERRQ(ierr); 1024 if (!bisdense) SETERRQ(PETSC_ERR_ARG_INCOMP,"B matrix must be a SeqDense matrix"); 1025 ierr = PetscTypeCompare((PetscObject)X,MATSEQDENSE,&xisdense);CHKERRQ(ierr); 1026 if (!xisdense) SETERRQ(PETSC_ERR_ARG_INCOMP,"X matrix must be a SeqDense matrix"); 1027 1028 ierr = MatGetArray(B,&b);CHKERRQ(ierr); 1029 ierr = MatGetArray(X,&x);CHKERRQ(ierr); 1030 1031 tmp = a->solve_work; 1032 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 1033 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 1034 1035 for (neq=0; neq<B->cmap->n; neq++){ 1036 /* forward solve the lower triangular */ 1037 tmp[0] = b[r[0]]; 1038 tmps = tmp; 1039 for (i=1; i<n; i++) { 1040 v = aa + ai[i] ; 1041 vi = aj + ai[i] ; 1042 nz = a->diag[i] - ai[i]; 1043 sum = b[r[i]]; 1044 PetscSparseDenseMinusDot(sum,tmps,v,vi,nz); 1045 tmp[i] = sum; 1046 } 1047 /* backward solve the upper triangular */ 1048 for (i=n-1; i>=0; i--){ 1049 v = aa + a->diag[i] + 1; 1050 vi = aj + a->diag[i] + 1; 1051 nz = ai[i+1] - a->diag[i] - 1; 1052 sum = tmp[i]; 1053 PetscSparseDenseMinusDot(sum,tmps,v,vi,nz); 1054 x[c[i]] = tmp[i] = sum*aa[a->diag[i]]; 1055 } 1056 1057 b += n; 1058 x += n; 1059 } 1060 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 1061 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1062 ierr = MatRestoreArray(B,&b);CHKERRQ(ierr); 1063 ierr = MatRestoreArray(X,&x);CHKERRQ(ierr); 1064 ierr = PetscLogFlops(B->cmap->n*(2.0*a->nz - n));CHKERRQ(ierr); 1065 PetscFunctionReturn(0); 1066 } 1067 1068 #undef __FUNCT__ 1069 #define __FUNCT__ "MatMatSolve_SeqAIJ" 1070 PetscErrorCode MatMatSolve_SeqAIJ(Mat A,Mat B,Mat X) 1071 { 1072 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1073 IS iscol = a->col,isrow = a->row; 1074 PetscErrorCode ierr; 1075 PetscInt i, n = A->rmap->n,*vi,*ai = a->i,*aj = a->j,*adiag = a->diag; 1076 PetscInt nz,neq; 1077 const PetscInt *rout,*cout,*r,*c; 1078 PetscScalar *x,*b,*tmp,sum; 1079 const MatScalar *aa = a->a,*v; 1080 PetscTruth bisdense,xisdense; 1081 1082 PetscFunctionBegin; 1083 if (!n) PetscFunctionReturn(0); 1084 1085 ierr = PetscTypeCompare((PetscObject)B,MATSEQDENSE,&bisdense);CHKERRQ(ierr); 1086 if (!bisdense) SETERRQ(PETSC_ERR_ARG_INCOMP,"B matrix must be a SeqDense matrix"); 1087 ierr = PetscTypeCompare((PetscObject)X,MATSEQDENSE,&xisdense);CHKERRQ(ierr); 1088 if (!xisdense) SETERRQ(PETSC_ERR_ARG_INCOMP,"X matrix must be a SeqDense matrix"); 1089 1090 ierr = MatGetArray(B,&b);CHKERRQ(ierr); 1091 ierr = MatGetArray(X,&x);CHKERRQ(ierr); 1092 1093 tmp = a->solve_work; 1094 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 1095 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 1096 1097 for (neq=0; neq<B->cmap->n; neq++){ 1098 /* forward solve the lower triangular */ 1099 tmp[0] = b[r[0]]; 1100 v = aa; 1101 vi = aj; 1102 for (i=1; i<n; i++) { 1103 nz = ai[i+1] - ai[i]; 1104 sum = b[r[i]]; 1105 PetscSparseDenseMinusDot(sum,tmp,v,vi,nz); 1106 tmp[i] = sum; 1107 v += nz; vi += nz; 1108 } 1109 1110 /* backward solve the upper triangular */ 1111 for (i=n-1; i>=0; i--){ 1112 v = aa + adiag[i+1]+1; 1113 vi = aj + adiag[i+1]+1; 1114 nz = adiag[i]-adiag[i+1]-1; 1115 sum = tmp[i]; 1116 PetscSparseDenseMinusDot(sum,tmp,v,vi,nz); 1117 x[c[i]] = tmp[i] = sum*v[nz]; /* v[nz] = aa[adiag[i]] */ 1118 } 1119 1120 b += n; 1121 x += n; 1122 } 1123 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 1124 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1125 ierr = MatRestoreArray(B,&b);CHKERRQ(ierr); 1126 ierr = MatRestoreArray(X,&x);CHKERRQ(ierr); 1127 ierr = PetscLogFlops(B->cmap->n*(2.0*a->nz - n));CHKERRQ(ierr); 1128 PetscFunctionReturn(0); 1129 } 1130 1131 #undef __FUNCT__ 1132 #define __FUNCT__ "MatSolve_SeqAIJ_InplaceWithPerm" 1133 PetscErrorCode MatSolve_SeqAIJ_InplaceWithPerm(Mat A,Vec bb,Vec xx) 1134 { 1135 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1136 IS iscol = a->col,isrow = a->row; 1137 PetscErrorCode ierr; 1138 const PetscInt *r,*c,*rout,*cout; 1139 PetscInt i, n = A->rmap->n,*vi,*ai = a->i,*aj = a->j; 1140 PetscInt nz,row; 1141 PetscScalar *x,*b,*tmp,*tmps,sum; 1142 const MatScalar *aa = a->a,*v; 1143 1144 PetscFunctionBegin; 1145 if (!n) PetscFunctionReturn(0); 1146 1147 ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 1148 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1149 tmp = a->solve_work; 1150 1151 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 1152 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1); 1153 1154 /* forward solve the lower triangular */ 1155 tmp[0] = b[*r++]; 1156 tmps = tmp; 1157 for (row=1; row<n; row++) { 1158 i = rout[row]; /* permuted row */ 1159 v = aa + ai[i] ; 1160 vi = aj + ai[i] ; 1161 nz = a->diag[i] - ai[i]; 1162 sum = b[*r++]; 1163 PetscSparseDenseMinusDot(sum,tmps,v,vi,nz); 1164 tmp[row] = sum; 1165 } 1166 1167 /* backward solve the upper triangular */ 1168 for (row=n-1; row>=0; row--){ 1169 i = rout[row]; /* permuted row */ 1170 v = aa + a->diag[i] + 1; 1171 vi = aj + a->diag[i] + 1; 1172 nz = ai[i+1] - a->diag[i] - 1; 1173 sum = tmp[row]; 1174 PetscSparseDenseMinusDot(sum,tmps,v,vi,nz); 1175 x[*c--] = tmp[row] = sum*aa[a->diag[i]]; 1176 } 1177 1178 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 1179 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1180 ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 1181 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1182 ierr = PetscLogFlops(2.0*a->nz - A->cmap->n);CHKERRQ(ierr); 1183 PetscFunctionReturn(0); 1184 } 1185 1186 /* ----------------------------------------------------------- */ 1187 #include "../src/mat/impls/aij/seq/ftn-kernels/fsolve.h" 1188 #undef __FUNCT__ 1189 #define __FUNCT__ "MatSolve_SeqAIJ_NaturalOrdering_inplace" 1190 PetscErrorCode MatSolve_SeqAIJ_NaturalOrdering_inplace(Mat A,Vec bb,Vec xx) 1191 { 1192 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1193 PetscErrorCode ierr; 1194 PetscInt n = A->rmap->n; 1195 const PetscInt *ai = a->i,*aj = a->j,*adiag = a->diag; 1196 PetscScalar *x; 1197 const PetscScalar *b; 1198 const MatScalar *aa = a->a; 1199 #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ) 1200 PetscInt adiag_i,i,nz,ai_i; 1201 const PetscInt *vi; 1202 const MatScalar *v; 1203 PetscScalar sum; 1204 #endif 1205 1206 PetscFunctionBegin; 1207 if (!n) PetscFunctionReturn(0); 1208 1209 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1210 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1211 1212 #if defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ) 1213 fortransolveaij_(&n,x,ai,aj,adiag,aa,b); 1214 #else 1215 /* forward solve the lower triangular */ 1216 x[0] = b[0]; 1217 for (i=1; i<n; i++) { 1218 ai_i = ai[i]; 1219 v = aa + ai_i; 1220 vi = aj + ai_i; 1221 nz = adiag[i] - ai_i; 1222 sum = b[i]; 1223 PetscSparseDenseMinusDot(sum,x,v,vi,nz); 1224 x[i] = sum; 1225 } 1226 1227 /* backward solve the upper triangular */ 1228 for (i=n-1; i>=0; i--){ 1229 adiag_i = adiag[i]; 1230 v = aa + adiag_i + 1; 1231 vi = aj + adiag_i + 1; 1232 nz = ai[i+1] - adiag_i - 1; 1233 sum = x[i]; 1234 PetscSparseDenseMinusDot(sum,x,v,vi,nz); 1235 x[i] = sum*aa[adiag_i]; 1236 } 1237 #endif 1238 ierr = PetscLogFlops(2.0*a->nz - A->cmap->n);CHKERRQ(ierr); 1239 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1240 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1241 PetscFunctionReturn(0); 1242 } 1243 1244 #undef __FUNCT__ 1245 #define __FUNCT__ "MatSolveAdd_SeqAIJ_inplace" 1246 PetscErrorCode MatSolveAdd_SeqAIJ_inplace(Mat A,Vec bb,Vec yy,Vec xx) 1247 { 1248 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1249 IS iscol = a->col,isrow = a->row; 1250 PetscErrorCode ierr; 1251 PetscInt i, n = A->rmap->n,j; 1252 PetscInt nz; 1253 const PetscInt *rout,*cout,*r,*c,*vi,*ai = a->i,*aj = a->j; 1254 PetscScalar *x,*tmp,sum; 1255 const PetscScalar *b; 1256 const MatScalar *aa = a->a,*v; 1257 1258 PetscFunctionBegin; 1259 if (yy != xx) {ierr = VecCopy(yy,xx);CHKERRQ(ierr);} 1260 1261 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1262 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1263 tmp = a->solve_work; 1264 1265 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 1266 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1); 1267 1268 /* forward solve the lower triangular */ 1269 tmp[0] = b[*r++]; 1270 for (i=1; i<n; i++) { 1271 v = aa + ai[i] ; 1272 vi = aj + ai[i] ; 1273 nz = a->diag[i] - ai[i]; 1274 sum = b[*r++]; 1275 for (j=0; j<nz; j++) sum -= v[j]*tmp[vi[j]]; 1276 tmp[i] = sum; 1277 } 1278 1279 /* backward solve the upper triangular */ 1280 for (i=n-1; i>=0; i--){ 1281 v = aa + a->diag[i] + 1; 1282 vi = aj + a->diag[i] + 1; 1283 nz = ai[i+1] - a->diag[i] - 1; 1284 sum = tmp[i]; 1285 for (j=0; j<nz; j++) sum -= v[j]*tmp[vi[j]]; 1286 tmp[i] = sum*aa[a->diag[i]]; 1287 x[*c--] += tmp[i]; 1288 } 1289 1290 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 1291 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1292 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1293 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1294 ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1295 1296 PetscFunctionReturn(0); 1297 } 1298 1299 #undef __FUNCT__ 1300 #define __FUNCT__ "MatSolveAdd_SeqAIJ" 1301 PetscErrorCode MatSolveAdd_SeqAIJ(Mat A,Vec bb,Vec yy,Vec xx) 1302 { 1303 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1304 IS iscol = a->col,isrow = a->row; 1305 PetscErrorCode ierr; 1306 PetscInt i, n = A->rmap->n,j; 1307 PetscInt nz; 1308 const PetscInt *rout,*cout,*r,*c,*vi,*ai = a->i,*aj = a->j,*adiag = a->diag; 1309 PetscScalar *x,*tmp,sum; 1310 const PetscScalar *b; 1311 const MatScalar *aa = a->a,*v; 1312 1313 PetscFunctionBegin; 1314 if (yy != xx) {ierr = VecCopy(yy,xx);CHKERRQ(ierr);} 1315 1316 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1317 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1318 tmp = a->solve_work; 1319 1320 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 1321 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 1322 1323 /* forward solve the lower triangular */ 1324 tmp[0] = b[r[0]]; 1325 v = aa; 1326 vi = aj; 1327 for (i=1; i<n; i++) { 1328 nz = ai[i+1] - ai[i]; 1329 sum = b[r[i]]; 1330 for (j=0; j<nz; j++) sum -= v[j]*tmp[vi[j]]; 1331 tmp[i] = sum; 1332 v += nz; vi += nz; 1333 } 1334 1335 /* backward solve the upper triangular */ 1336 v = aa + adiag[n-1]; 1337 vi = aj + adiag[n-1]; 1338 for (i=n-1; i>=0; i--){ 1339 nz = adiag[i] - adiag[i+1] - 1; 1340 sum = tmp[i]; 1341 for (j=0; j<nz; j++) sum -= v[j]*tmp[vi[j]]; 1342 tmp[i] = sum*v[nz]; 1343 x[c[i]] += tmp[i]; 1344 v += nz+1; vi += nz+1; 1345 } 1346 1347 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 1348 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1349 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1350 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1351 ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1352 1353 PetscFunctionReturn(0); 1354 } 1355 1356 #undef __FUNCT__ 1357 #define __FUNCT__ "MatSolveTranspose_SeqAIJ_inplace" 1358 PetscErrorCode MatSolveTranspose_SeqAIJ_inplace(Mat A,Vec bb,Vec xx) 1359 { 1360 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1361 IS iscol = a->col,isrow = a->row; 1362 PetscErrorCode ierr; 1363 const PetscInt *rout,*cout,*r,*c,*diag = a->diag,*ai = a->i,*aj = a->j,*vi; 1364 PetscInt i,n = A->rmap->n,j; 1365 PetscInt nz; 1366 PetscScalar *x,*tmp,s1; 1367 const MatScalar *aa = a->a,*v; 1368 const PetscScalar *b; 1369 1370 PetscFunctionBegin; 1371 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1372 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1373 tmp = a->solve_work; 1374 1375 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 1376 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 1377 1378 /* copy the b into temp work space according to permutation */ 1379 for (i=0; i<n; i++) tmp[i] = b[c[i]]; 1380 1381 /* forward solve the U^T */ 1382 for (i=0; i<n; i++) { 1383 v = aa + diag[i] ; 1384 vi = aj + diag[i] + 1; 1385 nz = ai[i+1] - diag[i] - 1; 1386 s1 = tmp[i]; 1387 s1 *= (*v++); /* multiply by inverse of diagonal entry */ 1388 for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j]; 1389 tmp[i] = s1; 1390 } 1391 1392 /* backward solve the L^T */ 1393 for (i=n-1; i>=0; i--){ 1394 v = aa + diag[i] - 1 ; 1395 vi = aj + diag[i] - 1 ; 1396 nz = diag[i] - ai[i]; 1397 s1 = tmp[i]; 1398 for (j=0; j>-nz; j--) tmp[vi[j]] -= s1*v[j]; 1399 } 1400 1401 /* copy tmp into x according to permutation */ 1402 for (i=0; i<n; i++) x[r[i]] = tmp[i]; 1403 1404 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 1405 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1406 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1407 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1408 1409 ierr = PetscLogFlops(2.0*a->nz-A->cmap->n);CHKERRQ(ierr); 1410 PetscFunctionReturn(0); 1411 } 1412 1413 #undef __FUNCT__ 1414 #define __FUNCT__ "MatSolveTranspose_SeqAIJ" 1415 PetscErrorCode MatSolveTranspose_SeqAIJ(Mat A,Vec bb,Vec xx) 1416 { 1417 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1418 IS iscol = a->col,isrow = a->row; 1419 PetscErrorCode ierr; 1420 const PetscInt *rout,*cout,*r,*c,*adiag = a->diag,*ai = a->i,*aj = a->j,*vi; 1421 PetscInt i,n = A->rmap->n,j; 1422 PetscInt nz; 1423 PetscScalar *x,*tmp,s1; 1424 const MatScalar *aa = a->a,*v; 1425 const PetscScalar *b; 1426 1427 PetscFunctionBegin; 1428 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1429 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1430 tmp = a->solve_work; 1431 1432 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 1433 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 1434 1435 /* copy the b into temp work space according to permutation */ 1436 for (i=0; i<n; i++) tmp[i] = b[c[i]]; 1437 1438 /* forward solve the U^T */ 1439 for (i=0; i<n; i++) { 1440 v = aa + adiag[i+1] + 1; 1441 vi = aj + adiag[i+1] + 1; 1442 nz = adiag[i] - adiag[i+1] - 1; 1443 s1 = tmp[i]; 1444 s1 *= v[nz]; /* multiply by inverse of diagonal entry */ 1445 for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j]; 1446 tmp[i] = s1; 1447 } 1448 1449 /* backward solve the L^T */ 1450 for (i=n-1; i>=0; i--){ 1451 v = aa + ai[i]; 1452 vi = aj + ai[i]; 1453 nz = ai[i+1] - ai[i]; 1454 s1 = tmp[i]; 1455 for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j]; 1456 } 1457 1458 /* copy tmp into x according to permutation */ 1459 for (i=0; i<n; i++) x[r[i]] = tmp[i]; 1460 1461 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 1462 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1463 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1464 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1465 1466 ierr = PetscLogFlops(2.0*a->nz-A->cmap->n);CHKERRQ(ierr); 1467 PetscFunctionReturn(0); 1468 } 1469 1470 #undef __FUNCT__ 1471 #define __FUNCT__ "MatSolveTransposeAdd_SeqAIJ_inplace" 1472 PetscErrorCode MatSolveTransposeAdd_SeqAIJ_inplace(Mat A,Vec bb,Vec zz,Vec xx) 1473 { 1474 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1475 IS iscol = a->col,isrow = a->row; 1476 PetscErrorCode ierr; 1477 const PetscInt *rout,*cout,*r,*c,*diag = a->diag,*ai = a->i,*aj = a->j,*vi; 1478 PetscInt i,n = A->rmap->n,j; 1479 PetscInt nz; 1480 PetscScalar *x,*tmp,s1; 1481 const MatScalar *aa = a->a,*v; 1482 const PetscScalar *b; 1483 1484 PetscFunctionBegin; 1485 if (zz != xx) {ierr = VecCopy(zz,xx);CHKERRQ(ierr);} 1486 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1487 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1488 tmp = a->solve_work; 1489 1490 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 1491 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 1492 1493 /* copy the b into temp work space according to permutation */ 1494 for (i=0; i<n; i++) tmp[i] = b[c[i]]; 1495 1496 /* forward solve the U^T */ 1497 for (i=0; i<n; i++) { 1498 v = aa + diag[i] ; 1499 vi = aj + diag[i] + 1; 1500 nz = ai[i+1] - diag[i] - 1; 1501 s1 = tmp[i]; 1502 s1 *= (*v++); /* multiply by inverse of diagonal entry */ 1503 for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j]; 1504 tmp[i] = s1; 1505 } 1506 1507 /* backward solve the L^T */ 1508 for (i=n-1; i>=0; i--){ 1509 v = aa + diag[i] - 1 ; 1510 vi = aj + diag[i] - 1 ; 1511 nz = diag[i] - ai[i]; 1512 s1 = tmp[i]; 1513 for (j=0; j>-nz; j--) tmp[vi[j]] -= s1*v[j]; 1514 } 1515 1516 /* copy tmp into x according to permutation */ 1517 for (i=0; i<n; i++) x[r[i]] += tmp[i]; 1518 1519 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 1520 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1521 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1522 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1523 1524 ierr = PetscLogFlops(2.0*a->nz-A->cmap->n);CHKERRQ(ierr); 1525 PetscFunctionReturn(0); 1526 } 1527 1528 #undef __FUNCT__ 1529 #define __FUNCT__ "MatSolveTransposeAdd_SeqAIJ" 1530 PetscErrorCode MatSolveTransposeAdd_SeqAIJ(Mat A,Vec bb,Vec zz,Vec xx) 1531 { 1532 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1533 IS iscol = a->col,isrow = a->row; 1534 PetscErrorCode ierr; 1535 const PetscInt *rout,*cout,*r,*c,*adiag = a->diag,*ai = a->i,*aj = a->j,*vi; 1536 PetscInt i,n = A->rmap->n,j; 1537 PetscInt nz; 1538 PetscScalar *x,*tmp,s1; 1539 const MatScalar *aa = a->a,*v; 1540 const PetscScalar *b; 1541 1542 PetscFunctionBegin; 1543 if (zz != xx) {ierr = VecCopy(zz,xx);CHKERRQ(ierr);} 1544 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1545 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1546 tmp = a->solve_work; 1547 1548 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 1549 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 1550 1551 /* copy the b into temp work space according to permutation */ 1552 for (i=0; i<n; i++) tmp[i] = b[c[i]]; 1553 1554 /* forward solve the U^T */ 1555 for (i=0; i<n; i++) { 1556 v = aa + adiag[i+1] + 1; 1557 vi = aj + adiag[i+1] + 1; 1558 nz = adiag[i] - adiag[i+1] - 1; 1559 s1 = tmp[i]; 1560 s1 *= v[nz]; /* multiply by inverse of diagonal entry */ 1561 for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j]; 1562 tmp[i] = s1; 1563 } 1564 1565 1566 /* backward solve the L^T */ 1567 for (i=n-1; i>=0; i--){ 1568 v = aa + ai[i] ; 1569 vi = aj + ai[i]; 1570 nz = ai[i+1] - ai[i]; 1571 s1 = tmp[i]; 1572 for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j]; 1573 } 1574 1575 /* copy tmp into x according to permutation */ 1576 for (i=0; i<n; i++) x[r[i]] += tmp[i]; 1577 1578 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 1579 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1580 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1581 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1582 1583 ierr = PetscLogFlops(2.0*a->nz-A->cmap->n);CHKERRQ(ierr); 1584 PetscFunctionReturn(0); 1585 } 1586 1587 /* ----------------------------------------------------------------*/ 1588 1589 EXTERN PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat,Mat,MatDuplicateOption,PetscTruth); 1590 1591 /* 1592 ilu() under revised new data structure. 1593 Factored arrays bj and ba are stored as 1594 L(0,:), L(1,:), ...,L(n-1,:), U(n-1,:),...,U(i,:),U(i-1,:),...,U(0,:) 1595 1596 bi=fact->i is an array of size n+1, in which 1597 bi+ 1598 bi[i]: points to 1st entry of L(i,:),i=0,...,n-1 1599 bi[n]: points to L(n-1,n-1)+1 1600 1601 bdiag=fact->diag is an array of size n+1,in which 1602 bdiag[i]: points to diagonal of U(i,:), i=0,...,n-1 1603 bdiag[n]: points to entry of U(n-1,0)-1 1604 1605 U(i,:) contains bdiag[i] as its last entry, i.e., 1606 U(i,:) = (u[i,i+1],...,u[i,n-1],diag[i]) 1607 */ 1608 #undef __FUNCT__ 1609 #define __FUNCT__ "MatILUFactorSymbolic_SeqAIJ_ilu0" 1610 PetscErrorCode MatILUFactorSymbolic_SeqAIJ_ilu0(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 1611 { 1612 1613 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b; 1614 PetscErrorCode ierr; 1615 const PetscInt n=A->rmap->n,*ai=a->i,*aj,*adiag=a->diag; 1616 PetscInt i,j,k=0,nz,*bi,*bj,*bdiag; 1617 PetscTruth missing; 1618 IS isicol; 1619 1620 PetscFunctionBegin; 1621 if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n); 1622 ierr = MatMissingDiagonal(A,&missing,&i);CHKERRQ(ierr); 1623 if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",i); 1624 ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 1625 1626 ierr = MatDuplicateNoCreate_SeqAIJ(fact,A,MAT_DO_NOT_COPY_VALUES,PETSC_FALSE);CHKERRQ(ierr); 1627 b = (Mat_SeqAIJ*)(fact)->data; 1628 1629 /* allocate matrix arrays for new data structure */ 1630 ierr = PetscMalloc3(ai[n]+1,PetscScalar,&b->a,ai[n]+1,PetscInt,&b->j,n+1,PetscInt,&b->i);CHKERRQ(ierr); 1631 ierr = PetscLogObjectMemory(fact,ai[n]*(sizeof(PetscScalar)+sizeof(PetscInt))+(n+1)*sizeof(PetscInt));CHKERRQ(ierr); 1632 b->singlemalloc = PETSC_TRUE; 1633 if (!b->diag){ 1634 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&b->diag);CHKERRQ(ierr); 1635 ierr = PetscLogObjectMemory(fact,(n+1)*sizeof(PetscInt));CHKERRQ(ierr); 1636 } 1637 bdiag = b->diag; 1638 1639 if (n > 0) { 1640 ierr = PetscMemzero(b->a,(ai[n])*sizeof(MatScalar));CHKERRQ(ierr); 1641 } 1642 1643 /* set bi and bj with new data structure */ 1644 bi = b->i; 1645 bj = b->j; 1646 1647 /* L part */ 1648 bi[0] = 0; 1649 for (i=0; i<n; i++){ 1650 nz = adiag[i] - ai[i]; 1651 bi[i+1] = bi[i] + nz; 1652 aj = a->j + ai[i]; 1653 for (j=0; j<nz; j++){ 1654 /* *bj = aj[j]; bj++; */ 1655 bj[k++] = aj[j]; 1656 } 1657 } 1658 1659 /* U part */ 1660 bdiag[n] = bi[n]-1; 1661 for (i=n-1; i>=0; i--){ 1662 nz = ai[i+1] - adiag[i] - 1; 1663 aj = a->j + adiag[i] + 1; 1664 for (j=0; j<nz; j++){ 1665 /* *bj = aj[j]; bj++; */ 1666 bj[k++] = aj[j]; 1667 } 1668 /* diag[i] */ 1669 /* *bj = i; bj++; */ 1670 bj[k++] = i; 1671 bdiag[i] = bdiag[i+1] + nz + 1; 1672 } 1673 1674 fact->factor = MAT_FACTOR_ILU; 1675 fact->info.factor_mallocs = 0; 1676 fact->info.fill_ratio_given = info->fill; 1677 fact->info.fill_ratio_needed = 1.0; 1678 fact->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ; 1679 1680 b = (Mat_SeqAIJ*)(fact)->data; 1681 b->row = isrow; 1682 b->col = iscol; 1683 b->icol = isicol; 1684 ierr = PetscMalloc((fact->rmap->n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 1685 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 1686 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 1687 PetscFunctionReturn(0); 1688 } 1689 1690 #undef __FUNCT__ 1691 #define __FUNCT__ "MatILUFactorSymbolic_SeqAIJ" 1692 PetscErrorCode MatILUFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 1693 { 1694 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b; 1695 IS isicol; 1696 PetscErrorCode ierr; 1697 const PetscInt *r,*ic; 1698 PetscInt n=A->rmap->n,*ai=a->i,*aj=a->j; 1699 PetscInt *bi,*cols,nnz,*cols_lvl; 1700 PetscInt *bdiag,prow,fm,nzbd,reallocs=0,dcount=0; 1701 PetscInt i,levels,diagonal_fill; 1702 PetscTruth col_identity,row_identity; 1703 PetscReal f; 1704 PetscInt nlnk,*lnk,*lnk_lvl=PETSC_NULL; 1705 PetscBT lnkbt; 1706 PetscInt nzi,*bj,**bj_ptr,**bjlvl_ptr; 1707 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 1708 PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL; 1709 1710 PetscFunctionBegin; 1711 /* // Testing new data structure for MatSolve() 1712 PetscTruth olddatastruct=PETSC_FALSE 1713 ierr = PetscOptionsGetTruth(PETSC_NULL,"-ilu_old",&olddatastruct,PETSC_NULL);CHKERRQ(ierr); 1714 if(olddatastruct){ 1715 ierr = MatILUFactorSymbolic_SeqAIJ_inplace(fact,A,isrow,iscol,info);CHKERRQ(ierr); 1716 PetscFunctionReturn(0); 1717 } 1718 */ 1719 1720 levels = (PetscInt)info->levels; 1721 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 1722 ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr); 1723 1724 if (!levels && row_identity && col_identity) { 1725 /* special case: ilu(0) with natural ordering */ 1726 ierr = MatILUFactorSymbolic_SeqAIJ_ilu0(fact,A,isrow,iscol,info);CHKERRQ(ierr); 1727 ierr = Mat_CheckInode_FactorLU(fact,PETSC_FALSE);CHKERRQ(ierr); 1728 PetscFunctionReturn(0); 1729 } 1730 1731 if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n); 1732 ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 1733 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 1734 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 1735 1736 /* get new row and diagonal pointers, must be allocated separately because they will be given to the Mat_SeqAIJ and freed separately */ 1737 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 1738 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr); 1739 bi[0] = bdiag[0] = 0; 1740 1741 ierr = PetscMalloc2(n,PetscInt*,&bj_ptr,n,PetscInt*,&bjlvl_ptr);CHKERRQ(ierr); 1742 1743 /* create a linked list for storing column indices of the active row */ 1744 nlnk = n + 1; 1745 ierr = PetscIncompleteLLCreate(n,n,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 1746 1747 /* initial FreeSpace size is f*(ai[n]+1) */ 1748 f = info->fill; 1749 diagonal_fill = (PetscInt)info->diagonal_fill; 1750 ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space);CHKERRQ(ierr); 1751 current_space = free_space; 1752 ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space_lvl);CHKERRQ(ierr); 1753 current_space_lvl = free_space_lvl; 1754 1755 for (i=0; i<n; i++) { 1756 nzi = 0; 1757 /* copy current row into linked list */ 1758 nnz = ai[r[i]+1] - ai[r[i]]; 1759 if (!nnz) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i); 1760 cols = aj + ai[r[i]]; 1761 lnk[i] = -1; /* marker to indicate if diagonal exists */ 1762 ierr = PetscIncompleteLLInit(nnz,cols,n,ic,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 1763 nzi += nlnk; 1764 1765 /* make sure diagonal entry is included */ 1766 if (diagonal_fill && lnk[i] == -1) { 1767 fm = n; 1768 while (lnk[fm] < i) fm = lnk[fm]; 1769 lnk[i] = lnk[fm]; /* insert diagonal into linked list */ 1770 lnk[fm] = i; 1771 lnk_lvl[i] = 0; 1772 nzi++; dcount++; 1773 } 1774 1775 /* add pivot rows into the active row */ 1776 nzbd = 0; 1777 prow = lnk[n]; 1778 while (prow < i) { 1779 nnz = bdiag[prow]; 1780 cols = bj_ptr[prow] + nnz + 1; 1781 cols_lvl = bjlvl_ptr[prow] + nnz + 1; 1782 nnz = bi[prow+1] - bi[prow] - nnz - 1; 1783 ierr = PetscILULLAddSorted(nnz,cols,levels,cols_lvl,prow,nlnk,lnk,lnk_lvl,lnkbt,prow);CHKERRQ(ierr); 1784 nzi += nlnk; 1785 prow = lnk[prow]; 1786 nzbd++; 1787 } 1788 bdiag[i] = nzbd; 1789 bi[i+1] = bi[i] + nzi; 1790 1791 /* if free space is not available, make more free space */ 1792 if (current_space->local_remaining<nzi) { 1793 nnz = 2*nzi*(n - i); /* estimated and max additional space needed */ 1794 ierr = PetscFreeSpaceGet(nnz,¤t_space);CHKERRQ(ierr); 1795 ierr = PetscFreeSpaceGet(nnz,¤t_space_lvl);CHKERRQ(ierr); 1796 reallocs++; 1797 } 1798 1799 /* copy data into free_space and free_space_lvl, then initialize lnk */ 1800 ierr = PetscIncompleteLLClean(n,n,nzi,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr); 1801 bj_ptr[i] = current_space->array; 1802 bjlvl_ptr[i] = current_space_lvl->array; 1803 1804 /* make sure the active row i has diagonal entry */ 1805 if (*(bj_ptr[i]+bdiag[i]) != i) { 1806 SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,"Row %D has missing diagonal in factored matrix\n\ 1807 try running with -pc_factor_nonzeros_along_diagonal or -pc_factor_diagonal_fill",i); 1808 } 1809 1810 current_space->array += nzi; 1811 current_space->local_used += nzi; 1812 current_space->local_remaining -= nzi; 1813 current_space_lvl->array += nzi; 1814 current_space_lvl->local_used += nzi; 1815 current_space_lvl->local_remaining -= nzi; 1816 } 1817 1818 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 1819 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 1820 1821 /* destroy list of free space and other temporary arrays */ 1822 ierr = PetscMalloc((bi[n]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 1823 1824 /* copy free_space into bj and free free_space; set bi, bj, bdiag in new datastructure; */ 1825 ierr = PetscFreeSpaceContiguous_LU(&free_space,bj,n,bi,bdiag);CHKERRQ(ierr); 1826 1827 ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 1828 ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr); 1829 ierr = PetscFree2(bj_ptr,bjlvl_ptr);CHKERRQ(ierr); 1830 1831 #if defined(PETSC_USE_INFO) 1832 { 1833 PetscReal af = ((PetscReal)bi[n])/((PetscReal)ai[n]); 1834 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,f,af);CHKERRQ(ierr); 1835 ierr = PetscInfo1(A,"Run with -[sub_]pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 1836 ierr = PetscInfo1(A,"PCFactorSetFill([sub]pc,%G);\n",af);CHKERRQ(ierr); 1837 ierr = PetscInfo(A,"for best performance.\n");CHKERRQ(ierr); 1838 if (diagonal_fill) { 1839 ierr = PetscInfo1(A,"Detected and replaced %D missing diagonals",dcount);CHKERRQ(ierr); 1840 } 1841 } 1842 #endif 1843 1844 /* put together the new matrix */ 1845 ierr = MatSeqAIJSetPreallocation_SeqAIJ(fact,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr); 1846 ierr = PetscLogObjectParent(fact,isicol);CHKERRQ(ierr); 1847 b = (Mat_SeqAIJ*)(fact)->data; 1848 b->free_a = PETSC_TRUE; 1849 b->free_ij = PETSC_TRUE; 1850 b->singlemalloc = PETSC_FALSE; 1851 ierr = PetscMalloc((bdiag[0]+1)*sizeof(PetscScalar),&b->a);CHKERRQ(ierr); 1852 b->j = bj; 1853 b->i = bi; 1854 b->diag = bdiag; 1855 b->ilen = 0; 1856 b->imax = 0; 1857 b->row = isrow; 1858 b->col = iscol; 1859 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 1860 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 1861 b->icol = isicol; 1862 ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 1863 /* In b structure: Free imax, ilen, old a, old j. 1864 Allocate bdiag, solve_work, new a, new j */ 1865 ierr = PetscLogObjectMemory(fact,(bdiag[0]+1)*(sizeof(PetscInt)+sizeof(PetscScalar)));CHKERRQ(ierr); 1866 b->maxnz = b->nz = bdiag[0]+1; 1867 (fact)->info.factor_mallocs = reallocs; 1868 (fact)->info.fill_ratio_given = f; 1869 (fact)->info.fill_ratio_needed = ((PetscReal)(bdiag[0]+1))/((PetscReal)ai[n]); 1870 (fact)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ; 1871 ierr = Mat_CheckInode_FactorLU(fact,PETSC_FALSE);CHKERRQ(ierr); 1872 PetscFunctionReturn(0); 1873 } 1874 1875 #undef __FUNCT__ 1876 #define __FUNCT__ "MatILUFactorSymbolic_SeqAIJ_inplace" 1877 PetscErrorCode MatILUFactorSymbolic_SeqAIJ_inplace(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 1878 { 1879 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b; 1880 IS isicol; 1881 PetscErrorCode ierr; 1882 const PetscInt *r,*ic; 1883 PetscInt n=A->rmap->n,*ai=a->i,*aj=a->j,d; 1884 PetscInt *bi,*cols,nnz,*cols_lvl; 1885 PetscInt *bdiag,prow,fm,nzbd,reallocs=0,dcount=0; 1886 PetscInt i,levels,diagonal_fill; 1887 PetscTruth col_identity,row_identity; 1888 PetscReal f; 1889 PetscInt nlnk,*lnk,*lnk_lvl=PETSC_NULL; 1890 PetscBT lnkbt; 1891 PetscInt nzi,*bj,**bj_ptr,**bjlvl_ptr; 1892 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 1893 PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL; 1894 PetscTruth missing; 1895 1896 PetscFunctionBegin; 1897 if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n); 1898 f = info->fill; 1899 levels = (PetscInt)info->levels; 1900 diagonal_fill = (PetscInt)info->diagonal_fill; 1901 ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 1902 1903 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 1904 ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr); 1905 if (!levels && row_identity && col_identity) { /* special case: ilu(0) with natural ordering */ 1906 ierr = MatDuplicateNoCreate_SeqAIJ(fact,A,MAT_DO_NOT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr); 1907 (fact)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ_inplace; 1908 1909 fact->factor = MAT_FACTOR_ILU; 1910 (fact)->info.factor_mallocs = 0; 1911 (fact)->info.fill_ratio_given = info->fill; 1912 (fact)->info.fill_ratio_needed = 1.0; 1913 b = (Mat_SeqAIJ*)(fact)->data; 1914 ierr = MatMissingDiagonal(A,&missing,&d);CHKERRQ(ierr); 1915 if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d); 1916 b->row = isrow; 1917 b->col = iscol; 1918 b->icol = isicol; 1919 ierr = PetscMalloc(((fact)->rmap->n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 1920 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 1921 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 1922 ierr = MatILUFactorSymbolic_SeqAIJ_Inode(fact,A,isrow,iscol,info);CHKERRQ(ierr); 1923 PetscFunctionReturn(0); 1924 } 1925 1926 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 1927 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 1928 1929 /* get new row and diagonal pointers, must be allocated separately because they will be given to the Mat_SeqAIJ and freed separately */ 1930 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 1931 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr); 1932 bi[0] = bdiag[0] = 0; 1933 1934 ierr = PetscMalloc2(n,PetscInt*,&bj_ptr,n,PetscInt*,&bjlvl_ptr);CHKERRQ(ierr); 1935 1936 /* create a linked list for storing column indices of the active row */ 1937 nlnk = n + 1; 1938 ierr = PetscIncompleteLLCreate(n,n,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 1939 1940 /* initial FreeSpace size is f*(ai[n]+1) */ 1941 ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space);CHKERRQ(ierr); 1942 current_space = free_space; 1943 ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space_lvl);CHKERRQ(ierr); 1944 current_space_lvl = free_space_lvl; 1945 1946 for (i=0; i<n; i++) { 1947 nzi = 0; 1948 /* copy current row into linked list */ 1949 nnz = ai[r[i]+1] - ai[r[i]]; 1950 if (!nnz) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i); 1951 cols = aj + ai[r[i]]; 1952 lnk[i] = -1; /* marker to indicate if diagonal exists */ 1953 ierr = PetscIncompleteLLInit(nnz,cols,n,ic,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 1954 nzi += nlnk; 1955 1956 /* make sure diagonal entry is included */ 1957 if (diagonal_fill && lnk[i] == -1) { 1958 fm = n; 1959 while (lnk[fm] < i) fm = lnk[fm]; 1960 lnk[i] = lnk[fm]; /* insert diagonal into linked list */ 1961 lnk[fm] = i; 1962 lnk_lvl[i] = 0; 1963 nzi++; dcount++; 1964 } 1965 1966 /* add pivot rows into the active row */ 1967 nzbd = 0; 1968 prow = lnk[n]; 1969 while (prow < i) { 1970 nnz = bdiag[prow]; 1971 cols = bj_ptr[prow] + nnz + 1; 1972 cols_lvl = bjlvl_ptr[prow] + nnz + 1; 1973 nnz = bi[prow+1] - bi[prow] - nnz - 1; 1974 ierr = PetscILULLAddSorted(nnz,cols,levels,cols_lvl,prow,nlnk,lnk,lnk_lvl,lnkbt,prow);CHKERRQ(ierr); 1975 nzi += nlnk; 1976 prow = lnk[prow]; 1977 nzbd++; 1978 } 1979 bdiag[i] = nzbd; 1980 bi[i+1] = bi[i] + nzi; 1981 1982 /* if free space is not available, make more free space */ 1983 if (current_space->local_remaining<nzi) { 1984 nnz = nzi*(n - i); /* estimated and max additional space needed */ 1985 ierr = PetscFreeSpaceGet(nnz,¤t_space);CHKERRQ(ierr); 1986 ierr = PetscFreeSpaceGet(nnz,¤t_space_lvl);CHKERRQ(ierr); 1987 reallocs++; 1988 } 1989 1990 /* copy data into free_space and free_space_lvl, then initialize lnk */ 1991 ierr = PetscIncompleteLLClean(n,n,nzi,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr); 1992 bj_ptr[i] = current_space->array; 1993 bjlvl_ptr[i] = current_space_lvl->array; 1994 1995 /* make sure the active row i has diagonal entry */ 1996 if (*(bj_ptr[i]+bdiag[i]) != i) { 1997 SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,"Row %D has missing diagonal in factored matrix\n\ 1998 try running with -pc_factor_nonzeros_along_diagonal or -pc_factor_diagonal_fill",i); 1999 } 2000 2001 current_space->array += nzi; 2002 current_space->local_used += nzi; 2003 current_space->local_remaining -= nzi; 2004 current_space_lvl->array += nzi; 2005 current_space_lvl->local_used += nzi; 2006 current_space_lvl->local_remaining -= nzi; 2007 } 2008 2009 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 2010 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 2011 2012 /* destroy list of free space and other temporary arrays */ 2013 ierr = PetscMalloc((bi[n]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 2014 ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr); /* copy free_space -> bj */ 2015 ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 2016 ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr); 2017 ierr = PetscFree2(bj_ptr,bjlvl_ptr);CHKERRQ(ierr); 2018 2019 #if defined(PETSC_USE_INFO) 2020 { 2021 PetscReal af = ((PetscReal)bi[n])/((PetscReal)ai[n]); 2022 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,f,af);CHKERRQ(ierr); 2023 ierr = PetscInfo1(A,"Run with -[sub_]pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 2024 ierr = PetscInfo1(A,"PCFactorSetFill([sub]pc,%G);\n",af);CHKERRQ(ierr); 2025 ierr = PetscInfo(A,"for best performance.\n");CHKERRQ(ierr); 2026 if (diagonal_fill) { 2027 ierr = PetscInfo1(A,"Detected and replaced %D missing diagonals",dcount);CHKERRQ(ierr); 2028 } 2029 } 2030 #endif 2031 2032 /* put together the new matrix */ 2033 ierr = MatSeqAIJSetPreallocation_SeqAIJ(fact,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr); 2034 ierr = PetscLogObjectParent(fact,isicol);CHKERRQ(ierr); 2035 b = (Mat_SeqAIJ*)(fact)->data; 2036 b->free_a = PETSC_TRUE; 2037 b->free_ij = PETSC_TRUE; 2038 b->singlemalloc = PETSC_FALSE; 2039 ierr = PetscMalloc(bi[n]*sizeof(PetscScalar),&b->a);CHKERRQ(ierr); 2040 b->j = bj; 2041 b->i = bi; 2042 for (i=0; i<n; i++) bdiag[i] += bi[i]; 2043 b->diag = bdiag; 2044 b->ilen = 0; 2045 b->imax = 0; 2046 b->row = isrow; 2047 b->col = iscol; 2048 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 2049 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 2050 b->icol = isicol; 2051 ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 2052 /* In b structure: Free imax, ilen, old a, old j. 2053 Allocate bdiag, solve_work, new a, new j */ 2054 ierr = PetscLogObjectMemory(fact,(bi[n]-n) * (sizeof(PetscInt)+sizeof(PetscScalar)));CHKERRQ(ierr); 2055 b->maxnz = b->nz = bi[n] ; 2056 (fact)->info.factor_mallocs = reallocs; 2057 (fact)->info.fill_ratio_given = f; 2058 (fact)->info.fill_ratio_needed = ((PetscReal)bi[n])/((PetscReal)ai[n]); 2059 (fact)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ_inplace; 2060 ierr = MatILUFactorSymbolic_SeqAIJ_Inode(fact,A,isrow,iscol,info);CHKERRQ(ierr); 2061 PetscFunctionReturn(0); 2062 } 2063 2064 #undef __FUNCT__ 2065 #define __FUNCT__ "MatCholeskyFactorNumeric_SeqAIJ" 2066 PetscErrorCode MatCholeskyFactorNumeric_SeqAIJ(Mat B,Mat A,const MatFactorInfo *info) 2067 { 2068 Mat C = B; 2069 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data; 2070 Mat_SeqSBAIJ *b=(Mat_SeqSBAIJ*)C->data; 2071 IS ip=b->row,iip = b->icol; 2072 PetscErrorCode ierr; 2073 const PetscInt *rip,*riip; 2074 PetscInt i,j,mbs=A->rmap->n,*bi=b->i,*bj=b->j,*bdiag=b->diag,*bjtmp; 2075 PetscInt *ai=a->i,*aj=a->j; 2076 PetscInt k,jmin,jmax,*c2r,*il,col,nexti,ili,nz; 2077 MatScalar *rtmp,*ba=b->a,*bval,*aa=a->a,dk,uikdi; 2078 PetscTruth perm_identity; 2079 2080 FactorShiftCtx sctx; 2081 PetscReal rs; 2082 MatScalar d,*v; 2083 2084 PetscFunctionBegin; 2085 /* MatPivotSetUp(): initialize shift context sctx */ 2086 ierr = PetscMemzero(&sctx,sizeof(FactorShiftCtx));CHKERRQ(ierr); 2087 2088 /* if both shift schemes are chosen by user, only use info->shiftpd */ 2089 if (info->shiftpd) { /* set sctx.shift_top=max{rs} */ 2090 sctx.shift_top = info->zeropivot; 2091 for (i=0; i<mbs; i++) { 2092 /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */ 2093 d = (aa)[a->diag[i]]; 2094 rs = -PetscAbsScalar(d) - PetscRealPart(d); 2095 v = aa+ai[i]; 2096 nz = ai[i+1] - ai[i]; 2097 for (j=0; j<nz; j++) 2098 rs += PetscAbsScalar(v[j]); 2099 if (rs>sctx.shift_top) sctx.shift_top = rs; 2100 } 2101 sctx.shift_top *= 1.1; 2102 sctx.nshift_max = 5; 2103 sctx.shift_lo = 0.; 2104 sctx.shift_hi = 1.; 2105 } 2106 2107 ierr = ISGetIndices(ip,&rip);CHKERRQ(ierr); 2108 ierr = ISGetIndices(iip,&riip);CHKERRQ(ierr); 2109 2110 /* allocate working arrays 2111 c2r: linked list, keep track of pivot rows for a given column. c2r[col]: head of the list for a given col 2112 il: for active k row, il[i] gives the index of the 1st nonzero entry in U[i,k:n-1] in bj and ba arrays 2113 */ 2114 ierr = PetscMalloc3(mbs,MatScalar,&rtmp,mbs,PetscInt,&il,mbs,PetscInt,&c2r);CHKERRQ(ierr); 2115 2116 do { 2117 sctx.useshift = PETSC_FALSE; 2118 2119 for (i=0; i<mbs; i++) c2r[i] = mbs; 2120 il[0] = 0; 2121 2122 for (k = 0; k<mbs; k++){ 2123 /* zero rtmp */ 2124 nz = bi[k+1] - bi[k]; 2125 bjtmp = bj + bi[k]; 2126 for (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0; 2127 2128 /* load in initial unfactored row */ 2129 bval = ba + bi[k]; 2130 jmin = ai[rip[k]]; jmax = ai[rip[k]+1]; 2131 for (j = jmin; j < jmax; j++){ 2132 col = riip[aj[j]]; 2133 if (col >= k){ /* only take upper triangular entry */ 2134 rtmp[col] = aa[j]; 2135 *bval++ = 0.0; /* for in-place factorization */ 2136 } 2137 } 2138 /* shift the diagonal of the matrix: ZeropivotApply() */ 2139 rtmp[k] += sctx.shift_amount; /* shift the diagonal of the matrix */ 2140 2141 /* modify k-th row by adding in those rows i with U(i,k)!=0 */ 2142 dk = rtmp[k]; 2143 i = c2r[k]; /* first row to be added to k_th row */ 2144 2145 while (i < k){ 2146 nexti = c2r[i]; /* next row to be added to k_th row */ 2147 2148 /* compute multiplier, update diag(k) and U(i,k) */ 2149 ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */ 2150 uikdi = - ba[ili]*ba[bdiag[i]]; /* diagonal(k) */ 2151 dk += uikdi*ba[ili]; /* update diag[k] */ 2152 ba[ili] = uikdi; /* -U(i,k) */ 2153 2154 /* add multiple of row i to k-th row */ 2155 jmin = ili + 1; jmax = bi[i+1]; 2156 if (jmin < jmax){ 2157 for (j=jmin; j<jmax; j++) rtmp[bj[j]] += uikdi*ba[j]; 2158 /* update il and c2r for row i */ 2159 il[i] = jmin; 2160 j = bj[jmin]; c2r[i] = c2r[j]; c2r[j] = i; 2161 } 2162 i = nexti; 2163 } 2164 2165 /* copy data into U(k,:) */ 2166 rs = 0.0; 2167 jmin = bi[k]; jmax = bi[k+1]-1; 2168 if (jmin < jmax) { 2169 for (j=jmin; j<jmax; j++){ 2170 col = bj[j]; ba[j] = rtmp[col]; rs += PetscAbsScalar(ba[j]); 2171 } 2172 /* add the k-th row into il and c2r */ 2173 il[k] = jmin; 2174 i = bj[jmin]; c2r[k] = c2r[i]; c2r[i] = k; 2175 } 2176 2177 /* MatPivotCheck() */ 2178 sctx.rs = rs; 2179 sctx.pv = dk; 2180 if (info->shiftnz){ 2181 ierr = MatPivotCheck_nz(info,sctx,k);CHKERRQ(ierr); 2182 } else if (info->shiftpd){ 2183 ierr = MatPivotCheck_pd(info,sctx,k);CHKERRQ(ierr); 2184 } else if (info->shiftinblocks){ 2185 ierr = MatPivotCheck_inblocks(info,sctx,k);CHKERRQ(ierr); 2186 } else { 2187 ierr = MatPivotCheck_none(info,sctx,k);CHKERRQ(ierr); 2188 } 2189 dk = sctx.pv; 2190 2191 ba[bdiag[k]] = 1.0/dk; /* U(k,k) */ 2192 } 2193 } while (sctx.useshift); 2194 2195 ierr = PetscFree3(rtmp,il,c2r);CHKERRQ(ierr); 2196 ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr); 2197 ierr = ISRestoreIndices(iip,&riip);CHKERRQ(ierr); 2198 2199 ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr); 2200 if (perm_identity){ 2201 B->ops->solve = MatSolve_SeqSBAIJ_1_NaturalOrdering; 2202 B->ops->solvetranspose = MatSolve_SeqSBAIJ_1_NaturalOrdering; 2203 B->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_1_NaturalOrdering; 2204 B->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_1_NaturalOrdering; 2205 } else { 2206 B->ops->solve = MatSolve_SeqSBAIJ_1; 2207 B->ops->solvetranspose = MatSolve_SeqSBAIJ_1; 2208 B->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_1; 2209 B->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_1; 2210 } 2211 2212 C->assembled = PETSC_TRUE; 2213 C->preallocated = PETSC_TRUE; 2214 ierr = PetscLogFlops(C->rmap->n);CHKERRQ(ierr); 2215 2216 /* MatPivotView() */ 2217 if (sctx.nshift){ 2218 if (info->shiftpd) { 2219 ierr = PetscInfo4(A,"number of shift_pd tries %D, shift_amount %G, diagonal shifted up by %e fraction top_value %e\n",sctx.nshift,sctx.shift_amount,sctx.shift_fraction,sctx.shift_top);CHKERRQ(ierr); 2220 } else if (info->shiftnz) { 2221 ierr = PetscInfo2(A,"number of shift_nz tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr); 2222 } else if (info->shiftinblocks){ 2223 ierr = PetscInfo2(A,"number of shift_inblocks applied %D, each shift_amount %G\n",sctx.nshift,info->shiftinblocks);CHKERRQ(ierr); 2224 } 2225 } 2226 PetscFunctionReturn(0); 2227 } 2228 2229 #undef __FUNCT__ 2230 #define __FUNCT__ "MatCholeskyFactorNumeric_SeqAIJ_inplace" 2231 PetscErrorCode MatCholeskyFactorNumeric_SeqAIJ_inplace(Mat B,Mat A,const MatFactorInfo *info) 2232 { 2233 Mat C = B; 2234 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data; 2235 Mat_SeqSBAIJ *b=(Mat_SeqSBAIJ*)C->data; 2236 IS ip=b->row,iip = b->icol; 2237 PetscErrorCode ierr; 2238 const PetscInt *rip,*riip; 2239 PetscInt i,j,mbs=A->rmap->n,*bi=b->i,*bj=b->j,*bcol,*bjtmp; 2240 PetscInt *ai=a->i,*aj=a->j; 2241 PetscInt k,jmin,jmax,*jl,*il,col,nexti,ili,nz; 2242 MatScalar *rtmp,*ba=b->a,*bval,*aa=a->a,dk,uikdi; 2243 PetscReal zeropivot,rs,shiftnz; 2244 PetscReal shiftpd; 2245 ChShift_Ctx sctx; 2246 PetscInt newshift; 2247 PetscTruth perm_identity; 2248 2249 PetscFunctionBegin; 2250 shiftnz = info->shiftnz; 2251 shiftpd = info->shiftpd; 2252 zeropivot = info->zeropivot; 2253 2254 ierr = ISGetIndices(ip,&rip);CHKERRQ(ierr); 2255 ierr = ISGetIndices(iip,&riip);CHKERRQ(ierr); 2256 2257 /* initialization */ 2258 ierr = PetscMalloc3(mbs,MatScalar,&rtmp,mbs,PetscInt,&il,mbs,PetscInt,&jl);CHKERRQ(ierr); 2259 sctx.shift_amount = 0; 2260 sctx.nshift = 0; 2261 do { 2262 sctx.chshift = PETSC_FALSE; 2263 for (i=0; i<mbs; i++) jl[i] = mbs; 2264 il[0] = 0; 2265 2266 for (k = 0; k<mbs; k++){ 2267 /* zero rtmp */ 2268 nz = bi[k+1] - bi[k]; 2269 bjtmp = bj + bi[k]; 2270 for (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0; 2271 2272 bval = ba + bi[k]; 2273 /* initialize k-th row by the perm[k]-th row of A */ 2274 jmin = ai[rip[k]]; jmax = ai[rip[k]+1]; 2275 for (j = jmin; j < jmax; j++){ 2276 col = riip[aj[j]]; 2277 if (col >= k){ /* only take upper triangular entry */ 2278 rtmp[col] = aa[j]; 2279 *bval++ = 0.0; /* for in-place factorization */ 2280 } 2281 } 2282 /* shift the diagonal of the matrix */ 2283 if (sctx.nshift) rtmp[k] += sctx.shift_amount; 2284 2285 /* modify k-th row by adding in those rows i with U(i,k)!=0 */ 2286 dk = rtmp[k]; 2287 i = jl[k]; /* first row to be added to k_th row */ 2288 2289 while (i < k){ 2290 nexti = jl[i]; /* next row to be added to k_th row */ 2291 2292 /* compute multiplier, update diag(k) and U(i,k) */ 2293 ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */ 2294 uikdi = - ba[ili]*ba[bi[i]]; /* diagonal(k) */ 2295 dk += uikdi*ba[ili]; 2296 ba[ili] = uikdi; /* -U(i,k) */ 2297 2298 /* add multiple of row i to k-th row */ 2299 jmin = ili + 1; jmax = bi[i+1]; 2300 if (jmin < jmax){ 2301 for (j=jmin; j<jmax; j++) rtmp[bj[j]] += uikdi*ba[j]; 2302 /* update il and jl for row i */ 2303 il[i] = jmin; 2304 j = bj[jmin]; jl[i] = jl[j]; jl[j] = i; 2305 } 2306 i = nexti; 2307 } 2308 2309 /* shift the diagonals when zero pivot is detected */ 2310 /* compute rs=sum of abs(off-diagonal) */ 2311 rs = 0.0; 2312 jmin = bi[k]+1; 2313 nz = bi[k+1] - jmin; 2314 bcol = bj + jmin; 2315 for (j=0; j<nz; j++) { 2316 rs += PetscAbsScalar(rtmp[bcol[j]]); 2317 } 2318 2319 sctx.rs = rs; 2320 sctx.pv = dk; 2321 ierr = MatCholeskyCheckShift_inline(info,sctx,k,newshift);CHKERRQ(ierr); 2322 2323 if (newshift == 1) { 2324 if (!sctx.shift_amount) { 2325 sctx.shift_amount = 1e-5; 2326 } 2327 break; 2328 } 2329 2330 /* copy data into U(k,:) */ 2331 ba[bi[k]] = 1.0/dk; /* U(k,k) */ 2332 jmin = bi[k]+1; jmax = bi[k+1]; 2333 if (jmin < jmax) { 2334 for (j=jmin; j<jmax; j++){ 2335 col = bj[j]; ba[j] = rtmp[col]; 2336 } 2337 /* add the k-th row into il and jl */ 2338 il[k] = jmin; 2339 i = bj[jmin]; jl[k] = jl[i]; jl[i] = k; 2340 } 2341 } 2342 } while (sctx.chshift); 2343 ierr = PetscFree3(rtmp,il,jl);CHKERRQ(ierr); 2344 ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr); 2345 ierr = ISRestoreIndices(iip,&riip);CHKERRQ(ierr); 2346 2347 ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr); 2348 if (perm_identity){ 2349 B->ops->solve = MatSolve_SeqSBAIJ_1_NaturalOrdering_inplace; 2350 B->ops->solvetranspose = MatSolve_SeqSBAIJ_1_NaturalOrdering_inplace; 2351 B->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_1_NaturalOrdering_inplace; 2352 B->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_1_NaturalOrdering_inplace; 2353 } else { 2354 B->ops->solve = MatSolve_SeqSBAIJ_1_inplace; 2355 B->ops->solvetranspose = MatSolve_SeqSBAIJ_1_inplace; 2356 B->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_1_inplace; 2357 B->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_1_inplace; 2358 } 2359 2360 C->assembled = PETSC_TRUE; 2361 C->preallocated = PETSC_TRUE; 2362 ierr = PetscLogFlops(C->rmap->n);CHKERRQ(ierr); 2363 if (sctx.nshift){ 2364 if (shiftnz) { 2365 ierr = PetscInfo2(A,"number of shiftnz tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr); 2366 } else if (shiftpd) { 2367 ierr = PetscInfo2(A,"number of shiftpd tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr); 2368 } 2369 } 2370 PetscFunctionReturn(0); 2371 } 2372 2373 /* 2374 icc() under revised new data structure. 2375 Factored arrays bj and ba are stored as 2376 U(0,:),...,U(i,:),U(n-1,:) 2377 2378 ui=fact->i is an array of size n+1, in which 2379 ui+ 2380 ui[i]: points to 1st entry of U(i,:),i=0,...,n-1 2381 ui[n]: points to U(n-1,n-1)+1 2382 2383 udiag=fact->diag is an array of size n,in which 2384 udiag[i]: points to diagonal of U(i,:), i=0,...,n-1 2385 2386 U(i,:) contains udiag[i] as its last entry, i.e., 2387 U(i,:) = (u[i,i+1],...,u[i,n-1],diag[i]) 2388 */ 2389 2390 #undef __FUNCT__ 2391 #define __FUNCT__ "MatICCFactorSymbolic_SeqAIJ" 2392 PetscErrorCode MatICCFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS perm,const MatFactorInfo *info) 2393 { 2394 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2395 Mat_SeqSBAIJ *b; 2396 PetscErrorCode ierr; 2397 PetscTruth perm_identity,missing; 2398 PetscInt reallocs=0,i,*ai=a->i,*aj=a->j,am=A->rmap->n,*ui,*udiag; 2399 const PetscInt *rip,*riip; 2400 PetscInt jmin,jmax,nzk,k,j,*jl,prow,*il,nextprow; 2401 PetscInt nlnk,*lnk,*lnk_lvl=PETSC_NULL,d; 2402 PetscInt ncols,ncols_upper,*cols,*ajtmp,*uj,**uj_ptr,**uj_lvl_ptr; 2403 PetscReal fill=info->fill,levels=info->levels; 2404 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 2405 PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL; 2406 PetscBT lnkbt; 2407 IS iperm; 2408 2409 PetscFunctionBegin; 2410 if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n); 2411 ierr = MatMissingDiagonal(A,&missing,&d);CHKERRQ(ierr); 2412 if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d); 2413 ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 2414 ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr); 2415 2416 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr); 2417 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&udiag);CHKERRQ(ierr); 2418 ui[0] = 0; 2419 2420 /* ICC(0) without matrix ordering: simply rearrange column indices */ 2421 if (!levels && perm_identity) { 2422 for (i=0; i<am; i++) { 2423 ncols = ai[i+1] - a->diag[i]; 2424 ui[i+1] = ui[i] + ncols; 2425 udiag[i] = ui[i+1] - 1; /* points to the last entry of U(i,:) */ 2426 } 2427 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2428 cols = uj; 2429 for (i=0; i<am; i++) { 2430 aj = a->j + a->diag[i] + 1; /* 1st entry of U(i,:) without diagonal */ 2431 ncols = ai[i+1] - a->diag[i] -1; 2432 for (j=0; j<ncols; j++) *cols++ = aj[j]; 2433 *cols++ = i; /* diagoanl is located as the last entry of U(i,:) */ 2434 } 2435 } else { /* case: levels>0 || (levels=0 && !perm_identity) */ 2436 ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr); 2437 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 2438 2439 /* initialization */ 2440 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ajtmp);CHKERRQ(ierr); 2441 2442 /* jl: linked list for storing indices of the pivot rows 2443 il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */ 2444 ierr = PetscMalloc4(am,PetscInt*,&uj_ptr,am,PetscInt*,&uj_lvl_ptr,am,PetscInt,&jl,am,PetscInt,&il);CHKERRQ(ierr); 2445 for (i=0; i<am; i++){ 2446 jl[i] = am; il[i] = 0; 2447 } 2448 2449 /* create and initialize a linked list for storing column indices of the active row k */ 2450 nlnk = am + 1; 2451 ierr = PetscIncompleteLLCreate(am,am,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 2452 2453 /* initial FreeSpace size is fill*(ai[am]+1) */ 2454 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr); 2455 current_space = free_space; 2456 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space_lvl);CHKERRQ(ierr); 2457 current_space_lvl = free_space_lvl; 2458 2459 for (k=0; k<am; k++){ /* for each active row k */ 2460 /* initialize lnk by the column indices of row rip[k] of A */ 2461 nzk = 0; 2462 ncols = ai[rip[k]+1] - ai[rip[k]]; 2463 if (!ncols) SETERRQ2(PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k); 2464 ncols_upper = 0; 2465 for (j=0; j<ncols; j++){ 2466 i = *(aj + ai[rip[k]] + j); /* unpermuted column index */ 2467 if (riip[i] >= k){ /* only take upper triangular entry */ 2468 ajtmp[ncols_upper] = i; 2469 ncols_upper++; 2470 } 2471 } 2472 ierr = PetscIncompleteLLInit(ncols_upper,ajtmp,am,riip,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 2473 nzk += nlnk; 2474 2475 /* update lnk by computing fill-in for each pivot row to be merged in */ 2476 prow = jl[k]; /* 1st pivot row */ 2477 2478 while (prow < k){ 2479 nextprow = jl[prow]; 2480 2481 /* merge prow into k-th row */ 2482 jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */ 2483 jmax = ui[prow+1]; 2484 ncols = jmax-jmin; 2485 i = jmin - ui[prow]; 2486 cols = uj_ptr[prow] + i; /* points to the 2nd nzero entry in U(prow,k:am-1) */ 2487 uj = uj_lvl_ptr[prow] + i; /* levels of cols */ 2488 j = *(uj - 1); 2489 ierr = PetscICCLLAddSorted(ncols,cols,levels,uj,am,nlnk,lnk,lnk_lvl,lnkbt,j);CHKERRQ(ierr); 2490 nzk += nlnk; 2491 2492 /* update il and jl for prow */ 2493 if (jmin < jmax){ 2494 il[prow] = jmin; 2495 j = *cols; jl[prow] = jl[j]; jl[j] = prow; 2496 } 2497 prow = nextprow; 2498 } 2499 2500 /* if free space is not available, make more free space */ 2501 if (current_space->local_remaining<nzk) { 2502 i = am - k + 1; /* num of unfactored rows */ 2503 i *= PetscMin(nzk, i-1); /* i*nzk, i*(i-1): estimated and max additional space needed */ 2504 ierr = PetscFreeSpaceGet(i,¤t_space);CHKERRQ(ierr); 2505 ierr = PetscFreeSpaceGet(i,¤t_space_lvl);CHKERRQ(ierr); 2506 reallocs++; 2507 } 2508 2509 /* copy data into free_space and free_space_lvl, then initialize lnk */ 2510 if (nzk == 0) SETERRQ1(PETSC_ERR_ARG_WRONG,"Empty row %D in ICC matrix factor",k); 2511 ierr = PetscIncompleteLLClean(am,am,nzk,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr); 2512 2513 /* add the k-th row into il and jl */ 2514 if (nzk > 1){ 2515 i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */ 2516 jl[k] = jl[i]; jl[i] = k; 2517 il[k] = ui[k] + 1; 2518 } 2519 uj_ptr[k] = current_space->array; 2520 uj_lvl_ptr[k] = current_space_lvl->array; 2521 2522 current_space->array += nzk; 2523 current_space->local_used += nzk; 2524 current_space->local_remaining -= nzk; 2525 2526 current_space_lvl->array += nzk; 2527 current_space_lvl->local_used += nzk; 2528 current_space_lvl->local_remaining -= nzk; 2529 2530 ui[k+1] = ui[k] + nzk; 2531 } 2532 2533 #if defined(PETSC_USE_INFO) 2534 if (ai[am] != 0) { 2535 PetscReal af = (PetscReal)ui[am]/((PetscReal)ai[am]); 2536 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr); 2537 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 2538 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr); 2539 } else { 2540 ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr); 2541 } 2542 #endif 2543 2544 ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); 2545 ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr); 2546 ierr = PetscFree4(uj_ptr,uj_lvl_ptr,jl,il);CHKERRQ(ierr); 2547 ierr = PetscFree(ajtmp);CHKERRQ(ierr); 2548 2549 /* destroy list of free space and other temporary array(s) */ 2550 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2551 ierr = PetscFreeSpaceContiguous_Cholesky(&free_space,uj,am,ui,udiag);CHKERRQ(ierr); /* store matrix factor */ 2552 ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 2553 ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr); 2554 2555 } /* end of case: levels>0 || (levels=0 && !perm_identity) */ 2556 2557 /* put together the new matrix in MATSEQSBAIJ format */ 2558 b = (Mat_SeqSBAIJ*)(fact)->data; 2559 b->singlemalloc = PETSC_FALSE; 2560 ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr); 2561 b->j = uj; 2562 b->i = ui; 2563 b->diag = udiag; 2564 b->free_diag = PETSC_TRUE; 2565 b->ilen = 0; 2566 b->imax = 0; 2567 b->row = perm; 2568 b->col = perm; 2569 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2570 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2571 b->icol = iperm; 2572 b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ 2573 ierr = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 2574 ierr = PetscLogObjectMemory(fact,ui[am]*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 2575 b->maxnz = b->nz = ui[am]; 2576 b->free_a = PETSC_TRUE; 2577 b->free_ij = PETSC_TRUE; 2578 2579 fact->info.factor_mallocs = reallocs; 2580 fact->info.fill_ratio_given = fill; 2581 if (ai[am] != 0) { 2582 fact->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]); 2583 } else { 2584 fact->info.fill_ratio_needed = 0.0; 2585 } 2586 fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ; 2587 PetscFunctionReturn(0); 2588 } 2589 2590 #undef __FUNCT__ 2591 #define __FUNCT__ "MatICCFactorSymbolic_SeqAIJ_inplace" 2592 PetscErrorCode MatICCFactorSymbolic_SeqAIJ_inplace(Mat fact,Mat A,IS perm,const MatFactorInfo *info) 2593 { 2594 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2595 Mat_SeqSBAIJ *b; 2596 PetscErrorCode ierr; 2597 PetscTruth perm_identity,missing; 2598 PetscInt reallocs=0,i,*ai=a->i,*aj=a->j,am=A->rmap->n,*ui,*udiag; 2599 const PetscInt *rip,*riip; 2600 PetscInt jmin,jmax,nzk,k,j,*jl,prow,*il,nextprow; 2601 PetscInt nlnk,*lnk,*lnk_lvl=PETSC_NULL,d; 2602 PetscInt ncols,ncols_upper,*cols,*ajtmp,*uj,**uj_ptr,**uj_lvl_ptr; 2603 PetscReal fill=info->fill,levels=info->levels; 2604 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 2605 PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL; 2606 PetscBT lnkbt; 2607 IS iperm; 2608 2609 PetscFunctionBegin; 2610 if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n); 2611 ierr = MatMissingDiagonal(A,&missing,&d);CHKERRQ(ierr); 2612 if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d); 2613 ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 2614 ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr); 2615 2616 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr); 2617 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&udiag);CHKERRQ(ierr); 2618 ui[0] = 0; 2619 2620 /* ICC(0) without matrix ordering: simply copies fill pattern */ 2621 if (!levels && perm_identity) { 2622 2623 for (i=0; i<am; i++) { 2624 ui[i+1] = ui[i] + ai[i+1] - a->diag[i]; 2625 udiag[i] = ui[i]; 2626 } 2627 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2628 cols = uj; 2629 for (i=0; i<am; i++) { 2630 aj = a->j + a->diag[i]; 2631 ncols = ui[i+1] - ui[i]; 2632 for (j=0; j<ncols; j++) *cols++ = *aj++; 2633 } 2634 } else { /* case: levels>0 || (levels=0 && !perm_identity) */ 2635 ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr); 2636 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 2637 2638 /* initialization */ 2639 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ajtmp);CHKERRQ(ierr); 2640 2641 /* jl: linked list for storing indices of the pivot rows 2642 il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */ 2643 ierr = PetscMalloc4(am,PetscInt*,&uj_ptr,am,PetscInt*,&uj_lvl_ptr,am,PetscInt,&jl,am,PetscInt,&il);CHKERRQ(ierr); 2644 for (i=0; i<am; i++){ 2645 jl[i] = am; il[i] = 0; 2646 } 2647 2648 /* create and initialize a linked list for storing column indices of the active row k */ 2649 nlnk = am + 1; 2650 ierr = PetscIncompleteLLCreate(am,am,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 2651 2652 /* initial FreeSpace size is fill*(ai[am]+1) */ 2653 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr); 2654 current_space = free_space; 2655 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space_lvl);CHKERRQ(ierr); 2656 current_space_lvl = free_space_lvl; 2657 2658 for (k=0; k<am; k++){ /* for each active row k */ 2659 /* initialize lnk by the column indices of row rip[k] of A */ 2660 nzk = 0; 2661 ncols = ai[rip[k]+1] - ai[rip[k]]; 2662 if (!ncols) SETERRQ2(PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k); 2663 ncols_upper = 0; 2664 for (j=0; j<ncols; j++){ 2665 i = *(aj + ai[rip[k]] + j); /* unpermuted column index */ 2666 if (riip[i] >= k){ /* only take upper triangular entry */ 2667 ajtmp[ncols_upper] = i; 2668 ncols_upper++; 2669 } 2670 } 2671 ierr = PetscIncompleteLLInit(ncols_upper,ajtmp,am,riip,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 2672 nzk += nlnk; 2673 2674 /* update lnk by computing fill-in for each pivot row to be merged in */ 2675 prow = jl[k]; /* 1st pivot row */ 2676 2677 while (prow < k){ 2678 nextprow = jl[prow]; 2679 2680 /* merge prow into k-th row */ 2681 jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */ 2682 jmax = ui[prow+1]; 2683 ncols = jmax-jmin; 2684 i = jmin - ui[prow]; 2685 cols = uj_ptr[prow] + i; /* points to the 2nd nzero entry in U(prow,k:am-1) */ 2686 uj = uj_lvl_ptr[prow] + i; /* levels of cols */ 2687 j = *(uj - 1); 2688 ierr = PetscICCLLAddSorted(ncols,cols,levels,uj,am,nlnk,lnk,lnk_lvl,lnkbt,j);CHKERRQ(ierr); 2689 nzk += nlnk; 2690 2691 /* update il and jl for prow */ 2692 if (jmin < jmax){ 2693 il[prow] = jmin; 2694 j = *cols; jl[prow] = jl[j]; jl[j] = prow; 2695 } 2696 prow = nextprow; 2697 } 2698 2699 /* if free space is not available, make more free space */ 2700 if (current_space->local_remaining<nzk) { 2701 i = am - k + 1; /* num of unfactored rows */ 2702 i *= PetscMin(nzk, (i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */ 2703 ierr = PetscFreeSpaceGet(i,¤t_space);CHKERRQ(ierr); 2704 ierr = PetscFreeSpaceGet(i,¤t_space_lvl);CHKERRQ(ierr); 2705 reallocs++; 2706 } 2707 2708 /* copy data into free_space and free_space_lvl, then initialize lnk */ 2709 if (nzk == 0) SETERRQ1(PETSC_ERR_ARG_WRONG,"Empty row %D in ICC matrix factor",k); 2710 ierr = PetscIncompleteLLClean(am,am,nzk,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr); 2711 2712 /* add the k-th row into il and jl */ 2713 if (nzk > 1){ 2714 i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */ 2715 jl[k] = jl[i]; jl[i] = k; 2716 il[k] = ui[k] + 1; 2717 } 2718 uj_ptr[k] = current_space->array; 2719 uj_lvl_ptr[k] = current_space_lvl->array; 2720 2721 current_space->array += nzk; 2722 current_space->local_used += nzk; 2723 current_space->local_remaining -= nzk; 2724 2725 current_space_lvl->array += nzk; 2726 current_space_lvl->local_used += nzk; 2727 current_space_lvl->local_remaining -= nzk; 2728 2729 ui[k+1] = ui[k] + nzk; 2730 } 2731 2732 #if defined(PETSC_USE_INFO) 2733 if (ai[am] != 0) { 2734 PetscReal af = (PetscReal)ui[am]/((PetscReal)ai[am]); 2735 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr); 2736 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 2737 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr); 2738 } else { 2739 ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr); 2740 } 2741 #endif 2742 2743 ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); 2744 ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr); 2745 ierr = PetscFree4(uj_ptr,uj_lvl_ptr,jl,il);CHKERRQ(ierr); 2746 ierr = PetscFree(ajtmp);CHKERRQ(ierr); 2747 2748 /* destroy list of free space and other temporary array(s) */ 2749 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2750 ierr = PetscFreeSpaceContiguous(&free_space,uj);CHKERRQ(ierr); 2751 ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 2752 ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr); 2753 2754 } /* end of case: levels>0 || (levels=0 && !perm_identity) */ 2755 2756 /* put together the new matrix in MATSEQSBAIJ format */ 2757 2758 b = (Mat_SeqSBAIJ*)fact->data; 2759 b->singlemalloc = PETSC_FALSE; 2760 ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr); 2761 b->j = uj; 2762 b->i = ui; 2763 b->diag = udiag; 2764 b->free_diag = PETSC_TRUE; 2765 b->ilen = 0; 2766 b->imax = 0; 2767 b->row = perm; 2768 b->col = perm; 2769 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2770 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2771 b->icol = iperm; 2772 b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ 2773 ierr = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 2774 ierr = PetscLogObjectMemory(fact,(ui[am]-am)*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 2775 b->maxnz = b->nz = ui[am]; 2776 b->free_a = PETSC_TRUE; 2777 b->free_ij = PETSC_TRUE; 2778 2779 fact->info.factor_mallocs = reallocs; 2780 fact->info.fill_ratio_given = fill; 2781 if (ai[am] != 0) { 2782 fact->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]); 2783 } else { 2784 fact->info.fill_ratio_needed = 0.0; 2785 } 2786 fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ_inplace; 2787 PetscFunctionReturn(0); 2788 } 2789 2790 PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS perm,const MatFactorInfo *info) 2791 { 2792 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2793 Mat_SeqSBAIJ *b; 2794 PetscErrorCode ierr; 2795 PetscTruth perm_identity; 2796 PetscReal fill = info->fill; 2797 const PetscInt *rip,*riip; 2798 PetscInt i,am=A->rmap->n,*ai=a->i,*aj=a->j,reallocs=0,prow; 2799 PetscInt *jl,jmin,jmax,nzk,*ui,k,j,*il,nextprow; 2800 PetscInt nlnk,*lnk,ncols,ncols_upper,*cols,*uj,**ui_ptr,*uj_ptr,*udiag; 2801 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 2802 PetscBT lnkbt; 2803 IS iperm; 2804 2805 PetscFunctionBegin; 2806 if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n); 2807 /* check whether perm is the identity mapping */ 2808 ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 2809 ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr); 2810 ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr); 2811 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 2812 2813 /* initialization */ 2814 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr); 2815 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&udiag);CHKERRQ(ierr); 2816 ui[0] = 0; 2817 2818 /* jl: linked list for storing indices of the pivot rows 2819 il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */ 2820 ierr = PetscMalloc4(am,PetscInt*,&ui_ptr,am,PetscInt,&jl,am,PetscInt,&il,am,PetscInt,&cols);CHKERRQ(ierr); 2821 for (i=0; i<am; i++){ 2822 jl[i] = am; il[i] = 0; 2823 } 2824 2825 /* create and initialize a linked list for storing column indices of the active row k */ 2826 nlnk = am + 1; 2827 ierr = PetscLLCreate(am,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2828 2829 /* initial FreeSpace size is fill*(ai[am]+1) */ 2830 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr); 2831 current_space = free_space; 2832 2833 for (k=0; k<am; k++){ /* for each active row k */ 2834 /* initialize lnk by the column indices of row rip[k] of A */ 2835 nzk = 0; 2836 ncols = ai[rip[k]+1] - ai[rip[k]]; 2837 if (!ncols) SETERRQ2(PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k); 2838 ncols_upper = 0; 2839 for (j=0; j<ncols; j++){ 2840 i = riip[*(aj + ai[rip[k]] + j)]; 2841 if (i >= k){ /* only take upper triangular entry */ 2842 cols[ncols_upper] = i; 2843 ncols_upper++; 2844 } 2845 } 2846 ierr = PetscLLAdd(ncols_upper,cols,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2847 nzk += nlnk; 2848 2849 /* update lnk by computing fill-in for each pivot row to be merged in */ 2850 prow = jl[k]; /* 1st pivot row */ 2851 2852 while (prow < k){ 2853 nextprow = jl[prow]; 2854 /* merge prow into k-th row */ 2855 jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */ 2856 jmax = ui[prow+1]; 2857 ncols = jmax-jmin; 2858 uj_ptr = ui_ptr[prow] + jmin - ui[prow]; /* points to the 2nd nzero entry in U(prow,k:am-1) */ 2859 ierr = PetscLLAddSorted(ncols,uj_ptr,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2860 nzk += nlnk; 2861 2862 /* update il and jl for prow */ 2863 if (jmin < jmax){ 2864 il[prow] = jmin; 2865 j = *uj_ptr; jl[prow] = jl[j]; jl[j] = prow; 2866 } 2867 prow = nextprow; 2868 } 2869 2870 /* if free space is not available, make more free space */ 2871 if (current_space->local_remaining<nzk) { 2872 i = am - k + 1; /* num of unfactored rows */ 2873 i *= PetscMin(nzk,i-1); /* i*nzk, i*(i-1): estimated and max additional space needed */ 2874 ierr = PetscFreeSpaceGet(i,¤t_space);CHKERRQ(ierr); 2875 reallocs++; 2876 } 2877 2878 /* copy data into free space, then initialize lnk */ 2879 ierr = PetscLLClean(am,am,nzk,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 2880 2881 /* add the k-th row into il and jl */ 2882 if (nzk > 1){ 2883 i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */ 2884 jl[k] = jl[i]; jl[i] = k; 2885 il[k] = ui[k] + 1; 2886 } 2887 ui_ptr[k] = current_space->array; 2888 current_space->array += nzk; 2889 current_space->local_used += nzk; 2890 current_space->local_remaining -= nzk; 2891 2892 ui[k+1] = ui[k] + nzk; 2893 } 2894 2895 #if defined(PETSC_USE_INFO) 2896 if (ai[am] != 0) { 2897 PetscReal af = (PetscReal)(ui[am])/((PetscReal)ai[am]); 2898 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr); 2899 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 2900 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr); 2901 } else { 2902 ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr); 2903 } 2904 #endif 2905 2906 ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); 2907 ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr); 2908 ierr = PetscFree4(ui_ptr,jl,il,cols);CHKERRQ(ierr); 2909 2910 /* destroy list of free space and other temporary array(s) */ 2911 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2912 ierr = PetscFreeSpaceContiguous_Cholesky(&free_space,uj,am,ui,udiag);CHKERRQ(ierr); /* store matrix factor */ 2913 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 2914 2915 /* put together the new matrix in MATSEQSBAIJ format */ 2916 2917 b = (Mat_SeqSBAIJ*)fact->data; 2918 b->singlemalloc = PETSC_FALSE; 2919 b->free_a = PETSC_TRUE; 2920 b->free_ij = PETSC_TRUE; 2921 ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr); 2922 b->j = uj; 2923 b->i = ui; 2924 b->diag = udiag; 2925 b->free_diag = PETSC_TRUE; 2926 b->ilen = 0; 2927 b->imax = 0; 2928 b->row = perm; 2929 b->col = perm; 2930 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2931 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2932 b->icol = iperm; 2933 b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ 2934 ierr = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 2935 ierr = PetscLogObjectMemory(fact,ui[am]*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 2936 b->maxnz = b->nz = ui[am]; 2937 2938 fact->info.factor_mallocs = reallocs; 2939 fact->info.fill_ratio_given = fill; 2940 if (ai[am] != 0) { 2941 fact->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]); 2942 } else { 2943 fact->info.fill_ratio_needed = 0.0; 2944 } 2945 fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ; 2946 PetscFunctionReturn(0); 2947 } 2948 2949 #undef __FUNCT__ 2950 #define __FUNCT__ "MatCholeskyFactorSymbolic_SeqAIJ_inplace" 2951 PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJ_inplace(Mat fact,Mat A,IS perm,const MatFactorInfo *info) 2952 { 2953 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2954 Mat_SeqSBAIJ *b; 2955 PetscErrorCode ierr; 2956 PetscTruth perm_identity; 2957 PetscReal fill = info->fill; 2958 const PetscInt *rip,*riip; 2959 PetscInt i,am=A->rmap->n,*ai=a->i,*aj=a->j,reallocs=0,prow; 2960 PetscInt *jl,jmin,jmax,nzk,*ui,k,j,*il,nextprow; 2961 PetscInt nlnk,*lnk,ncols,ncols_upper,*cols,*uj,**ui_ptr,*uj_ptr; 2962 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 2963 PetscBT lnkbt; 2964 IS iperm; 2965 2966 PetscFunctionBegin; 2967 if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n); 2968 /* check whether perm is the identity mapping */ 2969 ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 2970 ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr); 2971 ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr); 2972 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 2973 2974 /* initialization */ 2975 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr); 2976 ui[0] = 0; 2977 2978 /* jl: linked list for storing indices of the pivot rows 2979 il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */ 2980 ierr = PetscMalloc4(am,PetscInt*,&ui_ptr,am,PetscInt,&jl,am,PetscInt,&il,am,PetscInt,&cols);CHKERRQ(ierr); 2981 for (i=0; i<am; i++){ 2982 jl[i] = am; il[i] = 0; 2983 } 2984 2985 /* create and initialize a linked list for storing column indices of the active row k */ 2986 nlnk = am + 1; 2987 ierr = PetscLLCreate(am,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2988 2989 /* initial FreeSpace size is fill*(ai[am]+1) */ 2990 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr); 2991 current_space = free_space; 2992 2993 for (k=0; k<am; k++){ /* for each active row k */ 2994 /* initialize lnk by the column indices of row rip[k] of A */ 2995 nzk = 0; 2996 ncols = ai[rip[k]+1] - ai[rip[k]]; 2997 if (!ncols) SETERRQ2(PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k); 2998 ncols_upper = 0; 2999 for (j=0; j<ncols; j++){ 3000 i = riip[*(aj + ai[rip[k]] + j)]; 3001 if (i >= k){ /* only take upper triangular entry */ 3002 cols[ncols_upper] = i; 3003 ncols_upper++; 3004 } 3005 } 3006 ierr = PetscLLAdd(ncols_upper,cols,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 3007 nzk += nlnk; 3008 3009 /* update lnk by computing fill-in for each pivot row to be merged in */ 3010 prow = jl[k]; /* 1st pivot row */ 3011 3012 while (prow < k){ 3013 nextprow = jl[prow]; 3014 /* merge prow into k-th row */ 3015 jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */ 3016 jmax = ui[prow+1]; 3017 ncols = jmax-jmin; 3018 uj_ptr = ui_ptr[prow] + jmin - ui[prow]; /* points to the 2nd nzero entry in U(prow,k:am-1) */ 3019 ierr = PetscLLAddSorted(ncols,uj_ptr,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 3020 nzk += nlnk; 3021 3022 /* update il and jl for prow */ 3023 if (jmin < jmax){ 3024 il[prow] = jmin; 3025 j = *uj_ptr; jl[prow] = jl[j]; jl[j] = prow; 3026 } 3027 prow = nextprow; 3028 } 3029 3030 /* if free space is not available, make more free space */ 3031 if (current_space->local_remaining<nzk) { 3032 i = am - k + 1; /* num of unfactored rows */ 3033 i = PetscMin(i*nzk, i*(i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */ 3034 ierr = PetscFreeSpaceGet(i,¤t_space);CHKERRQ(ierr); 3035 reallocs++; 3036 } 3037 3038 /* copy data into free space, then initialize lnk */ 3039 ierr = PetscLLClean(am,am,nzk,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 3040 3041 /* add the k-th row into il and jl */ 3042 if (nzk-1 > 0){ 3043 i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */ 3044 jl[k] = jl[i]; jl[i] = k; 3045 il[k] = ui[k] + 1; 3046 } 3047 ui_ptr[k] = current_space->array; 3048 current_space->array += nzk; 3049 current_space->local_used += nzk; 3050 current_space->local_remaining -= nzk; 3051 3052 ui[k+1] = ui[k] + nzk; 3053 } 3054 3055 #if defined(PETSC_USE_INFO) 3056 if (ai[am] != 0) { 3057 PetscReal af = (PetscReal)(ui[am])/((PetscReal)ai[am]); 3058 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr); 3059 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 3060 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr); 3061 } else { 3062 ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr); 3063 } 3064 #endif 3065 3066 ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); 3067 ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr); 3068 ierr = PetscFree4(ui_ptr,jl,il,cols);CHKERRQ(ierr); 3069 3070 /* destroy list of free space and other temporary array(s) */ 3071 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 3072 ierr = PetscFreeSpaceContiguous(&free_space,uj);CHKERRQ(ierr); 3073 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 3074 3075 /* put together the new matrix in MATSEQSBAIJ format */ 3076 3077 b = (Mat_SeqSBAIJ*)fact->data; 3078 b->singlemalloc = PETSC_FALSE; 3079 b->free_a = PETSC_TRUE; 3080 b->free_ij = PETSC_TRUE; 3081 ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr); 3082 b->j = uj; 3083 b->i = ui; 3084 b->diag = 0; 3085 b->ilen = 0; 3086 b->imax = 0; 3087 b->row = perm; 3088 b->col = perm; 3089 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 3090 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 3091 b->icol = iperm; 3092 b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ 3093 ierr = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 3094 ierr = PetscLogObjectMemory(fact,(ui[am]-am)*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 3095 b->maxnz = b->nz = ui[am]; 3096 3097 fact->info.factor_mallocs = reallocs; 3098 fact->info.fill_ratio_given = fill; 3099 if (ai[am] != 0) { 3100 fact->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]); 3101 } else { 3102 fact->info.fill_ratio_needed = 0.0; 3103 } 3104 fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ_inplace; 3105 PetscFunctionReturn(0); 3106 } 3107 3108 #undef __FUNCT__ 3109 #define __FUNCT__ "MatSolve_SeqAIJ_NaturalOrdering" 3110 PetscErrorCode MatSolve_SeqAIJ_NaturalOrdering(Mat A,Vec bb,Vec xx) 3111 { 3112 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3113 PetscErrorCode ierr; 3114 PetscInt n = A->rmap->n; 3115 const PetscInt *ai = a->i,*aj = a->j,*adiag = a->diag,*vi; 3116 PetscScalar *x,sum; 3117 const PetscScalar *b; 3118 const MatScalar *aa = a->a,*v; 3119 PetscInt i,nz; 3120 3121 PetscFunctionBegin; 3122 if (!n) PetscFunctionReturn(0); 3123 3124 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 3125 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 3126 3127 /* forward solve the lower triangular */ 3128 x[0] = b[0]; 3129 v = aa; 3130 vi = aj; 3131 for (i=1; i<n; i++) { 3132 nz = ai[i+1] - ai[i]; 3133 sum = b[i]; 3134 PetscSparseDenseMinusDot(sum,x,v,vi,nz); 3135 v += nz; 3136 vi += nz; 3137 x[i] = sum; 3138 } 3139 3140 /* backward solve the upper triangular */ 3141 for (i=n-1; i>=0; i--){ 3142 v = aa + adiag[i+1] + 1; 3143 vi = aj + adiag[i+1] + 1; 3144 nz = adiag[i] - adiag[i+1]-1; 3145 sum = x[i]; 3146 PetscSparseDenseMinusDot(sum,x,v,vi,nz); 3147 x[i] = sum*v[nz]; /* x[i]=aa[adiag[i]]*sum; v++; */ 3148 } 3149 3150 ierr = PetscLogFlops(2.0*a->nz - A->cmap->n);CHKERRQ(ierr); 3151 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 3152 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 3153 PetscFunctionReturn(0); 3154 } 3155 3156 #undef __FUNCT__ 3157 #define __FUNCT__ "MatSolve_SeqAIJ" 3158 PetscErrorCode MatSolve_SeqAIJ(Mat A,Vec bb,Vec xx) 3159 { 3160 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3161 IS iscol = a->col,isrow = a->row; 3162 PetscErrorCode ierr; 3163 PetscInt i,n=A->rmap->n,*vi,*ai=a->i,*aj=a->j,*adiag = a->diag,nz; 3164 const PetscInt *rout,*cout,*r,*c; 3165 PetscScalar *x,*tmp,sum; 3166 const PetscScalar *b; 3167 const MatScalar *aa = a->a,*v; 3168 3169 PetscFunctionBegin; 3170 if (!n) PetscFunctionReturn(0); 3171 3172 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 3173 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 3174 tmp = a->solve_work; 3175 3176 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 3177 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 3178 3179 /* forward solve the lower triangular */ 3180 tmp[0] = b[r[0]]; 3181 v = aa; 3182 vi = aj; 3183 for (i=1; i<n; i++) { 3184 nz = ai[i+1] - ai[i]; 3185 sum = b[r[i]]; 3186 PetscSparseDenseMinusDot(sum,tmp,v,vi,nz); 3187 tmp[i] = sum; 3188 v += nz; vi += nz; 3189 } 3190 3191 /* backward solve the upper triangular */ 3192 for (i=n-1; i>=0; i--){ 3193 v = aa + adiag[i+1]+1; 3194 vi = aj + adiag[i+1]+1; 3195 nz = adiag[i]-adiag[i+1]-1; 3196 sum = tmp[i]; 3197 PetscSparseDenseMinusDot(sum,tmp,v,vi,nz); 3198 x[c[i]] = tmp[i] = sum*v[nz]; /* v[nz] = aa[adiag[i]] */ 3199 } 3200 3201 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 3202 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 3203 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 3204 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 3205 ierr = PetscLogFlops(2*a->nz - A->cmap->n);CHKERRQ(ierr); 3206 PetscFunctionReturn(0); 3207 } 3208 3209 #undef __FUNCT__ 3210 #define __FUNCT__ "MatILUDTFactor_SeqAIJ" 3211 /* 3212 This will get a new name and become a varient of MatILUFactor_SeqAIJ() there is no longer seperate functions in the matrix function table for dt factors 3213 */ 3214 PetscErrorCode MatILUDTFactor_SeqAIJ(Mat A,IS isrow,IS iscol,const MatFactorInfo *info,Mat *fact) 3215 { 3216 Mat B = *fact; 3217 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b; 3218 IS isicol; 3219 PetscErrorCode ierr; 3220 const PetscInt *r,*ic; 3221 PetscInt i,n=A->rmap->n,*ai=a->i,*aj=a->j,*ajtmp,*adiag; 3222 PetscInt *bi,*bj,*bdiag,*bdiag_rev; 3223 PetscInt row,nzi,nzi_bl,nzi_bu,*im,nzi_al,nzi_au; 3224 PetscInt nlnk,*lnk; 3225 PetscBT lnkbt; 3226 PetscTruth row_identity,icol_identity,both_identity; 3227 MatScalar *aatmp,*pv,*batmp,*ba,*rtmp,*pc,multiplier,*vtmp,diag_tmp; 3228 const PetscInt *ics; 3229 PetscInt j,nz,*pj,*bjtmp,k,ncut,*jtmp; 3230 PetscReal dt=info->dt,dtcol=info->dtcol,shift=info->shiftinblocks; 3231 PetscInt dtcount=(PetscInt)info->dtcount,nnz_max; 3232 PetscTruth missing; 3233 3234 PetscFunctionBegin; 3235 3236 if (dt == PETSC_DEFAULT) dt = 0.005; 3237 if (dtcol == PETSC_DEFAULT) dtcol = 0.01; /* XXX unused! */ 3238 if (dtcount == PETSC_DEFAULT) dtcount = (PetscInt)(1.5*a->rmax); 3239 3240 /* ------- symbolic factorization, can be reused ---------*/ 3241 ierr = MatMissingDiagonal(A,&missing,&i);CHKERRQ(ierr); 3242 if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",i); 3243 adiag=a->diag; 3244 3245 ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 3246 3247 /* bdiag is location of diagonal in factor */ 3248 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr); /* becomes b->diag */ 3249 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag_rev);CHKERRQ(ierr); /* temporary */ 3250 3251 /* allocate row pointers bi */ 3252 ierr = PetscMalloc((2*n+2)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 3253 3254 /* allocate bj and ba; max num of nonzero entries is (ai[n]+2*n*dtcount+2) */ 3255 if (dtcount > n-1) dtcount = n-1; /* diagonal is excluded */ 3256 nnz_max = ai[n]+2*n*dtcount+2; 3257 3258 ierr = PetscMalloc((nnz_max+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 3259 ierr = PetscMalloc((nnz_max+1)*sizeof(MatScalar),&ba);CHKERRQ(ierr); 3260 3261 /* put together the new matrix */ 3262 ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr); 3263 ierr = PetscLogObjectParent(B,isicol);CHKERRQ(ierr); 3264 b = (Mat_SeqAIJ*)B->data; 3265 b->free_a = PETSC_TRUE; 3266 b->free_ij = PETSC_TRUE; 3267 b->singlemalloc = PETSC_FALSE; 3268 b->a = ba; 3269 b->j = bj; 3270 b->i = bi; 3271 b->diag = bdiag; 3272 b->ilen = 0; 3273 b->imax = 0; 3274 b->row = isrow; 3275 b->col = iscol; 3276 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 3277 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 3278 b->icol = isicol; 3279 ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 3280 3281 ierr = PetscLogObjectMemory(B,nnz_max*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 3282 b->maxnz = nnz_max; 3283 3284 B->factor = MAT_FACTOR_ILUDT; 3285 B->info.factor_mallocs = 0; 3286 B->info.fill_ratio_given = ((PetscReal)nnz_max)/((PetscReal)ai[n]); 3287 CHKMEMQ; 3288 /* ------- end of symbolic factorization ---------*/ 3289 3290 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 3291 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 3292 ics = ic; 3293 3294 /* linked list for storing column indices of the active row */ 3295 nlnk = n + 1; 3296 ierr = PetscLLCreate(n,n,nlnk,lnk,lnkbt);CHKERRQ(ierr); 3297 3298 /* im: used by PetscLLAddSortedLU(); jtmp: working array for column indices of active row */ 3299 ierr = PetscMalloc2(n,PetscInt,&im,n,PetscInt,&jtmp);CHKERRQ(ierr); 3300 /* rtmp, vtmp: working arrays for sparse and contiguous row entries of active row */ 3301 ierr = PetscMalloc2(n,MatScalar,&rtmp,n,MatScalar,&vtmp);CHKERRQ(ierr); 3302 ierr = PetscMemzero(rtmp,n*sizeof(MatScalar));CHKERRQ(ierr); 3303 3304 bi[0] = 0; 3305 bdiag[0] = nnz_max-1; /* location of diag[0] in factor B */ 3306 bdiag_rev[n] = bdiag[0]; 3307 bi[2*n+1] = bdiag[0]+1; /* endof bj and ba array */ 3308 for (i=0; i<n; i++) { 3309 /* copy initial fill into linked list */ 3310 nzi = 0; /* nonzeros for active row i */ 3311 nzi = ai[r[i]+1] - ai[r[i]]; 3312 if (!nzi) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i); 3313 nzi_al = adiag[r[i]] - ai[r[i]]; 3314 nzi_au = ai[r[i]+1] - adiag[r[i]] -1; 3315 ajtmp = aj + ai[r[i]]; 3316 ierr = PetscLLAddPerm(nzi,ajtmp,ic,n,nlnk,lnk,lnkbt);CHKERRQ(ierr); 3317 3318 /* load in initial (unfactored row) */ 3319 aatmp = a->a + ai[r[i]]; 3320 for (j=0; j<nzi; j++) { 3321 rtmp[ics[*ajtmp++]] = *aatmp++; 3322 } 3323 3324 /* add pivot rows into linked list */ 3325 row = lnk[n]; 3326 while (row < i ) { 3327 nzi_bl = bi[row+1] - bi[row] + 1; 3328 bjtmp = bj + bdiag[row+1]+1; /* points to 1st column next to the diagonal in U */ 3329 ierr = PetscLLAddSortedLU(bjtmp,row,nlnk,lnk,lnkbt,i,nzi_bl,im);CHKERRQ(ierr); 3330 nzi += nlnk; 3331 row = lnk[row]; 3332 } 3333 3334 /* copy data from lnk into jtmp, then initialize lnk */ 3335 ierr = PetscLLClean(n,n,nzi,lnk,jtmp,lnkbt);CHKERRQ(ierr); 3336 3337 /* numerical factorization */ 3338 bjtmp = jtmp; 3339 row = *bjtmp++; /* 1st pivot row */ 3340 while ( row < i ) { 3341 pc = rtmp + row; 3342 pv = ba + bdiag[row]; /* 1./(diag of the pivot row) */ 3343 multiplier = (*pc) * (*pv); 3344 *pc = multiplier; 3345 if (PetscAbsScalar(*pc) > dt){ /* apply tolerance dropping rule */ 3346 pj = bj + bdiag[row+1] + 1; /* point to 1st entry of U(row,:) */ 3347 pv = ba + bdiag[row+1] + 1; 3348 /* if (multiplier < -1.0 or multiplier >1.0) printf("row/prow %d, %d, multiplier %g\n",i,row,multiplier); */ 3349 nz = bdiag[row] - bdiag[row+1] - 1; /* num of entries in U(row,:), excluding diagonal */ 3350 for (j=0; j<nz; j++) rtmp[*pj++] -= multiplier * (*pv++); 3351 ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr); 3352 } 3353 row = *bjtmp++; 3354 } 3355 3356 /* copy sparse rtmp into contiguous vtmp; separate L and U part */ 3357 diag_tmp = rtmp[i]; /* save diagonal value - may not needed?? */ 3358 nzi_bl = 0; j = 0; 3359 while (jtmp[j] < i){ /* Note: jtmp is sorted */ 3360 vtmp[j] = rtmp[jtmp[j]]; rtmp[jtmp[j]]=0.0; 3361 nzi_bl++; j++; 3362 } 3363 nzi_bu = nzi - nzi_bl -1; 3364 while (j < nzi){ 3365 vtmp[j] = rtmp[jtmp[j]]; rtmp[jtmp[j]]=0.0; 3366 j++; 3367 } 3368 3369 bjtmp = bj + bi[i]; 3370 batmp = ba + bi[i]; 3371 /* apply level dropping rule to L part */ 3372 ncut = nzi_al + dtcount; 3373 if (ncut < nzi_bl){ 3374 ierr = PetscSortSplit(ncut,nzi_bl,vtmp,jtmp);CHKERRQ(ierr); 3375 ierr = PetscSortIntWithScalarArray(ncut,jtmp,vtmp);CHKERRQ(ierr); 3376 } else { 3377 ncut = nzi_bl; 3378 } 3379 for (j=0; j<ncut; j++){ 3380 bjtmp[j] = jtmp[j]; 3381 batmp[j] = vtmp[j]; 3382 /* printf(" (%d,%g),",bjtmp[j],batmp[j]); */ 3383 } 3384 bi[i+1] = bi[i] + ncut; 3385 nzi = ncut + 1; 3386 3387 /* apply level dropping rule to U part */ 3388 ncut = nzi_au + dtcount; 3389 if (ncut < nzi_bu){ 3390 ierr = PetscSortSplit(ncut,nzi_bu,vtmp+nzi_bl+1,jtmp+nzi_bl+1);CHKERRQ(ierr); 3391 ierr = PetscSortIntWithScalarArray(ncut,jtmp+nzi_bl+1,vtmp+nzi_bl+1);CHKERRQ(ierr); 3392 } else { 3393 ncut = nzi_bu; 3394 } 3395 nzi += ncut; 3396 3397 /* mark bdiagonal */ 3398 bdiag[i+1] = bdiag[i] - (ncut + 1); 3399 bdiag_rev[n-i-1] = bdiag[i+1]; 3400 bi[2*n - i] = bi[2*n - i +1] - (ncut + 1); 3401 bjtmp = bj + bdiag[i]; 3402 batmp = ba + bdiag[i]; 3403 *bjtmp = i; 3404 *batmp = diag_tmp; /* rtmp[i]; */ 3405 if (*batmp == 0.0) { 3406 *batmp = dt+shift; 3407 /* printf(" row %d add shift %g\n",i,shift); */ 3408 } 3409 *batmp = 1.0/(*batmp); /* invert diagonal entries for simplier triangular solves */ 3410 /* printf(" (%d,%g),",*bjtmp,*batmp); */ 3411 3412 bjtmp = bj + bdiag[i+1]+1; 3413 batmp = ba + bdiag[i+1]+1; 3414 for (k=0; k<ncut; k++){ 3415 bjtmp[k] = jtmp[nzi_bl+1+k]; 3416 batmp[k] = vtmp[nzi_bl+1+k]; 3417 /* printf(" (%d,%g),",bjtmp[k],batmp[k]); */ 3418 } 3419 /* printf("\n"); */ 3420 3421 im[i] = nzi; /* used by PetscLLAddSortedLU() */ 3422 /* 3423 printf("row %d: bi %d, bdiag %d\n",i,bi[i],bdiag[i]); 3424 printf(" ----------------------------\n"); 3425 */ 3426 } /* for (i=0; i<n; i++) */ 3427 /* printf("end of L %d, beginning of U %d\n",bi[n],bdiag[n]); */ 3428 if (bi[n] >= bdiag[n]) SETERRQ2(PETSC_ERR_ARG_SIZ,"end of L array %d cannot >= the beginning of U array %d",bi[n],bdiag[n]); 3429 3430 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 3431 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 3432 3433 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 3434 ierr = PetscFree2(im,jtmp);CHKERRQ(ierr); 3435 ierr = PetscFree2(rtmp,vtmp);CHKERRQ(ierr); 3436 ierr = PetscFree(bdiag_rev);CHKERRQ(ierr); 3437 3438 ierr = PetscLogFlops(B->cmap->n);CHKERRQ(ierr); 3439 b->maxnz = b->nz = bi[n] + bdiag[0] - bdiag[n]; 3440 3441 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 3442 ierr = ISIdentity(isicol,&icol_identity);CHKERRQ(ierr); 3443 both_identity = (PetscTruth) (row_identity && icol_identity); 3444 if (row_identity && icol_identity) { 3445 B->ops->solve = MatSolve_SeqAIJ_NaturalOrdering; 3446 } else { 3447 B->ops->solve = MatSolve_SeqAIJ; 3448 } 3449 3450 B->ops->solveadd = 0; 3451 B->ops->solvetranspose = 0; 3452 B->ops->solvetransposeadd = 0; 3453 B->ops->matsolve = 0; 3454 B->assembled = PETSC_TRUE; 3455 B->preallocated = PETSC_TRUE; 3456 PetscFunctionReturn(0); 3457 } 3458 3459 /* a wraper of MatILUDTFactor_SeqAIJ() */ 3460 #undef __FUNCT__ 3461 #define __FUNCT__ "MatILUDTFactorSymbolic_SeqAIJ" 3462 /* 3463 This will get a new name and become a varient of MatILUFactor_SeqAIJ() there is no longer seperate functions in the matrix function table for dt factors 3464 */ 3465 3466 PetscErrorCode PETSCMAT_DLLEXPORT MatILUDTFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS row,IS col,const MatFactorInfo *info) 3467 { 3468 PetscErrorCode ierr; 3469 3470 PetscFunctionBegin; 3471 ierr = MatILUDTFactor_SeqAIJ(A,row,col,info,&fact);CHKERRQ(ierr); 3472 PetscFunctionReturn(0); 3473 } 3474 3475 /* 3476 same as MatLUFactorNumeric_SeqAIJ(), except using contiguous array matrix factors 3477 - intend to replace existing MatLUFactorNumeric_SeqAIJ() 3478 */ 3479 #undef __FUNCT__ 3480 #define __FUNCT__ "MatILUDTFactorNumeric_SeqAIJ" 3481 /* 3482 This will get a new name and become a varient of MatILUFactor_SeqAIJ() there is no longer seperate functions in the matrix function table for dt factors 3483 */ 3484 3485 PetscErrorCode PETSCMAT_DLLEXPORT MatILUDTFactorNumeric_SeqAIJ(Mat fact,Mat A,const MatFactorInfo *info) 3486 { 3487 Mat C=fact; 3488 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ *)C->data; 3489 IS isrow = b->row,isicol = b->icol; 3490 PetscErrorCode ierr; 3491 const PetscInt *r,*ic,*ics; 3492 PetscInt i,j,k,n=A->rmap->n,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j; 3493 PetscInt *ajtmp,*bjtmp,nz,nzl,nzu,row,*bdiag = b->diag,*pj; 3494 MatScalar *rtmp,*pc,multiplier,*v,*pv,*aa=a->a; 3495 PetscReal dt=info->dt,shift=info->shiftinblocks; 3496 PetscTruth row_identity, col_identity; 3497 3498 PetscFunctionBegin; 3499 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 3500 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 3501 ierr = PetscMalloc((n+1)*sizeof(MatScalar),&rtmp);CHKERRQ(ierr); 3502 ics = ic; 3503 3504 for (i=0; i<n; i++){ 3505 /* initialize rtmp array */ 3506 nzl = bi[i+1] - bi[i]; /* num of nozeros in L(i,:) */ 3507 bjtmp = bj + bi[i]; 3508 for (j=0; j<nzl; j++) rtmp[*bjtmp++] = 0.0; 3509 rtmp[i] = 0.0; 3510 nzu = bdiag[i] - bdiag[i+1]; /* num of nozeros in U(i,:) */ 3511 bjtmp = bj + bdiag[i+1] + 1; 3512 for (j=0; j<nzu; j++) rtmp[*bjtmp++] = 0.0; 3513 3514 /* load in initial unfactored row of A */ 3515 /* printf("row %d\n",i); */ 3516 nz = ai[r[i]+1] - ai[r[i]]; 3517 ajtmp = aj + ai[r[i]]; 3518 v = aa + ai[r[i]]; 3519 for (j=0; j<nz; j++) { 3520 rtmp[ics[*ajtmp++]] = v[j]; 3521 /* printf(" (%d,%g),",ics[ajtmp[j]],rtmp[ics[ajtmp[j]]]); */ 3522 } 3523 /* printf("\n"); */ 3524 3525 /* numerical factorization */ 3526 bjtmp = bj + bi[i]; /* point to 1st entry of L(i,:) */ 3527 nzl = bi[i+1] - bi[i]; /* num of entries in L(i,:) */ 3528 k = 0; 3529 while (k < nzl){ 3530 row = *bjtmp++; 3531 /* printf(" prow %d\n",row); */ 3532 pc = rtmp + row; 3533 pv = b->a + bdiag[row]; /* 1./(diag of the pivot row) */ 3534 multiplier = (*pc) * (*pv); 3535 *pc = multiplier; 3536 if (PetscAbsScalar(multiplier) > dt){ 3537 pj = bj + bdiag[row+1] + 1; /* point to 1st entry of U(row,:) */ 3538 pv = b->a + bdiag[row+1] + 1; 3539 nz = bdiag[row] - bdiag[row+1] - 1; /* num of entries in U(row,:), excluding diagonal */ 3540 for (j=0; j<nz; j++) rtmp[*pj++] -= multiplier * (*pv++); 3541 /* ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr); */ 3542 } 3543 k++; 3544 } 3545 3546 /* finished row so stick it into b->a */ 3547 /* L-part */ 3548 pv = b->a + bi[i] ; 3549 pj = bj + bi[i] ; 3550 nzl = bi[i+1] - bi[i]; 3551 for (j=0; j<nzl; j++) { 3552 pv[j] = rtmp[pj[j]]; 3553 /* printf(" (%d,%g),",pj[j],pv[j]); */ 3554 } 3555 3556 /* diagonal: invert diagonal entries for simplier triangular solves */ 3557 if (rtmp[i] == 0.0) rtmp[i] = dt+shift; 3558 b->a[bdiag[i]] = 1.0/rtmp[i]; 3559 /* printf(" (%d,%g),",i,b->a[bdiag[i]]); */ 3560 3561 /* U-part */ 3562 pv = b->a + bdiag[i+1] + 1; 3563 pj = bj + bdiag[i+1] + 1; 3564 nzu = bdiag[i] - bdiag[i+1] - 1; 3565 for (j=0; j<nzu; j++) { 3566 pv[j] = rtmp[pj[j]]; 3567 /* printf(" (%d,%g),",pj[j],pv[j]); */ 3568 } 3569 /* printf("\n"); */ 3570 } 3571 3572 ierr = PetscFree(rtmp);CHKERRQ(ierr); 3573 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 3574 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 3575 3576 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 3577 ierr = ISIdentity(isicol,&col_identity);CHKERRQ(ierr); 3578 if (row_identity && col_identity) { 3579 C->ops->solve = MatSolve_SeqAIJ_NaturalOrdering; 3580 } else { 3581 C->ops->solve = MatSolve_SeqAIJ; 3582 } 3583 C->ops->solveadd = 0; 3584 C->ops->solvetranspose = 0; 3585 C->ops->solvetransposeadd = 0; 3586 C->ops->matsolve = 0; 3587 C->assembled = PETSC_TRUE; 3588 C->preallocated = PETSC_TRUE; 3589 ierr = PetscLogFlops(C->cmap->n);CHKERRQ(ierr); 3590 PetscFunctionReturn(0); 3591 } 3592