在运行完R代码版本的GSEA后,我就在想不能把GSEA并行化运行,
最终发现,我并没有得到我想要的结果,百思不得其解,最终准备了一个小测试帮助我理解
i <- c(1:100)
i <- matrix(i,rep(1,length(i))) #识别的是矩阵,dim(X) must have a positive length
a <- vector(length = Ng, mode = "numeric")
#写一个函数,计算输入的值的开放,并且存入向量
mytest_fun <- function(i){
a[i] <- sqrt(i)
}
require(parallel)
cl.cores <- detectCores() #detectCores()检查当前电脑可用核数
cl <- makeCluster(28) #makeCluster(cl.cores)使用刚才检测的核并行运算
#这是坑,parApply里面用到的函数以及变量都需要申明
clusterExport(cl,c("sqrt","i","a"))
parApply(cl,i,2,mytest_fun)
最终有结果,但是没有存进a
使用<<- 符号设定全局变量
i <- c(1:100)
i <- matrix(i,rep(1,length(i)))
a <<- vector(length = Ng, mode = "numeric")
mytest_fun <- function(i){
a[i] <<- sqrt(i)
}
clusterExport(cl,c("sqrt","i","a"))
parApply(cl,i,2,mytest_fun)
依然没有存进a中
尝试其他两种方法,
for循环
a <<- vector(length =100, mode = "numeric")
mytest_fun <- function(i){
a[i] <<- sqrt(i)
}
for (i in 1:100) mytest_fun(i)
能够写入a
尝试再使用一下apply函数
i <- c(1:100)
i <- matrix(i,rep(1,length(i)))
a <<- vector(length =100, mode = "numeric")
mytest_fun <- function(i){
a[i] <<- sqrt(i)
}
apply(i,2,mytest_fun)
也能够写入a
下面尝试函数如何返回多个对象,把不同的结果写入矩阵a和矩阵b
parApply肯定是不行的
for循环呢?毫无疑问是可以的!!只要设定全局变量即可!
i <- c(1:100)
a <<- vector(length = 100, mode = "numeric")
b <<- vector(length = 100, mode = "numeric")
mytest_fun <- function(i){
a[i] <<- sqrt(i)
b[i] <<- log(i)
}
for (i in 1:100) mytest_fun(i)
apply呢?也是可以的!!!
i <- c(1:100)
i <- matrix(i,rep(1,length(i)))
a <<- vector(length = 100, mode = "numeric")
b <<- vector(length = 100, mode = "numeric")
clusterExport(cl,c("sqrt","i","a","b","log"))
mytest_fun <- function(i){
a[i] <<- sqrt(i)
b[i] <<- log(i)
}
apply(i,2,mytest_fun)
被<<-搞糊涂了,最终测试发现,只要在函数里面用这个符号就可以了
而parApply呢,无论如何返回不了多个对象,看来GSEA的并行化是泡汤了,难怪昨天使用GSEA并行化到了最后总是提示错误
原来是他没有返回该返回的对象。
现在的问题是:parApply不能把结果写入全局变量a中,解决这个问题,就可以把几乎所有的循环并行化啦!!!
暂时不能解决!!
网友评论