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!
Because there are a number of processes running simultaneously, it is harder to get a good view on the state and progress of a parallel computation than of a sequential one. Moreover, the very fact that a computation is split into simultaneous parts can introduce errors which can not exist in sequential programs.
These challenges include: finding and expressing concurrency, managing data distributions, managing inter- processor communication, balancing the computational load, and simply implementing the parallel algorithm correctly.
Dichotomy of Parallel Computing Platforms. • An explicitly parallel program must specify concurrency and interaction between concurrent subtasks. • The former is sometimes also referred to as the control structure and the latter as the communication model.
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. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With