libpressio 0.93.0
Loading...
Searching...
No Matches
subgroup_manager.h
Go to the documentation of this file.
1#ifndef LIBPRESSSIO_SUBGROUP_MANAGER
2#define LIBPRESSSIO_SUBGROUP_MANAGER
3#include "configurable.h"
4#include "std_compat/memory.h"
5#include "std_compat/span.h"
10#include "pressio_compressor.h"
11#include "pressio_data.h"
12#include "pressio_options.h"
13#include <cstddef>
14#include <memory>
15#include <numeric>
16#include <random>
17#include <vector>
18#include <set>
19
29public:
30 int set_options(const struct pressio_options &options) override {
31 pressio_data tmp;
32 if(get(options, "subgroups:input_data_groups", &tmp) == pressio_options_key_set) {
33 input_data_groups = tmp.to_vector<int>();
34 }
35 if(get(options, "subgroups:output_data_groups", &tmp) == pressio_options_key_set) {
36 output_data_groups = tmp.to_vector<int>();
37 }
38 return 0;
39 }
40 struct pressio_options get_documentation() const override {
41 pressio_options opts;
42 set(opts, "subgroups:input_data_groups", "which inputs get mapped to which sub compression operations");
43 set(opts, "subgroups:output_data_groups", "which outputs get mapped to which sub compression operations");
44 return opts;
45 }
46 struct pressio_options get_options() const override {
47 pressio_options opts;
48 set(opts, "subgroups:input_data_groups", pressio_data(std::begin(input_data_groups), std::end(input_data_groups)));
49 set(opts, "subgroups:output_data_groups", pressio_data(std::begin(output_data_groups), std::end(output_data_groups)));
50 return opts;
51 }
52
53 const char* prefix() const override {
54 return "subgroups";
55 }
56
63 template <class U, class V>
64 int normalize_and_validate(compat::span<U> const& inputs, compat::span<V> const& outputs) {
65 effective_input_group = normalize_data_group(input_data_groups, inputs.size());
66 effective_output_group = normalize_data_group(output_data_groups, outputs.size());
67
68 if(effective_input_group.size() != inputs.size()) {
69 return set_error(1, "invalid input group");
70 }
71 if(effective_output_group.size() != outputs.size()) {
72 return set_error(1, "invalid output group");
73 }
74 size_t num_groups = valid_data_groups(effective_input_group, effective_output_group);
75 if(num_groups == 0) {
76 return set_error(2, "invalid data groups");
77 }
78 return 0;
79 }
80
86 template <class Span>
87 std::vector<pressio_data const*> get_input_group(Span const& inputs, int idx) const {
88 return make_data_group<pressio_data const*>(inputs, idx, effective_input_group);
89 }
90
96 template <class Span>
97 std::vector<pressio_data*> get_output_group(Span const& inputs, int idx) const {
98 return make_data_group<pressio_data*>(inputs, idx, effective_output_group);
99 }
100
104 std::vector<int> const& effective_input_groups() const {
105 return effective_input_group;
106 }
107
111 std::vector<int> const& effective_output_groups() const {
112 return effective_output_group;
113 }
114
115
116private:
117
118 template <class T, class Span>
119 static std::vector<T> make_data_group(Span const& inputs, int idx, std::vector<int> const& data_groups) {
120 std::vector<T> data_group;
121 for (size_t i = 0; i < inputs.size(); ++i) {
122 if(data_groups[i] == idx) {
123 data_group.push_back(inputs[i]);
124 }
125 }
126 return data_group;
127 }
128 static std::vector<int> normalize_data_group(std::vector<int> const& data_group, size_t size) {
129 std::vector<int> ret;
130 if(not data_group.empty()) {
131 ret = data_group;
132 } else {
133 ret.resize(size);
134 std::iota(std::begin(ret), std::end(ret), 0);
135 }
136 return ret;
137 }
142 static size_t valid_data_groups(std::vector<int> const& effective_input_group, std::vector<int> const& effective_output_group) {
143 std::set<int> s1(std::begin(effective_input_group), std::end(effective_input_group));
144 std::set<int> s2(std::begin(effective_output_group), std::end(effective_output_group));
145 return (s1 == s2)? s1.size(): 0;
146 }
147
148 std::vector<int> effective_input_group, effective_output_group;
149 std::vector<int> input_data_groups, output_data_groups;
150};
151
152#endif /* end of include guard: LIBPRESSSIO_SUBGROUP_MANAGER */
Definition: configurable.h:17
enum pressio_options_key_status get(pressio_options const &options, StringType &&key, PointerType value) const
Definition: configurable.h:127
int set_error(int code, std::string const &msg)
Definition: subgroup_manager.h:28
struct pressio_options get_options() const override
Definition: subgroup_manager.h:46
std::vector< pressio_data * > get_output_group(Span const &inputs, int idx) const
Definition: subgroup_manager.h:97
std::vector< int > const & effective_input_groups() const
Definition: subgroup_manager.h:104
struct pressio_options get_documentation() const override
Definition: subgroup_manager.h:40
std::vector< pressio_data const * > get_input_group(Span const &inputs, int idx) const
Definition: subgroup_manager.h:87
std::vector< int > const & effective_output_groups() const
Definition: subgroup_manager.h:111
int set_options(const struct pressio_options &options) override
Definition: subgroup_manager.h:30
const char * prefix() const override
Definition: subgroup_manager.h:53
int normalize_and_validate(compat::span< U > const &inputs, compat::span< V > const &outputs)
Definition: subgroup_manager.h:64
an extension header for adding compressor plugins to libpressio
interface for configurable types
C++ pressio_data interface.
C++ interface to the compressor loader.
C++ pressio_options and pressio_option interfaces.
Compress, decompress, and configure pressio and lossless compressors.
an abstraction for a contagious memory region of a specified type
A set of options for a compressor.
@ pressio_options_key_set
Definition: pressio_options.h:29
Definition: data.h:52
std::vector< T > to_vector() const
Definition: data.h:484
Definition: options.h:352