Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • C# 2.0
  • Run on Windows Vista and Windows Server 2003

Updated again

  • This code is running as a service
  • I do not want to have the complete code... my goal here is to get your experience and how to start. Like I say, I have already use threads. What more can I do?
like image 441
Mister Dev Avatar asked Sep 23 '08 20:09

Mister Dev


1 Answers

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.

like image 101
5 revs Avatar answered Sep 19 '22 22:09

5 revs