Browse Source

Clean socl initialization tests

Samuel Thibault 8 years ago
parent
commit
c279154b32
3 changed files with 58 additions and 37 deletions
  1. 2 3
      socl/src/cl_createcontextfromtype.c
  2. 5 6
      socl/src/cl_getdeviceids.c
  3. 51 28
      socl/src/init.c

+ 2 - 3
socl/src/cl_createcontextfromtype.c

@@ -26,9 +26,8 @@ soclCreateContextFromType(const cl_context_properties * properties,
                         void *                        user_data,
                         cl_int *                      errcode_ret) CL_API_SUFFIX__VERSION_1_0
 {
-   if( ! _starpu_init )
-      if (socl_init_starpu() < 0)
-	return NULL;
+    if (socl_init_starpu() < 0)
+      return NULL;
 
 
    //TODO: appropriate error messages

+ 5 - 6
socl/src/cl_getdeviceids.c

@@ -31,12 +31,11 @@ soclGetDeviceIDs(cl_platform_id   platform,
                cl_device_id *   devices,
                cl_uint *        num_devices) CL_API_SUFFIX__VERSION_1_0
 {
-   if( ! _starpu_init )
-      if (socl_init_starpu() < 0)
-      {
-         *num_devices = 0;
-         return CL_SUCCESS;
-      }
+    if (socl_init_starpu() < 0)
+    {
+       *num_devices = 0;
+       return CL_SUCCESS;
+    }
 
    if (_starpu_init_failed) {
       *num_devices = 0;

+ 51 - 28
socl/src/init.c

@@ -17,46 +17,69 @@
  */
 
 #include <stdlib.h>
+#include "../src/core/workers.h"
 #include "socl.h"
 #include "gc.h"
 #include "mem_objects.h"
 
 int _starpu_init_failed;
-volatile int _starpu_init = 0;
-volatile int _starpu_initing = 0;
+static enum initialization _socl_init = UNINITIALIZED;
 static starpu_pthread_mutex_t _socl_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
+static starpu_pthread_cond_t _socl_cond = STARPU_PTHREAD_COND_INITIALIZER;
+static pthread_t _socl_thread_init;
 static struct starpu_conf conf;
 
 int socl_init_starpu(void) {
-  if (_starpu_initing)
-    /* Avoid recursion when starpu_init calls hwloc initialization which uses its opencl plugin */
-    return -1;
   STARPU_PTHREAD_MUTEX_LOCK(&_socl_mutex);
-  if( ! _starpu_init ){
-    _starpu_initing = 1;
-    starpu_conf_init(&conf);
-    conf.ncuda = 0;
-    conf.ncpus = 0;
-
+  if (_socl_init == INITIALIZED)
+  {
+    STARPU_PTHREAD_MUTEX_UNLOCK(&_socl_mutex);
+    return 0;
+  }
 
-    _starpu_init_failed = starpu_init(&conf);
-    if (_starpu_init_failed != 0)
+  if (_socl_init == CHANGING)
+  {
+    /* Avoid recursion when starpu_init calls hwloc initialization which uses its opencl plugin */
+    if (pthread_equal(_socl_thread_init, pthread_self()))
     {
-       DEBUG_MSG("Error when calling starpu_init: %d\n", _starpu_init_failed);
-    }
-    else {
-       if (starpu_opencl_worker_get_count() == 0)
-       {
-	    DEBUG_MSG("StarPU didn't find any OpenCL device. Try disabling CUDA support in StarPU (export STARPU_NCUDA=0).\n");
-	    _starpu_init_failed = -ENODEV;
-       }
+      STARPU_PTHREAD_MUTEX_UNLOCK(&_socl_mutex);
+      return -1;
     }
 
-    /* Disable dataflow implicit dependencies */
-    starpu_data_set_default_sequential_consistency_flag(0);
-    _starpu_init = 1;
-    _starpu_initing = 0;
+    /* Somebody else is initializing already, wait for him */
+    while (_socl_init != INITIALIZED)
+      STARPU_PTHREAD_COND_WAIT(&_socl_cond, &_socl_mutex);
+    STARPU_PTHREAD_MUTEX_UNLOCK(&_socl_mutex);
+    return 0;
   }
+  _socl_init = CHANGING;
+  _socl_thread_init = pthread_self();
+  STARPU_PTHREAD_MUTEX_UNLOCK(&_socl_mutex);
+
+  starpu_conf_init(&conf);
+  conf.ncuda = 0;
+  conf.ncpus = 0;
+
+
+  _starpu_init_failed = starpu_init(&conf);
+  if (_starpu_init_failed != 0)
+  {
+     DEBUG_MSG("Error when calling starpu_init: %d\n", _starpu_init_failed);
+  }
+  else {
+     if (starpu_opencl_worker_get_count() == 0)
+     {
+	  DEBUG_MSG("StarPU didn't find any OpenCL device. Try disabling CUDA support in StarPU (export STARPU_NCUDA=0).\n");
+	  _starpu_init_failed = -ENODEV;
+     }
+  }
+
+  /* Disable dataflow implicit dependencies */
+  starpu_data_set_default_sequential_consistency_flag(0);
+
+  STARPU_PTHREAD_MUTEX_LOCK(&_socl_mutex);
+  _socl_init = INITIALIZED;
+  STARPU_PTHREAD_COND_BROADCAST(&_socl_cond);
   STARPU_PTHREAD_MUTEX_UNLOCK(&_socl_mutex);
 
   return 0;
@@ -80,12 +103,12 @@ void soclShutdown() {
       shutdown = 1;
 
       STARPU_PTHREAD_MUTEX_LOCK(&_socl_mutex);
-      if( _starpu_init )
+      if( _socl_init )
          starpu_task_wait_for_all();
 
       gc_stop();
 
-      if( _starpu_init )
+      if( _socl_init )
          starpu_task_wait_for_all();
 
       int active_entities = gc_active_entity_count();
@@ -95,7 +118,7 @@ void soclShutdown() {
          gc_print_remaining_entities();
       }
 
-      if( _starpu_init && _starpu_init_failed != -ENODEV)
+      if( _socl_init && _starpu_init_failed != -ENODEV)
          starpu_shutdown();
       STARPU_PTHREAD_MUTEX_UNLOCK(&_socl_mutex);