|
| 1 | +/* |
| 2 | +// Copyright (c) 2019-2025 Ben Ashbaugh |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | +*/ |
| 6 | + |
| 7 | +#include <stdio.h> |
| 8 | +#include <vector> |
| 9 | +#include <popl/popl.hpp> |
| 10 | + |
| 11 | +#include <CL/opencl.hpp> |
| 12 | + |
| 13 | +static cl_int PrintPlatformInfoSummary( |
| 14 | + cl::Platform platform ) |
| 15 | +{ |
| 16 | + printf("\tName: %s\n", platform.getInfo<CL_PLATFORM_NAME>().c_str() ); |
| 17 | + printf("\tVendor: %s\n", platform.getInfo<CL_PLATFORM_VENDOR>().c_str() ); |
| 18 | + printf("\tDriver Version: %s\n", platform.getInfo<CL_PLATFORM_VERSION>().c_str() ); |
| 19 | + |
| 20 | + return CL_SUCCESS; |
| 21 | +} |
| 22 | + |
| 23 | +static void PrintDeviceType( |
| 24 | + const char* label, |
| 25 | + cl_device_type type ) |
| 26 | +{ |
| 27 | + printf("%s%s%s%s%s%s\n", |
| 28 | + label, |
| 29 | + ( type & CL_DEVICE_TYPE_DEFAULT ) ? "DEFAULT " : "", |
| 30 | + ( type & CL_DEVICE_TYPE_CPU ) ? "CPU " : "", |
| 31 | + ( type & CL_DEVICE_TYPE_GPU ) ? "GPU " : "", |
| 32 | + ( type & CL_DEVICE_TYPE_ACCELERATOR ) ? "ACCELERATOR " : "", |
| 33 | + ( type & CL_DEVICE_TYPE_CUSTOM ) ? "CUSTOM " : ""); |
| 34 | +} |
| 35 | + |
| 36 | +static cl_int PrintDeviceInfoSummary( |
| 37 | + const std::vector<cl::Device>& devices ) |
| 38 | +{ |
| 39 | + for( size_t i = 0; i < devices.size(); i++ ) |
| 40 | + { |
| 41 | + printf("Device[%zu]:\n", i ); |
| 42 | + |
| 43 | + cl_device_type deviceType = devices[i].getInfo<CL_DEVICE_TYPE>(); |
| 44 | + PrintDeviceType("\tType: ", deviceType); |
| 45 | + |
| 46 | + printf("\tName: %s\n", devices[i].getInfo<CL_DEVICE_NAME>().c_str() ); |
| 47 | + printf("\tVendor: %s\n", devices[i].getInfo<CL_DEVICE_VENDOR>().c_str() ); |
| 48 | + printf("\tDevice Version: %s\n", devices[i].getInfo<CL_DEVICE_VERSION>().c_str() ); |
| 49 | + printf("\tDriver Version: %s\n", devices[i].getInfo<CL_DRIVER_VERSION>().c_str() ); |
| 50 | + } |
| 51 | + |
| 52 | + return CL_SUCCESS; |
| 53 | +} |
| 54 | + |
| 55 | +static void PrintPlatformInfo() |
| 56 | +{ |
| 57 | + std::vector<cl::Platform> platforms; |
| 58 | + cl::Platform::get(&platforms); |
| 59 | + |
| 60 | + for( size_t i = 0; i < platforms.size(); i++ ) |
| 61 | + { |
| 62 | + printf( "Platform[%zu]:\n", i ); |
| 63 | + PrintPlatformInfoSummary( platforms[i] ); |
| 64 | + |
| 65 | + std::vector<cl::Device> devices; |
| 66 | + platforms[i].getDevices(CL_DEVICE_TYPE_ALL, &devices); |
| 67 | + |
| 68 | + PrintDeviceInfoSummary( devices ); |
| 69 | + printf( "\n" ); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +typedef cl_int (CL_API_CALL *_sclpfn_clTeardownOCLRT)(void); |
| 74 | + |
| 75 | +int main( |
| 76 | + int argc, |
| 77 | + char** argv ) |
| 78 | +{ |
| 79 | + { |
| 80 | + popl::OptionParser op("Supported Options"); |
| 81 | + |
| 82 | + bool printUsage = false; |
| 83 | + try { |
| 84 | + op.parse(argc, argv); |
| 85 | + } catch (std::exception& e) { |
| 86 | + fprintf(stderr, "Error: %s\n\n", e.what()); |
| 87 | + printUsage = true; |
| 88 | + } |
| 89 | + |
| 90 | + if (printUsage || !op.unknown_options().empty() || !op.non_option_args().empty()) { |
| 91 | + fprintf(stderr, |
| 92 | + "Usage: loaderunload [options]\n" |
| 93 | + "%s", op.help().c_str()); |
| 94 | + return -1; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + printf("Initially:\n"); |
| 99 | + PrintPlatformInfo(); |
| 100 | + |
| 101 | + auto clTeardownOCLRT = (_sclpfn_clTeardownOCLRT) |
| 102 | + clGetExtensionFunctionAddress("clTeardownOCLRT"); |
| 103 | + if (clTeardownOCLRT == nullptr) { |
| 104 | + printf("Couldn't get a pointer to clTeardownOCLRT()...\n"); |
| 105 | + } else { |
| 106 | + printf("Initiating teardown...\n"); |
| 107 | + clTeardownOCLRT(); |
| 108 | + |
| 109 | + printf("Now:\n"); |
| 110 | + PrintPlatformInfo(); |
| 111 | + } |
| 112 | + |
| 113 | + printf("Done.\n"); |
| 114 | + |
| 115 | + return 0; |
| 116 | +} |
0 commit comments