File: compute_capability.cpp

package info (click to toggle)
stdgpu 1.3.0%2Bgit20220507.32e0517-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,528 kB
  • sloc: cpp: 7,818; pascal: 1,893; xml: 214; sh: 181; makefile: 50
file content (50 lines) | stat: -rw-r--r-- 1,588 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Check for GPUs present and their compute capability
// based on http://stackoverflow.com/questions/2285185/easiest-way-to-test-for-existence-of-cuda-capable-gpu-from-cmake/2297877#2297877 (Christopher Bruns)

#include <stdio.h>
#include <cuda_runtime_api.h>

#include <iostream>
#include <set>
#include <sstream>

int main()
{
    int deviceCount;
    std::set<std::string> computeCapabilities;

    struct cudaDeviceProp properties;
    cudaError_t cudaResultCode = cudaGetDeviceCount(&deviceCount);
    if (cudaResultCode != cudaSuccess)
    {
        deviceCount = 0;
    }

    /* machines with no GPUs can still report one emulation device */
    for (int device = 0; device < deviceCount; ++device)
    {
        cudaDeviceProp currentProperties;
        cudaGetDeviceProperties(&currentProperties, device);

        /* 9999 means emulation only */
        if (currentProperties.major != 9999)
        {
            std::stringstream ss;
            ss << currentProperties.major;
            ss << currentProperties.minor;

            computeCapabilities.insert(ss.str());
        }
    }

    /* don't just return the number of gpus, because other runtime cuda
    errors can also yield non-zero return values */
    for (std::set<std::string>::const_iterator it = computeCapabilities.begin(); it != computeCapabilities.end(); ++it)
    {
        // Add a semicolon if we have already printed some output.
        if(it != computeCapabilities.begin()) std::cout << ';';
        std::cout << *it;
    }

    return computeCapabilities.size() == 0; /* 0 devices -> failure */
}