【问题标题】:Communication cost of MPI send and receiveMPI 发送和接收的通信成本
【发布时间】:2015-07-09 00:00:09
【问题描述】:

我是 MPI 新手,想测量两个节点之间 MPI_Send 和 MPI_Recv 的通信成本。为此,我编写了以下代码:

 /*==============================================================
 * print_elapsed (prints timing statistics)
 *==============================================================*/
 void print_elapsed(const char* desc, struct timeval* start, struct timeval* end, int numiterations) {

  struct timeval elapsed;
  /* calculate elapsed time */
  if(start->tv_usec > end->tv_usec) {

    end->tv_usec += 1000000;
    end->tv_sec--;
  }
  elapsed.tv_usec = end->tv_usec - start->tv_usec;
  elapsed.tv_sec  = end->tv_sec  - start->tv_sec;

  printf("\n%s total elapsed time = %ld (usec)\n",
    desc, (elapsed.tv_sec*1000000 + elapsed.tv_usec)/numiterations );
}


int main(int argc, char **argv) {

  int nprocs, nElements; /* command line args */

  int my_id;

  long double* buffer, *rec_buffer;         
 /* gettimeofday stuff */
  struct timeval start, end;         /* gettimeofday stuff */
  struct timezone tzp;

  MPI_Status status;              /* Status variable for MPI operations */

  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &my_id); /* Getting the ID for this process */

  /*---------------------------------------------------------
   *  Read Command Line
   *  - check usage and parse args
   *---------------------------------------------------------*/

  if(argc < 2) {

    if(my_id == 0)
      printf("Usage: %s [nElements]\n\n", argv[0]);

    MPI_Finalize();
    exit(1);
  }

  nElements = atoi(argv[1]);
  int numiterations = 64;
  MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* Get number of processors */

  if(my_id == 0)
    printf("\nExecuting %s: numElements=%d \n",
            argv[0], nElements);


  buffer = (long double *) malloc(sizeof(long double)*nElements);
  rec_buffer = (long double *) malloc(sizeof(long double)*nElements);

  if(buffer == NULL) {
    printf("Processor %d - unable to malloc()\n", my_id);
    MPI_Finalize();
    exit(1);
  }


  MPI_Barrier(MPI_COMM_WORLD);

  if(my_id == 1)
    gettimeofday(&start, &tzp);


  for(int i = 0 ; i < numiterations ; ++i)
  {
    if(my_id == 0)
      MPI_Send(buffer, nElements, MPI_LONG, 1, 0, MPI_COMM_WORLD);

    if(my_id == 1)
      MPI_Recv(rec_buffer, nElements, MPI_LONG, 0, 0, MPI_COMM_WORLD, &status);
  }


  if(my_id == 1) {
    gettimeofday(&end,&tzp);
  }

  MPI_Barrier(MPI_COMM_WORLD);
  if(my_id == 1) {
    print_elapsed("Summation", &start, &end, numiterations);
  }

  free(buffer);

  MPI_Finalize();

  return 0;
} /* main() */

我重复发送和接收numiteration 次,但我没有关于初始化成本和实际通信时间的信息。我想知道是否有更好的方法或工具来更详细地衡量沟通成本。

【问题讨论】:

  • 现代性与它无关(尽管 MPI 在今年推出的新版本规范中仍然活跃且良好)。 MPI 和线程完成不同的事情,用于解决不同的问题。
  • @WesleyBland 据我所知,TBB 用于共享内存系统,不支持分布式系统。

标签: c++ parallel-processing mpi


【解决方案1】:

如果您想要描述的详细程度,您可能必须深入了解 MPI 库本身的实现。您测量的是通信对您的应用程序的影响。但是,取决于您的基础架构,通信中涉及的内容可能更多。一些网络可以在不涉及应用程序的情况下取得进展,而一些 MPI 库也可以使用线程异步处理消息。

您如何衡量这些事情将取决于您的系统和上述限制。如果您关心的只是您的应用程序在通信调用中花费了多少时间,那么您或多或少已经实现了这一点。您可以使用其他跟踪工具来完成类似的事情(HPCtoolkit 是我过去使用过的一种)。

如果您想获得更多关于幕后发生的事情的详细信息,您必须深入了解您的实现并开始在内部进行检测(假设您使用的是开源实现,例如 MPICH 或 Open MPI) .这是一个涉及更多的过程,并且机制会从一种实现更改为另一种实现。

【讨论】:

    猜你喜欢
    • 2016-11-19
    • 2013-04-15
    • 2018-03-12
    • 2013-12-20
    • 2016-06-15
    • 2012-03-13
    • 2016-10-03
    • 2011-08-19
    相关资源
    最近更新 更多