I am trying to run a cuda program that has multiple kernels on specific MIG instances using setenv() method to change CUDA_VISIBLE_DEVICES to switch between MIG instances where the kernels are launched as shown in the snipped below. However, all kernels only run on MIG instance that is first made visible using the setenv() method, even if I pass in the UUID of another MIG instance. How do I get around this limitation? Thanks
void runKernelOnMigPartition(const char* migUuid, int size, float* h_output, int kernelValue) {
// Set the MIG partition via CUDA_VISIBLE_DEVICES
if (setenv("CUDA_VISIBLE_DEVICES", migUuid, 1) != 0) {
std::cerr << "Error setting CUDA_VISIBLE_DEVICES for MIG partition: " << migUuid << std::endl;
return;
}
std::cout << "Running on MIG Partition: " << migUuid << std::endl;
// Allocate device memory
float* d_output;
cudaMalloc(&d_output, size * sizeof(float));
// Launch the kernel
int threadsPerBlock = 256;
int blocksPerGrid = (size + threadsPerBlock - 1) / threadsPerBlock;
longRunningKernel<<<blocksPerGrid, threadsPerBlock>>>(d_output, size, kernelValue);
cudaDeviceSynchronize();
// Copy results back to host
cudaMemcpy(h_output, d_output, size * sizeof(float), cudaMemcpyDeviceToHost);
// Free device memory
cudaFree(d_output);
}