Discussion:
MPI Program help
(too old to reply)
MM
2010-05-12 16:19:45 UTC
Permalink
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);
}
_______________________________________________________________________
Michael Hofmann
2010-05-12 17:03:30 UTC
Permalink
Post by MM
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,
^^^^^^^^
This should be "1", because it is the number of elements you send to
_every_ process.
Post by MM
MPI_COMM_WORLD);
if( myid )
{
localresult = localresult/myid;
MPI_Reduce(&localresult, &final, 1, MPI_INT, MPI_SUM, 1,
MPI_COMM_WORLD);
}
The first process (with myid == 0) does not execute MPI_Reduce, but if you
use MPI_COMM_WORLD all processes have to call MPI_Reduce!


Michael
MM
2010-05-13 21:58:49 UTC
Permalink
Post by MM
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,
                         ^^^^^^^^
This should be "1", because it is the number of elements you send to  
_every_ process.
Post by MM
MPI_COMM_WORLD);
  if( myid )
  {
    localresult = localresult/myid;
    MPI_Reduce(&localresult, &final, 1, MPI_INT, MPI_SUM, 1,
MPI_COMM_WORLD);
  }
The first process (with myid == 0) does not execute MPI_Reduce, but if you  
use MPI_COMM_WORLD all processes have to call MPI_Reduce!
Michael- Hide quoted text -
- Show quoted text -
Thanks for your message.I still didn't understand.I am very new to
this program.Can you please show me the error in detail.Thanks!

Loading...