I am an MPI newbie. My program calculates the sum from 1 to 100 but throws back an error and i don't understand why. I am learning MPI_Reduce and MPI_Bcast so i try to use them as much as i can. This is my program.
// include something
int main (int argc, char * argv[])
{
int rank, size, root = 0;
int i,j,k,S[100],n=100,p, sum;
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &size );
//get n
if(rank==root){
n=100;
}
//send data to all process
MPI_Bcast( &n, n, MPI_INT,root, MPI_COMM_WORLD );
p=n/rank;
while(p>0){
for(i=1;i<p;i++){
for(k=0;k<rank;k++){
S[k]=i+i*rank;
}
}
p=p/2;
}
//get data from all process
MPI_Reduce( S, &sum, n, MPI_INT, MPI_SUM, root, MPI_COMM_WORLD );
if(rank==root){
printf("Gia tri cua S trong root: %d", sum);
}
MPI_Finalize();
return 0;
}
And this is my error:
job aborted:
[ranks] message
[0] process exited without calling finalize
[1-4] terminated
---- error analysis -----
[0] on DESKTOP-GFD7NIE
mpi.exe ended prematurely and may have crashed. exit code 0xc0000094
---- error analysis -----
I also have something not clear about MPI, I hope you can help me figure out:
1) if I have code like this:
//include something
int main(){
MPI_Init( &argc, &argv );
int rank, size, root = 0;
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &size );
//code 1
if(rank==0){
//code 2
}
}
It mean every process will execute code 1 and only rank 0 will execute code 2. Is it correct?
2) According to this, function MPI_Reduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm) has recvbuf. But I don't understand it clearly, is it variable will receive data from sendbuf or something else?
Thank for your help.
I modified you program to calculate the sum from 0 to 9 (which is 45). Compile it with mpic++ and run it with 2 process at the beginning, use "cout" in comment to better understand which rank is doing what.
The localsum is the sum of each rank, it gives one integer for each rank.
The globalsum gives one integer in the master process.
#include <iostream>
using namespace std;
int main (int argc, char * argv[])
{
int rank, size, root = 0;
int j,k,S[10],n=10,p;
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &size );
//get n
if(!rank){
n=10;
}
//send data to all process
MPI_Bcast( &n, n, MPI_INT,root, MPI_COMM_WORLD );
int localsum = 0, globalsum = 0 ;
for (int i = rank; i < n; i += size ) {
S[i] = i;
localsum += S[i];
// cout << rank << " " << S[i] << endl;
}
// cout << localsum << endl;
//get data from all process
MPI_Reduce( &localsum, &globalsum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD );
if(!rank){
cout << "Globalsum: " << globalsum << endl;
}
MPI_Finalize();
return 0;
}