# Cuda for loop parallezation

**URL:** <https://forums.developer.nvidia.com/t/cuda-for-loop-parallezation/127660>\
**Category:** CUDA Programming and Performance\
**Created:** [June 13, 2020, 5:53pm UTC](https://forums.developer.nvidia.com/t/cuda-for-loop-parallezation/127660 "2020-06-13T17:53:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![skymoon.raider](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@skymoon.raider](https://forums.developer.nvidia.com/u/skymoon.raider)\
**Post date:** [June 13, 2020, 5:53pm UTC](https://forums.developer.nvidia.com/t/cuda-for-loop-parallezation/127660/1 "2020-06-13T17:53:51Z")

</div>

i want to parallelize two for loop with using cuda. My simple c++ code is:

```auto
index=0;
	for (int m = 0; m < N; m++)
	{
		x[m] = (2 * m + 1) / (2 * N);
		for (int n = 0; n < N; n++)
		{
			Z1[n] = (n + 1) + x[m];
			Z[index] = Z1[n];
			index++;
		}
	}

```

i write cuda version is that:

```auto
    int tidx = blockIdx.x*blockDim.x + threadIdx.x;
    int tidy = blockIdx.y*blockDim.y + threadIdx.y;
    if (tidx < N && tidy < N)
    {
        x[tidx] = (2 * tidx + 1) / (2 * N);
        Z1[tidy] = (tidy + 1) * 100 + x[tidx];
    }
Z[tidx]=Z1[tidy];
}

```

When i run code. the first row of Z is correct and the others are equal to zerro. Z dimension is [nxn]. i dont understand where is the mistake? Can you help me thanks…

---

<div class="post-metadata">

**Author:** ![k6j1h9](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@k6j1h9](https://forums.developer.nvidia.com/u/k6j1h9)\
**Post date:** [June 15, 2020, 11:42am UTC](https://forums.developer.nvidia.com/t/cuda-for-loop-parallezation/127660/2 "2020-06-15T11:42:12Z")

</div>

Hi skymoon  
As in you code, variable “tidx” was been never navigating any of Y axis block or grid. See your first cuda code.  
Versa case, “tidy”, is also as on.  
And, There was not guaranteed Z[tidx] is must to refering of progressed Z1[tidy].  
Hope it goes well
