CPPuddle
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
stream_test.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-2024 Gregor Daiß
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef STREAM_TEST_HPP // NOLINT
7#define STREAM_TEST_HPP // NOLINT
8#include <hpx/async_base/apply.hpp>
9#include <hpx/async_base/async.hpp>
10#include <hpx/execution_base/execution.hpp>
11#include <hpx/async_cuda/cuda_executor.hpp>
15
16template <typename Interface, typename Pool, typename... Ts>
17void test_pool_memcpy(const size_t executor_parameter, Ts &&...ts) {
18 std::vector<double,
19 cppuddle::memory_recycling::recycle_allocator_cuda_host<double>>
20 hostbuffer(512);
21 cppuddle::memory_recycling::cuda_device_buffer<double> devicebuffer(512);
22 cppuddle::executor_recycling::executor_pool::init<Interface, Pool>(
23 executor_parameter, std::forward<Ts>(ts)...);
24 // without interface wrapper
25 {
26 auto test1 =
27 cppuddle::executor_recycling::executor_pool::get_interface<Interface,
28 Pool>(0);
29 Interface test1_interface = std::get<0>(test1);
30 size_t interface_id = std::get<1>(test1);
31 hpx::apply(test1_interface, cudaMemcpyAsync,
32 devicebuffer.device_side_buffer, hostbuffer.data(),
33 512 * sizeof(double), cudaMemcpyHostToDevice);
34 auto fut1 = hpx::async(test1_interface, cudaMemcpyAsync, hostbuffer.data(),
35 devicebuffer.device_side_buffer,
36 512 * sizeof(double), cudaMemcpyDeviceToHost);
37 fut1.get();
38 cppuddle::executor_recycling::executor_pool::release_interface<Interface,
39 Pool>(
40 interface_id, 0);
41 }
42
43 // with interface wrapper
44 {
45 cppuddle::executor_recycling::executor_interface<Interface, Pool>
46 test1_interface{0};
47 // hpx::cuda::cuda_executor test1_interface(0, false);
48 hpx::apply(test1_interface.interface, cudaMemcpyAsync,
49 devicebuffer.device_side_buffer, hostbuffer.data(),
50 512 * sizeof(double), cudaMemcpyHostToDevice);
51 auto fut1 = hpx::async(test1_interface.interface, cudaMemcpyAsync,
52 hostbuffer.data(), devicebuffer.device_side_buffer,
53 512 * sizeof(double), cudaMemcpyDeviceToHost);
54 fut1.get();
55 }
56 cppuddle::executor_recycling::executor_pool::cleanup<Interface, Pool>();
57}
58
59template <typename Interface, typename Pool, typename... Ts>
60void test_pool_ref_counting(const size_t executor_parameter, Ts &&...ts) {
61
62 // init ppol
63 cppuddle::executor_recycling::executor_pool::init<Interface, Pool>(
64 executor_parameter, std::forward<Ts>(ts)...);
65 {
66 // Allocating
67 auto test1 =
68 cppuddle::executor_recycling::executor_pool::get_interface<Interface,
69 Pool>(0);
70 auto load1 =
71 cppuddle::executor_recycling::executor_pool::get_current_load<Interface,
72 Pool>(0);
73 assert(load1 == 0);
74 Interface test1_interface = std::get<0>(test1);
75 size_t test1_index = std::get<1>(test1);
76 auto test2 =
77 cppuddle::executor_recycling::executor_pool::get_interface<Interface,
78 Pool>(0);
79 auto load2 =
80 cppuddle::executor_recycling::executor_pool::get_current_load<Interface,
81 Pool>(0);
82 assert(load2 == 1);
83 Interface test2_interface = std::get<0>(test2);
84 // auto fut = test2_interface.get_future();
85 size_t test2_index = std::get<1>(test2);
86 auto test3 =
87 cppuddle::executor_recycling::executor_pool::get_interface<Interface,
88 Pool>(0);
89 auto load3 =
90 cppuddle::executor_recycling::executor_pool::get_current_load<Interface,
91 Pool>(0);
92 assert(load3 == 1);
93 Interface test3_interface = std::get<0>(test3);
94 size_t test3_index = std::get<1>(test3);
95 auto test4 =
96 cppuddle::executor_recycling::executor_pool::get_interface<Interface,
97 Pool>(0);
98 auto load4 =
99 cppuddle::executor_recycling::executor_pool::get_current_load<Interface,
100 Pool>(0);
101 Interface test4_interface = std::get<0>(test4);
102 size_t test4_index = std::get<1>(test4);
103 assert(load4 == 2);
104 // Releasing
105 cppuddle::executor_recycling::executor_pool::release_interface<Interface,
106 Pool>(
107 test4_index, 0);
108 load4 =
109 cppuddle::executor_recycling::executor_pool::get_current_load<Interface,
110 Pool>(0);
111 assert(load4 == 1);
112 cppuddle::executor_recycling::executor_pool::release_interface<Interface,
113 Pool>(
114 test3_index, 0);
115 load3 =
116 cppuddle::executor_recycling::executor_pool::get_current_load<Interface,
117 Pool>(0);
118 assert(load3 == 1);
119 cppuddle::executor_recycling::executor_pool::release_interface<Interface,
120 Pool>(
121 test2_index, 0);
122 load2 =
123 cppuddle::executor_recycling::executor_pool::get_current_load<Interface,
124 Pool>(0);
125 assert(load2 == 0);
126 cppuddle::executor_recycling::executor_pool::release_interface<Interface,
127 Pool>(
128 test1_index, 0);
129 load1 =
130 cppuddle::executor_recycling::executor_pool::get_current_load<Interface,
131 Pool>(0);
132 assert(load1 == 0);
133 }
134 // Clear
135 auto load0 =
136 cppuddle::executor_recycling::executor_pool::get_current_load<Interface,
137 Pool>(0);
138 assert(load0 == 0);
139 cppuddle::executor_recycling::executor_pool::cleanup<Interface, Pool>();
140}
141
142template <typename Interface, typename Pool, typename... Ts>
143void test_pool_wrappers(const size_t executor_parameter, Ts &&...ts) {
144 using wrapper_type =
145 cppuddle::executor_recycling::executor_interface<Interface, Pool>;
146 // init ppol
147 cppuddle::executor_recycling::executor_pool::init<Interface, Pool>(
148 executor_parameter, std::forward<Ts>(ts)...);
149 {
150 wrapper_type test1{0};
151 auto load =
152 cppuddle::executor_recycling::executor_pool::get_current_load<Interface,
153 Pool>(0);
154 assert(load == 0);
155 wrapper_type test2{0};
156 load =
157 cppuddle::executor_recycling::executor_pool::get_current_load<Interface,
158 Pool>(0);
159 // auto fut = test2.get_future();
160 assert(load == 1);
161 wrapper_type test3{0};
162 load =
163 cppuddle::executor_recycling::executor_pool::get_current_load<Interface,
164 Pool>(0);
165 assert(load == 1);
166 wrapper_type test4{0};
167 load =
168 cppuddle::executor_recycling::executor_pool::get_current_load<Interface,
169 Pool>(0);
170 assert(load == 2);
171 // Check availability method:
172 bool avail =
173 cppuddle::executor_recycling::executor_pool::interface_available<
174 Interface, Pool>(1, 0);
175 assert(avail == false); // NOLINT
176 avail = cppuddle::executor_recycling::executor_pool::interface_available<
177 Interface, Pool>(2, 0);
178 assert(avail == false); // NOLINT
179 avail = cppuddle::executor_recycling::executor_pool::interface_available<
180 Interface, Pool>(3, 0);
181 assert(avail == true); // NOLINT
182 }
183 auto load0 =
184 cppuddle::executor_recycling::executor_pool::get_current_load<Interface,
185 Pool>(0);
186 assert(load0 == 0);
187 cppuddle::executor_recycling::executor_pool::cleanup<Interface, Pool>();
188}
189
190#endif
Contains a RAII wrappers for CUDA device buffers. Intended to be used with the recycling allocators b...