Memory Segment efficient array handling
leerho
leerho at gmail.com
Thu Apr 1 00:41:58 UTC 2021
Folks,
I am in the process of refactoring our code to use FMA (from JDK16) in our
application. But what I find missing is the ability to do efficient
getting and putting of arrays with MemorySegments.
What I need to do often is to place part of an existing array into a
segment at a specific offset and the reverse; getting an array of elements
from a segment at a specific offset and placing it into an existing array
at a specific offset. I have looked closely at MemoryAccess and the latest
ByteBuffer, but I have not found anything quite as flexible as the
following.
What I have ended up doing is creating an entire class of array methods
like the following:
public class MemoryArrays {
>
> public static void putIntArray(int[] srcArr, long srcIndex, long
> numInts,
> MemorySegment dstSeg, long dstOffsetBytes) {
> MemorySegment srcSeg = MemorySegment.ofArray(srcArr);
> MemorySegment srcSegSlice = srcSeg.asSlice(srcIndex << 2, numInts <<
> 2);
> MemorySegment dstSegSlice = dstSeg.asSlice(dstOffsetBytes, numInts <<
> 2);
> dstSegSlice.copyFrom(srcSegSlice);
> }
>
> /* ...Same as above for all primitive types... */
>
> public static void getIntArray(MemorySegment srcSeg, long
> srcOffsetBytes,
> int[] dstArr, long dstIndex, long numInts) {
> MemorySegment srcSegSlice = srcSeg.asSlice(srcOffsetBytes, numInts <<
> 2);
> MemorySegment dstSeg = MemorySegment.ofArray(dstArr);
> MemorySegment dstSegSlice = dstSeg.asSlice(dstIndex << 2, numInts <<
> 2);
> dstSegSlice.copyFrom(srcSegSlice);
> }
>
> /* ...Same as above for all primitive types... */
> }
>
I would think that if these methods were built-in to FMA either as a
separate class or included in MemoryAccess, it would be so much more
efficient. All of these separate
calls to MemorySegment could be eliminated and the entire method inlined
with a few lines of C++ code.
If Panama is interested I would be happy to contribute such a class.
Lee.
More information about the panama-dev
mailing list