瀏覽代碼

src: driver operations are stored in a struct _starpu_driver_ops which are used to implement the public driver API

Nathalie Furmento 8 年之前
父節點
當前提交
a017fcbdeb

+ 1 - 0
src/Makefile.am

@@ -171,6 +171,7 @@ libstarpu_@STARPU_EFFECTIVE_VERSION@_la_SOURCES = 		\
 	core/task.c						\
 	core/task_bundle.c					\
 	core/tree.c						\
+	core/drivers.c						\
 	core/workers.c						\
 	core/combined_workers.c					\
 	core/topology.c						\

+ 74 - 0
src/core/drivers.c

@@ -0,0 +1,74 @@
+/* StarPU --- Runtime system for heterogeneous multicore architectures.
+ *
+ * Copyright (C) 2009-2017  Université de Bordeaux
+ * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017  CNRS
+ * Copyright (C) 2010, 2011  INRIA
+ * Copyright (C) 2011  Télécom-SudParis
+ * Copyright (C) 2011-2012, 2016, 2017  INRIA
+ * Copyright (C) 2016  Uppsala University
+ *
+ * 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
+ * the Free Software Foundation; either version 2.1 of the License, or (at
+ * your option) any later version.
+ *
+ * StarPU is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * See the GNU Lesser General Public License in COPYING.LGPL for more details.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <common/config.h>
+#include <core/debug.h>
+
+int starpu_driver_init(struct starpu_driver *d)
+{
+	STARPU_ASSERT(d);
+	struct _starpu_worker *worker = _starpu_get_worker_from_driver(d);
+
+	if (worker->driver_ops == NULL)
+		return -EINVAL;
+	else
+		return worker->driver_ops->init(worker);
+}
+
+int starpu_driver_run(struct starpu_driver *d)
+{
+	if (!d)
+	{
+		_STARPU_DEBUG("Invalid argument\n");
+		return -EINVAL;
+	}
+
+	struct _starpu_worker *worker = _starpu_get_worker_from_driver(d);
+	if (worker->driver_ops == NULL)
+		return -EINVAL;
+	else
+		return worker->driver_ops->run(worker);
+}
+
+int starpu_driver_run_once(struct starpu_driver *d)
+{
+	STARPU_ASSERT(d);
+	struct _starpu_worker *worker = _starpu_get_worker_from_driver(d);
+
+	if (worker->driver_ops == NULL)
+		return -EINVAL;
+	else
+		return worker->driver_ops->run_once(worker);
+}
+
+int starpu_driver_deinit(struct starpu_driver *d)
+{
+	STARPU_ASSERT(d);
+	struct _starpu_worker *worker = _starpu_get_worker_from_driver(d);
+
+	if (worker->driver_ops == NULL)
+		return -EINVAL;
+	else
+		return worker->driver_ops->deinit(worker);
+}
+

+ 31 - 0
src/core/drivers.h

@@ -0,0 +1,31 @@
+/* StarPU --- Runtime system for heterogeneous multicore architectures.
+ *
+ * Copyright (C) 2009-2016  Université de Bordeaux
+ * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2016, 2017  CNRS
+ * Copyright (C) 2011, 2016  INRIA
+ * Copyright (C) 2016  Uppsala University
+ *
+ * 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
+ * the Free Software Foundation; either version 2.1 of the License, or (at
+ * your option) any later version.
+ *
+ * StarPU is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * See the GNU Lesser General Public License in COPYING.LGPL for more details.
+ */
+
+#ifndef __DRIVERS_H__
+#define __DRIVERS_H__
+
+struct _starpu_driver_ops
+{
+	int (*init)(struct _starpu_worker *worker);
+	int (*run)(struct _starpu_worker *worker);
+	int (*run_once)(struct _starpu_worker *worker);
+	int (*deinit)(struct _starpu_worker *worker);
+};
+
+#endif // __DRIVERS_H__

+ 2 - 3
src/core/topology.c

@@ -87,8 +87,7 @@ static struct _starpu_worker_set mic_worker_set[STARPU_MAXMICDEVS];
 struct _starpu_worker_set mpi_worker_set[STARPU_MAXMPIDEVS];
 #endif
 
-void *
-_starpu_get_worker_from_driver(struct starpu_driver *d)
+struct _starpu_worker *_starpu_get_worker_from_driver(struct starpu_driver *d)
 {
 	unsigned nworkers = starpu_worker_get_count();
 	unsigned workerid;
@@ -121,7 +120,7 @@ _starpu_get_worker_from_driver(struct starpu_driver *d)
 			case STARPU_CUDA_WORKER:
 			{
 				if (worker->devid == d->id.cuda_id)
-					return worker->set;
+					return worker;
 				break;
 
 			}

+ 2 - 2
src/core/topology.h

@@ -1,7 +1,7 @@
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
  *
  * Copyright (C) 2009-2010, 2012, 2014-2016  Université de Bordeaux
- * Copyright (C) 2010, 2015  CNRS
+ * Copyright (C) 2010, 2015, 2017  CNRS
  *
  * 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
@@ -66,6 +66,6 @@ struct _starpu_combined_worker;
 /* Bind the current thread on the set of CPUs for the given combined worker. */
 void _starpu_bind_thread_on_cpus(struct _starpu_machine_config *config STARPU_ATTRIBUTE_UNUSED, struct _starpu_combined_worker *combined_worker);
 
-void *_starpu_get_worker_from_driver(struct starpu_driver *d);
+struct _starpu_worker *_starpu_get_worker_from_driver(struct starpu_driver *d);
 
 #endif // __TOPOLOGY_H__

+ 4 - 111
src/core/workers.c

@@ -552,6 +552,7 @@ static void _starpu_worker_init(struct _starpu_worker *workerarg, struct _starpu
 	/* name initialized by driver */
 	/* short_name initialized by driver */
 	workerarg->run_by_starpu = 1;
+	workerarg->driver_ops = NULL;
 
 	workerarg->sched_ctx_list = NULL;
 	workerarg->tmp_sched_ctx = -1;
@@ -666,6 +667,7 @@ static void _starpu_launch_drivers(struct _starpu_machine_config *pconfig)
 #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
 			case STARPU_CPU_WORKER:
 				driver.id.cpu_id = devid;
+				workerarg->driver_ops = &_starpu_driver_cpu_ops;
 				if (_starpu_may_launch_driver(&pconfig->conf, &driver))
 				{
 					STARPU_PTHREAD_CREATE_ON(
@@ -696,6 +698,7 @@ static void _starpu_launch_drivers(struct _starpu_machine_config *pconfig)
 #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
 			case STARPU_CUDA_WORKER:
 				driver.id.cuda_id = devid;
+				workerarg->driver_ops = &_starpu_driver_cuda_ops;
 
 				if (worker_set->workers != workerarg)
 					/* We are not the first worker of the
@@ -730,6 +733,7 @@ static void _starpu_launch_drivers(struct _starpu_machine_config *pconfig)
 			case STARPU_OPENCL_WORKER:
 #ifndef STARPU_SIMGRID
 				starpu_opencl_get_device(devid, &driver.id.opencl_id);
+				workerarg->driver_ops = &_starpu_driver_opencl_ops;
 				if (!_starpu_may_launch_driver(&pconfig->conf, &driver))
 				{
 					workerarg->run_by_starpu = 0;
@@ -2148,117 +2152,6 @@ int starpu_worker_get_nids_ctx_free_by_type(enum starpu_worker_archtype type, in
 	return cnt;
 }
 
-
-int
-starpu_driver_run(struct starpu_driver *d)
-{
-	if (!d)
-	{
-		_STARPU_DEBUG("Invalid argument\n");
-		return -EINVAL;
-	}
-
-	void *worker = _starpu_get_worker_from_driver(d);
-
-	switch (d->type)
-	{
-#ifdef STARPU_USE_CPU
-	case STARPU_CPU_WORKER:
-		return _starpu_run_cpu(worker);
-#endif
-#ifdef STARPU_USE_CUDA
-	case STARPU_CUDA_WORKER:
-		return _starpu_run_cuda(worker);
-#endif
-#ifdef STARPU_USE_OPENCL
-	case STARPU_OPENCL_WORKER:
-		return _starpu_run_opencl(worker);
-#endif
-	default:
-		(void) worker;
-		_STARPU_DEBUG("Invalid device type\n");
-		return -EINVAL;
-	}
-}
-
-int
-starpu_driver_init(struct starpu_driver *d)
-{
-	STARPU_ASSERT(d);
-	void *worker = _starpu_get_worker_from_driver(d);
-
-	switch (d->type)
-	{
-#ifdef STARPU_USE_CPU
-	case STARPU_CPU_WORKER:
-		return _starpu_cpu_driver_init(worker);
-#endif
-#ifdef STARPU_USE_CUDA
-	case STARPU_CUDA_WORKER:
-		return _starpu_cuda_driver_init(worker);
-#endif
-#ifdef STARPU_USE_OPENCL
-	case STARPU_OPENCL_WORKER:
-		return _starpu_opencl_driver_init(worker);
-#endif
-	default:
-		(void) worker;
-		return -EINVAL;
-	}
-}
-
-int
-starpu_driver_run_once(struct starpu_driver *d)
-{
-	STARPU_ASSERT(d);
-	void *worker = _starpu_get_worker_from_driver(d);
-
-	switch (d->type)
-	{
-#ifdef STARPU_USE_CPU
-	case STARPU_CPU_WORKER:
-		return _starpu_cpu_driver_run_once(worker);
-#endif
-#ifdef STARPU_USE_CUDA
-	case STARPU_CUDA_WORKER:
-		return _starpu_cuda_driver_run_once(worker);
-#endif
-#ifdef STARPU_USE_OPENCL
-	case STARPU_OPENCL_WORKER:
-		return _starpu_opencl_driver_run_once(worker);
-#endif
-	default:
-		(void) worker;
-		return -EINVAL;
-	}
-}
-
-int
-starpu_driver_deinit(struct starpu_driver *d)
-{
-	STARPU_ASSERT(d);
-	void *worker = _starpu_get_worker_from_driver(d);
-
-	switch (d->type)
-	{
-#ifdef STARPU_USE_CPU
-	case STARPU_CPU_WORKER:
-		return _starpu_cpu_driver_deinit(worker);
-#endif
-#ifdef STARPU_USE_CUDA
-	case STARPU_CUDA_WORKER:
-		return _starpu_cuda_driver_deinit(worker);
-#endif
-#ifdef STARPU_USE_OPENCL
-	case STARPU_OPENCL_WORKER:
-		return _starpu_opencl_driver_deinit(worker);
-#endif
-	default:
-		(void) worker;
-		return -EINVAL;
-	}
-}
-
 void starpu_get_version(int *major, int *minor, int *release)
 {
 	*major = STARPU_MAJOR_VERSION;

+ 2 - 0
src/core/workers.h

@@ -36,6 +36,7 @@
 #include <hwloc.h>
 #endif
 
+#include <core/drivers.h>
 #include <drivers/cuda/driver_cuda.h>
 #include <drivers/opencl/driver_opencl.h>
 
@@ -103,6 +104,7 @@ LIST_TYPE(_starpu_worker,
 	char name[64];
 	char short_name[10];
 	unsigned run_by_starpu; /* Is this run by StarPU or directly by the application ? */
+	struct _starpu_driver_ops *driver_ops;
 
 	struct _starpu_sched_ctx_list *sched_ctx_list;
 	int tmp_sched_ctx;

+ 14 - 5
src/drivers/cpu/driver_cpu.c

@@ -2,7 +2,7 @@
  *
  * Copyright (C) 2010-2017  Université de Bordeaux
  * Copyright (C) 2010  Mehdi Juhoor <mjuhoor@gmail.com>
- * Copyright (C) 2010-2016  CNRS
+ * Copyright (C) 2010-2017  CNRS
  * Copyright (C) 2011  Télécom-SudParis
  * Copyright (C) 2014  INRIA
  *
@@ -27,11 +27,13 @@
 #include <common/utils.h>
 #include <core/debug.h>
 #include <core/workers.h>
-#include "driver_cpu.h"
+#include <core/drivers.h>
+#include <drivers/cpu/driver_cpu.h>
 #include <core/sched_policy.h>
 #include <datawizard/memory_manager.h>
 #include <datawizard/malloc.h>
 #include <core/simgrid.h>
+#include <core/task.h>
 
 #ifdef STARPU_HAVE_HWLOC
 #include <hwloc.h>
@@ -412,8 +414,7 @@ int _starpu_cpu_driver_deinit(struct _starpu_worker *cpu_worker)
 	return 0;
 }
 
-void *
-_starpu_cpu_worker(void *arg)
+void *_starpu_cpu_worker(void *arg)
 {
 	struct _starpu_worker *worker = arg;
 
@@ -430,7 +431,7 @@ _starpu_cpu_worker(void *arg)
 	return NULL;
 }
 
-int _starpu_run_cpu(struct _starpu_worker *worker)
+int _starpu_cpu_driver_run(struct _starpu_worker *worker)
 {
 	worker->set = NULL;
 	worker->worker_is_initialized = 0;
@@ -438,3 +439,11 @@ int _starpu_run_cpu(struct _starpu_worker *worker)
 
 	return 0;
 }
+
+struct _starpu_driver_ops _starpu_driver_cpu_ops =
+{
+	.init = _starpu_cpu_driver_init,
+	.run = _starpu_cpu_driver_run,
+	.run_once = _starpu_cpu_driver_run_once,
+	.deinit = _starpu_cpu_driver_deinit
+};

+ 4 - 15
src/drivers/cpu/driver_cpu.h

@@ -1,7 +1,7 @@
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
  *
  * Copyright (C) 2010, 2014  Université de Bordeaux
- * Copyright (C) 2010, 2012, 2013  CNRS
+ * Copyright (C) 2010, 2012, 2013, 2017  CNRS
  *
  * 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
@@ -19,23 +19,12 @@
 #define __DRIVER_CPU_H__
 
 #include <common/config.h>
-#include <core/jobs.h>
-#include <core/task.h>
-#include <core/workers.h>
-
-#include <core/perfmodel/perfmodel.h>
-#include <common/fxt.h>
-#include <datawizard/datawizard.h>
-
-#include <starpu.h>
 
 #ifdef STARPU_USE_CPU
+
+extern struct _starpu_driver_ops _starpu_driver_cpu_ops;
 void *_starpu_cpu_worker(void *);
-struct _starpu_worker;
-int _starpu_run_cpu(struct _starpu_worker *);
-int _starpu_cpu_driver_init(struct _starpu_worker *);
-int _starpu_cpu_driver_run_once(struct _starpu_worker *);
-int _starpu_cpu_driver_deinit(struct _starpu_worker *);
+
 #endif /* !STARPU_USE_CPU */
 
 #endif //  __DRIVER_CPU_H__

+ 31 - 2
src/drivers/cuda/driver_cuda.c

@@ -33,6 +33,7 @@
 #include <datawizard/memory_manager.h>
 #include <datawizard/memory_nodes.h>
 #include <datawizard/malloc.h>
+#include <core/task.h>
 
 #ifdef STARPU_SIMGRID
 #include <core/simgrid.h>
@@ -940,12 +941,12 @@ int _starpu_cuda_driver_deinit(struct _starpu_worker_set *worker_set)
                 {
 			/* I'm last, deinitialize device */
 			_starpu_handle_all_pending_node_data_requests(memnode);
-			
+
 			/* In case there remains some memory that was automatically
 			 * allocated by StarPU, we release it now. Note that data
 			 * coherency is not maintained anymore at that point ! */
 			_starpu_free_all_automatically_allocated_buffers(memnode);
-			
+
 			_starpu_malloc_shutdown(memnode);
 
 #ifndef STARPU_SIMGRID
@@ -1112,3 +1113,31 @@ int _starpu_run_cuda(struct _starpu_worker_set *workerarg)
 
 	return 0;
 }
+
+int _starpu_cuda_driver_init_from_worker(struct _starpu_worker *worker)
+{
+	return _starpu_cuda_driver_init(worker->set);
+}
+
+int _starpu_cuda_run_from_worker(struct _starpu_worker *worker)
+{
+	return _starpu_run_cuda(worker->set);
+}
+
+int _starpu_cuda_driver_run_once_from_worker(struct _starpu_worker *worker)
+{
+	return _starpu_cuda_driver_run_once(worker->set);
+}
+
+int _starpu_cuda_driver_deinit_from_worker(struct _starpu_worker *worker)
+{
+	return _starpu_cuda_driver_deinit(worker->set);
+}
+
+struct _starpu_driver_ops _starpu_driver_cuda_ops =
+{
+	.init = _starpu_cuda_driver_init_from_worker,
+	.run = _starpu_cuda_run_from_worker,
+	.run_once = _starpu_cuda_driver_run_once_from_worker,
+	.deinit = _starpu_cuda_driver_deinit_from_worker
+};

+ 5 - 14
src/drivers/cuda/driver_cuda.h

@@ -1,7 +1,7 @@
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
  *
  * Copyright (C) 2009, 2010, 2012-2014, 2016  Université de Bordeaux
- * Copyright (C) 2010, 2012  CNRS
+ * Copyright (C) 2010, 2012, 2017  CNRS
  *
  * 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
@@ -18,6 +18,8 @@
 #ifndef __DRIVER_CUDA_H__
 #define __DRIVER_CUDA_H__
 
+#include <common/config.h>
+
 #ifdef STARPU_USE_CUDA
 #include <cuda.h>
 #include <cuda_runtime_api.h>
@@ -25,14 +27,8 @@
 #endif
 
 #include <starpu.h>
-#include <common/config.h>
 
-#include <core/jobs.h>
-#include <core/task.h>
-#include <datawizard/datawizard.h>
-#include <core/perfmodel/perfmodel.h>
-
-#include <common/fxt.h>
+extern struct _starpu_driver_ops _starpu_driver_cuda_ops;
 
 void _starpu_cuda_init(void);
 unsigned _starpu_get_cuda_device_count(void);
@@ -45,18 +41,13 @@ void *_starpu_cuda_worker(void *);
 #else
 #  define _starpu_cuda_discover_devices(config) ((void) config)
 #endif
+
 #ifdef STARPU_USE_CUDA
 cudaStream_t starpu_cuda_get_local_in_transfer_stream(void);
 cudaStream_t starpu_cuda_get_in_transfer_stream(unsigned dst_node);
 cudaStream_t starpu_cuda_get_local_out_transfer_stream(void);
 cudaStream_t starpu_cuda_get_out_transfer_stream(unsigned src_node);
 cudaStream_t starpu_cuda_get_peer_transfer_stream(unsigned src_node, unsigned dst_node);
-
-struct _starpu_worker_set;
-int _starpu_run_cuda(struct _starpu_worker_set *);
-int _starpu_cuda_driver_init(struct _starpu_worker_set *);
-int _starpu_cuda_driver_run_once(struct _starpu_worker_set *);
-int _starpu_cuda_driver_deinit(struct _starpu_worker_set *);
 #endif
 
 #endif //  __DRIVER_CUDA_H__

+ 10 - 0
src/drivers/opencl/driver_opencl.c

@@ -31,6 +31,7 @@
 #include <datawizard/memory_manager.h>
 #include <datawizard/memory_nodes.h>
 #include <datawizard/malloc.h>
+#include <core/task.h>
 
 #ifdef STARPU_SIMGRID
 #include <core/simgrid.h>
@@ -1083,4 +1084,13 @@ int _starpu_run_opencl(struct _starpu_worker *workerarg)
 
 	return 0;
 }
+
+struct _starpu_driver_ops _starpu_driver_opencl_ops =
+{
+	.init = _starpu_opencl_driver_init,
+	.run = _starpu_run_opencl,
+	.run_once = _starpu_opencl_driver_run_once,
+	.deinit = _starpu_opencl_driver_deinit
+};
+
 #endif /* STARPU_USE_OPENCL */

+ 14 - 42
src/drivers/opencl/driver_opencl.h

@@ -1,7 +1,7 @@
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
  *
  * Copyright (C) 2010-2011, 2013-2014  Université de Bordeaux
- * Copyright (C) 2010, 2012  CNRS
+ * Copyright (C) 2010, 2012, 2017  CNRS
  *
  * 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
@@ -33,65 +33,37 @@
 
 #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
 struct _starpu_machine_config;
-void
-_starpu_opencl_discover_devices(struct _starpu_machine_config *config);
+void _starpu_opencl_discover_devices(struct _starpu_machine_config *config);
+
+unsigned _starpu_opencl_get_device_count(void);
+void _starpu_opencl_init(void);
+void *_starpu_opencl_worker(void *);
 #else
 #define _starpu_opencl_discover_devices(config) ((void) (config))
 #endif
 
 #ifdef STARPU_USE_OPENCL
-extern
-char *_starpu_opencl_program_dir;
+extern struct _starpu_driver_ops _starpu_driver_opencl_ops;
+extern char *_starpu_opencl_program_dir;
 
-extern
-int _starpu_opencl_init_context(int devid);
+int _starpu_run_opencl(struct _starpu_worker *);
+int _starpu_opencl_driver_init(struct _starpu_worker *);
+int _starpu_opencl_driver_run_once(struct _starpu_worker *);
+int _starpu_opencl_driver_deinit(struct _starpu_worker *);
 
-extern
+int _starpu_opencl_init_context(int devid);
 int _starpu_opencl_deinit_context(int devid);
-#endif
-
-#if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
-extern
-unsigned _starpu_opencl_get_device_count(void);
-#endif
-
-#ifdef STARPU_USE_OPENCL
-extern
 cl_device_type _starpu_opencl_get_device_type(int devid);
+#endif
 
 #if 0
-extern
 cl_int _starpu_opencl_copy_rect_opencl_to_ram(cl_mem buffer, unsigned src_node, void *ptr, unsigned dst_node, const size_t buffer_origin[3], const size_t host_origin[3],
                                               const size_t region[3], size_t buffer_row_pitch, size_t buffer_slice_pitch,
                                               size_t host_row_pitch, size_t host_slice_pitch, cl_event *event);
 
-extern
 cl_int _starpu_opencl_copy_rect_ram_to_opencl(void *ptr, unsigned src_node, cl_mem buffer, unsigned dst_node, const size_t buffer_origin[3], const size_t host_origin[3],
                                               const size_t region[3], size_t buffer_row_pitch, size_t buffer_slice_pitch,
                                               size_t host_row_pitch, size_t host_slice_pitch, cl_event *event);
 #endif
-#endif
-
-#if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
-extern
-void _starpu_opencl_init(void);
-
-extern
-void *_starpu_opencl_worker(void *);
-#endif
 
-#ifdef STARPU_USE_OPENCL
-struct _starpu_worker;
-extern
-int _starpu_run_opencl(struct _starpu_worker *);
-
-extern
-int _starpu_opencl_driver_init(struct _starpu_worker *);
-
-extern
-int _starpu_opencl_driver_run_once(struct _starpu_worker *);
-
-extern
-int _starpu_opencl_driver_deinit(struct _starpu_worker *);
-#endif // STARPU_USE_OPENCL
 #endif //  __DRIVER_OPENCL_H__