Hi Marco,
I work in Clojure, so I am not providing a Java code here, but the behavior is simple to describe, an I am not sure whether it is intentional (but it looks very strange). I am using JOCL 1.9.0 ON AMD platform (if that might be important).
Summary: the reference count for the context object keeps increasing when I call functions that use context, such as clCreateCommandQueue, and even the functions that do not take context as an argument, but use it indirectly, such as clEnqueueNDRangeKernel or clEnqueueReadBuffer
For, example, if i keep calling clEnqueueRead the context’s reference count just keeps increasing.
The increasing is not in the info function since if i call it repeatedly the reference count stays the same.
I am not calling clRetainContext anywhere and am simply delegating clojure calls to CL/clXXXXX methods, so I am pretty sure that the repeated increasing of the reference count is somewhere in JOCL code.
Is this behavior intended or incidental?
This is the clojure code, I hope it helps at least as pseudo-code:
Here is how I call JOCL api, nothing special here:
(defn command-queue* [context device ^long properties]
(let [err (int-array 1)
res (CL/clCreateCommandQueue context device properties err)]
(with-check-arr err res)))
And here is how I call it
(def program-source
(slurp "test/opencl/examples/openclinaction/ch04/hello-kernel.cl"))
(def p (first (platforms)))
(def dev (first (devices p)))
(def ch (chan))
(def ctx (context [dev]))
(def cqueue (command-queue ctx dev 0))
(def host-msg (direct-buffer 16))
(def cl-msg (cl-buffer ctx 16 :write-only))
(def prog (build-program! (program-with-source ctx [program-source])))
(def hello-kernel (kernel prog "hello_kernel"))
(def global-work-size (long-array [1]))
(def local-work-size (long-array [1]))
(set-args! hello-kernel cl-msg)
(enqueue-nd-range cqueue hello-kernel global-work-size local-work-size)
(follow ch (enqueue-read cqueue cl-msg host-msg) host-msg)
(apply str (map char (wrap-byte-seq int8 (byte-seq (:data (<!! ch))))))
At the end of this code, before clRelease calls, the reference count of ctx would be 6, and I expected it to be 1, just like most of other stuff. Program reference count also unexpectedly increases to 2…
*** Edit ***
I found the answer to my question, and here it is, if anybody bumps to the same issue:
- Context’s reference count keeps rising
- But, when i call release on the resources that are inside the context (queues, mem-objects, etc.) it keeps decreasing
So, apparently, JOCL keeps the reference to the context because it needs it implicitly when working on objects dependent on that context, but those objects’ release calls would also appropriately release the context.