1 2 #include <../src/mat/impls/baij/seq/baij.h> 3 #include <../src/mat/impls/sbaij/seq/sbaij.h> 4 #include <petsc/private/kernels/blockinvert.h> 5 #include <petscis.h> 6 7 /* 8 input: 9 F -- numeric factor 10 output: 11 nneg, nzero, npos: matrix inertia 12 */ 13 14 #undef __FUNCT__ 15 #define __FUNCT__ "MatGetInertia_SeqSBAIJ" 16 PetscErrorCode MatGetInertia_SeqSBAIJ(Mat F,PetscInt *nneig,PetscInt *nzero,PetscInt *npos) 17 { 18 Mat_SeqSBAIJ *fact_ptr = (Mat_SeqSBAIJ*)F->data; 19 MatScalar *dd = fact_ptr->a; 20 PetscInt mbs =fact_ptr->mbs,bs=F->rmap->bs,i,nneig_tmp,npos_tmp,*fi = fact_ptr->diag; 21 22 PetscFunctionBegin; 23 if (bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for bs: %D >1 yet",bs); 24 nneig_tmp = 0; npos_tmp = 0; 25 for (i=0; i<mbs; i++) { 26 if (PetscRealPart(dd[*fi]) > 0.0) npos_tmp++; 27 else if (PetscRealPart(dd[*fi]) < 0.0) nneig_tmp++; 28 fi++; 29 } 30 if (nneig) *nneig = nneig_tmp; 31 if (npos) *npos = npos_tmp; 32 if (nzero) *nzero = mbs - nneig_tmp - npos_tmp; 33 PetscFunctionReturn(0); 34 } 35 36 /* 37 Symbolic U^T*D*U factorization for SBAIJ format. Modified from SSF of YSMP. 38 Use Modified Sparse Row (MSR) storage for u and ju. See page 85, "Iterative Methods ..." by Saad. 39 */ 40 #undef __FUNCT__ 41 #define __FUNCT__ "MatCholeskyFactorSymbolic_SeqSBAIJ_MSR" 42 PetscErrorCode MatCholeskyFactorSymbolic_SeqSBAIJ_MSR(Mat F,Mat A,IS perm,const MatFactorInfo *info) 43 { 44 Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data,*b; 45 PetscErrorCode ierr; 46 const PetscInt *rip,*ai,*aj; 47 PetscInt i,mbs = a->mbs,*jutmp,bs = A->rmap->bs,bs2=a->bs2; 48 PetscInt m,reallocs = 0,prow; 49 PetscInt *jl,*q,jmin,jmax,juidx,nzk,qm,*iu,*ju,k,j,vj,umax,maxadd; 50 PetscReal f = info->fill; 51 PetscBool perm_identity; 52 53 PetscFunctionBegin; 54 /* check whether perm is the identity mapping */ 55 ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 56 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 57 58 if (perm_identity) { /* without permutation */ 59 a->permute = PETSC_FALSE; 60 61 ai = a->i; aj = a->j; 62 } else { /* non-trivial permutation */ 63 a->permute = PETSC_TRUE; 64 65 ierr = MatReorderingSeqSBAIJ(A,perm);CHKERRQ(ierr); 66 67 ai = a->inew; aj = a->jnew; 68 } 69 70 /* initialization */ 71 ierr = PetscMalloc1(mbs+1,&iu);CHKERRQ(ierr); 72 umax = (PetscInt)(f*ai[mbs] + 1); umax += mbs + 1; 73 ierr = PetscMalloc1(umax,&ju);CHKERRQ(ierr); 74 iu[0] = mbs+1; 75 juidx = mbs + 1; /* index for ju */ 76 /* jl linked list for pivot row -- linked list for col index */ 77 ierr = PetscMalloc2(mbs,&jl,mbs,&q);CHKERRQ(ierr); 78 for (i=0; i<mbs; i++) { 79 jl[i] = mbs; 80 q[i] = 0; 81 } 82 83 /* for each row k */ 84 for (k=0; k<mbs; k++) { 85 for (i=0; i<mbs; i++) q[i] = 0; /* to be removed! */ 86 nzk = 0; /* num. of nz blocks in k-th block row with diagonal block excluded */ 87 q[k] = mbs; 88 /* initialize nonzero structure of k-th row to row rip[k] of A */ 89 jmin = ai[rip[k]] +1; /* exclude diag[k] */ 90 jmax = ai[rip[k]+1]; 91 for (j=jmin; j<jmax; j++) { 92 vj = rip[aj[j]]; /* col. value */ 93 if (vj > k) { 94 qm = k; 95 do { 96 m = qm; qm = q[m]; 97 } while (qm < vj); 98 if (qm == vj) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Duplicate entry in A\n"); 99 nzk++; 100 q[m] = vj; 101 q[vj] = qm; 102 } /* if (vj > k) */ 103 } /* for (j=jmin; j<jmax; j++) */ 104 105 /* modify nonzero structure of k-th row by computing fill-in 106 for each row i to be merged in */ 107 prow = k; 108 prow = jl[prow]; /* next pivot row (== mbs for symbolic factorization) */ 109 110 while (prow < k) { 111 /* merge row prow into k-th row */ 112 jmin = iu[prow] + 1; jmax = iu[prow+1]; 113 qm = k; 114 for (j=jmin; j<jmax; j++) { 115 vj = ju[j]; 116 do { 117 m = qm; qm = q[m]; 118 } while (qm < vj); 119 if (qm != vj) { 120 nzk++; q[m] = vj; q[vj] = qm; qm = vj; 121 } 122 } 123 prow = jl[prow]; /* next pivot row */ 124 } 125 126 /* add k to row list for first nonzero element in k-th row */ 127 if (nzk > 0) { 128 i = q[k]; /* col value of first nonzero element in U(k, k+1:mbs-1) */ 129 jl[k] = jl[i]; jl[i] = k; 130 } 131 iu[k+1] = iu[k] + nzk; 132 133 /* allocate more space to ju if needed */ 134 if (iu[k+1] > umax) { 135 /* estimate how much additional space we will need */ 136 /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */ 137 /* just double the memory each time */ 138 maxadd = umax; 139 if (maxadd < nzk) maxadd = (mbs-k)*(nzk+1)/2; 140 umax += maxadd; 141 142 /* allocate a longer ju */ 143 ierr = PetscMalloc1(umax,&jutmp);CHKERRQ(ierr); 144 ierr = PetscMemcpy(jutmp,ju,iu[k]*sizeof(PetscInt));CHKERRQ(ierr); 145 ierr = PetscFree(ju);CHKERRQ(ierr); 146 ju = jutmp; 147 reallocs++; /* count how many times we realloc */ 148 } 149 150 /* save nonzero structure of k-th row in ju */ 151 i=k; 152 while (nzk--) { 153 i = q[i]; 154 ju[juidx++] = i; 155 } 156 } 157 158 #if defined(PETSC_USE_INFO) 159 if (ai[mbs] != 0) { 160 PetscReal af = ((PetscReal)iu[mbs])/((PetscReal)ai[mbs]); 161 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %g needed %g\n",reallocs,(double)f,(double)af);CHKERRQ(ierr); 162 ierr = PetscInfo1(A,"Run with -pc_factor_fill %g or use \n",(double)af);CHKERRQ(ierr); 163 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%g);\n",(double)af);CHKERRQ(ierr); 164 ierr = PetscInfo(A,"for best performance.\n");CHKERRQ(ierr); 165 } else { 166 ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr); 167 } 168 #endif 169 170 ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); 171 ierr = PetscFree2(jl,q);CHKERRQ(ierr); 172 173 /* put together the new matrix */ 174 ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(F,bs,MAT_SKIP_ALLOCATION,NULL);CHKERRQ(ierr); 175 176 /* ierr = PetscLogObjectParent((PetscObject)B,(PetscObject)iperm);CHKERRQ(ierr); */ 177 b = (Mat_SeqSBAIJ*)(F)->data; 178 b->singlemalloc = PETSC_FALSE; 179 b->free_a = PETSC_TRUE; 180 b->free_ij = PETSC_TRUE; 181 182 ierr = PetscMalloc1((iu[mbs]+1)*bs2,&b->a);CHKERRQ(ierr); 183 b->j = ju; 184 b->i = iu; 185 b->diag = 0; 186 b->ilen = 0; 187 b->imax = 0; 188 b->row = perm; 189 190 b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ 191 192 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 193 194 b->icol = perm; 195 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 196 ierr = PetscMalloc1(bs*mbs+bs,&b->solve_work);CHKERRQ(ierr); 197 /* In b structure: Free imax, ilen, old a, old j. 198 Allocate idnew, solve_work, new a, new j */ 199 ierr = PetscLogObjectMemory((PetscObject)F,(iu[mbs]-mbs)*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 200 b->maxnz = b->nz = iu[mbs]; 201 202 (F)->info.factor_mallocs = reallocs; 203 (F)->info.fill_ratio_given = f; 204 if (ai[mbs] != 0) { 205 (F)->info.fill_ratio_needed = ((PetscReal)iu[mbs])/((PetscReal)ai[mbs]); 206 } else { 207 (F)->info.fill_ratio_needed = 0.0; 208 } 209 ierr = MatSeqSBAIJSetNumericFactorization_inplace(F,perm_identity);CHKERRQ(ierr); 210 PetscFunctionReturn(0); 211 } 212 /* 213 Symbolic U^T*D*U factorization for SBAIJ format. 214 See MatICCFactorSymbolic_SeqAIJ() for description of its data structure. 215 */ 216 #include <petscbt.h> 217 #include <../src/mat/utils/freespace.h> 218 #undef __FUNCT__ 219 #define __FUNCT__ "MatCholeskyFactorSymbolic_SeqSBAIJ" 220 PetscErrorCode MatCholeskyFactorSymbolic_SeqSBAIJ(Mat fact,Mat A,IS perm,const MatFactorInfo *info) 221 { 222 Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data; 223 Mat_SeqSBAIJ *b; 224 PetscErrorCode ierr; 225 PetscBool perm_identity,missing; 226 PetscReal fill = info->fill; 227 const PetscInt *rip,*ai=a->i,*aj=a->j; 228 PetscInt i,mbs=a->mbs,bs=A->rmap->bs,reallocs=0,prow; 229 PetscInt *jl,jmin,jmax,nzk,*ui,k,j,*il,nextprow; 230 PetscInt nlnk,*lnk,ncols,*cols,*uj,**ui_ptr,*uj_ptr,*udiag; 231 PetscFreeSpaceList free_space=NULL,current_space=NULL; 232 PetscBT lnkbt; 233 234 PetscFunctionBegin; 235 if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n); 236 ierr = MatMissingDiagonal(A,&missing,&i);CHKERRQ(ierr); 237 if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",i); 238 if (bs > 1) { 239 ierr = MatCholeskyFactorSymbolic_SeqSBAIJ_inplace(fact,A,perm,info);CHKERRQ(ierr); 240 PetscFunctionReturn(0); 241 } 242 243 /* check whether perm is the identity mapping */ 244 ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 245 if (!perm_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix reordering is not supported for sbaij matrix. Use aij format"); 246 a->permute = PETSC_FALSE; 247 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 248 249 /* initialization */ 250 ierr = PetscMalloc1(mbs+1,&ui);CHKERRQ(ierr); 251 ierr = PetscMalloc1(mbs+1,&udiag);CHKERRQ(ierr); 252 ui[0] = 0; 253 254 /* jl: linked list for storing indices of the pivot rows 255 il: il[i] points to the 1st nonzero entry of U(i,k:mbs-1) */ 256 ierr = PetscMalloc4(mbs,&ui_ptr,mbs,&il,mbs,&jl,mbs,&cols);CHKERRQ(ierr); 257 for (i=0; i<mbs; i++) { 258 jl[i] = mbs; il[i] = 0; 259 } 260 261 /* create and initialize a linked list for storing column indices of the active row k */ 262 nlnk = mbs + 1; 263 ierr = PetscLLCreate(mbs,mbs,nlnk,lnk,lnkbt);CHKERRQ(ierr); 264 265 /* initial FreeSpace size is fill*(ai[mbs]+1) */ 266 ierr = PetscFreeSpaceGet(PetscRealIntMultTruncate(fill,ai[mbs]+1),&free_space);CHKERRQ(ierr); 267 current_space = free_space; 268 269 for (k=0; k<mbs; k++) { /* for each active row k */ 270 /* initialize lnk by the column indices of row rip[k] of A */ 271 nzk = 0; 272 ncols = ai[k+1] - ai[k]; 273 if (!ncols) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MAT_CH_ZRPVT,"Empty row %D in matrix ",k); 274 for (j=0; j<ncols; j++) { 275 i = *(aj + ai[k] + j); 276 cols[j] = i; 277 } 278 ierr = PetscLLAdd(ncols,cols,mbs,nlnk,lnk,lnkbt);CHKERRQ(ierr); 279 nzk += nlnk; 280 281 /* update lnk by computing fill-in for each pivot row to be merged in */ 282 prow = jl[k]; /* 1st pivot row */ 283 284 while (prow < k) { 285 nextprow = jl[prow]; 286 /* merge prow into k-th row */ 287 jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:mbs-1) */ 288 jmax = ui[prow+1]; 289 ncols = jmax-jmin; 290 uj_ptr = ui_ptr[prow] + jmin - ui[prow]; /* points to the 2nd nzero entry in U(prow,k:mbs-1) */ 291 ierr = PetscLLAddSorted(ncols,uj_ptr,mbs,nlnk,lnk,lnkbt);CHKERRQ(ierr); 292 nzk += nlnk; 293 294 /* update il and jl for prow */ 295 if (jmin < jmax) { 296 il[prow] = jmin; 297 j = *uj_ptr; jl[prow] = jl[j]; jl[j] = prow; 298 } 299 prow = nextprow; 300 } 301 302 /* if free space is not available, make more free space */ 303 if (current_space->local_remaining<nzk) { 304 i = mbs - k + 1; /* num of unfactored rows */ 305 i = PetscIntMultTruncate(i,PetscMin(nzk, i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */ 306 ierr = PetscFreeSpaceGet(i,¤t_space);CHKERRQ(ierr); 307 reallocs++; 308 } 309 310 /* copy data into free space, then initialize lnk */ 311 ierr = PetscLLClean(mbs,mbs,nzk,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 312 313 /* add the k-th row into il and jl */ 314 if (nzk > 1) { 315 i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:mbs-1) */ 316 jl[k] = jl[i]; jl[i] = k; 317 il[k] = ui[k] + 1; 318 } 319 ui_ptr[k] = current_space->array; 320 321 current_space->array += nzk; 322 current_space->local_used += nzk; 323 current_space->local_remaining -= nzk; 324 325 ui[k+1] = ui[k] + nzk; 326 } 327 328 ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); 329 ierr = PetscFree4(ui_ptr,il,jl,cols);CHKERRQ(ierr); 330 331 /* destroy list of free space and other temporary array(s) */ 332 ierr = PetscMalloc1(ui[mbs]+1,&uj);CHKERRQ(ierr); 333 ierr = PetscFreeSpaceContiguous_Cholesky(&free_space,uj,mbs,ui,udiag);CHKERRQ(ierr); /* store matrix factor */ 334 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 335 336 /* put together the new matrix in MATSEQSBAIJ format */ 337 ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(fact,bs,MAT_SKIP_ALLOCATION,NULL);CHKERRQ(ierr); 338 339 b = (Mat_SeqSBAIJ*)fact->data; 340 b->singlemalloc = PETSC_FALSE; 341 b->free_a = PETSC_TRUE; 342 b->free_ij = PETSC_TRUE; 343 344 ierr = PetscMalloc1(ui[mbs]+1,&b->a);CHKERRQ(ierr); 345 346 b->j = uj; 347 b->i = ui; 348 b->diag = udiag; 349 b->free_diag = PETSC_TRUE; 350 b->ilen = 0; 351 b->imax = 0; 352 b->row = perm; 353 b->icol = perm; 354 355 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 356 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 357 358 b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ 359 360 ierr = PetscMalloc1(mbs+1,&b->solve_work);CHKERRQ(ierr); 361 ierr = PetscLogObjectMemory((PetscObject)fact,ui[mbs]*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 362 363 b->maxnz = b->nz = ui[mbs]; 364 365 fact->info.factor_mallocs = reallocs; 366 fact->info.fill_ratio_given = fill; 367 if (ai[mbs] != 0) { 368 fact->info.fill_ratio_needed = ((PetscReal)ui[mbs])/ai[mbs]; 369 } else { 370 fact->info.fill_ratio_needed = 0.0; 371 } 372 #if defined(PETSC_USE_INFO) 373 if (ai[mbs] != 0) { 374 PetscReal af = fact->info.fill_ratio_needed; 375 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %g needed %g\n",reallocs,(double)fill,(double)af);CHKERRQ(ierr); 376 ierr = PetscInfo1(A,"Run with -pc_factor_fill %g or use \n",(double)af);CHKERRQ(ierr); 377 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%g) for best performance.\n",(double)af);CHKERRQ(ierr); 378 } else { 379 ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr); 380 } 381 #endif 382 fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering; 383 PetscFunctionReturn(0); 384 } 385 386 #undef __FUNCT__ 387 #define __FUNCT__ "MatCholeskyFactorSymbolic_SeqSBAIJ_inplace" 388 PetscErrorCode MatCholeskyFactorSymbolic_SeqSBAIJ_inplace(Mat fact,Mat A,IS perm,const MatFactorInfo *info) 389 { 390 Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data; 391 Mat_SeqSBAIJ *b; 392 PetscErrorCode ierr; 393 PetscBool perm_identity,missing; 394 PetscReal fill = info->fill; 395 const PetscInt *rip,*ai,*aj; 396 PetscInt i,mbs=a->mbs,bs=A->rmap->bs,reallocs=0,prow,d; 397 PetscInt *jl,jmin,jmax,nzk,*ui,k,j,*il,nextprow; 398 PetscInt nlnk,*lnk,ncols,*cols,*uj,**ui_ptr,*uj_ptr; 399 PetscFreeSpaceList free_space=NULL,current_space=NULL; 400 PetscBT lnkbt; 401 402 PetscFunctionBegin; 403 ierr = MatMissingDiagonal(A,&missing,&d);CHKERRQ(ierr); 404 if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d); 405 406 /* 407 This code originally uses Modified Sparse Row (MSR) storage 408 (see page 85, "Iterative Methods ..." by Saad) for the output matrix B - bad choise! 409 Then it is rewritten so the factor B takes seqsbaij format. However the associated 410 MatCholeskyFactorNumeric_() have not been modified for the cases of bs>1 or !perm_identity, 411 thus the original code in MSR format is still used for these cases. 412 The code below should replace MatCholeskyFactorSymbolic_SeqSBAIJ_MSR() whenever 413 MatCholeskyFactorNumeric_() is modified for using sbaij symbolic factor. 414 */ 415 if (bs > 1) { 416 ierr = MatCholeskyFactorSymbolic_SeqSBAIJ_MSR(fact,A,perm,info);CHKERRQ(ierr); 417 PetscFunctionReturn(0); 418 } 419 420 /* check whether perm is the identity mapping */ 421 ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); 422 423 if (perm_identity) { 424 a->permute = PETSC_FALSE; 425 426 ai = a->i; aj = a->j; 427 } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix reordering is not supported for sbaij matrix. Use aij format"); 428 ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); 429 430 /* initialization */ 431 ierr = PetscMalloc1(mbs+1,&ui);CHKERRQ(ierr); 432 ui[0] = 0; 433 434 /* jl: linked list for storing indices of the pivot rows 435 il: il[i] points to the 1st nonzero entry of U(i,k:mbs-1) */ 436 ierr = PetscMalloc4(mbs,&ui_ptr,mbs,&il,mbs,&jl,mbs,&cols);CHKERRQ(ierr); 437 for (i=0; i<mbs; i++) { 438 jl[i] = mbs; il[i] = 0; 439 } 440 441 /* create and initialize a linked list for storing column indices of the active row k */ 442 nlnk = mbs + 1; 443 ierr = PetscLLCreate(mbs,mbs,nlnk,lnk,lnkbt);CHKERRQ(ierr); 444 445 /* initial FreeSpace size is fill*(ai[mbs]+1) */ 446 ierr = PetscFreeSpaceGet(PetscRealIntMultTruncate(fill,ai[mbs]+1),&free_space);CHKERRQ(ierr); 447 current_space = free_space; 448 449 for (k=0; k<mbs; k++) { /* for each active row k */ 450 /* initialize lnk by the column indices of row rip[k] of A */ 451 nzk = 0; 452 ncols = ai[rip[k]+1] - ai[rip[k]]; 453 for (j=0; j<ncols; j++) { 454 i = *(aj + ai[rip[k]] + j); 455 cols[j] = rip[i]; 456 } 457 ierr = PetscLLAdd(ncols,cols,mbs,nlnk,lnk,lnkbt);CHKERRQ(ierr); 458 nzk += nlnk; 459 460 /* update lnk by computing fill-in for each pivot row to be merged in */ 461 prow = jl[k]; /* 1st pivot row */ 462 463 while (prow < k) { 464 nextprow = jl[prow]; 465 /* merge prow into k-th row */ 466 jmin = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:mbs-1) */ 467 jmax = ui[prow+1]; 468 ncols = jmax-jmin; 469 uj_ptr = ui_ptr[prow] + jmin - ui[prow]; /* points to the 2nd nzero entry in U(prow,k:mbs-1) */ 470 ierr = PetscLLAddSorted(ncols,uj_ptr,mbs,nlnk,lnk,lnkbt);CHKERRQ(ierr); 471 nzk += nlnk; 472 473 /* update il and jl for prow */ 474 if (jmin < jmax) { 475 il[prow] = jmin; 476 477 j = *uj_ptr; jl[prow] = jl[j]; jl[j] = prow; 478 } 479 prow = nextprow; 480 } 481 482 /* if free space is not available, make more free space */ 483 if (current_space->local_remaining<nzk) { 484 i = mbs - k + 1; /* num of unfactored rows */ 485 i = PetscMin(PetscIntMultTruncate(i,nzk), PetscIntMultTruncate(i,i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */ 486 ierr = PetscFreeSpaceGet(i,¤t_space);CHKERRQ(ierr); 487 reallocs++; 488 } 489 490 /* copy data into free space, then initialize lnk */ 491 ierr = PetscLLClean(mbs,mbs,nzk,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 492 493 /* add the k-th row into il and jl */ 494 if (nzk-1 > 0) { 495 i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:mbs-1) */ 496 jl[k] = jl[i]; jl[i] = k; 497 il[k] = ui[k] + 1; 498 } 499 ui_ptr[k] = current_space->array; 500 501 current_space->array += nzk; 502 current_space->local_used += nzk; 503 current_space->local_remaining -= nzk; 504 505 ui[k+1] = ui[k] + nzk; 506 } 507 508 ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); 509 ierr = PetscFree4(ui_ptr,il,jl,cols);CHKERRQ(ierr); 510 511 /* destroy list of free space and other temporary array(s) */ 512 ierr = PetscMalloc1(ui[mbs]+1,&uj);CHKERRQ(ierr); 513 ierr = PetscFreeSpaceContiguous(&free_space,uj);CHKERRQ(ierr); 514 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 515 516 /* put together the new matrix in MATSEQSBAIJ format */ 517 ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(fact,bs,MAT_SKIP_ALLOCATION,NULL);CHKERRQ(ierr); 518 519 b = (Mat_SeqSBAIJ*)fact->data; 520 b->singlemalloc = PETSC_FALSE; 521 b->free_a = PETSC_TRUE; 522 b->free_ij = PETSC_TRUE; 523 524 ierr = PetscMalloc1(ui[mbs]+1,&b->a);CHKERRQ(ierr); 525 526 b->j = uj; 527 b->i = ui; 528 b->diag = 0; 529 b->ilen = 0; 530 b->imax = 0; 531 b->row = perm; 532 533 b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ 534 535 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 536 b->icol = perm; 537 ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); 538 ierr = PetscMalloc1(mbs+1,&b->solve_work);CHKERRQ(ierr); 539 ierr = PetscLogObjectMemory((PetscObject)fact,(ui[mbs]-mbs)*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr); 540 b->maxnz = b->nz = ui[mbs]; 541 542 fact->info.factor_mallocs = reallocs; 543 fact->info.fill_ratio_given = fill; 544 if (ai[mbs] != 0) { 545 fact->info.fill_ratio_needed = ((PetscReal)ui[mbs])/ai[mbs]; 546 } else { 547 fact->info.fill_ratio_needed = 0.0; 548 } 549 #if defined(PETSC_USE_INFO) 550 if (ai[mbs] != 0) { 551 PetscReal af = fact->info.fill_ratio_needed; 552 ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %g needed %g\n",reallocs,(double)fill,(double)af);CHKERRQ(ierr); 553 ierr = PetscInfo1(A,"Run with -pc_factor_fill %g or use \n",(double)af);CHKERRQ(ierr); 554 ierr = PetscInfo1(A,"PCFactorSetFill(pc,%g) for best performance.\n",(double)af);CHKERRQ(ierr); 555 } else { 556 ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr); 557 } 558 #endif 559 ierr = MatSeqSBAIJSetNumericFactorization_inplace(fact,perm_identity);CHKERRQ(ierr); 560 PetscFunctionReturn(0); 561 } 562 563 #undef __FUNCT__ 564 #define __FUNCT__ "MatCholeskyFactorNumeric_SeqSBAIJ_N" 565 PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_N(Mat C,Mat A,const MatFactorInfo *info) 566 { 567 Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data,*b = (Mat_SeqSBAIJ*)C->data; 568 IS perm = b->row; 569 PetscErrorCode ierr; 570 const PetscInt *ai,*aj,*perm_ptr,mbs=a->mbs,*bi=b->i,*bj=b->j; 571 PetscInt i,j; 572 PetscInt *a2anew,k,k1,jmin,jmax,*jl,*il,vj,nexti,ili; 573 PetscInt bs =A->rmap->bs,bs2 = a->bs2; 574 MatScalar *ba = b->a,*aa,*ap,*dk,*uik; 575 MatScalar *u,*diag,*rtmp,*rtmp_ptr; 576 MatScalar *work; 577 PetscInt *pivots; 578 579 PetscFunctionBegin; 580 /* initialization */ 581 ierr = PetscCalloc1(bs2*mbs,&rtmp);CHKERRQ(ierr); 582 ierr = PetscMalloc2(mbs,&il,mbs,&jl);CHKERRQ(ierr); 583 for (i=0; i<mbs; i++) { 584 jl[i] = mbs; il[0] = 0; 585 } 586 ierr = PetscMalloc3(bs2,&dk,bs2,&uik,bs,&work);CHKERRQ(ierr); 587 ierr = PetscMalloc1(bs,&pivots);CHKERRQ(ierr); 588 589 ierr = ISGetIndices(perm,&perm_ptr);CHKERRQ(ierr); 590 591 /* check permutation */ 592 if (!a->permute) { 593 ai = a->i; aj = a->j; aa = a->a; 594 } else { 595 ai = a->inew; aj = a->jnew; 596 ierr = PetscMalloc1(bs2*ai[mbs],&aa);CHKERRQ(ierr); 597 ierr = PetscMemcpy(aa,a->a,bs2*ai[mbs]*sizeof(MatScalar));CHKERRQ(ierr); 598 ierr = PetscMalloc1(ai[mbs],&a2anew);CHKERRQ(ierr); 599 ierr = PetscMemcpy(a2anew,a->a2anew,(ai[mbs])*sizeof(PetscInt));CHKERRQ(ierr); 600 601 for (i=0; i<mbs; i++) { 602 jmin = ai[i]; jmax = ai[i+1]; 603 for (j=jmin; j<jmax; j++) { 604 while (a2anew[j] != j) { 605 k = a2anew[j]; a2anew[j] = a2anew[k]; a2anew[k] = k; 606 for (k1=0; k1<bs2; k1++) { 607 dk[k1] = aa[k*bs2+k1]; 608 aa[k*bs2+k1] = aa[j*bs2+k1]; 609 aa[j*bs2+k1] = dk[k1]; 610 } 611 } 612 /* transform columnoriented blocks that lie in the lower triangle to roworiented blocks */ 613 if (i > aj[j]) { 614 /* printf("change orientation, row: %d, col: %d\n",i,aj[j]); */ 615 ap = aa + j*bs2; /* ptr to the beginning of j-th block of aa */ 616 for (k=0; k<bs2; k++) dk[k] = ap[k]; /* dk <- j-th block of aa */ 617 for (k=0; k<bs; k++) { /* j-th block of aa <- dk^T */ 618 for (k1=0; k1<bs; k1++) *ap++ = dk[k + bs*k1]; 619 } 620 } 621 } 622 } 623 ierr = PetscFree(a2anew);CHKERRQ(ierr); 624 } 625 626 /* for each row k */ 627 for (k = 0; k<mbs; k++) { 628 629 /*initialize k-th row with elements nonzero in row perm(k) of A */ 630 jmin = ai[perm_ptr[k]]; jmax = ai[perm_ptr[k]+1]; 631 632 ap = aa + jmin*bs2; 633 for (j = jmin; j < jmax; j++) { 634 vj = perm_ptr[aj[j]]; /* block col. index */ 635 rtmp_ptr = rtmp + vj*bs2; 636 for (i=0; i<bs2; i++) *rtmp_ptr++ = *ap++; 637 } 638 639 /* modify k-th row by adding in those rows i with U(i,k) != 0 */ 640 ierr = PetscMemcpy(dk,rtmp+k*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr); 641 i = jl[k]; /* first row to be added to k_th row */ 642 643 while (i < k) { 644 nexti = jl[i]; /* next row to be added to k_th row */ 645 646 /* compute multiplier */ 647 ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */ 648 649 /* uik = -inv(Di)*U_bar(i,k) */ 650 diag = ba + i*bs2; 651 u = ba + ili*bs2; 652 ierr = PetscMemzero(uik,bs2*sizeof(MatScalar));CHKERRQ(ierr); 653 PetscKernel_A_gets_A_minus_B_times_C(bs,uik,diag,u); 654 655 /* update D(k) += -U(i,k)^T * U_bar(i,k) */ 656 PetscKernel_A_gets_A_plus_Btranspose_times_C(bs,dk,uik,u); 657 ierr = PetscLogFlops(4.0*bs*bs2);CHKERRQ(ierr); 658 659 /* update -U(i,k) */ 660 ierr = PetscMemcpy(ba+ili*bs2,uik,bs2*sizeof(MatScalar));CHKERRQ(ierr); 661 662 /* add multiple of row i to k-th row ... */ 663 jmin = ili + 1; jmax = bi[i+1]; 664 if (jmin < jmax) { 665 for (j=jmin; j<jmax; j++) { 666 /* rtmp += -U(i,k)^T * U_bar(i,j) */ 667 rtmp_ptr = rtmp + bj[j]*bs2; 668 u = ba + j*bs2; 669 PetscKernel_A_gets_A_plus_Btranspose_times_C(bs,rtmp_ptr,uik,u); 670 } 671 ierr = PetscLogFlops(2.0*bs*bs2*(jmax-jmin));CHKERRQ(ierr); 672 673 /* ... add i to row list for next nonzero entry */ 674 il[i] = jmin; /* update il(i) in column k+1, ... mbs-1 */ 675 j = bj[jmin]; 676 jl[i] = jl[j]; jl[j] = i; /* update jl */ 677 } 678 i = nexti; 679 } 680 681 /* save nonzero entries in k-th row of U ... */ 682 683 /* invert diagonal block */ 684 diag = ba+k*bs2; 685 ierr = PetscMemcpy(diag,dk,bs2*sizeof(MatScalar));CHKERRQ(ierr); 686 ierr = PetscKernel_A_gets_inverse_A(bs,diag,pivots,work);CHKERRQ(ierr); 687 688 jmin = bi[k]; jmax = bi[k+1]; 689 if (jmin < jmax) { 690 for (j=jmin; j<jmax; j++) { 691 vj = bj[j]; /* block col. index of U */ 692 u = ba + j*bs2; 693 rtmp_ptr = rtmp + vj*bs2; 694 for (k1=0; k1<bs2; k1++) { 695 *u++ = *rtmp_ptr; 696 *rtmp_ptr++ = 0.0; 697 } 698 } 699 700 /* ... add k to row list for first nonzero entry in k-th row */ 701 il[k] = jmin; 702 i = bj[jmin]; 703 jl[k] = jl[i]; jl[i] = k; 704 } 705 } 706 707 ierr = PetscFree(rtmp);CHKERRQ(ierr); 708 ierr = PetscFree2(il,jl);CHKERRQ(ierr); 709 ierr = PetscFree3(dk,uik,work);CHKERRQ(ierr); 710 ierr = PetscFree(pivots);CHKERRQ(ierr); 711 if (a->permute) { 712 ierr = PetscFree(aa);CHKERRQ(ierr); 713 } 714 715 ierr = ISRestoreIndices(perm,&perm_ptr);CHKERRQ(ierr); 716 717 C->ops->solve = MatSolve_SeqSBAIJ_N_inplace; 718 C->ops->solvetranspose = MatSolve_SeqSBAIJ_N_inplace; 719 C->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_N_inplace; 720 C->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_N_inplace; 721 722 C->assembled = PETSC_TRUE; 723 C->preallocated = PETSC_TRUE; 724 725 ierr = PetscLogFlops(1.3333*bs*bs2*b->mbs);CHKERRQ(ierr); /* from inverting diagonal blocks */ 726 PetscFunctionReturn(0); 727 } 728 729 #undef __FUNCT__ 730 #define __FUNCT__ "MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering" 731 PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering(Mat C,Mat A,const MatFactorInfo *info) 732 { 733 Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data,*b = (Mat_SeqSBAIJ*)C->data; 734 PetscErrorCode ierr; 735 PetscInt i,j,mbs=a->mbs,*bi=b->i,*bj=b->j; 736 PetscInt *ai,*aj,k,k1,jmin,jmax,*jl,*il,vj,nexti,ili; 737 PetscInt bs =A->rmap->bs,bs2 = a->bs2; 738 MatScalar *ba = b->a,*aa,*ap,*dk,*uik; 739 MatScalar *u,*diag,*rtmp,*rtmp_ptr; 740 MatScalar *work; 741 PetscInt *pivots; 742 743 PetscFunctionBegin; 744 ierr = PetscCalloc1(bs2*mbs,&rtmp);CHKERRQ(ierr); 745 ierr = PetscMalloc2(mbs,&il,mbs,&jl);CHKERRQ(ierr); 746 for (i=0; i<mbs; i++) { 747 jl[i] = mbs; il[0] = 0; 748 } 749 ierr = PetscMalloc3(bs2,&dk,bs2,&uik,bs,&work);CHKERRQ(ierr); 750 ierr = PetscMalloc1(bs,&pivots);CHKERRQ(ierr); 751 752 ai = a->i; aj = a->j; aa = a->a; 753 754 /* for each row k */ 755 for (k = 0; k<mbs; k++) { 756 757 /*initialize k-th row with elements nonzero in row k of A */ 758 jmin = ai[k]; jmax = ai[k+1]; 759 ap = aa + jmin*bs2; 760 for (j = jmin; j < jmax; j++) { 761 vj = aj[j]; /* block col. index */ 762 rtmp_ptr = rtmp + vj*bs2; 763 for (i=0; i<bs2; i++) *rtmp_ptr++ = *ap++; 764 } 765 766 /* modify k-th row by adding in those rows i with U(i,k) != 0 */ 767 ierr = PetscMemcpy(dk,rtmp+k*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr); 768 i = jl[k]; /* first row to be added to k_th row */ 769 770 while (i < k) { 771 nexti = jl[i]; /* next row to be added to k_th row */ 772 773 /* compute multiplier */ 774 ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */ 775 776 /* uik = -inv(Di)*U_bar(i,k) */ 777 diag = ba + i*bs2; 778 u = ba + ili*bs2; 779 ierr = PetscMemzero(uik,bs2*sizeof(MatScalar));CHKERRQ(ierr); 780 PetscKernel_A_gets_A_minus_B_times_C(bs,uik,diag,u); 781 782 /* update D(k) += -U(i,k)^T * U_bar(i,k) */ 783 PetscKernel_A_gets_A_plus_Btranspose_times_C(bs,dk,uik,u); 784 ierr = PetscLogFlops(2.0*bs*bs2);CHKERRQ(ierr); 785 786 /* update -U(i,k) */ 787 ierr = PetscMemcpy(ba+ili*bs2,uik,bs2*sizeof(MatScalar));CHKERRQ(ierr); 788 789 /* add multiple of row i to k-th row ... */ 790 jmin = ili + 1; jmax = bi[i+1]; 791 if (jmin < jmax) { 792 for (j=jmin; j<jmax; j++) { 793 /* rtmp += -U(i,k)^T * U_bar(i,j) */ 794 rtmp_ptr = rtmp + bj[j]*bs2; 795 u = ba + j*bs2; 796 PetscKernel_A_gets_A_plus_Btranspose_times_C(bs,rtmp_ptr,uik,u); 797 } 798 ierr = PetscLogFlops(2.0*bs*bs2*(jmax-jmin));CHKERRQ(ierr); 799 800 /* ... add i to row list for next nonzero entry */ 801 il[i] = jmin; /* update il(i) in column k+1, ... mbs-1 */ 802 j = bj[jmin]; 803 jl[i] = jl[j]; jl[j] = i; /* update jl */ 804 } 805 i = nexti; 806 } 807 808 /* save nonzero entries in k-th row of U ... */ 809 810 /* invert diagonal block */ 811 diag = ba+k*bs2; 812 ierr = PetscMemcpy(diag,dk,bs2*sizeof(MatScalar));CHKERRQ(ierr); 813 ierr = PetscKernel_A_gets_inverse_A(bs,diag,pivots,work);CHKERRQ(ierr); 814 815 jmin = bi[k]; jmax = bi[k+1]; 816 if (jmin < jmax) { 817 for (j=jmin; j<jmax; j++) { 818 vj = bj[j]; /* block col. index of U */ 819 u = ba + j*bs2; 820 rtmp_ptr = rtmp + vj*bs2; 821 for (k1=0; k1<bs2; k1++) { 822 *u++ = *rtmp_ptr; 823 *rtmp_ptr++ = 0.0; 824 } 825 } 826 827 /* ... add k to row list for first nonzero entry in k-th row */ 828 il[k] = jmin; 829 i = bj[jmin]; 830 jl[k] = jl[i]; jl[i] = k; 831 } 832 } 833 834 ierr = PetscFree(rtmp);CHKERRQ(ierr); 835 ierr = PetscFree2(il,jl);CHKERRQ(ierr); 836 ierr = PetscFree3(dk,uik,work);CHKERRQ(ierr); 837 ierr = PetscFree(pivots);CHKERRQ(ierr); 838 839 C->ops->solve = MatSolve_SeqSBAIJ_N_NaturalOrdering_inplace; 840 C->ops->solvetranspose = MatSolve_SeqSBAIJ_N_NaturalOrdering_inplace; 841 C->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_N_NaturalOrdering_inplace; 842 C->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_N_NaturalOrdering_inplace; 843 C->assembled = PETSC_TRUE; 844 C->preallocated = PETSC_TRUE; 845 846 ierr = PetscLogFlops(1.3333*bs*bs2*b->mbs);CHKERRQ(ierr); /* from inverting diagonal blocks */ 847 PetscFunctionReturn(0); 848 } 849 850 /* 851 Numeric U^T*D*U factorization for SBAIJ format. Modified from SNF of YSMP. 852 Version for blocks 2 by 2. 853 */ 854 #undef __FUNCT__ 855 #define __FUNCT__ "MatCholeskyFactorNumeric_SeqSBAIJ_2" 856 PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_2(Mat C,Mat A,const MatFactorInfo *info) 857 { 858 Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data,*b = (Mat_SeqSBAIJ*)C->data; 859 IS perm = b->row; 860 PetscErrorCode ierr; 861 const PetscInt *ai,*aj,*perm_ptr; 862 PetscInt i,j,mbs=a->mbs,*bi=b->i,*bj=b->j; 863 PetscInt *a2anew,k,k1,jmin,jmax,*jl,*il,vj,nexti,ili; 864 MatScalar *ba = b->a,*aa,*ap; 865 MatScalar *u,*diag,*rtmp,*rtmp_ptr,dk[4],uik[4]; 866 PetscReal shift = info->shiftamount; 867 PetscBool zeropivotdetected; 868 869 PetscFunctionBegin; 870 /* initialization */ 871 /* il and jl record the first nonzero element in each row of the accessing 872 window U(0:k, k:mbs-1). 873 jl: list of rows to be added to uneliminated rows 874 i>= k: jl(i) is the first row to be added to row i 875 i< k: jl(i) is the row following row i in some list of rows 876 jl(i) = mbs indicates the end of a list 877 il(i): points to the first nonzero element in columns k,...,mbs-1 of 878 row i of U */ 879 ierr = PetscCalloc1(4*mbs,&rtmp);CHKERRQ(ierr); 880 ierr = PetscMalloc2(mbs,&il,mbs,&jl);CHKERRQ(ierr); 881 for (i=0; i<mbs; i++) { 882 jl[i] = mbs; il[0] = 0; 883 } 884 ierr = ISGetIndices(perm,&perm_ptr);CHKERRQ(ierr); 885 886 /* check permutation */ 887 if (!a->permute) { 888 ai = a->i; aj = a->j; aa = a->a; 889 } else { 890 ai = a->inew; aj = a->jnew; 891 ierr = PetscMalloc1(4*ai[mbs],&aa);CHKERRQ(ierr); 892 ierr = PetscMemcpy(aa,a->a,4*ai[mbs]*sizeof(MatScalar));CHKERRQ(ierr); 893 ierr = PetscMalloc1(ai[mbs],&a2anew);CHKERRQ(ierr); 894 ierr = PetscMemcpy(a2anew,a->a2anew,(ai[mbs])*sizeof(PetscInt));CHKERRQ(ierr); 895 896 for (i=0; i<mbs; i++) { 897 jmin = ai[i]; jmax = ai[i+1]; 898 for (j=jmin; j<jmax; j++) { 899 while (a2anew[j] != j) { 900 k = a2anew[j]; a2anew[j] = a2anew[k]; a2anew[k] = k; 901 for (k1=0; k1<4; k1++) { 902 dk[k1] = aa[k*4+k1]; 903 aa[k*4+k1] = aa[j*4+k1]; 904 aa[j*4+k1] = dk[k1]; 905 } 906 } 907 /* transform columnoriented blocks that lie in the lower triangle to roworiented blocks */ 908 if (i > aj[j]) { 909 /* printf("change orientation, row: %d, col: %d\n",i,aj[j]); */ 910 ap = aa + j*4; /* ptr to the beginning of the block */ 911 dk[1] = ap[1]; /* swap ap[1] and ap[2] */ 912 ap[1] = ap[2]; 913 ap[2] = dk[1]; 914 } 915 } 916 } 917 ierr = PetscFree(a2anew);CHKERRQ(ierr); 918 } 919 920 /* for each row k */ 921 for (k = 0; k<mbs; k++) { 922 923 /*initialize k-th row with elements nonzero in row perm(k) of A */ 924 jmin = ai[perm_ptr[k]]; jmax = ai[perm_ptr[k]+1]; 925 ap = aa + jmin*4; 926 for (j = jmin; j < jmax; j++) { 927 vj = perm_ptr[aj[j]]; /* block col. index */ 928 rtmp_ptr = rtmp + vj*4; 929 for (i=0; i<4; i++) *rtmp_ptr++ = *ap++; 930 } 931 932 /* modify k-th row by adding in those rows i with U(i,k) != 0 */ 933 ierr = PetscMemcpy(dk,rtmp+k*4,4*sizeof(MatScalar));CHKERRQ(ierr); 934 i = jl[k]; /* first row to be added to k_th row */ 935 936 while (i < k) { 937 nexti = jl[i]; /* next row to be added to k_th row */ 938 939 /* compute multiplier */ 940 ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */ 941 942 /* uik = -inv(Di)*U_bar(i,k): - ba[ili]*ba[i] */ 943 diag = ba + i*4; 944 u = ba + ili*4; 945 uik[0] = -(diag[0]*u[0] + diag[2]*u[1]); 946 uik[1] = -(diag[1]*u[0] + diag[3]*u[1]); 947 uik[2] = -(diag[0]*u[2] + diag[2]*u[3]); 948 uik[3] = -(diag[1]*u[2] + diag[3]*u[3]); 949 950 /* update D(k) += -U(i,k)^T * U_bar(i,k): dk += uik*ba[ili] */ 951 dk[0] += uik[0]*u[0] + uik[1]*u[1]; 952 dk[1] += uik[2]*u[0] + uik[3]*u[1]; 953 dk[2] += uik[0]*u[2] + uik[1]*u[3]; 954 dk[3] += uik[2]*u[2] + uik[3]*u[3]; 955 956 ierr = PetscLogFlops(16.0*2.0);CHKERRQ(ierr); 957 958 /* update -U(i,k): ba[ili] = uik */ 959 ierr = PetscMemcpy(ba+ili*4,uik,4*sizeof(MatScalar));CHKERRQ(ierr); 960 961 /* add multiple of row i to k-th row ... */ 962 jmin = ili + 1; jmax = bi[i+1]; 963 if (jmin < jmax) { 964 for (j=jmin; j<jmax; j++) { 965 /* rtmp += -U(i,k)^T * U_bar(i,j): rtmp[bj[j]] += uik*ba[j]; */ 966 rtmp_ptr = rtmp + bj[j]*4; 967 u = ba + j*4; 968 rtmp_ptr[0] += uik[0]*u[0] + uik[1]*u[1]; 969 rtmp_ptr[1] += uik[2]*u[0] + uik[3]*u[1]; 970 rtmp_ptr[2] += uik[0]*u[2] + uik[1]*u[3]; 971 rtmp_ptr[3] += uik[2]*u[2] + uik[3]*u[3]; 972 } 973 ierr = PetscLogFlops(16.0*(jmax-jmin));CHKERRQ(ierr); 974 975 /* ... add i to row list for next nonzero entry */ 976 il[i] = jmin; /* update il(i) in column k+1, ... mbs-1 */ 977 j = bj[jmin]; 978 jl[i] = jl[j]; jl[j] = i; /* update jl */ 979 } 980 i = nexti; 981 } 982 983 /* save nonzero entries in k-th row of U ... */ 984 985 /* invert diagonal block */ 986 diag = ba+k*4; 987 ierr = PetscMemcpy(diag,dk,4*sizeof(MatScalar));CHKERRQ(ierr); 988 ierr = PetscKernel_A_gets_inverse_A_2(diag,shift,!A->erroriffailure,&zeropivotdetected);CHKERRQ(ierr); 989 if (zeropivotdetected) C->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 990 991 jmin = bi[k]; jmax = bi[k+1]; 992 if (jmin < jmax) { 993 for (j=jmin; j<jmax; j++) { 994 vj = bj[j]; /* block col. index of U */ 995 u = ba + j*4; 996 rtmp_ptr = rtmp + vj*4; 997 for (k1=0; k1<4; k1++) { 998 *u++ = *rtmp_ptr; 999 *rtmp_ptr++ = 0.0; 1000 } 1001 } 1002 1003 /* ... add k to row list for first nonzero entry in k-th row */ 1004 il[k] = jmin; 1005 i = bj[jmin]; 1006 jl[k] = jl[i]; jl[i] = k; 1007 } 1008 } 1009 1010 ierr = PetscFree(rtmp);CHKERRQ(ierr); 1011 ierr = PetscFree2(il,jl);CHKERRQ(ierr); 1012 if (a->permute) { 1013 ierr = PetscFree(aa);CHKERRQ(ierr); 1014 } 1015 ierr = ISRestoreIndices(perm,&perm_ptr);CHKERRQ(ierr); 1016 1017 C->ops->solve = MatSolve_SeqSBAIJ_2_inplace; 1018 C->ops->solvetranspose = MatSolve_SeqSBAIJ_2_inplace; 1019 C->assembled = PETSC_TRUE; 1020 C->preallocated = PETSC_TRUE; 1021 1022 ierr = PetscLogFlops(1.3333*8*b->mbs);CHKERRQ(ierr); /* from inverting diagonal blocks */ 1023 PetscFunctionReturn(0); 1024 } 1025 1026 /* 1027 Version for when blocks are 2 by 2 Using natural ordering 1028 */ 1029 #undef __FUNCT__ 1030 #define __FUNCT__ "MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering" 1031 PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering(Mat C,Mat A,const MatFactorInfo *info) 1032 { 1033 Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data,*b = (Mat_SeqSBAIJ*)C->data; 1034 PetscErrorCode ierr; 1035 PetscInt i,j,mbs=a->mbs,*bi=b->i,*bj=b->j; 1036 PetscInt *ai,*aj,k,k1,jmin,jmax,*jl,*il,vj,nexti,ili; 1037 MatScalar *ba = b->a,*aa,*ap,dk[8],uik[8]; 1038 MatScalar *u,*diag,*rtmp,*rtmp_ptr; 1039 PetscReal shift = info->shiftamount; 1040 PetscBool zeropivotdetected; 1041 1042 PetscFunctionBegin; 1043 /* initialization */ 1044 /* il and jl record the first nonzero element in each row of the accessing 1045 window U(0:k, k:mbs-1). 1046 jl: list of rows to be added to uneliminated rows 1047 i>= k: jl(i) is the first row to be added to row i 1048 i< k: jl(i) is the row following row i in some list of rows 1049 jl(i) = mbs indicates the end of a list 1050 il(i): points to the first nonzero element in columns k,...,mbs-1 of 1051 row i of U */ 1052 ierr = PetscCalloc1(4*mbs,&rtmp);CHKERRQ(ierr); 1053 ierr = PetscMalloc2(mbs,&il,mbs,&jl);CHKERRQ(ierr); 1054 for (i=0; i<mbs; i++) { 1055 jl[i] = mbs; il[0] = 0; 1056 } 1057 ai = a->i; aj = a->j; aa = a->a; 1058 1059 /* for each row k */ 1060 for (k = 0; k<mbs; k++) { 1061 1062 /*initialize k-th row with elements nonzero in row k of A */ 1063 jmin = ai[k]; jmax = ai[k+1]; 1064 ap = aa + jmin*4; 1065 for (j = jmin; j < jmax; j++) { 1066 vj = aj[j]; /* block col. index */ 1067 rtmp_ptr = rtmp + vj*4; 1068 for (i=0; i<4; i++) *rtmp_ptr++ = *ap++; 1069 } 1070 1071 /* modify k-th row by adding in those rows i with U(i,k) != 0 */ 1072 ierr = PetscMemcpy(dk,rtmp+k*4,4*sizeof(MatScalar));CHKERRQ(ierr); 1073 i = jl[k]; /* first row to be added to k_th row */ 1074 1075 while (i < k) { 1076 nexti = jl[i]; /* next row to be added to k_th row */ 1077 1078 /* compute multiplier */ 1079 ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */ 1080 1081 /* uik = -inv(Di)*U_bar(i,k): - ba[ili]*ba[i] */ 1082 diag = ba + i*4; 1083 u = ba + ili*4; 1084 uik[0] = -(diag[0]*u[0] + diag[2]*u[1]); 1085 uik[1] = -(diag[1]*u[0] + diag[3]*u[1]); 1086 uik[2] = -(diag[0]*u[2] + diag[2]*u[3]); 1087 uik[3] = -(diag[1]*u[2] + diag[3]*u[3]); 1088 1089 /* update D(k) += -U(i,k)^T * U_bar(i,k): dk += uik*ba[ili] */ 1090 dk[0] += uik[0]*u[0] + uik[1]*u[1]; 1091 dk[1] += uik[2]*u[0] + uik[3]*u[1]; 1092 dk[2] += uik[0]*u[2] + uik[1]*u[3]; 1093 dk[3] += uik[2]*u[2] + uik[3]*u[3]; 1094 1095 ierr = PetscLogFlops(16.0*2.0);CHKERRQ(ierr); 1096 1097 /* update -U(i,k): ba[ili] = uik */ 1098 ierr = PetscMemcpy(ba+ili*4,uik,4*sizeof(MatScalar));CHKERRQ(ierr); 1099 1100 /* add multiple of row i to k-th row ... */ 1101 jmin = ili + 1; jmax = bi[i+1]; 1102 if (jmin < jmax) { 1103 for (j=jmin; j<jmax; j++) { 1104 /* rtmp += -U(i,k)^T * U_bar(i,j): rtmp[bj[j]] += uik*ba[j]; */ 1105 rtmp_ptr = rtmp + bj[j]*4; 1106 u = ba + j*4; 1107 rtmp_ptr[0] += uik[0]*u[0] + uik[1]*u[1]; 1108 rtmp_ptr[1] += uik[2]*u[0] + uik[3]*u[1]; 1109 rtmp_ptr[2] += uik[0]*u[2] + uik[1]*u[3]; 1110 rtmp_ptr[3] += uik[2]*u[2] + uik[3]*u[3]; 1111 } 1112 ierr = PetscLogFlops(16.0*(jmax-jmin));CHKERRQ(ierr); 1113 1114 /* ... add i to row list for next nonzero entry */ 1115 il[i] = jmin; /* update il(i) in column k+1, ... mbs-1 */ 1116 j = bj[jmin]; 1117 jl[i] = jl[j]; jl[j] = i; /* update jl */ 1118 } 1119 i = nexti; 1120 } 1121 1122 /* save nonzero entries in k-th row of U ... */ 1123 1124 /* invert diagonal block */ 1125 diag = ba+k*4; 1126 ierr = PetscMemcpy(diag,dk,4*sizeof(MatScalar));CHKERRQ(ierr); 1127 ierr = PetscKernel_A_gets_inverse_A_2(diag,shift,!A->erroriffailure,&zeropivotdetected);CHKERRQ(ierr); 1128 if (zeropivotdetected) C->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 1129 1130 jmin = bi[k]; jmax = bi[k+1]; 1131 if (jmin < jmax) { 1132 for (j=jmin; j<jmax; j++) { 1133 vj = bj[j]; /* block col. index of U */ 1134 u = ba + j*4; 1135 rtmp_ptr = rtmp + vj*4; 1136 for (k1=0; k1<4; k1++) { 1137 *u++ = *rtmp_ptr; 1138 *rtmp_ptr++ = 0.0; 1139 } 1140 } 1141 1142 /* ... add k to row list for first nonzero entry in k-th row */ 1143 il[k] = jmin; 1144 i = bj[jmin]; 1145 jl[k] = jl[i]; jl[i] = k; 1146 } 1147 } 1148 1149 ierr = PetscFree(rtmp);CHKERRQ(ierr); 1150 ierr = PetscFree2(il,jl);CHKERRQ(ierr); 1151 1152 C->ops->solve = MatSolve_SeqSBAIJ_2_NaturalOrdering_inplace; 1153 C->ops->solvetranspose = MatSolve_SeqSBAIJ_2_NaturalOrdering_inplace; 1154 C->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_2_NaturalOrdering_inplace; 1155 C->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_2_NaturalOrdering_inplace; 1156 C->assembled = PETSC_TRUE; 1157 C->preallocated = PETSC_TRUE; 1158 1159 ierr = PetscLogFlops(1.3333*8*b->mbs);CHKERRQ(ierr); /* from inverting diagonal blocks */ 1160 PetscFunctionReturn(0); 1161 } 1162 1163 /* 1164 Numeric U^T*D*U factorization for SBAIJ format. 1165 Version for blocks are 1 by 1. 1166 */ 1167 #undef __FUNCT__ 1168 #define __FUNCT__ "MatCholeskyFactorNumeric_SeqSBAIJ_1_inplace" 1169 PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_1_inplace(Mat C,Mat A,const MatFactorInfo *info) 1170 { 1171 Mat_SeqSBAIJ *a=(Mat_SeqSBAIJ*)A->data,*b=(Mat_SeqSBAIJ*)C->data; 1172 IS ip=b->row; 1173 PetscErrorCode ierr; 1174 const PetscInt *ai,*aj,*rip; 1175 PetscInt *a2anew,i,j,mbs=a->mbs,*bi=b->i,*bj=b->j,*bcol; 1176 PetscInt k,jmin,jmax,*jl,*il,col,nexti,ili,nz; 1177 MatScalar *rtmp,*ba=b->a,*bval,*aa,dk,uikdi; 1178 PetscReal rs; 1179 FactorShiftCtx sctx; 1180 1181 PetscFunctionBegin; 1182 /* MatPivotSetUp(): initialize shift context sctx */ 1183 ierr = PetscMemzero(&sctx,sizeof(FactorShiftCtx));CHKERRQ(ierr); 1184 1185 ierr = ISGetIndices(ip,&rip);CHKERRQ(ierr); 1186 if (!a->permute) { 1187 ai = a->i; aj = a->j; aa = a->a; 1188 } else { 1189 ai = a->inew; aj = a->jnew; 1190 nz = ai[mbs]; 1191 ierr = PetscMalloc1(nz,&aa);CHKERRQ(ierr); 1192 a2anew = a->a2anew; 1193 bval = a->a; 1194 for (j=0; j<nz; j++) { 1195 aa[a2anew[j]] = *(bval++); 1196 } 1197 } 1198 1199 /* initialization */ 1200 /* il and jl record the first nonzero element in each row of the accessing 1201 window U(0:k, k:mbs-1). 1202 jl: list of rows to be added to uneliminated rows 1203 i>= k: jl(i) is the first row to be added to row i 1204 i< k: jl(i) is the row following row i in some list of rows 1205 jl(i) = mbs indicates the end of a list 1206 il(i): points to the first nonzero element in columns k,...,mbs-1 of 1207 row i of U */ 1208 ierr = PetscMalloc3(mbs,&rtmp,mbs,&il,mbs,&jl);CHKERRQ(ierr); 1209 1210 do { 1211 sctx.newshift = PETSC_FALSE; 1212 for (i=0; i<mbs; i++) { 1213 rtmp[i] = 0.0; jl[i] = mbs; il[0] = 0; 1214 } 1215 1216 for (k = 0; k<mbs; k++) { 1217 /*initialize k-th row by the perm[k]-th row of A */ 1218 jmin = ai[rip[k]]; jmax = ai[rip[k]+1]; 1219 bval = ba + bi[k]; 1220 for (j = jmin; j < jmax; j++) { 1221 col = rip[aj[j]]; 1222 rtmp[col] = aa[j]; 1223 *bval++ = 0.0; /* for in-place factorization */ 1224 } 1225 1226 /* shift the diagonal of the matrix */ 1227 if (sctx.nshift) rtmp[k] += sctx.shift_amount; 1228 1229 /* modify k-th row by adding in those rows i with U(i,k)!=0 */ 1230 dk = rtmp[k]; 1231 i = jl[k]; /* first row to be added to k_th row */ 1232 1233 while (i < k) { 1234 nexti = jl[i]; /* next row to be added to k_th row */ 1235 1236 /* compute multiplier, update diag(k) and U(i,k) */ 1237 ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */ 1238 uikdi = -ba[ili]*ba[bi[i]]; /* diagonal(k) */ 1239 dk += uikdi*ba[ili]; 1240 ba[ili] = uikdi; /* -U(i,k) */ 1241 1242 /* add multiple of row i to k-th row */ 1243 jmin = ili + 1; jmax = bi[i+1]; 1244 if (jmin < jmax) { 1245 for (j=jmin; j<jmax; j++) rtmp[bj[j]] += uikdi*ba[j]; 1246 ierr = PetscLogFlops(2.0*(jmax-jmin));CHKERRQ(ierr); 1247 1248 /* update il and jl for row i */ 1249 il[i] = jmin; 1250 j = bj[jmin]; jl[i] = jl[j]; jl[j] = i; 1251 } 1252 i = nexti; 1253 } 1254 1255 /* shift the diagonals when zero pivot is detected */ 1256 /* compute rs=sum of abs(off-diagonal) */ 1257 rs = 0.0; 1258 jmin = bi[k]+1; 1259 nz = bi[k+1] - jmin; 1260 if (nz) { 1261 bcol = bj + jmin; 1262 while (nz--) { 1263 rs += PetscAbsScalar(rtmp[*bcol]); 1264 bcol++; 1265 } 1266 } 1267 1268 sctx.rs = rs; 1269 sctx.pv = dk; 1270 ierr = MatPivotCheck(C,A,info,&sctx,k);CHKERRQ(ierr); 1271 if (sctx.newshift) break; /* sctx.shift_amount is updated */ 1272 dk = sctx.pv; 1273 1274 /* copy data into U(k,:) */ 1275 ba[bi[k]] = 1.0/dk; /* U(k,k) */ 1276 jmin = bi[k]+1; jmax = bi[k+1]; 1277 if (jmin < jmax) { 1278 for (j=jmin; j<jmax; j++) { 1279 col = bj[j]; ba[j] = rtmp[col]; rtmp[col] = 0.0; 1280 } 1281 /* add the k-th row into il and jl */ 1282 il[k] = jmin; 1283 i = bj[jmin]; jl[k] = jl[i]; jl[i] = k; 1284 } 1285 } 1286 } while (sctx.newshift); 1287 ierr = PetscFree3(rtmp,il,jl);CHKERRQ(ierr); 1288 if (a->permute) {ierr = PetscFree(aa);CHKERRQ(ierr);} 1289 1290 ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr); 1291 1292 C->ops->solve = MatSolve_SeqSBAIJ_1_inplace; 1293 C->ops->solves = MatSolves_SeqSBAIJ_1_inplace; 1294 C->ops->solvetranspose = MatSolve_SeqSBAIJ_1_inplace; 1295 C->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_1_inplace; 1296 C->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_1_inplace; 1297 C->assembled = PETSC_TRUE; 1298 C->preallocated = PETSC_TRUE; 1299 1300 ierr = PetscLogFlops(C->rmap->N);CHKERRQ(ierr); 1301 if (sctx.nshift) { 1302 if (info->shifttype == (PetscReal)MAT_SHIFT_NONZERO) { 1303 ierr = PetscInfo2(A,"number of shiftnz tries %D, shift_amount %g\n",sctx.nshift,(double)sctx.shift_amount);CHKERRQ(ierr); 1304 } else if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) { 1305 ierr = PetscInfo2(A,"number of shiftpd tries %D, shift_amount %g\n",sctx.nshift,(double)sctx.shift_amount);CHKERRQ(ierr); 1306 } 1307 } 1308 PetscFunctionReturn(0); 1309 } 1310 1311 /* 1312 Version for when blocks are 1 by 1 Using natural ordering under new datastructure 1313 Modified from MatCholeskyFactorNumeric_SeqAIJ() 1314 */ 1315 #undef __FUNCT__ 1316 #define __FUNCT__ "MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering" 1317 PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering(Mat B,Mat A,const MatFactorInfo *info) 1318 { 1319 Mat_SeqSBAIJ *a=(Mat_SeqSBAIJ*)A->data; 1320 Mat_SeqSBAIJ *b=(Mat_SeqSBAIJ*)B->data; 1321 PetscErrorCode ierr; 1322 PetscInt i,j,mbs=A->rmap->n,*bi=b->i,*bj=b->j,*bdiag=b->diag,*bjtmp; 1323 PetscInt *ai=a->i,*aj=a->j,*ajtmp; 1324 PetscInt k,jmin,jmax,*c2r,*il,col,nexti,ili,nz; 1325 MatScalar *rtmp,*ba=b->a,*bval,*aa=a->a,dk,uikdi; 1326 FactorShiftCtx sctx; 1327 PetscReal rs; 1328 MatScalar d,*v; 1329 1330 PetscFunctionBegin; 1331 ierr = PetscMalloc3(mbs,&rtmp,mbs,&il,mbs,&c2r);CHKERRQ(ierr); 1332 1333 /* MatPivotSetUp(): initialize shift context sctx */ 1334 ierr = PetscMemzero(&sctx,sizeof(FactorShiftCtx));CHKERRQ(ierr); 1335 1336 if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) { /* set sctx.shift_top=max{rs} */ 1337 sctx.shift_top = info->zeropivot; 1338 1339 ierr = PetscMemzero(rtmp,mbs*sizeof(MatScalar));CHKERRQ(ierr); 1340 1341 for (i=0; i<mbs; i++) { 1342 /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */ 1343 d = (aa)[a->diag[i]]; 1344 rtmp[i] += -PetscRealPart(d); /* diagonal entry */ 1345 ajtmp = aj + ai[i] + 1; /* exclude diagonal */ 1346 v = aa + ai[i] + 1; 1347 nz = ai[i+1] - ai[i] - 1; 1348 for (j=0; j<nz; j++) { 1349 rtmp[i] += PetscAbsScalar(v[j]); 1350 rtmp[ajtmp[j]] += PetscAbsScalar(v[j]); 1351 } 1352 if (PetscRealPart(rtmp[i]) > sctx.shift_top) sctx.shift_top = PetscRealPart(rtmp[i]); 1353 } 1354 sctx.shift_top *= 1.1; 1355 sctx.nshift_max = 5; 1356 sctx.shift_lo = 0.; 1357 sctx.shift_hi = 1.; 1358 } 1359 1360 /* allocate working arrays 1361 c2r: linked list, keep track of pivot rows for a given column. c2r[col]: head of the list for a given col 1362 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 1363 */ 1364 do { 1365 sctx.newshift = PETSC_FALSE; 1366 1367 for (i=0; i<mbs; i++) c2r[i] = mbs; 1368 if (mbs) il[0] = 0; 1369 1370 for (k = 0; k<mbs; k++) { 1371 /* zero rtmp */ 1372 nz = bi[k+1] - bi[k]; 1373 bjtmp = bj + bi[k]; 1374 for (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0; 1375 1376 /* load in initial unfactored row */ 1377 bval = ba + bi[k]; 1378 jmin = ai[k]; jmax = ai[k+1]; 1379 for (j = jmin; j < jmax; j++) { 1380 col = aj[j]; 1381 rtmp[col] = aa[j]; 1382 *bval++ = 0.0; /* for in-place factorization */ 1383 } 1384 /* shift the diagonal of the matrix: ZeropivotApply() */ 1385 rtmp[k] += sctx.shift_amount; /* shift the diagonal of the matrix */ 1386 1387 /* modify k-th row by adding in those rows i with U(i,k)!=0 */ 1388 dk = rtmp[k]; 1389 i = c2r[k]; /* first row to be added to k_th row */ 1390 1391 while (i < k) { 1392 nexti = c2r[i]; /* next row to be added to k_th row */ 1393 1394 /* compute multiplier, update diag(k) and U(i,k) */ 1395 ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */ 1396 uikdi = -ba[ili]*ba[bdiag[i]]; /* diagonal(k) */ 1397 dk += uikdi*ba[ili]; /* update diag[k] */ 1398 ba[ili] = uikdi; /* -U(i,k) */ 1399 1400 /* add multiple of row i to k-th row */ 1401 jmin = ili + 1; jmax = bi[i+1]; 1402 if (jmin < jmax) { 1403 for (j=jmin; j<jmax; j++) rtmp[bj[j]] += uikdi*ba[j]; 1404 /* update il and c2r for row i */ 1405 il[i] = jmin; 1406 j = bj[jmin]; c2r[i] = c2r[j]; c2r[j] = i; 1407 } 1408 i = nexti; 1409 } 1410 1411 /* copy data into U(k,:) */ 1412 rs = 0.0; 1413 jmin = bi[k]; jmax = bi[k+1]-1; 1414 if (jmin < jmax) { 1415 for (j=jmin; j<jmax; j++) { 1416 col = bj[j]; ba[j] = rtmp[col]; rs += PetscAbsScalar(ba[j]); 1417 } 1418 /* add the k-th row into il and c2r */ 1419 il[k] = jmin; 1420 i = bj[jmin]; c2r[k] = c2r[i]; c2r[i] = k; 1421 } 1422 1423 sctx.rs = rs; 1424 sctx.pv = dk; 1425 ierr = MatPivotCheck(B,A,info,&sctx,k);CHKERRQ(ierr); 1426 if (sctx.newshift) break; 1427 dk = sctx.pv; 1428 1429 ba[bdiag[k]] = 1.0/dk; /* U(k,k) */ 1430 } 1431 } while (sctx.newshift); 1432 1433 ierr = PetscFree3(rtmp,il,c2r);CHKERRQ(ierr); 1434 1435 B->ops->solve = MatSolve_SeqSBAIJ_1_NaturalOrdering; 1436 B->ops->solves = MatSolves_SeqSBAIJ_1; 1437 B->ops->solvetranspose = MatSolve_SeqSBAIJ_1_NaturalOrdering; 1438 B->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_1_NaturalOrdering; 1439 B->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_1_NaturalOrdering; 1440 1441 B->assembled = PETSC_TRUE; 1442 B->preallocated = PETSC_TRUE; 1443 1444 ierr = PetscLogFlops(B->rmap->n);CHKERRQ(ierr); 1445 1446 /* MatPivotView() */ 1447 if (sctx.nshift) { 1448 if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) { 1449 ierr = PetscInfo4(A,"number of shift_pd tries %D, shift_amount %g, diagonal shifted up by %e fraction top_value %e\n",sctx.nshift,(double)sctx.shift_amount,(double)sctx.shift_fraction,(double)sctx.shift_top);CHKERRQ(ierr); 1450 } else if (info->shifttype == (PetscReal)MAT_SHIFT_NONZERO) { 1451 ierr = PetscInfo2(A,"number of shift_nz tries %D, shift_amount %g\n",sctx.nshift,(double)sctx.shift_amount);CHKERRQ(ierr); 1452 } else if (info->shifttype == (PetscReal)MAT_SHIFT_INBLOCKS) { 1453 ierr = PetscInfo2(A,"number of shift_inblocks applied %D, each shift_amount %g\n",sctx.nshift,(double)info->shiftamount);CHKERRQ(ierr); 1454 } 1455 } 1456 PetscFunctionReturn(0); 1457 } 1458 1459 #undef __FUNCT__ 1460 #define __FUNCT__ "MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering_inplace" 1461 PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering_inplace(Mat C,Mat A,const MatFactorInfo *info) 1462 { 1463 Mat_SeqSBAIJ *a=(Mat_SeqSBAIJ*)A->data,*b=(Mat_SeqSBAIJ*)C->data; 1464 PetscErrorCode ierr; 1465 PetscInt i,j,mbs = a->mbs; 1466 PetscInt *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j; 1467 PetscInt k,jmin,*jl,*il,nexti,ili,*acol,*bcol,nz; 1468 MatScalar *rtmp,*ba=b->a,*aa=a->a,dk,uikdi,*aval,*bval; 1469 PetscReal rs; 1470 FactorShiftCtx sctx; 1471 1472 PetscFunctionBegin; 1473 /* MatPivotSetUp(): initialize shift context sctx */ 1474 ierr = PetscMemzero(&sctx,sizeof(FactorShiftCtx));CHKERRQ(ierr); 1475 1476 /* initialization */ 1477 /* il and jl record the first nonzero element in each row of the accessing 1478 window U(0:k, k:mbs-1). 1479 jl: list of rows to be added to uneliminated rows 1480 i>= k: jl(i) is the first row to be added to row i 1481 i< k: jl(i) is the row following row i in some list of rows 1482 jl(i) = mbs indicates the end of a list 1483 il(i): points to the first nonzero element in U(i,k:mbs-1) 1484 */ 1485 ierr = PetscMalloc1(mbs,&rtmp);CHKERRQ(ierr); 1486 ierr = PetscMalloc2(mbs,&il,mbs,&jl);CHKERRQ(ierr); 1487 1488 do { 1489 sctx.newshift = PETSC_FALSE; 1490 for (i=0; i<mbs; i++) { 1491 rtmp[i] = 0.0; jl[i] = mbs; il[0] = 0; 1492 } 1493 1494 for (k = 0; k<mbs; k++) { 1495 /*initialize k-th row with elements nonzero in row perm(k) of A */ 1496 nz = ai[k+1] - ai[k]; 1497 acol = aj + ai[k]; 1498 aval = aa + ai[k]; 1499 bval = ba + bi[k]; 1500 while (nz--) { 1501 rtmp[*acol++] = *aval++; 1502 *bval++ = 0.0; /* for in-place factorization */ 1503 } 1504 1505 /* shift the diagonal of the matrix */ 1506 if (sctx.nshift) rtmp[k] += sctx.shift_amount; 1507 1508 /* modify k-th row by adding in those rows i with U(i,k)!=0 */ 1509 dk = rtmp[k]; 1510 i = jl[k]; /* first row to be added to k_th row */ 1511 1512 while (i < k) { 1513 nexti = jl[i]; /* next row to be added to k_th row */ 1514 /* compute multiplier, update D(k) and U(i,k) */ 1515 ili = il[i]; /* index of first nonzero element in U(i,k:bms-1) */ 1516 uikdi = -ba[ili]*ba[bi[i]]; 1517 dk += uikdi*ba[ili]; 1518 ba[ili] = uikdi; /* -U(i,k) */ 1519 1520 /* add multiple of row i to k-th row ... */ 1521 jmin = ili + 1; 1522 nz = bi[i+1] - jmin; 1523 if (nz > 0) { 1524 bcol = bj + jmin; 1525 bval = ba + jmin; 1526 ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr); 1527 while (nz--) rtmp[*bcol++] += uikdi*(*bval++); 1528 1529 /* update il and jl for i-th row */ 1530 il[i] = jmin; 1531 j = bj[jmin]; jl[i] = jl[j]; jl[j] = i; 1532 } 1533 i = nexti; 1534 } 1535 1536 /* shift the diagonals when zero pivot is detected */ 1537 /* compute rs=sum of abs(off-diagonal) */ 1538 rs = 0.0; 1539 jmin = bi[k]+1; 1540 nz = bi[k+1] - jmin; 1541 if (nz) { 1542 bcol = bj + jmin; 1543 while (nz--) { 1544 rs += PetscAbsScalar(rtmp[*bcol]); 1545 bcol++; 1546 } 1547 } 1548 1549 sctx.rs = rs; 1550 sctx.pv = dk; 1551 ierr = MatPivotCheck(C,A,info,&sctx,k);CHKERRQ(ierr); 1552 if (sctx.newshift) break; /* sctx.shift_amount is updated */ 1553 dk = sctx.pv; 1554 1555 /* copy data into U(k,:) */ 1556 ba[bi[k]] = 1.0/dk; 1557 jmin = bi[k]+1; 1558 nz = bi[k+1] - jmin; 1559 if (nz) { 1560 bcol = bj + jmin; 1561 bval = ba + jmin; 1562 while (nz--) { 1563 *bval++ = rtmp[*bcol]; 1564 rtmp[*bcol++] = 0.0; 1565 } 1566 /* add k-th row into il and jl */ 1567 il[k] = jmin; 1568 i = bj[jmin]; jl[k] = jl[i]; jl[i] = k; 1569 } 1570 } /* end of for (k = 0; k<mbs; k++) */ 1571 } while (sctx.newshift); 1572 ierr = PetscFree(rtmp);CHKERRQ(ierr); 1573 ierr = PetscFree2(il,jl);CHKERRQ(ierr); 1574 1575 C->ops->solve = MatSolve_SeqSBAIJ_1_NaturalOrdering_inplace; 1576 C->ops->solves = MatSolves_SeqSBAIJ_1_inplace; 1577 C->ops->solvetranspose = MatSolve_SeqSBAIJ_1_NaturalOrdering_inplace; 1578 C->ops->forwardsolve = MatForwardSolve_SeqSBAIJ_1_NaturalOrdering_inplace; 1579 C->ops->backwardsolve = MatBackwardSolve_SeqSBAIJ_1_NaturalOrdering_inplace; 1580 1581 C->assembled = PETSC_TRUE; 1582 C->preallocated = PETSC_TRUE; 1583 1584 ierr = PetscLogFlops(C->rmap->N);CHKERRQ(ierr); 1585 if (sctx.nshift) { 1586 if (info->shifttype == (PetscReal)MAT_SHIFT_NONZERO) { 1587 ierr = PetscInfo2(A,"number of shiftnz tries %D, shift_amount %g\n",sctx.nshift,(double)sctx.shift_amount);CHKERRQ(ierr); 1588 } else if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) { 1589 ierr = PetscInfo2(A,"number of shiftpd tries %D, shift_amount %g\n",sctx.nshift,(double)sctx.shift_amount);CHKERRQ(ierr); 1590 } 1591 } 1592 PetscFunctionReturn(0); 1593 } 1594 1595 #undef __FUNCT__ 1596 #define __FUNCT__ "MatCholeskyFactor_SeqSBAIJ" 1597 PetscErrorCode MatCholeskyFactor_SeqSBAIJ(Mat A,IS perm,const MatFactorInfo *info) 1598 { 1599 PetscErrorCode ierr; 1600 Mat C; 1601 1602 PetscFunctionBegin; 1603 ierr = MatGetFactor(A,"petsc",MAT_FACTOR_CHOLESKY,&C);CHKERRQ(ierr); 1604 ierr = MatCholeskyFactorSymbolic(C,A,perm,info);CHKERRQ(ierr); 1605 ierr = MatCholeskyFactorNumeric(C,A,info);CHKERRQ(ierr); 1606 1607 A->ops->solve = C->ops->solve; 1608 A->ops->solvetranspose = C->ops->solvetranspose; 1609 1610 ierr = MatHeaderMerge(A,&C);CHKERRQ(ierr); 1611 PetscFunctionReturn(0); 1612 } 1613 1614 1615