Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cythonizing calc_grid_values_ext #15

Merged
merged 2 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#include "Python.h"
#include "numpy/arrayobject.h"
#include <stdio.h>
#include <math.h>
//#include <malloc.h>

#define DDATA(p) ((double*)(((PyArrayObject *)p)->data))
#define IDATA(p) ((long*)(((PyArrayObject *)p)->data))

#define MIN(a, b) (((a)<=(b))?(a):(b))
#define MAX(a, b) (((a)>(b))?(a):(b))
Expand Down Expand Up @@ -405,89 +399,3 @@ void _calc_grid_values( double *x, double *y, double *norms,
}

}

static PyObject *calc_grid_values( PyObject *self, PyObject *args )
{
int i, ok, num_tri, num_vert, ncol, nrow, num_norms, num_grid_val;
long *volumes;
double nodata_val;
double cell_size;
double *x, *y;
double *norms;
double *result;
double *grid_val;
PyObject *pyobj_x;
PyObject *pyobj_y;
PyObject *pyobj_norms;
PyObject *pyobj_volumes;
PyObject *pyobj_result;
PyObject *pyobj_grid_val;

ok = PyArg_ParseTuple( args, "iiddOOOOOO",
&nrow,
&ncol,
&cell_size,
&nodata_val,
&pyobj_x,
&pyobj_y,
&pyobj_norms,
&pyobj_volumes,
&pyobj_result,
&pyobj_grid_val );




if( !ok ){
fprintf( stderr, "calc_grid_values: argument parsing error\n" );
exit(1);
}

// get data from python objects
x = DDATA( pyobj_x );
y = DDATA( pyobj_y );
norms = DDATA( pyobj_norms );
result = DDATA( pyobj_result );
grid_val = DDATA( pyobj_grid_val );
volumes = IDATA( pyobj_volumes );


num_tri = ((PyArrayObject*)pyobj_volumes)->dimensions[0];
num_vert = ((PyArrayObject*)pyobj_x)->dimensions[0];
num_norms = ((PyArrayObject*)pyobj_norms)->dimensions[0];
num_grid_val = ((PyArrayObject*)pyobj_grid_val)->dimensions[0];

//printf("==== %d %d %d %d %d \n",num_norms,num_tri,num_vert,nrow,ncol);

// init triangle array
init_norms( x,y, norms, volumes, num_tri );



//printf("+++ %d\n",nrow*ncol);
// evaluate grid
for ( i = 0 ; i < nrow*ncol; i++ )
grid_val[i] = nodata_val;



_calc_grid_values( x,y, norms, num_vert, volumes, num_tri, \
cell_size, nrow, ncol, \
result, grid_val );


return Py_BuildValue("");
}

static PyMethodDef calc_grid_values_ext_methods[] = {
{"calc_grid_values", calc_grid_values, METH_VARARGS},
{NULL, NULL}
};

void initcalc_grid_values_ext( )
{
(void) Py_InitModule( "calc_grid_values_ext", calc_grid_values_ext_methods );

import_array( );
}

45 changes: 45 additions & 0 deletions anuga/file_conversion/calc_grid_values_ext.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#cython: wraparound=False, boundscheck=False, cdivision=True, profile=False, nonecheck=False, overflowcheck=False, cdivision_warnings=False, unraisable_tracebacks=False
import cython

# import both numpy and the Cython declarations for numpy
import numpy as np
cimport numpy as np

# declare the interface to the C code
cdef extern from "calc_grid_values.c":
void _calc_grid_values(double* x, double* y, double* norms, int num_vert, long* volumes, int num_tri, double cell_size, int nrow, int ncol, double* vertex_val, double* grid_val)
void init_norms(double* x, double* y, double* norms, long* volumes, int num_tri)


def calc_grid_values(int nrow, int ncol,\
double cell_size,\
double nodata_val,\
np.ndarray[double, ndim=1, mode="c"] x not None,\
np.ndarray[double, ndim=1, mode="c"] y not None,\
np.ndarray[double, ndim=1, mode="c"] norms not None,\
np.ndarray[long, ndim=2, mode="c"] volumes not None,\
np.ndarray[double, ndim=1, mode="c"] result not None,\
np.ndarray[double, ndim=1, mode="c"] grid_val not None):

cdef int i, num_tri, num_vert, num_norms, num_grid_val

num_tri = volumes.shape[0]
num_vert = x.shape[0]
num_norms = norms.shape[0]
num_grid_val = grid_val.shape[0]

init_norms(&x[0], &y[0], &norms[0], &volumes[0,0], num_tri)

for i in xrange(nrow*ncol):
grid_val[i] = nodata_val

_calc_grid_values(&x[0], &y[0],\
&norms[0],\
num_vert,\
&volumes[0,0],\
num_tri,\
cell_size,\
nrow,\
ncol,\
&result[0],\
&grid_val[0])
8 changes: 6 additions & 2 deletions anuga/file_conversion/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import sys

from os.path import join
from Cython.Build import cythonize
import Cython.Compiler.Options
Cython.Compiler.Options.annotate = True

def configuration(parent_package='',top_path=None):

Expand All @@ -18,9 +21,10 @@ def configuration(parent_package='',top_path=None):
util_dir = join('..','utilities')

config.add_extension('calc_grid_values_ext',
sources=['calc_grid_values_ext.c'],
sources=['calc_grid_values_ext.pyx'],
include_dirs=[util_dir])


config.ext_modules = cythonize(config.ext_modules, annotate=True)

return config

Expand Down