Newsgroups: comp.parallel.mpi
From: Frank Meisgen <meisgen@informatik.uni-koeln.de>
Subject: Re: C++/MPI
Organization: Institut fuer Informatik, Lehrstuhl III
Date: Mon, 14 Oct 1996 15:36:57 +0200
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-ID: <326234AD.3BAB@informatik.uni-koeln.de>

Jim Webber wrote:
> 
> Is anyone using MPI with C++?
>
yes, I do.
 
> In particular I would like to know about sending C++ objects as messages?
> 
> Is this possible?

It is similar to the transfer of structures, for example:

#include "mpi.h"
#include <iostream.h>
#include <string.h>

   
class A {
  public:
    void CreateType(MPI_Datatype* dt);
    void Set(int mi, char* mc) { i = mi; strcpy(c,mc);};
    void Out(int id) {cout << id << ": " << i << ", " << c << endl; }  
  private:
    float dummy;
    int i;
    char c[10];
};


void A::CreateType(MPI_Datatype* dt) 
{

	// See also example 3.34, MPI Standard 1.1, June 12, 1995    
	MPI_Datatype types[2] = {MPI_INT, MPI_CHAR};
	int blk[2] = {1,10};
	MPI_Aint dsp[2];
	MPI_Address(&i,dsp);
	MPI_Address(&c,dsp+1);

	MPI_Aint base;
	MPI_Address(this, &base);
	dsp[0] -= base; dsp[1] -= base;
	MPI_Type_struct(2, blk, dsp, types, dt);
	MPI_Type_commit(dt);
}


int main(int argc,char* argv[]) 
{
    int me;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &me);

    MPI_Datatype a_type;
    A a;
    a.CreateType(&a_type);

    if (me == 0) {
	a.Set(9, "Hello You");
    }
	
    MPI_Bcast(&a, 1, a_type, 0, MPI_COMM_WORLD);
    a.Out(me); 

    MPI_Finalize();
}

RESULT:

mpirun -np 5 broad
solaris
1: 9, Hello You
4: 9, Hello You
0: 9, Hello You
2: 9, Hello You
3: 9, Hello You

(mpich 1.0.12 on a SUN Workstation Cluster)

BUT: 
There is no support for derived classes, so you have 
to create a new type for each class or write some virtual
functions, which make it easier to derive transferable classes.


-- 
___________________________________________________________________
Frank Meisgen               email:  meisgen@informatik.uni-koeln.de 
Institut fuer Informatik      tel:  +49 211 470-5376                
Universitaet zu Koeln         fax:  +49 211 470-5387		  
___________________________________________________________________

