thrust_rmm_allocator.h
1 /*
2  * Copyright (c) 2018-2020, 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/cuda_stream_view.hpp>
20 #include <rmm/device_vector.hpp>
21 #include <rmm/mr/device/thrust_allocator_adaptor.hpp>
22 
23 #include <thrust/execution_policy.h>
24 
25 namespace rmm {
26 
27 using par_t = decltype(thrust::cuda::par(*(new rmm::mr::thrust_allocator<char>())));
28 using deleter_t = std::function<void(par_t *)>;
29 using exec_policy_t = std::unique_ptr<par_t, deleter_t>;
30 
40 inline exec_policy_t exec_policy(cudaStream_t stream = 0)
41 {
42  auto *alloc = new rmm::mr::thrust_allocator<char>(stream);
43  auto deleter = [alloc](par_t *pointer) {
44  delete alloc;
45  delete pointer;
46  };
47 
48  exec_policy_t policy{new par_t(*alloc), deleter};
49  return policy;
50 }
51 
52 } // namespace rmm
rmm::mr::thrust_allocator
An allocator compatible with Thrust containers and algorithms using a device_memory_resource for memo...
Definition: thrust_allocator_adaptor.hpp:38
rmm::exec_policy
auto exec_policy(cuda_stream_view stream=cuda_stream_default, rmm::mr::device_memory_resource *mr=mr::get_current_device_resource())
Returns a Thrust CUDA execution policy that uses RMM for temporary memory allocation on the specified...
Definition: exec_policy.hpp:33