job.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), 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 <unistd.h>
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. /*
  21. * Test that job creation is threadsafe
  22. */
  23. #define N 1000
  24. static struct starpu_task *tasks[N];
  25. void dummy_func(void *arg)
  26. {
  27. unsigned worker, i;
  28. (void) arg;
  29. if (starpu_worker_get_id_check() == 0)
  30. /* One worker creates the tasks */
  31. for (i = 0; i < N; i++)
  32. {
  33. struct starpu_task *task = starpu_task_create();
  34. task->destroy = 0;
  35. STARPU_WMB();
  36. tasks[i] = task;
  37. }
  38. else
  39. /* While others eagerly wait for it before trying to get their id */
  40. for (i = 0; i < N; i++)
  41. {
  42. struct starpu_task *task;
  43. while (!(task = tasks[i]))
  44. STARPU_SYNCHRONIZE();
  45. STARPU_RMB();
  46. starpu_task_get_job_id(task);
  47. }
  48. }
  49. int main(void)
  50. {
  51. int ret;
  52. unsigned i;
  53. ret = starpu_init(NULL);
  54. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  55. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  56. starpu_execute_on_each_worker(dummy_func, NULL, STARPU_CPU);
  57. for (i = 0; i < N; i++)
  58. {
  59. starpu_task_destroy(tasks[i]);
  60. }
  61. struct starpu_task *task = starpu_task_create();
  62. unsigned long id;
  63. task->destroy = 0;
  64. id = starpu_task_get_job_id(task);
  65. starpu_task_destroy(task);
  66. FPRINTF(stderr, "jobid %lu for %u tasks and %u workers\n",
  67. id, N, starpu_worker_get_count());
  68. /* We are not supposed to have created more than one jobid for each
  69. * worker (for execute_on_each) and for each of the N user tasks. */
  70. ret = id > starpu_worker_get_count() + N + 1;
  71. starpu_shutdown();
  72. return ret;
  73. }