STL, iostream, new, delete in C/C++ for CUDA

我们两清 提交于 2019-12-07 01:54:09

问题


Can I use STL,iostream,new, delete in C/C++ for CUDA?


回答1:


If you have a Fermi class GPU (so compute capability >=2.0), and are using CUDA 4.0 or later, then both new and delete are avialable for use in device code. STL containers and algorithms and iostream are not supported.

If you want to use "STL like" operations with CUDA, you might be interested in the Thrust template library. It allows host code to transparently interact with the GPU using container types and implements a number of very useful data parallel primitives, like sorting, reduction, and scan. Note that this is still a host side apparatus, Thrust and its containers cannot be used inside your own kernel code.




回答2:


Let's break this down some.

The general case: Can I use C++ standard library construct XYZ on the GPU?

No, you can't use standard library code on the GPU (i.e. in your device-side code). The most direct obstacle is that the standard library is not targeting the CUDA compiler - not indicating its code should be compiled both for host-side and device-side execution. But even if this technical issue was dispensed with someone, there are various reasons why quite a bit of the standard library would not work as-is, or at all, on the GPU.

STL

As talonmies suggests, the Thrust library provides some STL-like functionality, in a useful and nicely packaged way. But it's still mostly a "no" as an answer to your question since:

  1. Its interface is host-side rather than device-side. That is, it will do things for you using the GPU, but those will be under the hood; it's not a toolbox to write your own device-side code.
  2. It covers a small part of the STL: As data structures go, it's essentially just vectors (AFAIK - I haven't scoured the code); streaming on-GPU data using iostreams or a similar abstraction is not supported

iostreams

No, you can't use iostream in CUDA device-side code. We do have C-style printf, however: printf("my_int_value is %05d\n", my_int_value);. This is a very different beast than the standard library printf(), though, since it needs to send data across the PCI bus and have the driver get it to the host-side process' output stream.

See the CUDA Programming Guide's section on formatted output for details.

new and delete

The new and delete operators do work, similarly to on-device malloc() and free() - which is different than on the host-side and somewhat limited; see RobertCrovella's answer on this matter and the links in it.

I would advise, however, that you think very carefully about whether you really need to do on-device memory allocation and de-allocation; it's likely to be costly performance-wise, and often/usually you can do better by pre-allocating memory via a host-side API call.



来源:https://stackoverflow.com/questions/8986686/stl-iostream-new-delete-in-c-c-for-cuda

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!