-
Notifications
You must be signed in to change notification settings - Fork 3
Registering functions with Meld
Imagine the user has created a function that adds two numbers:
int add(int i, int j) { return i + j; }For simplicity, we assume this function has been declared in a header file add.hpp and that any code using it includes that header file and (possibly) links against the library that provides the function definition.
Now imagine that the user would like to incorporate the add function into a framework program. In order for this to succeed, the user must tell the framework:
- What function to call
- When to call it (i.e. which data-processing level)
- How to locate the data in the data-processing level that will serve as input arguments to the function
- What to do with the data that is created from the user's function
- The maximum allowed concurrency to use when calling the function
The difficulty in providing such information depends on the framework—we provide two examples below.
The Adder class listed below is the minimum amount of code required to use the add function as part of an art framework program. The code has been annotated with comments showing which aspects of the class satisfy the 5 criteria listed above.
#include "add.hpp"
#include "art/Framework/Core/SharedProducer.h"
#include "fhiclcpp/ParameterSet.h"
class Adder : public art::SharedProducer {
public:
Adder(fhicl::ParameterSet const& pset, art::ProcessingFrame const&) :
SharedProducer{pset},
i_token{consumes<int>("i")}, // ← 3
j_token{consumes<int>("j")} // ← 3
{
produces<int>("sum"); // ← 4
async<art::Event>(); // ← 5
}
void produce(art::Event& e, // ← 2
art::ProcessingFrame const&) override
{
// 1 3 3
// ↓ ↓ ↓
auto const sum = add(e.getProduct(i_token), e.getProduct(j_token));
e.put(std::make_unique<int>(sum), "sum");
// ↑
// 4
}
private:
art::ProductToken<int> i_token;
art::ProductToken<int> j_token;
};
DEFINE_ART_MODULE(Adder);The equivalent registration code with Meld is below.
#include "add.hpp"
#include "meld/module.hpp"
DEFINE_MODULE(m) {
using namespace meld::concurrency;
m.with(add).transform("i", "j").to("sum").using_concurrency(unlimited);
// ↑ ↑ ↑ ↑ ↑
// 1 3 3 4 5
}Observe that it is not strictly necessary to specify the data-processing level in which the products reside.