1 //------------------------------------------------------------------------------
2 // GB_cuda_get_device_properties.cu: get the properties of a GPU
3 //------------------------------------------------------------------------------
4
5 // SPDX-License-Identifier: Apache-2.0
6 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2019, All Rights Reserved.
7 // http://suitesparse.com See GraphBLAS/Doc/License.txt for license.
8
9 //------------------------------------------------------------------------------
10
11 #include "GB_cuda.h"
12
GB_cuda_get_device(int & device)13 bool GB_cuda_get_device ( int &device){
14 bool goodreturn = false;
15 if (&device == NULL)
16 {
17 // invalid inputs
18 return (false) ;
19 }
20
21 CHECK_CUDA_SIMPLE ( cudaGetDevice( &device ) );
22 goodreturn = true;
23
24 return goodreturn;
25
26 }
27
GB_cuda_set_device(int device)28 bool GB_cuda_set_device( int device) {
29 bool goodreturn = false;
30 if (device < 0)
31 {
32 // invalid inputs
33 return (false) ;
34 }
35
36 CHECK_CUDA_SIMPLE ( cudaSetDevice( device ) );
37 goodreturn = true;
38
39 return goodreturn;
40 }
41
GB_cuda_get_device_properties(int device,GB_cuda_device * prop)42 bool GB_cuda_get_device_properties // true if OK, false if failure
43 (
44 int device,
45 GB_cuda_device *prop
46 )
47 {
48
49 //--------------------------------------------------------------------------
50 // check inputs
51 //--------------------------------------------------------------------------
52 bool goodreturn = false;
53 if (prop == NULL || device < 0)
54 {
55 // invalid inputs
56 return (false) ;
57 }
58
59 int old_device;
60 CHECK_CUDA_SIMPLE ( cudaGetDevice( &old_device ) ) ;
61
62
63 //--------------------------------------------------------------------------
64 // get the properties
65 //--------------------------------------------------------------------------
66 int num_sms;
67 int compute_capability_major;
68 int compute_capability_minor;
69 size_t memfree, memtotal;
70
71 CHECK_CUDA_SIMPLE( cudaDeviceGetAttribute (&num_sms,
72 cudaDevAttrMultiProcessorCount,
73 device) );
74 CHECK_CUDA_SIMPLE( cudaDeviceGetAttribute (&compute_capability_major,
75 cudaDevAttrComputeCapabilityMajor,
76 device) );
77 CHECK_CUDA_SIMPLE( cudaDeviceGetAttribute (&compute_capability_minor,
78 cudaDevAttrComputeCapabilityMajor,
79 device) );
80
81 CHECK_CUDA_SIMPLE ( cudaSetDevice( device ) );
82 CHECK_CUDA_SIMPLE ( cudaMemGetInfo( & memfree, &memtotal) ) ;
83 CHECK_CUDA_SIMPLE ( cudaSetDevice( old_device ) );
84
85 prop->total_global_memory = memtotal;
86 prop->number_of_sms = num_sms;
87 prop->compute_capability_major = compute_capability_major;
88 prop->compute_capability_minor = compute_capability_minor;
89
90 goodreturn = true;
91 //--------------------------------------------------------------------------
92 // return result
93 //--------------------------------------------------------------------------
94
95 return goodreturn;
96 }
97
98