| 1 |
/* |
| 2 |
* Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 |
* |
| 4 |
* This source code is licensed under the MIT license found in the |
| 5 |
* LICENSE file in the root directory of this source tree. |
| 6 |
*/ |
| 7 |
|
| 8 |
// -*- c -*- |
| 9 |
|
| 10 |
#ifndef FAISS_GPU_RESOURCES_C_H |
| 11 |
#define FAISS_GPU_RESOURCES_C_H |
| 12 |
|
| 13 |
#include <cublas_v2.h> |
| 14 |
#include <cuda_runtime_api.h> |
| 15 |
#include "../faiss_c.h" |
| 16 |
|
| 17 |
#ifdef __cplusplus |
| 18 |
extern "C" { |
| 19 |
#endif |
| 20 |
|
| 21 |
/// Base class of GPU-side resource provider; hides provision of |
| 22 |
/// cuBLAS handles, CUDA streams and a temporary memory manager |
| 23 |
FAISS_DECLARE_CLASS(GpuResources) |
| 24 |
|
| 25 |
FAISS_DECLARE_DESTRUCTOR(GpuResources) |
| 26 |
|
| 27 |
/// Call to pre-allocate resources for a particular device. If this is |
| 28 |
/// not called, then resources will be allocated at the first time |
| 29 |
/// of demand |
| 30 |
int faiss_GpuResources_initializeForDevice(FaissGpuResources*, int); |
| 31 |
|
| 32 |
/// Returns the cuBLAS handle that we use for the given device |
| 33 |
int faiss_GpuResources_getBlasHandle(FaissGpuResources*, int, cublasHandle_t*); |
| 34 |
|
| 35 |
/// Returns the stream that we order all computation on for the |
| 36 |
/// given device |
| 37 |
int faiss_GpuResources_getDefaultStream(FaissGpuResources*, int, cudaStream_t*); |
| 38 |
|
| 39 |
/// Returns the available CPU pinned memory buffer |
| 40 |
int faiss_GpuResources_getPinnedMemory(FaissGpuResources*, void**, size_t*); |
| 41 |
|
| 42 |
/// Returns the stream on which we perform async CPU <-> GPU copies |
| 43 |
int faiss_GpuResources_getAsyncCopyStream( |
| 44 |
FaissGpuResources*, |
| 45 |
int, |
| 46 |
cudaStream_t*); |
| 47 |
|
| 48 |
/// Calls getBlasHandle with the current device |
| 49 |
int faiss_GpuResources_getBlasHandleCurrentDevice( |
| 50 |
FaissGpuResources*, |
| 51 |
cublasHandle_t*); |
| 52 |
|
| 53 |
/// Calls getDefaultStream with the current device |
| 54 |
int faiss_GpuResources_getDefaultStreamCurrentDevice( |
| 55 |
FaissGpuResources*, |
| 56 |
cudaStream_t*); |
| 57 |
|
| 58 |
/// Synchronizes the CPU with respect to the default stream for the |
| 59 |
/// given device |
| 60 |
// equivalent to cudaDeviceSynchronize(getDefaultStream(device)) |
| 61 |
int faiss_GpuResources_syncDefaultStream(FaissGpuResources*, int); |
| 62 |
|
| 63 |
/// Calls syncDefaultStream for the current device |
| 64 |
int faiss_GpuResources_syncDefaultStreamCurrentDevice(FaissGpuResources*); |
| 65 |
|
| 66 |
/// Calls getAsyncCopyStream for the current device |
| 67 |
int faiss_GpuResources_getAsyncCopyStreamCurrentDevice( |
| 68 |
FaissGpuResources*, |
| 69 |
cudaStream_t*); |
| 70 |
|
| 71 |
FAISS_DECLARE_CLASS(GpuResourcesProvider) |
| 72 |
|
| 73 |
FAISS_DECLARE_DESTRUCTOR(GpuResourcesProvider) |
| 74 |
|
| 75 |
int faiss_GpuResourcesProvider_getResources( |
| 76 |
FaissGpuResourcesProvider*, |
| 77 |
FaissGpuResources**); |
| 78 |
|
| 79 |
#ifdef __cplusplus |
| 80 |
} |
| 81 |
#endif |
| 82 |
#endif |