cuMemAlloc'ing memory in one CUDA context, and freeing it in another - why does this succeed?

I create 2 cuda context “ctx1” and "ctx2" and set current context to "ctx1" and allocate 8 bytes of memory and switch current context to ctx2. Then free Memory alloc in ctx1. Why does this return CUDA_SUCCESS?

And when I destroy ctx1 and then free Memory, it will cause CUDA_INVALID_VALUE. In my opinion, each context contain their unique resources and not allowed access by other Context. Can someone explain this behaviour?

int main() {
    using std::cout;
    CUresult answer;
    CUdeviceptr dptr = 4;
    int device_enum = 0;
    CUcontext ctx1,ctx2;
    cuInit(0);
    CUdevice able_dev = 0;
    CUresult create_ctx1 = cuCtxCreate(&ctx1,CU_CTX_SCHED_AUTO,able_dev);
    CUresult create_ctx2 = cuCtxCreate(&ctx2,CU_CTX_SCHED_AUTO,able_dev);
    assert(cuCtxSetCurrent(ctx1) == CUDA_SUCCESS);
    answer = cuMemAlloc(&dptr,8);
    cout << "maloc result1 = " << answer << '\n';
    assert(cuCtxSetCurrent(ctx2) == CUDA_SUCCESS);
    cout << "free in ctx2 result = " << cuMemFree(dptr) << '\n';
}

Solution 1:

Why does this return CUDA_SUCCESS?

Why should it not return CUDA_SUCCESS? I don't see anywhere in the documentation that says a free operation is only valid if the referenced pointer is associated to the current context. This seems perfectly valid, and your test case seems to confirm it.

And when I destroy ctx1 and then free Memory, it will cause CUDA_INVALID_VALUE.

That is expected behavior. You allocated dptr in ctx1. When you destroy ctx1, all state associated with that context, including any associated allocations, are destroyed. Attempting to free a pointer that has already been freed via context destruction is invalid.

In case you thought, as someone else indicated in the comments, that the context would be "needed" for the free operation:

  1. It's not documented
  2. It's not necessary in a UVA setting. A pointer is introspectable in a UVA setting partly because the UVA setting ensures that relevant address spaces do not overlap.