okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1//==---- memory.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_MEMORY_HPP__ 10#define __DPCT_MEMORY_HPP__ 11 12#include "device.hpp" 13#include <sycl/sycl.hpp> 14#include <cassert> 15#include <cstdint> 16#include <cstring> 17#include <mutex> 18#include <unordered_map> 19#include <map> 20#include <utility> 21#include <thread> 22#include <type_traits> 23 24#if defined(__linux__ ) 25#include <sys/mman.h> 26#elif defined(_WIN64 ) 27#ifndef NOMINMAX 28#define NOMINMAX 29#endif 30#include <windows.h> 31#else 32#error "Only support Windows and Linux." 33#endif 34 35namespace dpct { 36enum memcpy_direction { 37host_to_host , 38host_to_device , 39device_to_host , 40device_to_device , 41automatic 42}; 43enum memory_region { 44global = 0 ,// device global memory 45constant ,// device constant memory 46local ,// device local memory 47shared ,// memory which can be accessed by host and device 48}; 49 50typedef uint8_t byte_t ; 51 52/// Buffer type to be used in Memory Management runtime. 53typedef sycl::buffer < byte_t > buffer_t ; 54 55class image_matrix ; 56 57/// dpct accessor used as device function parameter. 58template < class T ,memory_region Memory ,size_t Dimension > class accessor ; 59static inline void dpct_free (void * ptr , sycl::queue & q ); 60template < typename T > static sycl::buffer < T > get_buffer (const void * ptr ); 61static buffer_t get_buffer (const void * ptr ); 62 63/// Pitched 2D/3D memory data. 64class pitched_data { 65public : 66pitched_data () :pitched_data (nullptr ,0 ,0 ,0 ) {} 67pitched_data (void * data ,size_t pitch ,size_t x ,size_t y ) 68 :_data (data ),_pitch (pitch ),_x (x ),_y (y ) {} 69 70void * get_data_ptr () {return _data ; } 71void set_data_ptr (const void * data ) {_data = const_cast < void *> (data ); } 72 73size_t get_pitch () {return _pitch ; } 74void set_pitch (size_t pitch ) {_pitch = pitch ; } 75 76size_t get_x () {return _x ; } 77void set_x (size_t x ) {_x = x ; }; 78 79size_t get_y () {return _y ; } 80void set_y (size_t y ) {_y = y ; } 81 82private : 83void * _data ; 84size_t _pitch ,_x ,_y ; 85}; 86 87namespace experimental { 88class image_mem_wrapper ; 89}// namespace experimental 90 91/// Memory copy parameters for 2D/3D memory data. 92struct memcpy_parameter { 93struct data_wrapper { 94pitched_data pitched {}; 95 sycl::id < 3 > pos {}; 96size_t pos_x_in_bytes {0 }; 97int dev_id {0 }; 98#ifdef SYCL_EXT_ONEAPI_BINDLESS_IMAGES 99 experimental::image_mem_wrapper * image_bindless {nullptr }; 100#endif 101image_matrix * image {nullptr }; 102 }; 103data_wrapper from {}; 104data_wrapper to {}; 105 sycl::range < 3 > size {}; 106size_t size_x_in_bytes {0 }; 107memcpy_direction direction {memcpy_direction::automatic }; 108}; 109 110}// namespace dpct 111 112#include "detail/memory_detail.hpp" 113namespace dpct { 114 115#ifdef DPCT_USM_LEVEL_NONE 116/// Check if the pointer \p ptr represents device pointer or not. 117/// 118/// \param ptr The pointer to be checked. 119/// \returns true if \p ptr is a device pointer. 120template < class T > 121static inline bool is_device_ptr (T ptr ) { 122if constexpr (std::is_pointer < T > ::value ) { 123return detail::mem_mgr::instance ().is_device_ptr (ptr ); 124 } 125return false; 126} 127/// Get the base address of the allocated memory for the given pointer \p ptr. 128template < class T > static inline void * get_base_addr (T ptr ) { 129if constexpr (std::is_pointer < T > ::value ) { 130return detail::mem_mgr::instance ().get_base_addr (ptr ); 131 } 132return nullptr ; 133} 134#endif 135 136/// Get the buffer and the offset of a piece of memory pointed to by \p ptr. 137/// 138/// \param ptr Pointer to a piece of memory. 139/// If NULL is passed as an argument, an exception will be thrown. 140/// \returns a pair containing both the buffer and the offset. 141static std::pair < buffer_t ,size_t > get_buffer_and_offset (const void * ptr ) { 142if (ptr ) { 143auto alloc = detail::mem_mgr::instance ().translate_ptr (ptr ); 144size_t offset = (byte_t * )ptr - alloc .alloc_ptr ; 145return std::make_pair (alloc .buffer ,offset ); 146 }else { 147throw std::runtime_error ( 148"NULL pointer argument in get_buffer_and_offset function is invalid" ); 149 } 150} 151 152/// Get the data pointed from \p ptr as a 1D buffer reinterpreted as type T. 153template < typename T > static sycl::buffer < T > get_buffer (const void * ptr ) { 154if (!ptr ) 155return sycl::buffer < T > (sycl::range < 1 > (0 )); 156auto alloc = detail::mem_mgr::instance ().translate_ptr (ptr ); 157return alloc .buffer .reinterpret < T > ( 158 sycl::range < 1 > (alloc .size /sizeof (T ))); 159} 160 161/// Get the buffer of a piece of memory pointed to by \p ptr. 162/// 163/// \param ptr Pointer to a piece of memory. 164/// \returns the buffer. 165static buffer_t get_buffer (const void * ptr ) { 166return detail::mem_mgr::instance ().translate_ptr (ptr ).buffer ; 167} 168 169/// A wrapper class contains an accessor and an offset. 170template < typename PtrT , 171 sycl::access_mode accessMode = sycl::access_mode::read_write > 172class access_wrapper { 173 sycl::accessor < byte_t ,1 ,accessMode > accessor ; 174size_t offset ; 175 176public : 177/// Construct the accessor wrapper for memory pointed by \p ptr. 178/// 179/// \param ptr Pointer to memory. 180/// \param cgh The command group handler. 181access_wrapper (const void * ptr , sycl::handler & cgh ) 182 :accessor (get_buffer (ptr ).get_access < accessMode > (cgh )),offset (0 ) { 183auto alloc = detail::mem_mgr::instance ().translate_ptr (ptr ); 184offset = (byte_t * )ptr - alloc .alloc_ptr ; 185 } 186template < typename U = PtrT > 187access_wrapper ( 188PtrT ptr , sycl::handler & cgh , 189typename std::enable_if_t < !std::is_same_v < 190 std::remove_cv_t < std::remove_reference_t < U >> ,void *>> * = 0 ) 191 :access_wrapper ((const void * )ptr ,cgh ) {} 192 193/// Get the device pointer. 194/// 195/// \returns a device pointer with offset. 196PtrT get_raw_pointer ()const {return (PtrT )(& accessor [0 ]+ offset ); } 197}; 198 199/// Get the accessor for memory pointed by \p ptr. 200/// 201/// \param ptr Pointer to memory. 202/// If NULL is passed as an argument, an exception will be thrown. 203/// \param cgh The command group handler. 204/// \returns an accessor. 205template < typename T , 206 sycl::access_mode accessMode = sycl::access_mode::read_write > 207static auto get_access (const T * ptr , sycl::handler & cgh ) { 208if (ptr ) { 209auto alloc = detail::mem_mgr::instance ().translate_ptr (ptr ); 210if constexpr (std::is_same_v < std::remove_reference_t < T > ,void > ) 211return alloc .buffer .template get_access < accessMode > (cgh ); 212else 213return alloc .buffer 214 .template reinterpret < T > (sycl::range < 1 > (alloc .size /sizeof (T ))) 215 .template get_access < accessMode > (cgh ); 216 }else { 217throw std::runtime_error ( 218"NULL pointer argument in get_access function is invalid" ); 219 } 220} 221 222/// Allocate memory block on the device. 223/// \param num_bytes Number of bytes to allocate. 224/// \param q Queue to execute the allocate task. 225/// \returns A pointer to the newly allocated memory. 226template < typename T > 227static inline void * dpct_malloc (T num_bytes , 228 sycl::queue & q = get_default_queue ()) { 229return detail::dpct_malloc (static_cast < size_t > (num_bytes ),q ); 230} 231 232/// Get the host pointer from a buffer that is mapped to virtual pointer ptr. 233/// \param ptr Virtual Pointer mapped to device buffer 234/// \returns A host pointer 235template < typename T > static inline T * get_host_ptr (const void * ptr ) { 236auto BufferOffset = get_buffer_and_offset (ptr ); 237auto host_ptr = 238BufferOffset .first .get_host_access () 239 .get_pointer (); 240return (T * )(host_ptr + BufferOffset .second ); 241} 242 243/// Allocate memory block for 3D array on the device. 244/// \param size Size of the memory block, in bytes. 245/// \param q Queue to execute the allocate task. 246/// \returns A pitched_data object which stores the memory info. 247static inline pitched_data 248dpct_malloc (sycl::range < 3 > size , sycl::queue & q = get_default_queue ()) { 249pitched_data pitch (nullptr ,0 ,size .get (0 ),size .get (1 )); 250size_t pitch_size ; 251pitch .set_data_ptr (detail::dpct_malloc (pitch_size ,size .get (0 ),size .get (1 ), 252size .get (2 ),q )); 253pitch .set_pitch (pitch_size ); 254return pitch ; 255} 256 257/// Allocate memory block for 2D array on the device. 258/// \param [out] pitch Aligned size of x in bytes. 259/// \param x Range in dim x. 260/// \param y Range in dim y. 261/// \param q Queue to execute the allocate task. 262/// \returns A pointer to the newly allocated memory. 263static inline void * dpct_malloc (size_t & pitch ,size_t x ,size_t y , 264 sycl::queue & q = get_default_queue ()) { 265return detail::dpct_malloc (pitch ,x ,y ,1 ,q ); 266} 267 268/// free 269/// \param ptr Point to free. 270/// \param q Queue to execute the free task. 271/// \returns no return value. 272static inline void dpct_free (void * ptr , 273 sycl::queue & q = get_default_queue ()) { 274#ifndef DPCT_USM_LEVEL_NONE 275 dpct::get_device (dpct::get_device_id (q .get_device ())).queues_wait_and_throw (); 276#endif 277 detail::dpct_free (ptr ,q ); 278} 279 280/// Free the device memory pointed by a batch of pointers in \p pointers which 281/// are related to \p q after \p events completed. 282/// 283/// \param pointers The pointers point to the device memory requested to be freed. 284/// \param events The events to be waited. 285/// \param q The sycl::queue the memory relates to. 286inline void async_dpct_free (const std::vector < void *> & pointers , 287const std::vector < sycl::event > & events , 288 sycl::queue & q = get_default_queue ()) { 289 detail::async_dpct_free (pointers ,events ,q ); 290} 291 292/// Synchronously copies \p size bytes from the address specified by \p from_ptr 293/// to the address specified by \p to_ptr. The value of \p direction is used to 294/// set the copy direction, it can be \a host_to_host, \a host_to_device, 295/// \a device_to_host, \a device_to_device or \a automatic. The function will 296/// return after the copy is completed. 297/// 298/// \param to_ptr Pointer to destination memory address. 299/// \param from_ptr Pointer to source memory address. 300/// \param size Number of bytes to be copied. 301/// \param direction Direction of the copy. 302/// \param q Queue to execute the copy task. 303/// \returns no return value. 304static void dpct_memcpy (void * to_ptr ,const void * from_ptr ,size_t size , 305memcpy_direction direction = automatic , 306 sycl::queue & q = get_default_queue ()) { 307 detail::dpct_memcpy (q ,to_ptr ,from_ptr ,size ,direction ).wait (); 308} 309 310/// Synchronously copies \p size bytes from the address specified by \p from_ptr 311/// on device specified by \p from_dev_id to the address specified by \p to_ptr 312/// on device specified by \p to_dev_id. The function will return after the copy 313/// is completed. 314/// 315/// \param to_ptr Pointer to destination memory address. 316/// \param to_dev_id Destination device ID. 317/// \param from_ptr Pointer to source memory address. 318/// \param from_dev_id Source device ID. 319/// \param size Number of bytes to be copied. 320/// \param q Queue to execute the copy task. 321/// \returns no return value. 322static void dpct_memcpy (void * to_ptr ,int to_dev_id ,const void * from_ptr , 323int from_dev_id ,size_t size , 324 sycl::queue & q = get_default_queue ()) { 325 detail::dpct_memcpy (q ,to_ptr ,to_dev_id ,from_ptr ,from_dev_id ,size ).wait (); 326} 327 328/// Asynchronously copies \p size bytes from the address specified by \p 329/// from_ptr to the address specified by \p to_ptr. The value of \p direction is 330/// used to set the copy direction, it can be \a host_to_host, \a 331/// host_to_device, \a device_to_host, \a device_to_device or \a automatic. The 332/// return of the function does NOT guarantee the copy is completed. 333/// 334/// \param to_ptr Pointer to destination memory address. 335/// \param from_ptr Pointer to source memory address. 336/// \param size Number of bytes to be copied. 337/// \param direction Direction of the copy. 338/// \param q Queue to execute the copy task. 339/// \returns no return value. 340static void async_dpct_memcpy (void * to_ptr ,const void * from_ptr ,size_t size , 341memcpy_direction direction = automatic , 342 sycl::queue & q = dpct::get_default_queue ()) { 343 detail::dpct_memcpy (q ,to_ptr ,from_ptr ,size ,direction ); 344} 345 346/// Asynchronously copies \p size bytes from the address specified by \p 347/// from_ptr on device specified by \p from_dev_id to the address specified by 348/// \p to_ptr on device specified by \p to_dev_id. The return of the function 349/// does NOT guarantee the copy is completed. 350/// 351/// \param to_ptr Pointer to destination memory address. 352/// \param to_dev_id Destination device ID. 353/// \param from_ptr Pointer to source memory address. 354/// \param from_dev_id Source device ID. 355/// \param size Number of bytes to be copied. 356/// \param q Queue to execute the copy task. 357/// \returns no return value. 358static void async_dpct_memcpy (void * to_ptr ,int to_dev_id ,const void * from_ptr , 359int from_dev_id ,size_t size , 360 sycl::queue & q = get_default_queue ()) { 361 detail::dpct_memcpy (q ,to_ptr ,to_dev_id ,from_ptr ,from_dev_id ,size ); 362} 363 364/// Synchronously copies 2D matrix specified by \p x and \p y from the address 365/// specified by \p from_ptr to the address specified by \p to_ptr, while \p 366/// from_pitch and \p to_pitch are the range of dim x in bytes of the matrix 367/// specified by \p from_ptr and \p to_ptr. The value of \p direction is used to 368/// set the copy direction, it can be \a host_to_host, \a host_to_device, \a 369/// device_to_host, \a device_to_device or \a automatic. The function will 370/// return after the copy is completed. 371/// 372/// \param to_ptr Pointer to destination memory address. 373/// \param to_pitch Range of dim x in bytes of destination matrix. 374/// \param from_ptr Pointer to source memory address. 375/// \param from_pitch Range of dim x in bytes of source matrix. 376/// \param x Range of dim x of matrix to be copied. 377/// \param y Range of dim y of matrix to be copied. 378/// \param direction Direction of the copy. 379/// \param q Queue to execute the copy task. 380/// \returns no return value. 381static inline void dpct_memcpy (void * to_ptr ,size_t to_pitch , 382const void * from_ptr ,size_t from_pitch , 383size_t x ,size_t y , 384memcpy_direction direction = automatic , 385 sycl::queue & q = dpct::get_default_queue ()) { 386 sycl::event::wait (detail::dpct_memcpy (q ,to_ptr ,from_ptr ,to_pitch , 387from_pitch ,x ,y ,direction )); 388} 389 390/// Asynchronously copies 2D matrix specified by \p x and \p y from the address 391/// specified by \p from_ptr to the address specified by \p to_ptr, while \p 392/// \p from_pitch and \p to_pitch are the range of dim x in bytes of the matrix 393/// specified by \p from_ptr and \p to_ptr. The value of \p direction is used to 394/// set the copy direction, it can be \a host_to_host, \a host_to_device, \a 395/// device_to_host, \a device_to_device or \a automatic. The return of the 396/// function does NOT guarantee the copy is completed. 397/// 398/// \param to_ptr Pointer to destination memory address. 399/// \param to_pitch Range of dim x in bytes of destination matrix. 400/// \param from_ptr Pointer to source memory address. 401/// \param from_pitch Range of dim x in bytes of source matrix. 402/// \param x Range of dim x of matrix to be copied. 403/// \param y Range of dim y of matrix to be copied. 404/// \param direction Direction of the copy. 405/// \param q Queue to execute the copy task. 406/// \returns no return value. 407static inline void 408async_dpct_memcpy (void * to_ptr ,size_t to_pitch ,const void * from_ptr , 409size_t from_pitch ,size_t x ,size_t y , 410memcpy_direction direction = automatic , 411 sycl::queue & q = get_default_queue ()) { 412 detail::dpct_memcpy (q ,to_ptr ,from_ptr ,to_pitch ,from_pitch ,x ,y , 413direction ); 414} 415 416/// Synchronously copies a subset of a 3D matrix specified by \p to to another 417/// 3D matrix specified by \p from. The from and to position info are specified 418/// by \p from_pos and \p to_pos The copied matrix size is specified by \p size. 419/// The value of \p direction is used to set the copy direction, it can be \a 420/// host_to_host, \a host_to_device, \a device_to_host, \a device_to_device or 421/// \a automatic. The function will return after the copy is completed. 422/// 423/// \param to Destination matrix info. 424/// \param to_pos Position of destination. 425/// \param from Source matrix info. 426/// \param from_pos Position of destination. 427/// \param size Range of the submatrix to be copied. 428/// \param direction Direction of the copy. 429/// \param q Queue to execute the copy task. 430/// \returns no return value. 431static inline void dpct_memcpy (pitched_data to , sycl::id < 3 > to_pos , 432pitched_data from , sycl::id < 3 > from_pos , 433 sycl::range < 3 > size , 434memcpy_direction direction = automatic , 435 sycl::queue & q = dpct::get_default_queue ()) { 436 sycl::event::wait ( 437 detail::dpct_memcpy (q ,to ,to_pos ,from ,from_pos ,size ,direction )); 438} 439 440/// Asynchronously copies a subset of a 3D matrix specified by \p to to another 441/// 3D matrix specified by \p from. The from and to position info are specified 442/// by \p from_pos and \p to_pos The copied matrix size is specified by \p size. 443/// The value of \p direction is used to set the copy direction, it can be \a 444/// host_to_host, \a host_to_device, \a device_to_host, \a device_to_device or 445/// \a automatic. The return of the function does NOT guarantee the copy is 446/// completed. 447/// 448/// \param to Destination matrix info. 449/// \param to_pos Position of destination. 450/// \param from Source matrix info. 451/// \param from_pos Position of destination. 452/// \param size Range of the submatrix to be copied. 453/// \param direction Direction of the copy. 454/// \param q Queue to execute the copy task. 455/// \returns no return value. 456static inline void 457async_dpct_memcpy (pitched_data to , sycl::id < 3 > to_pos ,pitched_data from , 458 sycl::id < 3 > from_pos , sycl::range < 3 > size , 459memcpy_direction direction = automatic , 460 sycl::queue & q = get_default_queue ()) { 461 detail::dpct_memcpy (q ,to ,to_pos ,from ,from_pos ,size ,direction ); 462} 463 464/// Synchronously copies 2D/3D memory data specified by \p param . The function 465/// will return after the copy is completed. 466/// 467/// \param param Memory copy parameters. 468/// \param q Queue to execute the copy task. 469/// \returns no return value. 470static inline void dpct_memcpy (const memcpy_parameter & param , 471 sycl::queue & q = dpct::get_default_queue ()) { 472 sycl::event::wait (detail::dpct_memcpy (q ,param )); 473} 474 475/// Asynchronously copies 2D/3D memory data specified by \p param . The return 476/// of the function does NOT guarantee the copy is completed. 477/// 478/// \param param Memory copy parameters. 479/// \param q Queue to execute the copy task. 480/// \returns no return value. 481static inline void async_dpct_memcpy (const memcpy_parameter & param , 482 sycl::queue & q = get_default_queue ()) { 483 detail::dpct_memcpy (q ,param ); 484} 485 486/** 487* @brief Sets 1 byte data \p value to the first \p size elements starting from 488* \p dev_ptr in \p q synchronously. 489* @param [in] dev_ptr Pointer to the virtual device memory address. 490* @param [in] value The value to be set. 491* @param [in] size Number of elements to be set to the value. 492* @param [in] q The queue in which the operation is done. 493*/ 494static void dpct_memset (void * dev_ptr ,int value ,size_t size , 495 sycl::queue & q = get_default_queue ()) { 496 detail::dpct_memset < unsigned char > (q ,dev_ptr ,value ,size ).wait (); 497} 498 499/** 500* @brief Sets 2 bytes data \p value to the first \p size elements starting from 501* \p dev_ptr in \p q synchronously. 502* @param [in] dev_ptr Pointer to the virtual device memory address. 503* @param [in] value The value to be set. 504* @param [in] size Number of elements to be set to the value. 505* @param [in] q The queue in which the operation is done. 506*/ 507static void dpct_memset_d16 (void * dev_ptr ,unsigned short value ,size_t size , 508 sycl::queue & q = get_default_queue ()) { 509 detail::dpct_memset (q ,dev_ptr ,value ,size ).wait (); 510} 511/** 512* @brief Sets 4 bytes data \p value to the first \p size elements starting from 513* \p dev_ptr in \p q synchronously. 514* @param [in] dev_ptr Pointer to the virtual device memory address. 515* @param [in] value The value to be set. 516* @param [in] size Number of elements to be set to the value. 517* @param [in] q The queue in which the operation is done. 518*/ 519static void dpct_memset_d32 (void * dev_ptr ,unsigned int value ,size_t size , 520 sycl::queue & q = get_default_queue ()) { 521 detail::dpct_memset (q ,dev_ptr ,value ,size ).wait (); 522} 523 524/** 525* @brief Sets 1 byte data \p value to the first \p size elements starting from 526* \p dev_ptr in \p q asynchronously. 527* @param [in] dev_ptr Pointer to the virtual device memory address. 528* @param [in] value The value to be set. 529* @param [in] size Number of elements to be set to the value. 530* @param [in] q The queue in which the operation is done. 531*/ 532static void async_dpct_memset (void * dev_ptr ,int value ,size_t size , 533 sycl::queue & q = dpct::get_default_queue ()) { 534 detail::dpct_memset < unsigned char > (q ,dev_ptr ,value ,size ); 535} 536/** 537* @brief Sets 2 bytes data \p value to the first \p size elements starting from 538* \p dev_ptr in \p q asynchronously. 539* @param [in] dev_ptr Pointer to the virtual device memory address. 540* @param [in] value The value to be set. 541* @param [in] size Number of elements to be set to the value. 542* @param [in] q The queue in which the operation is done. 543*/ 544static void async_dpct_memset_d16 (void * dev_ptr ,unsigned short value ,size_t size , 545 sycl::queue & q = dpct::get_default_queue ()) { 546 detail::dpct_memset (q ,dev_ptr ,value ,size ); 547} 548/** 549* @brief Sets 4 bytes data \p value to the first \p size elements starting from 550* \p dev_ptr in \p q asynchronously. 551* @param [in] dev_ptr Pointer to the virtual device memory address. 552* @param [in] value The value to be set. 553* @param [in] size Number of elements to be set to the value. 554* @param [in] q The queue in which the operation is done. 555*/ 556static void async_dpct_memset_d32 (void * dev_ptr ,unsigned int value ,size_t size , 557 sycl::queue & q = dpct::get_default_queue ()) { 558 detail::dpct_memset (q ,dev_ptr ,value ,size ); 559} 560 561/** 562* @brief Sets 1 byte data \p val to the pitched 2D memory region pointed by \p ptr in \p q 563* synchronously. 564* @param [in] ptr Pointer to the virtual device memory. 565* @param [in] pitch The pitch size by number of elements, including padding. 566* @param [in] val The value to be set. 567* @param [in] x The width of memory region by number of elements. 568* @param [in] y The height of memory region by number of elements. 569* @param [in] q The queue in which the operation is done. 570*/ 571static inline void dpct_memset (void * ptr ,size_t pitch ,int val ,size_t x , 572size_t y , 573 sycl::queue & q = get_default_queue ()) { 574 sycl::event::wait (detail::dpct_memset < unsigned char > (q ,ptr ,pitch ,val ,x ,y )); 575} 576/** 577* @brief Sets 2 bytes data \p val to the pitched 2D memory region pointed by \p ptr in \p q 578* synchronously. 579* @param [in] ptr Pointer to the virtual device memory. 580* @param [in] pitch The pitch size by number of elements, including padding. 581* @param [in] val The value to be set. 582* @param [in] x The width of memory region by number of elements. 583* @param [in] y The height of memory region by number of elements. 584* @param [in] q The queue in which the operation is done. 585*/ 586static inline void dpct_memset_d16 (void * ptr ,size_t pitch ,unsigned short val ,size_t x , 587size_t y , 588 sycl::queue & q = get_default_queue ()) { 589 sycl::event::wait (detail::dpct_memset (q ,ptr ,pitch ,val ,x ,y )); 590} 591/** 592* @brief Sets 4 bytes data \p val to the pitched 2D memory region pointed by \p ptr in \p q 593* synchronously. 594* @param [in] ptr Pointer to the virtual device memory. 595* @param [in] pitch The pitch size by number of elements, including padding. 596* @param [in] val The value to be set. 597* @param [in] x The width of memory region by number of elements. 598* @param [in] y The height of memory region by number of elements. 599* @param [in] q The queue in which the operation is done. 600*/ 601static inline void dpct_memset_d32 (void * ptr ,size_t pitch ,unsigned int val ,size_t x , 602size_t y , 603 sycl::queue & q = get_default_queue ()) { 604 sycl::event::wait (detail::dpct_memset (q ,ptr ,pitch ,val ,x ,y )); 605} 606 607/** 608* @brief Sets 1 byte data \p val to the pitched 2D memory region pointed by \p ptr in \p q 609* asynchronously. 610* @param [in] ptr Pointer to the virtual device memory. 611* @param [in] pitch The pitch size by number of elements, including padding. 612* @param [in] val The value to be set. 613* @param [in] x The width of memory region by number of elements. 614* @param [in] y The height of memory region by number of elements. 615* @param [in] q The queue in which the operation is done. 616*/ 617static inline void async_dpct_memset (void * ptr ,size_t pitch ,int val ,size_t x , 618size_t y , 619 sycl::queue & q = get_default_queue ()) { 620 detail::dpct_memset < unsigned char > (q ,ptr ,pitch ,val ,x ,y ); 621} 622 623/** 624* @brief Sets 2 bytes data \p val to the pitched 2D memory region pointed by \p ptr in \p q 625* asynchronously. 626* @param [in] ptr Pointer to the virtual device memory. 627* @param [in] pitch The pitch size by number of elements, including padding. 628* @param [in] val The value to be set. 629* @param [in] x The width of memory region by number of elements. 630* @param [in] y The height of memory region by number of elements. 631* @param [in] q The queue in which the operation is done. 632*/ 633static inline void async_dpct_memset_d16 (void * ptr ,size_t pitch , 634unsigned short val ,size_t x ,size_t y , 635 sycl::queue & q = get_default_queue ()) { 636 detail::dpct_memset (q ,ptr ,pitch ,val ,x ,y ); 637} 638 639/** 640* @brief Sets 4 bytes data \p val to the pitched 2D memory region pointed by \p ptr in \p q 641* asynchronously. 642* @param [in] ptr Pointer to the virtual device memory. 643* @param [in] pitch The pitch size by number of elements, including padding. 644* @param [in] val The value to be set. 645* @param [in] x The width of memory region by number of elements. 646* @param [in] y The height of memory region by number of elements. 647* @param [in] q The queue in which the operation is done. 648*/ 649static inline void async_dpct_memset_d32 (void * ptr ,size_t pitch , 650unsigned int val ,size_t x ,size_t y , 651 sycl::queue & q = get_default_queue ()) { 652 detail::dpct_memset (q ,ptr ,pitch ,val ,x ,y ); 653} 654 655/** 656* @brief Sets 1 byte data \p value to the 3D memory region pointed by \p data in \p q 657* synchronously. 658* @param [in] data Pointer to the pitched device memory region. 659* @param [in] value The value to be set. 660* @param [in] size 3D memory region by number of elements. 661* @param [in] q The queue in which the operation is done. 662*/ 663static inline void dpct_memset (pitched_data pitch ,int val , 664 sycl::range < 3 > size , 665 sycl::queue & q = get_default_queue ()) { 666 sycl::event::wait (detail::dpct_memset < unsigned char > (q ,pitch ,val ,size )); 667} 668 669/** 670* @brief Sets 1 byte data \p value to the 3D memory region pointed by \p data in \p q 671* asynchronously. 672* @param [in] data Pointer to the pitched device memory region. 673* @param [in] value The value to be set. 674* @param [in] size 3D memory region by number of elements. 675* @param [in] q The queue in which the operation is done. 676*/ 677static inline void async_dpct_memset (pitched_data pitch ,int val , 678 sycl::range < 3 > size , 679 sycl::queue & q = get_default_queue ()) { 680 detail::dpct_memset < unsigned char > (q ,pitch ,val ,size ); 681} 682 683namespace experimental { 684typedef sycl::ext::oneapi::experimental::physical_mem * physical_mem_ptr ; 685 686struct mem_location { 687int id = 0 ; 688// Location type. Value 1 means device location, and thus, id is a device id. 689int type = 1 ; 690}; 691 692struct mem_prop { 693mem_location location ; 694int type = 1 ;// Memory type. Value 1 means default device memory. 695}; 696 697struct mem_access_desc { 698 sycl::ext::oneapi::experimental::address_access_mode flags ; 699mem_location location ; 700}; 701}// namespace experimental 702 703template < class T ,memory_region Memory > class accessor < T ,Memory ,3 > { 704public : 705using memory_t = detail::memory_traits < Memory ,T > ; 706using pointer_t = typename memory_t::pointer_t ; 707using accessor_t = typename memory_t::template accessor_t < 3 > ; 708accessor (pointer_t data ,const sycl::range < 3 > & in_range ) 709 :_data (data ),_range (in_range ) {} 710template < memory_region M = Memory > 711accessor (typename std::enable_if < M != local ,const accessor_t > ::type & acc ) 712 :accessor (acc ,acc .get_range ()) {} 713accessor (const accessor_t & acc ,const sycl::range < 3 > & in_range ) 714 :accessor ( 715acc .template get_multi_ptr < sycl::access::decorated::no > ().get (), 716in_range ) {} 717accessor < T ,Memory ,2 > operator[](size_t index )const { 718 sycl::range < 2 > sub (_range .get (1 ),_range .get (2 )); 719return accessor < T ,Memory ,2 > (_data + index * sub .size (),sub ); 720 } 721 722pointer_t get_ptr ()const {return _data ; } 723 724private : 725pointer_t _data ; 726 sycl::range < 3 > _range ; 727}; 728template < class T ,memory_region Memory > class accessor < T ,Memory ,2 > { 729public : 730using memory_t = detail::memory_traits < Memory ,T > ; 731using pointer_t = typename memory_t::pointer_t ; 732using accessor_t = typename memory_t::template accessor_t < 2 > ; 733accessor (pointer_t data ,const sycl::range < 2 > & in_range ) 734 :_data (data ),_range (in_range ) {} 735 736template < memory_region M = Memory > 737 [[deprecated ]]accessor ( 738typename std::enable_if < M != local ,const accessor_t > ::type & acc ) 739 :accessor (acc ,acc .get_range ()) {} 740 [[deprecated ]]accessor (const accessor_t & acc ,const sycl::range < 2 > & in_range ) 741 :accessor (const_cast < pointer_t > ( 742acc .template get_multi_ptr < sycl::access::decorated::no > () 743 .get ()), 744in_range ) {} 745 746pointer_t operator[](size_t index )const { 747return _data + _range .get (1 )* index ; 748 } 749 750pointer_t get_ptr ()const {return _data ; } 751 752private : 753pointer_t _data ; 754 sycl::range < 2 > _range ; 755}; 756 757template < class T ,size_t Dimension > 758using global_memory = detail::device_memory < T ,global ,Dimension > ; 759template < class T ,size_t Dimension > 760using constant_memory = detail::device_memory < T ,constant ,Dimension > ; 761template < class T ,size_t Dimension > 762using shared_memory = detail::device_memory < T ,shared ,Dimension > ; 763 764// dpct::deprecated:: is for functionality that was introduced for compatibility 765// purpose, but relies on deprecated C++ features, which are either removed or 766// will be removed in the future standards. 767// Direct use of deprecated functionality in this namespace should be avoided. 768namespace deprecated { 769 770template < typename T > 771using usm_host_allocator = detail::deprecated::usm_allocator < T , sycl::usm::alloc::host > ; 772 773template < typename T > 774using usm_device_allocator = detail::deprecated::usm_allocator < T , sycl::usm::alloc::shared > ; 775}// namespace deprecated 776 777class pointer_attributes { 778public : 779enum class type { 780memory_type , 781device_pointer , 782host_pointer , 783is_managed , 784device_id , 785unsupported 786 }; 787 788void init (const void * ptr , 789 sycl::queue & q = dpct::get_default_queue ()) { 790#ifdef DPCT_USM_LEVEL_NONE 791throw std::runtime_error ( 792"dpct::pointer_attributes: only works for USM pointer." ); 793#else 794memory_type = sycl::get_pointer_type (ptr ,q .get_context ()); 795if (memory_type == sycl::usm::alloc::unknown ) { 796device_id = -1 ; 797return ; 798 } 799device_pointer = (memory_type != 800 sycl::usm::alloc::unknown ) ?ptr :nullptr ; 801host_pointer = (memory_type != 802 sycl::usm::alloc::unknown )&& 803 (memory_type != sycl::usm::alloc::device ) ?ptr :nullptr ; 804 sycl::device device_obj = sycl::get_pointer_device (ptr ,q .get_context ()); 805device_id = dpct::dev_mgr::instance ().get_device_id (device_obj ); 806#endif 807 } 808 809// Query pointer propreties listed in attributes and store the results in data array 810static void get (unsigned int numAttributes ,type * attributes , 811void ** data ,device_ptr ptr ) { 812pointer_attributes sycl_attributes ; 813 814sycl_attributes .init (ptr ); 815 816for (int i = 0 ;i < numAttributes ;i ++ ) { 817switch (attributes [i ]) { 818case type::memory_type : 819* static_cast < int *> (data [i ])= 820static_cast < int > (sycl_attributes .get_memory_type ()); 821break ; 822case type::device_pointer : 823* (reinterpret_cast < void **> (data [i ]))= 824const_cast < void *> (sycl_attributes .get_device_pointer ()); 825break ; 826case type::host_pointer : 827* (reinterpret_cast < void **> (data [i ]))= 828const_cast < void *> (sycl_attributes .get_host_pointer ()); 829break ; 830case type::is_managed : 831* static_cast < unsigned int *> (data [i ])= 832sycl_attributes .is_memory_shared (); 833break ; 834case type::device_id : 835* static_cast < unsigned int *> (data [i ])= sycl_attributes .get_device_id (); 836break ; 837default : 838data [i ]= nullptr ; 839break ; 840 } 841 } 842 } 843 844 sycl::usm::alloc get_memory_type () { 845return memory_type ; 846 } 847 848const void * get_device_pointer () { 849return device_pointer ; 850 } 851 852const void * get_host_pointer () { 853return host_pointer ; 854 } 855 856bool is_memory_shared () { 857return memory_type == sycl::usm::alloc::shared ; 858 } 859 860unsigned int get_device_id () { 861return device_id ; 862 } 863 864private : 865 sycl::usm::alloc memory_type = sycl::usm::alloc::unknown ; 866const void * device_pointer = nullptr ; 867const void * host_pointer = nullptr ; 868unsigned int device_id = -1 ; 869}; 870 871}// namespace dpct 872#endif // __DPCT_MEMORY_HPP__