Santi Peñate-Vera
Santi Peñate-Vera

Reputation: 1186

processor usage (force full usage)

First of all, I did not study computer science, and I teached programming my self, said that;

I have a C# program that runs heavy power flow simulations for very large demand profiles. I use a laptop with an intel i7 processor (4 cores -> 8 threads) under windows 7. When I run the simulations the processor ussage is arround 32%.

I have read other threads about process prority, and I have more or less clear that when something runs on the OS, it runs at full speed, but the OS keeps the interfaces responsive (is this correct?)

Well I want to "completely flood the processor" with the simulation; get a 100% of usage (if possible) ?

Thanks in advance.

Ref#1: Is there a way of restricting an API's processor resource in c#?

Ref#2: Multiple Processors and PerformanceCounter C#

EDIT: piece of code that calls the simulations after removing the non relevant stuff

while ( current_step < sim_times.Count ) {

    bool repeat_all = false;
    power_flow( sim_times[current_step] );

    current_step++;
}

I know it is super simple, and it is a while becausein the original code I may want to repeat a certain number of steps.

The power_flow() function calls a third party software, so I guess is this third party software the one that should do the multy threading, isn't it?

Upvotes: 1

Views: 2720

Answers (2)

Paul Coghill
Paul Coghill

Reputation: 677

You can't really force full usage - you need to provide more work for the processor to do. You could do this by increasing the number of threads to process more data in parallel. If you provide your samples of your source code we could provide specific advice on how you could alter your code to achieve this.

If you are using a third party piece of software for data processing, this often makes it difficult to split into multiple threads. One tactic that's often helpful is to split up your input data, then start a new thread for each data set. This requires domain specific knowledge to know what you can split up. For simulations, once you have split up one run as much as possible, an alternative is to process multiple runs in parallel.

The Task Parallel Library can be really useful to break down your code into multiple threads without much refactoring. Particularly the data parallelism section.

One big note of caution - you need to make sure what you're doing is thread-safe. I'll provide some further reading for you. The basic principal is that you need to made sure if you're sharing data between threads then you need to be very careful they don't affect one another - this can cause bizarre problems!

As for your question regarding interfaces - within your process you can allocate thread priority to each thread. An interface thread is just a thread like any other. Usually a UI thread is given the highest priority to remain responsive, whereas a long background process is given a normal/below normal priority as it can be processed during idle time. You can set the priority manually, the default for any new thread is Normal.

Upvotes: 3

Gusdor
Gusdor

Reputation: 14334

You should process these simulations in parallel so that you use as many CPUs as possible. Do this by creating a Task for each simulation run.

using System.Threading.Tasks;

...

List<Task> tasks = new List<Task>();

for(;current_step < sim_times.Count; current_step++)
{
    var simTime = sim_times[current_step]; //extract the sim parameter
    Task.Factory.StartNew(() => power_flow(simTime)); //create a 'hot' task - one that is immediately scheduled for execution
}

Task.WaitAll(tasks.ToArray()); //wait for the simulations to finish so that you can process results.

Data Parallelism (Task Parallel Library)

Upvotes: 0

Related Questions