init.c 3.7 KB

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