You are viewing [info]lhealy's journal

Liam Healy

Apr. 28th, 2012

06:31 pm - mem-aref and mem-aptr in CFFI

The master branch of CFFI has the cffi-libffi system, which permits passing and returning structures by value. As part of that addition, some of the syntax for structures has changed, even without loading that system. Before, "bare structure" references were permitted, and they were interpreted to mean pointers. Now, the syntax to specify a structure type is (:struct name) for the structure by value, and :pointer or (:pointer (:struct name)) for the structure by reference. The bare structure form is still accepted, with a style warning.

The function mem-aref returns an element of the array, an object; since we now can translate foreign structures, that includes structures. If the type is designated as (:struct name), that means a CL representation of the structure will be returned. By default (with no translate-from-foreign method defined), that is a plist. If you want a pointer instead, use the new function mem-aptr. As a special case, using the bare structure type specification, mem-aref still returns a pointer, but if you use the new form, you need to call mem-aptr to get the pointer instead of the object translated into CL.

(Rationale for the new function is here.)

If this isn't working as described, please post to cffi-devel.

Tags:

Apr. 14th, 2012

03:02 pm - cffi-libffi

CFFI now has (on the master branch, but not yet in quicklisp) the system cffi-libffi, which allows calling and returning structures by value. For example, in the GSL library, complex numbers and functions are defined passing and returning the complex structures by value, so we can define

    (cffi:defcstruct (complex-double-c :class complex-double-type)
      (dat :double :count 2))

    (defmethod cffi:translate-into-foreign-memory ((value complex) (type complex-double-type) p)
      (cffi:with-foreign-slots ((dat) p (:struct complex-double-c))
        (setf (cffi:mem-aref dat :double 0) (realpart value)
    	  (cffi:mem-aref dat :double 1) (imagpart value))))

    (defmethod cffi:translate-from-foreign (p (type complex-double-type))
      (cffi:with-foreign-slots ((dat) p (:struct complex-double-c))
        (complex (cffi:mem-aref dat :double 0)
    	     (cffi:mem-aref dat :double 1))))

    (cffi:define-foreign-library libgslcblas
      (:unix (:or "libgslcblas.so.0" "libgslcblas.so"))
      (t (:default "libgslcblas")))

    (cffi:define-foreign-library libgsl
      (:unix (:or "libgsl.so.0" "libgsl.so"))
      (t (:default "libgsl")))

    (cffi:use-foreign-library libgslcblas)
    (cffi:use-foreign-library libgsl)

    (defun complex-log (c)
       (cffi:foreign-funcall "gsl_complex_log"
        (:struct complex-double-c) c (:struct complex-double-c)))

and then find
   (complex-log #C(1.0d0 2.0d0))
   #C(0.8047189562170501d0 1.1071487177940904d0)

Note that there are changes that affect CFFI users even if they don't load cffi-libffi; using bare structure names foo insead of (:struct foo) or :pointer or (:pointer (:struct foo)) will trigger a style warning, and there is a new function mem-aptr that does for structure arrays what mem-aref does when the bare structure names are used. Please try it and report issues on cffi-devel. Thank you.

Note: the old system FSBV, which did what cffi-libffi does but in a clumsier way, is now obsolete and will not work with this version of CFFI.

Tags:

Nov. 29th, 2011

10:00 pm - How to specify foreign libraries

Does this look right to you?

(define-foreign-library frobnicator
  (cffi-features:unix "/usr/lib/libfrobnicate.so"))

If it does, you should read Anton Kovalenko's blog, whether or not you use CFFI. (Reblogging this because the original was evidently not picked up by Planet Lisp.)

Tags:

Sep. 25th, 2011

11:33 pm - CFFI-FSBV first light

Luis and I have been working on merging FSBV into CFFI as a new system CFFI-FSBV. This will allow calling foreign functions with structures passed and/or returned by value. It uses libffi.

The new system will have some advantages over the old. From the users perspective, there won't be a need to download a separate repository, and there won't be a need to directly use anything from CFFI-FSBV; all calls by value look just like regular CFFI with the appropriate type declarations. Based on a discussion on the cffi-devel mailing list, we decided and Luis implemented the syntax (:struct foo) for the structure (by value) and (:pointer (:struct foo)) for the pointer. Using 'foo directly is accepted for now with a warning, but is deprecated.

We now have, as astronomers say about a new telescope, "first light". Using my favorite complex number example from GSL

(defcstruct (complex-double :class complex-type)
  (dat :double :count 2))

and then with a few more definitions, we can call the foreign functions with the structure passed by value,
(foreign-funcall "gsl_complex_conjugate"
 (:struct complex-double) #C(3.0d0 4.0d0) (:struct complex-double))
#C(3.0d0 -4.0d0)
(foreign-funcall "gsl_complex_abs" (:struct complex-double) #C(3.0d0 4.0d0) :double)
5.0d0

Soon I hope we can reduce the "few more definitions", which do the translation between Lisp and C and are given in fsbv/examples.lisp, to something like what's in the old system's definition of defcstruct, with :constructor and :deconstructor options.

There's a lot more to do before this is ready for release, but we've got a start. The work is in the fsbv branch.

Tags:

Apr. 5th, 2011

10:40 pm - Kawa Google summer of code

Kawa is an implementation of lisp-like and dynamic languages (notably Scheme) that compiles to JVM. They are part of Google Summer of Code this year. One of their wishlist tasks is to enhance Common Lisp support. It would be really interesting to see work on this; I understand the deadline is this week for GSoC if anyone is interested.

Tags:

Feb. 5th, 2011

05:52 pm - Tablet usage in Ubuntu

Having just received a Thinkpad X201 Tablet, and installing Ubuntu 10.10, I decided that I should try the tablet function. There are some packages available for Ubuntu that work well: Xournal, which allows you to draw and write on the stylus, CellWriter, which recognizes your printed letters (one at a time, each in a box), and Magick Rotation, which rotates the screen from landscape to portrait. when you flip the cover to tablet mode. The first two are regular Ubuntu packages; Magick Rotation is downloaded from Launchpad and has a somewhat odd installation; it appears that you download a script that then wgets the real application.

They all work quite well. I particularly like xournal's feature that can read a PDF and save the written-on result to a PDF; even apart from the tablet functionality, just being able to fill in a PDF form and save it is nice (you can use the keys to write, so you don't need a stylus and touchscreen). Of course, you're not really filling in a form in the PDF sense, you are creating an image on top of the old one.

The buttons adjacent to the screen don't work, I'm not sure how to make them work.

Oct. 15th, 2010

10:50 pm - Quicklisp is now the official installation method for GSLL

Quicklisp is now the official installation method for GSLL. As far as I know it is the first and maybe so far the only project to recommend quicklisp as the installation method. Part of my motivation for writing GSLL was to lure^H^H^H^H induce people doing numerical computation into using Lisp, even if only as a desk calculator. A major impediment to that was loading everything up, and quicklisp goes a long way to solving that problem. There is a subtle difficulty in being able to push patches from a frequently-changing library like GSLL but I think we can work around that.

Tags:

Jun. 21st, 2010

10:36 pm - A CL workbook

I am looking for a way to conveniently keep a session of Lisp interaction. For example, when I write down a problem and solution for class, I define a succession of variables with defparameter (or a slightly more convenient form I've defined). In a later year, if I give the problem again with different numbers, I just change the defining parameters, and recalculate. This is clumsy, but it mostly works. What I'd really like is to define a few input parameters, and stepwise calculate the intermediate quantities. A single defun isn't appropriate because I need the intermediate values to show to the students. The defparameter approach is tedious and prone to error.

What I envision is a structure or class instance with "given values" provided at the beginning, and steps consisting of the form and the value. A new instance of the same problem would have the same forms to evaluate, but different values. It might also be nice to branch, i.e., at some step in the calculation, continue with a different set of calculations. but still be able to keep the tree structure (kind of like git for Lisp forms? hmmm).

Is there anything like this (in any language) out there? I tried some googling but if there's something along these lines out there, I'm not using the right keywords. For lack of a better word, I'm calling it a Lisp workbook or notebook. Bonus if it will integrate with LaTeX and/or org-mode.

Tags:

Nov. 25th, 2009

06:29 pm - Sanitizing history in git

It may happen that you wish to replace some string throughout a git repository and its history. This can be done with a combination of rpl and git filter-branch. The former recursively replaces strings in a directory and the latter applies it to all items in the history. So for example,

git filter-branch --tree-filter 'rpl -iR oldstring newstring *' HEAD

will do the job. If this branch tracks a remote, then untrack it,
  git remote rm origin
  rm -rf .git/refs/original

Now this is a freestanding branch which can be pushed to a new remote. Note that everyone who cloned from an existing remote that has been replaced will need to discard their files and clone again, because all the IDs for the entire history will have changed. Also it appears that newstring has to be non-empty; I tried "" but it doesn't work in filter-branch because rpl wants to query the user to make sure it's OK to replace with an empty string.

Tags:

May. 3rd, 2009

05:19 pm - GSLL using FSBV

The GNU Scientific Library for Lisp (GSLL) now makes use of Foreign Structures By Value (FSBV) to define functions that pass or return complex scalars, which are represented as structures in GSL. The tests in gsll-tests will test some of these functions, and they all pass.

Since my last posting, there have been some improvements to FSBV; among them, the copying between Lisp and foreign code happens automatically in the definition with either fsbv:defcfun or fsbv:foreign-funcall. Since there is some preparation involving structures needed for libffi, and this need be done only once per function when it is defined, fsbv:defcfun makes a closure separately when it is loaded and then the invocation of the function just calls that closure on the specific arguments. If fsbv:foreign-funcall is called with the symbol used for the Lisp name of the fsbv:defcfun, it will also use that closure. GSLL takes advantage of that efficiency. Since I think FSBV won't change too much now (at least in the interface) I have tagged it with "0.1" release.

For those GSLL users who have no need for complex functions and don't want to bother installing libffi and FSBV, GSLL will compile and load fine without them; of course, those functions won't be available.

Tags:

Navigate: (Previous 10 Entries)