Skip to content

Commit 0a77590

Browse files
committed
Make use_cuda() only set to true if there is also a GPU available
1 parent 412ef35 commit 0a77590

File tree

5 files changed

+476
-460
lines changed

5 files changed

+476
-460
lines changed

dlib/cuda/cuda_dlib.cu

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ namespace dlib
4444
CHECK_CUDA(cudaSetDeviceFlags(cudaDeviceScheduleBlockingSync));
4545
}
4646

47+
bool is_available(
48+
)
49+
{
50+
int num_devices;
51+
return cudaGetDeviceCount(&num_devices) == cudaSuccess && num_devices > 0;
52+
}
53+
4754
int get_num_devices (
4855
)
4956
{

dlib/cuda/cuda_dlib.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ namespace dlib
2525
int get_num_devices (
2626
);
2727

28+
bool is_available (
29+
);
30+
2831
std::string get_device_name (
2932
int device
3033
);
@@ -892,6 +895,9 @@ namespace dlib
892895
inline int get_num_devices (
893896
) { return 1; }
894897

898+
inline bool is_available (
899+
) { return false; }
900+
895901
inline std::string get_device_name (
896902
int device
897903
)

dlib/cuda/tensor_tools.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace dlib
2222
)
2323
{
2424
#ifdef DLIB_USE_CUDA
25-
thread_local bool var(true);
25+
thread_local bool var(cuda::is_available());
2626
#else
2727
thread_local bool var(false);
2828
#endif

dlib/dnn/core_abstract.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,20 @@ namespace dlib
202202
ensures
203203
- If dlib should use the CUDA implementation of a deep neural network
204204
then this function returns true and false otherwise.
205-
- On program startup this function will return true if DLIB_USE_CUDA is defined.
205+
- On program startup this function will return true if DLIB_USE_CUDA is defined and
206+
there is an available GPU device to use.
206207
- This function always returns false if DLIB_USE_CUDA is not defined.
208+
- This function sets a thread local variable. That is, each thread has its own value
209+
for use_cuda(). This means that one thread may use cuda while another thread might
210+
not, depending on the setting of use_cuda().
207211
!*/
208212

209213
void set_use_cuda(
210214
bool flag
211215
);
212216
/*!
213217
ensures
214-
- #use_cuda() == flag
218+
- #use_cuda() == flag for the calling thread.
215219
!*/
216220

217221
// ----------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)