Hello,
What is the right way to create a 2D array to pass to a c-function that expects **arr?
I tried:
(defun make-matrix (lisp-mat) "Creates a two-dimensional c array, initializes with lisp matrix" (let* ((x-dim (array-dimension lisp-mat 0)) (y-dim (array-dimension lisp-mat 1)) (c-mat (foreign-alloc :pointer :count y-dim))) (dotimes (y y-dim) (let ((cur (foreign-alloc :double :count x-dim))) (setf (mem-aref c-mat :pointer y) cur) (dotimes (x x-dim) (setf (mem-aref cur :double y) (coerce (aref lisp-mat x y) 'double-float))))) c-mat))
To pass the argument z to the c-function:
c_plmesh(PLFLT *x, PLFLT *y, PLFLT **z, PLINT nx, PLINT ny, PLINT opt);
(PLFLT is of type double & PLINT is of type int)
But some of the values appear to get mangled in transit.
Thanks, -Hazen
(CFFI 0.9.0, SBCL 0.9.9, OS-X 10.4)
hbabcockos1@mac.com writes:
What is the right way to create a 2D array to pass to a c-function that expects **arr?
In message ID BAY17-DAV41D78728D384B9C2D15ADD9300@phx.gbl, Vasilis sent some functions macros to manipulate and create arrays of arbitrary ranks: http://common-lisp.net/pipermail/cffi-devel/2005-December/000281.html
This hasn't been tested so let us know if it works for you as we should probably incorporate that into CFFI. (Ah, and watch out for ,@ mangling in the archive).
HTH
On Mar 15, 2006, at 6:46 AM, Luís Oliveira wrote:
hbabcockos1@mac.com writes:
What is the right way to create a 2D array to pass to a c-function that expects **arr?
In message ID BAY17-DAV41D78728D384B9C2D15ADD9300@phx.gbl, Vasilis sent some functions macros to manipulate and create arrays of arbitrary ranks: http://common-lisp.net/pipermail/cffi-devel/2005-December/ 000281.html
I was able to get this to work. I was using the wrong index variable (y instead of x) in the inner loop.
(defun make-matrix (lisp-mat) "Creates a two-dimensional c array, initializes with lisp matrix" (let* ((x-dim (array-dimension lisp-mat 0)) (y-dim (array-dimension lisp-mat 1)) (c-mat (foreign-alloc :pointer :count y-dim))) (dotimes (y y-dim) (let ((cur (foreign-alloc :double :count x-dim))) (setf (mem-aref c-mat :pointer y) cur) (dotimes (x x-dim) (setf (mem-aref cur :double x) (coerce (aref lisp-mat x y) 'double-float))))) c-mat)) ^^^
Thanks for the suggestion though, it looks like a nice generalization of the specific case I am trying to handle.
-Hazen