# char array - my mistake?

**URL:** <https://forums.developer.nvidia.com/t/char-array-my-mistake/19269>\
**Category:** CUDA Programming and Performance\
**Created:** [October 18, 2010, 1:02pm UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269 "2010-10-18T13:02:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_OskaR](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@\_OskaR](https://forums.developer.nvidia.com/u/_OskaR)\
**Post date:** [October 18, 2010, 1:02pm UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/1 "2010-10-18T13:02:17Z")

</div>

I want to edit bitmap using CUDA. So I have:

```auto
unsigned char *pdata;

unsigned char *pdataGPU;

int numPixels=1280*720;

pdata=new unsigned char[3*numPixels];

size=numPixels*sizeof(unsigned char)*3;

cudaMalloc((void **) &pdataGPU, size);

//copying etc.:

cudaMemcpy(pdataGPU, pdata, size, cudaMemcpyHostToDevice);

cudaThreadSynchronize();

//calculating:

frameGPUeffect1(img1->pdataGPU,1280*720);

//copying to main mem:

cudaMemcpy(pdata, pdataGPU, size, cudaMemcpyDeviceToHost);

cudaThreadSynchronize();

//and functions:

__global__ void CUDAframeGPUeffect1(unsigned char* pdataGPU,int max)

{

	int idx = blockIdx.x * blockDim.x + threadIdx.x;

	if(idx<max)

	{

		pdataGPU[3*idx]=pdataGPU[3*idx];

		pdataGPU[3*idx+1]=0;

		pdataGPU[3*idx+2]=pdataGPU[3*idx+2];

	}

}

extern "C"

void frameGPUeffect1(unsigned char* pdataGPU,int max)

{

	int size=720;

	int block_size = 2;

	int n_blocks = max/block_size + (size%block_size == 0 ? 0:1);

	printf("\nGPU\n");

	CUDAframeGPUeffect1 <<< n_blocks, block_size >>> (pdataGPU,max);

}

```

But here’s no difference between input and output - I have the same image.

---

<div class="post-metadata">

**Author:** ![\_OskaR](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@\_OskaR](https://forums.developer.nvidia.com/u/_OskaR)\
**Post date:** [October 18, 2010, 1:02pm UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/2 "2010-10-18T13:02:17Z")

</div>

I want to edit bitmap using CUDA. So I have:

```auto
unsigned char *pdata;

unsigned char *pdataGPU;

int numPixels=1280*720;

pdata=new unsigned char[3*numPixels];

size=numPixels*sizeof(unsigned char)*3;

cudaMalloc((void **) &pdataGPU, size);

//copying etc.:

cudaMemcpy(pdataGPU, pdata, size, cudaMemcpyHostToDevice);

cudaThreadSynchronize();

//calculating:

frameGPUeffect1(img1->pdataGPU,1280*720);

//copying to main mem:

cudaMemcpy(pdata, pdataGPU, size, cudaMemcpyDeviceToHost);

cudaThreadSynchronize();

//and functions:

__global__ void CUDAframeGPUeffect1(unsigned char* pdataGPU,int max)

{

	int idx = blockIdx.x * blockDim.x + threadIdx.x;

	if(idx<max)

	{

		pdataGPU[3*idx]=pdataGPU[3*idx];

		pdataGPU[3*idx+1]=0;

		pdataGPU[3*idx+2]=pdataGPU[3*idx+2];

	}

}

extern "C"

void frameGPUeffect1(unsigned char* pdataGPU,int max)

{

	int size=720;

	int block_size = 2;

	int n_blocks = max/block_size + (size%block_size == 0 ? 0:1);

	printf("\nGPU\n");

	CUDAframeGPUeffect1 <<< n_blocks, block_size >>> (pdataGPU,max);

}

```

But here’s no difference between input and output - I have the same image.

---

<div class="post-metadata">

**Author:** ![insmvb00](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@insmvb00](https://forums.developer.nvidia.com/u/insmvb00)\
**Post date:** [October 18, 2010, 3:13pm UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/3 "2010-10-18T15:13:45Z")

</div>

Hi!

You have several errors:  
 → first: your blocksize is 2??? it’s very strange;  
 → second: 1280\*720 = 921600/2 = 460800 blocks??? Are you sure? You have to check the limit of the grid. In the CCs 1.0,1.1,1.2, 1.3 and 2.0 is 65535 blocks. If you exceeds this limit, your execution does not fail but the kernel is not executed.

Regards!

---

<div class="post-metadata">

**Author:** ![insmvb00](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@insmvb00](https://forums.developer.nvidia.com/u/insmvb00)\
**Post date:** [October 18, 2010, 3:13pm UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/4 "2010-10-18T15:13:45Z")

</div>

Hi!

You have several errors:  
 → first: your blocksize is 2??? it’s very strange;  
 → second: 1280\*720 = 921600/2 = 460800 blocks??? Are you sure? You have to check the limit of the grid. In the CCs 1.0,1.1,1.2, 1.3 and 2.0 is 65535 blocks. If you exceeds this limit, your execution does not fail but the kernel is not executed.

Regards!

---

<div class="post-metadata">

**Author:** ![\_OskaR](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@\_OskaR](https://forums.developer.nvidia.com/u/_OskaR)\
**Post date:** [October 18, 2010, 4:47pm UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/5 "2010-10-18T16:47:07Z")

</div>

Ahh, thanks - I increased block size and now it works.

---

<div class="post-metadata">

**Author:** ![\_OskaR](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@\_OskaR](https://forums.developer.nvidia.com/u/_OskaR)\
**Post date:** [October 18, 2010, 4:47pm UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/6 "2010-10-18T16:47:07Z")

</div>

Ahh, thanks - I increased block size and now it works.

---

<div class="post-metadata">

**Author:** ![raikkonnen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@raikkonnen](https://forums.developer.nvidia.com/u/raikkonnen)\
**Post date:** [October 20, 2010, 8:52am UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/7 "2010-10-20T08:52:37Z")

</div>

can i ask something?

why do you multiply by 3 in the size and the index?  
what does it means?

execuse me if it is evident because i’m a beginner in CUDA

---

<div class="post-metadata">

**Author:** ![raikkonnen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@raikkonnen](https://forums.developer.nvidia.com/u/raikkonnen)\
**Post date:** [October 20, 2010, 8:52am UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/8 "2010-10-20T08:52:37Z")

</div>

can i ask something?

why do you multiply by 3 in the size and the index?  
what does it means?

execuse me if it is evident because i’m a beginner in CUDA

---

<div class="post-metadata">

**Author:** ![PsychoLogic](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@PsychoLogic](https://forums.developer.nvidia.com/u/PsychoLogic)\
**Post date:** [October 20, 2010, 9:40am UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/9 "2010-10-20T09:40:03Z")

</div>

Each pixel in this example would be 24bits (8bits each for Red, Green and Blue), so three chars are needed per pixel to represent the data.

---

<div class="post-metadata">

**Author:** ![PsychoLogic](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@PsychoLogic](https://forums.developer.nvidia.com/u/PsychoLogic)\
**Post date:** [October 20, 2010, 9:40am UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/10 "2010-10-20T09:40:03Z")

</div>

Each pixel in this example would be 24bits (8bits each for Red, Green and Blue), so three chars are needed per pixel to represent the data.

---

<div class="post-metadata">

**Author:** ![raikkonnen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@raikkonnen](https://forums.developer.nvidia.com/u/raikkonnen)\
**Post date:** [October 20, 2010, 10:59am UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/11 "2010-10-20T10:59:19Z")

</div>

this code is about the longest common subsequence problem

```auto
__global__ void LCS( int* C, char* A, char* B, int wA, int wB)

{

int current_Index = blockIdx.x * blockDim.x + thread_Id;

int wC=wA+1;

int j,i;

int mn=0;

if(wA>wB)

	{

   	

		//for(k=0;k<=wA;k++){

			if (current_Index<=wA)

			{

				j=current_Index;

				i=0;

				mn=min(current_Index,wB);

				while((i<=mn)||(j>0))

				{

					if (i==0 && j==0)

					C[i*wC+j]=0;

					if (A[current_Index-1]==B[current_Index-1])

						C[i*wC+j]=C[(i-1)*wC+j-1]+1;

						

					else

						C[i*wC+j]=max(C[(i-1)*wC+j],C[i*wC+j-1]); 

						

					if (i<=mn)

						i=i+1;

					if (j>0)

						j=j+1;

			}

			__syncthreads();

		}

		

			current_Index = blockIdx.x * blockDim.x + thread_Id+1;;

			if(current_Index<=wB)

			{

				j=wA;

				i=current_Index;

				while(i<=wB)

				{

					if (A[current_Index-1]==B[current_Index-1])

						C[i*wC+j]=C[(i-1)*wC+j-1]+1;

					else

					C[i*wC+j]=max(C[(i-1)*wC+j],C[i*wC+j-1]);

					i=i+1;

					j=j-1;

				}

			__syncthreads();

			}

```

(the parallelism exists in the elements of the antidiagonal)

Which is equivalent to

```auto
for (i=0;i<N+1;i++)

C[i][0]=0;

for (j=1;j<M+1;j++)

C[0][j]=0;

for (i=1;i<N+1;i++)

	for(j=1;j<M+1;j++)

	{

			if (X[j-1]==Y[i-1])

			C[i][j]=C[i-1][j-1]+1;

			else

			C[i][j]=max(C[i-1][j],C[i][j-1]);

	}

```

in C

block\_size=16

wA= 2\*Block\_size //dimension of the array of char A

wB= bloch\_size // dimension of the array of char B

when i execute the kernel i get wrong result in the matrix C (the matrix result of the dynamic programming of LCS)

Can you help me to find the error? if it is in the index of matrix and char’s array or either

---

<div class="post-metadata">

**Author:** ![raikkonnen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@raikkonnen](https://forums.developer.nvidia.com/u/raikkonnen)\
**Post date:** [October 20, 2010, 10:59am UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/12 "2010-10-20T10:59:19Z")

</div>

this code is about the longest common subsequence problem

```auto
__global__ void LCS( int* C, char* A, char* B, int wA, int wB)

{

int current_Index = blockIdx.x * blockDim.x + thread_Id;

int wC=wA+1;

int j,i;

int mn=0;

if(wA>wB)

	{

   	

		//for(k=0;k<=wA;k++){

			if (current_Index<=wA)

			{

				j=current_Index;

				i=0;

				mn=min(current_Index,wB);

				while((i<=mn)||(j>0))

				{

					if (i==0 && j==0)

					C[i*wC+j]=0;

					if (A[current_Index-1]==B[current_Index-1])

						C[i*wC+j]=C[(i-1)*wC+j-1]+1;

						

					else

						C[i*wC+j]=max(C[(i-1)*wC+j],C[i*wC+j-1]); 

						

					if (i<=mn)

						i=i+1;

					if (j>0)

						j=j+1;

			}

			__syncthreads();

		}

		

			current_Index = blockIdx.x * blockDim.x + thread_Id+1;;

			if(current_Index<=wB)

			{

				j=wA;

				i=current_Index;

				while(i<=wB)

				{

					if (A[current_Index-1]==B[current_Index-1])

						C[i*wC+j]=C[(i-1)*wC+j-1]+1;

					else

					C[i*wC+j]=max(C[(i-1)*wC+j],C[i*wC+j-1]);

					i=i+1;

					j=j-1;

				}

			__syncthreads();

			}

```

(the parallelism exists in the elements of the antidiagonal)

Which is equivalent to

```auto
for (i=0;i<N+1;i++)

C[i][0]=0;

for (j=1;j<M+1;j++)

C[0][j]=0;

for (i=1;i<N+1;i++)

	for(j=1;j<M+1;j++)

	{

			if (X[j-1]==Y[i-1])

			C[i][j]=C[i-1][j-1]+1;

			else

			C[i][j]=max(C[i-1][j],C[i][j-1]);

	}

```

in C

block\_size=16

wA= 2\*Block\_size //dimension of the array of char A

wB= bloch\_size // dimension of the array of char B

when i execute the kernel i get wrong result in the matrix C (the matrix result of the dynamic programming of LCS)

Can you help me to find the error? if it is in the index of matrix and char’s array or either

---

<div class="post-metadata">

**Author:** ![insmvb00](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@insmvb00](https://forums.developer.nvidia.com/u/insmvb00)\
**Post date:** [October 20, 2010, 12:04pm UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/13 "2010-10-20T12:04:01Z")

</div>

Hi!  
Well, i have any doubts about your code:  
 → what is thread\_Id?? Perhaps you wanted to write threadIdx.x? Did not?  
 → If you wanted to write threadIdx.x, you are trying to access to A[-1] = B[-1] in the sentence ‘if (A[current\_Index-1]==B[current\_Index-1])’  
This is a bit strange.  
 → 16 threads is a halfwarp, i would use 32 of blockSize. The size of blocks is always better if it is a multiple of 32.

Check this details. Regards.

---

<div class="post-metadata">

**Author:** ![insmvb00](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@insmvb00](https://forums.developer.nvidia.com/u/insmvb00)\
**Post date:** [October 20, 2010, 12:04pm UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/14 "2010-10-20T12:04:01Z")

</div>

Hi!  
Well, i have any doubts about your code:  
 → what is thread\_Id?? Perhaps you wanted to write threadIdx.x? Did not?  
 → If you wanted to write threadIdx.x, you are trying to access to A[-1] = B[-1] in the sentence ‘if (A[current\_Index-1]==B[current\_Index-1])’  
This is a bit strange.  
 → 16 threads is a halfwarp, i would use 32 of blockSize. The size of blocks is always better if it is a multiple of 32.

Check this details. Regards.

---

<div class="post-metadata">

**Author:** ![raikkonnen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@raikkonnen](https://forums.developer.nvidia.com/u/raikkonnen)\
**Post date:** [October 20, 2010, 1:26pm UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/15 "2010-10-20T13:26:22Z")

</div>

I change it to threadIdx.x and Block\_size to 32;

and i execute it and it gives me an error ("LCS has triggered a breakpoint) in this stage (after the execution of the kernel):

LCS\<\<\< grid, threads \>\>\>(d\_C, d\_A, d\_B, WA, WB);  
CheckConditionXR\_(cudaGetLastError() == cudaSuccess, -1);

so i get a wrong result.  
is there any problem in my code in the index of the matrix result C?

---

<div class="post-metadata">

**Author:** ![raikkonnen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@raikkonnen](https://forums.developer.nvidia.com/u/raikkonnen)\
**Post date:** [October 20, 2010, 1:26pm UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/16 "2010-10-20T13:26:22Z")

</div>

I change it to threadIdx.x and Block\_size to 32;

and i execute it and it gives me an error ("LCS has triggered a breakpoint) in this stage (after the execution of the kernel):

LCS\<\<\< grid, threads \>\>\>(d\_C, d\_A, d\_B, WA, WB);  
CheckConditionXR\_(cudaGetLastError() == cudaSuccess, -1);

so i get a wrong result.  
is there any problem in my code in the index of the matrix result C?

---

<div class="post-metadata">

**Author:** ![insmvb00](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@insmvb00](https://forums.developer.nvidia.com/u/insmvb00)\
**Post date:** [October 20, 2010, 1:47pm UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/17 "2010-10-20T13:47:51Z")

</div>

As i said before, this code ‘A[current\_Index-1]==B[current\_Index-1]’ when current\_index = 0 (thread #0 in block #0) is attempting to  
access a A[-1] and B[-1]. This is an error. The range of current\_index is [0,(blockDim.x\*gridDim.x-1)], is not? Then, the correct access would be A[current\_index] and B[current\_index].

More, if you have changed the size of blocks (16 to 32), you have to change the size of arrays and its initialization too.

---

<div class="post-metadata">

**Author:** ![insmvb00](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@insmvb00](https://forums.developer.nvidia.com/u/insmvb00)\
**Post date:** [October 20, 2010, 1:47pm UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/18 "2010-10-20T13:47:51Z")

</div>

As i said before, this code ‘A[current\_Index-1]==B[current\_Index-1]’ when current\_index = 0 (thread #0 in block #0) is attempting to  
access a A[-1] and B[-1]. This is an error. The range of current\_index is [0,(blockDim.x\*gridDim.x-1)], is not? Then, the correct access would be A[current\_index] and B[current\_index].

More, if you have changed the size of blocks (16 to 32), you have to change the size of arrays and its initialization too.

---

<div class="post-metadata">

**Author:** ![raikkonnen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@raikkonnen](https://forums.developer.nvidia.com/u/raikkonnen)\
**Post date:** [October 20, 2010, 2:16pm UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/19 "2010-10-20T14:16:16Z")

</div>

yes i changed it to A[current\_Index] the same thing in B

the size of the arrays A and B are:

wA= 2\*Block\_size //dimension of the array of char A  
wB= bloch\_size // dimension of the array of char B

my problem is that i want to fill the matrix result anti-diagonal by anti-diagonal because in my algorithm every cell C[i][j] depends on the cell C[i-1][j-1] , C[i-1][j] and C[i][j-1]  
so we can fill one anti-diagonal in the same time (in parallel)

is there any change in the index to realize this parallelism?

---

<div class="post-metadata">

**Author:** ![raikkonnen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@raikkonnen](https://forums.developer.nvidia.com/u/raikkonnen)\
**Post date:** [October 20, 2010, 2:16pm UTC](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269/20 "2010-10-20T14:16:16Z")

</div>

yes i changed it to A[current\_Index] the same thing in B

the size of the arrays A and B are:

wA= 2\*Block\_size //dimension of the array of char A  
wB= bloch\_size // dimension of the array of char B

my problem is that i want to fill the matrix result anti-diagonal by anti-diagonal because in my algorithm every cell C[i][j] depends on the cell C[i-1][j-1] , C[i-1][j] and C[i][j-1]  
so we can fill one anti-diagonal in the same time (in parallel)

is there any change in the index to realize this parallelism?

[Next page](https://forums.developer.nvidia.com/t/char-array-my-mistake/19269.md?page=2)
