gpt4 book ai didi

c++ - MPI_Gather 的段错误

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

我是第一次使用 MPI_Gather 并遵循了一些示例,但出于某种原因,我每次调用它时都会遇到段错误。相关代码在这里:

    //Get the top N matches for each node
for (int j = 0; j < send_counts[id]; j++)
{
data = read_file(my_dir + files[rec_buf[j]]);
temp_results = circularSubvectorMatch(test_vectors[i], data, N);
results.insert(results.end(), temp_results.begin(), temp_results.end());
}

std::sort(results.begin(), results.end(), sort_function);
results.resize(N);

//Send the N dissimilarities from each node to the root process and let it figure out
//the Nth best one overall
float *best_times = new float[N];
for (int j = 0; j < N; j++)
{
best_times[j] = results[j].dissimilarity;
}

MPI_Barrier(MPI_COMM_WORLD);

float *all_dissimilarities = NULL;
if (id == 0)
{
float *all_dissimilarities = new float[N * procs];
}

MPI_Gather(best_times, N, MPI_FLOAT, all_dissimilarities, N, MPI_FLOAT, 0, MPI_COMM_WORLD);
float *nth_best;
if (id == 0)
{
std::sort(all_dissimilarities, all_dissimilarities + N * procs - 1);
*nth_best = all_dissimilarities[N-1];
*nth_best = 1.0;
}
MPI_Bcast(nth_best, 1, MPI_FLOAT, 0, MPI_COMM_WORLD);

cout << "My id is " << id << "and I received: " << *nth_best << endl;

//each process prints each result it has that is better than or equal
//to the Nth best result calculated by the root process


//output search vector and search time

free(all_dissimilarities);
free(best_times);
MPI_Barrier(MPI_COMM_WORLD);

我已经像示例中那样分配了发送缓冲区和接收缓冲区,有人能解释一下为什么我会收到此错误吗?

最佳答案

您的代码有两个问题,一个需要修复,另一个可以清理您的代码。另外,由于我们真的不知道“j”的值是什么,所以我所能做的就是假设这些值是有效的。

问题如下:

问题 1:在使用 new[] 分配的数据上调用 free()。

永远不要像这样混合分配和释放函数。如果使用 new[] 分配,则使用 delete[] 释放,而不是 free(),也不是 delete(非数组删除)。

问题 2:在不需要使用时使用 new[]。

您可以将所有这些对 new[] 的调用替换为 std::vector。这是使用 vector 重写的代码片段:

 //Get the top N matches for each node
#include <vector>
//...
typedef std::vector<float> FloatArray;
//...
for (int j = 0; j < send_counts[id]; j++)
{
data = read_file(my_dir + files[rec_buf[j]]);
temp_results = circularSubvectorMatch(test_vectors[i], data, N);
results.insert(results.end(), temp_results.begin(), temp_results.end());
}

std::sort(results.begin(), results.end(), sort_function);
results.resize(N);

//Send the N dissimilarities from each node to the root process and let it figure out
//the Nth best one overall
FloatArray best_times(N);
for (int j = 0; j < N; j++)
best_times[j] = results[j].dissimilarity;

MPI_Barrier(MPI_COMM_WORLD);

float *pFirst = NULL;
FloatArray all_dissimilarities;
if (id == 0)
{
all_dissimilarities.resize(N * procs);
pFirst = &all_disimilarities[0];
}

MPI_Gather(&best_times[0], N, MPI_FLOAT, pFirst, N, MPI_FLOAT, 0, MPI_COMM_WORLD);
float nth_best;
if (id == 0)
{
std::sort(all_dissimilarities.begin(), all_dissimilarities.end());
nth_best = all_dissimilarities.back();
nth_best = 1.0;
}
MPI_Bcast(&nth_best, 1, MPI_FLOAT, 0, MPI_COMM_WORLD);
cout << "My id is " << id << "and I received: " << nth_best << endl;
MPI_Barrier(MPI_COMM_WORLD);

现在没有对 new[] 的调用,也没有(错误的)对 free() 的调用。几乎没有指针的使用。由于 vector 知道如何销毁自身,因此不存在内存泄漏。

关于c++ - MPI_Gather 的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23176445/

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