For grads like me, who are running simulations everyday, one CPU is far from enough. How can we make the best of multiple cores in our machine?
For those who are more familiar with Matlab, parallel computing toolbox provides an extremely simple solution. Here is a simple way to use multiple cores in Matlab:
matlabpool(4) % creating 4 threads, which is the maximum cores on my machine.
% your code
matlabpool close
In this case, Matlab will launch 4 threads to run your code; if your code has parfor, it will allocate each thread a certain (toughly equal) amount of effort in that loop.
For those who really want to get something done in real-time, here I provide the simplest way to use the cores that are idle for most of the time:
//make sure that your programming environment support openMP
#include “omp.h”
…
omp_set_num_threads(4); // again, creating 4 threads
#pragma omp parallel
{
unsigned int cpu_thread_id = omp_get_thread_num();
unsigned int num_cpu_threads = omp_get_num_threads();
// your code
}
In this case, you have to use the thread id to distinguish which specific code or data that thread will run. Well, if you meet any problem, feel free to contact me. See you on Friday.