mlr.c 5.4 KB

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