[v0.04] Add option to select GPU

This commit is contained in:
Dr-Noob
2021-08-16 13:38:54 +02:00
parent eecc040e98
commit c7a08e7fd0
6 changed files with 98 additions and 17 deletions

View File

@@ -2,30 +2,81 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <climits>
#include "args.hpp" #include "args.hpp"
#include "global.hpp" #include "global.hpp"
#define OVERFLOW -1
#define UNDERFLOW -2
#define INVALID_ARG -3
#define NUM_COLORS 4 #define NUM_COLORS 4
struct args_struct { struct args_struct {
bool help_flag; bool help_flag;
bool version_flag; bool version_flag;
int gpu_idx;
STYLE style; STYLE style;
struct color** colors; struct color** colors;
}; };
int errn = 0;
static struct args_struct args;
const char args_chr[] = { const char args_chr[] = {
/* [ARG_CHAR_GPU] = */ 'g',
/* [ARG_CHAR_HELP] = */ 'h', /* [ARG_CHAR_HELP] = */ 'h',
/* [ARG_CHAR_VERSION] = */ 'V', /* [ARG_CHAR_VERSION] = */ 'V',
}; };
const char *args_str[] = { const char *args_str[] = {
/* [ARG_CHAR_GPU] = */ "gpu",
/* [ARG_CHAR_HELP] = */ "help", /* [ARG_CHAR_HELP] = */ "help",
/* [ARG_CHAR_VERSION] = */ "version", /* [ARG_CHAR_VERSION] = */ "version",
}; };
static struct args_struct args; int getarg_int(char* str) {
errn = 0;
char* endptr;
long tmp = strtol(str, &endptr, 10);
if(*endptr) {
errn = INVALID_ARG;
return -1;
}
if(tmp == LONG_MIN) {
errn = UNDERFLOW;
return -1;
}
if(tmp == LONG_MAX) {
errn = OVERFLOW;
return -1;
}
if(tmp >= INT_MIN && tmp <= INT_MAX) {
return (int)tmp;
}
errn = OVERFLOW;
return -1;
}
void print_getarg_error() {
switch (errn) {
case OVERFLOW:
printf("overflow detected while parsing the arguments\n");
break;
case UNDERFLOW:
printf("underflow detected while parsing the arguments\n");
break;
case INVALID_ARG:
printf("invalid argument\n");
break;
default:
printf("invalid error: %d\n", errn);
break;
}
}
STYLE get_style() { STYLE get_style() {
return args.style; return args.style;
@@ -35,6 +86,10 @@ struct color** get_colors() {
return args.colors; return args.colors;
} }
int get_gpu_idx() {
return args.gpu_idx;
}
bool show_help() { bool show_help() {
return args.help_flag; return args.help_flag;
} }
@@ -58,7 +113,7 @@ char* build_short_options() {
char* str = (char *) emalloc(sizeof(char) * (len*2 + 1)); char* str = (char *) emalloc(sizeof(char) * (len*2 + 1));
memset(str, 0, sizeof(char) * (len*2 + 1)); memset(str, 0, sizeof(char) * (len*2 + 1));
sprintf(str, "%c%c", sprintf(str, "%c:%c%c", c[ARG_GPU],
c[ARG_HELP], c[ARG_VERSION]); c[ARG_HELP], c[ARG_VERSION]);
return str; return str;
@@ -71,8 +126,10 @@ bool parse_args(int argc, char* argv[]) {
args.version_flag = false; args.version_flag = false;
args.help_flag = false; args.help_flag = false;
args.gpu_idx = 0;
const struct option long_options[] = { const struct option long_options[] = {
{args_str[ARG_GPU], required_argument, 0, args_chr[ARG_GPU] },
{args_str[ARG_HELP], no_argument, 0, args_chr[ARG_HELP] }, {args_str[ARG_HELP], no_argument, 0, args_chr[ARG_HELP] },
{args_str[ARG_VERSION], no_argument, 0, args_chr[ARG_VERSION] }, {args_str[ARG_VERSION], no_argument, 0, args_chr[ARG_VERSION] },
{0, 0, 0, 0} {0, 0, 0, 0}
@@ -82,7 +139,16 @@ bool parse_args(int argc, char* argv[]) {
opt = getopt_long(argc, argv, short_options, long_options, &option_index); 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 && opt != -1) {
if(opt == args_chr[ARG_HELP]) { if(opt == args_chr[ARG_GPU]) {
args.gpu_idx = getarg_int(optarg);
if(errn != 0) {
printErr("Option %s: ", args_str[ARG_GPU]);
print_getarg_error();
args.help_flag = true;
return false;
}
}
else if(opt == args_chr[ARG_HELP]) {
args.help_flag = true; args.help_flag = true;
} }
else if(opt == args_chr[ARG_VERSION]) { else if(opt == args_chr[ARG_VERSION]) {

View File

@@ -19,6 +19,7 @@ enum {
}; };
enum { enum {
ARG_GPU,
ARG_HELP, ARG_HELP,
ARG_VERSION ARG_VERSION
}; };
@@ -33,6 +34,7 @@ bool parse_args(int argc, char* argv[]);
bool show_help(); bool show_help();
bool show_version(); bool show_version();
void free_colors_struct(struct color** cs); void free_colors_struct(struct color** cs);
int get_gpu_idx();
struct color** get_colors(); struct color** get_colors();
STYLE get_style(); STYLE get_style();

View File

@@ -18,6 +18,7 @@ void print_help(char *argv[]) {
printf("Simple yet fancy GPU architecture fetching tool\n\n"); printf("Simple yet fancy GPU architecture fetching tool\n\n");
printf("Options: \n"); printf("Options: \n");
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 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 Prints gpufetch version and exit\n", c[ARG_VERSION], t[ARG_VERSION], (int) (max_len-strlen(t[ARG_VERSION])), "");
@@ -56,7 +57,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\ 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"); any inconsistencies to https://github.com/Dr-Noob/gpufetch/issues");
struct gpu_info* gpu = get_gpu_info(); struct gpu_info* gpu = get_gpu_info(get_gpu_idx());
if(gpu == NULL) if(gpu == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;

View File

@@ -399,12 +399,6 @@ bool print_gpufetch_cuda(struct gpu_info* gpu, STYLE s, struct color** cs, struc
free(art->attributes); free(art->attributes);
free(art); free(art);
/* if(cs != NULL) free_colors_struct(cs);
free_cache_struct(cpu->cach);
free_topo_struct(cpu->topo);
free_freq_struct(cpu->freq);
free_cpuinfo_struct(cpu);*/
return true; return true;
} }

View File

@@ -64,25 +64,43 @@ int64_t get_peak_performance(struct gpu_info* gpu) {
return gpu->freq * 1000000 * gpu->topo->cuda_cores * 2; return gpu->freq * 1000000 * gpu->topo->cuda_cores * 2;
} }
struct gpu_info* get_gpu_info() { struct gpu_info* get_gpu_info(int gpu_idx) {
struct gpu_info* gpu = (struct gpu_info*) emalloc(sizeof(struct gpu_info)); struct gpu_info* gpu = (struct gpu_info*) emalloc(sizeof(struct gpu_info));
gpu->pci = NULL; gpu->pci = NULL;
if(gpu_idx < 0) {
printErr("GPU index must be equal or greater than zero");
return NULL;
}
printf("Waiting for CUDA driver to start..."); printf("Waiting for CUDA driver to start...");
fflush(stdout); fflush(stdout);
int dev = 0;
cudaSetDevice(dev); int num_gpus = -1;
cudaDeviceProp deviceProp; cudaGetDeviceCount(&num_gpus);
cudaGetDeviceProperties(&deviceProp, dev);
printf("\r "); printf("\r ");
if(num_gpus <= 0) {
printErr("No CUDA capable devices found!");
return NULL;
}
if(gpu_idx+1 > num_gpus) {
printErr("Requested GPU index %d in a system with %d GPUs", gpu_idx, num_gpus);
return NULL;
}
cudaSetDevice(gpu_idx);
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, gpu_idx);
gpu->freq = deviceProp.clockRate * 1e-3f; gpu->freq = deviceProp.clockRate * 1e-3f;
gpu->vendor = GPU_VENDOR_NVIDIA; gpu->vendor = GPU_VENDOR_NVIDIA;
gpu->name = (char *) emalloc(sizeof(char) * (strlen(deviceProp.name) + 1)); gpu->name = (char *) emalloc(sizeof(char) * (strlen(deviceProp.name) + 1));
strcpy(gpu->name, deviceProp.name); strcpy(gpu->name, deviceProp.name);
gpu->nvmld = nvml_init(); gpu->nvmld = nvml_init();
if(nvml_get_pci_info(dev, gpu->nvmld)) { if(nvml_get_pci_info(gpu_idx, gpu->nvmld)) {
gpu->pci = get_pci_from_nvml(gpu->nvmld); gpu->pci = get_pci_from_nvml(gpu->nvmld);
} }

View File

@@ -3,7 +3,7 @@
#include "../common/gpu.hpp" #include "../common/gpu.hpp"
struct gpu_info* get_gpu_info(); struct gpu_info* get_gpu_info(int gpu_idx);
char* get_str_sm(struct gpu_info* gpu); char* get_str_sm(struct gpu_info* gpu);
char* get_str_cores_sm(struct gpu_info* gpu); char* get_str_cores_sm(struct gpu_info* gpu);
char* get_str_cuda_cores(struct gpu_info* gpu); char* get_str_cuda_cores(struct gpu_info* gpu);