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; 608 } else { 609 C->ops->solve = MatSolve_SeqAIJ_newdatastruct; 610 } 611 612 C->ops->solveadd = MatSolveAdd_SeqAIJ_newdatastruct; 613 C->ops->solvetranspose = MatSolveTranspose_SeqAIJ_newdatastruct; 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__ "MatSolveAdd_SeqAIJ_newdatastruct" 1245 PetscErrorCode MatSolveAdd_SeqAIJ_newdatastruct(Mat A,Vec bb,Vec yy,Vec xx) 1246 { 1247 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1248 IS iscol = a->col,isrow = a->row; 1249 PetscErrorCode ierr; 1250 PetscInt i, n = A->rmap->n,j; 1251 PetscInt nz; 1252 const PetscInt *rout,*cout,*r,*c,*vi,*ai = a->i,*aj = a->j,*adiag = a->diag; 1253 PetscScalar *x,*tmp,sum; 1254 const PetscScalar *b; 1255 const MatScalar *aa = a->a,*v; 1256 1257 PetscFunctionBegin; 1258 if (yy != xx) {ierr = VecCopy(yy,xx);CHKERRQ(ierr);} 1259 1260 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1261 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1262 tmp = a->solve_work; 1263 1264 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 1265 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 1266 1267 /* forward solve the lower triangular */ 1268 tmp[0] = b[r[0]]; 1269 v = aa; 1270 vi = aj; 1271 for (i=1; i<n; i++) { 1272 nz = ai[i+1] - ai[i]; 1273 sum = b[r[i]]; 1274 for (j=0; j<nz; j++) sum -= v[j]*tmp[vi[j]]; 1275 tmp[i] = sum; 1276 v += nz; vi += nz; 1277 } 1278 1279 /* backward solve the upper triangular */ 1280 v = aa + adiag[n-1]; 1281 vi = aj + adiag[n-1]; 1282 for (i=n-1; i>=0; i--){ 1283 nz = adiag[i] - adiag[i+1] - 1; 1284 sum = tmp[i]; 1285 for (j=0; j<nz; j++) sum -= v[j]*tmp[vi[j]]; 1286 tmp[i] = sum*v[nz]; 1287 x[c[i]] += tmp[i]; 1288 v += nz+1; vi += nz+1; 1289 } 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 ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1296 1297 PetscFunctionReturn(0); 1298 } 1299 1300 #undef __FUNCT__ 1301 #define __FUNCT__ "MatSolveTranspose_SeqAIJ" 1302 PetscErrorCode MatSolveTranspose_SeqAIJ(Mat A,Vec bb,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 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1316 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1317 tmp = a->solve_work; 1318 1319 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 1320 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 1321 1322 /* copy the b into temp work space according to permutation */ 1323 for (i=0; i<n; i++) tmp[i] = b[c[i]]; 1324 1325 /* forward solve the U^T */ 1326 for (i=0; i<n; i++) { 1327 v = aa + diag[i] ; 1328 vi = aj + diag[i] + 1; 1329 nz = ai[i+1] - diag[i] - 1; 1330 s1 = tmp[i]; 1331 s1 *= (*v++); /* multiply by inverse of diagonal entry */ 1332 for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j]; 1333 tmp[i] = s1; 1334 } 1335 1336 /* backward solve the L^T */ 1337 for (i=n-1; i>=0; i--){ 1338 v = aa + diag[i] - 1 ; 1339 vi = aj + diag[i] - 1 ; 1340 nz = diag[i] - ai[i]; 1341 s1 = tmp[i]; 1342 for (j=0; j>-nz; j--) tmp[vi[j]] -= s1*v[j]; 1343 } 1344 1345 /* copy tmp into x according to permutation */ 1346 for (i=0; i<n; i++) x[r[i]] = tmp[i]; 1347 1348 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 1349 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1350 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1351 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1352 1353 ierr = PetscLogFlops(2.0*a->nz-A->cmap->n);CHKERRQ(ierr); 1354 PetscFunctionReturn(0); 1355 } 1356 1357 #undef __FUNCT__ 1358 #define __FUNCT__ "MatSolveTranspose_SeqAIJ_newdatastruct" 1359 PetscErrorCode MatSolveTranspose_SeqAIJ_newdatastruct(Mat A,Vec bb,Vec xx) 1360 { 1361 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1362 IS iscol = a->col,isrow = a->row; 1363 PetscErrorCode ierr; 1364 const PetscInt *rout,*cout,*r,*c,*adiag = a->diag,*ai = a->i,*aj = a->j,*vi; 1365 PetscInt i,n = A->rmap->n,j; 1366 PetscInt nz; 1367 PetscScalar *x,*tmp,s1; 1368 const MatScalar *aa = a->a,*v; 1369 const PetscScalar *b; 1370 1371 PetscFunctionBegin; 1372 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1373 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1374 tmp = a->solve_work; 1375 1376 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 1377 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 1378 1379 /* copy the b into temp work space according to permutation */ 1380 for (i=0; i<n; i++) tmp[i] = b[c[i]]; 1381 1382 /* forward solve the U^T */ 1383 for (i=0; i<n; i++) { 1384 v = aa + adiag[i+1] + 1; 1385 vi = aj + adiag[i+1] + 1; 1386 nz = adiag[i] - adiag[i+1] - 1; 1387 s1 = tmp[i]; 1388 s1 *= v[nz]; /* multiply by inverse of diagonal entry */ 1389 for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j]; 1390 tmp[i] = s1; 1391 } 1392 1393 /* backward solve the L^T */ 1394 for (i=n-1; i>=0; i--){ 1395 v = aa + ai[i]; 1396 vi = aj + ai[i]; 1397 nz = ai[i+1] - ai[i]; 1398 s1 = tmp[i]; 1399 for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j]; 1400 } 1401 1402 /* copy tmp into x according to permutation */ 1403 for (i=0; i<n; i++) x[r[i]] = tmp[i]; 1404 1405 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 1406 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1407 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1408 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1409 1410 ierr = PetscLogFlops(2.0*a->nz-A->cmap->n);CHKERRQ(ierr); 1411 PetscFunctionReturn(0); 1412 } 1413 1414 #undef __FUNCT__ 1415 #define __FUNCT__ "MatSolveTransposeAdd_SeqAIJ" 1416 PetscErrorCode MatSolveTransposeAdd_SeqAIJ(Mat A,Vec bb,Vec zz,Vec xx) 1417 { 1418 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1419 IS iscol = a->col,isrow = a->row; 1420 PetscErrorCode ierr; 1421 const PetscInt *rout,*cout,*r,*c,*diag = a->diag,*ai = a->i,*aj = a->j,*vi; 1422 PetscInt i,n = A->rmap->n,j; 1423 PetscInt nz; 1424 PetscScalar *x,*tmp,s1; 1425 const MatScalar *aa = a->a,*v; 1426 const PetscScalar *b; 1427 1428 PetscFunctionBegin; 1429 if (zz != xx) {ierr = VecCopy(zz,xx);CHKERRQ(ierr);} 1430 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1431 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1432 tmp = a->solve_work; 1433 1434 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 1435 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 1436 1437 /* copy the b into temp work space according to permutation */ 1438 for (i=0; i<n; i++) tmp[i] = b[c[i]]; 1439 1440 /* forward solve the U^T */ 1441 for (i=0; i<n; i++) { 1442 v = aa + diag[i] ; 1443 vi = aj + diag[i] + 1; 1444 nz = ai[i+1] - diag[i] - 1; 1445 s1 = tmp[i]; 1446 s1 *= (*v++); /* multiply by inverse of diagonal entry */ 1447 for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j]; 1448 tmp[i] = s1; 1449 } 1450 1451 /* backward solve the L^T */ 1452 for (i=n-1; i>=0; i--){ 1453 v = aa + diag[i] - 1 ; 1454 vi = aj + diag[i] - 1 ; 1455 nz = diag[i] - ai[i]; 1456 s1 = tmp[i]; 1457 for (j=0; j>-nz; j--) tmp[vi[j]] -= s1*v[j]; 1458 } 1459 1460 /* copy tmp into x according to permutation */ 1461 for (i=0; i<n; i++) x[r[i]] += tmp[i]; 1462 1463 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 1464 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 1465 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1466 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1467 1468 ierr = PetscLogFlops(2.0*a->nz-A->cmap->n);CHKERRQ(ierr); 1469 PetscFunctionReturn(0); 1470 } 1471 1472 /* ----------------------------------------------------------------*/ 1473 EXTERN PetscErrorCode Mat_CheckInode(Mat,PetscTruth); 1474 EXTERN PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat,Mat,MatDuplicateOption,PetscTruth); 1475 1476 /* 1477 ilu() under revised new data structure. 1478 Factored arrays bj and ba are stored as 1479 L(0,:), L(1,:), ...,L(n-1,:), U(n-1,:),...,U(i,:),U(i-1,:),...,U(0,:) 1480 1481 bi=fact->i is an array of size n+1, in which 1482 bi+ 1483 bi[i]: points to 1st entry of L(i,:),i=0,...,n-1 1484 bi[n]: points to L(n-1,n-1)+1 1485 1486 bdiag=fact->diag is an array of size n+1,in which 1487 bdiag[i]: points to diagonal of U(i,:), i=0,...,n-1 1488 bdiag[n]: points to entry of U(n-1,0)-1 1489 1490 U(i,:) contains bdiag[i] as its last entry, i.e., 1491 U(i,:) = (u[i,i+1],...,u[i,n-1],diag[i]) 1492 */ 1493 #undef __FUNCT__ 1494 #define __FUNCT__ "MatILUFactorSymbolic_SeqAIJ_ilu0_newdatastruct" 1495 PetscErrorCode MatILUFactorSymbolic_SeqAIJ_ilu0_newdatastruct(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 1496 { 1497 1498 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b; 1499 PetscErrorCode ierr; 1500 PetscInt n=A->rmap->n,*ai=a->i,*aj,*adiag=a->diag; 1501 PetscInt i,j,nz,*bi,*bj,*bdiag; 1502 PetscTruth missing; 1503 IS isicol; 1504 1505 PetscFunctionBegin; 1506 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); 1507 ierr = MatMissingDiagonal(A,&missing,&i);CHKERRQ(ierr); 1508 if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",i); 1509 ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 1510 1511 ierr = MatDuplicateNoCreate_SeqAIJ(fact,A,MAT_DO_NOT_COPY_VALUES,PETSC_FALSE);CHKERRQ(ierr); 1512 b = (Mat_SeqAIJ*)(fact)->data; 1513 1514 /* allocate matrix arrays for new data structure */ 1515 ierr = PetscMalloc3(ai[n]+1,PetscScalar,&b->a,ai[n]+1,PetscInt,&b->j,n+1,PetscInt,&b->i);CHKERRQ(ierr); 1516 ierr = PetscLogObjectMemory(fact,ai[n]*(sizeof(PetscScalar)+sizeof(PetscInt))+(n+1)*sizeof(PetscInt));CHKERRQ(ierr); 1517 b->singlemalloc = PETSC_TRUE; 1518 if (!b->diag){ 1519 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&b->diag);CHKERRQ(ierr); 1520 ierr = PetscLogObjectMemory(fact,(n+1)*sizeof(PetscInt));CHKERRQ(ierr); 1521 } 1522 bdiag = b->diag; 1523 1524 if (n > 0) { 1525 ierr = PetscMemzero(b->a,(ai[n])*sizeof(MatScalar));CHKERRQ(ierr); 1526 } 1527 1528 /* set bi and bj with new data structure */ 1529 bi = b->i; 1530 bj = b->j; 1531 1532 /* L part */ 1533 bi[0] = 0; 1534 for (i=0; i<n; i++){ 1535 nz = adiag[i] - ai[i]; 1536 bi[i+1] = bi[i] + nz; 1537 aj = a->j + ai[i]; 1538 for (j=0; j<nz; j++){ 1539 *bj = aj[j]; bj++; 1540 } 1541 } 1542 1543 /* U part */ 1544 bdiag[n] = bi[n]-1; 1545 for (i=n-1; i>=0; i--){ 1546 nz = ai[i+1] - adiag[i] - 1; 1547 aj = a->j + adiag[i] + 1; 1548 for (j=0; j<nz; j++){ 1549 *bj = aj[j]; bj++; 1550 } 1551 /* diag[i] */ 1552 *bj = i; bj++; 1553 bdiag[i] = bdiag[i+1] + nz + 1; 1554 } 1555 1556 fact->factor = MAT_FACTOR_ILU; 1557 fact->info.factor_mallocs = 0; 1558 fact->info.fill_ratio_given = info->fill; 1559 fact->info.fill_ratio_needed = 1.0; 1560 fact->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ_newdatastruct; 1561 1562 b = (Mat_SeqAIJ*)(fact)->data; 1563 b->row = isrow; 1564 b->col = iscol; 1565 b->icol = isicol; 1566 ierr = PetscMalloc((fact->rmap->n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 1567 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 1568 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 1569 PetscFunctionReturn(0); 1570 } 1571 1572 #undef __FUNCT__ 1573 #define __FUNCT__ "MatILUFactorSymbolic_SeqAIJ_newdatastruct" 1574 PetscErrorCode MatILUFactorSymbolic_SeqAIJ_newdatastruct(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 1575 { 1576 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b; 1577 IS isicol; 1578 PetscErrorCode ierr; 1579 const PetscInt *r,*ic; 1580 PetscInt n=A->rmap->n,*ai=a->i,*aj=a->j; 1581 PetscInt *bi,*cols,nnz,*cols_lvl; 1582 PetscInt *bdiag,prow,fm,nzbd,reallocs=0,dcount=0; 1583 PetscInt i,levels,diagonal_fill; 1584 PetscTruth col_identity,row_identity; 1585 PetscReal f; 1586 PetscInt nlnk,*lnk,*lnk_lvl=PETSC_NULL; 1587 PetscBT lnkbt; 1588 PetscInt nzi,*bj,**bj_ptr,**bjlvl_ptr; 1589 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 1590 PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL; 1591 1592 PetscFunctionBegin; 1593 /* printf("MatILUFactorSymbolic_SeqAIJ_newdatastruct ...\n"); */ 1594 levels = (PetscInt)info->levels; 1595 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 1596 ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr); 1597 1598 if (!levels && row_identity && col_identity) { 1599 /* special case: ilu(0) with natural ordering */ 1600 ierr = MatILUFactorSymbolic_SeqAIJ_ilu0_newdatastruct(fact,A,isrow,iscol,info);CHKERRQ(ierr); 1601 PetscFunctionReturn(0); 1602 } 1603 1604 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); 1605 ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 1606 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 1607 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 1608 1609 /* get new row and diagonal pointers, must be allocated separately because they will be given to the Mat_SeqAIJ and freed separately */ 1610 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 1611 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr); 1612 bi[0] = bdiag[0] = 0; 1613 1614 ierr = PetscMalloc2(n,PetscInt*,&bj_ptr,n,PetscInt*,&bjlvl_ptr);CHKERRQ(ierr); 1615 1616 /* create a linked list for storing column indices of the active row */ 1617 nlnk = n + 1; 1618 ierr = PetscIncompleteLLCreate(n,n,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 1619 1620 /* initial FreeSpace size is f*(ai[n]+1) */ 1621 f = info->fill; 1622 diagonal_fill = (PetscInt)info->diagonal_fill; 1623 ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space);CHKERRQ(ierr); 1624 current_space = free_space; 1625 ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space_lvl);CHKERRQ(ierr); 1626 current_space_lvl = free_space_lvl; 1627 1628 for (i=0; i<n; i++) { 1629 nzi = 0; 1630 /* copy current row into linked list */ 1631 nnz = ai[r[i]+1] - ai[r[i]]; 1632 if (!nnz) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i); 1633 cols = aj + ai[r[i]]; 1634 lnk[i] = -1; /* marker to indicate if diagonal exists */ 1635 ierr = PetscIncompleteLLInit(nnz,cols,n,ic,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 1636 nzi += nlnk; 1637 1638 /* make sure diagonal entry is included */ 1639 if (diagonal_fill && lnk[i] == -1) { 1640 fm = n; 1641 while (lnk[fm] < i) fm = lnk[fm]; 1642 lnk[i] = lnk[fm]; /* insert diagonal into linked list */ 1643 lnk[fm] = i; 1644 lnk_lvl[i] = 0; 1645 nzi++; dcount++; 1646 } 1647 1648 /* add pivot rows into the active row */ 1649 nzbd = 0; 1650 prow = lnk[n]; 1651 while (prow < i) { 1652 nnz = bdiag[prow]; 1653 cols = bj_ptr[prow] + nnz + 1; 1654 cols_lvl = bjlvl_ptr[prow] + nnz + 1; 1655 nnz = bi[prow+1] - bi[prow] - nnz - 1; 1656 ierr = PetscILULLAddSorted(nnz,cols,levels,cols_lvl,prow,nlnk,lnk,lnk_lvl,lnkbt,prow);CHKERRQ(ierr); 1657 nzi += nlnk; 1658 prow = lnk[prow]; 1659 nzbd++; 1660 } 1661 bdiag[i] = nzbd; 1662 bi[i+1] = bi[i] + nzi; 1663 1664 /* if free space is not available, make more free space */ 1665 if (current_space->local_remaining<nzi) { 1666 nnz = 2*nzi*(n - i); /* estimated and max additional space needed */ 1667 ierr = PetscFreeSpaceGet(nnz,¤t_space);CHKERRQ(ierr); 1668 ierr = PetscFreeSpaceGet(nnz,¤t_space_lvl);CHKERRQ(ierr); 1669 reallocs++; 1670 } 1671 1672 /* copy data into free_space and free_space_lvl, then initialize lnk */ 1673 ierr = PetscIncompleteLLClean(n,n,nzi,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr); 1674 bj_ptr[i] = current_space->array; 1675 bjlvl_ptr[i] = current_space_lvl->array; 1676 1677 /* make sure the active row i has diagonal entry */ 1678 if (*(bj_ptr[i]+bdiag[i]) != i) { 1679 SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,"Row %D has missing diagonal in factored matrix\n\ 1680 try running with -pc_factor_nonzeros_along_diagonal or -pc_factor_diagonal_fill",i); 1681 } 1682 1683 current_space->array += nzi; 1684 current_space->local_used += nzi; 1685 current_space->local_remaining -= nzi; 1686 current_space_lvl->array += nzi; 1687 current_space_lvl->local_used += nzi; 1688 current_space_lvl->local_remaining -= nzi; 1689 } 1690 1691 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 1692 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 1693 1694 /* destroy list of free space and other temporary arrays */ 1695 ierr = PetscMalloc((bi[n]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 1696 1697 /* copy free_space into bj and free free_space; set bi, bj, bdiag in new datastructure; */ 1698 ierr = PetscFreeSpaceContiguous_LU_v2(&free_space,bj,n,bi,bdiag);CHKERRQ(ierr); 1699 1700 ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 1701 ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr); 1702 ierr = PetscFree2(bj_ptr,bjlvl_ptr);CHKERRQ(ierr); 1703 1704 #if defined(PETSC_USE_INFO) 1705 { 1706 PetscReal af = ((PetscReal)bi[n])/((PetscReal)ai[n]); 1707 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,f,af);CHKERRQ(ierr); 1708 ierr = PetscInfo1(A,"Run with -[sub_]pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 1709 ierr = PetscInfo1(A,"PCFactorSetFill([sub]pc,%G);\n",af);CHKERRQ(ierr); 1710 ierr = PetscInfo(A,"for best performance.\n");CHKERRQ(ierr); 1711 if (diagonal_fill) { 1712 ierr = PetscInfo1(A,"Detected and replaced %D missing diagonals",dcount);CHKERRQ(ierr); 1713 } 1714 } 1715 #endif 1716 1717 /* put together the new matrix */ 1718 ierr = MatSeqAIJSetPreallocation_SeqAIJ(fact,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr); 1719 ierr = PetscLogObjectParent(fact,isicol);CHKERRQ(ierr); 1720 b = (Mat_SeqAIJ*)(fact)->data; 1721 b->free_a = PETSC_TRUE; 1722 b->free_ij = PETSC_TRUE; 1723 b->singlemalloc = PETSC_FALSE; 1724 ierr = PetscMalloc((bdiag[0]+1)*sizeof(PetscScalar),&b->a);CHKERRQ(ierr); 1725 b->j = bj; 1726 b->i = bi; 1727 b->diag = bdiag; 1728 b->ilen = 0; 1729 b->imax = 0; 1730 b->row = isrow; 1731 b->col = iscol; 1732 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 1733 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 1734 b->icol = isicol; 1735 ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 1736 /* In b structure: Free imax, ilen, old a, old j. 1737 Allocate bdiag, solve_work, new a, new j */ 1738 ierr = PetscLogObjectMemory(fact,(bdiag[0]+1)*(sizeof(PetscInt)+sizeof(PetscScalar)));CHKERRQ(ierr); 1739 b->maxnz = b->nz = bdiag[0]+1; 1740 (fact)->info.factor_mallocs = reallocs; 1741 (fact)->info.fill_ratio_given = f; 1742 (fact)->info.fill_ratio_needed = ((PetscReal)(bdiag[0]+1))/((PetscReal)ai[n]); 1743 (fact)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ_newdatastruct; 1744 /* ierr = MatILUFactorSymbolic_SeqAIJ_Inode(fact,A,isrow,iscol,info);CHKERRQ(ierr); */ 1745 PetscFunctionReturn(0); 1746 } 1747 1748 #undef __FUNCT__ 1749 #define __FUNCT__ "MatILUFactorSymbolic_SeqAIJ" 1750 PetscErrorCode MatILUFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info) 1751 { 1752 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b; 1753 IS isicol; 1754 PetscErrorCode ierr; 1755 const PetscInt *r,*ic; 1756 PetscInt n=A->rmap->n,*ai=a->i,*aj=a->j,d; 1757 PetscInt *bi,*cols,nnz,*cols_lvl; 1758 PetscInt *bdiag,prow,fm,nzbd,reallocs=0,dcount=0; 1759 PetscInt i,levels,diagonal_fill; 1760 PetscTruth col_identity,row_identity; 1761 PetscReal f; 1762 PetscInt nlnk,*lnk,*lnk_lvl=PETSC_NULL; 1763 PetscBT lnkbt; 1764 PetscInt nzi,*bj,**bj_ptr,**bjlvl_ptr; 1765 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 1766 PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL; 1767 PetscTruth missing; 1768 PetscTruth newdatastruct=PETSC_FALSE; 1769 1770 PetscFunctionBegin; 1771 ierr = PetscOptionsGetTruth(PETSC_NULL,"-ilu_new",&newdatastruct,PETSC_NULL);CHKERRQ(ierr); 1772 if(newdatastruct){ 1773 ierr = MatILUFactorSymbolic_SeqAIJ_newdatastruct(fact,A,isrow,iscol,info);CHKERRQ(ierr); 1774 PetscFunctionReturn(0); 1775 } 1776 1777 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); 1778 f = info->fill; 1779 levels = (PetscInt)info->levels; 1780 diagonal_fill = (PetscInt)info->diagonal_fill; 1781 ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 1782 1783 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 1784 ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr); 1785 if (!levels && row_identity && col_identity) { /* special case: ilu(0) with natural ordering */ 1786 ierr = MatDuplicateNoCreate_SeqAIJ(fact,A,MAT_DO_NOT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr); 1787 (fact)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ; 1788 1789 fact->factor = MAT_FACTOR_ILU; 1790 (fact)->info.factor_mallocs = 0; 1791 (fact)->info.fill_ratio_given = info->fill; 1792 (fact)->info.fill_ratio_needed = 1.0; 1793 b = (Mat_SeqAIJ*)(fact)->data; 1794 ierr = MatMissingDiagonal(A,&missing,&d);CHKERRQ(ierr); 1795 if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d); 1796 b->row = isrow; 1797 b->col = iscol; 1798 b->icol = isicol; 1799 ierr = PetscMalloc(((fact)->rmap->n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 1800 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 1801 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 1802 ierr = MatILUFactorSymbolic_SeqAIJ_Inode(fact,A,isrow,iscol,info);CHKERRQ(ierr); 1803 PetscFunctionReturn(0); 1804 } 1805 1806 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 1807 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 1808 1809 /* get new row and diagonal pointers, must be allocated separately because they will be given to the Mat_SeqAIJ and freed separately */ 1810 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 1811 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr); 1812 bi[0] = bdiag[0] = 0; 1813 1814 ierr = PetscMalloc2(n,PetscInt*,&bj_ptr,n,PetscInt*,&bjlvl_ptr);CHKERRQ(ierr); 1815 1816 /* create a linked list for storing column indices of the active row */ 1817 nlnk = n + 1; 1818 ierr = PetscIncompleteLLCreate(n,n,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 1819 1820 /* initial FreeSpace size is f*(ai[n]+1) */ 1821 ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space);CHKERRQ(ierr); 1822 current_space = free_space; 1823 ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space_lvl);CHKERRQ(ierr); 1824 current_space_lvl = free_space_lvl; 1825 1826 for (i=0; i<n; i++) { 1827 nzi = 0; 1828 /* copy current row into linked list */ 1829 nnz = ai[r[i]+1] - ai[r[i]]; 1830 if (!nnz) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i); 1831 cols = aj + ai[r[i]]; 1832 lnk[i] = -1; /* marker to indicate if diagonal exists */ 1833 ierr = PetscIncompleteLLInit(nnz,cols,n,ic,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 1834 nzi += nlnk; 1835 1836 /* make sure diagonal entry is included */ 1837 if (diagonal_fill && lnk[i] == -1) { 1838 fm = n; 1839 while (lnk[fm] < i) fm = lnk[fm]; 1840 lnk[i] = lnk[fm]; /* insert diagonal into linked list */ 1841 lnk[fm] = i; 1842 lnk_lvl[i] = 0; 1843 nzi++; dcount++; 1844 } 1845 1846 /* add pivot rows into the active row */ 1847 nzbd = 0; 1848 prow = lnk[n]; 1849 while (prow < i) { 1850 nnz = bdiag[prow]; 1851 cols = bj_ptr[prow] + nnz + 1; 1852 cols_lvl = bjlvl_ptr[prow] + nnz + 1; 1853 nnz = bi[prow+1] - bi[prow] - nnz - 1; 1854 ierr = PetscILULLAddSorted(nnz,cols,levels,cols_lvl,prow,nlnk,lnk,lnk_lvl,lnkbt,prow);CHKERRQ(ierr); 1855 nzi += nlnk; 1856 prow = lnk[prow]; 1857 nzbd++; 1858 } 1859 bdiag[i] = nzbd; 1860 bi[i+1] = bi[i] + nzi; 1861 1862 /* if free space is not available, make more free space */ 1863 if (current_space->local_remaining<nzi) { 1864 nnz = nzi*(n - i); /* estimated and max additional space needed */ 1865 ierr = PetscFreeSpaceGet(nnz,¤t_space);CHKERRQ(ierr); 1866 ierr = PetscFreeSpaceGet(nnz,¤t_space_lvl);CHKERRQ(ierr); 1867 reallocs++; 1868 } 1869 1870 /* copy data into free_space and free_space_lvl, then initialize lnk */ 1871 ierr = PetscIncompleteLLClean(n,n,nzi,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr); 1872 bj_ptr[i] = current_space->array; 1873 bjlvl_ptr[i] = current_space_lvl->array; 1874 1875 /* make sure the active row i has diagonal entry */ 1876 if (*(bj_ptr[i]+bdiag[i]) != i) { 1877 SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,"Row %D has missing diagonal in factored matrix\n\ 1878 try running with -pc_factor_nonzeros_along_diagonal or -pc_factor_diagonal_fill",i); 1879 } 1880 1881 current_space->array += nzi; 1882 current_space->local_used += nzi; 1883 current_space->local_remaining -= nzi; 1884 current_space_lvl->array += nzi; 1885 current_space_lvl->local_used += nzi; 1886 current_space_lvl->local_remaining -= nzi; 1887 } 1888 1889 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 1890 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 1891 1892 /* destroy list of free space and other temporary arrays */ 1893 ierr = PetscMalloc((bi[n]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 1894 ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr); /* copy free_space -> bj */ 1895 ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 1896 ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr); 1897 ierr = PetscFree2(bj_ptr,bjlvl_ptr);CHKERRQ(ierr); 1898 1899 #if defined(PETSC_USE_INFO) 1900 { 1901 PetscReal af = ((PetscReal)bi[n])/((PetscReal)ai[n]); 1902 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,f,af);CHKERRQ(ierr); 1903 ierr = PetscInfo1(A,"Run with -[sub_]pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 1904 ierr = PetscInfo1(A,"PCFactorSetFill([sub]pc,%G);\n",af);CHKERRQ(ierr); 1905 ierr = PetscInfo(A,"for best performance.\n");CHKERRQ(ierr); 1906 if (diagonal_fill) { 1907 ierr = PetscInfo1(A,"Detected and replaced %D missing diagonals",dcount);CHKERRQ(ierr); 1908 } 1909 } 1910 #endif 1911 1912 /* put together the new matrix */ 1913 ierr = MatSeqAIJSetPreallocation_SeqAIJ(fact,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr); 1914 ierr = PetscLogObjectParent(fact,isicol);CHKERRQ(ierr); 1915 b = (Mat_SeqAIJ*)(fact)->data; 1916 b->free_a = PETSC_TRUE; 1917 b->free_ij = PETSC_TRUE; 1918 b->singlemalloc = PETSC_FALSE; 1919 ierr = PetscMalloc(bi[n]*sizeof(PetscScalar),&b->a);CHKERRQ(ierr); 1920 b->j = bj; 1921 b->i = bi; 1922 for (i=0; i<n; i++) bdiag[i] += bi[i]; 1923 b->diag = bdiag; 1924 b->ilen = 0; 1925 b->imax = 0; 1926 b->row = isrow; 1927 b->col = iscol; 1928 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 1929 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 1930 b->icol = isicol; 1931 ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 1932 /* In b structure: Free imax, ilen, old a, old j. 1933 Allocate bdiag, solve_work, new a, new j */ 1934 ierr = PetscLogObjectMemory(fact,(bi[n]-n) * (sizeof(PetscInt)+sizeof(PetscScalar)));CHKERRQ(ierr); 1935 b->maxnz = b->nz = bi[n] ; 1936 (fact)->info.factor_mallocs = reallocs; 1937 (fact)->info.fill_ratio_given = f; 1938 (fact)->info.fill_ratio_needed = ((PetscReal)bi[n])/((PetscReal)ai[n]); 1939 (fact)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ; 1940 ierr = MatILUFactorSymbolic_SeqAIJ_Inode(fact,A,isrow,iscol,info);CHKERRQ(ierr); 1941 PetscFunctionReturn(0); 1942 } 1943 1944 #undef __FUNCT__ 1945 #define __FUNCT__ "MatCholeskyFactorNumeric_SeqAIJ_newdatastruct" 1946 PetscErrorCode MatCholeskyFactorNumeric_SeqAIJ_newdatastruct(Mat B,Mat A,const MatFactorInfo *info) 1947 { 1948 Mat C = B; 1949 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data; 1950 Mat_SeqSBAIJ *b=(Mat_SeqSBAIJ*)C->data; 1951 IS ip=b->row,iip = b->icol; 1952 PetscErrorCode ierr; 1953 const PetscInt *rip,*riip; 1954 PetscInt i,j,mbs=A->rmap->n,*bi=b->i,*bj=b->j,*bdiag=b->diag,*bjtmp; 1955 PetscInt *ai=a->i,*aj=a->j; 1956 PetscInt k,jmin,jmax,*c2r,*il,col,nexti,ili,nz; 1957 MatScalar *rtmp,*ba=b->a,*bval,*aa=a->a,dk,uikdi; 1958 PetscTruth perm_identity; 1959 1960 LUShift_Ctx sctx; 1961 PetscInt newshift; 1962 PetscReal rs; 1963 MatScalar d,*v; 1964 1965 PetscFunctionBegin; 1966 /* MatPivotSetUp(): initialize shift context sctx */ 1967 ierr = PetscMemzero(&sctx,sizeof(LUShift_Ctx));CHKERRQ(ierr); 1968 1969 /* if both shift schemes are chosen by user, only use info->shiftpd */ 1970 if (info->shiftpd) { /* set sctx.shift_top=max{rs} */ 1971 sctx.shift_top = info->zeropivot; 1972 for (i=0; i<mbs; i++) { 1973 /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */ 1974 d = (aa)[a->diag[i]]; 1975 rs = -PetscAbsScalar(d) - PetscRealPart(d); 1976 v = aa+ai[i]; 1977 nz = ai[i+1] - ai[i]; 1978 for (j=0; j<nz; j++) 1979 rs += PetscAbsScalar(v[j]); 1980 if (rs>sctx.shift_top) sctx.shift_top = rs; 1981 } 1982 sctx.shift_top *= 1.1; 1983 sctx.nshift_max = 5; 1984 sctx.shift_lo = 0.; 1985 sctx.shift_hi = 1.; 1986 } 1987 1988 ierr = ISGetIndices(ip,&rip);CHKERRQ(ierr); 1989 ierr = ISGetIndices(iip,&riip);CHKERRQ(ierr); 1990 1991 /* allocate working arrays 1992 c2r: linked list, keep track of pivot rows for a given column. c2r[col]: head of the list for a given col 1993 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 1994 */ 1995 ierr = PetscMalloc3(mbs,MatScalar,&rtmp,mbs,PetscInt,&il,mbs,PetscInt,&c2r);CHKERRQ(ierr); 1996 1997 do { 1998 sctx.lushift = PETSC_FALSE; 1999 2000 for (i=0; i<mbs; i++) c2r[i] = mbs; 2001 il[0] = 0; 2002 2003 for (k = 0; k<mbs; k++){ 2004 /* zero rtmp */ 2005 nz = bi[k+1] - bi[k]; 2006 bjtmp = bj + bi[k]; 2007 for (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0; 2008 2009 /* load in initial unfactored row */ 2010 bval = ba + bi[k]; 2011 jmin = ai[rip[k]]; jmax = ai[rip[k]+1]; 2012 for (j = jmin; j < jmax; j++){ 2013 col = riip[aj[j]]; 2014 if (col >= k){ /* only take upper triangular entry */ 2015 rtmp[col] = aa[j]; 2016 *bval++ = 0.0; /* for in-place factorization */ 2017 } 2018 } 2019 /* shift the diagonal of the matrix: ZeropivotApply() */ 2020 rtmp[k] += sctx.shift_amount; /* shift the diagonal of the matrix */ 2021 2022 /* modify k-th row by adding in those rows i with U(i,k)!=0 */ 2023 dk = rtmp[k]; 2024 i = c2r[k]; /* first row to be added to k_th row */ 2025 2026 while (i < k){ 2027 nexti = c2r[i]; /* next row to be added to k_th row */ 2028 2029 /* compute multiplier, update diag(k) and U(i,k) */ 2030 ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */ 2031 uikdi = - ba[ili]*ba[bdiag[i]]; /* diagonal(k) */ 2032 dk += uikdi*ba[ili]; /* update diag[k] */ 2033 ba[ili] = uikdi; /* -U(i,k) */ 2034 2035 /* add multiple of row i to k-th row */ 2036 jmin = ili + 1; jmax = bi[i+1]; 2037 if (jmin < jmax){ 2038 for (j=jmin; j<jmax; j++) rtmp[bj[j]] += uikdi*ba[j]; 2039 /* update il and c2r for row i */ 2040 il[i] = jmin; 2041 j = bj[jmin]; c2r[i] = c2r[j]; c2r[j] = i; 2042 } 2043 i = nexti; 2044 } 2045 2046 /* copy data into U(k,:) */ 2047 rs = 0.0; 2048 jmin = bi[k]; jmax = bi[k+1]-1; 2049 if (jmin < jmax) { 2050 for (j=jmin; j<jmax; j++){ 2051 col = bj[j]; ba[j] = rtmp[col]; rs += PetscAbsScalar(ba[j]); 2052 } 2053 /* add the k-th row into il and c2r */ 2054 il[k] = jmin; 2055 i = bj[jmin]; c2r[k] = c2r[i]; c2r[i] = k; 2056 } 2057 2058 /* MatPivotCheck() */ 2059 sctx.rs = rs; 2060 sctx.pv = dk; 2061 if (info->shiftnz){ 2062 ierr = MatPivotCheck_nz(info,sctx,k,newshift);CHKERRQ(ierr); 2063 } else if (info->shiftpd){ 2064 ierr = MatPivotCheck_pd(info,sctx,k,newshift);CHKERRQ(ierr); 2065 } else if (info->shiftinblocks){ 2066 ierr = MatPivotCheck_inblocks(info,sctx,k,newshift);CHKERRQ(ierr); 2067 } else { 2068 ierr = MatPivotCheck_none(info,sctx,k,newshift);CHKERRQ(ierr); 2069 } 2070 dk = sctx.pv; 2071 if (newshift == 1) break; 2072 2073 ba[bdiag[k]] = 1.0/dk; /* U(k,k) */ 2074 } 2075 } while (sctx.lushift); 2076 2077 ierr = PetscFree3(rtmp,il,c2r);CHKERRQ(ierr); 2078 ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr); 2079 ierr = ISRestoreIndices(iip,&riip);CHKERRQ(ierr); 2080 2081 ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr); 2082 if (perm_identity){ 2083 (B)->ops->solve = MatSolve_SeqSBAIJ_1_NaturalOrdering_newdatastruct; 2084 (B)->ops->solvetranspose = 0; 2085 (B)->ops->forwardsolve = 0; 2086 (B)->ops->backwardsolve = 0; 2087 } else { 2088 (B)->ops->solve = MatSolve_SeqSBAIJ_1_newdatastruct; 2089 (B)->ops->solvetranspose = 0; 2090 (B)->ops->forwardsolve = 0; 2091 (B)->ops->backwardsolve = 0; 2092 } 2093 2094 C->assembled = PETSC_TRUE; 2095 C->preallocated = PETSC_TRUE; 2096 ierr = PetscLogFlops(C->rmap->n);CHKERRQ(ierr); 2097 2098 /* MatPivotView() */ 2099 if (sctx.nshift){ 2100 if (info->shiftpd) { 2101 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); 2102 } else if (info->shiftnz) { 2103 ierr = PetscInfo2(A,"number of shift_nz tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr); 2104 } else if (info->shiftinblocks){ 2105 ierr = PetscInfo2(A,"number of shift_inblocks applied %D, each shift_amount %G\n",sctx.nshift,info->shiftinblocks);CHKERRQ(ierr); 2106 } 2107 } 2108 PetscFunctionReturn(0); 2109 } 2110 2111 #undef __FUNCT__ 2112 #define __FUNCT__ "MatCholeskyFactorNumeric_SeqAIJ" 2113 PetscErrorCode MatCholeskyFactorNumeric_SeqAIJ(Mat B,Mat A,const MatFactorInfo *info) 2114 { 2115 Mat C = B; 2116 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data; 2117 Mat_SeqSBAIJ *b=(Mat_SeqSBAIJ*)C->data; 2118 IS ip=b->row,iip = b->icol; 2119 PetscErrorCode ierr; 2120 const PetscInt *rip,*riip; 2121 PetscInt i,j,mbs=A->rmap->n,*bi=b->i,*bj=b->j,*bcol,*bjtmp; 2122 PetscInt *ai=a->i,*aj=a->j; 2123 PetscInt k,jmin,jmax,*jl,*il,col,nexti,ili,nz; 2124 MatScalar *rtmp,*ba=b->a,*bval,*aa=a->a,dk,uikdi; 2125 PetscReal zeropivot,rs,shiftnz; 2126 PetscReal shiftpd; 2127 ChShift_Ctx sctx; 2128 PetscInt newshift; 2129 PetscTruth perm_identity; 2130 2131 PetscFunctionBegin; 2132 shiftnz = info->shiftnz; 2133 shiftpd = info->shiftpd; 2134 zeropivot = info->zeropivot; 2135 2136 ierr = ISGetIndices(ip,&rip);CHKERRQ(ierr); 2137 ierr = ISGetIndices(iip,&riip);CHKERRQ(ierr); 2138 2139 /* initialization */ 2140 ierr = PetscMalloc3(mbs,MatScalar,&rtmp,mbs,PetscInt,&il,mbs,PetscInt,&jl);CHKERRQ(ierr); 2141 sctx.shift_amount = 0; 2142 sctx.nshift = 0; 2143 do { 2144 sctx.chshift = PETSC_FALSE; 2145 for (i=0; i<mbs; i++) jl[i] = mbs; 2146 il[0] = 0; 2147 2148 for (k = 0; k<mbs; k++){ 2149 /* zero rtmp */ 2150 nz = bi[k+1] - bi[k]; 2151 bjtmp = bj + bi[k]; 2152 for (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0; 2153 2154 bval = ba + bi[k]; 2155 /* initialize k-th row by the perm[k]-th row of A */ 2156 jmin = ai[rip[k]]; jmax = ai[rip[k]+1]; 2157 for (j = jmin; j < jmax; j++){ 2158 col = riip[aj[j]]; 2159 if (col >= k){ /* only take upper triangular entry */ 2160 rtmp[col] = aa[j]; 2161 *bval++ = 0.0; /* for in-place factorization */ 2162 } 2163 } 2164 /* shift the diagonal of the matrix */ 2165 if (sctx.nshift) rtmp[k] += sctx.shift_amount; 2166 2167 /* modify k-th row by adding in those rows i with U(i,k)!=0 */ 2168 dk = rtmp[k]; 2169 i = jl[k]; /* first row to be added to k_th row */ 2170 2171 while (i < k){ 2172 nexti = jl[i]; /* next row to be added to k_th row */ 2173 2174 /* compute multiplier, update diag(k) and U(i,k) */ 2175 ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */ 2176 uikdi = - ba[ili]*ba[bi[i]]; /* diagonal(k) */ 2177 dk += uikdi*ba[ili]; 2178 ba[ili] = uikdi; /* -U(i,k) */ 2179 2180 /* add multiple of row i to k-th row */ 2181 jmin = ili + 1; jmax = bi[i+1]; 2182 if (jmin < jmax){ 2183 for (j=jmin; j<jmax; j++) rtmp[bj[j]] += uikdi*ba[j]; 2184 /* update il and jl for row i */ 2185 il[i] = jmin; 2186 j = bj[jmin]; jl[i] = jl[j]; jl[j] = i; 2187 } 2188 i = nexti; 2189 } 2190 2191 /* shift the diagonals when zero pivot is detected */ 2192 /* compute rs=sum of abs(off-diagonal) */ 2193 rs = 0.0; 2194 jmin = bi[k]+1; 2195 nz = bi[k+1] - jmin; 2196 bcol = bj + jmin; 2197 for (j=0; j<nz; j++) { 2198 rs += PetscAbsScalar(rtmp[bcol[j]]); 2199 } 2200 2201 sctx.rs = rs; 2202 sctx.pv = dk; 2203 ierr = MatCholeskyCheckShift_inline(info,sctx,k,newshift);CHKERRQ(ierr); 2204 2205 if (newshift == 1) { 2206 if (!sctx.shift_amount) { 2207 sctx.shift_amount = 1e-5; 2208 } 2209 break; 2210 } 2211 2212 /* copy data into U(k,:) */ 2213 ba[bi[k]] = 1.0/dk; /* U(k,k) */ 2214 jmin = bi[k]+1; jmax = bi[k+1]; 2215 if (jmin < jmax) { 2216 for (j=jmin; j<jmax; j++){ 2217 col = bj[j]; ba[j] = rtmp[col]; 2218 } 2219 /* add the k-th row into il and jl */ 2220 il[k] = jmin; 2221 i = bj[jmin]; jl[k] = jl[i]; jl[i] = k; 2222 } 2223 } 2224 } while (sctx.chshift); 2225 ierr = PetscFree3(rtmp,il,jl);CHKERRQ(ierr); 2226 ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr); 2227 ierr = ISRestoreIndices(iip,&riip);CHKERRQ(ierr); 2228 2229 ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr); 2230 if (perm_identity){ 2231 (B)->ops->solve = MatSolve_SeqSBAIJ_1_NaturalOrdering; 2232 (B)->ops->solvetranspose = MatSolve_SeqSBAIJ_1_NaturalOrdering; 2233 (B)->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_1_NaturalOrdering; 2234 (B)->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_1_NaturalOrdering; 2235 } else { 2236 (B)->ops->solve = MatSolve_SeqSBAIJ_1; 2237 (B)->ops->solvetranspose = MatSolve_SeqSBAIJ_1; 2238 (B)->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_1; 2239 (B)->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_1; 2240 } 2241 2242 C->assembled = PETSC_TRUE; 2243 C->preallocated = PETSC_TRUE; 2244 ierr = PetscLogFlops(C->rmap->n);CHKERRQ(ierr); 2245 if (sctx.nshift){ 2246 if (shiftnz) { 2247 ierr = PetscInfo2(A,"number of shiftnz tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr); 2248 } else if (shiftpd) { 2249 ierr = PetscInfo2(A,"number of shiftpd tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr); 2250 } 2251 } 2252 PetscFunctionReturn(0); 2253 } 2254 2255 /* 2256 icc() under revised new data structure. 2257 Factored arrays bj and ba are stored as 2258 U(0,:),...,U(i,:),U(n-1,:) 2259 2260 ui=fact->i is an array of size n+1, in which 2261 ui+ 2262 ui[i]: points to 1st entry of U(i,:),i=0,...,n-1 2263 ui[n]: points to U(n-1,n-1)+1 2264 2265 udiag=fact->diag is an array of size n,in which 2266 udiag[i]: points to diagonal of U(i,:), i=0,...,n-1 2267 2268 U(i,:) contains udiag[i] as its last entry, i.e., 2269 U(i,:) = (u[i,i+1],...,u[i,n-1],diag[i]) 2270 */ 2271 2272 #undef __FUNCT__ 2273 #define __FUNCT__ "MatICCFactorSymbolic_SeqAIJ_newdatastruct" 2274 PetscErrorCode MatICCFactorSymbolic_SeqAIJ_newdatastruct(Mat fact,Mat A,IS perm,const MatFactorInfo *info) 2275 { 2276 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2277 Mat_SeqSBAIJ *b; 2278 PetscErrorCode ierr; 2279 PetscTruth perm_identity,missing; 2280 PetscInt reallocs=0,i,*ai=a->i,*aj=a->j,am=A->rmap->n,*ui,*udiag; 2281 const PetscInt *rip,*riip; 2282 PetscInt jmin,jmax,nzk,k,j,*jl,prow,*il,nextprow; 2283 PetscInt nlnk,*lnk,*lnk_lvl=PETSC_NULL,d; 2284 PetscInt ncols,ncols_upper,*cols,*ajtmp,*uj,**uj_ptr,**uj_lvl_ptr; 2285 PetscReal fill=info->fill,levels=info->levels; 2286 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 2287 PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL; 2288 PetscBT lnkbt; 2289 IS iperm; 2290 2291 PetscFunctionBegin; 2292 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); 2293 ierr = MatMissingDiagonal(A,&missing,&d);CHKERRQ(ierr); 2294 if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d); 2295 ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 2296 ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr); 2297 2298 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr); 2299 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&udiag);CHKERRQ(ierr); 2300 ui[0] = 0; 2301 2302 /* ICC(0) without matrix ordering: simply rearrange column indices */ 2303 if (!levels && perm_identity) { 2304 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2305 cols = uj; 2306 for (i=0; i<am; i++) { 2307 ncols = ai[i+1] - a->diag[i]; 2308 ui[i+1] = ui[i] + ncols; 2309 udiag[i] = ui[i+1] - 1; /* points to the last entry of U(i,:) */ 2310 2311 aj = a->j + a->diag[i] + 1; /* 1st entry of U(i,:) without diagonal */ 2312 ncols--; /* exclude diagonal */ 2313 for (j=0; j<ncols; j++) *cols++ = aj[j]; 2314 *cols++ = i; /* diagoanl is located as the last entry of U(i,:) */ 2315 } 2316 } else { /* case: levels>0 || (levels=0 && !perm_identity) */ 2317 ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr); 2318 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 2319 2320 /* initialization */ 2321 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ajtmp);CHKERRQ(ierr); 2322 2323 /* jl: linked list for storing indices of the pivot rows 2324 il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */ 2325 ierr = PetscMalloc4(am,PetscInt*,&uj_ptr,am,PetscInt*,&uj_lvl_ptr,am,PetscInt,&jl,am,PetscInt,&il);CHKERRQ(ierr); 2326 for (i=0; i<am; i++){ 2327 jl[i] = am; il[i] = 0; 2328 } 2329 2330 /* create and initialize a linked list for storing column indices of the active row k */ 2331 nlnk = am + 1; 2332 ierr = PetscIncompleteLLCreate(am,am,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 2333 2334 /* initial FreeSpace size is fill*(ai[am]+1) */ 2335 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr); 2336 current_space = free_space; 2337 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space_lvl);CHKERRQ(ierr); 2338 current_space_lvl = free_space_lvl; 2339 2340 for (k=0; k<am; k++){ /* for each active row k */ 2341 /* initialize lnk by the column indices of row rip[k] of A */ 2342 nzk = 0; 2343 ncols = ai[rip[k]+1] - ai[rip[k]]; 2344 if (!ncols) SETERRQ2(PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k); 2345 ncols_upper = 0; 2346 for (j=0; j<ncols; j++){ 2347 i = *(aj + ai[rip[k]] + j); /* unpermuted column index */ 2348 if (riip[i] >= k){ /* only take upper triangular entry */ 2349 ajtmp[ncols_upper] = i; 2350 ncols_upper++; 2351 } 2352 } 2353 ierr = PetscIncompleteLLInit(ncols_upper,ajtmp,am,riip,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 2354 nzk += nlnk; 2355 2356 /* update lnk by computing fill-in for each pivot row to be merged in */ 2357 prow = jl[k]; /* 1st pivot row */ 2358 2359 while (prow < k){ 2360 nextprow = jl[prow]; 2361 2362 /* merge prow into k-th row */ 2363 jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */ 2364 jmax = ui[prow+1]; 2365 ncols = jmax-jmin; 2366 i = jmin - ui[prow]; 2367 cols = uj_ptr[prow] + i; /* points to the 2nd nzero entry in U(prow,k:am-1) */ 2368 uj = uj_lvl_ptr[prow] + i; /* levels of cols */ 2369 j = *(uj - 1); 2370 ierr = PetscICCLLAddSorted(ncols,cols,levels,uj,am,nlnk,lnk,lnk_lvl,lnkbt,j);CHKERRQ(ierr); 2371 nzk += nlnk; 2372 2373 /* update il and jl for prow */ 2374 if (jmin < jmax){ 2375 il[prow] = jmin; 2376 j = *cols; jl[prow] = jl[j]; jl[j] = prow; 2377 } 2378 prow = nextprow; 2379 } 2380 2381 /* if free space is not available, make more free space */ 2382 if (current_space->local_remaining<nzk) { 2383 i = am - k + 1; /* num of unfactored rows */ 2384 i *= PetscMin(nzk, i-1); /* i*nzk, i*(i-1): estimated and max additional space needed */ 2385 ierr = PetscFreeSpaceGet(i,¤t_space);CHKERRQ(ierr); 2386 ierr = PetscFreeSpaceGet(i,¤t_space_lvl);CHKERRQ(ierr); 2387 reallocs++; 2388 } 2389 2390 /* copy data into free_space and free_space_lvl, then initialize lnk */ 2391 if (nzk == 0) SETERRQ1(PETSC_ERR_ARG_WRONG,"Empty row %D in ICC matrix factor",k); 2392 ierr = PetscIncompleteLLClean(am,am,nzk,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr); 2393 2394 /* add the k-th row into il and jl */ 2395 if (nzk > 1){ 2396 i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */ 2397 jl[k] = jl[i]; jl[i] = k; 2398 il[k] = ui[k] + 1; 2399 } 2400 uj_ptr[k] = current_space->array; 2401 uj_lvl_ptr[k] = current_space_lvl->array; 2402 2403 current_space->array += nzk; 2404 current_space->local_used += nzk; 2405 current_space->local_remaining -= nzk; 2406 2407 current_space_lvl->array += nzk; 2408 current_space_lvl->local_used += nzk; 2409 current_space_lvl->local_remaining -= nzk; 2410 2411 ui[k+1] = ui[k] + nzk; 2412 } 2413 2414 #if defined(PETSC_USE_INFO) 2415 if (ai[am] != 0) { 2416 PetscReal af = (PetscReal)ui[am]/((PetscReal)ai[am]); 2417 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr); 2418 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 2419 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr); 2420 } else { 2421 ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr); 2422 } 2423 #endif 2424 2425 ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); 2426 ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr); 2427 ierr = PetscFree4(uj_ptr,uj_lvl_ptr,jl,il);CHKERRQ(ierr); 2428 ierr = PetscFree(ajtmp);CHKERRQ(ierr); 2429 2430 /* destroy list of free space and other temporary array(s) */ 2431 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2432 ierr = PetscFreeSpaceContiguous_Cholesky(&free_space,uj,am,ui,udiag);CHKERRQ(ierr); /* store matrix factor in newdatastruct */ 2433 ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 2434 ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr); 2435 2436 } /* end of case: levels>0 || (levels=0 && !perm_identity) */ 2437 2438 /* put together the new matrix in MATSEQSBAIJ format */ 2439 2440 b = (Mat_SeqSBAIJ*)(fact)->data; 2441 b->singlemalloc = PETSC_FALSE; 2442 ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr); 2443 b->j = uj; 2444 b->i = ui; 2445 b->diag = udiag; 2446 b->free_diag = PETSC_TRUE; 2447 b->ilen = 0; 2448 b->imax = 0; 2449 b->row = perm; 2450 b->col = perm; 2451 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2452 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2453 b->icol = iperm; 2454 b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ 2455 ierr = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 2456 ierr = PetscLogObjectMemory((fact),(ui[am]-am)*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 2457 b->maxnz = b->nz = ui[am]; 2458 b->free_a = PETSC_TRUE; 2459 b->free_ij = PETSC_TRUE; 2460 2461 (fact)->info.factor_mallocs = reallocs; 2462 (fact)->info.fill_ratio_given = fill; 2463 if (ai[am] != 0) { 2464 (fact)->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]); 2465 } else { 2466 (fact)->info.fill_ratio_needed = 0.0; 2467 } 2468 (fact)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ_newdatastruct; 2469 PetscFunctionReturn(0); 2470 } 2471 2472 #undef __FUNCT__ 2473 #define __FUNCT__ "MatICCFactorSymbolic_SeqAIJ" 2474 PetscErrorCode MatICCFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS perm,const MatFactorInfo *info) 2475 { 2476 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2477 Mat_SeqSBAIJ *b; 2478 PetscErrorCode ierr; 2479 PetscTruth perm_identity,missing; 2480 PetscInt reallocs=0,i,*ai=a->i,*aj=a->j,am=A->rmap->n,*ui,*udiag; 2481 const PetscInt *rip,*riip; 2482 PetscInt jmin,jmax,nzk,k,j,*jl,prow,*il,nextprow; 2483 PetscInt nlnk,*lnk,*lnk_lvl=PETSC_NULL,d; 2484 PetscInt ncols,ncols_upper,*cols,*ajtmp,*uj,**uj_ptr,**uj_lvl_ptr; 2485 PetscReal fill=info->fill,levels=info->levels; 2486 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 2487 PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL; 2488 PetscBT lnkbt; 2489 IS iperm; 2490 PetscTruth newdatastruct=PETSC_FALSE; 2491 2492 PetscFunctionBegin; 2493 ierr = PetscOptionsGetTruth(PETSC_NULL,"-icc_new",&newdatastruct,PETSC_NULL);CHKERRQ(ierr); 2494 if(newdatastruct){ 2495 ierr = MatICCFactorSymbolic_SeqAIJ_newdatastruct(fact,A,perm,info);CHKERRQ(ierr); 2496 PetscFunctionReturn(0); 2497 } 2498 2499 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); 2500 ierr = MatMissingDiagonal(A,&missing,&d);CHKERRQ(ierr); 2501 if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d); 2502 ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 2503 ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr); 2504 2505 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr); 2506 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&udiag);CHKERRQ(ierr); 2507 ui[0] = 0; 2508 2509 /* ICC(0) without matrix ordering: simply copies fill pattern */ 2510 if (!levels && perm_identity) { 2511 2512 for (i=0; i<am; i++) { 2513 ui[i+1] = ui[i] + ai[i+1] - a->diag[i]; 2514 udiag[i] = ui[i]; 2515 } 2516 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2517 cols = uj; 2518 for (i=0; i<am; i++) { 2519 aj = a->j + a->diag[i]; 2520 ncols = ui[i+1] - ui[i]; 2521 for (j=0; j<ncols; j++) *cols++ = *aj++; 2522 } 2523 } else { /* case: levels>0 || (levels=0 && !perm_identity) */ 2524 ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr); 2525 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 2526 2527 /* initialization */ 2528 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ajtmp);CHKERRQ(ierr); 2529 2530 /* jl: linked list for storing indices of the pivot rows 2531 il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */ 2532 ierr = PetscMalloc4(am,PetscInt*,&uj_ptr,am,PetscInt*,&uj_lvl_ptr,am,PetscInt,&jl,am,PetscInt,&il);CHKERRQ(ierr); 2533 for (i=0; i<am; i++){ 2534 jl[i] = am; il[i] = 0; 2535 } 2536 2537 /* create and initialize a linked list for storing column indices of the active row k */ 2538 nlnk = am + 1; 2539 ierr = PetscIncompleteLLCreate(am,am,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 2540 2541 /* initial FreeSpace size is fill*(ai[am]+1) */ 2542 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr); 2543 current_space = free_space; 2544 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space_lvl);CHKERRQ(ierr); 2545 current_space_lvl = free_space_lvl; 2546 2547 for (k=0; k<am; k++){ /* for each active row k */ 2548 /* initialize lnk by the column indices of row rip[k] of A */ 2549 nzk = 0; 2550 ncols = ai[rip[k]+1] - ai[rip[k]]; 2551 if (!ncols) SETERRQ2(PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k); 2552 ncols_upper = 0; 2553 for (j=0; j<ncols; j++){ 2554 i = *(aj + ai[rip[k]] + j); /* unpermuted column index */ 2555 if (riip[i] >= k){ /* only take upper triangular entry */ 2556 ajtmp[ncols_upper] = i; 2557 ncols_upper++; 2558 } 2559 } 2560 ierr = PetscIncompleteLLInit(ncols_upper,ajtmp,am,riip,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr); 2561 nzk += nlnk; 2562 2563 /* update lnk by computing fill-in for each pivot row to be merged in */ 2564 prow = jl[k]; /* 1st pivot row */ 2565 2566 while (prow < k){ 2567 nextprow = jl[prow]; 2568 2569 /* merge prow into k-th row */ 2570 jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */ 2571 jmax = ui[prow+1]; 2572 ncols = jmax-jmin; 2573 i = jmin - ui[prow]; 2574 cols = uj_ptr[prow] + i; /* points to the 2nd nzero entry in U(prow,k:am-1) */ 2575 uj = uj_lvl_ptr[prow] + i; /* levels of cols */ 2576 j = *(uj - 1); 2577 ierr = PetscICCLLAddSorted(ncols,cols,levels,uj,am,nlnk,lnk,lnk_lvl,lnkbt,j);CHKERRQ(ierr); 2578 nzk += nlnk; 2579 2580 /* update il and jl for prow */ 2581 if (jmin < jmax){ 2582 il[prow] = jmin; 2583 j = *cols; jl[prow] = jl[j]; jl[j] = prow; 2584 } 2585 prow = nextprow; 2586 } 2587 2588 /* if free space is not available, make more free space */ 2589 if (current_space->local_remaining<nzk) { 2590 i = am - k + 1; /* num of unfactored rows */ 2591 i *= PetscMin(nzk, (i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */ 2592 ierr = PetscFreeSpaceGet(i,¤t_space);CHKERRQ(ierr); 2593 ierr = PetscFreeSpaceGet(i,¤t_space_lvl);CHKERRQ(ierr); 2594 reallocs++; 2595 } 2596 2597 /* copy data into free_space and free_space_lvl, then initialize lnk */ 2598 if (nzk == 0) SETERRQ1(PETSC_ERR_ARG_WRONG,"Empty row %D in ICC matrix factor",k); 2599 ierr = PetscIncompleteLLClean(am,am,nzk,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr); 2600 2601 /* add the k-th row into il and jl */ 2602 if (nzk > 1){ 2603 i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */ 2604 jl[k] = jl[i]; jl[i] = k; 2605 il[k] = ui[k] + 1; 2606 } 2607 uj_ptr[k] = current_space->array; 2608 uj_lvl_ptr[k] = current_space_lvl->array; 2609 2610 current_space->array += nzk; 2611 current_space->local_used += nzk; 2612 current_space->local_remaining -= nzk; 2613 2614 current_space_lvl->array += nzk; 2615 current_space_lvl->local_used += nzk; 2616 current_space_lvl->local_remaining -= nzk; 2617 2618 ui[k+1] = ui[k] + nzk; 2619 } 2620 2621 #if defined(PETSC_USE_INFO) 2622 if (ai[am] != 0) { 2623 PetscReal af = (PetscReal)ui[am]/((PetscReal)ai[am]); 2624 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr); 2625 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 2626 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr); 2627 } else { 2628 ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr); 2629 } 2630 #endif 2631 2632 ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); 2633 ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr); 2634 ierr = PetscFree4(uj_ptr,uj_lvl_ptr,jl,il);CHKERRQ(ierr); 2635 ierr = PetscFree(ajtmp);CHKERRQ(ierr); 2636 2637 /* destroy list of free space and other temporary array(s) */ 2638 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2639 ierr = PetscFreeSpaceContiguous(&free_space,uj);CHKERRQ(ierr); 2640 ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 2641 ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr); 2642 2643 } /* end of case: levels>0 || (levels=0 && !perm_identity) */ 2644 2645 /* put together the new matrix in MATSEQSBAIJ format */ 2646 2647 b = (Mat_SeqSBAIJ*)(fact)->data; 2648 b->singlemalloc = PETSC_FALSE; 2649 ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr); 2650 b->j = uj; 2651 b->i = ui; 2652 b->diag = udiag; 2653 b->free_diag = PETSC_TRUE; 2654 b->ilen = 0; 2655 b->imax = 0; 2656 b->row = perm; 2657 b->col = perm; 2658 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2659 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2660 b->icol = iperm; 2661 b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ 2662 ierr = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 2663 ierr = PetscLogObjectMemory((fact),(ui[am]-am)*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 2664 b->maxnz = b->nz = ui[am]; 2665 b->free_a = PETSC_TRUE; 2666 b->free_ij = PETSC_TRUE; 2667 2668 (fact)->info.factor_mallocs = reallocs; 2669 (fact)->info.fill_ratio_given = fill; 2670 if (ai[am] != 0) { 2671 (fact)->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]); 2672 } else { 2673 (fact)->info.fill_ratio_needed = 0.0; 2674 } 2675 (fact)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ; 2676 PetscFunctionReturn(0); 2677 } 2678 2679 PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJ_newdatastruct(Mat fact,Mat A,IS perm,const MatFactorInfo *info) 2680 { 2681 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2682 Mat_SeqSBAIJ *b; 2683 PetscErrorCode ierr; 2684 PetscTruth perm_identity; 2685 PetscReal fill = info->fill; 2686 const PetscInt *rip,*riip; 2687 PetscInt i,am=A->rmap->n,*ai=a->i,*aj=a->j,reallocs=0,prow; 2688 PetscInt *jl,jmin,jmax,nzk,*ui,k,j,*il,nextprow; 2689 PetscInt nlnk,*lnk,ncols,ncols_upper,*cols,*uj,**ui_ptr,*uj_ptr,*udiag; 2690 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 2691 PetscBT lnkbt; 2692 IS iperm; 2693 2694 PetscFunctionBegin; 2695 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); 2696 /* check whether perm is the identity mapping */ 2697 ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 2698 ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr); 2699 ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr); 2700 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 2701 2702 /* initialization */ 2703 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr); 2704 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&udiag);CHKERRQ(ierr); 2705 ui[0] = 0; 2706 2707 /* jl: linked list for storing indices of the pivot rows 2708 il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */ 2709 ierr = PetscMalloc4(am,PetscInt*,&ui_ptr,am,PetscInt,&jl,am,PetscInt,&il,am,PetscInt,&cols);CHKERRQ(ierr); 2710 for (i=0; i<am; i++){ 2711 jl[i] = am; il[i] = 0; 2712 } 2713 2714 /* create and initialize a linked list for storing column indices of the active row k */ 2715 nlnk = am + 1; 2716 ierr = PetscLLCreate(am,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2717 2718 /* initial FreeSpace size is fill*(ai[am]+1) */ 2719 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr); 2720 current_space = free_space; 2721 2722 for (k=0; k<am; k++){ /* for each active row k */ 2723 /* initialize lnk by the column indices of row rip[k] of A */ 2724 nzk = 0; 2725 ncols = ai[rip[k]+1] - ai[rip[k]]; 2726 if (!ncols) SETERRQ2(PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k); 2727 ncols_upper = 0; 2728 for (j=0; j<ncols; j++){ 2729 i = riip[*(aj + ai[rip[k]] + j)]; 2730 if (i >= k){ /* only take upper triangular entry */ 2731 cols[ncols_upper] = i; 2732 ncols_upper++; 2733 } 2734 } 2735 ierr = PetscLLAdd(ncols_upper,cols,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2736 nzk += nlnk; 2737 2738 /* update lnk by computing fill-in for each pivot row to be merged in */ 2739 prow = jl[k]; /* 1st pivot row */ 2740 2741 while (prow < k){ 2742 nextprow = jl[prow]; 2743 /* merge prow into k-th row */ 2744 jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */ 2745 jmax = ui[prow+1]; 2746 ncols = jmax-jmin; 2747 uj_ptr = ui_ptr[prow] + jmin - ui[prow]; /* points to the 2nd nzero entry in U(prow,k:am-1) */ 2748 ierr = PetscLLAddSorted(ncols,uj_ptr,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2749 nzk += nlnk; 2750 2751 /* update il and jl for prow */ 2752 if (jmin < jmax){ 2753 il[prow] = jmin; 2754 j = *uj_ptr; jl[prow] = jl[j]; jl[j] = prow; 2755 } 2756 prow = nextprow; 2757 } 2758 2759 /* if free space is not available, make more free space */ 2760 if (current_space->local_remaining<nzk) { 2761 i = am - k + 1; /* num of unfactored rows */ 2762 i *= PetscMin(nzk,i-1); /* i*nzk, i*(i-1): estimated and max additional space needed */ 2763 ierr = PetscFreeSpaceGet(i,¤t_space);CHKERRQ(ierr); 2764 reallocs++; 2765 } 2766 2767 /* copy data into free space, then initialize lnk */ 2768 ierr = PetscLLClean(am,am,nzk,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 2769 2770 /* add the k-th row into il and jl */ 2771 if (nzk-1 > 0){ 2772 i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */ 2773 jl[k] = jl[i]; jl[i] = k; 2774 il[k] = ui[k] + 1; 2775 } 2776 ui_ptr[k] = current_space->array; 2777 current_space->array += nzk; 2778 current_space->local_used += nzk; 2779 current_space->local_remaining -= nzk; 2780 2781 ui[k+1] = ui[k] + nzk; 2782 } 2783 2784 #if defined(PETSC_USE_INFO) 2785 if (ai[am] != 0) { 2786 PetscReal af = (PetscReal)(ui[am])/((PetscReal)ai[am]); 2787 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr); 2788 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 2789 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr); 2790 } else { 2791 ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr); 2792 } 2793 #endif 2794 2795 ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); 2796 ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr); 2797 ierr = PetscFree4(ui_ptr,jl,il,cols);CHKERRQ(ierr); 2798 2799 /* destroy list of free space and other temporary array(s) */ 2800 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2801 ierr = PetscFreeSpaceContiguous_Cholesky(&free_space,uj,am,ui,udiag);CHKERRQ(ierr); /* store matrix factor in newdatastruct */ 2802 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 2803 2804 /* put together the new matrix in MATSEQSBAIJ format */ 2805 2806 b = (Mat_SeqSBAIJ*)(fact)->data; 2807 b->singlemalloc = PETSC_FALSE; 2808 b->free_a = PETSC_TRUE; 2809 b->free_ij = PETSC_TRUE; 2810 ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr); 2811 b->j = uj; 2812 b->i = ui; 2813 b->diag = udiag; 2814 b->free_diag = PETSC_TRUE; 2815 b->ilen = 0; 2816 b->imax = 0; 2817 b->row = perm; 2818 b->col = perm; 2819 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2820 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2821 b->icol = iperm; 2822 b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ 2823 ierr = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 2824 ierr = PetscLogObjectMemory(fact,(ui[am]-am)*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 2825 b->maxnz = b->nz = ui[am]; 2826 2827 (fact)->info.factor_mallocs = reallocs; 2828 (fact)->info.fill_ratio_given = fill; 2829 if (ai[am] != 0) { 2830 (fact)->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]); 2831 } else { 2832 (fact)->info.fill_ratio_needed = 0.0; 2833 } 2834 (fact)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ_newdatastruct; 2835 PetscFunctionReturn(0); 2836 } 2837 2838 #undef __FUNCT__ 2839 #define __FUNCT__ "MatCholeskyFactorSymbolic_SeqAIJ" 2840 PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS perm,const MatFactorInfo *info) 2841 { 2842 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2843 Mat_SeqSBAIJ *b; 2844 PetscErrorCode ierr; 2845 PetscTruth perm_identity; 2846 PetscReal fill = info->fill; 2847 const PetscInt *rip,*riip; 2848 PetscInt i,am=A->rmap->n,*ai=a->i,*aj=a->j,reallocs=0,prow; 2849 PetscInt *jl,jmin,jmax,nzk,*ui,k,j,*il,nextprow; 2850 PetscInt nlnk,*lnk,ncols,ncols_upper,*cols,*uj,**ui_ptr,*uj_ptr; 2851 PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 2852 PetscBT lnkbt; 2853 IS iperm; 2854 PetscTruth newdatastruct=PETSC_FALSE; 2855 2856 PetscFunctionBegin; 2857 ierr = PetscOptionsGetTruth(PETSC_NULL,"-cholesky_new",&newdatastruct,PETSC_NULL);CHKERRQ(ierr); 2858 if(newdatastruct){ 2859 ierr = MatCholeskyFactorSymbolic_SeqAIJ_newdatastruct(fact,A,perm,info);CHKERRQ(ierr); 2860 PetscFunctionReturn(0); 2861 } 2862 2863 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); 2864 /* check whether perm is the identity mapping */ 2865 ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 2866 ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr); 2867 ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr); 2868 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 2869 2870 /* initialization */ 2871 ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr); 2872 ui[0] = 0; 2873 2874 /* jl: linked list for storing indices of the pivot rows 2875 il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */ 2876 ierr = PetscMalloc4(am,PetscInt*,&ui_ptr,am,PetscInt,&jl,am,PetscInt,&il,am,PetscInt,&cols);CHKERRQ(ierr); 2877 for (i=0; i<am; i++){ 2878 jl[i] = am; il[i] = 0; 2879 } 2880 2881 /* create and initialize a linked list for storing column indices of the active row k */ 2882 nlnk = am + 1; 2883 ierr = PetscLLCreate(am,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2884 2885 /* initial FreeSpace size is fill*(ai[am]+1) */ 2886 ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr); 2887 current_space = free_space; 2888 2889 for (k=0; k<am; k++){ /* for each active row k */ 2890 /* initialize lnk by the column indices of row rip[k] of A */ 2891 nzk = 0; 2892 ncols = ai[rip[k]+1] - ai[rip[k]]; 2893 if (!ncols) SETERRQ2(PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k); 2894 ncols_upper = 0; 2895 for (j=0; j<ncols; j++){ 2896 i = riip[*(aj + ai[rip[k]] + j)]; 2897 if (i >= k){ /* only take upper triangular entry */ 2898 cols[ncols_upper] = i; 2899 ncols_upper++; 2900 } 2901 } 2902 ierr = PetscLLAdd(ncols_upper,cols,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2903 nzk += nlnk; 2904 2905 /* update lnk by computing fill-in for each pivot row to be merged in */ 2906 prow = jl[k]; /* 1st pivot row */ 2907 2908 while (prow < k){ 2909 nextprow = jl[prow]; 2910 /* merge prow into k-th row */ 2911 jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */ 2912 jmax = ui[prow+1]; 2913 ncols = jmax-jmin; 2914 uj_ptr = ui_ptr[prow] + jmin - ui[prow]; /* points to the 2nd nzero entry in U(prow,k:am-1) */ 2915 ierr = PetscLLAddSorted(ncols,uj_ptr,am,nlnk,lnk,lnkbt);CHKERRQ(ierr); 2916 nzk += nlnk; 2917 2918 /* update il and jl for prow */ 2919 if (jmin < jmax){ 2920 il[prow] = jmin; 2921 j = *uj_ptr; jl[prow] = jl[j]; jl[j] = prow; 2922 } 2923 prow = nextprow; 2924 } 2925 2926 /* if free space is not available, make more free space */ 2927 if (current_space->local_remaining<nzk) { 2928 i = am - k + 1; /* num of unfactored rows */ 2929 i = PetscMin(i*nzk, i*(i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */ 2930 ierr = PetscFreeSpaceGet(i,¤t_space);CHKERRQ(ierr); 2931 reallocs++; 2932 } 2933 2934 /* copy data into free space, then initialize lnk */ 2935 ierr = PetscLLClean(am,am,nzk,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 2936 2937 /* add the k-th row into il and jl */ 2938 if (nzk-1 > 0){ 2939 i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */ 2940 jl[k] = jl[i]; jl[i] = k; 2941 il[k] = ui[k] + 1; 2942 } 2943 ui_ptr[k] = current_space->array; 2944 current_space->array += nzk; 2945 current_space->local_used += nzk; 2946 current_space->local_remaining -= nzk; 2947 2948 ui[k+1] = ui[k] + nzk; 2949 } 2950 2951 #if defined(PETSC_USE_INFO) 2952 if (ai[am] != 0) { 2953 PetscReal af = (PetscReal)(ui[am])/((PetscReal)ai[am]); 2954 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr); 2955 ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr); 2956 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr); 2957 } else { 2958 ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr); 2959 } 2960 #endif 2961 2962 ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); 2963 ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr); 2964 ierr = PetscFree4(ui_ptr,jl,il,cols);CHKERRQ(ierr); 2965 2966 /* destroy list of free space and other temporary array(s) */ 2967 ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr); 2968 ierr = PetscFreeSpaceContiguous(&free_space,uj);CHKERRQ(ierr); 2969 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 2970 2971 /* put together the new matrix in MATSEQSBAIJ format */ 2972 2973 b = (Mat_SeqSBAIJ*)(fact)->data; 2974 b->singlemalloc = PETSC_FALSE; 2975 b->free_a = PETSC_TRUE; 2976 b->free_ij = PETSC_TRUE; 2977 ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr); 2978 b->j = uj; 2979 b->i = ui; 2980 b->diag = 0; 2981 b->ilen = 0; 2982 b->imax = 0; 2983 b->row = perm; 2984 b->col = perm; 2985 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2986 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 2987 b->icol = iperm; 2988 b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ 2989 ierr = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 2990 ierr = PetscLogObjectMemory(fact,(ui[am]-am)*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 2991 b->maxnz = b->nz = ui[am]; 2992 2993 (fact)->info.factor_mallocs = reallocs; 2994 (fact)->info.fill_ratio_given = fill; 2995 if (ai[am] != 0) { 2996 (fact)->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]); 2997 } else { 2998 (fact)->info.fill_ratio_needed = 0.0; 2999 } 3000 (fact)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ; 3001 PetscFunctionReturn(0); 3002 } 3003 3004 #if defined(OLD_ROUTINE_TO_BE_REPLACED) 3005 #undef __FUNCT__ 3006 #define __FUNCT__ "MatSolve_SeqAIJ_NaturalOrdering_newdatastruct" 3007 PetscErrorCode MatSolve_SeqAIJ_NaturalOrdering_newdatastruct(Mat A,Vec bb,Vec xx) 3008 { 3009 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3010 PetscErrorCode ierr; 3011 PetscInt n = A->rmap->n; 3012 const PetscInt *ai = a->i,*aj = a->j,*vi; 3013 PetscScalar *x,sum; 3014 const PetscScalar *b; 3015 const MatScalar *aa = a->a,*v; 3016 PetscInt i,nz; 3017 3018 PetscFunctionBegin; 3019 if (!n) PetscFunctionReturn(0); 3020 3021 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 3022 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 3023 3024 /* forward solve the lower triangular */ 3025 x[0] = b[0]; 3026 v = aa; 3027 vi = aj; 3028 for (i=1; i<n; i++) { 3029 nz = ai[i+1] - ai[i]; 3030 sum = b[i]; 3031 PetscSparseDenseMinusDot(sum,x,v,vi,nz); 3032 v += nz; 3033 vi += nz; 3034 x[i] = sum; 3035 } 3036 3037 /* backward solve the upper triangular */ 3038 v = aa + ai[n+1]; 3039 vi = aj + ai[n+1]; 3040 for (i=n-1; i>=0; i--){ 3041 nz = ai[2*n-i +1] - ai[2*n-i]-1; 3042 sum = x[i]; 3043 PetscSparseDenseMinusDot(sum,x,v,vi,nz); 3044 v += nz; 3045 vi += nz; vi++; 3046 x[i] = *v++ *sum; /* x[i]=aa[adiag[i]]*sum; v++; */ 3047 } 3048 3049 ierr = PetscLogFlops(2.0*a->nz - A->cmap->n);CHKERRQ(ierr); 3050 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 3051 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 3052 PetscFunctionReturn(0); 3053 } 3054 #endif 3055 3056 #undef __FUNCT__ 3057 #define __FUNCT__ "MatSolve_SeqAIJ_NaturalOrdering_newdatastruct" 3058 PetscErrorCode MatSolve_SeqAIJ_NaturalOrdering_newdatastruct(Mat A,Vec bb,Vec xx) 3059 { 3060 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3061 PetscErrorCode ierr; 3062 PetscInt n = A->rmap->n; 3063 const PetscInt *ai = a->i,*aj = a->j,*adiag = a->diag,*vi; 3064 PetscScalar *x,sum; 3065 const PetscScalar *b; 3066 const MatScalar *aa = a->a,*v; 3067 PetscInt i,nz; 3068 3069 PetscFunctionBegin; 3070 if (!n) PetscFunctionReturn(0); 3071 3072 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 3073 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 3074 3075 /* forward solve the lower triangular */ 3076 x[0] = b[0]; 3077 v = aa; 3078 vi = aj; 3079 for (i=1; i<n; i++) { 3080 nz = ai[i+1] - ai[i]; 3081 sum = b[i]; 3082 PetscSparseDenseMinusDot(sum,x,v,vi,nz); 3083 v += nz; 3084 vi += nz; 3085 x[i] = sum; 3086 } 3087 3088 /* backward solve the upper triangular */ 3089 for (i=n-1; i>=0; i--){ 3090 v = aa + adiag[i+1] + 1; 3091 vi = aj + adiag[i+1] + 1; 3092 nz = adiag[i] - adiag[i+1]-1; 3093 sum = x[i]; 3094 PetscSparseDenseMinusDot(sum,x,v,vi,nz); 3095 x[i] = sum*v[nz]; /* x[i]=aa[adiag[i]]*sum; v++; */ 3096 } 3097 3098 ierr = PetscLogFlops(2.0*a->nz - A->cmap->n);CHKERRQ(ierr); 3099 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 3100 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 3101 PetscFunctionReturn(0); 3102 } 3103 3104 #if defined(OLD_ROUTINE_TO_BE_REPLACED) 3105 #undef __FUNCT__ 3106 #define __FUNCT__ "MatSolve_SeqAIJ_newdatastruct" 3107 PetscErrorCode MatSolve_SeqAIJ_newdatastruct(Mat A,Vec bb,Vec xx) 3108 { 3109 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3110 IS iscol = a->col,isrow = a->row; 3111 PetscErrorCode ierr; 3112 PetscInt i,n=A->rmap->n,*vi,*ai=a->i,*aj=a->j,nz,k; 3113 const PetscInt *rout,*cout,*r,*c; 3114 PetscScalar *x,*tmp,*tmps,sum; 3115 const PetscScalar *b; 3116 const MatScalar *aa = a->a,*v; 3117 3118 PetscFunctionBegin; 3119 if (!n) PetscFunctionReturn(0); 3120 3121 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 3122 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 3123 tmp = a->solve_work; 3124 3125 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 3126 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 3127 3128 /* forward solve the lower triangular */ 3129 tmp[0] = b[*r++]; 3130 tmps = tmp; 3131 v = aa; 3132 vi = aj; 3133 for (i=1; i<n; i++) { 3134 nz = ai[i+1] - ai[i]; 3135 sum = b[*r++]; 3136 PetscSparseDenseMinusDot(sum,tmps,v,vi,nz); 3137 tmp[i] = sum; 3138 v += nz; vi += nz; 3139 } 3140 3141 /* backward solve the upper triangular */ 3142 k = n+1; 3143 v = aa + ai[k]; /* 1st entry of U(n-1,:) */ 3144 vi = aj + ai[k]; 3145 for (i=n-1; i>=0; i--){ 3146 k = 2*n-i; 3147 nz = ai[k +1] - ai[k] - 1; 3148 sum = tmp[i]; 3149 PetscSparseDenseMinusDot(sum,tmps,v,vi,nz); 3150 x[c[i]] = tmp[i] = sum*v[nz]; /* v[nz] = aa[adiag[i]] */ 3151 v += nz+1; vi += nz+1; 3152 } 3153 3154 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 3155 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 3156 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 3157 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 3158 ierr = PetscLogFlops(2*a->nz - A->cmap->n);CHKERRQ(ierr); 3159 PetscFunctionReturn(0); 3160 } 3161 #endif 3162 3163 #undef __FUNCT__ 3164 #define __FUNCT__ "MatSolve_SeqAIJ_newdatastruct" 3165 PetscErrorCode MatSolve_SeqAIJ_newdatastruct(Mat A,Vec bb,Vec xx) 3166 { 3167 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3168 IS iscol = a->col,isrow = a->row; 3169 PetscErrorCode ierr; 3170 PetscInt i,n=A->rmap->n,*vi,*ai=a->i,*aj=a->j,*adiag = a->diag,nz; 3171 const PetscInt *rout,*cout,*r,*c; 3172 PetscScalar *x,*tmp,sum; 3173 const PetscScalar *b; 3174 const MatScalar *aa = a->a,*v; 3175 3176 PetscFunctionBegin; 3177 if (!n) PetscFunctionReturn(0); 3178 3179 ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 3180 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 3181 tmp = a->solve_work; 3182 3183 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 3184 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout; 3185 3186 /* forward solve the lower triangular */ 3187 tmp[0] = b[r[0]]; 3188 v = aa; 3189 vi = aj; 3190 for (i=1; i<n; i++) { 3191 nz = ai[i+1] - ai[i]; 3192 sum = b[r[i]]; 3193 PetscSparseDenseMinusDot(sum,tmp,v,vi,nz); 3194 tmp[i] = sum; 3195 v += nz; vi += nz; 3196 } 3197 3198 /* backward solve the upper triangular */ 3199 for (i=n-1; i>=0; i--){ 3200 v = aa + adiag[i+1]+1; 3201 vi = aj + adiag[i+1]+1; 3202 nz = adiag[i]-adiag[i+1]-1; 3203 sum = tmp[i]; 3204 PetscSparseDenseMinusDot(sum,tmp,v,vi,nz); 3205 x[c[i]] = tmp[i] = sum*v[nz]; /* v[nz] = aa[adiag[i]] */ 3206 } 3207 3208 ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr); 3209 ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr); 3210 ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 3211 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 3212 ierr = PetscLogFlops(2*a->nz - A->cmap->n);CHKERRQ(ierr); 3213 PetscFunctionReturn(0); 3214 } 3215 3216 #undef __FUNCT__ 3217 #define __FUNCT__ "MatILUDTFactor_SeqAIJ" 3218 PetscErrorCode MatILUDTFactor_SeqAIJ(Mat A,IS isrow,IS iscol,const MatFactorInfo *info,Mat *fact) 3219 { 3220 Mat B = *fact; 3221 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b; 3222 IS isicol; 3223 PetscErrorCode ierr; 3224 const PetscInt *r,*ic; 3225 PetscInt i,n=A->rmap->n,*ai=a->i,*aj=a->j,*ajtmp,*adiag; 3226 PetscInt *bi,*bj,*bdiag,*bdiag_rev; 3227 PetscInt row,nzi,nzi_bl,nzi_bu,*im,nzi_al,nzi_au; 3228 PetscInt nlnk,*lnk; 3229 PetscBT lnkbt; 3230 PetscTruth row_identity,icol_identity,both_identity; 3231 MatScalar *aatmp,*pv,*batmp,*ba,*rtmp,*pc,multiplier,*vtmp,diag_tmp; 3232 const PetscInt *ics; 3233 PetscInt j,nz,*pj,*bjtmp,k,ncut,*jtmp; 3234 PetscReal dt=info->dt,dtcol=info->dtcol,shift=info->shiftinblocks; 3235 PetscInt dtcount=(PetscInt)info->dtcount,nnz_max; 3236 PetscTruth missing; 3237 3238 PetscFunctionBegin; 3239 3240 if (dt == PETSC_DEFAULT) dt = 0.005; 3241 if (dtcol == PETSC_DEFAULT) dtcol = 0.01; /* XXX unused! */ 3242 if (dtcount == PETSC_DEFAULT) dtcount = (PetscInt)(1.5*a->rmax); 3243 3244 /* ------- symbolic factorization, can be reused ---------*/ 3245 ierr = MatMissingDiagonal(A,&missing,&i);CHKERRQ(ierr); 3246 if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",i); 3247 adiag=a->diag; 3248 3249 ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr); 3250 3251 /* bdiag is location of diagonal in factor */ 3252 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr); /* becomes b->diag */ 3253 ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag_rev);CHKERRQ(ierr); /* temporary */ 3254 3255 /* allocate row pointers bi */ 3256 ierr = PetscMalloc((2*n+2)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 3257 3258 /* allocate bj and ba; max num of nonzero entries is (ai[n]+2*n*dtcount+2) */ 3259 if (dtcount > n-1) dtcount = n-1; /* diagonal is excluded */ 3260 nnz_max = ai[n]+2*n*dtcount+2; 3261 3262 ierr = PetscMalloc((nnz_max+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 3263 ierr = PetscMalloc((nnz_max+1)*sizeof(MatScalar),&ba);CHKERRQ(ierr); 3264 3265 /* put together the new matrix */ 3266 ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr); 3267 ierr = PetscLogObjectParent(B,isicol);CHKERRQ(ierr); 3268 b = (Mat_SeqAIJ*)(B)->data; 3269 b->free_a = PETSC_TRUE; 3270 b->free_ij = PETSC_TRUE; 3271 b->singlemalloc = PETSC_FALSE; 3272 b->a = ba; 3273 b->j = bj; 3274 b->i = bi; 3275 b->diag = bdiag; 3276 b->ilen = 0; 3277 b->imax = 0; 3278 b->row = isrow; 3279 b->col = iscol; 3280 ierr = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr); 3281 ierr = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr); 3282 b->icol = isicol; 3283 ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); 3284 3285 ierr = PetscLogObjectMemory(B,nnz_max*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 3286 b->maxnz = nnz_max; 3287 3288 (B)->factor = MAT_FACTOR_ILUDT; 3289 (B)->info.factor_mallocs = 0; 3290 (B)->info.fill_ratio_given = ((PetscReal)nnz_max)/((PetscReal)ai[n]); 3291 CHKMEMQ; 3292 /* ------- end of symbolic factorization ---------*/ 3293 3294 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 3295 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 3296 ics = ic; 3297 3298 /* linked list for storing column indices of the active row */ 3299 nlnk = n + 1; 3300 ierr = PetscLLCreate(n,n,nlnk,lnk,lnkbt);CHKERRQ(ierr); 3301 3302 /* im: used by PetscLLAddSortedLU(); jtmp: working array for column indices of active row */ 3303 ierr = PetscMalloc2(n,PetscInt,&im,n,PetscInt,&jtmp);CHKERRQ(ierr); 3304 /* rtmp, vtmp: working arrays for sparse and contiguous row entries of active row */ 3305 ierr = PetscMalloc2(n,MatScalar,&rtmp,n,MatScalar,&vtmp);CHKERRQ(ierr); 3306 ierr = PetscMemzero(rtmp,n*sizeof(MatScalar));CHKERRQ(ierr); 3307 3308 bi[0] = 0; 3309 bdiag[0] = nnz_max-1; /* location of diag[0] in factor B */ 3310 bdiag_rev[n] = bdiag[0]; 3311 bi[2*n+1] = bdiag[0]+1; /* endof bj and ba array */ 3312 for (i=0; i<n; i++) { 3313 /* copy initial fill into linked list */ 3314 nzi = 0; /* nonzeros for active row i */ 3315 nzi = ai[r[i]+1] - ai[r[i]]; 3316 if (!nzi) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i); 3317 nzi_al = adiag[r[i]] - ai[r[i]]; 3318 nzi_au = ai[r[i]+1] - adiag[r[i]] -1; 3319 ajtmp = aj + ai[r[i]]; 3320 ierr = PetscLLAddPerm(nzi,ajtmp,ic,n,nlnk,lnk,lnkbt);CHKERRQ(ierr); 3321 3322 /* load in initial (unfactored row) */ 3323 aatmp = a->a + ai[r[i]]; 3324 for (j=0; j<nzi; j++) { 3325 rtmp[ics[*ajtmp++]] = *aatmp++; 3326 } 3327 3328 /* add pivot rows into linked list */ 3329 row = lnk[n]; 3330 while (row < i ) { 3331 nzi_bl = bi[row+1] - bi[row] + 1; 3332 bjtmp = bj + bdiag[row+1]+1; /* points to 1st column next to the diagonal in U */ 3333 ierr = PetscLLAddSortedLU(bjtmp,row,nlnk,lnk,lnkbt,i,nzi_bl,im);CHKERRQ(ierr); 3334 nzi += nlnk; 3335 row = lnk[row]; 3336 } 3337 3338 /* copy data from lnk into jtmp, then initialize lnk */ 3339 ierr = PetscLLClean(n,n,nzi,lnk,jtmp,lnkbt);CHKERRQ(ierr); 3340 3341 /* numerical factorization */ 3342 bjtmp = jtmp; 3343 row = *bjtmp++; /* 1st pivot row */ 3344 while ( row < i ) { 3345 pc = rtmp + row; 3346 pv = ba + bdiag[row]; /* 1./(diag of the pivot row) */ 3347 multiplier = (*pc) * (*pv); 3348 *pc = multiplier; 3349 if (PetscAbsScalar(*pc) > dt){ /* apply tolerance dropping rule */ 3350 pj = bj + bdiag[row+1] + 1; /* point to 1st entry of U(row,:) */ 3351 pv = ba + bdiag[row+1] + 1; 3352 /* if (multiplier < -1.0 or multiplier >1.0) printf("row/prow %d, %d, multiplier %g\n",i,row,multiplier); */ 3353 nz = bdiag[row] - bdiag[row+1] - 1; /* num of entries in U(row,:), excluding diagonal */ 3354 for (j=0; j<nz; j++) rtmp[*pj++] -= multiplier * (*pv++); 3355 ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr); 3356 } 3357 row = *bjtmp++; 3358 } 3359 3360 /* copy sparse rtmp into contiguous vtmp; separate L and U part */ 3361 diag_tmp = rtmp[i]; /* save diagonal value - may not needed?? */ 3362 nzi_bl = 0; j = 0; 3363 while (jtmp[j] < i){ /* Note: jtmp is sorted */ 3364 vtmp[j] = rtmp[jtmp[j]]; rtmp[jtmp[j]]=0.0; 3365 nzi_bl++; j++; 3366 } 3367 nzi_bu = nzi - nzi_bl -1; 3368 while (j < nzi){ 3369 vtmp[j] = rtmp[jtmp[j]]; rtmp[jtmp[j]]=0.0; 3370 j++; 3371 } 3372 3373 bjtmp = bj + bi[i]; 3374 batmp = ba + bi[i]; 3375 /* apply level dropping rule to L part */ 3376 ncut = nzi_al + dtcount; 3377 if (ncut < nzi_bl){ 3378 ierr = PetscSortSplit(ncut,nzi_bl,vtmp,jtmp);CHKERRQ(ierr); 3379 ierr = PetscSortIntWithScalarArray(ncut,jtmp,vtmp);CHKERRQ(ierr); 3380 } else { 3381 ncut = nzi_bl; 3382 } 3383 for (j=0; j<ncut; j++){ 3384 bjtmp[j] = jtmp[j]; 3385 batmp[j] = vtmp[j]; 3386 /* printf(" (%d,%g),",bjtmp[j],batmp[j]); */ 3387 } 3388 bi[i+1] = bi[i] + ncut; 3389 nzi = ncut + 1; 3390 3391 /* apply level dropping rule to U part */ 3392 ncut = nzi_au + dtcount; 3393 if (ncut < nzi_bu){ 3394 ierr = PetscSortSplit(ncut,nzi_bu,vtmp+nzi_bl+1,jtmp+nzi_bl+1);CHKERRQ(ierr); 3395 ierr = PetscSortIntWithScalarArray(ncut,jtmp+nzi_bl+1,vtmp+nzi_bl+1);CHKERRQ(ierr); 3396 } else { 3397 ncut = nzi_bu; 3398 } 3399 nzi += ncut; 3400 3401 /* mark bdiagonal */ 3402 bdiag[i+1] = bdiag[i] - (ncut + 1); 3403 bdiag_rev[n-i-1] = bdiag[i+1]; 3404 bi[2*n - i] = bi[2*n - i +1] - (ncut + 1); 3405 bjtmp = bj + bdiag[i]; 3406 batmp = ba + bdiag[i]; 3407 *bjtmp = i; 3408 *batmp = diag_tmp; /* rtmp[i]; */ 3409 if (*batmp == 0.0) { 3410 *batmp = dt+shift; 3411 /* printf(" row %d add shift %g\n",i,shift); */ 3412 } 3413 *batmp = 1.0/(*batmp); /* invert diagonal entries for simplier triangular solves */ 3414 /* printf(" (%d,%g),",*bjtmp,*batmp); */ 3415 3416 bjtmp = bj + bdiag[i+1]+1; 3417 batmp = ba + bdiag[i+1]+1; 3418 for (k=0; k<ncut; k++){ 3419 bjtmp[k] = jtmp[nzi_bl+1+k]; 3420 batmp[k] = vtmp[nzi_bl+1+k]; 3421 /* printf(" (%d,%g),",bjtmp[k],batmp[k]); */ 3422 } 3423 /* printf("\n"); */ 3424 3425 im[i] = nzi; /* used by PetscLLAddSortedLU() */ 3426 /* 3427 printf("row %d: bi %d, bdiag %d\n",i,bi[i],bdiag[i]); 3428 printf(" ----------------------------\n"); 3429 */ 3430 } /* for (i=0; i<n; i++) */ 3431 /* printf("end of L %d, beginning of U %d\n",bi[n],bdiag[n]); */ 3432 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]); 3433 3434 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 3435 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 3436 3437 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 3438 ierr = PetscFree2(im,jtmp);CHKERRQ(ierr); 3439 ierr = PetscFree2(rtmp,vtmp);CHKERRQ(ierr); 3440 ierr = PetscFree(bdiag_rev);CHKERRQ(ierr); 3441 3442 ierr = PetscLogFlops(B->cmap->n);CHKERRQ(ierr); 3443 b->maxnz = b->nz = bi[n] + bdiag[0] - bdiag[n]; 3444 3445 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 3446 ierr = ISIdentity(isicol,&icol_identity);CHKERRQ(ierr); 3447 both_identity = (PetscTruth) (row_identity && icol_identity); 3448 if (row_identity && icol_identity) { 3449 B->ops->solve = MatSolve_SeqAIJ_NaturalOrdering_newdatastruct; 3450 } else { 3451 B->ops->solve = MatSolve_SeqAIJ_newdatastruct; 3452 } 3453 3454 B->ops->lufactorsymbolic = MatILUDTFactorSymbolic_SeqAIJ; 3455 B->ops->lufactornumeric = MatILUDTFactorNumeric_SeqAIJ; 3456 B->ops->solveadd = 0; 3457 B->ops->solvetranspose = 0; 3458 B->ops->solvetransposeadd = 0; 3459 B->ops->matsolve = 0; 3460 B->assembled = PETSC_TRUE; 3461 B->preallocated = PETSC_TRUE; 3462 PetscFunctionReturn(0); 3463 } 3464 3465 /* a wraper of MatILUDTFactor_SeqAIJ() */ 3466 #undef __FUNCT__ 3467 #define __FUNCT__ "MatILUDTFactorSymbolic_SeqAIJ" 3468 PetscErrorCode PETSCMAT_DLLEXPORT MatILUDTFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS row,IS col,const MatFactorInfo *info) 3469 { 3470 PetscErrorCode ierr; 3471 3472 PetscFunctionBegin; 3473 ierr = MatILUDTFactor_SeqAIJ(A,row,col,info,&fact);CHKERRQ(ierr); 3474 3475 fact->ops->lufactornumeric = MatILUDTFactorNumeric_SeqAIJ; 3476 PetscFunctionReturn(0); 3477 } 3478 3479 /* 3480 same as MatLUFactorNumeric_SeqAIJ(), except using contiguous array matrix factors 3481 - intend to replace existing MatLUFactorNumeric_SeqAIJ() 3482 */ 3483 #undef __FUNCT__ 3484 #define __FUNCT__ "MatILUDTFactorNumeric_SeqAIJ" 3485 PetscErrorCode PETSCMAT_DLLEXPORT MatILUDTFactorNumeric_SeqAIJ(Mat fact,Mat A,const MatFactorInfo *info) 3486 { 3487 Mat C=fact; 3488 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ *)C->data; 3489 IS isrow = b->row,isicol = b->icol; 3490 PetscErrorCode ierr; 3491 const PetscInt *r,*ic,*ics; 3492 PetscInt i,j,k,n=A->rmap->n,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j; 3493 PetscInt *ajtmp,*bjtmp,nz,nzl,nzu,row,*bdiag = b->diag,*pj; 3494 MatScalar *rtmp,*pc,multiplier,*v,*pv,*aa=a->a; 3495 PetscReal dt=info->dt,shift=info->shiftinblocks; 3496 PetscTruth row_identity, col_identity; 3497 3498 PetscFunctionBegin; 3499 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 3500 ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr); 3501 ierr = PetscMalloc((n+1)*sizeof(MatScalar),&rtmp);CHKERRQ(ierr); 3502 ics = ic; 3503 3504 for (i=0; i<n; i++){ 3505 /* initialize rtmp array */ 3506 nzl = bi[i+1] - bi[i]; /* num of nozeros in L(i,:) */ 3507 bjtmp = bj + bi[i]; 3508 for (j=0; j<nzl; j++) rtmp[*bjtmp++] = 0.0; 3509 rtmp[i] = 0.0; 3510 nzu = bdiag[i] - bdiag[i+1]; /* num of nozeros in U(i,:) */ 3511 bjtmp = bj + bdiag[i+1] + 1; 3512 for (j=0; j<nzu; j++) rtmp[*bjtmp++] = 0.0; 3513 3514 /* load in initial unfactored row of A */ 3515 /* printf("row %d\n",i); */ 3516 nz = ai[r[i]+1] - ai[r[i]]; 3517 ajtmp = aj + ai[r[i]]; 3518 v = aa + ai[r[i]]; 3519 for (j=0; j<nz; j++) { 3520 rtmp[ics[*ajtmp++]] = v[j]; 3521 /* printf(" (%d,%g),",ics[ajtmp[j]],rtmp[ics[ajtmp[j]]]); */ 3522 } 3523 /* printf("\n"); */ 3524 3525 /* numerical factorization */ 3526 bjtmp = bj + bi[i]; /* point to 1st entry of L(i,:) */ 3527 nzl = bi[i+1] - bi[i]; /* num of entries in L(i,:) */ 3528 k = 0; 3529 while (k < nzl){ 3530 row = *bjtmp++; 3531 /* printf(" prow %d\n",row); */ 3532 pc = rtmp + row; 3533 pv = b->a + bdiag[row]; /* 1./(diag of the pivot row) */ 3534 multiplier = (*pc) * (*pv); 3535 *pc = multiplier; 3536 if (PetscAbsScalar(multiplier) > dt){ 3537 pj = bj + bdiag[row+1] + 1; /* point to 1st entry of U(row,:) */ 3538 pv = b->a + bdiag[row+1] + 1; 3539 nz = bdiag[row] - bdiag[row+1] - 1; /* num of entries in U(row,:), excluding diagonal */ 3540 for (j=0; j<nz; j++) rtmp[*pj++] -= multiplier * (*pv++); 3541 /* ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr); */ 3542 } 3543 k++; 3544 } 3545 3546 /* finished row so stick it into b->a */ 3547 /* L-part */ 3548 pv = b->a + bi[i] ; 3549 pj = bj + bi[i] ; 3550 nzl = bi[i+1] - bi[i]; 3551 for (j=0; j<nzl; j++) { 3552 pv[j] = rtmp[pj[j]]; 3553 /* printf(" (%d,%g),",pj[j],pv[j]); */ 3554 } 3555 3556 /* diagonal: invert diagonal entries for simplier triangular solves */ 3557 if (rtmp[i] == 0.0) rtmp[i] = dt+shift; 3558 b->a[bdiag[i]] = 1.0/rtmp[i]; 3559 /* printf(" (%d,%g),",i,b->a[bdiag[i]]); */ 3560 3561 /* U-part */ 3562 pv = b->a + bdiag[i+1] + 1; 3563 pj = bj + bdiag[i+1] + 1; 3564 nzu = bdiag[i] - bdiag[i+1] - 1; 3565 for (j=0; j<nzu; j++) { 3566 pv[j] = rtmp[pj[j]]; 3567 /* printf(" (%d,%g),",pj[j],pv[j]); */ 3568 } 3569 /* printf("\n"); */ 3570 } 3571 3572 ierr = PetscFree(rtmp);CHKERRQ(ierr); 3573 ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr); 3574 ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr); 3575 3576 ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr); 3577 ierr = ISIdentity(isicol,&col_identity);CHKERRQ(ierr); 3578 if (row_identity && col_identity) { 3579 C->ops->solve = MatSolve_SeqAIJ_NaturalOrdering_newdatastruct; 3580 } else { 3581 C->ops->solve = MatSolve_SeqAIJ_newdatastruct; 3582 } 3583 C->ops->solveadd = 0; 3584 C->ops->solvetranspose = 0; 3585 C->ops->solvetransposeadd = 0; 3586 C->ops->matsolve = 0; 3587 C->assembled = PETSC_TRUE; 3588 C->preallocated = PETSC_TRUE; 3589 ierr = PetscLogFlops(C->cmap->n);CHKERRQ(ierr); 3590 PetscFunctionReturn(0); 3591 } 3592