Download Parallel Computing for Data Science - matloff

Transcript
88
straint. Of course, one can still have a shared scalar, as a 1 × 1 matrix. A
shared matrix will have the type big.matrix.
Note that one must use brackets in referencing the shared matrices. For
instance, to print the shared matrix m, write
print (m[ , ] )
rather than
print (m)
The latter just prints out the location of the shared memory object.
As will be seen below, snow is also used as the mechanism to launch the
threads themselves.
Though Rdsm is intended to run on shared-memory machines, bigmemory enables shared variables with the storage in disk files. Thus Rdsm
can also be used to provide the shard-memory world view on a distributed
system, e.g. clusters.
5.4
Example: Matrix Multiplication
The standard “Hello World” example of the parallel processing community
is matrix multiplication. Here is the Rdsm code, along with a small test.
5.4.1
The Code
1 # m a t r i x m u l t i p l i c a t i o n ; t h e p r o d u c t u %∗% v i s computed on t h e
2 # snow c l u s t e r c l s , and w r i t t e n in−p l a c e i n w ; w i s a
3 # b i g . matrix o b j e c t
4
5 mmulthread <− function ( u , v , w) {
6
require ( p a r a l l e l )
7
# d e t e r m i n e which rows t h i s t h r e a d w i l l h a n d l e
8
myidxs <− s p l i t I n d i c e s (nrow( u ) , myinfo$ nwrkrs ) [ [ myinfo$ i d ] ]
9
w [ myidxs , ] <− u [ myidxs , ] %∗% v [ , ]
10
0 # don ’ t do e x p e n s i v e r e t u r n o f r e s u l t
11 }
12
13 t e s t <− function ( c l s ) {