@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
13
13
limitations under the License. */
14
14
#include < Python.h>
15
15
#include < algorithm>
16
+ #include < cstdlib>
16
17
#include < map>
17
18
#include < memory>
18
19
#include < mutex> // NOLINT // for call_once
@@ -786,13 +787,41 @@ All parameter, weight, gradient are variables in Paddle.
786
787
.def (" __init__" ,
787
788
[](platform::CUDAPlace &self, int dev_id) {
788
789
#ifdef PADDLE_WITH_CUDA
789
- PADDLE_ENFORCE (
790
- dev_id >= 0 && dev_id < platform::GetCUDADeviceCount (),
791
- " Invalid CUDAPlace(%d), must inside [0, %d)" , dev_id,
792
- platform::GetCUDADeviceCount ());
790
+ if (UNLIKELY (dev_id < 0 )) {
791
+ LOG (ERROR) << string::Sprintf (
792
+ " Invalid CUDAPlace(%d), device id must be 0 or "
793
+ " positive integer" ,
794
+ dev_id);
795
+ std::exit (-1 );
796
+ }
797
+
798
+ if (UNLIKELY (dev_id >= platform::GetCUDADeviceCount ())) {
799
+ if (platform::GetCUDADeviceCount () == 0 ) {
800
+ LOG (ERROR) << " Cannot use GPU because there is no GPU "
801
+ " detected on your "
802
+ " machine." ;
803
+ std::exit (-1 );
804
+ } else {
805
+ LOG (ERROR) << string::Sprintf (
806
+ " Invalid CUDAPlace(%d), must inside [0, %d), because GPU "
807
+ " number on your machine is %d" ,
808
+ dev_id, platform::GetCUDADeviceCount (),
809
+ platform::GetCUDADeviceCount ());
810
+ std::exit (-1 );
811
+ }
812
+ }
813
+
793
814
new (&self) platform::CUDAPlace (dev_id);
794
815
#else
795
- PADDLE_THROW (" Cannot use CUDAPlace in CPU only version" );
816
+ LOG (ERROR) << string::Sprintf (
817
+ " Cannot use GPU because you have installed CPU version "
818
+ " PaddlePaddle.\n "
819
+ " If you want to use GPU, please try to install GPU version "
820
+ " PaddlePaddle by: pip install paddlepaddle-gpu\n "
821
+ " If you only have CPU, please change CUDAPlace(%d) to be "
822
+ " CPUPlace().\n " ,
823
+ dev_id);
824
+ std::exit (-1 );
796
825
#endif
797
826
})
798
827
.def (" _type" , &PlaceIndex<platform::CUDAPlace>)
0 commit comments