diff --git a/example/anyflow/BUILD b/example/anyflow/BUILD new file mode 100644 index 00000000..fa7a4af1 --- /dev/null +++ b/example/anyflow/BUILD @@ -0,0 +1,7 @@ +cc_binary( + name = 'anyflow_multi_nodes', + srcs = ['anyflow_multi_nodes.cpp'], + deps = [ + '@com_baidu_babylon//:anyflow', + ], +) diff --git a/example/anyflow/WORKSPACE b/example/anyflow/WORKSPACE new file mode 100644 index 00000000..034bc9a5 --- /dev/null +++ b/example/anyflow/WORKSPACE @@ -0,0 +1,43 @@ +workspace(name = 'example') + +load('@bazel_tools//tools/build_defs/repo:http.bzl', 'http_archive') + +################################################################################ +# babylon +http_archive( + name = 'com_baidu_babylon', + urls = ['https://github.com/baidu/babylon/archive/refs/tags/v1.1.5.tar.gz'], + strip_prefix = 'babylon-1.1.5', + sha256 = 'a8d37251972a522b4c6f4d28ac6bf536444ff0e0c0e47eebff37aa75ca2a65a6', +) +################################################################################ + +################################################################################ +# copy from WORKSPACE of babylon +http_archive( + name = 'com_google_absl', + urls = ['https://github.com/abseil/abseil-cpp/archive/refs/tags/20230802.1.tar.gz'], + strip_prefix = 'abseil-cpp-20230802.1', + sha256 = '987ce98f02eefbaf930d6e38ab16aa05737234d7afbab2d5c4ea7adbe50c28ed', +) + +http_archive( + name = 'com_google_protobuf', + urls = ['https://github.com/protocolbuffers/protobuf/archive/refs/tags/v25.3.tar.gz'], + strip_prefix = 'protobuf-25.3', + sha256 = 'd19643d265b978383352b3143f04c0641eea75a75235c111cc01a1350173180e', + patches = ['@com_baidu_babylon//:registry/modules/protobuf/25.3.arenastring/patches/arenastring.patch'], + patch_args = ['-p1'], +) +load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") +protobuf_deps() + +http_archive( + name = 'com_github_nelhage_rules_boost', + urls = ['https://github.com/nelhage/rules_boost/archive/4ab574f9a84b42b1809978114a4664184716f4bf.tar.gz'], + strip_prefix = 'rules_boost-4ab574f9a84b42b1809978114a4664184716f4bf', + sha256 = '2215e6910eb763a971b1f63f53c45c0f2b7607df38c96287666d94d954da8cdc', +) +load("@com_github_nelhage_rules_boost//:boost/boost.bzl", "boost_deps") +boost_deps() +################################################################################ diff --git a/example/anyflow/anyflow_multi_nodes.cpp b/example/anyflow/anyflow_multi_nodes.cpp new file mode 100644 index 00000000..8469b1c8 --- /dev/null +++ b/example/anyflow/anyflow_multi_nodes.cpp @@ -0,0 +1,121 @@ +#include "babylon/anyflow/builder.h" + +#include + +using ::babylon::anyflow::GraphBuilder; +using ::babylon::anyflow::GraphProcessor; + +struct AddProcessor : public GraphProcessor { + virtual int process() noexcept override { + *c.emit() = *a + *b; + return 0; + } + + ANYFLOW_INTERFACE(ANYFLOW_DEPEND_DATA(int32_t, a, 0) + ANYFLOW_DEPEND_DATA(int32_t, b, 1) + ANYFLOW_EMIT_DATA(int32_t, c)) +}; + +struct SubtractProcessor : public GraphProcessor { + virtual int process() noexcept override { + *c.emit() = *a - *b; + return 0; + } + + ANYFLOW_INTERFACE(ANYFLOW_DEPEND_DATA(int32_t, a, 0) + ANYFLOW_DEPEND_DATA(int32_t, b, 1) + ANYFLOW_EMIT_DATA(int32_t, c)) +}; + +struct MultiplyProcessor : public GraphProcessor { + virtual int process() noexcept override { + *c.emit() = (*a) * (*b); + return 0; + } + + ANYFLOW_INTERFACE(ANYFLOW_DEPEND_DATA(int32_t, a, 0) + ANYFLOW_DEPEND_DATA(int32_t, b, 1) + ANYFLOW_EMIT_DATA(int32_t, c)) +}; + +int main() { + // let A = 10, B = 5 + // try to prove that (A + B) * (A - B) = A * A - B * B + int input_a = 10; + int input_b = 5; + int res_left = 0; + int res_right = 0; + { + GraphBuilder builder; + + auto& v1 = builder.add_vertex([] { + return ::std::unique_ptr(new AddProcessor); + }); + v1.named_depend("a").to("A"); + v1.named_depend("b").to("B"); + v1.named_emit("c").to("AddRes"); + + auto& v2 = builder.add_vertex([] { + return ::std::unique_ptr(new SubtractProcessor); + }); + v2.named_depend("a").to("A"); + v2.named_depend("b").to("B"); + v2.named_emit("c").to("SubtractRes"); + + auto& v3 = builder.add_vertex([] { + return ::std::unique_ptr(new MultiplyProcessor); + }); + v3.named_depend("a").to("AddRes"); + v3.named_depend("b").to("SubtractRes"); + v3.named_emit("c").to("FinalRes"); + + builder.finish(); + auto graph = builder.build(); + auto a = graph->find_data("A"); + auto b = graph->find_data("B"); + auto final_res = graph->find_data("FinalRes"); + + *(a->emit()) = input_a; + *(b->emit()) = input_b; + graph->run(final_res); + res_left = *final_res->value(); + } + { + GraphBuilder builder; + + auto& v1 = builder.add_vertex([] { + return ::std::unique_ptr(new MultiplyProcessor); + }); + v1.named_depend("a").to("A"); + v1.named_depend("b").to("A"); + v1.named_emit("c").to("MultiplyResForA"); + + auto& v2 = builder.add_vertex([] { + return ::std::unique_ptr(new MultiplyProcessor); + }); + v2.named_depend("a").to("B"); + v2.named_depend("b").to("B"); + v2.named_emit("c").to("MultiplyResForB"); + + auto& v3 = builder.add_vertex([] { + return ::std::unique_ptr(new SubtractProcessor); + }); + v3.named_depend("a").to("MultiplyResForA"); + v3.named_depend("b").to("MultiplyResForB"); + v3.named_emit("c").to("FinalRes"); + + builder.finish(); + auto graph = builder.build(); + auto a = graph->find_data("A"); + auto b = graph->find_data("B"); + auto final_res = graph->find_data("FinalRes"); + + *(a->emit()) = input_a; + *(b->emit()) = input_b; + graph->run(final_res); + res_right = *final_res->value(); + } + ::std::cout << "(A + B) * (A - B) = " << res_left << '\n'; + ::std::cout << "A * A - B * B = " << res_right << '\n'; + return 0; +} diff --git a/example/anyflow/build.sh b/example/anyflow/build.sh new file mode 100755 index 00000000..e0a2ebeb --- /dev/null +++ b/example/anyflow/build.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -ex + +bazel build //...