并行计算 - 混​​乱的输出? [英] Parallel computing -- jumbled up output?

查看:136
本文介绍了并行计算 - 混​​乱的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习并行计算的基础知识,但我遇到了我的计算机上的问题。看看我的低于code。基本上,我想打印出该行的Hello World!每一个核心我的电脑了。我的电脑有四个核心,所以它应该打印出该行四次。如果我用注释掉的COUT行,而不是printf的'行,输出会混在一起。这是因为'\\ n'逃生命令从单独执行的Hello World!,所以会随机出现新的线路输出。在printf的'行是这一问题的解决方案,因为该行执行一次全部(不分裂成几部分,如COUT线)。然而,当我使用'printf的',我的产量仍然混在一起,如果我用'COUT。我不知道为什么它这样做。我尝试另一台计算机上完全相同的code和它完美的作品。这只是我的电脑一直延续混杂了printf式的输出。我已经通过电子邮件发送它我的CS教授,他不知道为什么它在我的电脑上这样做。我知道,我设置了OpenMP的我的电脑上正确。有谁与并行计算的经验知道这是为什么我的电脑上搞乱?

 的#include< omp.h>
#包括LT&;&stdio.h中GT;
#包括LT&;&iostream的GT;
#包括LT&;&stdlib.h中GT;
使用命名空间std;诠释的main()
{
    OMP的#pragma并行
    {
        的printf(的Hello World \\ n!);
        // COUT<<的Hello World \\ n!<< ENDL;
    }
    返回0;
}

要展示一下我说的,这里是当我跑上面code我的电脑上的输出:


  

您好禾


  
  

世界,你好!


  
  

RLD!


  
  

世界,你好!



解决方案

对不起,你的教授的错了。你需要利用相互排斥或以保证不间断地使用共享资源(在这种情况下是 STDOUT 输出文件)的其他一些障碍。

混合输出潜力预期的行为,无论的printf 的std ::法院::运算<<()。在行为上你看到的差别是在每个执行持续时间,由于其不同的设计细微的差别。您应该会在两种情况下这种行为。


  

我只是不明白为什么它会工作其他人。


这不是。成为英雄到类,并解释它是如何工作的,以及如何解决它。告诉他们SO送上他们的祝福。 :)

I'm trying to learn the basics of parallel computing, but I'm running into an issue on my computer. Take a look at my code below. Basically, I want to print out the line "Hello World!" for every core my computer has. My computer has four cores, so it should print out that line four times. If I were to use the commented-out 'cout' line instead of the 'printf' line, the output would be all jumbled up. This is because the '\n' escape command is executed separately from the "Hello World!", so the new line output would occur randomly. The 'printf' line is a solution to this problem, because the line is executed all at once (not split up into parts like the 'cout' line). However, when I use 'printf', my output is still all jumbled up as if I used 'cout'. I have no idea why it does this. I tried the exact same code on another computer, and it works perfectly. It's only my computer that continues to jumble up the output with 'printf'. I've emailed my CS professor about it, and he has no idea why it's doing this on my computer. I know I set up OpenMP on my computer correctly. Does anyone with parallel computing experience know why this is messing up on my computer?

#include <omp.h>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
    #pragma omp parallel
    {
        printf("Hello World!\n");
        //cout << "Hello World!\n" << endl;
    }
    return 0;
}

To show what I'm talking about, here is the output from when I ran the above code on my computer:

Hello Wo

Hello World!

rld!

Hello World!

解决方案

Sorry, your professor's mistaken. You need to leverage mutual exclusion or some other barriers in order to guarantee uninterrupted use of a shared resource (which in this case is the STDOUT output file).

Mixed output is potential expected behavior regardless of printf or std::cout::operator<<(). The differences in behavior you see are subtle differences in the execution duration of each, due to their differing design. You should expect this behavior in either case.

I just don't understand why it would be working for everyone else.

It's not. Be a hero to your class and explain how it works and how to fix it. Tell them SO sends their love. :)

这篇关于并行计算 - 混​​乱的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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