82 #ifndef SDS_UDF_STENCIL
83 #define SDS_UDF_STENCIL
103 int has_set_output_value_flag =
false;
104 std::vector<unsigned long long> my_location;
105 std::vector<unsigned long long> my_location_no_ol;
106 std::vector<unsigned long long> global_coordinate;
107 unsigned long long global_coordinate_lineared;
109 unsigned long long my_g_location_rm;
110 unsigned long long chunk_id;
111 T *chunk_data_pointer = NULL;
112 unsigned long long chunk_data_size = 1;
113 unsigned long long chunk_data_size_no_ol = 1;
114 unsigned long long my_offset_no_ol;
115 std::vector<unsigned long long> chunk_dim_size;
120 bool trail_run_flag =
false;
121 mutable std::vector<int> trail_run_overlap_size;
124 int mpi_rank, mpi_size;
126 std::vector<unsigned long long> global_data_size;
131 bool is_output_vector_flag;
132 std::vector<size_t> output_vector_shape;
136 bool has_padding_value_flag =
false;
139 bool is_stencil_tag =
false;
140 std::map<std::string, std::string> stencil_tag_map;
152 chunk_data_pointer = chunk;
154 trail_run_flag =
true;
155 trail_run_overlap_size.resize(dims_input);
156 std::fill(trail_run_overlap_size.begin(), trail_run_overlap_size.end(), 0);
178 Stencil(T value_p, std::vector<size_t> shape_p)
181 is_output_vector_flag =
true;
182 output_vector_shape = shape_p;
186 Stencil(
unsigned long long my_offset, T *chunk, std::vector<unsigned long long> &my_coordinate, std::vector<unsigned long long> &chunk_size, std::vector<unsigned long long> &global_data_size_p)
189 MpiRankSize(&mpi_rank, &mpi_size);
190 std::cout <<
"my value =" << value <<
", coordinate = (" << my_coordinate[0] <<
", " << my_coordinate[1] <<
" )" << std::endl;
192 chunk_data_pointer = chunk;
193 dims = chunk_size.size();
194 chunk_dim_size.resize(dims);
195 my_location.resize(dims);
196 global_data_size.resize(dims);
200 for (
int i = 0; i < dims; i++)
202 chunk_data_size = chunk_data_size * chunk_size[i];
203 chunk_dim_size[i] = chunk_size[i];
204 my_location[i] = my_coordinate[i];
205 global_data_size[i] = global_data_size_p[i];
208 chunk_data_size = chunk_data_size - 1;
209 if (my_offset > chunk_data_size)
211 std::cout <<
"Error in intializing Stencil(). my_offset = " << my_offset <<
", chunk_data_size = " << chunk_data_size << std::endl;
216 value = chunk[my_offset];
415 template <
typename... Is>
418 std::vector<int> ov{{offsets...}};
419 int ov_rank = ov.size();
422 AU_EXIT(
"The # of offsets " + std::to_string(ov_rank) +
" is not equal to the data's: " + std::to_string(dims));
426 std::vector<int> coordinate_shift(dims);
427 std::vector<unsigned long long> coordinate(dims);
431 for (
int i = 0; i < ov_rank; i++)
433 if (std::abs(ov[i]) > trail_run_overlap_size[i])
434 trail_run_overlap_size[i] = std::abs(ov[i]);
436 return chunk_data_pointer[0];
439 for (
int i = 0; i < ov_rank; i++)
441 coordinate_shift[i] = ov[i];
442 if (coordinate_shift[i] == 0)
444 coordinate[i] = my_location[i];
446 else if (coordinate_shift[i] > 0)
449 coordinate[i] = my_location[i] + coordinate_shift[i];
450 if (coordinate[i] >= chunk_dim_size[i])
452 coordinate[i] = chunk_dim_size[i] - 1;
454 if (has_padding_value_flag)
456 return padding_value;
462 coordinate[i] = -coordinate_shift[i];
463 if (my_location[i] < coordinate[i])
466 if (has_padding_value_flag)
468 return padding_value;
474 coordinate[i] = my_location[i] - coordinate[i];
478 unsigned long long shift_offset = coordinate[0];
479 for (
int i = 1; i < ov_rank; i++)
481 shift_offset = shift_offset * chunk_dim_size[i] + coordinate[i];
484 if (shift_offset <= chunk_data_size)
486 return chunk_data_pointer[shift_offset];
493 AU_EXIT(
"Error in operator() of Stencil");
501 int ov_rank = ov.size();
504 AU_EXIT(
"The # of offsets " + std::to_string(ov_rank) +
" is not equal to the data's: " + std::to_string(dims));
508 std::vector<int> coordinate_shift(dims);
509 std::vector<unsigned long long> coordinate(dims);
511 for (
int i = 0; i < ov_rank; i++)
513 coordinate_shift[i] = ov[i];
514 if (coordinate_shift[i] == 0)
516 coordinate[i] = my_location[i];
518 else if (coordinate_shift[i] > 0)
521 coordinate[i] = my_location[i] + coordinate_shift[i];
522 if (coordinate[i] >= chunk_dim_size[i])
524 coordinate[i] = chunk_dim_size[i] - 1;
526 if (has_padding_value_flag)
528 return padding_value;
534 coordinate[i] = -coordinate_shift[i];
535 if (my_location[i] < coordinate[i])
538 if (has_padding_value_flag)
540 return padding_value;
546 coordinate[i] = my_location[i] - coordinate[i];
550 unsigned long long shift_offset = coordinate[0];
551 for (
int i = 1; i < ov_rank; i++)
553 shift_offset = shift_offset * chunk_dim_size[i] + coordinate[i];
556 if (shift_offset <= chunk_data_size)
558 return chunk_data_pointer[shift_offset];
565 AU_EXIT(
"Error in operator() of Stencil");
573 has_set_output_value_flag =
true;
578 return has_set_output_value_flag;
581 void ReadHoodBorder(std::vector<T> &rv, std::vector<int> &start_offset, std::vector<int> &end_offset)
const
583 int rank_temp = start_offset.size();
584 size_t element_count = rv.size();
586 std::vector<unsigned long long> count_size_t(rank_temp);
587 for (
int i = 0; i < rank_temp; i++)
589 count_size_t[i] = (end_offset[i] - start_offset[i] + 1);
594 for (
int iii = start_offset[0]; iii <= end_offset[0]; iii++)
596 rv[iii] = this->operator()(iii);
603 for (
int iii = start_offset[0]; iii <= end_offset[0]; iii++)
605 for (
int jjj = start_offset[1]; jjj <= end_offset[1]; jjj++)
607 rv[iii * count_size_t[1] + jjj] = this->operator()(iii, jjj);
612 unsigned long long array_buffer_offset = 0;
613 std::vector<int> ord(start_offset.begin(), start_offset.end());
614 for (
unsigned long long i = 0; i < element_count; i++)
618 rv[i] = ReadPoint(ord);
631 inline int ReadNeighbors(std::vector<int> &start_offset, std::vector<int> &end_offset, std::vector<T> &rv)
const
634 int rank_temp = start_offset.size();
635 std::vector<size_t> start_offset_size_t, end_offset_size_t;
636 std::vector<unsigned long long> count_size_t;
637 start_offset_size_t.resize(rank_temp);
638 end_offset_size_t.resize(rank_temp);
639 count_size_t.resize(rank_temp);
642 for (
int i = 0; i < rank_temp; i++)
644 count_size_t[i] = (end_offset[i] - start_offset[i] + 1);
645 n = n * count_size_t[i];
648 start_offset_size_t[i] = start_offset[i];
649 end_offset_size_t[i] = end_offset[i] + 1;
655 if (count_size_t == chunk_dim_size)
661 std::copy(&chunk_data_pointer[0], &chunk_data_pointer[n], back_inserter(rv));
667 std::vector<unsigned long long> view_start(rank_temp), view_end(rank_temp);
668 bool out_of_border =
false;
669 for (
int ii = 0; ii < rank_temp; ii++)
671 view_start[ii] = my_location[ii] + start_offset[ii];
672 view_end[ii] = my_location[ii] + end_offset[ii];
673 if (view_end[ii] > (chunk_dim_size[ii] - 1))
675 out_of_border =
true;
687 ReadHoodBorder(rv, start_offset, end_offset);
691 ArrayViewAccessP<T>(rv.data(), chunk_data_pointer, chunk_dim_size, view_start, view_end,
ARRAY_VIEW_READ);
701 inline std::vector<T>
ReadNeighbors(std::vector<int> &start_offset, std::vector<int> &end_offset)
const
704 int rank_temp = start_offset.size();
705 std::vector<size_t> start_offset_size_t, end_offset_size_t;
706 std::vector<unsigned long long> count_size_t;
707 start_offset_size_t.resize(rank_temp);
708 end_offset_size_t.resize(rank_temp);
709 count_size_t.resize(rank_temp);
712 for (
int i = 0; i < rank_temp; i++)
714 count_size_t[i] = (end_offset[i] - start_offset[i] + 1);
715 n = n * count_size_t[i];
716 assert(start_offset[i] >= 0);
717 assert(end_offset[i] >= 0);
718 start_offset_size_t[i] = start_offset[i];
719 end_offset_size_t[i] = end_offset[i] + 1;
722 if (count_size_t == chunk_dim_size)
724 std::vector<T> rv2(chunk_data_pointer, chunk_data_pointer + n);
731 std::vector<unsigned long long> view_start(rank_temp), view_end(rank_temp);
732 bool out_of_border =
false;
733 for (
int ii = 0; ii < rank_temp; ii++)
735 view_start[ii] = my_location[ii] + start_offset[ii];
736 view_end[ii] = my_location[ii] + end_offset[ii];
737 if (view_end[ii] > (chunk_dim_size[ii] - 1))
739 out_of_border =
true;
751 ReadHoodBorder(rv, start_offset, end_offset);
755 ArrayViewAccessP<T>(rv.data(), chunk_data_pointer, chunk_dim_size, view_start, view_end,
ARRAY_VIEW_READ);
814 int WriteNeighbors(std::vector<int> &start_offset, std::vector<int> &end_offset, std::vector<T> &data)
const
816 int rank_temp = start_offset.size();
830 std::vector<unsigned long long> view_start(rank_temp), view_end(rank_temp);
831 for (
int ii = 0; ii < rank_temp; ii++)
833 view_start[ii] = my_location[ii] + start_offset[ii];
834 view_end[ii] = my_location[ii] + end_offset[ii];
837 ArrayViewAccessP<T>(data.data(), chunk_data_pointer, chunk_dim_size, view_start, view_end,
ARRAY_VIEW_WRITE);
907 return chunk_data_size_no_ol - my_offset_no_ol;
946 void SetLocation(
unsigned long long my_offset, std::vector<unsigned long long> &my_coordinate, std::vector<unsigned long long> &my_location_no_ol_p, std::vector<unsigned long long> &chunk_dim_size_no_ol_p, std::vector<long long> ol_origin_offset_p, std::vector<unsigned long long> current_chunk_ol_size)
949 if (my_offset > chunk_data_size)
951 std::cout <<
"Error in intializing Stencil(). my_offset = " << my_offset <<
", chunk_data_size = " << chunk_data_size << std::endl;
956 value = chunk_data_pointer[my_offset];
959 chunk_data_size_no_ol = 1;
960 int rank = my_coordinate.size();
961 for (
int i = 0; i < rank; i++)
963 my_location[i] = my_coordinate[i];
964 my_location_no_ol[i] = my_location_no_ol_p[i];
967 chunk_data_size_no_ol = chunk_data_size_no_ol * chunk_dim_size_no_ol_p[i];
968 chunk_dim_size[i] = current_chunk_ol_size[i];
970 chunk_data_size_no_ol = chunk_data_size_no_ol - 1;
972 ROW_MAJOR_ORDER_MACRO(chunk_dim_size_no_ol_p, chunk_dim_size_no_ol_p.size(), my_location_no_ol_p, my_offset_no_ol);
973 my_offset_no_ol = my_offset_no_ol - 1;
989 void SetLocation(
unsigned long long &my_offset, std::vector<unsigned long long> &my_coordinate, std::vector<unsigned long long> &my_location_no_ol_p, std::vector<unsigned long long> &chunk_dim_size_no_ol_p, std::vector<long long> &ol_origin_offset_p, std::vector<unsigned long long> ¤t_chunk_ol_size, std::vector<unsigned long long> &global_coordinate_p,
unsigned long long &global_coordinate_lineared_p)
991 if (my_offset > chunk_data_size)
993 std::cout <<
"Error in intializing Stencil(). my_offset = " << my_offset <<
", chunk_data_size = " << chunk_data_size << std::endl;
998 value = chunk_data_pointer[my_offset];
1001 chunk_data_size_no_ol = 1;
1002 int rank = my_coordinate.size();
1003 for (
int i = 0; i < rank; i++)
1005 my_location[i] = my_coordinate[i];
1009 chunk_data_size_no_ol = chunk_data_size_no_ol * chunk_dim_size_no_ol_p[i];
1010 chunk_dim_size[i] = current_chunk_ol_size[i];
1012 chunk_data_size_no_ol = chunk_data_size_no_ol - 1;
1014 ROW_MAJOR_ORDER_MACRO(chunk_dim_size_no_ol_p, chunk_dim_size_no_ol_p.size(), my_location_no_ol_p, my_offset_no_ol);
1015 my_offset_no_ol = my_offset_no_ol - 1;
1017 my_g_location_rm = global_coordinate_lineared_p;
1019 global_coordinate = global_coordinate_p;
1020 global_coordinate_lineared = global_coordinate_lineared_p;
1030 return global_coordinate;
1035 return global_coordinate;
1040 index_p = global_coordinate;
1047 index_p = my_location;
1053 overlap_size_p.resize(trail_run_overlap_size.size());
1054 if (trail_run_flag == 1)
1056 for (
int i = 0; i < trail_run_overlap_size.size(); i++)
1057 overlap_size_p[i] = trail_run_overlap_size[i];
1082 my_g_location_rm = lrm;
1087 return my_g_location_rm;
1094 return my_g_location_rm;
1110 is_output_vector_flag = is_output_vector_flag_p;
1121 return is_output_vector_flag;
1126 output_vector_flat_direction = output_vector_flat_direction_p;
1131 return output_vector_flat_direction;
1141 has_padding_value_flag =
true;
1142 padding_value = padding_value_p;
1152 return padding_value;
1162 is_output_vector_flag =
true;
1163 output_vector_shape = shape_p;
1174 if (!is_output_vector_flag)
1176 AU_EXIT(
"The shape of output vector is not set in UDF !");
1178 shape_p = output_vector_shape;
1191 int rank = chunk_dim_size.size();
1192 for (
int i = 0; i < rank; i++)
1194 max_offset.push_back(chunk_dim_size[i] - my_location[i] - 1);
1206 int rank = chunk_dim_size.size();
1208 for (
int i = 0; i < rank; i++)
1210 max_offset.push_back(my_location[i]);
1222 chunk_id_p = chunk_id;
1228 chunk_id = chunk_id_p;
1231 inline int SetTagMap(std::map<std::string, std::string> &stencil_tag_map_p)
1233 is_stencil_tag =
true;
1234 stencil_tag_map = stencil_tag_map_p;
1239 inline int GetTagMap(std::map<std::string, std::string> &stencil_tag_map_p)
const
1241 stencil_tag_map_p = stencil_tag_map;
1247 return is_stencil_tag;
1260 chunk_size = chunk_dim_size;
1266 array_size = global_data_size;
1272 global_data_size = array_size;
Definition: ft_stencil.h:100
void SetLocation(unsigned long long my_offset, std::vector< unsigned long long > &my_coordinate, std::vector< unsigned long long > &my_location_no_ol_p, std::vector< unsigned long long > &chunk_dim_size_no_ol_p, std::vector< long long > ol_origin_offset_p, std::vector< unsigned long long > current_chunk_ol_size)
Definition: ft_stencil.h:946
unsigned long long get_local_neighbors_count_at_left() const
Definition: ft_stencil.h:905
int SetShape(const std::vector< size_t > &shape_p)
Set the Shape of the Stencil object.
Definition: ft_stencil.h:1160
int GetGlobalIndex(std::vector< unsigned long long > &index_p) const
Definition: ft_stencil.h:1038
int SetValue(const T value_p)
Set the value object.
Definition: ft_stencil.h:900
int GetLocalIndex(std::vector< unsigned long long > &index_p) const
Definition: ft_stencil.h:1044
int SetTagMap(std::map< std::string, std::string > &stencil_tag_map_p)
Definition: ft_stencil.h:1231
T get_prev_value()
Definition: ft_stencil.h:939
T GetPadding()
Get the Padding object.
Definition: ft_stencil.h:1150
void SetLocation(unsigned long long &my_offset, std::vector< unsigned long long > &my_coordinate, std::vector< unsigned long long > &my_location_no_ol_p, std::vector< unsigned long long > &chunk_dim_size_no_ol_p, std::vector< long long > &ol_origin_offset_p, std::vector< unsigned long long > ¤t_chunk_ol_size, std::vector< unsigned long long > &global_coordinate_p, unsigned long long &global_coordinate_lineared_p)
Set the Location object.
Definition: ft_stencil.h:989
int SetArraySize(std::vector< unsigned long long > &array_size) const
Definition: ft_stencil.h:1270
bool GetOutputVectorFlag()
Get the Output Vector Flag object.
Definition: ft_stencil.h:1119
T operator()(Is... offsets) const
Definition: ft_stencil.h:416
Stencil(T value_p)
Construct a new Stencil object contains the value Mostly, it is used as output.
Definition: ft_stencil.h:165
int GetOffsetUpper(std::vector< int > &max_offset) const
Get the Max Offset Upper.
Definition: ft_stencil.h:1187
void operator=(TO &others)
Definition: ft_stencil.h:570
void SetChunkID(unsigned long long chunk_id_p)
Definition: ft_stencil.h:1226
unsigned long long GetChunkID() const
Definition: ft_stencil.h:1215
T GetValue()
Get the Value object.
Definition: ft_stencil.h:884
void SetPadding(T padding_value_p)
Set the Padding object.
Definition: ft_stencil.h:1139
int GetShape(std::vector< size_t > &shape_p)
Get the Output Vector Shape object.
Definition: ft_stencil.h:1172
bool HasTagMap() const
Definition: ft_stencil.h:1245
int GetCurrentChunkSize(std::vector< unsigned long long > &chunk_size) const
Get the size of the current chunk which base cell is located This may be different from one run to an...
Definition: ft_stencil.h:1257
unsigned long long get_my_g_location_rm() const
Definition: ft_stencil.h:1085
int GetValue(T &value_p)
Definition: ft_stencil.h:889
std::vector< unsigned long long > GetGlobalCoordinate() const
Definition: ft_stencil.h:1033
int GetArraySize(std::vector< unsigned long long > &array_size) const
Definition: ft_stencil.h:1264
bool has_output_value()
Definition: ft_stencil.h:576
T get_value()
Get the value object.
Definition: ft_stencil.h:864
int GetOffsetLower(std::vector< int > &max_offset) const
Get the Max Offset lower.
Definition: ft_stencil.h:1204
T ReadPoint(std::vector< int > &ov) const
Definition: ft_stencil.h:498
int GetTagMap(std::map< std::string, std::string > &stencil_tag_map_p) const
Definition: ft_stencil.h:1239
void ReadHoodBorder(std::vector< T > &rv, std::vector< int > &start_offset, std::vector< int > &end_offset) const
Definition: ft_stencil.h:581
Stencil(T value_p, std::vector< size_t > shape_p)
Construct a new Stencil object contains the value and the value has the shape for n-dimensional array...
Definition: ft_stencil.h:178
int GetTrailRunResult(std::vector< int > &overlap_size_p)
Definition: ft_stencil.h:1051
void SetOutputVectorFlatDirection(OutputVectorFlatDirection output_vector_flat_direction_p)
Definition: ft_stencil.h:1124
Stencil(int dims_input, T *chunk)
Definition: ft_stencil.h:147
std::vector< unsigned long long > GetCoordinate() const
return the global coodinate of the current Stencil
Definition: ft_stencil.h:1028
OutputVectorFlatDirection GetOutputVectorFlatDirection()
Definition: ft_stencil.h:1129
int WriteNeighbors(std::vector< int > &start_offset, std::vector< int > &end_offset, std::vector< T > &data) const
WriteHood.
Definition: ft_stencil.h:814
~Stencil()
Definition: ft_stencil.h:220
std::vector< T > ReadNeighbors(std::vector< int > &start_offset, std::vector< int > &end_offset) const
read neighborhood
Definition: ft_stencil.h:701
int ReadNeighbors(std::vector< int > &start_offset, std::vector< int > &end_offset, std::vector< T > &rv) const
read neighborhood
Definition: ft_stencil.h:631
Stencil()
Definition: ft_stencil.h:144
unsigned long long get_id() const
Definition: ft_stencil.h:1092
void set_value(const T value_p)
Set the value object.
Definition: ft_stencil.h:874
int GetChunkID(unsigned long long &chunk_id_p) const
Definition: ft_stencil.h:1220
void set_my_g_location_rm(unsigned long long lrm)
Definition: ft_stencil.h:1080
Stencil(unsigned long long my_offset, T *chunk, std::vector< unsigned long long > &my_coordinate, std::vector< unsigned long long > &chunk_size, std::vector< unsigned long long > &global_data_size_p)
Definition: ft_stencil.h:186
void SetOutputVectorFlag(const bool is_output_vector_flag_p)
Set the Output Vector Flag object.
Definition: ft_stencil.h:1108
#define ITERATOR_MACRO(ordinates_p, start_p, end_p)
Definition: ft_array_iterator.h:97
#define ARRAY_VIEW_WRITE
Definition: ft_array_view_access.h:90
#define ARRAY_VIEW_READ
Definition: ft_array_view_access.h:89
double pre_row_major_order_cal
Definition: ft_stencil.h:96
double data_access_time
Definition: ft_stencil.h:96
double row_major_order_cal
Definition: ft_stencil.h:96
OutputVectorFlatDirection
Definition: ft_type.h:212
void PrintVector(std::string name, std::vector< T > v)
Definition: ft_utility.h:165
#define ROW_MAJOR_ORDER_MACRO(dsize, dsize_len, coordinate, offset)
macro version of above two functions for speed
Definition: ft_utility_macro.h:124
#define AU_EXIT(info)
Definition: ft_utility_macro.h:147