Newsgroups: comp.parallel.mpi
From: jskim@rose.etri.re.kr (Kim Jungsun)
Subject: Re: MPI_Op_create example needed
Organization: Korea Research Environment Open Network (KREONet)
Date: 5 Dec 1995 07:51:36 GMT
Message-ID: <4a0tm8$2eu@news.kreonet.re.kr>

In article <49vfr9$2s2@ccnet3.ccnet.com>, panda@ccnet.com (KandT) writes:
|> I have been trying to get the MPI_Op_create() feature to work 
|> in the 1.0.11 version of MPICH but without much success.  Any
|> working examples of such would be most gratefully appreciated.
|> 
|> -- thanks, tom faulkner
|>    snl/ca
|>    panda@ccnet.com
|> 


Maybe you can find some informations from any of the public MPI distributions
by consulting how the predefined functions like MPI_MIN etc are defined.
For example, if you have a MPICH implementation, (Woops! you've already
mentioned it) check $(TOP)/src/col/global_ops.c file.

Attached is another DUMMY example for MPI_Op_create. Hope this help you.

Kim, Jungsun

+=====================================================================+
| Jungsun Kim, PhD                 Parallel Programming Section       |
|                                  System Research Department         |
| Email: jskim@rose.etri.re.kr     Electronics And Telecommunications |
| Phone : +82-42-860-6699              Research Institute             |
| Fax   : +82-42-860-6645          P.O.Box 106, Yusong                |
|                                  Daejon, 305-600, KOREA             |
+=====================================================================+

----------------------------------------------------------
#include <stdio.h>
#include <mpi.h>

struct dum {
	double dd;
	int xx;
};

#define mpi_MAX(a,b) (((b)>(a)) ? (b):(a))

void
Ops(void *invec, void *inoutvec, int *len, MPI_Datatype *datatype)
{
	struct dum *a, *b;
	int i, leng = *len;

	a = (struct dum*) invec;
	b = (struct dum*) inoutvec;
	for (i = 0; i < leng; i++)
	{
		b[i].dd = mpi_MAX(a[i].dd, b[i].dd);
		b[i].xx = mpi_MAX(a[i].xx, b[i].xx);
	}

}

int		array_of_blkleng[2] = {1, 1};
MPI_Aint	array_of_disps[2];
MPI_Datatype	array_of_types[2] = { MPI_DOUBLE, MPI_INT };
MPI_Datatype	NewType;

main(int argc, char *argv[])
{
	int	myrank;
	int	mydata, finaldata;
	MPI_Aint	extent;
	struct dum Dum, Dum2;
	MPI_Op	newOps;

	MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
	mydata = myrank;

	MPI_Type_extent(MPI_DOUBLE, &extent);
	array_of_disps[0] = 0;
	array_of_disps[1] = extent;
	MPI_Type_struct(2, array_of_blkleng, array_of_disps,
			array_of_types, &NewType);
	MPI_Type_commit(&NewType);

	MPI_Op_create(Ops, 1, &newOps);

	Dum.dd = 1536.0;
	Dum.xx = myrank;

	MPI_Allreduce(&Dum, &Dum2, 1, NewType, newOps, MPI_COMM_WORLD);

	printf("Dum.dd = %lf, Dum.xx = %d\n", Dum2.dd, Dum2.xx);

	MPI_Finalize();
}

