为什么 POSIX 线程比 OpenMP 慢
我正在配备 Xeon 处理器的 Mac Pro 上运行完全并行的矩阵乘法程序。我创建了 8 个线程(与核心一样多的线程),并且不存在共享写入问题(不写入相同的位置)。由于某种原因,我使用 pthread_create
和 pthread_join
的速度大约是使用 #pragma openmp
的两倍。
没有任何其他差异...相同的编译选项,两种情况下相同的线程数,相同的代码(显然除了 pragma/pthread
部分)等。
并且循环是 非常大——我没有并行化小循环。
(我无法真正发布代码,因为这是学校作业。)
为什么会发生这种情况? OpenMP 本身不使用 POSIX 线程吗?怎样才能更快呢?
I'm running a completely parallel matrix multiplication program on a Mac Pro with a Xeon processor. I create 8 threads (as many threads as cores), and there are no shared writing issues (no writing to the same locations). For some reason, my use of pthread_create
and pthread_join
is about twice as slow as using #pragma openmp
.
There are no other differences in anything... same compile options, same number of threads in both cases, same code (except the pragma/pthread
portions obviously), etc.
And the loops are very big -- I'm not parallelizing small loops.
(I can't really post the code because it's school work.)
Why might this be happening? Doesn't OpenMP use POSIX threads itself? How can it be faster?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(已编辑)
你的主线程在做什么?在没有看到你的代码的情况下,我猜测主线程实际上几乎没有运行,但在 pthread 完成时仍然消耗时钟周期,然后它再次启动并继续。每次给定的周期都会有暂停/继续其他线程的开销。
在 OpenMP 中,主线程可能会进入休眠状态,并在并行区域完成时等待唤醒事件。
(edited)
What is your main thread doing? Without seeing your code, I was guessing that the main thread is actually barely running, but still eating up clock-cycles while the pthreads finish, then it starts again and continues. Each time its given cycles there is overhead to pausing/continuing the other threads.
In OpenMP, the main thread probably goes to sleep, and waits for a wake-up event when the parallel regions finish.