将所有处理器能力专用于某项任务
假设我们手头有一个处理器非常密集的任务,可以有效地并行化。我们如何才能将所有或几乎所有可用的处理器能力用于执行该任务?
任务可以是各种各样的,保存记录数字的迭代斐波那契数生成只是一个例子。
Let's say we have a very processor-intensive task at hand which could be effectively parallelized. How can we dedicate all or almost all available processor power to performing that task?
The task could be a variety of things, and iterative Fibonacci number generation that saves recorded numbers would be just one example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能提供的有关目标环境的详细信息太少。
通常,在使用 RTOS 时,您可以禁用中断,以便调度程序和中断服务例程都不会运行,或者您可以应用任务锁,以便调度程序不会运行,但 ISR 将继续运行。通过将任务的优先级提升到最高优先级,可以达到与任务锁相同的效果。
如果您不使用RTOS(或没有操作系统),您通常对调度没有太多控制权,但是如果您的线程运行时没有让步(即调用导致线程等待的函数),并且其他线程和进程不这样做需要很多周期,您的线程将获得几乎所有的 CPU。例如,如果您没有运行其他处理器密集型任务,Windows 中的繁忙循环大多数时候会在任务监视器中显示为一个核心的 100% 使用率。如果您的处理器有多个核心,则必须以某种方式并行化任务以使其使用所有核心。
I/O 调用通常会导致线程阻塞,因此您“保存”结果的要求可能会导致问题。解决方案是将结果缓冲在内存中(直接或作为队列或写入缓存),并推迟输出直到所有计算完成。
You have probably provided too few details about your target environment.
Typically when using an RTOS you can either disable interrupts so that neither the scheduler nor interrupt service routines will run, or you can apply a task lock so that the scheduler will not run, but ISR's will continue to run. You can achieve the same effect as a task lock by boosting the priority of the task to the highest priority.
If you are not using and RTOS (or no OS) you typically do not have that much control of scheduling, but if your thread runs without yielding (i.e. calling a function that causes the thread to wait), and other threads and processes do not need many cycles, your thread will get pretty much all of the CPU. For example a busy loop in Windows will most of the time show up as 100% usage of one core in the task monitor, if you are not running other processor intensive tasks. If your processor has multiple cores, you'll have to parallelize the task somehow to make it use all of them.
I/O calls typically cause thread blocking, so your requirement to 'save' the results may cause an issue. The solution is to buffer the results in memory (directly or as a queue or write-cache), and defer output until completion of all calculations.
操作系统处理 CPU 专用。大多数时候,如果你的程序需要更多的能力,它会得到它,但优先级较高(Linux 中的“nice”)进程在 CPU 时间请求队列中的位置会更高。
The operating system handles CPU dedication. Most of the time, if your program needs more power, it'll get it, but higher-priority ("nice" in Linux) processes will be higher in the queue for CPU time requests.
嗯,它在很大程度上取决于您的项目的上下文,以及处理器上运行的其他任务是什么。
考虑以下两个示例:
1/ 处理器仅执行一些低优先级作业,例如在 USB、CAN、SPI 或任何您拥有的设备上进行通信,但您没有这样做在计算过程中不必关心(处理器密集型任务),例如因为通信层等待该任务的输出。然后,您可以为此任务分配(静态或动态,具体取决于您所使用的操作系统)非常高的优先级。您还可以通过任何可用的同步来停止其他任务(消息、任务暂停...)
2/处理器正在执行高优先级作业,甚至是小型作业(看门狗、调节、测量),因此您可以为您的任务分配较低的优先级,因为它将占用微处理器的剩余电量,这就足够了!
因此,也许您会对如何为任务分配优先级有疑问……但这完全取决于您的固件架构以及您在操作系统或手动大“主”之间做出的选择!
如果您需要准确的答案,请向我们提供更多详细信息。
Well, it heavily depend on the context of your projet, and what are the other tasks running on the processor.
Consider these two examples :
1/ The processor is only doing some low priority jobs, let's say as communication on USB, CAN, SPI or whatever you have, that you don't care during the computation (processort intensive task), for example because commnication layer waits for the output of this task. Then, you can assign (statically or dynamically, depending on the operating system you have) this task a very high priority. You may also stop other tasks by whatever synchronization avaibale to you (messages, task pause ...)
2/ The processor is doing high priority jobs, even smalls one (watchdog, regulation, measurements), so you can assign your task a lower priority as it will take the residual microprocessor power, and that can be enough !
So, maybe you will have questions about HOW TO assign priority to task ... but it all depends on the your firmware architecture and the choice you made between a Operating system or a big 'main' by hand !
Give us more details if you need precise answers.
删除所有不必要的进程。
关闭由该进程或其他进程引起的所有磁盘访问和其他阻塞 IO。如果你必须这样做,那就晚点做,分批做。在并行线程中使用多个核心并设置 CPU 到进程或核心到进程的关联性可能会有所帮助 - 最后一个可能是特定于操作系统的。
将进程设置为高优先级,使其获得更大的份额。
Remove all unnecessary processes.
Turn off every disk access and other blocking IO caused by this or other process. If you have to do this, do it late, batch it. Using multiple cores in parallel threads and setting CPU-to-process or core-to-process affinity could help - the last one will be probably OS-specific.
Set the process high priority so it gets larger share.