|
| 1 | +/* |
| 2 | + * Copyright 2024 Patrick Stotko |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +#ifndef STDGPU_CUDA_MEMORY_DETAIL_H |
| 17 | +#define STDGPU_CUDA_MEMORY_DETAIL_H |
| 18 | + |
| 19 | +#include <stdgpu/cuda/memory.h> |
| 20 | + |
| 21 | +#include <thrust/detail/execution_policy.h> |
| 22 | +#include <thrust/system/cuda/detail/util.h> |
| 23 | + |
| 24 | +#include <stdgpu/cuda/impl/error.h> |
| 25 | + |
| 26 | +namespace stdgpu::cuda |
| 27 | +{ |
| 28 | + |
| 29 | +template <typename ExecutionPolicy, STDGPU_DETAIL_OVERLOAD_IF(is_execution_policy_v<remove_cvref_t<ExecutionPolicy>>)> |
| 30 | +void |
| 31 | +memcpy_impl(ExecutionPolicy&& policy, |
| 32 | + void* destination, |
| 33 | + const void* source, |
| 34 | + index64_t bytes, |
| 35 | + cudaMemcpyKind kind, |
| 36 | + bool needs_sychronization) |
| 37 | +{ |
| 38 | + cudaStream_t stream = thrust::cuda_cub::stream(thrust::detail::derived_cast(thrust::detail::strip_const(policy))); |
| 39 | + |
| 40 | + STDGPU_CUDA_SAFE_CALL(cudaMemcpyAsync(destination, source, static_cast<std::size_t>(bytes), kind, stream)); |
| 41 | + if (needs_sychronization) |
| 42 | + { |
| 43 | + STDGPU_CUDA_SAFE_CALL(cudaStreamSynchronize(stream)); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +template <typename ExecutionPolicy, |
| 48 | + STDGPU_DETAIL_OVERLOAD_DEFINITION_IF(is_execution_policy_v<remove_cvref_t<ExecutionPolicy>>)> |
| 49 | +void |
| 50 | +memcpy_device_to_device(ExecutionPolicy&& policy, void* destination, const void* source, index64_t bytes) |
| 51 | +{ |
| 52 | + memcpy_impl(std::forward<ExecutionPolicy>(policy), destination, source, bytes, cudaMemcpyDeviceToDevice, false); |
| 53 | +} |
| 54 | + |
| 55 | +template <typename ExecutionPolicy, |
| 56 | + STDGPU_DETAIL_OVERLOAD_DEFINITION_IF(is_execution_policy_v<remove_cvref_t<ExecutionPolicy>>)> |
| 57 | +void |
| 58 | +memcpy_device_to_host(ExecutionPolicy&& policy, void* destination, const void* source, index64_t bytes) |
| 59 | +{ |
| 60 | + memcpy_impl(std::forward<ExecutionPolicy>(policy), destination, source, bytes, cudaMemcpyDeviceToHost, true); |
| 61 | +} |
| 62 | + |
| 63 | +template <typename ExecutionPolicy, |
| 64 | + STDGPU_DETAIL_OVERLOAD_DEFINITION_IF(is_execution_policy_v<remove_cvref_t<ExecutionPolicy>>)> |
| 65 | +void |
| 66 | +memcpy_host_to_device(ExecutionPolicy&& policy, void* destination, const void* source, index64_t bytes) |
| 67 | +{ |
| 68 | + memcpy_impl(std::forward<ExecutionPolicy>(policy), destination, source, bytes, cudaMemcpyHostToDevice, false); |
| 69 | +} |
| 70 | + |
| 71 | +template <typename ExecutionPolicy, |
| 72 | + STDGPU_DETAIL_OVERLOAD_DEFINITION_IF(is_execution_policy_v<remove_cvref_t<ExecutionPolicy>>)> |
| 73 | +void |
| 74 | +memcpy_host_to_host(ExecutionPolicy&& policy, void* destination, const void* source, index64_t bytes) |
| 75 | +{ |
| 76 | + memcpy_impl(std::forward<ExecutionPolicy>(policy), destination, source, bytes, cudaMemcpyHostToHost, true); |
| 77 | +} |
| 78 | + |
| 79 | +} // namespace stdgpu::cuda |
| 80 | + |
| 81 | +#endif // STDGPU_CUDA_MEMORY_DETAIL_H |
0 commit comments