Skip to content

Commit

Permalink
Merge pull request #174 from krasznaa/CUDADeviceName-main-20220412
Browse files Browse the repository at this point in the history
CUDA Device Name Printout, main branch (2022.04.12.)
  • Loading branch information
krasznaa authored Apr 12, 2022
2 parents ca734fd + 2d18101 commit 611f45f
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 12 deletions.
8 changes: 3 additions & 5 deletions cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
# VecMem project, part of the ACTS project (R&D line)
#
# (c) 2021 CERN for the benefit of the ACTS project
# (c) 2021-2022 CERN for the benefit of the ACTS project
#
# Mozilla Public License Version 2.0

# CUDAToolkit requires CMake 3.17.
cmake_minimum_required( VERSION 3.17 )

# Enable CUDA as a language.
enable_language( CUDA )

# Project include(s).
include( vecmem-compiler-options-cpp )
include( vecmem-compiler-options-cuda )

# External dependency/dependencies.
find_package( CUDAToolkit REQUIRED )
Expand All @@ -35,6 +31,8 @@ vecmem_add_library( vecmem_cuda cuda
"src/utils/cuda_error_handling.cpp"
"src/utils/cuda_wrappers.hpp"
"src/utils/cuda_wrappers.cpp"
"src/utils/get_device_name.hpp"
"src/utils/get_device_name.cpp"
"src/utils/select_device.hpp"
"src/utils/select_device.cpp"
"include/vecmem/utils/cuda/stream_wrapper.hpp"
Expand Down
9 changes: 7 additions & 2 deletions cuda/src/memory/cuda/device_memory_resource.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* VecMem project, part of the ACTS project (R&D line)
*
* (c) 2021 CERN for the benefit of the ACTS project
* (c) 2021-2022 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand All @@ -11,6 +11,7 @@

#include "../../utils/cuda_error_handling.hpp"
#include "../../utils/cuda_wrappers.hpp"
#include "../../utils/get_device_name.hpp"
#include "../../utils/select_device.hpp"
#include "vecmem/utils/debug.hpp"

Expand All @@ -20,7 +21,11 @@
namespace vecmem::cuda {

device_memory_resource::device_memory_resource(int device)
: m_device(device >= 0 ? device : details::get_device()) {}
: m_device(device >= 0 ? device : details::get_device()) {

VECMEM_DEBUG_MSG(2, "Created device memory resource on: %s",
details::get_device_name(m_device).c_str());
}

void *device_memory_resource::do_allocate(std::size_t bytes, std::size_t) {

Expand Down
39 changes: 39 additions & 0 deletions cuda/src/utils/get_device_name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* VecMem project, part of the ACTS project (R&D line)
*
* (c) 2022 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

// Local include(s).
#include "get_device_name.hpp"

// CUDA include(s).
#include <cuda_runtime_api.h>

// System include(s).
#include <sstream>

namespace vecmem {
namespace cuda {
namespace details {

std::string get_device_name(int device) {

// Get the device's properties.
cudaDeviceProp props;
if (cudaGetDeviceProperties(&props, device) != cudaSuccess) {
return "Unknown";
}

// Construct a unique name out of those properties.
std::ostringstream result;
result << props.name << " [id: " << device << ", bus: " << props.pciBusID
<< ", device: " << props.pciDeviceID << "]";
return result.str();
}

} // namespace details
} // namespace cuda
} // namespace vecmem
29 changes: 29 additions & 0 deletions cuda/src/utils/get_device_name.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* VecMem project, part of the ACTS project (R&D line)
*
* (c) 2022 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
#pragma once

// System include(s).
#include <string>

namespace vecmem {
namespace cuda {
namespace details {

/// Get a "fully qualified" name for a given CUDA device
///
/// This function provides a uniform way for printing the names of the
/// devices that various VecMem objects would be interacting with.
///
/// @param device The device ID that the CUDA runtime assigned
/// @return A user friently name for the device
///
std::string get_device_name(int device);

} // namespace details
} // namespace cuda
} // namespace vecmem
10 changes: 8 additions & 2 deletions cuda/src/utils/select_device.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* VecMem project, part of the ACTS project (R&D line)
*
* (c) 2021 CERN for the benefit of the ACTS project
* (c) 2021-2022 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand All @@ -28,4 +28,10 @@ select_device::~select_device() {
*/
VECMEM_CUDA_ERROR_CHECK(cudaSetDevice(m_device));
}
} // namespace vecmem::cuda::details

int select_device::device() const {

return m_device;
}

} // namespace vecmem::cuda::details
9 changes: 7 additions & 2 deletions cuda/src/utils/select_device.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* VecMem project, part of the ACTS project (R&D line)
*
* (c) 2021 CERN for the benefit of the ACTS project
* (c) 2021-2022 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand Down Expand Up @@ -39,11 +39,16 @@ class select_device {
*/
~select_device();

/**
* @brief Identifier for the device being seleced
*/
int device() const;

private:
/**
* @brief The old device number, this is what we restore when the
* object goes out of scope.
*/
int m_device;
};
} // namespace vecmem::cuda::details
} // namespace vecmem::cuda::details
10 changes: 9 additions & 1 deletion cuda/src/utils/stream_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** VecMem project, part of the ACTS project (R&D line)
*
* (c) 2021 CERN for the benefit of the ACTS project
* (c) 2021-2022 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand All @@ -10,9 +10,13 @@

#include "cuda_error_handling.hpp"
#include "cuda_wrappers.hpp"
#include "get_device_name.hpp"
#include "opaque_stream.hpp"
#include "select_device.hpp"

// VecMem include(s).
#include "vecmem/utils/debug.hpp"

// CUDA include(s).
#include <cuda_runtime_api.h>

Expand All @@ -28,6 +32,10 @@ stream_wrapper::stream_wrapper(int device)
// Construct the stream.
m_managedStream = std::make_shared<details::opaque_stream>();
m_stream = m_managedStream->m_stream;

// Tell the user what happened.
VECMEM_DEBUG_MSG(2, "Created stream on device: %s",
details::get_device_name(dev_selector.device()).c_str());
}

stream_wrapper::stream_wrapper(void* stream)
Expand Down

0 comments on commit 611f45f

Please sign in to comment.