lp_test.c 4.4 KB

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