Using the Vector API on (externally allocated) foreign memory

Antoine Chambille ach at activeviam.com
Tue Mar 10 11:31:11 UTC 2020


Thanks for your insights Paul. I will write a JMH benchmark combining
double arrays, byte buffers and byte arrays, and run it with and without
bounds checking. I'll open another thread when I have the results.

Cheers,
-Antoine



On Mon, Mar 9, 2020 at 6:04 PM Paul Sandoz <paul.sandoz at oracle.com> wrote:

> Hi,
>
> Do you have a JMH benchmark to share?  That’s helpful so we know we are
> talking more precisely about the same thing, further it means we can look
> at the hotspots of the generated machine code and identify C2 issues.
>
> IIRC currently the performance focus has been mostly on fromArray/toArray
> using corresponding primitive array so I could imagine there are some perf
> issues to be resolved.  However both primitive array access and buffer
> access use the same load intrinsic.
>
> Some things to explore (if you have not done so already):
>
> - ensure the byte order is native order to avoid byte swapping.
>
> - compare with fromByteArray
>
> - switch off bounds checking using the system property
> “jdk.incubator.vector.VECTOR_ACCESS_OOB_CHECK”:
>
> static final int VECTOR_ACCESS_OOB_CHECK =
> Integer.getInteger("jdk.incubator.vector.VECTOR_ACCESS_OOB_CHECK", 2);
>
>>
> @ForceInline
> static int checkFromIndexSize(int ix, int vlen, int length) {
>     switch (VectorIntrinsics.VECTOR_ACCESS_OOB_CHECK) {
>         case 0: return ix; // no range check
>         case 1: return Objects.checkFromIndexSize(ix, vlen, length);
>         case 2: return Objects.checkIndex(ix, length - (vlen - 1));
>         default: throw new InternalError();
>     }
> }
>
> Hth,
> Paul.
>
> > On Mar 9, 2020, at 4:02 AM, Antoine Chambille <ach at activeviam.com>
> wrote:
> >
> > Hi John, thank you for the suggestion, wrapping native memory with a
> > ByteBuffer does work (com.sun.jna.Pointer.getByteBuffer(long address)).
> >
> > However after running a few benchmarks it looks like the code loading
> > vectors from byte buffers is about twice slower than the same code
> loading
> > vectors from java arrays. Is this expected?
> >
> > JDK freshly checked out and built for Windows 10 (
> > http://hg.openjdk.java.net/panama/dev -b vectorIntrinsics ). double[]
> data,
> > AVX2, Intel.
> >
> >
> > Best,
> > -Antoine
> >
> >
> >
> > On Fri, Mar 6, 2020 at 8:43 PM John Rose <john.r.rose at oracle.com> wrote:
> >
> >> On Mar 6, 2020, at 9:59 AM, Antoine Chambille <ach at activeviam.com>
> wrote:
> >>
> >>
> >> Is support for vector operations on (externally allocated) foreign
> memory
> >> being considered? Have we missed something?
> >>
> >>
> >> The current story is that you have to wrap a byte buffer around
> >> your native memory, using JNI’s NewDirectByteBuffer.  Please
> >> let us know if that doesn’t work for you.
> >>
> >> It is likely that vectors will interoperate with a more modern API
> >> for both on-heap and off-heap access, after it settles.  These “memory
> >> segments” are incubating in JDK 14:
> >>
> >>
> >>
> https://download.java.net/java/GA/jdk14/docs/api/jdk.incubator.foreign/jdk/incubator/foreign/MemorySegment.html
> >>
> >> — John
> >>
> >
> >
> > --
> >  [image: ActiveViam] <https://www.activeviam.com> [image: LinkedIn]
> > <https://www.linkedin.com/company/activeviam>
> >
> > Antoine Chambille
> > *Global Head of Research & Development *
> >
> > [image: Office] +33 (0)1 40 13 91 00
> > [image: YouTube] <https://www.youtube.com/user/QuartetFS/videos>
> > [image: Blog] <https://www.activeviam.com/blog/>
> > [image: Twitter] <https://twitter.com/active_viam>
> > [image: location]
> > <https://maps.google.com/?q=46+rue+de+l+Arbre+Sec,+75001+Paris,+France>
> 46
> > rue de l'Arbre Sec, 75001 Paris [image: url]
> > <https://www.activeviam.com>  visit
> > our website
>
>


More information about the panama-dev mailing list