English 中文(简体)
MPI 奴隶贩卖进程在不再工作的情况下消失
原标题:MPI Slave processes hang when there is no more work

我有一个序列C++方案,我希望同时进行。 我知道消费物价指数的基本原理,MPI_Send,MPI_Recv等。 基本情况 我有一个数据生成算法,其运行速度大大快于数据处理算法。 目前,他们正在从事一系列工作,但我认为,在根本进程中管理数据生成,对奴隶进程进行数据处理,并向奴隶发出含有有待处理数据的信息。 这样,每个奴隶都处理一套数据,然后等待下一个数据集。

问题是,一旦根本进程产生数据,方案就会因奴隶们等待更多人而hang。

这是问题的一个例子:

#include "mpi.h"

#include <cassert>
#include <cstdio>

class Generator {
  public:
    Generator(int min, int max) : value(min - 1), max(max) {}
    bool NextValue() {
      ++value;
      return value < max;
    }
    int Value() { return value; }
  private:
    int value, max;

    Generator() {}
    Generator(const Generator &other) {}
    Generator &operator=(const Generator &other) { return *this; }
};

long fibonnaci(int n) {
  assert(n > 0);
  if (n == 1 || n == 2) return 1;
  return fibonnaci(n-1) + fibonnaci(n-2);
}

int main(int argc, char **argv) {
  MPI_Init(&argc, &argv);

  int rank, num_procs;
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &num_procs);

  if (rank == 0) {
    Generator generator(1, 2 * num_procs);
    int proc = 1;
    while (generator.NextValue()) {
      int value = generator.Value();
      MPI_Send(&value, 1, MPI_INT, proc, 73, MPI_COMM_WORLD);
      printf("** Sent %d to process %d.
", value, proc);
      proc = proc % (num_procs - 1) + 1;
    }
  } else {
    while (true) {
      int value;
      MPI_Status status;
      MPI_Recv(&value, 1, MPI_INT, 0, 73, MPI_COMM_WORLD, &status);
      printf("** Received %d from process %d.
", value, status.MPI_SOURCE);
      printf("Process %d computed %d.
", rank, fibonnaci(2 * (value + 10)));
    }
  }

  MPI_Finalize();
  return 0;
}

显然,上述并非一切都是“良好做法”,但足以使这一点贯穿到底。

如果我从奴隶进程中删除<条码>,只要每个奴隶都离开,方案就退出。 我只想在根本进程完成后退出该方案,所有奴隶都处理过所寄出的一切。

如果我知道将产生多少数据集,那么我就能够看到,很多运行过程和一切都会 n然地消失,但这里的情况就是如此。

任何建议? 是否在APIC中有任何事情要做? 能否以更好的地形更好地解决这一问题? MPI_Isend或_MPI_IRecv 这更好吗? 我对我来说,我很新。

成就

最佳回答

The usual practice is to send to all worker processes an empty message with a special tag that signals them to exit the infinite processing loop. Let s say this tag is 42. You would do something like that in the worker loop:

while (true) {
  int value;
  MPI_Status status;
  MPI_Recv(&value, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
  if (status.MPI_TAG == 42) {
    printf("Process %d exiting work loop.
", rank);
    break;
  }
  printf("** Received %d from process %d.
", value, status.MPI_SOURCE);
  printf("Process %d computed %d.
", rank, fibonnaci(2 * (value + 10)));
}

经理在发电机组舍之后这样做:

for (int i = 1; i < num_procs; i++)
  MPI_Send(&i, 0, MPI_INT, i, 42, MPI_COMM_WORLD);

Regarding your next question. Using MPI_Isend() in the master process would deserialise the execution and increase the performance. The truth however is that you are sending very small messages and those are typically internally buffered (WARNING - implementation dependent!) so your MPI_Send() is actually non-blocking and you already have non-serial execution. MPI_Isend() returns an MPI_Request handle that you need to take care of later. You could either wait for it to finish with MPI_Wait() or MPI_Waitall() but you could also just call MPI_Request_free() on it and it will be automatically freed when the operation is over. This is usually done when you d like to send many messages asynchronously and would not care on when the sends will be completed, but it s a bad practice nevertheless since having a large number of outstanding requests can consume lots of precious memory. As for the worker processes - they need the data in order to proceed with the computation so using MPI_Irecv() is not necessary.

欢迎马普切人方案拟定世界。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签