imate
C++/CUDA Reference
cuda_interface.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright 2021, Siavash Ameli <sameli@berkeley.edu>
3  * SPDX-License-Identifier: BSD-3-Clause
4  * SPDX-FileType: SOURCE
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the license found in the LICENSE.txt file in the root
8  * directory of this source tree.
9  */
10 
11 
12 #ifndef _CUDA_UTILITIES_CUDA_INTERFACE_H_
13 #define _CUDA_UTILITIES_CUDA_INTERFACE_H_
14 
15 
16 // =======
17 // Headers
18 // =======
19 
20 #include <cuda_runtime_api.h> // cudaError_t, cudaMalloc, cudaMemcpy,
21  // cudaSuccess, cudaFree
22 #include "../_definitions/types.h" // LongIndexType
23 
24 
25 // ==========
26 // Cuda Tools
27 // ==========
28 
34 
35 template<typename ArrayType>
37 {
38  public:
39 
40  // alloc 1
41  static ArrayType* alloc(const LongIndexType array_size);
42 
43  // alloc 2
44  static void alloc(
45  ArrayType*& device_array,
46  const LongIndexType array_size);
47 
48  // alloc bytes
49  static void alloc_bytes(
50  void*& device_array,
51  const size_t num_bytes);
52 
53  // copy to device
54  static void copy_to_device(
55  const ArrayType* host_array,
56  const LongIndexType array_size,
57  ArrayType* device_array);
58 
59  // del
60  static void del(void* device_array);
61 
62  // set device
63  static void set_device(int device_id);
64 
65  // get device
66  static int get_device();
67 };
68 
69 #endif // _CUDA_UTILITIES_CUDA_INTERFACE_H_
An interface to CUDA linrary to facilitate working with CUDA, such as memory allocation,...
static int get_device()
Gets the current device in multi-gpu applications.
static void del(void *device_array)
Deletes memory on gpu device if its pointer is not NULL, then sets the pointer to NULL.
static void alloc_bytes(void *&device_array, const size_t num_bytes)
Allocates memory on gpu device. This function uses an existing given pointer.
static ArrayType * alloc(const LongIndexType array_size)
Allocates memory on gpu device. This function creates a pointer and returns it.
static void copy_to_device(const ArrayType *host_array, const LongIndexType array_size, ArrayType *device_array)
Copies memory on host to device memory.
static void set_device(int device_id)
Sets the current device in multi-gpu applications.
int LongIndexType
Definition: types.h:60