Search code examples
cembeddedoperating-systemprocessor

Dedicating all processor power to a task


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.


Solution

  • 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.