在使用纹理内存时,我遇到了以下代码: -
uint f = (blockIdx.x * blockDim.x) + threadIdx.x;
uint c = (blockIdx.y * blockDim.y) + threadIdx.y;
uint read = tex2D( refTex, c+0.5f, f+0.5f);
Run Code Online (Sandbox Code Playgroud)
我的问题是,为什么我们添加0.5f到两个c及f?这让我感到困惑..谢谢你
我试图在CUDA中为一维数组实现HAAR小波变换.
我在输入数组中有8个索引
在这种情况下,if(x_index>=o_width/2 || y_index>=o_height/2)我将有4个线程,应该是0,2,4,6,我计划在输入中使用每个指针处理handletwo索引.
我计算avg.EG:如果我的线程id是'0' - 然后avg是(输入[0] +输入[1])/ 2然后同时我得到将输入[0]的差异 - avg等等其余的线程.
现在重要的是输出的位置.我为输出创建了一个单独的thread_id,因为使用索引0,2,4,6会在将输出放置在正确的索引中时产生困难.
我的avgs应该放在前4个索引中,即输出的0,1,2,3和o_thread_id应该是0,1,2,3.类似地,为了将差异置于4,5,6,7,我将增加了0,1,2,3,其中"4"如代码所示
我的输出全部归零!无论我改变什么,我都会得到这个.
__global__ void cal_haar(int input[],float output [],int i_widthstep,int o_widthstep,int o_width,int o_height)
{
int x_index=blockIdx.x*blockDim.x+threadIdx.x;
int y_index=blockIdx.y*blockDim.y+threadIdx.y;
if(x_index>=o_width/2 || y_index>=o_height/2) return;
int i_thread_id=y_index*i_widthstep+(2*x_index);
int o_thread_id=y_index*o_widthstep+x_index;
float avg=(input[i_thread_id]+input[i_thread_id+1])/2;
float diff=input[i_thread_id]-avg;
output[o_thread_id]=avg;
output[o_thread_id+4]=diff;
}
void haar(int input[],float output [],int i_widthstep,int o_widthstep,int o_width,int o_height)
{
int * d_input;
float * d_output;
cudaMalloc(&d_input,i_widthstep*o_height);
cudaMalloc(&d_output,o_widthstep*o_height);
cudaMemcpy(d_input,input,i_widthstep*o_height,cudaMemcpyHostToDevice);
dim3 blocksize(16,16);
dim3 gridsize;
gridsize.x=(o_width+blocksize.x-1)/blocksize.x;
gridsize.y=(o_height+blocksize.y-1)/blocksize.y;
cal_haar<<<gridsize,blocksize>>>(d_input,d_output,i_widthstep,o_widthstep,o_width,o_height);
cudaMemcpy(output,d_output,o_widthstep*o_height,cudaMemcpyDeviceToHost);
cudaFree(d_input);
cudaFree(d_output);
} …Run Code Online (Sandbox Code Playgroud) 我认为它分配内存sizeof(A)和return地址pointer a,我是对的吗?第二个问题是,我不明白::operator为什么它使用符号::和单词operator?
A* a = ::operator new(sizeof(A));
Run Code Online (Sandbox Code Playgroud) 假设我将数组声明为 int myarray[5]
或者声明为 int*myarray=malloc(5*sizeof(int))
可同时将申报设置的内存等量的字节数?不考虑前一个声明是针对堆栈而后者是堆上的.
谢谢!
我正在尝试为2D矩阵计算求和区域表,其中行和列的数量不相等.我遇到了一个小问题,我的代码似乎在行和列相等的情况下运行正常,但是当行和列不相等时,它无法计算最终输出的最后一行.问题是我无法弄清楚为什么会这样.
基本上,在积分和中,每个像素或索引元素计算其上方和后方的所有矩阵元素的总和.例如,对于具有以下元素的3x2输入数组:
[5, 2|
|5, 2|
|5, 2]
Run Code Online (Sandbox Code Playgroud)
输出数组中的积分和如下:
[5, 7|
|10, 14|
|15, 21]
Run Code Online (Sandbox Code Playgroud)
基本上以下是我在CUDA C中尝试做的事情:
for(int matrixElement_y_index=0; matrixElement_y_index<=total_rows-1; matrixElement_y_index++)
{
//matrixElement_x_index and matrixElement_y_index represent (x,y) indices of each matrix element
for(int matrixElement_x_index=0; matrixElement_x_index<=total_columns-1; matrixElement_x_index++)
{
int temp=0;
for(int r=0;r<=(matrixElement_y_index);r++)
{
for(int c=0; c<=matrixElement_x_index;c++)
{
temp=temp+input[c][r];
}
}
output[matrixElement_y_index][matrixElement_x_index]=temp;
}
}
Run Code Online (Sandbox Code Playgroud)
我到目前为止提出的CUDA C代码如下:
#include <iostream>
#include <cuda_runtime.h>
using namespace std;
__global__ void image_integral(int *a, int*b, int width_x,int width_y)
{
// Thread Ids equal to …Run Code Online (Sandbox Code Playgroud) 在CUDA中,当我们谈论并行线程执行相同的代码时,它们的执行顺序是什么?
如果,我有4个线程,对于4个元素的1D数组.所有四个线程在数组的某个索引上执行某些操作.线程4是否总是在线程3之后执行,或者执行中没有特定的顺序?
谢谢!