[英]Segmentation Fault with MPI_Gather
我是第一次使用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()的(错误)调用。 几乎没有指针使用。 由于向量知道如何销毁自身,因此不会发生内存泄漏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.