C++ 恒定内存错误
我试图用CUDA5.5做一个具有恒定内存的示例代码。我有两个大小为3000的常量数组。我有另一个大小为N的全局数组X。 我想计算C++ 恒定内存错误,c++,cuda,cuda-gdb,gpu-constant-memory,C++,Cuda,Cuda Gdb,Gpu Constant Memory,我试图用CUDA5.5做一个具有恒定内存的示例代码。我有两个大小为3000的常量数组。我有另一个大小为N的全局数组X。 我想计算 Y[tid] = X[tid]*A[tid%3000] + B[tid%3000] 这是代码 #include <iostream> #include <stdio.h> using namespace std; #include <cuda.h> __device__ __constant__ int A[3000];
Y[tid] = X[tid]*A[tid%3000] + B[tid%3000]
这是代码
#include <iostream>
#include <stdio.h>
using namespace std;
#include <cuda.h>
__device__ __constant__ int A[3000];
__device__ __constant__ int B[3000];
__global__ void kernel( int *dc_A, int *dc_B, int *X, int *out, int N)
{
int tid = threadIdx.x + blockIdx.x*blockDim.x;
if( tid<N )
{
out[tid] = dc_A[tid%3000]*X[tid] + dc_B[tid%3000];
}
}
int main()
{
int N=100000;
// set affine constants on host
int *h_A, *h_B ; //host vectors
h_A = (int*) malloc( 3000*sizeof(int) );
h_B = (int*) malloc( 3000*sizeof(int) );
for( int i=0 ; i<3000 ; i++ )
{
h_A[i] = (int) (drand48() * 10);
h_B[i] = (int) (drand48() * 10);
}
//set X and Y on host
int * h_X = (int*) malloc( N*sizeof(int) );
int * h_out = (int *) malloc( N*sizeof(int) );
//set the vector
for( int i=0 ; i<N ; i++ )
{
h_X[i] = i;
h_out[i] = 0;
}
// copy, A,B,X,Y to device
int * d_X, *d_out;
cudaMemcpyToSymbol( A, h_A, 3000 * sizeof(int) ) ;
cudaMemcpyToSymbol( B, h_B, 3000 * sizeof(int) ) ;
cudaMalloc( (void**)&d_X, N*sizeof(int) ) );
cudaMemcpy( d_X, h_X, N*sizeof(int), cudaMemcpyHostToDevice ) ;
cudaMalloc( (void**)&d_out, N*sizeof(int) ) ;
//call kernel for vector addition
kernel<<< (N+1024)/1024,1024 >>>(A,B, d_X, d_out, N);
cudaPeekAtLastError() ;
cudaDeviceSynchronize() ;
// D --> H
cudaMemcpy(h_out, d_out, N * sizeof(int), cudaMemcpyDeviceToHost ) ;
free(h_A);
free(h_B);
return 0;
}
谁能帮我解决一下恒定内存问题吗这里有几个问题。从展示使用这两个常量数组的“正确”方法开始,然后解释为什么您所做的不起作用,可能会更容易。因此内核应该如下所示:
__global__ void kernel(int *X, int *out, int N)
{
int tid = threadIdx.x + blockIdx.x*blockDim.x;
if( tid<N )
{
out[tid] = A[tid%3000]*X[tid] + B[tid%3000];
}
}
\uuuu全局\uuuuu无效内核(int*X,int*out,int-N)
{
int tid=threadIdx.x+blockIdx.x*blockDim.x;
如果(tid这里有几个问题。从显示使用这两个常量数组的“正确”方法开始可能更容易,然后解释为什么您所做的不起作用。因此内核应该如下所示:
__global__ void kernel(int *X, int *out, int N)
{
int tid = threadIdx.x + blockIdx.x*blockDim.x;
if( tid<N )
{
out[tid] = A[tid%3000]*X[tid] + B[tid%3000];
}
}
\uuuu全局\uuuuu无效内核(int*X,int*out,int-N)
{
int tid=threadIdx.x+blockIdx.x*blockDim.x;
如果(TID您显示的调试器输出不是错误。启动新的调试器运行时,这是绝对正常的输出。您显示的调试器输出不是错误。启动新的调试器运行时,这是绝对正常的输出。非常感谢,这很管用。也感谢您分享有关性能的见解。实际上,我正在尝试学习使用恒定内存。但是,我会记住你关于生产代码的提示谢谢,这很有效。也感谢你分享关于性能的见解。实际上,我正在尝试学习使用恒定内存。但我会记住你关于生产代码的提示