CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 34

Hybrid View

  1. #1
    Join Date
    Dec 2008
    Posts
    7

    Angry multicore support in C#

    Hi,
    I tried searching but am coming up short.. I have a quadcore PC running Win XP and am looking for a solution where I can selectively assign jobs to each processor - i.e I want to pick one of my processors and tell it to exclusively work on some specific data crunching. I tried to find a library that will allow me to do so but with no luck.
    Anyone know of a library that will let me assign jobs to specific processor? Any other approaches and solutions are greatly appreciated

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: multicore support in C#

    The operating system will do a much better job of this than you can. Why not let the OS decide?
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Dec 2008
    Posts
    7

    Re: multicore support in C#

    Quote Originally Posted by Mutant_Fruit View Post
    The operating system will do a much better job of this than you can. Why not let the OS decide?
    we tried but are not getting performance expected (hoped for) results ... idea now is to see if we can dedicate core or two to do specific job....if I can olny figure out how to do it in C#

  4. #4
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: multicore support in C#

    Quote Originally Posted by cblind View Post
    we tried but are not getting performance expected (hoped for) results ... idea now is to see if we can dedicate core or two to do specific job....if I can olny figure out how to do it in C#
    I would go out on a limb and say that your multithreaded implementation is the cause of the performance problem. dedicating work to a single processor is usually never going to be as efficient as letting the operating systems thread scheduler deal with your program. doing multithreaded programming bad is worse IMHO than doing singlethreaded programming well.

    if scalability is what your ultimate goal is, dictating what cpu is doing what is going to over complicate your implementation and criple its ability to scale as you'll need to implement what's already implemented in the OS (and I'd venture to guess that the authors of the windows kernel know a thing or two more about concurrent processing than you might). Your app can run fine w/ you doing your own scheduling for 2 CPU's, or 4 CPU's but what if you throw your app on a 16 CPU system? does your app make use of a max of 4 CPU's and leave the other 12 CPU's unattended?

    having said that, you can spawn processes (small console exe's) that do the work. doing so allows you to specify processor affinity and thread priority.
    Code:
    ProcessStartInfo psi = new ProcessStartInfo("notepad.exe");
    Process p = new Process();
    p.StartInfo = psi;
    p.Start();
    byte cpu = 0x8; // bitmask for processors: 1 = cpu 1, 10 (0x2) = cpu 2, 100 (0x4) = cpu 3, 1000 (0x8) = cpu4
    p.ProcessorAffinity = new IntPtr(cpu);
    or you can simply set it in the worker exe:
    Code:
    using System;
    using System.Diagnostics;
    
    namespace CpuSpecificTask {
        class Program {
            static void Main(string[] args) {
                Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(0x08);
                Console.ReadLine();
            }
        }
    }
    Last edited by MadHatter; December 9th, 2008 at 11:41 AM.

  5. #5
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: multicore support in C#

    As far as I know, there are several project of developing C# with native support for parallel computation, but there are not in production phase yet.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  6. #6
    Join Date
    Nov 2008
    Posts
    15

    Re: multicore support in C#

    C# 4.0 and .net 4.0 will ship with functions supporting multiple cores.
    Untill then take a look here and here.

  7. #7
    Join Date
    Dec 2008
    Posts
    7

    Re: multicore support in C#

    Hi,
    thanks all!
    Boudino - would you mind sending me references (links) to the projects?

    Jensecj - thanks ! that article (aforge) is very informative, as well as video links

    MadHater - all good points and observations. I am not ruling out implementation as an issue although best practices were followed. Currently we let OS deal with threads but expected performance is not there. Perhaps we are asking too much from it....

  8. #8
    Join Date
    May 2007
    Posts
    1,546

    Re: multicore support in C#

    Forcing your application to only run on a specific core can give you significantly worse performance. Suppose another developer has decided that forcing his app to use thread0 on core0 and thread1 on core1 and you do the exact same. Now imagine you're running on a quadcore. You've just halved the performance of your application for no reason other than the mistaken belief that forcing a thread to operate on a specific core is a good thing.

    There are limited scenarios where setting processor affinity may be helpful, but those scenarios don't exist on a standard end-user computer system.

    If you're not getting the performance you're expecting, it's much more likely that your expectations are wrong, or your threading implementation is sub-optimal.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  9. #9
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: multicore support in C#

    Quote Originally Posted by cblind View Post
    best practices were followed
    Care to list them?

    Programming by numbers?

    Currently we let OS deal with threads but expected performance is not there. Perhaps we are asking too much from it....
    Or perhaps you don't know how to ask for it, or your code isn't as good as you think it is. The best solution would be to let the guys here have a look at it.. i.e. let them see the actual problem, not the problem with your envisaged solution. All too often we see this:

    Code is broken
    Solution is imagined
    Solution is broken
    Get help for fixing broken solution

    It should be:

    Code is broken
    Get help for fixing broken code
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  10. #10
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: multicore support in C#

    Just as an FYI, I write a number of high performance (ie realtime analysis of laboratory instrumentation) application in C# that are massively parallallized. 95% of the time I do not have to manually deal with assignments to spcecific cored (there is one video application soon to be poerted to a dedicated controller that is the exception).

    Usage of the Parallel Extensions library, along with a synthesized "Futres and Promises" architecture allow me to utilize all of the execution paths (the production lab has a sweet 16 core system) running on both my Q6600 and (brand new) i7 development machines.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  11. #11
    Join Date
    Dec 2008
    Posts
    7

    Re: multicore support in C#

    All valid points and questions

    Quote Originally Posted by cjard View Post
    Care to list them?

    Programming by numbers?

    I am not quite sure I understand reference to "programming by the numbers".... I realize that "best practices" statement is a bit general / umbrella statement. By that I meant that code documentation is done well, care was taken not to have race conditions, modular development, etc.

    I would love if I could post the code for peer review and help, but I can't for two main reasons:
    - it's huugeeee; it wouldn't be appropriate for typical forum code snippet (If I was sure where the problem in the code is I would post only that part, but we don't know

    - I don't think company bosses would appreciate company's secret souse in public domain

    There are 3 people working on the code - so it's manageable from standpoint of review, and who is doing what and where in the code (reference to an earlier post)

    But there is performance issue for some reason....hence my original post.

    Perhaps to clarify things - we have big app which is doing few things, including (desired) real time video processing - that is part of the app that I would like to dedicate core or two for. Currently we are able to get about 7 frames per second (and that is pushing it). I would like it to be 2x or 3x better.

    Ideally I would like us to move to RT OS / embedded target, but it's not an option for now. So we are left with trying to squeeze / optimize things on PC box....

  12. #12
    Join Date
    May 2007
    Posts
    1,546

    Re: multicore support in C#

    Unless you own 100% of the processes on the machine, you should not force a specific process to a specific core. You won't increase performance and you run the risk that some other developer has had the same (bad) idea and then both of your programs will suffer a huge performance penalty as they fight for the same core despite having several completely free cores.

    If you want faster encoding, the physical encoer itself must be multithreaded. Other than buffering input and output, there is absolutely nothing else you can do to speed up the encoder (other than reducing the quality settings). Once again, if you aren't getting the performance you're expecting, either your expectations are wrong, you're code doesn't benefit from multiple cores or you've just written it badly. Forcing a process to a core will not help. It definitely won't give you the 2x or 3x boost you appear to be looking for.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  13. #13

    Re: multicore support in C#

    Actually, this is not true.

    For many years methods have been used to manage multi-processor based systems very well actually, using factors.

    In some cases, you can gain the power of 1 CPU for every 4 managed on very poorly managed multi-processor based systems. This also applies to entire networks, where systems have the ability to launch processes on other systems in the network.

    More here: http://www.overlord.com/Balancer_Brochure.html

    While the above maybe for other muti-processor based systems, the same concepts apply to PC based muti-processor systems as well as any multi-processor based system.

    I designed and created the software above ("After Black-Monday for the NYSE") and it is used by many companies world wide including branches of the U.S. Military, it is proven fact that managed muti-processor systems perform better than unmanaged ones.
    Last edited by ZOverLord; December 10th, 2008 at 06:59 AM.

  14. #14

    Re: multicore support in C#

    This has nothing to do with owning or managing 100% of the active threads, again, your statement is not true.

    A vast majority of the companies that use these automatic load balancing techniques, have lots of 3rd party software, and never have seen or will see the source code for that software, and are not in 100% control of what processes or threads will be launched on their system by other systems in the network, let alone what processes will launch at specific times, since most of the load is user driven which makes it very dynamic.

    This statement is completely false:

    "You won't increase performance and you run the risk that some other developer has had the same (bad) idea and then both of your programs will suffer a huge performance penalty as they fight for the same core despite having several completely free cores."

    Having properly weighted factors used to determine process/thread placement in cores for the enviornment you are in, gives you much better odds of increasing your performance, if even for a short period of time, than blind-luck does.

    It also would make it impossible to "fight for the same core despite having several completely free cores" in all cases, again, worse case being better off in the short term than long term.

    Having this kind of load-balancing done on a network/system basis would be the preferred choice, however this is a vaild seconday choice as well for a process that launches many processes as was stated here, when this network/system load-balancing ability is not present already where this process in question is going to be running.

    This is not speculation on my part, this comes from over 20 years of experience of designing, selling, testing and having customers doing this on many different systems with many different combinations of software and workloads.

    This is Fact not Fiction ;-) Had you looked at the link I provided in my prior post you would have understood that, by seeing the testements of corporate customers using these techniques and the processing advantages they have gained and the hardware costs they have saved.
    Last edited by ZOverLord; December 10th, 2008 at 10:40 AM.

  15. #15
    Join Date
    May 2007
    Posts
    1,546

    Re: multicore support in C#

    Quote Originally Posted by ZOverLord View Post
    This statement is completely false:

    "You won't increase performance and you run the risk that some other developer has had the same (bad) idea and then both of your programs will suffer a huge performance penalty as they fight for the same core despite having several completely free cores."
    Let me make my position clearer:

    On a standard desktop (or standard server), if you app1thread1 is forced to execute on core0 and app2thread1 is forced to execute on core0, then both of those threads will execute on core0. If you have a 10000 core box, those two threads will still execute on core0.

    Therefore both applications will have 1/2 the performance they should have as compared to letting the OS schedule the two threads onto core0 and core1 respectively.

    So yes, my statement is correct. Sure, you can employ third party software which will change the processor for active threads/processes, but then you're just substituting the OS scheduler for a third party scheduler. Sure, this can work. The third party scheduler could easily be better. But unless you employ this software, you *should not* force threads to execute on a specific core.

    EDIT:
    If you have third party software which manages which thread operates on which core, then trying to set affinity in code is a waste of time as the third party scheduler will just override that. So you've just weakened the argument for trying to set the affinity through code.

    Having properly weighted factors used to determine process/thread placement in cores for the enviornment you are in, gives you much better odds of increasing your performance, if even for a short period of time, than blind-luck does.

    It also would make it impossible to "fight for the same core despite having several completely free cores" in all cases, again, worse case being better off in the short term than long term.
    Unfortunately these properly weighted factors will not be available in usercode.
    Last edited by Mutant_Fruit; December 10th, 2008 at 12:39 PM.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured