rm(list=ls())
mat<-data.frame(matrix(NA,32,11))
mat[c(1,11,21),]<-mtcars[c(1,11,21),]
tasks <- list(
job1 = function(){repeat{na<-which(is.na(mat1[,1]));mat1[na,]=mat1[na-1,];if(all(is.na(mat1[,1])==F)==T)break}mat1},
job2 = function(){repeat{na<-which(is.na(mat2[,1]));mat2[na,]=mat2[na-1,];if(all(is.na(mat2[,1])==F)==T)break}mat2},
job3 = function()repeat{na<-which(is.na(mat3[,1]));mat3[na,]=mat3[na-1,];if(all(is.na(mat3[,1])==F)==T)break}mat3},
# To check that the computations are indeed running in parallel.
job4 = function() for (i in 1:5) { cat("4"); Sys.sleep(1) },
job5 = function() for (i in 1:5) { cat("5"); Sys.sleep(1) },
job6 = function() for (i in 1:5) { cat("6"); Sys.sleep(1) }
)
mat<-data.frame(matrix(NA,32,11))
mat[c(1,11,16),]<-mtcars[c(1,11,21),]
mat1<-mat[1:10,]
mat2<-mat[11:15,]
mat3<-mat[16:32,]
cl <- makeCluster(length(tasks))
clusterExport(cl, list("mat1","mat2","mat3")) # make sure mtcars is loaded
out <- clusterApply(cl, tasks, function(f) f())
stopCluster(cl)
0
A
回答
0
试试这个:
cl <- makeCluster(length(tasks))
clusterExport(cl, "mat")
out <- clusterApply(cl, tasks,function(f) f())
你需要做的对象提供给工作进程。这由clusterExport
完成。
此外,请注意您的功能有点奇怪。在每个工作进程中完成相同的任务,这可能不是你打算做的。请注意,简单地写mat[1:10]
只是打印相应的输出,没有别的。尝试以下
job1 = function() {
mat <- mat[1:10, ]; # note the difference here
repeat{
na<-is.na(mat[,1]);
mat[na,] <-mtcars[na,];
if(all(is.na(mat[,1]))==F) break}
}
对其他工作做类似的调整。然后调用
cl <- makeCluster(length(tasks))
clusterExport(cl, list("mat", "mtcars")) # make sure mtcars is loaded
out <- clusterApply(cl, tasks, function(f) f())
stopCluster(cl)
这是仍有小幅低效率的,因为你是整个矩阵输出到每个工作进程,然后只使用一个子集。但我想这个原理是可以理解的。
相关问题
- 1. 如何同时运行多个“任务”?
- 2. 在crontab中同时运行多个任务没有延迟
- 3. 任务不同时运行
- 4. 在r中同时运行多个作业(并行)
- 5. 运行多个任务
- 6. 运行多个任务
- 7. 运行多个rake任务
- 8. 在R的不同线程/任务中运行不同的函数R
- 9. 任务调度程序可以同时运行多少个任务
- 10. 使用pyspark的Celery任务的Flask API不能同时运行多个任务
- 11. BackgroundWorker当前正忙,无法同时运行多个任务
- 12. C#以不同的时间间隔运行多个任务
- 13. grunt.js:可以同时运行多个npm任务吗?
- 14. 如何安全地同时运行多个任务?
- 15. ffmpeg在一个进程中同时执行多个任务ID
- 16. 在Java中同时运行两个任务
- 17. 如何在同一事务中并行运行多个数据流任务?
- 18. Plesk计划任务 - 运行多个脚本与多个任务
- 19. 如何在android中同时运行多个服务而没有任何冲突?
- 20. 在不同线程中运行任务
- 21. 与NAnt并行运行多个任务
- 22. 并行运行多个地图任务
- 23. 如何在两个不同的会话中同时并行运行相同的计划任务并行运行?
- 24. Laravel:同时运行cron作业任务
- 25. C#:使任务同时运行
- 26. 限制同时运行的任务数
- 27. 在SBT中多次运行任务
- 28. 如何在PostgreSQL中同时运行多个事务
- 29. 同时运行多个类?
- 30. 运行多个Asyntasks同时
可否请您提供适合上述任务的语法。 – 789372u