【问题标题】:MPI_Isend: how to keep data safe until they're received?MPI_Isend:在收到数据之前如何保证数据安全?
【发布时间】:2017-03-02 16:12:21
【问题描述】:

我要使用MPI_Isend 发送许多消息,但我不确定如何确保我的数据在收到之前是安全的,同时不要用完所有可用的内存。

目前,我正在做这样的事情:

vector<int*> outbox;
vector<MPI_Request> stats;

void run()
{
   while (!done()) 
   {
       //some magic
       //...

       sendMsg(rand(), getRecipRank());
  }
}

void sendMsg(int n, int recipRank)
{
    //Need to gen n random integers and send it to proc with rank recipRank
    //Can't block until the numbers are reveived. 
    //So how do I make sure these numbers are safe until then?


    int* data = (int*)malloc(sizeof(int) * n);
    //fill the data array with random numbers here

    MPI_Request req;
    MPI_Isend(data, n, MPI_INT, recipRank, 0, MPI_COMM_WORLD, &req);

    //Keeping the pointer to the array to free it later
    outbox.push_back(data); //Does this help keep the data safe until they're received?
    stats.push_back(req);
}

然后我有另一个函数偶尔会通过stats 向量来检查发送的状态。如果完成,则该函数将释放请求和outbox 中的相应数组。

我已经用少量消息对此进行了测试,它似乎有效,但我不确定它是否会总是有效,或者我只是很幸运。

【问题讨论】:

    标签: c++ parallel-processing mpi sendasynchronousrequest


    【解决方案1】:

    看起来不错!如果你使用malloc 分配内存,没有人会弄乱它,但你。既然你不乱来,那就安全了。

    这是一种通过使用更多内存来交错计算和通信的好模式。如果您想限制内存使用量,您可以为向量 outbox 设置最大长度,并在达到该长度时开始使用阻塞发送。

    澄清一下:您的数据并不“更安全”,因为您将其推入向量outbox,即使不这样做也是安全的。您将其推入向量以便稍后释放它。

    【讨论】:

    • "你的数据不是“更安全...”啊,原谅我的 Java 想法。我在想,因为我保留了一个 ref,它不会被垃圾收集。
    • 啊好的,我明白了:)
    【解决方案2】:

    这很好用,但您甚至不需要求助于 C-malloc

    您可以安全地使用 C++ 结构,例如:

    • new[] std::vector&lt;int*&gt; 中的指针
    • std::vector&lt;std::unique_ptr&lt;int[]&gt;&gt;(我更喜欢你的情况)
    • std::vector::data() 向量中的向量。但是,请确保在请求完成之前不要在提供内存的向量上调用任何非常量方法。

    使用std::vector&lt;int[10]&gt; outboxstd::vector&lt;std::array&lt;int, 10&gt;&gt; outbox 是安全的,因为此内存将直接在发件箱向量内引用,在进一步调用@987654328 时可能会重新分配(调整大小) @。但这仅对编译时已知的n 很重要。 std::vector&lt;std::unique_ptr&lt;std::array&lt;int, 10&gt;&gt;&gt; 会好起来的。

    【讨论】:

      猜你喜欢
      • 2016-07-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2021-01-28
      • 2018-03-15
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      相关资源
      最近更新 更多