5 ms·
CUDA is a language that specifically targets NVIDIA GPUs. OpenCL is a general-purpose framework for heterogenous compute tasks. You could be running against a
by paulmd 11y ago
CUDA is a language that specifically targets NVIDIA GPUs. OpenCL is a general-purpose framework for heterogenous compute tasks. You could be running against a FPGA, a DSP, or even a manycore CPU like Knight's Landing. It's definitely tied less tightly to the hardware, and that's really my main objection to it too. But from AMD's perspective it's an abstract framework, and they are providing stub functions for others to implement.
What you are looking at is largely just code to compile a kernel and launch it on the device, basically a makefile in C. You can really just ignore most of the boilerplate until you need to modify it.
"Hello World" is not a particularly meaningful program on a device with no console or LEDs to blink - why don't you try something simple like summing every number from 1 to N? Or, a Monte-Carlo simulator to calculate Pi? Easy to code, easy to verify...
CUDA is also quite verbose if you are playing by all the rules. You are still supposed to specify your target device, verify your capabilities, free your buffers, check your async return values, etc - and if you don't then you will get weird behavior down the road until you figure out what's going on. OpenCL might try to helpfully ignore misbehavior too, I don't know for sure.
Personally I find both CUDA and OpenCL to be quite verbose and when I write for them I use a library like Thrust or Bolt, which takes most of the boilerplate out of it and feel much like writing C++ STL code. It also allows you to work efficiently at a much higher level, and then come through after-the-fact and optimize the parts that are actually slowing you down. It also provides stuff like automatic occupancy tuning and so on. You can trivially switch between Thrust and native kernels by using raw_pointer, so it's great for sorts, scans, etc that "glue" things together. Use it like you would Python, bearing in mind that round trips to host memory are slow (but perhaps not fatally so!)
http://docs.nvidia.com/cuda/thrust/ http://docs.nvidia.com/cuda/thrust/
http://developer.amd.com/tools-and-sdks/opencl-zone/bolt-c-template-library/ http://developer.amd.com/tools-and-sdks/opencl-zone/bolt-c-t...
One useful trick is that you can write your CUDA functions inside a Thrust functor which iterates your data elements. So you can use a count_iterator which represents the index of the data element(s) or grid processing element in question, and then you write a functor which uses the counter value to load a data element from a pointer stored inside the functor, and does some work on it. This gives you a "kernel simulator" as an intermediate step between array-wide functional programming and native __device__ functions.
This also provides a great place for indirection so you can easily swap between a GPU backend and Thrust's OpenMP CPU backend.
- joe_the_user 11y ago"Hello World" is not a particularly meaningful program on a device with no console or LEDs to blink - why don't you try something simple like summing every number from 1 to N? Easy to code, easy to verify... I'm not trying to run "hello world", I'm looking at the easily accessible OpenCl code and seeing "hello world" as the most common example program.
- jcranmer 11y agoThat's a problem of the training materials, not of the language itself.
- sklogic 11y agoOpenCL is very low level (sort of like Vulkan, which was actually inspired by it). Start with some easy wrappers, like PyOpenCL.
- joe_the_user 11y agoExcept I'm a c/c++ programmer. Any "easy wrappers" for c?
- sklogic 11y agoThere is an official Khronos C++ wrapper API that is significantly more dense than the underlying C API.
- joe_the_user 11y agoI was thinking of easy and usable rather than dense. I think Khronos wrapper is just a translation of the original API into c++, which partakes of the basic problem - a zillion options for a zillion distinct sorts of hardware, in contrast to Cuda, with a main functional and supported approach.
- sklogic 11y ago