From 0ad6cb01b9176e99c3e4bb5643ac6a5dd477aad6 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Thu, 9 Nov 2017 20:57:31 +0100 Subject: Check for CUDA support at run-time in use_cuda() --- cuda/2d/astra.cu | 11 +++++++++++ cuda/2d/astra.h | 2 ++ cuda/3d/mem3d.cu | 19 ++++++------------- cuda/3d/mem3d.h | 1 - include/astra/Globals.h | 17 +---------------- matlab/mex/astra_mex_c.cpp | 2 +- python/astra/astra_c.pyx | 3 ++- src/CompositeGeometryManager.cpp | 3 ++- src/Globals.cpp | 16 +++++++++++++--- 9 files changed, 38 insertions(+), 36 deletions(-) diff --git a/cuda/2d/astra.cu b/cuda/2d/astra.cu index 81459de..69a4e86 100644 --- a/cuda/2d/astra.cu +++ b/cuda/2d/astra.cu @@ -529,5 +529,16 @@ _AstraExport bool setGPUIndex(int iGPUIndex) return true; } +_AstraExport size_t availableGPUMemory() +{ + size_t free, total; + cudaError_t err = cudaMemGetInfo(&free, &total); + if (err != cudaSuccess) + return 0; + return free; +} + + + } diff --git a/cuda/2d/astra.h b/cuda/2d/astra.h index 9355cb4..119d172 100644 --- a/cuda/2d/astra.h +++ b/cuda/2d/astra.h @@ -121,5 +121,7 @@ _AstraExport std::string getCudaDeviceString(int device); _AstraExport bool setGPUIndex(int index); +_AstraExport size_t availableGPUMemory(); + } #endif diff --git a/cuda/3d/mem3d.cu b/cuda/3d/mem3d.cu index 2369149..e55cc13 100644 --- a/cuda/3d/mem3d.cu +++ b/cuda/3d/mem3d.cu @@ -32,6 +32,8 @@ along with the ASTRA Toolbox. If not, see . #include "mem3d.h" +#include "../2d/astra.h" + #include "astra3d.h" #include "cone_fp.h" #include "cone_bp.h" @@ -53,15 +55,6 @@ struct SMemHandle3D_internal unsigned int nz; }; -size_t availableGPUMemory() -{ - size_t free, total; - cudaError_t err = cudaMemGetInfo(&free, &total); - if (err != cudaSuccess) - return 0; - return free; -} - int maxBlockDimension() { int dev; @@ -88,7 +81,7 @@ MemHandle3D allocateGPUMemory(unsigned int x, unsigned int y, unsigned int z, Me hnd.ny = y; hnd.nz = z; - size_t free = availableGPUMemory(); + size_t free = astraCUDA::availableGPUMemory(); cudaError_t err; err = cudaMalloc3D(&hnd.ptr, make_cudaExtent(sizeof(float)*x, y, z)); @@ -97,7 +90,7 @@ MemHandle3D allocateGPUMemory(unsigned int x, unsigned int y, unsigned int z, Me return MemHandle3D(); } - size_t free2 = availableGPUMemory(); + size_t free2 = astraCUDA::availableGPUMemory(); ASTRA_DEBUG("Allocated %d x %d x %d on GPU. (Pre: %lu, post: %lu)", x, y, z, free, free2); @@ -127,9 +120,9 @@ bool zeroGPUMemory(MemHandle3D handle, unsigned int x, unsigned int y, unsigned bool freeGPUMemory(MemHandle3D handle) { - size_t free = availableGPUMemory(); + size_t free = astraCUDA::availableGPUMemory(); cudaError_t err = cudaFree(handle.d->ptr.ptr); - size_t free2 = availableGPUMemory(); + size_t free2 = astraCUDA::availableGPUMemory(); ASTRA_DEBUG("Freeing memory. (Pre: %lu, post: %lu)", free, free2); diff --git a/cuda/3d/mem3d.h b/cuda/3d/mem3d.h index 619354b..12a532d 100644 --- a/cuda/3d/mem3d.h +++ b/cuda/3d/mem3d.h @@ -77,7 +77,6 @@ enum Mem3DZeroMode { INIT_ZERO }; -size_t availableGPUMemory(); int maxBlockDimension(); _AstraExport MemHandle3D wrapHandle(float *D_ptr, unsigned int x, unsigned int y, unsigned int z, unsigned int pitch); diff --git a/include/astra/Globals.h b/include/astra/Globals.h index 8d0d619..f7f130f 100644 --- a/include/astra/Globals.h +++ b/include/astra/Globals.h @@ -104,22 +104,6 @@ namespace astra { typedef short int int16; } -//---------------------------------------------------------------------------------------- -// globals vars & functions -//namespace astra { -//#define ToolboxVersion 0.1f; - -//float32 getVersion() { return ToolboxVersion; } - -//_AstraExport bool cudaEnabled() { -//#ifdef ASTRA_CUDA -// return true; -//#else -// return false; -//#endif -//} -//} - //---------------------------------------------------------------------------------------- // variables namespace astra { @@ -169,6 +153,7 @@ namespace astra { namespace astra { _AstraExport inline int getVersion() { return ASTRA_TOOLBOXVERSION; } _AstraExport inline const char* getVersionString() { return ASTRA_TOOLBOXVERSION_STRING; } +_AstraExport bool cudaAvailable(); #ifdef ASTRA_CUDA _AstraExport inline bool cudaEnabled() { return true; } #else diff --git a/matlab/mex/astra_mex_c.cpp b/matlab/mex/astra_mex_c.cpp index 017946a..eda5337 100644 --- a/matlab/mex/astra_mex_c.cpp +++ b/matlab/mex/astra_mex_c.cpp @@ -78,7 +78,7 @@ void astra_mex_credits(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[ void astra_mex_use_cuda(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { if (1 <= nlhs) { - plhs[0] = mxCreateDoubleScalar(astra::cudaEnabled() ? 1 : 0); + plhs[0] = mxCreateDoubleScalar(astra::cudaAvailable() ? 1 : 0); } } diff --git a/python/astra/astra_c.pyx b/python/astra/astra_c.pyx index f25fc2a..8858244 100644 --- a/python/astra/astra_c.pyx +++ b/python/astra/astra_c.pyx @@ -38,6 +38,7 @@ from .PyIndexManager cimport CAstraObjectManagerBase cdef extern from "astra/Globals.h" namespace "astra": bool cudaEnabled() + bool cudaAvailable() IF HAVE_CUDA==True: cdef extern from "../cuda/2d/astra.h" namespace "astraCUDA": @@ -74,7 +75,7 @@ def credits(): def use_cuda(): - return cudaEnabled() + return cudaAvailable() IF HAVE_CUDA==True: def set_gpu_index(idx, memory=0): diff --git a/src/CompositeGeometryManager.cpp b/src/CompositeGeometryManager.cpp index 8af430c..cfc2db3 100644 --- a/src/CompositeGeometryManager.cpp +++ b/src/CompositeGeometryManager.cpp @@ -43,6 +43,7 @@ along with the ASTRA Toolbox. If not, see . #include "astra/Float32VolumeData3DGPU.h" #include "astra/Logging.h" +#include "../cuda/2d/astra.h" #include "../cuda/3d/mem3d.h" #include @@ -1653,7 +1654,7 @@ bool CCompositeGeometryManager::doJobs(TJobList &jobs) // Get memory from first GPU. Not optimal... if (!m_GPUIndices.empty()) astraCUDA3d::setGPUIndex(m_GPUIndices[0]); - maxSize = astraCUDA3d::availableGPUMemory(); + maxSize = astraCUDA::availableGPUMemory(); if (maxSize == 0) { ASTRA_WARN("Unable to get available GPU memory. Defaulting to 1GB."); maxSize = 1024 * 1024 * 1024; diff --git a/src/Globals.cpp b/src/Globals.cpp index 0edb397..bbe0c2f 100644 --- a/src/Globals.cpp +++ b/src/Globals.cpp @@ -26,9 +26,19 @@ along with the ASTRA Toolbox. If not, see . */ #include "astra/Globals.h" +#include "cuda/2d/astra.h" + +namespace astra { + +bool running_in_matlab=false; + +_AstraExport bool cudaAvailable() { +#ifdef ASTRA_CUDA + return astraCUDA::availableGPUMemory() > 0; +#else + return false; +#endif +} -namespace astra{ - bool running_in_matlab=false; } -// nothing to see here :) -- cgit v1.2.3