Author Topic: How to make program use multi core???????????  (Read 4610 times)

Offline Botsareus

  • Society makes it all backwards - there is a good reason for that
  • Bot God
  • *****
  • Posts: 4483
    • View Profile
How to make program use multi core???????????
« on: June 20, 2011, 05:55:38 PM »
I am running windowsXP, ran into this:

I wrote a program in .NET (64bit) , but when I run it, its max CPU usage is 53%, even if I set the priority hell high.

Is there any special code I should add to make it go (i.e. use) multi core???

Thank You for Any Help.

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
Re: How to make program use multi core???????????
« Reply #1 on: June 20, 2011, 06:30:44 PM »
This is a bit complicated.

First, multi core means multithreaded, but not vice versa (that is, you need at least one thread per core to fully utilize all cores, but a single core can run multiple threads).  So to use all the cores you need to delve into programming with threads.

Basically, traditional programming languages are based on a single threading model (that is, a single core).  In fact, the C++ standard pre C++0x didn't even have the concept that other hardware threads were even possible (which makes cross platform multithreaded programming in C++ a nightmare).

Usually, making a program use more cores is a huge engineering effort because threads will be contending with each other.  Imagine, for instance, if two cores try to write to the same place in memory at the same time.  Or worse, if one thread reads from a memory location while another writes to it.  This is made even worse with caching (but luckily that's more a problem for hardware engineers than programmers).

That said, there are newer languages built around the idea of massive core systems, where the paradigm is shifted from threads to kernels (that is, instead of telling threads what to do you just define some basic "jobs" that need doing, in small chunks, and the environment decides how to split those up between available cores).  See, for instance, OpenCL.

On top of all that, it's useful to be aware of Amdaahl's Law: not all tasks can be parallelized, and as you get more cores to throw at a problem the speed up is not linear if the problem is not perfectly parallelizable, which most problems aren't.

...

All that said, to your specific question, in the .NET world the basic idea to set up a thread object, give it a task, and then "launch" it.  For a full breakdown, see this series.

It's definitely not as simple as setting a "go faster" flag or anything.  It's up to the programmer to divide the computational task into smaller sub tasks that can be farmed out to other threads, and then to synchronize those threads to get a final result, while ensuring that the threads aren't contending with each other for resources.  Which is why multithreading is usually considered a fairly advanced programming issue.

Offline Botsareus

  • Society makes it all backwards - there is a good reason for that
  • Bot God
  • *****
  • Posts: 4483
    • View Profile
Re: How to make program use multi core???????????
« Reply #2 on: June 22, 2011, 06:32:44 PM »
Ok, so if I do
Code: [Select]
      WaitCallback A = new WaitCallback(Count);
      ThreadPool.QueueUserWorkItem(A, count);


      WaitCallback B = new WaitCallback(Count);
      ThreadPool.QueueUserWorkItem(B, count);


      WaitCallback C = new WaitCallback(Count);
      ThreadPool.QueueUserWorkItem(C, count);

Am I running three more cores?

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
Re: How to make program use multi core???????????
« Reply #3 on: June 22, 2011, 07:32:17 PM »
Probably (it actually depends on what the OS wants to do, but generally speaking the OS will evenly distribute work to different cores).

Offline Botsareus

  • Society makes it all backwards - there is a good reason for that
  • Bot God
  • *****
  • Posts: 4483
    • View Profile
Re: How to make program use multi core???????????
« Reply #4 on: July 02, 2011, 11:40:33 AM »
ok, ty.

For some reason I thought if you compile a vb6 to native it does threding automatically...