From 149e5ad62c9ca09901aa639b4ec3812994438d67 Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Thu, 25 Nov 2021 19:03:52 +0100 Subject: [PATCH] [v0.11] Working for future support of Intel iGPUs --- CMakeLists.txt | 100 ++++++++++++++++++++++++++--------------- src/common/gpu.hpp | 3 +- src/common/main.cpp | 6 ++- src/common/master.cpp | 46 +++++++++++++++++++ src/common/master.hpp | 12 +++++ src/common/printer.cpp | 22 ++++++++- src/cuda/cuda.cpp | 4 +- src/cuda/cuda.hpp | 7 ++- src/cuda/uarch.hpp | 4 +- src/intel/intel.cpp | 10 +++++ src/intel/intel.hpp | 8 ++++ src/intel/uarch.hpp | 6 +++ 12 files changed, 181 insertions(+), 47 deletions(-) create mode 100644 src/common/master.cpp create mode 100644 src/common/master.hpp create mode 100644 src/intel/intel.cpp create mode 100644 src/intel/intel.hpp create mode 100644 src/intel/uarch.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a6fb9b..7ec324e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,23 +7,22 @@ project(gpufetch CXX) set(SRC_DIR "src") set(COMMON_DIR "${SRC_DIR}/common") set(CUDA_DIR "${SRC_DIR}/cuda") +set(INTEL_DIR "${SRC_DIR}/intel") -if(NOT WIN32) - string(ASCII 27 Esc) - set(ColorReset "${Esc}[m") - set(ColorBold "${Esc}[1m") - set(Red "${Esc}[31m") - set(Green "${Esc}[32m") - set(BoldRed "${Esc}[1;31m") - set(BoldGreen "${Esc}[1;32m") - set(BoldYellow "${Esc}[1;33m") +add_executable(gpufetch ${COMMON_DIR}/main.cpp ${COMMON_DIR}/args.cpp ${COMMON_DIR}/gpu.cpp ${COMMON_DIR}/pci.cpp ${COMMON_DIR}/global.cpp ${COMMON_DIR}/printer.cpp ${COMMON_DIR}/master.cpp) + +if(NOT DEFINED ENABLE_INTEL_BACKEND) + set(ENABLE_INTEL_BACKEND true) endif() -check_language(CUDA) -if(CMAKE_CUDA_COMPILER) - enable_language(CUDA) -else() - message(FATAL_ERROR "${BoldRed}[ERROR]${ColorReset} Unable to find CUDA compiler. You may use -DCMAKE_CUDA_COMPILER and -DCMAKE_CUDA_COMPILER_TOOLKIT_ROOT if CUDA is installed but not detected by CMake") +if(NOT DEFINED ENABLE_CUDA_BACKEND OR ENABLE_CUDA_BACKEND) + check_language(CUDA) + if(CMAKE_CUDA_COMPILER) + enable_language(CUDA) + set(ENABLE_CUDA_BACKEND true) + else() + set(ENABLE_CUDA_BACKEND false) + endif() endif() list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") @@ -51,31 +50,62 @@ endif() set(SANITY_FLAGS "-Wfloat-equal -Wshadow -Wpointer-arith") set(CMAKE_CXX_FLAGS "${SANITY_FLAGS} -Wall -Wextra -pedantic -fstack-protector-all -pedantic") -# https://en.wikipedia.org/w/index.php?title=CUDA§ion=5#GPUs_supported -# https://raw.githubusercontent.com/PointCloudLibrary/pcl/master/cmake/pcl_find_cuda.cmake -if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL "11.0") - set(CMAKE_CUDA_ARCHITECTURES 35 37 50 52 53 60 61 62 70 72 75 80 86) -elseif(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL "10.0") - set(CMAKE_CUDA_ARCHITECTURES 30 32 35 37 50 52 53 60 61 62 70 72 75) -elseif(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL "9.0") - set(CMAKE_CUDA_ARCHITECTURES 30 32 35 37 50 52 53 60 61 62 70 72) -elseif(${CMAKE_CUDA_COMPILER_VERSION} VERSION_EQUAL "8.0") - set(CMAKE_CUDA_ARCHITECTURES 20 21 30 32 35 37 50 52 53 60 61 62) +if(ENABLE_INTEL_BACKEND) + add_library(intel_backend STATIC ${INTEL_DIR}/intel.cpp) + target_link_libraries(gpufetch intel_backend pci z) endif() -link_directories(${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/targets/x86_64-linux/lib) +if(ENABLE_CUDA_BACKEND) + # https://en.wikipedia.org/w/index.php?title=CUDA§ion=5#GPUs_supported + # https://raw.githubusercontent.com/PointCloudLibrary/pcl/master/cmake/pcl_find_cuda.cmake + if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL "11.0") + set(CMAKE_CUDA_ARCHITECTURES 35 37 50 52 53 60 61 62 70 72 75 80 86) + elseif(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL "10.0") + set(CMAKE_CUDA_ARCHITECTURES 30 32 35 37 50 52 53 60 61 62 70 72 75) + elseif(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL "9.0") + set(CMAKE_CUDA_ARCHITECTURES 30 32 35 37 50 52 53 60 61 62 70 72) + elseif(${CMAKE_CUDA_COMPILER_VERSION} VERSION_EQUAL "8.0") + set(CMAKE_CUDA_ARCHITECTURES 20 21 30 32 35 37 50 52 53 60 61 62) + endif() -add_library(cuda_backend STATIC ${CUDA_DIR}/cuda.cpp ${CUDA_DIR}/uarch.cpp ${CUDA_DIR}/pci.cpp) -add_executable(gpufetch ${COMMON_DIR}/main.cpp ${COMMON_DIR}/args.cpp ${COMMON_DIR}/gpu.cpp ${COMMON_DIR}/pci.cpp ${COMMON_DIR}/global.cpp ${COMMON_DIR}/printer.cpp) + link_directories(${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/targets/x86_64-linux/lib) -if(NOT ${PCIUTILS_FOUND}) - add_dependencies(cuda_backend pciutils) - add_dependencies(gpufetch pciutils) + add_library(cuda_backend STATIC ${CUDA_DIR}/cuda.cpp ${CUDA_DIR}/uarch.cpp ${CUDA_DIR}/pci.cpp) + + if(NOT ${PCIUTILS_FOUND}) + add_dependencies(cuda_backend pciutils) + add_dependencies(gpufetch pciutils) + endif() + + target_include_directories(cuda_backend PUBLIC ${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/samples/common/inc ${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/targets/x86_64-linux/include) + + target_link_libraries(cuda_backend cudart) + target_link_libraries(gpufetch cuda_backend pci z) endif() -target_include_directories(cuda_backend PUBLIC ${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/samples/common/inc ${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/targets/x86_64-linux/include) - -target_link_libraries(cuda_backend cudart) -target_link_libraries(gpufetch cuda_backend pci z) - install(TARGETS gpufetch DESTINATION bin) + +if(NOT WIN32) + string(ASCII 27 Esc) + set(ColorReset "${Esc}[m") + set(ColorBold "${Esc}[1m") + set(Red "${Esc}[31m") + set(Green "${Esc}[32m") + set(BoldRed "${Esc}[1;31m") + set(BoldGreen "${Esc}[1;32m") + set(BoldYellow "${Esc}[1;33m") +endif() + +message(STATUS "----------------------") +message(STATUS "gpufetch build report:") +if(ENABLE_INTEL_BACKEND) + message(STATUS "Intel backend: ${BoldGreen}ON${ColorReset}") +else() + message(STATUS "Intel backend: ${BoldRed}OFF${ColorReset}") +endif() +if(ENABLE_CUDA_BACKEND) + message(STATUS "CUDA backend: ${BoldGreen}ON${ColorReset}") +else() + message(STATUS "CUDA backend: ${BoldRed}OFF${ColorReset}") +endif() +message(STATUS "----------------------") diff --git a/src/common/gpu.hpp b/src/common/gpu.hpp index 2928a11..dc08a3a 100644 --- a/src/common/gpu.hpp +++ b/src/common/gpu.hpp @@ -9,7 +9,8 @@ #define UNKNOWN_FREQ -1 enum { - GPU_VENDOR_NVIDIA + GPU_VENDOR_NVIDIA, + GPU_VENDOR_INTEL }; enum { diff --git a/src/common/main.cpp b/src/common/main.cpp index 899ff89..1f0478c 100644 --- a/src/common/main.cpp +++ b/src/common/main.cpp @@ -4,6 +4,7 @@ #include "args.hpp" #include "global.hpp" +#include "master.hpp" #include "../cuda/cuda.hpp" #include "../cuda/uarch.hpp" @@ -65,8 +66,9 @@ int main(int argc, char* argv[]) { return EXIT_SUCCESS; } + struct gpu_list* list = get_gpu_list(); if(list_gpus()) { - return print_gpus_list(); + return print_gpus_list(list); } set_log_level(true); @@ -76,7 +78,7 @@ If you want to help to improve gpufetch, please compare the output of the progra with a reliable source which you know is right (e.g, techpowerup.com) and report\n\ any inconsistencies to https://github.com/Dr-Noob/gpufetch/issues"); - struct gpu_info* gpu = get_gpu_info(get_gpu_idx()); + struct gpu_info* gpu = get_gpu_info(list, get_gpu_idx()); if(gpu == NULL) return EXIT_FAILURE; diff --git a/src/common/master.cpp b/src/common/master.cpp new file mode 100644 index 0000000..49cec7a --- /dev/null +++ b/src/common/master.cpp @@ -0,0 +1,46 @@ +#include +#include +#include + +#include "master.hpp" + +#define MAX_GPUS 1000 + +struct gpu_list { + struct gpu_info ** gpus; + int num_gpus; +}; + +struct gpu_list* get_gpu_list() { + bool valid = true; + struct gpu_list* list = (struct gpu_list*) malloc(sizeof(struct gpu_list)); + list->num_gpus = 0; + list->gpus = (struct gpu_info**) malloc(sizeof(struct info*) * MAX_GPUS); + +#ifdef ENABLE_CUDA_BACKEND + int idx = 0; + + while(valid) { + list->gpus[idx] = get_gpu_info_cuda(idx); + if(list->gpus[idx] != NULL) idx++; + else valid = false; + } + + list->num_gpus += idx; +#endif + +#ifdef ENABLE_INTEL_BACKEND + list->gpus[idx] = get_gpu_info_intel(); + if(list->gpus[idx] != NULL) list->num_gpus++; +#endif + + return list; +} + +bool print_gpus_list(struct gpu_list* list) { + return false; +} + +struct gpu_info* get_gpu_info(struct gpu_list* list, int idx) { + return list->gpus[idx]; +} diff --git a/src/common/master.hpp b/src/common/master.hpp new file mode 100644 index 0000000..5863cbc --- /dev/null +++ b/src/common/master.hpp @@ -0,0 +1,12 @@ +#ifndef __GPU_LIST__ +#define __GPU_LIST__ + +#include "gpu.hpp" + +struct gpu_list; + +struct gpu_list* get_gpu_list(); +bool print_gpus_list(struct gpu_list* list); +struct gpu_info* get_gpu_info(struct gpu_list* list, int idx); + +#endif diff --git a/src/common/printer.cpp b/src/common/printer.cpp index 2e490ac..abc341d 100644 --- a/src/common/printer.cpp +++ b/src/common/printer.cpp @@ -342,6 +342,13 @@ void print_ascii_generic(struct ascii* art, uint32_t la, int32_t text_space, con printf("\n"); } +#ifdef ENABLE_INTEL_BACKEND +bool print_gpufetch_intel(struct gpu_info* gpu, STYLE s, struct color** cs, struct terminal* term) { + return false; +} +#endif + +#ifdef ENABLE_CUDA_BACKEND bool print_gpufetch_cuda(struct gpu_info* gpu, STYLE s, struct color** cs, struct terminal* term) { struct ascii* art = set_ascii(get_gpu_vendor(gpu), s); @@ -416,6 +423,7 @@ bool print_gpufetch_cuda(struct gpu_info* gpu, STYLE s, struct color** cs, struc return true; } +#endif struct terminal* get_terminal_size() { struct terminal* term = (struct terminal*) emalloc(sizeof(struct terminal)); @@ -448,5 +456,17 @@ struct terminal* get_terminal_size() { bool print_gpufetch(struct gpu_info* gpu, STYLE s, struct color** cs) { struct terminal* term = get_terminal_size(); - return print_gpufetch_cuda(gpu, s, cs, term); + if(gpu->vendor == GPU_VENDOR_NVIDIA) + #ifdef ENABLE_CUDA_BACKEND + return print_gpufetch_cuda(gpu, s, cs, term); + #else + return false; + #endif + else { + #ifdef ENABLE_INTEL_BACKEND + return print_gpufetch_intel(gpu, s, cs, term); + #else + return false; + #endif + } } diff --git a/src/cuda/cuda.cpp b/src/cuda/cuda.cpp index c75efb9..a400145 100644 --- a/src/cuda/cuda.cpp +++ b/src/cuda/cuda.cpp @@ -6,7 +6,7 @@ #include "../common/pci.hpp" #include "../common/global.hpp" -int print_gpus_list() { +int print_gpus_list_deprecated() { cudaError_t err = cudaSuccess; int num_gpus = -1; @@ -113,7 +113,7 @@ int64_t get_peak_performance_t(struct gpu_info* gpu) { return gpu->freq * 1000000 * 4 * 4 * 8 * gpu->topo->tensor_cores; } -struct gpu_info* get_gpu_info(int gpu_idx) { +struct gpu_info* get_gpu_info_cuda(int gpu_idx) { struct gpu_info* gpu = (struct gpu_info*) emalloc(sizeof(struct gpu_info)); gpu->pci = NULL; gpu->idx = gpu_idx; diff --git a/src/cuda/cuda.hpp b/src/cuda/cuda.hpp index a132675..076ba76 100644 --- a/src/cuda/cuda.hpp +++ b/src/cuda/cuda.hpp @@ -1,10 +1,9 @@ -#ifndef __CUDA__ -#define __CUDA__ +#ifndef __CUDA_GPU__ +#define __CUDA_GPU__ #include "../common/gpu.hpp" -struct gpu_info* get_gpu_info(int gpu_idx); -int print_gpus_list(); +struct gpu_info* get_gpu_info_cuda(int gpu_idx); char* get_str_sm(struct gpu_info* gpu); char* get_str_cores_sm(struct gpu_info* gpu); char* get_str_cuda_cores(struct gpu_info* gpu); diff --git a/src/cuda/uarch.hpp b/src/cuda/uarch.hpp index 86355fe..ee64833 100644 --- a/src/cuda/uarch.hpp +++ b/src/cuda/uarch.hpp @@ -1,5 +1,5 @@ -#ifndef __UARCH__ -#define __UARCH__ +#ifndef __CUDA_UARCH__ +#define __CUDA_UARCH__ #include "../common/gpu.hpp" diff --git a/src/intel/intel.cpp b/src/intel/intel.cpp new file mode 100644 index 0000000..da27624 --- /dev/null +++ b/src/intel/intel.cpp @@ -0,0 +1,10 @@ +#include "intel.hpp" +#include "uarch.hpp" +#include "../common/pci.hpp" +#include "../common/global.hpp" + +struct gpu_info* get_gpu_info_intel() { + struct gpu_info* gpu = (struct gpu_info*) emalloc(sizeof(struct gpu_info)); + + return gpu; +} diff --git a/src/intel/intel.hpp b/src/intel/intel.hpp new file mode 100644 index 0000000..7fd5e9a --- /dev/null +++ b/src/intel/intel.hpp @@ -0,0 +1,8 @@ +#ifndef __INTEL_GPU__ +#define __INTEL_GPU__ + +#include "../common/gpu.hpp" + +struct gpu_info* get_gpu_info_intel(int gpu_idx); + +#endif diff --git a/src/intel/uarch.hpp b/src/intel/uarch.hpp new file mode 100644 index 0000000..2076854 --- /dev/null +++ b/src/intel/uarch.hpp @@ -0,0 +1,6 @@ +#ifndef __INTEL_UARCH__ +#define __INTEL_UARCH__ + +#include "../common/gpu.hpp" + +#endif