Pergunta

I've been trying to allocate a variable that can be accessed by each kernel function. My attempt is the code attached below, but it won't compile cause the dArray can't be viewed accessed by the kernel. In C++ you would place the variable at the top or declare static to be accessed in every scope through out the program.

__global__ void StoreThreadNumber()
{
    dArray[threadIdx.x] = threadIdx.x;
}

int main( int argc, char** argv)
{
    unsigned __int8 Array[16] = { 0 };
    unsigned __int8 dArray[16];

    for( __int8 Position = 0; Position < 16; Position++)
        cout << Array[Position] << " ";
    cout << endl;

    cudaMalloc((void**) dArray, 16*sizeof(__int8));
    cudaMemcpy( dArray, Array, 16*sizeof(__int8), cudaMemcpyHostToDevice);

    StoreThreadNumber<<<1, 16>>>();

    cudaMemcpy( Array, dArray, 16*sizeof(__int8), cudaMemcpyDeviceToHost);

    for( __int8 Position = 0; Position < 16; Position++)
        cout << Array[Position] << " ";
    cout << endl;

    cudaFree(dArray);
}

Nenhuma solução correta

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top