Newsgroups: comp.parallel.mpi
From: Joao Macedo <cgmacedo.pg@swansea.ac.uk>
Subject: Problem with MPI
Organization: University of Wales Swansea
Date: Thu, 21 Nov 1996 12:16:34 +0000
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii; name="news_mesg"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="news_mesg"
Message-ID: <32944822.41C6@swansea.ac.uk>

Hi there:

The following program (which can only be used by 2 processes), works as
expected.

But, if I uncomment line

/* #define USING_MPI_Probe */

the program just does not work anymore.

What am I doing wrong whit MPI_Probe?

Thanks a lot!
Joao

/* BEGIN OF PROGRAM */
/************************/
/* Unix> mpicc -o x x.c */
/* Unix> x -np 2        */
/************************/

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

/* #define USING_MPI_Probe */
#define SIZE              10
#define BOUNDARY_DATA_TAG 0x1

int	main( int argc, char *argv[])
{
	FILE         * fp;
	char	     file[127];
	int	     send_buf[SIZE], recv_buf[SIZE], i, nprocs = 0, my_id, flag;
	MPI_Request  request_send, request_recv;
	MPI_Status   status;

	MPI_Init(&argc, &argv);
	MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
	MPI_Comm_rank(MPI_COMM_WORLD, &my_id);

	if(nprocs==2)
	{

		if (!my_id) /* If I'm master then I clean files ... */ {
			unlink("out.0_of_2");
			unlink("out.1_of_2");
		}

		printf("[%d] - Before MPI_Irecv   ...\n", my_id);
		fflush(stdout);
		MPI_Irecv(recv_buf,
		    SIZE,
		    MPI_INT,
		    (int)(!my_id),
		    BOUNDARY_DATA_TAG,
		    MPI_COMM_WORLD,
		    &request_recv);

		printf("[%d] - Before MPI_Barrier ...\n", my_id);
		fflush(stdout);
		MPI_Barrier(MPI_COMM_WORLD); /* Be sure everybody had prepared 
					      * his receiving buf
					      */
		/* Fill send_buf ... */
		for (i = 0; i < SIZE; ++i)
			send_buf[i] = (1 + my_id) * i;

		/* send data */
		printf("[%d] - Before MPI_Irsend  ...\n", my_id);
		fflush(stdout);
		MPI_Irsend(send_buf,
		    SIZE,
		    MPI_INT,
		    (int)(!my_id),
		    BOUNDARY_DATA_TAG,
		    MPI_COMM_WORLD,
		    &request_send);

#ifdef USING_MPI_Probe

		printf("[%d] - Before MPI_Probe   ...\n", my_id);
		fflush(stdout);
		MPI_Probe(MPI_ANY_SOURCE, BOUNDARY_DATA_TAG, MPI_COMM_WORLD,
		    &status);

#else /* #ifdef USING_MPI_Probe */

		printf("[%d] - Before MPI_Test    ...\n", my_id);
		fflush(stdout);
		flag = 0;
		while (MPI_Test(&request_recv, &flag, &status), !flag)
			;

#endif /* #ifdef USING_MPI_Probe */

		/* print send_buf and recv_buf in a private file */
		sprintf(file, "out.%d_of_2", my_id);
		fp = fopen(file, "w");
		fprintf(fp, "\n     my_id=%3d\nsend_buf  =", my_id);
		for (i = 0; i < SIZE; ++i)
			fprintf(fp, "%3d", send_buf[i]);
		fprintf(fp, "\nMPI_SOURCE=%3d\nrecv_buf  =", status.MPI_SOURCE);
		for (i = 0; i < SIZE; ++i)
			fprintf(fp, "%3d", recv_buf[i]);
		fprintf(fp, "\n");
		fclose(fp);

		MPI_Barrier(MPI_COMM_WORLD); /* sure everybody wrote his file */
		if (!my_id)
			system("cat out.*_of_2");

	}
	else
		if(!my_id)
			fprintf(stderr,"Can only run 2 processes!\n");
	MPI_Finalize();

	return(0);
}
/* END OF PROGRAM */


