Actual source code: ex31.c

  1: static const char help[] = "Demonstrates PetscMatlabEngineXXX()\n";

  3: #include <petscvec.h>
  4: #include <petscmatlab.h>

  6: int main(int argc, char **argv)
  7: {
  8:   PetscMPIInt rank;
  9:   PetscInt    n = 5;
 10:   char       *output;
 11:   Vec         x;

 13:   PetscFunctionBeginUser;
 14:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));

 16:   PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
 17:   PetscCall(VecSetSizes(x, PETSC_DECIDE, n));
 18:   PetscCall(VecSetFromOptions(x));

 20:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
 21:   PetscCall(PetscMatlabEngineGetOutput(PETSC_MATLAB_ENGINE_WORLD, &output));
 22:   PetscCall(PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_WORLD, "MPI_Comm_rank"));
 23:   PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%d]Processor rank is\n %s", rank, output));
 24:   PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT));

 26:   PetscCall(PetscObjectSetName((PetscObject)x, "x"));
 27:   PetscCall(PetscMatlabEnginePut(PETSC_MATLAB_ENGINE_WORLD, (PetscObject)x));
 28:   PetscCall(PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_WORLD, "x = x + MPI_Comm_rank;\n"));
 29:   PetscCall(PetscMatlabEngineGet(PETSC_MATLAB_ENGINE_WORLD, (PetscObject)x));

 31:   PetscCall(PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_WORLD, "whos\n"));
 32:   PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%d]The result is\n %s", rank, output));
 33:   PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT));

 35:   PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
 36:   PetscCall(VecDestroy(&x));
 37:   PetscCall(PetscFinalize());
 38:   return 0;
 39: }