Squashed commit of the following:
commit 25e7cb77683736a588acb6b30a8ac89e2bd7f56a Author: Carl Pearson <pearson@illinois.edu> Date: Fri Sep 20 13:25:49 2019 -0500 automatically define PERFECT_HAS_CUDA with nvcc commit fcc699c165ba515619781aefb378d3c0c4d1093d Author: Carl Pearson <pearson@illinois.edu> Date: Fri Sep 20 13:18:42 2019 -0500 optional CUDA support
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <cerrno>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
#include <sched.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
@@ -46,7 +50,14 @@ Result set_governor(const int cpu, const std::string &governor) {
|
||||
ofs << governor;
|
||||
ofs.close();
|
||||
if (ofs.fail()) {
|
||||
return Result::NO_PERMISSION;
|
||||
switch (errno) {
|
||||
case EACCES:
|
||||
return Result::NO_PERMISSION;
|
||||
case ENOENT:
|
||||
return Result::NOT_SUPPORTED;
|
||||
default:
|
||||
return Result::UNKNOWN;
|
||||
}
|
||||
}
|
||||
return Result::SUCCESS;
|
||||
}
|
||||
|
@@ -1,6 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __NVCC__
|
||||
#define PERFECT_HAS_CUDA
|
||||
#endif
|
||||
|
||||
#ifdef PERFECT_HAS_CUDA
|
||||
#include <nvml.h>
|
||||
#endif
|
||||
|
||||
namespace perfect {
|
||||
|
||||
@@ -11,11 +17,13 @@ Result init() {
|
||||
if (init_)
|
||||
return Result::SUCCESS;
|
||||
|
||||
// init nvml
|
||||
// init nvml
|
||||
#ifdef PERFECT_HAS_CUDA
|
||||
nvmlReturn_t ret = nvmlInit();
|
||||
if (ret != NVML_SUCCESS) {
|
||||
return from_nvml(ret);
|
||||
}
|
||||
#endif
|
||||
|
||||
// don't init again if init() called twice
|
||||
init_ = true;
|
||||
|
@@ -2,22 +2,27 @@
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#ifdef __NVCC__
|
||||
#define PERFECT_HAS_CUDA
|
||||
#endif
|
||||
|
||||
#ifdef PERFECT_HAS_CUDA
|
||||
#include <nvml.h>
|
||||
#endif
|
||||
|
||||
namespace perfect {
|
||||
|
||||
enum class Result {
|
||||
SUCCESS,
|
||||
NVML_NOT_SUPPORTED,
|
||||
NVML_NO_PERMISSION,
|
||||
NVML_UNINITIALIZED,
|
||||
NO_PERMISSION,
|
||||
NOT_SUPPORTED,
|
||||
NVML_NO_PERMISSION,
|
||||
NVML_NOT_SUPPORTED,
|
||||
NVML_UNINITIALIZED,
|
||||
SUCCESS,
|
||||
UNKNOWN
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef PERFECT_HAS_CUDA
|
||||
Result from_nvml(nvmlReturn_t nvml) {
|
||||
switch (nvml) {
|
||||
case NVML_SUCCESS:
|
||||
@@ -26,6 +31,8 @@ Result from_nvml(nvmlReturn_t nvml) {
|
||||
return Result::NVML_UNINITIALIZED;
|
||||
case NVML_ERROR_NOT_SUPPORTED:
|
||||
return Result::NVML_NOT_SUPPORTED;
|
||||
case NVML_ERROR_NO_PERMISSION:
|
||||
return Result::NVML_NO_PERMISSION;
|
||||
case NVML_ERROR_INVALID_ARGUMENT:
|
||||
case NVML_ERROR_GPU_IS_LOST:
|
||||
case NVML_ERROR_UNKNOWN:
|
||||
@@ -34,6 +41,7 @@ Result from_nvml(nvmlReturn_t nvml) {
|
||||
}
|
||||
return Result::UNKNOWN;
|
||||
}
|
||||
#endif
|
||||
|
||||
const char *get_string(const Result &result) {
|
||||
switch (result) {
|
||||
@@ -47,6 +55,8 @@ const char *get_string(const Result &result) {
|
||||
return "nvidia-ml returned not supported";
|
||||
case Result::NVML_NO_PERMISSION:
|
||||
return "nvidia-ml returned no permission";
|
||||
case Result::NOT_SUPPORTED:
|
||||
return "unsupported operation";
|
||||
default:
|
||||
assert(0 && "unexpected perfect::Result");
|
||||
}
|
||||
@@ -58,7 +68,7 @@ const char *get_string(const Result &result) {
|
||||
inline void check(Result result, const char *file, const int line) {
|
||||
if (result != Result::SUCCESS) {
|
||||
fprintf(stderr, "%s@%d: perfect Error: %s\n", file, line,
|
||||
get_string(result));
|
||||
get_string(result));
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user