Skip to content

Commit

Permalink
refactor alloc. remove global (tls) SYCL context and device.
Browse files Browse the repository at this point in the history
  • Loading branch information
freibold committed Dec 9, 2024
1 parent 8e98a76 commit c5e0adb
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 120 deletions.
88 changes: 0 additions & 88 deletions common/sys/alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,6 @@

namespace embree
{
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)
{
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");
tls_context_embree = context;
tls_device_embree = device;
}

void disableUSMAllocEmbree()
{
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");
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()
{
//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");
tls_context_tutorial = nullptr;
tls_device_tutorial = nullptr;
}

#endif

void* alignedMalloc(size_t size, size_t align)
{
if (size == 0)
Expand Down Expand Up @@ -86,7 +42,6 @@ namespace embree
return nullptr;

assert((align & (align-1)) == 0);
total_allocations++;

void* ptr = nullptr;
if (mode == EMBREE_USM_SHARED_DEVICE_READ_ONLY)
Expand All @@ -109,7 +64,6 @@ namespace embree
return nullptr;

assert((align & (align-1)) == 0);
total_allocations++;

void* ptr = nullptr;
if (type == EmbreeMemoryType::SHARED) {
Expand All @@ -134,22 +88,6 @@ namespace embree
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* alignedSYCLMalloc(size_t size, size_t align, EmbreeUSMMode mode, EmbreeMemoryType type)
{
if (tls_context_tutorial) return alignedSYCLMalloc(tls_context_tutorial, tls_device_tutorial, size, align, mode, type);
if (tls_context_embree ) return alignedSYCLMalloc(tls_context_embree, tls_device_embree, size, align, mode, type);
return nullptr;
}

void alignedSYCLFree(sycl::context* context, void* ptr)
{
assert(context);
Expand All @@ -163,33 +101,7 @@ namespace embree
}
}

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;
Expand Down
33 changes: 6 additions & 27 deletions common/sys/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,22 @@

namespace embree
{
#if defined(EMBREE_SYCL_SUPPORT)

/* enables SYCL USM allocation */
void enableUSMAllocEmbree(sycl::context* context, sycl::device* device);
void enableUSMAllocTutorial(sycl::context* context, sycl::device* device);

/* disables SYCL USM allocation */
void disableUSMAllocEmbree();
void disableUSMAllocTutorial();

#endif

#define ALIGNED_STRUCT_(align) \
void* operator new(size_t size) { return alignedMalloc(size,align); } \
void operator delete(void* ptr) { alignedFree(ptr); } \
void* operator new[](size_t size) { return alignedMalloc(size,align); } \
void operator delete[](void* ptr) { alignedFree(ptr); }

#define ALIGNED_STRUCT_USM_(align) \
void* operator new(size_t size) { return alignedUSMMalloc(size,align); } \
void operator delete(void* ptr) { alignedUSMFree(ptr); } \
void* operator new[](size_t size) { return alignedUSMMalloc(size,align); } \
void operator delete[](void* ptr) { alignedUSMFree(ptr); }

#define ALIGNED_CLASS_(align) \
public: \
ALIGNED_STRUCT_(align) \
private:

/*! aligned allocation */
void* alignedMalloc(size_t size, size_t align);
void alignedFree(void* ptr);


enum EmbreeUSMMode {
EMBREE_USM_SHARED = 0,
EMBREE_USM_SHARED_DEVICE_READ_WRITE = 0,
Expand All @@ -50,17 +37,9 @@ namespace embree
SHARED = 2,
UNKNOWN = 3
};

/*! aligned allocation */
void* alignedMalloc(size_t size, size_t align);
void alignedFree(void* ptr);

/*! aligned allocation using SYCL USM */
void* alignedUSMMalloc(size_t size, size_t align = 16, EmbreeUSMMode mode = EMBREE_USM_SHARED_DEVICE_READ_ONLY);
void alignedUSMFree(void* ptr);

#if defined(EMBREE_SYCL_SUPPORT)

/*! aligned allocation using SYCL USM */
void* alignedSYCLMalloc(sycl::context* context, sycl::device* device, size_t size, size_t align, EmbreeUSMMode mode);
void* alignedSYCLMalloc(sycl::context* context, sycl::device* device, size_t size, size_t align, EmbreeUSMMode mode, EmbreeMemoryType type);
Expand Down
2 changes: 0 additions & 2 deletions kernels/common/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,11 +720,9 @@ namespace embree
}

void DeviceGPU::enter() {
enableUSMAllocEmbree(&gpu_context,&gpu_device);
}

void DeviceGPU::leave() {
disableUSMAllocEmbree();
}

void* DeviceGPU::malloc(size_t size, size_t align) {
Expand Down
1 change: 1 addition & 0 deletions tutorials/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ IF (EMBREE_TUTORIALS_GLFW)
ADD_SUBDIRECTORY(imgui)
ENDIF()

ADD_SUBDIRECTORY(alloc)
ADD_SUBDIRECTORY(tutorial)
ADD_SUBDIRECTORY(scenegraph)
ADD_SUBDIRECTORY(lights)
Expand Down
15 changes: 15 additions & 0 deletions tutorials/common/alloc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Copyright 2009-2021 Intel Corporation
## SPDX-License-Identifier: Apache-2.0

ADD_LIBRARY(alloc_tutorial STATIC alloc.cpp)
TARGET_LINK_LIBRARIES(alloc_tutorial sys)
SET_PROPERTY(TARGET alloc_tutorial PROPERTY FOLDER tutorials/common)
SET_PROPERTY(TARGET alloc_tutorial APPEND PROPERTY COMPILE_FLAGS " ${FLAGS_LOWEST}")

IF (EMBREE_SYCL_SUPPORT)
ADD_LIBRARY(alloc_tutorial_sycl STATIC alloc.cpp)
TARGET_LINK_LIBRARIES(alloc_tutorial_sycl sys)
SET_PROPERTY(TARGET alloc_tutorial_sycl PROPERTY FOLDER tutorials/common)
SET_PROPERTY(TARGET alloc_tutorial_sycl APPEND PROPERTY COMPILE_FLAGS " ${FLAGS_LOWEST} ${CMAKE_CXX_FLAGS_SYCL}")
TARGET_COMPILE_DEFINITIONS(alloc_tutorial_sycl PUBLIC EMBREE_SYCL_TUTORIAL)
ENDIF()
51 changes: 51 additions & 0 deletions tutorials/common/alloc/alloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include "alloc.h"

////////////////////////////////////////////////////////////////////////////////
/// All Platforms
////////////////////////////////////////////////////////////////////////////////

namespace embree
{
#if defined(EMBREE_SYCL_SUPPORT)

__thread sycl::context* tls_context = nullptr;
__thread sycl::device* tls_device = nullptr;

void enableUSMAllocTutorial(sycl::context* context, sycl::device* device)
{
tls_context = context;
tls_device = device;
}

void disableUSMAllocTutorial()
{
tls_context = nullptr;
tls_device = nullptr;
}

#endif

void* alignedUSMMalloc(size_t size, size_t align, EmbreeUSMMode mode)
{
#if defined(EMBREE_SYCL_SUPPORT)
if (tls_context)
return alignedSYCLMalloc(tls_context,tls_device,size,align,mode);
else
#endif
return alignedMalloc(size,align);
}

void alignedUSMFree(void* ptr)
{
#if defined(EMBREE_SYCL_SUPPORT)
if (tls_context)
return alignedSYCLFree(tls_context,ptr);
else
#endif
return alignedFree(ptr);
}

}
34 changes: 34 additions & 0 deletions tutorials/common/alloc/alloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "../../../common/sys/alloc.h"

#if defined(EMBREE_SYCL_SUPPORT)
#include <sycl/sycl.hpp>
#endif

namespace embree
{
#if defined(EMBREE_SYCL_SUPPORT)

/* enables SYCL USM allocation */
void enableUSMAllocTutorial(sycl::context* context, sycl::device* device);

/* disables SYCL USM allocation */
void disableUSMAllocTutorial();

#endif

#define ALIGNED_STRUCT_USM_(align) \
void* operator new(size_t size) { return alignedUSMMalloc(size,align); } \
void operator delete(void* ptr) { alignedUSMFree(ptr); } \
void* operator new[](size_t size) { return alignedUSMMalloc(size,align); } \
void operator delete[](void* ptr) { alignedUSMFree(ptr); }

/*! aligned allocation using SYCL USM */
void* alignedUSMMalloc(size_t size, size_t align = 16, EmbreeUSMMode mode = EMBREE_USM_SHARED_DEVICE_READ_ONLY);
void alignedUSMFree(void* ptr);

}
1 change: 1 addition & 0 deletions tutorials/common/default.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once

#include "alloc/alloc.h"
#include "../../kernels/config.h"
#include "../../common/sys/platform.h"
#include "../../common/sys/sysinfo.h"
Expand Down
2 changes: 2 additions & 0 deletions tutorials/common/device_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
RTC_NAMESPACE_USE
#include "../../kernels/config.h"

#include "alloc/alloc.h"

namespace embree
{
#if defined(EMBREE_SYCL_TUTORIAL) && defined(EMBREE_SYCL_SUPPORT)
Expand Down
2 changes: 1 addition & 1 deletion tutorials/common/scenegraph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ ADD_LIBRARY(scenegraph STATIC
scenegraph.cpp
geometry_creation.cpp)

TARGET_LINK_LIBRARIES(scenegraph sys math lexers image embree)
TARGET_LINK_LIBRARIES(scenegraph alloc_tutorial sys math lexers image embree)
SET_PROPERTY(TARGET scenegraph PROPERTY FOLDER tutorials/common)
SET_PROPERTY(TARGET scenegraph APPEND PROPERTY COMPILE_FLAGS " ${FLAGS_LOWEST}")
2 changes: 2 additions & 0 deletions tutorials/common/texture/texture2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "texture2d.h"

#include "../alloc/alloc.h"

namespace embree {


Expand Down
4 changes: 2 additions & 2 deletions tutorials/common/tutorial/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ IF (EMBREE_TUTORIALS_GLFW)
ENDIF()

ADD_LIBRARY(tutorial STATIC tutorial.cpp application.cpp scene.cpp tutorial_device.cpp scene_device.cpp)
TARGET_LINK_LIBRARIES(tutorial sys math lexers scenegraph lights embree tasking ${GUI_LIBRARIES})
TARGET_LINK_LIBRARIES(tutorial alloc_tutorial sys math lexers scenegraph lights embree tasking ${GUI_LIBRARIES})
SET_PROPERTY(TARGET tutorial PROPERTY FOLDER tutorials/common)
SET_PROPERTY(TARGET tutorial APPEND PROPERTY COMPILE_FLAGS " ${FLAGS_LOWEST}")

IF (EMBREE_SYCL_SUPPORT)
ADD_LIBRARY(tutorial_sycl STATIC tutorial.cpp application.cpp scene.cpp tutorial_device.cpp scene_device.cpp)
TARGET_LINK_LIBRARIES(tutorial_sycl sys math lexers scenegraph lights_sycl embree tasking ze_wrapper ${GUI_LIBRARIES})
TARGET_LINK_LIBRARIES(tutorial_sycl alloc_tutorial_sycl sys math lexers scenegraph lights_sycl embree tasking ze_wrapper ${GUI_LIBRARIES})
SET_PROPERTY(TARGET tutorial_sycl PROPERTY FOLDER tutorials/common)
SET_PROPERTY(TARGET tutorial_sycl APPEND PROPERTY COMPILE_FLAGS " ${FLAGS_LOWEST} ${CMAKE_CXX_FLAGS_SYCL}")
TARGET_COMPILE_DEFINITIONS(tutorial_sycl PUBLIC EMBREE_SYCL_TUTORIAL)
Expand Down

0 comments on commit c5e0adb

Please sign in to comment.