简体   繁体   English

CUDA指向设备常量的指针

[英]CUDA pointers to device constants

I have the following constant defined on the CUDA device: 我在CUDA设备上定义了以下常量:

__constant__ int deviceTempVariable = 1;

Now I tried getting the address of deviceTempVariable using two methods, and I'm getting different results. 现在我尝试使用两种方法获取deviceTempVariable的地址,并且我得到了不同的结果。 The first is a direct memory access from a CUDA kernel as follows: 第一个是来自CUDA内核的直接内存访问,如下所示:

__global__ void cudaPointers(pointerStruct* devicePointer)
{
    devicePointer->itsPointer = &deviceTempVariable;
}

The other is through host code as follows: 另一种是通过主机代码如下:

cudaGetSymbolAddress((void**) &pointerCuda, "deviceTempVariable");

I was curious and checked the address values; 我好奇并检查了地址值; the first gives something like 00000008 , and the second is 00110008 . 第一个给出类似00000008 ,第二个是00110008 The offset seems to be the same in all cases (the number 8), but the rest is different. 在所有情况下(数字8),偏移量似乎相同,但其余部分则不同。 What's going on here, and which address would I have to use? 这里发生了什么,我必须使用哪个地址?

A pointer created in the kernel is clearly usable on the device. 在内核中创建的指针显然可以在设备上使用。 It's probably a physical address, although some GPUs might be starting to add virtualization (MMU and TLB). 它可能是一个物理地址,虽然有些GPU可能会开始添加虚拟化(MMU和TLB)。

It looks like cudaGetSymbolAddress is giving you an address usable from the host processor. 看起来cudaGetSymbolAddress为您提供了可从主处理器使用的地址。 It's different because device memory has been mapped into the host's virtual address space with an offset. 这是不同的,因为设备内存已映射到主机的虚拟地址空间并带有偏移量。

Host code should use the address returned by cudaGetSymbolAddress , kernel code should use the address-of operator & . 主机代码应该使用cudaGetSymbolAddress返回的地址,内核代码应该使用address-of运算符&

Pointers embedded in shared-data structures need to use based addressing (basically the same as array indexing, you store the offset from a known location that both host and kernel can find). 嵌入在共享数据结构中的指针需要使用基于寻址的方法(基本上与数组索引相同,您可以存储主机和内核都可以找到的已知位置的偏移量)。

This is interesting. 这是有趣的。 How do you print the two address ? 你怎么打印这两个地址? are you sure you are not printing the address of the pointer ? 你确定你没有打印指针的地址吗?

You need to pass a 你需要传递一个

void * address;

to

cudaGetSymbolAddress( &address, "deviceTempVariable");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM