[foreign] Pointer to fixed-size array: is jexctract's mapping to Java correct?

Lev Serebryakov lev at serebryakov.spb.ru
Thu Feb 28 13:59:16 UTC 2019


On 28.02.2019 16:50, Maurizio Cimadamore wrote:

> On 28/02/2019 13:16, Lev Serebryakov wrote:
>> Yep. And copy data from "java native" array and back. It will dominate
>> execution time for small transform sizes, I'm afraid — copying from
>> native Java array to these pairs. Performance-wise, best solution will
>> be to have `Array<Double>` transparently backed by `double[]`, it will
>> mimic C code as close as possibly (and as fast as possibly).
> 
> So, if efficiency is your main concern, I'd suggest to:
> 
> 1) allocate a double array, as this:
> 
> Array<Double> buffer  = Scope.allocateArray(NativeTypes.DOUBLE, npairs *
> sizeofDouble * 2);
> 
> 2) Fill in the buffer with a for loop - no allocation is required - just
> set elements at position 'n'
> 
> 3) When you are done, do an (unsafe!) cast to what you want:
> 
> Array<Array<Double>> arr =
> buffer.elementPointer().cast(NativeTypes.VOID).cast(NativeTypes.DOUBLE.array(2)).withSize(npairs);
> 
> 
> This should give you what you want. No allocation, just fill in the
> buffer, convert to the type the API expects, pass it onto the API.
 Thank you! I'll try it and compare three way to calculate the same: (1)
super-fast native fftw via Panama/Foreign with this recipe, (2) same
fftw via JNA (3) Good pure-java implementation of same algorithm
(JTransforms library) and return with results!

> I'm also thinking about ways to let the API see that this cast is not as
> 'unsafe' as it looks (as the underlying layout is the same) - more on
> that in a separate email.


-- 
// Black Lion AKA Lev Serebryakov



More information about the panama-dev mailing list