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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
use super::core::{ af_array, AfError, Array, CublasMathMode, FloatingPoint, HasAfEnum, MatProp, HANDLE_ERROR, }; use libc::{c_int, c_uint, c_void}; use std::vec::Vec; extern "C" { fn af_gemm( out: *mut af_array, optlhs: c_uint, optrhs: c_uint, alpha: *const c_void, lhs: af_array, rhs: af_array, beta: *const c_void, ) -> c_int; fn af_matmul( out: *mut af_array, lhs: af_array, rhs: af_array, optlhs: c_uint, optrhs: c_uint, ) -> c_int; fn af_dot( out: *mut af_array, lhs: af_array, rhs: af_array, optlhs: c_uint, optrhs: c_uint, ) -> c_int; fn af_transpose(out: *mut af_array, arr: af_array, conjugate: bool) -> c_int; fn af_transpose_inplace(arr: af_array, conjugate: bool) -> c_int; fn afcu_cublasSetMathMode(mode: c_int) -> c_int; } /// BLAS general matrix multiply (GEMM) of two Array objects /// /// /// This provides a general interface to the BLAS level 3 general matrix multiply (GEMM), /// which is generally defined as: /// /// \begin{equation} /// C = \alpha * opA(A)opB(B) + \beta * C /// \end{equation} /// /// where $\alpha$ (**alpha**) and $\beta$ (**beta**) are both scalars; $A$ and $B$ are the matrix /// multiply operands; and $opA$ and $opB$ are noop /// (if optLhs is [MatProp::NONE](./enum.MatProp.html)) or transpose /// (if optLhs is [MatProp::TRANS](./enum.MatProp.html)) operations on $A$ or $B$ before the /// actual GEMM operation. Batched GEMM is supported if at least either $A$ or $B$ have more than /// two dimensions (see [af::matmul](http://arrayfire.org/docs/group__blas__func__matmul.htm#ga63306b6ed967bd1055086db862fe885b) /// for more details on broadcasting). However, only one **alpha** and one **beta** can be used /// for all of the batched matrix operands. /// /// The `output` Array can be used both as an input and output. An allocation will be performed /// if you pass an empty Array (i.e. `let c: Array<f32> = (0 as i64).into();`). If a valid Array /// is passed as $C$, the operation will be performed on that Array itself. The C Array must be /// the correct type and shape; otherwise, an error will be thrown. /// /// Note: Passing an Array that has not been initialized to the C array /// will cause undefined behavior. /// /// # Examples /// /// Given below is an example of using gemm API with existing Arrays /// /// ```rust /// use arrayfire::{Array, Dim4, print, randu, gemm}; /// /// let dims = Dim4::new(&[5, 5, 1, 1]); /// /// let alpha = vec![1.0 as f32]; /// let beta = vec![2.0 as f32]; /// /// let lhs = randu::<f32>(dims); /// let rhs = randu::<f32>(dims); /// /// let mut result = Array::new_empty(dims); /// gemm(&mut result, arrayfire::MatProp::NONE, arrayfire::MatProp::NONE, /// alpha, &lhs, &rhs, beta); /// ``` /// /// If you don't have an existing Array, you can also use gemm in the following fashion. /// However, if there is no existing Array that you need to fill and your use case doesn't /// deal with alpha and beta from gemm equation, it is recommended to use /// [matmul](./fn.matmul.html) for more terse code. /// /// ```rust /// use arrayfire::{Array, Dim4, af_array, print, randu, gemm}; /// /// let dims = Dim4::new(&[5, 5, 1, 1]); /// /// let alpha = vec![1.0 as f32]; /// let beta = vec![2.0 as f32]; /// /// let lhs = randu::<f32>(dims); /// let rhs = randu::<f32>(dims); /// /// let mut result: Array::<f32> = (std::ptr::null_mut() as af_array).into(); /// /// gemm(&mut result, arrayfire::MatProp::NONE, arrayfire::MatProp::NONE, /// alpha, &lhs, &rhs, beta); /// ``` /// /// # Parameters /// /// - `optlhs` - Transpose left hand side before the function is performed, uses one of the values of [MatProp](./enum.MatProp.html) /// - `optrhs` - Transpose right hand side before the function is performed, uses one of the values of [MatProp](./enum.MatProp.html) /// - `alpha` is alpha value; /// - `lhs` is the Array on left hand side /// - `rhs` is the Array on right hand side /// - `beta` is beta value; /// /// # Return Values /// /// Array, result of gemm operation pub fn gemm<T>( output: &mut Array<T>, optlhs: MatProp, optrhs: MatProp, alpha: Vec<T>, lhs: &Array<T>, rhs: &Array<T>, beta: Vec<T>, ) where T: HasAfEnum + FloatingPoint, { unsafe { let mut out = output.get(); let err_val = af_gemm( &mut out as *mut af_array, optlhs as c_uint, optrhs as c_uint, alpha.as_ptr() as *const c_void, lhs.get(), rhs.get(), beta.as_ptr() as *const c_void, ); HANDLE_ERROR(AfError::from(err_val)); output.set(out); } } /// Matrix multiple of two Arrays /// /// # Parameters /// /// - `lhs` is the Array on left hand side /// - `rhs` is the Array on right hand side /// - `optlhs` - Transpose left hand side before the function is performed, uses one of the values of [MatProp](./enum.MatProp.html) /// - `optrhs` - Transpose right hand side before the function is performed, uses one of the values of [MatProp](./enum.MatProp.html) /// /// # Return Values /// /// The result Array of matrix multiplication pub fn matmul<T>(lhs: &Array<T>, rhs: &Array<T>, optlhs: MatProp, optrhs: MatProp) -> Array<T> where T: HasAfEnum + FloatingPoint, { unsafe { let mut temp: af_array = std::ptr::null_mut(); let err_val = af_matmul( &mut temp as *mut af_array, lhs.get(), rhs.get(), optlhs as c_uint, optrhs as c_uint, ); HANDLE_ERROR(AfError::from(err_val)); temp.into() } } /// Calculate the dot product of vectors. /// /// Scalar dot product between two vectors. Also referred to as the inner product. This function returns the scalar product of two equal sized vectors. /// /// # Parameters /// /// - `lhs` - Left hand side of dot operation /// - `rhs` - Right hand side of dot operation /// - `optlhs` - Options for lhs. Currently only NONE value from [MatProp](./enum.MatProp.html) is supported. /// - `optrhs` - Options for rhs. Currently only NONE value from [MatProp](./enum.MatProp.html) is supported. /// /// # Return Values /// /// The result of dot product. pub fn dot<T>(lhs: &Array<T>, rhs: &Array<T>, optlhs: MatProp, optrhs: MatProp) -> Array<T> where T: HasAfEnum + FloatingPoint, { unsafe { let mut temp: af_array = std::ptr::null_mut(); let err_val = af_dot( &mut temp as *mut af_array, lhs.get(), rhs.get(), optlhs as c_uint, optrhs as c_uint, ); HANDLE_ERROR(AfError::from(err_val)); temp.into() } } /// Transpose of a matrix. /// /// # Parameters /// /// - `arr` is the input Array /// - `conjugate` is a boolean that indicates if the transpose operation needs to be a conjugate /// transpose /// /// # Return Values /// /// Transposed Array. pub fn transpose<T: HasAfEnum>(arr: &Array<T>, conjugate: bool) -> Array<T> { unsafe { let mut temp: af_array = std::ptr::null_mut(); let err_val = af_transpose(&mut temp as *mut af_array, arr.get(), conjugate); HANDLE_ERROR(AfError::from(err_val)); temp.into() } } /// Inplace transpose of a matrix. /// /// # Parameters /// /// - `arr` is the input Array that has to be transposed /// - `conjugate` is a boolean that indicates if the transpose operation needs to be a conjugate /// transpose pub fn transpose_inplace<T: HasAfEnum>(arr: &mut Array<T>, conjugate: bool) { unsafe { let err_val = af_transpose_inplace(arr.get(), conjugate); HANDLE_ERROR(AfError::from(err_val)); } } /// Sets the cuBLAS math mode for the internal handle. /// /// See the cuBLAS documentation for additional details /// /// # Parameters /// /// - `mode` takes a value of [CublasMathMode](./enum.CublasMathMode.html) enum pub fn set_cublas_mode(mode: CublasMathMode) { unsafe { afcu_cublasSetMathMode(mode as c_int); //let err_val = afcu_cublasSetMathMode(mode as c_int); // FIXME(wonder if this something to throw off, // the program state is not invalid or anything // HANDLE_ERROR(AfError::from(err_val)); } }