'The output of c language mpi program differ with number of processes
I need to write down a parallel code for this formula
I wrote a mpi program in c language for parallel programming
#include <stdio.h>
#include <math.h>
#include<mpi.h>
double sum(int n);
int main(void){
int my_rank,comm_sz, n=1;
double local_sum, total_sum;
int source;
int local_n;
MPI_Init(NULL,NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
local_n= n/comm_sz;
local_sum=sum(local_n);
if ( my_rank != 0) {
MPI_Send (&local_sum , 1, MPI_DOUBLE , 0, 0, MPI_COMM_WORLD ) ;
}
else{
total_sum = local_sum;
for(source=1;source<comm_sz;source++){
MPI_Recv (&local_sum , 1, MPI_DOUBLE , source , 0, MPI_COMM_WORLD , MPI_STATUS_IGNORE ) ;
total_sum+=local_sum;
}
}
if(my_rank==0){
printf("Sum is=%lf",total_sum);
printf("\n");
}
MPI_Finalize();
return 0;
}
double sum(int n){
int i;
double cal_sum=0;
for (i =0;i <= n;i++) {
cal_sum = cal_sum + 4*(pow (-1, i))/((2*i)+1);
}
return cal_sum;
}
The output should be same whatever the number of process is. But as per my code when I run the program for 1 process the result is different than the number of process 8.
For example, if n= 1, p= 1 the summation is 2.667
Whereas, if n= 1, p= 8 the summation is 32.00
But as per my understanding even if p= 8 the result of summation should be 2.667
Solution 1:[1]
There is logical error in your code.
Scenario 1:
When comm_sz = 1;
For the iteration i = 0 : n=1, so loop condition 0<=1 is true
Value of cal_sum will be 4
For the iteration i = 0 : n=1, so loop condition 1<=1 is true
Value of cal_sum will be 4 + - (4/3) = 2.667
Scenario 2:
When comm_sz = 2;
In my_rank = 0;
For the iteration i = 0 : n=0.5, so loop condition 0<=0 is true
So Value of cal_sum will be 4;
For the iteration i = 1 : n=0.5, so loop condition is false
exits the loop with cal_sum as 4;
In my_rank = 1;
For the iteration i = 0 : n=0.5, so loop condition 0<=0 is true
So Value of cal_sum will be 4;
For the iteration i = 1 : n=0.5, so loop condition is false
exits the loop with cal_sum as 4;
Hence the resulting value is comm_sz * 4 = 8.
Basically, the same thing happens, even if you have 8 processes, the sum will be 32.
The result from your code will always be comm_sz*4 if comm_sz > 1.
I hope this will assist you to understand the issue in your code.
You should also consider how to distribute the tasks across different processes.
Hint: Say if you have comm_size=4 and n=8, the local_sum for each process should calculate n/comm_size iterations but with different starting and ending indexes. i.e rank 0 should calculate sum for i=0 to 1, rank 1 for i=2 to i=3 ... rank 4 should calculate i=6 to i=8.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

