3

I am using parallel and collect functions from the multi-core R package to parallelize a simple matrix multiplication code. The answer is correct but the parallelized version seems to take the same time as the serial version.

I doubt that it is performing on only one core (instead of 8 available on my machine!). Is there a way to detect this and ensure usage of more than 1 cores?

Here is my code:

library("multicore")

A = read.table("matrixA.txt")
B = read.table("matrixB.txt")
A = as.matrix(A)
B = as.matrix(B)
rows = dim(A)[1]
columns = dim(B)[2] 

C <- mcparallel(A%*%B)
C <- collect(list(C))
C <- as.matrix(C[[1]])

write.table(C,"matrixC_mc.txt",row.names=FALSE, col.names=FALSE)
zx8754
  • 52,746
  • 12
  • 114
  • 209
Aditi
  • 59
  • 4

2 Answers2

5

The detectCores() function of the parallel package included since R 2.14.0 does what you need to see if you actually have multiple cores:

R> parallel::detectCores()
[1] 8
R> 

Also, mcparallel by itself does not turn the matrix multiplication into a parallel operation (as that is a "hard" problem, see the ScaLAPACK libraries). But you can try something simple such as this:

R> X <- 1:1e3
R> rbenchmark::benchmark(serial=sapply(X, function(x) log(sqrt(x))), 
+>                       parallel=mclapply(X, function(x) log(sqrt(x))), 
+>                       replications=500)
      test replications elapsed relative user.self sys.self user.child sys.child
2 parallel          500  12.018    10.96     0.000    10.59      0.952     15.07
1   serial          500   1.097     1.00     1.208     0.00      0.000      0.00
R>

So for 500 replications of a simple operation (sqrt(log(x))) on short vectors, parallel gains. But life is never as easy: on larger vectors the difference disappears:

R> X <- 1:1e5
R> rbenchmark::benchmark(serial=sapply(X, function(x) log(sqrt(x))),
+>                       parallel=mclapply(X, function(x) log(sqrt(x))), 
+>                       replications=10)
      test replications elapsed relative user.self sys.self user.child sys.child
2 parallel           10   2.030     1.00     0.476    0.272      1.952     1.112
1   serial           10   2.821     1.39     2.228    0.592      0.000     0.000
R> 

The sad news is that parallel computing is tricky, and much harder than just sticking an expression into mcparallel or parallel.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
4

Depending on which OS you use, just check you processor usage. When I use parallel processing I can clearly see in top that all my six processors are used.

In regard to your code example, mcparallel (which you should not use, use parallel according to the manual) just spawns a new process with that expression. So if you spawn one new process with the matrix multiplication, just one core is used. Only when calling parallel multiple times, multiple process are started, and multiple cores are used. Maybe if you cut up your matrices, run them in multiple processes, and combine them later, you can gain some advantages. You could look at mclapply for this. But parallel linear algebra is maybe easier to do using not the multicore package, but using a blas (linear algebra library) version which supports parallel processing.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • is there a call in R (rather than the user eye-balling top) that would allow one to check the number of cores being used by a task – tim Jan 14 '15 at 13:28
  • If you have a new question, I would recommend you pose a new one and not post it as a comment. As a comment it will not garner that much attention. Under linux, this line could be useful: http://stackoverflow.com/questions/6481005/obtain-the-number-of-cpus-cores-in-linux. – Paul Hiemstra Jan 14 '15 at 13:37