Skip to content

Commit

Permalink
Coroutine (#53)
Browse files Browse the repository at this point in the history
* babylon::Executor support coroutine
* babylon::ThreadPoolExecutor add work steal and balancer mode
  • Loading branch information
oathdruid committed Sep 20, 2024
1 parent 27b48d5 commit 3f881f1
Show file tree
Hide file tree
Showing 54 changed files with 2,716 additions and 564 deletions.
4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ common --registry=https://bcr.bazel.build
common --registry=file://%workspace%/registry
common --registry=https://raw.githubusercontent.com/bazelboost/registry/main

build --cxxopt=-std=c++17
build --cxxopt=-std=c++20

test --//:werror --features external_include_paths
test --@rules_cuda//cuda:enable=False

# SwissMemoryResource's patch violate odr rule. But still keep same struct with same defination and same size
# change detect_odr_violation to level 1 to check that
test --test_env=ASAN_OPTIONS=detect_odr_violation=1
Expand Down
24 changes: 14 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ on:
jobs:
basic:
strategy:
fail-fast: false
matrix:
compiler: [{name: gcc, flag: --action_env=CC=gcc-12}, {name: clang, flag: --action_env=CC=clang-14}]
std: [{name: c++17}, {name: c++14, flag: --cxxopt=-std=c++14 --cxxopt=-faligned-new}]
compiler: [{name: gcc, flag: --action_env=CC=gcc-14}, {name: clang, flag: --action_env=CC=clang-18}]
std: [{name: c++20}, {name: c++14, flag: --cxxopt=-std=c++14 --cxxopt=-faligned-new}]
stdlib: [{name: stdlibc++}, {name: libc++, flag: --cxxopt=-stdlib=libc++ --linkopt=-stdlib=libc++}]
feature: [{name: asan, flag: --config=asan}, {name: tsan, flag: --features=tsan}]
exclude:
- compiler: {name: gcc}
stdlib: {name: libc++}
- std: {name: c++14}
feature: {name: tsan}
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
name: basic-${{matrix.compiler.name}}-${{matrix.std.name}}-${{matrix.stdlib.name}}-${{matrix.feature.name}}
steps:
- uses: actions/checkout@v4
Expand All @@ -29,12 +30,14 @@ jobs:
key: bazel-disk-basic-${{matrix.compiler.name}}-${{matrix.std.name}}-${{matrix.stdlib.name}}-${{matrix.feature.name}}-${{github.sha}}
restore-keys: bazel-disk-basic-${{matrix.compiler.name}}-${{matrix.std.name}}-${{matrix.stdlib.name}}-${{matrix.feature.name}}-
save-always: true
- run: bazel test --compilation_mode=opt --disk_cache=bazel-disk --verbose_failures --test_output=errors ${{matrix.compiler.flag}} ${{matrix.std.flag}} ${{matrix.stdlib.flag}} ${{matrix.feature.flag}} test/...
- run: sudo apt install libc++-18-dev libc++abi-18-dev
- run: bazel test --compilation_mode=dbg --disk_cache=bazel-disk --verbose_failures --test_output=errors ${{matrix.compiler.flag}} ${{matrix.std.flag}} ${{matrix.stdlib.flag}} ${{matrix.feature.flag}} test/...

arenastring:
strategy:
fail-fast: false
matrix:
compiler: [{name: gcc, flag: --action_env=CC=gcc-12}, {name: clang, flag: --action_env=CC=clang-14}]
compiler: [{name: gcc, flag: --action_env=CC=gcc-14}, {name: clang, flag: --action_env=CC=clang-18}]
stdlib: [{name: stdlibc++}, {name: libc++, flag: --cxxopt=-stdlib=libc++ --linkopt=-stdlib=libc++}]
mutable: [{name: default}, {name: half, flag: --config=arenastring}, {name: full, flag: --config=mutable-donated-string}]
exclude:
Expand All @@ -44,7 +47,7 @@ jobs:
mutable: {name: half}
- compiler: {name: clang}
mutable: {name: full}
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
name: arenastring-${{matrix.compiler.name}}-${{matrix.stdlib.name}}-${{matrix.mutable.name}}
steps:
- uses: actions/checkout@v4
Expand All @@ -54,11 +57,12 @@ jobs:
key: bazel-disk-arenastring-${{matrix.compiler.name}}-${{matrix.stdlib.name}}-${{matrix.mutable.name}}-${{github.sha}}
restore-keys: bazel-disk-arenastring-${{matrix.compiler.name}}-${{matrix.stdlib.name}}-${{matrix.mutable.name}}-
save-always: true
- run: sudo apt install libc++-18-dev libc++abi-18-dev
- run: sed -i "/single_version_override.*protobuf/s/version = '[^']*'/version = '27.3.arenastring'/" MODULE.bazel
- run: bazel test --compilation_mode=opt --disk_cache=bazel-disk --verbose_failures --test_output=errors --config=asan ${{matrix.compiler.flag}} ${{matrix.stdlib.flag}} ${{matrix.mutable.flag}} test/...

aarch64:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
Expand All @@ -74,7 +78,7 @@ jobs:
- run: bazel test --disk_cache=bazel-disk --verbose_failures --test_output=errors --platforms='@cross_config_toolchain//:cross' --action_env=CROSS_CC=/usr/bin/aarch64-linux-gnu-gcc-12 --features=-default_link_flags --test_env=LD_LIBRARY_PATH=/usr/aarch64-linux-gnu/lib -- test/... -test/logging:test_log_statically

workspace:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
Expand All @@ -86,7 +90,7 @@ jobs:
- run: bazel test --disk_cache=bazel-disk --verbose_failures --test_output=errors --enable_bzlmod=false test/...

coverage:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
Expand All @@ -103,7 +107,7 @@ jobs:
file: bazel-out/_coverage/_coverage_report.lcov

cmake:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: hendrikmuhs/ccache-action@v1.2
Expand Down
5 changes: 5 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ alias(
actual = '//src/babylon/concurrent:vector',
)

alias(
name = 'coroutine',
actual = '//src/babylon:coroutine',
)

alias(
name = 'executor',
actual = '//src/babylon:executor',
Expand Down
16 changes: 15 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ include(CMakePackageConfigHelpers) # for write_basic_package_version_file

option(BUILD_DEPS "Use FetchContent download and build dependencies" OFF)

if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(CMAKE_CXX_STANDARD 20)
set(BUILD_TESTING ON)
endif()

if(BUILD_DEPS)
include(FetchContent)
FetchContent_Declare(
Expand All @@ -30,8 +35,16 @@ if(BUILD_DEPS)
URL "https://github.com/google/googletest/archive/refs/tags/v1.14.0.tar.gz"
URL_HASH SHA256=8ad598c73ad796e0d8280b082cebd82a630d73e73cd3c70057938a6501bba5d7
)
FetchContent_Declare(
libcoro
URL "https://github.com/jbaldwin/libcoro/archive/refs/tags/v0.12.1.tar.gz"
URL_HASH SHA256=2cb6f45fc73dad6008cc930d92939785684835e03b12df422b98fcab9e393add
)
if(BUILD_TESTING AND CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
FetchContent_MakeAvailable(googletest)
set(LIBCORO_FEATURE_NETWORKING OFF)
set(LIBCORO_BUILD_TESTS OFF)
set(LIBCORO_BUILD_EXAMPLES OFF)
FetchContent_MakeAvailable(googletest libcoro)
endif()
set(protobuf_BUILD_TESTS OFF)
set(ABSL_ENABLE_INSTALL ON)
Expand Down Expand Up @@ -113,6 +126,7 @@ if(BUILD_TESTING AND CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
target_link_libraries("${TARGET_NAME}" babylon_test_proto)
target_link_libraries("${TARGET_NAME}" babylon)
target_link_libraries("${TARGET_NAME}" GTest::gtest_main)
target_link_libraries("${TARGET_NAME}" libcoro)
gtest_discover_tests("${TARGET_NAME}")
endforeach()

Expand Down
12 changes: 4 additions & 8 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@ single_version_override(module_name = 'protobuf', version = '27.3')
bazel_dep(name = 'googletest', version = '1.15.2', repo_name = 'com_google_googletest', dev_dependency = True)
bazel_dep(name = 'platforms', version = '0.0.10', dev_dependency = True)
bazel_dep(name = 'rules_cc', version = '0.0.9', dev_dependency = True)
bazel_dep(name = 'rules_cuda', version = '0.2.3', dev_dependency = True)

# rules_cuda latest release 0.2.1 is too old and do not have auto detect feature
bazel_dep(name = 'rules_cuda', version = '0.2.1', dev_dependency = True)
archive_override(
module_name = 'rules_cuda',
urls = ['https://github.com/bazel-contrib/rules_cuda/archive/3482c70dc60d9ab1ad26b768c117fcd61ee12494.tar.gz'],
strip_prefix = 'rules_cuda-3482c70dc60d9ab1ad26b768c117fcd61ee12494',
integrity = 'sha256-x78dpBtaMUgKBHf0ztSe7QirHLOv93xwTjc8+cUmlPU=',
)
# --registry=https://baidu.github.io/babylon/registry
bazel_dep(name = 'libcoro', version = '0.12.1', dev_dependency = True)

# cuda toolchain
cuda = use_extension('@rules_cuda//cuda:extensions.bzl', 'toolchain', dev_dependency = True)
cuda.local_toolchain()

Expand Down
49 changes: 34 additions & 15 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ load('@bazel_tools//tools/build_defs/repo:http.bzl', 'http_archive')

http_archive(
name = 'com_google_absl',
urls = ['https://github.com/abseil/abseil-cpp/archive/refs/tags/20211102.0.tar.gz'],
strip_prefix = 'abseil-cpp-20211102.0',
sha256 = 'dcf71b9cba8dc0ca9940c4b316a0c796be8fab42b070bb6b7cab62b48f0e66c4',
urls = ['https://github.com/abseil/abseil-cpp/archive/refs/tags/20220623.1.tar.gz'],
strip_prefix = 'abseil-cpp-20220623.1',
sha256 = '91ac87d30cc6d79f9ab974c51874a704de9c2647c40f6932597329a282217ba8',
)

http_archive(
name = 'com_google_protobuf',
urls = ['https://github.com/protocolbuffers/protobuf/archive/refs/tags/v3.19.6.tar.gz'],
urls = ['https://github.com/protocolbuffers/protobuf/archive/refs/tags/v3.19.6.zip'],
strip_prefix = 'protobuf-3.19.6',
sha256 = '9a301cf94a8ddcb380b901e7aac852780b826595075577bb967004050c835056',
sha256 = '387e2c559bb2c7c1bc3798c4e6cff015381a79b2758696afcbf8e88730b47389',
)
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
load('@com_google_protobuf//:protobuf_deps.bzl', 'protobuf_deps')
protobuf_deps()

http_archive(
Expand All @@ -24,25 +24,44 @@ http_archive(
strip_prefix = 'rules_boost-4ab574f9a84b42b1809978114a4664184716f4bf',
sha256 = '2215e6910eb763a971b1f63f53c45c0f2b7607df38c96287666d94d954da8cdc',
)
load("@com_github_nelhage_rules_boost//:boost/boost.bzl", "boost_deps")
load('@com_github_nelhage_rules_boost//:boost/boost.bzl', 'boost_deps')
boost_deps()

################################################################################
# test only dependency
http_archive(
name = 'com_google_googletest',
urls = ['https://github.com/google/googletest/archive/refs/tags/v1.14.0.tar.gz'],
strip_prefix = 'googletest-1.14.0',
sha256 = '8ad598c73ad796e0d8280b082cebd82a630d73e73cd3c70057938a6501bba5d7',
urls = ['https://github.com/google/googletest/releases/download/v1.15.2/googletest-1.15.2.tar.gz'],
strip_prefix = 'googletest-1.15.2',
sha256 = '7b42b4d6ed48810c5362c265a17faebe90dc2373c885e5216439d37927f02926',
)

http_archive(
name = "rules_cuda",
urls = ["https://github.com/bazel-contrib/rules_cuda/archive/3482c70dc60d9ab1ad26b768c117fcd61ee12494.tar.gz"],
strip_prefix = "rules_cuda-3482c70dc60d9ab1ad26b768c117fcd61ee12494",
sha256 = 'c7bf1da41b5a31480a0477f4ced49eed08ab1cb3aff77c704e373cf9c52694f5',
name = 'rules_foreign_cc',
urls = ['https://github.com/bazelbuild/rules_foreign_cc/releases/download/0.12.0/rules_foreign_cc-0.12.0.tar.gz'],
strip_prefix = 'rules_foreign_cc-0.12.0',
sha256 = 'a2e6fb56e649c1ee79703e99aa0c9d13c6cc53c8d7a0cbb8797ab2888bbc99a3',
)
load("@rules_cuda//cuda:repositories.bzl", "register_detected_cuda_toolchains", "rules_cuda_dependencies")
load('@rules_foreign_cc//foreign_cc:repositories.bzl', 'rules_foreign_cc_dependencies')
rules_foreign_cc_dependencies()
load("@bazel_features//:deps.bzl", "bazel_features_deps")
bazel_features_deps()

http_archive(
name = 'libcoro',
urls = ['https://github.com/jbaldwin/libcoro/archive/refs/tags/v0.12.1.tar.gz'],
strip_prefix = 'libcoro-0.12.1',
sha256 = '2cb6f45fc73dad6008cc930d92939785684835e03b12df422b98fcab9e393add',
build_file = '@//:registry/modules/libcoro/0.12.1/overlay/BUILD',
)

http_archive(
name = 'rules_cuda',
urls = ['https://github.com/bazel-contrib/rules_cuda/releases/download/v0.2.3/rules_cuda-v0.2.3.tar.gz'],
strip_prefix = 'rules_cuda-v0.2.3',
sha256 = 'c92b334d769a07cd991b7675b2f6076b8b95cd3b28b14268a2f379f8baae58e0',
)
load('@rules_cuda//cuda:repositories.bzl', 'register_detected_cuda_toolchains', 'rules_cuda_dependencies')
rules_cuda_dependencies()
register_detected_cuda_toolchains()
################################################################################
6 changes: 4 additions & 2 deletions copts.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ BABYLON_CLANG_COPTS = ['-faligned-new', '-Weverything', '-Wno-unknown-warning-op
# BABYLON_SERIALIZABLE宏内部定义的一些辅助成员在继承场景下会发生同名隐藏
# 但是这个场景下的隐藏本身是无害的,目前也还没有很好的方法做命名区分
'-Wno-shadow-field',
# 采用和gcc更一致的-Wswitch-default风格
'-Wno-covered-switch-default',
# TODO(lijiang01): 逐步梳理清除
'-Wno-weak-vtables', '-Wno-float-conversion', '-Wno-switch-enum',
'-Wno-weak-vtables', '-Wno-float-conversion', '-Wno-switch-enum', '-Wno-c++17-extensions',
'-Wno-gnu-anonymous-struct', '-Wno-nested-anon-types',
'-Wno-array-bounds-pointer-arithmetic', '-Wno-cast-align', '-Wno-vla-extension',
'-Wno-unneeded-member-function', '-Wno-deprecated-declarations']
'-Wno-unneeded-member-function', '-Wno-deprecated-declarations', '-Wno-unsafe-buffer-usage']

BABYLON_COPTS = select({
'//:compiler_gcc': BABYLON_GCC_COPTS,
Expand Down
7 changes: 7 additions & 0 deletions registry/modules/libcoro/0.12.1/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module(
name = 'libcoro',
version = '0.12.1',
compatibility_level = 1,
)

bazel_dep(name = 'rules_foreign_cc', version = '0.8.0')
22 changes: 22 additions & 0 deletions registry/modules/libcoro/0.12.1/overlay/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package(
default_visibility = ['//:__pkg__'],
)

load('@rules_foreign_cc//foreign_cc:defs.bzl', 'cmake')

filegroup(
name = 'all_srcs',
srcs = glob(['**'], exclude = ['bazel-*/**']),
)

cmake(
name = 'libcoro',
lib_source = ':all_srcs',
cache_entries = {
'LIBCORO_BUILD_TESTS': 'OFF',
'LIBCORO_BUILD_EXAMPLES': 'OFF',
'LIBCORO_FEATURE_NETWORKING': 'OFF',
},
lib_name = 'libcoro',
visibility = ['//visibility:public'],
)
1 change: 1 addition & 0 deletions registry/modules/libcoro/0.12.1/overlay/MODULE.bazel
9 changes: 9 additions & 0 deletions registry/modules/libcoro/0.12.1/source.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"url": "https://github.com/jbaldwin/libcoro/archive/refs/tags/v0.12.1.tar.gz",
"strip_prefix": "libcoro-0.12.1",
"integrity": "sha256-LLb0X8c9rWAIzJMNkpOXhWhINeA7Et9CK5j8q545Ot0=",
"overlay": {
"MODULE.bazel": "sha256-qMTe85CeeBmQXe2lUdrFXVu1K6oMPJ7CiwGGPbd1+A0=",
"BUILD": "sha256-et8EWyLNFl1uG3Zq+wsCOQyGkTFir9XXQEfwSki+ocI="
}
}
30 changes: 22 additions & 8 deletions src/babylon/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,42 @@ cc_library(
)

cc_library(
name = 'environment',
hdrs = ['environment.h', 'protect.h', 'unprotect.h'],
name = 'coroutine_and_executor',
srcs = ['coroutine.cpp', 'executor.cpp'],
hdrs = ['coroutine.h', 'current_executor.h', 'executor.h', 'executor.hpp'],
copts = BABYLON_COPTS,
includes = ['//src'],
strip_include_prefix = '//src',
deps = [
'@com_google_absl//absl/base:core_headers',
':future',
'//src/babylon/logging',
'//src/babylon/concurrent:bounded_queue',
'@libcoro',
],
)

alias(
name = 'coroutine',
actual = '//src/babylon:coroutine_and_executor',
)


cc_library(
name = 'executor',
srcs = ['executor.cpp'],
hdrs = ['executor.h', 'executor.hpp'],
name = 'environment',
hdrs = ['environment.h', 'protect.h', 'unprotect.h'],
copts = BABYLON_COPTS,
includes = ['//src'],
strip_include_prefix = '//src',
deps = [
':future',
'//src/babylon/concurrent:bounded_queue',
'@com_google_absl//absl/base:core_headers',
],
)

alias(
name = 'executor',
actual = '//src/babylon:coroutine_and_executor',
)

cc_library(
name = 'future',
hdrs = ['future.h', 'future.hpp'],
Expand Down Expand Up @@ -220,5 +233,6 @@ cc_library(
deps = [
':absl_base_internal_invoke',
':string_view',
'@com_google_absl//absl/utility',
],
)
1 change: 1 addition & 0 deletions src/babylon/anyflow/builder.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "babylon/any.h"
#include "babylon/anyflow/dependency.h"
#include "babylon/anyflow/executor.h"
#include "babylon/reusable/memory_resource.h"

Expand Down
2 changes: 1 addition & 1 deletion src/babylon/anyflow/builtin/alias.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void AliasProcessor::apply(GraphBuilder& builder, const ::std::string& alias,
vertex.anonymous_emit().to(alias);
}

::std::atomic<size_t> AliasProcessor::_s_idx = ATOMIC_VAR_INIT(0);
::std::atomic<size_t> AliasProcessor::_s_idx {0};

} // namespace builtin
} // namespace anyflow
Expand Down
2 changes: 1 addition & 1 deletion src/babylon/anyflow/builtin/const.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ GraphVertexBuilder& ConstProcessor::apply_without_value(
return vertex;
}

::std::atomic<size_t> ConstProcessor::_s_idx = ATOMIC_VAR_INIT(0);
::std::atomic<size_t> ConstProcessor::_s_idx {0};

} // namespace builtin
} // namespace anyflow
Expand Down
Loading

0 comments on commit 3f881f1

Please sign in to comment.