瀏覽代碼

Clean up src/core/topology.c.

Add three functions:

_starpu_cpu_discover_devices()
_starpu_cuda_discover_devices()
_starpu_opencl_discover_devices()

This is a rework of a series of patches by Ludovic Stordeur (Ludovic.Stordeur@inria.fr)
Cyril Roelandt 12 年之前
父節點
當前提交
ace934c823

+ 11 - 33
src/core/topology.c

@@ -266,40 +266,10 @@ _starpu_init_topology (struct _starpu_machine_config *config)
 		return;
 
 	topology->nhwcpus = 0;
-#ifdef STARPU_HAVE_HWLOC
-	hwloc_topology_init(&topology->hwtopology);
-	hwloc_topology_load(topology->hwtopology);
-
-	config->cpu_depth = hwloc_get_type_depth (topology->hwtopology,
-						  HWLOC_OBJ_CORE);
-
-	/* Would be very odd */
-	STARPU_ASSERT(config->cpu_depth != HWLOC_TYPE_DEPTH_MULTIPLE);
-
-	if (config->cpu_depth == HWLOC_TYPE_DEPTH_UNKNOWN)
-		/* unknown, using logical procesors as fallback */
-		config->cpu_depth = hwloc_get_type_depth (topology->hwtopology,
-							  HWLOC_OBJ_PU);
-
-	topology->nhwcpus = hwloc_get_nbobjs_by_depth (topology->hwtopology,
-						       config->cpu_depth);
-#elif defined(__MINGW32__) || defined(__CYGWIN__)
-	SYSTEM_INFO sysinfo;
-	GetSystemInfo(&sysinfo);
-	topology->nhwcpus += sysinfo.dwNumberOfProcessors;
-#elif defined(HAVE_SYSCONF)
-	topology->nhwcpus = sysconf(_SC_NPROCESSORS_ONLN);
-#else
-#warning no way to know number of cores, assuming 1
-	topology->nhwcpus = 1;
-#endif
 
-#ifdef STARPU_USE_CUDA
-	config->topology.nhwcudagpus = _starpu_get_cuda_device_count();
-#endif
-#ifdef STARPU_USE_OPENCL
-	config->topology.nhwopenclgpus = _starpu_opencl_get_device_count();
-#endif
+	_starpu_cpu_discover_devices(config);
+	_starpu_cuda_discover_devices(config);
+	_starpu_opencl_discover_devices(config);
 
 	topology_is_initialized = 1;
 }
@@ -439,6 +409,8 @@ _starpu_get_next_bindid (struct _starpu_machine_config *config,
 unsigned
 _starpu_topology_get_nhwcpu (struct _starpu_machine_config *config)
 {
+	_starpu_opencl_init();
+	_starpu_init_cuda();
 	_starpu_init_topology(config);
 
 	return config->topology.nhwcpus;
@@ -455,6 +427,8 @@ _starpu_init_machine_config (struct _starpu_machine_config *config)
 
 	topology->nworkers = 0;
 	topology->ncombinedworkers = 0;
+	_starpu_opencl_init();
+	_starpu_init_cuda();
 	_starpu_init_topology(config);
 
 	_starpu_initialize_workers_bindid(config);
@@ -715,6 +689,8 @@ _starpu_bind_thread_on_cpu (
 #ifdef STARPU_HAVE_HWLOC
 	const struct hwloc_topology_support *support;
 
+	_starpu_opencl_init();
+	_starpu_init_cuda();
 	_starpu_init_topology(config);
 
 	support = hwloc_topology_get_support (config->topology.hwtopology);
@@ -773,6 +749,8 @@ _starpu_bind_thread_on_cpus (
 #ifdef STARPU_HAVE_HWLOC
 	const struct hwloc_topology_support *support;
 
+	_starpu_opencl_init();
+	_starpu_init_cuda();
 	_starpu_init_topology(config);
 
 	support = hwloc_topology_get_support(config->topology.hwtopology);

+ 74 - 0
src/drivers/cpu/driver_cpu.c

@@ -17,8 +17,11 @@
  * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  */
 
+#include <common/config.h>
+
 #include <math.h>
 #include <starpu.h>
+#include <starpu_scheduler.h> /* XXX For starpu_machine_topology */
 #include <starpu_profiling.h>
 #include <drivers/driver_common/driver_common.h>
 #include <common/utils.h>
@@ -26,6 +29,77 @@
 #include "driver_cpu.h"
 #include <core/sched_policy.h>
 
+#ifdef STARPU_HAVE_HWLOC
+#include <hwloc.h>
+#ifndef HWLOC_API_VERSION
+#define HWLOC_OBJ_PU HWLOC_OBJ_PROC
+#endif
+#endif
+
+#ifdef STARPU_HAVE_WINDOWS
+#include <windows.h>
+#endif
+
+#ifdef STARPU_HAVE_HWLOC
+void
+_starpu_cpu_discover_devices(struct _starpu_machine_config *config)
+{
+	/* Discover the CPUs relying on the hwloc interface and fills CONFIG
+	 * accordingly. */
+
+	struct starpu_machine_topology *topology = &config->topology;
+
+	// FIXME: This should already be set.
+	topology->nhwcpus = 0;
+
+	hwloc_topology_init(&topology->hwtopology);
+	hwloc_topology_load(topology->hwtopology);
+
+	config->cpu_depth = hwloc_get_type_depth (topology->hwtopology,
+						  HWLOC_OBJ_CORE);
+
+	/* Would be very odd */
+	STARPU_ASSERT(config->cpu_depth != HWLOC_TYPE_DEPTH_MULTIPLE);
+
+	if (config->cpu_depth == HWLOC_TYPE_DEPTH_UNKNOWN)
+		/* unknown, using logical procesors as fallback */
+		config->cpu_depth = hwloc_get_type_depth(topology->hwtopology,
+							 HWLOC_OBJ_PU);
+
+	topology->nhwcpus = hwloc_get_nbobjs_by_depth (topology->hwtopology,
+						       config->cpu_depth);
+}
+
+#elif defined(HAVE_SYSCONF)
+void
+_starpu_cpu_discover_cpus(struct _starpu_machine_config *config)
+{
+	/* Discover the CPUs relying on the sysconf(3) function and fills
+	 * CONFIG accordingly. */
+
+	config->topology->nhwcpus = sysconf(_SC_NPROCESSORS_ONLN);
+}
+
+#elif defined(__MINGW32__) || defined(__CYGWIN__)
+void
+_starpu_cpu_discover_cpus(struct _starpu_machine_config *config)
+{
+	/* Discover the CPUs on Cygwin and MinGW systems. */
+
+	SYSTEM_INFO sysinfo;
+	GetSystemInfo(&sysinfo);
+	config->topology->nhwcpus += sysinfo.dwNumberOfProcessors;
+}
+#else
+#warning no way to know number of cores, assuming 1
+void
+_starpu_cpu_discover_cpus(struct _starpu_machine_config *config)
+{
+	config->topology->nhwcpus = 1;
+}
+#endif
+
+
 /* Actually launch the job on a cpu worker.
  * Handle binding CPUs on cores.
  * In the case of a combined worker WORKER_TASK != J->TASK */

+ 1 - 0
src/drivers/cpu/driver_cpu.h

@@ -33,5 +33,6 @@ int _starpu_run_cpu(struct starpu_driver *);
 int _starpu_cpu_driver_init(struct starpu_driver *);
 int _starpu_cpu_driver_run_once(struct starpu_driver *);
 int _starpu_cpu_driver_deinit(struct starpu_driver *);
+void _starpu_cpu_discover_devices(struct _starpu_machine_config *config);
 
 #endif //  __DRIVER_CPU_H__

+ 14 - 0
src/drivers/cuda/driver_cuda.c

@@ -42,6 +42,20 @@ static struct cudaDeviceProp props[STARPU_MAXCUDADEVS];
  * is launched. */
 static char *wasted_memory[STARPU_NMAXWORKERS];
 
+void
+_starpu_cuda_discover_devices (struct _starpu_machine_config *config)
+{
+	/* Discover the number of CUDA devices. Fill the result in CONFIG. */
+
+	int cnt;
+	cudaError_t cures;
+
+	cures = cudaGetDeviceCount (&cnt);
+	if (STARPU_UNLIKELY(cures != cudaSuccess))
+		STARPU_CUDA_REPORT_ERROR(cures);
+	config->topology.nhwcudagpus = cnt;
+}
+
 static void limit_gpu_mem_if_needed(int devid)
 {
 	cudaError_t cures;

+ 3 - 0
src/drivers/cuda/driver_cuda.h

@@ -41,6 +41,7 @@
 unsigned _starpu_get_cuda_device_count(void);
 
 #ifdef STARPU_USE_CUDA
+void _starpu_cuda_discover_devices (struct _starpu_machine_config *);
 void _starpu_init_cuda(void);
 void *_starpu_cuda_worker(void *);
 cudaStream_t starpu_cuda_get_local_transfer_stream(void);
@@ -49,6 +50,8 @@ int _starpu_run_cuda(struct starpu_driver *);
 int _starpu_cuda_driver_init(struct starpu_driver *);
 int _starpu_cuda_driver_run_once(struct starpu_driver *);
 int _starpu_cuda_driver_deinit(struct starpu_driver *);
+#else
+#  define _starpu_cuda_discover_devices(config) ((void) config)
 #endif
 
 #endif //  __DRIVER_CUDA_H__

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

@@ -38,6 +38,16 @@ static cl_command_queue transfer_queues[STARPU_MAXOPENCLDEVS];
 static cl_uint nb_devices = -1;
 static int init_done = 0;
 
+void
+_starpu_opencl_discover_devices(struct _starpu_machine_config *config)
+{
+	/* Discover the number of OpenCL devices. Fill the result in CONFIG. */
+	/* As OpenCL must have been initialized before calling this function,
+	 * `nb_device' is ensured to be correctly set. */
+	STARPU_ASSERT(init_done == 1);
+	config->topology.nhwopenclgpus = nb_devices;
+}
+
 /* In case we want to cap the amount of memory available on the GPUs by the
  * mean of the STARPU_LIMIT_GPU_MEM, we allocate a big buffer when the driver
  * is launched. */

+ 5 - 1
src/drivers/opencl/driver_opencl.h

@@ -30,6 +30,9 @@
 #include <CL/cl.h>
 #endif
 
+struct _starpu_machine_config;
+void
+_starpu_opencl_discover_devices(struct _starpu_machine_config *config);
 extern
 char *_starpu_opencl_program_dir;
 
@@ -74,6 +77,7 @@ int _starpu_opencl_driver_run_once(struct starpu_driver *);
 
 extern
 int _starpu_opencl_driver_deinit(struct starpu_driver *);
-
+#else
+#define _starpu_opencl_discover_devices(config) ((void) (config))
 #endif // STARPU_USE_OPENCL
 #endif //  __DRIVER_OPENCL_H__