Skip to content

Commit

Permalink
Add TaskFlow dependency and test
Browse files Browse the repository at this point in the history
  • Loading branch information
yshekel committed Dec 30, 2024
1 parent f309d6f commit 1d82ef2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
20 changes: 20 additions & 0 deletions icicle/backend/cpu/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
cmake_minimum_required(VERSION 3.18)

message(STATUS "Fetching Taskflow v3.8.0 (CPU backend)")
include(FetchContent)
FetchContent_Declare(
Taskflow
GIT_REPOSITORY https://github.com/taskflow/taskflow.git
GIT_TAG v3.8.0
GIT_SHALLOW TRUE
)
# Disable unnecessary components
set(TF_BUILD_BENCHMARKS OFF CACHE BOOL "Disable Taskflow benchmarks" FORCE)
set(TF_BUILD_PROFILER OFF CACHE BOOL "Disable Taskflow profiler" FORCE)
set(TF_BUILD_CUDA OFF CACHE BOOL "Disable Taskflow CUDA support" FORCE)
set(TF_BUILD_SYCL OFF CACHE BOOL "Disable Taskflow SYCL support" FORCE)
set(TF_BUILD_TESTS OFF CACHE BOOL "Disable Taskflow tests" FORCE)
set(TF_BUILD_EXAMPLES OFF CACHE BOOL "Disable Taskflow examples" FORCE)

FetchContent_MakeAvailable(Taskflow)
# Use icicle_device as interface for TaskFlow headers
target_include_directories(icicle_device INTERFACE ${Taskflow_SOURCE_DIR})

# CPU backend is built directly into icicle library

target_sources(icicle_device PRIVATE src/cpu_device_api.cpp)
Expand Down
50 changes: 50 additions & 0 deletions icicle/tests/test_field_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,56 @@ TEST_F(FieldApiTestBase, CpuProgramExecutorReturningVal)
ASSERT_EQ(0, memcmp(out_element_wise.get(), out_vec_ops.get(), total_size * sizeof(scalar_t)));
}

#include <taskflow/taskflow.hpp> // Taskflow library
#include <iostream>
#include <vector>

TEST_F(FieldApiTestBase, Taskflow)
{
constexpr size_t N = 1 << 20;
auto vec1 = std::make_unique<scalar_t[]>(N);
auto vec2 = std::make_unique<scalar_t[]>(N);
auto resultSerial = std::make_unique<scalar_t[]>(N);
auto resultParallel = std::make_unique<scalar_t[]>(N);
scalar_t::rand_host_many(vec1.get(), N);
scalar_t::rand_host_many(vec2.get(), N);

// Measure time for Serial computation
START_TIMER(Serial)
for (size_t j = 0; j < N; ++j) {
resultSerial[j] = vec1[j] * vec2[j];
}
auto end = std::chrono::high_resolution_clock::now();
END_TIMER(Serial, "Serial computation completed in ", true);

// Measure time for parallel computation
START_TIMER(Parallel)

tf::Taskflow taskflow;
tf::Executor executor;

// Number of chunks for parallel processing
size_t num_chunks = 8; // Adjust based on the number of threads
size_t chunk_size = (N + num_chunks - 1) / num_chunks;

for (size_t i = 0; i < num_chunks; ++i) {
size_t start_index = i * chunk_size;
size_t end_index = std::min(start_index + chunk_size, N);

taskflow.emplace([&, start_index, end_index]() {
for (size_t j = start_index; j < end_index; ++j) {
resultParallel[j] = vec1[j] * vec2[j];
}
});
}

executor.run(taskflow).wait();

END_TIMER(Parallel, "Parallel computation completed in ", true);

ASSERT_EQ(0, memcmp(resultSerial.get(), resultParallel.get(), N * sizeof(scalar_t)));
}

int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
Expand Down

0 comments on commit 1d82ef2

Please sign in to comment.