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

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

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