mlr.c 4.3 KB

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