# System just halts for simple CUDA program

**URL:** <https://forums.developer.nvidia.com/t/system-just-halts-for-simple-cuda-program/18623>\
**Category:** CUDA Programming and Performance\
**Created:** [September 4, 2010, 5:13pm UTC](https://forums.developer.nvidia.com/t/system-just-halts-for-simple-cuda-program/18623 "2010-09-04T17:13:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chhenning\_air](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@chhenning\_air](https://forums.developer.nvidia.com/u/chhenning_air)\
**Post date:** [September 4, 2010, 5:13pm UTC](https://forums.developer.nvidia.com/t/system-just-halts-for-simple-cuda-program/18623/1 "2010-09-04T17:13:29Z")

</div>

Hi there, the following code brings my Dell Precision 7500 Box ( with a Tesla C2050 ) to a complete halt. The only thing left I can do is to press to power button to turn off the system. I cannot see what is wrong with this code. Despite the fact it’s the worst implementation of a simple convolution algorithm. It only runs one thread total. Here is the code:

[codebox]

#include

#include

#include

#include \<cuda.h\>

const unsigned int SRC\_N = 10000;

const unsigned int DST\_N = 2 \* SRC\_N - 1;

**global** void convolve\_gpu\_simple( float\* src\_1

```
                               , float* src_2

                               , float* dst

                               )

```

{

```
for( int i = 0; i < SRC_N; ++i )

{

    for( int j = 0; j < SRC_N; ++j )

    {

        dst[i + j] += src_1[i] * src_2[j];

    }

}

```

}

void check\_for\_error( cudaError\_t err )

{

```
if( err != cudaSuccess )

{

    const char* error_msg = cudaGetErrorString( err );

    exit( 1 );

}

```

}

void convolve\_cpu( float\* a, float\* b, float\* c )

{

```
for( int i = 0; i < SRC_N; ++i )

{

    for( int j = 0; j < SRC_N; ++j )

    {

        c[i + j] += a[i] * b[j];

    }

}

```

}

bool compare\_results( float\* a, float\* b )

{

```
unsigned int m = 0;

```

for( int i = 0; i \< DST\_N; ++i )

```
{

    int v_a = *(( unsigned int* )&a[i]);

    int v_b = *(( unsigned int* )&b[i]);

```

int ulp = abs( v\_a - v\_b );

```
    m = max( m, ulp );

```

if( ulp \> 5 )

```
        return false;

}

```

return true;

}

using namespace std;

int main(int argc, char\*\* argv)

{

```
srand( 0 );

```

float \*src\_1, \*src\_2, \*dst\_cpu, \*dst\_gpu;

```
float *dev_src_1, *dev_src_2, *dev_dst;

```

src\_1 = new float[SRC\_N];

```
src_2 = new float[SRC_N];

dst_cpu = new float[DST_N];

dst_gpu = new float[DST_N];

```

check\_for\_error( cudaMalloc( &dev\_src\_1, SRC\_N \* sizeof( float )));

```
check_for_error( cudaMalloc( &dev_src_2, SRC_N * sizeof( float )));

check_for_error( cudaMalloc( &dev_dst , DST_N * sizeof( float )));

```

fill( src\_1, src\_1 + SRC\_N, 1.0f );

```
fill( src_2, src_2 + SRC_N, 1.0f );

```

fill( dst\_cpu, dst\_cpu + DST\_N, 0.0f );

```
fill( dst_gpu, dst_gpu + DST_N, 0.0f );

```

convolve\_cpu( src\_1, src\_2, dst\_cpu );

check\_for\_error( cudaMemcpy( dev\_src\_1, src\_1, SRC\_N \* sizeof( float ), cudaMemcpyHostToDevice ));

```
check_for_error( cudaMemcpy( dev_src_2, src_2, SRC_N * sizeof( float ), cudaMemcpyHostToDevice ));

```

convolve\_gpu\_simple\<\<\< 1, 1 \>\>\>( dev\_src\_1, dev\_src\_2, dev\_dst );

```
check_for_error( cudaGetLastError() );

```

check\_for\_error( cudaMemcpy( dst\_gpu, dev\_dst, DST\_N \* sizeof( float ), cudaMemcpyDeviceToHost ));

assert( compare\_results( dst\_cpu, dst\_gpu ));

cudaFree( dev\_src\_1 );

```
cudaFree( dev_src_2 );

cudaFree( dev_dst );

```

delete src\_1;

```
delete[] src_2;

delete[] dst_cpu;

delete[] dst_gpu;

```

return 0;

}[/codebox]

Any help is very much appreciated. I’m using VS2008 on a Windows 7 x64 box. The NVidia driver is 258.96 and I use CUDA 3.1.

Thanks,

Christian

---

<div class="post-metadata">

**Author:** ![chhenning\_air](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@chhenning\_air](https://forums.developer.nvidia.com/u/chhenning_air)\
**Post date:** [September 9, 2010, 3:05pm UTC](https://forums.developer.nvidia.com/t/system-just-halts-for-simple-cuda-program/18623/2 "2010-09-09T15:05:30Z")

</div>

Hi there,

is there anyone who can tell me what the problem might be? This is kinda important for me and my company.

Thanks,

Christian

> [@](#):
>
> Hi there, the following code brings my Dell Precision 7500 Box ( with a Tesla C2050 ) to a complete halt. The only thing left I can do is to press to power button to turn off the system. I cannot see what is wrong with this code. Despite the fact it’s the worst implementation of a simple convolution algorithm. It only runs one thread total. Here is the code:
> 
> [codebox]
> 
> #include
> 
> #include
> 
> #include
> 
> #include \<cuda.h\>
> 
> const unsigned int SRC\_N = 10000;
> 
> const unsigned int DST\_N = 2 \* SRC\_N - 1;
> 
> **global** void convolve\_gpu\_simple( float\* src\_1
> 
> ```
> , float* src_2
> 
> , float* dst
> 
> )
> 
> ```
> 
> {
> 
> ```
> for( int i = 0; i < SRC_N; ++i )
> 
> {
> 
> for( int j = 0; j < SRC_N; ++j )
> 
> {
> 
> dst[i + j] += src_1[i] * src_2[j];
> 
> }
> 
> }
> 
> ```
> 
> }
> 
> void check\_for\_error( cudaError\_t err )
> 
> {
> 
> ```
> if( err != cudaSuccess )
> 
> {
> 
> const char* error_msg = cudaGetErrorString( err );
> 
> exit( 1 );
> 
> }
> 
> ```
> 
> }
> 
> void convolve\_cpu( float\* a, float\* b, float\* c )
> 
> {
> 
> ```
> for( int i = 0; i < SRC_N; ++i )
> 
> {
> 
> for( int j = 0; j < SRC_N; ++j )
> 
> {
> 
> c[i + j] += a[i] * b[j];
> 
> }
> 
> }
> 
> ```
> 
> }
> 
> bool compare\_results( float\* a, float\* b )
> 
> {
> 
> ```
> unsigned int m = 0;
> 
> ```
> 
> for( int i = 0; i \< DST\_N; ++i )
> 
> ```
> {
> 
> int v_a = *(( unsigned int* )&a[i]);
> 
> int v_b = *(( unsigned int* )&b[i]);
> 
> ```
> 
> int ulp = abs( v\_a - v\_b );
> 
> ```
> m = max( m, ulp );
> 
> ```
> 
> if( ulp \> 5 )
> 
> ```
> return false;
> 
> }
> 
> ```
> 
> return true;
> 
> }
> 
> using namespace std;
> 
> int main(int argc, char\*\* argv)
> 
> {
> 
> ```
> srand( 0 );
> 
> ```
> 
> float \*src\_1, \*src\_2, \*dst\_cpu, \*dst\_gpu;
> 
> ```
> float *dev_src_1, *dev_src_2, *dev_dst;
> 
> ```
> 
> src\_1 = new float[SRC\_N];
> 
> ```
> src_2 = new float[SRC_N];
> 
> dst_cpu = new float[DST_N];
> 
> dst_gpu = new float[DST_N];
> 
> ```
> 
> check\_for\_error( cudaMalloc( &dev\_src\_1, SRC\_N \* sizeof( float )));
> 
> ```
> check_for_error( cudaMalloc( &dev_src_2, SRC_N * sizeof( float )));
> 
> check_for_error( cudaMalloc( &dev_dst , DST_N * sizeof( float )));
> 
> ```
> 
> fill( src\_1, src\_1 + SRC\_N, 1.0f );
> 
> ```
> fill( src_2, src_2 + SRC_N, 1.0f );
> 
> ```
> 
> fill( dst\_cpu, dst\_cpu + DST\_N, 0.0f );
> 
> ```
> fill( dst_gpu, dst_gpu + DST_N, 0.0f );
> 
> ```
> 
> convolve\_cpu( src\_1, src\_2, dst\_cpu );
> 
> check\_for\_error( cudaMemcpy( dev\_src\_1, src\_1, SRC\_N \* sizeof( float ), cudaMemcpyHostToDevice ));
> 
> ```
> check_for_error( cudaMemcpy( dev_src_2, src_2, SRC_N * sizeof( float ), cudaMemcpyHostToDevice ));
> 
> ```
> 
> convolve\_gpu\_simple\<\<\< 1, 1 \>\>\>( dev\_src\_1, dev\_src\_2, dev\_dst );
> 
> ```
> check_for_error( cudaGetLastError() );
> 
> ```
> 
> check\_for\_error( cudaMemcpy( dst\_gpu, dev\_dst, DST\_N \* sizeof( float ), cudaMemcpyDeviceToHost ));
> 
> assert( compare\_results( dst\_cpu, dst\_gpu ));
> 
> cudaFree( dev\_src\_1 );
> 
> ```
> cudaFree( dev_src_2 );
> 
> cudaFree( dev_dst );
> 
> ```
> 
> delete src\_1;
> 
> ```
> delete[] src_2;
> 
> delete[] dst_cpu;
> 
> delete[] dst_gpu;
> 
> ```
> 
> return 0;
> 
> }[/codebox]
> 
> Any help is very much appreciated. I’m using VS2008 on a Windows 7 x64 box. The NVidia driver is 258.96 and I use CUDA 3.1.
> 
> Thanks,
> 
> Christian

---

<div class="post-metadata">

**Author:** ![chhenning\_air](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@chhenning\_air](https://forums.developer.nvidia.com/u/chhenning_air)\
**Post date:** [September 9, 2010, 3:05pm UTC](https://forums.developer.nvidia.com/t/system-just-halts-for-simple-cuda-program/18623/3 "2010-09-09T15:05:30Z")

</div>

Hi there,

is there anyone who can tell me what the problem might be? This is kinda important for me and my company.

Thanks,

Christian

> [@](#):
>
> Hi there, the following code brings my Dell Precision 7500 Box ( with a Tesla C2050 ) to a complete halt. The only thing left I can do is to press to power button to turn off the system. I cannot see what is wrong with this code. Despite the fact it’s the worst implementation of a simple convolution algorithm. It only runs one thread total. Here is the code:
> 
> [codebox]
> 
> #include
> 
> #include
> 
> #include
> 
> #include \<cuda.h\>
> 
> const unsigned int SRC\_N = 10000;
> 
> const unsigned int DST\_N = 2 \* SRC\_N - 1;
> 
> **global** void convolve\_gpu\_simple( float\* src\_1
> 
> ```
> , float* src_2
> 
> , float* dst
> 
> )
> 
> ```
> 
> {
> 
> ```
> for( int i = 0; i < SRC_N; ++i )
> 
> {
> 
> for( int j = 0; j < SRC_N; ++j )
> 
> {
> 
> dst[i + j] += src_1[i] * src_2[j];
> 
> }
> 
> }
> 
> ```
> 
> }
> 
> void check\_for\_error( cudaError\_t err )
> 
> {
> 
> ```
> if( err != cudaSuccess )
> 
> {
> 
> const char* error_msg = cudaGetErrorString( err );
> 
> exit( 1 );
> 
> }
> 
> ```
> 
> }
> 
> void convolve\_cpu( float\* a, float\* b, float\* c )
> 
> {
> 
> ```
> for( int i = 0; i < SRC_N; ++i )
> 
> {
> 
> for( int j = 0; j < SRC_N; ++j )
> 
> {
> 
> c[i + j] += a[i] * b[j];
> 
> }
> 
> }
> 
> ```
> 
> }
> 
> bool compare\_results( float\* a, float\* b )
> 
> {
> 
> ```
> unsigned int m = 0;
> 
> ```
> 
> for( int i = 0; i \< DST\_N; ++i )
> 
> ```
> {
> 
> int v_a = *(( unsigned int* )&a[i]);
> 
> int v_b = *(( unsigned int* )&b[i]);
> 
> ```
> 
> int ulp = abs( v\_a - v\_b );
> 
> ```
> m = max( m, ulp );
> 
> ```
> 
> if( ulp \> 5 )
> 
> ```
> return false;
> 
> }
> 
> ```
> 
> return true;
> 
> }
> 
> using namespace std;
> 
> int main(int argc, char\*\* argv)
> 
> {
> 
> ```
> srand( 0 );
> 
> ```
> 
> float \*src\_1, \*src\_2, \*dst\_cpu, \*dst\_gpu;
> 
> ```
> float *dev_src_1, *dev_src_2, *dev_dst;
> 
> ```
> 
> src\_1 = new float[SRC\_N];
> 
> ```
> src_2 = new float[SRC_N];
> 
> dst_cpu = new float[DST_N];
> 
> dst_gpu = new float[DST_N];
> 
> ```
> 
> check\_for\_error( cudaMalloc( &dev\_src\_1, SRC\_N \* sizeof( float )));
> 
> ```
> check_for_error( cudaMalloc( &dev_src_2, SRC_N * sizeof( float )));
> 
> check_for_error( cudaMalloc( &dev_dst , DST_N * sizeof( float )));
> 
> ```
> 
> fill( src\_1, src\_1 + SRC\_N, 1.0f );
> 
> ```
> fill( src_2, src_2 + SRC_N, 1.0f );
> 
> ```
> 
> fill( dst\_cpu, dst\_cpu + DST\_N, 0.0f );
> 
> ```
> fill( dst_gpu, dst_gpu + DST_N, 0.0f );
> 
> ```
> 
> convolve\_cpu( src\_1, src\_2, dst\_cpu );
> 
> check\_for\_error( cudaMemcpy( dev\_src\_1, src\_1, SRC\_N \* sizeof( float ), cudaMemcpyHostToDevice ));
> 
> ```
> check_for_error( cudaMemcpy( dev_src_2, src_2, SRC_N * sizeof( float ), cudaMemcpyHostToDevice ));
> 
> ```
> 
> convolve\_gpu\_simple\<\<\< 1, 1 \>\>\>( dev\_src\_1, dev\_src\_2, dev\_dst );
> 
> ```
> check_for_error( cudaGetLastError() );
> 
> ```
> 
> check\_for\_error( cudaMemcpy( dst\_gpu, dev\_dst, DST\_N \* sizeof( float ), cudaMemcpyDeviceToHost ));
> 
> assert( compare\_results( dst\_cpu, dst\_gpu ));
> 
> cudaFree( dev\_src\_1 );
> 
> ```
> cudaFree( dev_src_2 );
> 
> cudaFree( dev_dst );
> 
> ```
> 
> delete src\_1;
> 
> ```
> delete[] src_2;
> 
> delete[] dst_cpu;
> 
> delete[] dst_gpu;
> 
> ```
> 
> return 0;
> 
> }[/codebox]
> 
> Any help is very much appreciated. I’m using VS2008 on a Windows 7 x64 box. The NVidia driver is 258.96 and I use CUDA 3.1.
> 
> Thanks,
> 
> Christian
