记录一下,这是一份作业,考虑到这一点,请适度提供帮助。我们使用常量内存来存储“掩模矩阵”,以便对较大的矩阵执行卷积。当我在主机代码中时,我正在使用cudaMemcpyToSymbol()将掩模复制到常量内存中。
我的问题是,一旦这个被复制了并启动设备内核代码,设备怎么知道在哪里访问常量内存掩模矩阵。是否需要在内核启动时传入指针?教授给我们的大部分代码都不应该改变(没有传递掩码的指针),但总会有错误的可能性(尽管最可能是我对某些事情的理解错误)。
常量内存声明是否应该包含在单独的kernel.cu文件中?
我正在将代码最小化,只显示与常量内存有关的内容。因此,请不要指出某些东西未初始化等问题。有相关的代码,但现在不是关注的重点。
main.cu:
我的问题是,一旦这个被复制了并启动设备内核代码,设备怎么知道在哪里访问常量内存掩模矩阵。是否需要在内核启动时传入指针?教授给我们的大部分代码都不应该改变(没有传递掩码的指针),但总会有错误的可能性(尽管最可能是我对某些事情的理解错误)。
常量内存声明是否应该包含在单独的kernel.cu文件中?
我正在将代码最小化,只显示与常量内存有关的内容。因此,请不要指出某些东西未初始化等问题。有相关的代码,但现在不是关注的重点。
main.cu:
#include <stdio.h>
#include "kernel.cu"
__constant__ float M_d[FILTER_SIZE * FILTER_SIZE];
int main(int argc, char* argv[])
{
Matrix M_h, N_h, P_h; // M: filter, N: input image, P: output image
/* Allocate host memory */
M_h = allocateMatrix(FILTER_SIZE, FILTER_SIZE);
N_h = allocateMatrix(imageHeight, imageWidth);
P_h = allocateMatrix(imageHeight, imageWidth);
/* Initialize filter and images */
initMatrix(M_h);
initMatrix(N_h);
cudaError_t cudda_ret = cudaMemcpyToSymbol(M_d, M_h.elements, M_h.height * M_h.width * sizeof(float), 0, cudaMemcpyHostToDevice);
//char* cudda_ret_pointer = cudaGetErrorString(cudda_ret);
if( cudda_ret != cudaSuccess){
printf("\n\ncudaMemcpyToSymbol failed\n\n");
printf("%s, \n\n", cudaGetErrorString(cudda_ret));
}
// Launch kernel ----------------------------------------------------------
printf("Launching kernel..."); fflush(stdout);
//INSERT CODE HERE
//block size is 16x16
// \\\\\\\\\\\\\**DONE**
dim_grid = dim3(ceil(N_h.width / (float) BLOCK_SIZE), ceil(N_h.height / (float) BLOCK_SIZE));
dim_block = dim3(BLOCK_SIZE, BLOCK_SIZE);
//KERNEL Launch
convolution<<<dim_grid, dim_block>>>(N_d, P_d);
return 0;
}
kernel.cu: 这是我不知道如何访问常量内存的地方。
//__constant__ float M_c[FILTER_SIZE][FILTER_SIZE];
__global__ void convolution(Matrix N, Matrix P)
{
/********************************************************************
Determine input and output indexes of each thread
Load a tile of the input image to shared memory
Apply the filter on the input image tile
Write the compute values to the output image at the correct indexes
********************************************************************/
//INSERT KERNEL CODE HERE
//__shared__ float N_shared[BLOCK_SIZE][BLOCK_SIZE];
//int row = (blockIdx.y * blockDim.y) + threadIdx.y;
//int col = (blockIdx.x * blockDim.x) + threadIdx.x;
}