Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 10-11-2009
Member
 
Join Date: Aug 2009
Posts: 54
Multi-threaded application uses only one core

I made a ray-tracer in C#, but I can not get it to use more than one core (on my dual core its always at 50% CPU)

I use the following function that I wrote
Code:
public delegate void ParallelDelegate (int index); 
public class Parallel 
{
public static void ParallellFor (int start, int count, int increment, ParallelDelegate action) 
{
ParallelDelegate [] deleg = new ParallelDelegate [count]; 
IAsyncResult [] results = new IAsyncResult [count]; 
for (int i = start; i <start + count; i + = increment) 
{
deleg [i] = new ParallelDelegate (action); 
results [i] = deleg [i]. BeginInvoke (i, null, null); 
}
for (int i = 0; i <deleg.Length; i + +) 
deleg [i]. EndInvoke (results [i]); 
}
}
Which is then called to the code:
Code:
Parallell.ParallellFor (0, data.Length, 1, new ParallelDelegate (delegate (int i) 
{
DoRender (data [i]); 
}));
Are there any tricks that need to so that multiple cores will be used? I sat across from STA to MTA, and as expected it had obviously no effect (since this is the COM Interop to do)
Reply With Quote
  #2  
Old 10-11-2009
Member
 
Join Date: Nov 2008
Posts: 1,022
Re: Multi-threaded application uses only one core

ProcessThread.ProcessorAffinity Property is what you need. It sets the processors on which the associated thread can run. The processor affinity of a thread is the set of processors it has a relationship to. In other words, those it can be scheduled to run on.

Syntax
Code:
[BrowsableAttribute(false)]
public IntPtr ProcessorAffinity { set; }
ProcessorAffinity represents each processor as a bit. Bit 0 represents processor one, bit 1 represents processor two, and so on. The following are the subset of the possible ProcessorAffinity for a four-processor system

0x0001: 1
0x0002: 2
0x0003: 1 or 2
0x0004: 3
0x0005: 1 or 3
0x0007: 1, 2, or 3
0x000F: 1, 2, 3, or 4
Reply With Quote
  #3  
Old 10-11-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Multi-threaded application uses only one core

Do not thread it, too? I do not suggest it that you actually use multiple cores? Only the threads that run on another core is not as intensive. Just run it asynchronously means not that you get split up the program in multiple threads.
Reply With Quote
  #4  
Old 10-11-2009
Member
 
Join Date: Aug 2009
Posts: 54
Re: Multi-threaded application uses only one core

I have made a similar function that uses the Thread class (since I suspect the same as you) but it had little to say on the performance (more fluctuating between 45 and 60)

But so far I have no idea what. For all I know it can be GUI or graphics card that runs on the second core. I make the threads are fairly CPU intensive, so they should quickly get the CPU to sweat.

But if someone has a quad-core or more, so maybe you could set how much CPU the application used, and whether there are more than on my dual-core (which runs from 50-60% CPU)
Reply With Quote
  #5  
Old 10-11-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Multi-threaded application uses only one core

Just code it in a while (true) {} in the worker threads so that you will get well "debugger" just quite okay. Alternatively, find out a way to print the affinity.

On my end, with and without the program running on Quad Core Windows 7 64-bit. Total CPU usage was at 25% -30%. (see the attachment)
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Multi-threaded application uses only one core"
Thread Thread Starter Forum Replies Last Post
Multi Core Processor Programming maxxiejw Software Development 2 04-05-2011 06:02 PM
Information about Programming for Multi-core processors InnoVer Software Development 6 04-02-2011 11:24 AM
Visual Studio 2008 multi-Threaded DLL Panchu Windows Software 6 25-09-2010 10:00 AM
Multi-threaded Java Server Paul Guides & Tutorials 5 02-03-2010 05:40 PM
Multi core cpu working on Mac Sandroo Operating Systems 5 19-02-2010 11:48 AM


All times are GMT +5.5. The time now is 04:20 AM.