starpu_mlr_analysis.Rmd 9.5 KB

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