Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef HCCL_CCU_SUPER_FAST_LOAD_H
12 : #define HCCL_CCU_SUPER_FAST_LOAD_H
13 : #include <cstdlib>
14 : #include <array>
15 : #include <vector>
16 : #include <cstring>
17 : #include <utility>
18 : #include <cstddef>
19 :
20 : #include "securec.h"
21 : #include "string_util.h"
22 :
23 : #include "types.h"
24 : #include "op_type.h"
25 : #include "orion_adapter_rts.h"
26 : #include "ccu/ccu_task_param.h"
27 : #include "task_param.h"
28 : #include "ccu_ins.h"
29 : #include "acl/acl_rt.h"
30 :
31 : namespace Hccl {
32 : using CcuParamsMappingKeyType = std::uint32_t;
33 : constexpr std::size_t CCU_SFL_PARAM_KEY_LEN = 3;
34 : using CcuSFLMappingKey = std::array<CcuParamsMappingKeyType, CCU_SFL_PARAM_KEY_LEN>;
35 : struct CachedCCUParams {
36 : public:
37 : rtCcuTaskInfo_t* ccuParams{nullptr};
38 : std::vector<std::size_t> count;
39 : std::vector<TaskParam> taskParams;
40 : u64 execId{0};
41 : std::size_t totalCounts{0};
42 : CcuInstType insType{};
43 : bool isSlave{false};
44 :
45 0 : CachedCCUParams() = default;
46 : explicit CachedCCUParams(
47 : std::vector<std::vector<Hccl::CcuTaskParam>>&& ccuInstruction,
48 : std::vector<std::vector<CcuProfilingInfo>>&& profilingInfo, std::size_t execId, CcuInstType insType,
49 : bool isSlave, void* comm);
50 :
51 : CachedCCUParams(const CachedCCUParams&) = delete;
52 : CachedCCUParams& operator=(const CachedCCUParams&) = delete;
53 :
54 : CachedCCUParams(CachedCCUParams&& other) noexcept;
55 : CachedCCUParams& operator=(CachedCCUParams&& other) noexcept;
56 :
57 : ~CachedCCUParams();
58 :
59 : private:
60 13 : inline void* aligned_malloc(size_t align, size_t size) const
61 : {
62 13 : void* p = nullptr;
63 13 : if (posix_memalign(&p, align, size) != 0) {
64 0 : throw std::bad_alloc();
65 : }
66 13 : return p;
67 : }
68 :
69 13 : inline void aligned_free(void* ptr) const
70 : {
71 13 : if (ptr) {
72 13 : std::free(ptr);
73 13 : ptr = nullptr;
74 : }
75 13 : }
76 :
77 13 : inline bool is_power_of_2(std::size_t x) const { return static_cast<bool>(x) && !static_cast<bool>((x & (x - 1))); }
78 13 : inline std::size_t round_up_to(std::size_t size, std::size_t alignment) const
79 : {
80 13 : return alignment != 0 ? (size + alignment - 1) / alignment * alignment : size;
81 : }
82 13 : inline void* alloc_aligned_raw(std::size_t alignment, std::size_t size) const
83 : {
84 13 : if (alignment == 0) {
85 0 : alignment = alignof(std::max_align_t);
86 : }
87 13 : if (!is_power_of_2(alignment)) {
88 0 : return nullptr;
89 : };
90 13 : return aligned_malloc(alignment, round_up_to(size, alignment));
91 : }
92 :
93 : rtCcuTaskInfo_t*
94 : alloc_and_memcpy_aligned(const std::vector<std::vector<rtCcuTaskInfo_t>>& vecs, std::size_t alignment);
95 : };
96 :
97 9 : inline void SuperFastLoad(rtCcuTaskInfo_t* params, aclrtStream const streamPtr, int counts)
98 : {
99 27 : for (int i = 0; i < counts; ++i) {
100 18 : HrtCcuLaunch(params[i], streamPtr);
101 : }
102 9 : }
103 : } // namespace Hccl
104 :
105 : namespace std {
106 : constexpr std::size_t NUMBER_SIX = 6;
107 : constexpr std::size_t NUMBER_TWO = 2;
108 : struct ArrayHasher {
109 : std::size_t
110 28 : operator()(const std::array<Hccl::CcuParamsMappingKeyType, Hccl::CCU_SFL_PARAM_KEY_LEN>& ccuParams) const noexcept
111 : {
112 28 : std::size_t hashVal = 0;
113 112 : for (auto ccuParam : ccuParams) {
114 84 : hashVal ^= std::hash<std::uint32_t>{}(ccuParam) + 0x9e3779b9 + (hashVal << NUMBER_SIX)
115 84 : + (hashVal >> NUMBER_TWO);
116 : }
117 28 : return hashVal;
118 : }
119 : };
120 :
121 : template <>
122 : struct hash<Hccl::OpType> {
123 : std::size_t operator()(const Hccl::OpType& type) const noexcept { return static_cast<std::size_t>(type); }
124 : };
125 : template <>
126 : struct hash<const Hccl::OpType> {
127 48 : std::size_t operator()(const Hccl::OpType& type) const noexcept { return static_cast<std::size_t>(type); }
128 : };
129 : }; // namespace std
130 : #endif // HCCL_CCU_SUPER_FAST_LOAD_H
|