1 #define PETSCMAT_DLL 2 3 4 #include "../src/mat/impls/aij/seq/aij.h" 5 #include "petscbt.h" 6 #include "../src/mat/utils/freespace.h" 7 8 EXTERN_C_BEGIN 9 #undef __FUNCT__ 10 #define __FUNCT__ "MatOrdering_Flow_SeqAIJ" 11 /* 12 Computes an ordering to get most of the large numerical values in the lower triangular part of the matrix 13 */ 14 PetscErrorCode MatOrdering_Flow_SeqAIJ(Mat mat,const MatOrderingType type,IS *irow,IS *icol) 15 { 16 Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->data; 17 PetscErrorCode ierr; 18 PetscInt i,j,jj,k, kk,n = mat->rmap->n, current = 0, newcurrent = 0,*order; 19 const PetscInt *ai = a->i, *aj = a->j; 20 const PetscScalar *aa = a->a; 21 PetscTruth *done; 22 PetscReal best,past = 0,future; 23 24 PetscFunctionBegin; 25 /* pick initial row */ 26 best = -1; 27 for (i=0; i<n; i++) { 28 future = 0; 29 for (j=ai[i]; j<ai[i+1]; j++) { 30 if (aj[j] != i) future += PetscAbsScalar(aa[j]); else past = PetscAbsScalar(aa[j]); 31 } 32 if (!future) future = 1.e-10; /* if there is zero in the upper diagonal part want to rank this row high */ 33 if (past/future > best) { 34 best = past/future; 35 current = i; 36 } 37 } 38 39 ierr = PetscMalloc(n*sizeof(PetscTruth),&done);CHKERRQ(ierr); 40 ierr = PetscMalloc(n*sizeof(PetscInt),&order);CHKERRQ(ierr); 41 ierr = PetscMemzero(done,n*sizeof(PetscTruth));CHKERRQ(ierr); 42 order[0] = current; 43 for (i=0; i<n-1; i++) { 44 done[current] = PETSC_TRUE; 45 best = -1; 46 /* loop over all neighbors of current pivot */ 47 for (j=ai[current]; j<ai[current+1]; j++) { 48 jj = aj[j]; 49 if (done[jj]) continue; 50 /* loop over columns of potential next row computing weights for below and above diagonal */ 51 past = future = 0.0; 52 for (k=ai[jj]; k<ai[jj+1]; k++) { 53 kk = aj[k]; 54 if (done[kk]) past += PetscAbsScalar(aa[k]); 55 else if (kk != jj) future += PetscAbsScalar(aa[k]); 56 } 57 if (!future) future = 1.e-10; /* if there is zero in the upper diagonal part want to rank this row high */ 58 if (past/future > best) { 59 best = past/future; 60 newcurrent = jj; 61 } 62 } 63 if (best == -1) { /* no neighbors to select from so select best of all that remain */ 64 best = -1; 65 for (k=0; k<n; k++) { 66 if (done[k]) continue; 67 future = 0; 68 past = 0; 69 for (j=ai[k]; j<ai[k+1]; j++) { 70 kk = aj[j]; 71 if (done[kk]) past += PetscAbsScalar(aa[j]); 72 else if (kk != k) future += PetscAbsScalar(aa[j]); 73 } 74 if (!future) future = 1.e-10; /* if there is zero in the upper diagonal part want to rank this row high */ 75 if (past/future > best) { 76 best = past/future; 77 newcurrent = k; 78 } 79 } 80 } 81 if (current == newcurrent) SETERRQ(PETSC_ERR_PLIB,"newcurrent cannot be current"); 82 current = newcurrent; 83 order[i+1] = current; 84 } 85 ierr = ISCreateGeneral(PETSC_COMM_SELF,n,order,irow);CHKERRQ(ierr); 86 *icol = *irow; 87 ierr = PetscObjectReference((PetscObject)*irow);CHKERRQ(ierr); 88 ierr = PetscFree(done);CHKERRQ(ierr); 89 ierr = PetscFree(order);CHKERRQ(ierr); 90 PetscFunctionReturn(0); 91 } 92 EXTERN_C_END 93 94 EXTERN_C_BEGIN 95 #undef __FUNCT__ 96 #define __FUNCT__ "MatGetFactorAvailable_seqaij_petsc" 97 PetscErrorCode MatGetFactorAvailable_seqaij_petsc(Mat A,MatFactorType ftype,PetscTruth *flg) 98 { 99 PetscFunctionBegin; 100 *flg = PETSC_TRUE; 101 PetscFunctionReturn(0); 102 } 103 EXTERN_C_END 104 105 EXTERN_C_BEGIN 106 #undef __FUNCT__ 107 #define __FUNCT__ "MatGetFactor_seqaij_petsc" 108 PetscErrorCode MatGetFactor_seqaij_petsc(Mat A,MatFactorType ftype,Mat *B) 109 { 110 PetscInt n = A->rmap->n; 111 PetscErrorCode ierr; 112 113 PetscFunctionBegin; 114 ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr); 115 ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr); 116 if (ftype == MAT_FACTOR_LU || ftype == MAT_FACTOR_ILU || ftype == MAT_FACTOR_ILUDT){ 117 ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr); 118 (*B)->ops->ilufactorsymbolic = MatILUFactorSymbolic_SeqAIJ; 119 (*B)->ops->lufactorsymbolic = MatLUFactorSymbolic_SeqAIJ; 120 (*B)->ops->iludtfactor = MatILUDTFactor_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" 134 PetscErrorCode MatLUFactorSymbolic_SeqAIJ(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 PetscTruth newdatastruct=PETSC_FALSE; 148 149 PetscFunctionBegin; 150 ierr = PetscOptionsGetTruth(PETSC_NULL,"-lu_new",&newdatastruct,PETSC_NULL);CHKERRQ(ierr); 151 if(newdatastruct){ 152 ierr = MatLUFactorSymbolic_SeqAIJ_newdatastruct(B,A,isrow,iscol,info);CHKERRQ(ierr); 153 PetscFunctionReturn(0); 154 } 155 156 if (A->rmap->N != A->cmap->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square"); 157 ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 158 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 159 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 160 161 /* get new row pointers */ 162 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 163 bi[0] = 0; 164 165 /* bdiag is location of diagonal in factor */ 166 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr); 167 bdiag[0] = 0; 168 169 /* linked list for storing column indices of the active row */ 170 nlnk = n + 1; 171 ierr = PetscLLCreate(n,n,nlnk,lnk,lnkbt);CHKERRQ(ierr); 172 173 ierr = PetscMalloc2(n+1,PetscInt**,&bi_ptr,n+1,PetscInt,&im);CHKERRQ(ierr); 174 175 /* initial FreeSpace size is f*(ai[n]+1) */ 176 f = info->fill; 177 ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space);CHKERRQ(ierr); 178 current_space = free_space; 179 180 for (i=0; i<n; i++) { 181 /* copy previous fill into linked list */ 182 nzi = 0; 183 nnz = ai[r[i]+1] - ai[r[i]]; 184 if (!nnz) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i); 185 ajtmp = aj + ai[r[i]]; 186 ierr = PetscLLAddPerm(nnz,ajtmp,ic,n,nlnk,lnk,lnkbt);CHKERRQ(ierr); 187 nzi += nlnk; 188 189 /* add pivot rows into linked list */ 190 row = lnk[n]; 191 while (row < i) { 192 nzbd = bdiag[row] - bi[row] + 1; /* num of entries in the row with column index <= row */ 193 ajtmp = bi_ptr[row] + nzbd; /* points to the entry next to the diagonal */ 194 ierr = PetscLLAddSortedLU(ajtmp,row,nlnk,lnk,lnkbt,i,nzbd,im);CHKERRQ(ierr); 195 nzi += nlnk; 196 row = lnk[row]; 197 } 198 bi[i+1] = bi[i] + nzi; 199 im[i] = nzi; 200 201 /* mark bdiag */ 202 nzbd = 0; 203 nnz = nzi; 204 k = lnk[n]; 205 while (nnz-- && k < i){ 206 nzbd++; 207 k = lnk[k]; 208 } 209 bdiag[i] = bi[i] + nzbd; 210 211 /* if free space is not available, make more free space */ 212 if (current_space->local_remaining<nzi) { 213 nnz = (n - i)*nzi; /* estimated and max additional space needed */ 214 ierr = PetscFreeSpaceGet(nnz,¤t_space);CHKERRQ(ierr); 215 reallocs++; 216 } 217 218 /* copy data into free space, then initialize lnk */ 219 ierr = PetscLLClean(n,n,nzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 220 bi_ptr[i] = current_space->array; 221 current_space->array += nzi; 222 current_space->local_used += nzi; 223 current_space->local_remaining -= nzi; 224 } 225 #if defined(PETSC_USE_INFO) 226 if (ai[n] != 0) { 227 PetscReal af = ((PetscReal)bi[n])/((PetscReal)ai[n]); 228 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,f,af);CHKERRQ(ierr); 229 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 230 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G);\n",af);CHKERRQ(ierr); 231 ierr = PetscInfo(A,"for best performance.\n");CHKERRQ(ierr); 232 } else { 233 ierr = PetscInfo(A,"Empty matrix\n");CHKERRQ(ierr); 234 } 235 #endif 236 237 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 238 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 239 240 /* destroy list of free space and other temporary array(s) */ 241 ierr = PetscMalloc((bi[n]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 242 ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr); 243 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 244 ierr = PetscFree2(bi_ptr,im);CHKERRQ(ierr); 245 246 /* put together the new matrix */ 247 ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr); 248 ierr = PetscLogObjectParent(B,isicol);CHKERRQ(ierr); 249 b = (Mat_SeqAIJ*)(B)->data; 250 b->free_a = PETSC_TRUE; 251 b->free_ij = PETSC_TRUE; 252 b->singlemalloc = PETSC_FALSE; 253 ierr = PetscMalloc((bi[n]+1)*sizeof(PetscScalar),&b->a);CHKERRQ(ierr); 254 b->j = bj; 255 b->i = bi; 256 b->diag = bdiag; 257 b->ilen = 0; 258 b->imax = 0; 259 b->row = isrow; 260 b->col = iscol; 261 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 262 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 263 b->icol = isicol; 264 ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 265 266 /* In b structure: Free imax, ilen, old a, old j. Allocate solve_work, new a, new j */ 267 ierr = PetscLogObjectMemory(B,(bi[n]-n)*(sizeof(PetscInt)+sizeof(PetscScalar)));CHKERRQ(ierr); 268 b->maxnz = b->nz = bi[n] ; 269 270 (B)->factor = MAT_FACTOR_LU; 271 (B)->info.factor_mallocs = reallocs; 272 (B)->info.fill_ratio_given = f; 273 274 if (ai[n]) { 275 (B)->info.fill_ratio_needed = ((PetscReal)bi[n])/((PetscReal)ai[n]); 276 } else { 277 (B)->info.fill_ratio_needed = 0.0; 278 } 279 (B)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ; 280 (B)->ops->solve = MatSolve_SeqAIJ; 281 (B)->ops->solvetranspose = MatSolveTranspose_SeqAIJ; 282 /* switch to inodes if appropriate */ 283 ierr = MatLUFactorSymbolic_Inode(B,A,isrow,iscol,info);CHKERRQ(ierr); 284 PetscFunctionReturn(0); 285 } 286 287 #undef __FUNCT__ 288 #define __FUNCT__ "MatLUFactorSymbolic_SeqAIJ_newdatastruct" 289 PetscErrorCode MatLUFactorSymbolic_SeqAIJ_newdatastruct(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 290 { 291 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b; 292 IS isicol; 293 PetscErrorCode ierr; 294 const PetscInt *r,*ic; 295 PetscInt i,n=A->rmap->n,*ai=a->i,*aj=a->j; 296 PetscInt *bi,*bj,*ajtmp; 297 PetscInt *bdiag,row,nnz,nzi,reallocs=0,nzbd,*im; 298 PetscReal f; 299 PetscInt nlnk,*lnk,k,**bi_ptr; 300 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 301 PetscBT lnkbt; 302 303 PetscFunctionBegin; 304 if (A->rmap->N != A->cmap->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square"); 305 ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 306 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 307 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 308 309 /* get new row pointers */ 310 ierr = PetscMalloc((2*n+2)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 311 bi[0] = 0; 312 313 /* bdiag is location of diagonal in factor */ 314 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr); 315 bdiag[0] = 0; 316 317 /* linked list for storing column indices of the active row */ 318 nlnk = n + 1; 319 ierr = PetscLLCreate(n,n,nlnk,lnk,lnkbt);CHKERRQ(ierr); 320 321 ierr = PetscMalloc2(n+1,PetscInt**,&bi_ptr,n+1,PetscInt,&im);CHKERRQ(ierr); 322 323 /* initial FreeSpace size is f*(ai[n]+1) */ 324 f = info->fill; 325 ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space);CHKERRQ(ierr); 326 current_space = free_space; 327 328 for (i=0; i<n; i++) { 329 /* copy previous fill into linked list */ 330 nzi = 0; 331 nnz = ai[r[i]+1] - ai[r[i]]; 332 if (!nnz) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i); 333 ajtmp = aj + ai[r[i]]; 334 ierr = PetscLLAddPerm(nnz,ajtmp,ic,n,nlnk,lnk,lnkbt);CHKERRQ(ierr); 335 nzi += nlnk; 336 337 /* add pivot rows into linked list */ 338 row = lnk[n]; 339 while (row < i) { 340 nzbd = bdiag[row] - bi[row] + 1; /* num of entries in the row with column index <= row */ 341 ajtmp = bi_ptr[row] + nzbd; /* points to the entry next to the diagonal */ 342 ierr = PetscLLAddSortedLU(ajtmp,row,nlnk,lnk,lnkbt,i,nzbd,im);CHKERRQ(ierr); 343 nzi += nlnk; 344 row = lnk[row]; 345 } 346 bi[i+1] = bi[i] + nzi; 347 im[i] = nzi; 348 349 /* mark bdiag */ 350 nzbd = 0; 351 nnz = nzi; 352 k = lnk[n]; 353 while (nnz-- && k < i){ 354 nzbd++; 355 k = lnk[k]; 356 } 357 bdiag[i] = bi[i] + nzbd; 358 359 /* if free space is not available, make more free space */ 360 if (current_space->local_remaining<nzi) { 361 nnz = 2*(n - i)*nzi; /* estimated and max additional space needed */ 362 ierr = PetscFreeSpaceGet(nnz,¤t_space);CHKERRQ(ierr); 363 reallocs++; 364 } 365 366 /* copy data into free space, then initialize lnk */ 367 ierr = PetscLLClean(n,n,nzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 368 bi_ptr[i] = current_space->array; 369 current_space->array += nzi; 370 current_space->local_used += nzi; 371 current_space->local_remaining -= nzi; 372 } 373 #if defined(PETSC_USE_INFO) 374 if (ai[n] != 0) { 375 PetscReal af = ((PetscReal)bi[n])/((PetscReal)ai[n]); 376 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,f,af);CHKERRQ(ierr); 377 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 378 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G);\n",af);CHKERRQ(ierr); 379 ierr = PetscInfo(A,"for best performance.\n");CHKERRQ(ierr); 380 } else { 381 ierr = PetscInfo(A,"Empty matrix\n");CHKERRQ(ierr); 382 } 383 #endif 384 385 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 386 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 387 388 /* destroy list of free space and other temporary array(s) */ 389 ierr = PetscMalloc((bi[n]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 390 ierr = PetscFreeSpaceContiguous_newdatastruct(&free_space,bj,n,bi,bdiag);CHKERRQ(ierr); 391 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 392 ierr = PetscFree2(bi_ptr,im);CHKERRQ(ierr); 393 394 /* put together the new matrix */ 395 ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr); 396 ierr = PetscLogObjectParent(B,isicol);CHKERRQ(ierr); 397 b = (Mat_SeqAIJ*)(B)->data; 398 b->free_a = PETSC_TRUE; 399 b->free_ij = PETSC_TRUE; 400 b->singlemalloc = PETSC_FALSE; 401 ierr = PetscMalloc((bi[2*n+1])*sizeof(PetscScalar),&b->a);CHKERRQ(ierr); 402 b->j = bj; 403 b->i = bi; 404 b->diag = bdiag; 405 b->ilen = 0; 406 b->imax = 0; 407 b->row = isrow; 408 b->col = iscol; 409 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 410 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 411 b->icol = isicol; 412 ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 413 414 /* In b structure: Free imax, ilen, old a, old j. Allocate solve_work, new a, new j */ 415 ierr = PetscLogObjectMemory(B,bi[2*n+1]*(sizeof(PetscInt)+sizeof(PetscScalar)));CHKERRQ(ierr); 416 b->maxnz = b->nz = bi[2*n+1] ; 417 418 (B)->factor = MAT_FACTOR_LU; 419 (B)->info.factor_mallocs = reallocs; 420 (B)->info.fill_ratio_given = f; 421 422 if (ai[n]) { 423 (B)->info.fill_ratio_needed = ((PetscReal)bi[n])/((PetscReal)ai[n]); 424 } else { 425 (B)->info.fill_ratio_needed = 0.0; 426 } 427 (B)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ_newdatastruct; 428 (B)->ops->solve = MatSolve_SeqAIJ; 429 (B)->ops->solvetranspose = MatSolveTranspose_SeqAIJ; 430 /* switch to inodes if appropriate */ 431 /* ierr = MatLUFactorSymbolic_Inode(B,A,isrow,iscol,info);CHKERRQ(ierr); */ 432 PetscFunctionReturn(0); 433 } 434 435 436 /* 437 Trouble in factorization, should we dump the original matrix? 438 */ 439 #undef __FUNCT__ 440 #define __FUNCT__ "MatFactorDumpMatrix" 441 PetscErrorCode MatFactorDumpMatrix(Mat A) 442 { 443 PetscErrorCode ierr; 444 PetscTruth flg = PETSC_FALSE; 445 446 PetscFunctionBegin; 447 ierr = PetscOptionsGetTruth(PETSC_NULL,"-mat_factor_dump_on_error",&flg,PETSC_NULL);CHKERRQ(ierr); 448 if (flg) { 449 PetscViewer viewer; 450 char filename[PETSC_MAX_PATH_LEN]; 451 452 ierr = PetscSNPrintf(filename,PETSC_MAX_PATH_LEN,"matrix_factor_error.%d",PetscGlobalRank);CHKERRQ(ierr); 453 ierr = PetscViewerBinaryOpen(((PetscObject)A)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 454 ierr = MatView(A,viewer);CHKERRQ(ierr); 455 ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); 456 } 457 PetscFunctionReturn(0); 458 } 459 460 extern PetscErrorCode MatSolve_Inode(Mat,Vec,Vec); 461 462 /* ----------------------------------------------------------- */ 463 extern PetscErrorCode MatSolve_SeqAIJ_NaturalOrdering_iludt(Mat,Vec,Vec); 464 extern PetscErrorCode MatSolve_SeqAIJ_iludt(Mat,Vec,Vec); 465 466 #undef __FUNCT__ 467 #define __FUNCT__ "MatLUFactorNumeric_SeqAIJ_newdatastruct" 468 PetscErrorCode MatLUFactorNumeric_SeqAIJ_newdatastruct(Mat B,Mat A,const MatFactorInfo *info) 469 { 470 Mat C=B; 471 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ *)C->data; 472 IS isrow = b->row,isicol = b->icol; 473 PetscErrorCode ierr; 474 const PetscInt *r,*ic,*ics; 475 PetscInt i,j,k,n=A->rmap->n,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j; 476 PetscInt *ajtmp,*bjtmp,nz,nzL,row,*bdiag=b->diag,*pj; 477 MatScalar *rtmp,*pc,multiplier,*v,*pv,*aa=a->a; 478 PetscReal shift=info->shiftinblocks; 479 PetscTruth row_identity, col_identity; 480 481 PetscFunctionBegin; 482 /* printf("MatLUFactorNumeric_SeqAIJ_newdatastruct is called ...\n"); */ 483 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 484 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 485 ierr = PetscMalloc((n+1)*sizeof(MatScalar),&rtmp);CHKERRQ(ierr); 486 ics = ic; 487 488 for (i=0; i<n; i++){ 489 /* zero rtmp */ 490 /* L part */ 491 nz = bi[i+1] - bi[i]; 492 bjtmp = bj + bi[i]; 493 for (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0; 494 495 /* U part */ 496 nz = bi[2*n-i+1] - bi[2*n-i]; 497 bjtmp = bj + bi[2*n-i]; 498 for (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0; 499 500 /* load in initial (unfactored row) */ 501 nz = ai[r[i]+1] - ai[r[i]]; 502 ajtmp = aj + ai[r[i]]; 503 v = aa + ai[r[i]]; 504 for (j=0; j<nz; j++) { 505 rtmp[ics[ajtmp[j]]] = v[j]; 506 } 507 if (rtmp[ics[r[i]]] == 0.0){ 508 rtmp[ics[r[i]]] += shift; /* shift the diagonal of the matrix */ 509 /* printf("row %d, shift %g\n",i,shift); */ 510 } 511 512 /* elimination */ 513 bjtmp = bj + bi[i]; 514 row = *bjtmp++; 515 nzL = bi[i+1] - bi[i]; 516 k = 0; 517 while (k < nzL) { 518 pc = rtmp + row; 519 if (*pc != 0.0) { 520 pv = b->a + bdiag[row]; 521 multiplier = *pc * (*pv); 522 *pc = multiplier; 523 pj = b->j + bi[2*n-row]; /* begining of U(row,:) */ 524 pv = b->a + bi[2*n-row]; 525 nz = bi[2*n-row+1] - bi[2*n-row] - 1; /* num of entries in U(row,:), excluding diag */ 526 for (j=0; j<nz; j++) rtmp[pj[j]] -= multiplier * pv[j]; 527 ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr); 528 } 529 row = *bjtmp++; k++; 530 } 531 532 /* finished row so stick it into b->a */ 533 /* L part */ 534 pv = b->a + bi[i] ; 535 pj = b->j + bi[i] ; 536 nz = bi[i+1] - bi[i]; 537 for (j=0; j<nz; j++) { 538 pv[j] = rtmp[pj[j]]; 539 } 540 541 /* Mark diagonal and invert diagonal for simplier triangular solves */ 542 pv = b->a + bdiag[i]; 543 pj = b->j + bdiag[i]; 544 /* if (*pj != i)SETERRQ2(PETSC_ERR_SUP,"row %d != *pj %d",i,*pj) */ 545 *pv = 1.0/rtmp[*pj]; 546 547 /* U part */ 548 pv = b->a + bi[2*n-i]; 549 pj = b->j + bi[2*n-i]; 550 nz = bi[2*n-i+1] - bi[2*n-i] - 1; 551 for (j=0; j<nz; j++) pv[j] = rtmp[pj[j]]; 552 } 553 ierr = PetscFree(rtmp);CHKERRQ(ierr); 554 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 555 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 556 557 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 558 ierr = ISIdentity(isicol,&col_identity);CHKERRQ(ierr); 559 if (row_identity && col_identity) { 560 C->ops->solve = MatSolve_SeqAIJ_NaturalOrdering_iludt; 561 } else { 562 C->ops->solve = MatSolve_SeqAIJ_iludt; 563 } 564 565 C->ops->solveadd = 0; 566 C->ops->solvetranspose = 0; 567 C->ops->solvetransposeadd = 0; 568 C->ops->matsolve = 0; 569 C->assembled = PETSC_TRUE; 570 C->preallocated = PETSC_TRUE; 571 ierr = PetscLogFlops(C->cmap->n);CHKERRQ(ierr); 572 PetscFunctionReturn(0); 573 } 574 575 #undef __FUNCT__ 576 #define __FUNCT__ "MatLUFactorNumeric_SeqAIJ" 577 PetscErrorCode MatLUFactorNumeric_SeqAIJ(Mat B,Mat A,const MatFactorInfo *info) 578 { 579 Mat C=B; 580 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ *)C->data; 581 IS isrow = b->row,isicol = b->icol; 582 PetscErrorCode ierr; 583 const PetscInt *r,*ic,*ics; 584 PetscInt nz,row,i,j,n=A->rmap->n,diag; 585 const PetscInt *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j; 586 const PetscInt *ajtmp,*bjtmp,*diag_offset = b->diag,*pj; 587 MatScalar *pv,*rtmp,*pc,multiplier,d; 588 const MatScalar *v,*aa=a->a; 589 PetscReal rs=0.0; 590 LUShift_Ctx sctx; 591 PetscInt newshift,*ddiag; 592 593 PetscFunctionBegin; 594 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 595 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 596 ierr = PetscMalloc((n+1)*sizeof(MatScalar),&rtmp);CHKERRQ(ierr); 597 ics = ic; 598 599 sctx.shift_top = 0; 600 sctx.nshift_max = 0; 601 sctx.shift_lo = 0; 602 sctx.shift_hi = 0; 603 sctx.shift_fraction = 0; 604 605 /* if both shift schemes are chosen by user, only use info->shiftpd */ 606 if (info->shiftpd) { /* set sctx.shift_top=max{rs} */ 607 ddiag = a->diag; 608 sctx.shift_top = info->zeropivot; 609 for (i=0; i<n; i++) { 610 /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */ 611 d = (aa)[ddiag[i]]; 612 rs = -PetscAbsScalar(d) - PetscRealPart(d); 613 v = aa+ai[i]; 614 nz = ai[i+1] - ai[i]; 615 for (j=0; j<nz; j++) 616 rs += PetscAbsScalar(v[j]); 617 if (rs>sctx.shift_top) sctx.shift_top = rs; 618 } 619 sctx.shift_top *= 1.1; 620 sctx.nshift_max = 5; 621 sctx.shift_lo = 0.; 622 sctx.shift_hi = 1.; 623 } 624 625 sctx.shift_amount = 0.0; 626 sctx.nshift = 0; 627 do { 628 sctx.lushift = PETSC_FALSE; 629 for (i=0; i<n; i++){ 630 nz = bi[i+1] - bi[i]; 631 bjtmp = bj + bi[i]; 632 for (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0; 633 634 /* load in initial (unfactored row) */ 635 nz = ai[r[i]+1] - ai[r[i]]; 636 ajtmp = aj + ai[r[i]]; 637 v = aa + ai[r[i]]; 638 for (j=0; j<nz; j++) { 639 rtmp[ics[ajtmp[j]]] = v[j]; 640 } 641 rtmp[ics[r[i]]] += sctx.shift_amount; /* shift the diagonal of the matrix */ 642 /* if (sctx.shift_amount > 0.0) printf("row %d, shift %g\n",i,sctx.shift_amount); */ 643 644 row = *bjtmp++; 645 while (row < i) { 646 pc = rtmp + row; 647 if (*pc != 0.0) { 648 pv = b->a + diag_offset[row]; 649 pj = b->j + diag_offset[row] + 1; 650 multiplier = *pc / *pv++; 651 *pc = multiplier; 652 nz = bi[row+1] - diag_offset[row] - 1; 653 for (j=0; j<nz; j++) rtmp[pj[j]] -= multiplier * pv[j]; 654 ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr); 655 } 656 row = *bjtmp++; 657 } 658 /* finished row so stick it into b->a */ 659 pv = b->a + bi[i] ; 660 pj = b->j + bi[i] ; 661 nz = bi[i+1] - bi[i]; 662 diag = diag_offset[i] - bi[i]; 663 rs = 0.0; 664 for (j=0; j<nz; j++) { 665 pv[j] = rtmp[pj[j]]; 666 rs += PetscAbsScalar(pv[j]); 667 } 668 rs -= PetscAbsScalar(pv[diag]); 669 670 /* 9/13/02 Victor Eijkhout suggested scaling zeropivot by rs for matrices with funny scalings */ 671 sctx.rs = rs; 672 sctx.pv = pv[diag]; 673 ierr = MatLUCheckShift_inline(info,sctx,i,newshift);CHKERRQ(ierr); 674 if (newshift == 1) break; 675 } 676 677 if (info->shiftpd && !sctx.lushift && sctx.shift_fraction>0 && sctx.nshift<sctx.nshift_max) { 678 /* 679 * if no shift in this attempt & shifting & started shifting & can refine, 680 * then try lower shift 681 */ 682 sctx.shift_hi = sctx.shift_fraction; 683 sctx.shift_fraction = (sctx.shift_hi+sctx.shift_lo)/2.; 684 sctx.shift_amount = sctx.shift_fraction * sctx.shift_top; 685 sctx.lushift = PETSC_TRUE; 686 sctx.nshift++; 687 } 688 } while (sctx.lushift); 689 690 /* invert diagonal entries for simplier triangular solves */ 691 for (i=0; i<n; i++) { 692 b->a[diag_offset[i]] = 1.0/b->a[diag_offset[i]]; 693 } 694 ierr = PetscFree(rtmp);CHKERRQ(ierr); 695 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 696 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 697 if (b->inode.use) { 698 C->ops->solve = MatSolve_Inode; 699 } else { 700 PetscTruth row_identity, col_identity; 701 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 702 ierr = ISIdentity(isicol,&col_identity);CHKERRQ(ierr); 703 if (row_identity && col_identity) { 704 C->ops->solve = MatSolve_SeqAIJ_NaturalOrdering; 705 } else { 706 C->ops->solve = MatSolve_SeqAIJ; 707 } 708 } 709 C->ops->solveadd = MatSolveAdd_SeqAIJ; 710 C->ops->solvetranspose = MatSolveTranspose_SeqAIJ; 711 C->ops->solvetransposeadd = MatSolveTransposeAdd_SeqAIJ; 712 C->ops->matsolve = MatMatSolve_SeqAIJ; 713 C->assembled = PETSC_TRUE; 714 C->preallocated = PETSC_TRUE; 715 ierr = PetscLogFlops(C->cmap->n);CHKERRQ(ierr); 716 if (sctx.nshift){ 717 if (info->shiftpd) { 718 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); 719 } else if (info->shiftnz) { 720 ierr = PetscInfo2(A,"number of shift_nz tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr); 721 } 722 } 723 PetscFunctionReturn(0); 724 } 725 726 /* 727 This routine implements inplace ILU(0) with row or/and column permutations. 728 Input: 729 A - original matrix 730 Output; 731 A - a->i (rowptr) is same as original rowptr, but factored i-the row is stored in rowperm[i] 732 a->j (col index) is permuted by the inverse of colperm, then sorted 733 a->a reordered accordingly with a->j 734 a->diag (ptr to diagonal elements) is updated. 735 */ 736 #undef __FUNCT__ 737 #define __FUNCT__ "MatLUFactorNumeric_SeqAIJ_InplaceWithPerm" 738 PetscErrorCode MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(Mat B,Mat A,const MatFactorInfo *info) 739 { 740 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data; 741 IS isrow = a->row,isicol = a->icol; 742 PetscErrorCode ierr; 743 const PetscInt *r,*ic,*ics; 744 PetscInt i,j,n=A->rmap->n,*ai=a->i,*aj=a->j; 745 PetscInt *ajtmp,nz,row; 746 PetscInt *diag = a->diag,nbdiag,*pj; 747 PetscScalar *rtmp,*pc,multiplier,d; 748 MatScalar *v,*pv; 749 PetscReal rs; 750 LUShift_Ctx sctx; 751 PetscInt newshift; 752 753 PetscFunctionBegin; 754 if (A != B) SETERRQ(PETSC_ERR_ARG_INCOMP,"input and output matrix must have same address"); 755 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 756 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 757 ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&rtmp);CHKERRQ(ierr); 758 ierr = PetscMemzero(rtmp,(n+1)*sizeof(PetscScalar));CHKERRQ(ierr); 759 ics = ic; 760 761 sctx.shift_top = 0; 762 sctx.nshift_max = 0; 763 sctx.shift_lo = 0; 764 sctx.shift_hi = 0; 765 sctx.shift_fraction = 0; 766 767 /* if both shift schemes are chosen by user, only use info->shiftpd */ 768 if (info->shiftpd) { /* set sctx.shift_top=max{rs} */ 769 sctx.shift_top = 0; 770 for (i=0; i<n; i++) { 771 /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */ 772 d = (a->a)[diag[i]]; 773 rs = -PetscAbsScalar(d) - PetscRealPart(d); 774 v = a->a+ai[i]; 775 nz = ai[i+1] - ai[i]; 776 for (j=0; j<nz; j++) 777 rs += PetscAbsScalar(v[j]); 778 if (rs>sctx.shift_top) sctx.shift_top = rs; 779 } 780 if (sctx.shift_top < info->zeropivot) sctx.shift_top = info->zeropivot; 781 sctx.shift_top *= 1.1; 782 sctx.nshift_max = 5; 783 sctx.shift_lo = 0.; 784 sctx.shift_hi = 1.; 785 } 786 787 sctx.shift_amount = 0; 788 sctx.nshift = 0; 789 do { 790 sctx.lushift = PETSC_FALSE; 791 for (i=0; i<n; i++){ 792 /* load in initial unfactored row */ 793 nz = ai[r[i]+1] - ai[r[i]]; 794 ajtmp = aj + ai[r[i]]; 795 v = a->a + ai[r[i]]; 796 /* sort permuted ajtmp and values v accordingly */ 797 for (j=0; j<nz; j++) ajtmp[j] = ics[ajtmp[j]]; 798 ierr = PetscSortIntWithScalarArray(nz,ajtmp,v);CHKERRQ(ierr); 799 800 diag[r[i]] = ai[r[i]]; 801 for (j=0; j<nz; j++) { 802 rtmp[ajtmp[j]] = v[j]; 803 if (ajtmp[j] < i) diag[r[i]]++; /* update a->diag */ 804 } 805 rtmp[r[i]] += sctx.shift_amount; /* shift the diagonal of the matrix */ 806 807 row = *ajtmp++; 808 while (row < i) { 809 pc = rtmp + row; 810 if (*pc != 0.0) { 811 pv = a->a + diag[r[row]]; 812 pj = aj + diag[r[row]] + 1; 813 814 multiplier = *pc / *pv++; 815 *pc = multiplier; 816 nz = ai[r[row]+1] - diag[r[row]] - 1; 817 for (j=0; j<nz; j++) rtmp[pj[j]] -= multiplier * pv[j]; 818 ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr); 819 } 820 row = *ajtmp++; 821 } 822 /* finished row so overwrite it onto a->a */ 823 pv = a->a + ai[r[i]] ; 824 pj = aj + ai[r[i]] ; 825 nz = ai[r[i]+1] - ai[r[i]]; 826 nbdiag = diag[r[i]] - ai[r[i]]; /* num of entries before the diagonal */ 827 828 rs = 0.0; 829 for (j=0; j<nz; j++) { 830 pv[j] = rtmp[pj[j]]; 831 if (j != nbdiag) rs += PetscAbsScalar(pv[j]); 832 } 833 834 /* 9/13/02 Victor Eijkhout suggested scaling zeropivot by rs for matrices with funny scalings */ 835 sctx.rs = rs; 836 sctx.pv = pv[nbdiag]; 837 ierr = MatLUCheckShift_inline(info,sctx,i,newshift);CHKERRQ(ierr); 838 if (newshift == 1) break; 839 } 840 841 if (info->shiftpd && !sctx.lushift && sctx.shift_fraction>0 && sctx.nshift<sctx.nshift_max) { 842 /* 843 * if no shift in this attempt & shifting & started shifting & can refine, 844 * then try lower shift 845 */ 846 sctx.shift_hi = sctx.shift_fraction; 847 sctx.shift_fraction = (sctx.shift_hi+sctx.shift_lo)/2.; 848 sctx.shift_amount = sctx.shift_fraction * sctx.shift_top; 849 sctx.lushift = PETSC_TRUE; 850 sctx.nshift++; 851 } 852 } while (sctx.lushift); 853 854 /* invert diagonal entries for simplier triangular solves */ 855 for (i=0; i<n; i++) { 856 a->a[diag[r[i]]] = 1.0/a->a[diag[r[i]]]; 857 } 858 859 ierr = PetscFree(rtmp);CHKERRQ(ierr); 860 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 861 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 862 A->ops->solve = MatSolve_SeqAIJ_InplaceWithPerm; 863 A->ops->solveadd = MatSolveAdd_SeqAIJ; 864 A->ops->solvetranspose = MatSolveTranspose_SeqAIJ; 865 A->ops->solvetransposeadd = MatSolveTransposeAdd_SeqAIJ; 866 A->assembled = PETSC_TRUE; 867 A->preallocated = PETSC_TRUE; 868 ierr = PetscLogFlops(A->cmap->n);CHKERRQ(ierr); 869 if (sctx.nshift){ 870 if (info->shiftpd) { 871 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); 872 } else if (info->shiftnz) { 873 ierr = PetscInfo2(A,"number of shift_nz tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr); 874 } 875 } 876 PetscFunctionReturn(0); 877 } 878 879 /* ----------------------------------------------------------- */ 880 #undef __FUNCT__ 881 #define __FUNCT__ "MatLUFactor_SeqAIJ" 882 PetscErrorCode MatLUFactor_SeqAIJ(Mat A,IS row,IS col,const MatFactorInfo *info) 883 { 884 PetscErrorCode ierr; 885 Mat C; 886 887 PetscFunctionBegin; 888 ierr = MatGetFactor(A,MAT_SOLVER_PETSC,MAT_FACTOR_LU,&C);CHKERRQ(ierr); 889 ierr = MatLUFactorSymbolic(C,A,row,col,info);CHKERRQ(ierr); 890 ierr = MatLUFactorNumeric(C,A,info);CHKERRQ(ierr); 891 A->ops->solve = C->ops->solve; 892 A->ops->solvetranspose = C->ops->solvetranspose; 893 ierr = MatHeaderCopy(A,C);CHKERRQ(ierr); 894 ierr = PetscLogObjectParent(A,((Mat_SeqAIJ*)(A->data))->icol);CHKERRQ(ierr); 895 PetscFunctionReturn(0); 896 } 897 /* ----------------------------------------------------------- */ 898 899 900 #undef __FUNCT__ 901 #define __FUNCT__ "MatSolve_SeqAIJ" 902 PetscErrorCode MatSolve_SeqAIJ(Mat A,Vec bb,Vec xx) 903 { 904 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 905 IS iscol = a->col,isrow = a->row; 906 PetscErrorCode ierr; 907 PetscInt i, n = A->rmap->n,*vi,*ai = a->i,*aj = a->j; 908 PetscInt nz; 909 const PetscInt *rout,*cout,*r,*c; 910 PetscScalar *x,*tmp,*tmps,sum; 911 const PetscScalar *b; 912 const MatScalar *aa = a->a,*v; 913 914 PetscFunctionBegin; 915 if (!n) PetscFunctionReturn(0); 916 917 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 918 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 919 tmp = a->solve_work; 920 921 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 922 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1); 923 924 /* forward solve the lower triangular */ 925 tmp[0] = b[*r++]; 926 tmps = tmp; 927 for (i=1; i<n; i++) { 928 v = aa + ai[i] ; 929 vi = aj + ai[i] ; 930 nz = a->diag[i] - ai[i]; 931 sum = b[*r++]; 932 PetscSparseDenseMinusDot(sum,tmps,v,vi,nz); 933 tmp[i] = sum; 934 } 935 936 /* backward solve the upper triangular */ 937 for (i=n-1; i>=0; i--){ 938 v = aa + a->diag[i] + 1; 939 vi = aj + a->diag[i] + 1; 940 nz = ai[i+1] - a->diag[i] - 1; 941 sum = tmp[i]; 942 PetscSparseDenseMinusDot(sum,tmps,v,vi,nz); 943 x[*c--] = tmp[i] = sum*aa[a->diag[i]]; 944 } 945 946 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 947 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 948 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 949 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 950 ierr = PetscLogFlops(2.0*a->nz - A->cmap->n);CHKERRQ(ierr); 951 PetscFunctionReturn(0); 952 } 953 954 #undef __FUNCT__ 955 #define __FUNCT__ "MatMatSolve_SeqAIJ" 956 PetscErrorCode MatMatSolve_SeqAIJ(Mat A,Mat B,Mat X) 957 { 958 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 959 IS iscol = a->col,isrow = a->row; 960 PetscErrorCode ierr; 961 PetscInt i, n = A->rmap->n,*vi,*ai = a->i,*aj = a->j; 962 PetscInt nz,neq; 963 const PetscInt *rout,*cout,*r,*c; 964 PetscScalar *x,*b,*tmp,*tmps,sum; 965 const MatScalar *aa = a->a,*v; 966 PetscTruth bisdense,xisdense; 967 968 PetscFunctionBegin; 969 if (!n) PetscFunctionReturn(0); 970 971 ierr = PetscTypeCompare((PetscObject)B,MATSEQDENSE,&bisdense);CHKERRQ(ierr); 972 if (!bisdense) SETERRQ(PETSC_ERR_ARG_INCOMP,"B matrix must be a SeqDense matrix"); 973 ierr = PetscTypeCompare((PetscObject)X,MATSEQDENSE,&xisdense);CHKERRQ(ierr); 974 if (!xisdense) SETERRQ(PETSC_ERR_ARG_INCOMP,"X matrix must be a SeqDense matrix"); 975 976 ierr = MatGetArray(B,&b);CHKERRQ(ierr); 977 ierr = MatGetArray(X,&x);CHKERRQ(ierr); 978 979 tmp = a->solve_work; 980 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 981 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 982 983 for (neq=0; neq<B->cmap->n; neq++){ 984 /* forward solve the lower triangular */ 985 tmp[0] = b[r[0]]; 986 tmps = tmp; 987 for (i=1; i<n; i++) { 988 v = aa + ai[i] ; 989 vi = aj + ai[i] ; 990 nz = a->diag[i] - ai[i]; 991 sum = b[r[i]]; 992 PetscSparseDenseMinusDot(sum,tmps,v,vi,nz); 993 tmp[i] = sum; 994 } 995 /* backward solve the upper triangular */ 996 for (i=n-1; i>=0; i--){ 997 v = aa + a->diag[i] + 1; 998 vi = aj + a->diag[i] + 1; 999 nz = ai[i+1] - a->diag[i] - 1; 1000 sum = tmp[i]; 1001 PetscSparseDenseMinusDot(sum,tmps,v,vi,nz); 1002 x[c[i]] = tmp[i] = sum*aa[a->diag[i]]; 1003 } 1004 1005 b += n; 1006 x += n; 1007 } 1008 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 1009 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1010 ierr = MatRestoreArray(B,&b);CHKERRQ(ierr); 1011 ierr = MatRestoreArray(X,&x);CHKERRQ(ierr); 1012 ierr = PetscLogFlops(B->cmap->n*(2.0*a->nz - n));CHKERRQ(ierr); 1013 PetscFunctionReturn(0); 1014 } 1015 1016 #undef __FUNCT__ 1017 #define __FUNCT__ "MatSolve_SeqAIJ_InplaceWithPerm" 1018 PetscErrorCode MatSolve_SeqAIJ_InplaceWithPerm(Mat A,Vec bb,Vec xx) 1019 { 1020 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1021 IS iscol = a->col,isrow = a->row; 1022 PetscErrorCode ierr; 1023 const PetscInt *r,*c,*rout,*cout; 1024 PetscInt i, n = A->rmap->n,*vi,*ai = a->i,*aj = a->j; 1025 PetscInt nz,row; 1026 PetscScalar *x,*b,*tmp,*tmps,sum; 1027 const MatScalar *aa = a->a,*v; 1028 1029 PetscFunctionBegin; 1030 if (!n) PetscFunctionReturn(0); 1031 1032 ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 1033 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1034 tmp = a->solve_work; 1035 1036 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 1037 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1); 1038 1039 /* forward solve the lower triangular */ 1040 tmp[0] = b[*r++]; 1041 tmps = tmp; 1042 for (row=1; row<n; row++) { 1043 i = rout[row]; /* permuted row */ 1044 v = aa + ai[i] ; 1045 vi = aj + ai[i] ; 1046 nz = a->diag[i] - ai[i]; 1047 sum = b[*r++]; 1048 PetscSparseDenseMinusDot(sum,tmps,v,vi,nz); 1049 tmp[row] = sum; 1050 } 1051 1052 /* backward solve the upper triangular */ 1053 for (row=n-1; row>=0; row--){ 1054 i = rout[row]; /* permuted row */ 1055 v = aa + a->diag[i] + 1; 1056 vi = aj + a->diag[i] + 1; 1057 nz = ai[i+1] - a->diag[i] - 1; 1058 sum = tmp[row]; 1059 PetscSparseDenseMinusDot(sum,tmps,v,vi,nz); 1060 x[*c--] = tmp[row] = sum*aa[a->diag[i]]; 1061 } 1062 1063 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 1064 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1065 ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 1066 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1067 ierr = PetscLogFlops(2.0*a->nz - A->cmap->n);CHKERRQ(ierr); 1068 PetscFunctionReturn(0); 1069 } 1070 1071 /* ----------------------------------------------------------- */ 1072 #include "../src/mat/impls/aij/seq/ftn-kernels/fsolve.h" 1073 #undef __FUNCT__ 1074 #define __FUNCT__ "MatSolve_SeqAIJ_NaturalOrdering" 1075 PetscErrorCode MatSolve_SeqAIJ_NaturalOrdering(Mat A,Vec bb,Vec xx) 1076 { 1077 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1078 PetscErrorCode ierr; 1079 PetscInt n = A->rmap->n; 1080 const PetscInt *ai = a->i,*aj = a->j,*adiag = a->diag; 1081 PetscScalar *x; 1082 const PetscScalar *b; 1083 const MatScalar *aa = a->a; 1084 #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ) 1085 PetscInt adiag_i,i,nz,ai_i; 1086 const PetscInt *vi; 1087 const MatScalar *v; 1088 PetscScalar sum; 1089 #endif 1090 1091 PetscFunctionBegin; 1092 if (!n) PetscFunctionReturn(0); 1093 1094 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1095 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1096 1097 #if defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ) 1098 fortransolveaij_(&n,x,ai,aj,adiag,aa,b); 1099 #else 1100 /* forward solve the lower triangular */ 1101 x[0] = b[0]; 1102 for (i=1; i<n; i++) { 1103 ai_i = ai[i]; 1104 v = aa + ai_i; 1105 vi = aj + ai_i; 1106 nz = adiag[i] - ai_i; 1107 sum = b[i]; 1108 PetscSparseDenseMinusDot(sum,x,v,vi,nz); 1109 x[i] = sum; 1110 } 1111 1112 /* backward solve the upper triangular */ 1113 for (i=n-1; i>=0; i--){ 1114 adiag_i = adiag[i]; 1115 v = aa + adiag_i + 1; 1116 vi = aj + adiag_i + 1; 1117 nz = ai[i+1] - adiag_i - 1; 1118 sum = x[i]; 1119 PetscSparseDenseMinusDot(sum,x,v,vi,nz); 1120 x[i] = sum*aa[adiag_i]; 1121 } 1122 #endif 1123 ierr = PetscLogFlops(2.0*a->nz - A->cmap->n);CHKERRQ(ierr); 1124 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1125 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1126 PetscFunctionReturn(0); 1127 } 1128 1129 #undef __FUNCT__ 1130 #define __FUNCT__ "MatSolveAdd_SeqAIJ" 1131 PetscErrorCode MatSolveAdd_SeqAIJ(Mat A,Vec bb,Vec yy,Vec xx) 1132 { 1133 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1134 IS iscol = a->col,isrow = a->row; 1135 PetscErrorCode ierr; 1136 PetscInt i, n = A->rmap->n,*vi,*ai = a->i,*aj = a->j; 1137 PetscInt nz; 1138 const PetscInt *rout,*cout,*r,*c; 1139 PetscScalar *x,*b,*tmp,sum; 1140 const MatScalar *aa = a->a,*v; 1141 1142 PetscFunctionBegin; 1143 if (yy != xx) {ierr = VecCopy(yy,xx);CHKERRQ(ierr);} 1144 1145 ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 1146 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1147 tmp = a->solve_work; 1148 1149 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 1150 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1); 1151 1152 /* forward solve the lower triangular */ 1153 tmp[0] = b[*r++]; 1154 for (i=1; i<n; i++) { 1155 v = aa + ai[i] ; 1156 vi = aj + ai[i] ; 1157 nz = a->diag[i] - ai[i]; 1158 sum = b[*r++]; 1159 while (nz--) sum -= *v++ * tmp[*vi++ ]; 1160 tmp[i] = sum; 1161 } 1162 1163 /* backward solve the upper triangular */ 1164 for (i=n-1; i>=0; i--){ 1165 v = aa + a->diag[i] + 1; 1166 vi = aj + a->diag[i] + 1; 1167 nz = ai[i+1] - a->diag[i] - 1; 1168 sum = tmp[i]; 1169 while (nz--) sum -= *v++ * tmp[*vi++ ]; 1170 tmp[i] = sum*aa[a->diag[i]]; 1171 x[*c--] += tmp[i]; 1172 } 1173 1174 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 1175 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1176 ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 1177 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1178 ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1179 1180 PetscFunctionReturn(0); 1181 } 1182 /* -------------------------------------------------------------------*/ 1183 #undef __FUNCT__ 1184 #define __FUNCT__ "MatSolveTranspose_SeqAIJ" 1185 PetscErrorCode MatSolveTranspose_SeqAIJ(Mat A,Vec bb,Vec xx) 1186 { 1187 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1188 IS iscol = a->col,isrow = a->row; 1189 PetscErrorCode ierr; 1190 const PetscInt *rout,*cout,*r,*c; 1191 PetscInt i,n = A->rmap->n,*vi,*ai = a->i,*aj = a->j; 1192 PetscInt nz,*diag = a->diag; 1193 PetscScalar *x,*b,*tmp,s1; 1194 const MatScalar *aa = a->a,*v; 1195 1196 PetscFunctionBegin; 1197 ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 1198 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1199 tmp = a->solve_work; 1200 1201 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 1202 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 1203 1204 /* copy the b into temp work space according to permutation */ 1205 for (i=0; i<n; i++) tmp[i] = b[c[i]]; 1206 1207 /* forward solve the U^T */ 1208 for (i=0; i<n; i++) { 1209 v = aa + diag[i] ; 1210 vi = aj + diag[i] + 1; 1211 nz = ai[i+1] - diag[i] - 1; 1212 s1 = tmp[i]; 1213 s1 *= (*v++); /* multiply by inverse of diagonal entry */ 1214 while (nz--) { 1215 tmp[*vi++ ] -= (*v++)*s1; 1216 } 1217 tmp[i] = s1; 1218 } 1219 1220 /* backward solve the L^T */ 1221 for (i=n-1; i>=0; i--){ 1222 v = aa + diag[i] - 1 ; 1223 vi = aj + diag[i] - 1 ; 1224 nz = diag[i] - ai[i]; 1225 s1 = tmp[i]; 1226 while (nz--) { 1227 tmp[*vi-- ] -= (*v--)*s1; 1228 } 1229 } 1230 1231 /* copy tmp into x according to permutation */ 1232 for (i=0; i<n; i++) x[r[i]] = tmp[i]; 1233 1234 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 1235 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1236 ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 1237 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1238 1239 ierr = PetscLogFlops(2.0*a->nz-A->cmap->n);CHKERRQ(ierr); 1240 PetscFunctionReturn(0); 1241 } 1242 1243 #undef __FUNCT__ 1244 #define __FUNCT__ "MatSolveTransposeAdd_SeqAIJ" 1245 PetscErrorCode MatSolveTransposeAdd_SeqAIJ(Mat A,Vec bb,Vec zz,Vec xx) 1246 { 1247 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1248 IS iscol = a->col,isrow = a->row; 1249 PetscErrorCode ierr; 1250 const PetscInt *r,*c,*rout,*cout; 1251 PetscInt i,n = A->rmap->n,*vi,*ai = a->i,*aj = a->j; 1252 PetscInt nz,*diag = a->diag; 1253 PetscScalar *x,*b,*tmp; 1254 const MatScalar *aa = a->a,*v; 1255 1256 PetscFunctionBegin; 1257 if (zz != xx) {ierr = VecCopy(zz,xx);CHKERRQ(ierr);} 1258 1259 ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 1260 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1261 tmp = a->solve_work; 1262 1263 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 1264 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 1265 1266 /* copy the b into temp work space according to permutation */ 1267 for (i=0; i<n; i++) tmp[i] = b[c[i]]; 1268 1269 /* forward solve the U^T */ 1270 for (i=0; i<n; i++) { 1271 v = aa + diag[i] ; 1272 vi = aj + diag[i] + 1; 1273 nz = ai[i+1] - diag[i] - 1; 1274 tmp[i] *= *v++; 1275 while (nz--) { 1276 tmp[*vi++ ] -= (*v++)*tmp[i]; 1277 } 1278 } 1279 1280 /* backward solve the L^T */ 1281 for (i=n-1; i>=0; i--){ 1282 v = aa + diag[i] - 1 ; 1283 vi = aj + diag[i] - 1 ; 1284 nz = diag[i] - ai[i]; 1285 while (nz--) { 1286 tmp[*vi-- ] -= (*v--)*tmp[i]; 1287 } 1288 } 1289 1290 /* copy tmp into x according to permutation */ 1291 for (i=0; i<n; i++) x[r[i]] += tmp[i]; 1292 1293 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 1294 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1295 ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 1296 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1297 1298 ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1299 PetscFunctionReturn(0); 1300 } 1301 /* ----------------------------------------------------------------*/ 1302 EXTERN PetscErrorCode Mat_CheckInode(Mat,PetscTruth); 1303 EXTERN PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat,Mat,MatDuplicateOption,PetscTruth); 1304 1305 /* 1306 ilu(0) with natural ordering under new data structure. 1307 Factored arrays bj and ba are stored as 1308 L(0,:), L(1,:), ...,L(n-1,:), U(n-1,:),...,U(i,:),U(i-1,:),...,U(0,:) 1309 1310 bi=fact->i is an array of size 2n+2, in which 1311 bi+ 1312 bi[i] -> 1st entry of L(i,:),i=0,...,i-1 1313 bi[n] -> points to L(n-1,:)+1 1314 bi[n+1] -> 1st entry of U(n-1,:) 1315 bi[2n-i] -> 1st entry of U(i,:) 1316 bi[2n-i+1] -> end of U(i,:)+1, the 1st entry of U(i-1,:) 1317 bi[2n] -> 1st entry of U(0,:) 1318 bi[2n+1] -> points to U(0,:)+1 1319 1320 U(i,:) contains diag[i] as its last entry, i.e., 1321 U(i,:) = (u[i,i+1],...,u[i,n-1],diag[i]) 1322 */ 1323 #undef __FUNCT__ 1324 #define __FUNCT__ "MatILUFactorSymbolic_SeqAIJ_ilu0_newdatastruct" 1325 PetscErrorCode MatILUFactorSymbolic_SeqAIJ_ilu0_newdatastruct(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 1326 { 1327 1328 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b; 1329 PetscErrorCode ierr; 1330 PetscInt n=A->rmap->n,*ai=a->i,*aj,*adiag=a->diag; 1331 PetscInt i,j,nz,*bi,*bj,*bdiag; 1332 1333 PetscFunctionBegin; 1334 ierr = MatDuplicateNoCreate_SeqAIJ(fact,A,MAT_DO_NOT_COPY_VALUES,PETSC_FALSE);CHKERRQ(ierr); 1335 b = (Mat_SeqAIJ*)(fact)->data; 1336 1337 /* allocate matrix arrays for new data structure */ 1338 ierr = PetscMalloc3(ai[n]+1,PetscScalar,&b->a,ai[n]+1,PetscInt,&b->j,2*n+2,PetscInt,&b->i);CHKERRQ(ierr); 1339 ierr = PetscLogObjectMemory(fact,ai[n]*(sizeof(PetscScalar)+sizeof(PetscInt))+(2*n+2)*sizeof(PetscInt));CHKERRQ(ierr); 1340 b->singlemalloc = PETSC_TRUE; 1341 if (!b->diag){ 1342 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&b->diag);CHKERRQ(ierr); 1343 } 1344 bdiag = b->diag; 1345 1346 if (n > 0) { 1347 ierr = PetscMemzero(b->a,(ai[n])*sizeof(MatScalar));CHKERRQ(ierr); 1348 } 1349 1350 /* set bi and bj with new data structure */ 1351 bi = b->i; 1352 bj = b->j; 1353 1354 /* L part */ 1355 bi[0] = 0; 1356 for (i=0; i<n; i++){ 1357 nz = adiag[i] - ai[i]; 1358 bi[i+1] = bi[i] + nz; 1359 aj = a->j + ai[i]; 1360 for (j=0; j<nz; j++){ 1361 *bj = aj[j]; bj++; 1362 } 1363 } 1364 1365 /* U part */ 1366 bi[n+1] = bi[n]; 1367 for (i=n-1; i>=0; i--){ 1368 nz = ai[i+1] - adiag[i] - 1; 1369 bi[2*n-i+1] = bi[2*n-i] + nz + 1; 1370 aj = a->j + adiag[i] + 1; 1371 for (j=0; j<nz; j++){ 1372 *bj = aj[j]; bj++; 1373 } 1374 /* diag[i] */ 1375 *bj = i; bj++; 1376 bdiag[i] = bi[2*n-i+1]-1; 1377 } 1378 PetscFunctionReturn(0); 1379 } 1380 1381 #undef __FUNCT__ 1382 #define __FUNCT__ "MatILUFactorSymbolic_SeqAIJ_newdatastruct" 1383 PetscErrorCode MatILUFactorSymbolic_SeqAIJ_newdatastruct(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 1384 { 1385 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b; 1386 IS isicol; 1387 PetscErrorCode ierr; 1388 const PetscInt *r,*ic; 1389 PetscInt n=A->rmap->n,*ai=a->i,*aj=a->j,d; 1390 PetscInt *bi,*cols,nnz,*cols_lvl; 1391 PetscInt *bdiag,prow,fm,nzbd,reallocs=0,dcount=0; 1392 PetscInt i,levels,diagonal_fill; 1393 PetscTruth col_identity,row_identity; 1394 PetscReal f; 1395 PetscInt nlnk,*lnk,*lnk_lvl=PETSC_NULL; 1396 PetscBT lnkbt; 1397 PetscInt nzi,*bj,**bj_ptr,**bjlvl_ptr; 1398 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 1399 PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL; 1400 PetscTruth missing; 1401 1402 PetscFunctionBegin; 1403 //printf("MatILUFactorSymbolic_SeqAIJ_newdatastruct ...\n"); 1404 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); 1405 f = info->fill; 1406 levels = (PetscInt)info->levels; 1407 diagonal_fill = (PetscInt)info->diagonal_fill; 1408 ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 1409 1410 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 1411 ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr); 1412 1413 if (!levels && row_identity && col_identity) { 1414 /* special case: ilu(0) with natural ordering */ 1415 ierr = MatILUFactorSymbolic_SeqAIJ_ilu0_newdatastruct(fact,A,isrow,iscol,info);CHKERRQ(ierr); 1416 (fact)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ_newdatastruct; 1417 1418 fact->factor = MAT_FACTOR_ILU; 1419 (fact)->info.factor_mallocs = 0; 1420 (fact)->info.fill_ratio_given = info->fill; 1421 (fact)->info.fill_ratio_needed = 1.0; 1422 b = (Mat_SeqAIJ*)(fact)->data; 1423 ierr = MatMissingDiagonal(A,&missing,&d);CHKERRQ(ierr); 1424 if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d); 1425 b->row = isrow; 1426 b->col = iscol; 1427 b->icol = isicol; 1428 ierr = PetscMalloc(((fact)->rmap->n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 1429 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 1430 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 1431 /* ierr = MatILUFactorSymbolic_Inode(fact,A,isrow,iscol,info);CHKERRQ(ierr); */ 1432 PetscFunctionReturn(0); 1433 } 1434 1435 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 1436 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 1437 1438 /* get new row pointers */ 1439 ierr = PetscMalloc((2*n+2)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 1440 bi[0] = 0; 1441 /* bdiag is location of diagonal in factor */ 1442 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr); 1443 bdiag[0] = 0; 1444 1445 ierr = PetscMalloc((2*n+1)*sizeof(PetscInt**),&bj_ptr);CHKERRQ(ierr); 1446 bjlvl_ptr = (PetscInt**)(bj_ptr + n); 1447 1448 /* create a linked list for storing column indices of the active row */ 1449 nlnk = n + 1; 1450 ierr = PetscIncompleteLLCreate(n,n,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 1451 1452 /* initial FreeSpace size is f*(ai[n]+1) */ 1453 ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space);CHKERRQ(ierr); 1454 current_space = free_space; 1455 ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space_lvl);CHKERRQ(ierr); 1456 current_space_lvl = free_space_lvl; 1457 1458 for (i=0; i<n; i++) { 1459 nzi = 0; 1460 /* copy current row into linked list */ 1461 nnz = ai[r[i]+1] - ai[r[i]]; 1462 if (!nnz) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i); 1463 cols = aj + ai[r[i]]; 1464 lnk[i] = -1; /* marker to indicate if diagonal exists */ 1465 ierr = PetscIncompleteLLInit(nnz,cols,n,ic,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 1466 nzi += nlnk; 1467 1468 /* make sure diagonal entry is included */ 1469 if (diagonal_fill && lnk[i] == -1) { 1470 fm = n; 1471 while (lnk[fm] < i) fm = lnk[fm]; 1472 lnk[i] = lnk[fm]; /* insert diagonal into linked list */ 1473 lnk[fm] = i; 1474 lnk_lvl[i] = 0; 1475 nzi++; dcount++; 1476 } 1477 1478 /* add pivot rows into the active row */ 1479 nzbd = 0; 1480 prow = lnk[n]; 1481 while (prow < i) { 1482 nnz = bdiag[prow]; 1483 cols = bj_ptr[prow] + nnz + 1; 1484 cols_lvl = bjlvl_ptr[prow] + nnz + 1; 1485 nnz = bi[prow+1] - bi[prow] - nnz - 1; 1486 ierr = PetscILULLAddSorted(nnz,cols,levels,cols_lvl,prow,nlnk,lnk,lnk_lvl,lnkbt,prow);CHKERRQ(ierr); 1487 nzi += nlnk; 1488 prow = lnk[prow]; 1489 nzbd++; 1490 } 1491 bdiag[i] = nzbd; 1492 bi[i+1] = bi[i] + nzi; 1493 1494 /* if free space is not available, make more free space */ 1495 if (current_space->local_remaining<nzi) { 1496 nnz = 2*nzi*(n - i); /* estimated and max additional space needed */ 1497 ierr = PetscFreeSpaceGet(nnz,¤t_space);CHKERRQ(ierr); 1498 ierr = PetscFreeSpaceGet(nnz,¤t_space_lvl);CHKERRQ(ierr); 1499 reallocs++; 1500 } 1501 1502 /* copy data into free_space and free_space_lvl, then initialize lnk */ 1503 ierr = PetscIncompleteLLClean(n,n,nzi,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr); 1504 bj_ptr[i] = current_space->array; 1505 bjlvl_ptr[i] = current_space_lvl->array; 1506 1507 /* make sure the active row i has diagonal entry */ 1508 if (*(bj_ptr[i]+bdiag[i]) != i) { 1509 SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,"Row %D has missing diagonal in factored matrix\n\ 1510 try running with -pc_factor_nonzeros_along_diagonal or -pc_factor_diagonal_fill",i); 1511 } 1512 1513 current_space->array += nzi; 1514 current_space->local_used += nzi; 1515 current_space->local_remaining -= nzi; 1516 current_space_lvl->array += nzi; 1517 current_space_lvl->local_used += nzi; 1518 current_space_lvl->local_remaining -= nzi; 1519 } 1520 1521 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 1522 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 1523 1524 /* destroy list of free space and other temporary arrays */ 1525 ierr = PetscMalloc((bi[n]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 1526 1527 /* copy free_space into bj and free free_space; set bi, bj, bdiag in new datastructure; */ 1528 ierr = PetscFreeSpaceContiguous_newdatastruct(&free_space,bj,n,bi,bdiag);CHKERRQ(ierr); 1529 1530 ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 1531 ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr); 1532 ierr = PetscFree(bj_ptr);CHKERRQ(ierr); 1533 1534 #if defined(PETSC_USE_INFO) 1535 { 1536 PetscReal af = ((PetscReal)bi[n])/((PetscReal)ai[n]); 1537 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,f,af);CHKERRQ(ierr); 1538 ierr = PetscInfo1(A,"Run with -[sub_]pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 1539 ierr = PetscInfo1(A,"PCFactorSetFill([sub]pc,%G);\n",af);CHKERRQ(ierr); 1540 ierr = PetscInfo(A,"for best performance.\n");CHKERRQ(ierr); 1541 if (diagonal_fill) { 1542 ierr = PetscInfo1(A,"Detected and replaced %D missing diagonals",dcount);CHKERRQ(ierr); 1543 } 1544 } 1545 #endif 1546 1547 /* put together the new matrix */ 1548 ierr = MatSeqAIJSetPreallocation_SeqAIJ(fact,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr); 1549 ierr = PetscLogObjectParent(fact,isicol);CHKERRQ(ierr); 1550 b = (Mat_SeqAIJ*)(fact)->data; 1551 b->free_a = PETSC_TRUE; 1552 b->free_ij = PETSC_TRUE; 1553 b->singlemalloc = PETSC_FALSE; 1554 ierr = PetscMalloc( (bi[2*n+1] )*sizeof(PetscScalar),&b->a);CHKERRQ(ierr); 1555 b->j = bj; 1556 b->i = bi; 1557 b->diag = bdiag; 1558 b->ilen = 0; 1559 b->imax = 0; 1560 b->row = isrow; 1561 b->col = iscol; 1562 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 1563 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 1564 b->icol = isicol; 1565 ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 1566 /* In b structure: Free imax, ilen, old a, old j. 1567 Allocate bdiag, solve_work, new a, new j */ 1568 ierr = PetscLogObjectMemory(fact,bi[2*n+1] * (sizeof(PetscInt)+sizeof(PetscScalar)));CHKERRQ(ierr); 1569 b->maxnz = b->nz = bi[2*n+1] ; 1570 (fact)->info.factor_mallocs = reallocs; 1571 (fact)->info.fill_ratio_given = f; 1572 (fact)->info.fill_ratio_needed = ((PetscReal)bi[2*n+1])/((PetscReal)ai[n]); 1573 (fact)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ_newdatastruct; 1574 /* ierr = MatILUFactorSymbolic_Inode(fact,A,isrow,iscol,info);CHKERRQ(ierr); */ 1575 PetscFunctionReturn(0); 1576 } 1577 1578 #undef __FUNCT__ 1579 #define __FUNCT__ "MatILUFactorSymbolic_SeqAIJ" 1580 PetscErrorCode MatILUFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 1581 { 1582 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b; 1583 IS isicol; 1584 PetscErrorCode ierr; 1585 const PetscInt *r,*ic; 1586 PetscInt n=A->rmap->n,*ai=a->i,*aj=a->j,d; 1587 PetscInt *bi,*cols,nnz,*cols_lvl; 1588 PetscInt *bdiag,prow,fm,nzbd,reallocs=0,dcount=0; 1589 PetscInt i,levels,diagonal_fill; 1590 PetscTruth col_identity,row_identity; 1591 PetscReal f; 1592 PetscInt nlnk,*lnk,*lnk_lvl=PETSC_NULL; 1593 PetscBT lnkbt; 1594 PetscInt nzi,*bj,**bj_ptr,**bjlvl_ptr; 1595 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 1596 PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL; 1597 PetscTruth missing; 1598 PetscTruth newdatastruct=PETSC_FALSE; 1599 1600 PetscFunctionBegin; 1601 ierr = PetscOptionsGetTruth(PETSC_NULL,"-ilu_new",&newdatastruct,PETSC_NULL);CHKERRQ(ierr); 1602 if (newdatastruct){ 1603 ierr = MatILUFactorSymbolic_SeqAIJ_newdatastruct(fact,A,isrow,iscol,info);CHKERRQ(ierr); 1604 PetscFunctionReturn(0); 1605 } 1606 1607 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); 1608 f = info->fill; 1609 levels = (PetscInt)info->levels; 1610 diagonal_fill = (PetscInt)info->diagonal_fill; 1611 ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 1612 1613 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 1614 ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr); 1615 if (!levels && row_identity && col_identity) { /* special case: ilu(0) with natural ordering */ 1616 ierr = MatDuplicateNoCreate_SeqAIJ(fact,A,MAT_DO_NOT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr); 1617 (fact)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ; 1618 1619 fact->factor = MAT_FACTOR_ILU; 1620 (fact)->info.factor_mallocs = 0; 1621 (fact)->info.fill_ratio_given = info->fill; 1622 (fact)->info.fill_ratio_needed = 1.0; 1623 b = (Mat_SeqAIJ*)(fact)->data; 1624 ierr = MatMissingDiagonal(A,&missing,&d);CHKERRQ(ierr); 1625 if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d); 1626 b->row = isrow; 1627 b->col = iscol; 1628 b->icol = isicol; 1629 ierr = PetscMalloc(((fact)->rmap->n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 1630 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 1631 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 1632 ierr = MatILUFactorSymbolic_Inode(fact,A,isrow,iscol,info);CHKERRQ(ierr); 1633 PetscFunctionReturn(0); 1634 } 1635 1636 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 1637 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 1638 1639 /* get new row pointers */ 1640 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 1641 bi[0] = 0; 1642 /* bdiag is location of diagonal in factor */ 1643 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr); 1644 bdiag[0] = 0; 1645 1646 ierr = PetscMalloc((2*n+1)*sizeof(PetscInt**),&bj_ptr);CHKERRQ(ierr); 1647 bjlvl_ptr = (PetscInt**)(bj_ptr + n); 1648 1649 /* create a linked list for storing column indices of the active row */ 1650 nlnk = n + 1; 1651 ierr = PetscIncompleteLLCreate(n,n,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 1652 1653 /* initial FreeSpace size is f*(ai[n]+1) */ 1654 ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space);CHKERRQ(ierr); 1655 current_space = free_space; 1656 ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space_lvl);CHKERRQ(ierr); 1657 current_space_lvl = free_space_lvl; 1658 1659 for (i=0; i<n; i++) { 1660 nzi = 0; 1661 /* copy current row into linked list */ 1662 nnz = ai[r[i]+1] - ai[r[i]]; 1663 if (!nnz) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i); 1664 cols = aj + ai[r[i]]; 1665 lnk[i] = -1; /* marker to indicate if diagonal exists */ 1666 ierr = PetscIncompleteLLInit(nnz,cols,n,ic,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 1667 nzi += nlnk; 1668 1669 /* make sure diagonal entry is included */ 1670 if (diagonal_fill && lnk[i] == -1) { 1671 fm = n; 1672 while (lnk[fm] < i) fm = lnk[fm]; 1673 lnk[i] = lnk[fm]; /* insert diagonal into linked list */ 1674 lnk[fm] = i; 1675 lnk_lvl[i] = 0; 1676 nzi++; dcount++; 1677 } 1678 1679 /* add pivot rows into the active row */ 1680 nzbd = 0; 1681 prow = lnk[n]; 1682 while (prow < i) { 1683 nnz = bdiag[prow]; 1684 cols = bj_ptr[prow] + nnz + 1; 1685 cols_lvl = bjlvl_ptr[prow] + nnz + 1; 1686 nnz = bi[prow+1] - bi[prow] - nnz - 1; 1687 ierr = PetscILULLAddSorted(nnz,cols,levels,cols_lvl,prow,nlnk,lnk,lnk_lvl,lnkbt,prow);CHKERRQ(ierr); 1688 nzi += nlnk; 1689 prow = lnk[prow]; 1690 nzbd++; 1691 } 1692 bdiag[i] = nzbd; 1693 bi[i+1] = bi[i] + nzi; 1694 1695 /* if free space is not available, make more free space */ 1696 if (current_space->local_remaining<nzi) { 1697 nnz = nzi*(n - i); /* estimated and max additional space needed */ 1698 ierr = PetscFreeSpaceGet(nnz,¤t_space);CHKERRQ(ierr); 1699 ierr = PetscFreeSpaceGet(nnz,¤t_space_lvl);CHKERRQ(ierr); 1700 reallocs++; 1701 } 1702 1703 /* copy data into free_space and free_space_lvl, then initialize lnk */ 1704 ierr = PetscIncompleteLLClean(n,n,nzi,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr); 1705 bj_ptr[i] = current_space->array; 1706 bjlvl_ptr[i] = current_space_lvl->array; 1707 1708 /* make sure the active row i has diagonal entry */ 1709 if (*(bj_ptr[i]+bdiag[i]) != i) { 1710 SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,"Row %D has missing diagonal in factored matrix\n\ 1711 try running with -pc_factor_nonzeros_along_diagonal or -pc_factor_diagonal_fill",i); 1712 } 1713 1714 current_space->array += nzi; 1715 current_space->local_used += nzi; 1716 current_space->local_remaining -= nzi; 1717 current_space_lvl->array += nzi; 1718 current_space_lvl->local_used += nzi; 1719 current_space_lvl->local_remaining -= nzi; 1720 } 1721 1722 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 1723 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 1724 1725 /* destroy list of free space and other temporary arrays */ 1726 ierr = PetscMalloc((bi[n]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 1727 ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr); /* copy free_space -> bj */ 1728 ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 1729 ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr); 1730 ierr = PetscFree(bj_ptr);CHKERRQ(ierr); 1731 1732 #if defined(PETSC_USE_INFO) 1733 { 1734 PetscReal af = ((PetscReal)bi[n])/((PetscReal)ai[n]); 1735 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,f,af);CHKERRQ(ierr); 1736 ierr = PetscInfo1(A,"Run with -[sub_]pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 1737 ierr = PetscInfo1(A,"PCFactorSetFill([sub]pc,%G);\n",af);CHKERRQ(ierr); 1738 ierr = PetscInfo(A,"for best performance.\n");CHKERRQ(ierr); 1739 if (diagonal_fill) { 1740 ierr = PetscInfo1(A,"Detected and replaced %D missing diagonals",dcount);CHKERRQ(ierr); 1741 } 1742 } 1743 #endif 1744 1745 /* put together the new matrix */ 1746 ierr = MatSeqAIJSetPreallocation_SeqAIJ(fact,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr); 1747 ierr = PetscLogObjectParent(fact,isicol);CHKERRQ(ierr); 1748 b = (Mat_SeqAIJ*)(fact)->data; 1749 b->free_a = PETSC_TRUE; 1750 b->free_ij = PETSC_TRUE; 1751 b->singlemalloc = PETSC_FALSE; 1752 ierr = PetscMalloc( (bi[n] )*sizeof(PetscScalar),&b->a);CHKERRQ(ierr); 1753 b->j = bj; 1754 b->i = bi; 1755 for (i=0; i<n; i++) bdiag[i] += bi[i]; 1756 b->diag = bdiag; 1757 b->ilen = 0; 1758 b->imax = 0; 1759 b->row = isrow; 1760 b->col = iscol; 1761 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 1762 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 1763 b->icol = isicol; 1764 ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 1765 /* In b structure: Free imax, ilen, old a, old j. 1766 Allocate bdiag, solve_work, new a, new j */ 1767 ierr = PetscLogObjectMemory(fact,(bi[n]-n) * (sizeof(PetscInt)+sizeof(PetscScalar)));CHKERRQ(ierr); 1768 b->maxnz = b->nz = bi[n] ; 1769 (fact)->info.factor_mallocs = reallocs; 1770 (fact)->info.fill_ratio_given = f; 1771 (fact)->info.fill_ratio_needed = ((PetscReal)bi[n])/((PetscReal)ai[n]); 1772 (fact)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ; 1773 ierr = MatILUFactorSymbolic_Inode(fact,A,isrow,iscol,info);CHKERRQ(ierr); 1774 PetscFunctionReturn(0); 1775 } 1776 1777 #include "../src/mat/impls/sbaij/seq/sbaij.h" 1778 #undef __FUNCT__ 1779 #define __FUNCT__ "MatCholeskyFactorNumeric_SeqAIJ" 1780 PetscErrorCode MatCholeskyFactorNumeric_SeqAIJ(Mat B,Mat A,const MatFactorInfo *info) 1781 { 1782 Mat C = B; 1783 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data; 1784 Mat_SeqSBAIJ *b=(Mat_SeqSBAIJ*)C->data; 1785 IS ip=b->row,iip = b->icol; 1786 PetscErrorCode ierr; 1787 const PetscInt *rip,*riip; 1788 PetscInt i,j,mbs=A->rmap->n,*bi=b->i,*bj=b->j,*bcol; 1789 PetscInt *ai=a->i,*aj=a->j; 1790 PetscInt k,jmin,jmax,*jl,*il,col,nexti,ili,nz; 1791 MatScalar *rtmp,*ba=b->a,*bval,*aa=a->a,dk,uikdi; 1792 PetscReal zeropivot,rs,shiftnz; 1793 PetscReal shiftpd; 1794 ChShift_Ctx sctx; 1795 PetscInt newshift; 1796 PetscTruth perm_identity; 1797 1798 PetscFunctionBegin; 1799 1800 shiftnz = info->shiftnz; 1801 shiftpd = info->shiftpd; 1802 zeropivot = info->zeropivot; 1803 1804 ierr = ISGetIndices(ip,&rip);CHKERRQ(ierr); 1805 ierr = ISGetIndices(iip,&riip);CHKERRQ(ierr); 1806 1807 /* initialization */ 1808 nz = (2*mbs+1)*sizeof(PetscInt)+mbs*sizeof(MatScalar); 1809 ierr = PetscMalloc(nz,&il);CHKERRQ(ierr); 1810 jl = il + mbs; 1811 rtmp = (MatScalar*)(jl + mbs); 1812 1813 sctx.shift_amount = 0; 1814 sctx.nshift = 0; 1815 do { 1816 sctx.chshift = PETSC_FALSE; 1817 for (i=0; i<mbs; i++) { 1818 rtmp[i] = 0.0; jl[i] = mbs; il[0] = 0; 1819 } 1820 1821 for (k = 0; k<mbs; k++){ 1822 bval = ba + bi[k]; 1823 /* initialize k-th row by the perm[k]-th row of A */ 1824 jmin = ai[rip[k]]; jmax = ai[rip[k]+1]; 1825 for (j = jmin; j < jmax; j++){ 1826 col = riip[aj[j]]; 1827 if (col >= k){ /* only take upper triangular entry */ 1828 rtmp[col] = aa[j]; 1829 *bval++ = 0.0; /* for in-place factorization */ 1830 } 1831 } 1832 /* shift the diagonal of the matrix */ 1833 if (sctx.nshift) rtmp[k] += sctx.shift_amount; 1834 1835 /* modify k-th row by adding in those rows i with U(i,k)!=0 */ 1836 dk = rtmp[k]; 1837 i = jl[k]; /* first row to be added to k_th row */ 1838 1839 while (i < k){ 1840 nexti = jl[i]; /* next row to be added to k_th row */ 1841 1842 /* compute multiplier, update diag(k) and U(i,k) */ 1843 ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */ 1844 uikdi = - ba[ili]*ba[bi[i]]; /* diagonal(k) */ 1845 dk += uikdi*ba[ili]; 1846 ba[ili] = uikdi; /* -U(i,k) */ 1847 1848 /* add multiple of row i to k-th row */ 1849 jmin = ili + 1; jmax = bi[i+1]; 1850 if (jmin < jmax){ 1851 for (j=jmin; j<jmax; j++) rtmp[bj[j]] += uikdi*ba[j]; 1852 /* update il and jl for row i */ 1853 il[i] = jmin; 1854 j = bj[jmin]; jl[i] = jl[j]; jl[j] = i; 1855 } 1856 i = nexti; 1857 } 1858 1859 /* shift the diagonals when zero pivot is detected */ 1860 /* compute rs=sum of abs(off-diagonal) */ 1861 rs = 0.0; 1862 jmin = bi[k]+1; 1863 nz = bi[k+1] - jmin; 1864 bcol = bj + jmin; 1865 while (nz--){ 1866 rs += PetscAbsScalar(rtmp[*bcol]); 1867 bcol++; 1868 } 1869 1870 sctx.rs = rs; 1871 sctx.pv = dk; 1872 ierr = MatCholeskyCheckShift_inline(info,sctx,k,newshift);CHKERRQ(ierr); 1873 1874 if (newshift == 1) { 1875 if (!sctx.shift_amount) { 1876 sctx.shift_amount = 1e-5; 1877 } 1878 break; 1879 } 1880 1881 /* copy data into U(k,:) */ 1882 ba[bi[k]] = 1.0/dk; /* U(k,k) */ 1883 jmin = bi[k]+1; jmax = bi[k+1]; 1884 if (jmin < jmax) { 1885 for (j=jmin; j<jmax; j++){ 1886 col = bj[j]; ba[j] = rtmp[col]; rtmp[col] = 0.0; 1887 } 1888 /* add the k-th row into il and jl */ 1889 il[k] = jmin; 1890 i = bj[jmin]; jl[k] = jl[i]; jl[i] = k; 1891 } 1892 } 1893 } while (sctx.chshift); 1894 ierr = PetscFree(il);CHKERRQ(ierr); 1895 1896 ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr); 1897 ierr = ISRestoreIndices(iip,&riip);CHKERRQ(ierr); 1898 1899 ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr); 1900 if (perm_identity){ 1901 (B)->ops->solve = MatSolve_SeqSBAIJ_1_NaturalOrdering; 1902 (B)->ops->solvetranspose = MatSolve_SeqSBAIJ_1_NaturalOrdering; 1903 (B)->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_1_NaturalOrdering; 1904 (B)->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_1_NaturalOrdering; 1905 } else { 1906 (B)->ops->solve = MatSolve_SeqSBAIJ_1; 1907 (B)->ops->solvetranspose = MatSolve_SeqSBAIJ_1; 1908 (B)->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_1; 1909 (B)->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_1; 1910 } 1911 1912 C->assembled = PETSC_TRUE; 1913 C->preallocated = PETSC_TRUE; 1914 ierr = PetscLogFlops(C->rmap->n);CHKERRQ(ierr); 1915 if (sctx.nshift){ 1916 if (shiftnz) { 1917 ierr = PetscInfo2(A,"number of shiftnz tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr); 1918 } else if (shiftpd) { 1919 ierr = PetscInfo2(A,"number of shiftpd tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr); 1920 } 1921 } 1922 PetscFunctionReturn(0); 1923 } 1924 1925 #undef __FUNCT__ 1926 #define __FUNCT__ "MatICCFactorSymbolic_SeqAIJ" 1927 PetscErrorCode MatICCFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS perm,const MatFactorInfo *info) 1928 { 1929 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1930 Mat_SeqSBAIJ *b; 1931 PetscErrorCode ierr; 1932 PetscTruth perm_identity,missing; 1933 PetscInt reallocs=0,i,*ai=a->i,*aj=a->j,am=A->rmap->n,*ui; 1934 const PetscInt *rip,*riip; 1935 PetscInt jmin,jmax,nzk,k,j,*jl,prow,*il,nextprow; 1936 PetscInt nlnk,*lnk,*lnk_lvl=PETSC_NULL,d; 1937 PetscInt ncols,ncols_upper,*cols,*ajtmp,*uj,**uj_ptr,**uj_lvl_ptr; 1938 PetscReal fill=info->fill,levels=info->levels; 1939 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 1940 PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL; 1941 PetscBT lnkbt; 1942 IS iperm; 1943 1944 PetscFunctionBegin; 1945 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); 1946 ierr = MatMissingDiagonal(A,&missing,&d);CHKERRQ(ierr); 1947 if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d); 1948 ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 1949 ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr); 1950 1951 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr); 1952 ui[0] = 0; 1953 1954 /* ICC(0) without matrix ordering: simply copies fill pattern */ 1955 if (!levels && perm_identity) { 1956 1957 for (i=0; i<am; i++) { 1958 ui[i+1] = ui[i] + ai[i+1] - a->diag[i]; 1959 } 1960 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 1961 cols = uj; 1962 for (i=0; i<am; i++) { 1963 aj = a->j + a->diag[i]; 1964 ncols = ui[i+1] - ui[i]; 1965 for (j=0; j<ncols; j++) *cols++ = *aj++; 1966 } 1967 } else { /* case: levels>0 || (levels=0 && !perm_identity) */ 1968 ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr); 1969 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 1970 1971 /* initialization */ 1972 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ajtmp);CHKERRQ(ierr); 1973 1974 /* jl: linked list for storing indices of the pivot rows 1975 il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */ 1976 ierr = PetscMalloc((2*am+1)*sizeof(PetscInt)+2*am*sizeof(PetscInt**),&jl);CHKERRQ(ierr); 1977 il = jl + am; 1978 uj_ptr = (PetscInt**)(il + am); 1979 uj_lvl_ptr = (PetscInt**)(uj_ptr + am); 1980 for (i=0; i<am; i++){ 1981 jl[i] = am; il[i] = 0; 1982 } 1983 1984 /* create and initialize a linked list for storing column indices of the active row k */ 1985 nlnk = am + 1; 1986 ierr = PetscIncompleteLLCreate(am,am,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 1987 1988 /* initial FreeSpace size is fill*(ai[am]+1) */ 1989 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr); 1990 current_space = free_space; 1991 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space_lvl);CHKERRQ(ierr); 1992 current_space_lvl = free_space_lvl; 1993 1994 for (k=0; k<am; k++){ /* for each active row k */ 1995 /* initialize lnk by the column indices of row rip[k] of A */ 1996 nzk = 0; 1997 ncols = ai[rip[k]+1] - ai[rip[k]]; 1998 if (!ncols) SETERRQ2(PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k); 1999 ncols_upper = 0; 2000 for (j=0; j<ncols; j++){ 2001 i = *(aj + ai[rip[k]] + j); /* unpermuted column index */ 2002 if (riip[i] >= k){ /* only take upper triangular entry */ 2003 ajtmp[ncols_upper] = i; 2004 ncols_upper++; 2005 } 2006 } 2007 ierr = PetscIncompleteLLInit(ncols_upper,ajtmp,am,riip,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 2008 nzk += nlnk; 2009 2010 /* update lnk by computing fill-in for each pivot row to be merged in */ 2011 prow = jl[k]; /* 1st pivot row */ 2012 2013 while (prow < k){ 2014 nextprow = jl[prow]; 2015 2016 /* merge prow into k-th row */ 2017 jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */ 2018 jmax = ui[prow+1]; 2019 ncols = jmax-jmin; 2020 i = jmin - ui[prow]; 2021 cols = uj_ptr[prow] + i; /* points to the 2nd nzero entry in U(prow,k:am-1) */ 2022 uj = uj_lvl_ptr[prow] + i; /* levels of cols */ 2023 j = *(uj - 1); 2024 ierr = PetscICCLLAddSorted(ncols,cols,levels,uj,am,nlnk,lnk,lnk_lvl,lnkbt,j);CHKERRQ(ierr); 2025 nzk += nlnk; 2026 2027 /* update il and jl for prow */ 2028 if (jmin < jmax){ 2029 il[prow] = jmin; 2030 j = *cols; jl[prow] = jl[j]; jl[j] = prow; 2031 } 2032 prow = nextprow; 2033 } 2034 2035 /* if free space is not available, make more free space */ 2036 if (current_space->local_remaining<nzk) { 2037 i = am - k + 1; /* num of unfactored rows */ 2038 i = PetscMin(i*nzk, i*(i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */ 2039 ierr = PetscFreeSpaceGet(i,¤t_space);CHKERRQ(ierr); 2040 ierr = PetscFreeSpaceGet(i,¤t_space_lvl);CHKERRQ(ierr); 2041 reallocs++; 2042 } 2043 2044 /* copy data into free_space and free_space_lvl, then initialize lnk */ 2045 if (nzk == 0) SETERRQ1(PETSC_ERR_ARG_WRONG,"Empty row %D in ICC matrix factor",k); 2046 ierr = PetscIncompleteLLClean(am,am,nzk,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr); 2047 2048 /* add the k-th row into il and jl */ 2049 if (nzk > 1){ 2050 i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */ 2051 jl[k] = jl[i]; jl[i] = k; 2052 il[k] = ui[k] + 1; 2053 } 2054 uj_ptr[k] = current_space->array; 2055 uj_lvl_ptr[k] = current_space_lvl->array; 2056 2057 current_space->array += nzk; 2058 current_space->local_used += nzk; 2059 current_space->local_remaining -= nzk; 2060 2061 current_space_lvl->array += nzk; 2062 current_space_lvl->local_used += nzk; 2063 current_space_lvl->local_remaining -= nzk; 2064 2065 ui[k+1] = ui[k] + nzk; 2066 } 2067 2068 #if defined(PETSC_USE_INFO) 2069 if (ai[am] != 0) { 2070 PetscReal af = (PetscReal)ui[am]/((PetscReal)ai[am]); 2071 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr); 2072 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 2073 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr); 2074 } else { 2075 ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr); 2076 } 2077 #endif 2078 2079 ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); 2080 ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr); 2081 ierr = PetscFree(jl);CHKERRQ(ierr); 2082 ierr = PetscFree(ajtmp);CHKERRQ(ierr); 2083 2084 /* destroy list of free space and other temporary array(s) */ 2085 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2086 ierr = PetscFreeSpaceContiguous(&free_space,uj);CHKERRQ(ierr); 2087 ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 2088 ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr); 2089 2090 } /* end of case: levels>0 || (levels=0 && !perm_identity) */ 2091 2092 /* put together the new matrix in MATSEQSBAIJ format */ 2093 2094 b = (Mat_SeqSBAIJ*)(fact)->data; 2095 b->singlemalloc = PETSC_FALSE; 2096 ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr); 2097 b->j = uj; 2098 b->i = ui; 2099 b->diag = 0; 2100 b->ilen = 0; 2101 b->imax = 0; 2102 b->row = perm; 2103 b->col = perm; 2104 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2105 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2106 b->icol = iperm; 2107 b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ 2108 ierr = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 2109 ierr = PetscLogObjectMemory((fact),(ui[am]-am)*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 2110 b->maxnz = b->nz = ui[am]; 2111 b->free_a = PETSC_TRUE; 2112 b->free_ij = PETSC_TRUE; 2113 2114 (fact)->info.factor_mallocs = reallocs; 2115 (fact)->info.fill_ratio_given = fill; 2116 if (ai[am] != 0) { 2117 (fact)->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]); 2118 } else { 2119 (fact)->info.fill_ratio_needed = 0.0; 2120 } 2121 (fact)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ; 2122 PetscFunctionReturn(0); 2123 } 2124 2125 #undef __FUNCT__ 2126 #define __FUNCT__ "MatCholeskyFactorSymbolic_SeqAIJ" 2127 PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS perm,const MatFactorInfo *info) 2128 { 2129 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2130 Mat_SeqSBAIJ *b; 2131 PetscErrorCode ierr; 2132 PetscTruth perm_identity; 2133 PetscReal fill = info->fill; 2134 const PetscInt *rip,*riip; 2135 PetscInt i,am=A->rmap->n,*ai=a->i,*aj=a->j,reallocs=0,prow; 2136 PetscInt *jl,jmin,jmax,nzk,*ui,k,j,*il,nextprow; 2137 PetscInt nlnk,*lnk,ncols,ncols_upper,*cols,*uj,**ui_ptr,*uj_ptr; 2138 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 2139 PetscBT lnkbt; 2140 IS iperm; 2141 2142 PetscFunctionBegin; 2143 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); 2144 /* check whether perm is the identity mapping */ 2145 ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 2146 ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr); 2147 ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr); 2148 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 2149 2150 /* initialization */ 2151 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr); 2152 ui[0] = 0; 2153 2154 /* jl: linked list for storing indices of the pivot rows 2155 il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */ 2156 ierr = PetscMalloc((3*am+1)*sizeof(PetscInt)+am*sizeof(PetscInt**),&jl);CHKERRQ(ierr); 2157 il = jl + am; 2158 cols = il + am; 2159 ui_ptr = (PetscInt**)(cols + am); 2160 for (i=0; i<am; i++){ 2161 jl[i] = am; il[i] = 0; 2162 } 2163 2164 /* create and initialize a linked list for storing column indices of the active row k */ 2165 nlnk = am + 1; 2166 ierr = PetscLLCreate(am,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2167 2168 /* initial FreeSpace size is fill*(ai[am]+1) */ 2169 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr); 2170 current_space = free_space; 2171 2172 for (k=0; k<am; k++){ /* for each active row k */ 2173 /* initialize lnk by the column indices of row rip[k] of A */ 2174 nzk = 0; 2175 ncols = ai[rip[k]+1] - ai[rip[k]]; 2176 if (!ncols) SETERRQ2(PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k); 2177 ncols_upper = 0; 2178 for (j=0; j<ncols; j++){ 2179 i = riip[*(aj + ai[rip[k]] + j)]; 2180 if (i >= k){ /* only take upper triangular entry */ 2181 cols[ncols_upper] = i; 2182 ncols_upper++; 2183 } 2184 } 2185 ierr = PetscLLAdd(ncols_upper,cols,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2186 nzk += nlnk; 2187 2188 /* update lnk by computing fill-in for each pivot row to be merged in */ 2189 prow = jl[k]; /* 1st pivot row */ 2190 2191 while (prow < k){ 2192 nextprow = jl[prow]; 2193 /* merge prow into k-th row */ 2194 jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */ 2195 jmax = ui[prow+1]; 2196 ncols = jmax-jmin; 2197 uj_ptr = ui_ptr[prow] + jmin - ui[prow]; /* points to the 2nd nzero entry in U(prow,k:am-1) */ 2198 ierr = PetscLLAddSorted(ncols,uj_ptr,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2199 nzk += nlnk; 2200 2201 /* update il and jl for prow */ 2202 if (jmin < jmax){ 2203 il[prow] = jmin; 2204 j = *uj_ptr; jl[prow] = jl[j]; jl[j] = prow; 2205 } 2206 prow = nextprow; 2207 } 2208 2209 /* if free space is not available, make more free space */ 2210 if (current_space->local_remaining<nzk) { 2211 i = am - k + 1; /* num of unfactored rows */ 2212 i = PetscMin(i*nzk, i*(i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */ 2213 ierr = PetscFreeSpaceGet(i,¤t_space);CHKERRQ(ierr); 2214 reallocs++; 2215 } 2216 2217 /* copy data into free space, then initialize lnk */ 2218 ierr = PetscLLClean(am,am,nzk,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 2219 2220 /* add the k-th row into il and jl */ 2221 if (nzk-1 > 0){ 2222 i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */ 2223 jl[k] = jl[i]; jl[i] = k; 2224 il[k] = ui[k] + 1; 2225 } 2226 ui_ptr[k] = current_space->array; 2227 current_space->array += nzk; 2228 current_space->local_used += nzk; 2229 current_space->local_remaining -= nzk; 2230 2231 ui[k+1] = ui[k] + nzk; 2232 } 2233 2234 #if defined(PETSC_USE_INFO) 2235 if (ai[am] != 0) { 2236 PetscReal af = (PetscReal)(ui[am])/((PetscReal)ai[am]); 2237 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr); 2238 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 2239 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr); 2240 } else { 2241 ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr); 2242 } 2243 #endif 2244 2245 ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); 2246 ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr); 2247 ierr = PetscFree(jl);CHKERRQ(ierr); 2248 2249 /* destroy list of free space and other temporary array(s) */ 2250 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2251 ierr = PetscFreeSpaceContiguous(&free_space,uj);CHKERRQ(ierr); 2252 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 2253 2254 /* put together the new matrix in MATSEQSBAIJ format */ 2255 2256 b = (Mat_SeqSBAIJ*)(fact)->data; 2257 b->singlemalloc = PETSC_FALSE; 2258 b->free_a = PETSC_TRUE; 2259 b->free_ij = PETSC_TRUE; 2260 ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr); 2261 b->j = uj; 2262 b->i = ui; 2263 b->diag = 0; 2264 b->ilen = 0; 2265 b->imax = 0; 2266 b->row = perm; 2267 b->col = perm; 2268 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2269 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2270 b->icol = iperm; 2271 b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ 2272 ierr = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 2273 ierr = PetscLogObjectMemory(fact,(ui[am]-am)*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 2274 b->maxnz = b->nz = ui[am]; 2275 2276 (fact)->info.factor_mallocs = reallocs; 2277 (fact)->info.fill_ratio_given = fill; 2278 if (ai[am] != 0) { 2279 (fact)->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]); 2280 } else { 2281 (fact)->info.fill_ratio_needed = 0.0; 2282 } 2283 (fact)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ; 2284 PetscFunctionReturn(0); 2285 } 2286 2287 #undef __FUNCT__ 2288 #define __FUNCT__ "MatSolve_SeqAIJ_NaturalOrdering_iludt" 2289 PetscErrorCode MatSolve_SeqAIJ_NaturalOrdering_iludt(Mat A,Vec bb,Vec xx) 2290 { 2291 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2292 PetscErrorCode ierr; 2293 PetscInt n = A->rmap->n; 2294 const PetscInt *ai = a->i,*aj = a->j,*vi; 2295 PetscScalar *x,sum; 2296 const PetscScalar *b; 2297 const MatScalar *aa = a->a,*v; 2298 PetscInt i,nz; 2299 2300 PetscFunctionBegin; 2301 if (!n) PetscFunctionReturn(0); 2302 2303 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 2304 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 2305 2306 /* forward solve the lower triangular */ 2307 x[0] = b[0]; 2308 v = aa; 2309 vi = aj; 2310 for (i=1; i<n; i++) { 2311 nz = ai[i+1] - ai[i]; 2312 sum = b[i]; 2313 PetscSparseDenseMinusDot(sum,x,v,vi,nz); 2314 /* while (nz--) sum -= *v++ * x[*vi++];*/ 2315 v += nz; 2316 vi += nz; 2317 x[i] = sum; 2318 } 2319 2320 /* backward solve the upper triangular */ 2321 v = aa + ai[n+1]; 2322 vi = aj + ai[n+1]; 2323 for (i=n-1; i>=0; i--){ 2324 nz = ai[2*n-i +1] - ai[2*n-i]-1; 2325 sum = x[i]; 2326 PetscSparseDenseMinusDot(sum,x,v,vi,nz); 2327 /* while (nz--) sum -= *v++ * x[*vi++]; */ 2328 v += nz; 2329 vi += nz; vi++; 2330 x[i] = *v++ *sum; /* x[i]=aa[adiag[i]]*sum; v++; */ 2331 } 2332 2333 ierr = PetscLogFlops(2.0*a->nz - A->cmap->n);CHKERRQ(ierr); 2334 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 2335 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 2336 PetscFunctionReturn(0); 2337 } 2338 2339 #undef __FUNCT__ 2340 #define __FUNCT__ "MatSolve_SeqAIJ_iludt" 2341 PetscErrorCode MatSolve_SeqAIJ_iludt(Mat A,Vec bb,Vec xx) 2342 { 2343 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2344 IS iscol = a->col,isrow = a->row; 2345 PetscErrorCode ierr; 2346 PetscInt i,n=A->rmap->n,*vi,*ai = a->i,*aj = a->j,*adiag=a->diag; 2347 PetscInt nz; 2348 const PetscInt *rout,*cout,*r,*c; 2349 PetscScalar *x,*tmp,*tmps; 2350 const PetscScalar *b; 2351 const MatScalar *aa = a->a,*v; 2352 2353 PetscFunctionBegin; 2354 if (!n) PetscFunctionReturn(0); 2355 2356 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 2357 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 2358 tmp = a->solve_work; 2359 2360 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 2361 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1); 2362 2363 /* forward solve the lower triangular */ 2364 tmp[0] = b[*r++]; 2365 tmps = tmp; 2366 v = aa; 2367 vi = aj; 2368 for (i=1; i<n; i++) { 2369 nz = ai[i+1] - ai[i]; 2370 tmp[i] = b[*r++]; 2371 PetscSparseDenseMinusDot(tmp[i],tmps,v,vi,nz); 2372 v += nz; vi += nz; 2373 } 2374 2375 /* backward solve the upper triangular */ 2376 v = aa + adiag[n] + 1; 2377 vi = aj + adiag[n] + 1; 2378 for (i=n-1; i>=0; i--){ 2379 nz = adiag[i] - adiag[i+1] - 1; 2380 PetscSparseDenseMinusDot(tmp[i],tmps,v,vi,nz); 2381 x[*c--] = tmp[i] = tmp[i]*aa[adiag[i]]; 2382 v += nz+1; vi += nz+1; 2383 } 2384 2385 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 2386 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 2387 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 2388 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 2389 ierr = PetscLogFlops(2*a->nz - A->cmap->n);CHKERRQ(ierr); 2390 PetscFunctionReturn(0); 2391 } 2392 2393 #undef __FUNCT__ 2394 #define __FUNCT__ "MatILUDTFactor_SeqAIJ" 2395 PetscErrorCode MatILUDTFactor_SeqAIJ(Mat A,IS isrow,IS iscol,const MatFactorInfo *info,Mat *fact) 2396 { 2397 Mat B = *fact; 2398 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b; 2399 IS isicol; 2400 PetscErrorCode ierr; 2401 const PetscInt *r,*ic; 2402 PetscInt i,n=A->rmap->n,*ai=a->i,*aj=a->j,*ajtmp,*adiag; 2403 PetscInt *bi,*bj,*bdiag,*bdiag_rev; 2404 PetscInt row,nzi,nzi_bl,nzi_bu,*im,nzi_al,nzi_au; 2405 PetscInt nlnk,*lnk; 2406 PetscBT lnkbt; 2407 PetscTruth row_identity,icol_identity,both_identity; 2408 MatScalar *aatmp,*pv,*batmp,*ba,*rtmp,*pc,multiplier,*vtmp,diag_tmp; 2409 const PetscInt *ics; 2410 PetscInt j,nz,*pj,*bjtmp,k,ncut,*jtmp; 2411 PetscReal dt=info->dt,dtcol=info->dtcol,shift=info->shiftinblocks; 2412 PetscInt dtcount=(PetscInt)info->dtcount,nnz_max; 2413 PetscTruth missing; 2414 2415 PetscFunctionBegin; 2416 2417 if (dt == PETSC_DEFAULT) dt = 0.005; 2418 if (dtcol == PETSC_DEFAULT) dtcol = 0.01; /* XXX unused! */ 2419 if (dtcount == PETSC_DEFAULT) dtcount = (PetscInt)(1.5*a->rmax); 2420 2421 /* ------- symbolic factorization, can be reused ---------*/ 2422 ierr = MatMissingDiagonal(A,&missing,&i);CHKERRQ(ierr); 2423 if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",i); 2424 adiag=a->diag; 2425 2426 ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 2427 2428 /* bdiag is location of diagonal in factor */ 2429 ierr = PetscMalloc((2*n+2)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr); 2430 bdiag_rev = bdiag + n+1; 2431 2432 /* allocate row pointers bi */ 2433 ierr = PetscMalloc((2*n+2)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 2434 2435 /* allocate bj and ba; max num of nonzero entries is (ai[n]+2*n*dtcount+2) */ 2436 if (dtcount > n-1) dtcount = n-1; /* diagonal is excluded */ 2437 nnz_max = ai[n]+2*n*dtcount+2; 2438 2439 ierr = PetscMalloc((nnz_max+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 2440 ierr = PetscMalloc((nnz_max+1)*sizeof(MatScalar),&ba);CHKERRQ(ierr); 2441 2442 /* put together the new matrix */ 2443 ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr); 2444 ierr = PetscLogObjectParent(B,isicol);CHKERRQ(ierr); 2445 b = (Mat_SeqAIJ*)(B)->data; 2446 b->free_a = PETSC_TRUE; 2447 b->free_ij = PETSC_TRUE; 2448 b->singlemalloc = PETSC_FALSE; 2449 b->a = ba; 2450 b->j = bj; 2451 b->i = bi; 2452 b->diag = bdiag; 2453 b->ilen = 0; 2454 b->imax = 0; 2455 b->row = isrow; 2456 b->col = iscol; 2457 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 2458 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 2459 b->icol = isicol; 2460 ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 2461 2462 ierr = PetscLogObjectMemory(B,nnz_max*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 2463 b->maxnz = nnz_max; 2464 2465 (B)->factor = MAT_FACTOR_ILUDT; 2466 (B)->info.factor_mallocs = 0; 2467 (B)->info.fill_ratio_given = ((PetscReal)nnz_max)/((PetscReal)ai[n]); 2468 CHKMEMQ; 2469 /* ------- end of symbolic factorization ---------*/ 2470 2471 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 2472 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 2473 ics = ic; 2474 2475 /* linked list for storing column indices of the active row */ 2476 nlnk = n + 1; 2477 ierr = PetscLLCreate(n,n,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2478 2479 /* im: used by PetscLLAddSortedLU(); jtmp: working array for column indices of active row */ 2480 ierr = PetscMalloc((2*n+1)*sizeof(PetscInt),&im);CHKERRQ(ierr); 2481 jtmp = im + n; 2482 /* rtmp, vtmp: working arrays for sparse and contiguous row entries of active row */ 2483 ierr = PetscMalloc((2*n+1)*sizeof(MatScalar),&rtmp);CHKERRQ(ierr); 2484 ierr = PetscMemzero(rtmp,(n+1)*sizeof(PetscScalar));CHKERRQ(ierr); 2485 vtmp = rtmp + n; 2486 2487 bi[0] = 0; 2488 bdiag[0] = nnz_max-1; /* location of diag[0] in factor B */ 2489 bdiag_rev[n] = bdiag[0]; 2490 bi[2*n+1] = bdiag[0]+1; /* endof bj and ba array */ 2491 for (i=0; i<n; i++) { 2492 /* copy initial fill into linked list */ 2493 nzi = 0; /* nonzeros for active row i */ 2494 nzi = ai[r[i]+1] - ai[r[i]]; 2495 if (!nzi) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i); 2496 nzi_al = adiag[r[i]] - ai[r[i]]; 2497 nzi_au = ai[r[i]+1] - adiag[r[i]] -1; 2498 ajtmp = aj + ai[r[i]]; 2499 ierr = PetscLLAddPerm(nzi,ajtmp,ic,n,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2500 2501 /* load in initial (unfactored row) */ 2502 aatmp = a->a + ai[r[i]]; 2503 for (j=0; j<nzi; j++) { 2504 rtmp[ics[*ajtmp++]] = *aatmp++; 2505 } 2506 2507 /* add pivot rows into linked list */ 2508 row = lnk[n]; 2509 while (row < i ) { 2510 nzi_bl = bi[row+1] - bi[row] + 1; 2511 bjtmp = bj + bdiag[row+1]+1; /* points to 1st column next to the diagonal in U */ 2512 ierr = PetscLLAddSortedLU(bjtmp,row,nlnk,lnk,lnkbt,i,nzi_bl,im);CHKERRQ(ierr); 2513 nzi += nlnk; 2514 row = lnk[row]; 2515 } 2516 2517 /* copy data from lnk into jtmp, then initialize lnk */ 2518 ierr = PetscLLClean(n,n,nzi,lnk,jtmp,lnkbt);CHKERRQ(ierr); 2519 2520 /* numerical factorization */ 2521 bjtmp = jtmp; 2522 row = *bjtmp++; /* 1st pivot row */ 2523 while ( row < i ) { 2524 pc = rtmp + row; 2525 pv = ba + bdiag[row]; /* 1./(diag of the pivot row) */ 2526 multiplier = (*pc) * (*pv); 2527 *pc = multiplier; 2528 if (PetscAbsScalar(*pc) > dt){ /* apply tolerance dropping rule */ 2529 pj = bj + bdiag[row+1] + 1; /* point to 1st entry of U(row,:) */ 2530 pv = ba + bdiag[row+1] + 1; 2531 /* if (multiplier < -1.0 or multiplier >1.0) printf("row/prow %d, %d, multiplier %g\n",i,row,multiplier); */ 2532 nz = bdiag[row] - bdiag[row+1] - 1; /* num of entries in U(row,:), excluding diagonal */ 2533 for (j=0; j<nz; j++) rtmp[*pj++] -= multiplier * (*pv++); 2534 ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr); 2535 } 2536 row = *bjtmp++; 2537 } 2538 2539 /* copy sparse rtmp into contiguous vtmp; separate L and U part */ 2540 diag_tmp = rtmp[i]; /* save diagonal value - may not needed?? */ 2541 nzi_bl = 0; j = 0; 2542 while (jtmp[j] < i){ /* Note: jtmp is sorted */ 2543 vtmp[j] = rtmp[jtmp[j]]; rtmp[jtmp[j]]=0.0; 2544 nzi_bl++; j++; 2545 } 2546 nzi_bu = nzi - nzi_bl -1; 2547 while (j < nzi){ 2548 vtmp[j] = rtmp[jtmp[j]]; rtmp[jtmp[j]]=0.0; 2549 j++; 2550 } 2551 2552 bjtmp = bj + bi[i]; 2553 batmp = ba + bi[i]; 2554 /* apply level dropping rule to L part */ 2555 ncut = nzi_al + dtcount; 2556 if (ncut < nzi_bl){ 2557 ierr = PetscSortSplit(ncut,nzi_bl,vtmp,jtmp);CHKERRQ(ierr); 2558 ierr = PetscSortIntWithScalarArray(ncut,jtmp,vtmp);CHKERRQ(ierr); 2559 } else { 2560 ncut = nzi_bl; 2561 } 2562 for (j=0; j<ncut; j++){ 2563 bjtmp[j] = jtmp[j]; 2564 batmp[j] = vtmp[j]; 2565 /* printf(" (%d,%g),",bjtmp[j],batmp[j]); */ 2566 } 2567 bi[i+1] = bi[i] + ncut; 2568 nzi = ncut + 1; 2569 2570 /* apply level dropping rule to U part */ 2571 ncut = nzi_au + dtcount; 2572 if (ncut < nzi_bu){ 2573 ierr = PetscSortSplit(ncut,nzi_bu,vtmp+nzi_bl+1,jtmp+nzi_bl+1);CHKERRQ(ierr); 2574 ierr = PetscSortIntWithScalarArray(ncut,jtmp+nzi_bl+1,vtmp+nzi_bl+1);CHKERRQ(ierr); 2575 } else { 2576 ncut = nzi_bu; 2577 } 2578 nzi += ncut; 2579 2580 /* mark bdiagonal */ 2581 bdiag[i+1] = bdiag[i] - (ncut + 1); 2582 bdiag_rev[n-i-1] = bdiag[i+1]; 2583 bi[2*n - i] = bi[2*n - i +1] - (ncut + 1); 2584 bjtmp = bj + bdiag[i]; 2585 batmp = ba + bdiag[i]; 2586 *bjtmp = i; 2587 *batmp = diag_tmp; /* rtmp[i]; */ 2588 if (*batmp == 0.0) { 2589 *batmp = dt+shift; 2590 /* printf(" row %d add shift %g\n",i,shift); */ 2591 } 2592 *batmp = 1.0/(*batmp); /* invert diagonal entries for simplier triangular solves */ 2593 /* printf(" (%d,%g),",*bjtmp,*batmp); */ 2594 2595 bjtmp = bj + bdiag[i+1]+1; 2596 batmp = ba + bdiag[i+1]+1; 2597 for (k=0; k<ncut; k++){ 2598 bjtmp[k] = jtmp[nzi_bl+1+k]; 2599 batmp[k] = vtmp[nzi_bl+1+k]; 2600 /* printf(" (%d,%g),",bjtmp[k],batmp[k]); */ 2601 } 2602 /* printf("\n"); */ 2603 2604 im[i] = nzi; /* used by PetscLLAddSortedLU() */ 2605 /* 2606 printf("row %d: bi %d, bdiag %d\n",i,bi[i],bdiag[i]); 2607 printf(" ----------------------------\n"); 2608 */ 2609 } /* for (i=0; i<n; i++) */ 2610 /* printf("end of L %d, beginning of U %d\n",bi[n],bdiag[n]); */ 2611 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]); 2612 2613 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 2614 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 2615 2616 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 2617 ierr = PetscFree(im);CHKERRQ(ierr); 2618 ierr = PetscFree(rtmp);CHKERRQ(ierr); 2619 2620 ierr = PetscLogFlops(B->cmap->n);CHKERRQ(ierr); 2621 b->maxnz = b->nz = bi[n] + bdiag[0] - bdiag[n]; 2622 2623 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 2624 ierr = ISIdentity(isicol,&icol_identity);CHKERRQ(ierr); 2625 both_identity = (PetscTruth) (row_identity && icol_identity); 2626 if (row_identity && icol_identity) { 2627 B->ops->solve = MatSolve_SeqAIJ_NaturalOrdering_iludt; 2628 } else { 2629 B->ops->solve = MatSolve_SeqAIJ_iludt; 2630 } 2631 2632 B->ops->lufactorsymbolic = MatILUDTFactorSymbolic_SeqAIJ; 2633 B->ops->lufactornumeric = MatILUDTFactorNumeric_SeqAIJ; 2634 B->ops->solveadd = 0; 2635 B->ops->solvetranspose = 0; 2636 B->ops->solvetransposeadd = 0; 2637 B->ops->matsolve = 0; 2638 B->assembled = PETSC_TRUE; 2639 B->preallocated = PETSC_TRUE; 2640 PetscFunctionReturn(0); 2641 } 2642 2643 /* a wraper of MatILUDTFactor_SeqAIJ() */ 2644 #undef __FUNCT__ 2645 #define __FUNCT__ "MatILUDTFactorSymbolic_SeqAIJ" 2646 PetscErrorCode PETSCMAT_DLLEXPORT MatILUDTFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS row,IS col,const MatFactorInfo *info) 2647 { 2648 PetscErrorCode ierr; 2649 2650 PetscFunctionBegin; 2651 ierr = MatILUDTFactor_SeqAIJ(A,row,col,info,&fact);CHKERRQ(ierr); 2652 2653 fact->ops->lufactornumeric = MatILUDTFactorNumeric_SeqAIJ; 2654 PetscFunctionReturn(0); 2655 } 2656 2657 /* 2658 same as MatLUFactorNumeric_SeqAIJ(), except using contiguous array matrix factors 2659 - intend to replace existing MatLUFactorNumeric_SeqAIJ() 2660 */ 2661 #undef __FUNCT__ 2662 #define __FUNCT__ "MatILUDTFactorNumeric_SeqAIJ" 2663 PetscErrorCode PETSCMAT_DLLEXPORT MatILUDTFactorNumeric_SeqAIJ(Mat fact,Mat A,const MatFactorInfo *info) 2664 { 2665 Mat C=fact; 2666 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ *)C->data; 2667 IS isrow = b->row,isicol = b->icol; 2668 PetscErrorCode ierr; 2669 const PetscInt *r,*ic,*ics; 2670 PetscInt i,j,k,n=A->rmap->n,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j; 2671 PetscInt *ajtmp,*bjtmp,nz,nzl,nzu,row,*bdiag = b->diag,*pj; 2672 MatScalar *rtmp,*pc,multiplier,*v,*pv,*aa=a->a; 2673 PetscReal dt=info->dt,shift=info->shiftinblocks; 2674 PetscTruth row_identity, col_identity; 2675 2676 PetscFunctionBegin; 2677 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 2678 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 2679 ierr = PetscMalloc((n+1)*sizeof(MatScalar),&rtmp);CHKERRQ(ierr); 2680 ics = ic; 2681 2682 for (i=0; i<n; i++){ 2683 /* initialize rtmp array */ 2684 nzl = bi[i+1] - bi[i]; /* num of nozeros in L(i,:) */ 2685 bjtmp = bj + bi[i]; 2686 for (j=0; j<nzl; j++) rtmp[*bjtmp++] = 0.0; 2687 rtmp[i] = 0.0; 2688 nzu = bdiag[i] - bdiag[i+1]; /* num of nozeros in U(i,:) */ 2689 bjtmp = bj + bdiag[i+1] + 1; 2690 for (j=0; j<nzu; j++) rtmp[*bjtmp++] = 0.0; 2691 2692 /* load in initial unfactored row of A */ 2693 /* printf("row %d\n",i); */ 2694 nz = ai[r[i]+1] - ai[r[i]]; 2695 ajtmp = aj + ai[r[i]]; 2696 v = aa + ai[r[i]]; 2697 for (j=0; j<nz; j++) { 2698 rtmp[ics[*ajtmp++]] = v[j]; 2699 /* printf(" (%d,%g),",ics[ajtmp[j]],rtmp[ics[ajtmp[j]]]); */ 2700 } 2701 /* printf("\n"); */ 2702 2703 /* numerical factorization */ 2704 bjtmp = bj + bi[i]; /* point to 1st entry of L(i,:) */ 2705 nzl = bi[i+1] - bi[i]; /* num of entries in L(i,:) */ 2706 k = 0; 2707 while (k < nzl){ 2708 row = *bjtmp++; 2709 /* printf(" prow %d\n",row); */ 2710 pc = rtmp + row; 2711 pv = b->a + bdiag[row]; /* 1./(diag of the pivot row) */ 2712 multiplier = (*pc) * (*pv); 2713 *pc = multiplier; 2714 if (PetscAbsScalar(multiplier) > dt){ 2715 pj = bj + bdiag[row+1] + 1; /* point to 1st entry of U(row,:) */ 2716 pv = b->a + bdiag[row+1] + 1; 2717 nz = bdiag[row] - bdiag[row+1] - 1; /* num of entries in U(row,:), excluding diagonal */ 2718 for (j=0; j<nz; j++) rtmp[*pj++] -= multiplier * (*pv++); 2719 /* ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr); */ 2720 } 2721 k++; 2722 } 2723 2724 /* finished row so stick it into b->a */ 2725 /* L-part */ 2726 pv = b->a + bi[i] ; 2727 pj = bj + bi[i] ; 2728 nzl = bi[i+1] - bi[i]; 2729 for (j=0; j<nzl; j++) { 2730 pv[j] = rtmp[pj[j]]; 2731 /* printf(" (%d,%g),",pj[j],pv[j]); */ 2732 } 2733 2734 /* diagonal: invert diagonal entries for simplier triangular solves */ 2735 if (rtmp[i] == 0.0) rtmp[i] = dt+shift; 2736 b->a[bdiag[i]] = 1.0/rtmp[i]; 2737 /* printf(" (%d,%g),",i,b->a[bdiag[i]]); */ 2738 2739 /* U-part */ 2740 pv = b->a + bdiag[i+1] + 1; 2741 pj = bj + bdiag[i+1] + 1; 2742 nzu = bdiag[i] - bdiag[i+1] - 1; 2743 for (j=0; j<nzu; j++) { 2744 pv[j] = rtmp[pj[j]]; 2745 /* printf(" (%d,%g),",pj[j],pv[j]); */ 2746 } 2747 /* printf("\n"); */ 2748 } 2749 2750 ierr = PetscFree(rtmp);CHKERRQ(ierr); 2751 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 2752 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 2753 2754 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 2755 ierr = ISIdentity(isicol,&col_identity);CHKERRQ(ierr); 2756 if (row_identity && col_identity) { 2757 C->ops->solve = MatSolve_SeqAIJ_NaturalOrdering_iludt; 2758 } else { 2759 C->ops->solve = MatSolve_SeqAIJ_iludt; 2760 } 2761 C->ops->solveadd = 0; 2762 C->ops->solvetranspose = 0; 2763 C->ops->solvetransposeadd = 0; 2764 C->ops->matsolve = 0; 2765 C->assembled = PETSC_TRUE; 2766 C->preallocated = PETSC_TRUE; 2767 ierr = PetscLogFlops(C->cmap->n);CHKERRQ(ierr); 2768 PetscFunctionReturn(0); 2769 } 2770