multiple_regression.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2011, 2015-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2016, 2017 CNRS
  5. * Copyright (C) 2016-2017 Inria
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /* Code for computing multiple linear regression */
  19. #include <core/perfmodel/multiple_regression.h>
  20. typedef long int integer;
  21. typedef double doublereal;
  22. #ifdef STARPU_MLR_MODEL
  23. int dgels_(char *trans, integer *m, integer *n, integer *nrhs, doublereal *a, integer *lda, doublereal *b, integer *ldb, doublereal *work, integer *lwork, integer *info);
  24. #endif //STARPU_MLR_MODEL
  25. static long count_file_lines(FILE *f)
  26. {
  27. int lines=0;
  28. while(!feof(f))
  29. {
  30. int ch = fgetc(f);
  31. if(ch == '\n')
  32. {
  33. lines++;
  34. }
  35. }
  36. rewind(f);
  37. return lines;
  38. }
  39. static void dump_multiple_regression_list(double *mpar, double *my, int start, unsigned nparameters, struct starpu_perfmodel_history_list *list_history)
  40. {
  41. struct starpu_perfmodel_history_list *ptr = list_history;
  42. int i = start;
  43. unsigned j;
  44. while (ptr)
  45. {
  46. my[i] = ptr->entry->duration;
  47. for(j=0; j<nparameters; j++)
  48. mpar[i*nparameters+j] = ptr->entry->parameters[j];
  49. ptr = ptr->next;
  50. i++;
  51. }
  52. }
  53. static void load_old_calibration(double *mx, double *my, unsigned nparameters, char *filepath)
  54. {
  55. char buffer[1024];
  56. char *line;
  57. int i=0;
  58. FILE *f=NULL;
  59. f = fopen(filepath, "a+");
  60. STARPU_ASSERT_MSG(f, "Could not save performance model into the file %s\n", filepath);
  61. line = fgets(buffer,sizeof(buffer),f);//skipping first line
  62. STARPU_ASSERT(line);
  63. while((line=fgets(buffer,sizeof(buffer),f))!=NULL)
  64. {
  65. char *record = strtok(line,",");
  66. my[i] = atof(record);
  67. record = strtok(NULL,",");
  68. int j=0;
  69. while(record != NULL)
  70. {
  71. mx[i*nparameters+j] = atof(record) ;
  72. ++j;
  73. record = strtok(NULL,",");
  74. }
  75. ++i ;
  76. }
  77. fclose(f);
  78. }
  79. static long find_long_list_size(struct starpu_perfmodel_history_list *list_history)
  80. {
  81. long cnt = 0;
  82. struct starpu_perfmodel_history_list *ptr = list_history;
  83. while (ptr)
  84. {
  85. cnt++;
  86. ptr = ptr->next;
  87. }
  88. return cnt;
  89. }
  90. #ifdef STARPU_MLR_MODEL
  91. int dgels_multiple_reg_coeff(double *mpar, double *my, long nn, unsigned ncoeff, unsigned nparameters, double *coeff, unsigned **combinations)
  92. {
  93. /* Arguments */
  94. /* ========= */
  95. /* TRANS (input) CHARACTER*1 */
  96. /* = 'N': the linear system involves A; */
  97. /* = 'T': the linear system involves A**T. */
  98. /* M (input) INTEGER */
  99. /* The number of rows of the matrix A. M >= 0. */
  100. /* N (input) INTEGER */
  101. /* The number of columns of the matrix A. N >= 0. */
  102. /* NRHS (input) INTEGER */
  103. /* The number of right hand sides, i.e., the number of */
  104. /* columns of the matrices B and X. NRHS >=0. */
  105. /* A (input/output) DOUBLE PRECISION array, dimension (LDA,N) */
  106. /* On entry, the M-by-N matrix A. */
  107. /* On exit, */
  108. /* if M >= N, A is overwritten by details of its QR */
  109. /* factorization as returned by DGEQRF; */
  110. /* if M < N, A is overwritten by details of its LQ */
  111. /* factorization as returned by DGELQF. */
  112. /* LDA (input) INTEGER */
  113. /* The leading dimension of the array A. LDA >= max(1,M). */
  114. /* B (input/output) DOUBLE PRECISION array, dimension (LDB,NRHS) */
  115. /* On entry, the matrix B of right hand side vectors, stored */
  116. /* columnwise; B is M-by-NRHS if TRANS = 'N', or N-by-NRHS */
  117. /* if TRANS = 'T'. */
  118. /* On exit, if INFO = 0, B is overwritten by the solution */
  119. /* vectors, stored columnwise: */
  120. /* if TRANS = 'N' and m >= n, rows 1 to n of B contain the least */
  121. /* squares solution vectors; the residual sum of squares for the */
  122. /* solution in each column is given by the sum of squares of */
  123. /* elements N+1 to M in that column; */
  124. /* if TRANS = 'N' and m < n, rows 1 to N of B contain the */
  125. /* minimum norm solution vectors; */
  126. /* if TRANS = 'T' and m >= n, rows 1 to M of B contain the */
  127. /* minimum norm solution vectors; */
  128. /* if TRANS = 'T' and m < n, rows 1 to M of B contain the */
  129. /* least squares solution vectors; the residual sum of squares */
  130. /* for the solution in each column is given by the sum of */
  131. /* squares of elements M+1 to N in that column. */
  132. /* LDB (input) INTEGER */
  133. /* The leading dimension of the array B. LDB >= MAX(1,M,N). */
  134. /* WORK (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */
  135. /* On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
  136. /* LWORK (input) INTEGER */
  137. /* The dimension of the array WORK. */
  138. /* LWORK >= max( 1, MN + max( MN, NRHS ) ). */
  139. /* For optimal performance, */
  140. /* LWORK >= max( 1, MN + max( MN, NRHS )*NB ). */
  141. /* where MN = min(M,N) and NB is the optimum block size. */
  142. /* If LWORK = -1, then a workspace query is assumed; the routine */
  143. /* only calculates the optimal size of the WORK array, returns */
  144. /* this value as the first entry of the WORK array, and no error */
  145. /* message related to LWORK is issued by XERBLA. */
  146. /* INFO (output) INTEGER */
  147. /* = 0: successful exit */
  148. /* < 0: if INFO = -i, the i-th argument had an illegal value */
  149. /* > 0: if INFO = i, the i-th diagonal element of the */
  150. /* triangular factor of A is zero, so that A does not have */
  151. /* full rank; the least squares solution could not be */
  152. /* computed. */
  153. /* ===================================================================== */
  154. if(nn <= ncoeff)
  155. {
  156. _STARPU_DISP("Warning: This function is not intended for the use when number of parameters is larger than the number of observations. Check how your matrices A and B were allocated or simply add more benchmarks.\n Multiple linear regression model will not be written into perfmodel file.\n");
  157. return 1;
  158. }
  159. char trans = 'N';
  160. integer m = nn;
  161. integer n = ncoeff;
  162. integer nrhs = 1; // number of columns of B and X (wich are vectors therefore nrhs=1)
  163. doublereal *X;
  164. _STARPU_MALLOC(X, sizeof(double)*n*m); // (/!\ modified at the output) contain the model and the different values of pararmters
  165. doublereal *Y;
  166. _STARPU_MALLOC(Y, sizeof(double)*m);
  167. double coefficient;
  168. int i;
  169. unsigned j, k;
  170. for (i=0; i < m; i++)
  171. {
  172. Y[i] = my[i];
  173. X[i] = 1.;
  174. for (j=1; j < n; j++)
  175. {
  176. coefficient = 1.;
  177. for(k=0; k < nparameters; k++)
  178. {
  179. coefficient *= pow(mpar[i*nparameters+k],combinations[j-1][k]);
  180. }
  181. X[i+j*m] = coefficient;
  182. }
  183. }
  184. integer lda = m;
  185. integer ldb = m; //
  186. integer info = 0;
  187. integer lwork = n*2;
  188. doublereal *work; // (output)
  189. _STARPU_MALLOC(work, sizeof(double)*lwork);
  190. /* // Running LAPACK dgels_ */
  191. dgels_(&trans, &m, &n, &nrhs, X, &lda, Y, &ldb, work, &lwork, &info);
  192. /* Check for the full rank */
  193. if( info != 0 )
  194. {
  195. _STARPU_DISP("Warning: Problems when executing dgels_ function. It seems like the diagonal element %ld is zero.\n Multiple linear regression model will not be written into perfmodel file.\n", info);
  196. return 1;
  197. }
  198. /* Copy computed coefficients */
  199. for(i=0; i<(int) ncoeff; i++)
  200. coeff[i] = Y[i];
  201. free(X);
  202. free(Y);
  203. free(work);
  204. return 0;
  205. }
  206. #endif //STARPU_MLR_MODEL
  207. /*
  208. Validating the accuracy of the coefficients.
  209. For the the validation is extremely basic, but it should be improved.
  210. */
  211. void starpu_validate_mlr(double *coeff, unsigned ncoeff, const char *codelet_name)
  212. {
  213. unsigned i;
  214. if (coeff[0] < 0)
  215. _STARPU_DISP("Warning: Constant computed by least square method is negative (%f). The model %s is likely to be inaccurate.\n", coeff[0], codelet_name);
  216. for(i=1; i<ncoeff; i++)
  217. if(fabs(coeff[i]) < 1E-10)
  218. _STARPU_DISP("Warning: Coefficient computed by least square method is extremelly small (%f). The model %s is likely to be inaccurate.\n", coeff[i], codelet_name);
  219. }
  220. int _starpu_multiple_regression(struct starpu_perfmodel_history_list *ptr, double *coeff, unsigned ncoeff, unsigned nparameters, const char **parameters_names, unsigned **combinations, const char *codelet_name)
  221. {
  222. long i;
  223. unsigned j;
  224. /* Computing number of rows */
  225. long n=find_long_list_size(ptr);
  226. /* Reading old calibrations if necessary */
  227. FILE *f=NULL;
  228. char directory[300];
  229. snprintf(directory, sizeof(directory), "%s/.starpu/sampling/codelets/tmp", _starpu_get_home_path());
  230. _starpu_mkpath_and_check(directory, S_IRWXU);
  231. char filepath[300];
  232. snprintf(filepath, sizeof(filepath), "%s/%s.out", directory,codelet_name);
  233. long old_lines=0;
  234. int calibrate = _starpu_get_calibrate_flag();
  235. if (calibrate==1)
  236. {
  237. f = fopen(filepath, "a+");
  238. STARPU_ASSERT_MSG(f, "Could not save performance model into the file %s\n", filepath);
  239. old_lines=count_file_lines(f);
  240. /* If the program is run for the first time the old_lines will be 0 */
  241. //STARPU_ASSERT(old_lines);
  242. n+=old_lines;
  243. fclose(f);
  244. }
  245. /* Allocating X and Y matrices */
  246. double *mpar;
  247. _STARPU_MALLOC(mpar, nparameters*n*sizeof(double));
  248. double *my;
  249. _STARPU_MALLOC(my, n*sizeof(double));
  250. /* Loading old calibration */
  251. if (calibrate==1)
  252. load_old_calibration(mpar, my, nparameters, filepath);
  253. /* Filling X and Y matrices with measured values */
  254. dump_multiple_regression_list(mpar, my, old_lines, nparameters, ptr);
  255. if (ncoeff!=0 && combinations!=NULL)
  256. {
  257. #ifdef STARPU_MLR_MODEL
  258. /* Computing coefficients using multiple linear regression */
  259. if(dgels_multiple_reg_coeff(mpar, my, n, ncoeff, nparameters, coeff, combinations))
  260. {
  261. free(mpar);
  262. free(my);
  263. return 1;
  264. }
  265. /* Basic validation of the model accuracy */
  266. starpu_validate_mlr(coeff, ncoeff, codelet_name);
  267. #else
  268. _STARPU_DISP("Warning: StarPU was compiled with '--disable-mlr' option or on Windows machine, thus multiple linear regression model will not be computed.\n");
  269. for(i=0; i<ncoeff; i++)
  270. coeff[i] = 0.;
  271. #endif //STARPU_MLR_MODEL
  272. }
  273. /* Preparing new output calibration file */
  274. if (calibrate==1 || calibrate==2)
  275. {
  276. if (old_lines > 0)
  277. {
  278. f = fopen(filepath, "a+");
  279. STARPU_ASSERT_MSG(f, "Could not save performance model into the file %s\n", filepath);
  280. }
  281. else
  282. {
  283. f = fopen(filepath, "w+");
  284. STARPU_ASSERT_MSG(f, "Could not save performance model into the file %s\n", filepath);
  285. fprintf(f, "Duration");
  286. for(j=0; j<nparameters; j++)
  287. {
  288. if(parameters_names != NULL && parameters_names[j]!= NULL)
  289. fprintf(f, ", %s", parameters_names[j]);
  290. else
  291. fprintf(f, ", P%u", j);
  292. }
  293. }
  294. }
  295. /* Writing parameters to calibration file */
  296. if (calibrate==1 || calibrate==2)
  297. {
  298. for(i=old_lines; i<n; i++)
  299. {
  300. fprintf(f, "\n%f", my[i]);
  301. for(j=0; j<nparameters; j++)
  302. fprintf(f, ", %f", mpar[i*nparameters+j]);
  303. }
  304. fclose(f);
  305. }
  306. /* Cleanup */
  307. free(mpar);
  308. free(my);
  309. return 0;
  310. }