distributed algorithm in C

135 views Asked by At

I am a beginner in C. I have to create a distributed architecture with the library MPI. The following code is:

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h>
#include <time.h>
#include <mpi.h>

int main(int argc, char **argv) 
{ 

int N,  w = 1, L = 2, M = 50; // with N number of threads
int T= 2;
int myid;
int buff;
float mit[N][T];                // I initialize a 2d array       
for(int i = 0; i < N; ++i){
    mit[i][0]= M / (float) N; 
    for (int j = 1; j < T; ++j){
            mit[i][j] = 0;
    } 
    }
float tab[T];    // 1d array 
MPI_Status stat; 
/*********************************************
  start 
 *********************************************/
MPI_Init(&argc,&argv);                 // Initialisation
MPI_Comm_size(MPI_COMM_WORLD, &N);   
MPI_Comm_rank(MPI_COMM_WORLD, &myid);      
for(int j = 0; j < T; j++) { 

   for(int i = 0; i < N; i++) {  // I iterate for each slave 

        if (myid !=0) {  

            float y = ((float) rand()) / (float) RAND_MAX; 
            mit[i][j + 1] =  mit[i][j]*(1 + w * L * y);
            buff=mit[i][j+1];
            MPI_Send(&buff, 128, MPI_INT, 0, 0, MPI_COMM_WORLD); // I send the variable buff to the master 
            buff=0; 

}           

   if( myid == 0 )  {  // Master

       for(int i = 1; i < N; i++){ 

           MPI_Recv(&buff, 128, MPI_INT, i, 0, MPI_COMM_WORLD, &stat); 
           tab[j] += buff; // I need to receive all the variables buff sent by the salves, sum them and stock into the tab at the index j 
              }
       printf("\n%.20f\n",tab[j]); // I print the result of the sum at index j 

} 
}
}
MPI_Finalize();
return 0; 
}
}

I use the command in the terminal: mpicc .c -o my_file to compile the program
Then mpirun -np 101 my_file_c to start the program with 101 threads

But the problem is I have the following error int the terminal:

It seems that [at least] one of the processes that was started with
> mpirun did not invoke MPI_INIT before quitting (it is possible that
> more than one process did not invoke MPI_INIT -- mpirun was only
> notified of the first one, which was on node n0).
> 
> mpirun can *only* be used with MPI programs (i.e., programs that
> invoke MPI_INIT and MPI_FINALIZE).  You can use the "lamexec" program
> to run non-MPI programs over the lambooted nodes.

It seems that I have a problem with the master but i don't know why...

Any idea ???

Thank you :)

1

There are 1 answers

0
Gilles Gouaillardet On BEST ANSWER

This behavior is very likely the result of a memory corruption.

You cannot

    int buff=mit[i][j+1];
    MPI_Send(&buff, 128, MPI_INT, ...);

depending on what you want to achieve, you can try instead

    int buff=mit[i][j+1];
    MPI_Send(&buff, 1, MPI_INT, ...);
    // ...
    MPI_Recv(&buff, 1, MPI_INT, ...);

or

    int *buff=&mit[i][j+1];
    MPI_Send(buff, 128, MPI_INT, ...);
    // fix MPI_Recv()