使用OpenMP的无用printf无法加速 [英] No speed-up with useless printf's using OpenMP

查看:88
本文介绍了使用OpenMP的无用printf无法加速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚编写了第一个OpenMP程序,该程序并行化了一个简单的for循环.我在双核计算机上运行了代码,看到从1个线程到2个线程时速度有所提高.但是,我在学校的linux服务器上运行了相同的代码,但没有看到速度提升.在尝试了不同的方法之后,我终于意识到,删除一些无用的printf语句会导致代码的速度大大提高.以下是我并行化的代码的主要部分:

I just wrote my first OpenMP program that parallelizes a simple for loop. I ran the code on my dual core machine and saw some speed up when going from 1 thread to 2 threads. However, I ran the same code on a school linux server and saw no speed-up. After trying different things, I finally realized that removing some useless printf statements caused the code to have significant speed-up. 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);
}

我猜想printf的实现有大量开销,必须与每个线程复制OpenMP.是什么造成了这种开销,为什么OpenMP无法克服它?

I guess that the implementation of printf has significant overhead that OpenMP must be duplicating with each thread. What causes this overhead and why can OpenMP not overcome it?

推荐答案

进行推测,但是stdout是否受锁保护?

Speculating, but maybe the stdout is guarded by a lock?

通常,printf是一项昂贵的操作,因为它会与其他资源(例如文件,控制台等)进行交互.

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

我的经验是,在Windows控制台上,printf非常慢,在Linux控制台上,printf相当快,但是如果重定向到文件或/dev/null,则速度仍然最快.

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

我发现printf-debugging会严重影响我的应用程序的性能,因此我很少使用它.

I've found that printf-debugging can seriously impact the performance of my apps, and I use it sparingly.

尝试将您的应用程序重定向到文件或/dev/null,以查看是否有明显影响;这将有助于缩小问题根源.

Try running your application redirected to a file or to /dev/null to see if this has any appreciable impact; this will help narrow down where the problem lays.

当然,如果printfs没用,为什么它们会循环出现?

Of course, if the printfs are useless, why are they in the loop at all?

这篇关于使用OpenMP的无用printf无法加速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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