Fenix @develop
 
Loading...
Searching...
No Matches
fenix_data_subset.hpp
1/*
2//@HEADER
3// ************************************************************************
4//
5//
6// _|_|_|_| _|_|_|_| _| _| _|_|_| _| _|
7// _| _| _|_| _| _| _| _|
8// _|_|_| _|_|_| _| _| _| _| _|
9// _| _| _| _|_| _| _| _|
10// _| _|_|_|_| _| _| _|_|_| _| _|
11//
12//
13//
14//
15// Copyright (C) 2016 Rutgers University and Sandia Corporation
16//
17// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
18// the U.S. Government retains certain rights in this software.
19//
20// Redistribution and use in source and binary forms, with or without
21// modification, are permitted provided that the following conditions are
22// met:
23//
24// 1. Redistributions of source code must retain the above copyright
25// notice, this list of conditions and the following disclaimer.
26//
27// 2. Redistributions in binary form must reproduce the above copyright
28// notice, this list of conditions and the following disclaimer in the
29// documentation and/or other materials provided with the distribution.
30//
31// 3. Neither the name of the Corporation nor the names of the
32// contributors may be used to endorse or promote products derived from
33// this software without specific prior written permission.
34//
35// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
36// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
39// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
41// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
42// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46//
47// Author Marc Gamell, Eric Valenzuela, Keita Teranishi, Manish Parashar,
48// Michael Heroux, and Matthew Whitlock
49//
50// Questions? Contact Keita Teranishi (knteran@sandia.gov) and
51// Marc Gamell (mgamell@cac.rutgers.edu)
52//
53// ************************************************************************
54//@HEADER
55*/
56#ifndef __FENIX_DATA_SUBSET_HPP__
57#define __FENIX_DATA_SUBSET_HPP__
58
59#include "fenix.h"
60#include "fenix_opt.hpp"
61#include "fenix_data_buffer.hpp"
62
63#include <utility>
64#include <set>
65#include <optional>
66#include <limits>
67#include <string>
68
69namespace fenix {
70
71namespace detail {
72
73struct DataRegionIterator;
74
75struct DataRegion {
76 static constexpr size_t MAX = std::numeric_limits<size_t>::max();
77
78 DataRegion(std::pair<size_t, size_t> b)
79 : DataRegion(b, 0, MAX) { };
80 DataRegion(std::pair<size_t, size_t> b, size_t m_reps, size_t m_stride)
81 : start(b.first),
82 end((b.second!=MAX && b.second+1==b.first+m_stride) ?
83 b.second+m_reps*m_stride : b.second),
84 reps((b.second==MAX || b.second+1==b.first+m_stride) ? 0 : m_reps),
85 stride(reps == 0 ? MAX : m_stride)
86 {
87 fenix_assert(start <= end);
88 fenix_assert(stride != MAX || reps == 0);
89 fenix_assert(reps == 0 || start+stride > end);
90 };
91
92 //Overall range of this region.
93 std::pair<size_t, size_t> range() const;
94
95 //Count of elements contained in this region
96 size_t count() const;
97
98 bool operator==(const DataRegion& other) const;
99 bool operator!=(const DataRegion& other) const;
100
101 //Order based on start
102 bool operator<(const DataRegion& other) const;
103
104 //Return true if these regions intersect
105 bool operator&&(const DataRegion& other) const;
106
107 //Returns intersection of two regions. Not defined when both regions are strided.
108 std::set<DataRegion> operator&(const DataRegion& other) const;
109
110 //This region w/o the overlap with other
111 std::set<DataRegion> operator-(const DataRegion& other) const;
112
113 //Get a region that is a single repetition of this one's, with bounds check
114 DataRegion get_rep(size_t n) const;
115 //As above, but multiple repetitions
116 DataRegion get_reps(size_t first, size_t last) const;
117
118 //For a strided region, returns region between repetitions
119 //Undefined for an unstrided region.
120 DataRegion inverted() const;
121
122 std::optional<DataRegion> try_merge(const DataRegion& other) const;
123
124 std::string str() const;
125
126 //Inclusive region bounds.
127 size_t start, end;
128
129 //Number of times to repeat this after the first
130 size_t reps;
131
132 //Distance between starts of each repetition
133 size_t stride;
134};
135} // namespace detail
136
138 static constexpr size_t MAX = detail::DataRegion::MAX;
139
140 enum SubsetType {
141 BasicSubset,
142 PrestagedSubset,
143 };
144
145 //DataSubset(const DataSubset&) = default;
146 //DataSubset(DataSubset&&) = default;
147 //Empty
148 DataSubset() = default;
149 //[0, end]
150 explicit DataSubset(size_t end);
151 //[bounds.first, bounds.second]
152 DataSubset(std::pair<size_t, size_t> bounds);
153 //[b.first, b.second], ..., [b.first+stride*(n-1), b.second+stride*(n-1)]
154 DataSubset(std::pair<size_t, size_t> b, size_t n, size_t stride);
155 //[bounds[0].first, bounds[0].second], ...
156 DataSubset(std::vector<std::pair<size_t, size_t>> bounds);
157 //[first[0], second[0]], ... [first[n-1], second[n-1]]
158 DataSubset(int n, int* first, int* second);
159 //Merge two subsets
160 DataSubset(const DataSubset& a, const DataSubset& b);
161 //Create from serialized subset object
162 DataSubset(const DataBuffer& buf);
163 explicit DataSubset(SubsetType special_type) : type(special_type) { };
164
165 DataSubset operator+(const DataSubset& other) const;
166 DataSubset& operator+=(const DataSubset& other);
167
168 DataSubset operator+(const Fenix_Data_subset& other) const;
169 DataSubset& operator+=(const Fenix_Data_subset& other);
170
171 DataSubset operator-(const DataSubset& other) const;
172 bool operator==(const DataSubset& other) const;
173 bool operator!=(const DataSubset& other) const;
174
175 bool empty() const;
176
177 //Overall range of this subset
178 std::pair<size_t, size_t> range() const;
179 //Equivalent to range().first and range().second, possibly more performant
180 size_t start() const;
181 size_t end() const;
182
183 //Count of elements in this subset from [0, max_index]
184 //Returns 0 if max_index==end()==MAX
185 size_t count(size_t max_index) const;
186
187 //Count of elements in this subset if it were full [0, end()]
188 //Returns 0 if end()==MAX
189 size_t max_count() const;
190
191 //Serialize this subset object into buf
192 //Will resize buf to fit exactly.
193 void serialize(DataBuffer& buf) const;
194
195 //Will reset dst to fit
196 void serialize_data(
197 size_t elm_size, const DataBuffer& src, DataBuffer& dst
198 ) const;
199 //If dst.size()==0, will resize dst to fit
200 void deserialize_data(
201 size_t elm_size, const DataBuffer& src, DataBuffer& dst
202 ) const;
203
204 //If src_len == 0, will assume src is a large as needed
205 //Will resize dst if too small
206 void copy_data(
207 const size_t elm_size, const size_t src_len, const char* src, DataBuffer& dst
208 ) const;
209 //If dst_len == 0, will assume dst is as large as needed
210 void copy_data(
211 const size_t elm_size, const DataBuffer& src, const size_t dst_len,
212 char* dst
213 ) const;
214
215 //Whether this subset includes the element at index idx
216 bool includes(size_t idx) const;
217 //Whether this subset includes the entire range [0, end] without gaps
218 bool includes_all(size_t end) const;
219
220 //Return equivalent of regions & [0, max_index]
221 std::set<detail::DataRegion> bounded_regions(size_t max_index) const;
222 //As above, but regions & [start, end]
223 std::set<detail::DataRegion> bounded_regions(size_t start, size_t end) const;
224
225 std::string str() const;
226
227 //Individual data regions in this subset
228 std::set<detail::DataRegion> regions;
229
230 SubsetType type = BasicSubset;
231
232 private:
233 //merge immediately adjacent regions to simplify
234 void merge_regions();
235};
236} // namespace fenix
237#endif // __FENIX_DATA_SUBSET_HPP_
Definition fenix_data_buffer.hpp:88
Contains all API function calls and Fenix types. This is the only header file a user should include.
Represents a data subset that can be stored/recovered.
Definition fenix.h:585
Definition fenix_data_subset.hpp:137
Definition fenix_data_subset.hpp:75