# Double precision in CUDA 2.3

**URL:** <https://forums.developer.nvidia.com/t/double-precision-in-cuda-2-3/11514>\
**Category:** CUDA Programming and Performance\
**Created:** [August 11, 2009, 5:59pm UTC](https://forums.developer.nvidia.com/t/double-precision-in-cuda-2-3/11514 "2009-08-11T17:59:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fivos\_rg](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@fivos\_rg](https://forums.developer.nvidia.com/u/fivos_rg)\
**Post date:** [August 11, 2009, 5:59pm UTC](https://forums.developer.nvidia.com/t/double-precision-in-cuda-2-3/11514/1 "2009-08-11T17:59:51Z")

</div>

Hi to everyone,

I am new to CUDA computing and I would like to ask a question the more experienced members about double precision in the CUDA 2.3 environment. First of all I have a GTX295 card which, according to the CUDA programming guide, supports compute capability 1.3, meaning that it supports double precision numbers. Also I am using the nvcc compiler through the Microsoft developer studio 2008 (actually the code is built using a one of the SDK sample programs as a template).

So I was experiencing with a simple code, which can be found here ( [url=“[My first CUDA program! | /// Parallel Panorama ///](http://llpanorama.wordpress.com/2008/05/21/my-first-cuda-program/)”][http://llpanorama.wordpress.com/2008/05/21...t-cuda-program/[/url]](http://llpanorama.wordpress.com/2008/05/21...t-cuda-program/%5B/url%5D) ), I have modified it a bit as following (in short I have changed the way the matrix a\_h is allocated and the array elements that have to be calculated):

#include \<stdio.h\>  
#include \<cuda.h\>

// Kernel that executes on the CUDA device  
**global** void square\_array(float \*a, int N)  
{  
int idx = blockIdx.x \* blockDim.x + threadIdx.x;  
a[idx] = a[idx] \* a[idx];  
}

// main routine that executes on the host  
int main(void)  
{

float \*a\_h=new float 60;  
float \*a\_d; // Pointer to host & device arrays  
const int N = 60; // Number of elements in arrays  
size\_t size = N \* sizeof(float);  
// a\_h = (float _)malloc(size); // Allocate array on host  
cudaMalloc((void \*\*) &a\_d, size); // Allocate array on device  
// Initialize host array and copy it to CUDA device  
for (int i=0; i\<N; i++) a\_h[i] = (float)i_2;  
cudaMemcpy(a\_d, a\_h, size, cudaMemcpyHostToDevice);  
// Do calculation on device:  
int nblocksize=500;  
int Nblocks=N/nblocksize+ (N%nblocksize == 0 ? 0 : 1);  
square\_array \<\<\< Nblocks, nblocksize \>\>\> (a\_d, N);  
// Retrieve result from device and store it in host array  
cudaMemcpy(a\_h, a\_d, size, cudaMemcpyDeviceToHost);  
// Print results  
for (int i=0; i\<N; i++) printf(“%d %f\n”, i, a\_h[i]);  
// Cleanup  
delete a\_h; cudaFree(a\_d);  
}

So when I run the executable the program works as it should since it gives the correct results. But when I replace all float declarations to double, then I get a warning :

warning : Double is not supported. Demoting to float

Also the results are wrong (to be precise it is like the array elements were never processed by the device, since what is printed is just the elements intialized by the host). Why do I get the warning and the error results ? Do I miss anything here?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![tmurray](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tmurray](https://forums.developer.nvidia.com/u/tmurray)\
**Post date:** [August 11, 2009, 6:25pm UTC](https://forums.developer.nvidia.com/t/double-precision-in-cuda-2-3/11514/2 "2009-08-11T18:25:04Z")

</div>

compile with -arch sm\_13

---

<div class="post-metadata">

**Author:** ![fivos\_rg](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@fivos\_rg](https://forums.developer.nvidia.com/u/fivos_rg)\
**Post date:** [August 11, 2009, 6:46pm UTC](https://forums.developer.nvidia.com/t/double-precision-in-cuda-2-3/11514/3 "2009-08-11T18:46:37Z")

</div>

> [@](#):
>
> compile with -arch sm\_13

You are absolutely right I forgot to enable 1.3 compute architecture from the compiler. Thank you very much for your quick response!

[External Media](http://hqnveipbwb20/public/style_emoticons/<#EMO_DIR#>/shifty.gif "Media hosted on another site. Click to open in a new tab.")

---

<div class="post-metadata">

**Author:** ![Hooiberg](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Hooiberg](https://forums.developer.nvidia.com/u/Hooiberg)\
**Post date:** [February 11, 2010, 7:46pm UTC](https://forums.developer.nvidia.com/t/double-precision-in-cuda-2-3/11514/4 "2010-02-11T19:46:26Z")

</div>

I get the same error.

I specifically bought a device with support for doubles, a GTX 260

In my common.mk, the versions are included:

```auto
# Add new SM Versions here as devices with new Compute Capability are released

SM_VERSIONS := sm_10 sm_11 sm_12 sm_13

```

Is this perhaps a different reference to the versions?

---

<div class="post-metadata">

**Author:** ![popleaf1](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@popleaf1](https://forums.developer.nvidia.com/u/popleaf1)\
**Post date:** [March 5, 2010, 9:03am UTC](https://forums.developer.nvidia.com/t/double-precision-in-cuda-2-3/11514/5 "2010-03-05T09:03:43Z")

</div>

there is a clear error in your **global** fuction.

**global** void square\_array(float \*a, int N)  
{  
int idx = blockIdx.x \* blockDim.x + threadIdx.x;  
a[idx] = a[idx] \* a[idx];  
}

the 2th para is not used, so if the number of thread is not equal to N, your result is of course error.

---

<div class="post-metadata">

**Author:** ![popleaf1](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@popleaf1](https://forums.developer.nvidia.com/u/popleaf1)\
**Post date:** [March 5, 2010, 9:14am UTC](https://forums.developer.nvidia.com/t/double-precision-in-cuda-2-3/11514/6 "2010-03-05T09:14:06Z")

</div>

#include \<stdio.h\>  
#include \<cuda.h\>

// Kernel that executes on the CUDA device  
**global** void square\_array(float \*a, int N)  
{  
int idx = blockIdx.x \* blockDim.x + threadIdx.x;

```
float temp = 0.0f;
for(int i = idx; i < N; i += blockDim.x * gridDim.x)
{
	temp = a[idx];
	a[idx] = temp * temp;
}

```

}

// main routine that executes on the host  
int main(void)  
{  
const int N = 21; // Number of elements in arrays  
float \*a\_h=new float[N];  
float \*a\_d; // Pointer to host & device arrays  
size\_t size = N \* sizeof(float);

```
// a_h = (float *)malloc(size); // Allocate array on host
cudaMalloc((void **) &a_d, size); // Allocate array on device
// Initialize host array and copy it to CUDA device
for (int i=0; i<N; i++)
    a_h[i] = (float)i*2;
cudaMemcpy(a_d, a_h, size, cudaMemcpyHostToDevice);
// Do calculation on device:
int nblocksize=512; //I edit here. or 512
int Nblocks=(N - 1)/nblocksize + 1; //I also edit here.
square_array <<< Nblocks, nblocksize >>> (a_d, N);
// Retrieve result from device and store it in host array
cudaMemcpy(a_h, a_d, size, cudaMemcpyDeviceToHost);
// Print results
for (int i=0; i<N; i++)
    printf("%d %f\n", i, a_h[i]);
// Cleanup
delete [] a_h;
cudaFree(a_d);

```

}
