Explorar el Código

datawizard: starpu_malloc and starpu_free no longer count the size of
the allocated memory. Introduce 2 new functions starpu_malloc_count
and starpu_free_count which do count.

Nathalie Furmento hace 12 años
padre
commit
30c203aa9f
Se han modificado 4 ficheros con 50 adiciones y 56 borrados
  1. 4 0
      include/starpu_data_interfaces.h
  2. 2 4
      include/starpu_stdlib.h
  3. 28 41
      src/datawizard/malloc.c
  4. 16 11
      tests/datawizard/allocate.c

+ 4 - 0
include/starpu_data_interfaces.h

@@ -78,6 +78,10 @@ struct starpu_data_copy_methods
 };
 
 int starpu_interface_copy(uintptr_t src, size_t src_offset, unsigned src_node, uintptr_t dst, size_t dst_offset, unsigned dst_node, size_t size, void *async_data);
+/* Allocate SIZE bytes on node NODE */
+uintptr_t starpu_malloc_on_node(unsigned dst_node, size_t size);
+/* Free ADDR on node NODE */
+void starpu_free_on_node(unsigned dst_node, uintptr_t addr, size_t size);
 
 enum starpu_data_interface_id
 {

+ 2 - 4
include/starpu_stdlib.h

@@ -29,10 +29,8 @@ void starpu_malloc_set_align(size_t align);
 int starpu_malloc(void **A, size_t dim);
 int starpu_free(void *A);
 
-/* Allocate SIZE bytes on node NODE */
-uintptr_t starpu_malloc_on_node(unsigned dst_node, size_t size);
-/* Free ADDR on node NODE */
-void starpu_free_on_node(unsigned dst_node, uintptr_t addr, size_t size);
+int starpu_malloc_count(void **A, size_t dim);
+int starpu_free_count(void *A, size_t dim);
 
 #ifdef __cplusplus
 }

+ 28 - 41
src/datawizard/malloc.c

@@ -22,19 +22,9 @@
 #include <starpu.h>
 #include <drivers/opencl/driver_opencl.h>
 #include <datawizard/memory_manager.h>
-#include <common/uthash.h>
 
 static size_t malloc_align = sizeof(void*);
 
-struct _starpu_malloc_entry
-{
-	UT_hash_handle hh;
-	void *data;
-        size_t size;
-};
-
-static struct _starpu_malloc_entry *_allocated_data = NULL;
-
 void starpu_malloc_set_align(size_t align)
 {
 	STARPU_ASSERT_MSG(!(align & (align - 1)), "Alignment given to starpu_malloc_set_align must be a power of two");
@@ -100,22 +90,6 @@ int starpu_malloc(void **A, size_t dim)
 
 	STARPU_ASSERT(A);
 
-	if (_starpu_memory_manager_can_allocate_size(dim, 0) == 0)
-	{
-		size_t freed;
-		size_t reclaim = 2 * dim;
-		_STARPU_DEBUG("There is not enough memory left, we are going to reclaim %ld\n", reclaim);
-		_STARPU_TRACE_START_MEMRECLAIM(0);
-		freed = _starpu_memory_reclaim_generic(0, 0, reclaim);
-		_STARPU_TRACE_END_MEMRECLAIM(0);
-		if (freed < dim)
-		{
-			// We could not reclaim enough memory
-			*A = NULL;
-			return -ENOMEM;
-		}
-	}
-
 #ifndef STARPU_SIMGRID
 	if (_starpu_can_submit_cuda_task())
 	{
@@ -202,15 +176,6 @@ int starpu_malloc(void **A, size_t dim)
 	if (ret == 0)
 	{
 		STARPU_ASSERT(*A);
-
-		// Store the allocated pointer along with its size in the hashtable
-		struct _starpu_malloc_entry *entry;
-		entry = (struct _starpu_malloc_entry *) malloc(sizeof(*entry));
-		STARPU_ASSERT(entry != NULL);
-		entry->data = *A;
-		entry->size = dim;
-
-		HASH_ADD_PTR(_allocated_data, data, entry);
 	}
 
 	return ret;
@@ -258,12 +223,6 @@ int starpu_free(void *A)
 	if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
 		return -EDEADLK;
 
-	// Look for the pointer in the hashtable to find out its size
-	struct _starpu_malloc_entry *entry;
-	HASH_FIND_PTR(_allocated_data, &A, entry);
-	STARPU_ASSERT_MSG(entry, "Pointer %p was not allocated with starpu_malloc\n", A);
-	_starpu_memory_manager_deallocate_size(entry->size, 0);
-
 #ifndef STARPU_SIMGRID
 #ifdef STARPU_USE_CUDA
 	if (_starpu_can_submit_cuda_task())
@@ -327,6 +286,34 @@ int starpu_free(void *A)
 	return 0;
 }
 
+
+int starpu_malloc_count(void **A, size_t dim)
+{
+	if (_starpu_memory_manager_can_allocate_size(dim, 0) == 0)
+	{
+		size_t freed;
+		size_t reclaim = 2 * dim;
+		_STARPU_DEBUG("There is not enough memory left, we are going to reclaim %ld\n", reclaim);
+		_STARPU_TRACE_START_MEMRECLAIM(0);
+		freed = _starpu_memory_reclaim_generic(0, 0, reclaim);
+		_STARPU_TRACE_END_MEMRECLAIM(0);
+		if (freed < dim)
+		{
+			// We could not reclaim enough memory
+			*A = NULL;
+			return -ENOMEM;
+		}
+	}
+
+	return starpu_malloc(A, dim);
+}
+
+int starpu_free_count(void *A, size_t dim)
+{
+	_starpu_memory_manager_deallocate_size(dim, 0);
+	starpu_free(A);
+}
+
 #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;

+ 16 - 11
tests/datawizard/allocate.c

@@ -53,27 +53,32 @@ int main(int argc, char **argv)
 	STARPU_CHECK_RETURN_VALUE_IS((int)global_size, 1*1024*1024, "get_global_memory_size");
 	FPRINTF(stderr, "Available memory size on node 0: %ld\n", global_size);
 
-	ret = starpu_malloc((void **)&buffer, 1);
-	STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
+	ret = starpu_malloc_count((void **)&buffer, 1);
+	STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc_count");
 	FPRINTF(stderr, "Allocation succesfull for 1 b\n");
 
-	ret = starpu_malloc((void **)&buffer2, 1*1024*512);
-	STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
+	ret = starpu_malloc_count((void **)&buffer2, 1*1024*512);
+	STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc_count");
 	FPRINTF(stderr, "Allocation succesfull for %d b\n", 1*1024*512);
 
-	ret = starpu_malloc((void **)&buffer3, 1*1024*512);
-	STARPU_CHECK_RETURN_VALUE_IS(ret, -ENOMEM, "starpu_malloc");
+	ret = starpu_malloc_count((void **)&buffer3, 1*1024*512);
+	STARPU_CHECK_RETURN_VALUE_IS(ret, -ENOMEM, "starpu_malloc_count");
 	FPRINTF(stderr, "Allocation failed for %d b\n", 1*1024*512);
 
-	starpu_free(buffer2);
-	FPRINTF(stderr, "Freeing %d b\n", 1*1024*512);
-
 	ret = starpu_malloc((void **)&buffer3, 1*1024*512);
 	STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
+	FPRINTF(stderr, "Allocation successful for %d b\n", 1*1024*512);
+	starpu_free(buffer3);
+
+	starpu_free_count(buffer2, 1*1024*512);
+	FPRINTF(stderr, "Freeing %d b\n", 1*1024*512);
+
+	ret = starpu_malloc_count((void **)&buffer3, 1*1024*512);
+	STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc_count");
 	FPRINTF(stderr, "Allocation succesfull for %d b\n", 1*1024*512);
 
-	starpu_free(buffer3);
-	starpu_free(buffer);
+	starpu_free_count(buffer3, 1*1024*512);
+	starpu_free_count(buffer, 1);
 
 	starpu_shutdown();
 	return 0;