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 PetscInt newshift; 2255 PetscTruth perm_identity; 2256 FactorShiftCtx sctx; 2257 PetscReal rs; 2258 MatScalar d,*v; 2259 2260 PetscFunctionBegin; 2261 /* MatPivotSetUp(): initialize shift context sctx */ 2262 ierr = PetscMemzero(&sctx,sizeof(FactorShiftCtx));CHKERRQ(ierr); 2263 2264 if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) { /* set sctx.shift_top=max{rs} */ 2265 sctx.shift_top = info->zeropivot; 2266 for (i=0; i<mbs; i++) { 2267 /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */ 2268 d = (aa)[a->diag[i]]; 2269 rs = -PetscAbsScalar(d) - PetscRealPart(d); 2270 v = aa+ai[i]; 2271 nz = ai[i+1] - ai[i]; 2272 for (j=0; j<nz; j++) 2273 rs += PetscAbsScalar(v[j]); 2274 if (rs>sctx.shift_top) sctx.shift_top = rs; 2275 } 2276 sctx.shift_top *= 1.1; 2277 sctx.nshift_max = 5; 2278 sctx.shift_lo = 0.; 2279 sctx.shift_hi = 1.; 2280 } 2281 2282 ierr = ISGetIndices(ip,&rip);CHKERRQ(ierr); 2283 ierr = ISGetIndices(iip,&riip);CHKERRQ(ierr); 2284 2285 /* initialization */ 2286 ierr = PetscMalloc3(mbs,MatScalar,&rtmp,mbs,PetscInt,&il,mbs,PetscInt,&jl);CHKERRQ(ierr); 2287 2288 do { 2289 sctx.useshift = PETSC_FALSE; 2290 2291 for (i=0; i<mbs; i++) jl[i] = mbs; 2292 il[0] = 0; 2293 2294 for (k = 0; k<mbs; k++){ 2295 /* zero rtmp */ 2296 nz = bi[k+1] - bi[k]; 2297 bjtmp = bj + bi[k]; 2298 for (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0; 2299 2300 bval = ba + bi[k]; 2301 /* initialize k-th row by the perm[k]-th row of A */ 2302 jmin = ai[rip[k]]; jmax = ai[rip[k]+1]; 2303 for (j = jmin; j < jmax; j++){ 2304 col = riip[aj[j]]; 2305 if (col >= k){ /* only take upper triangular entry */ 2306 rtmp[col] = aa[j]; 2307 *bval++ = 0.0; /* for in-place factorization */ 2308 } 2309 } 2310 /* shift the diagonal of the matrix */ 2311 if (sctx.nshift) rtmp[k] += sctx.shift_amount; 2312 2313 /* modify k-th row by adding in those rows i with U(i,k)!=0 */ 2314 dk = rtmp[k]; 2315 i = jl[k]; /* first row to be added to k_th row */ 2316 2317 while (i < k){ 2318 nexti = jl[i]; /* next row to be added to k_th row */ 2319 2320 /* compute multiplier, update diag(k) and U(i,k) */ 2321 ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */ 2322 uikdi = - ba[ili]*ba[bi[i]]; /* diagonal(k) */ 2323 dk += uikdi*ba[ili]; 2324 ba[ili] = uikdi; /* -U(i,k) */ 2325 2326 /* add multiple of row i to k-th row */ 2327 jmin = ili + 1; jmax = bi[i+1]; 2328 if (jmin < jmax){ 2329 for (j=jmin; j<jmax; j++) rtmp[bj[j]] += uikdi*ba[j]; 2330 /* update il and jl for row i */ 2331 il[i] = jmin; 2332 j = bj[jmin]; jl[i] = jl[j]; jl[j] = i; 2333 } 2334 i = nexti; 2335 } 2336 2337 /* shift the diagonals when zero pivot is detected */ 2338 /* compute rs=sum of abs(off-diagonal) */ 2339 rs = 0.0; 2340 jmin = bi[k]+1; 2341 nz = bi[k+1] - jmin; 2342 bcol = bj + jmin; 2343 for (j=0; j<nz; j++) { 2344 rs += PetscAbsScalar(rtmp[bcol[j]]); 2345 } 2346 2347 sctx.rs = rs; 2348 sctx.pv = dk; 2349 ierr = MatPivotCheck(info,&sctx,k,&newshift);CHKERRQ(ierr); 2350 if (newshift == 1) break; 2351 dk = sctx.pv; 2352 2353 /* copy data into U(k,:) */ 2354 ba[bi[k]] = 1.0/dk; /* U(k,k) */ 2355 jmin = bi[k]+1; jmax = bi[k+1]; 2356 if (jmin < jmax) { 2357 for (j=jmin; j<jmax; j++){ 2358 col = bj[j]; ba[j] = rtmp[col]; 2359 } 2360 /* add the k-th row into il and jl */ 2361 il[k] = jmin; 2362 i = bj[jmin]; jl[k] = jl[i]; jl[i] = k; 2363 } 2364 } 2365 } while (sctx.useshift); 2366 2367 ierr = PetscFree3(rtmp,il,jl);CHKERRQ(ierr); 2368 ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr); 2369 ierr = ISRestoreIndices(iip,&riip);CHKERRQ(ierr); 2370 2371 ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr); 2372 if (perm_identity){ 2373 B->ops->solve = MatSolve_SeqSBAIJ_1_NaturalOrdering_inplace; 2374 B->ops->solvetranspose = MatSolve_SeqSBAIJ_1_NaturalOrdering_inplace; 2375 B->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_1_NaturalOrdering_inplace; 2376 B->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_1_NaturalOrdering_inplace; 2377 } else { 2378 B->ops->solve = MatSolve_SeqSBAIJ_1_inplace; 2379 B->ops->solvetranspose = MatSolve_SeqSBAIJ_1_inplace; 2380 B->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_1_inplace; 2381 B->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_1_inplace; 2382 } 2383 2384 C->assembled = PETSC_TRUE; 2385 C->preallocated = PETSC_TRUE; 2386 ierr = PetscLogFlops(C->rmap->n);CHKERRQ(ierr); 2387 if (sctx.nshift){ 2388 if (info->shifttype == (PetscReal)MAT_SHIFT_NONZERO) { 2389 ierr = PetscInfo2(A,"number of shiftnz tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr); 2390 } else if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) { 2391 ierr = PetscInfo2(A,"number of shiftpd tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr); 2392 } 2393 } 2394 PetscFunctionReturn(0); 2395 } 2396 2397 /* 2398 icc() under revised new data structure. 2399 Factored arrays bj and ba are stored as 2400 U(0,:),...,U(i,:),U(n-1,:) 2401 2402 ui=fact->i is an array of size n+1, in which 2403 ui+ 2404 ui[i]: points to 1st entry of U(i,:),i=0,...,n-1 2405 ui[n]: points to U(n-1,n-1)+1 2406 2407 udiag=fact->diag is an array of size n,in which 2408 udiag[i]: points to diagonal of U(i,:), i=0,...,n-1 2409 2410 U(i,:) contains udiag[i] as its last entry, i.e., 2411 U(i,:) = (u[i,i+1],...,u[i,n-1],diag[i]) 2412 */ 2413 2414 #undef __FUNCT__ 2415 #define __FUNCT__ "MatICCFactorSymbolic_SeqAIJ" 2416 PetscErrorCode MatICCFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS perm,const MatFactorInfo *info) 2417 { 2418 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2419 Mat_SeqSBAIJ *b; 2420 PetscErrorCode ierr; 2421 PetscTruth perm_identity,missing; 2422 PetscInt reallocs=0,i,*ai=a->i,*aj=a->j,am=A->rmap->n,*ui,*udiag; 2423 const PetscInt *rip,*riip; 2424 PetscInt jmin,jmax,nzk,k,j,*jl,prow,*il,nextprow; 2425 PetscInt nlnk,*lnk,*lnk_lvl=PETSC_NULL,d; 2426 PetscInt ncols,ncols_upper,*cols,*ajtmp,*uj,**uj_ptr,**uj_lvl_ptr; 2427 PetscReal fill=info->fill,levels=info->levels; 2428 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 2429 PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL; 2430 PetscBT lnkbt; 2431 IS iperm; 2432 2433 PetscFunctionBegin; 2434 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); 2435 ierr = MatMissingDiagonal(A,&missing,&d);CHKERRQ(ierr); 2436 if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d); 2437 ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 2438 ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr); 2439 2440 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr); 2441 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&udiag);CHKERRQ(ierr); 2442 ui[0] = 0; 2443 2444 /* ICC(0) without matrix ordering: simply rearrange column indices */ 2445 if (!levels && perm_identity) { 2446 for (i=0; i<am; i++) { 2447 ncols = ai[i+1] - a->diag[i]; 2448 ui[i+1] = ui[i] + ncols; 2449 udiag[i] = ui[i+1] - 1; /* points to the last entry of U(i,:) */ 2450 } 2451 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2452 cols = uj; 2453 for (i=0; i<am; i++) { 2454 aj = a->j + a->diag[i] + 1; /* 1st entry of U(i,:) without diagonal */ 2455 ncols = ai[i+1] - a->diag[i] -1; 2456 for (j=0; j<ncols; j++) *cols++ = aj[j]; 2457 *cols++ = i; /* diagoanl is located as the last entry of U(i,:) */ 2458 } 2459 } else { /* case: levels>0 || (levels=0 && !perm_identity) */ 2460 ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr); 2461 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 2462 2463 /* initialization */ 2464 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ajtmp);CHKERRQ(ierr); 2465 2466 /* jl: linked list for storing indices of the pivot rows 2467 il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */ 2468 ierr = PetscMalloc4(am,PetscInt*,&uj_ptr,am,PetscInt*,&uj_lvl_ptr,am,PetscInt,&jl,am,PetscInt,&il);CHKERRQ(ierr); 2469 for (i=0; i<am; i++){ 2470 jl[i] = am; il[i] = 0; 2471 } 2472 2473 /* create and initialize a linked list for storing column indices of the active row k */ 2474 nlnk = am + 1; 2475 ierr = PetscIncompleteLLCreate(am,am,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 2476 2477 /* initial FreeSpace size is fill*(ai[am]+1) */ 2478 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr); 2479 current_space = free_space; 2480 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space_lvl);CHKERRQ(ierr); 2481 current_space_lvl = free_space_lvl; 2482 2483 for (k=0; k<am; k++){ /* for each active row k */ 2484 /* initialize lnk by the column indices of row rip[k] of A */ 2485 nzk = 0; 2486 ncols = ai[rip[k]+1] - ai[rip[k]]; 2487 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); 2488 ncols_upper = 0; 2489 for (j=0; j<ncols; j++){ 2490 i = *(aj + ai[rip[k]] + j); /* unpermuted column index */ 2491 if (riip[i] >= k){ /* only take upper triangular entry */ 2492 ajtmp[ncols_upper] = i; 2493 ncols_upper++; 2494 } 2495 } 2496 ierr = PetscIncompleteLLInit(ncols_upper,ajtmp,am,riip,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 2497 nzk += nlnk; 2498 2499 /* update lnk by computing fill-in for each pivot row to be merged in */ 2500 prow = jl[k]; /* 1st pivot row */ 2501 2502 while (prow < k){ 2503 nextprow = jl[prow]; 2504 2505 /* merge prow into k-th row */ 2506 jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */ 2507 jmax = ui[prow+1]; 2508 ncols = jmax-jmin; 2509 i = jmin - ui[prow]; 2510 cols = uj_ptr[prow] + i; /* points to the 2nd nzero entry in U(prow,k:am-1) */ 2511 uj = uj_lvl_ptr[prow] + i; /* levels of cols */ 2512 j = *(uj - 1); 2513 ierr = PetscICCLLAddSorted(ncols,cols,levels,uj,am,nlnk,lnk,lnk_lvl,lnkbt,j);CHKERRQ(ierr); 2514 nzk += nlnk; 2515 2516 /* update il and jl for prow */ 2517 if (jmin < jmax){ 2518 il[prow] = jmin; 2519 j = *cols; jl[prow] = jl[j]; jl[j] = prow; 2520 } 2521 prow = nextprow; 2522 } 2523 2524 /* if free space is not available, make more free space */ 2525 if (current_space->local_remaining<nzk) { 2526 i = am - k + 1; /* num of unfactored rows */ 2527 i *= PetscMin(nzk, i-1); /* i*nzk, i*(i-1): estimated and max additional space needed */ 2528 ierr = PetscFreeSpaceGet(i,¤t_space);CHKERRQ(ierr); 2529 ierr = PetscFreeSpaceGet(i,¤t_space_lvl);CHKERRQ(ierr); 2530 reallocs++; 2531 } 2532 2533 /* copy data into free_space and free_space_lvl, then initialize lnk */ 2534 if (nzk == 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Empty row %D in ICC matrix factor",k); 2535 ierr = PetscIncompleteLLClean(am,am,nzk,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr); 2536 2537 /* add the k-th row into il and jl */ 2538 if (nzk > 1){ 2539 i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */ 2540 jl[k] = jl[i]; jl[i] = k; 2541 il[k] = ui[k] + 1; 2542 } 2543 uj_ptr[k] = current_space->array; 2544 uj_lvl_ptr[k] = current_space_lvl->array; 2545 2546 current_space->array += nzk; 2547 current_space->local_used += nzk; 2548 current_space->local_remaining -= nzk; 2549 2550 current_space_lvl->array += nzk; 2551 current_space_lvl->local_used += nzk; 2552 current_space_lvl->local_remaining -= nzk; 2553 2554 ui[k+1] = ui[k] + nzk; 2555 } 2556 2557 #if defined(PETSC_USE_INFO) 2558 if (ai[am] != 0) { 2559 PetscReal af = (PetscReal)ui[am]/((PetscReal)ai[am]); 2560 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr); 2561 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 2562 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr); 2563 } else { 2564 ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr); 2565 } 2566 #endif 2567 2568 ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); 2569 ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr); 2570 ierr = PetscFree4(uj_ptr,uj_lvl_ptr,jl,il);CHKERRQ(ierr); 2571 ierr = PetscFree(ajtmp);CHKERRQ(ierr); 2572 2573 /* destroy list of free space and other temporary array(s) */ 2574 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2575 ierr = PetscFreeSpaceContiguous_Cholesky(&free_space,uj,am,ui,udiag);CHKERRQ(ierr); /* store matrix factor */ 2576 ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 2577 ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr); 2578 2579 } /* end of case: levels>0 || (levels=0 && !perm_identity) */ 2580 2581 /* put together the new matrix in MATSEQSBAIJ format */ 2582 b = (Mat_SeqSBAIJ*)(fact)->data; 2583 b->singlemalloc = PETSC_FALSE; 2584 ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr); 2585 b->j = uj; 2586 b->i = ui; 2587 b->diag = udiag; 2588 b->free_diag = PETSC_TRUE; 2589 b->ilen = 0; 2590 b->imax = 0; 2591 b->row = perm; 2592 b->col = perm; 2593 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2594 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2595 b->icol = iperm; 2596 b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ 2597 ierr = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 2598 ierr = PetscLogObjectMemory(fact,ui[am]*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 2599 b->maxnz = b->nz = ui[am]; 2600 b->free_a = PETSC_TRUE; 2601 b->free_ij = PETSC_TRUE; 2602 2603 fact->info.factor_mallocs = reallocs; 2604 fact->info.fill_ratio_given = fill; 2605 if (ai[am] != 0) { 2606 fact->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]); 2607 } else { 2608 fact->info.fill_ratio_needed = 0.0; 2609 } 2610 fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ; 2611 PetscFunctionReturn(0); 2612 } 2613 2614 #undef __FUNCT__ 2615 #define __FUNCT__ "MatICCFactorSymbolic_SeqAIJ_inplace" 2616 PetscErrorCode MatICCFactorSymbolic_SeqAIJ_inplace(Mat fact,Mat A,IS perm,const MatFactorInfo *info) 2617 { 2618 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2619 Mat_SeqSBAIJ *b; 2620 PetscErrorCode ierr; 2621 PetscTruth perm_identity,missing; 2622 PetscInt reallocs=0,i,*ai=a->i,*aj=a->j,am=A->rmap->n,*ui,*udiag; 2623 const PetscInt *rip,*riip; 2624 PetscInt jmin,jmax,nzk,k,j,*jl,prow,*il,nextprow; 2625 PetscInt nlnk,*lnk,*lnk_lvl=PETSC_NULL,d; 2626 PetscInt ncols,ncols_upper,*cols,*ajtmp,*uj,**uj_ptr,**uj_lvl_ptr; 2627 PetscReal fill=info->fill,levels=info->levels; 2628 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 2629 PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL; 2630 PetscBT lnkbt; 2631 IS iperm; 2632 2633 PetscFunctionBegin; 2634 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); 2635 ierr = MatMissingDiagonal(A,&missing,&d);CHKERRQ(ierr); 2636 if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d); 2637 ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 2638 ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr); 2639 2640 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr); 2641 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&udiag);CHKERRQ(ierr); 2642 ui[0] = 0; 2643 2644 /* ICC(0) without matrix ordering: simply copies fill pattern */ 2645 if (!levels && perm_identity) { 2646 2647 for (i=0; i<am; i++) { 2648 ui[i+1] = ui[i] + ai[i+1] - a->diag[i]; 2649 udiag[i] = ui[i]; 2650 } 2651 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2652 cols = uj; 2653 for (i=0; i<am; i++) { 2654 aj = a->j + a->diag[i]; 2655 ncols = ui[i+1] - ui[i]; 2656 for (j=0; j<ncols; j++) *cols++ = *aj++; 2657 } 2658 } else { /* case: levels>0 || (levels=0 && !perm_identity) */ 2659 ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr); 2660 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 2661 2662 /* initialization */ 2663 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ajtmp);CHKERRQ(ierr); 2664 2665 /* jl: linked list for storing indices of the pivot rows 2666 il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */ 2667 ierr = PetscMalloc4(am,PetscInt*,&uj_ptr,am,PetscInt*,&uj_lvl_ptr,am,PetscInt,&jl,am,PetscInt,&il);CHKERRQ(ierr); 2668 for (i=0; i<am; i++){ 2669 jl[i] = am; il[i] = 0; 2670 } 2671 2672 /* create and initialize a linked list for storing column indices of the active row k */ 2673 nlnk = am + 1; 2674 ierr = PetscIncompleteLLCreate(am,am,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 2675 2676 /* initial FreeSpace size is fill*(ai[am]+1) */ 2677 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr); 2678 current_space = free_space; 2679 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space_lvl);CHKERRQ(ierr); 2680 current_space_lvl = free_space_lvl; 2681 2682 for (k=0; k<am; k++){ /* for each active row k */ 2683 /* initialize lnk by the column indices of row rip[k] of A */ 2684 nzk = 0; 2685 ncols = ai[rip[k]+1] - ai[rip[k]]; 2686 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); 2687 ncols_upper = 0; 2688 for (j=0; j<ncols; j++){ 2689 i = *(aj + ai[rip[k]] + j); /* unpermuted column index */ 2690 if (riip[i] >= k){ /* only take upper triangular entry */ 2691 ajtmp[ncols_upper] = i; 2692 ncols_upper++; 2693 } 2694 } 2695 ierr = PetscIncompleteLLInit(ncols_upper,ajtmp,am,riip,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 2696 nzk += nlnk; 2697 2698 /* update lnk by computing fill-in for each pivot row to be merged in */ 2699 prow = jl[k]; /* 1st pivot row */ 2700 2701 while (prow < k){ 2702 nextprow = jl[prow]; 2703 2704 /* merge prow into k-th row */ 2705 jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */ 2706 jmax = ui[prow+1]; 2707 ncols = jmax-jmin; 2708 i = jmin - ui[prow]; 2709 cols = uj_ptr[prow] + i; /* points to the 2nd nzero entry in U(prow,k:am-1) */ 2710 uj = uj_lvl_ptr[prow] + i; /* levels of cols */ 2711 j = *(uj - 1); 2712 ierr = PetscICCLLAddSorted(ncols,cols,levels,uj,am,nlnk,lnk,lnk_lvl,lnkbt,j);CHKERRQ(ierr); 2713 nzk += nlnk; 2714 2715 /* update il and jl for prow */ 2716 if (jmin < jmax){ 2717 il[prow] = jmin; 2718 j = *cols; jl[prow] = jl[j]; jl[j] = prow; 2719 } 2720 prow = nextprow; 2721 } 2722 2723 /* if free space is not available, make more free space */ 2724 if (current_space->local_remaining<nzk) { 2725 i = am - k + 1; /* num of unfactored rows */ 2726 i *= PetscMin(nzk, (i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */ 2727 ierr = PetscFreeSpaceGet(i,¤t_space);CHKERRQ(ierr); 2728 ierr = PetscFreeSpaceGet(i,¤t_space_lvl);CHKERRQ(ierr); 2729 reallocs++; 2730 } 2731 2732 /* copy data into free_space and free_space_lvl, then initialize lnk */ 2733 if (!nzk) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Empty row %D in ICC matrix factor",k); 2734 ierr = PetscIncompleteLLClean(am,am,nzk,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr); 2735 2736 /* add the k-th row into il and jl */ 2737 if (nzk > 1){ 2738 i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */ 2739 jl[k] = jl[i]; jl[i] = k; 2740 il[k] = ui[k] + 1; 2741 } 2742 uj_ptr[k] = current_space->array; 2743 uj_lvl_ptr[k] = current_space_lvl->array; 2744 2745 current_space->array += nzk; 2746 current_space->local_used += nzk; 2747 current_space->local_remaining -= nzk; 2748 2749 current_space_lvl->array += nzk; 2750 current_space_lvl->local_used += nzk; 2751 current_space_lvl->local_remaining -= nzk; 2752 2753 ui[k+1] = ui[k] + nzk; 2754 } 2755 2756 #if defined(PETSC_USE_INFO) 2757 if (ai[am] != 0) { 2758 PetscReal af = (PetscReal)ui[am]/((PetscReal)ai[am]); 2759 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr); 2760 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 2761 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr); 2762 } else { 2763 ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr); 2764 } 2765 #endif 2766 2767 ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); 2768 ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr); 2769 ierr = PetscFree4(uj_ptr,uj_lvl_ptr,jl,il);CHKERRQ(ierr); 2770 ierr = PetscFree(ajtmp);CHKERRQ(ierr); 2771 2772 /* destroy list of free space and other temporary array(s) */ 2773 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2774 ierr = PetscFreeSpaceContiguous(&free_space,uj);CHKERRQ(ierr); 2775 ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 2776 ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr); 2777 2778 } /* end of case: levels>0 || (levels=0 && !perm_identity) */ 2779 2780 /* put together the new matrix in MATSEQSBAIJ format */ 2781 2782 b = (Mat_SeqSBAIJ*)fact->data; 2783 b->singlemalloc = PETSC_FALSE; 2784 ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr); 2785 b->j = uj; 2786 b->i = ui; 2787 b->diag = udiag; 2788 b->free_diag = PETSC_TRUE; 2789 b->ilen = 0; 2790 b->imax = 0; 2791 b->row = perm; 2792 b->col = perm; 2793 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2794 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2795 b->icol = iperm; 2796 b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ 2797 ierr = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 2798 ierr = PetscLogObjectMemory(fact,(ui[am]-am)*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 2799 b->maxnz = b->nz = ui[am]; 2800 b->free_a = PETSC_TRUE; 2801 b->free_ij = PETSC_TRUE; 2802 2803 fact->info.factor_mallocs = reallocs; 2804 fact->info.fill_ratio_given = fill; 2805 if (ai[am] != 0) { 2806 fact->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]); 2807 } else { 2808 fact->info.fill_ratio_needed = 0.0; 2809 } 2810 fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ_inplace; 2811 PetscFunctionReturn(0); 2812 } 2813 2814 PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS perm,const MatFactorInfo *info) 2815 { 2816 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2817 Mat_SeqSBAIJ *b; 2818 PetscErrorCode ierr; 2819 PetscTruth perm_identity; 2820 PetscReal fill = info->fill; 2821 const PetscInt *rip,*riip; 2822 PetscInt i,am=A->rmap->n,*ai=a->i,*aj=a->j,reallocs=0,prow; 2823 PetscInt *jl,jmin,jmax,nzk,*ui,k,j,*il,nextprow; 2824 PetscInt nlnk,*lnk,ncols,ncols_upper,*cols,*uj,**ui_ptr,*uj_ptr,*udiag; 2825 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 2826 PetscBT lnkbt; 2827 IS iperm; 2828 2829 PetscFunctionBegin; 2830 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); 2831 /* check whether perm is the identity mapping */ 2832 ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 2833 ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr); 2834 ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr); 2835 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 2836 2837 /* initialization */ 2838 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr); 2839 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&udiag);CHKERRQ(ierr); 2840 ui[0] = 0; 2841 2842 /* jl: linked list for storing indices of the pivot rows 2843 il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */ 2844 ierr = PetscMalloc4(am,PetscInt*,&ui_ptr,am,PetscInt,&jl,am,PetscInt,&il,am,PetscInt,&cols);CHKERRQ(ierr); 2845 for (i=0; i<am; i++){ 2846 jl[i] = am; il[i] = 0; 2847 } 2848 2849 /* create and initialize a linked list for storing column indices of the active row k */ 2850 nlnk = am + 1; 2851 ierr = PetscLLCreate(am,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2852 2853 /* initial FreeSpace size is fill*(ai[am]+1) */ 2854 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr); 2855 current_space = free_space; 2856 2857 for (k=0; k<am; k++){ /* for each active row k */ 2858 /* initialize lnk by the column indices of row rip[k] of A */ 2859 nzk = 0; 2860 ncols = ai[rip[k]+1] - ai[rip[k]]; 2861 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); 2862 ncols_upper = 0; 2863 for (j=0; j<ncols; j++){ 2864 i = riip[*(aj + ai[rip[k]] + j)]; 2865 if (i >= k){ /* only take upper triangular entry */ 2866 cols[ncols_upper] = i; 2867 ncols_upper++; 2868 } 2869 } 2870 ierr = PetscLLAdd(ncols_upper,cols,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2871 nzk += nlnk; 2872 2873 /* update lnk by computing fill-in for each pivot row to be merged in */ 2874 prow = jl[k]; /* 1st pivot row */ 2875 2876 while (prow < k){ 2877 nextprow = jl[prow]; 2878 /* merge prow into k-th row */ 2879 jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */ 2880 jmax = ui[prow+1]; 2881 ncols = jmax-jmin; 2882 uj_ptr = ui_ptr[prow] + jmin - ui[prow]; /* points to the 2nd nzero entry in U(prow,k:am-1) */ 2883 ierr = PetscLLAddSorted(ncols,uj_ptr,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2884 nzk += nlnk; 2885 2886 /* update il and jl for prow */ 2887 if (jmin < jmax){ 2888 il[prow] = jmin; 2889 j = *uj_ptr; jl[prow] = jl[j]; jl[j] = prow; 2890 } 2891 prow = nextprow; 2892 } 2893 2894 /* if free space is not available, make more free space */ 2895 if (current_space->local_remaining<nzk) { 2896 i = am - k + 1; /* num of unfactored rows */ 2897 i *= PetscMin(nzk,i-1); /* i*nzk, i*(i-1): estimated and max additional space needed */ 2898 ierr = PetscFreeSpaceGet(i,¤t_space);CHKERRQ(ierr); 2899 reallocs++; 2900 } 2901 2902 /* copy data into free space, then initialize lnk */ 2903 ierr = PetscLLClean(am,am,nzk,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 2904 2905 /* add the k-th row into il and jl */ 2906 if (nzk > 1){ 2907 i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */ 2908 jl[k] = jl[i]; jl[i] = k; 2909 il[k] = ui[k] + 1; 2910 } 2911 ui_ptr[k] = current_space->array; 2912 current_space->array += nzk; 2913 current_space->local_used += nzk; 2914 current_space->local_remaining -= nzk; 2915 2916 ui[k+1] = ui[k] + nzk; 2917 } 2918 2919 #if defined(PETSC_USE_INFO) 2920 if (ai[am] != 0) { 2921 PetscReal af = (PetscReal)(ui[am])/((PetscReal)ai[am]); 2922 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr); 2923 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 2924 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr); 2925 } else { 2926 ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr); 2927 } 2928 #endif 2929 2930 ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); 2931 ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr); 2932 ierr = PetscFree4(ui_ptr,jl,il,cols);CHKERRQ(ierr); 2933 2934 /* destroy list of free space and other temporary array(s) */ 2935 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2936 ierr = PetscFreeSpaceContiguous_Cholesky(&free_space,uj,am,ui,udiag);CHKERRQ(ierr); /* store matrix factor */ 2937 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 2938 2939 /* put together the new matrix in MATSEQSBAIJ format */ 2940 2941 b = (Mat_SeqSBAIJ*)fact->data; 2942 b->singlemalloc = PETSC_FALSE; 2943 b->free_a = PETSC_TRUE; 2944 b->free_ij = PETSC_TRUE; 2945 ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr); 2946 b->j = uj; 2947 b->i = ui; 2948 b->diag = udiag; 2949 b->free_diag = PETSC_TRUE; 2950 b->ilen = 0; 2951 b->imax = 0; 2952 b->row = perm; 2953 b->col = perm; 2954 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2955 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2956 b->icol = iperm; 2957 b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ 2958 ierr = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 2959 ierr = PetscLogObjectMemory(fact,ui[am]*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 2960 b->maxnz = b->nz = ui[am]; 2961 2962 fact->info.factor_mallocs = reallocs; 2963 fact->info.fill_ratio_given = fill; 2964 if (ai[am] != 0) { 2965 fact->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]); 2966 } else { 2967 fact->info.fill_ratio_needed = 0.0; 2968 } 2969 fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ; 2970 PetscFunctionReturn(0); 2971 } 2972 2973 #undef __FUNCT__ 2974 #define __FUNCT__ "MatCholeskyFactorSymbolic_SeqAIJ_inplace" 2975 PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJ_inplace(Mat fact,Mat A,IS perm,const MatFactorInfo *info) 2976 { 2977 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2978 Mat_SeqSBAIJ *b; 2979 PetscErrorCode ierr; 2980 PetscTruth perm_identity; 2981 PetscReal fill = info->fill; 2982 const PetscInt *rip,*riip; 2983 PetscInt i,am=A->rmap->n,*ai=a->i,*aj=a->j,reallocs=0,prow; 2984 PetscInt *jl,jmin,jmax,nzk,*ui,k,j,*il,nextprow; 2985 PetscInt nlnk,*lnk,ncols,ncols_upper,*cols,*uj,**ui_ptr,*uj_ptr; 2986 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 2987 PetscBT lnkbt; 2988 IS iperm; 2989 2990 PetscFunctionBegin; 2991 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); 2992 /* check whether perm is the identity mapping */ 2993 ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 2994 ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr); 2995 ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr); 2996 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 2997 2998 /* initialization */ 2999 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr); 3000 ui[0] = 0; 3001 3002 /* jl: linked list for storing indices of the pivot rows 3003 il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */ 3004 ierr = PetscMalloc4(am,PetscInt*,&ui_ptr,am,PetscInt,&jl,am,PetscInt,&il,am,PetscInt,&cols);CHKERRQ(ierr); 3005 for (i=0; i<am; i++){ 3006 jl[i] = am; il[i] = 0; 3007 } 3008 3009 /* create and initialize a linked list for storing column indices of the active row k */ 3010 nlnk = am + 1; 3011 ierr = PetscLLCreate(am,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 3012 3013 /* initial FreeSpace size is fill*(ai[am]+1) */ 3014 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr); 3015 current_space = free_space; 3016 3017 for (k=0; k<am; k++){ /* for each active row k */ 3018 /* initialize lnk by the column indices of row rip[k] of A */ 3019 nzk = 0; 3020 ncols = ai[rip[k]+1] - ai[rip[k]]; 3021 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); 3022 ncols_upper = 0; 3023 for (j=0; j<ncols; j++){ 3024 i = riip[*(aj + ai[rip[k]] + j)]; 3025 if (i >= k){ /* only take upper triangular entry */ 3026 cols[ncols_upper] = i; 3027 ncols_upper++; 3028 } 3029 } 3030 ierr = PetscLLAdd(ncols_upper,cols,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 3031 nzk += nlnk; 3032 3033 /* update lnk by computing fill-in for each pivot row to be merged in */ 3034 prow = jl[k]; /* 1st pivot row */ 3035 3036 while (prow < k){ 3037 nextprow = jl[prow]; 3038 /* merge prow into k-th row */ 3039 jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */ 3040 jmax = ui[prow+1]; 3041 ncols = jmax-jmin; 3042 uj_ptr = ui_ptr[prow] + jmin - ui[prow]; /* points to the 2nd nzero entry in U(prow,k:am-1) */ 3043 ierr = PetscLLAddSorted(ncols,uj_ptr,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 3044 nzk += nlnk; 3045 3046 /* update il and jl for prow */ 3047 if (jmin < jmax){ 3048 il[prow] = jmin; 3049 j = *uj_ptr; jl[prow] = jl[j]; jl[j] = prow; 3050 } 3051 prow = nextprow; 3052 } 3053 3054 /* if free space is not available, make more free space */ 3055 if (current_space->local_remaining<nzk) { 3056 i = am - k + 1; /* num of unfactored rows */ 3057 i = PetscMin(i*nzk, i*(i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */ 3058 ierr = PetscFreeSpaceGet(i,¤t_space);CHKERRQ(ierr); 3059 reallocs++; 3060 } 3061 3062 /* copy data into free space, then initialize lnk */ 3063 ierr = PetscLLClean(am,am,nzk,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 3064 3065 /* add the k-th row into il and jl */ 3066 if (nzk-1 > 0){ 3067 i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */ 3068 jl[k] = jl[i]; jl[i] = k; 3069 il[k] = ui[k] + 1; 3070 } 3071 ui_ptr[k] = current_space->array; 3072 current_space->array += nzk; 3073 current_space->local_used += nzk; 3074 current_space->local_remaining -= nzk; 3075 3076 ui[k+1] = ui[k] + nzk; 3077 } 3078 3079 #if defined(PETSC_USE_INFO) 3080 if (ai[am] != 0) { 3081 PetscReal af = (PetscReal)(ui[am])/((PetscReal)ai[am]); 3082 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr); 3083 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 3084 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr); 3085 } else { 3086 ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr); 3087 } 3088 #endif 3089 3090 ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); 3091 ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr); 3092 ierr = PetscFree4(ui_ptr,jl,il,cols);CHKERRQ(ierr); 3093 3094 /* destroy list of free space and other temporary array(s) */ 3095 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 3096 ierr = PetscFreeSpaceContiguous(&free_space,uj);CHKERRQ(ierr); 3097 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 3098 3099 /* put together the new matrix in MATSEQSBAIJ format */ 3100 3101 b = (Mat_SeqSBAIJ*)fact->data; 3102 b->singlemalloc = PETSC_FALSE; 3103 b->free_a = PETSC_TRUE; 3104 b->free_ij = PETSC_TRUE; 3105 ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr); 3106 b->j = uj; 3107 b->i = ui; 3108 b->diag = 0; 3109 b->ilen = 0; 3110 b->imax = 0; 3111 b->row = perm; 3112 b->col = perm; 3113 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 3114 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 3115 b->icol = iperm; 3116 b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ 3117 ierr = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 3118 ierr = PetscLogObjectMemory(fact,(ui[am]-am)*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 3119 b->maxnz = b->nz = ui[am]; 3120 3121 fact->info.factor_mallocs = reallocs; 3122 fact->info.fill_ratio_given = fill; 3123 if (ai[am] != 0) { 3124 fact->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]); 3125 } else { 3126 fact->info.fill_ratio_needed = 0.0; 3127 } 3128 fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ_inplace; 3129 PetscFunctionReturn(0); 3130 } 3131 3132 #undef __FUNCT__ 3133 #define __FUNCT__ "MatSolve_SeqAIJ_NaturalOrdering" 3134 PetscErrorCode MatSolve_SeqAIJ_NaturalOrdering(Mat A,Vec bb,Vec xx) 3135 { 3136 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3137 PetscErrorCode ierr; 3138 PetscInt n = A->rmap->n; 3139 const PetscInt *ai = a->i,*aj = a->j,*adiag = a->diag,*vi; 3140 PetscScalar *x,sum; 3141 const PetscScalar *b; 3142 const MatScalar *aa = a->a,*v; 3143 PetscInt i,nz; 3144 3145 PetscFunctionBegin; 3146 if (!n) PetscFunctionReturn(0); 3147 3148 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 3149 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 3150 3151 /* forward solve the lower triangular */ 3152 x[0] = b[0]; 3153 v = aa; 3154 vi = aj; 3155 for (i=1; i<n; i++) { 3156 nz = ai[i+1] - ai[i]; 3157 sum = b[i]; 3158 PetscSparseDenseMinusDot(sum,x,v,vi,nz); 3159 v += nz; 3160 vi += nz; 3161 x[i] = sum; 3162 } 3163 3164 /* backward solve the upper triangular */ 3165 for (i=n-1; i>=0; i--){ 3166 v = aa + adiag[i+1] + 1; 3167 vi = aj + adiag[i+1] + 1; 3168 nz = adiag[i] - adiag[i+1]-1; 3169 sum = x[i]; 3170 PetscSparseDenseMinusDot(sum,x,v,vi,nz); 3171 x[i] = sum*v[nz]; /* x[i]=aa[adiag[i]]*sum; v++; */ 3172 } 3173 3174 ierr = PetscLogFlops(2.0*a->nz - A->cmap->n);CHKERRQ(ierr); 3175 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 3176 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 3177 PetscFunctionReturn(0); 3178 } 3179 3180 #undef __FUNCT__ 3181 #define __FUNCT__ "MatSolve_SeqAIJ" 3182 PetscErrorCode MatSolve_SeqAIJ(Mat A,Vec bb,Vec xx) 3183 { 3184 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3185 IS iscol = a->col,isrow = a->row; 3186 PetscErrorCode ierr; 3187 PetscInt i,n=A->rmap->n,*vi,*ai=a->i,*aj=a->j,*adiag = a->diag,nz; 3188 const PetscInt *rout,*cout,*r,*c; 3189 PetscScalar *x,*tmp,sum; 3190 const PetscScalar *b; 3191 const MatScalar *aa = a->a,*v; 3192 3193 PetscFunctionBegin; 3194 if (!n) PetscFunctionReturn(0); 3195 3196 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 3197 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 3198 tmp = a->solve_work; 3199 3200 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 3201 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 3202 3203 /* forward solve the lower triangular */ 3204 tmp[0] = b[r[0]]; 3205 v = aa; 3206 vi = aj; 3207 for (i=1; i<n; i++) { 3208 nz = ai[i+1] - ai[i]; 3209 sum = b[r[i]]; 3210 PetscSparseDenseMinusDot(sum,tmp,v,vi,nz); 3211 tmp[i] = sum; 3212 v += nz; vi += nz; 3213 } 3214 3215 /* backward solve the upper triangular */ 3216 for (i=n-1; i>=0; i--){ 3217 v = aa + adiag[i+1]+1; 3218 vi = aj + adiag[i+1]+1; 3219 nz = adiag[i]-adiag[i+1]-1; 3220 sum = tmp[i]; 3221 PetscSparseDenseMinusDot(sum,tmp,v,vi,nz); 3222 x[c[i]] = tmp[i] = sum*v[nz]; /* v[nz] = aa[adiag[i]] */ 3223 } 3224 3225 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 3226 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 3227 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 3228 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 3229 ierr = PetscLogFlops(2*a->nz - A->cmap->n);CHKERRQ(ierr); 3230 PetscFunctionReturn(0); 3231 } 3232 3233 #undef __FUNCT__ 3234 #define __FUNCT__ "MatILUDTFactor_SeqAIJ" 3235 /* 3236 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 3237 */ 3238 PetscErrorCode MatILUDTFactor_SeqAIJ(Mat A,IS isrow,IS iscol,const MatFactorInfo *info,Mat *fact) 3239 { 3240 Mat B = *fact; 3241 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b; 3242 IS isicol; 3243 PetscErrorCode ierr; 3244 const PetscInt *r,*ic; 3245 PetscInt i,n=A->rmap->n,*ai=a->i,*aj=a->j,*ajtmp,*adiag; 3246 PetscInt *bi,*bj,*bdiag,*bdiag_rev; 3247 PetscInt row,nzi,nzi_bl,nzi_bu,*im,nzi_al,nzi_au; 3248 PetscInt nlnk,*lnk; 3249 PetscBT lnkbt; 3250 PetscTruth row_identity,icol_identity,both_identity; 3251 MatScalar *aatmp,*pv,*batmp,*ba,*rtmp,*pc,multiplier,*vtmp,diag_tmp; 3252 const PetscInt *ics; 3253 PetscInt j,nz,*pj,*bjtmp,k,ncut,*jtmp; 3254 PetscReal dt=info->dt,dtcol=info->dtcol,shift=info->shiftamount; 3255 PetscInt dtcount=(PetscInt)info->dtcount,nnz_max; 3256 PetscTruth missing; 3257 3258 PetscFunctionBegin; 3259 3260 if (dt == PETSC_DEFAULT) dt = 0.005; 3261 if (dtcol == PETSC_DEFAULT) dtcol = 0.01; /* XXX unused! */ 3262 if (dtcount == PETSC_DEFAULT) dtcount = (PetscInt)(1.5*a->rmax); 3263 3264 /* ------- symbolic factorization, can be reused ---------*/ 3265 ierr = MatMissingDiagonal(A,&missing,&i);CHKERRQ(ierr); 3266 if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",i); 3267 adiag=a->diag; 3268 3269 ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 3270 3271 /* bdiag is location of diagonal in factor */ 3272 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr); /* becomes b->diag */ 3273 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag_rev);CHKERRQ(ierr); /* temporary */ 3274 3275 /* allocate row pointers bi */ 3276 ierr = PetscMalloc((2*n+2)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 3277 3278 /* allocate bj and ba; max num of nonzero entries is (ai[n]+2*n*dtcount+2) */ 3279 if (dtcount > n-1) dtcount = n-1; /* diagonal is excluded */ 3280 nnz_max = ai[n]+2*n*dtcount+2; 3281 3282 ierr = PetscMalloc((nnz_max+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 3283 ierr = PetscMalloc((nnz_max+1)*sizeof(MatScalar),&ba);CHKERRQ(ierr); 3284 3285 /* put together the new matrix */ 3286 ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr); 3287 ierr = PetscLogObjectParent(B,isicol);CHKERRQ(ierr); 3288 b = (Mat_SeqAIJ*)B->data; 3289 b->free_a = PETSC_TRUE; 3290 b->free_ij = PETSC_TRUE; 3291 b->singlemalloc = PETSC_FALSE; 3292 b->a = ba; 3293 b->j = bj; 3294 b->i = bi; 3295 b->diag = bdiag; 3296 b->ilen = 0; 3297 b->imax = 0; 3298 b->row = isrow; 3299 b->col = iscol; 3300 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 3301 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 3302 b->icol = isicol; 3303 ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 3304 3305 ierr = PetscLogObjectMemory(B,nnz_max*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 3306 b->maxnz = nnz_max; 3307 3308 B->factortype = MAT_FACTOR_ILUDT; 3309 B->info.factor_mallocs = 0; 3310 B->info.fill_ratio_given = ((PetscReal)nnz_max)/((PetscReal)ai[n]); 3311 CHKMEMQ; 3312 /* ------- end of symbolic factorization ---------*/ 3313 3314 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 3315 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 3316 ics = ic; 3317 3318 /* linked list for storing column indices of the active row */ 3319 nlnk = n + 1; 3320 ierr = PetscLLCreate(n,n,nlnk,lnk,lnkbt);CHKERRQ(ierr); 3321 3322 /* im: used by PetscLLAddSortedLU(); jtmp: working array for column indices of active row */ 3323 ierr = PetscMalloc2(n,PetscInt,&im,n,PetscInt,&jtmp);CHKERRQ(ierr); 3324 /* rtmp, vtmp: working arrays for sparse and contiguous row entries of active row */ 3325 ierr = PetscMalloc2(n,MatScalar,&rtmp,n,MatScalar,&vtmp);CHKERRQ(ierr); 3326 ierr = PetscMemzero(rtmp,n*sizeof(MatScalar));CHKERRQ(ierr); 3327 3328 bi[0] = 0; 3329 bdiag[0] = nnz_max-1; /* location of diag[0] in factor B */ 3330 bdiag_rev[n] = bdiag[0]; 3331 bi[2*n+1] = bdiag[0]+1; /* endof bj and ba array */ 3332 for (i=0; i<n; i++) { 3333 /* copy initial fill into linked list */ 3334 nzi = 0; /* nonzeros for active row i */ 3335 nzi = ai[r[i]+1] - ai[r[i]]; 3336 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); 3337 nzi_al = adiag[r[i]] - ai[r[i]]; 3338 nzi_au = ai[r[i]+1] - adiag[r[i]] -1; 3339 ajtmp = aj + ai[r[i]]; 3340 ierr = PetscLLAddPerm(nzi,ajtmp,ic,n,nlnk,lnk,lnkbt);CHKERRQ(ierr); 3341 3342 /* load in initial (unfactored row) */ 3343 aatmp = a->a + ai[r[i]]; 3344 for (j=0; j<nzi; j++) { 3345 rtmp[ics[*ajtmp++]] = *aatmp++; 3346 } 3347 3348 /* add pivot rows into linked list */ 3349 row = lnk[n]; 3350 while (row < i ) { 3351 nzi_bl = bi[row+1] - bi[row] + 1; 3352 bjtmp = bj + bdiag[row+1]+1; /* points to 1st column next to the diagonal in U */ 3353 ierr = PetscLLAddSortedLU(bjtmp,row,nlnk,lnk,lnkbt,i,nzi_bl,im);CHKERRQ(ierr); 3354 nzi += nlnk; 3355 row = lnk[row]; 3356 } 3357 3358 /* copy data from lnk into jtmp, then initialize lnk */ 3359 ierr = PetscLLClean(n,n,nzi,lnk,jtmp,lnkbt);CHKERRQ(ierr); 3360 3361 /* numerical factorization */ 3362 bjtmp = jtmp; 3363 row = *bjtmp++; /* 1st pivot row */ 3364 while ( row < i ) { 3365 pc = rtmp + row; 3366 pv = ba + bdiag[row]; /* 1./(diag of the pivot row) */ 3367 multiplier = (*pc) * (*pv); 3368 *pc = multiplier; 3369 if (PetscAbsScalar(*pc) > dt){ /* apply tolerance dropping rule */ 3370 pj = bj + bdiag[row+1] + 1; /* point to 1st entry of U(row,:) */ 3371 pv = ba + bdiag[row+1] + 1; 3372 /* if (multiplier < -1.0 or multiplier >1.0) printf("row/prow %d, %d, multiplier %g\n",i,row,multiplier); */ 3373 nz = bdiag[row] - bdiag[row+1] - 1; /* num of entries in U(row,:), excluding diagonal */ 3374 for (j=0; j<nz; j++) rtmp[*pj++] -= multiplier * (*pv++); 3375 ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr); 3376 } 3377 row = *bjtmp++; 3378 } 3379 3380 /* copy sparse rtmp into contiguous vtmp; separate L and U part */ 3381 diag_tmp = rtmp[i]; /* save diagonal value - may not needed?? */ 3382 nzi_bl = 0; j = 0; 3383 while (jtmp[j] < i){ /* Note: jtmp is sorted */ 3384 vtmp[j] = rtmp[jtmp[j]]; rtmp[jtmp[j]]=0.0; 3385 nzi_bl++; j++; 3386 } 3387 nzi_bu = nzi - nzi_bl -1; 3388 while (j < nzi){ 3389 vtmp[j] = rtmp[jtmp[j]]; rtmp[jtmp[j]]=0.0; 3390 j++; 3391 } 3392 3393 bjtmp = bj + bi[i]; 3394 batmp = ba + bi[i]; 3395 /* apply level dropping rule to L part */ 3396 ncut = nzi_al + dtcount; 3397 if (ncut < nzi_bl){ 3398 ierr = PetscSortSplit(ncut,nzi_bl,vtmp,jtmp);CHKERRQ(ierr); 3399 ierr = PetscSortIntWithScalarArray(ncut,jtmp,vtmp);CHKERRQ(ierr); 3400 } else { 3401 ncut = nzi_bl; 3402 } 3403 for (j=0; j<ncut; j++){ 3404 bjtmp[j] = jtmp[j]; 3405 batmp[j] = vtmp[j]; 3406 /* printf(" (%d,%g),",bjtmp[j],batmp[j]); */ 3407 } 3408 bi[i+1] = bi[i] + ncut; 3409 nzi = ncut + 1; 3410 3411 /* apply level dropping rule to U part */ 3412 ncut = nzi_au + dtcount; 3413 if (ncut < nzi_bu){ 3414 ierr = PetscSortSplit(ncut,nzi_bu,vtmp+nzi_bl+1,jtmp+nzi_bl+1);CHKERRQ(ierr); 3415 ierr = PetscSortIntWithScalarArray(ncut,jtmp+nzi_bl+1,vtmp+nzi_bl+1);CHKERRQ(ierr); 3416 } else { 3417 ncut = nzi_bu; 3418 } 3419 nzi += ncut; 3420 3421 /* mark bdiagonal */ 3422 bdiag[i+1] = bdiag[i] - (ncut + 1); 3423 bdiag_rev[n-i-1] = bdiag[i+1]; 3424 bi[2*n - i] = bi[2*n - i +1] - (ncut + 1); 3425 bjtmp = bj + bdiag[i]; 3426 batmp = ba + bdiag[i]; 3427 *bjtmp = i; 3428 *batmp = diag_tmp; /* rtmp[i]; */ 3429 if (*batmp == 0.0) { 3430 *batmp = dt+shift; 3431 /* printf(" row %d add shift %g\n",i,shift); */ 3432 } 3433 *batmp = 1.0/(*batmp); /* invert diagonal entries for simplier triangular solves */ 3434 /* printf(" (%d,%g),",*bjtmp,*batmp); */ 3435 3436 bjtmp = bj + bdiag[i+1]+1; 3437 batmp = ba + bdiag[i+1]+1; 3438 for (k=0; k<ncut; k++){ 3439 bjtmp[k] = jtmp[nzi_bl+1+k]; 3440 batmp[k] = vtmp[nzi_bl+1+k]; 3441 /* printf(" (%d,%g),",bjtmp[k],batmp[k]); */ 3442 } 3443 /* printf("\n"); */ 3444 3445 im[i] = nzi; /* used by PetscLLAddSortedLU() */ 3446 /* 3447 printf("row %d: bi %d, bdiag %d\n",i,bi[i],bdiag[i]); 3448 printf(" ----------------------------\n"); 3449 */ 3450 } /* for (i=0; i<n; i++) */ 3451 /* printf("end of L %d, beginning of U %d\n",bi[n],bdiag[n]); */ 3452 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]); 3453 3454 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 3455 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 3456 3457 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 3458 ierr = PetscFree2(im,jtmp);CHKERRQ(ierr); 3459 ierr = PetscFree2(rtmp,vtmp);CHKERRQ(ierr); 3460 ierr = PetscFree(bdiag_rev);CHKERRQ(ierr); 3461 3462 ierr = PetscLogFlops(B->cmap->n);CHKERRQ(ierr); 3463 b->maxnz = b->nz = bi[n] + bdiag[0] - bdiag[n]; 3464 3465 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 3466 ierr = ISIdentity(isicol,&icol_identity);CHKERRQ(ierr); 3467 both_identity = (PetscTruth) (row_identity && icol_identity); 3468 if (row_identity && icol_identity) { 3469 B->ops->solve = MatSolve_SeqAIJ_NaturalOrdering; 3470 } else { 3471 B->ops->solve = MatSolve_SeqAIJ; 3472 } 3473 3474 B->ops->solveadd = 0; 3475 B->ops->solvetranspose = 0; 3476 B->ops->solvetransposeadd = 0; 3477 B->ops->matsolve = 0; 3478 B->assembled = PETSC_TRUE; 3479 B->preallocated = PETSC_TRUE; 3480 PetscFunctionReturn(0); 3481 } 3482 3483 /* a wraper of MatILUDTFactor_SeqAIJ() */ 3484 #undef __FUNCT__ 3485 #define __FUNCT__ "MatILUDTFactorSymbolic_SeqAIJ" 3486 /* 3487 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 3488 */ 3489 3490 PetscErrorCode PETSCMAT_DLLEXPORT MatILUDTFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS row,IS col,const MatFactorInfo *info) 3491 { 3492 PetscErrorCode ierr; 3493 3494 PetscFunctionBegin; 3495 ierr = MatILUDTFactor_SeqAIJ(A,row,col,info,&fact);CHKERRQ(ierr); 3496 PetscFunctionReturn(0); 3497 } 3498 3499 /* 3500 same as MatLUFactorNumeric_SeqAIJ(), except using contiguous array matrix factors 3501 - intend to replace existing MatLUFactorNumeric_SeqAIJ() 3502 */ 3503 #undef __FUNCT__ 3504 #define __FUNCT__ "MatILUDTFactorNumeric_SeqAIJ" 3505 /* 3506 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 3507 */ 3508 3509 PetscErrorCode PETSCMAT_DLLEXPORT MatILUDTFactorNumeric_SeqAIJ(Mat fact,Mat A,const MatFactorInfo *info) 3510 { 3511 Mat C=fact; 3512 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ *)C->data; 3513 IS isrow = b->row,isicol = b->icol; 3514 PetscErrorCode ierr; 3515 const PetscInt *r,*ic,*ics; 3516 PetscInt i,j,k,n=A->rmap->n,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j; 3517 PetscInt *ajtmp,*bjtmp,nz,nzl,nzu,row,*bdiag = b->diag,*pj; 3518 MatScalar *rtmp,*pc,multiplier,*v,*pv,*aa=a->a; 3519 PetscReal dt=info->dt,shift=info->shiftamount; 3520 PetscTruth row_identity, col_identity; 3521 3522 PetscFunctionBegin; 3523 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 3524 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 3525 ierr = PetscMalloc((n+1)*sizeof(MatScalar),&rtmp);CHKERRQ(ierr); 3526 ics = ic; 3527 3528 for (i=0; i<n; i++){ 3529 /* initialize rtmp array */ 3530 nzl = bi[i+1] - bi[i]; /* num of nozeros in L(i,:) */ 3531 bjtmp = bj + bi[i]; 3532 for (j=0; j<nzl; j++) rtmp[*bjtmp++] = 0.0; 3533 rtmp[i] = 0.0; 3534 nzu = bdiag[i] - bdiag[i+1]; /* num of nozeros in U(i,:) */ 3535 bjtmp = bj + bdiag[i+1] + 1; 3536 for (j=0; j<nzu; j++) rtmp[*bjtmp++] = 0.0; 3537 3538 /* load in initial unfactored row of A */ 3539 /* printf("row %d\n",i); */ 3540 nz = ai[r[i]+1] - ai[r[i]]; 3541 ajtmp = aj + ai[r[i]]; 3542 v = aa + ai[r[i]]; 3543 for (j=0; j<nz; j++) { 3544 rtmp[ics[*ajtmp++]] = v[j]; 3545 /* printf(" (%d,%g),",ics[ajtmp[j]],rtmp[ics[ajtmp[j]]]); */ 3546 } 3547 /* printf("\n"); */ 3548 3549 /* numerical factorization */ 3550 bjtmp = bj + bi[i]; /* point to 1st entry of L(i,:) */ 3551 nzl = bi[i+1] - bi[i]; /* num of entries in L(i,:) */ 3552 k = 0; 3553 while (k < nzl){ 3554 row = *bjtmp++; 3555 /* printf(" prow %d\n",row); */ 3556 pc = rtmp + row; 3557 pv = b->a + bdiag[row]; /* 1./(diag of the pivot row) */ 3558 multiplier = (*pc) * (*pv); 3559 *pc = multiplier; 3560 if (PetscAbsScalar(multiplier) > dt){ 3561 pj = bj + bdiag[row+1] + 1; /* point to 1st entry of U(row,:) */ 3562 pv = b->a + bdiag[row+1] + 1; 3563 nz = bdiag[row] - bdiag[row+1] - 1; /* num of entries in U(row,:), excluding diagonal */ 3564 for (j=0; j<nz; j++) rtmp[*pj++] -= multiplier * (*pv++); 3565 /* ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr); */ 3566 } 3567 k++; 3568 } 3569 3570 /* finished row so stick it into b->a */ 3571 /* L-part */ 3572 pv = b->a + bi[i] ; 3573 pj = bj + bi[i] ; 3574 nzl = bi[i+1] - bi[i]; 3575 for (j=0; j<nzl; j++) { 3576 pv[j] = rtmp[pj[j]]; 3577 /* printf(" (%d,%g),",pj[j],pv[j]); */ 3578 } 3579 3580 /* diagonal: invert diagonal entries for simplier triangular solves */ 3581 if (rtmp[i] == 0.0) rtmp[i] = dt+shift; 3582 b->a[bdiag[i]] = 1.0/rtmp[i]; 3583 /* printf(" (%d,%g),",i,b->a[bdiag[i]]); */ 3584 3585 /* U-part */ 3586 pv = b->a + bdiag[i+1] + 1; 3587 pj = bj + bdiag[i+1] + 1; 3588 nzu = bdiag[i] - bdiag[i+1] - 1; 3589 for (j=0; j<nzu; j++) { 3590 pv[j] = rtmp[pj[j]]; 3591 /* printf(" (%d,%g),",pj[j],pv[j]); */ 3592 } 3593 /* printf("\n"); */ 3594 } 3595 3596 ierr = PetscFree(rtmp);CHKERRQ(ierr); 3597 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 3598 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 3599 3600 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 3601 ierr = ISIdentity(isicol,&col_identity);CHKERRQ(ierr); 3602 if (row_identity && col_identity) { 3603 C->ops->solve = MatSolve_SeqAIJ_NaturalOrdering; 3604 } else { 3605 C->ops->solve = MatSolve_SeqAIJ; 3606 } 3607 C->ops->solveadd = 0; 3608 C->ops->solvetranspose = 0; 3609 C->ops->solvetransposeadd = 0; 3610 C->ops->matsolve = 0; 3611 C->assembled = PETSC_TRUE; 3612 C->preallocated = PETSC_TRUE; 3613 ierr = PetscLogFlops(C->cmap->n);CHKERRQ(ierr); 3614 PetscFunctionReturn(0); 3615 } 3616