gpt4 book ai didi

c - 使用 CUDA 5 的 cudaMemcpyToSymbol 出错

转载 作者:太空狗 更新时间:2023-10-29 16:35:04 26 4
gpt4 key购买 nike

问题

我已经准备了一个使用常量内存的示例 CUDA 代码。我可以在 cuda 4.2 中成功运行它,但是当我使用 CUDA 5 进行编译时,我得到了 "invalid device symbol"。我已在此处附上示例代码。

代码

#include <iostream>
#include <stdio.h>
#include <cuda_runtime.h>
#include <cuda.h>

struct CParameter
{
int A;
float B;
float C;
float D;
};

__constant__ CParameter * CONSTANT_PARAMETER;
#define PARAMETER "CONSTANT_PARAMETER"

bool ERROR_CHECK(cudaError_t Status)
{
if(Status != cudaSuccess)
{
printf(cudaGetErrorString(Status));
return false;
}
return true;
}

// Kernel that executes on the CUDA device
__global__ void square_array(float *a, int N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx<N)
{
a[idx] = CONSTANT_PARAMETER->A * a[idx];
}
}
////Main Function/////
int main(void)
{
/////Variable Definition
const int N = 10;
size_t size = N * sizeof(float);
cudaError_t Status = cudaSuccess;

CParameter * m_dParameter;
CParameter * m_hParameter;
float * m_D;
float * m_H;

//Memory Allocation Host
m_hParameter = new CParameter;
m_H = new float[N];

//Memory Allocation Device
cudaMalloc((void **) &m_D, size);
cudaMalloc((void**)&m_dParameter,sizeof(CParameter));

////Data Initialization
for (int i=0; i<N; i++)
m_H[i] = (float)i;

m_hParameter->A = 5;
m_hParameter->B = 3;
m_hParameter->C = 98;
m_hParameter->D = 100;

//Memory Copy from Host To Device
Status = cudaMemcpy(m_D, m_H, size, cudaMemcpyHostToDevice);
ERROR_CHECK(Status);

Status = cudaMemcpy(m_dParameter,m_hParameter,sizeof(CParameter),cudaMemcpyHostToDevice);
ERROR_CHECK(Status);

Status = cudaMemcpyToSymbol(PARAMETER, &m_dParameter, sizeof(m_dParameter));
ERROR_CHECK(Status);

// Do calculation on device:
int block_size = 4;

int n_blocks = N/block_size + (N%block_size == 0 ? 0:1);

square_array <<<n_blocks, block_size>>>(m_D,N);

// Retrieve result from device and store it in host array
cudaMemcpy(m_H, m_D, sizeof(float)*N, cudaMemcpyDeviceToHost);

// Print results
for (int i=0; i<N; i++)
printf("%d %f\n", i, m_H[i]);

// Cleanup
free(m_H);
free(m_hParameter);
cudaFree(m_dParameter);
cudaFree(m_D);
return 0;
}

我试过 WINDOWS: CUDA 5.0 Production Release 显卡是 GTX 590。
任何帮助将不胜感激。

最佳答案

努力避免成为 "Stringly Typed" ,在 CUDA 4.1 的 CUDA 运行时 API 函数中不推荐使用字符串来引用设备符号,并在 CUDA 5.0 中删除。

CUDA 5 release notes阅读:

** The use of a character string to indicate a device symbol, which was possible with certain API functions, is no longer supported. Instead, the symbol should be used directly.

如果您将代码更改为以下内容,它应该可以工作。

Status = cudaMemcpyToSymbol(CONSTANT_PARAMETER, &m_dParameter, sizeof(m_dParameter));
ERROR_CHECK(Status);

enter image description here

关于c - 使用 CUDA 5 的 cudaMemcpyToSymbol 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12947914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com