valid_model.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2017 CNRS
  4. * Copyright (C) 2017 Inria
  5. * Copyright (C) 2014-2016 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include <core/perfmodel/perfmodel.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. }
  29. static struct starpu_perfmodel rb_model =
  30. {
  31. .type = STARPU_REGRESSION_BASED,
  32. .symbol = "valid_model_regression_based"
  33. };
  34. static struct starpu_perfmodel nlrb_model =
  35. {
  36. .type = STARPU_NL_REGRESSION_BASED,
  37. .symbol = "valid_model_non_linear_regression_based"
  38. };
  39. #if 0
  40. static struct starpu_perfmodel hb_model =
  41. {
  42. .type = STARPU_HISTORY_BASED,
  43. .symbol = "valid_model_history_based"
  44. };
  45. #endif
  46. static struct starpu_codelet mycodelet =
  47. {
  48. .cuda_funcs = {func},
  49. .opencl_funcs = {func},
  50. .cpu_funcs = {func},
  51. .cpu_funcs_name = {"func"},
  52. .nbuffers = 1,
  53. .modes = {STARPU_W}
  54. };
  55. static int submit(struct starpu_codelet *codelet, struct starpu_perfmodel *model)
  56. {
  57. int nloops = 123;
  58. int loop;
  59. starpu_data_handle_t handle;
  60. struct starpu_perfmodel lmodel;
  61. int ret;
  62. int old_nsamples, new_nsamples;
  63. struct starpu_conf conf;
  64. starpu_conf_init(&conf);
  65. conf.sched_policy_name = "eager";
  66. conf.calibrate = 1;
  67. ret = starpu_init(&conf);
  68. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  69. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  70. codelet->model = model;
  71. old_nsamples = 0;
  72. memset(&lmodel, 0, sizeof(struct starpu_perfmodel));
  73. lmodel.type = model->type;
  74. ret = starpu_perfmodel_load_symbol(codelet->model->symbol, &lmodel);
  75. if (ret != 1)
  76. {
  77. int i, impl;
  78. for(i = 0; i < lmodel.state->ncombs; i++)
  79. {
  80. int comb = lmodel.state->combs[i];
  81. for(impl = 0; impl < lmodel.state->nimpls[comb]; impl++)
  82. old_nsamples += lmodel.state->per_arch[comb][impl].regression.nsample;
  83. }
  84. }
  85. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, 100, sizeof(int));
  86. for (loop = 0; loop < nloops; loop++)
  87. {
  88. ret = starpu_task_insert(codelet, STARPU_W, handle, 0);
  89. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  90. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  91. }
  92. starpu_data_unregister(handle);
  93. starpu_perfmodel_unload_model(&lmodel);
  94. starpu_shutdown(); // To force dumping perf models on disk
  95. // We need to call starpu_init again to initialise values used by perfmodels
  96. ret = starpu_init(NULL);
  97. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  98. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  99. char path[256];
  100. starpu_perfmodel_get_model_path(codelet->model->symbol, path, 256);
  101. FPRINTF(stderr, "Perfmodel File <%s>\n", path);
  102. ret = starpu_perfmodel_load_file(path, &lmodel);
  103. if (ret == 1)
  104. {
  105. FPRINTF(stderr, "The performance model for the symbol <%s> could not be loaded\n", codelet->model->symbol);
  106. starpu_shutdown();
  107. return 1;
  108. }
  109. else
  110. {
  111. int i;
  112. new_nsamples = 0;
  113. for(i = 0; i < lmodel.state->ncombs; i++)
  114. {
  115. int comb = lmodel.state->combs[i];
  116. int impl;
  117. for(impl = 0; impl < lmodel.state->nimpls[comb]; impl++)
  118. new_nsamples += lmodel.state->per_arch[comb][impl].regression.nsample;
  119. }
  120. }
  121. ret = starpu_perfmodel_unload_model(&lmodel);
  122. starpu_shutdown();
  123. if (ret == 1)
  124. {
  125. FPRINTF(stderr, "The performance model for the symbol <%s> could not be UNloaded\n", codelet->model->symbol);
  126. return 1;
  127. }
  128. if (old_nsamples + nloops == new_nsamples)
  129. {
  130. FPRINTF(stderr, "Sampling for <%s> OK %d + %d == %d\n", codelet->model->symbol, old_nsamples, nloops, new_nsamples);
  131. return EXIT_SUCCESS;
  132. }
  133. else
  134. {
  135. FPRINTF(stderr, "Sampling for <%s> failed %d + %d != %d\n", codelet->model->symbol, old_nsamples, nloops, new_nsamples);
  136. return EXIT_FAILURE;
  137. }
  138. }
  139. int main(void)
  140. {
  141. int ret;
  142. /* Use a linear regression model */
  143. ret = submit(&mycodelet, &rb_model);
  144. if (ret) return ret;
  145. /* Use a non-linear regression model */
  146. ret = submit(&mycodelet, &nlrb_model);
  147. if (ret) return ret;
  148. #ifdef STARPU_DEVEL
  149. # warning history based model cannot be validated with regression.nsample
  150. #endif
  151. #if 0
  152. /* Use a history model */
  153. ret = submit(&mycodelet, &hb_model);
  154. if (ret) return ret;
  155. #endif
  156. return EXIT_SUCCESS;
  157. }