gpt4 book ai didi

c++ - MPI_Send(109) : Invalid rank has value -2 but must be nonnegative and less than 1

转载 作者:太空宇宙 更新时间:2023-11-04 13:02:03 25 4
gpt4 key购买 nike

谁能帮我找出我代码中的以下错误:

"Fatal error in MPI_Send: Invalid rank, error stack: MPI_Send(190): MPI_Send(buf=0x1123060, count=40, MPI_FLOAT, dest=MPI_ANY_SOURCE, tag=25, MPI_COMM_WORLD) failed MPI_Send(109): Invalid rank has value -2 but must be nonnegative and less than 1"

int main(int argc, char* argv[])
{
name = "quick.dat";
n = 40;
auto start = std::chrono::high_resolution_clock::now();

int i;
double sp;
double *ap, *ae, *aw, *aww, *cte;
ap = new double[n];
ae = new double[n];
aw = new double[n];
aww = new double[n];
cte = new double[n];

int my_rank, nproc;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);

if (my_rank == 0)
{
MPI_Send(&ap[0], n, MPI_FLOAT, MPI_ANY_SOURCE, 25, MPI_COMM_WORLD);
MPI_Send(&ae[0], n, MPI_FLOAT, MPI_ANY_SOURCE, 25, MPI_COMM_WORLD);
MPI_Send(&aw[0], n, MPI_FLOAT, MPI_ANY_SOURCE, 25, MPI_COMM_WORLD);
MPI_Send(&aww[0], n, MPI_FLOAT, MPI_ANY_SOURCE, 25, MPI_COMM_WORLD);
MPI_Send(&cte[0], n, MPI_FLOAT, MPI_ANY_SOURCE, 25, MPI_COMM_WORLD);
for (i = 0; i < (n - 1) / nproc + 1; i++)
{
if (i == 0)
{
ae[i] = a((i + 1) * 1.0 / n) * D((i + 1) * 1.0 / n) +
1. / 3.0 * a(i * 1.0 / n) * D(i * 1.0 / n) -
3.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
cte[i] = -(8. / 3. * a(i * 1.0 / n) * D(i * 1.0 / n) +
2.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n) +
a(i * 1.0 / n) * u(i * 1.0 / n));
ap[i] = -(ae[i] - cte[i] + a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n) -
a(i * 1.0 / n) * u(i * 1.0 / n));
continue;
}
if (i == 1)
{
aw[i] = a(i * 1.0 / n) * D(i * 1.0 / n) +
7. / 8. * a(i * 1.0 / n) * u(i * 1.0 / n) +
1.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
cte[i] = 0.25 * a(i * 1.0 / n) * u(i * 1.0 / n);
ae[i] = a((i + 1) * 1.0 / n) * D((i + 1) * 1.0 / n) -
3.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
ap[i] = -(ae[i] + aw[i] - cte[i] + a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n) -
a(i * 1.0 / n) * u(i * 1.0 / n));
continue;
}
if (i == n - 1)
{
aw[i] = a(i * 1.0 / n) * D(i * 1.0 / n) +
1. / 3.0 * a((i + 1) * 1.0 / n) * D((i + 1) * 1.0 / n) +
6.0 / 8.0 * a(i * 1.0 / n) * u(i * 1.0 / n);
aww[i] = -1.0 / 8.0 * a(i * 1.0 / n) * u(i * 1.0 / n);
cte[i] = 0;
sp = 8.0 / 3.0 * a((i + 1) * 1.0 / n) * D((i + 1) * 1.0 / n) -
a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
ap[i] = -(aww[i] + aw[i] + sp + a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n) -
a(i * 1.0 / n) * u(i * 1.0 / n));
continue;
}
if (i != 0 && i != 1 && i != n - 1)
{
ae[i] = a((i + 1) * 1.0 / n) * D((i + 1) * 1.0 / n) -
3.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
aw[i] = a(i * 1.0 / n) * D(i * 1.0 / n) +
6. / 8. * a(i * 1.0 / n) * u(i * 1.0 / n) +
1.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
aww[i] = -1.0 / 8.0 * a(i * 1.0 / n) * u(i * 1.0 / n);
cte[i] = 0;
ap[i] = -(aww[i] + aw[i] + ae[i] - cte[i] +
a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n) -
a(i * 1.0 / n) * u(i * 1.0 / n));
}
}
MPI_Recv(&ap[0], n, MPI_FLOAT, MPI_ANY_SOURCE, 25, MPI_COMM_WORLD, &status);
MPI_Recv(&ae[0], n, MPI_FLOAT, MPI_ANY_SOURCE, 25, MPI_COMM_WORLD, &status);
MPI_Recv(&aw[0], n, MPI_FLOAT, MPI_ANY_SOURCE, 25, MPI_COMM_WORLD, &status);
MPI_Recv(&aww[0], n, MPI_FLOAT, MPI_ANY_SOURCE, 25, MPI_COMM_WORLD, &status);
MPI_Recv(&cte[0], n, MPI_FLOAT, MPI_ANY_SOURCE, 25, MPI_COMM_WORLD, &status);
}

else if (my_rank != 0 && my_rank != nproc - 1)
{
MPI_Recv(&ap[0], n, MPI_FLOAT, 0, 25, MPI_COMM_WORLD, &status);
MPI_Recv(&ae[0], n, MPI_FLOAT, 0, 25, MPI_COMM_WORLD, &status);
MPI_Recv(&aw[0], n, MPI_FLOAT, 0, 25, MPI_COMM_WORLD, &status);
MPI_Recv(&aww[0], n, MPI_FLOAT, 0, 25, MPI_COMM_WORLD, &status);
MPI_Recv(&cte[0], n, MPI_FLOAT, 0, 25, MPI_COMM_WORLD, &status);
for (i = ((n - 1) / nproc) * my_rank + 1; i < ((n - 1) / nproc) * (my_rank + 1) + 1; i++)
{
if (i == 0)
{
ae[i] = a((i + 1) * 1.0 / n) * D((i + 1) * 1.0 / n) +
1. / 3.0 * a(i * 1.0 / n) * D(i * 1.0 / n) -
3.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
cte[i] = -(8. / 3. * a(i * 1.0 / n) * D(i * 1.0 / n) +
2.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n) +
a(i * 1.0 / n) * u(i * 1.0 / n));
ap[i] = -(ae[i] - cte[i] + a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n) -
a(i * 1.0 / n) * u(i * 1.0 / n));
continue;
}
if (i == 1)
{
aw[i] = a(i * 1.0 / n) * D(i * 1.0 / n) +
7. / 8. * a(i * 1.0 / n) * u(i * 1.0 / n) +
1.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
cte[i] = 0.25 * a(i * 1.0 / n) * u(i * 1.0 / n);
ae[i] = a((i + 1) * 1.0 / n) * D((i + 1) * 1.0 / n) -
3.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
ap[i] = -(ae[i] + aw[i] - cte[i] + a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n) -
a(i * 1.0 / n) * u(i * 1.0 / n));
continue;
}
if (i == n - 1)
{
aw[i] = a(i * 1.0 / n) * D(i * 1.0 / n) +
1. / 3.0 * a((i + 1) * 1.0 / n) * D((i + 1) * 1.0 / n) +
6.0 / 8.0 * a(i * 1.0 / n) * u(i * 1.0 / n);
aww[i] = -1.0 / 8.0 * a(i * 1.0 / n) * u(i * 1.0 / n);
cte[i] = 0;
sp = 8.0 / 3.0 * a((i + 1) * 1.0 / n) * D((i + 1) * 1.0 / n) -
a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
ap[i] = -(aww[i] + aw[i] + sp + a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n) -
a(i * 1.0 / n) * u(i * 1.0 / n));
continue;
}
if (i != 0 && i != 1 && i != n - 1)
{
ae[i] = a((i + 1) * 1.0 / n) * D((i + 1) * 1.0 / n) -
3.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
aw[i] = a(i * 1.0 / n) * D(i * 1.0 / n) +
6. / 8. * a(i * 1.0 / n) * u(i * 1.0 / n) +
1.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
aww[i] = -1.0 / 8.0 * a(i * 1.0 / n) * u(i * 1.0 / n);
cte[i] = 0;
ap[i] = -(aww[i] + aw[i] + ae[i] - cte[i] +
a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n) -
a(i * 1.0 / n) * u(i * 1.0 / n));
}
}
MPI_Send(&ap[0], n, MPI_FLOAT, 0, 25, MPI_COMM_WORLD);
MPI_Send(&ae[0], n, MPI_FLOAT, 0, 25, MPI_COMM_WORLD);
MPI_Send(&aw[0], n, MPI_FLOAT, 0, 25, MPI_COMM_WORLD);
MPI_Send(&aww[0], n, MPI_FLOAT, 0, 25, MPI_COMM_WORLD);
MPI_Send(&cte[0], n, MPI_FLOAT, 0, 25, MPI_COMM_WORLD);
}
else if (my_rank == nproc - 1 && nproc != 1)
{
MPI_Recv(&ap[0], n, MPI_FLOAT, 0, 25, MPI_COMM_WORLD, &status);
MPI_Recv(&ae[0], n, MPI_FLOAT, 0, 25, MPI_COMM_WORLD, &status);
MPI_Recv(&aw[0], n, MPI_FLOAT, 0, 25, MPI_COMM_WORLD, &status);
MPI_Recv(&aww[0], n, MPI_FLOAT, 0, 25, MPI_COMM_WORLD, &status);
MPI_Recv(&cte[0], n, MPI_FLOAT, 0, 25, MPI_COMM_WORLD, &status);
for (i = ((n - 1) / nproc) * my_rank + 1; i < n; i++)
{

if (i == 0)
{
ae[i] = a((i + 1) * 1.0 / n) * D((i + 1) * 1.0 / n) +
1. / 3.0 * a(i * 1.0 / n) * D(i * 1.0 / n) -
3.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
cte[i] = -(8. / 3. * a(i * 1.0 / n) * D(i * 1.0 / n) +
2.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n) +
a(i * 1.0 / n) * u(i * 1.0 / n));
ap[i] = -(ae[i] - cte[i] + a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n) -
a(i * 1.0 / n) * u(i * 1.0 / n));
continue;
}
if (i == 1)
{
aw[i] = a(i * 1.0 / n) * D(i * 1.0 / n) +
7. / 8. * a(i * 1.0 / n) * u(i * 1.0 / n) +
1.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
cte[i] = 0.25 * a(i * 1.0 / n) * u(i * 1.0 / n);
ae[i] = a((i + 1) * 1.0 / n) * D((i + 1) * 1.0 / n) -
3.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
ap[i] = -(ae[i] + aw[i] - cte[i] + a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n) -
a(i * 1.0 / n) * u(i * 1.0 / n));
continue;
}
if (i == n - 1)
{
aw[i] = a(i * 1.0 / n) * D(i * 1.0 / n) +
1. / 3.0 * a((i + 1) * 1.0 / n) * D((i + 1) * 1.0 / n) +
6.0 / 8.0 * a(i * 1.0 / n) * u(i * 1.0 / n);
aww[i] = -1.0 / 8.0 * a(i * 1.0 / n) * u(i * 1.0 / n);
cte[i] = 0;
sp = 8.0 / 3.0 * a((i + 1) * 1.0 / n) * D((i + 1) * 1.0 / n) -
a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
ap[i] = -(aww[i] + aw[i] + sp + a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n) -
a(i * 1.0 / n) * u(i * 1.0 / n));
continue;
}
if (i != 0 && i != 1 && i != n - 1)
{
ae[i] = a((i + 1) * 1.0 / n) * D((i + 1) * 1.0 / n) -
3.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
aw[i] = a(i * 1.0 / n) * D(i * 1.0 / n) +
6. / 8. * a(i * 1.0 / n) * u(i * 1.0 / n) +
1.0 / 8.0 * a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n);
aww[i] = -1.0 / 8.0 * a(i * 1.0 / n) * u(i * 1.0 / n);
cte[i] = 0;
ap[i] = -(aww[i] + aw[i] + ae[i] - cte[i] +
a((i + 1) * 1.0 / n) * u((i + 1) * 1.0 / n) -
a(i * 1.0 / n) * u(i * 1.0 / n));
}
}
MPI_Send(&ap[0], n, MPI_FLOAT, 0, MPI_ANY_TAG, MPI_COMM_WORLD);
MPI_Send(&ae[0], n, MPI_FLOAT, 0, MPI_ANY_TAG, MPI_COMM_WORLD);
MPI_Send(&aw[0], n, MPI_FLOAT, 0, MPI_ANY_TAG, MPI_COMM_WORLD);
MPI_Send(&aww[0], n, MPI_FLOAT, 0, MPI_ANY_TAG, MPI_COMM_WORLD);
MPI_Send(&cte[0], n, MPI_FLOAT, 0, MPI_ANY_TAG, MPI_COMM_WORLD);
}

// double t22 = MPI_Wtime();
MPI_Finalize();

// printf("[IJK] Compute time [s] : %6.3f \n", t22-t11 );

iteration(aww, aw, ap, ae, cte);

auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Time to Execute the FVM code "
<< " is : " << diff.count() << " s\n";

return 0;
}

最佳答案

消息实际上很清楚:MPI_Send: Invalid rank。您不能将 MPI_ANY_SOURCE 指定为 MPI_Send 的目标等级。在点对点通信中,必须有一个特定的接收者。看来您想使用集体 MPI_Bcast,例如:

MPI_Send(&ap[0], n, MPI_FLOAT, 0, MPI_COMM_WORLD);

这应该完全像那样在所有级别上运行。

关于c++ - MPI_Send(109) : Invalid rank has value -2 but must be nonnegative and less than 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43733103/

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