Can i allocate constant memory for a structure?

I have a variable of structure type, which has another variable of structure type embedded in it, and I want to apply constant memory space for this variable, can anyone tell me how to do?

You mean something like this?

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

typedef struct {
    float x;
    float y;
} inner;

typedef struct {
    inner pos;
    float mag;
} outer;

const __constant__ outer foo = {{1.2f, 3.4f}, 5.6f};

__global__ void kernel (void)
{
    printf ("pos.x = %15.8e\n", foo.pos.x);
    printf ("pos.y = %15.8e\n", foo.pos.y);
    printf ("mag   = %15.8e\n", foo.mag);
}

int main (void)
{
    kernel<<<1,1>>>();
    cudaDeviceSynchronize ();
    return EXIT_SUCCESS;
}
1 Like