【问题标题】:Can't access nested for loop in MPI Bcast无法访问 MPI Bcast 中的嵌套 for 循环
【发布时间】:2014-03-11 16:16:56
【问题描述】:

我无法访问或打印嵌套 for 循环中的任何数据。这是我第一次编写 MPI 程序,所有的例子都没有比 Hello World 更复杂。我做错了什么?

void assignStarsToClusters(double *stars, double *clusters, int *azimuth)
{
ostringstream oss;

oss.str("");
oss.clear(); 

//double start = MPI_Wtime();   

// Assign a star to the closest cluster
double smallDistance;
double tmpDistance;
int indice = 0;

MPI_Barrier(MPI_COMM_WORLD);

MPI_Bcast(azimuth    /*the data we're broadcasting*/,
          NUMOFLINES /*the data size */,
          MPI_INT    /*the data type */,
          0          /*the process we're broadcasting from */,
          MPI_COMM_WORLD);
MPI_Bcast(stars    /*the data we're broadcasting*/,
          NUMOFSTARCOORD /*the data size */,
          MPI_DOUBLE    /*the data type */,
          0          /*the process we're broadcasting from */,
          MPI_COMM_WORLD);
MPI_Bcast(clusters    /*the data we're broadcasting*/,
          NUMOFCLUST /*the data size */,
          MPI_DOUBLE    /*the data type */,
          0          /*the process we're broadcasting from */,
          MPI_COMM_WORLD);  
MPI_Bcast(&NUMOFCLUST    /*the data we're broadcasting*/,
          1             /*the data size */,
          MPI_DOUBLE    /*the data type */,
          0          /*the process we're broadcasting from */,
          MPI_COMM_WORLD);     
MPI_Bcast(&NUMOFSTARCOORD    /*the data we're broadcasting*/,
          1             /*the data size */,
          MPI_DOUBLE    /*the data type */,
          0          /*the process we're broadcasting from */,
          MPI_COMM_WORLD);

int sx = NUMOFSTARCOORD/comm_sz;
int s_lower = my_rank * sx;
int s_upper = s_lower + sx;


for(int a = my_rank; a <= comm_sz; a++)
{ 
    for(int i = s_lower; i <= s_upper; i+=3)
    {
        smallDistance = sqrt(sqr(stars[i] - clusters[0]) + sqr(stars[i+1] - clusters[1]) + sqr(stars[i+2] - clusters[2]));

        indice = 0;

        for(int j = 3; j <= (NUMOFCLUST * 3); j+=3)
        {
            tmpDistance = sqrt(sqr(stars[i] - clusters[j]) + sqr(stars[i+1] - clusters[j+1]) + sqr(stars[i+2] - clusters[j+2]));

            oss << " " << j;        

            if(tmpDistance < smallDistance)
            {
                smallDistance = tmpDistance;
                indice = j;
            }           
        }
        azimuth[i/3] = indice / 3;  

        //oss << " " << indice;
    }
}
    MPI_Barrier(MPI_COMM_WORLD);

   // cout << oss.str() << endl;   

//oss.str("");
//oss.clear();
//if(my_rank == 0)
//{
    //for (int i = 0; i < NUMOFLINES; i++) {
    //  oss << " " << azimuth[i];
    //}
//}


cout << oss.str() << endl;

//cout << "Total Time: " << MPI_Wtime() - start << endl;

 }

【问题讨论】:

  • 你不能指望我们在 SO 上教你 MPI。只需搜索“MPI 教程”并从那里开始:“hello world”还有很多示例。
  • 我已经做了一个 hello world 并且在这种情况下没有帮助。简单的 MPI 程序无助于跳转到实际代码。让我开始的一点帮助会很好。我理解这些概念,我的第一个程序只需要一些帮助。
  • 在不知道azimuthclusterNUMOFSTARCOORD 是如何被清除的情况下很难告诉你出了什么问题。这是MPI_Bcast() 示例的链接:mpi.deino.net/mpi_functions/MPI_Bcast.html

标签: mpi broadcast


【解决方案1】:

我会告诉你我在你的 MPI 代码中看到的错误。

  1. MPI_Barrier():几乎不需要:MPI_Bcast() 同步是隐式的 - 当您需要与外部同步时需要屏障,例如将 I/O 排序到标准输出时。

  2. 这个循环:

    for(int a = my_rank; a <= comm_sz; a++)
    

    意图不明确。我希望my_rankcomm_sz 已经正确初始化。你确定他们是?您是否使用 mpirun 正确运行您的代码?但最重要的是:我不知道为什么,但我感觉你认为这个循环是并行的。 a 循环变量(它的名字是多么可怕的选择)没有在循环中使用。如果我的直觉是正确的……那么这不是 MPI 的工作原理。

    另一方面,如果它对你的算法是正确的(完全不清楚),它是不平衡的,因为不同的 MPI 任务将执行不同数量的迭代。因此,您的代码将作为最慢的代码执行,即 my_rank==0 的任务。

  3. 我负面评论的原因是您说“这是我第一次编写 MPI 程序”,而您之前只尝试过“hello world”。我强烈建议您尝试一些更典型的示例:分布式求和、幽灵单元......然后当您更好地了解它如何处理简单的测试用例时,您可以继续解决自己的问题。

  4. 你应该开始在这里发布一个工作代码,如果太大而不实用,则从你的实际问题中提取。请参阅here,尤其是here:您会发现,即使在发帖之前,您也会尝试解释它。

  5. MPI_Bcast()'s - 帖子的标题 - 看起来非常正确:如果您不想从并行调试器开始(这也不错),您应该开始打印它们以开始调试代码完全没有想法)......但似乎你也没有尝试过。我会在第一个 MPI_Bcast() 之后开始添加这一行:

    cout << my_rank << ": azimuth[0]="<< azimuth[0] << endl;
    

希望我的批评的原因更清楚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    相关资源
    最近更新 更多