├── .gitignore ├── LICENSE ├── README.md ├── examples └── helloworld.go └── src └── arrayfire ├── array.go ├── defines.go ├── device.go ├── errors.go ├── graphics.go └── util.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | 26 | arrayfire-go 27 | main 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, ArrayFire 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of arrayfire-go nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ArrayFire Go Bindings 2 | 3 | This project is still in the prototype phase. Please follow the discussion on [this issue](https://github.com/arrayfire/arrayfire-go/issues/1). You can also chat with us at the following locations: 4 | 5 | [![Join the chat at https://gitter.im/arrayfire/arrayfire-go](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/arrayfire/arrayfire-go?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 6 | 7 | ## Building arrayfire-go 8 | 9 | #### Install ArrayFire libraries 10 | Ensure you have the ArrayFire library installed on your system. 11 | 12 | You can get the ArrayFire library from one of the following ways: 13 | - [Binary Installers](http://arrayfire.com/download) 14 | - [Build from source](http://github.com/arrayfire/arrayfire) 15 | 16 | #### Set Environment variables 17 | 18 | Point `AF_PATH` to the **installed** location of ArrayFire. 19 | 20 | $ export AF_PATH=/path/to/arrayfire 21 | 22 | 23 | If using the CUDA backend, you will need to include path to nvvm 24 | 25 | $ export NVVM_LIB_PATH=/path/to/cuda/nvvm/lib64 # use nvvm/lib for OSX or 32 bit systems 26 | 27 | 28 | Point `GOPATH` to the location of arrayfire-go 29 | 30 | Export `LD_LIBRARY_PATH` on Linux and `DYLD_LIBRARY_PATH` on OSX to point to 31 | - `AF_PATH/lib` 32 | - `NVVM_LIB_PATH` if using CUDA backend. 33 | 34 | #### Building with arrayfire libraries 35 | 36 | For ArrayFire 3.2 and above: 37 | 38 | $ export CGO_CFLAGS="-I$AF_PATH/include" 39 | $ export CGO_LDFLAGS="-L$AF_PATH/lib -laf -lforge" 40 | $ go build 41 | 42 | For older versions replace `-laf` with either `-lafcuda`, `-lafopencl` or `-lafcpu` 43 | 44 | ## Contribute 45 | 46 | If you want to contribute to this project, start here: 47 | + https://github.com/golang/go/wiki/cgo 48 | + https://golang.org/cmd/cgo/ 49 | + http://blog.golang.org/c-go-cgo 50 | -------------------------------------------------------------------------------- /examples/helloworld.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | af "arrayfire" 5 | "fmt" 6 | "unsafe" 7 | ) 8 | 9 | func main() { 10 | 11 | af.Info() 12 | 13 | var err error 14 | 15 | var ndims uint = 1 16 | var data [][]uint32 = [][]uint32{ 17 | {1, 2, 3, 4, 5}, 18 | } 19 | 20 | var a af.Array 21 | var dims []af.Dim = []af.Dim{5} 22 | a, err = af.CreateArray((unsafe.Pointer)(&data[0][0]), ndims, dims, af.U32) 23 | 24 | if err != nil { 25 | panic(fmt.Sprintf("failed at s:\n", err)) 26 | } 27 | 28 | err = af.Print(a) 29 | if err != nil { 30 | panic(fmt.Sprintf("failed at %s:\n", err)) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/arrayfire/array.go: -------------------------------------------------------------------------------- 1 | package arrayfire 2 | 3 | /* 4 | #include 5 | #include 6 | */ 7 | import "C" 8 | import ( 9 | "runtime" 10 | "unsafe" 11 | ) 12 | 13 | type ( 14 | DType C.af_dtype 15 | Dim C.dim_t 16 | ) 17 | 18 | type array struct { 19 | arr C.af_array 20 | } 21 | 22 | type Array *array 23 | 24 | func release(a Array) (e error) { 25 | e = nil 26 | if a.arr != nil { 27 | e = af_call(C.af_release_array((C.af_array)(a.arr))) 28 | a.arr = nil 29 | } 30 | return 31 | } 32 | 33 | func register(in array) (out Array) { 34 | 35 | //TODO: Call runtime.GC() depending on how much memory is left on device 36 | 37 | out = &in 38 | runtime.SetFinalizer(out, release) 39 | return 40 | } 41 | 42 | func CreateArray(data unsafe.Pointer, ndims uint, dims []Dim, ty DType) (out Array, err error) { 43 | 44 | out = nil 45 | err = nil 46 | 47 | var a array 48 | err = af_call(C.af_create_array(&a.arr, data, (C.uint)(ndims), 49 | (*C.dim_t)(&dims[0]), (C.af_dtype)(ty))) 50 | 51 | out = register(a) 52 | return 53 | } 54 | 55 | func CopyArray(in Array) (out Array, err error) { 56 | 57 | out = nil 58 | err = nil 59 | 60 | var a array 61 | err = af_call(C.af_copy_array(&a.arr, in.arr)) 62 | 63 | out = register(a) 64 | return 65 | } 66 | 67 | func RetainArray(in Array) (out Array, err error) { 68 | 69 | out = nil 70 | err = nil 71 | 72 | var a array 73 | err = af_call(C.af_retain_array(&a.arr, in.arr)) 74 | 75 | out = register(a) 76 | return 77 | } 78 | 79 | /* 80 | 81 | af_err af_create_handle(af_array *arr, const unsigned ndims, const dim_t * const dims, const af_dtype type) 82 | { 83 | return CALL(arr, ndims, dims, type); 84 | } 85 | 86 | af_err af_write_array(af_array arr, const void *data, const size_t bytes, af_source src) 87 | { 88 | return CALL(arr, data, bytes, src); 89 | } 90 | 91 | af_err af_get_data_ptr(void *data, const af_array arr) 92 | { 93 | return CALL(data, arr); 94 | } 95 | 96 | af_err af_get_data_ref_count(int *use_count, const af_array in) 97 | { 98 | return CALL(use_count, in); 99 | } 100 | 101 | af_err af_eval(af_array in) 102 | { 103 | return CALL(in); 104 | } 105 | 106 | af_err af_get_elements(dim_t *elems, const af_array arr) 107 | { 108 | return CALL(elems, arr); 109 | } 110 | 111 | af_err af_get_type(af_dtype *type, const af_array arr) 112 | { 113 | return CALL(type, arr); 114 | } 115 | 116 | af_err af_get_dims(dim_t *d0, dim_t *d1, dim_t *d2, dim_t *d3, const af_array arr) 117 | { 118 | return CALL(d0, d1, d2, d3, arr); 119 | } 120 | 121 | af_err af_get_numdims(unsigned *result, const af_array arr) 122 | { 123 | return CALL(result, arr); 124 | } 125 | 126 | #define ARRAY_HAPI_DEF(af_func) \ 127 | af_err af_func(bool *result, const af_array arr)\ 128 | {\ 129 | return CALL(result, arr);\ 130 | } 131 | 132 | ARRAY_HAPI_DEF(af_is_empty) 133 | ARRAY_HAPI_DEF(af_is_scalar) 134 | ARRAY_HAPI_DEF(af_is_row) 135 | ARRAY_HAPI_DEF(af_is_column) 136 | ARRAY_HAPI_DEF(af_is_vector) 137 | ARRAY_HAPI_DEF(af_is_complex) 138 | ARRAY_HAPI_DEF(af_is_real) 139 | ARRAY_HAPI_DEF(af_is_double) 140 | ARRAY_HAPI_DEF(af_is_single) 141 | ARRAY_HAPI_DEF(af_is_realfloating) 142 | ARRAY_HAPI_DEF(af_is_floating) 143 | ARRAY_HAPI_DEF(af_is_integer) 144 | ARRAY_HAPI_DEF(af_is_bool) 145 | */ 146 | -------------------------------------------------------------------------------- /src/arrayfire/defines.go: -------------------------------------------------------------------------------- 1 | package arrayfire 2 | 3 | /* 4 | #include 5 | #include 6 | */ 7 | import "C" 8 | 9 | type ( 10 | Dype C.af_dtype 11 | Source C.af_source 12 | InterpType C.af_interp_type 13 | BorderType C.af_border_type 14 | Connectivity C.af_connectivity 15 | ConvMode C.af_conv_mode 16 | ConvDomain C.af_conv_domain 17 | MatchType C.af_match_type 18 | YccStd C.af_ycc_std 19 | CSpace C.af_cspace_t 20 | MatProp C.af_mat_prop 21 | NormType C.af_norm_type 22 | Colormap C.af_colormap 23 | ImageFormat C.af_image_format 24 | // TODO - causeda build error: Backend C.af_backend 25 | ) 26 | 27 | const ( 28 | F32 = 0 ///< 32-bit floating point values 29 | C32 = 1 ///< 32-bit complex floating point values 30 | F64 = 2 ///< 64-bit complex floating point values 31 | C64 = 3 ///< 64-bit complex floating point values 32 | B8 = 4 ///< 8-bit boolean values 33 | S32 = 5 ///< 32-bit signed integral values 34 | U32 = 6 ///< 32-bit unsigned integral values 35 | U8 = 7 ///< 8-bit unsigned integral values 36 | S64 = 8 ///< 64-bit signed integral values 37 | U64 = 9 ///< 64-bit unsigned integral values 38 | ) 39 | 40 | /* 41 | typedef enum { 42 | afDevice, ///< Device pointer 43 | afHost, ///< Host pointer 44 | } af_source; 45 | 46 | #define AF_MAX_DIMS 4 47 | 48 | // A handle for an internal array object 49 | typedef void * af_array; 50 | 51 | typedef enum { 52 | AF_INTERP_NEAREST, ///< Nearest Interpolation 53 | AF_INTERP_LINEAR, ///< Linear Interpolation 54 | AF_INTERP_BILINEAR, ///< Bilinear Interpolation 55 | AF_INTERP_CUBIC, ///< Cubic Interpolation 56 | AF_INTERP_LOWER ///< Floor Indexed 57 | } af_interp_type; 58 | 59 | typedef enum { 60 | /// 61 | /// Out of bound values are 0 62 | /// 63 | AF_PAD_ZERO = 0, 64 | 65 | /// 66 | /// Out of bound values are symmetric over the edge 67 | /// 68 | AF_PAD_SYM 69 | } af_border_type; 70 | 71 | typedef enum { 72 | /// 73 | /// Connectivity includes neighbors, North, East, South and West of current pixel 74 | /// 75 | AF_CONNECTIVITY_4 = 4, 76 | 77 | /// 78 | /// Connectivity includes 4-connectivity neigbors and also those on Northeast, Northwest, Southeast and Southwest 79 | /// 80 | AF_CONNECTIVITY_8 = 8 81 | } af_connectivity; 82 | 83 | typedef enum { 84 | 85 | /// 86 | /// Output of the convolution is the same size as input 87 | /// 88 | AF_CONV_DEFAULT, 89 | 90 | /// 91 | /// Output of the convolution is signal_len + filter_len - 1 92 | /// 93 | AF_CONV_EXPAND, 94 | } af_conv_mode; 95 | 96 | typedef enum { 97 | AF_CONV_AUTO, ///< ArrayFire automatically picks the right convolution algorithm 98 | AF_CONV_SPATIAL, ///< Perform convolution in spatial domain 99 | AF_CONV_FREQ, ///< Perform convolution in frequency domain 100 | } af_conv_domain; 101 | 102 | typedef enum { 103 | AF_SAD = 0, ///< Match based on Sum of Absolute Differences (SAD) 104 | AF_ZSAD, ///< Match based on Zero mean SAD 105 | AF_LSAD, ///< Match based on Locally scaled SAD 106 | AF_SSD, ///< Match based on Sum of Squared Differences (SSD) 107 | AF_ZSSD, ///< Match based on Zero mean SSD 108 | AF_LSSD, ///< Match based on Locally scaled SSD 109 | AF_NCC, ///< Match based on Normalized Cross Correlation (NCC) 110 | AF_ZNCC, ///< Match based on Zero mean NCC 111 | AF_SHD ///< Match based on Sum of Hamming Distances (SHD) 112 | } af_match_type; 113 | 114 | typedef enum { 115 | AF_YCC_601 = 601, ///< ITU-R BT.601 (formerly CCIR 601) standard 116 | AF_YCC_709 = 709, ///< ITU-R BT.709 standard 117 | AF_YCC_2020 = 2020 ///< ITU-R BT.2020 standard 118 | } af_ycc_std; 119 | 120 | typedef enum { 121 | AF_GRAY = 0, ///< Grayscale 122 | AF_RGB, ///< 3-channel RGB 123 | AF_HSV, ///< 3-channel HSV 124 | AF_YCbCr ///< 3-channel YCbCr 125 | } af_cspace_t; 126 | 127 | typedef enum { 128 | AF_MAT_NONE = 0, ///< Default 129 | AF_MAT_TRANS = 1, ///< Data needs to be transposed 130 | AF_MAT_CTRANS = 2, ///< Data needs to be conjugate tansposed 131 | AF_MAT_CONJ = 4, ///< Data needs to be conjugate 132 | AF_MAT_UPPER = 32, ///< Matrix is upper triangular 133 | AF_MAT_LOWER = 64, ///< Matrix is lower triangular 134 | AF_MAT_DIAG_UNIT = 128, ///< Matrix diagonal contains unitary values 135 | AF_MAT_SYM = 512, ///< Matrix is symmetric 136 | AF_MAT_POSDEF = 1024, ///< Matrix is positive definite 137 | AF_MAT_ORTHOG = 2048, ///< Matrix is orthogonal 138 | AF_MAT_TRI_DIAG = 4096, ///< Matrix is tri diagonal 139 | AF_MAT_BLOCK_DIAG = 8192 ///< Matrix is block diagonal 140 | } af_mat_prop; 141 | 142 | typedef enum { 143 | AF_NORM_VECTOR_1, ///< treats the input as a vector and returns the sum of absolute values 144 | AF_NORM_VECTOR_INF, ///< treats the input as a vector and returns the max of absolute values 145 | AF_NORM_VECTOR_2, ///< treats the input as a vector and returns euclidean norm 146 | AF_NORM_VECTOR_P, ///< treats the input as a vector and returns the p-norm 147 | AF_NORM_MATRIX_1, ///< return the max of column sums 148 | AF_NORM_MATRIX_INF, ///< return the max of row sums 149 | AF_NORM_MATRIX_2, ///< returns the max singular value). Currently NOT SUPPORTED 150 | AF_NORM_MATRIX_L_PQ, ///< returns Lpq-norm 151 | 152 | AF_NORM_EUCLID = AF_NORM_VECTOR_2, ///< The default. Same as AF_NORM_VECTOR_2 153 | } af_norm_type; 154 | 155 | typedef enum { 156 | AF_COLORMAP_DEFAULT = 0, ///< Default grayscale map 157 | AF_COLORMAP_SPECTRUM= 1, ///< Spectrum map 158 | AF_COLORMAP_COLORS = 2, ///< Colors 159 | AF_COLORMAP_RED = 3, ///< Red hue map 160 | AF_COLORMAP_MOOD = 4, ///< Mood map 161 | AF_COLORMAP_HEAT = 5, ///< Heat map 162 | AF_COLORMAP_BLUE = 6 ///< Blue hue map 163 | } af_colormap; 164 | 165 | typedef enum { 166 | AF_FIF_BMP = 0, ///< FreeImage Enum for Bitmap File 167 | AF_FIF_ICO = 1, ///< FreeImage Enum for Windows Icon File 168 | AF_FIF_JPEG = 2, ///< FreeImage Enum for JPEG File 169 | AF_FIF_JNG = 3, ///< FreeImage Enum for JPEG Network Graphics File 170 | AF_FIF_PNG = 13, ///< FreeImage Enum for Portable Network Graphics File 171 | AF_FIF_PPM = 14, ///< FreeImage Enum for Portable Pixelmap (ASCII) File 172 | AF_FIF_PPMRAW = 15, ///< FreeImage Enum for Portable Pixelmap (Binary) File 173 | AF_FIF_TIFF = 18, ///< FreeImage Enum for Tagged Image File Format File 174 | AF_FIF_PSD = 20, ///< FreeImage Enum for Adobe Photoshop File 175 | AF_FIF_HDR = 26, ///< FreeImage Enum for High Dynamic Range File 176 | AF_FIF_EXR = 29, ///< FreeImage Enum for ILM OpenEXR File 177 | AF_FIF_JP2 = 31, ///< FreeImage Enum for JPEG-2000 File 178 | AF_FIF_RAW = 34 ///< FreeImage Enum for RAW Camera Image File 179 | } af_image_format; 180 | 181 | typedef enum { 182 | AF_BACKEND_DEFAULT = 0, ///< Default backend order: OpenCL -> CUDA -> CPU 183 | AF_BACKEND_CPU = 1, ///< CPU a.k.a sequential algorithms 184 | AF_BACKEND_CUDA = 2, ///< CUDA Compute Backend 185 | AF_BACKEND_OPENCL = 3, ///< OpenCL Compute Backend 186 | } af_backend; 187 | 188 | // Below enum is purely added for example purposes 189 | // it doesn't and shoudn't be used anywhere in the 190 | // code. No Guarantee's provided if it is used. 191 | typedef enum { 192 | AF_ID = 0 193 | } af_someenum_t; 194 | 195 | #ifdef __cplusplus 196 | namespace af 197 | { 198 | typedef af_dtype dtype; 199 | typedef af_source source; 200 | typedef af_interp_type interpType; 201 | typedef af_border_type borderType; 202 | typedef af_connectivity connectivity; 203 | typedef af_match_type matchType; 204 | typedef af_cspace_t CSpace; 205 | typedef af_someenum_t SomeEnum; // Purpose of Addition: How to add Function example 206 | typedef af_mat_prop trans; 207 | typedef af_conv_mode convMode; 208 | typedef af_conv_domain convDomain; 209 | typedef af_mat_prop matProp; 210 | typedef af_colormap ColorMap; 211 | typedef af_norm_type normType; 212 | typedef af_ycc_std YCCStd; 213 | typedef af_image_format imageFormat; 214 | typedef af_backend Backend; 215 | } 216 | 217 | #endif 218 | */ 219 | -------------------------------------------------------------------------------- /src/arrayfire/device.go: -------------------------------------------------------------------------------- 1 | package arrayfire 2 | 3 | /* 4 | #include 5 | */ 6 | import "C" 7 | import ( 8 | "unsafe" 9 | ) 10 | 11 | type DeviceInfo struct { 12 | DName, DPlatform, Toolkit, Compute string 13 | } 14 | 15 | func Info() error { 16 | return af_call(C.af_info()) 17 | } 18 | 19 | func SetDevice(num int) error { 20 | return af_call(C.af_set_device((C.int)(num))) 21 | } 22 | 23 | // Info returns the name, platform, toolkit, and compute identifiers 24 | func GetDeviceInfo() (info DeviceInfo, e error) { 25 | 26 | cdname := C.CString(info.DName) 27 | cdplatform := C.CString(info.DPlatform) 28 | ctoolkit := C.CString(info.Toolkit) 29 | ccompute := C.CString(info.Compute) 30 | 31 | defer func() { 32 | C.free(unsafe.Pointer(cdname)) 33 | C.free(unsafe.Pointer(cdplatform)) 34 | C.free(unsafe.Pointer(ctoolkit)) 35 | C.free(unsafe.Pointer(ccompute)) 36 | }() 37 | 38 | e = af_call(C.af_device_info(cdname, cdplatform, ctoolkit, ccompute)) 39 | 40 | info.DName = C.GoString(cdname) 41 | info.DPlatform = C.GoString(cdplatform) 42 | info.Toolkit = C.GoString(ctoolkit) 43 | info.Compute = C.GoString(ccompute) 44 | 45 | return 46 | } 47 | 48 | // GetDeviceCount returned the device count 49 | func GetDeviceCount() (count int, e error) { 50 | e = af_call(C.af_get_device_count((*C.int)(unsafe.Pointer(&count)))) 51 | return 52 | } 53 | -------------------------------------------------------------------------------- /src/arrayfire/errors.go: -------------------------------------------------------------------------------- 1 | package arrayfire 2 | 3 | /* 4 | #include 5 | #include 6 | */ 7 | import "C" 8 | import ( 9 | "errors" 10 | ) 11 | 12 | var ( 13 | ErrNoMemory = errors.New("Out of device memory") 14 | ErrDriver = errors.New("Driver error") 15 | ErrRuntime = errors.New("Runtime error") 16 | ErrInvalidArray = errors.New("Input is an invalid Array") 17 | ErrArgument = errors.New("Value of input argument is incorrect") 18 | ErrSize = errors.New("Size of input argument is incorrect") 19 | ErrType = errors.New("Type of input argument is incorrect") 20 | ErrTypeMismatch = errors.New("Mismatch between input types") 21 | ErrBatch = errors.New("Batch mode / GFOR not supported") 22 | ErrNotSupported = errors.New("Option not supported") 23 | ErrNotConfigured = errors.New("ArrayFire not built with this feature") 24 | ErrNonFree = errors.New("Function requires nonfree build of ArrayFire") 25 | ErrNoDouble = errors.New("Device does not support double precision") 26 | ErrNoGraphics = errors.New("ArrayFire not built with graphics") 27 | ErrLoadLibrary = errors.New("Error while loading library") 28 | ErrLoadSymbol = errors.New("Error while loading symbol") 29 | ErrInternal = errors.New("Internal error") 30 | ErrUnknown = errors.New("Unknown error") 31 | ) 32 | 33 | const ( 34 | SUCCESS = 0 35 | ERR_NO_MEM = 101 36 | ERR_DRIVER = 102 37 | ERR_RUNTIME = 103 38 | ERR_INVALID_ARRAY = 201 39 | ERR_ARG = 202 40 | ERR_SIZE = 203 41 | ERR_TYPE = 204 42 | ERR_DIFF_TYPE = 205 43 | ERR_BATCH = 207 44 | ERR_NOT_SUPPORTED = 301 45 | ERR_NOT_CONFIGURED = 302 46 | ERR_NONFREE = 303 47 | ERR_NO_DBL = 401 48 | ERR_NO_GFX = 402 49 | ERR_LOAD_LIB = 501 50 | ERR_LOAD_SYM = 502 51 | ERR_INTERNAL = 998 52 | ERR_UNKNOWN = 999 53 | ) 54 | 55 | func af_call(e C.af_err) (err error) { 56 | switch e { 57 | case SUCCESS: 58 | return nil 59 | case ERR_NO_MEM: 60 | return ErrNoMemory 61 | case ERR_DRIVER: 62 | return ErrDriver 63 | case ERR_RUNTIME: 64 | return ErrRuntime 65 | case ERR_INVALID_ARRAY: 66 | return ErrInvalidArray 67 | case ERR_ARG: 68 | return ErrArgument 69 | case ERR_SIZE: 70 | return ErrSize 71 | case ERR_TYPE: 72 | return ErrType 73 | case ERR_DIFF_TYPE: 74 | return ErrTypeMismatch 75 | case ERR_BATCH: 76 | return ErrBatch 77 | case ERR_NOT_SUPPORTED: 78 | return ErrNotSupported 79 | case ERR_NOT_CONFIGURED: 80 | return ErrNotConfigured 81 | case ERR_NONFREE: 82 | return ErrNonFree 83 | case ERR_NO_DBL: 84 | return ErrNoDouble 85 | case ERR_NO_GFX: 86 | return ErrNoGraphics 87 | case ERR_LOAD_LIB: 88 | return ErrLoadLibrary 89 | case ERR_LOAD_SYM: 90 | return ErrLoadSymbol 91 | case ERR_INTERNAL: 92 | return ErrInternal 93 | case ERR_UNKNOWN: 94 | return ErrUnknown 95 | default: 96 | return ErrUnknown 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/arrayfire/graphics.go: -------------------------------------------------------------------------------- 1 | package arrayfire 2 | 3 | /* 4 | #include 5 | #include 6 | */ 7 | import "C" 8 | 9 | type ( 10 | Cell C.af_cell 11 | ) 12 | 13 | type Window struct { 14 | window C.af_window 15 | width int 16 | height int 17 | title string 18 | } 19 | 20 | func (w *Window) Create(width, height int, title string) error { 21 | w.width = width 22 | w.height = height 23 | w.title = title 24 | 25 | return af_call(C.af_create_window(&w.window, C.int(w.width), C.int(w.height), C.CString(w.title))) 26 | } 27 | 28 | func (w *Window) SetPosition(x, y uint) error { 29 | return af_call(C.af_set_position(w.window, C.uint(x), C.uint(y))) 30 | } 31 | 32 | func (w *Window) SetTitle(title string) error { 33 | return af_call(C.af_set_title(w.window, C.CString(title))) 34 | } 35 | 36 | func (w *Window) Grid(rows, cols int) error { 37 | return af_call(C.af_grid(w.window, C.int(rows), C.int(cols))) 38 | } 39 | 40 | func (w *Window) Show() error { 41 | return af_call(C.af_show(w.window)) 42 | } 43 | 44 | func (w *Window) DrawPlot(x Array, y Array, props *Cell) error { 45 | return af_call(C.af_draw_plot(w.window, (C.af_array)(x.arr), 46 | (C.af_array)(y.arr), (*C.af_cell)(props))) 47 | } 48 | 49 | func (w *Window) SetSize(width, height uint) error { 50 | return af_call(C.af_set_size(w.window, C.uint(width), C.uint(height))) 51 | } 52 | 53 | func (w *Window) IsClosed() (closed bool, e error) { 54 | e = af_call(C.af_is_window_closed((*C._Bool)(&closed), w.window)) 55 | return 56 | } 57 | 58 | func (w *Window) DrawImage(a Array, props Cell) error { 59 | return af_call(C.af_draw_image(w.window, (C.af_array)(a.arr), (*C.af_cell)(&props))) 60 | } 61 | 62 | func (w *Window) DrawHist(x Array, minval, maxval float64, props Cell) error { 63 | return af_call(C.af_draw_hist(w.window, (C.af_array)(x.arr), 64 | C.double(minval), C.double(maxval), (*C.af_cell)(&props))) 65 | } 66 | -------------------------------------------------------------------------------- /src/arrayfire/util.go: -------------------------------------------------------------------------------- 1 | package arrayfire 2 | 3 | /* 4 | #include 5 | */ 6 | import "C" 7 | 8 | func Print(a Array) error { 9 | return af_call(C.af_print_array_gen(C.CString(""), (C.af_array)(a.arr), 4)) 10 | } 11 | --------------------------------------------------------------------------------