0

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.

CPU usage given by Task Manager

does that mean, my code isn't using all 4 cores? Can anyone help me with a guidance? Can I optimize this performance?

Hansy Kumaralal
  • 169
  • 3
  • 13
  • There are much better algorithms for the Fibonacci sequence available. Trying to parallelize the [worst algorthm in the world](https://bosker.wordpress.com/2011/04/29/the-worst-algorithm-in-the-world/) seems like wasted effort. – Roland Sep 23 '16 at 13:18
  • Now I also started to feel like that.. :-/ Actually I am interested in studying parallelism, and that is why I used Fibonacci function as a demonstrator. Can you suggest me a better algorithm if possible? – Hansy Kumaralal Sep 23 '16 at 14:33

0 Answers0