views:

114

answers:

5

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.

A: 

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.

Delan Azabani
+1  A: 

In windows you can simply do this by using the Windows task manager then click the Processes tab, then right click on your 'processor-intensive task' then click priority --> high priority.

Have the necessary cooling.

Derek
A: 

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.

TridenT
+3  A: 

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.

Clifford
@Clifford : Yes, boosting the priority and disabling interrupts is a good option. +1 for it ! But, can a single task be parallelised in the case of multiple cores to get the maximum CPU utilization ?
S.Man
@S.Man: Not sure about that; it would require specific language support. Handel C supports paralellisation at the statement level but that is for FPGA programming; it would be difficult to organise and verify and probably inefficient on a processor. Even then it is the programmer that has to decide what is parallelised not the compiler, the compiler could probably not determine what was sequentially dependent and what was not. In Handle C the feature is used to implement pipelines to generate on result per cycle for multi-cycle processes. http://en.wikipedia.org/wiki/Handel-C
Clifford
A: 

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.

binary_runner