Mister Dev
Mister Dev

Reputation: 10431

How to make my code run on multiple cores?

I have built an application in C# that I would like to be optimized for multiple cores. I have some threads, should I do more?

Updated for more detail

Updated again

Upvotes: 19

Views: 31831

Answers (6)

stephbu
stephbu

Reputation: 5082

I'd generalize that writing a highly optimized multi-threaded process is a lot harder than just throwing some threads in the mix.

I recommend starting with the following steps:

  1. Split up your workloads into discrete parallel executable units
  2. Measure and characterize workload types - Network intensive, I/O intensive, CPU intensive etc - these become the basis for your worker pooling strategies. e.g. you can have pretty large pools of workers for network intensive applications, but it doesn't make sense having more workers than hardware-threads for CPU intensive tasks.
  3. Think about queuing/array or ThreadWorkerPool to manage pools of threads. Former more finegrain controlled than latter.
  4. Learn to prefer async I/O patterns over sync patterns if you can - frees more CPU time to perform other tasks.
  5. Work to eliminate or atleast reduce serialization around contended resources such as disk.
  6. Minimize I/O, acquire and hold minimum level of locks for minimum period possible. (Reader/Writer locks are your friend)
    5.Comb through that code to ensure that resources are locked in consistent sequence to minimize deadly embrace.
  7. Test like crazy - race conditions and bugs in multithreaded applications are hellish to troubleshoot - often you only see the forensic aftermath of the massacre.

Bear in mind that it is entirely possible that a multi-threaded version could perform worse than a single-threaded version of the same app. There is no excuse for good engineering measurement.

Upvotes: 49

McKenzieG1
McKenzieG1

Reputation: 14179

Understanding the parallelism (or potential for parallelism) in the problem(s) you are trying to solve, your application and its algorithms is much more important than any details of thread synchronization, libraries, etc.

Start by reading Patterns for Parallel Programming (which focuses on 'finding concurrency' and higher-level design issues), and then move on to The Art of Multiprocessor Programming (practical details starting from a theoretical basis).

Upvotes: 2

Xavier Nodet
Xavier Nodet

Reputation: 5085

You might want to read Herb Sutter's column 'Effective Concurrency'. You'll find those articles here, with others.

Upvotes: 8

Lars Truijens
Lars Truijens

Reputation: 43602

To be able to utilize multiple cores more efficiently you should divide your work up in parts that can be executed in parallel and use threads to divide the work over the cores. You could use threads, background workers, thread pools, etc

Upvotes: 6

Lou Franco
Lou Franco

Reputation: 89152

You might want to take a look at the parallel extensions for .NET

http://msdn.com/concurrency

Upvotes: 8

Chris Wenham
Chris Wenham

Reputation: 24017

For C#, start learning the LINQ-way of doing things, then make use of the Parallel LINQ library and its .AsParallel() extension.

Upvotes: 5

Related Questions