You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

io_remote.cpp 2.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * \file imperative/src/test/io_remote.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "./helper.h"
  12. #include "megbrain/imperative/ops/autogen.h"
  13. #include "megbrain/opr/mm_handler.h"
  14. using namespace mgb;
  15. using namespace imperative;
  16. TEST(TestImperative, IORemote) {
  17. REQUIRE_GPU(2);
  18. const char* server_addr = "127.0.0.1";
  19. uint32_t port = 4567;
  20. mgb_assert(create_zmqrpc_server(server_addr, port) > 0);
  21. HostTensorGenerator<> gen;
  22. CompNode cn0 = CompNode::load("gpu0"), cn1 = CompNode::load("gpu1");
  23. size_t vector_size = 233;
  24. auto host_x = gen({vector_size}, cn0), host_y = gen({vector_size}, cn1);
  25. auto expect = gen({vector_size});
  26. for (size_t i = 0; i < vector_size; ++i) {
  27. expect->ptr<float>()[i] = host_x->ptr<float>()[i];
  28. }
  29. auto run_send = [&](std::shared_ptr<HostTensorND> hnd) {
  30. auto def = imperative::RemoteSend::make(
  31. "io_remote_test", server_addr, port, 1, "nccl");
  32. auto inp = Tensor::make(*hnd);
  33. auto oup = OpDef::apply_on_physical_tensor(*def, {inp});
  34. };
  35. auto run_recv = [&](std::shared_ptr<HostTensorND> hnd) {
  36. auto def = imperative::RemoteRecv::make(
  37. "io_remote_test", server_addr, port, 0,
  38. CompNode::load("gpu1"), TensorShape{vector_size},
  39. dtype::Float32(), "nccl");
  40. auto inp = Tensor::make(*hnd);
  41. auto oup = OpDef::apply_on_physical_tensor(*def, {inp});
  42. HostTensorND host_v;
  43. host_v.copy_from(oup[0]->dev_tensor()).sync();
  44. MGB_ASSERT_TENSOR_NEAR(*expect, host_v, 1e-6);
  45. };
  46. std::thread t0(std::bind(run_send, host_x));
  47. std::thread t1(std::bind(run_recv, host_y));
  48. t0.join();
  49. t1.join();
  50. }
  51. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}
  52. // ./imperative_test --gtest_filter TestIORemote

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台