MPI Program help

  • Follow


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);
}
_______________________________________________________________________
0
Reply MM 5/12/2010 4:19:45 PM

Am 12.05.2010, 18:19 Uhr, schrieb MM <malarsr2001@gmail.com>:

> 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.

> 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
0
Reply Michael 5/12/2010 5:03:30 PM


On May 12, 12:03=A0pm, "Michael Hofmann" <michael.hofm...@s2000.tu-
chemnitz.de> wrote:
> Am 12.05.2010, 18:19 Uhr, schrieb MM <malarsr2...@gmail.com>:
>
>
>
>
>
> > 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[])
> > {
> > =A0 int myid, numprocs, localresult, result[MAXSIZE];
> > =A0 int i, tag, final;
> > =A0 MPI_Status status;
>
> > =A0 MPI_Init(&argc, &argv);
> > =A0 MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
> > =A0 MPI_Comm_rank(MPI_COMM_WORLD, &myid);
>
> > =A0 if ( numprocs > MAXSIZE )
> > =A0 {
> > =A0 =A0 =A0 =A0 if ( myid =3D=3D 0 )
> > =A0 =A0 =A0 =A0 =A0 printf("The number of processes must be less than %=
d\n",
> > MAXSIZE);
> > =A0 =A0 =A0 =A0 exit(0);
> > =A0 }
>
> > =A0 final =3D 0;
>
> > =A0 for ( i =3D 0; i < numprocs; i++)
> > =A0 =A0 =A0 result[i] =3D i;
>
> > =A0 MPI_Scatter(result, numprocs, MPI_INT, &localresult, 1, MPI_INT, 0,
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0^^^^^^^^
> This should be "1", because it is the number of elements you send to =A0
> _every_ process.
>
> > MPI_COMM_WORLD);
>
> > =A0 if( myid )
> > =A0 {
> > =A0 =A0 localresult =3D localresult/myid;
> > =A0 =A0 MPI_Reduce(&localresult, &final, 1, MPI_INT, MPI_SUM, 1,
> > MPI_COMM_WORLD);
> > =A0 }
>
> The first process (with myid =3D=3D 0) does not execute MPI_Reduce, but i=
f you =A0
> 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!
0
Reply MM 5/13/2010 9:58:49 PM

2 Replies
244 Views

(page loaded in 0.809 seconds)

Similiar Articles:













7/20/2012 3:22:55 PM


Reply: