#### EJEMPLO NORMALES 4 ####### # ejemplo normales4.R con otras funciones de medicion de tiempo # Se generan nn m.a.s compuestas por r valores normales N(a,b^2) separadas en 2 apquetes ###### LIBRERIAS ###### library(snow) library(parallel) #library(bench) library(foreach) library(doParallel) #library(doFuture) library(microbenchmark) library(tidyr) library(ggplot2) ###### PARAMETROS ###### # Cuantas veces se repite la generación de una MAS de normales nn = 5000 # Parametros de la normal a=5 b=12 # Tamaño de cada MAS de normales r= 7000 # Las nn mas de norales Se dividirán en grupos de longitud de 2 a max_nod max_nod = 4 #8 ##### FUNCIONES ##### media_norm3 = function(x,n,r,a,b){ # media_norm: genera n secuencias de normales N(a,b^2) cada una de tamaño r # y las promedia y=rep(0,n[x]) for(j in 1:n[x]) y[j]=mean(rnorm(r,a,b)) #z = c(summary(y),larg=length(y)) c(summary(y),larg=length(y)) #return(z) # media_norm3(3,nums,r,a,b) } ######## TRABAJANDO CON VARIOS NODOS/OPERACIONES ##### #### Inicializacion res_norm_each = list() res_norm_eachpar = list() # res_norm_futsec = list() # res_norm_futpar = list() dif_each = 0 dif_eachpar = 0 # dif_futsec = 0 # dif_futpar = 0 res_normplot_each = list() res_normplot_eachpar = list() # res_normplot_futsec = list() # res_normplot_futpar = list() res_normplot_sapply = list() res_normplot_clusap = list() dif_sapply = 0 dif_clusap = 0 elapses = list() max1 = 0 compare = list() for (j in 1:max_nod) { cat('\n\n nodos: ',j,'\n\n') nums = sapply(splitIndices(nn,j),length) nums ###### GRAFICOS DE NODOS ####### ###### Secuencial sencillo res_normplot_each[[j]] = snow.time( foreach(x=1:j) %do% media_norm3(x,nums,r,a,b) ) ##### Paralelo DoParallel cl <- makeCluster(j) registerDoParallel(cl) clusterExport(cl,c('media_norm3')) res_normplot_eachpar[[j]] = snow.time( foreach(x=1:j) %dopar% media_norm3(x,nums,r,a,b) ) stopCluster(cl) ####### Secuencial sapply ###### res_normplot_sapply[[j]] = snow.time( sapply(1:j, media_norm3,nums,r,a,b) ) ####### Paralelo clusterapply cl <- makeCluster(j) clusterEvalQ(cl, library(dplyr)) clusterExport(cl,c('media_norm3')) res_normplot_clusap[[j]] = snow.time( clusterApply(cl,1:j, media_norm3,nums,r,a,b) ) stopCluster(cl) ###### Resumir resultados de tiempo # Maximo tiempo de todos los calculos, para usar en los graficos y que tengan la misma escala elapses[[j]] = c(res_normplot_sapply[[j]]$elapsed,res_normplot_each[[j]]$elapsed, res_normplot_clusap[[j]]$elapsed,res_normplot_eachpar[[j]]$elapsed) names(elapses[[j]]) = c('sec sapply','sec each','par clust','par doParallel') max1[j] = max(elapses[[j]]) par(mfrow = c(2,2)) # crear 4 graficos en una sola ventana plot(res_normplot_sapply[[j]],title = 'Secuencial sapply',xlim=c(0,max1[j]), xlab='Tiempo') plot(res_normplot_each[[j]],title = 'Secuencial foreach',xlim=c(0,max1[j]), xlab='Tiempo') plot(res_normplot_clusap[[j]],title = 'Paralelo clusterApply',xlim=c(0,max1[j]), xlab= paste('Tiempo',j,'nodos') ) plot(res_normplot_eachpar[[j]],title = 'Paralelo DoParallel',xlim=c(0,max1[j]), xlab=paste('Tiempo',j,'nodos')) par(mfrow = c(1,1)) ###### GRAFICOS DE DISTRIBUCION ##### cl <- makeCluster(j) registerDoParallel(cl) clusterExport(cl,c('media_norm3')) compare[[j]] <- microbenchmark( sec_each=foreach(x=1:j) %do% media_norm3(x,nums,r,a,b), sec_sapply=sapply(1:j, media_norm3,nums,r,a,b) , par_doParallel=foreach(x=1:j) %dopar% media_norm3(x,nums,r,a,b), par_clusterApply=clusterApply(cl,1:j, media_norm3,nums,r,a,b), unit = 's',times = 10) stopCluster(cl) print(compare[[j]]) boxplot(compare[[j]],unit = 's',log = FALSE,xlab = '',ylab = 'Tiempo', main =paste('Boxplot Secuencial vs Paralelo - ',j,'nodos/operaciones'), names = c('sec each','sec sapply','par doParallel','par clusApp')) #ggplot2::autoplot(compare[[j]]) } ######## GRAFICOS DE RENDIMIENTO ######### maxi = max(max1)*1.5 # tomamos el mayor tiempo de todos elap = do.call(rbind,elapses) # secuencial sapply plot(1:max_nod,elap[,1],lwd=2,col='tomato1',xlab='Nodos/Operaciones', ylab='Tiempo en segundos',main="Desempeño Secuencial vs Paralelo", ylim=c(0,maxi) ,type='l') points(1:max_nod,elap[,1],pch=16,col='tomato4',cex=1.6) # secuencial sencillo (each) lines(1:max_nod,elap[,2],lwd=2,col='orchid1') points(1:max_nod,elap[,2],pch=16,col='orchid3',cex=1.6) # paralelo clusterApply lines(1:max_nod,elap[,3],lwd=2,col='seagreen1') points(1:max_nod,elap[,3],pch=16,col='seagreen3',cex=1.6) # paralelo doParallel (each) lines(1:max_nod,elap[,4],lwd=2,col='dodgerblue2') points(1:max_nod,elap[,4],pch=16,col='dodgerblue4',cex=1.6) legend(1,maxi,pch=c(16,16,16),col=c('tomato1','orchid1','seagreen1','dodgerblue2'),cex=0.8, legend=dimnames(elap)[[2]]) # Aceleracion adif = elap[,1]/elap[,3] adif_each = elap[,2]/elap[,4] maxa = max(c(adif,adif_each))*1.1 mina = min(c(adif,adif_each))-0.1 plot(1:max_nod,adif,lwd=2,col='tomato1',xlab='Nodos',ylab='Aceleración', main="Gráfico de Aceleración", ylim = c(mina,maxa),type='l') points(1:max_nod,adif,pch=16,col='tomato4',cex=1.6) lines(1:max_nod,adif_each,lwd=2,col='dodgerblue2') points(1:max_nod,adif_each,pch=16,col='dodgerblue4',cex=1.6) legend(1,maxa/1.01,pch=c(16,16),col=c('tomato1','dodgerblue2'),cex=0.8, legend=c('sapply - clusterApply','sec foreach - par doParallel')) # Eficiencia efic = (adif/(1:max_nod))*100 efic_each = (adif_each/(1:max_nod))*100 maxe = max(c(efic,efic_each))+15 plot(1:max_nod,efic,lwd=2,col='tomato1',xlab='Nodos',ylab='Eficiencia',main="Gráfico de Eficiencia", ylim = c(0,maxe),type='l') points(1:max_nod,efic,pch=16,col='tomato4',cex=1.6) lines(1:max_nod,efic_each,lwd=2,col='dodgerblue2') points(1:max_nod,efic_each,pch=16,col='dodgerblue4',cex=1.6) legend(1,20,pch=c(16,16),col=c('tomato1','dodgerblue2'),cex=0.8, legend=c('sapply - clusterApply','sec foreach - par doParallel')) abline(h=50,col="black",lty=3,lwd=3) # linea que marca el 50% text(1.05,51,'50%',col='black',pos = 3) #########