How to run “host” functions on GPU with CUDA?

后端 未结 1 1036
别那么骄傲
别那么骄傲 2020-12-07 00:22

I\'m going to run on GPU for example a strcmp function, but I get:

error: calling a host function(\"strcmp\") from a __device__/__global__ func         


        
1条回答
  •  囚心锁ツ
    2020-12-07 00:56

    CUDA has a standard library, documented in the CUDA programming guide. It includes printf() for devices that support it (Compute Capability 2.0 and higher), as well as assert(). It does not include a complete string or stdio library at this point, however.

    Implementing your own standard library as Jason R. Mick suggests may be possible, but it is not necessarily advisable. In some cases, it may be unsafe to naively port functions from the sequential standard library to CUDA -- not least because some of these implementations are not meant to be thread safe (rand() on Windows, for example). Even if it is safe, it might not be efficient -- and it might not really be what you need.

    In my opinion, you are better off avoiding standard library functions in CUDA that are not officially supported. If you need the behavior of a standard library function in your parallel code, first consider whether you really need it: * Are you really going to do thousands of strcmp operations in parallel? * If not, do you have strings to compare that are many thousands of characters long? If so, consider a parallel string comparison algorithm instead.

    If you determine that you really do need the behavior of the standard library function in your parallel CUDA code, then consider how you might implement it (safely and efficiently) in parallel.

    0 讨论(0)
提交回复
热议问题