私のプログラムでは、定数変数を含む構造体を使用し、プログラムの実行が完了するまでずっとそれをデバイスに保持したいと考えています。
「グローバル」関数の宣言と、それらの定義用のそれぞれの「.cu」ファイルを含むいくつかのヘッダー ファイルがあります。このスキームは、同様のコードを 1 か所に含めるのに役立つため、保持しました。たとえば、' KERNEL_1 ' を完了するのに必要なすべての ' device ' 関数は、カーネル定義とともに' KERNEL_2 'を完了するのに必要な' device ' 関数から分離されています。
コンパイルおよびリンク中に、このスキームに問題はありませんでした。定数変数に遭遇するまで。すべてのカーネルとデバイス関数で同じ定数変数を使用したいのですが、うまくいきません。
##########################################################################
CODE EXAMPLE
###########################################################################
filename: 'common.h'
--------------------------------------------------------------------------
typedef struct {
double height;
double weight;
int age;
} __CONSTANTS;
__constant__ __CONSTANTS d_const;
---------------------------------------------------------------------------
filename: main.cu
---------------------------------------------------------------------------
#include "common.h"
#include "gpukernels.h"
int main(int argc, char **argv) {
__CONSTANTS T;
T.height = 1.79;
T.weight = 73.2;
T.age = 26;
cudaMemcpyToSymbol(d_const, &T, sizeof(__CONSTANTS));
test_kernel <<< 1, 16 >>>();
cudaDeviceSynchronize();
}
---------------------------------------------------------------------------
filename: gpukernels.h
---------------------------------------------------------------------------
__global__ void test_kernel();
---------------------------------------------------------------------------
filename: gpukernels.cu
---------------------------------------------------------------------------
#include <stdio.h>
#include "gpukernels.h"
#include "common.h"
__global__ void test_kernel() {
printf("Id: %d, height: %f, weight: %f\n", threadIdx.x, d_const.height, d_const.weight);
}
このコードを実行すると、カーネルが実行され、スレッド ID が表示されますが、定数値はゼロとして表示されます。どうすればこれを修正できますか?
提案された変更
filename: gpukernels.h
----------------------------------------------------------------------
__global__ void test_kernel();
----------------------------------------------------------------------
filename: gpukernels.cu
----------------------------------------------------------------------
#include <stdio.h>
#include "common.h"
#include "gpukernels.h"
extern "C" __constant__ __CONSTANTS d_const;
__global__ void test_kernel() {
printf("Id: %d, Height: %f, Weight: %f\n", threadIdx.x, d_const.height, d_const.weight);
}
----------------------------------------------------------------------
filename: common.h
----------------------------------------------------------------------
typedef struct {
double height;
double weight;
int age;
} __CONSTANTS;
----------------------------------------------------------------------
filename: main.cu
----------------------------------------------------------------------
#include "common.h"
#include "gpukernels.h"
__constant__ __CONSTANTS d_const;
int main(int argc, char **argv) {
__CONSTANTS T;
T.height = 1.79;
T.weight = 73.2;
T.age = 26;
cudaMemcpyToSymbol(d_const, &T, sizeof(__CONSTANTS));
test_kernel <<< 1, 16 >>> ();
cudaDeviceSynchronize();
return 0;
}
提案どおり、コードを試しましたが、まだ機能しません。ここで何か見逃しましたか?