[quote name=‘Beteigeuze’ post=‘1106259’ date=‘Aug 19 2010, 10:51 PM’]
by the way i have a question about the constant memory and “allocation in the same file”.
I am executing a kernel every frame, and if i point to the constant memory every frame (see following code) - it works
[codebox]#include
#include “cutil_inline.h”
//#include “cuprintf.cu”
typedef struct{
char test [7];
int val1;
} Struct1;
typedef struct {
Struct1 *stru1;
int somedummies[4];
} Struct2;
Struct1 hoststruct={“foobar”,33};
constant Struct1 conststruct;
global void testkernel( Struct1 *b, Struct2 *c )
{
//cuPrintf("Kernel: pointer %p\n",c->stru1);
b->val1 = c->stru1->val1; // use pointer in struct2 to get values in constant struct1
memcpy(b->test,c->stru1->test,sizeof(b->test));
}
int main()
{
Struct1 *devicestruct=0, check;
Struct2 *test=0;
void *devptr;
cudaError_t cerr;
cudaMalloc( &devicestruct, sizeof(hoststruct) );
cerr = cudaMemcpyToSymbol( "conststruct", &hoststruct, sizeof(hoststruct), 0, cudaMemcpyHostToDevice );
if( cerr != cudaSuccess ) puts(cudaGetErrorString( cerr ));
cudaMalloc( &test, sizeof(*test) );
cerr = cudaGetSymbolAddress (&devptr, "conststruct"); // cannot do this directly to &test->stru1, need cudaMemcpy() !!
if( cerr != cudaSuccess ) puts(cudaGetErrorString( cerr ));
printf("Host: pointer to constant struct: %p\n",devptr);
cudaMemcpy(&test->stru1,&devptr,sizeof(test->stru1),cudaMemcpyHostToDevice);
//cudaPrintfInit();
testkernel <<< 1, 1 >>> ( devicestruct, test );
testkernel <<< 1, 1 >>> ( devicestruct, test ); // repeat to show pointer is retained
cutilCheckMsg( "Kernel execution failed" );
cudaThreadSynchronize();
//cudaPrintfDisplay(stdout, true);
//cudaPrintfEnd();
cudaMemcpy( &check , devicestruct, sizeof(check), cudaMemcpyDeviceToHost );
printf( "%s %s, %6d %6d\n", hoststruct.test, check.test, hoststruct.val1, check.val1 );
printf( "%s\n", hoststruct.val1 == check.val1 ? "Passed" : "Failed" );
cudaFree( devicestruct );
cudaFree( test );
}[/codebox]