我有这个代码使用Euler的方法来解决简单的ODE - dy / dt = 2y - 2t ^ 2 - 3。 序列代码为y_0采用随机值并计算结果。我想使用MPI向每个节点发送自己的y_0值并收集结果。我只需要使用MPI_SEND和MPI_RECV吗?这是我的序列号。
#include <iostream>
#include <cmath>
using namespace std;
double tmax = 2.0; //max time value
double h = 0.00000005; //grid spacing
double imax = tmax/h;
double tarray [40000000] = {0}; //array for time values
double xarray [40000000] = {0}; //array for x values
double yarray [40000000] = {0}; //array for y values
//serial calculation for euler's method problem 1.7.1
void final171(double y0)
{
yarray [0] = y0;
for (int k = 0; k < imax; k++) {
tarray[k+1] = tarray[k] + h;
yarray[k+1] = yarray[k] + h * (2 * yarray[k] - 2 * tarray[k] * tarray[k] - 3);
}
cout << "Our Solution Euler: " << yarray[int(imax)];
}
int main(int argc, const char * argv[])
{
clock_t serialStart;
double serialTotal;
srand((unsigned)time(0));
serialStart = clock();
for (int i = 0; i < 128; i++) {
double floor = 0.5, ceiling = 3.5, range = (ceiling - floor);
double rnd = floor + double((range * rand()) / (RAND_MAX + 1.0));
final171(rnd);
cout << ", for y0 = " << rnd << endl;
}
serialTotal = (double)(clock() - serialStart) / (double)(CLOCKS_PER_SEC);
cout << endl << "N = " << imax << endl;
cout << "Serial Time (s)" << serialTotal << endl;
return 0;
}
答案 0 :(得分:1)
使用MPI_SEND
/ MPI_RECV
肯定会奏效。您也可以使用MPI_SCATTER
,因为它可以做同样的事情,但效率更高。