MM
2010-05-12 16:19:45 UTC
I am new to MPI program.Please help me to correct the correct the
code.If there's an error please write your comment why its so.Thanks
for your help!
_________________________________________________________________
#include <stdio.h>
#include "mpi.h"
#define MAXSIZE 20
main(int argc, char *argv[])
{
int myid, numprocs, localresult, result[MAXSIZE];
int i, tag, final;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
if ( numprocs > MAXSIZE )
{
if ( myid == 0 )
printf("The number of processes must be less than %d\n",
MAXSIZE);
exit(0);
}
final = 0;
for ( i = 0; i < numprocs; i++)
result[i] = i;
MPI_Scatter(result, numprocs, MPI_INT, &localresult, 1, MPI_INT, 0,
MPI_COMM_WORLD);
if( myid )
{
localresult = localresult/myid;
MPI_Reduce(&localresult, &final, 1, MPI_INT, MPI_SUM, 1,
MPI_COMM_WORLD);
}
if( final )
{
tag = myid;
MPI_Send(&final, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
}
if(myid == 0)
MPI_Recv(&final, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG,
MPI_COMM_WORLD, &status);
if(myid == 0)
printf("The final result is: %d \n", final);
MPI_Finalize();
return (0);
}
_______________________________________________________________________
code.If there's an error please write your comment why its so.Thanks
for your help!
_________________________________________________________________
#include <stdio.h>
#include "mpi.h"
#define MAXSIZE 20
main(int argc, char *argv[])
{
int myid, numprocs, localresult, result[MAXSIZE];
int i, tag, final;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
if ( numprocs > MAXSIZE )
{
if ( myid == 0 )
printf("The number of processes must be less than %d\n",
MAXSIZE);
exit(0);
}
final = 0;
for ( i = 0; i < numprocs; i++)
result[i] = i;
MPI_Scatter(result, numprocs, MPI_INT, &localresult, 1, MPI_INT, 0,
MPI_COMM_WORLD);
if( myid )
{
localresult = localresult/myid;
MPI_Reduce(&localresult, &final, 1, MPI_INT, MPI_SUM, 1,
MPI_COMM_WORLD);
}
if( final )
{
tag = myid;
MPI_Send(&final, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
}
if(myid == 0)
MPI_Recv(&final, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG,
MPI_COMM_WORLD, &status);
if(myid == 0)
printf("The final result is: %d \n", final);
MPI_Finalize();
return (0);
}
_______________________________________________________________________