mlr.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2016-2020 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. /*
  17. * This examples demonstrates how to use multiple linear regression
  18. models.
  19. First, there is mlr_codelet__init codelet for which we know the
  20. parameters, but not the their exponents and relations. This tasks
  21. should be benchmarked and analyzed to find the model, using
  22. "tools/starpu_mlr_analysis" script as a template.
  23. For the second (codelet cl_model_final), it is assumed that the
  24. analysis has already been performed and that the duration of the
  25. codelet mlr_codelet_final will be computed using the following
  26. equation:
  27. T = a + b * (M^2*N) + c * (N^3*K)
  28. where M, N, K are the parameters of the task, exponents are coming
  29. from model->combinations[..][..] and finally a, b, c are
  30. coefficients which mostly depend on the machine speed.
  31. These coefficients are going to be automatically computed using
  32. least square method.
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <stdint.h>
  37. #include <starpu.h>
  38. static long sum;
  39. /* 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" */
  40. static void cl_params(struct starpu_task *task, double *parameters)
  41. {
  42. int m, n, k;
  43. int* vector_mn;
  44. vector_mn = (int*)STARPU_VECTOR_GET_PTR(task->interfaces[0]);
  45. m = vector_mn[0];
  46. n = vector_mn[1];
  47. starpu_codelet_unpack_args(task->cl_arg, &k);
  48. parameters[0] = m;
  49. parameters[1] = n;
  50. parameters[2] = k;
  51. }
  52. /* Function of the task that will be executed. In this case running dummy cycles, just to make sure task duration is significant */
  53. void cpu_func(void *buffers[], void *cl_arg)
  54. {
  55. long i;
  56. int m,n,k;
  57. int* vector_mn;
  58. vector_mn = (int*)STARPU_VECTOR_GET_PTR(buffers[0]);
  59. m = vector_mn[0];
  60. n = vector_mn[1];
  61. starpu_codelet_unpack_args(cl_arg, &k);
  62. for(i=0; i < (long) (m*m*n); i++)
  63. sum+=i;
  64. for(i=0; i < (long) (n*n*n*k); i++)
  65. sum+=i;
  66. }
  67. /* ############################################ */
  68. /* Start of the part specific to multiple linear regression perfmodels */
  69. /* Defining perfmodel, number of parameters and their names Initially
  70. application developer only knows these parameters. The execution of
  71. this codelet will generate traces that can be analyzed using
  72. "tools/starpu_mlr_analysis" as a template to obtain the parameters
  73. combinations and exponents.
  74. */
  75. static const char * parameters_names[] = { "M", "N", "K", };
  76. static struct starpu_perfmodel cl_model_init =
  77. {
  78. .type = STARPU_MULTIPLE_REGRESSION_BASED,
  79. .symbol = "mlr_init",
  80. .parameters = cl_params,
  81. .nparameters = 3,
  82. .parameters_names = parameters_names,
  83. };
  84. /* Defining the equation for modeling duration of the task. The
  85. parameters combinations and exponents are computed externally
  86. offline, for example using "tools/starpu_mlr_analysis" tool as a
  87. template.
  88. */
  89. /* M^2 * N^1 * K^0 */
  90. static unsigned combi1 [3] = { 2, 1, 0 };
  91. /* M^0 * N^3 * K^1 */
  92. static unsigned combi2 [3] = { 0, 3, 1 };
  93. static unsigned *combinations[] = { combi1, combi2 };
  94. static struct starpu_perfmodel cl_model_final =
  95. {
  96. .type = STARPU_MULTIPLE_REGRESSION_BASED,
  97. .symbol = "mlr_final",
  98. .parameters = cl_params,
  99. .nparameters = 3,
  100. .parameters_names = parameters_names,
  101. .ncombinations = 2,
  102. .combinations = combinations,
  103. };
  104. /* End of the part specific to multiple linear regression perfmodels */
  105. /* ############################################ */
  106. static struct starpu_codelet cl_init =
  107. {
  108. .cpu_funcs = { cpu_func },
  109. .cpu_funcs_name = { "cpu_func" },
  110. .nbuffers = 1,
  111. .modes = {STARPU_R},
  112. .model = &cl_model_init,
  113. };
  114. static struct starpu_codelet cl_final =
  115. {
  116. .cpu_funcs = { cpu_func },
  117. .cpu_funcs_name = { "cpu_func" },
  118. .nbuffers = 1,
  119. .modes = {STARPU_R},
  120. .model = &cl_model_final,
  121. };
  122. int main(void)
  123. {
  124. /* Initialization */
  125. unsigned i;
  126. int ret;
  127. ret = starpu_init(NULL);
  128. if (ret == -ENODEV)
  129. return 77;
  130. sum=0;
  131. int* vector_mn = calloc(2, sizeof(int));
  132. starpu_data_handle_t vector_mn_handle;
  133. starpu_vector_data_register(&vector_mn_handle,
  134. STARPU_MAIN_RAM,
  135. (uintptr_t)vector_mn, 2,
  136. sizeof(int));
  137. /* Giving pseudo-random values to the M,N,K parameters and inserting tasks */
  138. for (i = 0; i < 42; i++)
  139. {
  140. int j;
  141. int m,n,k;
  142. m = (int) ((rand() % 10)+1);
  143. n = (int) ((rand() % 10)+1);
  144. k = (int) ((rand() % 10)+1);
  145. /* To illustrate the usage, M and N are stored in a data handle */
  146. starpu_data_acquire(vector_mn_handle, STARPU_W);
  147. vector_mn[0] = m;
  148. vector_mn[1] = n;
  149. starpu_data_release(vector_mn_handle);
  150. for (j = 0; j < 42; j++)
  151. {
  152. starpu_insert_task(&cl_init,
  153. STARPU_R, vector_mn_handle,
  154. STARPU_VALUE, &k, sizeof(int),
  155. 0);
  156. starpu_insert_task(&cl_final,
  157. STARPU_R, vector_mn_handle,
  158. STARPU_VALUE, &k, sizeof(int),
  159. 0);
  160. }
  161. }
  162. starpu_data_unregister(vector_mn_handle);
  163. free(vector_mn);
  164. starpu_shutdown();
  165. return 0;
  166. }