Tag: cuda

对于使用CUDA的嵌套循环

我有一些问题需要嵌套循环,我必须从C / C ++转换为CUDA。 基本上我有4个嵌套循环,它们共享相同的数组并进行位移操作。 #define N 65536 // ———————————————————————————- int a1,a2,a3,a4, i1,i2,i3,i4; int Bit4CBitmapLookUp[16] = {0, 1, 3, 3, 7, 7, 7, 7, 15, 15, 15, 15, 15, 15, 15, 15}; int _cBitmapLookupTable[N]; int s = 0; // index into the cBitmapLookupTable for (i1 = 0; i1 < 16; i1++) { // first customer […]

cudaMemcpyToSymbol使用或不使用字符串

我试图以这种方式将结构复制到常量内存: struct Foo { int a, b, c; }; __constant__ Foo cData; int main() { Foo hData = {1, 2, 3}; cudaMemcpyToSymbol(cData, &hData, sizeof(Foo)); // … } 这工作正常,在我的内核中我可以直接访问常量数据: __global__ void kernel() { printf(“Data is: %d %d %d\n”, cData.a, cData.b, cData.c); // 1 2 3 } 但后来我尝试使用const char *作为符号名称,事情停止了: cudaMemcpyToSymbol(“cData”, &hData, sizeof(Foo)); // prints 0 0 […]

CUDA矩阵乘法中断了大型矩阵

我有以下矩阵乘法代码,使用CUDA 3.2和VS 2008实现。我在Windows server 2008 r2 enterprise上运行。 我正在运行Nvidia GTX 480.以下代码适用于“宽度”(矩阵宽度)的值高达约2500左右。 int size = Width*Width*sizeof(float); float* Md, *Nd, *Pd; cudaError_t err = cudaSuccess; //Allocate Device Memory for M, N and P err = cudaMalloc((void**)&Md, size); err = cudaMalloc((void**)&Nd, size); err = cudaMalloc((void**)&Pd, size); //Copy Matrix from Host Memory to Device Memory err = cudaMemcpy(Md, M, size, […]

CUDA上的块间障碍

我想在CUDA上实现Inter-block障碍,但遇到了严重的问题。 我无法弄清楚为什么它不起作用。 #include #include #include #define SIZE 10000000 #define BLOCKS 100 using namespace std; struct Barrier { int *count; __device__ void wait() { atomicSub(count, 1); while(*count) ; } Barrier() { int blocks = BLOCKS; cudaMalloc((void**) &count, sizeof(int)); cudaMemcpy(count, &blocks, sizeof(int), cudaMemcpyHostToDevice); } ~Barrier() { cudaFree(count); } }; __global__ void sum(int* vec, int* cache, int *sum, […]

printf里面有CUDA __global__函数

我目前正在GPU上编写矩阵乘法并想调试我的代码,但由于我无法在设备函数中使用printf,我还能做些什么来查看该函数内部的内容。 这是我目前的function: __global__ void MatrixMulKernel(Matrix Ad, Matrix Bd, Matrix Xd){ int tx = threadIdx.x; int ty = threadIdx.y; int bx = blockIdx.x; int by = blockIdx.y; float sum = 0; for( int k = 0; k < Ad.width ; ++k){ float Melement = Ad.elements[ty * Ad.width + k]; float Nelement = Bd.elements[k * Bd.width + […]

CUDA的__shared__内存何时有用?

有人可以帮我一个关于如何使用共享内存的一个非常简单的例子吗? Cuda C编程指南中包含的示例似乎与无关的细节混杂在一起。 例如,如果我将一个大型数组复制到设备全局内存并想要对每个元素求平方,那么如何使用共享内存来加速这个? 或者在这种情况下没用?

CUDA:if语句中的__syncthreads()

我有一个关于CUDA同步的问题。 特别是,我需要对if语句中的同步进行一些澄清。 我的意思是,如果我将__syncthreads()放在if语句的范围内,该块语句被块内的一小部分线程击中,会发生什么? 我认为一些线程将“永远”等待其他线程不会达到同步点。 所以,我编写并执行了一些示例代码来检查: __global__ void kernel(float* vett, int n) { int index = blockIdx.x*blockDim.x + threadIdx.x; int gridSize = blockDim.x*gridDim.x; while( index < n ) { vett[index] = 2; if(threadIdx.x < 10) { vett[index] = 100; __syncthreads(); } __syncthreads(); index += gridSize; } } 令人惊讶的是,我观察到输出非常“正常”(64个元素,块大小为32): 100 100 100 100 100 100 100 100 […]

2d char数组到CUDA内核

我需要帮助将char [] []传递给Cuda内核。 这是我的代码: __global__ void kernel(char** BiExponent){ for(int i=0; i<500; i++) printf("%c",BiExponent[1][i]); // I want print line 1 } int main(){ char (*Bi2dChar)[500] = new char [5000][500]; char **dev_Bi2dChar; …//HERE I INPUT DATA TO Bi2dChar size_t host_orig_pitch = 500 * sizeof(char); size_t pitch; cudaMallocPitch((void**)&dev_Bi2dChar, &pitch, 500 * sizeof(char), 5000); cudaMemcpy2D(dev_Bi2dChar, pitch, Bi2dChar, host_orig_pitch, 500 […]

GPU上的内存分配用于动态结构数组

将struct数组传递给gpu内核时遇到问题。 我基于这个主题 – cudaMemcpy分段错误 ,我写的是这样的: #include #include struct Test { char *array; }; __global__ void kernel(Test *dev_test) { for(int i=0; i < 5; i++) { printf("Kernel[0][i]: %c \n", dev_test[0].array[i]); } } int main(void) { int n = 4, size = 5; Test *dev_test, *test; test = (Test*)malloc(sizeof(Test)*n); for(int i = 0; i < n; i++) […]

在Cuda内核中生成变化范围内的随机数

我试图在cuda内核中生成随机数随机数。 我希望从均匀分布和整数forms生成随机数,从1到8开始。随机数对于每个线程都是不同的。 可以生成随机数的范围也会因线程而异。 一个线程中的最大范围可能低至2,或者在另一个线程中,它可以高达8,但不高于该值。 那么,我在下面提供了一个如何生成数字的示例: In thread#1 –> maximum of the range is 2 and so the random number should be between 1 and 2 In thread#2 –> maximum of the range is 6 and so the random number should be between 1 and 6 In thread#3 –> maximum of the range is 5 and […]