1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//! Provides the normalization functionality from the CUDA cuDNN API.
//!
//! This includes divisive normalization and Local Response Normalization.

use ::{API, Error};
use ffi::*;

impl API {
    /// Creates a generic CUDA cuDNN LRN Descriptor.
    pub fn create_lrn_descriptor() -> Result<cudnnLRNDescriptor_t, Error> {
        unsafe { API::ffi_create_lrn_descriptor() }
    }

    /// Destroys a CUDA cuDNN LRN Descriptor.
    ///
    /// Should be called when freeing a CUDA::Descriptor to not trash up the CUDA device.
    pub fn destroy_lrn_descriptor(desc: cudnnLRNDescriptor_t) -> Result<(), Error> {
        unsafe { API::ffi_destroy_lrn_descriptor(desc) }
    }

    /// Initializes a generic CUDA cuDNN LRN Descriptor with specific properties.
    pub fn set_lrn_descriptor(
        desc: cudnnLRNDescriptor_t,
        lrn_n: ::libc::c_uint,
        lrn_alpha: ::libc::c_double,
        lrn_beta: ::libc::c_double,
        lrn_k: ::libc::c_double
    ) -> Result<(), Error> {
        unsafe { API::ffi_set_lrn_descriptor(desc, lrn_n, lrn_alpha, lrn_beta, lrn_k) }
    }

    /// Computes an LRN cross channel forward function.
    pub fn lrn_cross_channel_forward(
        handle: cudnnHandle_t,
        norm_desc: cudnnLRNDescriptor_t,
        mode: cudnnLRNMode_t,
        alpha: *const ::libc::c_void,
        src_desc: cudnnTensorDescriptor_t,
        src_data: *const ::libc::c_void,
        beta: *const ::libc::c_void,
        dest_desc: cudnnTensorDescriptor_t,
        dest_data: *mut ::libc::c_void
    ) -> Result<(), Error> {
        unsafe { API::ffi_lrn_cross_channel_forward(handle, norm_desc, mode, alpha, src_desc, src_data, beta, dest_desc, dest_data) }
    }

    /// Computes an LRN cross channel backward function.
    pub fn lrn_cross_channel_backward(
        handle: cudnnHandle_t,
        norm_desc: cudnnLRNDescriptor_t,
        mode: cudnnDivNormMode_t,
        alpha: *const ::libc::c_void,
        src_desc: cudnnTensorDescriptor_t,
        src_data: *const ::libc::c_void,
        src_diff_desc: cudnnTensorDescriptor_t,
        src_diff_data: *const ::libc::c_void,
        beta: *const ::libc::c_void,
        dest_desc: cudnnTensorDescriptor_t,
        dest_data: *const ::libc::c_void,
        dest_diff_desc: cudnnTensorDescriptor_t,
        dest_diff_data: *mut ::libc::c_void
    ) -> Result<(), Error> {
        unsafe { API::ffi_lrn_cross_channel_backward(handle, norm_desc, mode, alpha, src_desc, src_data, src_diff_desc, src_diff_data, beta, dest_desc, dest_data, dest_diff_desc, dest_diff_data) }
    }

    /// Computes an devisive normalization forward function.
    pub fn divisive_normalization_forward(
        handle: cudnnHandle_t,
        norm_desc: cudnnLRNDescriptor_t,
        mode: cudnnDivNormMode_t,
        alpha: *const ::libc::c_void,
        src_desc: cudnnTensorDescriptor_t,
        src_data: *const ::libc::c_void,
        src_means_data: *const ::libc::c_void,
        temp_data: *mut ::libc::c_void,
        temp_data2: *mut ::libc::c_void,
        beta: *const ::libc::c_void,
        dest_desc: cudnnTensorDescriptor_t,
        dest_data: *mut ::libc::c_void
    ) -> Result<(), Error> {
        unsafe { API::ffi_divisive_normalization_forward(handle, norm_desc, mode, alpha, src_desc, src_data, src_means_data, temp_data, temp_data2, beta, dest_desc, dest_data) }
    }

    /// Computes an devisive normalization backward function.
    pub fn divisive_normalization_backward(
        handle: cudnnHandle_t,
        norm_desc: cudnnLRNDescriptor_t,
        mode: cudnnDivNormMode_t,
        alpha: *const ::libc::c_void,
        src_desc: cudnnTensorDescriptor_t,
        src_data: *const ::libc::c_void,
        src_means_data: *const ::libc::c_void,
        src_diff_data: *const ::libc::c_void,
        temp_data: *mut ::libc::c_void,
        temp_data2: *mut ::libc::c_void,
        beta: *const ::libc::c_void,
        dest_data_desc: cudnnTensorDescriptor_t,
        dest_data_diff: *mut ::libc::c_void,
        dest_means_diff: *mut ::libc::c_void
    ) -> Result<(), Error> {
        unsafe { API::ffi_divisive_normalization_backward(handle, norm_desc, mode, alpha, src_desc, src_data, src_means_data, src_diff_data, temp_data, temp_data2, beta, dest_data_desc, dest_data_diff, dest_means_diff) }
    }

    unsafe fn ffi_create_lrn_descriptor() -> Result<cudnnLRNDescriptor_t, Error> {
        let mut desc: cudnnLRNDescriptor_t = ::std::ptr::null_mut();
        match cudnnCreateLRNDescriptor(&mut desc) {
            cudnnStatus_t::CUDNN_STATUS_SUCCESS => Ok(desc),
            cudnnStatus_t::CUDNN_STATUS_ALLOC_FAILED => Err(Error::AllocFailed("The resources could not be allocated.")),
            _ => Err(Error::Unknown("Unable to create generic CUDA cuDNN LRN Descriptor.")),
        }
    }

    unsafe fn ffi_destroy_lrn_descriptor(desc: cudnnLRNDescriptor_t) -> Result<(), Error> {
        match cudnnDestroyLRNDescriptor(desc) {
            cudnnStatus_t::CUDNN_STATUS_SUCCESS => Ok(()),
            _ => Err(Error::Unknown("Unable to destroy CUDA cuDNN LRN Descriptor.")),
        }
    }

    unsafe fn ffi_set_lrn_descriptor(
        desc: cudnnLRNDescriptor_t,
        lrn_n: ::libc::c_uint,
        lrn_alpha: ::libc::c_double,
        lrn_beta: ::libc::c_double,
        lrn_k: ::libc::c_double
    ) -> Result<(), Error> {
        match cudnnSetLRNDescriptor(desc, lrn_n, lrn_alpha, lrn_beta, lrn_k) {
            cudnnStatus_t::CUDNN_STATUS_SUCCESS => Ok(()),
            cudnnStatus_t::CUDNN_STATUS_BAD_PARAM => Err(Error::BadParam("One of the input parameters was out of range.")),
            _ => Err(Error::Unknown("Unable to set CUDA cuDNN Pooling Descriptor.")),
        }
    }

    unsafe fn ffi_lrn_cross_channel_forward(
        handle: cudnnHandle_t,
        norm_desc: cudnnLRNDescriptor_t,
        mode: cudnnLRNMode_t,
        alpha: *const ::libc::c_void,
        src_desc: cudnnTensorDescriptor_t,
        src_data: *const ::libc::c_void,
        beta: *const ::libc::c_void,
        dest_desc: cudnnTensorDescriptor_t,
        dest_data: *mut ::libc::c_void
    ) -> Result<(), Error> {
        match cudnnLRNCrossChannelForward(handle, norm_desc, mode, alpha, src_desc, src_data, beta, dest_desc, dest_data) {
            cudnnStatus_t::CUDNN_STATUS_SUCCESS => Ok(()),
            cudnnStatus_t::CUDNN_STATUS_BAD_PARAM => Err(Error::BadParam("At least one of the following conditions are met: One of the tensor pointers  `src_data`, `dest_data` is NULL. Number of input tensor dimensions is 2 or less. LRN Descriptor params are out of valid range. Input Tensor is 5D but is not NCDHW DHW-packed format.")),
            cudnnStatus_t::CUDNN_STATUS_NOT_SUPPORTED => Err(Error::NotSupported("`data_type`, `sride`, `dimensions` mismatch or tensor strides are negative.")),
            cudnnStatus_t::CUDNN_STATUS_EXECUTION_FAILED => Err(Error::ExecutionFailed("Execution failed to launch on GPU.")),
            _ => Err(Error::Unknown("Unable to compute LRN cross channel forward.")),
        }
    }

    unsafe fn ffi_lrn_cross_channel_backward(
        handle: cudnnHandle_t,
        norm_desc: cudnnLRNDescriptor_t,
        mode: cudnnDivNormMode_t,
        alpha: *const ::libc::c_void,
        src_desc: cudnnTensorDescriptor_t,
        src_data: *const ::libc::c_void,
        src_diff_desc: cudnnTensorDescriptor_t,
        src_diff_data: *const ::libc::c_void,
        beta: *const ::libc::c_void,
        dest_desc: cudnnTensorDescriptor_t,
        dest_data: *const ::libc::c_void,
        dest_diff_desc: cudnnTensorDescriptor_t,
        dest_diff_data: *mut ::libc::c_void
    ) -> Result<(), Error> {
        match cudnnLRNCrossChannelBackward(handle, norm_desc, mode, alpha, src_desc, src_data, src_diff_desc, src_diff_data, dest_desc, dest_data, beta, dest_diff_desc, dest_diff_data) {
            cudnnStatus_t::CUDNN_STATUS_SUCCESS => Ok(()),
            cudnnStatus_t::CUDNN_STATUS_BAD_PARAM => Err(Error::BadParam("At least one of the following conditions are met: One of the tensor pointers  `src_data`, `dest_data` is NULL. Number of input tensor dimensions is 2 or less. LRN Descriptor params are out of valid range. Input Tensor is 5D but is not NCDHW DHW-packed format.")),
            cudnnStatus_t::CUDNN_STATUS_NOT_SUPPORTED => Err(Error::NotSupported("`data_type`, `sride`, `dimensions` mismatch or tensor strides are negative.")),
            cudnnStatus_t::CUDNN_STATUS_EXECUTION_FAILED => Err(Error::ExecutionFailed("Execution failed to launch on GPU.")),
            _ => Err(Error::Unknown("Unable to compute LRN cross channel backward.")),
        }
    }

    unsafe fn ffi_divisive_normalization_forward(
        handle: cudnnHandle_t,
        norm_desc: cudnnLRNDescriptor_t,
        mode: cudnnDivNormMode_t,
        alpha: *const ::libc::c_void,
        src_desc: cudnnTensorDescriptor_t,
        src_data: *const ::libc::c_void,
        src_means_data: *const ::libc::c_void,
        temp_data: *mut ::libc::c_void,
        temp_data2: *mut ::libc::c_void,
        beta: *const ::libc::c_void,
        dest_desc: cudnnTensorDescriptor_t,
        dest_data: *mut ::libc::c_void
    ) -> Result<(), Error> {
        match cudnnDivisiveNormalizationForward(handle, norm_desc, mode, alpha, src_desc, src_data, src_means_data, temp_data, temp_data2, beta, dest_desc, dest_data) {
            cudnnStatus_t::CUDNN_STATUS_SUCCESS => Ok(()),
            cudnnStatus_t::CUDNN_STATUS_BAD_PARAM => Err(Error::BadParam("At least one of the following conditions are met: One of the tensor pointers  `src_data`, `dest_data`, `tmp_data`, `tmp_data2` is NULL. Number of input tensor or output tensor dimensions is outside of [4,5] range. A mismatch in dimensions between any two of the input or output tensors. For in-place computation (`src_data` == `dest_data`), a mismatch in strides between the input data and output data tensors. Alpha or beta pointer is NULL. LRN descriptor parameters are outside or their valid ranges. Any of the tensor strides are negative.")),
            cudnnStatus_t::CUDNN_STATUS_NOT_SUPPORTED => Err(Error::NotSupported("stried of the input and output tensors mismatch.")),
            cudnnStatus_t::CUDNN_STATUS_EXECUTION_FAILED => Err(Error::ExecutionFailed("Execution failed to launch on GPU.")),
            _ => Err(Error::Unknown("Unable to compute divisive normalization forward.")),
        }
    }

    unsafe fn ffi_divisive_normalization_backward(
        handle: cudnnHandle_t,
        norm_desc: cudnnLRNDescriptor_t,
        mode: cudnnDivNormMode_t,
        alpha: *const ::libc::c_void,
        src_desc: cudnnTensorDescriptor_t,
        src_data: *const ::libc::c_void,
        src_means_data: *const ::libc::c_void,
        src_diff_data: *const ::libc::c_void,
        temp_data: *mut ::libc::c_void,
        temp_data2: *mut ::libc::c_void,
        beta: *const ::libc::c_void,
        dest_data_desc: cudnnTensorDescriptor_t,
        dest_data_diff: *mut ::libc::c_void,
        dest_means_diff: *mut ::libc::c_void
    ) -> Result<(), Error> {
        match cudnnDivisiveNormalizationBackward(handle, norm_desc, mode, alpha, src_desc, src_data, src_means_data, src_diff_data, temp_data, temp_data2, beta, dest_data_desc, dest_data_diff, dest_means_diff) {
            cudnnStatus_t::CUDNN_STATUS_SUCCESS => Ok(()),
            cudnnStatus_t::CUDNN_STATUS_BAD_PARAM => Err(Error::BadParam("At least one of the following conditions are met: One of the tensor pointers  `src_data`, `dest_data`, `tmp_data`, `tmp_data2` is NULL. Number of input tensor or output tensor dimensions is outside of [4,5] range. A mismatch in dimensions between any two of the input or output tensors. For in-place computation (`src_data` == `dest_data`), a mismatch in strides between the input data and output data tensors. Alpha or beta pointer is NULL. LRN descriptor parameters are outside or their valid ranges. Any of the tensor strides are negative.")),
            cudnnStatus_t::CUDNN_STATUS_NOT_SUPPORTED => Err(Error::NotSupported("`mode` is invalid or dimensions of input and output tensor differ or `data_type` or strides of the tensors differ.")),
            cudnnStatus_t::CUDNN_STATUS_EXECUTION_FAILED => Err(Error::ExecutionFailed("Execution failed to launch on GPU.")),
            _ => Err(Error::Unknown("Unable to compute divisive normalization backward.")),
        }
    }
}