cuda - 如何将字符指针从设备复制到主机
问题描述
我想在主要的“文本”变量中包含“一些文本”。如何使用cudaMemcpyFromSymbol()来实现这一点?
__device__ char* pointerToSomething;
__global__ void DoSomething()
{
pointerToSomething = "some text";
}
int main()
{
char text;
}
解决方案
它不能只通过一次调用来完成cudaMemcpyFromSymbol
(它将需要 2 个复制步骤),并且您不能将 9 个字符的文本存储在声明为的变量中char text;
因此,对于提到的这两个项目,您可以这样做:
$ cat t1606.cu
#include <iostream>
__device__ char* pointerToSomething;
__global__ void DoSomething()
{
pointerToSomething = "some text";
}
int main()
{
char text[32] = {0};
char *data;
DoSomething<<<1,1>>>();
cudaMemcpyFromSymbol(&data, pointerToSomething, sizeof(char *));
cudaMemcpy(text, data, 9, cudaMemcpyDeviceToHost);
std::cout << text << std::endl;
}
$ nvcc -o t1606 t1606.cu
t1606.cu(5): warning: conversion from a string literal to "char *" is deprecated
t1606.cu(5): warning: conversion from a string literal to "char *" is deprecated
$ cuda-memcheck ./t1606
========= CUDA-MEMCHECK
some text
========= ERROR SUMMARY: 0 errors
$
它可以以相同的方式工作printf
:
$ cat t1606.cu
#include <cstdio>
__device__ char* pointerToSomething;
__global__ void DoSomething()
{
pointerToSomething = "some text";
}
int main()
{
char text[32] = {0};
char *data;
DoSomething<<<1,1>>>();
cudaMemcpyFromSymbol(&data, pointerToSomething, sizeof(char *));
cudaMemcpy(text, data, 9, cudaMemcpyDeviceToHost);
printf("%s\n", text);
}
$ nvcc -o t1606 t1606.cu
t1606.cu(5): warning: conversion from a string literal to "char *" is deprecated
t1606.cu(5): warning: conversion from a string literal to "char *" is deprecated
$ cuda-memcheck ./t1606
========= CUDA-MEMCHECK
some text
========= ERROR SUMMARY: 0 errors
$
推荐阅读
- java - 在 ImageView Android-Java 顶部的带有圆角的视图的上部
- html - 我的背景图像与我的导航栏重叠?
- javascript - 如何调用 html 元素的触发更改事件
- excel - 生成遗漏字符的 Excel 公式
- joomla - 当前目录的子文件夹路径
- wpf - WPF MVVM 导航取消处理了吗?
- docker - Grafana Docker Plesk SSL
- reactjs - 如何在 facebook 上使用材料 ui 为 Avatar 制作边框?
- flutter - sharedmem_gpumem_alloc:mmap failed errno 12 Out of memory when Loading items in a streambuilder
- postgresql - Postgres; 外键引用不起作用