init.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 University of Bordeaux
  4. * Copyright (C) 2012 CNRS
  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 <pthread.h>
  19. #include <stdlib.h>
  20. #include "socl.h"
  21. #include "gc.h"
  22. #include "mem_objects.h"
  23. int _starpu_init_failed;
  24. int _starpu_init = 0;
  25. pthread_mutex_t _socl_mutex = PTHREAD_MUTEX_INITIALIZER;
  26. void socl_init_starpu(void) {
  27. pthread_mutex_lock(&_socl_mutex);
  28. if( ! _starpu_init ){
  29. struct starpu_conf conf;
  30. starpu_conf_init(&conf);
  31. conf.ncuda = 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_cpu_worker_get_count() == 0)
  39. {
  40. DEBUG_MSG("StarPU did not find any CPU device. SOCL needs at least 1 CPU.\n");
  41. _starpu_init_failed = -ENODEV;
  42. }
  43. if (starpu_opencl_worker_get_count() == 0)
  44. {
  45. DEBUG_MSG("StarPU didn't find any OpenCL device. Try disabling CUDA support in StarPU (export STARPU_NCUDA=0).\n");
  46. _starpu_init_failed = -ENODEV;
  47. }
  48. }
  49. /* Disable dataflow implicit dependencies */
  50. starpu_data_set_default_sequential_consistency_flag(0);
  51. _starpu_init = 1;
  52. }
  53. pthread_mutex_unlock(&_socl_mutex);
  54. }
  55. /**
  56. * Initialize SOCL
  57. */
  58. __attribute__((constructor)) static void socl_init() {
  59. mem_object_init();
  60. gc_start();
  61. }
  62. void soclShutdown() {
  63. static int shutdown = 0;
  64. if (!shutdown) {
  65. shutdown = 1;
  66. pthread_mutex_lock(&_socl_mutex);
  67. if( _starpu_init )
  68. starpu_task_wait_for_all();
  69. gc_stop();
  70. if( _starpu_init )
  71. starpu_task_wait_for_all();
  72. int active_entities = gc_active_entity_count();
  73. if (active_entities != 0)
  74. DEBUG_MSG("Unreleased entities: %d\n", active_entities);
  75. if( _starpu_init )
  76. starpu_shutdown();
  77. pthread_mutex_unlock(&_socl_mutex);
  78. if (socl_devices != NULL) {
  79. free(socl_devices);
  80. socl_devices = NULL;
  81. }
  82. }
  83. }
  84. /**
  85. * Shutdown SOCL
  86. */
  87. __attribute__((destructor)) static void socl_shutdown() {
  88. char * skip_str = getenv("SOCL_SKIP_DESTRUCTOR");
  89. int skip = (skip_str != NULL ? atoi(skip_str) : 0);
  90. if (!skip) soclShutdown();
  91. }