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