test_opr.cpp 22.6 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 24
/**
 * \file test/test_opr.cpp
 * 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.
 */

#include <algorithm>
#include <iostream>
#include <limits>
#include <string>
#include <thread>
#include <vector>

#include <gtest/gtest.h>

#include "test_base.h"

TEST(TestNcclCommunicator, Init) {
    const int nranks = 3;
25 26 27
    const int port = MegRay::get_free_port();
    auto ret = MegRay::create_server(nranks, port);
    ASSERT_EQ(MegRay::MEGRAY_OK, ret);
M
Megvii Engine Team 已提交
28

29 30
    auto run = [&](int rank) {
        cudaSetDevice(rank);
31 32
        auto comm = MegRay::get_communicator(nranks, rank, MegRay::MEGRAY_NCCL);
        ASSERT_EQ(MegRay::MEGRAY_OK, comm->init("localhost", port));
33
    };
M
Megvii Engine Team 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46

    std::vector<std::thread> threads;
    for (size_t i = 0; i < nranks; i++) {
        threads.push_back(std::thread(run, i));
    }

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

TEST(TestUcxCommunicator, Init) {
    const int nranks = 3;
47 48 49
    const int port = MegRay::get_free_port();
    auto ret = MegRay::create_server(nranks, port);
    ASSERT_EQ(MegRay::MEGRAY_OK, ret);
M
Megvii Engine Team 已提交
50 51 52

    auto run = [&](int rank) {
        cudaSetDevice(rank);
53 54
        auto comm = MegRay::get_communicator(nranks, rank, MegRay::MEGRAY_UCX);
        ASSERT_EQ(MegRay::MEGRAY_OK, comm->init("localhost", port));
M
Megvii Engine Team 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67
    };

    std::vector<std::thread> threads;
    for (int i = 0; i < nranks; i++) {
        threads.push_back(std::thread(run, i));
    }

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

TEST(TestOpr, SendRecv) {
68 69 70
    std::string msg("test_message");
    const int nranks = 2;
    const size_t len = msg.size();
M
Megvii Engine Team 已提交
71

72 73
    std::vector<std::vector<char>> inputs(nranks);
    std::vector<std::vector<char>> expected_outputs(nranks);
M
Megvii Engine Team 已提交
74

75 76 77 78
    for (size_t i = 0; i < len; i++) {
        inputs[0].push_back(msg[i]);
        expected_outputs[1].push_back(msg[i]);
    }
M
Megvii Engine Team 已提交
79

80
    auto run = [len](std::shared_ptr<MegRay::Communicator> comm,
81
                     int port, int rank,
82 83 84
                     std::vector<char>& input,
                     std::vector<char>& output) -> void {
        CUDA_ASSERT(cudaSetDevice(rank));
85
        comm->init("localhost", port);
M
Megvii Engine Team 已提交
86 87 88 89 90 91

        cudaStream_t stream;
        CUDA_ASSERT(cudaStreamCreate(&stream));
        auto ctx = MegRay::CudaContext::make(stream);

        void* ptr;
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        CUDA_ASSERT(cudaMalloc(&ptr, len));

        if (rank == 0) {  // send
            CUDA_ASSERT(cudaMemcpy(ptr, input.data(), len, cudaMemcpyHostToDevice));
            comm->send(ptr, len, 1, ctx);
            CUDA_ASSERT(cudaStreamSynchronize(stream));
        } else {  // recv
            comm->recv(ptr, len, 0, ctx);
            CUDA_ASSERT(cudaStreamSynchronize(stream));
            CUDA_ASSERT(cudaMemcpy(output.data(), ptr, len, cudaMemcpyDeviceToHost));
        }
    };

    run_test<char>(nranks, MegRay::MEGRAY_NCCL, inputs, expected_outputs, run);
    run_test<char>(nranks, MegRay::MEGRAY_UCX, inputs, expected_outputs, run);
}

TEST(TestOpr, Scatter) {
    const int nranks = 3;
    const size_t recvlen = 10;
    const int root = 1;

    std::vector<std::vector<float>> inputs(nranks);
    std::vector<std::vector<float>> outputs(nranks);
    for (size_t i = 0; i < nranks; i++) {
        for (size_t j = 0; j < recvlen; j++) {
            float val = 1.0 * (i + 1) * (j + 2);
            inputs[root].push_back(val);
            outputs[i].push_back(val);
        }
    }

    auto run = [nranks, recvlen, root](std::shared_ptr<MegRay::Communicator> comm,
125
                                       int port, int rank,
126 127 128
                                       std::vector<float>& input,
                                       std::vector<float>& output) -> void {
        CUDA_ASSERT(cudaSetDevice(rank));
129
        comm->init("localhost", port);
M
Megvii Engine Team 已提交
130

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        cudaStream_t stream;
        CUDA_ASSERT(cudaStreamCreate(&stream));
        auto ctx = MegRay::CudaContext::make(stream);

        void *in_ptr, *out_ptr;
        CUDA_ASSERT(cudaMalloc(&out_ptr, recvlen * sizeof(float)));

        if (rank == root) {
            CUDA_ASSERT(cudaMalloc(&in_ptr, nranks * recvlen * sizeof(float)));
            CUDA_ASSERT(cudaMemcpy(in_ptr, input.data(),
                                   nranks * recvlen * sizeof(float),
                                   cudaMemcpyHostToDevice));
        } else {
            in_ptr = nullptr;
        }

        int ret = comm->scatter(in_ptr, out_ptr, recvlen,
                                MegRay::MEGRAY_FLOAT32, root, ctx);
        ASSERT_EQ(ret, 0);
M
Megvii Engine Team 已提交
150 151

        CUDA_ASSERT(cudaStreamSynchronize(stream));
152 153 154
        CUDA_ASSERT(cudaMemcpy(output.data(), out_ptr,
                               recvlen * sizeof(float),
                               cudaMemcpyDeviceToHost));
M
Megvii Engine Team 已提交
155
    };
156 157 158
    run_test<float>(nranks, MegRay::MEGRAY_NCCL, inputs, outputs, run);
    run_test<float>(nranks, MegRay::MEGRAY_UCX, inputs, outputs, run);
}
M
Megvii Engine Team 已提交
159

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
TEST(TestOpr, Gather) {
    const int nranks = 3;
    const size_t sendlen = 10;
    const int root = 1;

    std::vector<std::vector<float>> inputs(nranks);
    std::vector<std::vector<float>> outputs(nranks);
    for (size_t i = 0; i < nranks; i++) {
        for (size_t j = 0; j < sendlen; j++) {
            float val = 1.0 * (i + 1) * (j + 2);
            inputs[i].push_back(val);
            outputs[root].push_back(val);
        }
    }

    auto run = [nranks, sendlen, root](std::shared_ptr<MegRay::Communicator> comm,
176
                                       int port, int rank,
177 178 179
                                       std::vector<float>& input,
                                       std::vector<float>& output) -> void {
        CUDA_ASSERT(cudaSetDevice(rank));
180
        comm->init("localhost", port);
M
Megvii Engine Team 已提交
181 182 183 184 185

        cudaStream_t stream;
        CUDA_ASSERT(cudaStreamCreate(&stream));
        auto ctx = MegRay::CudaContext::make(stream);

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
        void *in_ptr, *out_ptr;
        CUDA_ASSERT(cudaMalloc(&in_ptr, sendlen * sizeof(float)));
        CUDA_ASSERT(cudaMemcpy(in_ptr, input.data(),
                               sendlen * sizeof(float),
                               cudaMemcpyHostToDevice));

        if (rank == root) {
            CUDA_ASSERT(cudaMalloc(&out_ptr, nranks * sendlen * sizeof(float)));
        } else {
            out_ptr = nullptr;
        }

        int ret = comm->gather(in_ptr, out_ptr, sendlen,
                               MegRay::MEGRAY_FLOAT32, root, ctx);
        ASSERT_EQ(ret, 0);
M
Megvii Engine Team 已提交
201 202 203

        CUDA_ASSERT(cudaStreamSynchronize(stream));

204 205 206 207 208
        if (rank == root) {
            CUDA_ASSERT(cudaMemcpy(output.data(), out_ptr,
                                   nranks * sendlen * sizeof(float),
                                   cudaMemcpyDeviceToHost));
        }
M
Megvii Engine Team 已提交
209
    };
210 211 212
    run_test<float>(nranks, MegRay::MEGRAY_NCCL, inputs, outputs, run);
    run_test<float>(nranks, MegRay::MEGRAY_UCX, inputs, outputs, run);
}
M
Megvii Engine Team 已提交
213

214 215 216
TEST(TestOpr, AllToAll) {
    const int nranks = 3;
    const size_t len = 6;
M
Megvii Engine Team 已提交
217

218 219 220 221 222 223 224 225 226 227 228 229 230
    std::vector<std::vector<float>> inputs(nranks, std::vector<float>(nranks * len));
    std::vector<std::vector<float>> outputs(nranks, std::vector<float>(nranks * len));
    for (size_t i = 0; i < nranks; i++) {
        for (size_t j = 0; j < nranks; j++) {
            for (size_t k = 0; k < len; k++) {
                float val = 1.0 * (i + 1) * (j + 2) * (k + 3);
                inputs[i][j * len + k] = val;
                outputs[j][i * len + k] = val;
            }
        }
    }

    auto run = [nranks, len](std::shared_ptr<MegRay::Communicator> comm,
231
                             int port, int rank,
232 233 234
                             std::vector<float>& input,
                             std::vector<float>& output) -> void {
        CUDA_ASSERT(cudaSetDevice(rank));
235
        comm->init("localhost", port);
236 237 238 239

        cudaStream_t stream;
        CUDA_ASSERT(cudaStreamCreate(&stream));
        auto ctx = MegRay::CudaContext::make(stream);
M
Megvii Engine Team 已提交
240

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
        void *in_ptr, *out_ptr;
        CUDA_ASSERT(cudaMalloc(&in_ptr, nranks * len * sizeof(float)));
        CUDA_ASSERT(cudaMemcpy(in_ptr, input.data(),
                               nranks * len * sizeof(float),
                               cudaMemcpyHostToDevice));
        CUDA_ASSERT(cudaMalloc(&out_ptr, nranks * len * sizeof(float)));

        int ret = comm->all_to_all(in_ptr, out_ptr, len,
                                   MegRay::MEGRAY_FLOAT32, ctx);
        ASSERT_EQ(ret, 0);

        CUDA_ASSERT(cudaStreamSynchronize(stream));

        CUDA_ASSERT(cudaMemcpy(output.data(), out_ptr,
                               nranks * len * sizeof(float),
                               cudaMemcpyDeviceToHost));
    };
    run_test<float>(nranks, MegRay::MEGRAY_NCCL, inputs, outputs, run);
    run_test<float>(nranks, MegRay::MEGRAY_UCX, inputs, outputs, run);
M
Megvii Engine Team 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
}

TEST(TestOpr, AllGather) {
    const int nranks = 3;
    const size_t sendlen = 10;

    std::vector<std::vector<float>> inputs(nranks, std::vector<float>(sendlen));
    std::vector<std::vector<float>> outputs(
            nranks, std::vector<float>(nranks * sendlen));
    for (size_t j = 0; j < sendlen; j++) {
        for (size_t i = 0; i < nranks; i++) {
            inputs[i][j] = 1.0 * (i + 1) * (j + 1);
            for (int k = 0; k < nranks; k++) {
                outputs[k][i * sendlen + j] = inputs[i][j];
            }
        }
    }

    auto run = [nranks, sendlen](std::shared_ptr<MegRay::Communicator> comm,
279
                                 int port, int rank,
M
Megvii Engine Team 已提交
280 281 282
                                 std::vector<float>& input,
                                 std::vector<float>& output) -> void {
        CUDA_ASSERT(cudaSetDevice(rank));
283
        comm->init("localhost", port);
M
Megvii Engine Team 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

        cudaStream_t stream;
        CUDA_ASSERT(cudaStreamCreate(&stream));
        auto ctx = MegRay::CudaContext::make(stream);

        void *in_ptr, *out_ptr;
        CUDA_ASSERT(cudaMalloc(&in_ptr, sendlen * sizeof(float)));
        CUDA_ASSERT(cudaMalloc(&out_ptr, sendlen * nranks * sizeof(float)));

        CUDA_ASSERT(cudaMemcpy(in_ptr, input.data(), sendlen * sizeof(float),
                               cudaMemcpyHostToDevice));

        int ret = comm->all_gather(in_ptr, out_ptr, sendlen,
                                   MegRay::MEGRAY_FLOAT32, ctx);
        ASSERT_EQ(ret, 0);

        CUDA_ASSERT(cudaStreamSynchronize(stream));
        CUDA_ASSERT(cudaMemcpy(output.data(), out_ptr,
                               nranks * sendlen * sizeof(float),
                               cudaMemcpyDeviceToHost));
    };
305 306
    run_test<float>(nranks, MegRay::MEGRAY_NCCL, inputs, outputs, run);
    run_test<float>(nranks, MegRay::MEGRAY_UCX, inputs, outputs, run);
M
Megvii Engine Team 已提交
307 308 309 310 311 312 313 314 315 316 317
}

TEST(TestOpr, AllReduce) {
    const int nranks = 3;
    const size_t len = 10;
    std::vector<std::vector<float>> inputs(nranks, std::vector<float>(len));
    std::vector<std::vector<float>> expected_outputs(nranks,
                                                     std::vector<float>(len));

    auto reduce_func = [nranks, len](MegRay::ReduceOp op) {
        auto run = [nranks, len, op](std::shared_ptr<MegRay::Communicator> comm,
318
                                     int port, int rank,
M
Megvii Engine Team 已提交
319 320 321
                                     std::vector<float>& input,
                                     std::vector<float>& output) {
            CUDA_ASSERT(cudaSetDevice(rank));
322
            comm->init("localhost", port);
M
Megvii Engine Team 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

            cudaStream_t stream;
            CUDA_ASSERT(cudaStreamCreate(&stream));
            auto ctx = MegRay::CudaContext::make(stream);

            void *in_ptr, *out_ptr;
            CUDA_ASSERT(cudaMalloc(&in_ptr, len * sizeof(float)));
            CUDA_ASSERT(cudaMalloc(&out_ptr, len * sizeof(float)));

            CUDA_ASSERT(cudaMemcpy(in_ptr, input.data(), len * sizeof(float),
                                   cudaMemcpyHostToDevice));

            int ret = comm->all_reduce(in_ptr, out_ptr, len,
                                       MegRay::MEGRAY_FLOAT32, op, ctx);
            ASSERT_EQ(ret, 0);

            CUDA_ASSERT(cudaStreamSynchronize(stream));
            CUDA_ASSERT(cudaMemcpy(output.data(), out_ptr, len * sizeof(float),
                                   cudaMemcpyDeviceToHost));
        };
        return run;
    };

    for (size_t j = 0; j < len; j++) {
        float sum = 0;
        for (size_t i = 0; i < nranks; i++) {
            inputs[i][j] = 1.0 * (i + 1) * (j + 1);
            sum += inputs[i][j];
        }
        for (size_t i = 0; i < nranks; i++) {
            expected_outputs[i][j] = sum;
        }
    }
    run_test<float>(nranks, MegRay::MEGRAY_NCCL, inputs, expected_outputs,
357
                    reduce_func(MegRay::MEGRAY_SUM));
M
Megvii Engine Team 已提交
358
    run_test<float>(nranks, MegRay::MEGRAY_UCX, inputs, expected_outputs,
359
                    reduce_func(MegRay::MEGRAY_SUM));
M
Megvii Engine Team 已提交
360 361 362 363 364 365 366 367 368 369 370 371

    for (size_t j = 0; j < len; j++) {
        float max_val = std::numeric_limits<float>::min();
        for (size_t i = 0; i < nranks; i++) {
            inputs[i][j] = 1.0 * (i + 1) * (j + 1);
            max_val = std::max(max_val, inputs[i][j]);
        }
        for (size_t i = 0; i < nranks; i++) {
            expected_outputs[i][j] = max_val;
        }
    }
    run_test<float>(nranks, MegRay::MEGRAY_NCCL, inputs, expected_outputs,
372
                    reduce_func(MegRay::MEGRAY_MAX));
M
Megvii Engine Team 已提交
373
    run_test<float>(nranks, MegRay::MEGRAY_UCX, inputs, expected_outputs,
374
                    reduce_func(MegRay::MEGRAY_MAX));
M
Megvii Engine Team 已提交
375 376 377 378 379 380 381 382 383 384 385 386

    for (size_t j = 0; j < len; j++) {
        float min_val = std::numeric_limits<float>::max();
        for (size_t i = 0; i < nranks; i++) {
            inputs[i][j] = 1.0 * (i + 1) * (j + 1);
            min_val = std::min(min_val, inputs[i][j]);
        }
        for (size_t i = 0; i < nranks; i++) {
            expected_outputs[i][j] = min_val;
        }
    }
    run_test<float>(nranks, MegRay::MEGRAY_NCCL, inputs, expected_outputs,
387
                    reduce_func(MegRay::MEGRAY_MIN));
M
Megvii Engine Team 已提交
388
    run_test<float>(nranks, MegRay::MEGRAY_UCX, inputs, expected_outputs,
389
                    reduce_func(MegRay::MEGRAY_MIN));
M
Megvii Engine Team 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402
}

TEST(TestOpr, ReduceScatterSum) {
    const int nranks = 3;
    const size_t recvlen = 10;

    std::vector<std::vector<float>> inputs(
            nranks, std::vector<float>(nranks * recvlen));
    std::vector<std::vector<float>> expected_outputs(
            nranks, std::vector<float>(recvlen));
    auto reduce_func = [nranks, recvlen](MegRay::ReduceOp op) {
        auto run = [nranks, recvlen,
                    op](std::shared_ptr<MegRay::Communicator> comm,
403
                        int port, int rank,
M
Megvii Engine Team 已提交
404 405
                        std::vector<float>& input, std::vector<float>& output) {
            CUDA_ASSERT(cudaSetDevice(rank));
406
            comm->init("localhost", port);
M
Megvii Engine Team 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

            cudaStream_t stream;
            CUDA_ASSERT(cudaStreamCreate(&stream));
            auto ctx = MegRay::CudaContext::make(stream);

            void *in_ptr, *out_ptr;
            CUDA_ASSERT(cudaMalloc(&in_ptr, nranks * recvlen * sizeof(float)));
            CUDA_ASSERT(cudaMalloc(&out_ptr, recvlen * sizeof(float)));

            CUDA_ASSERT(cudaMemcpy(in_ptr, input.data(),
                                   nranks * recvlen * sizeof(float),
                                   cudaMemcpyHostToDevice));

            int ret = comm->reduce_scatter(in_ptr, out_ptr, recvlen,
                                           MegRay::MEGRAY_FLOAT32, op, ctx);
            ASSERT_EQ(ret, 0);

            CUDA_ASSERT(cudaStreamSynchronize(stream));
            CUDA_ASSERT(cudaMemcpy(output.data(), out_ptr,
                                   recvlen * sizeof(float),
                                   cudaMemcpyDeviceToHost));
        };
        return run;
    };

    for (int k = 0; k < nranks; k++) {
        for (size_t j = 0; j < recvlen; j++) {
            float sum = 0;
            for (size_t i = 0; i < nranks; i++) {
                int m = k * recvlen + j;
                inputs[i][m] = 1.0 * (i + 1) * (m + 1);
                sum += inputs[i][m];
            }
            expected_outputs[k][j] = sum;
        }
    }
    run_test<float>(nranks, MegRay::MEGRAY_NCCL, inputs, expected_outputs,
444
                    reduce_func(MegRay::MEGRAY_SUM));
M
Megvii Engine Team 已提交
445
    run_test<float>(nranks, MegRay::MEGRAY_UCX, inputs, expected_outputs,
446
                    reduce_func(MegRay::MEGRAY_SUM));
M
Megvii Engine Team 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459

    for (int k = 0; k < nranks; k++) {
        for (size_t j = 0; j < recvlen; j++) {
            float max_val = std::numeric_limits<float>::min();
            for (size_t i = 0; i < nranks; i++) {
                int m = k * recvlen + j;
                inputs[i][m] = 1.0 * (i + 1) * (m + 1);
                max_val = std::max(inputs[i][m], max_val);
            }
            expected_outputs[k][j] = max_val;
        }
    }
    run_test<float>(nranks, MegRay::MEGRAY_NCCL, inputs, expected_outputs,
460
                    reduce_func(MegRay::MEGRAY_MAX));
M
Megvii Engine Team 已提交
461
    run_test<float>(nranks, MegRay::MEGRAY_UCX, inputs, expected_outputs,
462
                    reduce_func(MegRay::MEGRAY_MAX));
M
Megvii Engine Team 已提交
463 464 465 466 467 468 469 470 471 472 473 474
    for (int k = 0; k < nranks; k++) {
        for (size_t j = 0; j < recvlen; j++) {
            float min_val = std::numeric_limits<float>::max();
            for (size_t i = 0; i < nranks; i++) {
                int m = k * recvlen + j;
                inputs[i][m] = 1.0 * (i + 1) * (m + 1);
                min_val = std::min(inputs[i][m], min_val);
            }
            expected_outputs[k][j] = min_val;
        }
    }
    run_test<float>(nranks, MegRay::MEGRAY_NCCL, inputs, expected_outputs,
475
                    reduce_func(MegRay::MEGRAY_MIN));
M
Megvii Engine Team 已提交
476
    run_test<float>(nranks, MegRay::MEGRAY_UCX, inputs, expected_outputs,
477
                    reduce_func(MegRay::MEGRAY_MIN));
M
Megvii Engine Team 已提交
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
}

TEST(TestOpr, Broadcast) {
    const int nranks = 3;
    const int root = 1;
    const size_t len = 10;

    std::vector<std::vector<float>> inputs(nranks, std::vector<float>(len));
    std::vector<std::vector<float>> outputs(nranks, std::vector<float>(len));
    for (size_t j = 0; j < len; j++) {
        for (size_t i = 0; i < nranks; i++) {
            inputs[i][j] = 1.0 * (i + 1) * (j + 1);
        }
        for (size_t i = 0; i < nranks; i++) {
            outputs[i][j] = inputs[root][j];
        }
    }

    auto run = [nranks, root, len](std::shared_ptr<MegRay::Communicator> comm,
497
                                   int port, int rank,
M
Megvii Engine Team 已提交
498 499 500
                                   std::vector<float>& input,
                                   std::vector<float>& output) {
        CUDA_ASSERT(cudaSetDevice(rank));
501
        comm->init("localhost", port);
M
Megvii Engine Team 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

        cudaStream_t stream;
        CUDA_ASSERT(cudaStreamCreate(&stream));
        auto ctx = MegRay::CudaContext::make(stream);

        void *in_ptr, *out_ptr;
        CUDA_ASSERT(cudaMalloc(&in_ptr, len * sizeof(float)));
        CUDA_ASSERT(cudaMalloc(&out_ptr, len * sizeof(float)));

        CUDA_ASSERT(cudaMemcpy(in_ptr, input.data(), len * sizeof(float),
                               cudaMemcpyHostToDevice));

        int ret = comm->broadcast(in_ptr, out_ptr, len, MegRay::MEGRAY_FLOAT32,
                                  root, ctx);
        ASSERT_EQ(ret, 0);

        CUDA_ASSERT(cudaStreamSynchronize(stream));
        CUDA_ASSERT(cudaMemcpy(output.data(), out_ptr, len * sizeof(float),
                               cudaMemcpyDeviceToHost));
    };

523 524
    run_test<float>(nranks, MegRay::MEGRAY_NCCL, inputs, outputs, run);
    run_test<float>(nranks, MegRay::MEGRAY_UCX, inputs, outputs, run);
M
Megvii Engine Team 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538
}

TEST(TestOpr, ReduceSum) {
    const int nranks = 3;
    const int root = 1;
    const size_t len = 10;

    std::vector<std::vector<float>> inputs(nranks, std::vector<float>(len));
    std::vector<std::vector<float>> expected_outputs(nranks);
    expected_outputs[root].resize(len);

    auto reduce_func = [nranks, root, len](MegRay::ReduceOp op) {
        auto run = [nranks, root, len,
                    op](std::shared_ptr<MegRay::Communicator> comm,
539
                        int port, int rank,
M
Megvii Engine Team 已提交
540 541
                        std::vector<float>& input, std::vector<float>& output) {
            CUDA_ASSERT(cudaSetDevice(rank));
542
            comm->init("localhost", port);
M
Megvii Engine Team 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578

            cudaStream_t stream;
            CUDA_ASSERT(cudaStreamCreate(&stream));
            auto ctx = MegRay::CudaContext::make(stream);

            void *in_ptr, *out_ptr;
            CUDA_ASSERT(cudaMalloc(&in_ptr, len * sizeof(float)));
            if (rank == root) {
                CUDA_ASSERT(cudaMalloc(&out_ptr, len * sizeof(float)));
            }

            CUDA_ASSERT(cudaMemcpy(in_ptr, input.data(), len * sizeof(float),
                                   cudaMemcpyHostToDevice));

            int ret = comm->reduce(in_ptr, out_ptr, len, MegRay::MEGRAY_FLOAT32,
                                   op, root, ctx);
            ASSERT_EQ(ret, 0);

            CUDA_ASSERT(cudaStreamSynchronize(stream));
            if (rank == root) {
                CUDA_ASSERT(cudaMemcpy(output.data(), out_ptr,
                                       len * sizeof(float),
                                       cudaMemcpyDeviceToHost));
            }
        };
        return run;
    };
    for (size_t j = 0; j < len; j++) {
        float sum = 0;
        for (size_t i = 0; i < nranks; i++) {
            inputs[i][j] = 1.0 * (i + 1) * (j + 1);
            sum += inputs[i][j];
        }
        expected_outputs[root][j] = sum;
    }
    run_test<float>(nranks, MegRay::MEGRAY_NCCL, inputs, expected_outputs,
579
                    reduce_func(MegRay::MEGRAY_SUM));
M
Megvii Engine Team 已提交
580
    run_test<float>(nranks, MegRay::MEGRAY_UCX, inputs, expected_outputs,
581
                    reduce_func(MegRay::MEGRAY_SUM));
M
Megvii Engine Team 已提交
582 583 584 585 586 587 588 589 590
    for (size_t j = 0; j < len; j++) {
        float max_val = std::numeric_limits<float>::min();
        for (size_t i = 0; i < nranks; i++) {
            inputs[i][j] = 1.0 * (i + 1) * (j + 1);
            max_val = std::max(inputs[i][j], max_val);
        }
        expected_outputs[root][j] = max_val;
    }
    run_test<float>(nranks, MegRay::MEGRAY_NCCL, inputs, expected_outputs,
591
                    reduce_func(MegRay::MEGRAY_MAX));
M
Megvii Engine Team 已提交
592
    run_test<float>(nranks, MegRay::MEGRAY_UCX, inputs, expected_outputs,
593
                    reduce_func(MegRay::MEGRAY_MAX));
M
Megvii Engine Team 已提交
594 595 596 597 598 599 600 601 602
    for (size_t j = 0; j < len; j++) {
        float min_val = std::numeric_limits<float>::max();
        for (size_t i = 0; i < nranks; i++) {
            inputs[i][j] = 1.0 * (i + 1) * (j + 1);
            min_val = std::min(inputs[i][j], min_val);
        }
        expected_outputs[root][j] = min_val;
    }
    run_test<float>(nranks, MegRay::MEGRAY_NCCL, inputs, expected_outputs,
603
                    reduce_func(MegRay::MEGRAY_MIN));
M
Megvii Engine Team 已提交
604
    run_test<float>(nranks, MegRay::MEGRAY_UCX, inputs, expected_outputs,
605
                    reduce_func(MegRay::MEGRAY_MIN));
M
Megvii Engine Team 已提交
606
}