Skip to content

Commit 73c607d

Browse files
authored
feat: support MemorySpace on Iluvatar, Moore Threads, and Cambricon (#41)
* refactor: move NVIDIA's `nvidia/checks.h` to `cuda/checks.h` for code sharing among CUDA-alike platforms * feat: support `MemorySpace` on Iluvatar * feat: support `MemorySpace` on Moore Threads * feat: support `MemorySpace` on Cambricon
1 parent fbaaa32 commit 73c607d

7 files changed

Lines changed: 125 additions & 4 deletions

File tree

‎src/cambricon/checks.h‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef INFINI_CCL_CAMBRICON_CHECKS_H_
2+
#define INFINI_CCL_CAMBRICON_CHECKS_H_
3+
4+
#include <cnrt.h>
5+
6+
#include <iostream>
7+
8+
#include "return_status_impl.h"
9+
10+
#define INFINI_CHECK_CNRT(result) \
11+
::infini::ccl::detail::CheckCnrtImpl((result), __FILE__, __LINE__)
12+
13+
namespace infini::ccl {
14+
15+
namespace detail {
16+
17+
inline ReturnStatus CheckCnrtImpl(cnrtRet_t cnrt_result, const char *file,
18+
int line) {
19+
if (cnrt_result != cnrtSuccess) {
20+
cnrtGetLastError();
21+
std::cerr << "CNRT error code: " << cnrt_result << " at line " << line
22+
<< " in " << file << std::endl;
23+
std::abort();
24+
}
25+
return ReturnStatus::kSuccess;
26+
}
27+
28+
} // namespace detail
29+
30+
} // namespace infini::ccl
31+
32+
#endif // INFINI_CCL_CAMBRICON_CHECKS_H_

‎src/cambricon/device_.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
#ifndef INFINI_CCL_CAMBRICON_DEVICE__H_
22
#define INFINI_CCL_CAMBRICON_DEVICE__H_
33

4+
#include <cnrt.h>
5+
6+
#include "checks.h"
47
#include "device.h"
58

69
namespace infini::ccl {
710

811
template <>
912
struct DeviceEnabled<Device::Type::kCambricon> : std::true_type {};
1013

14+
template <>
15+
MemorySpace GetMemorySpace<Device::Type::kCambricon>(const void* ptr) {
16+
if (!ptr) {
17+
return MemorySpace::kHost;
18+
}
19+
20+
cnrtPointerAttributes_t attr;
21+
INFINI_CHECK_CNRT(cnrtPointerGetAttributes(&attr, ptr));
22+
23+
if (attr.type == cnrtMemTypeDevice) {
24+
return MemorySpace::kDevice;
25+
}
26+
27+
return MemorySpace::kHost;
28+
}
29+
1130
} // namespace infini::ccl
1231

1332
#endif // INFINI_CCL_CAMBRICON_DEVICE__H_
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef INFINI_CCL_NVIDIA_CHECKS_H_
2-
#define INFINI_CCL_NVIDIA_CHECKS_H_
1+
#ifndef INFINI_CCL_CUDA_CHECKS_H_
2+
#define INFINI_CCL_CUDA_CHECKS_H_
33

44
#include <cuda_runtime.h>
55

@@ -29,4 +29,4 @@ inline ReturnStatus CheckCudaImpl(cudaError_t cuda_result, const char *file,
2929

3030
} // namespace infini::ccl
3131

32-
#endif // INFINI_CCL_NVIDIA_CHECKS_H_
32+
#endif // INFINI_CCL_CUDA_CHECKS_H_

‎src/iluvatar/device_.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
#ifndef INFINI_CCL_ILUVATAR_DEVICE__H_
22
#define INFINI_CCL_ILUVATAR_DEVICE__H_
33

4+
#include <cuda_runtime.h>
5+
6+
#include "cuda/checks.h"
47
#include "device.h"
58

69
namespace infini::ccl {
710

811
template <>
912
struct DeviceEnabled<Device::Type::kIluvatar> : std::true_type {};
1013

14+
template <>
15+
MemorySpace GetMemorySpace<Device::Type::kIluvatar>(const void* ptr) {
16+
if (!ptr) {
17+
return MemorySpace::kHost;
18+
}
19+
20+
cudaPointerAttributes attr;
21+
INFINI_CHECK_CUDA(cudaPointerGetAttributes(&attr, ptr));
22+
23+
if (attr.type == cudaMemoryTypeDevice || attr.type == cudaMemoryTypeManaged) {
24+
return MemorySpace::kDevice;
25+
}
26+
27+
return MemorySpace::kHost;
28+
}
29+
1130
} // namespace infini::ccl
1231

1332
#endif // INFINI_CCL_ILUVATAR_DEVICE__H_

‎src/moore/checks.h‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef INFINI_CCL_MOORE_CHECKS_H_
2+
#define INFINI_CCL_MOORE_CHECKS_H_
3+
4+
#include <musa_runtime.h>
5+
6+
#include <iostream>
7+
8+
#include "return_status_impl.h"
9+
10+
#define INFINI_CHECK_MUSA(result) \
11+
::infini::ccl::detail::CheckMusaImpl((result), __FILE__, __LINE__)
12+
13+
namespace infini::ccl {
14+
15+
namespace detail {
16+
17+
inline ReturnStatus CheckMusaImpl(musaError_t musa_result, const char *file,
18+
int line) {
19+
if (musa_result != musaSuccess) {
20+
musaGetLastError();
21+
std::cerr << "MUSA error code: " << musa_result << " at line " << line
22+
<< " in " << file << std::endl;
23+
std::abort();
24+
}
25+
return ReturnStatus::kSuccess;
26+
}
27+
28+
} // namespace detail
29+
30+
} // namespace infini::ccl
31+
32+
#endif // INFINI_CCL_MOORE_CHECKS_H_

‎src/moore/device_.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
#ifndef INFINI_CCL_MOORE_DEVICE__H_
22
#define INFINI_CCL_MOORE_DEVICE__H_
33

4+
#include <musa_runtime.h>
5+
6+
#include "checks.h"
47
#include "device.h"
58

69
namespace infini::ccl {
710

811
template <>
912
struct DeviceEnabled<Device::Type::kMoore> : std::true_type {};
1013

14+
template <>
15+
MemorySpace GetMemorySpace<Device::Type::kMoore>(const void* ptr) {
16+
if (!ptr) {
17+
return MemorySpace::kHost;
18+
}
19+
20+
musaPointerAttributes attr;
21+
INFINI_CHECK_MUSA(musaPointerGetAttributes(&attr, ptr));
22+
23+
if (attr.type == musaMemoryTypeDevice || attr.type == musaMemoryTypeManaged) {
24+
return MemorySpace::kDevice;
25+
}
26+
27+
return MemorySpace::kHost;
28+
}
29+
1130
} // namespace infini::ccl
1231

1332
#endif // INFINI_CCL_MOORE_DEVICE__H_

‎src/nvidia/device_.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <cuda_runtime.h>
55

6-
#include "checks.h"
6+
#include "cuda/checks.h"
77
#include "device.h"
88

99
namespace infini::ccl {

0 commit comments

Comments
 (0)