C++/g++:并发程序

发布于 2024-09-05 12:25:52 字数 375 浏览 10 评论 0原文

我有一个 C++ 程序(源代码),据说可以并行工作。但是,如果我用 g++ 编译它(我使用 Ubuntu 10.04 和 g++ 4.4.3)并运行它,我的两个 CPU 核心之一会满载,而另一个则“什么也不做”。

所以我和给我这个程序的人聊了聊。有人告诉我,我必须为 g++ 设置特定标志才能为 2 个 CPU 核心编译程序。但是,如果我查看代码,我无法找到任何指向并行性的行。

所以我有两个问题:

  • 是否有任何用于多线程应用程序的 C++ 内在函数,即是否可以在没有任何额外库的情况下编写并行代码(因为我没有找到包含任何非标准库)?
  • 确实有 g++ 的标志告诉编译器为 2 个 CPU 核心编译程序并编译它以使其并行运行(如果:它们是什么)吗?

I got a C++ program (source) that is said to work in parallel. However, if I compile it (I am using Ubuntu 10.04 and g++ 4.4.3) with g++ and run it, one of my two CPU cores gets full load while the other is doing "nothing".

So I spoke to the one who gave me the program. I was told that I had to set specific flags for g++ in order to get the program compiled for 2 CPU cores. However, if I look at the code I'm not able to find any lines that point to parallelism.

So I have two questions:

  • Are there any C++-intrinsics for multithreaded applications, i.e. is it possible to write parallel code without any extra libraries (because I did not find any non-standard libraries included)?
  • Is it true that there are indeed flags for g++ that tell the compiler to compile the program for 2 CPU cores and to compile it so it runs in parallel (and if: what are they)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

︶ ̄淡然 2024-09-12 12:25:52

据我所知,没有编译器标志旨在使单线程应用程序利用并行性(这绝对是一个不平凡的操作),除了循环迭代的并行化(-ftree-parallelize-loops)之外,仍然必须小心激活;尽管如此,即使没有显式创建线程,也可能存在一些 OpenMP 并行化多个指令序列的指令。

AFAIK there are no compiler flags designed to make a single-threaded application exploit parallelism (it's definitely a nontrivial operation), with the exception of parallelization of loops iterations (-ftree-parallelize-loops), that, still, must be activated carefully; still, even if there's no explicit threads creation, there may be some OpenMP directives to parallelize several instruction sequences.

红墙和绿瓦 2024-09-12 12:25:52

在源代码中查找“thread”和/或“std::thread”的出现。

Look for the occurrence of "thread" and/or "std::thread" in the source code.

忆梦 2024-09-12 12:25:52

当前的 C++ 语言标准在语言或标准库中不支持多重处理。提议的 C++0x 标准确实对线程、锁等有一些支持。我不知道 g++ 的任何标志可以神奇地使您的程序进行多处理,并且很难看出这些标志可以做什么。

The current C++ language standard has no support for multi-processing in the language or the standard library. The proposed C++0x standard does have some support for threads, locks etc. I am not aware of any flags for g++ that would magically make your program do multi-processing, and it's hard to see what such flags could do.

很酷不放纵 2024-09-12 12:25:52

我唯一能想到的是 openMosixLinuxPMI(openMosix 的后继者)。如果代码使用进程,那么进程“迁移”技术使得可以将进程放在不同的机器上(安装了指定的 Linux 发行版)。

检查代码中的线程 (grep -i thread)、进程 (grep fork)。如果这些都不存在,则检查 MPI。我记得 MPI 需要一些额外的配置(仅用于教师的一些作业)。

如前所述,gcc(和其他)通过 带有一些编译指示的 OpenMP

The only thing I can think of is openMosix or LinuxPMI (the successor of openMosix). If the code uses processes then process "migration" technique makes is possible to put processes at work on different machines (which have the specified linux distribution installed).

Check for threads (grep -i thread), processes (grep fork) in your code. If none of this exists, then check for MPI. MPI requires some extra configuration since I recall (only used it for some homeworks in faculty).

As mentioned gcc (and others) implements some ways of parallelism with OpenMP with some pragmas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文