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';
}