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.

computing.cpp 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * \file dnn/test/rocm/megcore/computing.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 "hcc_detail/hcc_defs_prologue.h"
  12. #include "megcore.h"
  13. #include "megcore_rocm.h"
  14. #include "test/common/utils.h"
  15. #include "test/rocm/utils.h"
  16. #include "./fixture.h"
  17. #include "hip_header.h"
  18. TEST_F(MegcoreROCM, COMPUTING)
  19. {
  20. for (int id = -1; id < std::min(nr_devices(), 2); ++id) {
  21. megcoreDeviceHandle_t devHandle;
  22. megcoreCreateDeviceHandle(&devHandle,
  23. megcorePlatformROCM, id, 0);
  24. megcoreActivate(devHandle);
  25. megcoreComputingHandle_t compHandle;
  26. megcoreCreateComputingHandle(&compHandle,
  27. devHandle, 0);
  28. megcoreDeviceHandle_t devHandle2;
  29. megcoreGetDeviceHandle(compHandle, &devHandle2);
  30. ASSERT_EQ(devHandle, devHandle2);
  31. unsigned int flags;
  32. megcoreGetComputingFlags(compHandle, &flags);
  33. ASSERT_EQ(0u, flags);
  34. unsigned char *src, *dst;
  35. static const size_t N = 5;
  36. unsigned char src_host[N], dst_host[N];
  37. megcoreMalloc(devHandle, (void **)&src, N);
  38. megcoreMalloc(devHandle, (void **)&dst, N);
  39. megcoreMemset(compHandle, src, 0x0F, N);
  40. megcoreMemset(compHandle, dst, 0xF0, N);
  41. megcoreMemcpy(compHandle, src_host, src, N,
  42. megcoreMemcpyDeviceToHost);
  43. megcoreMemcpy(compHandle, dst_host, dst, N,
  44. megcoreMemcpyDeviceToHost);
  45. megcoreSynchronize(compHandle);
  46. for (size_t i = 0; i < N; ++i) {
  47. ASSERT_EQ(0x0F, src_host[i]);
  48. ASSERT_EQ(0xF0, dst_host[i]);
  49. }
  50. megcoreMemcpy(compHandle, dst, src, N,
  51. megcoreMemcpyDeviceToDevice);
  52. megcoreMemcpy(compHandle, src_host, src, N,
  53. megcoreMemcpyDeviceToHost);
  54. megcoreMemcpy(compHandle, dst_host, dst, N,
  55. megcoreMemcpyDeviceToHost);
  56. megcoreSynchronize(compHandle);
  57. for (size_t i = 0; i < N; ++i) {
  58. ASSERT_EQ(dst_host[i], src_host[i]);
  59. }
  60. megcoreFree(devHandle, src);
  61. megcoreFree(devHandle, dst);
  62. megcoreDestroyComputingHandle(compHandle);
  63. megcoreDestroyDeviceHandle(devHandle);
  64. }
  65. }
  66. TEST_F(MegcoreROCM, STREAM)
  67. {
  68. megcoreDeviceHandle_t devHandle;
  69. megcoreCreateDeviceHandle(&devHandle,
  70. megcorePlatformROCM, 0, 0);
  71. megcoreActivate(devHandle);
  72. hipStream_t stream;
  73. hip_check(hipStreamCreateWithFlags(&stream, hipStreamNonBlocking));
  74. megcoreComputingHandle_t compHandle;
  75. megcoreCreateComputingHandleWithROCMStream(&compHandle,
  76. devHandle, 0, stream);
  77. {
  78. hipStream_t stream2;
  79. megcoreGetROCMStream(compHandle, &stream2);
  80. ASSERT_EQ(stream, stream2);
  81. }
  82. megcoreDeviceHandle_t devHandle2;
  83. megcoreGetDeviceHandle(compHandle, &devHandle2);
  84. ASSERT_EQ(devHandle, devHandle2);
  85. unsigned int flags;
  86. megcoreGetComputingFlags(compHandle, &flags);
  87. ASSERT_EQ(0u, flags);
  88. unsigned char *src, *dst;
  89. static const size_t N = 5;
  90. unsigned char src_host[N], dst_host[N];
  91. megcoreMalloc(devHandle, (void **)&src, N);
  92. megcoreMalloc(devHandle, (void **)&dst, N);
  93. megcoreMemset(compHandle, src, 0x0F, N);
  94. megcoreMemset(compHandle, dst, 0xF0, N);
  95. megcoreMemcpy(compHandle, src_host, src, N,
  96. megcoreMemcpyDeviceToHost);
  97. megcoreMemcpy(compHandle, dst_host, dst, N,
  98. megcoreMemcpyDeviceToHost);
  99. megcoreSynchronize(compHandle);
  100. for (size_t i = 0; i < N; ++i) {
  101. ASSERT_EQ(0x0F, src_host[i]);
  102. ASSERT_EQ(0xF0, dst_host[i]);
  103. }
  104. megcoreMemcpy(compHandle, dst, src, N,
  105. megcoreMemcpyDeviceToDevice);
  106. megcoreMemcpy(compHandle, src_host, src, N,
  107. megcoreMemcpyDeviceToHost);
  108. megcoreMemcpy(compHandle, dst_host, dst, N,
  109. megcoreMemcpyDeviceToHost);
  110. megcoreSynchronize(compHandle);
  111. for (size_t i = 0; i < N; ++i) {
  112. ASSERT_EQ(dst_host[i], src_host[i]);
  113. }
  114. megcoreFree(devHandle, src);
  115. megcoreFree(devHandle, dst);
  116. megcoreDestroyComputingHandle(compHandle);
  117. megcoreDestroyDeviceHandle(devHandle);
  118. hip_check(hipStreamDestroy(stream));
  119. }
  120. // vim: syntax=cpp.doxygen

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