The Art of R Programming

(WallPaper) #1
You are probably familiar with dual-core machines. Any computer in-
cludes a CPU, which is the part that actually runs your program. In essence,
a dual-core machine has two CPUs, a quad-core system has four, and so on.
With multiple cores, you can do parallel computation!
This parallel computation is done withthreads, which are analogous
tosnow’s workers. In computationally intensive applications, you generally
set up as many threads as there are cores, for example two threads in a
dual-core machine. Ideally, these threads run simultaneously, though over-
head issues do arise, as will be explained when we look at general perfor-
mance issues in Section 16.4.1.
If your machine has multiple cores, it is structured as ashared-memory
system. All cores access the same RAM. The shared nature of the memory
makes communication between the cores easy to program. If a thread writes
to a memory location, the change is visible to the other threads, without the
programmer needing to insert code to make that happen.

16.3.2 Extended Example: Mutual Outlinks Problem in OpenMP...........


OpenMP is a very popular package for programming on multicore machines.
To see how it works, here is the mutual outlinks example again, this time in
R-callable OpenMP code:

1 #include <omp.h>
2 #include <R.h>
3
4 int tot; // grand total of matches, over all threads
5
6 // processes row pairs (i,i+1), (i,i+2), ...
7 int procpairs(int i, intm, int n)
8 { int j,k,sum=0;
9 for(j=i+1;j<n;j++) {
10 for(k=0;k<n;k++)
11 // find m[i][k]
m[j][k] but remember R uses col-major order
12 sum += m[nk+i]m[nk+j];
13 }
14 return sum;
15 }
16
17 void mutlinks(int
m, intn, doublemlmean)
18 { int nval =*n;
19 tot=0;
20 #pragma omp parallel
21 { int i,mysum=0,
22 me = omp_get_thread_num(),
23 nth = omp_get_num_threads();
24 // in checking all (i,j) pairs, partition the work according to i;
25 // this thread me will handle all i that equal me mod nth
26 for (i = me; i < nval; i += nth) {


Parallel R 341
Free download pdf