5

私は現在、デバイス側でのみ使用されるクラスを使用して CUDA コードを作成しようとしています (つまり、ホストはその存在を知る必要はありません)。ただし、クラスの正しい修飾子を見つけることができません (deviceclass以下):

__device__ float devicefunction (float *x) {return x[0]+x[1];}

class deviceclass {
    private:
        float _a;

    public:
        deviceclass(float *x) {_a = devicefunction(x);}

        float getvalue () {return _a;}
};    

// Device code
__global__ void VecInit(float* A, int N)
{
    int i = blockDim.x * blockIdx.x + threadIdx.x;
    if (i < N) {
        deviceclass *test;

        test = new deviceclass(1.0, 2.0);

        A[i] = test->getvalue();
    }
}

// Standard CUDA guff below: Variables
float *h_A, *d_A;

// Host code
int main(int argc, char** argv)
{
    printf("Vector initialization...\n");
    int N = 10000;
    size_t size = N * sizeof(float);

    // Allocate
    h_A = (float*)malloc(size);
    cudaMalloc(&d_A, size);

    printf("Computing...\n");
    // Invoke kernel
    int threadsPerBlock = 256;
    int blocksPerGrid = (N + threadsPerBlock - 1) / threadsPerBlock;
    VecInit<<<blocksPerGrid, threadsPerBlock>>>(d_A, N);

    // Copy result from device memory to host memory
    cudaMemcpy(h_A, d_A, size, cudaMemcpyDeviceToHost);

    //...etc
}

Deviceclassa のみを設定__device__すると、グローバル関数から呼び出されるためエラーがスローされますが、 __device__ __host__orとして設定する__global__必要はないようです。誰かが私を正しい方向に向けることができますか?

4

2 に答える 2

12

修飾子はクラスのメンバー関数に適用する必要があることがわかりました。以下は完全に機能するバージョンです。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

void Cleanup(void);


// Functions to be pointed to
__device__ float Plus (float a, float b) {return a+b;}

class deviceclass {

    private:
        float test;

    public:
        __device__ deviceclass(float a, float b) {
            test = Plus(a,b);
        }

        __device__ float getvalue() {return test;}
};

// Device code
__global__ void VecInit(float* A, int N)
{
    int i = blockDim.x * blockIdx.x + threadIdx.x;
    if (i < N) {
        deviceclass test(1.0, 2.0);

        A[i] = test.getvalue();
    }
}

// Standard CUDA guff below: Variables
float *h_A, *d_A;

// Host code
int main(int argc, char** argv)
{
    printf("Vector initialization...\n");
    int N = 10000;
    size_t size = N * sizeof(float);

    // Allocate
    h_A = (float*)malloc(size);
    cudaMalloc(&d_A, size);

    printf("Computing...\n");
    // Invoke kernel
    int threadsPerBlock = 256;
    int blocksPerGrid = (N + threadsPerBlock - 1) / threadsPerBlock;
    VecInit<<<blocksPerGrid, threadsPerBlock>>>(d_A, N);

    // Copy result from device memory to host memory
    cudaMemcpy(h_A, d_A, size, cudaMemcpyDeviceToHost);



    // Verify result
    int i;
    for (i = 0; i < N; ++i) {
        cout << endl << h_A[i];
    }

    cout << endl;

    Cleanup();
}

void Cleanup(void)
{
    // Free device memory
    if (d_A)
        cudaFree(d_A);

    // Free host memory
    if (h_A)
        free(h_A);

    cudaThreadExit();

    exit(0);
}
于 2011-02-23T16:36:26.140 に答える
0

Node()タイプミスだと思います。

CUDA C プログラミング ガイドのセクション 3.1.5 から:

ただし、デバイス コードで完全にサポートされているのは C++ のサブセットのみです。

および付録 D.6:

コンピューティング機能 2.x 以降を備えたデバイス用にコンパイルされたコードは、C++ クラスを使用する場合があります...

あなたのコードは互換性のない C++ を使用していると思います。

于 2011-02-22T17:41:35.470 に答える