Browse Source

Added a mutex release in starpu_init in case of several threads calling it simultaneously.
Added a testing program lauching several threads, each calling starpu_init.

Nicolas Collin 14 years ago
parent
commit
dc11e6e998
3 changed files with 58 additions and 3 deletions
  1. 5 3
      src/core/workers.c
  2. 6 0
      tests/Makefile.am
  3. 47 0
      tests/core/multithreaded_init.c

+ 5 - 3
src/core/workers.c

@@ -233,9 +233,11 @@ int starpu_init(struct starpu_conf *user_conf)
 		/* Wait for the other one changing it */
 		PTHREAD_COND_WAIT(&init_cond, &init_mutex);
 	init_count++;
-	if (initialized == INITIALIZED)
-		/* He initialized it, don't do it again */
-		return 0;
+	if (initialized == INITIALIZED) {
+	  /* He initialized it, don't do it again, and let the others get the mutex */
+	  PTHREAD_MUTEX_UNLOCK(&init_mutex);
+	  return 0;
+	  }
 	/* initialized == UNINITIALIZED */
 	initialized = CHANGING;
 	PTHREAD_MUTEX_UNLOCK(&init_mutex);

+ 6 - 0
tests/Makefile.am

@@ -79,6 +79,7 @@ check_PROGRAMS += 				\
 	core/restart				\
 	core/execute_on_a_specific_worker	\
 	core/multithreaded			\
+	core/multithreaded_init			\
 	core/starpu_task_wait_for_all		\
 	core/starpu_task_wait			\
 	core/static_restartable			\
@@ -149,6 +150,11 @@ core_multithreaded_SOURCES =			\
 	core/multithreaded.c
 
 testbin_PROGRAMS +=				\
+	core/multithreaded_init
+core_multithreaded_init_SOURCES =			\
+	core/multithreaded_init.c
+
+testbin_PROGRAMS +=				\
 	core/starpu_task_wait_for_all
 core_starpu_task_wait_for_all_SOURCES =		\
 	core/starpu_task_wait_for_all.c

+ 47 - 0
tests/core/multithreaded_init.c

@@ -0,0 +1,47 @@
+#include <stdio.h>
+#include <pthread.h>
+#include <starpu.h>
+
+#define NUM_THREADS 5
+
+void *launch_starpu(void *id)
+{ 
+   starpu_init(NULL);
+   return NULL;
+}
+
+int main(int argc, char **argv)
+{ 
+  unsigned i;
+  double timing;
+  struct timeval start;
+  struct timeval end;
+
+  pthread_t threads[NUM_THREADS];
+  
+  gettimeofday(&start, NULL);
+
+  for (i = 0; i < NUM_THREADS; ++i)
+    {
+      int ret = pthread_create(&threads[i], NULL, launch_starpu, NULL);
+      STARPU_ASSERT(ret == 0);
+    }
+
+  for (i = 0; i < NUM_THREADS; ++i)
+    {
+      int ret = pthread_join(threads[i], NULL);
+      STARPU_ASSERT(ret == 0);
+    }
+
+  gettimeofday(&end, NULL);
+
+  timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
+
+  fprintf(stderr, "Success : %d threads launching simultaneously starpu_init\n", NUM_THREADS);
+  fprintf(stderr, "Total: %lf secs\n", timing/1000000);
+  fprintf(stderr, "Per task: %lf usecs\n", timing/NUM_THREADS);
+
+  starpu_shutdown();
+
+  return 0;
+}