mlr.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2015-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /*
  18. * This examples demonstrates how to use multiple linear regression
  19. models.
  20. First, there is cl_model_init codelet for which we know the
  21. parameters, but not the their exponents and relations. This tasks
  22. should be benchmarked and analyzed to find the model, using
  23. "tools/starpu_mlr_analysis" script as a template. Before the model
  24. is defined by the application developer, the default model is
  25. automatically computed. This default models is a simple constant
  26. (thus making STARPU_MULTIPLE_REGRESSION_BASED model equal to the
  27. history based model).
  28. For the second (codelet cl_model_final), it is assumed that the
  29. analysis has already been performend and that he duration of the
  30. task test_mlr will be computed using the following equation:
  31. T = a + b * (M^2*N) + c * (N^3*K)
  32. where M, N, K are the parameters of the task, exponents are coming
  33. from model->combinations[..][..] and finally a, b, c are
  34. coefficients which mostly depend on the machine speed.
  35. These coefficients are going to be automatically computed using
  36. least square method.
  37. */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <stdint.h>
  41. #include <starpu.h>
  42. static long sum;
  43. /* Performance function of the task, which is in this case very simple, as the parameter values just need to be written in the array "parameters" */
  44. static void cl_params(struct starpu_task *task, double *parameters)
  45. {
  46. starpu_codelet_unpack_args(task->cl_arg,
  47. &parameters[0],
  48. &parameters[1],
  49. &parameters[2]);
  50. }
  51. /* Function of the task that will be executed. In this case running dummy cycles, just to make sure task duration is significant */
  52. void cpu_func(void *buffers[], void *cl_arg)
  53. {
  54. long i;
  55. double m,n,k;
  56. starpu_codelet_unpack_args(cl_arg,
  57. &m,
  58. &n,
  59. &k);
  60. for(i=0; i < (long) (m*m*n); i++)
  61. sum+=i;
  62. for(i=0; i < (long) (n*n*n*k); i++)
  63. sum+=i;
  64. }
  65. /* ############################################ */
  66. /* Start of the part specific to multiple linear regression perfmodels */
  67. /* Defining perfmodel, number of parameters and their names Initially
  68. application developer only knows these parameters. The execution of
  69. this codelet will generate traces that can be analyzed using
  70. "tools/starpu_mlr_analysis" as a template to obtain the parameters
  71. combinations and exponents.
  72. */
  73. static const char * parameters_names[] = { "M", "N", "K", };
  74. static struct starpu_perfmodel cl_model_init = {
  75. .type = STARPU_MULTIPLE_REGRESSION_BASED,
  76. .symbol = "mlr_init",
  77. .parameters = cl_params,
  78. .nparameters = 3,
  79. .parameters_names = parameters_names,
  80. };
  81. /* Defining the equation for modeling duration of the task. The
  82. parameters combinations and exponents are computed externally
  83. offline, for example using "tools/starpu_mlr_analysis" tool as a
  84. template.
  85. */
  86. static unsigned combi1 [3] = { 2, 1, 0 };
  87. static unsigned combi2 [3] = { 0, 3, 1 };
  88. static unsigned *combinations[] = { combi1, combi2 };
  89. static struct starpu_perfmodel cl_model_final = {
  90. .type = STARPU_MULTIPLE_REGRESSION_BASED,
  91. .symbol = "mlr_final",
  92. .parameters = cl_params,
  93. .nparameters = 3,
  94. .parameters_names = parameters_names,
  95. .ncombinations = 2,
  96. .combinations = combinations,
  97. };
  98. /* End of the part specific to multiple linear regression perfmodels */
  99. /* ############################################ */
  100. static struct starpu_codelet cl_init = {
  101. .cpu_funcs = { cpu_func },
  102. .cpu_funcs_name = { "mlr_codelet_init" },
  103. .nbuffers = 0,
  104. .model = &cl_model_init,
  105. };
  106. static struct starpu_codelet cl_final = {
  107. .cpu_funcs = { cpu_func },
  108. .cpu_funcs_name = { "mlr_codelet_final" },
  109. .nbuffers = 0,
  110. .model = &cl_model_final,
  111. };
  112. int main(int argc, char **argv)
  113. {
  114. /* Initialization */
  115. unsigned i,j;
  116. int ret;
  117. ret = starpu_init(NULL);
  118. if (ret == -ENODEV)
  119. return 77;
  120. sum=0;
  121. double m,n,k;
  122. /* Giving pseudo-random values to the M,N,K parameters and inserting tasks */
  123. for(i=0; i < 42; i++)
  124. {
  125. m = (double) ((rand() % 10)+1);
  126. n = (double) ((rand() % 10)+1);
  127. k = (double) ((rand() % 10)+1);
  128. for(j=0; j < 42; j++)
  129. {
  130. starpu_insert_task(&cl_init,
  131. STARPU_VALUE, &m, sizeof(double),
  132. STARPU_VALUE, &n, sizeof(double),
  133. STARPU_VALUE, &k, sizeof(double),
  134. 0);
  135. starpu_insert_task(&cl_final,
  136. STARPU_VALUE, &m, sizeof(double),
  137. STARPU_VALUE, &n, sizeof(double),
  138. STARPU_VALUE, &k, sizeof(double),
  139. 0);
  140. }
  141. }
  142. starpu_shutdown();
  143. return 0;
  144. }