在CUDA中实现通用格式的流输出的最繁琐的方法? [英] Least onerous way to implement generic formatted stream output in CUDA?

查看:67
本文介绍了在CUDA中实现通用格式的流输出的最繁琐的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够写一些接近的东西:

I want to be able to write something close to:

std::cout << "Hello" << my_world_string << ", " << std::setprecision(5) << my_double << '\n';

在CUDA设备端代码中,

用于调试模板函数-并使这种代码行产生单个不间断的输出行(即,等效于单个CUDA printf()调用-通常不会)不会被其他线程的输出所破坏).

in CUDA device-side code, for debugging templated functions - and for this kind of line of code to result in a single, unbroken, output line (i.e. the equivalent of a single CUDA printf() call - which typically doesn't get mangled with output from other threads).

当然,这是不可能的,因为在设备端代码中没有文件或文件描述符,在设备端代码中也没有任何std::ostream代码可用.从本质上讲,我们必须使用的是启用printf()的CUDA硬件+软件黑客.但是显然有可能得到类似的东西:

Of course, that's not possible since there are no files or file descriptors in device-side code, nor is any of the std::ostream code usable in device-side code. Essentially what we have to work with is CUDA's hardware+software hack enabling printf()s. But it is obviously possible to get something like:

stream  << "Hello" << my_world_string << ", " << foo::setprecision(5) << my_double << '\n'; 
stream.flush();

或:

stream  << "Hello" << my_world_string << ", " << foo::setprecision(5) << my_double << '\n'; 
printf("%s", stream.str());

我的问题是:我应该实现什么才能使我编写尽可能接近上述代码的代码,从而最大程度地减少工作量/编写代码量?

My question is: What should I implement which would allow me to write code as close to the above as possible, minimizing effort / amount of code to write?

注意:

  • 我使用了标识符stream,但它不必是流.代码也不需要看起来像我布置的那样.关键是我可以在模板化的设备功能中使用打印代码.
  • 所有代码都将用C ++ 11编写.
  • 代码可以假定使用C ++ 11或更高版本的标准执行编译.
  • 我可以使用现有的FOSS代码,但前提是其许可证是许可的,例如3-BSD,CC-BY-SA,麻省理工学院-但不是GPL.
  • I used the identifier stream but it doesn't have to be a stream. Nor does the code need to look just like I laid it out. The point is for me to be able to have printing code in a templated device function.
  • All code will be written in C++11.
  • Code may assume compilation is performed either with C++11 or a later version of the standard.
  • I can use existing FOSS code, but only if its license is permissive, e.g. 3-BSD, CC-BY-SA, MIT - but not GPL.

推荐答案

目前,我正在考虑的实现方式是:

Currently, the way I'm thinking of implementing this is:

  • 实现一个类似std::ostringstream的类,该类可以从其他地方(在构造中)获取其初始存储.
  • 有了这样的对象,您就可以printf("%s\n", my_gpu_sstream.str()).
  • 允许使用固定大小的缓冲区构造GPU-ostringstream.
  • 允许GPU-ostringstream使用CUDA的设备端malloc()分配可变大小的缓冲区.
  • Implement an std::ostringstream-like class which can take its initial storage from elsewhere (on construction).
  • With such an object, you can then printf("%s\n", my_gpu_sstream.str()) .
  • Allow the GPU-ostringstream to be constructed with a fixed-sized buffer.
  • Allow the GPU-ostringstream to allocate variable-size buffers using CUDA's device-side malloc().

而鲍勃是你的叔叔.

但是,我真的很想避免自己实现一个完整的字符串流.似乎有很多多余的工作和代码.

However, I would really rather avoid implementing a full-blown stringstream myself. Seems like a whole lot of redundant work and code.

编辑:完成!现在,我的 cuda-kat库中有一个有效的实现.我用过robhz786的 strf,即)字符串格式化库,而不是基于标准流的.在此基础上,我实现了设备上的字符串流, kat::stringstream ,并在此基础上添加.

Done! I now havea working implementation in my cuda-kat library. I've used robhz786's strf library, which is (header-only-if-you-like) string formatting library not based on standard streams. On its basis I've implemented an on-device stringstream, kat::stringstream, and on the basis of that, a "printf'ing ostream" class.

它远非完美:strf不使用标准的库操纵器,并且具有用于填充,设置精度等的专有用法.此外,编译时间也很长.但这是相当有用的.甚至可以选择在每个打印行之前加上前缀(例如,块和线程索引),前提是您对其进行了配置.输出使用CUDA的内在printf()机制-到达行尾时.

It's far from perfect: strf doesn't use standard library manipulators and has it's own idioms for filling, setting precision etc. Also, compilation time is quite high. But it is quite usable. Even has the option to prepend each printed line with a prefix (e.g. the block & thread indices) if you configure it to do so. Output uses CUDA's intrinsic printf() mechanism - when reaching the end of a line.

这篇关于在CUDA中实现通用格式的流输出的最繁琐的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆