@@ -48,91 +48,150 @@ T Bound(const T & v, const T & vmin, const T & vmax)
4848}
4949
5050// -------------------------------------------------------------------------------------------------
51- // Function returns a squared value.
51+ // Function rounds the value to the nearest integer .
5252// -------------------------------------------------------------------------------------------------
53- template <typename T>
54- T Square (const T & v)
53+ inline int Round (double val)
5554{
56- return (v * v);
55+ assert_true (std::fabs (val) < numeric_limits<int >::max ());
56+ return static_cast <int >(std::floor (val + 0.5 ));
5757}
5858
5959// -------------------------------------------------------------------------------------------------
60- // Functor converts 2D index (x,y) into a plain one.
61- // -------------------------------------------------------------------------------------------------
62- template <int SizeX, int SizeY>
63- struct Sub2Ind {
64- Sub2Ind () {
65- static_assert (SizeX * SizeY < static_cast <int >(numeric_limits<int >::max ()), " overflow" );
66- }
67-
68- int operator ()(int ix, int iy) const {
69- assert_true ((0 <= ix) && (ix < SizeX));
70- assert_true ((0 <= iy) && (iy < SizeY));
71- // return (x + static_cast<int>(SizeX) * y);
72- return (ix * SizeY + iy); // TODO: we already have observations based
73- } // on this indexing: y changes faster, swap?
74- };
75-
76- // -------------------------------------------------------------------------------------------------
77- // Functor converts a plane index into 2D one (x,y).
78- // -------------------------------------------------------------------------------------------------
79- template <int SizeX, int SizeY>
80- struct Ind2Sub {
81- Ind2Sub () {
82- static_assert (SizeX * SizeY < static_cast <int >(numeric_limits<int >::max ()), " overflow" );
83- }
84-
85- void operator ()(const int idx, int & x, int & y) const {
86- assert_true ((0 <= idx) && (idx < SizeX * SizeY));
87- std::div_t divresult = std::div (idx, SizeY); // if y is the fastest
88- x = divresult.quot ;
89- y = divresult.rem ;
90- }
91- };
92-
93- // -------------------------------------------------------------------------------------------------
94- // Function reshapes a vector into 2D grid structure,
95- // in Matlab notation: grid = reshape(vec, [SizeX, SizeY]).
60+ // Function creates a new directory or does nothing if it already exists.
61+ // TODO: this will not work on Windows, use STL "experimental" instead.
9662// -------------------------------------------------------------------------------------------------
97- template <int SizeX, int SizeY, typename GRID >
98- void Reshape1Dto2D (GRID & grid, const allscale::utils::grid<double , SizeX * SizeY> & vec)
63+ inline void MakeDirectory (const char * dir)
9964{
100- // TODO: check grid sizes: must be SizeX by SizeY
101- Sub2Ind<SizeX, SizeY> sub2ind;
102- for (int i = 0 ; i < static_cast <int >(SizeX); i++) {
103- for (int j = 0 ; j < static_cast <int >(SizeY); j++) {
104- grid[{i,j}] = vec[{sub2ind (i,j)}];
105- }
106- }
65+ assert_true (dir != nullptr );
66+ std::string cmd (" mkdir -p " );
67+ cmd += dir;
68+ int retval = std::system (cmd.c_str ());
69+ retval = std::system (" sync" );
70+ (void )retval;
10771}
10872
10973// -------------------------------------------------------------------------------------------------
110- // Function unrolls 2D grid structure into a vector, in Matlab notation: vec = grid(:).
74+ // Function creates an output directory inside the current one, which is supposed to be the
75+ // project root folder. If the directory is already exist all its content will be deleted.
76+ // TODO: this will not work on Windows, use STL "experimental" instead.
11177// -------------------------------------------------------------------------------------------------
112- template <int SizeX, int SizeY, typename GRID >
113- void Reshape2Dto1D (allscale::utils::grid<double , SizeX * SizeY> & vec, const GRID & grid)
78+ void CreateAndCleanOutputDir (const std::string & dir)
11479{
115- // TODO: check grid sizes: must be SizeX by SizeY
116- Sub2Ind<SizeX, SizeY> sub2ind;
117- for (int i = 0 ; i < static_cast <int >(SizeX); i++) {
118- for (int j = 0 ; j < static_cast <int >(SizeY); j++) {
119- vec[{sub2ind (i,j)}] = grid[{i,j}];
120- }
121- }
80+ assert_true (!dir.empty ());
81+ string cmd (" mkdir -p " );
82+ cmd += dir;
83+ int retval = std::system (cmd.c_str ());
84+ retval = std::system (" sync" );
85+ retval = std::system ((string (" /bin/rm -fr " ) + dir + " /*.png" ).c_str ());
86+ retval = std::system ((string (" /bin/rm -fr " ) + dir + " /*.pgm" ).c_str ());
87+ retval = std::system ((string (" /bin/rm -fr " ) + dir + " /*.jpg" ).c_str ());
88+ retval = std::system ((string (" /bin/rm -fr " ) + dir + " /*.avi" ).c_str ());
89+ retval = std::system (" sync" );
90+ (void )retval;
12291}
12392
93+ // @{
12494// -------------------------------------------------------------------------------------------------
125- // Function creates a new directory or does nothing if it already exists.
95+ // Functions for global reduction across all the subdomains.
96+ // A T T E N T I O N: these functions must be used ONLY for testing, debugging or visualization.
12697// -------------------------------------------------------------------------------------------------
127- inline void MakeDirectory (const char * dir )
98+ double ReduceMean (const ::allscale::api::user::data::Grid< double , 2 > & grid )
12899{
129- assert_true (dir != nullptr );
130- std::string cmd (" mkdir -p " ); // TODO: not portable, use STL "experimental" instead
131- cmd += dir;
132- int retval = std::system (cmd.c_str ()); // TODO: mutex
133- retval = std::system (" sync" );
134- (void )retval;
100+ double sum = 0.0 ;
101+ for (int x = 0 ; x < SubDomGridSize[_X_]; ++x) {
102+ for (int y = 0 ; y < SubDomGridSize[_Y_]; ++y) { sum += grid[{x,y}]; }}
103+ return (sum / static_cast <double >(SubDomGridSize[_X_] * SubDomGridSize[_Y_]));
104+ }
105+ double ReduceAbsMin (const ::allscale::api::user::data::Grid<double ,2 > & grid)
106+ {
107+ double v = std::fabs (grid[{0 ,0 }]);
108+ for (int x = 0 ; x < SubDomGridSize[_X_]; ++x) {
109+ for (int y = 0 ; y < SubDomGridSize[_Y_]; ++y) { v = std::min (v, std::fabs (grid[{x,y}])); }}
110+ return v;
111+ }
112+ double ReduceAbsMax (const ::allscale::api::user::data::Grid<double ,2 > & grid)
113+ {
114+ double v = std::fabs (grid[{0 ,0 }]);
115+ for (int x = 0 ; x < SubDomGridSize[_X_]; ++x) {
116+ for (int y = 0 ; y < SubDomGridSize[_Y_]; ++y) { v = std::max (v, std::fabs (grid[{x,y}])); }}
117+ return v;
135118}
119+ // @}
120+
121+ /* //-------------------------------------------------------------------------------------------------*/
122+ /* // Function returns a squared value.*/
123+ /* //-------------------------------------------------------------------------------------------------*/
124+ /* template<typename T>*/
125+ /* T Square(const T & v)*/
126+ /* {*/
127+ /* return (v * v);*/
128+ /* }*/
129+
130+ /* //-------------------------------------------------------------------------------------------------*/
131+ /* // Functor converts 2D index (x,y) into a plain one.*/
132+ /* //-------------------------------------------------------------------------------------------------*/
133+ /* template<int SizeX, int SizeY>*/
134+ /* struct Sub2Ind {*/
135+ /* Sub2Ind() {*/
136+ /* static_assert(SizeX * SizeY < static_cast<int>(numeric_limits<int>::max()), "overflow");*/
137+ /* }*/
138+
139+ /* int operator()(int ix, int iy) const {*/
140+ /* assert_true((0 <= ix) && (ix < SizeX));*/
141+ /* assert_true((0 <= iy) && (iy < SizeY));*/
142+ /* //return (x + static_cast<int>(SizeX) * y);*/
143+ /* return (ix * SizeY + iy); // TODO: we already have observations based*/
144+ /* } // on this indexing: y changes faster, swap?*/
145+ /* };*/
146+
147+ /* //-------------------------------------------------------------------------------------------------*/
148+ /* // Functor converts a plane index into 2D one (x,y).*/
149+ /* //-------------------------------------------------------------------------------------------------*/
150+ /* template<int SizeX, int SizeY>*/
151+ /* struct Ind2Sub {*/
152+ /* Ind2Sub() {*/
153+ /* static_assert(SizeX * SizeY < static_cast<int>(numeric_limits<int>::max()), "overflow");*/
154+ /* }*/
155+
156+ /* void operator()(const int idx, int & x, int & y) const {*/
157+ /* assert_true((0 <= idx) && (idx < SizeX * SizeY));*/
158+ /* std::div_t divresult = std::div(idx, SizeY); // if y is the fastest*/
159+ /* x = divresult.quot;*/
160+ /* y = divresult.rem;*/
161+ /* }*/
162+ /* };*/
163+
164+ /* //-------------------------------------------------------------------------------------------------*/
165+ /* // Function reshapes a vector into 2D grid structure,*/
166+ /* // in Matlab notation: grid = reshape(vec, [SizeX, SizeY]).*/
167+ /* //-------------------------------------------------------------------------------------------------*/
168+ /* template<int SizeX, int SizeY, typename GRID>*/
169+ /* void Reshape1Dto2D(GRID & grid, const allscale::utils::grid<double, SizeX * SizeY> & vec)*/
170+ /* {*/
171+ /* // TODO: check grid sizes: must be SizeX by SizeY*/
172+ /* Sub2Ind<SizeX, SizeY> sub2ind;*/
173+ /* for (int i = 0; i < static_cast<int>(SizeX); i++) {*/
174+ /* for (int j = 0; j < static_cast<int>(SizeY); j++) {*/
175+ /* grid[{i,j}] = vec[{sub2ind(i,j)}];*/
176+ /* }*/
177+ /* }*/
178+ /* }*/
179+
180+ /* //-------------------------------------------------------------------------------------------------*/
181+ /* // Function unrolls 2D grid structure into a vector, in Matlab notation: vec = grid(:).*/
182+ /* //-------------------------------------------------------------------------------------------------*/
183+ /* template<int SizeX, int SizeY, typename GRID>*/
184+ /* void Reshape2Dto1D(allscale::utils::grid<double, SizeX * SizeY> & vec, const GRID & grid)*/
185+ /* {*/
186+ /* // TODO: check grid sizes: must be SizeX by SizeY*/
187+ /* Sub2Ind<SizeX, SizeY> sub2ind;*/
188+ /* for (int i = 0; i < static_cast<int>(SizeX); i++) {*/
189+ /* for (int j = 0; j < static_cast<int>(SizeY); j++) {*/
190+ /* vec[{sub2ind(i,j)}] = grid[{i,j}];*/
191+ /* }*/
192+ /* }*/
193+ /* }*/
194+
136195
137196} // end namespace utils
138197} // end namespace app
0 commit comments