2 * Copyright (c) 2020-2022 Hailo Technologies Ltd. All rights reserved.
3 * Distributed under the MIT license (https://opensource.org/licenses/MIT)
6 * @file raw_async_streams_multi_thread_example
7 * This example demonstrates using low level async streams over c++
10 #include "hailo/hailort.hpp"
19 constexpr auto TIMEOUT = std::chrono::milliseconds(1000);
21 using namespace hailort;
23 using AlignedBuffer = std::shared_ptr<uint8_t>;
24 static AlignedBuffer page_aligned_alloc(size_t size)
27 auto addr = mmap(NULL, size, PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
28 if (MAP_FAILED == addr) throw std::bad_alloc();
29 return AlignedBuffer(reinterpret_cast<uint8_t*>(addr), [size](void *addr) { munmap(addr, size); });
30 #elif defined(_MSC_VER)
31 auto addr = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
32 if (!addr) throw std::bad_alloc();
33 return AlignedBuffer(reinterpret_cast<uint8_t*>(addr), [](void *addr){ VirtualFree(addr, 0, MEM_RELEASE); });
35 #pragma error("Aligned alloc not supported")
39 Expected<std::shared_ptr<ConfiguredNetworkGroup>> configure_network_group(Device &device, const std::string &hef_path)
41 auto hef = Hef::create(hef_path);
43 return make_unexpected(hef.status());
46 auto configure_params = device.create_configure_params(hef.value());
47 if (!configure_params) {
48 return make_unexpected(configure_params.status());
51 // change stream_params here
52 for (auto &ng_name_params_pair : *configure_params) {
53 for (auto &stream_params_name_pair : ng_name_params_pair.second.stream_params_by_name) {
54 stream_params_name_pair.second.flags = HAILO_STREAM_FLAGS_ASYNC;
58 auto network_groups = device.configure(hef.value(), configure_params.value());
59 if (!network_groups) {
60 return make_unexpected(network_groups.status());
63 if (1 != network_groups->size()) {
64 std::cerr << "Invalid amount of network groups" << std::endl;
65 return make_unexpected(HAILO_INTERNAL_FAILURE);
68 return std::move(network_groups->at(0));
71 static void output_async_callback(const OutputStream::CompletionInfo &completion_info)
73 // Real applications can free the buffer or forward it to post-process/display.
74 if ((HAILO_SUCCESS != completion_info.status) && (HAILO_STREAM_ABORTED_BY_USER != completion_info.status)) {
75 // We will get HAILO_STREAM_ABORTED_BY_USER when activated_network_group is destructed.
76 std::cerr << "Got an unexpected status on callback. status=" << completion_info.status << std::endl;
80 static void input_async_callback(const InputStream::CompletionInfo &completion_info)
82 // Real applications can free the buffer or reuse it for next transfer.
83 if ((HAILO_SUCCESS != completion_info.status) && (HAILO_STREAM_ABORTED_BY_USER != completion_info.status)) {
84 // We will get HAILO_STREAM_ABORTED_BY_USER when activated_network_group is destructed.
85 std::cerr << "Got an unexpected status on callback. status=" << completion_info.status << std::endl;
91 auto device = Device::create();
93 std::cerr << "Failed create device " << device.status() << std::endl;
97 static const auto HEF_FILE = "hefs/shortcut_net.hef";
98 auto network_group = configure_network_group(*device.value(), HEF_FILE);
100 std::cerr << "Failed to configure network group " << HEF_FILE << std::endl;
104 // Assume one input and output
105 auto &output = network_group->get()->get_output_streams()[0].get();
106 auto &input = network_group->get()->get_input_streams()[0].get();
108 // Allocate buffers. The buffers sent to the async API must be page aligned.
109 // For simplicity, in this example, we pass one buffer for each stream (It may be problematic in output since the
110 // buffer will be overridden on each read).
111 // Note - the buffers are allocated before we activate the network group. This will ensure that they won't be freed
112 // until the network group will become inactive.
113 auto output_buffer = page_aligned_alloc(output.get_frame_size());
114 auto input_buffer = page_aligned_alloc(input.get_frame_size());
116 // The destructor of activated_network_group will make sure that all async operations are done. All pending
117 // operations will be canceled and their callbacks will be called with status=HAILO_STREAM_ABORTED_BY_USER.
118 // Be sure to capture variables in the callbacks that will be destructed after the activated_network_group.
119 // Otherwise, the lambda would have access an uninitialized data.
120 auto activated_network_group = network_group.value()->activate();
121 if (!activated_network_group) {
122 std::cerr << "Failed to activate network group " << activated_network_group.status() << std::endl;
126 std::atomic<hailo_status> output_status(HAILO_UNINITIALIZED);
127 std::thread output_thread([&]() {
129 output_status = output.wait_for_async_ready(output.get_frame_size(), TIMEOUT);
130 if (HAILO_SUCCESS != output_status) { return; }
132 output_status = output.read_async(output_buffer.get(), output.get_frame_size(), output_async_callback);
133 if (HAILO_SUCCESS != output_status) { return; }
137 std::atomic<hailo_status> input_status(HAILO_UNINITIALIZED);
138 std::thread input_thread([&]() {
140 input_status = input.wait_for_async_ready(input.get_frame_size(), TIMEOUT);
141 if (HAILO_SUCCESS != input_status) { return; }
143 input_status = input.write_async(input_buffer.get(), input.get_frame_size(), input_async_callback);
144 if (HAILO_SUCCESS != input_status) { return; }
148 // After all async operations are launched, the inference is running.
149 std::this_thread::sleep_for(std::chrono::seconds(5));
151 // Make it stop. We explicitly destruct activated_network_group to stop all async I/O.
152 activated_network_group->reset();
154 // Thread should be stopped with HAILO_STREAM_NOT_ACTIVATED status.
155 output_thread.join();
157 if ((HAILO_STREAM_NOT_ACTIVATED != output_status) || (HAILO_STREAM_NOT_ACTIVATED != input_status)) {
158 std::cerr << "Got unexpected statues from thread: " << output_status << ", " << input_status << std::endl;
162 std::cout << "Inference finished successfully" << std::endl;