valid_model.c 4.6 KB

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