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