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 : #ifndef HCCL_CCU_SUPER_FAST_LOAD_H
11 : #define HCCL_CCU_SUPER_FAST_LOAD_H
12 : #include <cstdlib>
13 : #include <array>
14 : #include <vector>
15 : #include <cstring>
16 : #include <utility>
17 : #include <cstddef>
18 :
19 : #include "securec.h"
20 : #include "string_util.h"
21 :
22 : #include "types.h"
23 : #include "op_type.h"
24 : #include "orion_adapter_rts.h"
25 : #include "ccu/ccu_task_param.h"
26 : #include "task_param.h"
27 : #include "ccu_ins.h"
28 : #include "acl/acl_rt.h"
29 :
30 : namespace Hccl {
31 : using CcuParamsMappingKeyType = std::uint32_t;
32 : constexpr std::size_t CCU_SFL_PARAM_KEY_LEN = 3;
33 : using CcuSFLMappingKey = std::array<CcuParamsMappingKeyType, CCU_SFL_PARAM_KEY_LEN>;
34 : struct CachedCCUParams {
35 : public:
36 : rtCcuTaskInfo_t* ccuParams{nullptr};
37 : std::vector<std::size_t> count;
38 : std::vector<TaskParam> taskParams;
39 : u64 execId{0};
40 : std::size_t totalCounts{0};
41 : CcuInstType insType{};
42 : bool isSlave{false};
43 :
44 0 : CachedCCUParams() = default;
45 : explicit CachedCCUParams(
46 : std::vector<std::vector<Hccl::CcuTaskParam>>&& ccuInstruction,
47 : std::vector<std::vector<CcuProfilingInfo>>&& profilingInfo, std::size_t execId, CcuInstType insType,
48 : bool isSlave, void* comm);
49 :
50 : CachedCCUParams(const CachedCCUParams&) = delete;
51 : CachedCCUParams& operator=(const CachedCCUParams&) = delete;
52 :
53 : CachedCCUParams(CachedCCUParams&& other) noexcept;
54 : CachedCCUParams& operator=(CachedCCUParams&& other) noexcept;
55 :
56 : ~CachedCCUParams();
57 :
58 : private:
59 13 : inline void* aligned_malloc(size_t align, size_t size) const
60 : {
61 13 : void* p = nullptr;
62 13 : if (posix_memalign(&p, align, size) != 0) {
63 0 : throw std::bad_alloc();
64 : }
65 13 : return p;
66 : }
67 :
68 13 : inline void aligned_free(void* ptr) const
69 : {
70 13 : if (ptr) {
71 13 : std::free(ptr);
72 13 : ptr = nullptr;
73 : }
74 13 : }
75 :
76 13 : inline bool is_power_of_2(std::size_t x) const { return static_cast<bool>(x) && !static_cast<bool>((x & (x - 1))); }
77 13 : inline std::size_t round_up_to(std::size_t size, std::size_t alignment) const
78 : {
79 13 : return alignment != 0 ? (size + alignment - 1) / alignment * alignment : size;
80 : }
81 13 : inline void* alloc_aligned_raw(std::size_t alignment, std::size_t size) const
82 : {
83 13 : if (alignment == 0) {
84 0 : alignment = alignof(std::max_align_t);
85 : }
86 13 : if (!is_power_of_2(alignment)) {
87 0 : return nullptr;
88 : };
89 13 : return aligned_malloc(alignment, round_up_to(size, alignment));
90 : }
91 :
92 : rtCcuTaskInfo_t*
93 : alloc_and_memcpy_aligned(const std::vector<std::vector<rtCcuTaskInfo_t>>& vecs, std::size_t alignment);
94 : };
95 :
96 9 : inline void SuperFastLoad(rtCcuTaskInfo_t* params, aclrtStream const streamPtr, int counts)
97 : {
98 27 : for (int i = 0; i < counts; ++i) {
99 18 : HrtCcuLaunch(params[i], streamPtr);
100 : }
101 9 : }
102 : } // namespace Hccl
103 :
104 : namespace std {
105 : constexpr std::size_t NUMBER_SIX = 6;
106 : constexpr std::size_t NUMBER_TWO = 2;
107 : struct ArrayHasher {
108 : std::size_t
109 28 : operator()(const std::array<Hccl::CcuParamsMappingKeyType, Hccl::CCU_SFL_PARAM_KEY_LEN>& ccuParams) const noexcept
110 : {
111 28 : std::size_t hashVal = 0;
112 112 : for (auto ccuParam : ccuParams) {
113 84 : hashVal ^= std::hash<std::uint32_t>{}(ccuParam) + 0x9e3779b9 + (hashVal << NUMBER_SIX)
114 84 : + (hashVal >> NUMBER_TWO);
115 : }
116 28 : return hashVal;
117 : }
118 : };
119 :
120 : template <>
121 : struct hash<Hccl::OpType> {
122 : std::size_t operator()(const Hccl::OpType& type) const noexcept { return static_cast<std::size_t>(type); }
123 : };
124 : template <>
125 : struct hash<const Hccl::OpType> {
126 48 : std::size_t operator()(const Hccl::OpType& type) const noexcept { return static_cast<std::size_t>(type); }
127 : };
128 : }; // namespace std
129 : #endif // HCCL_CCU_SUPER_FAST_LOAD_H
|