Skip to content

Commit

Permalink
add scope::system::hip_devices
Browse files Browse the repository at this point in the history
  • Loading branch information
cwpearson committed Nov 8, 2022
1 parent f9250d9 commit 8a402b9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
40 changes: 40 additions & 0 deletions include/scope/device.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include "scope/hip.hpp"

class Device {
public:

enum class Kind {
hip
};

Device(const Kind &kind) : kind_(kind) {



}

int device_id() const;

static Device hip_device(int id) {
Device device(Kind::hip);
device.id_ = id;
HIP_RUNTIME(hipGetDeviceProperties(&device.hipDeviceProp_, id));
return device;
}

/*
HIP: returns hipDeviceProp_t::canMapHostMemory
*/
bool can_map_host_memory() const;

private:
Kind kind_;
int id_;

#if defined(SCOPE_USE_HIP)
hipDeviceProp_t hipDeviceProp_;
#endif
};

3 changes: 2 additions & 1 deletion include/scope/system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <vector>

#include "scope/device.hpp"
#include "scope/memory_space.hpp"
#include "scope/numa.hpp"

Expand Down Expand Up @@ -32,7 +33,7 @@ std::vector<TransferMethod> transfer_methods(const MemorySpace &src, const Memor

/* return HIP device IDs that the process can execute on
*/
std::vector<int> hip_device_ids();
std::vector<Device> hip_devices();

} // namespace system
} // namespace scope
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set(SCOPE_SOURCES ${SCOPE_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/cache.cpp
${CMAKE_CURRENT_LIST_DIR}/device.cpp
${CMAKE_CURRENT_LIST_DIR}/flags.cpp
${CMAKE_CURRENT_LIST_DIR}/governor.cpp
${CMAKE_CURRENT_LIST_DIR}/init.cpp
Expand Down
17 changes: 17 additions & 0 deletions src/device.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "scope/device.hpp"

int Device::device_id() const {
switch (kind_) {
case Kind::hip: return id_;
default:
throw std::runtime_error("can't get device_id for Device kind");
}
}

bool Device::can_map_host_memory() const {
switch (kind_) {
case Kind::hip: return hipDeviceProp_.canMapHostMemory;
default:
throw std::runtime_error("can't get can_map_host_memory for Device kind");
}
}
16 changes: 8 additions & 8 deletions src/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ namespace system {
std::vector<MemorySpace> hip_memory_spaces() {
std::vector<MemorySpace> ret;

const std::vector<int> hipIds = hip_device_ids();
for (const auto &id : hipIds) {
ret.push_back(MemorySpace::hip_device_space(id));
const std::vector<Device> hips = hip_devices();
for (const Device &hip : hips) {
ret.push_back(MemorySpace::hip_device_space(hip.device_id()));
}

const std::vector<int> numaIds = numa::nodes();
for (const auto &numaId : numaIds) {
for (const auto &hipId : hipIds) {
ret.push_back(MemorySpace::hip_mapped_pinned(hipId, numaId));
for (const Device &hip : hips) {
ret.push_back(MemorySpace::hip_mapped_pinned(hip.device_id(), numaId));
}
}

Expand Down Expand Up @@ -121,14 +121,14 @@ std::vector<TransferMethod> transfer_methods(const MemorySpace &src, const Memor
return {};
}

std::vector<int> hip_device_ids() {
std::vector<int> ret;
std::vector<Device> hip_devices() {
std::vector<Device> ret;
#if defined(SCOPE_USE_HIP)
int ndev;
HIP_RUNTIME(hipGetDeviceCount(&ndev));
for (int i = 0; i < ndev; ++i) {
if (scope::flags::gpu_is_visible(i)) {
ret.push_back(i);
ret.push_back(Device::hip_device(i));
}
}
#endif
Expand Down

0 comments on commit 8a402b9

Please sign in to comment.