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