Hi,

i am trying to implement a program using (open) mpi that sends groups of numbers to each process which calculate the sum and return it to the master which in turn calculates to the total sum.

i am new to mpi and c programming.

can anyone tell me what is wrong in my code please.

Code:

#include <stdio.h>
#include "mpi.h"
main(int argc, char ** argv)
{
int my_rank;
int source, dest;
int tag=1234;
int namelen;
char msg[100];
char processor_name[MPI_MAX_PROCESSOR_NAME];
int startInt = 1;
int endInt = 1000;
int nb = (endInt-startInt)+1;
int numbers[nbInt];
int test[nbInt];
int i, j, k, n, s;
int part_sum;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &processes);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Get_processor_name(processor_name,&namelen);
for(i=0, k=startInt; i<nb; i++, k++)
{
numbers[i] = k;
}
if(my_rank != 0)
{
MPI_Recv(&numbers[n], 100, MPI_INTEGER, source, tag, MPI_COMM_WORLD, &status);
part_sum = 0;
dest = 0;
for(i=0; i<s; i++)
{
part_sum = part_sum + numbers[i];
}
MPI_Send(&part_sum, 1, MPI_INTEGER, dest, tag, MPI_COMM_WORLD);
}
else
{
s = nb/processes;
for(j=0, n=0; j<processes; j++, n=n+s)
{
MPI_Send(&numbers[n], s, MPI_INTEGER, j, tag, MPI_COMM_WORLD);
}
int total_sum = 0;
for(i=0; i<processes; i++)
{
MPI_Recv(&part_sum, 100, MPI_INTEGER, i, tag, MPI_COMM_WORLD, &status);
total_sum += part_sum;
}
printf("%i\n",total_sum);
}
MPI_Finalize();
return 0;
}

thanks in advance.