job.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. int worker0;
  29. (void) arg;
  30. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, &worker0, 1);
  31. if ((int) starpu_worker_get_id_check() == worker0)
  32. /* One worker creates the tasks */
  33. for (i = 0; i < N; i++)
  34. {
  35. struct starpu_task *task = starpu_task_create();
  36. task->destroy = 0;
  37. STARPU_WMB();
  38. tasks[i] = task;
  39. }
  40. else
  41. /* While others eagerly wait for it before trying to get their id */
  42. for (i = 0; i < N; i++)
  43. {
  44. struct starpu_task *task;
  45. while (!(task = tasks[i]))
  46. {
  47. STARPU_UYIELD();
  48. STARPU_SYNCHRONIZE();
  49. }
  50. STARPU_RMB();
  51. starpu_task_get_job_id(task);
  52. }
  53. }
  54. int main(void)
  55. {
  56. int ret;
  57. unsigned i;
  58. struct starpu_conf conf;
  59. starpu_conf_init(&conf);
  60. conf.precedence_over_environment_variables = 1;
  61. starpu_conf_noworker(&conf);
  62. conf.ncpus = -1;
  63. ret = starpu_init(&conf);
  64. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  65. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  66. starpu_execute_on_each_worker(dummy_func, NULL, STARPU_CPU);
  67. for (i = 0; i < N; i++)
  68. {
  69. starpu_task_destroy(tasks[i]);
  70. }
  71. struct starpu_task *task = starpu_task_create();
  72. unsigned long id;
  73. task->destroy = 0;
  74. id = starpu_task_get_job_id(task);
  75. starpu_task_destroy(task);
  76. FPRINTF(stderr, "jobid %lu for %u tasks and %u workers\n",
  77. id, N, starpu_worker_get_count());
  78. /* We are not supposed to have created more than one jobid for each
  79. * worker (for execute_on_each) and for each of the N user tasks. */
  80. ret = id > starpu_worker_get_count() + N + 1;
  81. starpu_shutdown();
  82. return ret;
  83. }