我怎么打印日志,才能在MPI [英] how do I print the log in order in MPI

查看:119
本文介绍了我怎么打印日志,才能在MPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么

我在使用MPI一个C程序,它使用4个过程:1的车辆(任务id = 0)和3名乘客。
车辆可同时容纳2名乘客。
3客户继续回来送一程。

I have a C-program using MPI, and it uses 4 processes: 1 vehicle(taskid=0) and 3 passengers. Vehicle can accommodate 2 passengers at a time. 3 customers keep coming back to get a ride.

有关车辆,我有:

int passengers[C] = {0};
while(1)
    MPI_Recv(&pid, 1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, &status);
    //put pid in passengers[totalNumberArrived]
    if(totalNumberArrived == 2)    
        printf("vehicle left...");
        sleep(5);
        printf("vehicle came back...");
        for (i=0; i<2; i++)
            MPI_Send(&passengers[i], 1, MPI_INT, passengers[i], 1, MPI_COMM_WORLD);
        totalNumberArrived = 0;
    if(done)//omitting the details here
        break; 

和,对于每一个乘客,我有:

And, for each passengers, I have:

for (i to NumOfRound)
    sleep(X);
    printf("%d is sending a msg", tasked)
    MPI_Send(&taskid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
    MPI_Recv(&pid, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &pstatus);
    printf("%d received from %d\n", tasked, pid, pstatus.MPI_SOURCE);

问题

如果离开与任务id 1和3一骑车辆,我希望看到这样的输出:

If the vehicle left for a ride with taskid 1 and 3, I expect to see this kind of output:

vehicle left...
vehicle came back...
1 is sending a msg 
3 is sending a msg (this could be before 1's msg though)

但我有时会

vehicle left...
1 is sending a msg
3 is sending a msg
vehicle came back...

它看起来像乘客没有被阻塞,直到车辆回来。

which looks like the passenger is not blocked until the vehicle comes back.

我认为MPI_RECV阻塞任务直到从车辆中的味精,于是我研究和阅读MPI_RECV确实块,但是出现这样的问题,因为printf的不一定是为了打印。我也看过一些建议使用平齐,但在某些情况下,刷新不起作用。

I thought that MPI_Recv blocks the task until it gets a msg from the vehicle, so I researched and read that MPI_Recv does block but this kind of issues occur because the printf is not necessarily printing in order. I also read that some recommends to use flush but in some cases flush doesn't work.

我不知道我应该在我的情况下做的。难道真是为了printf的只是这件事?

I'm not sure what I should do in my case. Is it really just the matter of printf order?

我也看到了这一点:订购的MPI输出

和不知道我应该添加一个主线程,并让它为车辆和乘客??中央控制器

and wonder if I should add a master thread and let it be the central controller for vehicle and passengers??

推荐答案

您不能依赖于打印进程之间进行有序输出。你可以指望的唯一的事情就是输出每个流程订购。因此,如果由于某种原因,它的关键,你可以打印到标准输出/ STDERR才能,你需要把它汇总到一个进程第一。

You can't rely on printing output to be ordered between processes. The only thing you can count on is that output will be ordered per processes. Therefore, if for some reason it's critical that you can print things to STDOUT/STDERR in order, you need to aggregate it to one process first.

这篇关于我怎么打印日志,才能在MPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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