okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
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 (); 27auto ver = oneapi::ccl::get_library_version (); 28return 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 (); 34auto ptr = oneapi::ccl::create_main_kvs (); 35auto addr = ptr -> get_address (); 36 detail::get_kvs (addr )= ptr ; 37return 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 (); 44auto & ptr = detail::get_kvs (addr ); 45if (!ptr ) 46ptr = oneapi::ccl::create_kvs (addr ); 47return ptr ; 48} 49 50/// dpct communicator extension 51class communicator_wrapper :public dpct::ccl::detail::ccl_init_helper { 52public : 53communicator_wrapper ( 54int size ,int rank , oneapi::ccl::kvs::address_type id , 55const oneapi::ccl::comm_attr & attr = oneapi::ccl::default_comm_attr ) 56 :_device_comm (oneapi::ccl::create_device ( 57static_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 ( 61size ,rank ,_device_comm ,_context_comm , dpct::ccl::create_kvs (id ), 62attr )) { 63_queue_init = false; 64_ccl_stream_ptr = nullptr ; 65 } 66 67 ~communicator_wrapper () { 68delete _ccl_stream_ptr ; 69 }; 70 71/// Return the rank in a oneapi::ccl::communicator 72/// \returns The rank corresponding to communicator object 73int rank ()const { 74return _comm .rank (); 75 } 76 77/// Retrieves the number of rank in oneapi::ccl::communicator 78/// \returns The number of the ranks 79int size ()const { 80return _comm .size (); 81 } 82 83/// Return underlying native device, which was used in oneapi::ccl::communicator 84 sycl::device get_device ()const { 85return _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 97void allreduce (const void * sendbuff ,void * recvbuff ,size_t count , 98 oneapi::ccl::datatype dtype , oneapi::ccl::reduction rtype , 99 sycl::queue * queue_ptr ) { 100call_func_wrapper ( 101 [= ](const oneapi::ccl::stream & stream ) { 102return oneapi::ccl::allreduce (sendbuff ,recvbuff ,count ,dtype ,rtype , 103_comm ,stream ); 104 }, 105queue_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 121void reduce (const void * sendbuff ,void * recvbuff ,size_t count , 122 oneapi::ccl::datatype dtype , oneapi::ccl::reduction rtype , 123int root , sycl::queue * queue_ptr ) { 124call_func_wrapper ( 125 [= ](const oneapi::ccl::stream & stream ) { 126return oneapi::ccl::reduce (sendbuff ,recvbuff ,count ,dtype ,rtype , 127root ,_comm ,stream ); 128 }, 129queue_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 143void broadcast (void * sendbuff ,void * recvbuff ,size_t count , 144 oneapi::ccl::datatype dtype ,int root , 145 sycl::queue * queue_ptr ) { 146if (sendbuff != recvbuff ) { 147throw std::runtime_error ( 148"oneCCL broadcast only support in-place operation. " 149"sendbuff and recvbuff must be same." ); 150return ; 151 } 152call_func_wrapper ( 153 [= ](const oneapi::ccl::stream & stream ) { 154return oneapi::ccl::broadcast (recvbuff ,count ,dtype ,root ,_comm , 155stream ); 156 }, 157queue_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 169void 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 ) { 172call_func_wrapper ( 173 [= ](const oneapi::ccl::stream & stream ) { 174return oneapi::ccl::reduce_scatter (sendbuff ,recvbuff ,recv_count , 175dtype ,rtype ,_comm ,stream ); 176 }, 177queue_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 187void send (void * sendbuff ,size_t count , oneapi::ccl::datatype dtype ,int peer , 188 sycl::queue * queue_ptr ) { 189call_func_wrapper ( 190 [= ](const oneapi::ccl::stream & stream ) { 191return oneapi::ccl::send (sendbuff ,count ,dtype ,peer ,_comm ,stream ); 192 }, 193queue_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 203void recv (void * recvbuff ,size_t count , oneapi::ccl::datatype dtype ,int peer , 204 sycl::queue * queue_ptr ) { 205call_func_wrapper ( 206 [= ](const oneapi::ccl::stream & stream ) { 207return oneapi::ccl::recv (recvbuff ,count ,dtype ,peer ,_comm ,stream ); 208 }, 209queue_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 ; 217bool _queue_init ; 218 oneapi::ccl::stream * _ccl_stream_ptr ; 219 220template < class Fn > 221void call_func_wrapper (Fn func , sycl::queue * qptr ) { 222if (_queue_init && * qptr != _queue ) { 223call_func_async (func ,qptr ); 224 }else { 225if (!_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 234class call_func_async { 235 sycl::queue * _q_ptr ; 236struct call_async_impl { 237 oneapi::ccl::stream _ccl_stream_impl ; 238 oneapi::ccl::event _ccl_event_impl ; 239template < class Fn > 240explicit 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 }; 244call_async_impl * _imp ; 245 246public : 247template < class Fn > 248explicit 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 (); 256delete _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__