paralellize plots

This commit is contained in:
Alwin Berger 2023-05-23 12:06:07 +02:00
parent 8c6f8c861f
commit 81cbddc1be

View File

@ -1,10 +1,18 @@
library("mosaic") library("mosaic")
library("dplyr") library("dplyr")
library("foreach")
library("doParallel")
#setup parallel backend to use many processors
cores=detectCores()
cl <- makeCluster(cores[1]-1) #not to overload your computer
registerDoParallel(cl)
args = commandArgs(trailingOnly=TRUE) args = commandArgs(trailingOnly=TRUE)
if (length(args)==0) { if (length(args)==0) {
runtype="timedump_253048_1873f6/timedump" runtype="timedump_253048_1873f6_full/timedump"
target="waters" target="watersv2"
outputpath="~/code/FRET/LibAFL/fuzzers/FRET/benchmark/" outputpath="~/code/FRET/LibAFL/fuzzers/FRET/benchmark/"
#MY_SELECTION <- c('state', 'afl', 'graph', 'random') #MY_SELECTION <- c('state', 'afl', 'graph', 'random')
SAVE_FILE=TRUE SAVE_FILE=TRUE
@ -85,57 +93,94 @@ frame2maxlines <- function(tr) {
return(tr) return(tr)
} }
all_maxlines = c() trace2maxpoints <- function(tr) {
for (bn in BASENAMES) { minval = tr[1,1]
collect = tr[1,]
for (i in seq_len(dim(tr)[1])) {
if (minval < tr[i,1]) {
collect = rbind(collect,tr[i,])
minval = tr[i,1]
}
}
return(collect)
}
sample_maxpoints <- function(tr,po) {
index = 1
collect=NULL
for (p in po) {
done = FALSE
while (!done) {
if (p<=tr[1,2] || (index < dim(tr)[1] && tr[index,2] <= p && p < tr[index+1,2])) {
tmp = tr[index,]
tmp[2] = p
collect = rbind(collect, tmp)
done = TRUE
} else { if ( p >= tr[dim(tr)[1],2] ) {
tmp = tr[dim(tr)[1],]
tmp[2] = p
collect = rbind(collect, tmp)
done = TRUE
} else {
index = index + 1
} }
}
}
return(collect)
}
#https://www.r-bloggers.com/2012/01/parallel-r-loops-for-windows-and-linux/
all_runtypetables <- foreach (bn=BASENAMES) %do% {
runtypefiles <- list.files(file.path(BENCHDIR,bn),pattern=sprintf(PATTERNS,target),full.names = TRUE) runtypefiles <- list.files(file.path(BENCHDIR,bn),pattern=sprintf(PATTERNS,target),full.names = TRUE)
if (length(runtypefiles) > 0) { if (length(runtypefiles) > 0) {
runtypetables <- lapply(seq_len(length(runtypefiles)), runtypetables_reduced <- foreach(i=seq_len(length(runtypefiles))) %dopar% {
function(i)read.csv(runtypefiles[[i]], col.names=c(sprintf("%s%d",bn,i),"times"))) rtable = read.csv(runtypefiles[[i]], col.names=c(sprintf("%s%d",bn,i),sprintf("times%d",i)))
runtypetables = trim_data(runtypetables) trace2maxpoints(rtable)
runtypetables = lapply(runtypetables, function(i) i[1]) }
list_of_maxlines = data2maxlines(runtypetables) #runtypetables <- lapply(seq_len(length(runtypefiles)),
tmp_frame <- Reduce(bind_cols, list_of_maxlines) # function(i)read.csv(runtypefiles[[i]], col.names=c(sprintf("%s%d",bn,i),sprintf("times%d",i))))
statframe <- bind_cols(rowMeans(tmp_frame),apply(tmp_frame, 1, sd),apply(tmp_frame, 1, min),apply(tmp_frame, 1, max)) #runtypetables_reduced <- lapply(runtypetables, trace2maxpoints)
names(statframe) <- c(bn, sprintf("%s_sd",bn), sprintf("%s_min",bn), sprintf("%s_max",bn)) runtypetables_reduced
all_maxlines = c(all_maxlines, list(round(statframe))) #all_runtypetables = c(all_runtypetables, list(runtypetables_reduced))
#all_maxlines = append(all_maxlines, list_of_maxlines)
#mean_maxline<-Reduce(function(a,b) a+b,list_of_maxlines,0)/length(runtypetables)
#all_maxlines=append(all_maxlines,mean_maxline)
} }
} }
min_length <- min(sapply(all_maxlines, function(x) dim(x)[1])) all_runtypetables = all_runtypetables[lapply(all_runtypetables, length) > 0]
all_maxlines=lapply(all_maxlines, function(v) v[1:min_length,]) all_points = sort(unique(Reduce(c, lapply(all_runtypetables, function(v) Reduce(c, lapply(v, function(w) w[[2]]))))))
all_maxlines <- foreach (rtt=all_runtypetables) %dopar% {
bn = substr(names(rtt[[1]])[1],1,nchar(names(rtt[[1]])[1])-1)
runtypetables_sampled = lapply(rtt, function(v) sample_maxpoints(v, all_points)[1])
tmp_frame <- Reduce(cbind, runtypetables_sampled)
statframe <- data.frame(rowMeans(tmp_frame),apply(tmp_frame, 1, sd),apply(tmp_frame, 1, min),apply(tmp_frame, 1, max))
names(statframe) <- c(bn, sprintf("%s_sd",bn), sprintf("%s_min",bn), sprintf("%s_max",bn))
#statframe[sprintf("%s_times",bn)] = all_points
round(statframe)
#all_maxlines = c(all_maxlines, list(round(statframe)))
}
one_frame<-data.frame(all_maxlines) one_frame<-data.frame(all_maxlines)
one_frame[length(one_frame)+1] <- seq_len(length(one_frame[[1]])) one_frame[length(one_frame)+1] <- all_points/(3600 * 1000)
names(one_frame)[length(one_frame)] <- 'iters' names(one_frame)[length(one_frame)] <- 'time'
typenames = names(one_frame)[which(names(one_frame) != 'iters')] typenames = names(one_frame)[which(names(one_frame) != 'time')]
typenames = typenames[which(!endsWith(typenames, "_sd"))]
ylow=min(one_frame[typenames]) ylow=min(one_frame[typenames])
yhigh=max(one_frame[typenames],worst_case) yhigh=max(one_frame[typenames],worst_case)
typenames = typenames[which(!endsWith(typenames, "_sd"))]
typenames = typenames[which(!endsWith(typenames, "_min"))] typenames = typenames[which(!endsWith(typenames, "_min"))]
typenames = typenames[which(!endsWith(typenames, "_max"))] typenames = typenames[which(!endsWith(typenames, "_max"))]
#yhigh=3400000
#yhigh=max(one_frame[typenames],405669)
ml2lines <- function(ml) { ml2lines <- function(ml) {
lines = NULL lines = NULL
last = -1 last = 0
for (i in seq_len(length(ml))) { for (i in seq_len(dim(ml)[1])) {
if (ml[[i]] != last || (i >= length(ml))) { lines = rbind(lines, cbind(X=last, Y=ml[i,1]))
if (i != 1) { lines = rbind(lines, cbind(X=ml[i,2], Y=ml[i,1]))
lines = rbind(lines, cbind(X=i, Y=last)) last = ml[i,2]
}
lines = rbind(lines, cbind(X=i, Y=ml[[i]]))
last=ml[[i]]
}
} }
return(lines) return(lines)
} }
plotting <- function(selection, filename, MY_COLORS_) { plotting <- function(selection, filename, MY_COLORS_) {
# filter out names of iters and sd cols # filter out names of iters and sd cols
typenames = names(one_frame)[which(names(one_frame) != 'iters')] typenames = names(one_frame)[which(names(one_frame) != 'times')]
typenames = typenames[which(!endsWith(typenames, "_sd"))] typenames = typenames[which(!endsWith(typenames, "_sd"))]
typenames = typenames[which(!endsWith(typenames, "_min"))] typenames = typenames[which(!endsWith(typenames, "_min"))]
typenames = typenames[which(!endsWith(typenames, "_max"))] typenames = typenames[which(!endsWith(typenames, "_max"))]
@ -149,16 +194,16 @@ if (SAVE_FILE) {png(file=sprintf("%s%s_%s.png",outputpath,target,filename), widt
par(mar=c(4,4,1,1)) par(mar=c(4,4,1,1))
par(oma=c(0,0,0,0)) par(oma=c(0,0,0,0))
plot(c(1,length(one_frame[[1]])),c(ylow,yhigh), col='white', xlab="Iters", ylab="WORT", pch='.') plot(c(1,max(one_frame['time'])),c(ylow,yhigh), col='white', xlab="Time [h]", ylab="WORT [insn]", pch='.')
for (t in seq_len(length(typenames))) { for (t in seq_len(length(typenames))) {
proj = one_frame[seq(1, dim(one_frame)[1], by=max(1, length(one_frame[[1]])/(10*w_))),] proj = one_frame[seq(1, dim(one_frame)[1], by=max(1, length(one_frame[[1]])/(10*w_))),]
#points(proj[c('iters',typenames[t])], col=MY_COLORS_[t], pch='.') #points(proj[c('iters',typenames[t])], col=MY_COLORS_[t], pch='.')
avglines = ml2lines(one_frame[[typenames[t]]]) avglines = ml2lines(one_frame[c(typenames[t],'time')])
lines(avglines, col=MY_COLORS_[t]) lines(avglines, col=MY_COLORS_[t])
if (exists("RIBBON") && ( RIBBON=='both' || RIBBON=='span')) { if (exists("RIBBON") && ( RIBBON=='both' || RIBBON=='span')) {
milines = ml2lines(one_frame[[sprintf("%s_min",typenames[t])]]) milines = ml2lines(one_frame[c(sprintf("%s_min",typenames[t]),'time')])
malines = ml2lines(one_frame[[sprintf("%s_max",typenames[t])]]) malines = ml2lines(one_frame[c(sprintf("%s_max",typenames[t]),'time')])
lines(milines, col=MY_COLORS_[t], lty='dashed') lines(milines, col=MY_COLORS_[t], lty='dashed')
lines(malines, col=MY_COLORS_[t], lty='dashed') lines(malines, col=MY_COLORS_[t], lty='dashed')
#points(proj[c('iters',sprintf("%s_min",typenames[t]))], col=MY_COLORS_[t], pch='.') #points(proj[c('iters',sprintf("%s_min",typenames[t]))], col=MY_COLORS_[t], pch='.')
@ -167,7 +212,7 @@ for (t in seq_len(length(typenames))) {
if (exists("RIBBON") && RIBBON != '') { if (exists("RIBBON") && RIBBON != '') {
for (i in seq_len(dim(proj)[1])) { for (i in seq_len(dim(proj)[1])) {
row = proj[i,] row = proj[i,]
x_ <- row['iters'][[1]] x_ <- row['time'][[1]]
y_ <- row[typenames[t]][[1]] y_ <- row[typenames[t]][[1]]
sd_ <- row[sprintf("%s_sd",typenames[t])][[1]] sd_ <- row[sprintf("%s_sd",typenames[t])][[1]]
min_ <- row[sprintf("%s_min",typenames[t])][[1]] min_ <- row[sprintf("%s_min",typenames[t])][[1]]
@ -203,9 +248,9 @@ if (exists("MY_SELECTION")) {
#MY_SELECTION=c('state', 'afl', 'random', 'feedlongest', 'feedgeneration', 'feedgeneration10') #MY_SELECTION=c('state', 'afl', 'random', 'feedlongest', 'feedgeneration', 'feedgeneration10')
#MY_SELECTION=c('state_int', 'afl_int', 'random_int', 'feedlongest_int', 'feedgeneration_int', 'feedgeneration10_int') #MY_SELECTION=c('state_int', 'afl_int', 'random_int', 'feedlongest_int', 'feedgeneration_int', 'feedgeneration10_int')
#MY_SELECTION=c('state', 'frAFL', 'statenohash', 'feedgeneration10') #MY_SELECTION=c('state', 'frAFL', 'statenohash', 'feedgeneration10')
#MY_SELECTION=c('state_int', 'frAFL_int', 'statenohash_int', 'feedgeneration10_int') MY_SELECTION=c('state_int', 'frAFL_int', 'statenohash_int', 'feedgeneration10_int')
MY_SELECTION=typenames MY_SELECTION=typenames
RIBBON='both' RIBBON='span'
for (i in seq_len(length(MY_SELECTION))) { for (i in seq_len(length(MY_SELECTION))) {
n <- MY_SELECTION[i] n <- MY_SELECTION[i]
plotting(c(n), n, c(MY_COLORS[i])) plotting(c(n), n, c(MY_COLORS[i]))