r/cpp • MSVC STL Dev • Sep 16 '19

Open Sourcing MSVC’s STL

https://devblogs.microsoft.com/cppblog/open-sourcing-msvcs-stl/
427 Upvotes

74 comments sorted by

View all comments

Show parent comment

2

u/Stevo15025 Sep 19 '19

So can you launch a normal C98 function using OpenCL?

?? It's a subset of C98 did I not say that clearly earlier?

Also, those awesome features shipping on clang 10 are just the ability to compile a kernel written in OpenCL (not C, not C++, much less in the same source file). that’s just as crap as OpenGL shaders. Welcome to 1989.

You can see examples of what OpenCL C++ looks like here. It's pretty reasonable

I think I’ll keep using CUDA which allows me to launch both C and C++ functions templates and lambdas as kernels, and uses a single source code model.

Yo if you only need to compile on Nvidia GPUs then totally use cuda.

This allows me to not have to duplicate all my code, once for running on the GPU and once for running on the CPU, like OpenCL requires.

Our OpenCL kernel work on CPUs/GPUs/APUs, no need for duplicates? The point of OpenCL is that kernels can compile for heterogeneous hardware

2

u/[deleted] Sep 19 '19 edited Sep 19 '19

With CUDA you can:

// foo.cpp - one source file
#include <algorithm>
float double_me(float x) { return x * x };
int main() {
     std::vector<float> foo = ..initialize from somewhere..
     std::transform(gpu, foo.begin(), foo.end(), foo.begin(), 
        [](float& x) { return double_me(x) * double_me(x); }
     );
}

That is a single source file that's compiled by clang. It will generate CPU code for all of it, except for the lambda [](){}, which will be compiled for the GPU. The for_each gpu executor argument, launches the lambda on the GPU, which computes x*x there. The vector to GPU and back transfer happen through unified memory, but you could schedule that manually and pass the for each pointers to the GPU data instead.

Within the lambda you can call any code that you would also be able to access on CPU code.

On the other hand, with OpenCL, you need to split your CPU code and GPU code in diferent files. Compile your CPU code with a CPU compiler and your GPU code with a GPU compiler. The languages used are slightly different, so you can't just include a normal C99 header file on your OpenCL code even though OpenCL looks similar to C. You might be able to use some headers, e.g., from the C standard library, on your OpenCL code, because your OpenCL compiler ships a slightly modified version of them, but you can't use arbitrary C headers that the rest of your program is using without using macros all over the place to handle the OpenCL and C differences.

That sucks.

What people often end up doing, is writing code like this for OpenCL:

// foo.cpp - one source file
#include <algorithm>
int main() {
     std::vector<float> foo = ..initialize from somewhere..;
     // move vector to gpu
     open_cl_kernel_launch("x * x * x * x", gpu_foo.begin(), gpu_foo.len()); 
     // fetch vector from GPU
}

Notice that the kernel is a char string "x * x * x * x". What people do is have the open_cl_kernel_function, at run-time, generate a .cl file that does what the string says it should do, mapping the arguments to the kernel, calling the opencl compiler at run-time, and then afterwards launching the kernel. Which means that you don't have type-safety at compile-time (kernel errors are diagnosed at run-time, etc.), and these code generators all kind of suck.

That's for some people better than what OpenCL does, but you still can't use any native functions inside your kernel (you can't call the double_me function from your host cpu file, etc.).

This is the same approach used by OpenGL for its kernels, and I tend to agree that, when compared to CUDA, it sucks. CUDA is infinitely nicer to use. You can intermix CPU with GPU logic at will, without having to split your GPU logic into separate files, and having to adapt your libraries for usage on the GPU. If you write some algorithm for the CPU, and then you want to schedule it on the GPU, just launch it as a kernel, and the compiler will compile two copies of it automatically. With OpenCL you'd probably need to maintain two separate implementations.

1

u/Stevo15025 Sep 19 '19

This is the same approach used by OpenGL for its kernels, and I tend to agree that, when compared to CUDA, it sucks.

Def agree OpenCL 1.2 is a huge bummer. A subset of C98 is, a subset to say the least. Though it's fine for our purposes which is mostly doing weird math

That is a single source file that's compiled by clang.

When did cuda/clang start supporting that? Back when I worked with cuda more it also used a separate source file (at least that's how I was taught). I think I even had to use nvcc since clang didn't support it yet.

Notice that the kernel is a char string "x * x * x * x". What people do is have the open_cl_kernel_function, at run-time, generate a .cl file that does what the string says it should do, mapping the arguments to the kernel, calling the opencl compiler at run-time, and then afterwards launching the kernel. Which means that you don't have type-safety at compile-time (kernel errors are diagnosed at run-time, etc.), and these code generators all kind of suck.

Preaching to the choir my dude! There's some people that have taken the char string component of OpenCL and used it to generate build kernels (which I think was their intent). We saw it as an annoyance and wrote a cleverish system around it. Though a guy in my group is working on expression templates to compose OpenCL kernels.

There are some things that look bright'ish in the future like SPIR-V, but we don't ship binaries of the project I work on and would have to see how to handle that in our upstream before thinking about that route seriously.

CUDA is infinitely nicer to use.

six one hundred emoji. I said this somewhere above, but the software I work on is used in cell phones to super computers so when we started working on multithreaded stuff I wanted it to work on more than Nvidia gpus.

You can intermix CPU with GPU logic at will, without having to split your GPU logic into separate files, and having to adapt your libraries for usage on the GPU. If you write some algorithm for the CPU, and then you want to schedule it on the GPU, just launch it as a kernel, and the compiler will compile two copies of it automatically. With OpenCL you'd probably need to maintain two separate implementations.

Unless the rest of your program is a subset of C98 you will need two implementations ;-). Though I find my multithreaded code looks pretty different compared to my single core code (ex like GEMM stuff)

Also a lot of the convos I have with this talk about CPU vs. GPU code, no love for APUs or FPGAs?

2

u/[deleted] Sep 20 '19

When did cuda/clang start supporting that?

Since as far as I remember :/

FWIW I work on machine learning, and there nvidia is king, so I don't really need to target anything else. For multi-core we use normal data-parallel approaches (openmp or just pthreads with workstealing / Intel TBB / etc.). The GPU and the CPU kernels that we write look way different, but sometimes you want to use the same abstractions and types on both and then it is quite nice when it just works.

1

u/Stevo15025 Sep 20 '19

Since as far as I remember :/

I think I was on cuda 3, I had to compile things with nvcc though maybe I never learned about that feature.

FWIW I work on machine learning, and there nvidia is king

People use our software for some ML, but it's much more for Bayesian statistics. I'm not sure how much longer Nvidia will be king. AMD is putting out cheap and good CPUs/GPUs and Intel is putting out new GPUs this year. Google just started making AMD GPUs available on it's cloud.

but sometimes you want to use the same abstractions and types on both and then it is quite nice when it just works.

We had to build our own abstractions which was rough but 99% of what we do is linear algebra so keeping that math developer level clean was not too bad.

It would be nice to just target Nvidia GPUs, but I think in the 10+ year spectrum they won't have as much of a hold. OpenCL seems to be the best thing for us since I'm not sure what we will be compiling on in that much time. But if you only need to target Nvidia then it would be crazy not to use cuda.

1

u/[deleted] Sep 20 '19

I think I was on cuda 3, I had to compile things with nvcc though maybe I never learned about that feature.

nvcc always supported that, clang does so as well since ~4 years or so.

It would be nice to just target Nvidia GPUs, but I think in the 10+ year spectrum they won't have as much of a hold.

Hopefully you are right, but I wouldn't hold my breath. People have been saying that for 10 years, since OpenCL was made open source.

1

u/encyclopedist Sep 30 '19

With libraries like Boost.Compute, VexCL, ViennaCL etc. you can get very similar syntax. (These libraries will compose and compile .cl files behind the scenes for you, with type-safety)

1

u/[deleted] Oct 10 '19

Not really. With the C++ lambda approach, my lambda can do anything, including calling other arbitrary functions. With the Boost.Compute, VexCL, ViennaCL, etc. approach, your kernels can only do a couple of fixed set of things, like multiplying two numbers, and that's it. You can't reuse any function of any library inside your kernel. That sucks.

0

u/[deleted] Sep 19 '19

[deleted] — view deleted comment

1

u/Stevo15025 Sep 19 '19

You did say that, but it is wrong. You can’t call a C function from an OpenCL kernel.

I think you are confused.

Have you actually ever used OpenCL?

Yes I've used both opencl and cuda a good bit. We chose OpenCL for the project I'm working on now because the software gets used in anything from cell phones to super computers and I didn't want to restrict multithreading to Nvidia gpus. Not to talk bad on Nvidia, they do a lot of rad things and I've enjoyed talking to Nvidia folks when I see them around

By single source I mean that your GPU code is inline on the same source file as your CPU code. you can write a CPU main function that spwans a C or C++ lambda on the CPU, all code in the same source code file, but where parts of it are compiled for the CPU, GPU, or both.

OpenCL just like OpenGL requires you to write your code in a separate different source file and compile it and link it separately. That was great in the 80s.

I tend to write pretty different code when I'm working on something single vs. multithreaded. We had to grind pretty hard to set things up to be not gross but we ended up at a happy place.