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

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