-
Notifications
You must be signed in to change notification settings - Fork 1
/
upDownUp_NP_Code_modif.R
123 lines (101 loc) · 3.02 KB
/
upDownUp_NP_Code_modif.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
source("upDownUp_NP_Code_Paralelizado.R")
source("upDownUp_NP_Code_NoParalelizado.R")
source("NucleoComun.R")
leerTamRentableVersionParalela <- function(){
tryCatch({
load("tamRentableVersionParalela.RData")
return(tamRentableVersionParalela)
}, warning = function (w){
return(NA)
}, error = function(e){
return(NA)
})
}
leernRentableVersionMultiple <- function(){
tryCatch({
load("tamRentableVersionMultiple.RData")
return(nRentableVersionMultiple)
}, warning = function (w){
return(NA)
}, error = function(e){
return(NA)
})
}
leerPeriodoRentableVersionMultiple <- function(){
tryCatch({
load("tamRentableVersionMultiple.RData")
return(periodoRentableVersionMultiple)
}, warning = function (w){
return(NA)
}, error = function(e){
return(NA)
})
}
leerMaxCores <- function(){
tryCatch({
load("maxCores.RData")
return(maxCores)
}, warning = function (w){
return(NA)
}, error = function(e){
return(NA)
})
}
tamRentableVersionParalela <- leerTamRentableVersionParalela()
nRentableVersionMultiple <- leernRentableVersionMultiple()
periodoRentableVersionMultiple <- leerPeriodoRentableVersionMultiple()
maxCores <- leerMaxCores()
#Funci?n para el c?lculo del mejor estimador Up-Down-Up
function1Local_modif<-function(v, parallel = "auto"){
if(parallel == "si"){
return(function1Local_par(v))
} else if (parallel == "no"){
return(function1Local_sec(v))
} else if (parallel == "auto") {
if( !is.na(tamRentableVersionParalela) && length(v) > tamRentableVersionParalela){
return(function1Local_par(v))
} else {
return(function1Local_sec(v))
}
} else {
stop("Value of parallel not allowed\n")
}
}
#Ejecuci?n en paralelo siempre
function1Local_multiple <- function(datos){
n <- nrow(datos)
if( !is.na(nRentableVersionMultiple) && n < nRentableVersionMultiple){
aux <- n >= as.numeric(names(periodoRentableVersionMultiple))
perAux <- periodoRentableVersionMultiple[aux]
tamRentable <- perAux[length(perAux)]
if(all(aux) == FALSE || ncol(datos) < tamRentable){
cat("La version multiple podria no ser adecuada para tan pocos datos")
flush.console()
}
}
ncores <- min(detectCores()-1,n)
if(is.na(maxCores) == FALSE){
ncores <- min(ncores,maxCores)
}
programacion <- CutBySize(n,nb=ncores)
cl <- makeCluster(ncores)
registerDoParallel(cl)
resultados <- foreach(pr = iter(programacion,by='row'),
.inorder=T, .combine = c,
.export = c("function1Local_sec","extremosLocales","busquedaMejorC")) %dopar% {
dyn.load("busquedaMejor.dll")
datosInt <- datos[pr[1]:pr[2],]
resultadosInt <- list()
if(is.null(nrow(datosInt))){
resultadosInt[[1]] <- function1Local_sec(datosInt)
} else {
for(i in 1:nrow(datosInt)){
resultadosInt[[i]] <- function1Local_sec(datosInt[i,])
}
}
return(resultadosInt)
}
stopCluster(cl)
registerDoSEQ()
return(resultados)
}