diff --git a/src/common/args.cpp b/src/common/args.cpp index 25ecb8d..e72f291 100644 --- a/src/common/args.cpp +++ b/src/common/args.cpp @@ -19,6 +19,7 @@ struct args_struct { bool help_flag; bool version_flag; + bool list_gpus; int gpu_idx; STYLE style; struct color** colors; @@ -28,17 +29,19 @@ int errn = 0; static struct args_struct args; const char args_chr[] = { - /* [ARG_CHAR_COLOR] = */ 'c', - /* [ARG_CHAR_GPU] = */ 'g', - /* [ARG_CHAR_HELP] = */ 'h', - /* [ARG_CHAR_VERSION] = */ 'V', + /* [ARG_COLOR] = */ 'c', + /* [ARG_GPU] = */ 'g', + /* [ARG_LIST] = */ 'l', + /* [ARG_HELP] = */ 'h', + /* [ARG_VERSION] = */ 'V', }; const char *args_str[] = { - /* [ARG_CHAR_COLOR] = */ "color", - /* [ARG_CHAR_GPU] = */ "gpu", - /* [ARG_CHAR_HELP] = */ "help", - /* [ARG_CHAR_VERSION] = */ "version", + /* [ARG_COLOR] = */ "color", + /* [ARG_GPU] = */ "gpu", + /* [ARG_LIST] = */ "list-gpus", + /* [ARG_HELP] = */ "help", + /* [ARG_VERSION] = */ "version", }; int getarg_int(char* str) { @@ -100,6 +103,10 @@ bool show_help() { return args.help_flag; } +bool list_gpus() { + return args.list_gpus; +} + bool show_version() { return args.version_flag; } @@ -119,8 +126,9 @@ char* build_short_options() { char* str = (char *) emalloc(sizeof(char) * (len*2 + 1)); memset(str, 0, sizeof(char) * (len*2 + 1)); - sprintf(str, "%c:%c:%c%c", c[ARG_GPU], - c[ARG_COLOR], c[ARG_HELP], c[ARG_VERSION]); + sprintf(str, "%c:%c:%c%c%c", c[ARG_GPU], + c[ARG_COLOR], c[ARG_HELP], c[ARG_LIST], + c[ARG_VERSION]); return str; } @@ -185,12 +193,14 @@ bool parse_args(int argc, char* argv[]) { args.version_flag = false; args.help_flag = false; + args.list_gpus = false; args.gpu_idx = 0; args.colors = NULL; const struct option long_options[] = { {args_str[ARG_COLOR], required_argument, 0, args_chr[ARG_COLOR] }, {args_str[ARG_GPU], required_argument, 0, args_chr[ARG_GPU] }, + {args_str[ARG_LIST], no_argument, 0, args_chr[ARG_LIST] }, {args_str[ARG_HELP], no_argument, 0, args_chr[ARG_HELP] }, {args_str[ARG_VERSION], no_argument, 0, args_chr[ARG_VERSION] }, {0, 0, 0, 0} @@ -199,7 +209,7 @@ bool parse_args(int argc, char* argv[]) { char* short_options = build_short_options(); opt = getopt_long(argc, argv, short_options, long_options, &option_index); - while (!args.help_flag && !args.version_flag && opt != -1) { + while (!args.help_flag && !args.version_flag && !args.list_gpus && opt != -1) { if(opt == args_chr[ARG_COLOR]) { args.colors = (struct color **) emalloc(sizeof(struct color *) * NUM_COLORS); if(!parse_color(optarg, &args.colors)) { @@ -215,8 +225,11 @@ bool parse_args(int argc, char* argv[]) { return false; } } + else if(opt == args_chr[ARG_LIST]) { + args.list_gpus = true; + } else if(opt == args_chr[ARG_HELP]) { - args.help_flag = true; + args.help_flag = true; } else if(opt == args_chr[ARG_VERSION]) { args.version_flag = true; diff --git a/src/common/args.hpp b/src/common/args.hpp index 18ecb1e..401e6c4 100644 --- a/src/common/args.hpp +++ b/src/common/args.hpp @@ -21,6 +21,7 @@ enum { enum { ARG_COLOR, ARG_GPU, + ARG_LIST, ARG_HELP, ARG_VERSION }; @@ -33,6 +34,7 @@ extern const char *args_str[]; int max_arg_str_length(); bool parse_args(int argc, char* argv[]); bool show_help(); +bool list_gpus(); bool show_version(); void free_colors_struct(struct color** cs); int get_gpu_idx(); diff --git a/src/common/main.cpp b/src/common/main.cpp index b745002..7f27252 100644 --- a/src/common/main.cpp +++ b/src/common/main.cpp @@ -18,10 +18,11 @@ void print_help(char *argv[]) { printf("Simple yet fancy GPU architecture fetching tool\n\n"); printf("Options: \n"); - printf(" -%c, --%s %*s Sets the color scheme (by default, gpufetch uses the system color scheme) See COLORS section for a more detailed explanation\n", c[ARG_COLOR], t[ARG_COLOR], (int) (max_len-strlen(t[ARG_COLOR])), ""); - printf(" -%c, --%s %*s Selects the GPU to use (default: 0)\n", c[ARG_GPU], t[ARG_GPU], (int) (max_len-strlen(t[ARG_GPU])), ""); - printf(" -%c, --%s %*s Prints this help and exit\n", c[ARG_HELP], t[ARG_HELP], (int) (max_len-strlen(t[ARG_HELP])), ""); - printf(" -%c, --%s %*s Prints gpufetch version and exit\n", c[ARG_VERSION], t[ARG_VERSION], (int) (max_len-strlen(t[ARG_VERSION])), ""); + printf(" -%c, --%s %*s Set the color scheme (by default, gpufetch uses the system color scheme) See COLORS section for a more detailed explanation\n", c[ARG_COLOR], t[ARG_COLOR], (int) (max_len-strlen(t[ARG_COLOR])), ""); + printf(" -%c, --%s %*s List the available GPUs in the system\n", c[ARG_LIST], t[ARG_LIST], (int) (max_len-strlen(t[ARG_LIST])), ""); + printf(" -%c, --%s %*s Select the GPU to use (default: 0)\n", c[ARG_GPU], t[ARG_GPU], (int) (max_len-strlen(t[ARG_GPU])), ""); + printf(" -%c, --%s %*s Print this help and exit\n", c[ARG_HELP], t[ARG_HELP], (int) (max_len-strlen(t[ARG_HELP])), ""); + printf(" -%c, --%s %*s Print gpufetch version and exit\n", c[ARG_VERSION], t[ARG_VERSION], (int) (max_len-strlen(t[ARG_VERSION])), ""); printf("\nCOLORS: \n"); printf(" Color scheme can be set using a predefined color scheme or a custom one:\n"); @@ -64,6 +65,10 @@ int main(int argc, char* argv[]) { return EXIT_SUCCESS; } + if(list_gpus()) { + return print_gpus_list(); + } + set_log_level(true); printWarn("gpufetch is in beta. The provided information may be incomplete or wrong.\n\ diff --git a/src/cuda/cuda.cpp b/src/cuda/cuda.cpp index 06e44d6..ddb00f2 100644 --- a/src/cuda/cuda.cpp +++ b/src/cuda/cuda.cpp @@ -6,6 +6,42 @@ #include "uarch.hpp" #include "../common/global.hpp" +int print_gpus_list() { + cudaError_t err = cudaSuccess; + int num_gpus = -1; + + if ((err = cudaGetDeviceCount(&num_gpus)) != cudaSuccess) { + printErr("%s: %s", cudaGetErrorName(err), cudaGetErrorString(err)); + return EXIT_FAILURE; + } + printf("CUDA GPUs available: %d\n", num_gpus); + + if(num_gpus > 0) { + cudaDeviceProp deviceProp; + int max_len = 0; + + for(int idx=0; idx < num_gpus; idx++) { + if ((err = cudaGetDeviceProperties(&deviceProp, idx)) != cudaSuccess) { + printErr("%s: %s", cudaGetErrorName(err), cudaGetErrorString(err)); + return EXIT_FAILURE; + } + max_len = max(max_len, (int) strlen(deviceProp.name)); + } + + for(int i=0; i < max_len + 32; i++) putchar('-'); + putchar('\n'); + for(int idx=0; idx < num_gpus; idx++) { + if ((err = cudaGetDeviceProperties(&deviceProp, idx)) != cudaSuccess) { + printErr("%s: %s", cudaGetErrorName(err), cudaGetErrorString(err)); + return EXIT_FAILURE; + } + printf("GPU %d: %s (Compute Capability %d.%d)\n", idx, deviceProp.name, deviceProp.major, deviceProp.minor); + } + } + + return EXIT_SUCCESS; +} + struct cache* get_cache_info(cudaDeviceProp prop) { struct cache* cach = (struct cache*) emalloc(sizeof(struct cache)); diff --git a/src/cuda/cuda.hpp b/src/cuda/cuda.hpp index cbd4d8f..057552a 100644 --- a/src/cuda/cuda.hpp +++ b/src/cuda/cuda.hpp @@ -4,6 +4,7 @@ #include "../common/gpu.hpp" struct gpu_info* get_gpu_info(int gpu_idx); +int print_gpus_list(); 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);