从 cuda 设备上的统一内存打印阵列不起作用
Posted
技术标签:
【中文标题】从 cuda 设备上的统一内存打印阵列不起作用【英文标题】:printing array from unified memory on cuda device doesn`t work 【发布时间】:2021-02-06 23:56:55 【问题描述】:我尝试在 cuda 设备上创建一些哈希并将它们打印在主机上。但是在主机上的 printf 中,我在位置 0x000000000100002F 处出现读取错误
相关行如下所示:
int main()
const int block_size = 2;
const int num_blocks = 256;
const int N = block_size * num_blocks;
unsigned char** hashes;
cudaMallocManaged(&hashes, N * (32 * sizeof(unsigned char)));
cudaMemset(hashes, 0, N * (32 * sizeof(unsigned char)));
在设备上
__global__ void sha256_kernel(unsigned char **dhashes)
int idx = blockIdx.x * blockDim.x + threadIdx.x;
sha256_final(&ctx, sha);
dhashes[idx] = sha;
// printf("%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x\n", dhashes[idx][0], dhashes[idx][1], dhashes[idx][2], dhashes[idx][3], dhashes[idx][4], dhashes[idx][5], dhashes[idx][6], dhashes[idx][7], dhashes[idx][8], dhashes[idx][9], dhashes[idx][10], dhashes[idx][11], dhashes[idx][12], dhashes[idx][13], dhashes[idx][14], dhashes[idx][15],
// dhashes[idx][16], dhashes[idx][17], dhashes[idx][18], dhashes[idx][19], dhashes[idx][20], dhashes[idx][21], dhashes[idx][22], dhashes[idx][23], dhashes[idx][24], dhashes[idx][25], dhashes[idx][26], dhashes[idx][27], dhashes[idx][28], dhashes[idx][29], dhashes[idx][30], dhashes[idx][31]);
// printing here is correct
然后回到主机端...
sha256_kernel << < num_blocks, block_size>> > (hashes);
cudaDeviceSynchronize();
for (int i = 0; i < N; i++)
printf("%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x\n", hashes[i][0], hashes[i][1], hashes[i][2], hashes[i][3], hashes[i][4], hashes[i][5], hashes[i][6], hashes[i][7], hashes[i][8], hashes[i][9], hashes[i][10], hashes[i][11], hashes[i][12], hashes[i][13], hashes[i][14], hashes[i][15],
hashes[i][16], hashes[i][17], hashes[i][18], hashes[i][19], hashes[i][20], hashes[i][21], hashes[i][22], hashes[i][23], hashes[i][24], hashes[i][25], hashes[i][26], hashes[i][27], hashes[i][28], hashes[i][29], hashes[i][30], hashes[i][31]);
//printing here doesn't work
似乎是正确的,但是当我尝试在主机上打印哈希时,出现读取错误?
【问题讨论】:
sha
是什么?
设备 void sha256_final(SHA256_CTX *ctx, BYTE hash[]);
指向指针的指针不是二维数组,不能像一个那样分配。您的内存分配已损坏
【参考方案1】:
您用于保存哈希的内存分配不正确。要为每个哈希分配一个指向内存的指针数组,您需要为指针数组和哈希本身分配内存,例如:
unsigned char** hashes;
unsigned char* buff;
cudaMallocManaged(&hashes, N * sizeof(unsigned char*));
cudaMallocManaged(&buff, N * (32 * sizeof(unsigned char)));
cudaMemset(buff, 0, N * (32 * sizeof(unsigned char)));
for(i=0; i<N; i++) hashes[i] = &buff[i*32];
[免责声明:用浏览器编写,从未编译或测试,使用风险自负]
【讨论】:
抱歉有错别字。在这种情况下,我应该在标准免责声明中添加“醒来后不久和早上浓缩咖啡之前不戴眼镜在手机上写”以上是关于从 cuda 设备上的统一内存打印阵列不起作用的主要内容,如果未能解决你的问题,请参考以下文章
IOS 上的 mvvmcross 绑定问题(适用于模拟器,但某些属性在设备上不起作用)