app_driven_test.c 5.4 KB

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