valid_model.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2013 Thibaut Lambert
  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. #include <starpu.h>
  18. #include <core/perfmodel/perfmodel.h>
  19. #include <unistd.h>
  20. #include "../helper.h"
  21. /*
  22. * Check that measurements get recorded in the performance model
  23. */
  24. void func(void *descr[], void *arg)
  25. {
  26. (void)descr;
  27. (void)arg;
  28. usleep(1000);
  29. }
  30. static struct starpu_perfmodel rb_model =
  31. {
  32. .type = STARPU_REGRESSION_BASED,
  33. .symbol = "valid_model_regression_based"
  34. };
  35. static struct starpu_perfmodel nlrb_model =
  36. {
  37. .type = STARPU_NL_REGRESSION_BASED,
  38. .symbol = "valid_model_non_linear_regression_based"
  39. };
  40. #if 0
  41. static struct starpu_perfmodel hb_model =
  42. {
  43. .type = STARPU_HISTORY_BASED,
  44. .symbol = "valid_model_history_based"
  45. };
  46. #endif
  47. static struct starpu_codelet mycodelet =
  48. {
  49. .cuda_funcs = {func},
  50. .opencl_funcs = {func},
  51. .cpu_funcs = {func},
  52. .cpu_funcs_name = {"func"},
  53. .nbuffers = 1,
  54. .modes = {STARPU_W}
  55. };
  56. static int submit(struct starpu_codelet *codelet, struct starpu_perfmodel *model)
  57. {
  58. int nloops = 123;
  59. int loop;
  60. starpu_data_handle_t handle;
  61. struct starpu_perfmodel lmodel;
  62. int ret;
  63. int old_nsamples, new_nsamples;
  64. struct starpu_conf conf;
  65. starpu_conf_init(&conf);
  66. conf.sched_policy_name = "eager";
  67. conf.calibrate = 1;
  68. ret = starpu_init(&conf);
  69. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  70. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  71. codelet->model = model;
  72. old_nsamples = 0;
  73. memset(&lmodel, 0, sizeof(struct starpu_perfmodel));
  74. lmodel.type = model->type;
  75. ret = starpu_perfmodel_load_symbol(codelet->model->symbol, &lmodel);
  76. if (ret != 1)
  77. {
  78. int i, impl;
  79. for(i = 0; i < lmodel.state->ncombs; i++)
  80. {
  81. int comb = lmodel.state->combs[i];
  82. for(impl = 0; impl < lmodel.state->nimpls[comb]; impl++)
  83. old_nsamples += lmodel.state->per_arch[comb][impl].regression.nsample;
  84. }
  85. }
  86. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, 100, sizeof(int));
  87. for (loop = 0; loop < nloops; loop++)
  88. {
  89. ret = starpu_task_insert(codelet, STARPU_W, handle, 0);
  90. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  92. }
  93. starpu_data_unregister(handle);
  94. starpu_perfmodel_unload_model(&lmodel);
  95. starpu_shutdown(); // To force dumping perf models on disk
  96. // We need to call starpu_init again to initialise values used by perfmodels
  97. ret = starpu_init(NULL);
  98. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  99. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  100. char path[256];
  101. starpu_perfmodel_get_model_path(codelet->model->symbol, path, 256);
  102. FPRINTF(stderr, "Perfmodel File <%s>\n", path);
  103. ret = starpu_perfmodel_load_file(path, &lmodel);
  104. if (ret == 1)
  105. {
  106. FPRINTF(stderr, "The performance model for the symbol <%s> could not be loaded\n", codelet->model->symbol);
  107. starpu_shutdown();
  108. return 1;
  109. }
  110. else
  111. {
  112. int i;
  113. new_nsamples = 0;
  114. for(i = 0; i < lmodel.state->ncombs; i++)
  115. {
  116. int comb = lmodel.state->combs[i];
  117. int impl;
  118. for(impl = 0; impl < lmodel.state->nimpls[comb]; impl++)
  119. new_nsamples += lmodel.state->per_arch[comb][impl].regression.nsample;
  120. }
  121. }
  122. ret = starpu_perfmodel_unload_model(&lmodel);
  123. starpu_shutdown();
  124. if (ret == 1)
  125. {
  126. FPRINTF(stderr, "The performance model for the symbol <%s> could not be UNloaded\n", codelet->model->symbol);
  127. return 1;
  128. }
  129. if (old_nsamples + nloops == new_nsamples)
  130. {
  131. FPRINTF(stderr, "Sampling for <%s> OK %d + %d == %d\n", codelet->model->symbol, old_nsamples, nloops, new_nsamples);
  132. return EXIT_SUCCESS;
  133. }
  134. else
  135. {
  136. FPRINTF(stderr, "Sampling for <%s> failed %d + %d != %d\n", codelet->model->symbol, old_nsamples, nloops, new_nsamples);
  137. return EXIT_FAILURE;
  138. }
  139. }
  140. int main(void)
  141. {
  142. int ret;
  143. /* Use a linear regression model */
  144. ret = submit(&mycodelet, &rb_model);
  145. if (ret) return ret;
  146. /* Use a non-linear regression model */
  147. ret = submit(&mycodelet, &nlrb_model);
  148. if (ret) return ret;
  149. #ifdef STARPU_DEVEL
  150. # warning history based model cannot be validated with regression.nsample
  151. #endif
  152. #if 0
  153. /* Use a history model */
  154. ret = submit(&mycodelet, &hb_model);
  155. if (ret) return ret;
  156. #endif
  157. return EXIT_SUCCESS;
  158. }