具有大型阵列C ++的MPI动态工作池 [英] Dynamic pool of workers with MPI for large array C++

查看:78
本文介绍了具有大型阵列C ++的MPI动态工作池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如何使用MPI创建动态的工作人员池.

My question is how to create a dynamic pool of workers with MPI.

有一个很大的(NNN = 10 ^ 6-7个元素)一维数组/向量.我应该对每个单元格进行一些计算.这个问题非常尴尬地并行.

There is a large (NNN = 10^6-7 elements) 1D array/vector. I should perform some calculations on each cell. This problem is extremely embarrassingly parallel.

这个想法是(很好):每个MPI进程(并行运行时)读取通用的.dat文件,将值放入大小为NNN的局部(按等级)大向量中,并对大数组的适当部分执行计算,该部分"的长度是NNN/nprocs,其中"nprocs"是MPI的进程数.

The idea is (it works fine): each MPI process (when run in parallel) reads common .dat file, puts values in local (to each rank) large vector of size NNN and performs computation on appropriate part of large array, the lenght of this "part" is NNN/nprocs, where "nprocs" is the number of processes of MPI.

问题:该数组的某些部分"(NNN/nprocs)很快完成,因此某些CPU未使用(它们等待其他CPU完成运行).

The problem: some "parts" of this array (NNN/nprocs) are finished very quick and thus some of CPUs are unused (they wait for the others to finish the run).

问题1:如何制定动态时间表.完成任务的CPU可以选择新任务并继续工作.

The question1: How to make dynamic schedule. CPU's, that finished their tasks, can pick new task and continue working.

问题2:是否有MPI内置程序可以自动计划工作人员"和任务?

The question2: Is there MPI built-in procedure, that schedules automatically "workers" and tasks?

这是我的代码(静态时间表)

Here is my code (static schedule)

{  
  MPI_Init(&argc, &argv); 
  MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);

  MPI_Offset offset;
  MPI_File file;
  MPI_Status status;

   int Pstart = (NNN / nprocs) * rank + ((NNN % nprocs) < rank ? (NNN % nprocs) : rank);
   int Pend   = Pstart + (NNN / nprocs) + ((NNN % nprocs) > rank);
   offset = sizeof(double)*Pstart;

  MPI_File_open(MPI_COMM_WORLD, "shared.dat", MPI_MODE_CREATE|MPI_MODE_WRONLY, MPI_INFO_NULL, &file);


    double * local_array;
    local_array = new double [NNN/nprocs];

    for (int i=0;i<NNN/nprocs;i++)
       {
          /* next line calculates integral on each cell element of part NNN/nprocs of large array NNN */
          adapt_integrate(1, Integrand, par, 2, a, b, MaxEval, tol, tol, &val, &err);

          // putting result of integration to local array NNN/nprocs
          local_array[i] = val;
       }
 //  here, all local arrays are written to one shared file "shared.dat"    

 MPI_File_seek(file, offset, MPI_SEEK_SET);
 MPI_File_write(file, local_array, NNN/nprocs, MPI_DOUBLE, &status);
 MPI_File_close(&file);


}

推荐答案

这个问题是关于一个类似的问题,但仅是回顾:拥有一个指定的主进程,该主进程将工作分派给其他人.工作者所需要做的就是阻塞接收工作项,执行他们的计算,然后阻塞将结果发送给主节点并重复执行.主持人可以通过发布每个工人的无阻塞接收并进行轮询(如果其中任何一个完成)进行轮询来管理工作项,或者通过发布以MPI_ANY_SOURCE作为源的阻塞接收来管理工作项.

This question is about a similar problem, but just to recap: have a designated master process that issues chunks of work to the others. All the workers need to do is blocking receive a work item, perform their calculations, then blocking send the result to the master and repeat. The master can manage work items either by posting a nonblocking receive for each worker and polling if any of them completed, or by posting a blocking receive with MPI_ANY_SOURCE as source.

这篇关于具有大型阵列C ++的MPI动态工作池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆