user2668204
user2668204

Reputation: 37

Statically allocated global memory struct in CUDA

Is this definition of static global memory struct possible in CUDA?

struct ABC
{
    int nIntArray[3];
    float rFLoatArray[3];
};

__device__ static ABC xABCStruct = {{1, 2, 3}, {1.f, 2.f, 3.f}};

How can I watch this struct instance in CUDA memory?

Thanks in advance.

Upvotes: 1

Views: 574

Answers (1)

Vitality
Vitality

Reputation: 21455

Yes, it is possible. Take a look at the following code, showing also an example on how accessing it:

#include<stdio.h>

/********************/
/* CUDA ERROR CHECK */
/********************/
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, char *file, int line, bool abort=true)
{
    if (code != cudaSuccess) 
    {
        fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
        if (abort) exit(code);
    }
}

struct ABC { int nIntArray[3]; float rFLoatArray[3];};

__device__ static ABC xABCStruct = {{1, 2, 3}, {1.f, 2.f, 3.f}};

__device__ static float test_float = 3.f;

__global__ void kernel1() { printf("%f\n", test_float); }

__global__ void kernel2() { 
    printf("%i\n", xABCStruct.nIntArray[0]); 
    printf("%i\n", xABCStruct.nIntArray[1]); 
    printf("%i\n", xABCStruct.nIntArray[2]); 
    printf("%f\n", xABCStruct.rFLoatArray[0]); 
    printf("%f\n", xABCStruct.rFLoatArray[1]); 
    printf("%f\n", xABCStruct.rFLoatArray[2]); 
}

void main() {
    kernel1<<<1,1>>>();
    gpuErrchk(cudaPeekAtLastError());
    gpuErrchk(cudaDeviceSynchronize());

    kernel2<<<1,1>>>();
    gpuErrchk(cudaPeekAtLastError());
    gpuErrchk(cudaDeviceSynchronize());
}

Upvotes: 1

Related Questions