starpu_mlr_analysis.Rmd 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. ```{r Setup, echo=FALSE}
  2. opts_chunk$set(echo=FALSE)
  3. ```
  4. ```{r Load_R_files_and_functions}
  5. print_codelet <- function(reg,codelet){
  6. cat(paste("/* ############################################ */", "\n"))
  7. cat(paste("/*\t Automatically generated code */", "\n"))
  8. cat(paste("\t Check for potential errors and be sure parameter value are written in good order (alphabetical one by default)", "\n"))
  9. cat(paste("\t Adjusted R-squared: ", summary(reg)$adj.r.squared, "*/\n\n"))
  10. ncomb <- reg$rank - 1
  11. cat(paste("\t ", codelet, ".model->ncombinations = ", ncomb, ";\n", sep=""))
  12. cat(paste("\t ", codelet, ".model->combinations = (unsigned **) malloc(", codelet, ".model->ncombinations*sizeof(unsigned *))", ";\n\n", sep=""))
  13. cat(paste("\t if (", codelet, ".model->combinations)", "\n", "\t {\n", sep=""))
  14. cat(paste("\t for (unsigned i = 0; i < ", codelet, ".model->ncombinations; i++)", "\n", "\t {\n", sep=""))
  15. cat(paste("\t ", codelet, ".model->combinations[i] = (unsigned *) malloc(", codelet, ".model->nparameters*sizeof(unsigned))", ";\n", "\t }\n", "\t }\n\n", sep=""))
  16. # Computing combinations
  17. df <- data.frame(attr(reg$terms, "factors"))
  18. df <- df/2
  19. df$Params <- row.names(df)
  20. df <-df[c(2:nrow(df)),]
  21. i=1
  22. options(warn=-1)
  23. for(i in (1:nrow(df)))
  24. {
  25. name <- df[i,]$Params
  26. if (grepl("I\\(*", name))
  27. {
  28. exp <- as.numeric(gsub("(.*?)\\^(.*?)\\)", "\\2", name))
  29. df[i,] <- as.numeric(df[i,]) * exp
  30. df[i,]$Params <- as.character(gsub("I\\((.*?)\\^(.*?)\\)", "\\1", name))
  31. }
  32. }
  33. df <- aggregate(. ~ Params, transform(df, Params), sum)
  34. options(warn=0)
  35. i=1
  36. j=1
  37. for(j in (2:length(df)))
  38. {
  39. for(i in (1:nrow(df)))
  40. {
  41. cat(paste("\t ", codelet, ".model->combinations[", j-2, "][", i-1, "] = ", as.numeric(df[i,j]), ";\n", sep=""))
  42. }
  43. }
  44. cat(paste("/* ############################################ */", "\n"))
  45. }
  46. df<-read.csv(input_trace, header=TRUE)
  47. ```
  48. # Multiple Linear Regression Model Example
  49. ## Introduction
  50. This document demonstrates the type of the analysis needed to compute
  51. the multiple linear regression model of the task. It relies on the
  52. input data benchmarked by the StarPU (or any other tool, but following
  53. the same format). The input data used in this example is generated by
  54. the task "mlr_init", from the "examples/basic_examples/mlr.c".
  55. This document can be used as an template for the analysis of any other
  56. task.
  57. ### How to compile
  58. ./starpu_mlr_analysis .starpu/sampling/codelets/tmp/mlr_init.out
  59. ### Software dependencies
  60. In order to run the analysis you need to have R installed:
  61. sudo apt-get install r-base
  62. In order to compile this document, you need *knitr* (although you can
  63. perfectly only use the R code from this document without knitr). If
  64. you decided that you want to generate this document, then start R
  65. (e.g., from terminal) and install knitr package:
  66. R> install.packages("knitr")
  67. No additional R packages are needed.
  68. ## First glimpse at the data
  69. First, we show the relations between all parameters in a single plot.
  70. ```{r InitPlot}
  71. plot(df)
  72. ```
  73. For this example, all three parameters M, N, K have some influence,
  74. but their relation is not easy to understand.
  75. In general, this type of plots can typically show if there are
  76. outliers. It can also show if there is a group of parameters which are
  77. mutually perfectly correlated, in which case only a one parameter from
  78. the group should be kept for the further analysis. Additionally, plot
  79. can show the parameters that have a constant value, and since these
  80. cannot have an influence on the model, they should also be ignored.
  81. However, making conclusions based solely on the visual analysis can be
  82. treacherous and it is better to rely on the statistical tools. The
  83. multiple linear regression methods used in the following sections will
  84. also be able to detect and ignore these irrelevant
  85. parameters. Therefore, this initial visual look should only be used to
  86. get a basic idea about the model, but all the parameters should be
  87. kept for now.
  88. ## Initial model
  89. At this point, an initial model is computed, using all the parameters,
  90. but not taking into account their exponents or the relations between
  91. them.
  92. ```{r Model1}
  93. model1 <- lm(data=df, Duration ~ M+N+K)
  94. summary(model1)
  95. ```
  96. For each parameter and the constant in the first column, an estimation
  97. of the corresponding coefficient is provided along with the 95%
  98. confidence interval. If there are any parameters with NA value, which
  99. suggests that the parameters are correlated to another parameter or
  100. that their value is constant, these parameters should not be used in
  101. the following model computations. The stars in the last column
  102. indicate the significance of each parameter. However, having maximum
  103. three stars for each parameter does not necessarily mean that the
  104. model is perfect and we should always inspect the adjusted R^2 value
  105. (the closer it is to 1, the better the model is). To the users that
  106. are not common to the multiple linear regression analysis and R tools,
  107. we suggest to the R documentation. Some explanations are also provided
  108. in the following article https://hal.inria.fr/hal-01180272.
  109. In this example, all parameters M, N, K are very important. However,
  110. it is not clear if there are some relations between them or if some of
  111. these parameters should be used with an exponent. Moreover, adjusted
  112. R^2 value is not extremely high and we hope we can get a better
  113. one. Thus, we proceed to the more advanced analysis.
  114. ## Refining the model
  115. Now, we can seek for the relations between the parameters. Note that
  116. trying all the possible combinations for the cases with a huge number
  117. of parameters can be prohibitively long. Thus, it may be better to first
  118. get rid of the parameters which seem to have very small influence
  119. (typically the ones with no stars from the table in the previous
  120. section).
  121. ```{r Model2}
  122. model2 <- lm(data=df, Duration ~ M*N*K)
  123. summary(model2)
  124. ```
  125. This model is more accurate, as the R^2 value increased. We can also
  126. try some of these parameters with the exponents.
  127. ```{r Model3}
  128. model3 <- lm(data=df, Duration ~ I(M^2)+I(M^3)+I(N^2)+I(N^3)+I(K^2)+I(K^3))
  129. summary(model3)
  130. ```
  131. It seems like some parameters are important. Now we combine these and
  132. try to find the optimal combination (here we go directly to the final
  133. solution, although this process typically takes several iterations of
  134. trying different combinations).
  135. ```{r Model4}
  136. model4 <- lm(data=df, Duration ~ I(M^2):N+I(N^3):K)
  137. summary(model4)
  138. ```
  139. This seems to be the most accurate model, with a high R^2 value. We
  140. can proceed to its validation.
  141. ## Validation
  142. Once the model has been computed, we should validate it. Apart from
  143. the low adjusted R^2 value, the model weakness can also be observed
  144. even better when inspecting the residuals. The results on two
  145. following plots (and thus the accuracy of the model) will greatly
  146. depend on the measurements variability and the design of experiments.
  147. ```{r Validation}
  148. par(mfrow=c(1,2))
  149. plot(model4, which=c(1:2))
  150. ```
  151. Generally speaking, if there are some structures on the left plot,
  152. this can indicate that there are certain phenomena not explained by
  153. the model. Many points on the same horizontal line represent
  154. repetitive occurrences of the task with the same parameter values,
  155. which is typical for a single experiment run with a homogeneous
  156. data. The fact that there is some variability is common, as executing
  157. exactly the same code on a real machine will always have slightly
  158. different duration. However, having a huge variability means that the
  159. benchmarks were very noisy, thus deriving an accurate models from them
  160. will be hard.
  161. Plot on the right may show that the residuals do not follow the normal
  162. distribution. Therefore, such model in overall would have a limited
  163. predictive power.
  164. If we are not satisfied with the accuracy of the observed models, we
  165. should go back to the previous section and try to find a better
  166. one. In some cases, the benchmarked data is just be too noisy or the
  167. choice of the parameters is not appropriate, and thus the experiments
  168. should be redesigned and rerun.
  169. When we are finally satisfied with the model accuracy, we should
  170. modify our task code, so that StarPU knows which parameters
  171. combinations are used in the model.
  172. ## Generating C code
  173. Depending on the way the task codelet is programmed, this section may
  174. be somehow useful. This is a simple helper to generate C code for the
  175. parameters combinations and it should be copied to the task
  176. description in the application. The function generating the code is
  177. not so robust, so make sure that the generated code correctly
  178. corresponds to computed model (for example parameters are considered
  179. in the alphabetical order).
  180. ```{r Code}
  181. print_codelet(model4, "mlr_cl")
  182. ```
  183. ## Conclusion
  184. We have computed the model for our benchmarked data using multiple
  185. linear regression. After encoding this model into the task code,
  186. StarPU will be able to automatically compute the coefficients and use
  187. the model to predict task duration.