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.3 kB

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