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