序列化mpi线程 [英] Serialize mpi threads

查看:204
本文介绍了序列化mpi线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有序列化你的MPI代码的某一部分?例如,当将信息打印到屏幕上时。如下所示:

Is there anyway to serialize a certain part of your MPI code ? For example when printing the information out to the screen. Something like below:

MPI_SERIALIZE();

cerr << "THIS WILL BE PRINTED ";
cerr << "IN ORDER" << endl;

MPI_END_SERILIZE();

如果有两个MPI线程,则不会有任何情况:

If there are two MPI threads, there will be no case :

THIS WILL BE PRINTED THIS WILL BE PRINTED IN ORDER 
IN ORDER

感谢

推荐答案

我在演示程序中这样做只会在小演示程序中执行此操作,因为同步成本很高;如果您试图控制输出到数据文件,您将使用MPI-IO,如果您尝试协调输出到终端,最容易发送数据到任务0,并且它执行所有输出)是循环障碍,像这样:

The way I do this in demonstration programs (and note - you would only do this in little demo programs because of the high synchronization cost; if you're trying to control output to a data file, you'd use MPI-IO, and if you're trying to coordinate output to the terminal, easiest to send data to task 0 and have it do all the output) is to loop over barriers, something like this:

#include <iostream>
#include <mpi.h>

using namespace std;

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

    int rank, size;
    int ierr;

    ierr = MPI_Init(&argc, &argv);

    ierr = MPI_Comm_size(MPI_COMM_WORLD, &size);
    ierr = MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    for (int i=0; i<size; i++)
    {
        if (i == rank) {
            cout << "Hello from task " << rank << " of "
                 << size << " world!" << endl;
        }
        MPI_Barrier(MPI_COMM_WORLD);
    }

    MPI_Finalize();

    return 0;
}

(更小的东西,MPI没有线程,这可能看起来像一个小细节,但如果你开始结合MPI和OpenMP,例如,线程和进程之间的区别变得重要。)

(And as a smaller thing, MPI doesn't have threads, it has processes. That may seem like a small detail, but if you start combining MPI with OpenMP, for instance, the distinction between threads and processes becomes important.)

这篇关于序列化mpi线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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