Browse Source

memory manager: simplify management of global memory size

 - Function void _starpu_memory_manager_set_global_memory_size(unsigned node, size_t size);
   is called by the driver on initialisation to indicate their global memory size

 - Function size_t _starpu_memory_manager_get_global_memory_size(unsigned node);
   is called to find out how much global memory a node has
Nathalie Furmento 12 years ago
parent
commit
d0591ff302

+ 0 - 8
doc/chapters/basic-api.texi

@@ -2501,10 +2501,6 @@ This function returns a pointer to device properties for worker @var{workerid}
 (assumed to be a CUDA worker).
 @end deftypefun
 
-@deftypefun size_t starpu_cuda_get_global_mem_size (unsigned @var{devid})
-Return the size of the global memory of CUDA device @var{devid}.
-@end deftypefun
-
 @deftypefun void starpu_cuda_report_error ({const char *}@var{func}, {const char *}@var{file}, int @var{line}, cudaError_t @var{status})
 Report a CUDA error.
 @end deftypefun
@@ -2572,10 +2568,6 @@ OpenCL as shown in @ref{Full source code for the 'Scaling a Vector' example}.
 @node Writing OpenCL kernels
 @subsection Writing OpenCL kernels
 
-@deftypefun size_t starpu_opencl_get_global_mem_size (int @var{devid})
-Return the size of global device memory in bytes.
-@end deftypefun
-
 @deftypefun void starpu_opencl_get_context (int @var{devid}, {cl_context *}@var{context})
 Places the OpenCL context of the device designated by @var{devid} into @var{context}.
 @end deftypefun

+ 1 - 2
include/starpu_cuda.h

@@ -1,7 +1,7 @@
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
  *
  * Copyright (C) 2010-2012  Université de Bordeaux 1
- * Copyright (C) 2010, 2011, 2012  Centre National de la Recherche Scientifique
+ * Copyright (C) 2010, 2011, 2012, 2013  Centre National de la Recherche Scientifique
  *
  * 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
@@ -39,7 +39,6 @@ void starpu_cuda_report_error(const char *func, const char *file, int line, cuda
 #define STARPU_CUDA_REPORT_ERROR(status) \
 	starpu_cuda_report_error(__starpu_func__, __FILE__, __LINE__, status)
 
-size_t starpu_cuda_get_global_mem_size(unsigned devid);
 cudaStream_t starpu_cuda_get_local_stream(void);
 
 const struct cudaDeviceProp *starpu_cuda_get_device_properties(unsigned workerid);

+ 1 - 2
include/starpu_opencl.h

@@ -1,7 +1,7 @@
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
  *
  * Copyright (C) 2010-2013  Université de Bordeaux 1
- * Copyright (C) 2010, 2011, 2012  Centre National de la Recherche Scientifique
+ * Copyright (C) 2010, 2011, 2012, 2013  Centre National de la Recherche Scientifique
  *
  * 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
@@ -53,7 +53,6 @@ struct starpu_opencl_program
 	cl_program programs[STARPU_MAXOPENCLDEVS];
 };
 
-size_t starpu_opencl_get_global_mem_size(int devid);
 void starpu_opencl_get_context(int devid, cl_context *context);
 void starpu_opencl_get_device(int devid, cl_device_id *device);
 void starpu_opencl_get_queue(int devid, cl_command_queue *queue);

+ 1 - 37
src/datawizard/memalloc.c

@@ -723,42 +723,6 @@ void _starpu_request_mem_chunk_removal(starpu_data_handle_t handle, unsigned nod
 	_STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
 }
 
-static size_t _starpu_get_global_mem_size(int dst_node)
-{
-	enum starpu_node_kind kind = starpu_node_get_kind(dst_node);
-	size_t global_mem_size;
-
-	switch(kind)
-	{
-		case STARPU_CPU_RAM:
-		{
-			/* We should probably never get here : if there is no
- 			 * space left in RAM, the operating system should swap
-			 * to disk for us. */
-			STARPU_ABORT();
-		}
-#ifdef STARPU_USE_CUDA
-		case STARPU_CUDA_RAM:
-		{
-			int devid = _starpu_memory_node_get_devid(dst_node);
-			global_mem_size = starpu_cuda_get_global_mem_size(devid);
-			break;
-		}
-#endif
-#ifdef STARPU_USE_OPENCL
-		case STARPU_OPENCL_RAM:
-		{
-			int devid = _starpu_memory_node_get_devid(dst_node);
-			global_mem_size = starpu_opencl_get_global_mem_size(devid);
-			break;
-		}
-#endif
-		default:
-			STARPU_ABORT();
-	}
-	return global_mem_size;
-}
-
 #ifdef STARPU_SIMGRID
 static _starpu_pthread_mutex_t cuda_alloc_mutex = _STARPU_PTHREAD_MUTEX_INITIALIZER;
 static _starpu_pthread_mutex_t opencl_alloc_mutex = _STARPU_PTHREAD_MUTEX_INITIALIZER;
@@ -964,7 +928,7 @@ static ssize_t _starpu_allocate_interface(starpu_data_handle_t handle, struct _s
 
 		if (allocated_memory == -ENOMEM)
 		{
-			size_t reclaim = 0.25*_starpu_get_global_mem_size(dst_node);
+			size_t reclaim = 0.25*_starpu_memory_manager_get_global_memory_size(dst_node);
 			size_t handle_size = handle->ops->get_size(handle);
 			if (starpu_memstrategy_data_size_coefficient*handle_size > reclaim)
 				reclaim = starpu_memstrategy_data_size_coefficient*handle_size;

+ 9 - 35
src/datawizard/memory_manager.c

@@ -15,10 +15,8 @@
  */
 
 #include <starpu.h>
-#include <common/config.h>
+#include <common/utils.h>
 #include <datawizard/memory_manager.h>
-#include <starpu_cuda.h>
-#include <starpu_opencl.h>
 
 static size_t global_size[STARPU_MAXNODES];
 static size_t used_size[STARPU_MAXNODES];
@@ -35,42 +33,18 @@ int _starpu_memory_manager_init()
 	return 0;
 }
 
-void _starpu_memory_manager_init_global_memory(unsigned node, enum starpu_archtype type, int devid, struct _starpu_machine_config *config)
+void _starpu_memory_manager_set_global_memory_size(unsigned node, size_t size)
 {
-	switch (type)
-	{
-#ifdef STARPU_USE_CPU
-	case STARPU_CPU_WORKER:
-	{
-		/* FIXME: when we have NUMA support, properly turn node number into NUMA node number */
-		global_size[node] = _starpu_cpu_get_global_mem_size(node, config);
-		break;
-	}
-#endif
-
-#ifdef STARPU_USE_CUDA
-	case STARPU_CUDA_WORKER:
-	{
-		global_size[node] = starpu_cuda_get_global_mem_size(devid);
-		break;
-	}
-#endif /* STARPU_USE_CUDA */
-
-#ifdef STARPU_USE_OPENCL
-	case STARPU_OPENCL_WORKER:
-	{
-		global_size[node] = starpu_opencl_get_global_mem_size(devid);
-		break;
-	}
-#endif /* STARPU_USE_OPENCL */
-
-	default:
-		STARPU_ABORT();
-	}
+	global_size[node] = size;
+	_STARPU_DEBUG("Global size for node %d is %ld\n", node, (long)global_size[node]);
+}
 
-	_STARPU_DEBUG("Global size for node %d (%d) is %ld\n", node, type, (long)global_size[node]);
+size_t _starpu_memory_manager_get_global_memory_size(unsigned node)
+{
+	return global_size[node];
 }
 
+
 int _starpu_memory_manager_can_allocate_size(size_t size, unsigned node)
 {
 	if (global_size[node] == 0)

+ 8 - 4
src/datawizard/memory_manager.h

@@ -18,8 +18,6 @@
 #define __MEMORY_MANAGER_H__
 
 #include <starpu.h>
-#include <common/config.h>
-#include <core/workers.h>
 
 /**
  * Initialises the memory manager
@@ -27,10 +25,16 @@
 int _starpu_memory_manager_init();
 
 /**
- * Initialises the global memory for the given node
+ * Initialises the global memory size for the given node
  *
  */
-void _starpu_memory_manager_init_global_memory(unsigned node, enum starpu_archtype type, int devid, struct _starpu_machine_config *config);
+void _starpu_memory_manager_set_global_memory_size(unsigned node, size_t size);
+
+/**
+ * Gets the global memory size for the given node
+ *
+ */
+size_t _starpu_memory_manager_get_global_memory_size(unsigned node);
 
 /**
  * Indicates if memory can be allocated on the given node

+ 22 - 21
src/drivers/cpu/driver_cpu.c

@@ -189,6 +189,26 @@ _starpu_get_worker_from_driver(struct starpu_driver *d)
 	return _starpu_get_worker_struct(n);
 }
 
+static size_t _starpu_cpu_get_global_mem_size(int devid, struct _starpu_machine_config *config)
+{
+#if defined(STARPU_HAVE_HWLOC)
+        int depth_node;
+	struct starpu_machine_topology *topology = &config->topology;
+        depth_node = hwloc_get_type_depth(topology->hwtopology, HWLOC_OBJ_NODE);
+
+	if (depth_node == HWLOC_TYPE_DEPTH_UNKNOWN)
+	     return hwloc_get_root_obj(topology->hwtopology)->memory.total_memory;
+	else
+	     return hwloc_get_obj_by_depth(topology->hwtopology, depth_node, devid)->memory.local_memory;
+
+#else /* STARPU_HAVE_HWLOC */
+#ifdef STARPU_DEVEL
+#  warning use sysinfo when available to get global size
+#endif
+	return 0;
+#endif
+}
+
 int _starpu_cpu_driver_init(struct starpu_driver *d)
 {
 	struct _starpu_worker *cpu_worker;
@@ -198,7 +218,8 @@ int _starpu_cpu_driver_init(struct starpu_driver *d)
 	int devid = cpu_worker->devid;
 
 	_starpu_worker_init(cpu_worker, _STARPU_FUT_CPU_KEY);
-	_starpu_memory_manager_init_global_memory(cpu_worker->memory_node, STARPU_CPU_WORKER, cpu_worker->devid, cpu_worker->config);
+	/* FIXME: when we have NUMA support, properly turn node number into NUMA node number */
+	_starpu_memory_manager_set_global_memory_size(cpu_worker->memory_node, _starpu_cpu_get_global_mem_size(cpu_worker->memory_node, cpu_worker->config));
 
 	snprintf(cpu_worker->name, sizeof(cpu_worker->name), "CPU %d", devid);
 	snprintf(cpu_worker->short_name, sizeof(cpu_worker->short_name), "CPU %d", devid);
@@ -361,23 +382,3 @@ int _starpu_run_cpu(struct starpu_driver *d)
 
 	return 0;
 }
-
-size_t _starpu_cpu_get_global_mem_size(int devid, struct _starpu_machine_config *config)
-{
-#if defined(STARPU_HAVE_HWLOC)
-        int depth_node;
-	struct starpu_machine_topology *topology = &config->topology;
-        depth_node = hwloc_get_type_depth(topology->hwtopology, HWLOC_OBJ_NODE);
-
-	if (depth_node == HWLOC_TYPE_DEPTH_UNKNOWN)
-	     return hwloc_get_root_obj(topology->hwtopology)->memory.total_memory;
-	else
-	     return hwloc_get_obj_by_depth(topology->hwtopology, depth_node, devid)->memory.local_memory;
-
-#else /* STARPU_HAVE_HWLOC */
-#ifdef STARPU_DEVEL
-#  warning use sysinfo when available to get global size
-#endif
-	return 0;
-#endif
-}

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

@@ -42,6 +42,4 @@ void _starpu_cpu_discover_devices(struct _starpu_machine_config *config);
 } while (0)
 #endif /* !STARPU_USE_CPU */
 
-size_t _starpu_cpu_get_global_mem_size(int devid, struct _starpu_machine_config *config);
-
 #endif //  __DRIVER_CPU_H__

+ 3 - 3
src/drivers/cuda/driver_cuda.c

@@ -68,7 +68,7 @@ _starpu_cuda_discover_devices (struct _starpu_machine_config *config)
 /* In case we want to cap the amount of memory available on the GPUs by the
  * mean of the STARPU_LIMIT_CUDA_MEM, we decrease the value of
  * props[devid].totalGlobalMem which is the value returned by
- * starpu_cuda_get_global_mem_size() to indicate how much memory can
+ * _starpu_cuda_get_global_mem_size() to indicate how much memory can
  * be allocated on the device
  */
 static void _starpu_cuda_limit_gpu_mem_if_needed(unsigned devid)
@@ -100,7 +100,7 @@ static void _starpu_cuda_limit_gpu_mem_if_needed(unsigned devid)
 			(size_t)(totalGlobalMem - to_waste)/(1024*1024));
 }
 
-size_t starpu_cuda_get_global_mem_size(unsigned devid)
+static size_t _starpu_cuda_get_global_mem_size(unsigned devid)
 {
 	return (size_t)props[devid].totalGlobalMem;
 }
@@ -389,7 +389,7 @@ int _starpu_cuda_driver_init(struct starpu_driver *d)
 #ifdef STARPU_USE_CUDA
 	_starpu_cuda_limit_gpu_mem_if_needed(devid);
 #endif
-	_starpu_memory_manager_init_global_memory(args->memory_node, STARPU_CUDA_WORKER, args->devid, args->config);
+	_starpu_memory_manager_set_global_memory_size(args->memory_node, _starpu_cuda_get_global_mem_size(devid));
 
 	/* one more time to avoid hacks from third party lib :) */
 	_starpu_bind_thread_on_cpu(args->config, args->bindid);

+ 2 - 2
src/drivers/opencl/driver_opencl.c

@@ -112,7 +112,7 @@ static void unlimit_gpu_mem_if_needed(int devid)
 }
 #endif
 
-size_t starpu_opencl_get_global_mem_size(int devid)
+static size_t _starpu_opencl_get_global_mem_size(int devid)
 {
 	cl_int err;
 	cl_ulong totalGlobalMem;
@@ -631,7 +631,7 @@ int _starpu_opencl_driver_init(struct starpu_driver *d)
 	/* one more time to avoid hacks from third party lib :) */
 	_starpu_bind_thread_on_cpu(args->config, args->bindid);
 
-	_starpu_memory_manager_init_global_memory(args->memory_node, STARPU_OPENCL_WORKER, args->devid, args->config);
+	_starpu_memory_manager_set_global_memory_size(args->memory_node, _starpu_opencl_get_global_mem_size(devid));
 
 	args->status = STATUS_UNKNOWN;