1 module derelict.cudnn5;
2 
3 /**
4     Translation of cudnn.h
5 */
6 
7 import derelict.cuda.runtimeapi;
8 import derelict.util.loader;
9 
10 private
11 {
12     import derelict.util.system;
13 
14     static if(Derelict_OS_Linux)
15     {
16         version(X86_64)
17             enum libNames = "libcudnn.so.5";
18         else
19             static assert(0, "Need to implement cuDNN libNames for this arch.");
20     }
21     else static if(Derelict_OS_Windows)
22     {
23         version(X86_64)
24             enum libNames = "cudnn64_5.dll";
25         else
26             static assert(0, "There are no cuDNN libNames for this arch and operating system.");
27     }
28     else static if(Derelict_OS_Mac)
29     {
30         version(X86_64)
31             enum libNames = "libcudnn.5.dylib,libcudnn.dylib";
32         else
33             static assert(0, "There are no cuDNN libNames for this arch and operating system.");
34     }
35     else
36     {
37         static assert(0, "Need to implement cuDNN libNames for this operating system.");
38     }
39 
40     enum functionTypes = [
41         ["cudnnCreate", "cudnnHandle_t *"],
42         ["cudnnDestroy", "cudnnHandle_t"],
43         ["cudnnSetStream", "cudnnHandle_t", "cudaStream_t"],
44         ["cudnnGetStream", "cudnnHandle_t", "cudaStream_t *"],
45 
46 
47         ["cudnnCreateTensorDescriptor", "cudnnTensorDescriptor_t *"],
48         ["cudnnSetTensor4dDescriptor", "cudnnTensorDescriptor_t", "cudnnTensorFormat_t", "cudnnDataType_t", "int",
49             "int", "int", "int"],
50         ["cudnnSetTensor4dDescriptorEx", "cudnnTensorDescriptor_t", "cudnnDataType_t", "int", "int", "int", "int",
51             "int", "int", "int", "int"],
52         ["cudnnGetTensor4dDescriptor", "const cudnnTensorDescriptor_t", "cudnnDataType_t *", "int *", "int *", "int *",
53             "int *", "int *", "int *", "int *", "int *"],
54         ["cudnnSetTensorNdDescriptor", "cudnnTensorDescriptor_t", "cudnnDataType_t", "int", "const int", "const int"],
55         ["cudnnGetTensorNdDescriptor", "const cudnnTensorDescriptor_t", "int", "cudnnDataType_t *", "int *", "int",
56             "int"],
57         ["cudnnDestroyTensorDescriptor", "cudnnTensorDescriptor_t"],
58         ["cudnnTransformTensor", "cudnnHandle_t", "const void *", "const cudnnTensorDescriptor_t", "const void *",
59             "const void *", "const cudnnTensorDescriptor_t", "void *"],
60         ["cudnnAddTensor", "cudnnHandle_t", "const void *", "const cudnnTensorDescriptor_t", "const void *",
61             "const void *", "const cudnnTensorDescriptor_t", "void *"],
62         
63 
64         ["cudnnCreateOpTensorDescriptor", "cudnnOpTensorDescriptor_t *"],
65         ["cudnnSetOpTensorDescriptor", "cudnnOpTensorDescriptor_t", "cudnnOpTensorOp_t", "cudnnDataType_t",
66             "cudnnNanPropagation_t"],
67         ["cudnnGetOpTensorDescriptor", "const cudnnOpTensorDescriptor_t", "cudnnOpTensorOp_t *", "cudnnDataType_t *",
68             "cudnnNanPropagation_t *"],
69         ["cudnnDestroyOpTensorDescriptor", "cudnnOpTensorDescriptor_t"],
70         ["cudnnOpTensor", "cudnnHandle_t", "const cudnnOpTensorDescriptor_t", "const void *",
71             "const cudnnTensorDescriptor_t", "const void *", "const void *", "const cudnnTensorDescriptor_t",
72             "const void *", "const void *", "const cudnnTensorDescriptor_t", "void *"],
73         ["cudnnSetTensor", "cudnnHandle_t", "const cudnnTensorDescriptor_t", "void *", "const void *"],
74         ["cudnnScaleTensor", "cudnnHandle_t", "const cudnnTensorDescriptor_t", "void *", "const void *"],
75         
76 
77         ["cudnnCreateFilterDescriptor", "cudnnFilterDescriptor_t *"],
78         ["cudnnSetFilter4dDescriptor", "cudnnFilterDescriptor_t", "cudnnDataType_t", "cudnnTensorFormat_t", "int",
79 			"int", "int", "int"],
80         ["cudnnGetFilter4dDescriptor", "cudnnDataType_t *", "cudnnTensorFormat_t *", "int *", "int *", "int *",
81 			"int *"],
82         ["cudnnSetFilterNdDescriptor", "cudnnFilterDescriptor_t", "cudnnDataType_t", "cudnnTensorFormat_t", "int",
83             "const int[]"],
84         ["cudnnGetFilterNdDescriptor", "const cudnnFilterDescriptor_t", "int", "cudnnDataType_t *",
85             "cudnnTensorFormat_t *", "int *", "int[]"],
86         ["cudnnDestroyFilterDescriptor", "cudnnFilterDescriptor_t"],
87         ["cudnnCreateConvolutionDescriptor", "cudnnConvolutionDescriptor_t *"],
88         ["cudnnSetConvolution2dDescriptor", "cudnnConvolutionDescriptor_t", "int", "int", "int", "int", "int", "int",
89             "cudnnConvolutionMode_t"],
90         ["cudnnSetConvolution2dDescriptor_v5", "cudnnConvolutionDescriptor_t", "int", "int", "int", "int", "int",
91             "int", "cudnnConvolutionMode_t", "cudnnDataType_t"],
92         ["cudnnGetConvolution2dDescriptor", "const cudnnConvolutionDescriptor_t", "int *", "int *", "int *", "int *",
93             "int *", "int *", "cudnnConvolutionMode_t *"],
94         ["cudnnGetConvolution2dDescriptor_v5", "const cudnnConvolutionDescriptor_t", "int *", "int *", "int *",
95             "int *", "int *", "int *", "cudnnConvolutionMode_t *", "cudnnDataType_t *"],
96         ["cudnnGetConvolution2dForwardOutputDim", "const cudnnConvolutionDescriptor_t",
97             "const cudnnTensorDescriptor_t", "const cudnnFilterDescriptor_t", "int", "int", "int", "int"],
98         ["cudnnSetConvolutionNdDescriptor", "cudnnConvolutionDescriptor_t", "int", "const int[]", "const int[]",
99             "const int[]", "cudnnConvolutionMode_t", "cudnnDataType_t"],
100         ["cudnnGetConvolutionNdDescriptor", "const cudnnConvolutionDescriptor_t", "int", "int *", "int[]", "int[]",
101             "int[]", "cudnnConvolutionMode_t *", "cudnnDataType_t *"],
102         ["cudnnGetConvolutionNdForwardOutputDim", "const cudnnConvolutionDescriptor_t",
103             "const cudnnTensorDescriptor_t", "const cudnnFilterDescriptor_t", "int", "int[]"],
104         ["cudnnDestroyConvolutionDescriptor", "cudnnConvolutionDescriptor_t"],
105 
106         
107         ["cudnnFindConvolutionForwardAlgorithm", "cudnnHandle_t", "const cudnnTensorDescriptor_t",
108             "const cudnnFilterDescriptor_t", "const cudnnConvolutionDescriptor_t", "const cudnnTensorDescriptor_t",
109             "const int", "int *", "cudnnConvolutionFwdAlgoPerf_t *"],
110         ["cudnnFindConvolutionForwardAlgorithmEx", "cudnnHandle_t", "const cudnnTensorDescriptor_t", "const void *",
111             "const cudnnFilterDescriptor_t", "const void *", "const cudnnConvolutionDescriptor_t",
112             "const cudnnTensorDescriptor_t", "void *", "const int", "int *", "cudnnConvolutionFwdAlgoPerf_t *",
113             "void *", "size_t"],
114         ["cudnnGetConvolutionForwardAlgorithm", "cudnnHandle_t", "const cudnnTensorDescriptor_t",
115             "const cudnnFilterDescriptor_t", "const cudnnConvolutionDescriptor_t", "const cudnnTensorDescriptor_t",
116             "cudnnConvolutionFwdPreference_t", "size_t", "cudnnConvolutionFwdAlgo_t *"],
117         ["cudnnGetConvolutionForwardWorkspaceSize", "cudnnHandle_t", "const cudnnTensorDescriptor_t",
118             "const cudnnFilterDescriptor_t", "const cudnnConvolutionDescriptor_t", "const cudnnTensorDescriptor_t",
119             "cudnnConvolutionFwdAlgo_t", "size_t *"],
120         ["cudnnConvolutionForward", "cudnnHandle_t", "const void *", "const cudnnTensorDescriptor_t", "const void *",
121             "const cudnnFilterDescriptor_t", "const void *", "const cudnnConvolutionDescriptor_t",
122             "cudnnConvolutionFwdAlgo_t", "void *", "size_t", "const void *", "const cudnnTensorDescriptor_t",
123             "void *"],
124         ["cudnnConvolutionBackwardBias", "cudnnHandle_t", "const void *", "const cudnnTensorDescriptor_t",
125             "const void *", "const void *", "const cudnnTensorDescriptor_t", "void *"],
126         
127 
128         ["cudnnFindConvolutionBackwardFilterAlgorithm", "cudnnHandle_t", "const cudnnTensorDescriptor_t",
129             "const cudnnTensorDescriptor_t", "const cudnnConvolutionDescriptor_t", "const cudnnFilterDescriptor_t",
130             "const int", "int *", "cudnnConvolutionBwdFilterAlgoPerf_t *"],
131         ["cudnnFindConvolutionBackwardFilterAlgorithmEx", "cudnnHandle_t", "const cudnnTensorDescriptor_t",
132             "const void *", "const cudnnTensorDescriptor_t", "const void *", "const cudnnConvolutionDescriptor_t",
133             "const cudnnFilterDescriptor_t", "void *", "const int", "int *", "cudnnConvolutionBwdFilterAlgoPerf_t *",
134             "void *", "size_t"],
135         ["cudnnGetConvolutionBackwardFilterAlgorithm", "cudnnHandle_t", "const cudnnTensorDescriptor_t",
136             "const cudnnTensorDescriptor_t", "const cudnnConvolutionDescriptor_t", "const cudnnFilterDescriptor_t",
137             "cudnnConvolutionBwdFilterPreference_t", "size_t", "cudnnConvolutionBwdFilterAlgo_t *"],
138         ["cudnnGetConvolutionBackwardFilterWorkspaceSize", "cudnnHandle_t", "const cudnnTensorDescriptor_t",
139             "const cudnnTensorDescriptor_t", "const cudnnConvolutionDescriptor_t", "const cudnnFilterDescriptor_t",
140             "cudnnConvolutionBwdFilterAlgo_t", "size_t *"],
141         ["cudnnConvolutionBackwardFilter", "cudnnHandle_t", "const void *", "const cudnnTensorDescriptor_t",
142             "const void *", "const cudnnTensorDescriptor_t", "const void *", "const cudnnConvolutionDescriptor_t",
143             "cudnnConvolutionBwdFilterAlgo_t", "void *", "size_t", "const void *", "const cudnnFilterDescriptor_t",
144             "void *"],
145         
146 
147         ["cudnnFindConvolutionBackwardDataAlgorithm", "cudnnHandle_t", "const cudnnFilterDescriptor_t",
148             "const cudnnTensorDescriptor_t", "const cudnnConvolutionDescriptor_t", "const cudnnTensorDescriptor_t",
149             "const int", "int *", "cudnnConvolutionBwdDataAlgoPerf_t *"],
150         ["cudnnFindConvolutionBackwardDataAlgorithmEx", "cudnnHandle_t", "const cudnnFilterDescriptor_t",
151             "const void *", "const cudnnTensorDescriptor_t", "const void *", "const cudnnConvolutionDescriptor_t",
152             "const cudnnTensorDescriptor_t", "void *", "const int", "int *", "cudnnConvolutionBwdDataAlgoPerf_t *",
153             "void *", "size_t"],
154         ["cudnnGetConvolutionBackwardDataAlgorithm", "cudnnHandle_t", "const cudnnFilterDescriptor_t",
155             "const cudnnTensorDescriptor_t", "const cudnnConvolutionDescriptor_t", "const cudnnTensorDescriptor_t",
156             "cudnnConvolutionBwdDataPreference_t", "size_t", "cudnnConvolutionBwdDataAlgo_t *"],
157         ["cudnnGetConvolutionBackwardDataWorkspaceSize", "cudnnHandle_t", "const cudnnFilterDescriptor_t",
158             "const cudnnTensorDescriptor_t", "const cudnnConvolutionDescriptor_t", "const cudnnTensorDescriptor_t",
159             "cudnnConvolutionBwdDataAlgo_t", "size_t *"],
160         ["cudnnConvolutionBackwardData", "cudnnHandle_t", "const void *", "const cudnnFilterDescriptor_t",
161             "const void *", "const cudnnTensorDescriptor_t", "const void *", "const cudnnConvolutionDescriptor_t",
162             "cudnnConvolutionBwdDataAlgo_t", "void *", "size_t", "const void *", "const cudnnTensorDescriptor_t",
163             "void *"],
164         ["cudnnIm2Col", "cudnnHandle_t", "const cudnnTensorDescriptor_t", "const void *",
165             "const cudnnFilterDescriptor_t", "const cudnnConvolutionDescriptor_t", "void *"],
166         
167 
168         ["cudnnSoftmaxForward", "cudnnHandle_t", "cudnnSoftmaxAlgorithm_t", "cudnnSoftmaxMode_t", "const void *",
169             "const cudnnTensorDescriptor_t", "const void *", "const void *", "const cudnnTensorDescriptor_t",
170             "void *"],
171         ["cudnnSoftmaxBackward", "cudnnHandle_t", "cudnnSoftmaxAlgorithm_t", "cudnnSoftmaxMode_t", "const void *",
172             "const cudnnTensorDescriptor_t", "const void *", "const cudnnTensorDescriptor_t", "const void *",
173             "const void *", "const cudnnTensorDescriptor_t", "void *"],
174         
175 
176         ["cudnnCreatePoolingDescriptor", "cudnnPoolingDescriptor_t *"],
177         ["cudnnSetPooling2dDescriptor", "cudnnPoolingDescriptor_t", "cudnnPoolingMode_t", "cudnnNanPropagation_t",
178             "int", "int", "int", "int", "int", "int"],
179         ["cudnnGetPooling2dDescriptor", "const cudnnPoolingDescriptor_t", "cudnnPoolingMode_t *",
180             "cudnnNanPropagation_t *", "int *", "int *", "int *", "int *", "int *", "int *"],
181         ["cudnnSetPoolingNdDescriptor", "cudnnPoolingDescriptor_t", "const cudnnPoolingMode_t",
182             "const cudnnNanPropagation_t", "int", "const int[]", "const int[]", "const int[]"],
183         ["cudnnGetPoolingNdDescriptor", "cudnnPoolingDescriptor_t", "int", "cudnnPoolingMode_t *",
184             "cudnnNanPropagation_t *", "int *", "int[]", "int[]", "int[]"],
185         ["cudnnGetPoolingNdForwardOutputDim", "const cudnnPoolingDescriptor_t", "const cudnnTensorDescriptor_t",
186             "int", "int[]"],
187         ["cudnnGetPooling2dForwardOutputDim", "const cudnnPoolingDescriptor_t", "const cudnnTensorDescriptor_t",
188             "int *", "int *", "int *", "int *"],
189         ["cudnnDestroyPoolingDescriptor", "cudnnPoolingDescriptor_t"],
190         ["cudnnPoolingForward", "cudnnHandle_t", "const cudnnPoolingDescriptor_t", "const void *",
191             "const cudnnTensorDescriptor_t", "const void *", "const void *", "const cudnnTensorDescriptor_t",
192             "void *"],
193         ["cudnnPoolingBackward", "cudnnHandle_t", "const cudnnPoolingDescriptor_t", "const void *",
194             "const cudnnTensorDescriptor_t", "const void *", "const cudnnTensorDescriptor_t", "const void *",
195             "const cudnnTensorDescriptor_t", "const void *", "const void *", "const cudnnTensorDescriptor_t",
196             "void *"],
197         
198 
199         ["cudnnCreateActivationDescriptor", "cudnnActivationDescriptor_t *"],
200         ["cudnnSetActivationDescriptor", "cudnnActivationDescriptor_t", "cudnnActivationMode_t",
201             "cudnnNanPropagation_t", "double"],
202         ["cudnnGetActivationDescriptor", "const cudnnActivationDescriptor_t", "cudnnActivationMode_t *",
203             "cudnnNanPropagation_t ", "double *"],
204         ["cudnnDestroyActivationDescriptor", "cudnnActivationDescriptor_t"],
205         ["cudnnActivationForward", "cudnnHandle_t", "cudnnActivationDescriptor_t", "const void *",
206             "const cudnnTensorDescriptor_t", "const void *", "const void *", "const cudnnTensorDescriptor_t",
207             "void *"],
208         ["cudnnActivationBackward", "cudnnHandle_t", "cudnnActivationDescriptor_t", "const void *",
209             "const cudnnTensorDescriptor_t", "const void *", "const cudnnTensorDescriptor_t", "const void *",
210             "const cudnnTensorDescriptor_t", "const void *", "const void *", "const cudnnTensorDescriptor_t",
211             "void *"],
212         
213 
214         ["cudnnCreateLRNDescriptor", "cudnnLRNDescriptor_t *"],
215         ["cudnnSetLRNDescriptor", "cudnnLRNDescriptor_t", "uint", "double", "double", "double"],
216         ["cudnnGetLRNDescriptor", "cudnnLRNDescriptor_t", "uint *", "double *", "double *", "double *"],
217         ["cudnnDestroyLRNDescriptor", "cudnnLRNDescriptor_t"],
218         ["cudnnLRNCrossChannelForward", "cudnnHandle_t", "cudnnLRNDescriptor_t", "cudnnLRNMode_t", "const void *",
219             "const cudnnTensorDescriptor_t", "const void *", "const void *", "const cudnnTensorDescriptor_t",
220             "void *"],
221         ["cudnnLRNCrossChannelBackward", "cudnnHandle_t", "cudnnLRNDescriptor_t", "cudnnLRNMode_t", "const void *",
222             "const cudnnTensorDescriptor_t", "const void *", "const cudnnTensorDescriptor_t", "const void *",
223             "const cudnnTensorDescriptor_t", "const void *", "const void *", "const cudnnTensorDescriptor_t",
224             "void *"],
225         ["cudnnDivisiveNormalizationForward", "cudnnHandle_t", "cudnnLRNDescriptor_t", "cudnnDivNormMode_t",
226             "const void *", "const cudnnTensorDescriptor_t", "const void *", "const void *", "void *", "void *",
227             "const void *", "const cudnnTensorDescriptor_t", "void *"],
228         ["cudnnDivisiveNormalizationBackward", "cudnnHandle_t", "cudnnLRNDescriptor_t", "cudnnDivNormMode_t",
229             "const void *", "const cudnnTensorDescriptor_t", "const void *", "const void *", "const void *",
230             "void *", "void *", "const void *", "const cudnnTensorDescriptor_t", "void *", "void *"],
231         
232 
233         ["cudnnDeriveBNTensorDescriptor", "cudnnTensorDescriptor_t", "const cudnnTensorDescriptor_t",
234             "cudnnBatchNormMode_t"],
235         ["cudnnBatchNormalizationForwardTraining", "cudnnHandle_t", "cudnnBatchNormMode_t", "const void *",
236             "const void *", "const cudnnTensorDescriptor_t", "const void *", "const cudnnTensorDescriptor_t",
237             "void *", "const cudnnTensorDescriptor_t", "const void *", "const void *", "double", "void *", "void *",
238             "double", "void *", "void *"],
239         ["cudnnBatchNormalizationForwardInference", "cudnnHandle_t", "cudnnBatchNormMode_t", "const void *",
240             "const void *", "const cudnnTensorDescriptor_t", "const void *", "const cudnnTensorDescriptor_t", "void *",
241             "const cudnnTensorDescriptor_t", "const void *", "const void *", "const void *", "const void *", "double"],
242         ["cudnnBatchNormalizationBackward", "cudnnHandle_t", "cudnnBatchNormMode_t", "const void *", "const void *",
243             "const void *", "const void *", "const cudnnTensorDescriptor_t", "const void *",
244             "const cudnnTensorDescriptor_t", "const void *", "const cudnnTensorDescriptor_t", "void *",
245             "const cudnnTensorDescriptor_t", "const void *", "void *", "void *", "double", "const void *",
246             "const void *"],
247         
248 
249         ["cudnnCreateSpatialTransformerDescriptor", "cudnnSpatialTransformerDescriptor_t *"],
250         ["cudnnSetSpatialTransformerNdDescriptor", "cudnnSpatialTransformerDescriptor_t", "cudnnSamplerType_t",
251             "cudnnDataType_t", "const int", "const int[]"],
252         ["cudnnDestroySpatialTransformerDescriptor", "cudnnSpatialTransformerDescriptor_t"],
253         ["cudnnSpatialTfGridGeneratorForward", "cudnnHandle_t", "const cudnnSpatialTransformerDescriptor_t",
254             "const void *", "void *"],
255         ["cudnnSpatialTfGridGeneratorBackward", "cudnnHandle_t", "const cudnnSpatialTransformerDescriptor_t",
256             "const void *", "void *"],
257         ["cudnnSpatialTfSamplerForward", "cudnnHandle_t", "cudnnSpatialTransformerDescriptor_t", "const void *",
258             "const cudnnTensorDescriptor_t", "const void *", "const void *", "const void *", "const void *",
259             "cudnnTensorDescriptor_t", "void *"],
260         ["cudnnSpatialTfSamplerBackward", "cudnnHandle_t", "cudnnSpatialTransformerDescriptor_t", "const void *",
261             "const cudnnTensorDescriptor_t", "const void *", "const void *", "const cudnnTensorDescriptor_t", "void *",
262             "const void *", "const cudnnTensorDescriptor_t", "const void *", "const void *", "const void *", "void *"],
263         
264 
265         ["cudnnCreateDropoutDescriptor", "cudnnDropoutDescriptor_t *"],
266         ["cudnnDestroyDropoutDescriptor", "cudnnDropoutDescriptor_t"],
267         ["cudnnDropoutGetStatesSize", "cudnnHandle_t", "size_t *"],
268         ["cudnnDropoutGetReserveSpaceSize", "cudnnTensorDescriptor_t", "size_t *"],
269         ["cudnnSetDropoutDescriptor", "cudnnDropoutDescriptor_t", "cudnnHandle_t", "float", "void *", "size_t",
270             "ulong"],
271         ["cudnnDropoutForward", "cudnnHandle_t", "const cudnnDropoutDescriptor_t", "const cudnnTensorDescriptor_t",
272             "const void *", "const cudnnTensorDescriptor_t", "void *", "void *", "size_t"],
273         ["cudnnDropoutBackward", "cudnnHandle_t", "const cudnnDropoutDescriptor_t", "const cudnnTensorDescriptor_t",
274             "const void *", "const cudnnTensorDescriptor_t", "void *", "void *", "size_t"],
275         
276 
277         ["cudnnCreateRNNDescriptor", "cudnnRNNDescriptor_t *"],
278         ["cudnnDestroyRNNDescriptor", "cudnnRNNDescriptor_t"],
279         ["cudnnSetRNNDescriptor", "cudnnRNNDescriptor_t", "int", "int", "cudnnDropoutDescriptor_t",
280             "cudnnRNNInputMode_t", "cudnnDirectionMode_t", "cudnnRNNMode_t", "cudnnDataType_t"],
281         ["cudnnGetRNNWorkspaceSize", "cudnnHandle_t", "const cudnnRNNDescriptor_t", "const int",
282             "const cudnnTensorDescriptor_t *", "size_t *"],
283         ["cudnnGetRNNTrainingReserveSize", "cudnnHandle_t", "const cudnnRNNDescriptor_t", "const int",
284             "const cudnnTensorDescriptor_t *", "size_t *"],
285         ["cudnnGetRNNParamsSize", "cudnnHandle_t", "const cudnnRNNDescriptor_t", "const cudnnTensorDescriptor_t",
286             "size_t *", "cudnnDataType_t"],
287         ["cudnnGetRNNLinLayerMatrixParams", "cudnnHandle_t", "const cudnnRNNDescriptor_t", "const int",
288             "const cudnnTensorDescriptor_t", "const cudnnFilterDescriptor_t", "const void *", "const int",
289             "cudnnFilterDescriptor_t", "void **"],
290         ["cudnnGetRNNLinLayerBiasParams", "cudnnHandle_t", "const cudnnRNNDescriptor_t", "const int",
291             "const cudnnTensorDescriptor_t", "const cudnnFilterDescriptor_t", "const void *", "const int",
292             "cudnnFilterDescriptor_t", "void **"],
293         ["cudnnRNNForwardInference", "cudnnHandle_t", "const cudnnRNNDescriptor_t", "const int",
294             "const cudnnTensorDescriptor_t *", "const void *", "const cudnnTensorDescriptor_t", "const void *",
295             "const void *", "const cudnnTensorDescriptor_t", "const void *", "const cudnnFilterDescriptor_t",
296             "const void *", "const cudnnTensorDescriptor_t *", "void *", "const cudnnTensorDescriptor_t", "void *",
297             "const cudnnTensorDescriptor_t", "void *", "void *", "size_t"],
298         ["cudnnRNNForwardTraining", "cudnnHandle_t", "const cudnnRNNDescriptor_t", "const int",
299             "const cudnnTensorDescriptor_t *", "const void *", "const cudnnTensorDescriptor_t", "const void *",
300             "const cudnnTensorDescriptor_t", "const void *", "const cudnnFilterDescriptor_t", "const void *",
301             "const cudnnTensorDescriptor_t *", "void *", "const cudnnTensorDescriptor_t", "void *",
302             "const cudnnTensorDescriptor_t", "void *", "void *", "size_t", "void *", "size_t"],
303         ["cudnnRNNBackwardData", "cudnnHandle_t", "const cudnnRNNDescriptor_t", "const int",
304             "const cudnnTensorDescriptor_t *", "const void *", "const cudnnTensorDescriptor_t *", "const void *",
305             "const cudnnTensorDescriptor_t", "const void *", "const cudnnTensorDescriptor_t", "const void *",
306             "const cudnnFilterDescriptor_t", "const void *", "const cudnnTensorDescriptor_t", "const void *",
307             "const cudnnTensorDescriptor_t", "const void *", "const cudnnTensorDescriptor_t *", "void *",
308             "const cudnnTensorDescriptor_t", "void *", "const cudnnTensorDescriptor_t", "void *", "void *",
309             "size_t", "const void *", "size_t"],
310         ["cudnnRNNBackwardWeights", "cudnnHandle_t", "const cudnnRNNDescriptor_t", "const int",
311             "const cudnnTensorDescriptor_t *", "const void *", "const cudnnTensorDescriptor_t", "const void *",
312             "const cudnnTensorDescriptor_t *", "const void *", "const void *", "size_t",
313             "const cudnnFilterDescriptor_t", "void *", "const void *", "size_t"]
314     ];
315 
316     string generateFunctionAliases()
317     {
318         import std.algorithm : joiner;
319         import std.conv : to;
320 
321         string ret;
322 
323         foreach(ft; functionTypes)
324         {
325             ret ~= "alias da_" ~ ft[0] ~ " = cudnnStatus_t function(" ~ ft[1 .. $].joiner(",").to!string ~ ");";
326         }
327 
328         return ret;
329     }
330 
331     string generateFunctionPointers()
332     {
333         string ret;
334 
335         foreach(ft; functionTypes)
336         {
337             ret ~= "da_" ~ ft[0] ~ " " ~ ft[0] ~ ";";
338         }
339 
340         return ret;
341     }
342 
343     string generateFunctionBinds()
344     {
345         string ret;
346 
347         foreach(ft; functionTypes)
348         {
349             ret ~= "bindFunc(cast(void**)&" ~ ft[0] ~ ", \"" ~ ft[0] ~ "\");";
350         }
351 
352         return ret;
353     }
354 }
355 
356 struct cudnnContext;
357 alias cudnnHandle_t = cudnnContext*;
358 
359 alias cudnnStatus_t = int;
360 enum : cudnnStatus_t
361 {
362     CUDNN_STATUS_SUCCESS          = 0,
363     CUDNN_STATUS_NOT_INITIALIZED  = 1,
364     CUDNN_STATUS_ALLOC_FAILED     = 2,
365     CUDNN_STATUS_BAD_PARAM        = 3,
366     CUDNN_STATUS_INTERNAL_ERROR   = 4,
367     CUDNN_STATUS_INVALID_VALUE    = 5,
368     CUDNN_STATUS_ARCH_MISMATCH    = 6,
369     CUDNN_STATUS_MAPPING_ERROR    = 7,
370     CUDNN_STATUS_EXECUTION_FAILED = 8,
371     CUDNN_STATUS_NOT_SUPPORTED    = 9,
372     CUDNN_STATUS_LICENSE_ERROR    = 10
373 }
374 
375 struct cudnnTensorStruct;
376 alias cudnnTensorDescriptor_t = cudnnTensorStruct*;
377 
378 struct cudnnConvolutionStruct;
379 alias cudnnConvolutionDescriptor_t = cudnnConvolutionStruct*;
380 
381 struct cudnnPoolingStruct;
382 alias cudnnPoolingDescriptor_t = cudnnPoolingStruct*;
383 
384 struct cudnnFilterStruct;
385 alias cudnnFilterDescriptor_t = cudnnFilterStruct*;
386 
387 struct cudnnLRNStruct;
388 alias cudnnLRNDescriptor_t = cudnnLRNStruct*;
389 
390 struct cudnnActivationStruct;
391 alias cudnnActivationDescriptor_t = cudnnActivationStruct*;
392 
393 struct cudnnSpatialTransformerStruct;
394 alias cudnnSpatialTransformerDescriptor_t = cudnnSpatialTransformerStruct*;
395 
396 struct cudnnOpTensorStruct;
397 alias cudnnOpTensorDescriptor_t = cudnnOpTensorStruct*;
398 
399 alias cudnnDataType_t = int;
400 enum : cudnnDataType_t
401 {
402     CUDNN_DATA_FLOAT  = 0,
403     CUDNN_DATA_DOUBLE = 1,
404     CUDNN_DATA_HALF   = 2
405 }
406 
407 alias cudnnNanPropagation_t = int;
408 enum : cudnnNanPropagation_t
409 {
410     CUDNN_NOT_PROPAGATE_NAN  = 0,
411     CUDNN_PROPAGATE_NAN      = 1
412 }
413 
414 alias cudnnTensorFormat_t = int;
415 enum : cudnnTensorFormat_t
416 {
417     CUDNN_TENSOR_NCHW = 0,   /* row major (wStride = 1, hStride = w) */
418     CUDNN_TENSOR_NHWC = 1    /* feature maps interleaved ( cStride = 1 )*/
419 }
420 
421 alias cudnnOpTensorOp_t = int;
422 enum : cudnnOpTensorOp_t
423 {
424     CUDNN_OP_TENSOR_ADD = 0,
425     CUDNN_OP_TENSOR_MUL = 1,
426     CUDNN_OP_TENSOR_MIN = 2,
427     CUDNN_OP_TENSOR_MAX = 3
428 }
429 
430 alias cudnnConvolutionMode_t = int;
431 enum : cudnnConvolutionMode_t
432 {
433     CUDNN_CONVOLUTION       = 0,
434     CUDNN_CROSS_CORRELATION = 1
435 }
436 
437 alias cudnnConvolutionFwdPreference_t = int;
438 enum : cudnnConvolutionFwdPreference_t
439 {
440     CUDNN_CONVOLUTION_FWD_NO_WORKSPACE            = 0,
441     CUDNN_CONVOLUTION_FWD_PREFER_FASTEST          = 1,
442     CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT = 2
443 }
444 
445 alias cudnnConvolutionFwdAlgo_t = int;
446 enum : cudnnConvolutionFwdAlgo_t
447 {
448     CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_GEMM         = 0,
449     CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_PRECOMP_GEMM = 1,
450     CUDNN_CONVOLUTION_FWD_ALGO_GEMM                  = 2,
451     CUDNN_CONVOLUTION_FWD_ALGO_DIRECT                = 3,
452     CUDNN_CONVOLUTION_FWD_ALGO_FFT                   = 4,
453     CUDNN_CONVOLUTION_FWD_ALGO_FFT_TILING            = 5,
454     CUDNN_CONVOLUTION_FWD_ALGO_WINOGRAD              = 6,
455     CUDNN_CONVOLUTION_FWD_ALGO_WINOGRAD_NONFUSED     = 7
456 }
457 
458 struct cudnnConvolutionFwdAlgoPerf_t
459 {
460     cudnnConvolutionFwdAlgo_t   algo;
461     cudnnStatus_t               status;
462     float                       time;
463     size_t                      memory;
464 }
465 
466 alias cudnnConvolutionBwdFilterPreference_t = int;
467 enum : cudnnConvolutionBwdFilterPreference_t
468 {
469     CUDNN_CONVOLUTION_BWD_FILTER_NO_WORKSPACE            = 0,
470     CUDNN_CONVOLUTION_BWD_FILTER_PREFER_FASTEST          = 1,
471     CUDNN_CONVOLUTION_BWD_FILTER_SPECIFY_WORKSPACE_LIMIT = 2
472 }
473 
474 alias cudnnConvolutionBwdFilterAlgo_t = int;
475 enum : cudnnConvolutionBwdFilterAlgo_t
476 {
477     CUDNN_CONVOLUTION_BWD_FILTER_ALGO_0         = 0,  // non-deterministic
478     CUDNN_CONVOLUTION_BWD_FILTER_ALGO_1         = 1,
479     CUDNN_CONVOLUTION_BWD_FILTER_ALGO_FFT       = 2,
480     CUDNN_CONVOLUTION_BWD_FILTER_ALGO_3         = 3,  // non-deterministic, algo0 with workspace
481     // CUDNN_CONVOLUTION_BWD_FILTER_ALGO_WINOGRAD  = 4, // not implemented
482     CUDNN_CONVOLUTION_BWD_FILTER_ALGO_WINOGRAD_NONFUSED = 5
483 }
484 
485 struct cudnnConvolutionBwdFilterAlgoPerf_t
486 {
487     cudnnConvolutionBwdFilterAlgo_t algo;
488     cudnnStatus_t status;
489     float time;
490     size_t memory;
491 }
492 
493 alias cudnnConvolutionBwdDataPreference_t = int;
494 enum : cudnnConvolutionBwdDataPreference_t
495 {
496     CUDNN_CONVOLUTION_BWD_DATA_NO_WORKSPACE             = 0,
497     CUDNN_CONVOLUTION_BWD_DATA_PREFER_FASTEST           = 1,
498     CUDNN_CONVOLUTION_BWD_DATA_SPECIFY_WORKSPACE_LIMIT  = 2
499 }
500 
501 alias cudnnConvolutionBwdDataAlgo_t = int;
502 enum : cudnnConvolutionBwdDataAlgo_t
503 {
504     CUDNN_CONVOLUTION_BWD_DATA_ALGO_0                 = 0, // non-deterministic
505     CUDNN_CONVOLUTION_BWD_DATA_ALGO_1                 = 1,
506     CUDNN_CONVOLUTION_BWD_DATA_ALGO_FFT               = 2,
507     CUDNN_CONVOLUTION_BWD_DATA_ALGO_FFT_TILING        = 3,
508     CUDNN_CONVOLUTION_BWD_DATA_ALGO_WINOGRAD          = 4,
509     CUDNN_CONVOLUTION_BWD_DATA_ALGO_WINOGRAD_NONFUSED = 5
510 }
511 
512 struct cudnnConvolutionBwdDataAlgoPerf_t
513 {
514     cudnnConvolutionBwdDataAlgo_t   algo;
515     cudnnStatus_t                   status;
516     float                           time;
517     size_t                          memory;
518 }
519 
520 alias cudnnSoftmaxAlgorithm_t = int;
521 enum : cudnnSoftmaxAlgorithm_t
522 {
523     CUDNN_SOFTMAX_FAST     = 0,         /* straightforward implementation */
524     CUDNN_SOFTMAX_ACCURATE = 1,         /* subtract max from every point to avoid overflow */
525     CUDNN_SOFTMAX_LOG      = 2
526 }
527 
528 alias cudnnSoftmaxMode_t = int;
529 enum : cudnnSoftmaxMode_t
530 {
531     CUDNN_SOFTMAX_MODE_INSTANCE = 0,   /* compute the softmax over all C, H, W for each N */
532     CUDNN_SOFTMAX_MODE_CHANNEL = 1
533 }
534 
535 alias cudnnPoolingMode_t = int;
536 enum : cudnnPoolingMode_t
537 {
538     CUDNN_POOLING_MAX     = 0,
539     CUDNN_POOLING_AVERAGE_COUNT_INCLUDE_PADDING = 1, // count for average includes padded values
540     CUDNN_POOLING_AVERAGE_COUNT_EXCLUDE_PADDING = 2
541 }
542 
543 alias cudnnActivationMode_t = int;
544 enum : cudnnActivationMode_t
545 {
546     CUDNN_ACTIVATION_SIGMOID      = 0,
547     CUDNN_ACTIVATION_RELU         = 1,
548     CUDNN_ACTIVATION_TANH         = 2,
549     CUDNN_ACTIVATION_CLIPPED_RELU = 3
550 }
551 
552 alias cudnnLRNMode_t = int;
553 enum : cudnnLRNMode_t
554 {
555     CUDNN_LRN_CROSS_CHANNEL_DIM1 = 0
556 }
557 
558 alias cudnnDivNormMode_t = int;
559 enum : cudnnDivNormMode_t
560 {
561     CUDNN_DIVNORM_PRECOMPUTED_MEANS = 0
562 }
563 
564 alias cudnnBatchNormMode_t = int;
565 enum : cudnnBatchNormMode_t
566 {
567     // bnScale, bnBias tensor dims are 1xCxHxWx.. (one value per CHW...-slice, normalized over N slice)
568     CUDNN_BATCHNORM_PER_ACTIVATION = 0,
569 
570     //bnScale, bnBias tensor dims are 1xCx1x1 (one value per C-dim normalized over Nx1xHxW subtensors)
571     CUDNN_BATCHNORM_SPATIAL        = 1
572 }
573 
574 alias cudnnSamplerType_t = int;
575 enum : cudnnSamplerType_t
576 {
577     CUDNN_SAMPLER_BILINEAR=0
578 }
579 
580 struct cudnnDropoutStruct;
581 alias cudnnDropoutDescriptor_t = cudnnDropoutStruct*;
582 
583 alias cudnnRNNMode_t = int;
584 enum : cudnnRNNMode_t
585 {
586     CUDNN_RNN_RELU = 0, // Stock RNN with ReLu activation
587     CUDNN_RNN_TANH = 1, // Stock RNN with tanh activation
588     CUDNN_LSTM = 2,     // LSTM with no peephole connections
589     CUDNN_GRU = 3       // Using h' = tanh(r * Uh(t-1) + Wx) and h = (1 - z) * h' + z * h(t-1);
590 }
591 
592 alias cudnnDirectionMode_t = int;
593 enum : cudnnDirectionMode_t
594 {
595     CUDNN_UNIDIRECTIONAL = 0,
596     CUDNN_BIDIRECTIONAL = 1      // Using output concatination at each step. Do we also want to support output sum?
597 }
598 
599 alias cudnnRNNInputMode_t = int;
600 enum : cudnnRNNInputMode_t
601 {
602     CUDNN_LINEAR_INPUT = 0,
603     CUDNN_SKIP_INPUT = 1
604 }
605 
606 struct cudnnRNNStruct;
607 alias cudnnRNNDescriptor_t = cudnnRNNStruct*;
608 
609 extern(System) @nogc nothrow
610 {
611     alias da_cudnnGetErrorString = const char *function(cudnnStatus_t);
612 
613     mixin(generateFunctionAliases());
614 }
615 
616 __gshared
617 {
618     da_cudnnGetErrorString cudnnGetErrorString;
619 
620     mixin(generateFunctionPointers());
621 }
622 
623 class DerelictCuDNN5Loader : SharedLibLoader
624 {
625     public
626     {
627         this()
628         {
629             super(libNames);
630         }
631     }
632 
633     protected
634     {
635         override void loadSymbols()
636         {
637             bindFunc(cast(void**)&cudnnGetErrorString, "cudnnGetErrorString");
638 
639             mixin(generateFunctionBinds());
640         }
641     }
642 }
643 
644 __gshared DerelictCuDNN5Loader DerelictCuDNN5;
645 
646 shared static this()
647 {
648     DerelictCuDNN5 = new DerelictCuDNN5Loader();
649 }
650 
651 unittest
652 {
653     import std.conv : to;
654     import std.stdio : writeln;
655 
656     try
657     {
658         DerelictCuDNN5.load();
659 
660         writeln("Successfully loaded cuDNN v5");
661     }
662     catch(Exception e)
663     {
664         writeln("Could not load cuDNN v5");
665     }
666 }