如何在 R 中使用并行代码生成和绘制 Mandelbrot 集?

问题描述

我想在 R 中进行并行编码。到目前为止,我发现了一些类似以下的示例,它们运行良好:

library(parallelly)
library(parallel)
library(future)

# define function to test whether an number is prime
is_prime <- function(num)
{
  if(num == 2 | num == 3)
    return(TRUE)
  if(num == 1)
    return(FALSE)
  if(num %% 2 == 0)
    return(FALSE)
  root <- floor(sqrt(num))
  for(elt in seq(5,root))
  {
    if (num %% elt == 0)
      return(FALSE)
  }
  return(TRUE)
}

# get random sample of 1 million integers from integers between 1 and
# 10 million
# set seed so the random sample will be the same every time
set.seed(2)
sample_numbers <- sample(10000000,1000000)


start <- proc.time()
cl <- makeCluster(4) 
results <- parSapply(cl,sample_numbers,is_prime)
stopCluster(cl)
end <- proc.time()
print(end - start)

现在我想构建一个脚本来并行运行生成 mandelbrot 集的函数。在以下脚本中,按顺序生成并绘制了两个 Mandelbrot 集。我想将该过程转换为并行处理,其中 xlim、ylim 和颜色都可以传递给函数,但我不知道该怎么做。以下是旨在并行的代码

library(mandelbrot)
xlim <- c(-0.8438146,-0.8226294)
ylim <- c(0.1963144,0.2174996)

view <- mandelbrot(xlim,ylim,iterations = 1000,resolution = 1000)
cols <- mandelbrot_palette(RColorBrewer::brewer.pal(11,"Spectral"),fold = FALSE) 

plot(view,col = cols)


xlim <- c(-2,2)
ylim <- c(-2,2)
view <- mandelbrot(xlim,resolution = 1000,iterations = 1000)

blues <- RColorBrewer::brewer.pal(9,"Blues")
cols <- mandelbrot_palette(blues,in_set = "white",fold = TRUE,reps = 2)
plot(view,col = cols,transform = "log")

set 1 set 2

如果有人能帮助我正确构建以下脚本,我将不胜感激,该脚本允许并行生成和绘制 mandelbrot 集。

library(parallelly)
library(parallel)
library(future)
library(mandelbrot)


#The following is the function that allows you to call the mandelbrot set generator
get_mandelbrot_set <-function(xlim,color) {
  
  view <- mandelbrot(xlim,resolution = 1000)
  cols <- mandelbrot_palette(RColorBrewer::brewer.pal(11,color),fold = FALSE) 
  
  plot(view,col = cols) #I don't kNow if it can be graphed here,inside the function that is executed in parallel or has to be executed at the end,but I don't kNow how?
}

repetitions <- 1:100
color <-"Blues"
#Generate 100 values for x1,x2,y1,y2,but I don't kNow how?
xlim <- c(x1,x2)
ylim <- c(y1,y2)

start <- proc.time()
cl <- makeCluster(4) 
results <- parSapply(cl,repetitions,get_mandelbrot_set,xlim,color) #I don't kNow how to pass the parameters to the get_mandelbrot_set function
stopCluster(cl)
end <- proc.time()
print(end - start)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)