# What is double buffering?

**URL:** <https://forums.developer.nvidia.com/t/what-is-double-buffering/13531>\
**Category:** CUDA Programming and Performance\
**Created:** [November 30, 2009, 6:49am UTC](https://forums.developer.nvidia.com/t/what-is-double-buffering/13531 "2009-11-30T06:49:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sonia](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Sonia](https://forums.developer.nvidia.com/u/Sonia)\
**Post date:** [November 30, 2009, 6:49am UTC](https://forums.developer.nvidia.com/t/what-is-double-buffering/13531/1 "2009-11-30T06:49:13Z")

</div>

Hi,

Can anybody tell me what is double buffering in CUDA or in general and why do we use it?

Thanks

---

<div class="post-metadata">

**Author:** ![Sarnath](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/sarnath/32/9742_2.png) [@Sarnath](https://forums.developer.nvidia.com/u/Sarnath)\
**Post date:** [November 30, 2009, 7:23am UTC](https://forums.developer.nvidia.com/t/what-is-double-buffering/13531/2 "2009-11-30T07:23:37Z")

</div>

Say you want to convert values in a BUFFER to another…(say Farenheit to Centigrade) …each thread works on an element… and would convert the same… No problem…

However, say, if each thread has to compare itself to its left and right successors and update the maximum or average or in gernaral a function of all three — then, you have race condition…

Thats when you use double buffering… ALl threads “read” from one buffer and update the other buffer…and so on…

Usually doub buff happens in loop… like BufferA is read, BufferB is written, Syncthreads, BufferB is read, BufferA is written, Syncthreads,… and so on

---

<div class="post-metadata">

**Author:** ![Sonia](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Sonia](https://forums.developer.nvidia.com/u/Sonia)\
**Post date:** [November 30, 2009, 7:42am UTC](https://forums.developer.nvidia.com/t/what-is-double-buffering/13531/3 "2009-11-30T07:42:41Z")

</div>

> [@](#):
>
> Say you want to convert values in a BUFFER to another…(say Farenheit to Centigrade) …each thread works on an element… and would convert the same… No problem…
> 
> However, say, if each thread has to compare itself to its left and right successors and update the maximum or average or in gernaral a function of all three — then, you have race condition…
> 
> Thats when you use double buffering… ALl threads “read” from one buffer and update the other buffer…and so on…
> 
> Usually doub buff happens in loop… like BufferA is read, BufferB is written, Syncthreads, BufferB is read, BufferA is written, Syncthreads,… and so on

Thanks Sarnath for your quick reply!

Well, the following implements a double-buffered version of the sum scan

```auto
for d := 1 to log2n do

	forall k in parallel do

		  if k â‰¥ 2d then

			   x[out][k] := x[in][k âˆ’ 2d-1] + x[in][k]

		  else

			   x[out][k] := x[in][k]

	swap(in,out)

```

I am not able to find where is BUFFER ‘A’ and where is BUFFER ‘B’ in the above code.

The corresponding CUDA code is:

```auto
__global__ void scan(float *g_odata, float *g_idata, int n)

{

	extern __shared__ float temp[]; // allocated on invocation

	int thid = threadIdx.x;

	int pout = 0, pin = 1;

	// load input into shared memory.

	// This is exclusive scan, so shift right by one and set first elt to 0

	temp[pout*n + thid] = (thid > 0) ? g_idata[thid-1] : 0;

	__syncthreads();

	for (int offset = 1; offset < n; offset *= 2)

	{

		pout = 1 - pout; // swap double buffer indices

		pin = 1 - pout;

		if (thid >= offset)

			 temp[pout*n+thid] += temp[pin*n+thid - offset];

		else

			 temp[pout*n+thid] = temp[pin*n+thid];

		__syncthreads();

	}

	g_odata[thid] = temp[pout*n+thid1]; // write output

}

```

---

<div class="post-metadata">

**Author:** ![Sarnath](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/sarnath/32/9742_2.png) [@Sarnath](https://forums.developer.nvidia.com/u/Sarnath)\
**Post date:** [November 30, 2009, 7:52am UTC](https://forums.developer.nvidia.com/t/what-is-double-buffering/13531/4 "2009-11-30T07:52:15Z")

</div>

“in” and “out” are the 2 buffers… Note that the last statement in the first code box, comes under the OUTER Loop… Thats where the buffers are exchanged… Isnt it?

Although in and out are just indices, the way they are using in the code says that they operate at different dimensions of a multi-Dimensional array… Jus thnk about it

---

<div class="post-metadata">

**Author:** ![Sonia](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Sonia](https://forums.developer.nvidia.com/u/Sonia)\
**Post date:** [November 30, 2009, 8:25am UTC](https://forums.developer.nvidia.com/t/what-is-double-buffering/13531/5 "2009-11-30T08:25:31Z")

</div>

> [@](#):
>
> Although in and out are just indices, the way they are using in the code says that they operate at different dimensions of a multi-Dimensional array… Jus thnk about it

“in” and “out” are always the first dimension x, so I simply did not get how they are operating at different dimensions :(

---

<div class="post-metadata">

**Author:** ![Sarnath](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/sarnath/32/9742_2.png) [@Sarnath](https://forums.developer.nvidia.com/u/Sarnath)\
**Post date:** [November 30, 2009, 8:56am UTC](https://forums.developer.nvidia.com/t/what-is-double-buffering/13531/6 "2009-11-30T08:56:56Z")

</div>

Even if it in the same dimension, they are different buffers, isn’t it?

The code reads one buffer and updates another buffer… If there was no another buffer, there would be lot of race… isnt it?
