Results 1 to 5 of 5

Thread: Multi-threaded application uses only one core

  1. #1
    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)

  2. #2
    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

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    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.

  4. #4
    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)

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    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)

Similar Threads

  1. Multi Core Processor Programming
    By maxxiejw in forum Software Development
    Replies: 2
    Last Post: 04-05-2011, 06:02 PM
  2. Information about Programming for Multi-core processors
    By InnoVer in forum Software Development
    Replies: 6
    Last Post: 04-02-2011, 11:24 AM
  3. Visual Studio 2008 multi-Threaded DLL
    By Panchu in forum Windows Software
    Replies: 6
    Last Post: 25-09-2010, 10:00 AM
  4. Multi-threaded Java Server
    By Paul in forum Guides & Tutorials
    Replies: 5
    Last Post: 02-03-2010, 05:40 PM
  5. Multi core cpu working on Mac
    By Sandroo in forum Operating Systems
    Replies: 5
    Last Post: 19-02-2010, 11:48 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,188,428.41861 seconds with 17 queries