Browse Source

memory allocation: update memory allocation flag settings. We do not
want a global flag but each call to starpu_malloc to define its
own flags.

New function starpu_malloc_flag to define a memory allocation with
constraints based on the following values:
- STARPU_MALLOC_PINNED specifies memory should be pinned
- STARPU_MALLOC_COUNT specifies the memory allocation should be in
the limits defined by the environment variables
STARPU_LIMIT_xxx. When no memory is left, starpu_malloc_flag
tries to reclaim memory from StarPU and returns -ENOMEM on
failure.

Nathalie Furmento 12 years ago
parent
commit
eeab601075
4 changed files with 26 additions and 40 deletions
  1. 4 4
      ChangeLog
  2. 9 14
      doc/chapters/basic-api.texi
  3. 2 3
      include/starpu_stdlib.h
  4. 11 19
      src/datawizard/malloc.c

+ 4 - 4
ChangeLog

@@ -94,12 +94,12 @@ New features:
     STARPU_LIMIT_OPENCL_devid_MEM to limit memory per specific device
   * Introduce new variable STARPU_LIMIT_CPU_MEM to limit memory for
     the CPU devices
-  * New functions starpu_malloc_set_flags and starpu_malloc_get_flags
-    to define flags to be used by the memory allocator.
+  * New function starpu_malloc_flag to define a memory allocation with
+    constraints based on the following values:
     - STARPU_MALLOC_PINNED specifies memory should be pinned
-    - STARPU_MALLOC_COUNT specifies memory allocations should be in
+    - STARPU_MALLOC_COUNT specifies the memory allocation should be in
       the limits defined by the environment variables STARPU_LIMIT_xxx
-      (see above). When no memory is left, starpu_malloc tries
+      (see above). When no memory is left, starpu_malloc_flag tries
       to reclaim memory from StarPU and returns -ENOMEM on failure.
   * Define new function starpu_free_count to
     be used for freeing memory with a specified size. This is needed

+ 9 - 14
doc/chapters/basic-api.texi

@@ -240,13 +240,13 @@ are disabled.
 @section Standard memory library
 
 @defmac STARPU_MALLOC_PINNED
-Value passed to the function @code{starpu_malloc_set_flags} to
-indicate further memory allocations should be pinned
+Value passed to the function @code{starpu_malloc_flags} to
+indicate the memory allocation should be pinned.
 @end defmac
 
 @defmac STARPU_MALLOC_COUNT
-Value passed to the function @code{starpu_malloc_set_flags} to
-indicate further memory allocations should be in the limit defined by
+Value passed to the function @code{starpu_malloc_flags} to
+indicate the memory allocation should be in the limit defined by
 the environment variables @code{STARPU_LIMIT_CUDA_devid_MEM},
 @code{STARPU_LIMIT_CUDA_MEM}, @code{STARPU_LIMIT_OPENCL_devid_MEM},
 @code{STARPU_LIMIT_OPENCL_MEM} and @code{STARPU_LIMIT_CPU_MEM}
@@ -255,13 +255,9 @@ memory from StarPU. Memory allocated this way needs to be freed thanks to the
 @code{starpu_free_count} function.
 @end defmac
 
-@deftypefun int starpu_malloc_set_flags (int @var{flags})
-Set the current flags used by the memory allocator. @var{flag} must be
- a combinaison of the values defined above.
-@end deftypefun
-
-@deftypefun int starpu_malloc_get_flags ()
-Return the current value of the flags used by the memory allocator.
+@deftypefun int starpu_malloc_flags (void **@var{A}, size_t @var{dim}, int @var{flags})
+Performs a memory allocation based on the constraints defined by the
+given @var{flag}.
 @end deftypefun
 
 @deftypefun void starpu_malloc_set_align (size_t @var{align})
@@ -278,14 +274,13 @@ be freed thanks to the @code{starpu_free} function.
 @end deftypefun
 
 @deftypefun int starpu_free (void *@var{A})
-This function frees memory which has previously allocated with
+This function frees memory which has previously been allocated with
 @code{starpu_malloc}.
 @end deftypefun
 
 @deftypefun int starpu_free_count (void *@var{A}, size_t @var{dim})
 This function frees memory by specifying its size. It should be used
-to free memory which was allocated following a call to
-@code{starpu_malloc_set_flags} with the value
+to free memory which was allocated by @code{starpu_malloc_flags} with the value
 @code{STARPU_MALLOC_COUNT}.
 @end deftypefun
 

+ 2 - 3
include/starpu_stdlib.h

@@ -28,13 +28,12 @@ extern "C"
 #define STARPU_MALLOC_PINNED	((1ULL)<<1)
 #define STARPU_MALLOC_COUNT	((1ULL)<<3)
 
-int starpu_malloc_set_flags(int flags);
-int starpu_malloc_get_flags();
-
 void starpu_malloc_set_align(size_t align);
 int starpu_malloc(void **A, size_t dim);
 int starpu_free(void *A);
 
+int starpu_malloc_flags(void **A, size_t dim, int flags);
+
 int starpu_free_count(void *A, size_t dim);
 
 #ifdef __cplusplus

+ 11 - 19
src/datawizard/malloc.c

@@ -24,18 +24,6 @@
 #include <datawizard/memory_manager.h>
 
 static size_t _malloc_align = sizeof(void*);
-static int _malloc_flags = 0;
-
-int starpu_malloc_set_flags(int flags)
-{
-	_malloc_flags = flags;
-	return _malloc_flags;
-}
-
-int starpu_malloc_get_flags()
-{
-	return _malloc_flags;
-}
 
 void starpu_malloc_set_align(size_t align)
 {
@@ -94,16 +82,12 @@ static struct starpu_codelet malloc_pinned_cl =
 #endif
 
 #ifdef STARPU_DEVEL
-#warning starpu_malloc should check if STARPU_MALLOC_PINNED is set, but that is going to break the compatibility with old code as memory which used to be pinned will no longer be (unless we force by default the flag STARPU_MALLOC_PINNED)
+#warning starpu_malloc_flags should check if STARPU_MALLOC_PINNED is set, but that is going to break the compatibility with old code as memory which used to be pinned will no longer be (unless we force by default the flag STARPU_MALLOC_PINNED)
 #endif
 
-int starpu_malloc(void **A, size_t dim)
+int starpu_malloc_flags(void **A, size_t dim, int flags)
 {
-	int ret=0;
-
-	STARPU_ASSERT(A);
-
-	if (_malloc_flags & STARPU_MALLOC_COUNT)
+	if (flags & STARPU_MALLOC_COUNT)
 	{
 		if (_starpu_memory_manager_can_allocate_size(dim, 0) == 0)
 		{
@@ -121,6 +105,14 @@ int starpu_malloc(void **A, size_t dim)
 			}
 		}
 	}
+	return starpu_malloc(A, dim);
+}
+
+int starpu_malloc(void **A, size_t dim)
+{
+	int ret=0;
+
+	STARPU_ASSERT(A);
 
 #ifndef STARPU_SIMGRID
 	if (_starpu_can_submit_cuda_task())