need to convert C++ template to C99 code

两盒软妹~` 提交于 2020-01-13 05:19:05

问题


I am porting CUDA code to OpenCL - CUDA allows C++ constructs like templates while OpenCL is strictly C99. So, what is the most painless way of porting templatest to C? I thought of using function pointers for the template parameters.


回答1:


Before there were templates, there were preprocessor macros.

Search the web for "generic programming in C" for inspiration.




回答2:


Here is the technique I used for conversion of some of CUDA algorithms from Modern GPU code to my GPGPU VexCL library (with OpenCL support).

Each template function in CUDA code is converted to two template functions in OpenCL host code. The first host function ('name' function) returns mangled name of the generated OpenCL function (so that functions with different template parameters have different names); the second host function ('source' function) returns the string representation of the generated OpenCL function source code. These functions are then used for generation of the main kernel code.

Take, for example, the CTAMergeSort CUDA function template. It gets converted to the two overloads of merge_sort function in VexCL code. I call the 'source' function in order to add the function definition to the OpenCL kernel source here and then use the 'name' function to add its call to the kernel here.

Note that the backend::source_generator in VexCL is used in order to generate either OpenCL or CUDA code transparently. In your case the code generation could be much simpler.

To make it all a bit more clear, here is the code that gets generated for the mergesort<256,11,int,float> template instance:

void mergesort_256_11_int_float
(
  int count,
  int tid,
  int * thread_keys0,
  local int * keys_shared0,
  float * thread_vals0,
  local float * vals_shared0
)
{
  if(11 * tid < count) odd_even_transpose_sort_11_int_float(thread_keys0, thread_vals0);
  thread_to_shared_11_int(thread_keys0, tid, keys_shared0);
  block_sort_loop_256_11_int_float(tid, count, keys_shared0, thread_vals0, vals_shared0);
}



回答3:


Take a look at Boost.Compute. It provides a C++, STL-like API for OpenCL.



来源:https://stackoverflow.com/questions/22467579/need-to-convert-c-template-to-c99-code

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