ml_cuda_utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2024, 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 <raft/util/cudart_utils.hpp>
20 
21 #include <cuda_runtime.h>
22 
23 namespace ML {
24 
25 inline int get_device(const void* ptr)
26 {
27  cudaPointerAttributes att;
28  cudaPointerGetAttributes(&att, ptr);
29  return att.device;
30 }
31 
32 inline cudaMemoryType memory_type(const void* p)
33 {
34  cudaPointerAttributes att;
35  cudaError_t err = cudaPointerGetAttributes(&att, p);
36  ASSERT(err == cudaSuccess || err == cudaErrorInvalidValue, "%s", cudaGetErrorString(err));
37 
38  if (err == cudaErrorInvalidValue) {
39  // Make sure the current thread error status has been reset
40  err = cudaGetLastError();
41  ASSERT(err == cudaErrorInvalidValue, "%s", cudaGetErrorString(err));
42  }
43  return att.type;
44 }
45 
46 inline bool is_device_or_managed_type(const void* p)
47 {
48  cudaMemoryType p_memory_type = memory_type(p);
49  return p_memory_type == cudaMemoryTypeDevice || p_memory_type == cudaMemoryTypeManaged;
50 }
51 
52 } // namespace ML
Definition: dbscan.hpp:30
bool is_device_or_managed_type(const void *p)
Definition: ml_cuda_utils.h:46
cudaMemoryType memory_type(const void *p)
Definition: ml_cuda_utils.h:32
int get_device(const void *ptr)
Definition: ml_cuda_utils.h:25