Commits

Anonymous committed a917c40

accesscarray for checking numpy c array accessing

Comments (0)

Files changed (5)

+syntax: glob
+build/

accesscarray/Makefile

+build_ext: accesscarray.so setup.py
+	python setup.py build_ext -i
+
+accesscarray.so: accesscarraymodule.c

accesscarray/accesscarraymodule.c

+#include <Python.h>
+#include "structmember.h"
+#include <numpy/arrayobject.h>
+
+static PyObject *
+print_a1(PyObject *dummy, PyObject *args)
+{
+  int i,j,k, nd;
+  PyObject *o1;
+
+  if (PyArg_ParseTuple(args, "O:print_a1", &o1) < 0) {
+    PyErr_SetString( PyExc_TypeError,  "bad arguments");
+    return NULL;
+  }
+
+  nd = PyArray_NDIM(o1);
+
+  switch(nd){
+  case 1:
+    printf("[%d] ", (int)PyArray_DIMS(o1)[0]);
+    for (i = 0; i < PyArray_DIMS(o1)[0]; ++i){
+      /* printf("%.2f ", ((double *)c1)[i]); */
+    }
+    break;
+  case 2:
+    printf("[%d] ", (int)PyArray_DIMS(o1)[0]);
+    for (i = 0; i < PyArray_DIMS(o1)[0]; ++i){
+      printf("\n  [%d] ", (int)PyArray_DIMS(o1)[1]);
+      for (j = 0; j < PyArray_DIMS(o1)[1]; ++j){
+	/* printf("%.2f ", ((double **)c1)[i][j]); */
+	/* printf("%.2f ", ((double **)PyArray_DATA(o1))[i][j]); */
+	printf("%.2f ", ((double *)PyArray_DATA(o1))[i*PyArray_DIMS(o1)[1]+j]);
+      }
+    }
+    break;
+  case 3:
+    printf("[%d] ", (int)PyArray_DIMS(o1)[0]);
+    for (i = 0; i < PyArray_DIMS(o1)[0]; ++i){
+      printf("\n  [%d] ", (int)PyArray_DIMS(o1)[1]);
+      for (j = 0; j < PyArray_DIMS(o1)[1]; ++j){
+	printf("\n    [%d] ", (int)PyArray_DIMS(o1)[2]);
+	for (k = 0; k < PyArray_DIMS(o1)[2]; ++k){
+	  /* printf("%.2f ", ((double ***)c1)[i][j][k]); */
+	  printf("%.2f ", ((double *)PyArray_DATA(o1))[i*PyArray_DIMS(o1)[1]*PyArray_DIMS(o1)[2]+j*PyArray_DIMS(o1)[2]+k]);
+	}
+      }
+    }
+    break;
+  }
+  printf("\n");
+
+  /* printf("if ( ((PyArrayObject *)o1)->data == c1): "); */
+  /* if ( ((PyArrayObject *)o1)->data == c1){ */
+  /*   printf("True\n"); */
+  /* }else{ */
+  /*   printf("False\n"); */
+  /* } */
+
+  /* if (PyArray_Free(o1, (void *)c1) < 0){ */
+  /*   PyErr_SetString( PyExc_TypeError,  "PyArray_Free fail"); */
+  /*   return NULL; */
+  /* } */
+
+  return Py_BuildValue("");  /* return None */
+}
+
+
+static PyObject *
+print_a2(PyObject *dummy, PyObject *args)
+{
+  int i,j,k, nd;
+  PyObject *o1;
+  char *c1;
+
+  if (PyArg_ParseTuple(args, "O:print_a1", &o1) < 0) {
+    PyErr_SetString( PyExc_TypeError,  "bad arguments");
+    return NULL;
+  }
+
+  nd = PyArray_NDIM(o1);
+  c1 = PyArray_BYTES(o1);
+
+  switch(nd){
+  case 1:
+    {
+      int n0 = PyArray_DIM(o1,0);
+      printf("[%d] ", n0);
+      for (i = 0; i < n0; ++i){
+	printf("%.2f ", ((double *)c1)[i]);
+      }
+    }
+    break;
+  case 2:
+    {
+      int n0 = PyArray_DIM(o1,0);
+      int n1 = PyArray_DIM(o1,1);
+      printf("[%d] ", n0);
+      for (i = 0; i < n0; ++i){
+	printf("\n  [%d] ", n1);
+	for (j = 0; j < n1; ++j){
+	  printf("%.2f ", ((double *)c1)[i*n0+j]);
+	}
+      }
+    }
+    break;
+  case 3:
+    {
+      int n0 = PyArray_DIM(o1,0);
+      int n1 = PyArray_DIM(o1,1);
+      int n2 = PyArray_DIM(o1,2);
+      printf("[%d] ", n0);
+      for (i = 0; i < n0; ++i){
+	printf("\n  [%d] ", n1);
+	for (j = 0; j < n1; ++j){
+	  printf("\n    [%d] ", n2);
+	  for (k = 0; k < n2; ++k){
+	    printf("%.2f ", ((double *)c1)[i*n0*n1+j*n1+k]);
+	  }
+	}
+      }
+    }
+    break;
+  }
+  printf("\n");
+
+  return Py_BuildValue("");  /* return None */
+}
+
+static PyObject *
+check_pointer(PyObject *dummy, PyObject *args)
+{
+  int nd;
+  PyObject *obj;
+  char *data;
+  int num_elem=0, num_missmatch=0;
+
+  if (PyArg_ParseTuple(args, "O:check_pointer", &obj) < 0) {
+    PyErr_SetString( PyExc_TypeError,  "bad arguments");
+    return NULL;
+  }
+
+  nd = PyArray_NDIM(obj);
+  data = PyArray_BYTES(obj);
+
+  switch(nd){
+  case 1:
+    {
+      int n0 = PyArray_DIM(obj,0);
+      int i;
+      for (i = 0; i < n0; ++i){
+	if (PyArray_GETPTR1(obj, i) != (void *)&data[i]){
+	  num_missmatch++;
+	}
+	num_elem++;
+      }
+    }
+    break;
+  case 2:
+    {
+      int n0 = PyArray_DIM(obj,0);
+      int n1 = PyArray_DIM(obj,1);
+      int i,j;
+      for (i = 0; i < n0; ++i){
+	for (j = 0; j < n1; ++j){
+	  if (PyArray_GETPTR2(obj, i, j) != (void *)&data[i*n0+j]){
+	    num_missmatch++;
+	  }
+	  num_elem++;
+	}
+      }
+    }
+    break;
+  case 3:
+    {
+      int n0 = PyArray_DIM(obj,0);
+      int n1 = PyArray_DIM(obj,1);
+      int n2 = PyArray_DIM(obj,2);
+      int i,j,k;
+      for (i = 0; i < n0; ++i){
+	for (j = 0; j < n1; ++j){
+	  for (k = 0; k < n2; ++k){
+	    if (PyArray_GETPTR1(obj, i) != (void *)&data[i*n0*n1+j*n1+k]){
+	      num_missmatch++;
+	    }
+	    num_elem++;
+	  }
+	}
+      }
+    }
+    break;
+  }
+  //PySys_WriteStdout
+  printf("Missmatch: %.2f%% (%d/%d) \n",
+	 100.0*num_missmatch/num_elem, num_missmatch, num_elem);
+
+  return Py_BuildValue("");  /* return None */
+}
+
+
+
+static PyMethodDef module_methods[] = {
+  {"print_a1", (PyCFunction)print_a1, METH_VARARGS, ""},
+  {"print_a2", (PyCFunction)print_a2, METH_VARARGS, ""},
+  {"check_pointer", (PyCFunction)check_pointer, METH_VARARGS, ""},
+  {NULL}  /* Sentinel */
+};
+
+#ifndef PyMODINIT_FUNC	/* declarations for DLL import/export */
+#define PyMODINIT_FUNC void
+#endif
+PyMODINIT_FUNC
+initaccesscarray(void)
+{
+  (void) Py_InitModule("accesscarray", module_methods);
+  import_array();   /* required NumPy initialization */
+}

accesscarray/check_pointer.py

+import numpy
+from accesscarray import *
+
+fac1d = numpy.array([1,2,3], dtype=numpy.float, order='C')
+faf1d = numpy.array([1,2,3], dtype=numpy.float, order='F')
+fac2d = numpy.array([[1,2,3],[1,2,3],[1,2,3]], dtype=numpy.float)
+faf2d = numpy.array([[1,2,3],[1,2,3],[1,2,3]], dtype=numpy.float, order='F')
+fac3d = numpy.array([[[1,2],[1,2]],[[1,2],[1,2]]], dtype=numpy.float)
+
+print
+print 'print_a1'
+print_a1(fac1d)
+print_a1(faf1d)
+print_a1(fac2d)
+print_a1(faf2d)
+print_a1(fac3d)
+
+print
+print 'print_a2'
+print_a2(fac1d)
+print_a2(faf1d)
+print_a2(fac2d)
+print_a2(faf2d)
+print_a2(fac3d)
+
+print
+print 'check_pointer'
+check_pointer(fac1d)
+check_pointer(faf1d)
+check_pointer(fac2d)
+check_pointer(faf2d)
+check_pointer(fac3d)

accesscarray/setup.py

+from numpy.distutils.core import setup, Extension
+
+module1 = Extension(
+    'accesscarray',
+    sources = ['accesscarraymodule.c'],
+    extra_compile_args = ["-O3", "-fPIC", ],
+)
+
+setup( name = 'my',
+       ext_modules = [module1],
+)
+
+# compile: python setup.py build_ext -i -f