Skip to content

Commit

Permalink
BUG: modify CudaContextManager() with cudaSetDevice() and remove GetC…
Browse files Browse the repository at this point in the history
…urrentContext()
  • Loading branch information
LAURENDEAU Matthieu committed Feb 8, 2024
1 parent 9d3fe9b commit e045737
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 42 deletions.
3 changes: 0 additions & 3 deletions include/itkCudaContextManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ class CudaCommon_EXPORT CudaContextManager : public LightObject
static void
DestroyInstance();

CUcontext *
GetCurrentContext();

int
GetCurrentDevice();

Expand Down
6 changes: 0 additions & 6 deletions include/itkCudaImageDataManager.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ CudaImageDataManager<ImageType>::MakeCPUBufferUpToDate()
std::cout << this << ": GPU->CPU data copy" << std::endl;
#endif

CUDA_CHECK(cuCtxSetCurrent(
*(this->m_ContextManager->GetCurrentContext()))); // This is necessary when running multithread to bind the host
// CPU thread to the right context
errid = cudaMemcpy(m_CPUBuffer, m_GPUBuffer->GetPointer(), m_BufferSize, cudaMemcpyDeviceToHost);
CudaCheckError(errid, __FILE__, __LINE__, ITK_LOCATION);

Expand Down Expand Up @@ -117,9 +114,6 @@ CudaImageDataManager<ImageType>::MakeGPUBufferUpToDate()
std::cout << "CPU->GPU data copy" << std::endl;
#endif

CUDA_CHECK(cuCtxSetCurrent(
*(this->m_ContextManager->GetCurrentContext()))); // This is necessary when running multithread to bind the host
// CPU thread to the right context
errid = cudaMemcpy(m_GPUBuffer->GetPointer(), m_CPUBuffer, m_BufferSize, cudaMemcpyHostToDevice);
CudaCheckError(errid, __FILE__, __LINE__, ITK_LOCATION);

Expand Down
23 changes: 2 additions & 21 deletions src/itkCudaContextManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,8 @@ CudaContextManager::CudaContextManager()
if (m_NumberOfDevices)
{
CUdevice device = 0;
m_DeviceIdx = itk::CudaGetMaxFlopsDev();
CUDA_CHECK(cuDeviceGet(&device, m_DeviceIdx));

CUDA_CHECK(cuCtxCreate(&m_Context, CU_CTX_SCHED_AUTO, device));

CUDA_CHECK(cuCtxSetCurrent(m_Context));

m_Device = device;
CUDA_CHECK(cudaSetDevice(m_Device));
}
else
{
Expand All @@ -83,14 +77,7 @@ CudaContextManager::CudaContextManager()
}
}

CudaContextManager::~CudaContextManager()
{
if (m_Context)
{
CUDA_CHECK(cuCtxDestroy(m_Context));
}
cudaDeviceReset();
}
CudaContextManager::~CudaContextManager() {}

int
CudaContextManager::GetCurrentDevice()
Expand All @@ -100,10 +87,4 @@ CudaContextManager::GetCurrentDevice()
return device;
}

CUcontext *
CudaContextManager::GetCurrentContext()
{
return &m_Context;
}

} // namespace itk
12 changes: 0 additions & 12 deletions src/itkCudaDataManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ CudaDataManager::CudaDataManager()
// is no CUDA device available, we just do not set the context (SR). This fixes
// the problem reported here:
// https://www.creatis.insa-lyon.fr/pipermail/rtk-users/2015-July/000570.html
CUcontext * ctx = m_ContextManager->GetCurrentContext();
if (ctx)
CUDA_CHECK(cuCtxSetCurrent(*ctx));

m_CPUBuffer = nullptr;
m_GPUBuffer = GPUMemPointer::New();
Expand Down Expand Up @@ -91,9 +88,6 @@ CudaDataManager::Free()
{
try
{
CUDA_CHECK(cuCtxSetCurrent(
*(this->m_ContextManager->GetCurrentContext()))); // This is necessary when running multithread to bind the host
// CPU thread to the right context
m_GPUBuffer->Free();
}
catch (itk::ExceptionObject & e)
Expand Down Expand Up @@ -171,9 +165,6 @@ CudaDataManager::UpdateCPUBuffer()
std::cout << this << "::UpdateCPUBuffer GPU->CPU data copy " << m_GPUBuffer->GetPointer() << "->" << m_CPUBuffer
<< " : " << m_BufferSize << std::endl;
#endif
CUDA_CHECK(cuCtxSetCurrent(
*(this->m_ContextManager->GetCurrentContext()))); // This is necessary when running multithread to bind the host
// CPU thread to the right context
CUDA_CHECK(cudaMemcpy(m_CPUBuffer, m_GPUBuffer->GetPointer(), m_BufferSize, cudaMemcpyDeviceToHost));
m_IsCPUBufferDirty = false;
}
Expand Down Expand Up @@ -212,9 +203,6 @@ CudaDataManager::UpdateGPUBuffer()
std::cout << this << "::UpdateGPUBuffer CPU->GPU data copy " << m_CPUBuffer << "->" << m_GPUBuffer->GetPointer()
<< " : " << m_BufferSize << std::endl;
#endif
CUDA_CHECK(cuCtxSetCurrent(
*(this->m_ContextManager->GetCurrentContext()))); // This is necessary when running multithread to bind the host
// CPU thread to the right context
CUDA_CHECK(cudaMemcpy(m_GPUBuffer->GetPointer(), m_CPUBuffer, m_BufferSize, cudaMemcpyHostToDevice));
}
m_IsGPUBufferDirty = false;
Expand Down

0 comments on commit e045737

Please sign in to comment.