1919#include " status_reporting.h"
2020
2121#include < cstring> // std::memcmp, std::memcpy
22+ #include < ostream>
23+ #include < sstream>
2224#include < utility> // std::pair, std::move
2325
2426namespace grtest {
27+
28+ bool operator ==(const GridLayout& lhs, const GridLayout& rhs) noexcept {
29+ bool rank = lhs.rank_ == rhs.rank_ ;
30+ bool start = std::memcmp (lhs.start_ , rhs.start_ , 3 * sizeof (int )) == 0 ;
31+ bool stop = std::memcmp (lhs.stop_ , rhs.stop_ , 3 * sizeof (int )) == 0 ;
32+ bool dim = std::memcmp (lhs.dim_ , rhs.dim_ , 3 * sizeof (int )) == 0 ;
33+ return rank && start && stop && dim;
34+ }
35+
36+ void PrintTo (const GridLayout& layout, std::ostream* os) {
37+ auto write_arr_ = [&](const int * vals) -> void {
38+ for (int i = 0 ; i < layout.rank_ ; i++) {
39+ *os << ((i == 0 ) ? " {" : " , " );
40+ *os << vals[i];
41+ }
42+ *os << ' }' ;
43+ };
44+ *os << " GridLayout{ dim: " ;
45+ write_arr_ (layout.dim_ );
46+ *os << " , start: " ;
47+ write_arr_ (layout.start_ );
48+ *os << " , stop: " ;
49+ write_arr_ (layout.stop_ );
50+ *os << ' }' ;
51+ }
52+
53+ std::string GridLayout::to_string () const {
54+ std::stringstream s;
55+ PrintTo (*this , &s);
56+ return s.str ();
57+ }
58+
2559namespace field_detail {
2660
2761// / Construct a name-pointer mapping based on @p ctx_pack
@@ -48,23 +82,19 @@ static MapType make_nullptr_map_(const GrackleCtxPack& ctx_pack,
4882 return m;
4983}
5084
51- // / Construct a `CorePack` instance by consuming @p premade_map
52- // /
53- // / @param premade_map A string to pointer mapping. The keys of this argument
54- // / should specify the names for each desired Grackle field. We assume that
55- // / the pointers associated with each key hold meaningless garbage values
56- // / @param buf_size The number of elements to allocate per field
57- static std::pair<CorePack, Status> setup_1d_CorePack (MapType&& premade_map,
58- int buf_size) {
85+ std::pair<CorePack, Status> CorePack::setup_1d (MapType&& premade_map,
86+ int buf_size) {
5987 if (buf_size <= 0 ) {
6088 return {CorePack{}, error::Adhoc (" buf_size must be positive" )};
6189 }
6290 std::size_t data_sz = premade_map.size () * buf_size;
6391
92+ GridLayout layout = GridLayout::from_dim (1 , &buf_size);
93+
6494 CorePack pack{
6595 // the trailing parentheses when allocating an array of integers or
6696 // floating point values sets the initial values to 0
67- /* prop_buf */ std::unique_ptr<int [] >(new int [ 9 ]( )),
97+ /* layout */ std::unique_ptr<GridLayout >(new GridLayout (layout )),
6898 /* data_buf */ std::unique_ptr<gr_float[]>(new gr_float[data_sz]()),
6999 /* my_fields */
70100 std::unique_ptr<grackle_field_data>(new grackle_field_data),
@@ -75,9 +105,9 @@ static std::pair<CorePack, Status> setup_1d_CorePack(MapType&& premade_map,
75105 }
76106
77107 // setup grid properties
78- pack.my_fields ->grid_dimension = pack.prop_buf . get () ;
79- pack.my_fields ->grid_start = pack.prop_buf . get () + 3 ;
80- pack.my_fields ->grid_end = pack.prop_buf . get () + 6 ;
108+ pack.my_fields ->grid_dimension = pack.layout -> dim_ ;
109+ pack.my_fields ->grid_start = pack.layout -> start_ ;
110+ pack.my_fields ->grid_end = pack.layout -> end_ ;
81111
82112 pack.my_fields ->grid_rank = 1 ;
83113 pack.my_fields ->grid_dimension [0 ] = buf_size;
@@ -120,7 +150,7 @@ std::pair<FieldContainer, Status> FieldContainer::create_1d(
120150 field_detail::MapType m =
121151 field_detail::make_nullptr_map_ (ctx_pack, disable_metal);
122152 std::pair<field_detail::CorePack, Status> tmp =
123- field_detail::setup_1d_CorePack (std::move (m), buf_size);
153+ field_detail::CorePack::setup_1d (std::move (m), buf_size);
124154 if (tmp.second .is_err ()) {
125155 return {FieldContainer (), std::move (tmp.second )};
126156 } else {
@@ -133,38 +163,13 @@ std::pair<FieldContainer, Status> FieldContainer::create_1d(
133163std::pair<FieldContainer, Status> FieldContainer::create (
134164 const GrackleCtxPack& ctx_pack, const GridLayout& layout,
135165 bool disable_metal) {
136- if (layout.rank < 1 || layout.rank > 3 ) {
137- return {FieldContainer (), error::Adhoc (" layout.rank must be 1, 2, or 3" )};
138- }
139- int total_count = 1 ;
140- for (int i = 0 ; i < layout.rank ; i++) {
141- if (layout.dimension [i] <= 0 ) {
142- return {FieldContainer (),
143- error::Adhoc (" layout.dimension[i] must be positive" )};
144- } else if (layout.ghost_depth [i] < 0 ) {
145- return {FieldContainer (),
146- error::Adhoc (" layout.ghost_depth[i] is negative" )};
147- } else if (layout.ghost_depth [i] * 2 >= layout.dimension [i]) {
148- return {FieldContainer (),
149- error::Adhoc (
150- " layout.dimension[i] must exceed 2*layout.ghost_depth[i]" )};
151- }
152- total_count *= layout.dimension [i];
153- }
166+ int total_count = layout.n_elements ();
154167
155168 std::pair<FieldContainer, Status> tmp =
156169 FieldContainer::create_1d (ctx_pack, total_count, disable_metal);
157170
158171 if (tmp.second .is_ok ()) {
159- grackle_field_data* my_fields = tmp.first .data_ .my_fields .get ();
160- tmp.first .data_ .my_fields ->grid_rank = layout.rank ;
161- for (int i = 0 ; i < layout.rank ; i++) {
162- int dim = layout.dimension [i];
163- int ghost_depth = layout.ghost_depth [i];
164- my_fields->grid_dimension [i] = dim;
165- my_fields->grid_start [i] = ghost_depth;
166- my_fields->grid_end [i] = dim - ghost_depth - 1 ;
167- }
172+ (*tmp.first .data_ .layout ) = layout;
168173 }
169174 return tmp;
170175}
@@ -175,20 +180,16 @@ FieldContainer FieldContainer::clone() const {
175180 // -> right after we create
176181 field_detail::MapType m = this ->data_ .map ;
177182 std::pair<field_detail::CorePack, Status> tmp =
178- field_detail::setup_1d_CorePack (std::move (m), this ->elements_per_field ());
183+ field_detail::CorePack::setup_1d (std::move (m),
184+ this ->elements_per_field ());
179185 // it shouldn't be possible for tmp.second.is_err() to return true
180186 FieldContainer out;
181187 out.data_ = std::move (tmp.first );
182188
183189 // copy over layout properties
184- int rank = this ->rank ();
185- for (int i = 0 ; i < rank; i++) {
186- out.data_ .my_fields ->grid_start [i] = this ->data_ .my_fields ->grid_start [i];
187- out.data_ .my_fields ->grid_end [i] = this ->data_ .my_fields ->grid_end [i];
188- out.data_ .my_fields ->grid_dimension [i] =
189- this ->data_ .my_fields ->grid_dimension [i];
190- }
190+ (*out.data_ .layout ) = *this ->data_ .layout ;
191191
192+ // now copy over field values
192193 copy_into_helper_ (out);
193194 return out;
194195}
@@ -207,21 +208,6 @@ void FieldContainer::copy_into_helper_(FieldContainer& other) const {
207208 std::memcpy (dst, src, length * sizeof (gr_float));
208209}
209210
210- bool FieldContainer::same_grid_props (const FieldContainer& other) const {
211- const grackle_field_data* a = this ->data_ .my_fields .get ();
212- const grackle_field_data* b = other.data_ .my_fields .get ();
213-
214- if (a->grid_rank != b->grid_rank ) {
215- return false ;
216- }
217- int rank = a->grid_rank ;
218- std::size_t sz = rank * sizeof (int );
219- bool s = std::memcmp (a->grid_start , b->grid_start , sz) == 0 ;
220- bool e = std::memcmp (a->grid_end , b->grid_end , sz) == 0 ;
221- bool d = std::memcmp (a->grid_dimension , b->grid_dimension , sz) == 0 ;
222- return s && d && e;
223- }
224-
225211bool FieldContainer::same_fields (const FieldContainer& other) const {
226212 // this takes advantage of the fact that MapType is sorted
227213 MapType::const_iterator it_a = this ->data_ .map .begin ();
0 commit comments