init.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2012 Vincent Danjean
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <stdlib.h>
  18. #include "../src/core/workers.h"
  19. #include "socl.h"
  20. #include "gc.h"
  21. #include "mem_objects.h"
  22. int _starpu_init_failed;
  23. static enum initialization _socl_init = UNINITIALIZED;
  24. static starpu_pthread_mutex_t _socl_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  25. static starpu_pthread_cond_t _socl_cond = STARPU_PTHREAD_COND_INITIALIZER;
  26. static pthread_t _socl_thread_init;
  27. static struct starpu_conf conf;
  28. int socl_init_starpu(void)
  29. {
  30. STARPU_PTHREAD_MUTEX_LOCK(&_socl_mutex);
  31. if (_socl_init == INITIALIZED)
  32. {
  33. STARPU_PTHREAD_MUTEX_UNLOCK(&_socl_mutex);
  34. return 0;
  35. }
  36. if (_socl_init == CHANGING)
  37. {
  38. /* Avoid recursion when starpu_init calls hwloc initialization which uses its opencl plugin */
  39. if (pthread_equal(_socl_thread_init, pthread_self()))
  40. {
  41. STARPU_PTHREAD_MUTEX_UNLOCK(&_socl_mutex);
  42. return -1;
  43. }
  44. /* Somebody else is initializing already, wait for him */
  45. while (_socl_init != INITIALIZED)
  46. STARPU_PTHREAD_COND_WAIT(&_socl_cond, &_socl_mutex);
  47. STARPU_PTHREAD_MUTEX_UNLOCK(&_socl_mutex);
  48. return 0;
  49. }
  50. _socl_init = CHANGING;
  51. _socl_thread_init = pthread_self();
  52. STARPU_PTHREAD_MUTEX_UNLOCK(&_socl_mutex);
  53. starpu_conf_init(&conf);
  54. unsetenv("STARPU_NCPU");
  55. unsetenv("STARPU_NCUDA");
  56. conf.ncuda = 0;
  57. conf.ncpus = 0;
  58. _starpu_init_failed = starpu_init(&conf);
  59. if (_starpu_init_failed != 0)
  60. {
  61. DEBUG_MSG("Error when calling starpu_init: %d\n", _starpu_init_failed);
  62. }
  63. else
  64. {
  65. if (starpu_opencl_worker_get_count() == 0)
  66. {
  67. DEBUG_MSG("StarPU didn't find any OpenCL device. Try disabling CUDA support in StarPU (export STARPU_NCUDA=0).\n");
  68. _starpu_init_failed = -ENODEV;
  69. }
  70. }
  71. /* Disable dataflow implicit dependencies */
  72. starpu_data_set_default_sequential_consistency_flag(0);
  73. STARPU_PTHREAD_MUTEX_LOCK(&_socl_mutex);
  74. _socl_init = INITIALIZED;
  75. STARPU_PTHREAD_COND_BROADCAST(&_socl_cond);
  76. STARPU_PTHREAD_MUTEX_UNLOCK(&_socl_mutex);
  77. return 0;
  78. }
  79. /**
  80. * Initialize SOCL
  81. */
  82. __attribute__((constructor)) static void socl_init()
  83. {
  84. mem_object_init();
  85. gc_start();
  86. }
  87. void soclShutdown()
  88. {
  89. static int shutdown = 0;
  90. if (!shutdown)
  91. {
  92. shutdown = 1;
  93. STARPU_PTHREAD_MUTEX_LOCK(&_socl_mutex);
  94. if( _socl_init )
  95. starpu_task_wait_for_all();
  96. gc_stop();
  97. if( _socl_init )
  98. starpu_task_wait_for_all();
  99. int active_entities = gc_active_entity_count();
  100. if (active_entities != 0)
  101. {
  102. DEBUG_MSG("Unreleased entities: %d\n", active_entities);
  103. gc_print_remaining_entities();
  104. }
  105. if( _socl_init && _starpu_init_failed != -ENODEV)
  106. starpu_shutdown();
  107. STARPU_PTHREAD_MUTEX_UNLOCK(&_socl_mutex);
  108. if (socl_devices != NULL)
  109. {
  110. free(socl_devices);
  111. socl_devices = NULL;
  112. }
  113. }
  114. }
  115. /**
  116. * Shutdown SOCL
  117. */
  118. __attribute__((destructor)) static void socl_shutdown()
  119. {
  120. char * skip_str = getenv("SOCL_SKIP_DESTRUCTOR");
  121. int skip = (skip_str != NULL ? atoi(skip_str) : 0);
  122. if (!skip) soclShutdown();
  123. }