Sorry it has taken so long to get back to you, Im working on my phd so my
time comes and goes. Im specifically trying to write a python c extension,
which takes some numbers (or arrays) and a string representing a formula,
and uses openmp and libmatheval to compute the output. I will attach a copy
of the code as it stands now. Thank you for the help.
nate

On Sat, Jun 18, 2011 at 1:00 PM, Aleksandar Samardzic <[email protected]>wrote:

> On Tue, Jun 14, 2011 at 12:01:59AM -0400, nate lust wrote:
> > I am working on a small project, using libmatheval and openmp, it seems
> > that when i try and call the libmatheval function from inside the
> parallel
> > region i segfault (or get other memory tramples). If i set the number of
> > threads to one, it runs fine, but defeats the purpose of my code. Is
> > there anyway to make this tread safe?
> > nate
> >
>
> Hello Nate,
>
> Thanks for your message.  Would it be possible for you to provide more
> details, or even better code sample demostrating the type of problem
> you're facing with libmatheval?  The library is written to be
> thread-safe, but I haven't used it along with OpenMP so far...
>
> Regards,
> Alex
>
#include<Python.h>
#include<numpy/arrayobject.h>
#include<math.h>
#include<omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <matheval.h>

#define IND(a,i) *((double *)(a->data+i*a->strides[0]))
#define IND_int(a,i) *((int *)(a->data+i*a->strides[0]))
#define IND2(a,i,j) *((double *)(a->data+i*a->strides[0]+j*a->strides[1]))
#define IND2_int(a,i,j) *((int *)(a->data+i*a->strides[0]+j*a->strides[1]))

static PyObject *mapper(PyObject *self, PyObject *args)
{

  //store the string that is the function
  char * func_string;
  PyObject *array_tuple, *sec;
  void *f,*g;
  char **names;
  int count,i,j;
  PyArrayObject *tmp,*output;
  npy_intp dims[1];

  if(!PyArg_ParseTuple(args,"Os",&array_tuple,&func_string))
    {
      return NULL;
    }

  f = evaluator_create(func_string);
  assert (f);
  
  evaluator_get_variables(f,&names,&count);

  double vals[count], res;

  sec = PySequence_Fast(array_tuple,"expected list or tuple");

  dims[0] = ((PyArrayObject *)PySequence_Fast_GET_ITEM(sec,0))->dimensions[0];

  //setup the output array
  output = (PyArrayObject *) PyArray_SimpleNew(1,dims,PyArray_DOUBLE);

  PyArrayObject * cot[count];

  for(i = 0;i<count;i++)
    {
      cot[i] = (PyArrayObject *) PySequence_Fast_GET_ITEM(sec,i);
    }
  //evaluator_destroy(f);

  //loop over each element in the array

#pragma omp parallel for private(i,vals,tmp,j,res) shared(f,count,names,output,func_string,cot,dims) default(shared)
  for(j=0;j<dims[0];j++)
    {
      //loop over each input array to generate the values to evaluate
      for(i=0;i<count;i++)
	{
	  tmp = cot[i];
	  vals[i] = IND(tmp,j);
	  //printf("%lf ",vals[i]);
	}
      #pragma omp crititcal
      {res = evaluator_evaluate(f,count,names,vals);}
      //printf("%lf\n",evaluator_evaluate(g,count,names,vals));
      IND(output,j) = res;
      //evaluator_destroy(g);
      //printf("\n");

    }
  evaluator_destroy(f);
  return PyArray_Return(output);

}

static PyMethodDef mapper_methods[] = {
  {"mapper",(PyCFunction)mapper,METH_VARARGS},{NULL}};

void initmapper(void)
{
  Py_InitModule("mapper",mapper_methods);
  import_array();
}

Reply via email to