viennacl / viennacl-dev

Developer repository for ViennaCL. Visit http://viennacl.sourceforge.net/ for the latest releases.
Other
281 stars 89 forks source link

OpenCL and CUDA concurrent? #267

Open cdeterman opened 5 years ago

cdeterman commented 5 years ago

@karlrupp replied to the issue here that it was possible to use both OpenCL and CUDA backends at runtime. Is there any example of this? I'm a little confused at the moment as the CUDA backend requires the nvcc compiler. Is there a repetitive compilation that runs through the files once with nvcc and then another time with g++? I would assume that flags for both OpenCL and CUDA would need to be defined

#define VIENNACL_WITH_OPENCL 1
#define VIENNACL_WITH_CUDA 1

Any guidance on how to accomplish using both backends would be valuable.

karlrupp commented 5 years ago

Yes, you need to have both define statements. However, you only need to compile that with nvcc. That provides an executable that can do CUDA, OpenCL, and the CPU fallback.

As for the usage: The simplest is to create three viennacl::context objects - one for CUDA, one for OpenCL, one for CPU. Then, you can switch between them via switch_memory_context, e.g.

viennacl::switch_memory_context(vec, host_context);

See also here: http://viennacl.sourceforge.net/doc/classviennacl_1_1context.html#a88b51d155aee396ef6d8d8a96bd7e90a

This way you can shift data between CUDA, OpenCL, and CPU. Keep in mind, though, that you have to make sure that all objects involved in a computation are on the same memory domain. That is, if you try to add a vector with CUDA data to a vector with OpenCL data, an error will be thrown.

Early next week I will add an example of doing this to the repository.

cdeterman commented 5 years ago

@karlrupp did you ever have a chance to add the mentioned example?