gpt4 book ai didi

c++ - MPI c++ 环形拓扑发送和接收不同的值,同时只传递相同的值?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:11:33 25 4
gpt4 key购买 nike

我正在学习 C++ 中的环形拓扑 MPI。我编写了一个 C++ 脚本来计算 10 维蒙特卡洛积分并计算其均值和局部最大值。我的目标是通过“环”传递每个处理器的局部最大值。

现在,我仍然没有弄清楚如何将不同处理器在运行时产生的最大值存储在一个数组中,所以我编译并执行了一次代码,然后手动将这些值组成一个数组。

接下来我想通过环传递每个数组值,并最终计算出全局最大值。现在我只是尝试传递第一个数组值,我看到处理器发送相同的值但接收不同的值。老实说,我不知道 c++ 使用 MPI 库的方式是否不同,我遵循了 MPI 的在线教程和 C,并且在我的 c++ 代码中使用了与 C 相同的结构。

我在这里分享代码。

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <mpi.h>
using namespace std;


//define multivariate function F(x1, x2, ...xk)

double f(double x[], int n)
{
double y;
int j;
y = 0.0;

for (j = 0; j < n-1; j = j+1)
{
y = y + exp(-pow((1-x[j]),2)-100*(pow((x[j+1] - pow(x[j],2)),2)));

}

y = y;
return y;
}

//define function for Monte Carlo Multidimensional integration

double int_mcnd(double(*fn)(double[],int),double a[], double b[], int n, int m)

{
double r, x[n], v;
int i, j;
r = 0.0;
v = 1.0;
// initial seed value (use system time)
//srand(time(NULL));


// step 1: calculate the common factor V
for (j = 0; j < n; j = j+1)
{
v = v*(b[j]-a[j]);
}

// step 2: integration
for (i = 1; i <= m; i=i+1)
{
// calculate random x[] points
for (j = 0; j < n; j = j+1)
{
x[j] = a[j] + (rand()) /( (RAND_MAX/(b[j]-a[j])));
}
r = r + fn(x,n);
}
r = r*v/m;

return r;
}




double f(double[], int);
double int_mcnd(double(*)(double[],int), double[], double[], int, int);



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

int rank, size;

MPI_Init (&argc, &argv); // initializes MPI
MPI_Comm_rank (MPI_COMM_WORLD, &rank); // get current MPI-process ID. O, 1, ...
MPI_Comm_size (MPI_COMM_WORLD, &size); // get the total number of processes


/* define how many integrals */
const int n = 10;

double b[n] = {5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0,5.0};
double a[n] = {-5.0, -5.0, -5.0, -5.0, -5.0, -5.0, -5.0, -5.0, -5.0,-5.0};

double result, mean;
int m;

const unsigned int N = 5;
double max = -1;


cout.precision(6);
cout.setf(ios::fixed | ios::showpoint);


srand(time(NULL) * rank); // each MPI process gets a unique seed

m = 4; // initial number of intervals

// convert command-line input to N = number of points
//N = atoi( argv[1] );


for (unsigned int i=0; i <=N; i++)
{
result = int_mcnd(f, a, b, n, m);
mean = result/(pow(10,10));

if( mean > max)
{
max = mean;
}
//cout << setw(10) << m << setw(10) << max << setw(10) << mean << setw(10) << rank << setw(10) << size <<endl;
m = m*4;
}

//cout << setw(30) << m << setw(30) << result << setw(30) << mean <<endl;
printf("Process %d of %d mean = %1.5e\n and local max = %1.5e\n", rank, size, mean, max );


double max_store[4] = {4.43095e-02, 5.76586e-02, 3.15962e-02, 4.23079e-02};

double send_junk = max_store[0];
double rec_junk;
MPI_Status status;


// This next if-statment implemeents the ring topology
// the last process ID is size-1, so the ring topology is: 0->1, 1->2, ... size-1->0
// rank 0 starts the chain of events by passing to rank 1
if(rank==0) {
// only the process with rank ID = 0 will be in this block of code.
MPI_Send(&send_junk, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); // send data to process 1
MPI_Recv(&rec_junk, 1, MPI_INT, size-1, 0, MPI_COMM_WORLD, &status); // receive data from process size-1
}
else if( rank == size-1) {
MPI_Recv(&rec_junk, 1, MPI_INT, rank-1, 0, MPI_COMM_WORLD, &status); // recieve data from process rank-1 (it "left" neighbor")
MPI_Send(&send_junk, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); // send data to its "right neighbor", rank 0
}
else {
MPI_Recv(&rec_junk, 1, MPI_INT, rank-1, 0, MPI_COMM_WORLD, &status); // recieve data from process rank-1 (it "left" neighbor")
MPI_Send(&send_junk, 1, MPI_INT, rank+1, 0, MPI_COMM_WORLD); // send data to its "right neighbor" (rank+1)
}
printf("Process %d send %1.5e\n and recieved %1.5e\n", rank, send_junk, rec_junk );


MPI_Finalize(); // programs should always perform a "graceful" shutdown
return 0;
}

我编译了:

mpiCC -std=c++11 -o hg test_code.cpp
mpirun -np 4 ./hg

输出看起来像这样,当然 mean amd max 不同,但我现在担心 send 和 recvd 值:

Process 2 of 4 mean = 2.81817e-02
and local max = 5.61707e-02
Process 0 of 4 mean = 2.59220e-02
and local max = 4.43095e-02
Process 3 of 4 mean = 2.21734e-02
and local max = 4.30539e-02
Process 1 of 4 mean = 2.87403e-02
and local max = 6.58530e-02
Process 1 send 4.43095e-02
and recieved 2.22181e-315
Process 2 send 4.43095e-02
and recieved 6.90945e-310
Process 3 send 4.43095e-02
and recieved 6.93704e-310
Process 0 send 4.43095e-02
and recieved 6.89842e-310

我想我搞砸了 C 和 C++ 中的 MPI 用法,我将不胜感激任何建议,而且我没有在互联网上看到任何好的 C++ MPI 教程,所以我的代码或教程链接的一个很好的修改示例将很有帮助。谢谢

最佳答案

MPI_Recv 的第三个参数和 MPI_Send是数据类型。现在您正在发送 double但您将数据类型设置为 MPI_INT .在大多数系统中int是 4 个字节和 double是 8 个字节,因此 rec_junk 中的一半字节未初始化。

要修复它,只需更改 MPI_INTMPI_DOUBLEMPI_Recv的所有电话中和 MPI_Send .

关于c++ - MPI c++ 环形拓扑发送和接收不同的值,同时只传递相同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40584459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com