[mlpack-svn] r15856 - in mlpack/branches/mlpack-bindings/src/mlpack/bindings: . python r

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Thu Sep 26 22:39:34 EDT 2013


Author: pyrotekniq
Date: Thu Sep 26 22:39:34 2013
New Revision: 15856

Log:
Modified (... fixed ...) includes latest pythonarma.i, rarma.i and updated CMakeLists files.

Note that this revision contains broken features.

So long as "-D SWIGR=ON" or "-D SWIGOCTAVE=ON" are not invoked with CMake
("-D SWIGPYTHON=ON" should work), the build should work.



Added:
   mlpack/branches/mlpack-bindings/src/mlpack/bindings/CMakeLists.txt~   (contents, props changed)
   mlpack/branches/mlpack-bindings/src/mlpack/bindings/python/pythonarma.i   (contents, props changed)
   mlpack/branches/mlpack-bindings/src/mlpack/bindings/r/rarma.i   (contents, props changed)

Added: mlpack/branches/mlpack-bindings/src/mlpack/bindings/CMakeLists.txt~
==============================================================================
--- (empty file)
+++ mlpack/branches/mlpack-bindings/src/mlpack/bindings/CMakeLists.txt~	Thu Sep 26 22:39:34 2013
@@ -0,0 +1,14 @@
+# Recurse into individual binding subdirectories, if we are supposed to.
+
+
+#MATLAB ( Old / not SWIG )
+if(MATLAB_BINDINGS)
+  add_subdirectory(matlab)
+endif(MATLAB_BINDINGS)
+
+
+#Methods to bind with SWIG
+
+if(SWIGPYTHON OR SWIGR OR SWIGOCTAVE)
+ add_subdirectory(methods_bindings)
+endif

Added: mlpack/branches/mlpack-bindings/src/mlpack/bindings/python/pythonarma.i
==============================================================================
--- (empty file)
+++ mlpack/branches/mlpack-bindings/src/mlpack/bindings/python/pythonarma.i	Thu Sep 26 22:39:34 2013
@@ -0,0 +1,256 @@
+/**
+ * @file pythonarma.i
+ * @author Nicholas Johnston
+ *
+ * SWIG interface file
+ *
+ * ~ Python ~
+ *
+ */
+
+
+/*******************************
+
+////////////////////////////////
+
+definitions and includes section
+
+////////////////////////////////
+
+*******************************/
+
+//Header %{  %} block
+%{
+
+  #define SWIG_FILE_WITH_INIT 
+
+  //Include mlpack core; this should allow us to use Armadillo types
+  #include <mlpack/core.hpp>
+  #include "stdio.h"
+  #include <numpy/arrayobject.h>
+
+template<typename T> extern void destr(T *ptr)
+{     
+
+    T *p = (T *) PyCapsule_GetPointer((PyObject *) ptr, NULL);
+      
+    delete p;     
+
+}
+
+%}
+
+
+/************************
+
+/////////////////////////
+
+%init section
+
+////////////////////////
+
+***********************/
+
+%init%{
+
+import_array();
+
+%}
+
+
+//Capsule destructor
+
+  template<typename T> extern void destr(T *ptr)
+  {
+
+    T *p = (T *) PyCapsule_GetPointer((PyObject *) ptr, NULL);
+      
+    delete p;     
+
+  }
+
+
+/************************
+
+/////////////////////////
+
+fragment section
+
+/////////////////////////
+
+************************/
+
+%fragment("PyArma", "header")
+{
+
+  void* getPyArrayData(PyObject* o)
+  {
+
+    return (void*)( (PyArrayObject *) o)->data; 
+
+  }
+
+  int getPyNumDim(PyObject* o)
+  {
+
+    return (int)( (PyArrayObject *) o)->nd;
+
+  }
+
+  int getPyDimSize(PyObject* o, int i)
+  {
+
+    return (int)( (PyArrayObject *) o)->dimensions[i];
+
+  }
+
+  void checkDimensions(int dim, int req)
+  {
+
+    if (dim != req)
+    {
+      
+      PyErr_Format(PyExc_IndexError, 
+                   "Wrong number of dimensions.");
+      
+
+    }
+
+  }
+
+}
+
+
+/**********************
+
+///////////////////////
+
+Typemap section
+
+///////////////////////
+
+**********************/
+
+%define PYARMA_TYPEMAPS(type, npy)
+
+  /************************************
+    typemap(in)
+  ************************************/
+   
+  %typemap(in, fragment="PyArma") arma::Mat< type > *INPUT
+  {
+
+    // - NumPy to Armadillo
+    
+    type* dataPointer;
+    int nd;
+    int* dimensions = new int[2];
+  
+    dataPointer   = (type*) getPyArrayData($input);
+    nd            = getPyNumDim($input);
+
+
+    //Make sure there are 2 dimensions 
+    checkDimensions(nd, 2);
+
+    dimensions[0] = getPyDimSize($input, 0);
+    dimensions[1] = getPyDimSize($input, 1);
+
+
+    //Options for Armadillo's Mat constructor
+    bool copy, strict;
+    
+    copy          = false; //Do not let Mat constructor copy auxiliary memory
+    strict        = false;  //Mat instance not tied to auxiliary memory for lifetime, can reallocate
+
+    $1 = new arma::Mat< type >( dataPointer, dimensions[0], dimensions[1], copy, strict);
+    //We don't need "strict=true" as this is an input argument, memory does not need to be
+    //saved from deallocation after going out of scope
+
+  }
+
+  %typemap(freearg) arma::Mat< type > *INPUT
+  {
+
+    delete $1;
+
+  }
+
+
+  /************************************
+    typemap(argout)
+  ************************************/
+
+%typemap(in, numinputs = 0, fragment = "PyArma") arma::Mat < type > *OUTPUT
+{
+
+  //Pass the pointer to Armadillo matrix ...
+arma::Mat< type > *x;
+x = new arma::Mat< type >;
+
+$1 = x;
+
+
+}
+
+
+//argout part...
+%typemap(argout, fragment = "PyArma") arma::Mat < type > *OUTPUT
+{
+
+    //Initialize some temporary variables to store information about the arma::Mat< type > object
+    type* auxMem;
+    npy_intp dims[2]; //2 dimensions; we are passing matrices, not cubes, columns, or rows
+    int typeNum;
+
+    typeNum = npy; //WHY DIDNT I SEE THIS!@#!@##@!#!@
+  
+    
+    //Retrieve the information from our arma::Mat< type > object
+    auxMem      = ($1) -> memptr();
+    dims[0]     = ($1) -> n_rows;
+    dims[1]     = ($1) -> n_cols;
+
+
+  
+    //Create a new PyArray using auxiliary memory (i.e the memory of the arma::Mat< type > object)
+    PyArrayObject *ar = NULL;
+    ar = (PyArrayObject *) PyArray_SimpleNewFromData(2, dims, typeNum, auxMem);
+
+    ar -> flags = ar -> flags & ~( NPY_OWNDATA );
+    ar -> strides[0]    = sizeof(  type  );
+    ar -> strides[1]    = sizeof(  type  ) * dims[0];
+
+
+
+
+    //Create a capsule...
+    PyObject *capsule = PyCapsule_New($1, "cap", destr);
+
+
+    //Set the PyArray Base to the newly created capsule
+    PyArray_BASE(ar) = capsule;
+  
+  
+    //We need to cast 'ar' from PyArrayObject* to PyObject*
+    $result = SWIG_Python_AppendOutput($result, (PyObject *) ar);
+
+}
+
+%enddef
+
+/* Expand macros for C types that are supported by NumPy */
+PYARMA_TYPEMAPS(double, NPY_DOUBLE)
+PYARMA_TYPEMAPS(int, NPY_INT)
+PYARMA_TYPEMAPS(short, NPY_SHORT)
+PYARMA_TYPEMAPS(unsigned short, NPY_USHORT)
+PYARMA_TYPEMAPS(unsigned int, NPY_UINT)
+PYARMA_TYPEMAPS(float, NPY_FLOAT)
+PYARMA_TYPEMAPS(signed char, NPY_BYTE)
+PYARMA_TYPEMAPS(unsigned char, NPY_UBYTE)
+PYARMA_TYPEMAPS(long, NPY_LONG)
+PYARMA_TYPEMAPS(unsigned long, NPY_ULONG)
+PYARMA_TYPEMAPS(long long, NPY_ULONGLONG)
+PYARMA_TYPEMAPS(unsigned long long, NPY_ULONGLONG)
+PYARMA_TYPEMAPS(size_t, NPY_UINTP)
+

Added: mlpack/branches/mlpack-bindings/src/mlpack/bindings/r/rarma.i
==============================================================================
--- (empty file)
+++ mlpack/branches/mlpack-bindings/src/mlpack/bindings/r/rarma.i	Thu Sep 26 22:39:34 2013
@@ -0,0 +1,221 @@
+/**
+ * @file rarma.i
+ * @author Nicholas Johnston
+ *
+ * SWIG interface file
+ *
+ *
+ * ~ R ~
+ *
+ */
+    
+/*******************************
+
+////////////////////////////////
+
+definitions and includes section
+
+////////////////////////////////
+
+*******************************/
+
+//Header %{  %} block
+%{
+
+  //Include mlpack core; this should allow us to use Armadillo types
+  #include <mlpack/core.hpp>
+  //#include <Rinternals.h>
+  #include <R.h>
+
+
+template< typename T > extern void armaFinal(SEXP p)
+{
+
+  if(TYPEOF(p) == EXTPTRSXP)
+  {
+
+    arma::Mat< T > *ptr = NULL;
+
+    ptr = (arma::Mat< T > *) R_ExternalPtrAddr(p); 
+ 
+    delete ptr;
+  
+  }
+
+}
+
+
+
+%}
+
+/************************
+
+/////////////////////////
+
+%init section
+
+////////////////////////
+
+***********************/
+
+
+
+template< typename T > extern void armaFinal(SEXP p)
+{
+
+  if(TYPEOF(p) == EXTPTRSXP)
+  {
+
+    arma::Mat< T > *ptr = NULL;
+
+    ptr = (arma::Mat< T > *) R_ExternalPtrAddr(p); 
+ 
+    delete ptr;
+  
+  }
+
+}
+
+/************************
+
+/////////////////////////
+
+fragment section
+
+/////////////////////////
+
+************************/
+
+//Empty
+
+
+
+/**********************
+
+///////////////////////
+
+Typemap section
+
+///////////////////////
+
+**********************/
+
+%define RARMA_TYPEMAPS(type, S)
+
+  /************************************
+    typemap(in)
+  ************************************/
+   
+  %typemap(in) arma::Mat< type > *INPUT
+  {
+
+    // - R to Armadillo  
+
+    PROTECT($input);
+
+    void *memPtr;
+    int dim[2];
+    bool copy;
+    bool strict;
+
+
+    memPtr = (((SEXPREC_ALIGN *) ($input)) + 1);
+    dim[0] = Rf_ncols($input);
+    dim[1] = Rf_nrows($input);
+
+    copy = false;
+    strict = false;
+
+    UNPROTECT_PTR($input);
+
+
+
+    $1 = new arma::Mat< type > ( (type *) memPtr, dim[0], dim[1], copy, strict);
+
+  }
+
+  %typemap(freearg) arma::Mat< type > *INPUT
+  {
+
+    delete $1;
+
+  }
+
+
+  /************************************
+    typemap(argout)
+  ************************************/
+
+  %typemap(in, numinputs=0) arma::Mat< type > *OUTPUT
+  {
+
+    //Make some R shit
+
+    SEXP m;
+ 
+    m = Rf_allocMatrix(S,2,2);
+
+
+    // - Armadillo to R
+
+    
+
+    arma::Mat< type > *x;
+   
+    x = new arma::Mat< type > ( (type *) (((SEXPREC_ALIGN *) (m)) + 1),2,2,false,true);
+
+    $1 = x;
+    
+
+  }
+
+
+  %typemap(argout) arma::Mat< type > *OUTPUT
+  {    
+    type *memPtr;
+    int dim[2];
+    SEXP m;
+
+    memPtr = ($1) -> memptr();
+    dim[0]= ($1) -> n_rows;
+    dim[1]= ($1) -> n_cols;
+    /*
+    PROTECT(m = Rf_allocMatrix(NILSXP,0,0) );	
+
+    m = SETCDR(m , (SEXP) memPtr);*/
+
+    
+
+
+
+    //Register finalizer
+
+    R_CFinalizer_t f = &armaFinal< type >;
+
+    R_RegisterCFinalizer(m, f);
+
+    
+
+    //Return
+
+    UNPROTECT_PTR(m);
+    
+    $result = m;
+
+  }
+
+%enddef
+
+/* Expand macros for C types that are supported by NumPy */
+RARMA_TYPEMAPS(double, REALSXP)
+RARMA_TYPEMAPS(int, INTSXP)
+RARMA_TYPEMAPS(short, NILSXP)
+RARMA_TYPEMAPS(unsigned short, NILSXP)
+RARMA_TYPEMAPS(unsigned int, NILSXP)
+RARMA_TYPEMAPS(float, NILSXP)
+RARMA_TYPEMAPS(signed char, NILSXP)
+RARMA_TYPEMAPS(unsigned char, NILSXP)
+RARMA_TYPEMAPS(long, NILSXP)
+RARMA_TYPEMAPS(unsigned long, NILSXP)
+RARMA_TYPEMAPS(unsigned long long, NILSXP)
+RARMA_TYPEMAPS(size_t, EXTPTRSXP)



More information about the mlpack-svn mailing list