app_driven_test.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013,2016 Inria
  4. * Copyright (C) 2012-2013,2016-2017 CNRS
  5. * Copyright (C) 2013 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 <stdio.h>
  19. #include <stdint.h>
  20. #include <starpu.h>
  21. #include <sc_hypervisor.h>
  22. #define NTASKS 1000
  23. #define NINCR 10
  24. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  25. struct params
  26. {
  27. unsigned sched_ctx;
  28. int task_tag;
  29. };
  30. unsigned val[2];
  31. starpu_pthread_mutex_t mut[2];
  32. /* Every implementation of a codelet must have this prototype, the first * argument (buffers) describes the buffers/streams that are managed by the
  33. * DSM; the second arguments references read-only data that is passed as an
  34. * argument of the codelet (task->cl_arg). Here, "buffers" is unused as there
  35. * are no data input/output managed by the DSM (cl.nbuffers = 0) */
  36. void cpu_func(__attribute__((unused))void *buffers[], void *cl_arg)
  37. {
  38. struct params *params = (struct params *) cl_arg;
  39. int i;
  40. for(i = 0; i < NINCR; i++)
  41. {
  42. STARPU_PTHREAD_MUTEX_LOCK(&mut[params->sched_ctx - 1]);
  43. val[params->sched_ctx - 1]++;
  44. STARPU_PTHREAD_MUTEX_UNLOCK(&mut[params->sched_ctx - 1]);
  45. }
  46. if(params->task_tag != 0)
  47. FPRINTF(stdout, "Task with tag %d executed in ctx = %u %u counter_tests\n", params->task_tag, params->sched_ctx, val[params->sched_ctx - 1]);
  48. }
  49. struct starpu_codelet cl = {0};
  50. /* the management of the tags is done by the user */
  51. /* who will take care that the tags will be unique */
  52. int tag = 1;
  53. void* submit_tasks_thread(void *arg)
  54. {
  55. unsigned sched_ctx = *((unsigned*)arg);
  56. starpu_sched_ctx_set_context(&sched_ctx);
  57. struct starpu_task *task[NTASKS];
  58. struct params params[NTASKS];
  59. int i;
  60. for(i = 0; i < NTASKS; i++)
  61. {
  62. task[i] = starpu_task_create();
  63. // usleep(5000);
  64. cl.cpu_funcs[0] = cpu_func;
  65. cl.nbuffers = 0;
  66. task[i]->cl = &cl;
  67. if(sched_ctx == 1 && i == 5)
  68. {
  69. /* tag the tasks whose execution will start the resizing process */
  70. task[i]->hypervisor_tag = tag;
  71. /* indicate particular settings the context should have when the
  72. resizing will be done */
  73. sc_hypervisor_ctl(sched_ctx,
  74. SC_HYPERVISOR_TIME_TO_APPLY, tag,
  75. SC_HYPERVISOR_MIN_WORKERS, 2,
  76. SC_HYPERVISOR_MAX_WORKERS, 12,
  77. SC_HYPERVISOR_NULL);
  78. printf("require resize for sched_ctx %u at tag %d\n", sched_ctx, tag);
  79. /* specify that the contexts should be resized when the task having this
  80. particular tag will finish executing */
  81. sc_hypervisor_post_resize_request(sched_ctx, tag);
  82. }
  83. params[i].sched_ctx = sched_ctx;
  84. params[i].task_tag = task[i]->hypervisor_tag;
  85. task[i]->cl_arg = &params[i];
  86. task[i]->cl_arg_size = sizeof(params);
  87. int ret = starpu_task_submit(task[i]);
  88. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  89. }
  90. starpu_task_wait_for_all();
  91. return NULL;
  92. }
  93. int main()
  94. {
  95. int ret = starpu_init(NULL);
  96. if (ret == -ENODEV)
  97. return 77;
  98. int num_workers = starpu_worker_get_count();
  99. int nres1 = num_workers;
  100. int nres2 = num_workers;
  101. int ressources1[nres1];
  102. int ressources2[nres2];
  103. int i;
  104. for(i = 0; i < nres1; i++)
  105. ressources1[i] = i;
  106. for(i = 0; i < nres2; i++)
  107. ressources2[i] = i;
  108. /* create contexts */
  109. unsigned sched_ctx1 = starpu_sched_ctx_create(ressources1, nres1, "sched_ctx1", STARPU_SCHED_CTX_POLICY_NAME, "dmda", 0);
  110. unsigned sched_ctx2 = starpu_sched_ctx_create(ressources2, nres2, "sched_ctx2", STARPU_SCHED_CTX_POLICY_NAME, "dmda", 0);
  111. /* initialize the hypervisor */
  112. struct sc_hypervisor_policy policy = {};
  113. policy.custom = 0;
  114. /* indicate which strategy to use
  115. in this particular case we use app_driven which allows the user to resize
  116. the ctxs dynamically at particular moments of the execution of the application */
  117. policy.name = "app_driven";
  118. void *perf_counters = sc_hypervisor_init(&policy);
  119. /* let starpu know which performance counters should use
  120. to inform the hypervisor how the application and the resources are executing */
  121. starpu_sched_ctx_set_perf_counters(sched_ctx1, perf_counters);
  122. starpu_sched_ctx_set_perf_counters(sched_ctx2, perf_counters);
  123. /* register the contexts that should be managed by the hypervisor
  124. and indicate an approximate amount of workload if known;
  125. in this case we don't know it and we put 0 */
  126. sc_hypervisor_register_ctx(sched_ctx1, 0.0);
  127. sc_hypervisor_register_ctx(sched_ctx2, 0.0);
  128. starpu_pthread_t tid[2];
  129. val[0] = 0;
  130. val[1] = 0;
  131. STARPU_PTHREAD_MUTEX_INIT(&mut[0], NULL);
  132. STARPU_PTHREAD_MUTEX_INIT(&mut[1], NULL);
  133. /* we create two threads to simulate simultaneous submission of tasks */
  134. STARPU_PTHREAD_CREATE(&tid[0], NULL, submit_tasks_thread, (void*)&sched_ctx1);
  135. STARPU_PTHREAD_CREATE(&tid[1], NULL, submit_tasks_thread, (void*)&sched_ctx2);
  136. STARPU_PTHREAD_JOIN(tid[0], NULL);
  137. STARPU_PTHREAD_JOIN(tid[1], NULL);
  138. /* free starpu and hypervisor data */
  139. starpu_shutdown();
  140. sc_hypervisor_shutdown();
  141. FPRINTF(stdout, "ctx = %u executed %u counter_tests out of %d \n", sched_ctx1, val[0], NTASKS*NINCR);
  142. FPRINTF(stdout, "ctx = %u executed %u counter_tests out of %d \n", sched_ctx2, val[1], NTASKS*NINCR);
  143. return 0;
  144. }