lp_test.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Inria
  4. * Copyright (C) 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. unsigned val[2];
  26. starpu_pthread_mutex_t mut[2];
  27. /* Every implementation of a codelet must have this prototype, the first * argument (buffers) describes the buffers/streams that are managed by the
  28. * DSM; the second arguments references read-only data that is passed as an
  29. * argument of the codelet (task->cl_arg). Here, "buffers" is unused as there
  30. * are no data input/output managed by the DSM (cl.nbuffers = 0) */
  31. void cpu_func(__attribute__((unused))void *buffers[], void *cl_arg)
  32. {
  33. unsigned sched_ctx = *((unsigned *) cl_arg);
  34. int i;
  35. for(i = 0; i < NINCR; i++)
  36. {
  37. STARPU_PTHREAD_MUTEX_LOCK(&mut[sched_ctx - 1]);
  38. val[sched_ctx - 1]++;
  39. STARPU_PTHREAD_MUTEX_UNLOCK(&mut[sched_ctx - 1]);
  40. }
  41. }
  42. struct starpu_codelet cl = {0};
  43. void* submit_tasks_thread(void *arg)
  44. {
  45. unsigned sched_ctx = *((unsigned*)arg);
  46. starpu_sched_ctx_set_context(&sched_ctx);
  47. struct starpu_task *task[NTASKS];
  48. int i;
  49. for(i = 0; i < NTASKS; i++)
  50. {
  51. task[i] = starpu_task_create();
  52. cl.cpu_funcs[0] = cpu_func;
  53. cl.nbuffers = 0;
  54. task[i]->cl = &cl;
  55. task[i]->cl_arg = &sched_ctx;
  56. task[i]->cl_arg_size = sizeof(unsigned);
  57. task[i]->flops = NINCR*1000000000.0;
  58. int ret = starpu_task_submit(task[i]);
  59. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  60. }
  61. starpu_task_wait_for_all();
  62. return NULL;
  63. }
  64. int main()
  65. {
  66. int ret = starpu_init(NULL);
  67. if (ret == -ENODEV)
  68. return 77;
  69. /* create contexts */
  70. unsigned sched_ctx1 = starpu_sched_ctx_create(NULL, 0, "sched_ctx1", STARPU_SCHED_CTX_POLICY_NAME, "dmda", 0);
  71. unsigned sched_ctx2 = starpu_sched_ctx_create(NULL, 0, "sched_ctx2", STARPU_SCHED_CTX_POLICY_NAME, "dmda", 0);
  72. /* initialize the hypervisor */
  73. struct sc_hypervisor_policy policy;
  74. policy.custom = 0;
  75. /* indicate which strategy to use
  76. in this particular case we use app_driven which allows the user to resize
  77. the ctxs dynamically at particular moments of the execution of the application */
  78. policy.name = "feft_lp";
  79. void *perf_counters = sc_hypervisor_init(&policy);
  80. /* let starpu know which performance counters should use
  81. to inform the hypervisor how the application and the resources are executing */
  82. starpu_sched_ctx_set_perf_counters(sched_ctx1, perf_counters);
  83. starpu_sched_ctx_set_perf_counters(sched_ctx2, perf_counters);
  84. double flops1 = NTASKS*NINCR*1000000000.0;
  85. double flops2 = NTASKS*NINCR*1000000000.0;
  86. /* register the contexts that should be managed by the hypervisor
  87. and indicate an approximate amount of workload if known;
  88. in this case we don't know it and we put 0 */
  89. sc_hypervisor_register_ctx(sched_ctx1, flops1);
  90. sc_hypervisor_register_ctx(sched_ctx2, flops2);
  91. /* lp strategy allows sizing the contexts because we know the total number of flops
  92. to be executed */
  93. sc_hypervisor_size_ctxs(NULL, -1, NULL, -1);
  94. starpu_pthread_t tid[2];
  95. val[0] = 0;
  96. val[1] = 0;
  97. STARPU_PTHREAD_MUTEX_INIT(&mut[0], NULL);
  98. STARPU_PTHREAD_MUTEX_INIT(&mut[1], NULL);
  99. /* we create two threads to simulate simultaneous submission of tasks */
  100. STARPU_PTHREAD_CREATE(&tid[0], NULL, submit_tasks_thread, (void*)&sched_ctx1);
  101. STARPU_PTHREAD_CREATE(&tid[1], NULL, submit_tasks_thread, (void*)&sched_ctx2);
  102. STARPU_PTHREAD_JOIN(tid[0], NULL);
  103. STARPU_PTHREAD_JOIN(tid[1], NULL);
  104. /* free starpu and hypervisor data */
  105. starpu_shutdown();
  106. sc_hypervisor_shutdown();
  107. FPRINTF(stdout, "ctx = %u executed %u counter_tests out of %d \n", sched_ctx1, val[0], NTASKS*NINCR);
  108. FPRINTF(stdout, "ctx = %u executed %u counter_tests out of %d \n", sched_ctx2, val[1], NTASKS*NINCR);
  109. return 0;
  110. }