記録のために、これは宿題なので、それを念頭に置いて少しでも多くでも助けてください。定数メモリを使用して、より大きな行列で畳み込みを実行するために使用される「マスク行列」を格納しています。ホスト コードにいるときは、cudaMemcpyToSymbol() を使用してマスクを定数メモリにコピーしています。
私の質問は、これがコピーされ、デバイス カーネル コードを起動した後、デバイスが定数メモリ マスク マトリックスにアクセスする場所をどのように認識しているのかということです。カーネルの起動時に渡す必要があるポインターはありますか。教授が私たちに与えてくれたコードのほとんどは変更されるべきではありません (渡されたマスクへのポインタはありません) が、彼が間違いを犯した可能性は常にあります (私の理解の可能性が最も高いですが)
定数メモリ宣言は別の 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;
}