-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Verify that CL_MEM_USE_HOST_PTR works for pointers to Java arrays #7
Comments
As far as I remember, pointers to Java arrays will work from native code, but it is not guaranteed. It all depends on the JVM implementation and the memory resources available at each particular moment. That also makes it difficult for testing, because it will work - most of the time. But, that can fail anytime, since JVM is free to rearrange Java arrays in memory as it pleases. There are methods to tell it not to do it, but that makes JVM less effective at its job. All in all, the safest bet is to use DirectByteBuffer. |
They will not work directly. You can obtain a pointer to a Java array in JNI, using
But as the name suggests, this imposes a "critical section". Particularly, it is not possible to do Now, when calling And the crucial point is: Nobody knows what exactly the OpenCL implementation is doing with this pointer when Usually, I try to support Java arrays whereever possible, because always doing everything with direct buffers is cumbersome. But I'm afraid that
I'm not sure when I'll find the time to investigate this in more detail, but wanted to summarize it here, so that people know that there likely is an issue with this usage pattern, and ... that it does not get lost somewhere at the bottom of my TODO list.... |
Doesn't this also apply to other non blocking memory operations to arrays? So e.g.
should be considered unsafe? |
@TPolzer In this case, JOCL will internally keep a reference to the input data until the write operation finished. So the input data will not be garbage collected in the meantime. (I'm not sure whether a similar concept could be applied to the But you indirectly raised a point that is related (or structurally similar) to this issue: Although the data may not be garbage collected, it might still be relocated by the JVM when the "critical section" that is opened with (So this issue just bubbled up to the top of my TODO list) |
It is likely almost never a real concern, because humongous objects are only compacted in full gc cycles, which should rarely occur. |
Again, I have never encountered any issues here. But this may have different reasons:
But, in doubt, even if I cannot provoke an error, and/or even if it only causes problems in full GCs and these happen "rarely", this case has to be covered. The worst thing that could happen is that it "rarely" causes a problem in a production system. Such a "problem" would probably be a JVM crash, and I consider this as totally unacceptable - even if it happens "rarely". It may never crash
|
^ This comment was by me. I was logged in with the wrong account. |
I did some general tests, particularly regarding the secondary issue. From the first test, it indeed seems that a relocation of arrays occurs only during a full GC, and the likeliness of a real relocation decreases with a larger array size. All this is to be taken with a grain of salt, as it is mainly based on some But undoubtedly, an array may be relocated by the GC, at "arbitrary" points in time. However, it's really hard to provoke the error that suspectedly may happen during a non-blocking Nevertheless, right now, I assume that I'll have to drop the support of non-blocking operations on Java arrays, because it is impossible to guarantee that this will work. This will likely be a change in JOCL 2.1 (which is pending anyhow). Unfortunately, this will affect backward compatibility, I'll still have to figure out how exactly this change will be implemented. All this refers to the secondary issue. The primary one, namely the use of |
@gpu In your place, I won't be that concerned with backward compatibility, since it seems that all users are early adopters and our primary goal is to get the best possible technical solution. Please just provide clear and precise documentation (and warnings :) about changes and best ways to use the library, and don't worry. Thank you for the great effort you put into this! Also, if you could release the tools you use for creating this as open source, that would be great, since we could try to help you more :) |
@blueberry Some off-topic (I'm not sure how much I should elaborate this here) : Eight years ago I started this "code generation" with some crude hacks, to generate the code of JCublas. Later, I generalized this into a "proper" code generator, but went waaay to far with that (it became an "abstraction hell"). Later, I concretized it a bit, and tried to bring it into a shape that could actually be published, but it's still far from perfect. It is mainly used for parts of the libraries like JCublas or JOCLBlast, but there are still manual steps involved to integrate the generated code (and it's hard to convey the necessary assembly steps for someone who is not already familiar with it). So I think that publishing it now would be of very limited use for externals. (I would, however, provide it for anyhone who is interested in it (via mail). One should not expect it to be "easy to use" or run "out of the box", but ... maybe some feedback would help to bring it into a really publishable shape). But more importantly, and leading to this issue: The code of JOCL itself is not generated with this code generator. One of the reasons is exactly related to this issue: There are many subtleties that might get lost when "blindly" doing a header-to-JNI-code-generation. Now, specifically regarding this issue: I consider backward compatibility as an important point. If I change the behavior here, this would mean that someone who used
will receive an exception in the next version -
And this could annoy people. I'm aware of this, and if this is changed, I will prominently point it out in the release notes. However, I think that this change is still reasonable, and the reasons are:
|
Non-blocking read- and write operations that involve Java arrays are no longer supported, to avoid possibly undeterministic behavior that may be caused by Java arrays being moved by the memory manager. This is related to #7 , but does not yet cover the original question about the usage of the CL_MEM_USE_HOST_PTR flag for pointers to Java arrays.
According to the OpenCL specification for the
clEnqueueMapBuffer
function:Although JOCL supports pointers to Java arrays (as far as reasonably possible), it is not clear whether this particular application pattern works as expected.
The goal of this issue is to
CL_MEM_USE_HOST_PTR
is used with a pointer to a Java array)The text was updated successfully, but these errors were encountered: