Browse Source

use condition variables instead of semaphores

Cédric Augonnet 16 years ago
parent
commit
0d4b1091bb

+ 11 - 5
examples/incrementer/incrementer.c

@@ -14,7 +14,6 @@
  * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  */
 
-#include <semaphore.h>
 #include <string.h>
 #include <math.h>
 #include <sys/types.h>
@@ -36,7 +35,8 @@
 starpu_data_handle my_float_state;
 starpu_data_handle unity_state;
 
-sem_t sem;
+static pthread_mutex_t mutex;
+static pthread_cond_t cond;
 
 unsigned size __attribute__ ((aligned (16))) = 4*sizeof(float);
 
@@ -50,7 +50,10 @@ void callback_func(void *argcb)
 
 	if (cnt == NITER) 
 	{
-		sem_post(&sem);
+		pthread_mutex_lock(&mutex);
+		pthread_cond_signal(&cond);
+		pthread_mutex_unlock(&mutex);
+
 	}
 }
 
@@ -113,7 +116,8 @@ int main(__attribute__ ((unused)) int argc, __attribute__ ((unused)) char **argv
 	starpu_init();
 	fprintf(stderr, "StarPU initialized ...\n");
 
-	sem_init(&sem, 0, 0);
+	pthread_mutex_init(&mutex, NULL);
+	pthread_cond_init(&cond, NULL);
 
 	init_data();
 
@@ -156,7 +160,9 @@ int main(__attribute__ ((unused)) int argc, __attribute__ ((unused)) char **argv
 		starpu_submit_task(task);
 	}
 
-	sem_wait(&sem);
+	pthread_mutex_lock(&mutex);
+	pthread_cond_wait(&cond, &mutex);
+	pthread_mutex_unlock(&mutex);
 
 //	/* stop monitoring data and grab it in RAM */
 //	unpartition_data(&my_float_state, 0);

+ 11 - 4
examples/mult/dw_mult.c

@@ -19,6 +19,9 @@
 float *A, *B, *C;
 starpu_data_handle A_state, B_state, C_state;
 
+pthread_mutex_t mutex;
+pthread_cond_t cond;
+
 /*
  * That program should compute C = A * B 
  * 
@@ -80,7 +83,9 @@ void terminate(void)
 	}
 #endif // CHECK_OUTPUT
 
-	sem_post(&sem);
+	pthread_mutex_lock(&mutex);
+	pthread_cond_signal(&cond);
+	pthread_mutex_unlock(&mutex);
 }
 
 void callback_func(void *arg)
@@ -321,7 +326,8 @@ int main(__attribute__ ((unused)) int argc,
 	/* start the runtime */
 	starpu_init();
 
-	sem_init(&sem, 0, 0U);
+	pthread_mutex_init(&mutex, NULL);
+	pthread_cond_init(&cond, NULL);
 
 	init_problem_data();
 
@@ -329,8 +335,9 @@ int main(__attribute__ ((unused)) int argc,
 
 	launch_codelets();
 
-	sem_wait(&sem);
-	sem_destroy(&sem);
+	pthread_mutex_lock(&mutex);
+	pthread_cond_wait(&cond, &mutex);
+	pthread_mutex_unlock(&mutex);
 
 	starpu_shutdown();
 

+ 0 - 2
examples/mult/dw_mult.h

@@ -17,7 +17,6 @@
 #ifndef __MULT_H__
 #define __MULT_H__
 
-#include <semaphore.h>
 #include <string.h>
 #include <math.h>
 #include <sys/types.h>
@@ -77,7 +76,6 @@ uint64_t ls_atlas = 0;
 
 struct timeval start;
 struct timeval end;
-sem_t sem;
 
 static int taskcounter __attribute__ ((unused));
 static struct block_conf conf __attribute__ ((aligned (128)));

+ 11 - 4
examples/mult/dw_mult_no_stride.c

@@ -16,6 +16,9 @@
 
 #include "dw_mult.h"
 
+static pthread_mutex_t mutex;
+static pthread_cond_t cond;
+
 float *A[MAXSLICESY][MAXSLICESZ];
 float *B[MAXSLICESZ][MAXSLICESX];
 float *C[MAXSLICESY][MAXSLICESX];
@@ -73,7 +76,9 @@ static void terminate(void)
 	fprintf(stderr, "	GFlop : total (%2.2f) cublas (%2.2f) atlas (%2.2f)\n", (double)total_flop/1000000000.0f, (double)flop_cublas/1000000000.0f, (double)flop_atlas/1000000000.0f);
 	fprintf(stderr, "	GFlop/s : %2.2f\n", (double)total_flop / (double)timing/1000);
 
-	sem_post(&sem);
+	pthread_mutex_lock(&mutex);
+	pthread_cond_signal(&cond);
+	pthread_mutex_unlock(&mutex);
 }
 
 
@@ -455,14 +460,16 @@ int main(__attribute__ ((unused)) int argc,
 	/* start the runtime */
 	starpu_init();
 
-	sem_init(&sem, 0, 0U);
+	pthread_mutex_init(&mutex, NULL);
+	pthread_cond_init(&cond, NULL);
 
 	init_problem_data();
 
 	launch_codelets();
 
-	sem_wait(&sem);
-	sem_destroy(&sem);
+	pthread_mutex_lock(&mutex);
+	pthread_cond_wait(&cond, &mutex);
+	pthread_mutex_unlock(&mutex);
 
 	cleanup_problem();
 

+ 11 - 4
examples/mult/dw_mult_no_stride_no_tag.c

@@ -16,6 +16,9 @@
 
 #include "dw_mult.h"
 
+static pthread_mutex_t mutex;
+static pthread_cond_t cond;
+
 struct pos {
 	unsigned x,y, z,iter;
 };
@@ -66,7 +69,9 @@ static void terminate(void)
 	fprintf(stderr, "	GFlop : total (%2.2f) cublas (%2.2f) atlas (%2.2f)\n", (double)total_flop/1000000000.0f, (double)flop_cublas/1000000000.0f, (double)flop_atlas/1000000000.0f);
 	fprintf(stderr, "	GFlop/s : %2.2f\n", (double)total_flop / (double)timing/1000);
 
-	sem_post(&sem);
+	pthread_mutex_lock(&mutex);
+	pthread_cond_signal(&cond);
+	pthread_mutex_unlock(&mutex);
 }
 
 
@@ -461,14 +466,16 @@ int main(__attribute__ ((unused)) int argc,
 	/* start the runtime */
 	starpu_init();
 
-	sem_init(&sem, 0, 0U);
+	pthread_mutex_init(&mutex, NULL);
+	pthread_cond_init(&cond, NULL);
 
 	init_problem_data();
 
 	launch_codelets();
 
-	sem_wait(&sem);
-	sem_destroy(&sem);
+	pthread_mutex_lock(&mutex);
+	pthread_cond_wait(&cond, &mutex);
+	pthread_mutex_unlock(&mutex);
 
 	cleanup_problem();