Improve time consumption within matrix column calculation in R

Viewed 39

I have to calculate values of a matrix which one column depends of others. In summary this code is part of a simulation where I want to see how values of m1[,1] would change in function of the amount of columns in this matrix and other parameters.

My problem is that this simulation took around 10 days (or more) to complete and I was wondering if it's possible to do this calculations in a more efficient way.

Here is the code (this is only an example of the operations for time composition, because those results have no significance):

library(microbenchmark)

number_of_columns <- 6 #In my simulation I'm using 10,000 columns

microbenchmark({
m1 <- matrix(1.12345678912356789, nrow = 6, ncol = number_of_columns)
m2 <- matrix(1.12345678912356789, nrow = 6, ncol = number_of_columns)

v1 <- rnorm(6)
v2 <- rnorm(6)
v3 <- rnorm(6)

for (j in 1:10000) { #I need to loop 1e07 times
  m1[,1] <- m2[, 2] + m2[, 1]*v1
  m2[, 1] <- m2[, 1] + m1[, 1]
  
  for (i in 2:(ncol(m1) - 1)) {
    m1[,i] <- (m2[, i + 1] - m2[, i])*v2
    m2[, i] <- m2[, i] + (m1[, i] - m1[, i - 1])*v3
  }
  m2[, 6] <- m2[, 5]
}
})

In each simulations I want to change values of v1, v2 and v3. Also, the number of columns of m1 and m2 would be 10000.

The result of time consumption of microbenchmark in my computer is:

     min       lq     mean   median       uq      max neval
 133.823 144.2911 154.8575 151.2269 157.7208 232.0194   100

I'm using a i5-1135G7. I also have a NVIDIA GeForce MX350 in my laptop. I tried to use library gpuR to run my simulation in my GPU but I did not understand how to install OpenCL.

0 Answers
Related