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.

checksum.cpp 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * \file dnn/test/cuda/checksum.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 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 "test/cuda/fixture.h"
  12. #include "megdnn/oprs.h"
  13. #include "test/common/checker.h"
  14. using namespace megdnn;
  15. using namespace test;
  16. TEST_F(CUDA, CHECKSUM_FORWARD) {
  17. auto cuda_opr = handle_cuda()->create_operator<megdnn::Checksum>(),
  18. naive_opr = handle_naive()->create_operator<megdnn::Checksum>();
  19. std::mt19937 rng(std::random_device{}());
  20. for (size_t size: {3, 8, 12345, 1024 * 1024, 1024 * 1024 * 10}) {
  21. auto aligned_size = size + ((512 - size % 512) % 512);
  22. auto run = [&](megdnn::Checksum *opr, void *ptr, bool log_size) {
  23. TensorND tensor;
  24. tensor.raw_ptr = ptr;
  25. tensor.layout.init_contiguous_stride({size});
  26. tensor.layout.dtype = dtype::Byte();
  27. WorkspaceWrapper workspace(handle_cuda(),
  28. opr->get_workspace_in_bytes(tensor.layout));
  29. if (log_size) {
  30. printf("checksum(%zu): workspace=%zu\n", size,
  31. workspace.workspace().size);
  32. }
  33. return opr->exec(tensor, workspace.workspace());
  34. };
  35. std::vector<uint8_t> buf(aligned_size);
  36. for (size_t i = 0; i < size; ++ i)
  37. buf[i] = rng();
  38. auto run_offsset = [&](size_t offset) {
  39. void* dev_ptr = megdnn_malloc(handle_cuda(), buf.size() + offset);
  40. void* dev_buf = static_cast<char*>(dev_ptr) + offset;
  41. Checksum::Result res_cuda[2], res_naive[2];
  42. for (int change_last = 0; change_last < 2; ++ change_last) {
  43. if (change_last)
  44. ++ buf[size - 1];
  45. megdnn_memcpy_H2D(handle_cuda(), dev_buf, buf.data(), size);
  46. res_cuda[change_last] = run(cuda_opr.get(), dev_buf, !change_last);
  47. res_naive[change_last] = run(naive_opr.get(), buf.data(), false);
  48. }
  49. megdnn_free(handle_cuda(), dev_ptr);
  50. ASSERT_EQ(res_naive[0], res_cuda[0]) << "failed for size " << size;
  51. ASSERT_EQ(res_naive[1], res_cuda[1]);
  52. ASSERT_NE(res_cuda[0], res_cuda[1]);
  53. };
  54. for (size_t i = 0; i < 8; ++i) {
  55. run_offsset(i);
  56. }
  57. }
  58. }
  59. // vim: syntax=cpp.doxygen

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