diff --git a/docs/source/guide/sources_and_providers.rst b/docs/source/guide/sources_and_providers.rst index 3127ea5df..f195ae30c 100644 --- a/docs/source/guide/sources_and_providers.rst +++ b/docs/source/guide/sources_and_providers.rst @@ -26,12 +26,15 @@ A plugin with a ``PHLEX_REGISTER_PROVIDERS`` entry point (from auto const layer = config.get("layer"); m.provide("provide_i", [](phlex::data_cell_index const& id) -> int { return id.number(); }) - .output_product("input", "i", phlex::experimental::identifier{layer}); + .output_product("input", "i", phlex::experimental::layer_path{layer}); } ``output_product(creator, suffix, layer, stage)`` sets the identity of the product: algorithms select it with ``.creator = "input"`` and ``.suffix = "i"`` in that layer. +``layer`` must be a complete layer path such as ``/job/run/event``, that is, +it must start with ``/job``. +Otherwise registration throws. ``stage`` defaults to the job's stage. Sources and implicit providers diff --git a/form/form_source.cpp b/form/form_source.cpp index 2d4763121..369d1c0d5 100644 --- a/form/form_source.cpp +++ b/form/form_source.cpp @@ -108,7 +108,8 @@ namespace { algorithm_name::create(advertised_creator_), identifier{name}, selector.type}; // Read and provide products at the configured stage. - identifier const selector_layer = selector.layer; + // FIXME: This layer is always going to match + layer_path const selector_layer = selector.layer; identifier const product_stage{stage_}; if (!selector.match(spec, selector_layer, product_stage)) { @@ -123,12 +124,11 @@ namespace { return this->read_product_from_form(actual_creator_, name, id.to_string(), product_type); }; - bundles.push_back( - phlex::provider_bundle{.provider_function = provider_func, - .max_concurrency = phlex::concurrency::serial, - .spec = std::move(spec), - .layer = std::string(selector_layer.trans_get_string()), - .stage = stage_}); + bundles.push_back(phlex::provider_bundle{.provider_function = provider_func, + .max_concurrency = phlex::concurrency::serial, + .spec = std::move(spec), + .layer = selector_layer.to_string(), + .stage = stage_}); } return bundles; diff --git a/phlex/configuration.cpp b/phlex/configuration.cpp index be391140d..ee13618ce 100644 --- a/phlex/configuration.cpp +++ b/phlex/configuration.cpp @@ -31,6 +31,17 @@ namespace phlex::detail { auto const& val = obj.at(parameter); return boost::json::value_to(val); } + + std::optional string_view_if_exists( + boost::json::object const& obj, // will be used later for new product_selector + std::string_view parameter) + { + if (!obj.contains(parameter)) { + return std::nullopt; + } + auto const& val = obj.at(parameter); + return boost::json::value_to(val); + } } namespace phlex { @@ -52,6 +63,7 @@ namespace phlex { product_selector tag_invoke(boost::json::value_to_tag const&, boost::json::value const& jv) { + using detail::string_view_if_exists; using detail::value_if_exists; using namespace std::string_view_literals; static std::set const valid_keys = { @@ -68,11 +80,13 @@ namespace phlex { fmt::format("Product selector contains unrecognized keys: {}", unrecognized_keys)); } auto creator = value_if_exists(query_object, "creator"); - auto layer = value_if_exists(query_object, "layer"); + auto layer = string_view_if_exists(query_object, "layer"); auto suffix = value_if_exists(query_object, "suffix"); auto stage = value_if_exists(query_object, "stage"); - return product_selector{ - .creator = std::move(creator), .layer = std::move(layer), .suffix = suffix, .stage = stage}; + return product_selector{.creator = std::move(creator), + .layer = layer, + .suffix = std::move(suffix), + .stage = std::move(stage)}; } experimental::identifier experimental::tag_invoke(boost::json::value_to_tag const&, diff --git a/phlex/core/consumer.cpp b/phlex/core/consumer.cpp index 0e80d64c1..5760b61f3 100644 --- a/phlex/core/consumer.cpp +++ b/phlex/core/consumer.cpp @@ -3,7 +3,7 @@ #include "phlex/core/message.hpp" #include "phlex/core/product_selector.hpp" #include "phlex/model/algorithm_name.hpp" -#include "phlex/model/identifier.hpp" +#include "phlex/model/layer_path.hpp" #include "phlex/utilities/bulleted_list.hpp" #include @@ -11,22 +11,20 @@ #include #include +#include #include #include #include #include namespace { - std::vector layers_from(phlex::product_selectors const& queries) + std::vector layers_from(phlex::product_selectors const& queries) { - using namespace phlex::experimental::literals; - std::vector result; + std::vector result; result.reserve(queries.size()); for (auto const& query : queries) { if (query.layer) { - result.push_back(query.layer); - } else { - result.push_back("*"_id); + result.push_back(*query.layer); } } return result; @@ -55,6 +53,23 @@ namespace { throw std::runtime_error(error); } } + + void normalize_layer_selectors(phlex::product_selectors& selectors) + { + if (selectors.size() <= 1) { + return; + } + using phlex::experimental::layer_path; + auto const layers = selectors | std::views::transform([](auto const& selector) { + return static_cast(selector.layer); + }) | + std::ranges::to(); + for (auto const& [selector, layer] : std::views::zip(selectors, layers)) { + auto extensions = + layers | std::views::filter([&layer](auto const& other) { return other.ends_with(layer); }); + selector.layer = *std::ranges::max_element(extensions, {}, &layer_path::depth); + } + } } namespace phlex::detail { @@ -66,10 +81,11 @@ namespace phlex::detail { name_{std::move(name)}, predicates_{std::move(predicates)}, graph_{graph}, - input_products_{std::move(input_products)}, - layers_{layers_from(input_products_)} + input_products_{std::move(input_products)} { validate_layers(layers_required, input_products_, this->name()); + normalize_layer_selectors(input_products_); + layers_ = layers_from(input_products_); } consumer::~consumer() = default; @@ -87,9 +103,9 @@ namespace phlex::detail { auto& layer_check = layer_checkers_.emplace_back(std::make_unique( graph_, tbb::flow::unlimited, - [&layer = static_cast(input_product.layer)]( + [&layer = static_cast(input_product.layer)]( message const& msg, auto& output) { - if (msg.store->layer_name() == layer) { + if (msg.store->layer_path_ends_with(layer)) { std::get<0>(output).try_put(msg); } })); @@ -101,7 +117,7 @@ namespace phlex::detail { } product_selectors const& consumer::input() const noexcept { return input_products_; } - std::vector const& consumer::layers() const noexcept + std::vector const& consumer::layers() const noexcept { return layers_; } diff --git a/phlex/core/consumer.hpp b/phlex/core/consumer.hpp index 0694ea7d9..35593a268 100644 --- a/phlex/core/consumer.hpp +++ b/phlex/core/consumer.hpp @@ -7,6 +7,7 @@ #include "phlex/core/product_selector.hpp" #include "phlex/model/algorithm_name.hpp" #include "phlex/model/identifier.hpp" +#include "phlex/model/layer_path.hpp" #include "phlex/phlex_core_export.hpp" #include @@ -34,7 +35,7 @@ namespace phlex::detail { std::vector const& when() const noexcept; product_selectors const& input() const noexcept; - std::vector const& layers() const noexcept; + std::vector const& layers() const noexcept; tbb::flow::receiver& port(product_selector const& input_product); virtual named_index_ports index_ports() = 0; @@ -53,7 +54,7 @@ namespace phlex::detail { std::vector predicates_; std::reference_wrapper graph_; product_selectors input_products_; - std::vector layers_; + std::vector layers_; std::vector> layer_checkers_; }; } diff --git a/phlex/core/declared_fold.cpp b/phlex/core/declared_fold.cpp index b27ec537f..e07b7ad46 100644 --- a/phlex/core/declared_fold.cpp +++ b/phlex/core/declared_fold.cpp @@ -15,7 +15,7 @@ namespace phlex::detail { std::vector predicates, product_selectors input_products, tbb::flow::graph& graph, - std::string partition_layer) : + phlex::experimental::layer_path partition_layer) : consumer{std::move(name), std::move(predicates), std::move(input_products), diff --git a/phlex/core/declared_fold.hpp b/phlex/core/declared_fold.hpp index efe45ceb4..bef424061 100644 --- a/phlex/core/declared_fold.hpp +++ b/phlex/core/declared_fold.hpp @@ -7,6 +7,7 @@ #include "phlex/core/product_selector.hpp" #include "phlex/model/algorithm_name.hpp" #include "phlex/model/identifier.hpp" +#include "phlex/model/layer_path.hpp" #include "phlex/phlex_core_export.hpp" #include "phlex/utilities/simple_ptr_map.hpp" @@ -23,7 +24,7 @@ namespace phlex::detail { std::vector predicates, product_selectors input_products, tbb::flow::graph& graph, - std::string partition_layer); + phlex::experimental::layer_path partition_layer); ~declared_fold() override; phlex::experimental::algorithm_name const& name() const noexcept override @@ -32,10 +33,10 @@ namespace phlex::detail { } virtual tbb::flow::receiver& partition_port() = 0; - phlex::experimental::identifier const& partition_layer() const { return partition_layer_; } + phlex::experimental::layer_path const& partition_layer() const { return partition_layer_; } private: - phlex::experimental::identifier partition_layer_; + phlex::experimental::layer_path partition_layer_; }; using declared_fold_ptr = std::unique_ptr; diff --git a/phlex/core/detail/accumulator_node.hpp b/phlex/core/detail/accumulator_node.hpp index 8a0d2b148..8679ad857 100644 --- a/phlex/core/detail/accumulator_node.hpp +++ b/phlex/core/detail/accumulator_node.hpp @@ -102,7 +102,7 @@ namespace phlex::detail::internal { accumulator_node(tbb::flow::graph& g, phlex::experimental::algorithm_name node_name, phlex::experimental::identifier stage, - phlex::experimental::identifier partition_layer_name, + phlex::experimental::layer_path partition_layer_name, phlex::experimental::product_specifications output, result_initializer_t initializer); @@ -156,7 +156,7 @@ namespace phlex::detail::internal { cache_t cached_results_; phlex::experimental::algorithm_name node_name_; phlex::experimental::identifier stage_; - phlex::experimental::identifier partition_layer_; + phlex::experimental::layer_path partition_layer_; result_initializer_t initializer_; phlex::experimental::product_specifications output_; std::atomic emitted_result_count_{0}; @@ -169,7 +169,7 @@ namespace phlex::detail::internal { accumulator_node::accumulator_node(tbb::flow::graph& g, phlex::experimental::algorithm_name node_name, phlex::experimental::identifier stage, - phlex::experimental::identifier partition_layer_name, + phlex::experimental::layer_path partition_layer_name, phlex::experimental::product_specifications output, result_initializer_t initializer) : base_t{g}, diff --git a/phlex/core/detail/repeater_node.cpp b/phlex/core/detail/repeater_node.cpp index 879746387..1e54cddc9 100644 --- a/phlex/core/detail/repeater_node.cpp +++ b/phlex/core/detail/repeater_node.cpp @@ -2,7 +2,7 @@ #include "phlex/core/message.hpp" #include "phlex/model/algorithm_name.hpp" -#include "phlex/model/identifier.hpp" +#include "phlex/model/layer_path.hpp" #include "phlex/utilities/signed_size.hpp" #include @@ -19,7 +19,7 @@ namespace phlex::detail::internal { repeater_node::repeater_node(tbb::flow::graph& g, phlex::experimental::algorithm_name node_name, - phlex::experimental::identifier layer_name) : + phlex::experimental::layer_path layer_name) : base_t{g}, indexer_{g}, repeater_{g, diff --git a/phlex/core/detail/repeater_node.hpp b/phlex/core/detail/repeater_node.hpp index 83e42f4f2..4d6766fb9 100644 --- a/phlex/core/detail/repeater_node.hpp +++ b/phlex/core/detail/repeater_node.hpp @@ -23,7 +23,7 @@ namespace phlex::detail::internal { public: repeater_node(tbb::flow::graph& g, phlex::experimental::algorithm_name node_name, - phlex::experimental::identifier layer_name); + phlex::experimental::layer_path layer_name); tbb::flow::receiver& data_port(); tbb::flow::receiver& flush_port(); @@ -69,7 +69,7 @@ namespace phlex::detail::internal { cache_t cached_products_; std::atomic index_cache_mode_{cache_mode::unset}; phlex::experimental::algorithm_name node_name_; - phlex::experimental::identifier layer_; + phlex::experimental::layer_path layer_; }; } diff --git a/phlex/core/fold_join_node.hpp b/phlex/core/fold_join_node.hpp index cbb39d9f0..b85993c5a 100644 --- a/phlex/core/fold_join_node.hpp +++ b/phlex/core/fold_join_node.hpp @@ -77,8 +77,8 @@ namespace phlex::detail { fold_join_node(tbb::flow::graph& g, phlex::experimental::algorithm_name const& node_name, phlex::experimental::identifier const& stage, - phlex::experimental::identifier const& partition_layer_name, - std::vector layer_names, + phlex::experimental::layer_path const& partition_layer_name, + std::vector layer_names, phlex::experimental::product_specifications output, result_initializer_t result_initializer) : base_t{g}, @@ -132,27 +132,22 @@ namespace phlex::detail { // // The first entry is the *partition slot* — it routes the partition index to the // accumulator's index_port and delivers the partition-level flush token to the - // accumulator's flush_port. Its routing layer is the partition layer (e.g. "job"), - // but its counting layer is currently the first input data layer. + // accumulator's flush_port. Its routing layer is the partition layer (e.g. "job"). // - // The remaining entries are the data-input slots, one per repeater. For these the - // routing layer and counting layer are the same: the repeater both routes and counts - // at its own input layer. + // The remaining entries are the data-input slots, one per repeater. // - // TODO: For multi-input folds, the counting layer must be the deepest input layer - // in the data hierarchy. Until the router exposes a depth comparison, we pick - // layers_[0]; this is only correct when the first input is at the deepest layer. + // No slot sets a counting layer, so the router counts at the node's deepest input layer. std::vector index_ports() { std::vector result; result.reserve(1 + repeaters_.size()); // +1 for the result repeater result.emplace_back(partition_layer_, - layers_[0], + std::nullopt, &result_repeater_.flush_port(), &result_repeater_.index_port()); for (std::size_t i = 0; i != repeaters_.size(); ++i) { result.emplace_back( - layers_[i], layers_[i], &repeaters_[i]->flush_port(), &repeaters_[i]->index_port()); + layers_[i], std::nullopt, &repeaters_[i]->flush_port(), &repeaters_[i]->index_port()); } return result; } @@ -164,8 +159,8 @@ namespace phlex::detail { // Immutable after construction; tbb::flow::join_node is already non-movable. // NOLINTBEGIN(cppcoreguidelines-avoid-const-or-ref-data-members) phlex::experimental::algorithm_name const name_; - phlex::experimental::identifier const partition_layer_; - std::vector const layers_; + phlex::experimental::layer_path const partition_layer_; + std::vector const layers_; // NOLINTEND(cppcoreguidelines-avoid-const-or-ref-data-members) }; diff --git a/phlex/core/fold_node.hpp b/phlex/core/fold_node.hpp index f1498f249..d09d37d35 100644 --- a/phlex/core/fold_node.hpp +++ b/phlex/core/fold_node.hpp @@ -85,7 +85,7 @@ namespace phlex::detail { InitTuple initializer, product_selectors input_products, std::vector output, - std::string partition_layer, + phlex::experimental::layer_path partition_layer, resource_catalog& resources) : declared_fold{std::move(algo_name), std::move(predicates), diff --git a/phlex/core/framework_graph.cpp b/phlex/core/framework_graph.cpp index 1fd80f9ed..d0da2819b 100644 --- a/phlex/core/framework_graph.cpp +++ b/phlex/core/framework_graph.cpp @@ -73,12 +73,15 @@ namespace phlex::detail { result.reserve(unfolds.size()); using phlex::experimental::identifier; + using phlex::experimental::layer_path; for (auto const& [name, n] : unfolds) { index_router::unfold_layer_spec spec{ .name = name, .input_layers = - std::views::transform( - n->input(), [](auto const& input) -> identifier const& { return input.layer; }) | + std::views::transform(n->input(), + [](auto const& input) -> layer_path const& { + return static_cast(input.layer); + }) | std::ranges::to(), .output_layer = identifier{n->child_layer()}}; result.push_back(std::move(spec)); diff --git a/phlex/core/framework_graph.hpp b/phlex/core/framework_graph.hpp index 698d0d218..c358fa3ce 100644 --- a/phlex/core/framework_graph.hpp +++ b/phlex/core/framework_graph.hpp @@ -99,7 +99,7 @@ namespace phlex::detail { auto fold(std::string_view name, is_fold_like auto f, concurrency c = concurrency::serial, - std::string partition = "job", + phlex::experimental::layer_path partition = "job", InitArgs&&... init_args) { return make_glue().fold( diff --git a/phlex/core/glue.hpp b/phlex/core/glue.hpp index ae63b33b8..fe0dfdbef 100644 --- a/phlex/core/glue.hpp +++ b/phlex/core/glue.hpp @@ -70,7 +70,7 @@ namespace phlex::detail { auto fold(std::string_view name, auto f, // NOLINT(performance-unnecessary-value-param) concurrency c, - std::string partition, + phlex::experimental::layer_path partition, InitArgs&&... init_args) { internal::verify_name(name, config_); diff --git a/phlex/core/graph_proxy.hpp b/phlex/core/graph_proxy.hpp index 56794d59f..db28c044f 100644 --- a/phlex/core/graph_proxy.hpp +++ b/phlex/core/graph_proxy.hpp @@ -74,7 +74,7 @@ namespace phlex::detail { auto fold(std::string_view name, is_fold_like auto f, // NOLINT(performance-unnecessary-value-param) concurrency c = concurrency::serial, - std::string partition = "job", + phlex::experimental::layer_path partition = "job", InitArgs&&... init_args) const; /// @brief Registers an observer node. @@ -194,7 +194,7 @@ namespace phlex::detail { auto graph_proxy::fold(std::string_view name, is_fold_like auto f, concurrency c, - std::string partition, + phlex::experimental::layer_path partition, InitArgs&&... init_args) const { return create_glue().fold( diff --git a/phlex/core/index_router.cpp b/phlex/core/index_router.cpp index 623b29801..de1eaa352 100644 --- a/phlex/core/index_router.cpp +++ b/phlex/core/index_router.cpp @@ -53,7 +53,7 @@ namespace phlex::detail { return paths; } - bool matches_input_chain(layer_path const& path, std::vector const& input_layers) + bool matches_input_chain(layer_path const& path, std::vector const& input_layers) { Expects(not input_layers.empty()); @@ -63,9 +63,11 @@ namespace phlex::detail { return false; } - // Check that all input layers are present in the path. - return std::ranges::all_of(input_layers, - [&path](auto const& input) { return path.contains(input); }); + // Check that all input layers are present in the path. An input need not start at the root, + // so it must match anywhere along the path rather than as a prefix. + return std::ranges::all_of(input_layers, [&path](auto const& input) { + return not std::ranges::search(path.components(), input.components()).empty(); + }); } } @@ -86,7 +88,7 @@ namespace phlex::detail { class multilayer_slot { public: multilayer_slot(tbb::flow::graph& g, - identifier layer, + layer_path layer, tbb::flow::receiver* input_port); void put_message(data_cell_index_ptr const& index, std::size_t message_id); @@ -94,15 +96,15 @@ namespace phlex::detail { bool matches_exactly(layer_path const& path) const; bool is_parent_of(data_cell_index_ptr const& index) const; - identifier const& layer() const { return layer_; } + layer_path const& layer() const { return layer_; } private: - identifier layer_; + layer_path layer_; index_set_node broadcaster_; }; multilayer_slot::multilayer_slot(tbb::flow::graph& g, - identifier layer, + layer_path layer, tbb::flow::receiver* input_port) : layer_{std::move(layer)}, broadcaster_{g} { @@ -111,7 +113,7 @@ namespace phlex::detail { void multilayer_slot::put_message(data_cell_index_ptr const& index, std::size_t message_id) { - if (layer_ == index->layer_name()) { + if (index->layer_path_ends_with(layer_)) { broadcaster_.try_put({.index = index, .msg_id = message_id, .cache = false}); return; } @@ -157,7 +159,6 @@ namespace phlex::detail { fold_partition_ports_t fold_partition_ports, std::map const& multilayer_join_ports) { - using namespace phlex::experimental::literals; if (layer_paths_from_driver.empty()) { throw std::runtime_error("Cannot finalize index_router with an empty driver hierarchy."); } @@ -258,13 +259,26 @@ namespace phlex::detail { } } + void index_router::connect_index_set(tbb::flow::graph& g, + layer_path const& target_layer, + tbb::flow::receiver& port) + { + for (auto const& path : sorted_layer_paths_) { + if (path.ends_with(target_layer)) { + auto [it, _] = + index_set_nodes_.emplace(path.hash(), std::make_shared(g)); + make_edge(*it->second, port); + } + } + } + void index_router::wire_provider_index_sets(tbb::flow::graph& g, provider_input_ports_t provider_input_ports) { for (auto& [input_product, provider_port] : provider_input_ports | std::views::values) { - auto [it, _] = index_set_nodes_.emplace(input_product.layer ? *input_product.layer : "*"_id, - std::make_shared(g)); - make_edge(*it->second, *provider_port); + if (input_product.layer) { + connect_index_set(g, *input_product.layer, *provider_port); + } } } @@ -272,8 +286,7 @@ namespace phlex::detail { fold_partition_ports_t fold_partition_ports) { for (auto& [layer, port] : fold_partition_ports | std::views::values) { - auto [it, _] = index_set_nodes_.emplace(layer, std::make_shared(g)); - make_edge(*it->second, *port); + connect_index_set(g, layer, *port); } } @@ -285,18 +298,27 @@ namespace phlex::detail { for (auto const& [node_name, join_ports] : multilayer_join_ports) { spdlog::trace("Making multilayer slots for {}", node_name); - std::vector slot_layers; + std::vector slot_layers; slot_layers.reserve(join_ports.size()); for (auto const& port : join_ports) { slot_layers.push_back(port.layer); } - identifier const node_deepest_layer = deepest_layer_name(slot_layers); + if (std::ranges::none_of(sorted_layer_paths_, [&slot_layers](auto const& path) { + return matches_input_chain(path, slot_layers); + })) { + throw std::runtime_error( + fmt::format("The input layers of node '{}' do not all lie on one path through the " + "data-layer hierarchy:\n{}", + node_name, + bulleted_list(slot_layers))); + } + layer_path const node_deepest_layer = deepest_layer_name(slot_layers); internal::join_node_slots node_slots; node_slots.slots.reserve(join_ports.size()); node_slots.flush_specs.reserve(join_ports.size()); for (auto const& [layer, counting_layer, flush_port, input_port] : join_ports) { - identifier const effective_counting_layer = counting_layer.value_or(node_deepest_layer); + layer_path const effective_counting_layer = counting_layer.value_or(node_deepest_layer); node_slots.slots.push_back( std::make_shared(g, layer, input_port)); node_slots.flush_specs.push_back( @@ -317,8 +339,8 @@ namespace phlex::detail { bool const is_lowest_layer, std::size_t const message_id) { - if (auto index_set_node = index_set_node_for(index)) { - index_set_node->try_put({.index = index, .msg_id = message_id}); + if (auto it = index_set_nodes_.find(index->layer_hash()); it != index_set_nodes_.end()) { + it->second->try_put({.index = index, .msg_id = message_id}); } auto [message_slots, end_token_entries] = multilayer_slots_for(index); @@ -361,44 +383,6 @@ namespace phlex::detail { "Index layer path '{}' is not in the established layer hierarchy.", index->layer_path())); } - internal::index_set_node_ptr index_router::index_set_node_for(data_cell_index_ptr const& index) - { - auto const layer_hash = index->layer_hash(); - if (auto it = index_set_node_cache_.find(layer_hash); it != index_set_node_cache_.end()) { - return it->second; - } - - layer_path const layerish_path{{index->layer_name()}}; - auto broadcaster = index_set_node_for(layerish_path); - index_set_node_cache_.insert({layer_hash, broadcaster}); - return broadcaster; - } - - auto index_router::index_set_node_for(layer_path const& layer_path) - -> internal::index_set_node_ptr - { - std::vector candidates; - for (auto it = index_set_nodes_.begin(), e = index_set_nodes_.end(); it != e; ++it) { - if (layer_path.ends_with(it->first)) { - candidates.push_back(it); - } - } - - if (candidates.size() == 1uz) { - return candidates[0]->second; - } - - if (candidates.empty()) { - return nullptr; - } - - std::string const msg = fmt::format( - "Multiple layers match specification {}:\n{}", - layer_path, - bulleted_list(candidates | std::views::transform([](auto const& it) { return it->first; }))); - throw std::runtime_error(msg); - } - std::pair index_router::multilayer_slots_for(data_cell_index_ptr const& index) { @@ -488,7 +472,7 @@ namespace phlex::detail { } std::vector index_router::counting_layer_hashes_under( - layer_path const& partition_layer_path, identifier const& counting_layer_name) const + layer_path const& partition_layer_path, layer_path const& counting_layer_name) const { std::vector result; for (auto const& candidate : sorted_layer_paths_) { @@ -504,12 +488,14 @@ namespace phlex::detail { return result; } - identifier index_router::deepest_layer_name(std::vector const& layer_names) const + layer_path index_router::deepest_layer_name(std::vector const& layer_names) const { - auto depth_of = [this](identifier const& name) -> std::size_t { + // Only paths containing every layer are compared, so an unrelated deeper path cannot make a + // parent layer look deeper than its child. + auto depth_of = [this, &layer_names](layer_path const& name) -> std::size_t { std::size_t best = 0; for (auto const& path : sorted_layer_paths_) { - if (path.ends_with(name)) { + if (path.ends_with(name) and matches_input_chain(path, layer_names)) { best = std::max(path.depth(), best); } } diff --git a/phlex/core/index_router.hpp b/phlex/core/index_router.hpp index 0cb2d6ede..3dedcd18d 100644 --- a/phlex/core/index_router.hpp +++ b/phlex/core/index_router.hpp @@ -7,6 +7,7 @@ #include "phlex/model/flush_gate.hpp" #include "phlex/model/flush_messages.hpp" #include "phlex/model/identifier.hpp" +#include "phlex/model/layer_path.hpp" #include "phlex/phlex_core_export.hpp" #include @@ -38,7 +39,7 @@ namespace phlex::detail { // Descendants ending in the counting layer supply one combined completion count // forwarded as an indexed_end_token to flush_port. struct flush_spec { - phlex::experimental::identifier counting_layer; + phlex::experimental::layer_path counting_layer; tbb::flow::receiver* flush_port; }; @@ -77,7 +78,7 @@ namespace phlex::detail { using provider_input_ports_t = std::map; struct fold_input_port_t { - phlex::experimental::identifier layer; + phlex::experimental::layer_path layer; tbb::flow::receiver* partition_port; }; using fold_partition_ports_t = std::map; @@ -87,7 +88,7 @@ namespace phlex::detail { // This metadata lets the router discover generated paths before any runtime indices arrive. struct unfold_layer_spec { std::string name; - std::vector input_layers; + std::vector input_layers; phlex::experimental::identifier output_layer; }; @@ -134,11 +135,13 @@ namespace phlex::detail { std::vector const& layer_paths_from_driver, unfold_data const& unfolds); void wire_provider_index_sets(tbb::flow::graph& g, provider_input_ports_t provider_input_ports); + void connect_index_set(tbb::flow::graph& g, + phlex::experimental::layer_path const& target_layer, + tbb::flow::receiver& port); void wire_fold_partition_index_sets(tbb::flow::graph& g, fold_partition_ports_t fold_partition_ports); void build_multilayer_join_slots( tbb::flow::graph& g, std::map const& multilayer_join_ports); - internal::index_set_node_ptr index_set_node_for(phlex::experimental::layer_path const& layer); internal::index_set_node_ptr index_set_node_for(data_cell_index_ptr const& index); std::pair multilayer_slots_for( data_cell_index_ptr const& index); @@ -159,10 +162,10 @@ namespace phlex::detail { // Resolve descendant paths ending in the counting layer beneath this partition. std::vector counting_layer_hashes_under( phlex::experimental::layer_path const& partition_layer_path, - phlex::experimental::identifier const& counting_layer_name) const; + phlex::experimental::layer_path const& counting_layer_name) const; - phlex::experimental::identifier deepest_layer_name( - std::vector const& layer_names) const; + phlex::experimental::layer_path deepest_layer_name( + std::vector const& layer_names) const; tbb::flow::function_node unfold_index_receiver_; tbb::flow::function_node unfold_flush_receiver_; @@ -175,13 +178,9 @@ namespace phlex::detail { // ========================================================================================== // Routing to provider nodes - // The following maps are used to route data-cell indices to provider nodes. - // The first map is from layer name to the corresponding index-set node. - tbb::concurrent_unordered_map - index_set_nodes_; - // The second map is a cache from a layer hash to an index-set node, to avoid - // repeated lookups for the same layer. - tbb::concurrent_unordered_map index_set_node_cache_; + // The following map is used to route data-cell indices to provider nodes. + // It is from a layer path hash to the corresponding index-set node. + tbb::concurrent_unordered_map index_set_nodes_; // ========================================================================================== // Routing to multi-layer join nodes diff --git a/phlex/core/make_computational_edges.cpp b/phlex/core/make_computational_edges.cpp index 9b2f6b5bc..17e8b958c 100644 --- a/phlex/core/make_computational_edges.cpp +++ b/phlex/core/make_computational_edges.cpp @@ -32,9 +32,10 @@ using namespace std::string_literals; namespace phlex::detail { namespace { - provider_node* find_matching_provider(provider_nodes& providers, - product_selector const& input_product, - phlex::experimental::identifier const& stage) + std::vector find_matching_providers( + provider_nodes& providers, + product_selector const& input_product, + phlex::experimental::identifier const& stage) { auto query = input_product; if (query.stage == "CURRENT"_idq) { @@ -47,12 +48,15 @@ namespace phlex::detail { auto candidates = providers | std::views::transform(proj) | std::views::filter(pred) | std::ranges::to(); - switch (candidates.size()) { - case 0: - return nullptr; - case 1: - return candidates[0]; - default: + // Providers of one product at one stage may coexist if they serve different layer paths. + auto const conflict = [](provider_node const* a, provider_node const* b) { + return a != b and (a->layer() == b->layer() or a->output_product() != b->output_product() or + a->stage() != b->stage()); + }; + bool const ambiguous = std::ranges::any_of(candidates, [&](auto const* a) { + return std::ranges::any_of(candidates, [&](auto const* b) { return conflict(a, b); }); + }); + if (ambiguous) { auto items = candidates | std::views::transform([](provider_node* p) { return fmt::format("spec: {}, layer: {}, stage: {}", p->output_product().to_string(), @@ -63,6 +67,7 @@ namespace phlex::detail { input_product.to_string(), bulleted_list(items))); } + return candidates; } provider_bundles find_matching_implicit_providers(source_map const& sources, @@ -99,21 +104,21 @@ namespace phlex::detail { for (auto const& [input_product, port] : ports) { // Find the provider that has the right product name (hidden in the // output port) and the right family (hidden in the input port). - if (auto* matched_provider = - find_matching_provider(explicit_providers, input_product, stage)) { + auto const matched_providers = + find_matching_providers(explicit_providers, input_product, stage); + for (auto* matched_provider : matched_providers) { auto const provider_name = matched_provider->name().to_string(); auto&& [it, _] = provider_input_ports.try_emplace( provider_name, input_product, matched_provider->input_port()); - // Rewrite the stored layer if it's empty - if (!input_product.layer) { - it->second.input_product.layer = matched_provider->layer(); - } + // Index routing follows the provider's layer, not the selector's. + it->second.input_product.layer = matched_provider->layer(); spdlog::debug("Connecting provider {} to node {} (product: {})", provider_name, node_name, input_product.to_string()); make_edge(matched_provider->output_port(), *port); - } else { + } + if (matched_providers.empty()) { unconsumed_head_ports[node_name].push_back( {.input_product = input_product, .port = port}); } @@ -183,10 +188,8 @@ namespace phlex::detail { auto it = provider_input_ports.try_emplace(provider_name, input_product, node->input_port()) .first; - // Rewrite the stored layer if it's empty - if (!input_product.layer) { - it->second.input_product.layer = node->layer(); - } + // Index routing follows the provider's layer, not the selector's. + it->second.input_product.layer = node->layer(); make_edge(node->output_port(), *port); providers.try_emplace(provider_name, std::move(node)); } diff --git a/phlex/core/message.hpp b/phlex/core/message.hpp index 9a1bdbb91..36544b405 100644 --- a/phlex/core/message.hpp +++ b/phlex/core/message.hpp @@ -6,6 +6,7 @@ #include "phlex/model/fwd.hpp" #include "phlex/model/handle.hpp" #include "phlex/model/identifier.hpp" +#include "phlex/model/layer_path.hpp" #include "phlex/model/product_store.hpp" #include "phlex/phlex_core_export.hpp" #include "phlex/utilities/signed_size.hpp" @@ -60,15 +61,15 @@ namespace phlex::detail { // // - `layer` — the *routing* layer. The router decides whether a routed index // feeds this slot's `index_port` (and whether the slot should - // receive a flush token) using this layer name via + // receive a flush token) using this layer path via // `matches_exactly` / `is_parent_of` checks. // // - `counting_layer` — the *counting* layer preference. `std::nullopt` selects the // node's deepest layer, while a populated value selects that - // explicit layer name. + // explicit layer path. struct named_index_port { - phlex::experimental::identifier layer; - std::optional counting_layer; + phlex::experimental::layer_path layer; + std::optional counting_layer; tbb::flow::receiver* token_port; tbb::flow::receiver* index_port; }; diff --git a/phlex/core/multilayer_join_node.hpp b/phlex/core/multilayer_join_node.hpp index 7666cbdec..f49c6b8f9 100644 --- a/phlex/core/multilayer_join_node.hpp +++ b/phlex/core/multilayer_join_node.hpp @@ -70,7 +70,7 @@ namespace phlex::detail { public: multilayer_join_node(tbb::flow::graph& g, std::string node_name, - std::vector layer_names) : + std::vector layer_names) : base_t{g}, join_{make_join(g, std::make_index_sequence{})}, name_{std::move(node_name)}, @@ -126,7 +126,7 @@ namespace phlex::detail { // Immutable after construction; tbb::flow::join_node is already non-movable. // NOLINTBEGIN(cppcoreguidelines-avoid-const-or-ref-data-members) std::string const name_; - std::vector const layers_; + std::vector const layers_; // NOLINTEND(cppcoreguidelines-avoid-const-or-ref-data-members) }; @@ -158,7 +158,7 @@ namespace phlex::detail { template join_or_none_t make_join_or_none(tbb::flow::graph& g, std::string const& node_name, - std::vector const& layers) + std::vector const& layers) { if constexpr (N > 1ull) { return multilayer_join_node{g, node_name, layers}; diff --git a/phlex/core/product_selector.cpp b/phlex/core/product_selector.cpp index 87515d8d7..1d1b6b5a0 100644 --- a/phlex/core/product_selector.cpp +++ b/phlex/core/product_selector.cpp @@ -19,7 +19,7 @@ namespace phlex { if (creator && creator != other.creator) { return false; } - if (layer && layer != other.layer) { + if (layer && (!other.layer || !other.layer->ends_with(layer))) { return false; } if (suffix && suffix != other.suffix) { @@ -54,13 +54,13 @@ namespace phlex { // Check if a product_specification, layer, and stage together satisfies this query bool product_selector::match(experimental::product_specification const& spec, - experimental::identifier const& layer, + experimental::layer_path const& layer, experimental::identifier const& stage) const { if (!match(spec)) { return false; } - if (this->layer && experimental::identifier(this->layer) != layer) { + if (this->layer && !layer.ends_with(this->layer)) { return false; } if (this->stage && this->stage != stage) { @@ -87,7 +87,7 @@ namespace phlex { suffix.transform(&identifier::operator std::string_view).value_or("[ANY]"); std::string type_str = this->type.valid() ? fmt::format("<{}>", this->type) : "[UNSET TYPE]"; // will later be concept - auto layer_str = std::string_view(layer); + auto layer_str = layer ? layer->to_string() : "[ANY]"; std::string_view creator_str = creator ? std::string_view(*creator) : "[ANY]"; std::string_view stage_str = stage.transform(&identifier::operator std::string_view).value_or("[ANY]"); @@ -118,7 +118,7 @@ namespace phlex { { // Protect against layer mismatch if (query.layer) { - if (store.index()->layer_name() != *query.layer) { + if (!store.layer_path_ends_with(query.layer)) { return nullptr; } } diff --git a/phlex/core/product_selector.hpp b/phlex/core/product_selector.hpp index 00a6e8cab..b4d8e56c1 100644 --- a/phlex/core/product_selector.hpp +++ b/phlex/core/product_selector.hpp @@ -2,6 +2,7 @@ #define PHLEX_CORE_PRODUCT_SELECTOR_HPP #include "phlex/model/identifier.hpp" +#include "phlex/model/layer_path.hpp" #include "phlex/model/product_specification.hpp" #include "phlex/model/product_store.hpp" #include "phlex/model/type_id.hpp" @@ -52,27 +53,19 @@ namespace phlex { std::optional content_; }; - class layer_name { + class layer_selector { public: - layer_name() = default; - layer_name(std::optional&& content) : content_{std::move(content)} - { - if (content_ && content_.value().empty()) { - throw std::runtime_error("Cannot specify the empty string as a data layer."); - } - } + layer_selector() = default; + layer_selector(std::optional const& content) : content_{content} {} template - requires std::constructible_from + requires std::constructible_from // NOLINTNEXTLINE(google-explicit-constructor) - Implicit conversion is intentional - layer_name(U&& rhs) : content_(std::forward(rhs)) + layer_selector(U&& rhs) : content_(std::forward(rhs)) { - if (content_.value().empty()) { - throw std::runtime_error("Cannot specify the empty string as a data layer."); - } } // NOLINTNEXTLINE(google-explicit-constructor) - Implicit conversion is intentional - operator experimental::identifier const&() const + operator experimental::layer_path const&() const { if (!content_.has_value()) { throw std::logic_error("Cannot retrieve layer from product_selector with no layer"); @@ -80,19 +73,18 @@ namespace phlex { return *content_; } - experimental::identifier const& operator*() const noexcept { return content_.operator*(); } - explicit operator std::string_view() const noexcept + experimental::layer_path const& operator*() const noexcept { return content_.operator*(); } + experimental::layer_path const* operator->() const noexcept { return content_.operator->(); } + explicit operator std::string() const noexcept { - using namespace std::string_view_literals; - return content_ - .transform([](experimental::identifier const& id) { return std::string_view(id); }) - .value_or("[ANY]"sv); + using namespace std::string_literals; + return content_ ? content_->to_string() : "[ANY]"s; } operator bool() const noexcept { return content_.has_value(); } - auto operator<=>(layer_name const&) const noexcept = default; + auto operator<=>(layer_selector const&) const noexcept = default; private: - std::optional content_; + std::optional content_; }; } @@ -107,7 +99,7 @@ namespace phlex { detail::creator_name creator; /// Data layer of the product. Required for every input of multi-input algorithms, /// folds and unfolds; products from other layers are not delivered. - detail::layer_name layer; + detail::layer_selector layer; /// Product suffix, as given to @c output_product_suffixes(...). std::optional suffix; /// Stage that created the product; @c "CURRENT" means the job's own stage. @@ -123,7 +115,7 @@ namespace phlex { // Check if a product_specification, layer, and stage together satisfies this query bool match(experimental::product_specification const& spec, - experimental::identifier const& layer, + experimental::layer_path const& layer, experimental::identifier const& stage) const; // Check if an algorithm name matches this query's creator diff --git a/phlex/core/provider_node.cpp b/phlex/core/provider_node.cpp index 77e11797f..e6991b728 100644 --- a/phlex/core/provider_node.cpp +++ b/phlex/core/provider_node.cpp @@ -30,7 +30,7 @@ namespace phlex::detail { tbb::flow::graph& g, phlex::experimental::provider_function provider_func, phlex::experimental::product_specification output_spec, - phlex::experimental::identifier output_layer, + phlex::experimental::layer_path output_layer, phlex::experimental::identifier stage) : name_{std::move(algo_name)}, output_{std::move(output_spec)}, @@ -68,7 +68,7 @@ namespace phlex::detail { return output_; } - phlex::experimental::identifier const& provider_node::layer() const noexcept { return layer_; } + phlex::experimental::layer_path const& provider_node::layer() const noexcept { return layer_; } phlex::experimental::identifier const& provider_node::stage() const noexcept { return stage_; } diff --git a/phlex/core/provider_node.hpp b/phlex/core/provider_node.hpp index 150da3706..478fae816 100644 --- a/phlex/core/provider_node.hpp +++ b/phlex/core/provider_node.hpp @@ -51,12 +51,12 @@ namespace phlex::detail { tbb::flow::graph& g, phlex::experimental::provider_function provider_func, phlex::experimental::product_specification output_spec, - phlex::experimental::identifier output_layer, + phlex::experimental::layer_path output_layer, phlex::experimental::identifier stage); phlex::experimental::algorithm_name const& name() const noexcept; phlex::experimental::product_specification const& output_product() const noexcept; - phlex::experimental::identifier const& layer() const noexcept; + phlex::experimental::layer_path const& layer() const noexcept; phlex::experimental::identifier const& stage() const noexcept; tbb::flow::receiver* input_port() { return &provider_; } @@ -66,7 +66,7 @@ namespace phlex::detail { private: phlex::experimental::algorithm_name name_; phlex::experimental::product_specification output_; - phlex::experimental::identifier layer_; + phlex::experimental::layer_path layer_; phlex::experimental::identifier stage_; tbb::flow::function_node provider_; std::atomic calls_; diff --git a/phlex/core/registration_api.hpp b/phlex/core/registration_api.hpp index c89e4865a..35564acc7 100644 --- a/phlex/core/registration_api.hpp +++ b/phlex/core/registration_api.hpp @@ -15,13 +15,17 @@ #include "phlex/metaprogramming/delegate.hpp" #include "phlex/metaprogramming/type_deduction.hpp" #include "phlex/model/algorithm_name.hpp" +#include "phlex/model/layer_path.hpp" #include "phlex/phlex_core_export.hpp" +#include + #include #include #include #include #include +#include #include #include #include @@ -201,13 +205,26 @@ namespace phlex::detail { { } + /// @brief Registers the explicit provider's output product. + /// + /// @param output_layer Complete path of the data layer of the product, for example + /// @c "/job/run/event". Throws @c std::runtime_error if the path is not complete + /// (does not start with @c "/job"). auto output_product(phlex::experimental::algorithm_name creator, phlex::experimental::identifier suffix, - phlex::experimental::identifier output_layer, + phlex::experimental::layer_path output_layer, phlex::experimental::identifier stage = "CURRENT"_id) { using namespace phlex::experimental; + if (!output_layer.is_complete()) { + throw std::runtime_error(fmt::format( + "The output layer of provider '{}' must be a complete layer path (starting with " + "'/job'), but is '{}'.", + name_.to_string(), + output_layer.to_string())); + } + // "CURRENT" is special and means "use the stage configured for this program execution." if (stage == "CURRENT"_id) { stage = stage_; @@ -276,7 +293,7 @@ namespace phlex::detail { node_catalog& nodes, std::vector& errors, resource_catalog& resources, - std::string partition, + phlex::experimental::layer_path partition, InitArgs&&... init_args) : config_{config}, name_{phlex::experimental::internal::make_algorithm_name(config, name)}, @@ -360,7 +377,7 @@ namespace phlex::detail { concurrency concurrency_; // Non-owning reference to the TBB graph; this class is a short-lived registration builder. tbb::flow::graph& graph_; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) - std::string partition_; + phlex::experimental::layer_path partition_; init_tuple init_; registrar registrar_; resource_catalog& resources_; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) diff --git a/phlex/model/data_cell_index.cpp b/phlex/model/data_cell_index.cpp index 3dcc83332..2b4607665 100644 --- a/phlex/model/data_cell_index.cpp +++ b/phlex/model/data_cell_index.cpp @@ -76,6 +76,24 @@ namespace phlex { return experimental::layer_path{std::move(layers)}; } + bool data_cell_index::layer_path_ends_with( + experimental::layer_path const& layer_path_suffix) const noexcept + { + if (layer_path_suffix.depth() > depth_ + 1) { + // Can't end with a suffix longer than our path + return false; + } + auto const* ptr = this; + for (auto const& layer : std::views::reverse(layer_path_suffix.components())) { + assert(ptr); + if (ptr->layer_name() != layer) { + return false; + } + ptr = ptr->parent_.get(); + } + return true; + } + std::size_t data_cell_index::depth() const noexcept { return depth_; } data_cell_index_ptr data_cell_index::make_child(std::string child_layer_name, @@ -124,6 +142,19 @@ namespace phlex { return nullptr; } + data_cell_index_ptr data_cell_index::parent( + experimental::layer_path const& ancestor_path) const noexcept + { + auto curr = parent_; + while (curr) { + if (curr->layer_path_ends_with(ancestor_path)) { + return curr; + } + curr = curr->parent_; + } + return nullptr; + } + std::string data_cell_index::to_string() const { // TODO: prefix needs to be adjusted esp. if a root name can be supplied by the user. diff --git a/phlex/model/data_cell_index.hpp b/phlex/model/data_cell_index.hpp index 12257bbb7..fdeadc635 100644 --- a/phlex/model/data_cell_index.hpp +++ b/phlex/model/data_cell_index.hpp @@ -24,8 +24,10 @@ namespace phlex { data_cell_index_ptr make_child(std::string layer_name, std::size_t data_cell_number) const; experimental::identifier const& layer_name() const noexcept; experimental::layer_path layer_path() const; + bool layer_path_ends_with(experimental::layer_path const& layer_path_suffix) const noexcept; std::size_t depth() const noexcept; data_cell_index_ptr parent(experimental::identifier const& layer_name) const; + data_cell_index_ptr parent(experimental::layer_path const& ancestor_path) const noexcept; data_cell_index_ptr parent() const noexcept; bool has_parent() const noexcept; std::size_t number() const; diff --git a/phlex/model/layer_path.cpp b/phlex/model/layer_path.cpp index fbd584e06..80161eacf 100644 --- a/phlex/model/layer_path.cpp +++ b/phlex/model/layer_path.cpp @@ -29,7 +29,7 @@ namespace phlex::experimental { if (layer_path_.empty()) { throw std::runtime_error("Layer paths cannot be empty."); } - if (path.starts_with("/") and not is_complete()) { + if (path.starts_with('/') and not is_complete()) { throw std::runtime_error( fmt::format("A complete layer path must start with '/job'. '{}' does not!", path)); } diff --git a/phlex/model/layer_path.hpp b/phlex/model/layer_path.hpp index af8ee0838..5ed15da8e 100644 --- a/phlex/model/layer_path.hpp +++ b/phlex/model/layer_path.hpp @@ -42,6 +42,9 @@ namespace phlex::experimental { std::span components() const noexcept { return layer_path_; } + /// Return the lowest level (leaf) layer identifier + identifier const& leaf() const noexcept { return layer_path_.back(); } + std::string to_string() const; /// This function assumes incomplete paths have an implicit job root diff --git a/phlex/model/product_store.hpp b/phlex/model/product_store.hpp index 975df8dfb..e6ac380c7 100644 --- a/phlex/model/product_store.hpp +++ b/phlex/model/product_store.hpp @@ -35,6 +35,10 @@ namespace phlex::experimental { bool empty() const noexcept { return products_.empty(); } identifier const& layer_name() const noexcept; + bool layer_path_ends_with(layer_path const& layer_path_suffix) const noexcept + { + return id_->layer_path_ends_with(layer_path_suffix); + } algorithm_name const& source() const noexcept; identifier const& stage() const noexcept; data_cell_index_ptr const& index() const noexcept; diff --git a/plugins/python/src/modulewrap.cpp b/plugins/python/src/modulewrap.cpp index 0f55e1718..1180d0d8e 100644 --- a/plugins/python/src/modulewrap.cpp +++ b/plugins/python/src/modulewrap.cpp @@ -312,11 +312,14 @@ namespace { } // in the following, each of these parameters is passed differently b/c: - // "c" and "layer" are internal types that only takes an optional through a - // move for its contructor + // "c" is an internal type that only takes an optional through a move for its + // contructor + // "layer" is an internal type that takes an optional string_view // "suffix" is an optional itself, so can pass directly return std::optional{ - product_selector{.creator = std::move(c), .layer = std::move(l), .suffix = s}}; + product_selector{.creator = std::move(c), + .layer = l.transform(&identifier::operator std::string_view), + .suffix = s}}; } template @@ -1089,28 +1092,28 @@ namespace { return result; } - std::optional transform_output_layer( + phlex::detail::layer_selector transform_output_layer( std::string const& name, std::vector const& input_selectors) { // if a layer was provided, we'll re-use it for the intermediate Python products, // otherwise also specify no layer for the intermediates (TODO: it may be worthwhile // to explore using a "workspace" layer) - std::optional output_layer; + phlex::detail::layer_selector output_layer; // note: the following treats the first layer as special (as in, if the first layer // is optional, but the next one is not, the check will succeed), but that's fine // as for now such mixing isn't supported by the product selector if (input_selectors[0].layer) { - output_layer = static_cast(input_selectors[0].layer); + output_layer = input_selectors[0].layer; // TODO: it's not clear what the output layer will be if the input layers are not // all the same, so for now, simply raise an error if their is any ambiguity for (auto const& iq_pq : input_selectors | std::views::drop(1)) { - if (static_cast(iq_pq.layer) != output_layer.value()) { + if (iq_pq.layer != output_layer) { PyErr_Format(PyExc_ValueError, "transform %s output layer is ambiguous", name.c_str()); - return std::nullopt; // error return + return {}; // error return } } } - return output_layer; // not an error return if nullopt + return output_layer; // not an error return if empty } bool validate_transform_output(std::string const& name, @@ -1192,7 +1195,7 @@ namespace { std::vector const& input_selectors, std::string const& output_type, std::string const& output_suffix, - std::optional output_layer, + phlex::detail::layer_selector output_layer, concurrency nconcur) { // Only a single output is supported until typed tuple conversion is implemented. @@ -1507,7 +1510,7 @@ namespace { PyObject* callable, algorithm_name const& creator, identifier const& suffix, - identifier const& layer, + layer_path const& layer, std::string const& output_type) { auto provide = [&]() { @@ -1537,7 +1540,7 @@ namespace { } algorithm_name const creator = algorithm_name::create(std::string_view(*opq.value().creator)); - identifier const layer = opq.value().layer; + layer_path const& layer = *opq.value().layer; identifier const suffix = opq.value().suffix.value_or(""); // Provider callbacks retain the callable for the graph lifetime. A translator node diff --git a/test/accumulator_test.cpp b/test/accumulator_test.cpp index 309757d22..6fa9a2f4e 100644 --- a/test/accumulator_test.cpp +++ b/test/accumulator_test.cpp @@ -86,7 +86,7 @@ namespace { accumulator_{g_, node_name, "test_stage"_id, - "run"_id, + "run", product_specifications(1), [](phlex::data_cell_index const&) { return std::make_unique(0); }}, fold_op_{g_, tbb::flow::unlimited, count_fold}, @@ -233,7 +233,7 @@ TEST_CASE("Test accumulator warning message if cache is not flushed", "[multithr g, "test_accumulator_warning", "test_stage"_id, - "run"_id, + "run", product_specifications(1), [](phlex::data_cell_index const&) { return std::make_unique(0); }); diff --git a/test/allowed_families_test.cpp b/test/allowed_families_test.cpp index d7f7fb30e..699e33cdf 100644 --- a/test/allowed_families_test.cpp +++ b/test/allowed_families_test.cpp @@ -43,11 +43,11 @@ TEST_CASE("Testing families", "[data model]") // Wire up providers for each level g.provide("run_id_provider", provide_index, concurrency::unlimited) - .output_product("dummy", "id", "run"); + .output_product("dummy", "id", "/job/run"); g.provide("subrun_id_provider", provide_index, concurrency::unlimited) - .output_product("dummy", "id", "subrun"); + .output_product("dummy", "id", "/job/run/subrun"); g.provide("event_id_provider", provide_index, concurrency::unlimited) - .output_product("dummy", "id", "event"); + .output_product("dummy", "id", "/job/run/subrun/event"); g.observe("se", check_two_ids) .input_family(product_selector{.creator = "dummy", .layer = "subrun", .suffix = "id"}, diff --git a/test/benchmarks/benchmarks_provider.cpp b/test/benchmarks/benchmarks_provider.cpp index 8e4f53d9b..ed0f21faf 100644 --- a/test/benchmarks/benchmarks_provider.cpp +++ b/test/benchmarks/benchmarks_provider.cpp @@ -5,5 +5,5 @@ PHLEX_REGISTER_PROVIDERS(s) { using namespace phlex; s.provide("provide_id", [](data_cell_index const& id) { return id; }) - .output_product("input", "id", "event"); + .output_product("input", "id", "/job/event"); } diff --git a/test/cached_execution_test.cpp b/test/cached_execution_test.cpp index 6b3160b1f..da6a7beea 100644 --- a/test/cached_execution_test.cpp +++ b/test/cached_execution_test.cpp @@ -65,11 +65,11 @@ TEST_CASE("Cached function calls", "[data model]") // Register providers g.provide("provide_number", provide_number, concurrency::unlimited) - .output_product("input", "number", "run"); + .output_product("input", "number", "/job/run"); g.provide("provide_another", provide_another, concurrency::unlimited) - .output_product("input", "another", "subrun"); + .output_product("input", "another", "/job/run/subrun"); g.provide("provide_still", provide_still, concurrency::unlimited) - .output_product("input", "still", "event"); + .output_product("input", "still", "/job/run/subrun/event"); g.transform("A1", call_one, concurrency::unlimited) .input_family(product_selector{.creator = "input", .layer = "run", .suffix = "number"}) diff --git a/test/class_registration_test.cpp b/test/class_registration_test.cpp index ed54369b4..81df8d053 100644 --- a/test/class_registration_test.cpp +++ b/test/class_registration_test.cpp @@ -81,17 +81,17 @@ namespace { graph .provide( "provide_number", [](data_cell_index const&) { return 3; }, concurrency::unlimited) - .output_product("input", "number", "job"); + .output_product("input", "number", "/job"); graph .provide( "provide_temperature", [](data_cell_index const&) { return 98.5; }, concurrency::unlimited) - .output_product("input", "temperature", "job"); + .output_product("input", "temperature", "/job"); graph .provide( "provide_name", [](data_cell_index const&) { return std::string{"John"}; }, concurrency::unlimited) - .output_product("input", "name", "job"); + .output_product("input", "name", "/job"); } auto transformed_product_suffixes() diff --git a/test/config_test.cpp b/test/config_test.cpp index ed8d0fad2..19361c29f 100644 --- a/test/config_test.cpp +++ b/test/config_test.cpp @@ -80,7 +80,7 @@ TEST_CASE("Retrieve product_selector", "[config]") ContainsSubstring("not a string")); CHECK_THROWS_WITH(config.get("malformed2"), ContainsSubstring("Error retrieving parameter 'malformed2'") && - ContainsSubstring("Cannot specify the empty string as a data layer.")); + ContainsSubstring("Layer paths cannot be empty")); CHECK_THROWS_WITH(config.get("malformed3"), ContainsSubstring("Error retrieving parameter 'malformed3'") && ContainsSubstring("Cannot specify product with empty creator name.")); diff --git a/test/demo-giantdata/unfold_transform_fold_test.cpp b/test/demo-giantdata/unfold_transform_fold_test.cpp index f78144225..c6c8cec2f 100644 --- a/test/demo-giantdata/unfold_transform_fold_test.cpp +++ b/test/demo-giantdata/unfold_transform_fold_test.cpp @@ -63,7 +63,7 @@ TEST_CASE("Unfold-transform-fold pipeline", "[concurrency][unfold][fold]") spill_index.parent()->number(), spill_index.number()); }) - .output_product("input", "wgen", "spill"); + .output_product("input", "wgen", "/job/run/subrun/spill"); g.unfold( "WaveformGenerator", diff --git a/test/filter_test.cpp b/test/filter_test.cpp index 3c17b0503..d90ad71e6 100644 --- a/test/filter_test.cpp +++ b/test/filter_test.cpp @@ -134,7 +134,7 @@ TEST_CASE("Predicate receives a resource token", "[filtering][resource]") g.add_driver(gen); g.add_serialized_resource(); g.provide("provide_num", give_me_nums, concurrency::unlimited) - .output_product("input", "num", "event"); + .output_product("input", "num", "/job/event"); g.predicate("evens_with_resource", evens_with_resource, concurrency::unlimited) .input_family({.creator = "input", .layer = "event", .suffix = "num"}, resource{}); @@ -156,7 +156,7 @@ TEST_CASE("Two predicates", "[filtering]") auto g = phlex::detail::framework_graph::without_driver("test"); g.add_driver(gen); g.provide("provide_num", give_me_nums, concurrency::unlimited) - .output_product("input", "num", "event"); + .output_product("input", "num", "/job/event"); g.predicate("evens_only", evens_only, concurrency::unlimited) .input_family(product_selector{.creator = "input", .layer = "event", .suffix = "num"}); g.predicate("odds_only", odds_only, concurrency::unlimited) @@ -183,7 +183,7 @@ TEST_CASE("Two predicates in series", "[filtering]") auto g = phlex::detail::framework_graph::without_driver("test"); g.add_driver(gen); g.provide("provide_num", give_me_nums, concurrency::unlimited) - .output_product("input", "num", "event"); + .output_product("input", "num", "/job/event"); g.predicate("evens_only", evens_only, concurrency::unlimited) .input_family(product_selector{.creator = "input", .layer = "event", .suffix = "num"}); g.predicate("odds_only", odds_only, concurrency::unlimited) @@ -206,7 +206,7 @@ TEST_CASE("Two predicates in parallel", "[filtering]") auto g = phlex::detail::framework_graph::without_driver("test"); g.add_driver(gen); g.provide("provide_num", give_me_nums, concurrency::unlimited) - .output_product("input", "num", "event"); + .output_product("input", "num", "/job/event"); g.predicate("evens_only", evens_only, concurrency::unlimited) .input_family(product_selector{.creator = "input", .layer = "event", .suffix = "num"}); g.predicate("odds_only", odds_only, concurrency::unlimited) @@ -237,7 +237,7 @@ TEST_CASE("Three predicates in parallel", "[filtering]") auto g = phlex::detail::framework_graph::without_driver("test"); g.add_driver(gen); g.provide("provide_num", give_me_nums, concurrency::unlimited) - .output_product("input", "num", "event"); + .output_product("input", "num", "/job/event"); for (auto const& [name, b, e] : configs) { g.make(b, e) .predicate(name, ¬_in_range::eval, concurrency::unlimited) @@ -264,9 +264,9 @@ TEST_CASE("Two predicates in parallel (each with multiple arguments)", "[filteri auto g = phlex::detail::framework_graph::without_driver("test"); g.add_driver(gen); g.provide("provide_num", give_me_nums, concurrency::unlimited) - .output_product("input", "num", "event"); + .output_product("input", "num", "/job/event"); g.provide("provide_other_num", give_me_other_nums, concurrency::unlimited) - .output_product("input", "other_num", "event"); + .output_product("input", "other_num", "/job/event"); g.predicate("evens_only", evens_only, concurrency::unlimited) .input_family(product_selector{.creator = "input", .layer = "event", .suffix = "num"}); g.predicate("odds_only", odds_only, concurrency::unlimited) diff --git a/test/fold_duplicate_layer_name_test.cpp b/test/fold_duplicate_layer_name_test.cpp index 090fa3767..63668802f 100644 --- a/test/fold_duplicate_layer_name_test.cpp +++ b/test/fold_duplicate_layer_name_test.cpp @@ -25,8 +25,9 @@ // As the run_add node performs folds only over "runs", any top-level "events" // stores are excluded from the fold result. // -// N.B. The index_router sends data products to nodes based on the name of the lowest -// layer. For example, the top-level "event" and the nested "run/event" are both +// N.B. Each provider declares the complete path it serves, so the index_router sends a cell +// only to the providers of its own branch. The "event" selectors of the folds match +// both branches, so the top-level "event" and the nested "run/event" are both // candidates for the "job" fold. // ======================================================================================= @@ -52,7 +53,18 @@ using namespace phlex; namespace { // Provider function - unsigned int provide_number(data_cell_index const& index) { return index.number(); } + std::atomic top_level_provider_calls{0}; + std::atomic run_provider_calls{0}; + unsigned int provide_top_level_number(data_cell_index const& index) + { + ++top_level_provider_calls; + return index.number(); + } + unsigned int provide_run_number(data_cell_index const& index) + { + ++run_provider_calls; + return index.number(); + } void add(std::atomic& counter, unsigned int number) { counter += number; } @@ -97,8 +109,12 @@ TEST_CASE("Fold different layer paths with same trailing name", "[graph]") g.add_driver(gen); // Register provider - g.provide("provide_number", provide_number, concurrency::unlimited) - .output_product("input", "number", "event"); + top_level_provider_calls = 0; + run_provider_calls = 0; + g.provide("provide_top_level_number", provide_top_level_number, concurrency::unlimited) + .output_product("input", "number", "/job/event"); + g.provide("provide_run_number", provide_run_number, concurrency::unlimited) + .output_product("input", "number", "/job/run/event"); g.fold("run_add", add, concurrency::unlimited, "run", 0u) .input_family(product_selector{.creator = "input", .layer = "event", .suffix = "number"}) @@ -123,6 +139,8 @@ TEST_CASE("Fold different layer paths with same trailing name", "[graph]") CHECK(g.execution_count("run_add") == std::size_t{number_runs} * number_events_per_run); CHECK(g.execution_count("job_add") == (number_runs * number_events_per_run) + number_top_level_events); + CHECK(top_level_provider_calls == number_top_level_events); + CHECK(run_provider_calls == number_runs * number_events_per_run); CHECK(g.execution_count("verify_run_sum") == number_runs); CHECK(g.execution_count("verify_job_sum") == 1); return std::make_pair(std::move(run_sums), std::move(job_sums)); @@ -158,12 +176,12 @@ TEST_CASE("Fold duplicate event paths with empty unfolds", "[graph][issue955]") "top_count", [top_count](data_cell_index const&) { return top_count; }, concurrency::unlimited) - .output_product("input", "top_count", "top"); + .output_product("input", "top_count", "/job/top"); g.provide( "run_count", [run_count](data_cell_index const&) { return run_count; }, concurrency::unlimited) - .output_product("input", "run_count", "run"); + .output_product("input", "run_count", "/job/run"); g.unfold( "top_events", &split::predicate, &split::unfold, concurrency::unlimited, "event") .input_family(product_selector{.creator = "input", .layer = "top", .suffix = "top_count"}) @@ -172,11 +190,13 @@ TEST_CASE("Fold duplicate event paths with empty unfolds", "[graph][issue955]") "run_events", &split::predicate, &split::unfold, concurrency::unlimited, "event") .input_family(product_selector{.creator = "input", .layer = "run", .suffix = "run_count"}) .output_product_suffixes("nested_seed"); - g.provide( - "event_number", - [](data_cell_index const& index) { return static_cast(index.number() + 1); }, - concurrency::unlimited) - .output_product("input", "number", "event"); + auto const event_number = [](data_cell_index const& index) { + return static_cast(index.number() + 1); + }; + g.provide("top_event_number", event_number, concurrency::unlimited) + .output_product("input", "number", "/job/top/event"); + g.provide("run_event_number", event_number, concurrency::unlimited) + .output_product("input", "number", "/job/run/event"); g.fold("job_add", add, concurrency::unlimited, "job", 0u) .input_family(product_selector{.creator = "input", .layer = "event", .suffix = "number"}) .output_product_suffixes("sum"); diff --git a/test/fold_test.cpp b/test/fold_test.cpp index dcffd5988..9aa7edbe0 100644 --- a/test/fold_test.cpp +++ b/test/fold_test.cpp @@ -96,7 +96,7 @@ TEST_CASE("Different data layers of fold", "[graph]") g.add_driver(gen); g.provide("provide_number", provide_number, concurrency::unlimited) - .output_product("input", "number", "event"); + .output_product("input", "number", "/job/run/event"); g.fold("run_add", add, concurrency::unlimited, "run") .input_family(product_selector{.creator = "input", .layer = "event", .suffix = "number"}) @@ -140,7 +140,7 @@ TEST_CASE("Fold output without send consumed downstream", "[graph]") g.add_driver(gen); g.provide("provide_number", provide_number, concurrency::unlimited) - .output_product("input", "number", "event"); + .output_product("input", "number", "/job/run/event"); g.fold("collect_numbers", collect, concurrency::serial, "run") .input_family(product_selector{.creator = "input", .layer = "event", .suffix = "number"}) @@ -171,7 +171,7 @@ TEST_CASE("Fold receives a resource token", "[graph][fold][resource]") g.add_serialized_resource(); g.provide("provide_number", provide_number, concurrency::unlimited) - .output_product("input", "number", "event"); + .output_product("input", "number", "/job/run/event"); auto counter = detail::thread_counter::counter_type{}; auto add_with_resources = [&counter](std::atomic& sum, @@ -208,7 +208,7 @@ TEST_CASE("Multi-output fold", "[graph][fold]") g.add_driver(gen); g.provide("provide_number", provide_number, concurrency::unlimited) - .output_product("input", "number", "event"); + .output_product("input", "number", "/job/run/event"); g.fold("run_add_and_count", add_and_count, concurrency::unlimited, "run") .input_family(product_selector{.creator = "input", .layer = "event", .suffix = "number"}) diff --git a/test/form/cell_number_vector.cpp b/test/form/cell_number_vector.cpp index bc0c347d5..d7d798a6f 100644 --- a/test/form/cell_number_vector.cpp +++ b/test/form/cell_number_vector.cpp @@ -31,7 +31,7 @@ PHLEX_REGISTER_PROVIDERS(graph, config) auto const creator = config.get("creator"); graph.provide("cell_numbers", number_of, concurrency::unlimited) .output_product( - creator, experimental::identifier("numbers"), experimental::identifier("event")); + creator, experimental::identifier("numbers"), experimental::layer_path("/job/event")); } PHLEX_REGISTER_ALGORITHMS(m, config) diff --git a/test/form/dune_example_hits.cpp b/test/form/dune_example_hits.cpp index 2aec7cc94..bbe0dbce0 100644 --- a/test/form/dune_example_hits.cpp +++ b/test/form/dune_example_hits.cpp @@ -58,11 +58,11 @@ namespace { PHLEX_REGISTER_PROVIDERS(s) { s.provide("cand_hit_standard", roi_candidates) - .output_product("cand_hit_standard", "hitCandidates", "roi"); + .output_product("cand_hit_standard", "hitCandidates", "/job/spill/wire/roi"); s.provide("merge_roi_candidates", wire_candidates) - .output_product("merge_roi_candidates", "wireCandidates", "wire"); + .output_product("merge_roi_candidates", "wireCandidates", "/job/spill/wire"); s.provide("merge_wire_candidates", spill_candidates) - .output_product("merge_wire_candidates", "spillCandidates", "spill"); + .output_product("merge_wire_candidates", "spillCandidates", "/job/spill"); } PHLEX_REGISTER_ALGORITHMS(m) diff --git a/test/form/generate_vector.cpp b/test/form/generate_vector.cpp index 35839e252..544605632 100644 --- a/test/form/generate_vector.cpp +++ b/test/form/generate_vector.cpp @@ -51,8 +51,9 @@ PHLEX_REGISTER_PROVIDERS(graph, config) graph.make(n_time_ticks, seed, mean, stddev) .provide("random_wires", &gaussian_generator::operator()) - .output_product( - creator, phlex::experimental::identifier(creator), phlex::experimental::identifier("event")); + .output_product(creator, + phlex::experimental::identifier(creator), + phlex::experimental::layer_path("/job/event")); } PHLEX_REGISTER_ALGORITHMS(graph, config) diff --git a/test/framework_graph_test.cpp b/test/framework_graph_test.cpp index f83ca69c6..1c2abcf3b 100644 --- a/test/framework_graph_test.cpp +++ b/test/framework_graph_test.cpp @@ -115,7 +115,7 @@ TEST_CASE("Make progress with one thread", "[graph]") "provide_number", [](data_cell_index const& index) -> unsigned int { return index.number(); }, concurrency::unlimited) - .output_product("input", "number", "spill"); + .output_product("input", "number", "/job/spill"); g.observe( "observe_number", [](unsigned int const /*number*/) {}, concurrency::unlimited) .input_family(product_selector{.creator = "input", .layer = "spill", .suffix = "number"}); @@ -139,7 +139,7 @@ TEST_CASE("Stop driver when workflow throws exception", "[graph]") throw std::runtime_error("Error to stop driver"); }, concurrency::unlimited) - .output_product("input", "number", "spill"); + .output_product("input", "number", "/job/spill"); // Must have at least one downstream node that requires something of the // provider...otherwise provider will not be executed. @@ -175,7 +175,7 @@ TEST_CASE("Throw when predicate specified by consumer does not exist", "[graph]" "provide_num", [](data_cell_index const& id) -> unsigned int { return id.number(); }, concurrency::unlimited) - .output_product("input", "num", "event"); + .output_product("input", "num", "/job/event"); g.observe( "observe_num", [](unsigned int const) {}, concurrency::unlimited) @@ -231,7 +231,7 @@ TEST_CASE("Use default driver", "[graph]") "provide_number", [](data_cell_index const&) -> unsigned int { return 42u; }, concurrency::unlimited) - .output_product("input", "number", "job"); + .output_product("input", "number", "/job"); g.observe( "observe_number", [](unsigned int const) {}, concurrency::unlimited) .input_family(product_selector{.creator = "input", .layer = "job", .suffix = "number"}); @@ -270,7 +270,7 @@ TEST_CASE("Allow late driver configuration", "[graph]") "provide_number", [](data_cell_index const& index) -> unsigned int { return index.number(); }, concurrency::unlimited) - .output_product("input", "number", "spill"); + .output_product("input", "number", "/job/spill"); g.observe( "observe_number", [](unsigned int const /*number*/) {}, concurrency::unlimited) .input_family(product_selector{.creator = "input", .layer = "spill", .suffix = "number"}); diff --git a/test/function_registration_test.cpp b/test/function_registration_test.cpp index d4cbe9264..df84a0813 100644 --- a/test/function_registration_test.cpp +++ b/test/function_registration_test.cpp @@ -67,11 +67,11 @@ TEST_CASE("Call non-framework functions", "[programming model]") // Register providers g.provide("provide_number", provide_number, concurrency::unlimited) - .output_product("input", "number", "job"); + .output_product("input", "number", "/job"); g.provide("provide_temperature", provide_temperature, concurrency::unlimited) - .output_product("input", "temperature", "job"); + .output_product("input", "temperature", "/job"); g.provide("provide_name", provide_name, concurrency::unlimited) - .output_product("input", "name", "job"); + .output_product("input", "name", "/job"); SECTION("No framework") { diff --git a/test/hierarchical_nodes_test.cpp b/test/hierarchical_nodes_test.cpp index aa6143562..bb1828c84 100644 --- a/test/hierarchical_nodes_test.cpp +++ b/test/hierarchical_nodes_test.cpp @@ -93,7 +93,7 @@ TEST_CASE("Hierarchical nodes", "[graph]") spdlog::info("Providing time for {}", index.to_string()); return std::chrono::system_clock::now(); }) - .output_product("input", "time", "run"); + .output_product("input", "time", "/job/run"); g.provide("provide_number", [](data_cell_index const& index) -> unsigned int { @@ -101,7 +101,7 @@ TEST_CASE("Hierarchical nodes", "[graph]") auto const run_number = index.parent()->number(); return event_number + run_number; }) - .output_product("input", "number", "event"); + .output_product("input", "number", "/job/run/event"); g.transform("get_the_time", strtime, concurrency::unlimited) .input_family(product_selector{.creator = "input", .layer = "run", .suffix = "time"}) diff --git a/test/index_router_test.cpp b/test/index_router_test.cpp index a3445683c..449ee1e79 100644 --- a/test/index_router_test.cpp +++ b/test/index_router_test.cpp @@ -13,6 +13,8 @@ #include #include +#include +#include #include #include @@ -80,8 +82,12 @@ TEST_CASE("Index routing accepts data cells from chained unfolds", "[graph][unfo router.finalize( graph, {experimental::layer_path{"/job/spill"}}, - {{.name = "a_split_b", .input_layers = {"a"_id, "spill"_id}, .output_layer = "b"_id}, - {.name = "z_split_a", .input_layers = {"spill"_id}, .output_layer = "a"_id}}, + {{.name = "a_split_b", + .input_layers = {experimental::layer_path{"a"}, experimental::layer_path{"spill"}}, + .output_layer = "b"_id}, + {.name = "z_split_a", + .input_layers = {experimental::layer_path{"spill"}}, + .output_layer = "a"_id}}, {{"dummy", {.input_product = {.layer = "spill"}, .port = &indices}}}, {}, {}); @@ -106,7 +112,9 @@ TEST_CASE("Unfold hierarchy rejects cyclic layer production", "[graph][unfold]") SECTION("Self-cycle") { - unfolds = {{.name = "split_run", .input_layers = {"run"_id}, .output_layer = "run"_id}}; + unfolds = {{.name = "split_run", + .input_layers = {experimental::layer_path{"run"}}, + .output_layer = "run"_id}}; expected_error = "Unfold layer hierarchy expansion exceeded max depth 3 (initial deepest 2 + 1 unfold " "node(s)). Offending unfold: split_run"; @@ -114,8 +122,12 @@ TEST_CASE("Unfold hierarchy rejects cyclic layer production", "[graph][unfold]") SECTION("Two-unfold cycle") { - unfolds = {{.name = "split_spill", .input_layers = {"run"_id}, .output_layer = "spill"_id}, - {.name = "split_run", .input_layers = {"spill"_id}, .output_layer = "run"_id}}; + unfolds = {{.name = "split_spill", + .input_layers = {experimental::layer_path{"run"}}, + .output_layer = "spill"_id}, + {.name = "split_run", + .input_layers = {experimental::layer_path{"spill"}}, + .output_layer = "run"_id}}; expected_error = "Unfold layer hierarchy expansion exceeded max depth 4 (initial deepest 2 + 2 unfold " "node(s)). Offending unfold: split_spill"; @@ -138,14 +150,16 @@ TEST_CASE("Unfold hierarchy rejects driver output layer collisions", "[graph][un tbb::flow::graph graph; tbb::flow::queue_node indices{graph}; detail::index_router router{graph}; - auto const driver_path = - GENERATE(layer_path{"/job/run/spill"}, layer_path{"/job/run/spill/event"}); + auto const driver_path = GENERATE(experimental::layer_path{"/job/run/spill"}, + experimental::layer_path{"/job/run/spill/event"}); CAPTURE(driver_path); CHECK_THROWS_WITH( router.finalize(graph, {driver_path}, - {{.name = "split", .input_layers = {"run"_id}, .output_layer = "spill"_id}}, + {{.name = "split", + .input_layers = {experimental::layer_path{"run"}}, + .output_layer = "spill"_id}}, {{"dummy", {.input_product = {.layer = "run"}, .port = &indices}}}, {}, {}), @@ -160,13 +174,19 @@ TEST_CASE("Unfold hierarchy rejects duplicate output layers", "[graph][unfold]") tbb::flow::queue_node indices{graph}; detail::index_router router{graph}; - auto const driver_path = GENERATE(layer_path{"/job/run"}, layer_path{"/job"}); + auto const driver_path = + GENERATE(experimental::layer_path{"/job/run"}, experimental::layer_path{"/job"}); CAPTURE(driver_path); - detail::index_router::unfold_data unfolds{ - {.name = "split_a", .input_layers = {"run"_id}, .output_layer = "spill"_id}, - {.name = "split_b", .input_layers = {"run"_id}, .output_layer = "spill"_id}}; - if (driver_path == layer_path{"/job"}) { - unfolds.push_back({.name = "split_run", .input_layers = {"job"_id}, .output_layer = "run"_id}); + detail::index_router::unfold_data unfolds{{.name = "split_a", + .input_layers = {experimental::layer_path{"run"}}, + .output_layer = "spill"_id}, + {.name = "split_b", + .input_layers = {experimental::layer_path{"run"}}, + .output_layer = "spill"_id}}; + if (driver_path == experimental::layer_path{"/job"}) { + unfolds.push_back({.name = "split_run", + .input_layers = {experimental::layer_path{"job"}}, + .output_layer = "run"_id}); } CHECK_THROWS_WITH( @@ -181,7 +201,6 @@ TEST_CASE("Unfold hierarchy rejects duplicate output layers", "[graph][unfold]") TEST_CASE("Counting layers on other branches do not produce end tokens", "[graph]") { - using namespace phlex::experimental::literals; tbb::flow::graph graph; tbb::flow::queue_node indices{graph}; tbb::flow::queue_node tokens{graph}; @@ -193,8 +212,8 @@ TEST_CASE("Counting layers on other branches do not produce end tokens", "[graph {}, {}, {{"fold", - {{.layer = "run"_id, - .counting_layer = "event"_id, + {{.layer = experimental::layer_path{"run"}, + .counting_layer = experimental::layer_path{"event"}, .token_port = &tokens, .index_port = &indices}}}}); @@ -222,7 +241,6 @@ TEST_CASE("Counting layers on other branches do not produce end tokens", "[graph TEST_CASE("Duplicate counting paths deliver one combined end token", "[graph][issue955]") { - using namespace phlex::experimental::literals; auto const [description, top_count, nested_count] = GENERATE(Catch::Generators::table( {{"Both event paths populated", 2u, 3u}, @@ -242,8 +260,8 @@ TEST_CASE("Duplicate counting paths deliver one combined end token", "[graph][is {{"dummy", {.input_product = {.layer = "job"}, .port = &indices}}}, {}, {{"fold", - {{.layer = "job"_id, - .counting_layer = "event"_id, + {{.layer = experimental::layer_path{"job"}, + .counting_layer = experimental::layer_path{"event"}, .token_port = &tokens, .index_port = &indices}}}}); @@ -268,3 +286,73 @@ TEST_CASE("Duplicate counting paths deliver one combined end token", "[graph][is CHECK(std::cmp_equal(token.count, top_count + nested_count)); CHECK_FALSE(tokens.try_get(token)); } + +TEST_CASE("The deepest layer is compared only along branches containing every layer", "[graph]") +{ + // /job/run/subrun/calib is deeper than /job/calib/step, but only the latter contains both + // "calib" and "calib/step". The "calib" slot must therefore count "calib/step" cells. + tbb::flow::graph graph; + tbb::flow::queue_node calib_indices{graph}; + tbb::flow::queue_node step_indices{graph}; + tbb::flow::queue_node calib_tokens{graph}; + tbb::flow::queue_node step_tokens{graph}; + detail::index_router router{graph}; + router.finalize(graph, + {experimental::layer_path{"/job/calib/step"}, + experimental::layer_path{"/job/run/subrun/calib"}}, + {}, + {}, + {}, + {{"join", + {{.layer = experimental::layer_path{"calib"}, + .counting_layer = std::nullopt, + .token_port = &calib_tokens, + .index_port = &calib_indices}, + {.layer = experimental::layer_path{"calib/step"}, + .counting_layer = std::nullopt, + .token_port = &step_tokens, + .index_port = &step_indices}}}}); + + auto const job = data_cell_index::job(); + auto const calib = job->make_child("calib", 0); + router.route(job, {}); + router.route(calib, {}); + + auto calib_counts = std::make_shared(); + calib_counts->emplace(calib->make_child("step", 0)->layer_hash(), 2); + auto job_counts = std::make_shared(); + job_counts->emplace(calib->layer_hash(), 1); + router.drain({{.index = calib, .counts = calib_counts}, {.index = job, .counts = job_counts}}); + graph.wait_for_all(); + + detail::indexed_end_token token; + REQUIRE(calib_tokens.try_get(token)); + CHECK(token.index == calib); + CHECK(std::cmp_equal(token.count, 2)); +} + +TEST_CASE("Joining layers on sibling branches is rejected when finalizing", "[graph]") +{ + tbb::flow::graph graph; + tbb::flow::queue_node spill_indices{graph}; + tbb::flow::queue_node run_indices{graph}; + tbb::flow::queue_node spill_tokens{graph}; + tbb::flow::queue_node run_tokens{graph}; + detail::index_router router{graph}; + CHECK_THROWS_AS(router.finalize(graph, + {experimental::layer_path{"/job/spill/event"}, + experimental::layer_path{"/job/run/event"}}, + {}, + {}, + {}, + {{"join", + {{.layer = experimental::layer_path{"spill/event"}, + .counting_layer = std::nullopt, + .token_port = &spill_tokens, + .index_port = &spill_indices}, + {.layer = experimental::layer_path{"run/event"}, + .counting_layer = std::nullopt, + .token_port = &run_tokens, + .index_port = &run_indices}}}}), + std::runtime_error); +} diff --git a/test/memory-checks/many_events_test.cpp b/test/memory-checks/many_events_test.cpp index 3d0b5fd9c..ac334588d 100644 --- a/test/memory-checks/many_events_test.cpp +++ b/test/memory-checks/many_events_test.cpp @@ -24,7 +24,7 @@ try { g.add_driver(gen); g.provide("provide_number", [](data_cell_index const& id) -> unsigned { return id.number(); }) - .output_product("input", "number", "event"); + .output_product("input", "number", "/job/event"); g.transform("pass_on", pass_on, concurrency::unlimited) .input_family(product_selector{.creator = "input", .layer = "event", .suffix = "number"}) .output_product_suffixes("different"); diff --git a/test/mock-workflow/id_provider.cpp b/test/mock-workflow/id_provider.cpp index 8e4f53d9b..ed0f21faf 100644 --- a/test/mock-workflow/id_provider.cpp +++ b/test/mock-workflow/id_provider.cpp @@ -5,5 +5,5 @@ PHLEX_REGISTER_PROVIDERS(s) { using namespace phlex; s.provide("provide_id", [](data_cell_index const& id) { return id; }) - .output_product("input", "id", "event"); + .output_product("input", "id", "/job/event"); } diff --git a/test/multilayer_join_test.cpp b/test/multilayer_join_test.cpp index 379678191..670dd2886 100644 --- a/test/multilayer_join_test.cpp +++ b/test/multilayer_join_test.cpp @@ -3,6 +3,7 @@ #include "phlex/model/algorithm_name.hpp" #include "phlex/model/fwd.hpp" #include "phlex/model/identifier.hpp" +#include "phlex/model/layer_path.hpp" #include "phlex/model/product_specification.hpp" #include "phlex/model/product_store.hpp" @@ -65,7 +66,8 @@ TEST_CASE("multilayer_join_node joins multiple input products", "[join]") auto join = multilayer_join_node<2>{ graph, "multilayer_join_test", - std::vector{identifier{"left_layer"}, identifier{"right_layer"}}}; + std::vector{experimental::layer_path{"left_layer"}, + experimental::layer_path{"right_layer"}}}; oneapi::tbb::flow::queue_node> sink{graph}; make_edge(output_port<0>(join), sink); diff --git a/test/multiple_function_registration_test.cpp b/test/multiple_function_registration_test.cpp index 803a5a48d..6f6b42d7e 100644 --- a/test/multiple_function_registration_test.cpp +++ b/test/multiple_function_registration_test.cpp @@ -49,9 +49,9 @@ TEST_CASE("Call multiple functions", "[programming model]") g.provide("provide_numbers", [](data_cell_index const&) -> std::vector { return {0, 1, 2, 3, 4}; }) - .output_product("input", "numbers", "job"); + .output_product("input", "numbers", "/job"); g.provide("provide_offset", [](data_cell_index const&) -> unsigned { return 6u; }) - .output_product("input", "offset", "job"); + .output_product("input", "offset", "/job"); SECTION("All free functions") { diff --git a/test/output_products_test.cpp b/test/output_products_test.cpp index 762e0c2b4..3f7c2e577 100644 --- a/test/output_products_test.cpp +++ b/test/output_products_test.cpp @@ -80,7 +80,7 @@ TEST_CASE("Output data products", "[graph]") g.add_source("test_source"); g.provide("provide_number", [](data_cell_index const&) -> int { return 17; }) - .output_product("input", "number_from_provider", "spill"); + .output_product("input", "number_from_provider", "/job/spill"); g.transform( "square_number", diff --git a/test/plugins/ij_source.cpp b/test/plugins/ij_source.cpp index 3dd2a7892..e5b25de81 100644 --- a/test/plugins/ij_source.cpp +++ b/test/plugins/ij_source.cpp @@ -14,7 +14,7 @@ PHLEX_REGISTER_PROVIDERS(s) // Explicitly test s.make syntax for provider registration, in addition to the lambda syntax. s.make() .provide("provide_i", &signed_value_provider::provide_i) - .output_product("input", "i", "event"); + .output_product("input", "i", "/job/event"); s.provide("provide_j", [](data_cell_index const& id) { return -static_cast(id.number()); }) - .output_product("input", "j", "event"); + .output_product("input", "j", "/job/event"); } diff --git a/test/product_selecting_test.cpp b/test/product_selecting_test.cpp index 5e8978456..b69f9abe1 100644 --- a/test/product_selecting_test.cpp +++ b/test/product_selecting_test.cpp @@ -6,16 +6,21 @@ #include "phlex/model/data_cell_index.hpp" #include "phlex/model/handle.hpp" #include "phlex/model/identifier.hpp" +#include "phlex/model/layer_path.hpp" #include "phlex/model/product_specification.hpp" #include "phlex/model/products.hpp" #include "phlex/model/type_id.hpp" #include "plugins/layer_generator.hpp" +#include "test/ostream_logger.hpp" #include +#include #include +#include #include #include +#include #include #include #include @@ -41,22 +46,41 @@ namespace { return experimental::product_for(static_cast(dci.number())); } + /// Source of an archived @c int count in the layer given at construction. The layer is a + /// leaf, partial, or complete layer path, and defaults to the leaf layer @c "event". class archived_count_source : public source { public: + archived_count_source() = default; + explicit archived_count_source(std::string layer) : layer_{std::move(layer)} {} + provider_bundles create_providers(product_selector const& selector) override { using namespace experimental::literals; phlex::experimental::product_specification spec{ "archived_input", "archived_count", phlex::experimental::make_type_id()}; - if (!selector.match(spec, "event"_id, "previous_process"_id)) { + if (!selector.match(spec, experimental::layer_path{layer_}, "previous_process"_id)) { return {}; } return {{.provider_function = provide_archived_count, .max_concurrency = concurrency::unlimited, .spec = std::move(spec), - .layer = "event", + .layer = layer_, .stage = "previous_process"}}; } + + private: + std::string layer_{"event"}; + }; + + class count_up { + public: + explicit count_up(unsigned int const count) : count_{count} {} + static unsigned int initial_value() { return 0; } + bool predicate(unsigned int const i) const { return i < count_; } + static auto unfold(unsigned int const i) { return std::make_pair(i + 1, i + 1); } + + private: + unsigned int count_; }; class copy_temperature_once { @@ -82,15 +106,15 @@ TEST_CASE("Querying products in different ways", "[graph]") // Register providers g.provide("provide_number_in_job", provide_number, concurrency::unlimited) - .output_product("input", "number", "job"); + .output_product("input", "number", "/job"); g.provide("provide_number_in_event", provide_idx, concurrency::unlimited) - .output_product("input", "evt_number", "event"); + .output_product("input", "evt_number", "/job/event"); g.provide("provide_temperature_in_event", provide_temperature, concurrency::unlimited) - .output_product("input", "temperature", "event"); + .output_product("input", "temperature", "/job/event"); g.provide("provide_temperature_in_event_again", provide_temperature, concurrency::unlimited) - .output_product("thermometer", "temperature", "event"); + .output_product("thermometer", "temperature", "/job/event"); g.provide("provide_name_in_event", provide_name, concurrency::unlimited) - .output_product("give_name", "name", "event"); + .output_product("give_name", "name", "/job/event"); // Duplicate with transform g.transform("duplicate_temperature", [](double const& t) { return t; }) @@ -254,3 +278,242 @@ TEST_CASE("Querying products in different ways", "[graph]") CHECK_THROWS_WITH(g.execute(), "Ambiguous product selector -- see errors"); } } + +TEST_CASE("Selecting by layer path segments beyond the leaf", "[graph]") +{ + constexpr unsigned runs = 2; + constexpr unsigned events_per_run = 3; + constexpr unsigned num_events = runs * events_per_run; + auto gen = experimental::layer_generator::make(); + gen->add_layer("run", {.parent_layer = "job", .count = runs}); + gen->add_layer("event", {.parent_layer = "run", .count = events_per_run}); + auto g = phlex::detail::framework_graph::without_driver("test"); + g.add_driver(gen); + g.add_source("archived_count_source", "/job/run/event"s); + g.provide("provide_number_in_event", provide_idx, concurrency::unlimited) + .output_product("input", "evt_number", "/job/run/event"); + + auto observe_with = [&g](std::string const& layer, bool from_source) { + auto selector = + from_source ? product_selector{.creator = "archived_input", .layer = layer} + : product_selector{.creator = "input", .layer = layer, .suffix = "evt_number"}; + g.observe("observer", [](int const&) {}, concurrency::unlimited).input_family(selector); + }; + + auto [layer, from_source] = GENERATE(table({{"event", false}, + {"run/event", false}, + {"/job/run/event", false}, + {"event", true}, + {"run/event", true}, + {"/job/run/event", true}})); + CAPTURE(layer, from_source); + observe_with(layer, from_source); + g.execute(); + CHECK(g.execution_count("observer") == num_events); +} + +namespace { + void add_ints(std::atomic& total, int const value) { total += value; } +} + +// The observer's selector goes beyond the leaf layer for products made by an upstream +// transform, fold, or unfold. Providers are always selected by their leaf layer here. +TEST_CASE("Selecting by layer path segments after a computed product", "[graph]") +{ + constexpr unsigned runs = 2; + constexpr unsigned subruns_per_run = 2; + constexpr unsigned events_per_subrun = 2; + constexpr unsigned num_subruns = runs * subruns_per_run; + constexpr unsigned num_events = num_subruns * events_per_subrun; + + auto gen = experimental::layer_generator::make(); + gen->add_layer("run", {.parent_layer = "job", .count = runs}); + gen->add_layer("subrun", {.parent_layer = "run", .count = subruns_per_run}); + gen->add_layer("event", {.parent_layer = "subrun", .count = events_per_subrun}); + auto g = phlex::detail::framework_graph::without_driver("test"); + g.add_driver(gen); + g.provide("provide_idx", provide_idx, concurrency::unlimited) + .output_product("input", "number", "/job/run/subrun/event"); + g.provide("provide_temperature", provide_temperature, concurrency::unlimited) + .output_product("input", "temperature", "/job/run/subrun/event"); + + SECTION("Transform output") + { + g.transform("plus_one", [](int const i) { return i + 1; }) + .input_family(product_selector{.creator = "input", .layer = "event", .suffix = "number"}) + .output_product_suffixes("incremented"); + auto const layer = + GENERATE("event", "subrun/event", "run/subrun/event", "/job/run/subrun/event"); + CAPTURE(layer); + g.observe( + "observer", [](int const) {}, concurrency::unlimited) + .input_family(product_selector{.creator = "plus_one", .layer = layer}); + g.execute(); + CHECK(g.execution_count("observer") == num_events); + } + + SECTION("Fold output") + { + g.fold("sum_events", add_ints, concurrency::unlimited, "subrun") + .input_family(product_selector{.creator = "input", .layer = "event", .suffix = "number"}) + .output_product_suffixes("sum"); + auto const layer = GENERATE("subrun", "run/subrun", "/job/run/subrun"); + CAPTURE(layer); + g.observe( + "observer", [](int const) {}, concurrency::unlimited) + .input_family(product_selector{.creator = "sum_events", .layer = layer}); + g.execute(); + CHECK(g.execution_count("observer") == num_subruns); + } + + SECTION("Unfold output") + { + g.unfold("copy_once", + ©_temperature_once::predicate, + ©_temperature_once::unfold, + concurrency::unlimited, + "chunk") + .input_family(product_selector{.creator = "input", .layer = "event", .suffix = "temperature"}) + .output_product_suffixes("copy"); + auto const layer = + GENERATE("chunk", "event/chunk", "subrun/event/chunk", "/job/run/subrun/event/chunk"); + CAPTURE(layer); + g.observe( + "observer", [](double const) {}, concurrency::unlimited) + .input_family(product_selector{.creator = "copy_once", .layer = layer}); + g.execute(); + CHECK(g.execution_count("observer") == num_events); + } + + SECTION("Selectors that do not match the computed product's path deliver nothing") + { + g.transform("plus_one", [](int const i) { return i + 1; }) + .input_family(product_selector{.creator = "input", .layer = "event", .suffix = "number"}) + .output_product_suffixes("incremented"); + g.fold("sum_events", add_ints, concurrency::unlimited, "subrun") + .input_family(product_selector{.creator = "input", .layer = "event", .suffix = "number"}) + .output_product_suffixes("sum"); + auto const [creator, layer] = GENERATE(table({ + {"plus_one", "spill/event"}, // leaf matches, parent segment differs + {"plus_one", "run/event"}, // skips subrun + {"plus_one", "/job/subrun/event"}, // complete path that skips run + {"plus_one", "/job/run/subrun"}, // wrong leaf + {"sum_events", "spill/subrun"}, + {"sum_events", "/job/run/subrun/event"}, // longer than the fold's layer path + })); + CAPTURE(creator, layer); + g.observe( + "observer", [](int const) {}, concurrency::unlimited) + .input_family(product_selector{.creator = creator, .layer = layer}); + // The graph is accepted, but no product reaches the observer. + g.execute(); + CHECK(g.execution_count("observer") == 0); + } +} + +TEST_CASE("Explicit providers require complete output layer paths", "[graph]") +{ + auto g = phlex::detail::framework_graph::without_driver("test"); + for (auto const* incomplete : {"event", "run/event"}) { + CAPTURE(incomplete); + CHECK_THROWS_WITH(g.provide("provide_number", provide_number, concurrency::unlimited) + .output_product("input", "number", incomplete), + Catch::Matchers::ContainsSubstring("must be a complete layer path")); + } +} + +// The hierarchy has two branches that end in "event": /job/spill/event with 3 cells and +// /job/run/event with 4 cells. A multi-segment selector must pick out one branch for every +// kind of node that registers ports with the index router. +TEST_CASE("Selecting one of two branches that share a leaf layer name", "[graph]") +{ + constexpr unsigned spill_events = 3; + constexpr unsigned run_events = 4; + + std::ostringstream output; + auto logger = test::use_ostream_logger(output); + auto const run_graph = [&](auto const& configure) { + auto gen = experimental::layer_generator::make(); + gen->add_layer("spill", {.parent_layer = "job", .count = 1}); + gen->add_layer("event", {.parent_layer = "spill", .count = spill_events}); + gen->add_layer("run", {.parent_layer = "job", .count = 1}); + gen->add_layer("event", {.parent_layer = "run", .count = run_events}); + auto g = phlex::detail::framework_graph::without_driver("test"); + g.add_driver(gen); + g.provide("provide_spill_idx", provide_idx, concurrency::unlimited) + .output_product("input", "number", "/job/spill/event"); + g.provide("provide_run_idx", provide_idx, concurrency::unlimited) + .output_product("input", "number", "/job/run/event"); + g.provide("provide_run_number", provide_number, concurrency::unlimited) + .output_product("input", "run_number", "/job/run"); + configure(g); + g.execute(); + return g.execution_count("consumer"); + }; + + SECTION("Fold") + { + int sum = 0; + int num_sums = 0; + auto const num_folds = run_graph([&](auto& g) { + g.fold("consumer", add_ints, concurrency::unlimited) + .input_family( + product_selector{.creator = "input", .layer = "run/event", .suffix = "number"}) + .output_product_suffixes("sum"); + g.observe( + "observer", + [&](int const total) { + sum = total; + ++num_sums; + }, + concurrency::serial) + .input_family(product_selector{.creator = "consumer", .layer = "job"}); + }); + CHECK(num_folds == run_events); + CHECK(num_sums == 1); + CHECK(sum == 0 + 1 + 2 + 3); + } + + SECTION("Unfold") + { + auto const num_unfolds = run_graph([](auto& g) { + g.provide( + "provide_run_count", [](data_cell_index const&) { return 2u; }, concurrency::unlimited) + .output_product("input", "count", "/job/run/event"); + g.template unfold( + "consumer", &count_up::predicate, &count_up::unfold, concurrency::unlimited, "hit") + .input_family(product_selector{.creator = "input", .layer = "run/event", .suffix = "count"}) + .output_product_suffixes("seed"); + }); + CHECK(num_unfolds == run_events); + } + + SECTION("Two-input join of a parent and a child layer") + { + auto const num_calls = run_graph([](auto& g) { + g.observe( + "consumer", [](int const, int const) {}, concurrency::unlimited) + .input_family( + product_selector{.creator = "input", .layer = "/job/run", .suffix = "run_number"}, + product_selector{.creator = "input", .layer = "run/event", .suffix = "number"}); + }); + CHECK(num_calls == run_events); + } + + SECTION("Two inputs naming the same layer with selectors of different lengths") + { + auto const num_calls = run_graph([](auto& g) { + g.provide("provide_run_temperature", provide_temperature, concurrency::unlimited) + .output_product("input", "temperature", "/job/run/event"); + g.observe( + "consumer", [](int const, double const) {}, concurrency::unlimited) + .input_family( + product_selector{.creator = "input", .layer = "event", .suffix = "number"}, + product_selector{.creator = "input", .layer = "run/event", .suffix = "temperature"}); + }); + CHECK(num_calls == run_events); + } + + // The graph is destroyed, so any entries left in repeaters have been reported. + CHECK_FALSE(output.str().contains("Cached")); +} diff --git a/test/product_selector_test.cpp b/test/product_selector_test.cpp index 4eab694e2..e3ab422a9 100644 --- a/test/product_selector_test.cpp +++ b/test/product_selector_test.cpp @@ -25,15 +25,14 @@ TEST_CASE("Creator name formatting", "[data model]") TEST_CASE("Empty layer name", "[data model]") { - CHECK_THROWS_WITH( - (product_selector{.creator = "creator", .layer = ""}), - Catch::Matchers::ContainsSubstring("Cannot specify the empty string as a data layer.")); + CHECK_THROWS_WITH((product_selector{.creator = "creator", .layer = ""}), + Catch::Matchers::ContainsSubstring("Layer paths cannot be empty")); } TEST_CASE("Retrieving a non-existent layer name", "[data model]") { product_selector const selector; - CHECK_THROWS_WITH((static_cast(selector.layer)), + CHECK_THROWS_WITH((static_cast(selector.layer)), Catch::Matchers::ContainsSubstring( "Cannot retrieve layer from product_selector with no layer")); } @@ -42,7 +41,7 @@ TEST_CASE("Product name with data layer", "[data model]") { product_selector label({.creator = "creator", .layer = "event", .suffix = "product"}); CHECK(*label.creator == "creator"_idq); - CHECK(*label.layer == "event"_idq); + CHECK(label.layer->leaf() == "event"_idq); CHECK(label.suffix == "product"_id); // Mismatched creator CHECK(!product_selector{.creator = "1", .layer = "event", .suffix = "prod"}.match( @@ -89,3 +88,56 @@ TEST_CASE("Selectors do not move from lvalue arguments", "[data model]") CHECK(layer_string == "event"); CHECK(from_string.layer == second.layer); } + +TEST_CASE("Selecting by a multi-segment layer path", "[data model]") +{ + experimental::product_specification const spec{ + "creator", "prod"_id, experimental::make_type_id()}; + experimental::identifier const stage{"stage"}; + experimental::layer_path const full{"/job/run/subrun/event"}; + + SECTION("Selector with the leaf layer only") + { + CHECK(product_selector{.layer = "event"}.match(spec, full, stage)); + } + SECTION("Selector with a trailing segment sequence") + { + CHECK(product_selector{.layer = "subrun/event"}.match(spec, full, stage)); + CHECK(product_selector{.layer = "run/subrun/event"}.match(spec, full, stage)); + } + SECTION("Selector with the complete path") + { + CHECK(product_selector{.layer = "/job/run/subrun/event"}.match(spec, full, stage)); + } + SECTION("Selector whose leaf matches but parent segment differs") + { + CHECK(!product_selector{.layer = "spill/event"}.match(spec, full, stage)); + CHECK(!product_selector{.layer = "run/event"}.match(spec, full, stage)); + } + SECTION("Selector with a non-trailing segment only") + { + CHECK(!product_selector{.layer = "run/subrun"}.match(spec, full, stage)); + } + SECTION("Selector longer than the layer path") + { + CHECK(!product_selector{.layer = "/job/extra/run/subrun/event"}.match(spec, full, stage)); + } + SECTION("Complete selector path that differs from the layer path") + { + CHECK(!product_selector{.layer = "/job/subrun/event"}.match(spec, full, stage)); + } + SECTION("Distinct layers sharing a leaf name") + { + experimental::layer_path const other{"/job/mc_sample/event"}; + product_selector const by_run{.layer = "run/event"}; + CHECK(!by_run.match(spec, other, stage)); + CHECK(product_selector{.layer = "mc_sample/event"}.match(spec, other, stage)); + } + SECTION("Selector-to-selector matching") + { + product_selector const other{.layer = "/job/run/subrun/event"}; + CHECK(product_selector{.layer = "subrun/event"}.match(other)); + CHECK(!product_selector{.layer = "spill/event"}.match(other)); + CHECK(!product_selector{.layer = "run/subrun"}.match(other)); + } +} diff --git a/test/provider_test.cpp b/test/provider_test.cpp index f340a4203..c6e3ae87f 100644 --- a/test/provider_test.cpp +++ b/test/provider_test.cpp @@ -106,7 +106,7 @@ TEST_CASE("Explicit providers") g.add_driver(gen); g.provide("my_name_here", give_me_vertices, concurrency::unlimited) - .output_product("vertices_maker", "happy_vertices", "spill"); + .output_product("vertices_maker", "happy_vertices", "/job/spill"); g.transform("passer", pass_on, concurrency::unlimited) .input_family( @@ -137,7 +137,7 @@ TEST_CASE("Specifying current stage does not match a provider from another stage { auto g = phlex::detail::framework_graph::with_default_driver("test"); g.provide("provide_previous_vertices", give_me_vertices, concurrency::unlimited) - .output_product("vertices_maker", "happy_vertices", "job", "previous_process"); + .output_product("vertices_maker", "happy_vertices", "/job", "previous_process"); std::string_view stage_name; SECTION("Specify named current stage") { stage_name = "test"; } @@ -161,9 +161,9 @@ TEST_CASE("Explicit Provider Ambiguity") // Register two providers that can provide the same product g.provide("provide_vertices", give_me_vertices, concurrency::unlimited) - .output_product("vertices_maker", "happy_vertices", "job"); + .output_product("vertices_maker", "happy_vertices", "/job"); g.provide("provide_vertices_again", give_me_vertices, concurrency::unlimited) - .output_product("vertices_maker", "happy_vertices", "job"); + .output_product("vertices_maker", "happy_vertices", "/job"); g.transform("passer", pass_on, concurrency::unlimited) .input_family( @@ -172,7 +172,7 @@ TEST_CASE("Explicit Provider Ambiguity") CHECK_THROWS_WITH( g.execute(), ContainsSubstring("Multiple explicit providers found for (id.number() % 2); }) - .output_product("input", "i", "event"); + .output_product("input", "i", "/job/event"); s.provide("provide_j", [](data_cell_index const& id) -> int { return 1 - static_cast(id.number() % 2); }) - .output_product("input", "j", "event"); + .output_product("input", "j", "/job/event"); s.provide("provide_k", [](data_cell_index const&) -> int { return 0; }) - .output_product("input", "k", "event"); + .output_product("input", "k", "/job/event"); s.provide("provide_f1", [](data_cell_index const& id) -> float { return static_cast(id.number() % 100u) / 100.0f; }) - .output_product("input", "f1", "event"); + .output_product("input", "f1", "/job/event"); s.provide("provide_f2", [](data_cell_index const& id) -> float { return 1.0f - (static_cast(id.number() % 100u) / 100.0f); }) - .output_product("input", "f2", "event"); + .output_product("input", "f2", "/job/event"); s.provide("provide_d1", [](data_cell_index const& id) -> double { return static_cast(id.number() % 100u) / 100.0; }) - .output_product("input", "d1", "event"); + .output_product("input", "d1", "/job/event"); s.provide("provide_d2", [](data_cell_index const& id) -> double { return 1.0 - (static_cast(id.number() % 100u) / 100.0); }) - .output_product("input", "d2", "event"); + .output_product("input", "d2", "/job/event"); s.provide("provide_u1", [](data_cell_index const& id) -> unsigned int { return static_cast(id.number() % 2); }) - .output_product("input", "u1", "event"); + .output_product("input", "u1", "/job/event"); s.provide("provide_u2", [](data_cell_index const& id) -> unsigned int { return 1u - static_cast(id.number() % 2); }) - .output_product("input", "u2", "event"); + .output_product("input", "u2", "/job/event"); s.provide("provide_l1", [](data_cell_index const& id) -> long { return static_cast(id.number() % 2); }) - .output_product("input", "l1", "event"); + .output_product("input", "l1", "/job/event"); s.provide( "provide_l2", [](data_cell_index const& id) -> long { return 1L - static_cast(id.number() % 2); }) - .output_product("input", "l2", "event"); + .output_product("input", "l2", "/job/event"); s.provide("provide_ul1", [](data_cell_index const& id) -> unsigned long { return static_cast(id.number() % 101); }) - .output_product("input", "ul1", "event"); + .output_product("input", "ul1", "/job/event"); s.provide("provide_ul2", [](data_cell_index const& id) -> unsigned long { return 100ul - static_cast(id.number() % 101); }) - .output_product("input", "ul2", "event"); + .output_product("input", "ul2", "/job/event"); s.provide("provide_b1", [](data_cell_index const& id) -> bool { return (id.number() % 2) == 0; }) - .output_product("input", "b1", "event"); + .output_product("input", "b1", "/job/event"); s.provide("provide_b2", [](data_cell_index const& id) -> bool { return (id.number() % 2) != 0; }) - .output_product("input", "b2", "event"); + .output_product("input", "b2", "/job/event"); } diff --git a/test/repeater_node_test.cpp b/test/repeater_node_test.cpp index b7fb4af95..0d5263e6c 100644 --- a/test/repeater_node_test.cpp +++ b/test/repeater_node_test.cpp @@ -74,7 +74,7 @@ namespace { class repeater_test_fixture { public: explicit repeater_test_fixture(std::string const& node_name) : - repeater_{g_, node_name, "run"_id}, consumer_{g_} + repeater_{g_, node_name, "run"}, consumer_{g_} { make_edge(repeater_, consumer_); } @@ -299,8 +299,8 @@ TEST_CASE("Test warning message if there are cached messages", "[multithreading] // new scope, we create the repeater_nopde as a unique_ptr and then reset it at the end of // the test, which will invoke the destructor. tbb::flow::graph g; - auto repeater = std::make_unique( - g, "test_repeater_warning_on_cached_messages", "run"_id); + auto repeater = + std::make_unique(g, "test_repeater_warning_on_cached_messages", "run"); SECTION("Cached data product") { diff --git a/test/resources_test.cpp b/test/resources_test.cpp index 8a525ae67..9da018477 100644 --- a/test/resources_test.cpp +++ b/test/resources_test.cpp @@ -220,7 +220,7 @@ TEST_CASE("registering a pointer resource with the graph", "[graph][resource]") g.provide( "number_maker", [](data_cell_index const&) { return 42; }, concurrency::unlimited) - .output_product("number_maker", "", "job"); + .output_product("number_maker", "", "/job"); auto counter = thread_counter::counter_type{}; // Catch2 assertions are not thread-safe, so each observer records its diff --git a/test/type_distinction_test.cpp b/test/type_distinction_test.cpp index 607d00fbd..ab3c48db8 100644 --- a/test/type_distinction_test.cpp +++ b/test/type_distinction_test.cpp @@ -51,9 +51,9 @@ TEST_CASE("Distinguish products with same name and different types", "[programmi // Register providers g.provide("provide_numbers", provide_numbers, concurrency::unlimited) - .output_product("input", "numbers", "event"); + .output_product("input", "numbers", "/job/event"); g.provide("provide_length", provide_length, concurrency::unlimited) - .output_product("input", "length", "event"); + .output_product("input", "length", "/job/event"); SECTION("Duplicate product name but differ in creator name") { diff --git a/test/unfold_test.cpp b/test/unfold_test.cpp index 3c849b306..7669eeefd 100644 --- a/test/unfold_test.cpp +++ b/test/unfold_test.cpp @@ -147,9 +147,9 @@ TEST_CASE("Splitting the processing", "[graph]") g.add_driver(gen); g.provide("provide_max_number", provide_max_number, concurrency::unlimited) - .output_product("input", "max_number", "event"); + .output_product("input", "max_number", "/job/event"); g.provide("provide_ten_numbers", provide_ten_numbers, concurrency::unlimited) - .output_product("input", "ten_numbers", "event"); + .output_product("input", "ten_numbers", "/job/event"); g.unfold("iota", &iota::predicate, &iota::unfold, concurrency::unlimited, "lower1") .input_family(product_selector{.creator = "input", .layer = "event", .suffix = "max_number"}) @@ -229,7 +229,7 @@ TEST_CASE("Multi-layer transform with one input from an unfold", "[graph]") g.add_driver(gen); g.provide("provide_max_number", provide_max_number, concurrency::unlimited) - .output_product("input", "max_number", "event"); + .output_product("input", "max_number", "/job/event"); g.unfold("iota", &iota::predicate, &iota::unfold, concurrency::unlimited, "subevent") .input_family(product_selector{.creator = "input", .layer = "event", .suffix = "max_number"}) @@ -262,9 +262,9 @@ TEST_CASE("Unfold deduplicates same-layer inputs for bookkeeping", "[graph]") g.add_driver(gen); g.provide("provide_max_number", provide_max_number, concurrency::unlimited) - .output_product("input", "max_number", "event"); + .output_product("input", "max_number", "/job/event"); g.provide("provide_ten_numbers", provide_ten_numbers, concurrency::unlimited) - .output_product("input", "ten_numbers", "event"); + .output_product("input", "ten_numbers", "/job/event"); g.unfold("iota_two_inputs", &iota_two_inputs::predicate, @@ -304,7 +304,7 @@ TEST_CASE("Unfold receives a resource token", "[graph][unfold][resource]") g.provide( "provide_max_number", [](data_cell_index const&) { return 10u; }, concurrency::unlimited) - .output_product("input", "max_number", "event"); + .output_product("input", "max_number", "/job/event"); g.unfold("resource_iota", &resource_iota::predicate, diff --git a/test/vector_of_abstract_types_test.cpp b/test/vector_of_abstract_types_test.cpp index c5836c15f..e7e365086 100644 --- a/test/vector_of_abstract_types_test.cpp +++ b/test/vector_of_abstract_types_test.cpp @@ -48,7 +48,7 @@ TEST_CASE("Test vector of abstract types") auto g = phlex::detail::framework_graph::without_driver("test"); g.add_driver(gen); g.provide("provide_thing", [](data_cell_index const&) { return make_derived_as_abstract(); }) - .output_product("dummy", "thing", "event"); + .output_product("dummy", "thing", "/job/event"); g.transform("read_thing", read_abstract) .input_family(product_selector{.creator = "dummy", .layer = "event", .suffix = "thing"}) .output_product_suffixes("sum");