Skip to content

Commit

Permalink
Initial Nvml support.
Browse files Browse the repository at this point in the history
  • Loading branch information
xmrig committed Aug 24, 2017
1 parent e5633cf commit f09d280
Show file tree
Hide file tree
Showing 6 changed files with 338 additions and 1 deletion.
18 changes: 17 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ endif()
include(cmake/flags.cmake)
include(cmake/CUDA.cmake)

find_package(NVML)
if (NVML_FOUND)
include_directories(${NVML_INCLUDE_DIR})

set(SOURCES_NVML
src/nvidia/NvmlApi.h
src/nvidia/NvmlApi.cpp
)
else()
add_definitions(/DXMRIG_NO_NVML)

set(SOURCES_NVML
src/nvidia/NvmlApi.h
src/nvidia/NvmlApi_stub.cpp
)
endif()

CHECK_INCLUDE_FILE (syslog.h HAVE_SYSLOG_H)
if (HAVE_SYSLOG_H)
Expand All @@ -149,5 +165,5 @@ include_directories(${UV_INCLUDE_DIR})

add_subdirectory(src/3rdparty/jansson)

add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG})
add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_NVML} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG})
target_link_libraries(${PROJECT_NAME} xmrig-cuda jansson ${UV_LIBRARIES} ${LIBS} ${EXTRA_LIBS} ${CPUID_LIB})
141 changes: 141 additions & 0 deletions cmake/FindNVML.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#
# This file is part of the GROMACS molecular simulation package.
#
# Copyright (c) 2014,2015,2017, by the GROMACS development team, led by
# Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
# and including many others, as listed in the AUTHORS file in the
# top-level source directory and at http://www.gromacs.org.
#
# GROMACS is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# GROMACS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with GROMACS; if not, see
# http://www.gnu.org/licenses, or write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# If you want to redistribute modifications to GROMACS, please
# consider that scientific software is very special. Version
# control is crucial - bugs must be traceable. We will be happy to
# consider code for inclusion in the official distribution, but
# derived work must not be called official GROMACS. Details are found
# in the README & COPYING files - if they are missing, get the
# official version at http://www.gromacs.org.
#
# To help us fund GROMACS development, we humbly ask that you cite
# the research papers on the package. Check out http://www.gromacs.org.

#.rst:
# FindNVML
# --------
#
# Find the NVIDIA Management Library (NVML) includes and library. NVML documentation
# is available at: http://docs.nvidia.com/deploy/nvml-api/index.html
#
# Starting with CUDA 8 NVML is part of the CUDA Toolkit. Prior to CUDA 8 NVML was part
# of the GPU Deployment Kit (GDK) and GPU_DEPLOYMENT_KIT_ROOT_DIR can be specified
# if the GPU Deployment Kit is not installed in a default location.
#
# FindNVML defines the following variables:
#
# NVML_INCLUDE_DIR, where to find nvml.h, etc.
# NVML_LIBRARY, the libraries needed to use NVML.
# NVML_FOUND, If false, do not try to use NVML.
#

# Jiri Kraus, NVIDIA Corp (nvidia.com - jkraus)
#
# Copyright (c) 2008 - 2014,2017 NVIDIA Corporation. All rights reserved.
#
# This code is licensed under the MIT License. See the FindNVML.cmake script
# for the text of the license.

# The MIT License
#
# License for the specific language governing rights and limitations under
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
###############################################################################

if( CMAKE_SYSTEM_NAME STREQUAL "Windows" )
set(NVML_NAMES nvml)
if(${CUDA_VERSION_STRING} VERSION_LESS "8.0")
set( NVML_LIB_PATHS "C:/Program Files/NVIDIA Corporation/GDK/nvml/lib" )
if(GPU_DEPLOYMENT_KIT_ROOT_DIR)
list(APPEND NVML_LIB_PATHS "${GPU_DEPLOYMENT_KIT_ROOT_DIR}/nvml/lib")
endif()

set( NVML_INC_PATHS "C:/Program Files/NVIDIA Corporation/GDK/nvml/include" )
if(GPU_DEPLOYMENT_KIT_ROOT_DIR)
list(APPEND NVML_INC_PATHS "${GPU_DEPLOYMENT_KIT_ROOT_DIR}/nvml/include")
endif()
else()
set( NVML_LIB_PATHS "${CUDA_TOOLKIT_ROOT_DIR}/lib/x64" )
set( NVML_INC_PATHS ${CUDA_INCLUDE_DIRS} )
endif()
else()
set(NVML_NAMES nvidia-ml)

set( NVML_LIB_PATHS /usr/lib64 )
if(${CUDA_VERSION_STRING} VERSION_LESS "8.0")
# The Linux installer for the GPU Deployment Kit adds a "usr"
# suffix to a custom path if one is used, so a user could
# reasonably set GPU_DEPLOYMENT_KIT_ROOT_DIR to the value they
# passed to the installer, or the root where they later found the
# kit to be installed. Below, we cater for both possibilities.
if(GPU_DEPLOYMENT_KIT_ROOT_DIR)
list(APPEND NVML_LIB_PATHS
"${GPU_DEPLOYMENT_KIT_ROOT_DIR}/src/gdk/nvml/lib"
"${GPU_DEPLOYMENT_KIT_ROOT_DIR}/usr/src/gdk/nvml/lib"
)
endif()
else()
list(APPEND NVML_LIB_PATHS "${CUDA_TOOLKIT_ROOT_DIR}/lib64/stubs")
endif()

if(${CUDA_VERSION_STRING} VERSION_LESS "8.0")
set( NVML_INC_PATHS /usr/include/nvidia/gdk/ /usr/include )
if(GPU_DEPLOYMENT_KIT_ROOT_DIR)
list(APPEND NVML_INC_PATHS
"${GPU_DEPLOYMENT_KIT_ROOT_DIR}/include/nvidia/gdk"
"${GPU_DEPLOYMENT_KIT_ROOT_DIR}/usr/include/nvidia/gdk"
)
endif()
else()
set( NVML_INC_PATHS ${CUDA_INCLUDE_DIRS} )
endif()
endif()

find_library(NVML_LIBRARY NAMES ${NVML_NAMES} PATHS ${NVML_LIB_PATHS} )

find_path(NVML_INCLUDE_DIR nvml.h PATHS ${NVML_INC_PATHS})

# handle the QUIETLY and REQUIRED arguments and set NVML_FOUND to TRUE if
# all listed variables are TRUE
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(NVML DEFAULT_MSG NVML_LIBRARY NVML_INCLUDE_DIR)

mark_as_advanced(NVML_LIBRARY NVML_INCLUDE_DIR)
4 changes: 4 additions & 0 deletions src/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "donate.h"
#include "net/Url.h"
#include "nvidia/cryptonight.h"
#include "nvidia/NvmlApi.h"
#include "Options.h"
#include "Platform.h"
#include "version.h"
Expand Down Expand Up @@ -278,6 +279,8 @@ Options::Options(int argc, char **argv) :
m_bsleep = 100;
# endif

NvmlApi::init();

m_pools.push_back(new Url());

int key;
Expand Down Expand Up @@ -324,6 +327,7 @@ Options::Options(int argc, char **argv) :

Options::~Options()
{
NvmlApi::release();
}


Expand Down
96 changes: 96 additions & 0 deletions src/nvidia/NvmlApi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* XMRig
* Copyright 2010 Jeff Garzik <[email protected]>
* Copyright 2012-2014 pooler <[email protected]>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <[email protected]>
* Copyright 2016-2017 XMRig <[email protected]>
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


#include <nvml.h>
#include <uv.h>


#include "nvidia/NvmlApi.h"


static uv_lib_t nvmlLib;


bool NvmlApi::m_available = false;

static nvmlReturn_t(*pNvmlInit)(void) = nullptr;
static nvmlReturn_t(*pNvmlShutdown)(void) = nullptr;


bool NvmlApi::init()
{
# ifdef _WIN32
char tmp[512];
ExpandEnvironmentStringsA("%PROGRAMFILES%\\NVIDIA Corporation\\NVSMI\\nvml.dll", tmp, sizeof(tmp));
if (uv_dlopen(tmp, &nvmlLib) == -1 && uv_dlopen("nvml.dll", &nvmlLib) == -1) {
return false;
}
# else
if (uv_dlopen("libnvidia-ml.so", &nvmlLib) == -1) {
return false;
}
# endif

if (uv_dlsym(&nvmlLib, "nvmlInit_v2", reinterpret_cast<void**>(&pNvmlInit)) == -1) {
return false;
}

uv_dlsym(&nvmlLib, "nvmlShutdown", reinterpret_cast<void**>(&pNvmlShutdown));

m_available = pNvmlInit() == NVML_SUCCESS;

printf("OK %d\n", m_available);
return m_available;
}


void NvmlApi::release()
{
if (isAvailable() && !pNvmlShutdown) {
return;
}

pNvmlShutdown();

printf("release\n");
}


/*void NvmlApi::test()
{
nvmlDevice_t device;
nvmlReturn_t result = nvmlDeviceGetHandleByIndex(0, &device);
if (NVML_SUCCESS != result)
{
printf("Failed to get handle for device %i: %s\n", 0, nvmlErrorString(result));
}
unsigned int temp;
result = nvmlDeviceGetTemperature(device, NVML_TEMPERATURE_GPU, &temp);
if (NVML_SUCCESS != result) {
printf("Failed to get temperature of device %i: %s\n", 0, nvmlErrorString(result));
}
printf("%d\n", temp);
}*/
41 changes: 41 additions & 0 deletions src/nvidia/NvmlApi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* XMRig
* Copyright 2010 Jeff Garzik <[email protected]>
* Copyright 2012-2014 pooler <[email protected]>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <[email protected]>
* Copyright 2016-2017 XMRig <[email protected]>
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __NVML_H__
#define __NVML_H__


class NvmlApi
{
public:
static bool init();
static void release();

static inline bool isAvailable() { return m_available; }

private:
static bool m_available;
};


#endif /* __NVML_H__ */
39 changes: 39 additions & 0 deletions src/nvidia/NvmlApi_stub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* XMRig
* Copyright 2010 Jeff Garzik <[email protected]>
* Copyright 2012-2014 pooler <[email protected]>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <[email protected]>
* Copyright 2016-2017 XMRig <[email protected]>
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


#include "workers/NvmlApi.h"


bool NvmlApi::m_available = false;


bool NvmlApi::init()
{
return false;
}


void NvmlApi::release()
{
}

0 comments on commit f09d280

Please sign in to comment.