cuda.h
Go to the documentation of this file.
1 /*******************************************************
2  * Copyright (c) 2014, ArrayFire
3  * All rights reserved.
4  *
5  * This file is distributed under 3-clause BSD license.
6  * The complete license agreement can be obtained at:
7  * http://arrayfire.com/licenses/BSD-3-Clause
8  ********************************************************/
9 
10 #pragma once
11 #include <af/defines.h>
12 #include <af/exception.h>
13 
18 
19 #ifdef __NVCC__
20 #include <cuda.h>
21 #include <cuda_runtime.h>
22 #include <cublas_v2.h>
23 #else
24 #ifdef AF_DEFINE_CUDA_TYPES
25 typedef struct CUstream_st *cudaStream_t;
26 
27 /*Enum for default math mode/tensor operation*/
28 typedef enum {
29  CUBLAS_DEFAULT_MATH = 0,
30  CUBLAS_TENSOR_OP_MATH = 1
31 } cublasMath_t;
32 #endif
33 #endif
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 
40 #if AF_API_VERSION >= 31
41 
50 AFAPI af_err afcu_get_stream(cudaStream_t* stream, int id);
51 #endif
52 
53 #if AF_API_VERSION >= 31
54 
63 AFAPI af_err afcu_get_native_id(int* nativeid, int id);
64 #endif
65 
66 #if AF_API_VERSION >= 32
67 
75 AFAPI af_err afcu_set_native_id(int nativeid);
76 #endif
77 
78 #if AF_API_VERSION >= 37
79 
89 AFAPI af_err afcu_cublasSetMathMode(cublasMath_t mode);
90 #endif
91 
92 #ifdef __cplusplus
93 }
94 #endif
95 
96 #ifdef __cplusplus
97 
98 namespace afcu
99 {
100 
101 #if AF_API_VERSION >= 31
102 
110 static inline cudaStream_t getStream(int id)
111 {
112  cudaStream_t retVal;
113  af_err err = afcu_get_stream(&retVal, id);
114  if (err!=AF_SUCCESS)
115  throw af::exception("Failed to get CUDA stream from ArrayFire");
116  return retVal;
117 }
118 #endif
119 
120 #if AF_API_VERSION >= 31
121 
129 static inline int getNativeId(int id)
130 {
131  int retVal;
132  af_err err = afcu_get_native_id(&retVal, id);
133  if (err!=AF_SUCCESS)
134  throw af::exception("Failed to get CUDA device native id from ArrayFire");
135  return retVal;
136 }
137 #endif
138 
139 #if AF_API_VERSION >= 32
140 
147 static inline void setNativeId(int nativeId)
148 {
149  af_err err = afcu_set_native_id(nativeId);
150  if (err!=AF_SUCCESS)
151  throw af::exception("Failed to change active CUDA device to the device with given native id");
152 }
153 #endif
154 
155 }
156 #endif
afcu::getStream
static cudaStream_t getStream(int id)
Get the stream for the CUDA device with id in ArrayFire context.
Definition: cuda.h:110
AFAPI
#define AFAPI
Definition: defines.h:38
afcu_get_stream
AFAPI af_err afcu_get_stream(cudaStream_t *stream, int id)
This file contain functions that apply only to the CUDA backend.
afcu::getNativeId
static int getNativeId(int id)
Get the native device id of the CUDA device with id in ArrayFire context.
Definition: cuda.h:129
afcu_cublasSetMathMode
AFAPI af_err afcu_cublasSetMathMode(cublasMath_t mode)
Sets the cuBLAS math mode for the internal handle.
afcu_get_native_id
AFAPI af_err afcu_get_native_id(int *nativeid, int id)
Get the native device id of the CUDA device with id in ArrayFire context.
afcu
Definition: cuda.h:98
exception.h
cuda.h
af::exception
An ArrayFire exception class.
Definition: exception.h:28
AF_SUCCESS
@ AF_SUCCESS
The function returned successfully.
Definition: defines.h:75
afcu::setNativeId
static void setNativeId(int nativeId)
Set the CUDA device with given native id as the active device for ArrayFire.
Definition: cuda.h:147
afcu_set_native_id
AFAPI af_err afcu_set_native_id(int nativeid)
Set the CUDA device with given native id as the active device for ArrayFire.
af_err
af_err
Definition: defines.h:71
defines.h