pinned_host_vector.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2023, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <rmm/mr/host/pinned_memory_resource.hpp>
20 
21 namespace ML {
22 
23 template <typename T>
25  public:
26  pinned_host_vector() = default;
27 
28  explicit pinned_host_vector(std::size_t n)
29  : size_{n}, data_{static_cast<T*>(pinned_mr.allocate(n * sizeof(T)))}
30  {
31  std::uninitialized_fill(data_, data_ + n, static_cast<T>(0));
32  }
33  ~pinned_host_vector() { pinned_mr.deallocate(data_, size_ * sizeof(T)); }
34 
39 
40  void resize(std::size_t n)
41  {
42  size_ = n;
43  data_ = static_cast<T*>(pinned_mr.allocate(n * sizeof(T)));
44  std::uninitialized_fill(data_, data_ + n, static_cast<T>(0));
45  }
46 
47  T* data() { return data_; }
48 
49  T* begin() { return data_; }
50 
51  T* end() { return data_ + size_; }
52 
53  std::size_t size() { return size_; }
54 
55  T operator[](std::size_t idx) const { return *(data_ + idx); }
56  T& operator[](std::size_t idx) { return *(data_ + idx); }
57 
58  private:
59  rmm::mr::pinned_memory_resource pinned_mr{};
60  T* data_;
61  std::size_t size_;
62 };
63 
64 } // namespace ML
Definition: pinned_host_vector.hpp:24
pinned_host_vector & operator=(pinned_host_vector &&)=delete
T & operator[](std::size_t idx)
Definition: pinned_host_vector.hpp:56
pinned_host_vector()=default
T * begin()
Definition: pinned_host_vector.hpp:49
pinned_host_vector(pinned_host_vector const &)=delete
T operator[](std::size_t idx) const
Definition: pinned_host_vector.hpp:55
void resize(std::size_t n)
Definition: pinned_host_vector.hpp:40
T * end()
Definition: pinned_host_vector.hpp:51
T * data()
Definition: pinned_host_vector.hpp:47
pinned_host_vector(std::size_t n)
Definition: pinned_host_vector.hpp:28
pinned_host_vector(pinned_host_vector &&)=delete
std::size_t size()
Definition: pinned_host_vector.hpp:53
~pinned_host_vector()
Definition: pinned_host_vector.hpp:33
pinned_host_vector & operator=(pinned_host_vector const &)=delete
Definition: dbscan.hpp:30