rexlib 0.1.0
Device-agnostic array computing library for CryoEM and CryoET image processing, with a pluggable backend architecture
Loading...
Searching...
No Matches
operand_shape_rule.hpp
1// SPDX-License-Identifier: GPL-3.0-only
2
3#pragma once
4
5#include <rexlib/core/operation/shape_rule/core_dimensions.hpp>
6#include <rexlib/core/operation/shape_rule/core_placement.hpp>
7#include <rexlib/core/operation/shape_rule/extent_pivot.hpp>
8#include <rexlib/core/operation/shape_rule/extent_slot.hpp>
9#include <rexlib/core/operation/shape_rule/output_batch.hpp>
10#include <rexlib/core/operation/shape_rule/same_extent_transform.hpp>
11#include <rexlib/core/operation/shape_rule/trailing_core.hpp>
12
13#include <rexlib/core/meta/type_list.hpp>
14#include <rexlib/core/platform/constexpr.hpp>
15
16#include <cstddef>
17#include <type_traits>
18#include <utility>
19
20namespace rexlib
21{
22
23namespace detail
24{
25
26template <bool... Values>
28
29template <bool... Values>
31 : std::is_same<
32 shape_rule_bool_pack<true, Values...>,
33 shape_rule_bool_pack<Values..., true>
34 >
35{
36};
37
38template <bool... Values>
40 : std::integral_constant<bool, !shape_rule_all_of<(!Values)...>::value>
41{
42};
43
44template <std::size_t PivotCount, typename SlotList>
46
47template <std::size_t PivotCount, typename... Slots>
49 : shape_rule_all_of<(Slots::pivot_index < PivotCount)...>
50{
51};
52
53template <std::size_t PivotCount, typename Core>
54struct core_names_declared_extents
55 : slots_name_declared_pivots<PivotCount, typename Core::slot_list_type>
56{
57};
58
59template <std::size_t PivotCount, typename Cores>
60struct cores_name_declared_extents;
61
62template <std::size_t PivotCount, typename... Cores>
63struct cores_name_declared_extents<PivotCount, type_list<Cores...>>
64 : shape_rule_all_of<
65 core_names_declared_extents<PivotCount, Cores>::value...
66 >
67{
68};
69
70template <std::size_t Extent, typename SlotList>
71struct slots_bind_extent;
72
73template <std::size_t Extent, typename... Slots>
74struct slots_bind_extent<Extent, type_list<Slots...>>
75 : shape_rule_any_of<(
76 Slots::pivot_index == Extent &&
77 std::is_same<
78 typename Slots::transform_type,
79 same_extent_transform
80 >::value
81 )...>
82{
83};
84
85template <std::size_t Extent, typename Core>
86struct core_binds_extent
87 : slots_bind_extent<Extent, typename Core::slot_list_type>
88{
89};
90
91template <std::size_t Extent, typename Cores>
92struct cores_bind_extent;
93
94template <std::size_t Extent, typename... Cores>
95struct cores_bind_extent<Extent, type_list<Cores...>>
96 : shape_rule_any_of<core_binds_extent<Extent, Cores>::value...>
97{
98};
99
100template <typename Extents, typename InputCores, typename Indices>
101struct input_extents_bound;
102
103template <typename... Extents, typename InputCores, std::size_t... Indices>
104struct input_extents_bound<
105 type_list<Extents...>,
106 InputCores,
107 std::index_sequence<Indices...>
108>
109 : shape_rule_all_of<(
110 Extents::source != extent_pivot_source::inputs ||
111 cores_bind_extent<Indices, InputCores>::value
112 )...>
113{
114};
115
116template <typename Extents>
117struct extents_from_operation;
118
119template <typename... Extents>
120struct extents_from_operation<type_list<Extents...>>
121 : shape_rule_any_of<(Extents::source == extent_pivot_source::operation)...>
122{
123};
124
125template <typename Cores>
126struct cores_without_run;
127
128template <typename... Cores>
129struct cores_without_run<type_list<Cores...>>
130 : shape_rule_all_of<(!Cores::has_run)...>
131{
132};
133
134template <typename Cores>
135struct cores_empty;
136
137template <typename... Cores>
138struct cores_empty<type_list<Cores...>>
139 : shape_rule_all_of<(Cores::axis_count == 0 && !Cores::has_run)...>
140{
141};
142
143template <typename Core>
144struct core_carries_axes
145 : std::integral_constant<bool, (Core::axis_count > 0 || Core::has_run)>
146{
147};
148
149template <typename Cores>
151
152template <typename... Cores>
154 : shape_rule_any_of<core_carries_axes<Cores>::value...>
155{
156};
157
158template <typename Cores>
160{
161 using type = void;
162};
163
164template <typename First, typename... Rest>
166{
167 using type = typename std::conditional<
169 First,
170 typename first_core_carrying_axes<type_list<Rest...>>::type
171 >::type;
172};
173
174template <std::size_t AxisCount, bool HasRun, typename Cores>
176
177template <std::size_t AxisCount, bool HasRun, typename... Cores>
180 (Cores::axis_count == 0 && !Cores::has_run) ||
181 (Cores::axis_count == AxisCount && Cores::has_run == HasRun)
182 )...>
183{
184};
185
186template <typename Reference, typename Inputs, typename Outputs>
188 : std::integral_constant<
189 bool,
190 cores_empty_or_alike<
191 Reference::axis_count,
192 Reference::has_run,
193 Inputs
194 >::value &&
195 cores_empty_or_alike<
196 Reference::axis_count,
197 Reference::has_run,
198 Outputs
199 >::value
200 >
201{
202};
203
204template <typename Inputs, typename Outputs>
206 : std::true_type
207{
208};
209
210template <typename Outputs, typename Inputs, typename Indices>
212
213template <typename Outputs, typename Inputs, std::size_t... Indices>
215 : shape_rule_all_of<std::is_same<
216 typename type_list_element<Indices, Outputs>::type,
217 typename type_list_element<Indices, Inputs>::type
218 >::value...>
219{
220};
221
222template <
223 typename Outputs,
224 typename Inputs,
225 bool Enough = (
227 )
228>
230 : std::false_type
231{
232};
233
234template <typename Outputs, typename Inputs>
237 Outputs,
238 Inputs,
239 std::make_index_sequence<type_list_size<Outputs>::value>
240 >
241{
242};
243
244template <typename Inputs, typename Outputs>
247 typename first_core_carrying_axes<Inputs>::type,
248 Inputs,
249 Outputs
250 >
251{
252};
253
254} // namespace detail
255
280template <
281 typename Pivots,
282 typename Outputs,
283 typename Inputs,
284 typename Placement = trailing_core,
286>
288{
289 using pivot_list_type = Pivots;
293
294 static REXLIB_CONST_CONSTEXPR output_batch batch = Batch;
295
296 static REXLIB_CONST_CONSTEXPR std::size_t pivot_count =
298 static REXLIB_CONST_CONSTEXPR std::size_t output_count =
300 static REXLIB_CONST_CONSTEXPR std::size_t input_count =
302
306 static REXLIB_CONST_CONSTEXPR bool requires_parameters =
307 Placement::placement == core_placement::named ||
309
310 static_assert(
311 pivot_count <= max_extent_pivot_count,
312 "A shape rule declares more extent variables than a resolution can "
313 "hold. Raise max_extent_pivot_count if this is genuinely needed."
314 );
315
316 static_assert(
319 "A core axis names an extent variable the rule does not declare."
320 );
321
322 static_assert(
324 Pivots,
325 Inputs,
326 std::make_index_sequence<pivot_count>
327 >::value,
328 "An extent variable bound by the inputs is not carried untransformed "
329 "by any input axis, so nothing determines it."
330 );
331
332 static_assert(
333 Placement::placement == core_placement::named ||
334 (
337 ),
338 "A run is as long as the axes the operation instance names, so only "
339 "a rule whose core axes are named may declare one."
340 );
341
342 static_assert(
343 Placement::placement != core_placement::named ||
345 "A rule whose core axes are named needs an input carrying them."
346 );
347
348 static_assert(
349 Placement::placement != core_placement::named ||
351 "Under named placement every core carrying the named axes must "
352 "declare as many axes and runs as the others."
353 );
354
355 static_assert(
357 (
358 Placement::placement == core_placement::trailing &&
361 ),
362 "Only an operation without core axes may write into outputs larger "
363 "than its inputs broadcast to."
364 );
365
366 static_assert(
368 (
369 Placement::placement == core_placement::trailing &&
371 ),
372 "An output accumulating into an input needs core axes that lie last "
373 "and an input at its position declaring the same core."
374 );
375};
376
377} // namespace rexlib
Container for multidimensional numeric data.
Definition array.hpp:31
Arrays, the operations on them and the devices that run those operations.
Definition cpu_device.hpp:10
REXLIB_INLINE_CONST_CONSTEXPR std::size_t max_extent_pivot_count
Maximum number of extent variables a shape rule may declare.
Definition extent_pivot.hpp:23
output_batch
Which batch axes an output supplied in advance may have.
Definition output_batch.hpp:12
@ exact
Exactly those the inputs broadcast to.
@ accumulated
Those of the input at the same position, its accumulator.
@ broadcast_target
Any the inputs broadcast to, shared by every output.
@ named
The axes the operation instance names.
@ trailing
The last axes of every operand.
Definition operand_shape_rule.hpp:146
Definition operand_shape_rule.hpp:231
Definition operand_shape_rule.hpp:201
Definition operand_shape_rule.hpp:150
Definition operand_shape_rule.hpp:175
Definition operand_shape_rule.hpp:135
Definition operand_shape_rule.hpp:60
Definition operand_shape_rule.hpp:211
Definition operand_shape_rule.hpp:126
Definition operand_shape_rule.hpp:117
Definition operand_shape_rule.hpp:160
Definition operand_shape_rule.hpp:101
Definition operand_shape_rule.hpp:251
Definition operand_shape_rule.hpp:35
Definition operand_shape_rule.hpp:41
Definition operand_shape_rule.hpp:27
Definition operand_shape_rule.hpp:45
Declarative shape rule for the operands of an operation.
Definition operand_shape_rule.hpp:288
static REXLIB_CONST_CONSTEXPR bool requires_parameters
Whether the rule reads values of the operation instance.
Definition operand_shape_rule.hpp:306
Placement of the core axes at the end of every operand.
Definition trailing_core.hpp:20
Utility class to obtain the size of a type_list.
Definition type_list.hpp:28
Helper class to pass a heterogeneous sequence of types to a function.
Definition type_list.hpp:20