mlr.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2015 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. int 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. void cl_perf_func(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. double m,n,k;
  46. starpu_codelet_unpack_args(cl_arg,
  47. &m,
  48. &n,
  49. &k);
  50. for(int i=0; i < (int) (m*m*n); i++)
  51. sum+=i;
  52. for(int i=0; i < (int) (n*n*n*k); i++)
  53. sum+=i;
  54. }
  55. int main(int argc, char **argv)
  56. {
  57. struct starpu_codelet cl;
  58. starpu_init(NULL);
  59. /* Allocating and naming codelet, similar to any other StarPU program */
  60. memset(&cl, 0, sizeof(cl));
  61. cl.cpu_funcs[0] = cpu_func;
  62. cl.cpu_funcs_name[0] = "mlr_codelet";
  63. cl.nbuffers = 0;
  64. cl.name="test_mlr";
  65. /* ############################################ */
  66. /* Start of the part specific to multiple linear regression perfmodels */
  67. /* Defining perfmodel, number of parameters and their names */
  68. struct starpu_perfmodel *model = calloc(1,sizeof(struct starpu_perfmodel));
  69. cl.model = model;
  70. cl.model->type = STARPU_MULTIPLE_REGRESSION_BASED;
  71. cl.model->symbol = cl.name;
  72. cl.model->parameters = cl_perf_func;
  73. cl.model->nparameters = 3;
  74. cl.model->parameters_names = (const char **) calloc(1, cl.model->nparameters*sizeof(char *));
  75. cl.model->parameters_names[0] = "M";
  76. cl.model->parameters_names[1] = "N";
  77. cl.model->parameters_names[2] = "K";
  78. /* Defining the equation for modeling duration of the task */
  79. /* Refer to the explanation and equation on the top of this file
  80. to get more detailed explanation */
  81. cl.model->ncombinations = 2;
  82. cl.model->combinations = (unsigned **) malloc(cl.model->ncombinations*sizeof(unsigned *));
  83. if (cl.model->combinations)
  84. {
  85. for (unsigned i = 0; i < cl.model->ncombinations; i++)
  86. {
  87. cl.model->combinations[i] = (unsigned *) malloc(cl.model->nparameters*sizeof(unsigned));
  88. }
  89. }
  90. cl.model->combinations[0][0] = 2;
  91. cl.model->combinations[0][1] = 1;
  92. cl.model->combinations[0][2] = 0;
  93. cl.model->combinations[1][0] = 0;
  94. cl.model->combinations[1][1] = 3;
  95. cl.model->combinations[1][2] = 1;
  96. /* End of the part specific to multiple linear regression perfmodels */
  97. /* ############################################ */
  98. sum=0;
  99. double m,n,k;
  100. /* Giving pseudo-random values to the M,N,K parameters and inserting tasks */
  101. for(int i=0; i < 42; i++)
  102. {
  103. m = (double) ((rand() % 10)+1);
  104. n = (double) ((rand() % 10)+1);
  105. k = (double) ((rand() % 10)+1);
  106. for(int j=0; j < 42; j++)
  107. starpu_insert_task(&cl,
  108. STARPU_VALUE, &m, sizeof(double),
  109. STARPU_VALUE, &n, sizeof(double),
  110. STARPU_VALUE, &k, sizeof(double),
  111. 0);
  112. }
  113. starpu_shutdown();
  114. return 0;
  115. }