Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CI workflow for testing documentation examples #1629

Merged
merged 25 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3d6330a
Save progress
kboyarinov Dec 25, 2024
8907e3a
Merge remote-tracking branch 'origin/master' into dev/kboyarinov/test…
kboyarinov Feb 4, 2025
a668456
Commit missed files
kboyarinov Feb 4, 2025
d2f7ea5
Add CI workflow
kboyarinov Feb 5, 2025
0ce3b4b
Fix CI workflow
kboyarinov Feb 5, 2025
99d1bbe
Fix build library stage
kboyarinov Feb 5, 2025
6d16822
Add tmp workflow debug
kboyarinov Feb 5, 2025
9ad06ec
Fix directory
kboyarinov Feb 5, 2025
78a49b1
Add source vars
kboyarinov Feb 5, 2025
2881955
Add end of files
kboyarinov Feb 5, 2025
601df07
Remove .h files use comment tags instead
kboyarinov Feb 5, 2025
9823c70
Add blocked_nd_range example
kboyarinov Feb 5, 2025
1b12acd
Update copyright for ci.yml
kboyarinov Feb 5, 2025
39af2e8
Bump STL version for testing
kboyarinov Feb 5, 2025
b9f1e5e
Add to main CMake lists + Windows workflow
kboyarinov Feb 6, 2025
0c4225b
Add brackets
kboyarinov Feb 6, 2025
00292b9
Separate Windows workflow, remove public CMake option
kboyarinov Feb 6, 2025
e183300
Fix Windows workflow variable name
kboyarinov Feb 6, 2025
53a4bf7
Apply review comments
kboyarinov Feb 6, 2025
9c00370
Add tbbmalloc_proxy
kboyarinov Feb 6, 2025
54ba82d
Add config into the build command on Windows
kboyarinov Feb 6, 2025
78d1b99
Fix arguments order
kboyarinov Feb 6, 2025
dfbb5bf
Update fixed_pool_example.cpp
kboyarinov Feb 11, 2025
72435a3
Fix inclusions for examples
kboyarinov Feb 11, 2025
1840c13
Merge branch 'dev/kboyarinov/testing-doc-examples' of https://github.…
kboyarinov Feb 11, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2021-2024 Intel Corporation
# Copyright (c) 2021-2025 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -366,3 +366,53 @@ jobs:
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx_compiler }} `
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }} -DTBB_CPF=${{ matrix.preview }} -DTBB_TEST=OFF -DTBB_EXAMPLES=ON ..
cmake --build . -v --target light_test_examples

linux-doc-examples-testing:
name: doc_examples_${{ matrix.os }}_${{ matrix.cxx_compiler }}_cxx${{ matrix.std }}_${{ matrix.build_type }}
runs-on: ['${{ matrix.os }}']
timeout-minutes: 10
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
c_compiler: gcc
cxx_compiler: g++
std: 20
build_type: relwithdebinfo
steps:
- uses: actions/checkout@v4
- name: Test doc examples
run: |
mkdir build && cd build
cmake -DCMAKE_CXX_STANDARD=${{ matrix.std }} -DCMAKE_BUILD_TYPE=${{ matrix.build_type}} \
-DCMAKE_CXX_COMPILER=${{ matrix.cxx_compiler }} -DCMAKE_C_COMPILER=${{ matrix.c_compiler }} \
-DTBB_TEST=OFF -DTBB_DOC_EXAMPLES=ON ..
cmake --build . -v --parallel --target build-doc-examples
ctest -C ${{ matrix.build_type }} --timeout ${env:TEST_TIMEOUT} --output-on-failure -L doc-examples

windows-doc-examples-testing:
name: doc_examples_${{ matrix.os }}_${{ matrix.cxx_compiler }}_cxx${{ matrix.std }}_${{ matrix.build_type }}
runs-on: ['${{ matrix.os }}']
timeout-minutes: 10
strategy:
fail-fast: false
matrix:
include:
- os: windows-2022
generator: "Visual Studio 17 2022"
c_compiler: cl
cxx_compiler: cl
std: 20
build_type: relwithdebinfo
steps:
- uses: actions/checkout@v4
- name: Test doc examples
run: |
mkdir build
cd build
cmake -G "${{ matrix.generator }}" -A x64 -DCMAKE_CXX_STANDARD=${{ matrix.std }} `
-DCMAKE_BUILD_TYPE=${{ matrix.build_type}} -DCMAKE_CXX_COMPILER=${{ matrix.cxx_compiler }} `
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }} -DTBB_TEST=OFF -DTBB_DOC_EXAMPLES=ON ..
cmake --build . --config ${{ matrix.build_type }} -v --parallel --target build-doc-examples
ctest -C ${{ matrix.build_type }} --timeout ${env:TEST_TIMEOUT} --output-on-failure -L doc-examples
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,18 @@ if (TBB_EXAMPLES)
add_subdirectory(examples)
endif()


if (TBB_DOC_EXAMPLES)
set(TBB_DOC_EXAMPLES_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/doc/main/examples_testing")
if (NOT EXISTS ${TBB_DOC_EXAMPLES_DIRECTORY})
message(FATAL_ERROR "Documentation examples are not found while testing was enabled")
isaevil marked this conversation as resolved.
Show resolved Hide resolved
else()
message(STATUS "Enabling testing for examples from documentation")
enable_testing()
add_subdirectory(${TBB_DOC_EXAMPLES_DIRECTORY})
endif()
endif()

if (TBB_BENCH)
if (NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/benchmark)
message(FATAL_ERROR "Benchmarks are not supported yet")
Expand Down
41 changes: 41 additions & 0 deletions doc/main/examples_testing/CMakeLists.txt
kboyarinov marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (c) 2025 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.11)
project(doc_examples_testing LANGUAGES CXX)

set(_reference_examples_path "${CMAKE_CURRENT_SOURCE_DIR}/../reference/examples")
set(_userguide_examples_path "${CMAKE_CURRENT_SOURCE_DIR}/../tbb_userguide/examples")

add_custom_target(build-doc-examples
COMMENT "Build oneTBB documentation samples")
set(doc_examples_test_label "doc-examples")

macro(add_doc_example _doc_example_path)
get_filename_component(_doc_example_name "${_doc_example_path}" NAME_WE)
add_executable(${_doc_example_name} EXCLUDE_FROM_ALL "${_doc_example_path}")

add_dependencies(${_doc_example_name} TBB::tbb TBB::tbbmalloc TBB::tbbmalloc_proxy)
target_link_libraries(${_doc_example_name} TBB::tbb TBB::tbbmalloc TBB::tbbmalloc_proxy)

add_dependencies(build-doc-examples ${_doc_example_name})
add_test(NAME ${_doc_example_name} COMMAND ${_doc_example_name})
set_tests_properties(${_doc_example_name} PROPERTIES LABELS "${doc_examples_test_label}")
endmacro()

file(GLOB_RECURSE DOC_EXAMPLES_LIST "${_reference_examples_path}/*.cpp" "${_userguide_examples_path}/*.cpp")

foreach(_doc_example_path IN LISTS DOC_EXAMPLES_LIST)
add_doc_example(${_doc_example_path})
endforeach()
93 changes: 4 additions & 89 deletions doc/main/reference/custom_mutex_chmap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,92 +67,7 @@ and ``oneapi::tbb::rw_mutex`` meet the requirements above.
The example below demonstrates how to wrap ``std::shared_mutex`` (C++17) to meet the requirements
of `ReaderWriterMutex` and how to customize ``concurrent_hash_map`` to use this mutex.

.. code:: cpp

#define TBB_PREVIEW_CONCURRENT_HASH_MAP_EXTENSIONS 1
#include "oneapi/tbb/concurrent_hash_map.h"
#include <shared_mutex>

class SharedMutexWrapper {
public:
// ReaderWriterMutex requirements

static constexpr bool is_rw_mutex = true;
static constexpr bool is_recursive_mutex = false;
static constexpr bool is_fair_mutex = false;

class scoped_lock {
public:
scoped_lock() : my_mutex_ptr(nullptr), my_writer_flag(false) {}
scoped_lock(SharedMutexWrapper& mutex, bool write = true)
: my_mutex_ptr(&mutex), my_writer_flag(write)
{
if (my_writer_flag) {
my_mutex_ptr->my_mutex.lock();
} else {
my_mutex_ptr->my_mutex.lock_shared();
}
}

~scoped_lock() {
if (my_mutex_ptr) release();
}

void acquire(SharedMutexWrapper& mutex, bool write = true) {
if (my_mutex_ptr) release();

my_mutex_ptr = &mutex;
my_writer_flag = write;

if (my_writer_flag) {
my_mutex_ptr->my_mutex.lock();
} else {
my_mutex_ptr->my_mutex.lock_shared();
}
}

void release() {
if (my_writer_flag) {
my_mutex_ptr->my_mutex.unlock();
} else {
my_mutex_ptr->my_mutex.unlock_shared();
}
}

bool upgrade_to_writer() {
// std::shared_mutex does not have the upgrade/downgrade parallel_for_each_semantics
if (my_writer_flag) return true; // Already a writer

my_mutex_ptr->my_mutex.unlock_shared();
my_mutex_ptr->my_mutex.lock();
return false; // The lock was reacquired
}

bool downgrade_to_reader() {
if (!my_writer_flag) return true; // Already a reader

my_mutex_ptr->my_mutex.unlock();
my_mutex_ptr->my_mutex.lock_shared();
return false;
}

bool is_writer() const {
return my_writer_flag;
}

private:
SharedMutexWrapper* my_mutex_ptr;
bool my_writer_flag;
};
private:
std::shared_mutex my_mutex;
}; // struct SharedMutexWrapper

int main() {
using map_type = oneapi::tbb::concurrent_hash_map<int, int,
oneapi::tbb::tbb_hash_compare<int>,
oneapi::tbb::tbb_allocator<std::pair<const int, int>>,
SharedMutexWrapper>;

map_type map; // This object will use SharedMutexWrapper for thread safety of insert/find/erase operations
}
.. literalinclude:: ./examples/custom_mutex_chmap_example.cpp
:language: c++
:start-after: /*begin_custom_mutex_chmap_example*/
:end-before: /*end_custom_mutex_chmap_example*/
129 changes: 129 additions & 0 deletions doc/main/reference/examples/custom_mutex_chmap_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
Copyright (c) 2025 Intel Corporation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#if __cplusplus >= 201703L

/*begin_custom_mutex_chmap_example*/
#define TBB_PREVIEW_CONCURRENT_HASH_MAP_EXTENSIONS 1
#include "oneapi/tbb/concurrent_hash_map.h"
#include <shared_mutex>

class SharedMutexWrapper {
public:
// ReaderWriterMutex requirements

static constexpr bool is_rw_mutex = true;
static constexpr bool is_recursive_mutex = false;
static constexpr bool is_fair_mutex = false;

class scoped_lock {
public:
scoped_lock() : my_mutex_ptr(nullptr), my_writer_flag(false) {}
scoped_lock(SharedMutexWrapper& mutex, bool write = true)
: my_mutex_ptr(&mutex), my_writer_flag(write)
{
if (my_writer_flag) {
my_mutex_ptr->my_mutex.lock();
} else {
my_mutex_ptr->my_mutex.lock_shared();
}
}

~scoped_lock() {
if (my_mutex_ptr) release();
}

void acquire(SharedMutexWrapper& mutex, bool write = true) {
if (my_mutex_ptr) release();

my_mutex_ptr = &mutex;
my_writer_flag = write;

if (my_writer_flag) {
my_mutex_ptr->my_mutex.lock();
} else {
my_mutex_ptr->my_mutex.lock_shared();
}
}

bool try_acquire(SharedMutexWrapper& mutex, bool write = true) {
if (my_mutex_ptr) release();

my_mutex_ptr = &mutex;

bool result = false;

if (my_writer_flag) {
result = my_mutex_ptr->my_mutex.try_lock();
} else {
result = my_mutex_ptr->my_mutex.try_lock_shared();
}

if (result) my_writer_flag = write;
return result;
}

void release() {
if (my_writer_flag) {
my_mutex_ptr->my_mutex.unlock();
} else {
my_mutex_ptr->my_mutex.unlock_shared();
}
}

bool upgrade_to_writer() {
// std::shared_mutex does not have the upgrade/downgrade semantics
if (my_writer_flag) return true; // Already a writer

my_mutex_ptr->my_mutex.unlock_shared();
my_mutex_ptr->my_mutex.lock();
return false; // The lock was reacquired
}

bool downgrade_to_reader() {
if (!my_writer_flag) return true; // Already a reader

my_mutex_ptr->my_mutex.unlock();
my_mutex_ptr->my_mutex.lock_shared();
return false;
}

bool is_writer() const {
return my_writer_flag;
}

private:
SharedMutexWrapper* my_mutex_ptr;
bool my_writer_flag;
};
private:
std::shared_mutex my_mutex;
}; // struct SharedMutexWrapper

int main() {
using map_type = oneapi::tbb::concurrent_hash_map<int, int,
oneapi::tbb::tbb_hash_compare<int>,
oneapi::tbb::tbb_allocator<std::pair<const int, int>>,
SharedMutexWrapper>;

map_type map; // This object will use SharedMutexWrapper for thread safety of insert/find/erase operations
}
/*end_custom_mutex_chmap_example*/

#else // C++17
// Skip
int main() {}
#endif
28 changes: 28 additions & 0 deletions doc/main/reference/examples/fixed_pool_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright (c) 2025 Intel Corporation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/*begin_fixed_pool_example*/
#define TBB_PREVIEW_MEMORY_POOL 1
#include "oneapi/tbb/memory_pool.h"

int main() {
char buf[1024];
oneapi::tbb::fixed_pool my_pool(buf, 1024);

void* my_ptr = my_pool.malloc(10);
my_pool.free(my_ptr);
}
/*end_fixed_pool_example*/
Loading