Description
Hi Marco!
Not sure if this might be appropriate to ask here, but I was more curious of native pointer address concept implementation in JOCL.
Since java might start using a safe api for foreign memory access (currently incubating in jdk 19, and proposed in JEP424), this might open doors to off-heap access of data (outside the jvm) and memory access might be a terabyte size depending on the physical memory available. Of-course native memory can be accessed through unconventional methods, such as the infamous Unsafe API (this API should have been made an official API aeons ago - but good thing the OpenJDK team is making strides to create and implement similar methods from Unsafe), and also through a direct buffer with a possibility of using a hack to get its pointer... public static long addressOfDirectBuffer(ByteBuffer buffer) {return ((DirectBuffer) buffer).address();}
(caution, this is using sun.com code hence doesn't work in every java version), whereby the buffer parameter is a direct buffer. I'm aware that Direct ByteBuffer does use Unsafe, but unfortunately, the offheap data is still read into heap first, and still limited to the size of 2GB or 2^31.
Some great insights here
Based on the knowledge above, I was thinking of the possibility of implementing the "addressOfDirectBuffer", and use the long address and use it as an official pointer address as a way to experiment with native pointers and use of OpenCL USE_HOST_PTR. This is shown below.
package wrapper.core;
import org.jocl.NativePointerObject;
import org.jocl.Pointer;
/**
*
* @author user
*/
public class CPointer extends NativePointerObject{
private final long pointer;
public CPointer(long pointer)
{
super();
this.pointer = pointer;
}
@Override
protected long getNativePointer()
{
return pointer;
}
public Pointer getPointer()
{
return Pointer.to(this);
}
@Override
public int hashCode()
{
int result = 227;
int c = (int)(pointer ^ (pointer >>> 32));
return 37 * result + c;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final CPointer other = (CPointer) obj;
return this.pointer == other.pointer;
}
}
Unfortunately, this approach doesn't work, and the data is corrupted in a way. Would you mind giving insights on how native pointers are implemented in jocl. For example, are the pointers accessed in jocl through JNI similar to maybe the hacks describe above? I know this might seem out of scope of what JOCL tries to provide (safe code), but I was a bit curious. There is a project I'm implementing that might require off-heap data only, due to out of memory heap issues.
You can play around with this project. Uses jdk 1.8 (use windows only - the file dialog explorer is quite custom), but good for debugging my other raytracing codes (like implementing new concepts rapidly).