MPI_Gather的分段错误

时间:2014-04-19 22:46:41

标签: c++ parallel-processing segmentation-fault openmpi

我第一次使用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);

我已经分配了一个发送缓冲区和接收缓冲区,如示例所示,是否有人可以解释为什么我会收到此错误?

1 个答案:

答案 0 :(得分:1)

您的代码有两个问题,一个需要修复,另一个需要清理代码。此外,因为我们真的不知道&#34; j&#34;的价值。是的,那么我所能做的就是假设这些值是有效的。

问题如下:

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

永远不要混用像这样的分配和释放功能。如果您使用new[]分配,则使用delete[]而不是free()而非delete(非数组删除)取消分配。

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

您可以使用std :: vector替换所有对new []的调用。以下是使用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);

现在没有调用free()的新[],没有(错误)调用。如果有任何指针使用,则很少。由于向量知道如何破坏自身,因此没有内存泄漏。