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