MPI - 按订单打印 [英] MPI - Printing in an order

查看:25
本文介绍了MPI - 按订单打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 C 编写一个函数,其中每个处理器都打印自己的数据.这是我所拥有的:

I'm trying to write a function in C where every processor prints it's own data. Here is what i have:

void print_mesh(int p,int myid,int** U0,int X,int Y){
    int i,m,n;
    for(i=0;i<p;i++){
        if(myid==i){
            printf("myid=%d
",myid);
            for(n=0;n<X;n++){
                for(m=0;m<Y;m++){
                    printf("%d ",U0[n][m]);
                }
                printf("
");
            }
        }
        else MPI_Barrier(MPI_COMM_WORLD);
    }
}

由于某种原因它不起作用.数组全部混合打印.您对为什么这不起作用有任何见解吗?还有其他可行的想法吗?如果可能,我不想在主进程中发送整个数组.另外我不想使用预编译函数.

It doesn't work for some reason. The arrays are printed all mixed up. Do you have any insight as to why this doesn't work? Any other ideas that work? If possible, I don't want to send the whole array in a master process. Also I don't want to use precompiled functions.

推荐答案

没有办法保证来自许多不同进程的消息在到达另一个进程时会以正确"的顺序到达.这基本上就是这里发生的事情.

There is no way to guarantee that messages from many different processes will arrive in the "correct" order when they arrive to another process. This is essentially what is happening here.

即使您没有明确发送消息,当您将某些内容打印到屏幕上时,也必须将其发送到本地系统上的进程(mpiexecmpirun>) 它可以打印到屏幕上.MPI 无法知道这些消息的正确顺序是什么,因此它只会在它们到达时打印它们.

Even though you aren't explicitly sending messages, when you print something to the screen, it has to be sent to the process on your local system (mpiexec or mpirun) where it can be printed to the screen. There is no way for MPI to know what the correct order for these messages is so it just prints them as they arrive.

如果您要求按特定顺序打印消息,则必须将它们全部发送到一个等级,该等级可以按照您喜欢的任何顺序打印它们.只要一个等级完成所有的打印,所有的消息都会被正确排序.

If you require that your messages are printed in a specific order, you must send them all to one rank which can print them in whatever order you like. As long as one rank does all of the printing, all of the messages will be ordered correctly.

应该说,您可能会找到一些答案,其中说您可以在字符串末尾放置换行符或使用flush() 来确保刷新缓冲区,但这不会由于上述原因,保证在远程端订购.

It should be said that there will probably be answers that you can find out there which say you can put a newline at the end of your string or use flush() to ensure that the buffers are flushed, but that won't guarantee ordering on the remote end for the reasons mentioned above.

这篇关于MPI - 按订单打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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