@@ -18,6 +18,15 @@ int gpu_ndevices() {
1818}
1919
2020void gpu_set_device (int32_t igpu ) {
21+ if (igpu < 0 ) {
22+ fprintf (stderr , "gpu_set_device: invalid device index %d (must be >= 0)\n" , igpu );
23+ return ;
24+ }
25+ int ngpus = gpu_ndevices ();
26+ if (igpu >= ngpus ) {
27+ fprintf (stderr , "gpu_set_device: invalid device index %d (only %d devices available)\n" , igpu , ngpus );
28+ return ;
29+ }
2130 cudaError_t rc = cudaSetDevice ((int ) igpu );
2231 if (rc != cudaSuccess ) {
2332 fprintf (stderr ,"cudaSetDevice(%d) failed: %s\n" , igpu , cudaGetErrorString (rc ));
@@ -36,29 +45,29 @@ void gpu_get_memory(size_t* free, size_t* total) {
3645/* Allocation functions */
3746
3847void gpu_allocate (void * * ptr , const int64_t size ) {
48+ if (ptr == NULL ) {
49+ fprintf (stderr , "gpu_allocate: ptr argument is NULL\n" );
50+ return ;
51+ }
52+
3953 size_t free , total ;
4054 cudaError_t rc = cudaMemGetInfo ( & free , & total );
4155 if (rc != cudaSuccess ) {
4256 free = INT64_MAX ;
4357 }
4458
45- // rc = cudaMallocManaged(ptr, size, cudaMemAttachGlobal);
46- rc = cudaMalloc (ptr , size );
59+ rc = cudaMalloc (ptr , size );
4760
48- // /* Use managed memory if it does not fit on the GPU */
49- // if (size < free && size < total/2) {
50- // rc= cudaMalloc(ptr, size);
51- // } else {
52- // rc = cudaMallocManaged(ptr, size, cudaMemAttachGlobal);
53- // }
5461 if (rc != cudaSuccess ) {
55- fprintf (stderr ,"cudaMallocManaged failed: %s\n" , cudaGetErrorString (rc ));
62+ fprintf (stderr ,"cudaMalloc failed: %s\n" , cudaGetErrorString (rc ));
5663 assert (rc == cudaSuccess );
5764 }
5865}
5966
6067void gpu_deallocate (void * * ptr ) {
61- assert (* ptr != NULL );
68+ if (ptr == NULL || * ptr == NULL ) {
69+ return ;
70+ }
6271 cudaFree (* ptr );
6372 * ptr = NULL ;
6473}
@@ -71,6 +80,10 @@ void gpu_free(void** ptr) {
7180/* Memory transfer functions */
7281
7382void gpu_upload (const void * cpu_ptr , void * gpu_ptr , const int64_t n ) {
83+ if (cpu_ptr == NULL || gpu_ptr == NULL ) {
84+ fprintf (stderr , "gpu_upload: NULL pointer argument\n" );
85+ return ;
86+ }
7487 cudaError_t rc = cudaMemcpy (gpu_ptr , cpu_ptr , n , cudaMemcpyHostToDevice );
7588 if (rc != cudaSuccess ) {
7689 fprintf (stderr ,"cudaMemcpy (upload) failed: %s\n" , cudaGetErrorString (rc ));
@@ -79,6 +92,10 @@ void gpu_upload(const void* cpu_ptr, void* gpu_ptr, const int64_t n) {
7992}
8093
8194void gpu_download (const void * gpu_ptr , void * cpu_ptr , const int64_t n ) {
95+ if (gpu_ptr == NULL || cpu_ptr == NULL ) {
96+ fprintf (stderr , "gpu_download: NULL pointer argument\n" );
97+ return ;
98+ }
8299 cudaError_t rc = cudaMemcpy (cpu_ptr , gpu_ptr , n , cudaMemcpyDeviceToHost );
83100 if (rc != cudaSuccess ) {
84101 fprintf (stderr ,"cudaMemcpy (download) failed: %s\n" , cudaGetErrorString (rc ));
@@ -87,6 +104,10 @@ void gpu_download(const void* gpu_ptr, void* cpu_ptr, const int64_t n) {
87104}
88105
89106void gpu_copy (const void * gpu_ptr_src , void * gpu_ptr_dest , const int64_t n ) {
107+ if (gpu_ptr_src == NULL || gpu_ptr_dest == NULL ) {
108+ fprintf (stderr , "gpu_copy: NULL pointer argument\n" );
109+ return ;
110+ }
90111 cudaError_t rc = cudaMemcpy (gpu_ptr_dest , gpu_ptr_src , n , cudaMemcpyDeviceToDevice );
91112 if (rc != cudaSuccess ) {
92113 fprintf (stderr ,"cudaMemcpy (copy) failed: %s\n" , cudaGetErrorString (rc ));
@@ -118,8 +139,8 @@ void gpu_stream_destroy(cudaStream_t* ptr) {
118139void gpu_set_stream (cublasHandle_t handle , cudaStream_t stream ) {
119140 cublasStatus_t rc = cublasSetStream (handle , stream );
120141 if (rc != CUBLAS_STATUS_SUCCESS ) {
121- fprintf (stderr ,"cudaSetStream failed\n" );
122- assert (rc == cudaSuccess );
142+ fprintf (stderr ,"cublasSetStream failed\n" );
143+ assert (rc == CUBLAS_STATUS_SUCCESS );
123144 }
124145}
125146
0 commit comments