使用JNI将数据从Java复制到C ++对象数组 [英] Copying Data from Java to C++ Objects Array Using JNI

查看:167
本文介绍了使用JNI将数据从Java复制到C ++对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要大量计算的Java代码,我想使用JNI将其转发到C ++.
我主要关心的是将所有数据序列化到内存中,然后将计算转发到GPU.
由于数据是用Java接收的,但是主要的计算是使用C ++完成的,所以我想到了将所有数据连续排列在原始数组(ByteBufferUnsafe中的原始字节)中,结构与C ++对象相同.
例如,假设我有一个xy的点.在C ++中,对象的大小为24个字节. (我猜)VTable 8个字节,x 8个字节,y 8个字节.
因此,在Java中,我将所有数据安排在相同的结构中,并使用JNI将缓冲区传递给C ++,然后在C ++中将其转换为点数组.

I have Java code that requires heavy calculations which I would like to forward into C++ using JNI.
My main concern is having all the data serialized in memory, and next to forward the computation to GPU.
Since the data is received in Java, but the main calculations are done using C++, I thought of arranging all the data continuously in a raw array (ByteBuffer or raw bytes from Unsafe), in the same structure as the C++ object.
For example, suppose I have a point with x and y. In C++ the object has a size of 24 bytes. 8 bytes for (I guess) VTable, 8 bytes for x and 8 bytes for y.
So in Java, I would arrange all the data in the same structure and pass the buffer to C++ using JNI, and in C++ cast it to an array of points.

这很好,我允许自己假设我将始终使用相同的C ++编译器,相同的JDK,相同的操作系统和相同的硬件(至少用于测试该解决方案的可行性).

This worked fine, and I am allowing myself to assume that I will always use the same C++ compiler, same JDK, same OS and same HW (at least for testing the feasibility of the solution).

我的问题是这些假设是否正确,或者有更好的方法在Java和C ++之间传递序列化数据(我必须使用JNI而不是某种IPC)?

My question is if these assumptions are correct, or there is a better way to pass serialized data between Java and C++ (I must use JNI and not some kind of IPC)?

推荐答案

如果我可以依靠C ++结构(偏移量,字段对齐等)

if I can rely on the C++ structure (offset, alignment of the fields, etc.)

否,除非您知道特定平台上的编译器在这种情况下将执行的操作.会产生不确定的行为.

No, unless you know what your compiler on your specific platform will do in that case. It yields undefined behavior.

在惯用的C语言中,无法将ByteBuffer(又名char *)的内容与Point *别名化以便以后访问其成员.请查看C标准N1570 6.5 (p7):

Aliasing the content of ByteBuffer (aka char *) with Point * to later access its members is not possible in idiomatic C. Take a look at the C Standard N1570 6.5 (p7):

一个对象只能由一个对象访问其存储值 具有以下之一的左值表达式 以下类型:88)

An object shall have its stored value accessed only by an lvalue expression that has one of the following types:88)

-与对象的有效类型兼容的类型,

— a type compatible with the effective type of the object,

假设您知道GetDirectBufferAddress返回的void *本身是通过调用malloc(size)或朋友(实际上使用malloc)返回的,其中size_t size = sizeof(struct Point)可以将其强制转换为Point * 本地代码中的成员,以后再使用.那将是一种顺应方式(之一).

Assuming you know that void * returned by GetDirectBufferAddress is itself returned by a call to malloc(size) or friends (it actually uses malloc) where size_t size = sizeof(struct Point) than you can cast it to Point * initialize its member from native code and later use it. That would be a conforming way (one of).

这篇关于使用JNI将数据从Java复制到C ++对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆