【发布时间】:2018-04-26 04:16:54
【问题描述】:
我是 MPI 的新手,我正在尝试开发一个非阻塞程序(使用 Isend 和 Irecv)。功能非常基础(很有教育意义):
- 有一个进程(等级 0)是主进程并从从属进程(等级 1-P)接收消息。 master 只接收结果。
- slave 生成一个介于 0 和 R 之间的 N 个随机数数组,然后它们对这些数字进行一些运算(同样,这只是为了教育目的,这些运算没有任何意义)
- 整个过程(操作 + 发送数据)执行 M 次(这只是为了比较不同的实现;阻塞和非阻塞)
当我调用 MPI_waitall() 函数时,我在主进程中遇到分段错误
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
#include <math.h>
#include <time.h>
#define M 1000 //Number of times
#define N 2000 //Quantity of random numbers
#define R 1000 //Max value of random numbers
double SumaDeRaices (double*);
int main(int argc, char* argv[]) {
int yo; /* rank of process */
int p; /* number of processes */
int dest; /* rank of receiver */
/* Start up MPI */
MPI_Init(&argc, &argv);
/* Find out process rank */
MPI_Comm_rank(MPI_COMM_WORLD, &yo);
/* Find out number of processes */
MPI_Comm_size(MPI_COMM_WORLD, &p);
MPI_Request reqs[p-1];
MPI_Status stats[p-1];
if (yo == 0) {
int i,j;
double result;
clock_t inicio,fin;
inicio = clock();
for(i = 0; i<M; i++){ //M times
for(j = 1; j<p; j++){ //for every slave
MPI_Irecv(&result, sizeof(double), MPI_DOUBLE, j, i, MPI_COMM_WORLD, &reqs[j-1]);
}
MPI_Waitall(p-1,reqs,stats); //wait all slaves (SEG_FAULT)
}
fin = clock()-inicio;
printf("Tiempo total de ejecucion %f segundos \n", ((double)fin)/CLOCKS_PER_SEC);
}
else {
double* numAleatorios = (double*) malloc( sizeof(double) * ((double) N) ); //array with numbers
int i,j;
double resultado;
dest=0;
for(i=0; i<M; i++){ //again, M times
for(j=0; j<N; j++){
numAleatorios[j] = rand() % R ;
}
resultado = SumaDeRaices(numAleatorios);
MPI_Isend(&resultado,sizeof(double), MPI_DOUBLE, dest, i, MPI_COMM_WORLD,&reqs[p-1]); //send result to master
}
}
/* Shut down MPI */
MPI_Finalize();
exit(0);
} /* main */
double SumaDeRaices (double* valores){
int i;
double sumaTotal = 0.0;
//Raices cuadradas de los valores y suma de estos
for(i=0; i<N; i++){
sumaTotal = sqrt(valores[i]) + sumaTotal;
}
return sumaTotal;
}
【问题讨论】:
-
您收到 M*p 次,但您只发送 M 次。正确的数字是多少?
-
@mcsim 我为每个进程发送 M 次(所以我发送 M*p 次)
-
啊。你是对的。
标签: c segmentation-fault mpi