Esempio n. 1
0
static PyObject *sps_create(PyObject *self, PyObject *args)
{
  char *spec_version, *array_name;
  int rows, cols, type = SPS_DOUBLE, flag = 0;
  npy_intp dims[2];
  int ptype, stype;
  PyArrayObject *arrobj;
  void *data;

  if (!PyArg_ParseTuple(args, "ssii|ii", &spec_version, &array_name,
            &rows, &cols, &type, &flag)) {
    return NULL;
  }

  if (SPS_CreateArray(spec_version, array_name, rows, cols, type, flag)) {
  	struct module_state *st = GETSTATE(self);
    PyErr_SetString(st->SPSError, "Error getting array info");
    return NULL;
  }

  if ((data = SPS_GetDataPointer(spec_version, array_name, 1)) == NULL) {
    
  	struct module_state *st = GETSTATE(self);
    PyErr_SetString(st->SPSError, "Error getting data pointer");
    return NULL;
  }

  dims[0]=rows;
  dims[1]=cols;
  ptype = sps_type2py(type);
  stype = sps_py2type(ptype);

  if (type != stype) {
    struct module_state *st = GETSTATE(self);
    PyErr_SetString(st->SPSError, "Type of data in shared memory not supported");
    return NULL;
  }

  if ((arrobj = (PyArrayObject*) PyArray_SimpleNewFromData(2, dims, ptype, data))
      == NULL) {
    /* Should delete the array  - don't have a lib function !!! FIXTHIS */
    struct module_state *st = GETSTATE(self);
    PyErr_SetString(st->SPSError, "Could not create mathematical array");
    return NULL;
  }

  return (PyObject*) arrobj;
}
Esempio n. 2
0
static PyObject *sps_putdatarow(PyObject *self, PyObject *args)
{
  char *spec_version, *array_name;
  int ptype, stype;
  PyObject *in_src;
  PyArrayObject *src;
  int no_items;
  int in_row;


  if (!PyArg_ParseTuple(args, "ssiO", &spec_version, &array_name, &in_row,
            &in_src)) {
    return NULL;
  }

  if (!(src = (PyArrayObject*) PyArray_ContiguousFromObject(in_src,
                NPY_NOTYPE, 1, 1))) {
    struct module_state *st = GETSTATE(self);
    PyErr_SetString(st->SPSError, "Input Array is not a 1 dim array");
    return NULL;
  }

  ptype = PyArray_DESCR(src)->type_num;
  stype = sps_py2type(ptype);

  if (ptype == -1) {
    struct module_state *st = GETSTATE(self);
    PyErr_SetString(st->SPSError, "Type of data in shared memory not supported");
    Py_DECREF(src);
    return NULL;
  }

  no_items = (int) (PyArray_DIMS(src)[0]);

  if (SPS_CopyRowToShared(spec_version, array_name, PyArray_DATA(src), stype,
              in_row, no_items, NULL)
      == -1) {
    struct module_state *st = GETSTATE(self);
    PyErr_SetString(st->SPSError, "Error copying data to shared memory");
    Py_DECREF(src);
    return NULL;
  }else
   Py_DECREF(src);

  Py_INCREF(Py_None);
  return Py_None;
}
Esempio n. 3
0
static PyObject *sps_attach(PyObject *self, PyObject *args)
{
  char *spec_version, *array_name;
  int rows, cols, type, flag;
  npy_intp dims[2];
  int ptype, stype;
  PyArrayObject *arrobj;
  void *data;

  if (!PyArg_ParseTuple(args, "ss", &spec_version, &array_name)) {
    return NULL;
  }

  if (SPS_GetArrayInfo(spec_version, array_name, &rows, &cols, &type, &flag)) {
  	struct module_state *st = GETSTATE(self);
    PyErr_SetString(st->SPSError, "Error getting array info");
    return NULL;
  }

  if ((data = SPS_GetDataPointer(spec_version, array_name, 1)) == NULL) {
    struct module_state *st = GETSTATE(self);
    PyErr_SetString(st->SPSError, "Error getting data pointer");
    return NULL;
  }

  dims[0]=rows;
  dims[1]=cols;
  ptype = sps_type2py(type);
  stype = sps_py2type(ptype);

  if (type != stype) {
    SPS_ReturnDataPointer(data);
  	struct module_state *st = GETSTATE(self);
    PyErr_SetString(st->SPSError, "Type of data in shared memory not supported");
    return NULL;
  }

  if ((arrobj = (PyArrayObject*) PyArray_SimpleNewFromData(2, dims, ptype, data))
      == NULL) {
    SPS_ReturnDataPointer(data);
    struct module_state *st = GETSTATE(self);
    PyErr_SetString(st->SPSError, "Could not create mathematical array");
    return NULL;
  }

  return (PyObject*) arrobj;
}
Esempio n. 4
0
static PyObject *sps_getdatarow(PyObject *self, PyObject *args)
{
  char *spec_version, *array_name;
  int rows, cols, type, flag, in_row, in_col = 0;
  npy_intp dims[2];
  int ptype, stype;
  PyArrayObject *arrobj, *arrobj_nc;

  if (!PyArg_ParseTuple(args, "ssi|i", &spec_version, &array_name, &in_row,
            &in_col)) {
    return NULL;
  }

  if (SPS_GetArrayInfo(spec_version, array_name, &rows, &cols, &type, &flag)) {
    struct module_state *st = GETSTATE(self);
    PyErr_SetString(st->SPSError, "Error getting array info");
    return NULL;
  }

  dims[0] = (in_col == 0) ? cols : in_col;

  ptype = sps_type2py(type);
  if ((arrobj_nc = (PyArrayObject*) PyArray_SimpleNew(1, dims, ptype))
      == NULL) {
    struct module_state *st = GETSTATE(self);
    PyErr_SetString(st->SPSError, "Could not create mathematical array");
    return NULL;
  }

  if ((arrobj = (PyArrayObject*) PyArray_ContiguousFromObject(
            (PyObject*) arrobj_nc, ptype, 1, 1)) == NULL) {
    Py_DECREF(arrobj_nc);
    struct module_state *st = GETSTATE(self);
    PyErr_SetString(st->SPSError, "Could not make our array contiguous");
    return NULL;
  } else
    Py_DECREF(arrobj_nc);

  stype = sps_py2type(ptype);
  SPS_CopyRowFromShared(spec_version, array_name, PyArray_DATA(arrobj), stype ,
             in_row, in_col, NULL);

  return (PyObject*) arrobj;
}
Esempio n. 5
0
static PyObject *sps_putdatarow(PyObject *self, PyObject *args)
{
  char *spec_version, *array_name;
  int ptype, stype;
  PyObject *in_src;
  PyArrayObject *src;
  int no_items;
  int in_row;


  if (!PyArg_ParseTuple(args, "ssiO", &spec_version, &array_name, &in_row,
            &in_src)) {
    return NULL;
  }

  if (!(src = (PyArrayObject*) PyArray_ContiguousFromObject(in_src,
                PyArray_NOTYPE, 1, 1))) {
    PyErr_SetString(SPSError, "Input Array is not a 1 dim array");
    return NULL;
  }

  ptype = src->descr->type_num;
  stype = sps_py2type(ptype);

  if (ptype == -1) {
    PyErr_SetString(SPSError, "Type of data in shared memory not supported");
    Py_DECREF(src);
    return NULL;
  }

  no_items = src->dimensions[0];

  if (SPS_CopyRowToShared(spec_version, array_name, src->data, stype,
              in_row, no_items, NULL)
      == -1) {
    PyErr_SetString(SPSError, "Error copying data to shared memory");
    Py_DECREF(src);
    return NULL;
  }else
   Py_DECREF(src);

  Py_INCREF(Py_None);
  return Py_None;
}
Esempio n. 6
0
static PyObject *sps_getdata(PyObject *self, PyObject *args)
{
  char *spec_version, *array_name;
  int rows, cols, type, flag;
  npy_intp dims[2];
  int ptype, stype;
  PyArrayObject *arrobj, *arrobj_nc;

  if (!PyArg_ParseTuple(args, "ss", &spec_version, &array_name)) {
    return NULL;
  }

  if (SPS_GetArrayInfo(spec_version, array_name, &rows, &cols, &type, &flag)) {
    PyErr_SetString(SPSError, "Error getting array info");
    return NULL;
  }

  dims[0]=rows;
  dims[1]=cols;
  ptype = sps_type2py(type);
  if ((arrobj_nc = (PyArrayObject*) PyArray_SimpleNew(2, dims, ptype))
      == NULL) {
    PyErr_SetString(SPSError, "Could not create mathematical array");
    return NULL;
  }

  if ((arrobj = (PyArrayObject*) PyArray_ContiguousFromObject(
           (PyObject*) arrobj_nc, ptype, 2, 2)) == NULL) {
    Py_DECREF(arrobj_nc);
    PyErr_SetString(SPSError, "Could not make our array contiguous");
    return NULL;
  } else
    Py_DECREF(arrobj_nc);

  stype = sps_py2type(ptype);
  SPS_CopyFromShared(spec_version, array_name, arrobj->data, stype ,
             rows * cols);

  return (PyObject*) arrobj;
}