Kaynağa Gözat

- fix un-initialized StarPU-OpenMP's pthread keys when
starpu_init() is called directly instead of through
starpu_omp_init()

Olivier Aumage 11 yıl önce
ebeveyn
işleme
8bbd0a50c8

+ 6 - 0
src/core/workers.c

@@ -975,6 +975,9 @@ int starpu_initialize(struct starpu_conf *user_conf, int *argc, char ***argv)
 
 	int ret;
 
+#ifdef STARPU_OPENMP
+	_starpu_omp_dummy_init();
+#endif
 #ifdef STARPU_SIMGRID
 	_starpu_simgrid_init();
 #else
@@ -1367,6 +1370,9 @@ void starpu_shutdown(void)
 	/* Drop all remaining tags */
 	_starpu_tag_clear();
 
+#ifdef STARPU_OPENMP
+	_starpu_omp_dummy_shutdown();
+#endif
 	_starpu_close_debug_logfile();
 
 	STARPU_PTHREAD_MUTEX_LOCK(&init_mutex);

+ 38 - 2
src/util/openmp_runtime_support.c

@@ -39,6 +39,7 @@ static struct starpu_omp_global _global_state;
 static starpu_pthread_key_t omp_thread_key;
 static starpu_pthread_key_t omp_task_key;
 static struct starpu_conf omp_starpu_conf;
+static int omp_dummy_init = 0;
 
 struct starpu_omp_global *_starpu_omp_global_state = NULL;
 double _starpu_omp_clock_ref = 0.0; /* clock reference for starpu_omp_get_wtick */
@@ -766,12 +767,49 @@ static void omp_initial_region_exit(void)
 	_global_state.initial_region->nb_threads--;
 }
 
+/*
+ * If StarPU was compiled with --enable-openmp, but the OpenMP runtime support
+ * is not in use, starpu_init() may have been called directly instead of
+ * through starpu_omp_init(). However, some starpu_omp functions may be still
+ * be called such as _starpu_omp_get_task(). So let's setup a basic environment
+ * for them.
+ */
+void _starpu_omp_dummy_init(void)
+{
+	if (_starpu_omp_global_state != &_global_state)
+	{
+		if (omp_dummy_init == 0)
+		{
+			STARPU_PTHREAD_KEY_CREATE(&omp_thread_key, NULL);
+			STARPU_PTHREAD_KEY_CREATE(&omp_task_key, NULL);
+		}
+		omp_dummy_init++;
+	}
+}
+
+/*
+ * Free data structures allocated by _starpu_omp_dummy_init().
+ */
+void _starpu_omp_dummy_shutdown(void)
+{
+	if (omp_dummy_init > 0)
+	{
+		if (omp_dummy_init == 1)
+		{
+			STARPU_PTHREAD_KEY_DELETE(omp_thread_key);
+			STARPU_PTHREAD_KEY_DELETE(omp_task_key);
+		}
+		omp_dummy_init--;
+	}
+}
 
 /*
  * Entry point to be called by the OpenMP runtime constructor
  */
 int starpu_omp_init(void)
 {
+	_starpu_omp_global_state = &_global_state;
+
 	STARPU_PTHREAD_KEY_CREATE(&omp_thread_key, NULL);
 	STARPU_PTHREAD_KEY_CREATE(&omp_task_key, NULL);
 	_global_state.initial_device = create_omp_device_struct();
@@ -792,8 +830,6 @@ int starpu_omp_init(void)
 	/* init clock reference for starpu_omp_get_wtick */
 	_starpu_omp_clock_ref = starpu_timing_now();
 
-	_starpu_omp_global_state = &_global_state;
-
 	return 0;
 }
 

+ 2 - 0
src/util/openmp_runtime_support.h

@@ -371,6 +371,8 @@ void _starpu_omp_environment_init(void);
 void _starpu_omp_environment_exit(void);
 struct starpu_omp_thread *_starpu_omp_get_thread(void);
 struct starpu_omp_task *_starpu_omp_get_task(void);
+void _starpu_omp_dummy_init(void);
+void _starpu_omp_dummy_shutdown(void);
 #endif // STARPU_OPENMP
 
 #endif // __OPENMP_RUNTIME_SUPPORT_H__