cuPrintf Problem printing char arrays / pointers with cuPrintf

Hi;

I am trying to copy a struct array to device.I am working with one GPU atm, and i have a problem with cuPrintf function which i use to debug my code.

My struct definition is as below:

struct Node{

    char Key[25];

    char ConsAlterKey[25];

    char MasterKey[3];

    int VowelDeletion;

    char Data[6];

    char MasterData[6];

    int Children[35];

    int ChildCount;

};

and for test purpose i fill the struct array like this :

void FillArray(Node *NodeArray)

{	

	for(int i=0;i<TotalNodeCount;i++)

	{

		strcpy(NodeArray[i].Key,"Key");

		

		strcpy(NodeArray[i].ConsAlterKey,"ConsAlterKey");

		strcpy(NodeArray[i].MasterKey,"MasterKey");

				

		NodeArray[i].VowelDeletion=0;

				

		strcpy(NodeArray[i].Data,"Data");

	

		strcpy(NodeArray[i].MasterData,"MasterData");

		

		NodeArray[i].ChildCount=5;

		for(int j =0;j<NodeArray[i].ChildCount;j++)

		{

			NodeArray[i].Children[j]=i+j;

		}

	}

}

my main function looks like this:

int main()

{

	Node *NodeArray;

	Node *GpuTree;

	int tokenCount=0;

        int *tokenCountGPU;

NodeArray =(Node *)malloc(sizeof(Node)*(TotalNodeCount));

        FillArray(NodeArray);

        printf("Filling test : %s\n", NodeArray[13].Key);

	

	cudaMalloc( (void**)&GpuTree, sizeof(Node)*(TotalNodeCount));

	cudaMemcpy(GpuTree, NodeArray,sizeof(Node)*(TotalNodeCount), cudaMemcpyHostToDevice);

	

        //test value

	tokenCount=35;

	

	

        gpuAssert( cudaMalloc((void **)&tokenCountGPU, sizeof(int)) );

	gpuAssert( cudaMemcpy(tokenCountGPU, &tokenCount, sizeof(int), cudaMemcpyHostToDevice) );

cudaPrintfInit();

	Test <<< 1, tokenCount >>> (GpuTree,tokenCountGPU);

	cudaPrintfDisplay(stdout, true);

	cudaPrintfEnd();

	gpuAssert( cudaGetLastError() );

	

        //TODO:free pointers

	return(0);

}

and if I write test function as below:

__global__ void Test(Node *Trie,int *tokenCount)

{

	if (threadIdx.x < *tokenCount) 

	{

		cuPrintf("%s\n",Trie[threadIdx.x].Key);

        }   

        return;

}

i get output like this:

Filling test : Key

[0, 0]: <

[0, 1]: ¶☺!

[0, 2]: ì☺!

[0, 3]: Ä☻!

[0, 4]: o♥!

[0, 5]: t♦!

[0, 6]: L♣!

[0, 7]: $â™ !

[0, 8]: ü♠!

[0, 9]: Ô!

[0, 10]: !

[0, 11]: "

[0, 12]: \

!

[0, 13]: 4♂!

[0, 14]: ♀♀!

[0, 15]: ä♀!

!0, 16]: ¼

[0, 17]: "♫!

[0, 18]: l☼!

[0, 19]: Dâ–º!

[0, 20]: ∟◄!

[0, 21]: ô◄!

[0, 22]: Ì↕!

[0, 23]: ¤‼!

[0, 24]: |¶!

[0, 25]: T§!

[0, 26]: ,â–¬!

[0, 27]: ♦↨!

[0, 28]: Ü↨!

[0, 29]: ´↑!

[0, 30]: O↓!

[0, 31]: d→!

[0, 32]: <←!

[0, 33]: ¶∟!

[0, 34]: ì∟!

but if i change my test method like this:

__global__ void Test(Node *Trie,int *tokenCount)

{

        if (threadIdx.x < *tokenCount) 

        {

            cuPrintf("%c%c%c\n",

                                Trie[threadIdx.x].Key[0],

                                Trie[threadIdx.x].Key[1],

                                Trie[threadIdx.x].Key[2]);

        }

        return;

}

i get the correct output:

Filling test : Key

[0, 0]: Key

[0, 1]: Key

[0, 2]: Key

[0, 3]: Key

[0, 4]: Key

[0, 5]: Key

[0, 6]: Key

[0, 7]: Key

[0, 8]: Key

[0, 9]: Key

[0, 10]: Key

[0, 11]: Key

[0, 12]: Key

[0, 13]: Key

[0, 14]: Key

[0, 15]: Key

[0, 16]: Key

[0, 17]: Key

[0, 18]: Key

[0, 19]: Key

[0, 20]: Key

[0, 21]: Key

[0, 22]: Key

[0, 23]: Key

[0, 24]: Key

[0, 25]: Key

[0, 26]: Key

[0, 27]: Key

[0, 28]: Key

[0, 29]: Key

[0, 30]: Key

[0, 31]: Key

[0, 32]: Key

[0, 33]: Key

[0, 34]: Key

So the question is why do i get corrupt output when I try to print strings through using “%s”?

Thank you.

You can check Key[3] to ensure it is zero. When the null character is somehow gone, you could get that kind of output.