Skip to content

Commit

Permalink
libdispatch/tbb on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-Leo-Smith committed Dec 17, 2024
1 parent 042d6b7 commit 78501f2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/backends/fallback/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,23 @@ if (LLVM_FOUND AND embree_FOUND)
# exclude codegen from unity build since it's too big
set_source_files_properties(fallback_codegen.cpp PROPERTIES UNITY_BUILD OFF)

# use TBB for parallel_for on non-Apple Unix systems if available
# use libdispatch or TBB for parallel_for on non-Apple Unix systems if available
if (UNIX AND NOT APPLE)
find_package(TBB CONFIG)
if (TBB_FOUND)
target_compile_definitions(luisa-compute-backend-fallback PRIVATE LUISA_COMPUTE_ENABLE_TBB=1)
target_link_libraries(luisa-compute-backend-fallback PRIVATE TBB::tbb)
# libdispatch
find_library(DISPATCH_LIB dispatch)
find_path(DISPATCH_INCLUDE_DIR dispatch/dispatch.h)
if (DISPATCH_LIB AND DISPATCH_INCLUDE_DIR)
target_compile_definitions(luisa-compute-backend-fallback PRIVATE LUISA_COMPUTE_ENABLE_LIBDISPATCH=1)
target_link_libraries(luisa-compute-backend-fallback PRIVATE ${DISPATCH_LIB})
target_include_directories(luisa-compute-backend-fallback PRIVATE ${DISPATCH_INCLUDE_DIR})
else ()
message(WARNING "TBB not found. Fallback backend will not use TBB. Performance may be affected.")
find_package(TBB CONFIG)
if (TBB_FOUND)
target_compile_definitions(luisa-compute-backend-fallback PRIVATE LUISA_COMPUTE_ENABLE_TBB=1)
target_link_libraries(luisa-compute-backend-fallback PRIVATE TBB::tbb)
else ()
message(WARNING "TBB and libdispatch not found. Performance of the fallback backend may be affected.")
endif ()
endif ()
endif ()

Expand Down
4 changes: 4 additions & 0 deletions src/backends/fallback/fallback_command_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ void FallbackCommandQueue::enqueue_parallel(uint n, luisa::move_only_function<vo
enqueue([this, n, task = std::move(task)]() mutable noexcept {
#if defined(LUISA_FALLBACK_USE_DISPATCH_QUEUE)
if (_dispatch_queue == nullptr) {
#ifdef QOS_CLASS_USER_INTERACTIVE
_dispatch_queue = dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0);
#else
_dispatch_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
#endif
dispatch_retain(_dispatch_queue);
}
dispatch_apply_f(n, _dispatch_queue, &task, [](void *context, size_t idx) noexcept {
Expand Down
3 changes: 3 additions & 0 deletions src/backends/fallback/fallback_command_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#elif defined(LUISA_PLATFORM_WINDOWS)
#define LUISA_FALLBACK_USE_PPL
#include <ppl.h>
#elif defined(LUISA_COMPUTE_ENABLE_LIBDISPATCH)
#define LUISA_FALLBACK_USE_DISPATCH_QUEUE
#include <dispatch/dispatch.h>
#elif defined(LUISA_COMPUTE_ENABLE_TBB)
#define LUISA_FALLBACK_USE_TBB
#include <tbb/parallel_for.h>
Expand Down

0 comments on commit 78501f2

Please sign in to comment.