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 3.9 kB

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