Without speeding up useless printing using OpenMP

I just wrote my first OpenMP program that parallelizes a simple loop. I ran the code on my dual core machine and saw some speed when switching from 1 thread to 2 threads. However, I ran the same code on a school Linux server and did not see the acceleration. After trying various things, I finally realized that removing some useless printf statements led to significant code acceleration. Below is the main part of the code that I parallelized:

#pragma omp parallel for private(i)
for(i = 2; i <= n; i++)
{
  printf("useless statement");
  prime[i-2] = is_prime(i);
}

I assume that the printf implementation has significant overhead that OpenMP has to duplicate with each thread. What causes this overhead and why can't OpenMP overcome it?

+3
source share
4 answers

Conclusion, but maybe stdout is protected by a lock?

In general, printf is an expensive operation because it interacts with other resources (such as files, console, etc.).

My empirical experience is that printf is very slow on the Windows console, relatively much faster on the Linux console, but faster if redirected to a file or / dev / null.

I found that debugging printf can seriously affect the performance of my applications, and I use it sparingly.

, , /dev/null, , - ; .

, printfs , ?

+6

@Will answer...

, stdout , , - . printf, OP, , stdout, .

, OP printf, i, , .

- ?

+3

, .

#pragma omp parallel for private(i)
for(i = 2; i <= n; i++)

, OpenMP 3.0. , OMP_SCHEDULE type[,chunk],

  • , ,
  • chunk - ,

- openmp omp_set_schedule

is_prime ./ /

  prime[i-2] = is_prime(i);

, - , .

printf 2 / glibc libc libc/

  • ( FILE, stdout glibc )

printf , _IO_flockfile.

+1

- printf? printf ; , is_prime() , printf, () is_prime().

0
source

Source: https://habr.com/ru/post/1742758/


All Articles