diff options
Diffstat (limited to 'thirdparty/embree/common/sys/alloc.cpp')
-rw-r--r-- | thirdparty/embree/common/sys/alloc.cpp | 212 |
1 files changed, 184 insertions, 28 deletions
diff --git a/thirdparty/embree/common/sys/alloc.cpp b/thirdparty/embree/common/sys/alloc.cpp index abdd269069..71616a3982 100644 --- a/thirdparty/embree/common/sys/alloc.cpp +++ b/thirdparty/embree/common/sys/alloc.cpp @@ -12,33 +12,177 @@ namespace embree { - void* alignedMalloc(size_t size, size_t align) + size_t total_allocations = 0; + +#if defined(EMBREE_SYCL_SUPPORT) + + __thread sycl::context* tls_context_tutorial = nullptr; + __thread sycl::device* tls_device_tutorial = nullptr; + + __thread sycl::context* tls_context_embree = nullptr; + __thread sycl::device* tls_device_embree = nullptr; + + void enableUSMAllocEmbree(sycl::context* context, sycl::device* device) + { + // -- GODOT start -- + // if (tls_context_embree != nullptr) throw std::runtime_error("USM allocation already enabled"); + // if (tls_device_embree != nullptr) throw std::runtime_error("USM allocation already enabled"); + if (tls_context_embree != nullptr) { + abort(); + } + if (tls_device_embree != nullptr) { + abort(); + } + // -- GODOT end -- + tls_context_embree = context; + tls_device_embree = device; + } + + void disableUSMAllocEmbree() + { + // -- GODOT start -- + // if (tls_context_embree == nullptr) throw std::runtime_error("USM allocation not enabled"); + // if (tls_device_embree == nullptr) throw std::runtime_error("USM allocation not enabled"); + if (tls_context_embree == nullptr) { + abort(); + } + if (tls_device_embree == nullptr) { + abort(); + } + // -- GODOT end -- + tls_context_embree = nullptr; + tls_device_embree = nullptr; + } + + void enableUSMAllocTutorial(sycl::context* context, sycl::device* device) + { + //if (tls_context_tutorial != nullptr) throw std::runtime_error("USM allocation already enabled"); + //if (tls_device_tutorial != nullptr) throw std::runtime_error("USM allocation already enabled"); + tls_context_tutorial = context; + tls_device_tutorial = device; + } + + void disableUSMAllocTutorial() + { + // -- GODOT start -- + // if (tls_context_tutorial == nullptr) throw std::runtime_error("USM allocation not enabled"); + // if (tls_device_tutorial == nullptr) throw std::runtime_error("USM allocation not enabled"); + if (tls_context_tutorial == nullptr) { + abort(); + } + if (tls_device_tutorial == nullptr) { + abort(); + } + // -- GODOT end -- + + tls_context_tutorial = nullptr; + tls_device_tutorial = nullptr; + } + +#endif + + void* alignedMalloc(size_t size, size_t align) { if (size == 0) return nullptr; - + assert((align & (align-1)) == 0); void* ptr = _mm_malloc(size,align); - - if (size != 0 && ptr == nullptr) - // -- GODOT start -- - // throw std::bad_alloc(); + // -- GODOT start -- + // if (size != 0 && ptr == nullptr) + // throw std::bad_alloc(); + if (size != 0 && ptr == nullptr) { abort(); - // -- GODOT end -- - + } + // -- GODOT end -- return ptr; } - + void alignedFree(void* ptr) { if (ptr) _mm_free(ptr); } +#if defined(EMBREE_SYCL_SUPPORT) + + void* alignedSYCLMalloc(sycl::context* context, sycl::device* device, size_t size, size_t align, EmbreeUSMMode mode) + { + assert(context); + assert(device); + + if (size == 0) + return nullptr; + + assert((align & (align-1)) == 0); + total_allocations++; + + void* ptr = nullptr; + if (mode == EMBREE_USM_SHARED_DEVICE_READ_ONLY) + ptr = sycl::aligned_alloc_shared(align,size,*device,*context,sycl::ext::oneapi::property::usm::device_read_only()); + else + ptr = sycl::aligned_alloc_shared(align,size,*device,*context); + + // -- GODOT start -- + // if (size != 0 && ptr == nullptr) + // throw std::bad_alloc(); + if (size != 0 && ptr == nullptr) { + abort(); + } + // -- GODOT end -- + + return ptr; + } + + static MutexSys g_alloc_mutex; + + void* alignedSYCLMalloc(size_t size, size_t align, EmbreeUSMMode mode) + { + if (tls_context_tutorial) return alignedSYCLMalloc(tls_context_tutorial, tls_device_tutorial, size, align, mode); + if (tls_context_embree ) return alignedSYCLMalloc(tls_context_embree, tls_device_embree, size, align, mode); + return nullptr; + } + + void alignedSYCLFree(sycl::context* context, void* ptr) + { + assert(context); + if (ptr) { + sycl::free(ptr,*context); + } + } + + void alignedSYCLFree(void* ptr) + { + if (tls_context_tutorial) return alignedSYCLFree(tls_context_tutorial, ptr); + if (tls_context_embree ) return alignedSYCLFree(tls_context_embree, ptr); + } + +#endif + + void* alignedUSMMalloc(size_t size, size_t align, EmbreeUSMMode mode) + { +#if defined(EMBREE_SYCL_SUPPORT) + if (tls_context_embree || tls_context_tutorial) + return alignedSYCLMalloc(size,align,mode); + else +#endif + return alignedMalloc(size,align); + } + + void alignedUSMFree(void* ptr) + { +#if defined(EMBREE_SYCL_SUPPORT) + if (tls_context_embree || tls_context_tutorial) + return alignedSYCLFree(ptr); + else +#endif + return alignedFree(ptr); + } + static bool huge_pages_enabled = false; static MutexSys os_init_mutex; - __forceinline bool isHugePageCandidate(const size_t bytes) + __forceinline bool isHugePageCandidate(const size_t bytes) { if (!huge_pages_enabled) return false; @@ -133,7 +277,9 @@ namespace embree char* ptr = (char*) VirtualAlloc(nullptr,bytes,flags,PAGE_READWRITE); // -- GODOT start -- // if (ptr == nullptr) throw std::bad_alloc(); - if (ptr == nullptr) abort(); + if (ptr == nullptr) { + abort(); + } // -- GODOT end -- hugepages = false; return ptr; @@ -150,11 +296,13 @@ namespace embree if (bytesNew >= bytesOld) return bytesOld; - if (!VirtualFree((char*)ptr+bytesNew,bytesOld-bytesNew,MEM_DECOMMIT)) - // -- GODOT start -- - // throw std::bad_alloc(); + // -- GODOT start -- + // if (!VirtualFree((char*)ptr+bytesNew,bytesOld-bytesNew,MEM_DECOMMIT)) + // throw std::bad_alloc(); + if (!VirtualFree((char*)ptr+bytesNew,bytesOld-bytesNew,MEM_DECOMMIT)) { abort(); - // -- GODOT end -- + } + // -- GODOT end -- return bytesNew; } @@ -164,11 +312,13 @@ namespace embree if (bytes == 0) return; - if (!VirtualFree(ptr,0,MEM_RELEASE)) - // -- GODOT start -- - // throw std::bad_alloc(); + // -- GODOT start -- + // if (!VirtualFree(ptr,0,MEM_RELEASE)) + // throw std::bad_alloc(); + if (!VirtualFree(ptr,0,MEM_RELEASE)) { abort(); - // -- GODOT end -- + } + // -- GODOT end -- } void os_advise(void *ptr, size_t bytes) @@ -274,7 +424,9 @@ namespace embree void* ptr = (char*) mmap(0, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); // -- GODOT start -- // if (ptr == MAP_FAILED) throw std::bad_alloc(); - if (ptr == MAP_FAILED) abort(); + if (ptr == MAP_FAILED) { + abort(); + } // -- GODOT end -- hugepages = false; @@ -291,11 +443,13 @@ namespace embree if (bytesNew >= bytesOld) return bytesOld; - if (munmap((char*)ptr+bytesNew,bytesOld-bytesNew) == -1) - // -- GODOT start -- - // throw std::bad_alloc(); + // -- GODOT start -- + // if (munmap((char*)ptr+bytesNew,bytesOld-bytesNew) == -1) + // throw std::bad_alloc(); + if (munmap((char*)ptr+bytesNew,bytesOld-bytesNew) == -1) { abort(); - // -- GODOT end -- + } + // -- GODOT end -- return bytesNew; } @@ -308,11 +462,13 @@ namespace embree /* for hugepages we need to also align the size */ const size_t pageSize = hugepages ? PAGE_SIZE_2M : PAGE_SIZE_4K; bytes = (bytes+pageSize-1) & ~(pageSize-1); - if (munmap(ptr,bytes) == -1) - // -- GODOT start -- - // throw std::bad_alloc(); + // -- GODOT start -- + // if (munmap(ptr,bytes) == -1) + // throw std::bad_alloc(); + if (munmap(ptr,bytes) == -1) { abort(); - // -- GODOT end -- + } + // -- GODOT end -- } /* hint for transparent huge pages (THP) */ |