1 # R script that produces histograms from benchmarked values
2 # Can be called from the bash script with the following code:
3 # export R_INPUT=$inputfile
4 # export R_OUTPUT=$outputfile
5 # export R_TYPE=$hist_type
7 # R CMD BATCH $this_script.R or Rscript $this_script.R
9 # Use functions from bench.h to benchmark execution time of the desired block, then Rhist.R script to read all timings
10 # and produce histograms and finally inject.h to inject values instead of executing block
12 # This is a small function to help merging empty nbins for dhist histograms
13 merge_empty_bins <- function (h){
19 if (length(h$counts)>1)
20 for(i in 1:(length(h$counts)-1)){
21 if(h$counts[i]!=0 || h$counts[i+1]!=0){
22 counts2[j]<-h$counts[i]
23 breaks2[j+1]<-h$breaks[i+1];
28 counts2[j]<-h$counts[length(h$counts)]
29 breaks2[j+1]<-h$breaks[length(h$breaks)]
38 source("analysis/hist_script/Rdhist.R")
40 inputfile<-Sys.getenv("R_INPUT")
41 outputfile<-Sys.getenv("R_OUTPUT")
42 type<-Sys.getenv("R_TYPE")
44 if (!(type %in% c("mean","default","sturges","scott"))){stop("Wrong histogram type")}
46 df<-read.table(inputfile,header=F)
48 names(df)<-c("NAME","TIME")
51 for(i in unique(NAME)){
52 vector1<-df[NAME==i,2]
53 if (length(vector1)==1){
54 #If there is only one element
55 h<-hist(vector1) # Just for R compatibility reasons
56 h$breaks<-c(vector1,vector1)
61 h<-hist(vector1) # Just for R compatibility reasons
62 h$breaks<-c(mean(vector1),mean(vector1))
63 h$counts<-length(vector1)
70 h<-dhist(vector1,nbins=type, plot = FALSE, lab.spikes = FALSE, a=5*iqr(vector1), eps=0.15)
72 h$count<-as.vector(h$counts)
74 h<-merge_empty_bins(h)
78 cat(i, file = outputfile, sep = "\t", append = TRUE)
79 cat("\t", file = outputfile, append = TRUE)
80 cat(sum(h$counts), file =outputfile, sep = "\t", append = TRUE)
81 cat("\t", file = outputfile, append = TRUE)
82 cat(sprintf("%.8f", mean(vector1)), file =outputfile, sep = "\t", append = TRUE)
83 cat("\t", file = outputfile, append = TRUE)
84 cat(length(h$breaks), file = outputfile, append = TRUE)
85 cat("\t", file = outputfile, append = TRUE)
86 cat(sprintf("%.8f", h$breaks), file = outputfile, sep = "\t", append = TRUE)
87 cat("\t", file = outputfile, append = TRUE)
88 h$density = h$counts/sum(h$counts)
89 cat(sprintf("%.8f", h$density), file = outputfile, sep = "\t", append = TRUE)
90 cat("\n", file = outputfile, append = TRUE)