[Gsll-devel] inverse of cl-array
Liam Healy
lhealy at common-lisp.net
Thu Feb 12 04:14:38 UTC 2009
I've had further thoughts (and coding) on this topic. Since the
:cl-array initarg and #'cl-array function are potentially unsafe, I've
extended the function #'copy to copy contents between CL arrays and
marrays. I recommend its routine use in place of the cl-arrays,
because it is always safe. Here is the description I put in the
commit log:
The function #'copy will now copy contents between marrays and CL
arrays. The destination array can be supplied literally, or created
by this function by specifying the desired type: when copying from a
marray to a CL array, specify 'array; when copying from a CL array to
a marray, specify the element type of the marray. For example,
(COPY
(COPY
(MAKE-MARRAY '(COMPLEX DOUBLE-FLOAT) :INITIAL-CONTENTS
'((-34.5 8.24 3.29 -8.93 34.12 -6.15)
(-8.93 34.12 -6.15 49.27 -13.49 32.5)
(49.27 -13.49 32.5 42.73 -17.24 43.31)))
'ARRAY)
'(COMPLEX DOUBLE-FLOAT))
copies from the matrix-complex-double-float made in the make-marray
form to a CL array, then copies the contents to a new
matrix-complex-double-float.
This has been pushed to the repository.
Liam
On Tue, Feb 10, 2009 at 11:18 PM, Liam Healy <lhealy at common-lisp.net> wrote:
> OK the latest commit in master now has the following feature:
>
> #'make-marray will optionally take a :cl-array argument. This should
> be a CL array generated by #'make-ffa, with the same element type as
> specified in the first argument to make-marray. The symbol 'make-ffa
> is exported for users.
>
> Be cautious here. There are things you can do that could mess things
> up - changing the contents of the CL array from outside the marray
> object, sending the same CL array to two different marrays, etc.
>
> Liam
>
>
>
> On Tue, Feb 10, 2009 at 10:12 PM, Liam Healy <lhealy at common-lisp.net> wrote:
>> There isn't an inverse to cl-array at present.
>>
>> Some thoughts about this, without having yet dived into the code: my
>> first recommendation is to just make-marray for everything by default.
>> When you need a regular CL array, use cl-array to extract it, and
>> then if you need to, copy it (see below). The problem with supplying
>> an array directly to be made into a marray is that it may not be
>> "foreign friendly". This won't make any difference on most
>> implementations, but on SBCL, you will get unpredictable results
>> because SBCL will avoid copying the array over to the C side and just
>> send a pointer to the GSL functions. I could perhaps add the ability
>> to supply a foreign-friendly array, as made by make-ffa, as an
>> :initial-contents for make-marray.
>>
>> If you are willing to always have the array copied, that could be done
>> pretty easily too. As a side point, please note that changing an
>> element of an array extracted with cl-array will change the marray in
>> SBCL and have unpredictable results on other implementations. This is
>> why I recommend you copy the array after you extract it.
>>
>> Liam
>>
>> On Tue, Feb 10, 2009 at 8:49 AM, Mirko Vukovic <mirko.vukovic at gmail.com> wrote:
>>>
>>>
>>> On Tue, Feb 10, 2009 at 5:09 AM, Niitsuma Hirotaka
>>> <hirotaka.niitsuma at gmail.com> wrote:
>>>>
>>>> I can not find convert function
>>>> #A(...) -> #m(...)
>>>>
>>>> ( cl-array is conversion #m -> #a )
>>>>
>>>> Thus I write the following ,but that looks slow.
>>>> --------------
>>>> (defun coerce-array-to-list (in-array)
>>>> (map 'list
>>>> #'identity
>>>> (make-array (array-total-size in-array)
>>>> :element-type (array-element-type in-array)
>>>> :displaced-to in-array)))
>>>> ;borrow from
>>>> http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/a925ca60d88d4047/cc06e579dfe840a7?lnk=gst&q=displaced+array
>>>>
>>>>
>>>> (defun appropriate-name (element-type ar)
>>>> (make-marray element-type :dimensions (array-dimensions ar)
>>>> :initial-contents (coerce-array-to-list ar))
>>>> )
>>>> ---------------
>>>>
>>>> usage:
>>>> ( appropriate-name 'double-float #2A((1.0d0 2.0d0) (2.0d0 5.0d0)))
>>>> ->
>>>> #m((1.0d0 2.0d0) (2.0d0 5.0d0)))
>>>>
>>>> _______________________________________________
>>>> Gsll-devel mailing list
>>>> Gsll-devel at common-lisp.net
>>>> http://common-lisp.net/cgi-bin/mailman/listinfo/gsll-devel
>>>
>>> I don't know an answer better than yours. I would name the function
>>> gsll-array.
>>> Also, since marray's are objects, you may want to look into its constructor
>>> to see how the array is stored.
>>>
>>> I was bitten a few times by the make-marray syntax that requires a list
>>> input.
>>>
>>> (As far as I see, lisp's coerce syntax prevents us from customizing it to do
>>> the job we want.)
>>>
>>> Mirko
>>>
>>> _______________________________________________
>>> Gsll-devel mailing list
>>> Gsll-devel at common-lisp.net
>>> http://common-lisp.net/cgi-bin/mailman/listinfo/gsll-devel
>>>
>>>
>>
>
More information about the gsll-devel
mailing list