starpu_mlr_analysis.Rmd 9.5 KB

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