Arrays of structs in device memory

I have a problem with the following code:

#include <stdio.h>

#include <cuComplex.h>

#include "cuda.h"

extern "C" struct vector

{

	float x;

	float y;

	float z;

} vector;

struct vector *test;

__device__ struct vector *d_test;

const int dim = 50;

__global__ void d_testfunc(struct vector *values)

{

  values[0].z = 3;

}

int main()

{

  test = (struct vector*) malloc(sizeof(vector) * dim);

  cudaMalloc((void**)&d_test, sizeof(vector) * dim);

  for (unsigned int i=0; i<dim; i++)

  {

	test[i].x = 2;

	test[i].y = 2;

	test[i].z = 2;

  }

  printf("initial: %g\n", test[0].z);

  cudaMemcpy(d_test, test, sizeof(vector) * dim, cudaMemcpyHostToDevice);

  d_testfunc<<<1,1>>>(d_test);

  cudaMemcpy(test, d_test, sizeof(vector) * dim, cudaMemcpyDeviceToHost);

  printf("final  : %g\n", test[0].z);

}

Running it like that does exactly what it should, it write “2” and “3”.

But when I change the data type in my vector from “float” to “double” it stops working, the output will be “2” both times. In my program I sadly need doubles. Why do doubles mess things up? Is there any way around it?

I have a problem with the following code:

#include <stdio.h>

#include <cuComplex.h>

#include "cuda.h"

extern "C" struct vector

{

	float x;

	float y;

	float z;

} vector;

struct vector *test;

__device__ struct vector *d_test;

const int dim = 50;

__global__ void d_testfunc(struct vector *values)

{

  values[0].z = 3;

}

int main()

{

  test = (struct vector*) malloc(sizeof(vector) * dim);

  cudaMalloc((void**)&d_test, sizeof(vector) * dim);

  for (unsigned int i=0; i<dim; i++)

  {

	test[i].x = 2;

	test[i].y = 2;

	test[i].z = 2;

  }

  printf("initial: %g\n", test[0].z);

  cudaMemcpy(d_test, test, sizeof(vector) * dim, cudaMemcpyHostToDevice);

  d_testfunc<<<1,1>>>(d_test);

  cudaMemcpy(test, d_test, sizeof(vector) * dim, cudaMemcpyDeviceToHost);

  printf("final  : %g\n", test[0].z);

}

Running it like that does exactly what it should, it write “2” and “3”.

But when I change the data type in my vector from “float” to “double” it stops working, the output will be “2” both times. In my program I sadly need doubles. Why do doubles mess things up? Is there any way around it?

Did you choose the compute capability when you compile?

Did you choose the compute capability when you compile?

Oh, I didn’t know I have to manually specify that (I’m using a 1.3 card).

Compiling it with -arch compute_13 solved the problem, thanks.

Oh, I didn’t know I have to manually specify that (I’m using a 1.3 card).

Compiling it with -arch compute_13 solved the problem, thanks.