void ring_array_test(const communicator& comm, const T* pass_values, int n, const char* kind, int root = 0) { T* transferred_values = new T[n]; int rank = comm.rank(); int size = comm.size(); if (rank == root) { std::cout << "Passing " << kind << " array around a ring from root " << root << "..."; comm.send((rank + 1) % size, 0, pass_values, n); comm.recv((rank + size - 1) % size, 0, transferred_values, n); bool okay = std::equal(pass_values, pass_values + n, transferred_values); BOOST_CHECK(okay); if (okay) std::cout << " OK." << std::endl; } else { status stat = comm.probe(boost::mpi::any_source, 0); boost::optional<int> num_values = stat.template count<T>(); if (boost::mpi::is_mpi_datatype<T>()) BOOST_CHECK(num_values && *num_values == n); else BOOST_CHECK(!num_values || *num_values == n); comm.recv(stat.source(), 0, transferred_values, n); BOOST_CHECK(std::equal(pass_values, pass_values + n, transferred_values)); comm.send((rank + 1) % size, 0, transferred_values, n); } (comm.barrier)(); delete [] transferred_values; }
void gather_impl(const communicator& comm, const T* in_values, int n, int root, mpl::false_) { int tag = environment::collectives_tag(); comm.send(root, tag, in_values, n); }
void ring_test(const communicator& comm, const T& pass_value, const char* kind, int root = 0) { T transferred_value; int rank = comm.rank(); int size = comm.size(); if (rank == root) { std::cout << "Passing " << kind << " around a ring from root " << root << "..."; comm.send((rank + 1) % size, 0, pass_value); comm.recv((rank + size - 1) % size, 0, transferred_value); BOOST_CHECK(transferred_value == pass_value); if (transferred_value == pass_value) std::cout << " OK." << std::endl; } else { comm.recv((rank + size - 1) % size, 0, transferred_value); BOOST_CHECK(transferred_value == pass_value); comm.send((rank + 1) % size, 0, transferred_value); } (comm.barrier)(); }
void upper_lower_scan(const communicator& comm, const T* in_values, int n, T* out_values, Op& op, int lower, int upper) { int tag = environment::collectives_tag(); int rank = comm.rank(); if (lower + 1 == upper) { std::copy(in_values, in_values + n, out_values); } else { int middle = (lower + upper) / 2; if (rank < middle) { // Lower half upper_lower_scan(comm, in_values, n, out_values, op, lower, middle); // If we're the last process in the lower half, send our values // to everyone in the upper half. if (rank == middle - 1) { packed_oarchive oa(comm); for (int i = 0; i < n; ++i) oa << out_values[i]; for (int p = middle; p < upper; ++p) comm.send(p, tag, oa); } } else { // Upper half upper_lower_scan(comm, in_values, n, out_values, op, middle, upper); // Receive value from the last process in the lower half. packed_iarchive ia(comm); comm.recv(middle - 1, tag, ia); // Combine value that came from the left with our value T left_value; for (int i = 0; i < n; ++i) { ia >> left_value; out_values[i] = op(left_value, out_values[i]); } } } }
void test_skeleton_and_content(const communicator& comm, int root, bool manual_broadcast) { using boost::mpi::skeleton; using boost::mpi::content; using boost::mpi::get_content; using boost::make_counting_iterator; using boost::mpi::broadcast; int list_size = comm.size() + 7; if (comm.rank() == root) { // Fill in the seed data std::list<int> original_list; for (int i = 0; i < list_size; ++i) original_list.push_back(i); std::cout << "Broadcasting integer list skeleton from root " << root << "..."; if (manual_broadcast) { // Broadcast the skeleton (manually) for (int p = 0; p < comm.size(); ++p) if (p != root) comm.send(p, 0, skeleton(original_list)); } else { broadcast(comm, skeleton(original_list), root); } std::cout << "OK." << std::endl; // Broadcast the content (manually) std::cout << "Broadcasting integer list content from root " << root << "..."; { content c = get_content(original_list); for (int p = 0; p < comm.size(); ++p) if (p != root) comm.send(p, 1, c); } std::cout << "OK." << std::endl; // Reverse the list, broadcast the content again std::reverse(original_list.begin(), original_list.end()); std::cout << "Broadcasting reversed integer list content from root " << root << "..."; { content c = get_content(original_list); for (int p = 0; p < comm.size(); ++p) if (p != root) comm.send(p, 2, c); } std::cout << "OK." << std::endl; } else { // Allocate some useless data, to try to get the addresses of the // list<int>'s used later to be different across processes. std::list<int> junk_list(comm.rank() * 3 + 1, 17); // Receive the skeleton to build up the transferred list std::list<int> transferred_list; if (manual_broadcast) { comm.recv(root, 0, skeleton(transferred_list)); } else { broadcast(comm, skeleton(transferred_list), root); } BOOST_CHECK((int)transferred_list.size() == list_size); // Receive the content and check it comm.recv(root, 1, get_content(transferred_list)); BOOST_CHECK(std::equal(make_counting_iterator(0), make_counting_iterator(list_size), transferred_list.begin())); // Receive the reversed content and check it comm.recv(root, 2, get_content(transferred_list)); BOOST_CHECK(std::equal(make_counting_iterator(0), make_counting_iterator(list_size), transferred_list.rbegin())); } (comm.barrier)(); }