test_base.h 1.8 KB
Newer Older
M
Megvii Engine Team 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/**
 * \file test/test_base.h
 * MegRay is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

#pragma once

#include <thread>
#include <vector>

#include <gtest/gtest.h>

#include "../src/megray.h"

template <typename T>
void run_test(int nranks, MegRay::Backend backend,
              std::vector<std::vector<T>>& inputs,
24
              std::vector<std::vector<T>>& expect_outputs,
M
Megvii Engine Team 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
              std::function<void(std::shared_ptr<MegRay::Communicator>,
                                 std::vector<std::string>&, int,
                                 std::vector<T>&, std::vector<T>&)>
                      main_func) {
    std::vector<std::shared_ptr<MegRay::Communicator>> comms(nranks);
    std::vector<std::string> uids(nranks);
    std::vector<std::vector<T>> outputs(nranks);

    for (int i = 0; i < nranks; i++) {
        comms[i] = MegRay::get_communicator(nranks, i, backend);
        uids[i] = comms[i]->get_uid();
        outputs[i].resize(expect_outputs[i].size());
    }

    std::vector<std::thread> threads;
    for (int i = 0; i < nranks; i++) {
        threads.push_back(std::thread(main_func, comms[i], std::ref(uids), i,
                                      std::ref(inputs[i]),
                                      std::ref(outputs[i])));
    }

    for (int i = 0; i < nranks; i++) {
        threads[i].join();
    }

    for (int i = 0; i < nranks; i++) {
        for (size_t j = 0; j < expect_outputs[i].size(); j++) {
            ASSERT_FLOAT_EQ(expect_outputs[i][j], outputs[i][j]);
        }
    }
}