Skip to content

Commit e3ccc33

Browse files
authored
Merge pull request #3 from MP-Gadget/freecuda
Implement freeing and reallocing cuda memory
2 parents ee60387 + 84b1e84 commit e3ccc33

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

libgadget/utils/memory.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ allocator_reset(Allocator * alloc, int zero)
9393
AllocatorIter iter[1];
9494
for(allocator_iter_start(iter, alloc); !allocator_iter_ended(iter); allocator_iter_next(iter))
9595
{
96+
#ifdef USE_CUDA
97+
cudaFree(iter->ptr - ALIGNMENT);
98+
#else
9699
free(iter->ptr - ALIGNMENT);
100+
#endif
97101
}
98102
}
99103
alloc->refcount = 1;
@@ -332,7 +336,19 @@ allocator_realloc_int(Allocator * alloc, void * ptr, const size_t new_size, cons
332336
}
333337

334338
if(alloc->use_malloc) {
335-
struct BlockHeader * header2 = (struct BlockHeader *) realloc(header, new_size + ALIGNMENT);
339+
struct BlockHeader * header2;
340+
#ifdef USE_CUDA
341+
if (cudaMallocManaged(&header2, new_size + ALIGNMENT, cudaMemAttachGlobal) != cudaSuccess) {
342+
endrun(1, "Failed to allocate %lu bytes for %s\n", new_size, header->name);
343+
}
344+
// Copy old data to the new block (don't forget the header)
345+
memcpy(header2, header, ALIGNMENT + (new_size < header->request_size ? new_size : header->request_size));
346+
// Free the old block
347+
cudaFree(header);
348+
#else
349+
header2 = (struct BlockHeader *) realloc(header, new_size + ALIGNMENT);
350+
#endif
351+
// Update header pointer in the new block
336352
header2->ptr = (char*) header2 + ALIGNMENT;
337353
header2->request_size = new_size;
338354
/* update record */
@@ -407,7 +423,11 @@ allocator_dealloc (Allocator * alloc, void * ptr)
407423
}
408424

409425
if(alloc->use_malloc) {
426+
#ifdef USE_CUDA
427+
cudaFree(header);
428+
#else
410429
free(header);
430+
#endif
411431
}
412432

413433
/* remove the link to the memory. */

0 commit comments

Comments
 (0)