C中的分布式算法

问题描述 投票:-1回答:1

我是C的初学者。我必须使用库MPI创建分布式架构。以下代码是:

#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; 
}
}

我在终端中使用命令:mpicc .c -o my_file来编译程序 然后mpirun -np 101 my_file_c用101个线程启动程序

但问题是我在终端中有以下错误:

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.

看来我和主人有问题,但我不知道为什么......

任何的想法 ???

谢谢 :)

c compiler-errors mpi distributed-system distributed-algorithm
1个回答
1
投票

此行为很可能是内存损坏的结果。

你不能

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

根据你想要达到的目的,你可以尝试

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

要么

    int *buff=&mit[i][j+1];
    MPI_Send(buff, 128, MPI_INT, ...);
    // fix MPI_Recv()
© www.soinside.com 2019 - 2024. All rights reserved.