| 23 | namespace cuda { |
| 24 | template <typename T> |
| 25 | class SharedArray { |
| 26 | static_assert(std::is_pod<T>::value, "Unsupported datatype"); |
| 27 | |
| 28 | public: |
| 29 | using device_t = thrust::device_vector<T>; |
| 30 | using host_t = pinned_vector<T>; |
| 31 | |
| 32 | SharedArray() = default; |
| 33 | |
| 34 | explicit SharedArray(size_t size) { |
| 35 | d_buffer_.resize(size); |
| 36 | h_buffer_.resize(size); |
| 37 | } |
| 38 | |
| 39 | void resize(size_t size) { |
| 40 | d_buffer_.resize(size); |
| 41 | h_buffer_.resize(size); |
| 42 | } |
| 43 | |
| 44 | void set(size_t idx, const T& t) { d_buffer_[idx] = t; } |
| 45 | |
| 46 | void set(size_t idx, const T& t, const Stream& stream) { |
| 47 | h_buffer_[idx] = t; |
| 48 | CHECK_CUDA(cudaMemcpyAsync(thrust::raw_pointer_cast(d_buffer_.data()), |
| 49 | thrust::raw_pointer_cast(h_buffer_.data()), |
| 50 | sizeof(T), cudaMemcpyHostToDevice, |
| 51 | stream.cuda_stream())); |
| 52 | } |
| 53 | |
| 54 | void fill(const T& t) { |
| 55 | auto size = h_buffer_.size(); |
| 56 | thrust::fill_n(h_buffer_.data(), size, t); |
| 57 | d_buffer_ = h_buffer_; |
| 58 | } |
| 59 | |
| 60 | void fill(const T& t, const Stream& stream) { |
| 61 | auto size = h_buffer_.size(); |
| 62 | |
| 63 | thrust::fill_n(h_buffer_.data(), size, t); |
| 64 | CHECK_CUDA(cudaMemcpyAsync(thrust::raw_pointer_cast(d_buffer_.data()), |
| 65 | thrust::raw_pointer_cast(h_buffer_.data()), |
| 66 | sizeof(T) * size, cudaMemcpyHostToDevice, |
| 67 | stream.cuda_stream())); |
| 68 | } |
| 69 | |
| 70 | typename thrust::device_vector<T>::reference get(size_t idx) { |
| 71 | return d_buffer_[idx]; |
| 72 | } |
| 73 | |
| 74 | typename thrust::device_vector<T>::const_reference get(size_t idx) const { |
| 75 | return d_buffer_[idx]; |
| 76 | } |
| 77 | |
| 78 | T get(size_t idx, const Stream& stream) const { |
| 79 | CHECK_CUDA(cudaMemcpyAsync( |
| 80 | const_cast<T*>((thrust::raw_pointer_cast(h_buffer_.data()) + idx)), |
| 81 | thrust::raw_pointer_cast(d_buffer_.data()) + idx, sizeof(T), |
| 82 | cudaMemcpyDeviceToHost, stream.cuda_stream())); |
nothing calls this directly
no test coverage detected