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