如何使用Windows 7中的所有核心?

发布于 2024-09-06 02:10:19 字数 252 浏览 5 评论 0原文

我不确定这是否属于 Stackoverflow 还是 Superuser,但我想我应该在这里问。

我有一个用 C 语言编写的基于控制台的应用程序,目前在 Windows 7 64 位操作系统中需要大约一个小时才能终止。任务管理器报告应用程序仅使用了 25% 的可用 CPU。我想通过增加CPU使用率来减少运行时间。

有没有办法让应用程序使用全部四个核心(笔记本电脑有 Core i5)而不是仅一个?我假设任务管理器报告 25%,因为只有一个核心分配给该程序。

I am not sure if this belongs to Stackoverflow or Superuser but I thought I would ask here.

I have a console based application written in C which currently takes about an hour to terminate in Windows 7 64-bit OS. The task manager reports that the application is using only 25% of the available CPU. I would like to reduce the run time by increasing cpu usage.

Is there any way to let the application use all four cores (the laptop has Core i5) instead of just one? I am assuming that task manager reports 25% because only one core is allocated to the program.

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

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

发布评论

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

评论(5

毅然前行 2024-09-13 02:10:19

如果不将应用程序重写为多线程,则无法更改应用程序的行为。
您唯一可以做的就是,如果应用程序可以处理输入数据的范围,那么您可以启动具有不同范围的输入数据的应用程序的 4 个实例来处理,并在全部完成后合并结果。

想象一下渲染 3D 动画并且
渲染器是单线程的,但是
您可以指定开始和结束帧。
你有 100 帧来渲染你
将启动 4 个实例,并指定
起始帧和结束帧范围 0 - 25、26
- 50, 51 - 75, 76 - 100。然后您可以将所有输出合并到您的
最终电影文件。

Without re-writing the app to be multi=threaded no, you can't change the behavior of the app.
The only thing that you might can do, is if the app can process ranges of input data, then you can launch 4 instances of the app with different ranges of input data to process and the combine the results after they are all done.

Imagine rendering a 3D animation and
the renderer is single threaded, but
you can specify start and end frames.
You have 100 frames to render you
would start 4 instances, and specify
start and end frame ranges 0 - 25, 26
- 50, 51 - 75, 76 - 100. Then you would combine all the outputs to your
final movie file.

┾廆蒐ゝ 2024-09-13 02:10:19

应用程序需要以多线程方式编写才能使用多个内核。没有办法让单线程应用程序使用多个核心。

Applications need to be written in a multithreaded manner in order to use multiple cores. There is no way to make a single threaded app use multiple cores.

一城柳絮吹成雪 2024-09-13 02:10:19

您可以考虑使用 OpenMP 或 POSIX 线程,我推荐 OpenMP,因为它有一个非常基本的结构,根据您的程序可以使实现快速而简单。

You could look at using OpenMP or POSIX thread, I'd recommend OpenMP because it has a pretty basic structure that depending on your program could make implementation quick and easy.

渡你暖光 2024-09-13 02:10:19

目前还没有标准工具可以将任意 C 程序转换为多线程程序。因此,如果作为一名开发人员您没有显式编写多线程代码,则不要期望您的程序一次使用多个内核,除非您调用使用线程编写的第三方库函数。

为了更好地使用 CPU,您最多可以做的(如果与您的情况相关)是将输入分成 N 个块,并在每个块上多次运行程序,然后在输入全部完成后执行一些操作来组合输入。

At this point in time there are no standard tools for converting an arbitrary C program to a multithreaded program. Therefore, if as a developer you do not explicitly write multithreaded code, do not expect your program to use more than one core at a time unless you invoke a third-party library function that is written using threading.

The most you could do (if relevant in your case) to better use your CPU is to slice your input into N chunks, and run the program multiple time on each chunk, then do something to combine the inputs once they are all done.

橘虞初梦 2024-09-13 02:10:19

迂腐地说,您的应用程序已经使用了所有内核。正如您上面所说,没有一个特定的核心分配给该进程(除非您明确努力将您的进程绑定到一个特定的核心)。

该进程使用所有核心,但它按顺序使用它们:它在一个核心上运行一点,然后在另一个核心上运行一点,依此类推。即,平均而言,您的应用程序消耗的 CPU 时间均匀分布在系统中的所有内核上。由于在任何给定时刻仅使用一个核心,因此 CPU 负载永远不会超过 25% 的限制(显然,您的系统有 4 个核心)。

如果您想突破 25% 的限制,则必须能够同时使用两个或更多内核,这意味着至少部分代码必须并行运行 。为此,必须专门编写应用程序以并行运行。普通的单线程应用程序本身永远无法做到这一点。

Pedantically speaking, your application is already using all cores. There's no one specific core allocated to the process, as you say above (unless you make a explicit effort to tie your process to one specific core).

The process uses all cores, but it uses them sequentially: it runs a little on one core, then it runs a little more on another core and so on. I.e. on average the CPU time your application consumes is spread evenly across all cores in the system. Since only one core is used at any given moment of time, the CPU load will never cross the 25% limit (your system, apparently, has 4 cores).

If you want to cross the 25% limit, you have to be able to use two or more cores simultaneously, meaning that at least some of the code has to run in parallel. In order to do that the application has to be written specifically to run in parallel. An ordinary single-threaded application will never do that by itself.

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