Skip to content

Support running multiple instances of the same kernel simultaneously #9

@kenba

Description

@kenba

@vmx has created a couple of PRs to add Clone traits: #4 and #7 and another PR (#6) to add Send and Sync traits.

I'm not happy with these PR's, I don't think that they take the correct approach.

Cloning

The first issue is what should Clone` do?

@vmx has implemented Clone using the appropriate OpenCL clRetain* functions.

According to the OpenCL spec, the clRetain* functions, increment an object's reference count, i.e. they perform a shallow copy. This use of Clone is similar to using shared pointers in C++ and nowhere near as powerful as Rust references.

Most opencl3 objects are immutable after they have been constructed. Normally, only the Drop trait is mutable to implement RAII by calling the relevant clRelease* function. Therefore, the opencl3 objects can (and should) be accessed by immutable Rust references wherever possible. The exception is Context, where: sub-devices, command queues, and programs are added to a Context in the Initialisation phase:

OpenCL Application Lifecycle
Figure 1 OpenCL Application Lifecycle

However, after Initialisation., Contexts can (and should) also be accessed by immutable Rust references.

Multi-threading

The Query and Initialisation phases of an application should be performed before threads are started. Once the relevant context(s) have been created they can be shared with the threads using immutable Rust references, or new command queues and kernels can be created and moved to the new threads.

Each thread should operate independently; i.e. each should have it's own: command queues, memory buffers and especially kernels.

If an OpenCL application wishes to run multiple instances of a Kernel simultaneously on different threads, then it should use different instances of the Kernel on the threads, not references to the same Kernel instance, see: OpenCL multiple host threads.

Therefore, there is definitely a need to provide multiple deep copies of a Kernel to support multi-threading.

I cannot see a need to Send or Sync OpenCL objects with the possible exception of Events.
Events can be used to synchronise OpenCL execution on multiple command queues in the same context. However, it should be possible to use immutable references or pass them from a thread after it has finished execution.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions