okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-139b-arc' into f-139-relad33b8074

main
10.4 KiB266 linesraw
1//==---- ccl_utils.hpp----------------------------*- C++ -*----------------==//
2//
3// Copyright (C) Intel Corporation
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5// See https://llvm.org/LICENSE.txt for license information.
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef __DPCT_CCL_UTILS_HPP__
10#define __DPCT_CCL_UTILS_HPP__
11
12#include "compat_service.hpp"
13
14#include <oneapi/ccl.hpp>
15
16#include <unordered_map>
17#include <memory>
18
19#include "detail/ccl_utils_detail.hpp"
20
21namespace dpct {
22namespace ccl {
23
24/// Get concatenated library version as an integer.
25static inline int get_version() {
26  oneapi::ccl::init();
27  auto ver = oneapi::ccl::get_library_version();
28  return ver.major * 10000 + ver.minor * 100 + ver.update;
29}
30
31/// Create main kvs and return its address.
32static inline oneapi::ccl::kvs::address_type create_kvs_address() {
33  oneapi::ccl::init();
34  auto ptr = oneapi::ccl::create_main_kvs();
35  auto addr = ptr->get_address();
36  detail::get_kvs(addr) = ptr;
37  return addr;
38}
39
40/// Get stored kvs with /p addr if exist. Otherwise, create kvs with /p addr.
41static inline std::shared_ptr<oneapi::ccl::kvs>
42create_kvs(const oneapi::ccl::kvs::address_type &addr) {
43  oneapi::ccl::init();
44  auto &ptr = detail::get_kvs(addr);
45  if (!ptr)
46    ptr = oneapi::ccl::create_kvs(addr);
47  return ptr;
48}
49
50/// dpct communicator extension
51class communicator_wrapper : public dpct::ccl::detail::ccl_init_helper {
52public:
53  communicator_wrapper(
54      int size, int rank, oneapi::ccl::kvs::address_type id,
55      const oneapi::ccl::comm_attr &attr = oneapi::ccl::default_comm_attr)
56      : _device_comm(oneapi::ccl::create_device(
57            static_cast<sycl::device &>(::dpct::cs::get_current_device()))),
58        _context_comm(
59            oneapi::ccl::create_context(::dpct::cs::get_default_context())),
60        _comm(oneapi::ccl::create_communicator(
61            size, rank, _device_comm, _context_comm, dpct::ccl::create_kvs(id),
62            attr)) {
63    _queue_init = false;
64    _ccl_stream_ptr = nullptr;
65  }
66
67  ~communicator_wrapper() {
68    delete _ccl_stream_ptr;
69  };
70
71  /// Return the rank in a oneapi::ccl::communicator
72  /// \returns The rank corresponding to communicator object
73  int rank() const {
74    return _comm.rank();
75  }
76
77  /// Retrieves the number of rank in oneapi::ccl::communicator
78  /// \returns The number of the ranks
79  int size() const {
80    return _comm.size();
81  }
82
83  /// Return underlying native device, which was used in oneapi::ccl::communicator
84  sycl::device get_device() const {
85    return _comm.get_device().get_native();
86  }
87
88  /// \brief allreduce is a collective communication operation that performs the global reduction operation
89  ///       on values from all ranks of communicator and distributes the result back to all ranks.
90  /// \param sendbuff the buffer with @c count elements of @c dtype that stores local data to be reduced
91  /// \param recvbuff [out] the buffer to store reduced result, must have the same dimension as @c sendbuff
92  /// \param count the number of elements of type @c dtype in @c sendbuff and @c recvbuff
93  /// \param dtype the datatype of elements in @c sendbuff and @c recvbuff
94  /// \param rtype the type of the reduction operation to be applied
95  /// \param queue_ptr a sycl::queue ptr associated with the operation
96  /// \return @ref void
97  void allreduce(const void *sendbuff, void *recvbuff, size_t count,
98                 oneapi::ccl::datatype dtype, oneapi::ccl::reduction rtype,
99                 sycl::queue *queue_ptr) {
100    call_func_wrapper(
101        [=](const oneapi::ccl::stream &stream) {
102          return oneapi::ccl::allreduce(sendbuff, recvbuff, count, dtype, rtype,
103                                        _comm, stream);
104        },
105        queue_ptr);
106  }
107
108  /// \brief reduce is a collective communication operation that performs the
109  ///        global reduction operation on values from all ranks of the communicator
110  ///        and returns the result to the root rank.
111  /// \param sendbuff the buffer with @c count elements of @c dtype that stores
112  ///        local data to be reduced 
113  /// \param recvbuff [out] the buffer to store reduced result, 
114  ///        must have the same dimension as @c sendbuff 
115  /// \param count the number of elements of type @c dtype in @c sendbuff and @c recvbuff 
116  /// \param dtype the datatype of elements in @c sendbuff and @c recvbuff 
117  /// \param root the rank that gets the result of reduction 
118  /// \param rtype the type of the reduction operation to be applied 
119  /// \param queue_ptr a sycl::queue ptr associated with the operation 
120  /// \return @ref void
121  void reduce(const void *sendbuff, void *recvbuff, size_t count,
122              oneapi::ccl::datatype dtype, oneapi::ccl::reduction rtype,
123              int root, sycl::queue *queue_ptr) {
124    call_func_wrapper(
125        [=](const oneapi::ccl::stream &stream) {
126          return oneapi::ccl::reduce(sendbuff, recvbuff, count, dtype, rtype,
127                                     root, _comm, stream);
128        },
129        queue_ptr);
130  }
131
132  /// \brief broadcast is a collective communication operation that broadcasts data
133  ///        from one rank of communicator (denoted as root) to all other ranks.
134  ///        Only support in-place operation
135  /// \param sendbuff the buffer with @c count elements of @c dtype that stores
136  ///        local data to be reduced 
137  /// \param recvbuff [out] the buffer to store reduced result
138  /// \param count the number of elements of type @c dtype in @c buf 
139  /// \param dtype thedatatype of elements in @c buf 
140  /// \param root the rank that broadcasts @c buf
141  /// \param queue_ptr a sycl::queue ptr associated with the operation
142  /// \return @ref void
143  void broadcast(void *sendbuff, void *recvbuff, size_t count,
144                 oneapi::ccl::datatype dtype, int root,
145                 sycl::queue *queue_ptr) {
146    if (sendbuff != recvbuff) {
147      throw std::runtime_error(
148          "oneCCL broadcast only support in-place operation. "
149          "sendbuff and recvbuff must be same.");
150      return;
151    }
152    call_func_wrapper(
153        [=](const oneapi::ccl::stream &stream) {
154          return oneapi::ccl::broadcast(recvbuff, count, dtype, root, _comm,
155                                        stream);
156        },
157        queue_ptr);
158  }
159
160  /// \brief reduce_scatter is a collective communication operation that performs the global reduction operation
161  ///        on values from all ranks of the communicator and scatters the result in blocks back to all ranks.
162  /// \param sendbuff the buffer with @c count elements of @c dtype that stores local data to be reduced
163  /// \param recvbuff [out] the buffer to store reduced result, must have the same dimension as @c sendbuff
164  /// \param recv_count the number of elements of type @c dtype in receive block
165  /// \param dtype the datatype of elements in @c sendbuff and @c recvbuff
166  /// \param rtype the type of the reduction operation to be applied
167  /// \param queue_ptr a sycl::queue ptr associated with the operation
168  /// \return @ref void
169  void reduce_scatter(const void *sendbuff, void *recvbuff, size_t recv_count,
170                      oneapi::ccl::datatype dtype, oneapi::ccl::reduction rtype,
171                      sycl::queue *queue_ptr) {
172    call_func_wrapper(
173        [=](const oneapi::ccl::stream &stream) {
174          return oneapi::ccl::reduce_scatter(sendbuff, recvbuff, recv_count,
175                                             dtype, rtype, _comm, stream);
176        },
177        queue_ptr);
178  }
179
180  /// \brief send is a pt2pt communication operation that sends data from one rank of communicator.
181  /// \param sendbuff the buffer with @c count elements of @c dtype serves as send buffer for root
182  /// \param count the number of elements of type @c dtype in @c sendbuff
183  /// \param dtype the datatype of elements in @c sendbuff
184  /// \param peer the rank that receives @c sendbuff
185  /// \param queue_ptr a sycl::queue ptr associated with the operation
186  /// \return @ref void
187  void send(void *sendbuff, size_t count, oneapi::ccl::datatype dtype, int peer,
188            sycl::queue *queue_ptr) {
189    call_func_wrapper(
190        [=](const oneapi::ccl::stream &stream) {
191          return oneapi::ccl::send(sendbuff, count, dtype, peer, _comm, stream);
192        },
193        queue_ptr);
194  }
195
196  /// \brief recv is a pt2pt communication operation that sends data from one rank of communicator.
197  /// \param recvbuff the buffer with @c count elements of @c dtype serves as  receive buffer
198  /// \param count the number of elements of type @c dtype in @c recvbuff
199  /// \param dtype the datatype of elements in @c recvbuff
200  /// \param peer the rank that receives @c recvbuff
201  /// \param queue_ptr a sycl::queue ptr associated with the operation
202  /// \return @ref void
203  void recv(void *recvbuff, size_t count, oneapi::ccl::datatype dtype, int peer,
204            sycl::queue *queue_ptr) {
205    call_func_wrapper(
206        [=](const oneapi::ccl::stream &stream) {
207          return oneapi::ccl::recv(recvbuff, count, dtype, peer, _comm, stream);
208        },
209        queue_ptr);
210  }
211
212private:
213  oneapi::ccl::device _device_comm;
214  oneapi::ccl::context _context_comm;
215  oneapi::ccl::communicator _comm;
216  sycl::queue _queue;
217  bool _queue_init;
218  oneapi::ccl::stream *_ccl_stream_ptr;
219
220  template <class Fn>
221  void call_func_wrapper(Fn func, sycl::queue *qptr) {
222    if (_queue_init && *qptr != _queue) {
223      call_func_async(func, qptr);
224    } else {
225      if(!_queue_init) {
226        _queue = *qptr;
227        _queue_init = true;
228        _ccl_stream_ptr = new oneapi::ccl::stream(oneapi::ccl::create_stream(_queue));
229      }
230      std::invoke(func, *_ccl_stream_ptr);
231    }
232  }
233
234  class call_func_async {
235    sycl::queue *_q_ptr;
236    struct call_async_impl {
237      oneapi::ccl::stream _ccl_stream_impl;
238      oneapi::ccl::event _ccl_event_impl;
239      template <class Fn>
240      explicit call_async_impl(Fn func, sycl::queue *qptr)
241          : _ccl_stream_impl(oneapi::ccl::create_stream(*qptr)),
242            _ccl_event_impl(std::invoke(func, _ccl_stream_impl)) {}
243    };
244    call_async_impl *_imp;
245
246  public:
247    template <class Fn>
248    explicit call_func_async(Fn func, sycl::queue *qptr)
249        : _q_ptr(qptr),
250          _imp(new call_async_impl(func, qptr)) {}
251    ~call_func_async() {
252      _q_ptr->submit([&](sycl::handler &cgh)
253                     { cgh.host_task([=]
254                                     {
255        _imp->_ccl_event_impl.wait();
256        delete _imp; }); });
257    }
258  };
259};
260
261typedef dpct::ccl::communicator_wrapper *comm_ptr;
262
263} // namespace ccl
264} // namespace dpct
265
266#endif // __DPCT_CCL_UTILS_HPP__