static PetscErrorCode MatWrapML_SHELL(ML_Operator *mlmat,MatReuse reuse,Mat *newmat) { PetscErrorCode ierr; PetscInt m,n; ML_Comm *MLcomm; Mat_MLShell *shellctx; PetscFunctionBegin; m = mlmat->outvec_leng; n = mlmat->invec_leng; if (reuse){ ierr = MatShellGetContext(*newmat,(void **)&shellctx);CHKERRQ(ierr); shellctx->mlmat = mlmat; PetscFunctionReturn(0); } MLcomm = mlmat->comm; ierr = PetscNew(Mat_MLShell,&shellctx);CHKERRQ(ierr); ierr = MatCreateShell(MLcomm->USR_comm,m,n,PETSC_DETERMINE,PETSC_DETERMINE,shellctx,newmat);CHKERRQ(ierr); ierr = MatShellSetOperation(*newmat,MATOP_MULT,(void(*)(void))MatMult_ML);CHKERRQ(ierr); ierr = MatShellSetOperation(*newmat,MATOP_MULT_ADD,(void(*)(void))MatMultAdd_ML);CHKERRQ(ierr); shellctx->A = *newmat; shellctx->mlmat = mlmat; shellctx->work = PETSC_NULL; ierr = VecCreate(MLcomm->USR_comm,&shellctx->y);CHKERRQ(ierr); ierr = VecSetSizes(shellctx->y,m,PETSC_DECIDE);CHKERRQ(ierr); ierr = VecSetFromOptions(shellctx->y);CHKERRQ(ierr); (*newmat)->ops->destroy = MatDestroy_ML; PetscFunctionReturn(0); }
PetscErrorCode MatGetDiagonal_myMat(Mat A, Vec diag) { PetscFunctionBeginUser; PetscErrorCode ierr; model *m; ierr = MatShellGetContext(A, &m); CHKERRQ(ierr); PetscScalar *pd; ierr = VecGetArray(diag, &pd); CHKERRQ(ierr); PetscInt Istart, Iend; ierr = MatGetOwnershipRange(A, &Istart, &Iend); CHKERRQ(ierr); PetscInt A_m, n; ierr = MatGetSize(A, &A_m, &n); CHKERRQ(ierr); PetscScalar* pgd = new PetscScalar[n]; rokko::heisenberg_hamiltonian::fill_diagonal(m->L, m->lattice, pgd); int k=0; for(int i = Istart; i < Iend; ++i) { pd[k] = pgd[i]; ++k; } ierr = VecRestoreArray(diag ,&pd); CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode krylov_petsc_apply_aij( Mat A, Vec x, Vec y ) { void *ctx; PetscErrorCode ierr; krylov_pc_ctx_t* kct; const double* px; double* py; /* PetscFunctionBegin; */ ierr = MatShellGetContext( A, &ctx ); CHKERRQ(ierr); kct = (krylov_pc_ctx_t *)ctx; ierr = VecGetArrayRead( x, &px ); CHKERRQ(ierr); ierr = VecGetArray( y, &py ); CHKERRQ(ierr); problem_data_t* vecs = kct->vecs; weakeqn_ptrs_t* fcns = kct->fcns; p4est_t* p4est = kct->p4est; p4est_ghost_t* ghost = *kct->ghost; element_data_t* ghost_data = *kct->ghost_data; dgmath_jit_dbase_t* dgmath_jit_dbase = kct->dgmath_jit_dbase; problem_data_t vecs_for_aij; problem_data_copy_ptrs(vecs, &vecs_for_aij); vecs_for_aij.u = (double*)px; vecs_for_aij.Au = py; fcns->apply_lhs(p4est, ghost, ghost_data, &vecs_for_aij, dgmath_jit_dbase); ierr = VecRestoreArrayRead( x, &px ); CHKERRQ(ierr); ierr = VecRestoreArray( y, &py ); CHKERRQ(ierr); return ierr; }
// ------------------------------------------------------------- // MatTranspose_DenseGA // ------------------------------------------------------------- static PetscErrorCode MatTranspose_DenseGA(Mat mat, MatReuse reuse, Mat *B) { PetscErrorCode ierr = 0; MPI_Comm comm; ierr = PetscObjectGetComm((PetscObject)mat, &comm); CHKERRQ(ierr); struct MatGACtx *ctx, *newctx; ierr = MatShellGetContext(mat, &ctx); CHKERRQ(ierr); PetscInt lrows, grows, lcols, gcols; ierr = MatGetSize(mat, &grows, &gcols); CHKERRQ(ierr); ierr = MatGetLocalSize(mat, &lrows, &lcols); CHKERRQ(ierr); ierr = PetscMalloc(sizeof(struct MatGACtx), &newctx); CHKERRQ(ierr); newctx->gaGroup = ctx->gaGroup; ierr = CreateMatGA(newctx->gaGroup, lcols, lrows, gcols, grows, &(newctx->ga)); CHKERRQ(ierr); GA_Transpose(ctx->ga, newctx->ga); ierr = MatCreateShell(comm, lcols, lrows, gcols, grows, newctx, B); CHKERRQ(ierr); ierr = MatSetOperations_DenseGA(*B); return ierr; }
PetscErrorCode MatGetDiagonal_Brussel(Mat A,Vec diag) { Vec d1,d2; PetscInt n; PetscScalar *pd; MPI_Comm comm; CTX_BRUSSEL *ctx; PetscErrorCode ierr; PetscFunctionBeginUser; ierr = MatShellGetContext(A,(void**)&ctx);CHKERRQ(ierr); ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); ierr = MatGetLocalSize(ctx->T,&n,NULL);CHKERRQ(ierr); ierr = VecGetArray(diag,&pd);CHKERRQ(ierr); ierr = VecCreateMPIWithArray(comm,1,n,PETSC_DECIDE,pd,&d1);CHKERRQ(ierr); ierr = VecCreateMPIWithArray(comm,1,n,PETSC_DECIDE,pd+n,&d2);CHKERRQ(ierr); ierr = VecSet(d1,-2.0*ctx->tau1 + ctx->beta - 1.0 + ctx->sigma);CHKERRQ(ierr); ierr = VecSet(d2,-2.0*ctx->tau2 - ctx->alpha*ctx->alpha + ctx->sigma);CHKERRQ(ierr); ierr = VecDestroy(&d1);CHKERRQ(ierr); ierr = VecDestroy(&d2);CHKERRQ(ierr); ierr = VecRestoreArray(diag,&pd);CHKERRQ(ierr); PetscFunctionReturn(0); }
// ------------------------------------------------------------- // MatGetValues_DenseGA // ------------------------------------------------------------- static PetscErrorCode MatGetValues_DenseGA(Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], PetscScalar v[]) { PetscErrorCode ierr = 0; struct MatGACtx *ctx; int i, j, idx; PetscScalar vij; int lo[2], hi[2], ld[2] = {1, 1}; ierr = MatShellGetContext(mat, (void *)&ctx); CHKERRQ(ierr); idx = 0; for (i = 0; i < m; ++i) { for (j = 0; j < n; ++j, ++idx) { lo[0] = idxm[i]; hi[0] = idxm[i]; lo[1] = idxn[j]; hi[1] = idxn[j]; NGA_Get(ctx->ga, lo, hi, (void *)&vij, ld); v[idx] = vij; } } return ierr; }
/* Computes y = w + A * x It is possible that w == y, but not x == y */ static PetscErrorCode MatMultAdd_ML(Mat A,Vec x,Vec w,Vec y) { Mat_MLShell *shell; PetscScalar *xarray,*yarray; PetscInt x_length,y_length; PetscErrorCode ierr; PetscFunctionBegin; ierr = MatShellGetContext(A, (void **) &shell);CHKERRQ(ierr); if (y == w) { if (!shell->work) { ierr = VecDuplicate(y, &shell->work);CHKERRQ(ierr); } ierr = VecGetArray(x, &xarray);CHKERRQ(ierr); ierr = VecGetArray(shell->work, &yarray);CHKERRQ(ierr); x_length = shell->mlmat->invec_leng; y_length = shell->mlmat->outvec_leng; ML_Operator_Apply(shell->mlmat, x_length, xarray, y_length, yarray); ierr = VecRestoreArray(x, &xarray);CHKERRQ(ierr); ierr = VecRestoreArray(shell->work, &yarray);CHKERRQ(ierr); ierr = VecAXPY(y, 1.0, shell->work);CHKERRQ(ierr); } else { ierr = VecGetArray(x, &xarray);CHKERRQ(ierr); ierr = VecGetArray(y, &yarray);CHKERRQ(ierr); x_length = shell->mlmat->invec_leng; y_length = shell->mlmat->outvec_leng; ML_Operator_Apply(shell->mlmat, x_length, xarray, y_length, yarray); ierr = VecRestoreArray(x, &xarray);CHKERRQ(ierr); ierr = VecRestoreArray(y, &yarray);CHKERRQ(ierr); ierr = VecAXPY(y, 1.0, w);CHKERRQ(ierr); } PetscFunctionReturn(0); }
// ------------------------------------------------------------- // MatSetValues_DenseGA // ------------------------------------------------------------- static PetscErrorCode MatSetValues_DenseGA(Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], const PetscScalar v[],InsertMode addv) { PetscErrorCode ierr = 0; struct MatGACtx *ctx; int i, j, idx; PetscScalar vij, one(1.0); int lo[2], hi[2], ld[2] = {1, 1}; ierr = MatShellGetContext(mat, (void *)&ctx); CHKERRQ(ierr); idx = 0; for (i = 0; i < m; ++i) { for (j = 0; j < n; ++j, ++idx) { lo[0] = idxm[i]; hi[0] = idxm[i]; lo[1] = idxn[j]; hi[1] = idxn[j]; vij = v[idx]; switch (addv) { case INSERT_VALUES: NGA_Put(ctx->ga, lo, hi, (void *)&vij, ld); break; case ADD_VALUES: NGA_Acc(ctx->ga, lo, hi, (void *)&vij, ld, &one); break; default: BOOST_ASSERT_MSG(false, "Unknown set operation"); } } } return ierr; }
extern PetscErrorCode MatLMVMAllocateVectors(Mat m, Vec v) { PetscErrorCode ierr; MatLMVMCtx *ctx; PetscBool same; PetscFunctionBegin; PetscValidHeaderSpecific(m,MAT_CLASSID,1); PetscValidHeaderSpecific(v,VEC_CLASSID,2); ierr = PetscObjectTypeCompare((PetscObject)m,MATSHELL,&same);CHKERRQ(ierr); if (!same) SETERRQ(PETSC_COMM_SELF,1,"Matrix m is not type MatLMVM"); ierr = MatShellGetContext(m,(void**)&ctx);CHKERRQ(ierr); /* Perform allocations */ ierr = VecDuplicateVecs(v,ctx->lm+1,&ctx->S);CHKERRQ(ierr); ierr = VecDuplicateVecs(v,ctx->lm+1,&ctx->Y);CHKERRQ(ierr); ierr = VecDuplicate(v,&ctx->D);CHKERRQ(ierr); ierr = VecDuplicate(v,&ctx->U);CHKERRQ(ierr); ierr = VecDuplicate(v,&ctx->V);CHKERRQ(ierr); ierr = VecDuplicate(v,&ctx->W);CHKERRQ(ierr); ierr = VecDuplicate(v,&ctx->P);CHKERRQ(ierr); ierr = VecDuplicate(v,&ctx->Q);CHKERRQ(ierr); ctx->allocated = PETSC_TRUE; PetscFunctionReturn(0); }
/* HessianProductMat - Computes the matrix-vector product y = mat*svec. Input Parameters: . mat - input matrix . svec - input vector Output Parameters: . y - solution vector */ PetscErrorCode HessianProductMat(Mat mat,Vec svec,Vec y) { void *ptr; PetscErrorCode ierr; ierr = MatShellGetContext(mat,&ptr);CHKERRQ(ierr); ierr = HessianProduct(ptr,svec,y);CHKERRQ(ierr); return 0; }
// ------------------------------------------------------------- // MatAssemmblyBegin_DenseGA // ------------------------------------------------------------- static PetscErrorCode MatAssemmblyBegin_DenseGA(Mat mat, MatAssemblyType type) { PetscErrorCode ierr = 0; struct MatGACtx *ctx; ierr = MatShellGetContext(mat, &ctx); CHKERRQ(ierr); return ierr; }
PetscErrorCode H2PlusMatMultS(Mat S, Vec x, Vec y) { PetscErrorCode ierr; OceH2plus ctx; ierr = MatShellGetContext(S, &ctx); CHKERRQ(ierr); ierr = MatMatDecomposedMult(ctx->s_r1, ctx->s_y1, x, y); CHKERRQ(ierr); return 0; }
static PetscErrorCode MatGetDiagonal_Op(Mat A,Vec D) { PetscErrorCode ierr; Mat_Op *ctx; PetscFunctionBegin; ierr = MatShellGetContext(A,&ctx);CHKERRQ(ierr); ierr = OpGetDiagonal(ctx->op,ctx->dm,D);CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode MatMult_Op(Mat A,Vec X,Vec Y) { PetscErrorCode ierr; Mat_Op *ctx; PetscFunctionBegin; ierr = MatShellGetContext(A,&ctx);CHKERRQ(ierr); ierr = OpApply(ctx->op,ctx->dm,X,Y);CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode MatDestroy_Op(Mat A) { PetscErrorCode ierr; Mat_Op *ctx; PetscFunctionBegin; ierr = MatShellGetContext(A,&ctx);CHKERRQ(ierr); ierr = PetscFree(ctx);CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode MatMult_User(Mat A,Vec X,Vec Y) { User user; PetscErrorCode ierr; PetscFunctionBegin; ierr = MatShellGetContext(A,&user);CHKERRQ(ierr); ierr = MatMult(user->A,X,Y);CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode MatGetDiagonal_User(Mat A,Vec X) { User user; PetscErrorCode ierr; PetscFunctionBegin; ierr = MatShellGetContext(A,&user);CHKERRQ(ierr); ierr = MatGetDiagonal(user->B,X);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode MatAssemblyEnd_MyShell(Mat A,MatAssemblyType tp) { PetscErrorCode ierr; MatShellCtx *matshellctx; PetscFunctionBegin; ierr = MatShellGetContext(A,&matshellctx);CHKERRQ(ierr); ierr = MatAssemblyEnd(matshellctx->Jmf,tp);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode MatShift_Brussel(PetscScalar* a,Mat Y) { CTX_BRUSSEL *ctx; PetscErrorCode ierr; PetscFunctionBeginUser; ierr = MatShellGetContext(Y,(void**)&ctx);CHKERRQ(ierr); ctx->sigma += *a; PetscFunctionReturn(0); }
PetscErrorCode MatGetColumnVector_SMF(Mat mat,Vec Y, PetscInt col) { PetscErrorCode ierr; MatSubMatFreeCtx ctx; PetscFunctionBegin; ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); ierr = MatGetColumnVector(ctx->A,Y,col);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode MatGetRowMax_SMF(Mat M, Vec D) { MatSubMatFreeCtx ctx; PetscErrorCode ierr; PetscFunctionBegin; ierr = MatShellGetContext(M,(void **)&ctx);CHKERRQ(ierr); ierr = MatGetRowMax(ctx->A,D,NULL);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode MatRestoreRow_SMF(Mat mat,PetscInt row,PetscInt *ncols,const PetscInt **cols,const PetscScalar **vals) { PetscErrorCode ierr; MatSubMatFreeCtx ctx; PetscFunctionBegin; ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); ierr = MatRestoreRow(ctx->A,row,ncols,cols,vals);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode MatScale_SMF(Mat mat, PetscReal a) { PetscErrorCode ierr; MatSubMatFreeCtx ctx; PetscFunctionBegin; ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); ierr = MatScale(ctx->A,a);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode MatGetDiagonal_SMF(Mat mat,Vec v) { PetscErrorCode ierr; MatSubMatFreeCtx ctx; PetscFunctionBegin; ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); ierr = MatGetDiagonal(ctx->A,v);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode MatDuplicate_SMF(Mat mat,MatDuplicateOption op,Mat *M) { PetscErrorCode ierr; MatSubMatFreeCtx ctx; PetscFunctionBegin; ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); ierr = MatCreateSubMatrixFree(ctx->A,ctx->Rows,ctx->Cols,M);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode MatView_SMF(Mat mat,PetscViewer viewer) { PetscErrorCode ierr; MatSubMatFreeCtx ctx; PetscFunctionBegin; ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); ierr = MatView(ctx->A,viewer);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode MatDiagonalSet_SMF(Mat M, Vec D,InsertMode is) { MatSubMatFreeCtx ctx; PetscErrorCode ierr; PetscFunctionBegin; ierr = MatShellGetContext(M,(void **)&ctx);CHKERRQ(ierr); ierr = MatDiagonalSet(ctx->A,D,is);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode MatConvert_SMF(Mat mat,MatType newtype,Mat *NewMat) { PetscErrorCode ierr; PetscMPIInt size; MatSubMatFreeCtx ctx; PetscFunctionBegin; ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr); PetscFunctionReturn(1); }
PetscErrorCode MatEqual_SMF(Mat A,Mat B,PetscBool *flg) { PetscErrorCode ierr; MatSubMatFreeCtx ctx1,ctx2; PetscBool flg1,flg2,flg3; PetscFunctionBegin; ierr = MatShellGetContext(A,(void **)&ctx1);CHKERRQ(ierr); ierr = MatShellGetContext(B,(void **)&ctx2);CHKERRQ(ierr); ierr = ISEqual(ctx1->Rows,ctx2->Rows,&flg2);CHKERRQ(ierr); ierr = ISEqual(ctx1->Cols,ctx2->Cols,&flg3);CHKERRQ(ierr); if (flg2==PETSC_FALSE || flg3==PETSC_FALSE){ *flg=PETSC_FALSE; } else { ierr = MatEqual(ctx1->A,ctx2->A,&flg1);CHKERRQ(ierr); if (flg1==PETSC_FALSE){ *flg=PETSC_FALSE;} else { *flg=PETSC_TRUE;} } PetscFunctionReturn(0); }
PetscErrorCode MyMatMult(Mat H_shell, Vec X, Vec Y) { PetscErrorCode ierr; void *ptr; AppCtx *user; PetscFunctionBegin; ierr = MatShellGetContext(H_shell,&ptr);CHKERRQ(ierr); user = (AppCtx*)ptr; ierr = MatMult(user->H,X,Y);CHKERRQ(ierr); PetscFunctionReturn(0); }