|
| 1 | +/* |
| 2 | +Copyright 2024 Huawei Technologies Co., Ltd. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +
|
| 16 | +@author Toni Boehnlein, Benjamin Lozes, Pal Andras Papp, Raphael S. Steiner |
| 17 | +*/ |
| 18 | + |
| 19 | +#define BOOST_TEST_MODULE BSP_SCHEDULERS |
| 20 | +#include <boost/test/unit_test.hpp> |
| 21 | + |
| 22 | +#include <filesystem> |
| 23 | +#include <string> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | + |
| 27 | +#include "osp/bsp/scheduler/GreedySchedulers/GreedyBspScheduler.hpp" |
| 28 | +#include "osp/bsp/scheduler/GreedySchedulers/GreedyVarianceSspScheduler.hpp" |
| 29 | +#include "osp/bsp/scheduler/Serial.hpp" |
| 30 | +#include "osp/bsp/scheduler/MaxBspScheduler.hpp" |
| 31 | +#include "osp/graph_implementations/adj_list_impl/compact_sparse_graph.hpp" |
| 32 | +#include "osp/graph_implementations/adj_list_impl/computational_dag_edge_idx_vector_impl.hpp" |
| 33 | +#include "osp/graph_implementations/adj_list_impl/computational_dag_vector_impl.hpp" |
| 34 | +#include "osp/auxiliary/io/arch_file_reader.hpp" |
| 35 | +#include "osp/auxiliary/io/hdag_graph_file_reader.hpp" |
| 36 | +#include "osp/auxiliary/io/general_file_reader.hpp" |
| 37 | +#include "test_graphs.hpp" |
| 38 | + |
| 39 | +using namespace osp; |
| 40 | + |
| 41 | +std::vector<std::string> test_architectures() { return {"data/machine_params/p3.arch"}; } |
| 42 | + |
| 43 | +template<typename Graph_t> |
| 44 | +void run_test(Scheduler<Graph_t> *test_scheduler) { |
| 45 | + // static_assert(std::is_base_of<Scheduler, T>::value, "Class is not a scheduler!"); |
| 46 | + std::vector<std::string> filenames_graph = tiny_spaa_graphs(); |
| 47 | + std::vector<std::string> filenames_architectures = test_architectures(); |
| 48 | + |
| 49 | + // Getting root git directory |
| 50 | + std::filesystem::path cwd = std::filesystem::current_path(); |
| 51 | + std::cout << cwd << std::endl; |
| 52 | + while ((!cwd.empty()) && (cwd.filename() != "OneStopParallel")) { |
| 53 | + cwd = cwd.parent_path(); |
| 54 | + std::cout << cwd << std::endl; |
| 55 | + } |
| 56 | + |
| 57 | + for (auto &filename_graph : filenames_graph) { |
| 58 | + for (auto &filename_machine : filenames_architectures) { |
| 59 | + std::string name_graph = filename_graph.substr(filename_graph.find_last_of("/\\") + 1); |
| 60 | + name_graph = name_graph.substr(0, name_graph.find_last_of(".")); |
| 61 | + std::string name_machine = filename_machine.substr(filename_machine.find_last_of("/\\") + 1); |
| 62 | + name_machine = name_machine.substr(0, name_machine.rfind(".")); |
| 63 | + |
| 64 | + std::cout << std::endl << "Scheduler: " << test_scheduler->getScheduleName() << std::endl; |
| 65 | + std::cout << "Graph: " << name_graph << std::endl; |
| 66 | + std::cout << "Architecture: " << name_machine << std::endl; |
| 67 | + |
| 68 | + BspInstance<Graph_t> instance; |
| 69 | + |
| 70 | + bool status_graph = file_reader::readGraph((cwd / filename_graph).string(), |
| 71 | + instance.getComputationalDag()); |
| 72 | + bool status_architecture = file_reader::readBspArchitecture((cwd / "data/machine_params/p3.arch").string(), |
| 73 | + instance.getArchitecture()); |
| 74 | + |
| 75 | + if (!status_graph || !status_architecture) { |
| 76 | + |
| 77 | + std::cout << "Reading files failed." << std::endl; |
| 78 | + BOOST_CHECK(false); |
| 79 | + } |
| 80 | + |
| 81 | + BspSchedule<Graph_t> schedule(instance); |
| 82 | + const auto result = test_scheduler->computeSchedule(schedule); |
| 83 | + |
| 84 | + BOOST_CHECK_EQUAL(RETURN_STATUS::OSP_SUCCESS, result); |
| 85 | + BOOST_CHECK(schedule.satisfiesPrecedenceConstraints()); |
| 86 | + } |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +template<typename Graph_t> |
| 91 | +void run_test_max_bsp(MaxBspScheduler<Graph_t>* test_scheduler) { |
| 92 | + std::vector<std::string> filenames_graph = tiny_spaa_graphs(); |
| 93 | + std::vector<std::string> filenames_architectures = test_architectures(); |
| 94 | + |
| 95 | + // Locate project root |
| 96 | + std::filesystem::path cwd = std::filesystem::current_path(); |
| 97 | + while ((!cwd.empty()) && (cwd.filename() != "OneStopParallel")) { |
| 98 | + cwd = cwd.parent_path(); |
| 99 | + } |
| 100 | + |
| 101 | + for (auto& filename_graph : filenames_graph) { |
| 102 | + for (auto& filename_machine : filenames_architectures) { |
| 103 | + std::string name_graph = filename_graph.substr(filename_graph.find_last_of("/\\") + 1); |
| 104 | + name_graph = name_graph.substr(0, name_graph.find_last_of(".")); |
| 105 | + std::string name_machine = filename_machine.substr(filename_machine.find_last_of("/\\") + 1); |
| 106 | + name_machine = name_machine.substr(0, name_machine.rfind(".")); |
| 107 | + |
| 108 | + std::cout << std::endl |
| 109 | + << "Scheduler (MaxBsp): " << test_scheduler->getScheduleName() << std::endl |
| 110 | + << "Graph: " << name_graph << std::endl |
| 111 | + << "Architecture: " << name_machine << std::endl; |
| 112 | + |
| 113 | + computational_dag_edge_idx_vector_impl_def_int_t graph; |
| 114 | + BspArchitecture<Graph_t> arch; |
| 115 | + |
| 116 | + bool status_graph = file_reader::readGraph((cwd / filename_graph).string(), graph); |
| 117 | + bool status_architecture = |
| 118 | + file_reader::readBspArchitecture((cwd / filename_machine).string(), arch); |
| 119 | + |
| 120 | + BOOST_REQUIRE_MESSAGE(status_graph, "Failed to read graph: " << filename_graph); |
| 121 | + BOOST_REQUIRE_MESSAGE(status_architecture, "Failed to read architecture: " << filename_machine); |
| 122 | + |
| 123 | + BspInstance<Graph_t> instance(graph, arch); |
| 124 | + |
| 125 | + MaxBspSchedule<Graph_t> schedule(instance); |
| 126 | + |
| 127 | + const auto result = test_scheduler->computeSchedule(schedule); |
| 128 | + |
| 129 | + BOOST_CHECK_EQUAL(result, RETURN_STATUS::OSP_SUCCESS); |
| 130 | + BOOST_CHECK(schedule.satisfiesPrecedenceConstraints()); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// Tests computeSchedule(BspSchedule&) → staleness = 1 |
| 136 | +BOOST_AUTO_TEST_CASE(GreedyVarianceSspScheduler_test_vector_impl) { |
| 137 | + GreedyVarianceSspScheduler<computational_dag_vector_impl_def_t> test; |
| 138 | + run_test(&test); |
| 139 | +} |
| 140 | + |
| 141 | +// Tests computeSchedule(BspSchedule&) → staleness = 1 (different graph impl) |
| 142 | +BOOST_AUTO_TEST_CASE(GreedyVarianceSspScheduler_test_edge_idx_impl) { |
| 143 | + GreedyVarianceSspScheduler<computational_dag_edge_idx_vector_impl_def_t> test; |
| 144 | + run_test(&test); |
| 145 | +} |
| 146 | + |
| 147 | +// Tests computeSchedule(MaxBspSchedule&) → staleness = 2 |
| 148 | +BOOST_AUTO_TEST_CASE(GreedyVarianceSspScheduler_MaxBspSchedule_large_test) { |
| 149 | + GreedyVarianceSspScheduler<computational_dag_edge_idx_vector_impl_def_int_t> test; |
| 150 | + run_test_max_bsp(&test); |
| 151 | +} |
0 commit comments