-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hardware): 实现 mlu 硬件相关的函数;改变编译方式按照以硬件名称命名的目录名区分是否需要编译
- Loading branch information
1 parent
535134b
commit bd6ad6b
Showing
13 changed files
with
167 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ namespace refactor::hardware { | |
enum class Type : int32_t { | ||
Cpu, | ||
Nvidia, | ||
Mlu, | ||
Kunlun, | ||
}; | ||
|
||
protected: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#ifndef HARDWARE_DEVICES_MLU_H | ||
#define HARDWARE_DEVICES_MLU_H | ||
|
||
#include "../device.h" | ||
|
||
namespace refactor::hardware { | ||
|
||
class Mlu final : public Device { | ||
public: | ||
explicit Mlu(int32_t card); | ||
void setContext() const noexcept final; | ||
Type type() const noexcept final { | ||
return Type::Mlu; | ||
} | ||
}; | ||
|
||
}// namespace refactor::hardware | ||
|
||
#endif// HARDWARE_DEVICES_MLU_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "functions.cc" | ||
#include "hardware/devices/mlu.h" | ||
#include "hardware/mem_pool.h" | ||
#include "memory.hh" | ||
|
||
namespace refactor::hardware { | ||
|
||
static Arc<Memory> bangMemory(int32_t card) { | ||
ASSERT(0 <= card && card < getDeviceCount(), "Invalid card id: {}", card); | ||
setDevice(card); | ||
auto [free, total] = getMemInfo(); | ||
auto size = std::min(free, std::max(5ul << 30, total * 4 / 5)); | ||
fmt::println("initializing Nvidia GPU {}, memory {} / {}, alloc {}", | ||
card, free, total, size); | ||
return std::make_shared<MemPool>( | ||
std::make_shared<MluMemory>(), | ||
size, | ||
256ul); | ||
} | ||
|
||
Mlu::Mlu(int32_t card) : Device(card, bangMemory(card)) {} | ||
|
||
void Mlu::setContext() const noexcept { | ||
setDevice(_card); | ||
} | ||
|
||
}// namespace refactor::hardware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "functions.hh" | ||
|
||
namespace refactor::hardware { | ||
|
||
int getDeviceCount() { | ||
int deviceCount; | ||
BANG_ASSERT(cnrtGetDeviceCount(&deviceCount)); | ||
return deviceCount; | ||
} | ||
void setDevice(int device) { | ||
BANG_ASSERT(cnrtSetDevice(device)); | ||
} | ||
MemInfo getMemInfo() { | ||
MemInfo memInfo; | ||
BANG_ASSERT(cudaMemGetInfo(&memInfo.free, &memInfo.total)); | ||
return memInfo; | ||
} | ||
|
||
}// namespace refactor::hardware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef HARDWARE_DEVICES_MLU_FUNCTIONS_CUH | ||
#define HARDWARE_DEVICES_MLU_FUNCTIONS_CUH | ||
|
||
#include "common.h" | ||
|
||
#define BANG_ASSERT(STATUS) \ | ||
if (auto status = (STATUS); status != CNRT_RET_SUCCESS) { \ | ||
RUNTIME_ERROR(fmt::format("bang failed on \"" #STATUS "\" with \"{}\" ({})", \ | ||
cnrtGetErrorStr(status), (int) status)); \ | ||
} | ||
|
||
namespace refactor::hardware { | ||
|
||
struct MemInfo { | ||
size_t free, total; | ||
}; | ||
|
||
int getDeviceCount(); | ||
void setDevice(int device); | ||
MemInfo getMemInfo(); | ||
|
||
}// namespace refactor::hardware | ||
|
||
#endif// HARDWARE_DEVICES_NVIDIA_FUNCTIONS_CUH |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include "memory.hh" | ||
#include "functions.hh" | ||
|
||
namespace refactor::hardware { | ||
using M = MluMemory; | ||
|
||
void *M::malloc(size_t size) { | ||
void *ptr; | ||
BANG_ASSERT(cnrtMalloc(&ptr, size)); | ||
return ptr; | ||
} | ||
void M::free(void *ptr) { | ||
BANG_ASSERT(cnrtFree(ptr)); | ||
} | ||
void *M::copyHD(void *dst, void const *src, size_t bytes) const { | ||
BANG_ASSERT(cnrtMemcpy(dst, const_cast<void *>(src), bytes, | ||
CNRT_MEM_TRANS_DIR_HOST2DEV)) | ||
return dst; | ||
} | ||
void *M::copyDH(void *dst, void const *src, size_t bytes) const { | ||
BANG_ASSERT(cnrtMemcpy(dst, const_cast<void *>(src), bytes, | ||
CNRT_MEM_TRANS_DIR_DEV2HOST)); | ||
return dst; | ||
} | ||
void *M::copyDD(void *dst, void const *src, size_t bytes) const { | ||
BANG_ASSERT(cnrtMemcpy(dst, const_cast<void *>(src), bytes, | ||
CNRT_MEM_TRANS_DIR_PEER2PEER)); | ||
return dst; | ||
} | ||
|
||
}// namespace refactor::hardware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef HARDWARE_DEVICES_MLU_MEMORY_CUH | ||
#define HARDWARE_DEVICES_MLU_MEMORY_CUH | ||
|
||
#include "hardware/memory.h" | ||
|
||
namespace refactor::hardware { | ||
|
||
class MluMemory final : public Memory { | ||
void *malloc(size_t) final; | ||
void free(void *) final; | ||
void *copyHD(void *dst, void const *src, size_t bytes) const final; | ||
void *copyDH(void *dst, void const *src, size_t bytes) const final; | ||
void *copyDD(void *dst, void const *src, size_t bytes) const final; | ||
}; | ||
|
||
}// namespace refactor::hardware | ||
|
||
#endif// HARDWARE_DEVICES_MLU_MEMORY_HH |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters