[英]Adding strings together in CUDA?
What I want to do is have both strings processed in the device and called on the host. 我想做的是在设备中处理两个字符串并在主机上调用它们。 I get an error telling me that calling host function from global is not allowed. 我收到一条错误消息,告诉我不允许从全局调用主机函数。 With that in mind, how would I go about re-structuring this code so that it is run through the device? 考虑到这一点,我将如何重新构造此代码,以使其在设备中运行?
__global__ void whatever(string *x, string *y){
*x += *y;}
int main(){
string x, y, *x_in, *y_in;
x = "Hello ";
y = "World!\n";
cudaMalloc((void**)&x_in, sizeof(int));
cudaMalloc((void**)&y_in, sizeof(int));
cudaMemcpy(x_in, &x, sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(y_in, &y, sizeof(int), cudaMemcpyHostToDevice);
whatever << <1, 1 >> > (x_in, y_in);
cudaMemcpy(&x, x_in, sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(&y, y_in, sizeof(int), cudaMemcpyDeviceToHost);
cudaFree(&x_in);
cudaFree(&y_in);
cout << x;
return 0; }
Answer to your question: you call *x += *y;
回答您的问题:您致电*x += *y;
, ie concatenation and assignment operation for std::string
objects (obviously defined only as a host function in STL). ,即std::string
对象的连接和赋值操作(显然仅在STL中定义为宿主函数)。
About restructuring - you can pass to device char*
buffers (of course, allocated on the device) instead of string objects and work with them. 关于重组-您可以传递给设备char*
缓冲区(当然是在设备上分配的)而不是字符串对象,然后使用它们。
Some more points about the code: 有关代码的其他几点:
sizeof(int)
bytes from/to device, why? 您仅从设备复制sizeof(int)
字节到设备,为什么? I see that your data is more than 4 bytes; 我发现您的数据超过4个字节; sizeof(int)
bytes for each object, why? 您只为每个对象分配sizeof(int)
字节,为什么? << <1, 1> >>
syntax instead of <<<1, 1,>>>
(I believe that it's only formatting issue after copy-pasting code here); 您使用<< <1, 1> >>
语法而不是<<<1, 1,>>>
(我相信这只是复制粘贴代码后的格式问题);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.