-1

I want to redefine malloc() and free() in my code, but when I run, two errors appear:

allowing all exceptions is incompatible with previous function "malloc";
allowing all exceptions is incompatible with previous function "free";

Then I search for this error, it seems CUDA doesn't allow us to redefine libary function, is this true? If we can't redefine those functions, how can I resolve the error?

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
shrimp
  • 1
  • 2
  • Assuming that creating a wrapper for those functions is not enough for you, take a look at [this](http://stackoverflow.com/questions/262439/create-a-wrapper-function-for-malloc-and-free-in-c) question. – srodrb Nov 07 '16 at 11:59
  • Also, [this](http://codinggorilla.domemtech.com/?p=504) post describes how to implement a hook for cudaMalloc in detail. – srodrb Nov 07 '16 at 12:06
  • 2
    Is this basically your earlier question again? http://stackoverflow.com/questions/39565884/error-allowing-all-exceptions-is-incompatible-with-previous-function-malloc – talonmies Nov 07 '16 at 12:54

1 Answers1

1

The very short answer is that you cannot.

malloc is fundamentally a C++ standard library function which the CUDA toolchain internally overloads with a device hook in device code. Attempting to define your own device version of malloc or free can and will break the toolchain's internals. Exactly how depends on platform and compiler.

In your previous question on this, you had code like this:

__device__ void* malloc(size_t)
{ return theHeap.alloc(t); }
__device__ void free(void* p)
{ the Heap.dealloc(p); }

Because of existing standard library requirements, malloc and free must be defined as __device__ __host__ at global namespace scope. It is illegal in CUDA to have separate __device__ and __host__definitions of the same function. You could probably get around this restriction by using a private namespace for the custom allocator, or using different function names. But don't try and redefine anything from the standard library in device or host code. It will break things.

talonmies
  • 70,661
  • 34
  • 192
  • 269