xref: /petsc/src/sys/tests/ex16.c (revision d71ae5a4db6382e7f06317b8d368875286fe9008)
1 
2 static char help[] = "Demonstrates PetscMatlabEngineXXX()\n";
3 
4 #include <petscsys.h>
5 #include <petscmatlab.h>
6 
7 int main(int argc, char **argv)
8 {
9   PetscMPIInt rank;
10   char        buffer[256], *output, user[256];
11   PetscBool   userhappy = PETSC_FALSE;
12 
13   PetscFunctionBeginUser;
14   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
15   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
16 
17   PetscCall(PetscMatlabEngineGetOutput(PETSC_MATLAB_ENGINE_(PETSC_COMM_WORLD), &output));
18 
19   PetscCall(PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PETSC_COMM_WORLD), "MPI_Comm_rank"));
20   PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%d]Processor rank is %s", rank, output));
21   PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT));
22   PetscCall(PetscPrintf(PETSC_COMM_WORLD, ">>"));
23   PetscCall(PetscSynchronizedFGets(PETSC_COMM_WORLD, stdin, 256, user));
24   PetscCall(PetscStrncmp(user, "exit", 4, &userhappy));
25   while (!userhappy) {
26     PetscCall(PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PETSC_COMM_WORLD), user));
27     PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%d]The result is %s", rank, output));
28     PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT));
29     PetscCall(PetscPrintf(PETSC_COMM_WORLD, ">>"));
30     PetscCall(PetscSynchronizedFGets(PETSC_COMM_WORLD, stdin, 256, user));
31     PetscCall(PetscStrncmp(user, "exit", 4, &userhappy));
32   }
33   PetscCall(PetscFinalize());
34   return 0;
35 }
36