int main(int argc,char **argv) { PetscErrorCode ierr; PetscMPIInt rank; char buffer[256],*output,user[256]; PetscBool userhappy = PETSC_FALSE; PetscInitialize(&argc,&argv,(char*)0,help); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank); ierr = PetscMatlabEngineGetOutput(PETSC_MATLAB_ENGINE_(PETSC_COMM_WORLD),&output);CHKERRQ(ierr); ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PETSC_COMM_WORLD),"MPI_Comm_rank");CHKERRQ(ierr); ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d]Processor rank is %s",rank,output);CHKERRQ(ierr); ierr = PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,">>");CHKERRQ(ierr); ierr = PetscSynchronizedFGets(PETSC_COMM_WORLD,stdin,256,user);CHKERRQ(ierr); ierr = PetscStrncmp(user,"exit",4,&userhappy);CHKERRQ(ierr); while (!userhappy) { ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PETSC_COMM_WORLD),user);CHKERRQ(ierr); ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d]The result is %s",rank,output);CHKERRQ(ierr); ierr = PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,">>");CHKERRQ(ierr); ierr = PetscSynchronizedFGets(PETSC_COMM_WORLD,stdin,256,user);CHKERRQ(ierr); ierr = PetscStrncmp(user,"exit",4,&userhappy);CHKERRQ(ierr); } ierr = PetscFinalize(); return 0; }
PetscErrorCode MatSolve_Matlab(Mat A,Vec b,Vec x) { PetscErrorCode ierr; const char *_A,*_b,*_x; PetscFunctionBegin; /* make sure objects have names; use default if not */ ierr = PetscObjectName((PetscObject)b); CHKERRQ(ierr); ierr = PetscObjectName((PetscObject)x); CHKERRQ(ierr); ierr = PetscObjectGetName((PetscObject)A,&_A); CHKERRQ(ierr); ierr = PetscObjectGetName((PetscObject)b,&_b); CHKERRQ(ierr); ierr = PetscObjectGetName((PetscObject)x,&_x); CHKERRQ(ierr); ierr = PetscMatlabEnginePut(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),(PetscObject)b); CHKERRQ(ierr); ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"%s = u%s\\(l%s\\(p%s*%s));",_x,_A,_A,_A,_b); CHKERRQ(ierr); ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"%s = 0;",_b); CHKERRQ(ierr); /* ierr = PetscMatlabEnginePrintOutput(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),stdout);CHKERRQ(ierr); */ ierr = PetscMatlabEngineGet(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),(PetscObject)x); CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode FormFunctionMatlab(SNES snes,Vec X,Vec F,void *ptr) { AppCtx *user = (AppCtx*)ptr; PetscErrorCode ierr; PetscInt Mx,My; PetscReal lambda,hx,hy; Vec localX,localF; MPI_Comm comm; DM da; PetscFunctionBeginUser; ierr = SNESGetDM(snes,&da);CHKERRQ(ierr); ierr = DMGetLocalVector(da,&localX);CHKERRQ(ierr); ierr = DMGetLocalVector(da,&localF);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject)localX,"localX");CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject)localF,"localF");CHKERRQ(ierr); ierr = DMDAGetInfo(da,PETSC_IGNORE,&Mx,&My,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);CHKERRQ(ierr); lambda = user->param; hx = 1.0/(PetscReal)(Mx-1); hy = 1.0/(PetscReal)(My-1); ierr = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr); /* Scatter ghost points to local vector,using the 2-step process DMGlobalToLocalBegin(),DMGlobalToLocalEnd(). By placing code between these two statements, computations can be done while messages are in transition. */ ierr = DMGlobalToLocalBegin(da,X,INSERT_VALUES,localX);CHKERRQ(ierr); ierr = DMGlobalToLocalEnd(da,X,INSERT_VALUES,localX);CHKERRQ(ierr); ierr = PetscMatlabEnginePut(PETSC_MATLAB_ENGINE_(comm),(PetscObject)localX);CHKERRQ(ierr); ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(comm),"localF=ex5m(localX,%18.16e,%18.16e,%18.16e)",hx,hy,lambda);CHKERRQ(ierr); ierr = PetscMatlabEngineGet(PETSC_MATLAB_ENGINE_(comm),(PetscObject)localF);CHKERRQ(ierr); /* Insert values into global vector */ ierr = DMLocalToGlobalBegin(da,localF,INSERT_VALUES,F);CHKERRQ(ierr); ierr = DMLocalToGlobalEnd(da,localF,INSERT_VALUES,F);CHKERRQ(ierr); ierr = DMRestoreLocalVector(da,&localX);CHKERRQ(ierr); ierr = DMRestoreLocalVector(da,&localF);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode MatDestroy_matlab(Mat A) { PetscErrorCode ierr; const char *_A; PetscFunctionBegin; ierr = PetscObjectGetName((PetscObject)A,&_A);CHKERRQ(ierr); ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"delete %s l_%s u_%s;",_A,_A,_A);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode MatLUFactorNumeric_Matlab(Mat F,Mat A,const MatFactorInfo *info) { PetscErrorCode ierr; size_t len; char *_A,*name; PetscReal dtcol = info->dtcol; PetscFunctionBegin; if (F->factortype == MAT_FACTOR_ILU || info->dt > 0) { /* the ILU form is not currently registered */ if (info->dtcol == PETSC_DEFAULT) dtcol = .01; F->ops->solve = MatSolve_Matlab; F->factortype = MAT_FACTOR_LU; ierr = PetscMatlabEnginePut(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),(PetscObject)A); CHKERRQ(ierr); _A = ((PetscObject)A)->name; ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"info_%s = struct('droptol',%g,'thresh',%g);",_A,info->dt,dtcol); CHKERRQ(ierr); ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"[l_%s,u_%s,p_%s] = luinc(%s',info_%s);",_A,_A,_A,_A,_A); CHKERRQ(ierr); ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"%s = 0;",_A); CHKERRQ(ierr); ierr = PetscStrlen(_A,&len); CHKERRQ(ierr); ierr = PetscMalloc1(len+2,&name); CHKERRQ(ierr); sprintf(name,"_%s",_A); ierr = PetscObjectSetName((PetscObject)F,name); CHKERRQ(ierr); ierr = PetscFree(name); CHKERRQ(ierr); } else { ierr = PetscMatlabEnginePut(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),(PetscObject)A); CHKERRQ(ierr); _A = ((PetscObject)A)->name; ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"[l_%s,u_%s,p_%s] = lu(%s',%g);",_A,_A,_A,_A,dtcol); CHKERRQ(ierr); ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"%s = 0;",_A); CHKERRQ(ierr); ierr = PetscStrlen(_A,&len); CHKERRQ(ierr); ierr = PetscMalloc1(len+2,&name); CHKERRQ(ierr); sprintf(name,"_%s",_A); ierr = PetscObjectSetName((PetscObject)F,name); CHKERRQ(ierr); ierr = PetscFree(name); CHKERRQ(ierr); F->ops->solve = MatSolve_Matlab; } PetscFunctionReturn(0); }