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