// Copyright 2006 The Trustees of Indiana University. // Use, modification and distribution is subject to the Boost Software // License, Version 1.0. (See copy at http://www.boost.org/LICENSE_1_0.txt) // Authors: Douglas Gregor // Andrew Lumsdaine #include "metasweep.hpp" #include #include #include #include class print_experiment { public: template void operator()(const State& state) { using boost::mpl::int_; using boost::tuples::length; std::cout << "("; print(state, int_<0>(), int_::value>()); std::cout << ")\n"; } private: template void print(const State& state, boost::mpl::int_, boost::mpl::int_ n) { using boost::tuples::get; using boost::mpl::int_; if (I > 0) std::cout << ", "; std::cout << get(state); print(state, int_(), n); } template void print(const State&, boost::mpl::int_, boost::mpl::int_) { // Done. } }; struct print_last_param { template void operator()(State& state) const { using boost::tuples::length; using boost::tuples::get; const int len = length::value; std::cout << get(state); } }; // Print out a table whose title is the parameter we're sweeping over. struct table_task { template void setup(State& state) { std::cout << "\n"; } template void cleanup(State&) { std::cout << "
\n"; } }; // Print out a table row whose first element is a containing the // parameter we're sweeping over. struct table_row_task { template void setup(State& state) { using boost::tuples::length; using boost::tuples::get; const int len = length::value; std::cout << " \n"; std::cout << " "; print_last_param()(state); std::cout << "\n"; } template void cleanup(State&) { std::cout << " \n"; } }; // Print out a table data cell with nothing in it. struct table_data_task { template void setup(State& state) { std::cout << " "; } template void cleanup(State&) { std::cout << "\n"; } }; int main() { using boost::make_tuple; // Any kind of container that has begin() and end() member functions // returning iterators will work. We can hack up simple contains for // common cases, e.g., range(0, 100, 0.5), if we want them for // presentation purposes. Here, we demonstrate the use of arrays and // vectors. boost::array procs = { 1, 2, 17, 31 }; std::vector algorithms; algorithms.push_back("all_reduce"); algorithms.push_back("all_gather"); algorithms.push_back("broadcast"); // Sweep the parameter space of procs X algorithms X [ -1, 3.14159, // "honk"] print_experiment just prints out the parameters passed // into it. sweep(make_tuple(procs, algorithms, make_tuple(-1, 3.14159, std::string("honk!"))), print_experiment()); // Use the same parameter sweep above, but bind tasks to parameters // so that we get output in a bunch of tables. sweep(make_tuple(procs, algorithms, make_tuple(-1, 3.14159, std::string("honk!"))), empty_state(). bind<1>(table_row_task()). bind<0>(table_task()). bind<2>(table_data_task()), print_last_param()); return 0; }