11#include " purify/distribute.h"
22#include " purify/wide_field_utilities.h"
33
4- namespace purify {
5- namespace distribute {
4+ namespace purify ::distribute {
65
76std::vector<t_int> distribute_measurements (Vector<t_real> const &u, Vector<t_real> const &v,
87 Vector<t_real> const &w, t_int const number_of_nodes,
98 distribute::plan const distribution_plan,
109 t_int const &grid_size) {
1110 // distrubte visibilities from a measurement
1211 Vector<t_int> index = Vector<t_int>::LinSpaced (u.size (), 0 , u.size ());
13- t_int const patition_size =
12+ t_int const partition_size =
1413 std::ceil (static_cast <t_real>(u.size ()) / static_cast <t_real>(number_of_nodes));
1514 // return a vector of vectors of indicies for each node
1615 std::string plan_name = " " ;
@@ -41,23 +40,25 @@ std::vector<t_int> distribute_measurements(Vector<t_real> const &u, Vector<t_rea
4140 }
4241 PURIFY_DEBUG (
4342 " Using {} to make {} partitions from {} visibilities, with {} visibilities per a node." ,
44- plan_name, number_of_nodes, index.size (), patition_size );
45- std::vector<t_int> patitions (u.size ());
46- // creating patitions
47- for (t_int i = 0 ; i < index. size (); i++ ) {
48- if ( std::floor ( static_cast <t_real>(i) / static_cast <t_real>(patition_size)) >
49- number_of_nodes - 1 ) {
50- PURIFY_ERROR ( " Error: Probably a bug in distribution plan. " );
51- throw std::runtime_error ( " Distributing data into too many nodes " );
52- }
53- patitions [index (i)] = std::floor (static_cast <t_real>(i) / static_cast <t_real>(patition_size ));
43+ plan_name, number_of_nodes, index.size (), partition_size );
44+ std::vector<t_int> partitions (u.size ());
45+ if ( std::floor ( static_cast <t_real>(index. size () - 1 ) / static_cast <t_real>(partition_size)) >
46+ number_of_nodes - 1 ) {
47+ PURIFY_ERROR ( " Error: Probably a bug in distribution plan. " );
48+ throw std::runtime_error ( " Distributing data into too many nodes " );
49+ }
50+ // creating partitions
51+ for (t_int i = 0 ; i < index. size (); ++i) {
52+ partitions [index (i)] = std::floor (static_cast <t_real>(i) / static_cast <t_real>(partition_size ));
5453 }
55- return patitions ;
54+ return partitions ;
5655}
56+
5757Vector<t_int> w_distribution (const Vector<t_real> &u, const Vector<t_real> &v,
5858 Vector<t_real> const &w) {
5959 return w_distribution (w);
6060}
61+
6162Vector<t_int> w_distribution (Vector<t_real> const &w) {
6263 // sort visibilities by w from w_max to w_min
6364 Vector<t_int> index = Vector<t_int>::LinSpaced (w.size (), 0 , w.size ());
@@ -98,6 +99,7 @@ Vector<t_int> equal_distribution(Vector<t_real> const &u, Vector<t_real> const &
9899 });
99100 return index;
100101}
102+
101103std::tuple<std::vector<t_int>, std::vector<t_real>> kmeans_algo (
102104 const Vector<t_real> &w, const t_int number_of_nodes, const t_int iters,
103105 const std::function<t_real(t_real)> &cost, const t_real rel_diff) {
@@ -147,6 +149,7 @@ std::tuple<std::vector<t_int>, std::vector<t_real>> kmeans_algo(
147149
148150 return std::make_tuple (w_node, w_centre);
149151}
152+
150153#ifdef PURIFY_MPI
151154std::tuple<std::vector<t_int>, std::vector<t_real>> kmeans_algo (
152155 const Vector<t_real> &w, const t_int number_of_nodes, const t_int iters,
@@ -164,7 +167,7 @@ std::tuple<std::vector<t_int>, std::vector<t_real>> kmeans_algo(
164167 for (int i = 0 ; i < w_centre.size (); i++)
165168 w_centre[i] =
166169 (static_cast <t_real>(i) * (wmax - wmin) / static_cast <t_real>(number_of_nodes) + wmin);
167- // lopp through even nodes to reduces w-term
170+ // loop through even nodes to reduce w-term
168171 for (int n = 0 ; n < iters; n++) {
169172 if (comm.is_root ()) PURIFY_DEBUG (" clustering iteration {}" , n);
170173 for (int i = 0 ; i < w.size (); i++) {
@@ -216,8 +219,9 @@ std::vector<t_int> w_support(Vector<t_real> const &w, const std::vector<t_int> &
216219 min_support, max_support);
217220 const t_real coeff_average =
218221 comm.all_sum_all <t_real>(coeff_total) / static_cast <t_real>(comm.size ());
219- if (comm.is_root ())
222+ if (comm.is_root ()) {
220223 PURIFY_DEBUG (" Each node should have on average {} coefficients." , coeff_average);
224+ }
221225 t_real coeff_sum = 0 ;
222226 t_int group = 0 ;
223227 std::vector<t_int> groups (w.size (), comm.rank ());
@@ -242,22 +246,23 @@ std::vector<t_int> w_support(Vector<t_real> const &w, const std::vector<t_int> &
242246 groups[i] = group;
243247 }
244248 }
245- if (group > comm.size () - 1 )
249+ if (group > comm.size () - 1 ) {
246250 throw std::runtime_error (
247251 " Error distributing visibilites to even computational load for wide field imaging. Group "
248252 " number out of bounds." );
253+ }
249254 coeff_sum = comm.broadcast (coeff_sum, rank);
250255 group = comm.broadcast (group, rank);
251256
252- if (total != coeff_total and comm.rank () == rank)
257+ if (total != coeff_total and comm.rank () == rank) {
253258 throw std::runtime_error (
254259 " Total number of coefficients calculated is not the same, loop might be broken. " +
255260 std::to_string (total) + " != " + std::to_string (coeff_total));
261+ }
256262 }
257263 if (comm.is_root ()) PURIFY_DEBUG (" {} node should have {} coefficients." , group, coeff_sum);
258264 return groups;
259265}
260266#endif
261267
262- } // namespace distribute
263- } // namespace purify
268+ } // namespace purify::distribute
0 commit comments