Matlab and PETSc communication
Barry Smith
bsmith at mcs.anl.gov
Sat Aug 9 19:23:51 CDT 2008
I just installed Matlab R2008a and everything seems to run fine.
You open the socket connection on the Matlab end with a = sreader;
then use b = PetscBinaryRead(a) to read a PETSc object, for
example, sent
from ex12.c (At the same time as calling sreader; you need to run
ex12 or
some other PETSc code that uses the socket viewer)
I see that ex12.m is actually for an older version of PETSc and so
has outdated
commands. I've attached a replacement that should work.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ex12.m
Type: application/octet-stream
Size: 511 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20080809/d012ce23/attachment.obj>
-------------- next part --------------
Regarding sending a Matlab object to PETSc I'll see if I can get
an example
going for you.
Barry
On Aug 8, 2008, at 11:18 AM, Michel Cancelliere wrote:
> Hi,
>
> I am trying to use the function sread() but when I run the matlab
> program it give me this error:
>
> -One or more output arguments not assigned during call to "sread".
>
> and nothing happen... I also tried to use sreader but matlab didn't
> respond, am I doing something wrong? I am using Matlab R2008a, which
> matlab version is the most compatible with Petsc?
>
> Thank you,
>
> Michel
>
> On Mon, Jul 28, 2008 at 8:45 PM, Barry Smith <bsmith at mcs.anl.gov>
> wrote:
>
> On Jul 28, 2008, at 12:33 PM, Michel Cancelliere wrote:
>
> Hi,
>
> Thank you for your response, I think the socket communication
> between Matlab and PETSc is what I need, I took a look to the
> example ex12.c and ex12.m (Simple example to show how to PETSc
> programs can be run from Matlab) but in this case PETSc send the
> Vector solution that matlab "receive", is it possible to make the
> inverse? Send the variable from matlab to PETSc using socket
> communication? can PetscMatlabEngineGet do that ? How it works?
>
> You don't really want to use the Matlab Engine code, because with
> this approach the PETSc program is "in charge" and just sends work
> requests
> to Matlab. Instead you want Matlab in charge and to send work
> requests to PETSc.
>
> The basic idea is that both Matlab and PETSc open a socket
> connection to each other. On the PETSc side use
> PetscViewerSocketOpen() on the Matlab side
> use the bin/matlab/@sreader/sreader.m class to create a (Matlab)
> socket object. Now on the PETSc side you can make calls to VecView()
> to send vectors to
> Matlab and VecLoad() and MatLoad() to read vectors and matrices from
> Matlab. On the Matlab side use PetscBinaryRead() and
> PetscBinaryWrite() to
> read vectors from PETSc and to write vectors and matrices to PETSc.
> Your PETSc program may end up looking something like
>
> PetscViewerSocketOpen(..... &socket);
>
> do forever:
> VecLoad(socket, &b)
> MatLoad(socket,....,&A)
>
> KSPSetOperators(ksp,A,A,....)
> KSPSolve(ksp,b,x)
> VecView(socket,x)
>
> You Matlab program could look something like
>
> socket = sread()
>
> do forever:
>
> Create your vector and matrix
> PetscBinaryWrite(socket,b)
> PetscBinaryWrite(socket,A)
> x = PetscBinaryRead(socket)
>
> The details and correct syntax are obviously not given.
>
> Good luck,
>
> Barry
>
>
>
>
> Aron, I was studying your recommendation but it means that I have to
> write all the function from my Matlab Code to C?, because it should
> take me a lot of time and this work is part of my MSc Thesis and
> maybe I need a less time consuming solution.
>
> Thank you in advance,
>
> Michel
>
> On Tue, Jul 22, 2008 at 8:56 PM, Aron Ahmadia <aja2111 at columbia.edu>
> wrote:
> Michel,
>
> I would recommend investing the time to write your C/C++ wrapper
> code a little higher up around the Newton iteration, since PETSc
> provides a great abstraction interface for it. Then you could write
> code to build the matrix (or assemble a matrix-free routine!) in C/C+
> +, and pass the parameters in from there.
>
> ~Aron
>
>
> On Tue, Jul 22, 2008 at 12:12 AM, Matthew Knepley
> <knepley at gmail.com> wrote:
> On Mon, Jul 21, 2008 at 10:14 PM, Barry Smith <bsmith at mcs.anl.gov>
> wrote:
> >
> > On Jul 21, 2008, at 7:57 PM, Michel Cancelliere wrote:
> >
> >> Hi, I am a new user of PETSc. I am working in Reservoir
> Simulation and I
> >> have been developing the simulator inside Matlab. I have some
> question in
> >> order to understand better my possibilities of success in what I
> want to do:
> >>
> >> ? I want to solve the linear system obtained from the inner
> >> iterations in the newton method using PETSc, is it possible to
> communicate
> >> in an efficient way PETSc with Matlab to do that? I now that I
> can write
> >> binary files and then read with PETSc but due the size of the
> matrix it is a
> >> very time-expensive way. Where i can find some examples? I look
> at the
> >> examples within the package but I could not find it. \
> >> ? It is possible to call PETSc library inside Matlab?
> Using the Mex
> >> files and Matlab compiler?
> >
> > There is no code to do this. It is possible but pretty
> complicated to
> > write the appropriate Mex code. (Because
> > each Mex function is a separate shared library you cannot just
> write a Mex
> > function for each PETSc function since they
> > would not share the PETSc global variables. You would have to
> write one Mex
> > function that is a "gatekeeper" and calls
> > the requested PETSc function underneath. I've monkeyed with this a
> few times
> > but did not have the time/energy/intellect
> > to write code to automate this process. Give me 300,000 dollars
> and we could
> > hire someone to write this :-)
> >
> > You might look at the "newly improved" socket interface in petsc-
> dev
> > (http://www-unix.mcs.anl.gov/petsc/petsc-as/developers/index.html).
> > With this you write a stand alone C PETSc program that waits at a
> socket,
> > receive the matrix and right hand side and then
> > sends back the solution. The code for marshalling the matrices and
> vector is
> > common between the sockets and binary files.
> > On the Matlab side you create a "file" that is actually a socket
> connection.
> > See src/sys/viewer/impls/socket/matlab This may
> > take a little poking around and you asking us a couple of
> questions to get
> > it.
> > Note there is no inherent support for parallelism on the PETSc
> side with
> > this setup but I think it is possible.
>
> I personally think this would be much easier in Sage than in Matlab
> proper. In fact,
> with Sage you could use petsc4py directly, and directly access the
> data structures
> as numpy arrays if necessary.
>
> Matt
>
> > Barry
> >
> >
> >> Thank you very much for your time,
> >>
> >> Michel Cancelliere
> >
> >
>
>
>
> --
> What most experimenters take for granted before they begin their
> experiments is infinitely more interesting than any results to which
> their experiments lead.
> -- Norbert Wiener
>
>
>
>
>
More information about the petsc-users
mailing list