I was trying to implement fibonacci function using snowfall parallel package in R. Following is the code I used.
vec <- 1:37
fib <- function(x)
{ if (x==0) return(0)
if (x==1) return(1)
if (x==2) return(2)
return(fib(x-1)+fib(x-2))
}
library(snowfall)
sfInit(parallel = TRUE, cpus = 4)
sfExport("vec","fib")
result <- sfLapply(vec,fib)
sfstop()
while running the code, I observed the cpu usage. Though I have asked to use 4 cores, the machine was always using 2 cores.
does that mean, my code isn't using all 4 cores? Can anyone help me with a guidance? Can I optimize this performance?