init.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 University of Bordeaux
  4. * Copyright (C) 2012,2014 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2012 Vincent Danjean <Vincent.Danjean@ens-lyon.org>
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <stdlib.h>
  19. #include "socl.h"
  20. #include "gc.h"
  21. #include "mem_objects.h"
  22. int _starpu_init_failed;
  23. volatile int _starpu_init = 0;
  24. static starpu_pthread_mutex_t _socl_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  25. static struct starpu_conf conf;
  26. void socl_init_starpu(void) {
  27. starpu_pthread_mutex_lock(&_socl_mutex);
  28. if( ! _starpu_init ){
  29. starpu_conf_init(&conf);
  30. conf.ncuda = 0;
  31. conf.ncpus = 0;
  32. _starpu_init_failed = starpu_init(&conf);
  33. if (_starpu_init_failed != 0)
  34. {
  35. DEBUG_MSG("Error when calling starpu_init: %d\n", _starpu_init_failed);
  36. }
  37. else {
  38. if (starpu_opencl_worker_get_count() == 0)
  39. {
  40. DEBUG_MSG("StarPU didn't find any OpenCL device. Try disabling CUDA support in StarPU (export STARPU_NCUDA=0).\n");
  41. _starpu_init_failed = -ENODEV;
  42. }
  43. }
  44. /* Disable dataflow implicit dependencies */
  45. starpu_data_set_default_sequential_consistency_flag(0);
  46. _starpu_init = 1;
  47. }
  48. starpu_pthread_mutex_unlock(&_socl_mutex);
  49. }
  50. /**
  51. * Initialize SOCL
  52. */
  53. __attribute__((constructor)) static void socl_init() {
  54. mem_object_init();
  55. gc_start();
  56. }
  57. void soclShutdown() {
  58. static int shutdown = 0;
  59. if (!shutdown) {
  60. shutdown = 1;
  61. starpu_pthread_mutex_lock(&_socl_mutex);
  62. if( _starpu_init )
  63. starpu_task_wait_for_all();
  64. gc_stop();
  65. if( _starpu_init )
  66. starpu_task_wait_for_all();
  67. int active_entities = gc_active_entity_count();
  68. if (active_entities != 0) {
  69. DEBUG_MSG("Unreleased entities: %d\n", active_entities);
  70. gc_print_remaining_entities();
  71. }
  72. if( _starpu_init )
  73. starpu_shutdown();
  74. starpu_pthread_mutex_unlock(&_socl_mutex);
  75. if (socl_devices != NULL) {
  76. free(socl_devices);
  77. socl_devices = NULL;
  78. }
  79. }
  80. }
  81. /**
  82. * Shutdown SOCL
  83. */
  84. __attribute__((destructor)) static void socl_shutdown() {
  85. char * skip_str = getenv("SOCL_SKIP_DESTRUCTOR");
  86. int skip = (skip_str != NULL ? atoi(skip_str) : 0);
  87. if (!skip) soclShutdown();
  88. }