我有一段必须同时在CPU和CUDA上运行的代码-GPU和另外一段单独在CPU上运行的代码。#define ENABLE_CUDA是我用来在整个应用程序中启用CUDA代码的工具。下面是我的代码...
# define ENABEL_CUDA is the preprocessor directive to turn ON/OFF CUDA code.
CPU and GPU code --This piece of code has to be executed irrespective of whether CUDA is ON / OFF.
standalone CPU code alone -- This piece of code has to be executed only if CUDA is OFF.我的解决方案是:
#ifdef ENABLE_CUDA
CPU AND GPU code
# else
CPU AND GPU code
standalone CPU code
# endif但是这涉及到ifdef和else块中的代码重复(CPU和GPU代码),我想避免这种情况。
我如何才能做到这一点?为了避免代码重复,需要做些什么呢?任何关于这方面的建议都很感谢...
发布于 2013-03-29 00:29:25
#ifdef ENABLE_CUDA
CPU AND GPU code
# else
CPU AND GPU code
standalone CPU code
# endif等同于:
CPU AND GPU code
# ifndef ENABLE_CUDA
standalone CPU code
# endif通常,如果代码对if和else都是通用的,则可以将其移出这两种语言。
发布于 2013-03-29 00:29:03
为什么不简单地使用:
CPU AND GPU code
#ifndef ENABLE_CUDA
standalone CPU code
# endif发布于 2013-03-29 06:14:44
除了其他人已经说过的话,
#ifndef ENABLE_CUDA
# define __device__
#endif如果CUDA存在,您将花费很长时间编写在设备上运行的函数,如果没有CUDA,则在主机上运行,而不会出现代码重复。
https://stackoverflow.com/questions/15687138
复制相似问题