xref: /petsc/doc/manual/matlab.md (revision b11d9968bc79904c690b122f9399be46447eb113)
1*7f296bb3SBarry Smith(ch_matlab)=
2*7f296bb3SBarry Smith
3*7f296bb3SBarry Smith# Using MATLAB with PETSc
4*7f296bb3SBarry Smith
5*7f296bb3SBarry SmithThere are three basic ways to use MATLAB with PETSc:
6*7f296bb3SBarry Smith
7*7f296bb3SBarry Smith1. {any}`sec_matlabdump` into files to be read into
8*7f296bb3SBarry Smith   MATLAB,
9*7f296bb3SBarry Smith2. {any}`sec_matlabsocket` from
10*7f296bb3SBarry Smith   a running PETSc program to a MATLAB process where you may
11*7f296bb3SBarry Smith   interactively type MATLAB commands (or run scripts), and
12*7f296bb3SBarry Smith3. {any}`sec_matlabengine` to send data back
13*7f296bb3SBarry Smith   and forth between PETSc and MATLAB where MATLAB commands are issued,
14*7f296bb3SBarry Smith   not interactively, but from a script or the PETSc program (this uses
15*7f296bb3SBarry Smith   the MATLAB Engine).
16*7f296bb3SBarry Smith
17*7f296bb3SBarry SmithFor the latter two approaches one must `./configure` PETSc with the argument `--with-matlab [--with-matlab-dir=matlab_root_directory]`.
18*7f296bb3SBarry Smith
19*7f296bb3SBarry Smith(sec_matlabdump)=
20*7f296bb3SBarry Smith
21*7f296bb3SBarry Smith## Dumping Data for MATLAB
22*7f296bb3SBarry Smith
23*7f296bb3SBarry Smith### Dumping ASCII MATLAB data
24*7f296bb3SBarry Smith
25*7f296bb3SBarry SmithOne can dump PETSc matrices and vectors to the screen in an ASCII format
26*7f296bb3SBarry Smiththat MATLAB can read in directly. This is done with the command line
27*7f296bb3SBarry Smithoptions `-vec_view ::ascii_matlab` or `-mat_view ::ascii_matlab`. To
28*7f296bb3SBarry Smithwrite a file, use `-vec_view :filename.m:ascii_matlab` or
29*7f296bb3SBarry Smith`-mat_view :filename.m:ascii_matlab`.
30*7f296bb3SBarry Smith
31*7f296bb3SBarry SmithThis causes the PETSc program to print the vectors and matrices every
32*7f296bb3SBarry Smithtime `VecAssemblyEnd()` or `MatAssemblyEnd()` are called. To provide
33*7f296bb3SBarry Smithfiner control over when and what vectors and matrices are dumped one can
34*7f296bb3SBarry Smithuse the `VecView()` and `MatView()` functions with a viewer type of
35*7f296bb3SBarry Smith`PETSCVIEWERASCII` (see `PetscViewerASCIIOpen()`, `PETSC_VIEWER_STDOUT_WORLD`,
36*7f296bb3SBarry Smith`PETSC_VIEWER_STDOUT_SELF`, or `PETSC_VIEWER_STDOUT_(MPI_Comm)`).
37*7f296bb3SBarry SmithBefore calling the viewer set the output type with, for example,
38*7f296bb3SBarry Smith
39*7f296bb3SBarry Smith```
40*7f296bb3SBarry SmithPetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_MATLAB);
41*7f296bb3SBarry SmithVecView(A,PETSC_VIEWER_STDOUT_WORLD);
42*7f296bb3SBarry SmithPetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
43*7f296bb3SBarry Smith```
44*7f296bb3SBarry Smith
45*7f296bb3SBarry SmithThe name of each PETSc variable printed for MATLAB may be set with
46*7f296bb3SBarry Smith
47*7f296bb3SBarry Smith```
48*7f296bb3SBarry SmithPetscObjectSetName((PetscObject)A,"name");
49*7f296bb3SBarry Smith```
50*7f296bb3SBarry Smith
51*7f296bb3SBarry SmithIf no name is specified, the object is given a default name using
52*7f296bb3SBarry Smith`PetscObjectName()`.
53*7f296bb3SBarry Smith
54*7f296bb3SBarry Smith### Dumping Binary Data for MATLAB
55*7f296bb3SBarry Smith
56*7f296bb3SBarry SmithOne can also read PETSc binary files (see
57*7f296bb3SBarry Smith{any}`sec_viewers`) directly into MATLAB via the scripts available
58*7f296bb3SBarry Smithin `$PETSC_DIR/share/petsc/matlab`. This requires less disk space and is
59*7f296bb3SBarry Smithrecommended for all but the smallest data sizes. One can also use
60*7f296bb3SBarry Smith
61*7f296bb3SBarry Smith```
62*7f296bb3SBarry SmithPetscViewerPushFormat(viewer,PETSC_VIEWER_BINARY_MATLAB)
63*7f296bb3SBarry Smith```
64*7f296bb3SBarry Smith
65*7f296bb3SBarry Smithto dump both a PETSc binary file and a corresponding `.info` file
66*7f296bb3SBarry Smithwhich `PetscReadBinaryMatlab.m` will use to format the binary file in
67*7f296bb3SBarry Smithmore complex cases, such as using a `DMDA`. For an example, see
68*7f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/dm/tutorials/ex7.c.html">DM Tutorial ex7</a>.
69*7f296bb3SBarry SmithIn MATLAB one may then generate a useful structure. For
70*7f296bb3SBarry Smithexample:
71*7f296bb3SBarry Smith
72*7f296bb3SBarry Smith```matlab
73*7f296bb3SBarry Smithsetenv('PETSC_DIR','~/petsc');
74*7f296bb3SBarry Smithsetenv('PETSC_ARCH','arch-darwin-double-debug');
75*7f296bb3SBarry Smithaddpath('~/petsc/share/petsc/matlab');
76*7f296bb3SBarry SmithgridData=PetscReadBinaryMatlab('output_file');
77*7f296bb3SBarry Smith```
78*7f296bb3SBarry Smith
79*7f296bb3SBarry Smith(sec_matlabsocket)=
80*7f296bb3SBarry Smith
81*7f296bb3SBarry Smith## Sending Data to an Interactive MATLAB Session
82*7f296bb3SBarry Smith
83*7f296bb3SBarry SmithOne creates a viewer to MATLAB via
84*7f296bb3SBarry Smith
85*7f296bb3SBarry Smith```
86*7f296bb3SBarry SmithPetscViewerSocketOpen(MPI_Comm,char *machine,int port,PetscViewer *v);
87*7f296bb3SBarry Smith```
88*7f296bb3SBarry Smith
89*7f296bb3SBarry Smith(`port` is usually set to `PETSC_DEFAULT`; use `NULL` for the
90*7f296bb3SBarry Smithmachine if the MATLAB interactive session is running on the same machine
91*7f296bb3SBarry Smithas the PETSc program) and then sends matrices or vectors via
92*7f296bb3SBarry Smith
93*7f296bb3SBarry Smith```
94*7f296bb3SBarry SmithVecView(Vec A,v);
95*7f296bb3SBarry SmithMatView(Mat B,v);
96*7f296bb3SBarry Smith```
97*7f296bb3SBarry Smith
98*7f296bb3SBarry SmithSee {any}`sec_viewers` for more on PETSc viewers. One may
99*7f296bb3SBarry Smithstart the MATLAB program manually or use the PETSc command
100*7f296bb3SBarry Smith`PetscStartMatlab(MPI_Comm,char *machine,char *script,FILE **fp)`;
101*7f296bb3SBarry Smithwhere `machine` and `script` may be `NULL`. It is also possible to
102*7f296bb3SBarry Smithstart your PETSc program from MATLAB via `launch()`.
103*7f296bb3SBarry Smith
104*7f296bb3SBarry SmithTo receive the objects in MATLAB, make sure that
105*7f296bb3SBarry Smith`$PETSC_DIR/$PETSC_ARCH/lib/petsc/matlab` and
106*7f296bb3SBarry Smith`$PETSC_DIR/share/petsc/matlab` are in the MATLAB path. Use
107*7f296bb3SBarry Smith`p = PetscOpenSocket();` (or `p = PetscOpenSocket(portnum)` if you
108*7f296bb3SBarry Smithprovided a port number in your call to `PetscViewerSocketOpen()`), and
109*7f296bb3SBarry Smiththen `a = PetscBinaryRead(p);` returns the object passed from PETSc.
110*7f296bb3SBarry Smith`PetscBinaryRead()` may be called any number of times. Each call
111*7f296bb3SBarry Smithshould correspond on the PETSc side with viewing a single vector or
112*7f296bb3SBarry Smithmatrix. `close()` closes the connection from MATLAB. On the PETSc
113*7f296bb3SBarry Smithside, one should destroy the viewer object with
114*7f296bb3SBarry Smith`PetscViewerDestroy()`.
115*7f296bb3SBarry Smith
116*7f296bb3SBarry SmithFor an example, which includes sending data back to PETSc, see
117*7f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/vec/vec/tutorials/ex42.c.html">Vec Tutorial ex42</a>
118*7f296bb3SBarry Smithand the associated `.m` file.
119*7f296bb3SBarry Smith
120*7f296bb3SBarry Smith(sec_matlabengine)=
121*7f296bb3SBarry Smith
122*7f296bb3SBarry Smith## Using the MATLAB Compute Engine
123*7f296bb3SBarry Smith
124*7f296bb3SBarry SmithOne creates access to the MATLAB engine via
125*7f296bb3SBarry Smith
126*7f296bb3SBarry Smith```
127*7f296bb3SBarry SmithPetscMatlabEngineCreate(MPI_Comm comm,char *machine,PetscMatlabEngine *e);
128*7f296bb3SBarry Smith```
129*7f296bb3SBarry Smith
130*7f296bb3SBarry Smithwhere `machine` is the name of the machine hosting MATLAB (`NULL`
131*7f296bb3SBarry Smithmay be used for localhost). One can send objects to MATLAB via
132*7f296bb3SBarry Smith
133*7f296bb3SBarry Smith```
134*7f296bb3SBarry SmithPetscMatlabEnginePut(PetscMatlabEngine e,PetscObject obj);
135*7f296bb3SBarry Smith```
136*7f296bb3SBarry Smith
137*7f296bb3SBarry SmithOne can get objects via
138*7f296bb3SBarry Smith
139*7f296bb3SBarry Smith```
140*7f296bb3SBarry SmithPetscMatlabEngineGet(PetscMatlabEngine e,PetscObject obj);
141*7f296bb3SBarry Smith```
142*7f296bb3SBarry Smith
143*7f296bb3SBarry SmithSimilarly, one can send arrays via
144*7f296bb3SBarry Smith
145*7f296bb3SBarry Smith```
146*7f296bb3SBarry SmithPetscMatlabEnginePutArray(PetscMatlabEngine e,int m,int n,PetscScalar *array,char *name);
147*7f296bb3SBarry Smith```
148*7f296bb3SBarry Smith
149*7f296bb3SBarry Smithand get them back via
150*7f296bb3SBarry Smith
151*7f296bb3SBarry Smith```
152*7f296bb3SBarry SmithPetscMatlabEngineGetArray(PetscMatlabEngine e,int m,int n,PetscScalar *array,char *name);
153*7f296bb3SBarry Smith```
154*7f296bb3SBarry Smith
155*7f296bb3SBarry SmithOne cannot use MATLAB interactively in this mode but one can send MATLAB
156*7f296bb3SBarry Smithcommands via
157*7f296bb3SBarry Smith
158*7f296bb3SBarry Smith```
159*7f296bb3SBarry SmithPetscMatlabEngineEvaluate(PetscMatlabEngine,"format",...);
160*7f296bb3SBarry Smith```
161*7f296bb3SBarry Smith
162*7f296bb3SBarry Smithwhere `format` has the usual `printf()` format. For example,
163*7f296bb3SBarry Smith
164*7f296bb3SBarry Smith```
165*7f296bb3SBarry SmithPetscMatlabEngineEvaluate(PetscMatlabEngine,"x = \%g *y + z;",avalue);
166*7f296bb3SBarry Smith```
167*7f296bb3SBarry Smith
168*7f296bb3SBarry SmithThe name of each PETSc variable passed to MATLAB may be set with
169*7f296bb3SBarry Smith
170*7f296bb3SBarry Smith```
171*7f296bb3SBarry SmithPetscObjectSetName((PetscObject)A,"name");
172*7f296bb3SBarry Smith```
173*7f296bb3SBarry Smith
174*7f296bb3SBarry SmithText responses can be returned from MATLAB via
175*7f296bb3SBarry Smith
176*7f296bb3SBarry Smith```
177*7f296bb3SBarry SmithPetscMatlabEngineGetOutput(PetscMatlabEngine,char **);
178*7f296bb3SBarry Smith```
179*7f296bb3SBarry Smith
180*7f296bb3SBarry Smithor
181*7f296bb3SBarry Smith
182*7f296bb3SBarry Smith```
183*7f296bb3SBarry SmithPetscMatlabEnginePrintOutput(PetscMatlabEngine,FILE*).
184*7f296bb3SBarry Smith```
185*7f296bb3SBarry Smith
186*7f296bb3SBarry SmithThere is a short-cut to starting the MATLAB engine with
187*7f296bb3SBarry Smith`PETSC_MATLAB_ENGINE_(MPI_Comm)`.
188*7f296bb3SBarry Smith
189*7f296bb3SBarry SmithIf you are running PETSc on a cluster (or machine) that does not have a license for MATLAB, you might be able to run MATLAB on the
190*7f296bb3SBarry Smith`head node` of the cluster or some other machine accessible to the cluster using the `-matlab_engine_host hostname` option.
191*7f296bb3SBarry Smith
192*7f296bb3SBarry Smith## Licensing the MATLAB Compute Engine on a cluster
193*7f296bb3SBarry Smith
194*7f296bb3SBarry SmithTo activate MATLAB on head node which does not have access to the internet. [^matlabsection-footnote]
195*7f296bb3SBarry Smith
196*7f296bb3SBarry SmithFirst ssh into the head node using the command: ssh node_name
197*7f296bb3SBarry Smith
198*7f296bb3SBarry SmithObtain the Host Id using the command: ip addr | grep ether [^matlabsection-footnote2]
199*7f296bb3SBarry SmithYou will see something like this: link/ether xx:xx:xx:xx:xx:xx ABC yy:yy:yy:yy:yy:yy
200*7f296bb3SBarry SmithNote the value: xx:xx:xx:xx:xx:xx
201*7f296bb3SBarry Smith
202*7f296bb3SBarry SmithLogin to your MathWorks Account from a computer which has internet access. You will see the available license that your account has. Select a license from the list.
203*7f296bb3SBarry Smith
204*7f296bb3SBarry Smith:::{figure} /images/manual/mathworks-account.png
205*7f296bb3SBarry Smith:::
206*7f296bb3SBarry Smith
207*7f296bb3SBarry SmithThen, select Install and Activate option and select the Activate to Retrieve License File option.
208*7f296bb3SBarry Smith
209*7f296bb3SBarry Smith> :::{figure} /images/manual/mathworks-account-2.png
210*7f296bb3SBarry Smith> :::
211*7f296bb3SBarry Smith
212*7f296bb3SBarry SmithEnter the information and click Continue.
213*7f296bb3SBarry Smith
214*7f296bb3SBarry Smith:::{figure} /images/manual/mathworks-account-3.png
215*7f296bb3SBarry Smith:::
216*7f296bb3SBarry Smith
217*7f296bb3SBarry SmithAn option to download the License file will appear. Download it and copy the license file to the cluster (your home directory).
218*7f296bb3SBarry SmithNow, launch MATLAB where you have sshed into your head node.
219*7f296bb3SBarry Smith
220*7f296bb3SBarry Smith:::{figure} /images/manual/mathworks-account-4.png
221*7f296bb3SBarry Smith:::
222*7f296bb3SBarry Smith
223*7f296bb3SBarry SmithSelect the Activate manually without the internet option and click Next >.
224*7f296bb3SBarry SmithBrowse and locate the license file.
225*7f296bb3SBarry Smith
226*7f296bb3SBarry Smith:::{figure} /images/manual/mathworks-account-5.png
227*7f296bb3SBarry Smith:::
228*7f296bb3SBarry Smith
229*7f296bb3SBarry SmithMATLAB is activated and ready to use.
230*7f296bb3SBarry Smith
231*7f296bb3SBarry Smith```{rubric} Footnotes
232*7f296bb3SBarry Smith```
233*7f296bb3SBarry Smith
234*7f296bb3SBarry Smith[^matlabsection-footnote]: <https://www.mathworks.com/matlabcentral/answers/259627-how-do-i-activate-matlab-or-other-mathworks-products-without-an-internet-connection>
235*7f296bb3SBarry Smith
236*7f296bb3SBarry Smith[^matlabsection-footnote2]: <http://www.mathworks.com/matlabcentral/answers/101892>
237