Browse Source

merge trunk@6519 (src/drivers/cuda/driver_cuda.c was merged by hand i.e by copying the version out of trunk@6519 and applying by hand all patches from the branch sched_ctx)

Nathalie Furmento 13 years ago
parent
commit
e6b45c7d1a
3 changed files with 180 additions and 71 deletions
  1. 4 0
      include/starpu.h
  2. 63 0
      src/core/workers.c
  3. 113 71
      src/drivers/cuda/driver_cuda.c

+ 4 - 0
include/starpu.h

@@ -204,6 +204,10 @@ void starpu_profiling_init();
 	void starpu_display_stats();
 int starpu_run_driver(struct starpu_driver *);
 void starpu_set_end_of_submissions(void);
+
+int starpu_driver_init(struct starpu_driver *d);
+int starpu_driver_run_once(struct starpu_driver *d);
+int starpu_driver_deinit(struct starpu_driver *d);
 #ifdef __cplusplus
 }
 #endif

+ 63 - 0
src/core/workers.c

@@ -1111,3 +1111,66 @@ starpu_run_driver(struct starpu_driver *d)
 		return -EINVAL;
 	}
 }
+
+#ifdef STARPU_USE_CUDA
+extern int _starpu_cuda_driver_init(struct starpu_driver *);
+extern int _starpu_cuda_driver_run_once(struct starpu_driver *);
+extern int _starpu_cuda_driver_deinit(struct starpu_driver *);
+#endif
+
+int
+starpu_driver_init(struct starpu_driver *d)
+{
+	STARPU_ASSERT(d);
+
+	switch (d->type)
+	{
+#ifdef STARPU_USE_CUDA
+	case STARPU_CUDA_WORKER:
+		return _starpu_cuda_driver_init(d);
+#endif
+	case STARPU_CPU_WORKER:    /* Not supported yet */
+	case STARPU_OPENCL_WORKER: /* Not supported yet */
+	case STARPU_GORDON_WORKER: /* Not supported yet */
+	default:
+		return -EINVAL;
+	}
+}
+
+int
+starpu_driver_run_once(struct starpu_driver *d)
+{
+	STARPU_ASSERT(d);
+
+	switch (d->type)
+	{
+#ifdef STARPU_USE_CUDA
+	case STARPU_CUDA_WORKER:
+		return _starpu_cuda_driver_run_once(d);
+#endif
+	case STARPU_CPU_WORKER:    /* Not supported yet */
+	case STARPU_OPENCL_WORKER: /* Not supported yet */
+	case STARPU_GORDON_WORKER: /* Not supported yet */
+	default:
+		return -EINVAL;
+	}
+}
+
+int
+starpu_driver_deinit(struct starpu_driver *d)
+{
+	STARPU_ASSERT(d);
+
+	switch (d->type)
+	{
+#ifdef STARPU_USE_CUDA
+	case STARPU_CUDA_WORKER:
+		return _starpu_cuda_driver_deinit(d);
+#endif
+	case STARPU_CPU_WORKER:    /* Not supported yet */
+	case STARPU_OPENCL_WORKER: /* Not supported yet */
+	case STARPU_GORDON_WORKER: /* Not supported yet */
+	default:
+		return -EINVAL;
+	}
+}

+ 113 - 71
src/drivers/cuda/driver_cuda.c

@@ -4,7 +4,7 @@
  * Copyright (C) 2010  Mehdi Juhoor <mjuhoor@gmail.com>
  * Copyright (C) 2010, 2011, 2012  Centre National de la Recherche Scientifique
  * Copyright (C) 2011  Télécom-SudParis
- * Copyright (C) 2011  INRIA
+ * Copyright (C) 2011, 2012  INRIA
  *
  * StarPU is free software; you can redistribute it and/or modify
  * it under the terms of the GNU Lesser General Public License as published by
@@ -24,6 +24,7 @@
 #include <common/utils.h>
 #include <common/config.h>
 #include <core/debug.h>
+#include <core/sched_ctx.h>
 #include <drivers/driver_common/driver_common.h>
 #include "driver_cuda.h"
 #include <core/sched_policy.h>
@@ -273,13 +274,25 @@ static int execute_job_on_cuda(struct _starpu_job *j, struct _starpu_worker *arg
 	return 0;
 }
 
-void *_starpu_cuda_worker(void *arg)
+static struct _starpu_worker*
+_starpu_get_worker_from_driver(struct starpu_driver *d)
 {
-	struct _starpu_worker* args = arg;
+	int workers[d->id.cuda_id + 1];
+	int nworkers;
+	nworkers = starpu_worker_get_ids_by_type(STARPU_CUDA_WORKER, workers, d->id.cuda_id+1);
+	if (nworkers >= 0 && (unsigned) nworkers < d->id.cuda_id)
+		return NULL; // No device was found.
+	
+	return _starpu_get_worker_struct(workers[d->id.cuda_id]);
+}
+
+/* XXX Should this be merged with _starpu_init_cuda ? */
+int _starpu_cuda_driver_init(struct starpu_driver *d)
+{
+	struct _starpu_worker* args = _starpu_get_worker_from_driver(d);
+	STARPU_ASSERT(args);
 
 	int devid = args->devid;
-	int workerid = args->workerid;
-	unsigned memnode = args->memory_node;
 
 #ifdef STARPU_USE_FXT
 	_starpu_fxt_register_thread(args->bindid);
@@ -288,7 +301,7 @@ void *_starpu_cuda_worker(void *arg)
 
 	_starpu_bind_thread_on_cpu(args->config, args->bindid);
 
-	_starpu_set_local_memory_node_key(&memnode);
+	_starpu_set_local_memory_node_key(&args->memory_node);
 
 	_starpu_set_local_worker_key(args);
 
@@ -325,90 +338,103 @@ void *_starpu_cuda_worker(void *arg)
 	_STARPU_PTHREAD_COND_SIGNAL(&args->ready_cond);
 	_STARPU_PTHREAD_MUTEX_UNLOCK(&args->mutex);
 
-	struct _starpu_job * j;
-	struct starpu_task *task;
-	int res;
+	return 0;
+}
+
+int _starpu_cuda_driver_run_once(struct starpu_driver *d)
+{
+	struct _starpu_worker* args = _starpu_get_worker_from_driver(d);
+	STARPU_ASSERT(args);
 
-	pthread_cond_t *sched_cond = &args->sched_cond;
-	pthread_mutex_t *sched_mutex = &args->sched_mutex;
+	unsigned memnode = args->memory_node;
+	int workerid = args->workerid;
 	struct timespec start_time, end_time;
 	unsigned idle = 0;
-	while (_starpu_machine_is_running())
-	{
-		_STARPU_TRACE_START_PROGRESS(memnode);
-		_starpu_datawizard_progress(memnode, 1);
-		_STARPU_TRACE_END_PROGRESS(memnode);
 
-		task = _starpu_pop_task(args);
+	_STARPU_TRACE_START_PROGRESS(memnode);
+	_starpu_datawizard_progress(memnode, 1);
+	_STARPU_TRACE_END_PROGRESS(memnode);
 
-		if (!task) 
-		{
-			_STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
-			if (_starpu_worker_can_block(memnode))
-				_starpu_block_worker(workerid, sched_cond, sched_mutex);
-			else
-			{
-				_starpu_clock_gettime(&start_time);
-				_starpu_worker_register_sleeping_start_date(workerid, &start_time);
-				idle = 1;
-			}
-		  
-
-			_STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
-
-			continue;
-		};
-
-		if(idle)
+	pthread_cond_t *sched_cond = &args->sched_cond;
+        pthread_mutex_t *sched_mutex = &args->sched_mutex;
+
+	struct starpu_task *task = _starpu_pop_task(args);
+	struct _starpu_job *j = NULL;
+
+	if (task == NULL)
+	{
+		_STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
+		if (_starpu_worker_can_block(memnode))
+			_starpu_block_worker(workerid, sched_cond, sched_mutex);
+		else
 		{
-			_starpu_clock_gettime(&end_time);
-			
-			int profiling = starpu_profiling_status_get();
-			if (profiling)
-			{
-				struct timespec sleeping_time;
-				starpu_timespec_sub(&end_time, &start_time, &sleeping_time);
-				_starpu_worker_update_profiling_info_sleeping(workerid, &start_time, &end_time);
-			}
-			idle = 0;
+			_starpu_clock_gettime(&start_time);
+			_starpu_worker_register_sleeping_start_date(workerid, &start_time);
+			idle = 1;
 		}
 
-		STARPU_ASSERT(task);
-		j = _starpu_get_job_associated_to_task(task);
+		_STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
+
+		return 0;
+	}
+
+	if(idle)
+	{
+		_starpu_clock_gettime(&end_time);
 
-		/* can CUDA do that task ? */
-		if (!_STARPU_CUDA_MAY_PERFORM(j))
+		int profiling = starpu_profiling_status_get();
+		if (profiling)
 		{
-			/* this is neither a cuda or a cublas task */
-			_starpu_push_task(j);
-			continue;
+			struct timespec sleeping_time;
+			starpu_timespec_sub(&end_time, &start_time, &sleeping_time);
+			_starpu_worker_update_profiling_info_sleeping(workerid, &start_time, &end_time);
 		}
+		idle = 0;
+	}
+
+	STARPU_ASSERT(task);
+	j = _starpu_get_job_associated_to_task(task);
+
+	/* can CUDA do that task ? */
+	if (!_STARPU_CUDA_MAY_PERFORM(j))
+	{
+		/* this is neither a cuda or a cublas task */
+		_starpu_push_task(j);
+		return 0;
+	}
 
-		_starpu_set_current_task(task);
-		args->current_task = j->task;
+	_starpu_set_current_task(task);
+	args->current_task = j->task;
 
-		res = execute_job_on_cuda(j, args);
+	int res = execute_job_on_cuda(j, args);
 
-		_starpu_set_current_task(NULL);
-		args->current_task = NULL;
+	_starpu_set_current_task(NULL);
+	args->current_task = NULL;
 
-		if (res)
+	if (res)
+	{
+		switch (res)
 		{
-			switch (res)
-			{
-				case -EAGAIN:
-					_STARPU_DISP("ouch, put the codelet %p back ... \n", j);
-					_starpu_push_task(j);
-					STARPU_ABORT();
-					continue;
-				default:
-					STARPU_ASSERT(0);
-			}
+			case -EAGAIN:
+				_STARPU_DISP("ouch, put the codelet %p back ... \n", j);
+				_starpu_push_task(j);
+				STARPU_ABORT();
+			default:
+				STARPU_ABORT();
 		}
-
-		_starpu_handle_job_termination(j, workerid);
 	}
 
+	_starpu_handle_job_termination(j, workerid);
+
+	return 0;
+}
+
+int _starpu_cuda_driver_deinit(struct starpu_driver *d)
+{
+	struct _starpu_worker* args = _starpu_get_worker_from_driver(d);
+	STARPU_ASSERT(args);
+	unsigned memnode = args->memory_node;
+
 	_STARPU_TRACE_WORKER_DEINIT_START
 
 	_starpu_handle_all_pending_node_data_requests(memnode);
@@ -422,6 +448,22 @@ void *_starpu_cuda_worker(void *arg)
 
 	_STARPU_TRACE_WORKER_DEINIT_END(_STARPU_FUT_CUDA_KEY);
 
+	return 0;
+}
+
+void *_starpu_cuda_worker(void *arg)
+{
+	struct _starpu_worker* args = arg;
+	struct starpu_driver d = {
+		.type       = STARPU_CUDA_WORKER,
+		.id.cuda_id = args->devid
+	};
+
+	_starpu_cuda_driver_init(&d);
+	while (_starpu_machine_is_running())
+		_starpu_cuda_driver_run_once(&d);
+	_starpu_cuda_driver_deinit(&d);
+
 	return NULL;
 }