mlr.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 models.
  19. The duration of the task test_mlr will
  20. be computed using the following equation:
  21. T = a + b * (M^2*N) + c * (N^3*K)
  22. where M, N, K are the parameters of the task,
  23. exponents are coming from cl.model->combinations[..][..]
  24. and finally a, b, c are coefficients
  25. which mostly depend on the machine speed.
  26. These coefficients are going to be automatically computed
  27. using least square method.
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <stdint.h>
  32. #include <starpu.h>
  33. static long sum;
  34. /* 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" */
  35. static void cl_params(struct starpu_task *task, double *parameters)
  36. {
  37. starpu_codelet_unpack_args(task->cl_arg,
  38. &parameters[0],
  39. &parameters[1],
  40. &parameters[2]);
  41. }
  42. /* Function of the task that will be executed. In this case running dummy cycles, just to make sure task duration is significant */
  43. void cpu_func(void *buffers[], void *cl_arg)
  44. {
  45. long i;
  46. double m,n,k;
  47. starpu_codelet_unpack_args(cl_arg,
  48. &m,
  49. &n,
  50. &k);
  51. for(i=0; i < (long) (m*m*n); i++)
  52. sum+=i;
  53. for(i=0; i < (long) (n*n*n*k); i++)
  54. sum+=i;
  55. }
  56. /* ############################################ */
  57. /* Start of the part specific to multiple linear regression perfmodels */
  58. /* Defining perfmodel, number of parameters and their names */
  59. /* Defining the equation for modeling duration of the task */
  60. /* Refer to the explanation and equation on the top of this file
  61. to get more detailed explanation, here we have M^2*N and N^3*K */
  62. static const char * parameters_names[] = { "M", "N", "K", };
  63. static unsigned combi1 [3] = { 2, 1, 0 };
  64. static unsigned combi2 [3] = { 0, 3, 1 };
  65. static unsigned *combinations[] = { combi1, combi2 };
  66. static struct starpu_perfmodel cl_model = {
  67. .type = STARPU_MULTIPLE_REGRESSION_BASED,
  68. .symbol = "test_mlr",
  69. .parameters = cl_params,
  70. .nparameters = 3,
  71. .parameters_names = parameters_names,
  72. .ncombinations = 2,
  73. .combinations = combinations,
  74. };
  75. static struct starpu_codelet cl = {
  76. .cpu_funcs = { cpu_func },
  77. .cpu_funcs_name = { "mlr_codelet" },
  78. .nbuffers = 0,
  79. .model = &cl_model,
  80. };
  81. /* End of the part specific to multiple linear regression perfmodels */
  82. /* ############################################ */
  83. int main(int argc, char **argv)
  84. {
  85. /* Initialization */
  86. unsigned i,j;
  87. int ret;
  88. ret = starpu_init(NULL);
  89. if (ret == -ENODEV)
  90. return 77;
  91. sum=0;
  92. double m,n,k;
  93. /* Giving pseudo-random values to the M,N,K parameters and inserting tasks */
  94. for(i=0; i < 42; i++)
  95. {
  96. m = (double) ((rand() % 10)+1);
  97. n = (double) ((rand() % 10)+1);
  98. k = (double) ((rand() % 10)+1);
  99. for(j=0; j < 42; j++)
  100. starpu_insert_task(&cl,
  101. STARPU_VALUE, &m, sizeof(double),
  102. STARPU_VALUE, &n, sizeof(double),
  103. STARPU_VALUE, &k, sizeof(double),
  104. 0);
  105. }
  106. starpu_shutdown();
  107. return 0;
  108. }