kokosing
kokosing

Reputation: 5601

CUDA fails when freeing memory after kernel execution error

I have found weird behavior of CUDA. After I got segfault in my kernel, I was trying to free prior allocated memory and it fails. Does CUDA do it automatically? I mean, does CUDA free memory after segfault during kernel executions?

Here is code to reproduce my situation. I have tested that on CUDA 4.0, 4.0rc2 and 3.2

#include <cassert>

__global__ void segfault(){
    int* null = NULL;
    *null = 0;
}

int main() {
    int* i;
    assert (cudaSuccess ==  cudaHostAlloc(&i, sizeof(int)*100, cudaHostAllocMapped));
    segfault<<<1,100>>>();
    assert (cudaErrorLaunchFailure == cudaThreadSynchronize());
    assert (cudaErrorLaunchFailure == cudaGetLastError());
    assert (cudaSuccess == cudaGetLastError());
    assert (cudaSuccess == cudaFreeHost(i));
    return 0;
}

Upvotes: 1

Views: 1966

Answers (1)

Edric
Edric

Reputation: 25140

In my experience, once you hit that kind of error in CUDA execution, all subsequent operations will fail until you reset the device. The documentation indicates that the memory is scoped to the CUDA context, so when you destroy the context (by resetting the device), the memory will be freed.

Upvotes: 1

Related Questions