MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: MathLink and MPI

  • To: mathgroup at smc.vnet.net
  • Subject: [mg29445] Re: MathLink and MPI
  • From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
  • Date: Wed, 20 Jun 2001 04:36:25 -0400 (EDT)
  • Organization: Universitaet Leipzig
  • References: <9gn6h2$emp$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

can you tell me for what do you need MPI ?
If you have alread MathLink you have a high level
communication protocol for the data exchange.

You can have a 2 process parallel initial value 
solver that use MathLink only and can be called
from the kernel as an ordinary Mathematica/MathLink
function. On a dual processor machine you can call it
with an ordinary Install[] command, for the connection of two
or three computers some rsh calls may be usefull.

The source has been tested on Sun and Linux machines.

Mail me direct, to obtain the source.

Regards
  Jens

Kashif Rasul wrote:
> 
> Hello,
> 
> I was wondering if anyone has tried using MathLink with MPI (message
> passing interface), with this I mean, a program that say gets its input
> from Mathematica via MathLink and does the calculation in parallel via
> MPI and returns the answer back to Mathematica again via MathLink.
> 
> I was experimenting with such a program, and was curious to know what
> the best/most elegant way of doing this would be? The problem I think is
> that all the other processes need to know if MathLink is still up and
> hence continue waiting for a message but only one process needs to be
> running MathLink. What this does is that the other processes spend a lot
> of cpu waiting when the program is doing nothing. My example is a
> MathLink program to calculate pi:
> 
> :Begin:
> :Function:       mlpi
> :Pattern:        mlpi[n_Integer]
> :Arguments:      {n}
> :ArgumentTypes:  {Integer}
> :Return:
> 
> #include <mathlink.h>
> #include <mpi.h>
> 
> double mlpi( int n )
> {
>     int myid, numprocs, i, retval=0;
>     double mypi, pi, h, sum, x;
> 
>     MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
>     MPI_Comm_rank(MPI_COMM_WORLD,&myid);
> 
>     MPI_Bcast(&retval, 1, MPI_INT, 0, MPI_COMM_WORLD);
>     MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
> 
>     h   = 1.0 / (double) n;
>     sum = 0.0;
>     for (i = myid + 1; i <= n; i += numprocs) {
>      x = h * ((double)i - 0.5);
>         sum += (4.0 / (1.0 + x*x));
>      }
>      mypi = h * sum;
>      MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
> 
>     return pi;
> }
> 
> int main( int argc, char *argv[] )
> {
>  int n, myid, numprocs, i, retval=0;
>  double mypi, pi, h, sum, x;
> 
>  MPI_Init(&argc,&argv);
>  MPI_Comm_rank(MPI_COMM_WORLD,&myid);
>  MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
> 
>  if (myid==0) {
>   retval = MLMain(argc, argv);
>   MPI_Bcast(&retval, 1, MPI_INT, 0, MPI_COMM_WORLD);
>  } else {
>   while (1) {
>    MPI_Bcast(&retval, 1, MPI_INT, 0, MPI_COMM_WORLD);
>    if (!retval) {
>     MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
>     h   = 1.0 / (double) n;
>         sum = 0.0;
>         for (i = myid + 1; i <= n; i += numprocs) {
>          x = h * ((double)i - 0.5);
>             sum += (4.0 / (1.0 + x*x));
>          }
>          mypi = h * sum;
>          MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0,
>      MPI_COMM_WORLD);
>            } else
>     break;
>   }
>  }
>  MPI_Finalize();
>  return 0;
> }
> 
> I start it say on two processors as
> 
> $ mpirun -np 2 mlpi -linkcreate
> $ Link created on: 34715 at ...
> 
> and then l install and run it in Mathematica with:
> 
> link = Install[LinkConnect["34715 at ..."]]
> mlpi[10]
> 
> However when the program is doing nothing, one of the mlpi program takes
> a lot of cpu just waiting for me to call mlpi[...] again.
> 
> I would appreciate any comments on this.
> 
> Thanks in advance.
> 
> Kashif


  • Prev by Date: Re: Re: Errors im Mathematica??
  • Next by Date: Re: Rows
  • Previous by thread: MathLink and MPI
  • Next by thread: Re: MathLink and MPI