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