c-不太了解MPI



我正在尝试使用MPI制作一个程序,该程序将使用MPI查找PI的值。

目前我可以通过这种方式找到总和:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUMSTEPS 1000000
int main() {
int i;
double x, pi, sum = 0.0;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
double step = 1.0/(double) NUMSTEPS;
x = 0.5 * step;
for (i=0;i<= NUMSTEPS; i++){
x+=step;
sum += 4.0/(1.0+x*x);
}
pi = step * sum;
clock_gettime(CLOCK_MONOTONIC, &end);
u_int64_t diff = 1000000000L * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
printf("PI is %.20fn",pi);
printf("elapsed time = %llu nanosecondsn", (long long unsigned int) diff);
return 0;
}

但这并不使用MPI。

因此,我尝试在MPI中创建自己的应用程序。我的逻辑是:

  1. 根据我的处理器数量将1000000分成相等的部分
  2. 计算每个范围的值
  3. 将计算出的值发送回主机,然后除以处理器的数量。我想保持主线自由,不做任何工作。类似于主从系统

这是我目前拥有的。这似乎不起作用,发送/接收会给出关于接收和发送不兼容变量的错误。

#include <mpi.h>
#include <stdio.h>
#include <string.h>
#define NUMSTEPS 1000000

int main(int argc, char** argv) {
int  comm_sz; //number of processes
int  my_rank; //my process rank
// Initialize the MPI environment
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
// Get the name of the processor
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
// Slaves
if (my_rank != 0) {

// Process math then send 

int i;
double x, pi, sum = 0.0;
double step = 1.0/(double) NUMSTEPS;
x = 0.5 * step;
// Find the start and end for the number
int processors = comm_sz - 1;

int thread_multi = NUMSTEPS / processors;

int start = my_rank * thread_multi;

if((my_rank - 1) != 0){
start += 1;
}

int end = start + thread_multi ;

for (i=start; i <= end; i++){
x+=step;
sum += 4.0 / (1.0 + x * x);
}
pi = step * sum;


MPI_Send(pi, 1.0, MPI_DOUBLE 1, 0, MPI_COMM_WORLD);

// Master
} else {
// Things in here only get called once.
double pi = 0.0;
double total = 0.0;
for (int q = 1; q < comm_sz; q++) {
MPI_Recv(pi, 1, MPI_DOUBLE, q, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
total += pi;
pi = 0.0;
}

// Take the added totals and divide by amount of processors that processed, to get the average
double finished = total / (comm_sz - 1);

// Print sum here
printf("Pi Is: %d", finished);
}
// Finalize the MPI environment.
MPI_Finalize();

}

我目前已经花了大约3个小时来研究这个问题。从未使用MPI。如有任何帮助,我们将不胜感激。

尝试使用更多编译器警告进行编译并尝试修复它们,例如-Wall -Wextra应该会为您提供有关问题的极好线索。

根据MPI_Send文档,第一个参数是指针,因此您似乎忽略了一个自动";转换为指针";错误您在MPI_Recv()调用中遇到了相同的问题。

您可以尝试在MPI_RecvMPI_Send中将pi作为&pi传递,并检查这是否修复了错误。

作为注释,您可以将伪变量声明为pi,作为主循环中的局部变量,以避免副作用:

for (int q = 1; q < comm_sz; q++) {
double pi = 0;
MPI_Recv(&pi, 1, MPI_DOUBLE, q, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
total += pi;
}