-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add test for multi processors * move an anyflow example to a standalone folder
- Loading branch information
Showing
4 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
cc_binary( | ||
name = 'anyflow_multi_nodes', | ||
srcs = ['anyflow_multi_nodes.cpp'], | ||
deps = [ | ||
'@com_baidu_babylon//:anyflow', | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
################################################################################ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#include "babylon/anyflow/builder.h" | ||
|
||
#include <iostream> | ||
|
||
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<AddProcessor>(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<SubtractProcessor>(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<MultiplyProcessor>(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<int>()) = input_a; | ||
*(b->emit<int>()) = input_b; | ||
graph->run(final_res); | ||
res_left = *final_res->value<int>(); | ||
} | ||
{ | ||
GraphBuilder builder; | ||
|
||
auto& v1 = builder.add_vertex([] { | ||
return ::std::unique_ptr<MultiplyProcessor>(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<MultiplyProcessor>(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<SubtractProcessor>(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<int>()) = input_a; | ||
*(b->emit<int>()) = input_b; | ||
graph->run(final_res); | ||
res_right = *final_res->value<int>(); | ||
} | ||
::std::cout << "(A + B) * (A - B) = " << res_left << '\n'; | ||
::std::cout << "A * A - B * B = " << res_right << '\n'; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
set -ex | ||
|
||
bazel build //... |