Poplar and PopLibs
HostFunctionCallback.hpp
1// Copyright (c) 2021 Graphcore Ltd. All rights reserved.
2#ifndef poplar_HostCallback_hpp
3#define poplar_HostCallback_hpp
4
5#include <poplar/CallbackTraits.hpp>
6
7#include <memory>
8
9namespace poplar {
10
15public:
16 virtual ~HostCallback();
17
21 virtual void operator()(ArrayRef<const void *> inputs,
22 ArrayRef<void *> outputs) = 0;
23};
24
32public:
38 template <class CallbackImpl,
39 typename = typename std::enable_if<
40 std::is_base_of<HostCallback, CallbackImpl>::value>::type>
41 HostCallbackHandle(std::unique_ptr<CallbackImpl> f)
42 : callback(std::move(f)) {}
43
50 template <class F, typename = typename std::enable_if<
51 traits::is_host_callback<F>::value>::type>
52 HostCallbackHandle(F &&f) : callback(makeCallback(std::forward<F>(f))) {}
53
54 // Non copy constructible
55 HostCallbackHandle(const HostCallbackHandle &) = delete;
56
57 // Move constructible
59
60 // This forces the user to provide a non-empty callback and allows the
61 // internal implementation to only make use of std::unique_ptr<HostCallback>
62 // if necessary.
65 operator std::unique_ptr<HostCallback>() && { return std::move(callback); }
66
67private:
68 template <class F> std::unique_ptr<HostCallback> makeCallback(F &&f) {
69 struct Callback final : HostCallback {
70 using Function = typename traits::remove_cvref<F>::type;
71
72 Function function;
73
74 Callback(F &&f) : function(std::forward<F>(f)) {}
75
76 void operator()(ArrayRef<const void *> ins,
77 ArrayRef<void *> outs) override {
78 function(ins, outs);
79 }
80 };
81 return std::unique_ptr<HostCallback>(new Callback{std::forward<F>(f)});
82 }
83
84 std::unique_ptr<HostCallback> callback;
85};
86
87} // namespace poplar
88
89#endif // poplar_HostCallback_hpp
A reference to a function stored within a graph.
Definition: GraphElements.hpp:148
Wrapper for HostCallback instances.
Definition: HostFunctionCallback.hpp:31
HostCallbackHandle(F &&f)
Constructs a handle from a callable instance.
Definition: HostFunctionCallback.hpp:52
HostCallbackHandle(std::unique_ptr< CallbackImpl > f)
Constructs a handle from an instance of a host callback implementation.
Definition: HostFunctionCallback.hpp:41
Interface used during host function calls to produce/consume the data being exchanged between the hos...
Definition: HostFunctionCallback.hpp:14
virtual void operator()(ArrayRef< const void * > inputs, ArrayRef< void * > outputs)=0
Callback function that is invoked as a result of calling a host function in an IPU program.
Poplar classes and functions.
Definition: ArrayRef.hpp:14