MCPcopy Create free account
hub / github.com/AdaptiveCpp/AdaptiveCpp / test_interop

Function test_interop

tests/sycl/extensions.cpp:422–479  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

420
421template<sycl::backend B>
422void test_interop(sycl::queue& q) {
423 const std::size_t test_size = 1024;
424
425 std::vector<int> initial_data(test_size, 14);
426 std::vector<int> target_data(test_size);
427 int* target_ptr = target_data.data();
428
429 sycl::buffer<int, 1> buff{initial_data.data(), sycl::range<1>{test_size}};
430
431 q.submit([&](sycl::handler &cgh) {
432 auto acc = buff.get_access<sycl::access::mode::read>(cgh);
433
434 cgh.AdaptiveCpp_enqueue_custom_operation([=](sycl::interop_handle &h) {
435 // All backends support obtaining native memory
436 void *native_mem = h.get_native_mem<B>(acc);
437
438 // OpenMP backend doesn't support extracting a native queue or device
439 if constexpr(B == sycl::backend::cuda) {
440 auto stream = h.get_native_queue<B>();
441 // dev is not really used, just test that this function call works for now
442 typename sycl::backend_traits<B>::template native_type<sycl::device> dev =
443 h.get_native_device<B>();
444
445 // Even though we can target multiple backends simultaneously,
446 // the HIP headers cannot be included simultaneously with CUDA.
447 // We can therefore only directly call either CUDA or HIP runtime functions.
448#if ACPP_LIBKERNEL_COMPILER_SUPPORTS_CUDA
449 cudaMemcpyAsync(target_ptr, native_mem, test_size * sizeof(int),
450 cudaMemcpyDeviceToHost, stream);
451#endif
452 }
453 else if constexpr(B == sycl::backend::hip) {
454
455 auto stream = h.get_native_queue<B>();
456 // dev is not really used, just test that this function call works for now
457 typename sycl::backend_traits<B>::template native_type<sycl::device> dev =
458 h.get_native_device<B>();
459
460#if ACPP_LIBKERNEL_COMPILER_SUPPORTS_HIP
461 hipMemcpyAsync(target_ptr, native_mem, test_size * sizeof(int),
462 hipMemcpyDeviceToHost, stream);
463#endif
464 }
465 });
466 });
467
468 q.wait();
469
470 constexpr bool has_hip_memcpy_test = (B == sycl::backend::hip) &&
471 ACPP_LIBKERNEL_COMPILER_SUPPORTS_HIP;
472 constexpr bool has_cuda_memcpy_test = (B == sycl::backend::cuda) &&
473 ACPP_LIBKERNEL_COMPILER_SUPPORTS_CUDA;
474 if constexpr (has_hip_memcpy_test || has_cuda_memcpy_test) {
475 for (std::size_t i = 0; i < test_size; ++i) {
476 BOOST_TEST(initial_data[i] == target_data[i]);
477 }
478 }
479}

Callers

nothing calls this directly

Calls 3

submitMethod · 0.45
waitMethod · 0.45

Tested by

no test coverage detected