Error: Break in Vector Addition program Program hanging

Hello,

I wrote the following program to add two vectors a_h and b_h and store the result in c_h after computation in emulation mode. I am using Visual Studio 2005 and running the program in emulation mode. The compilation do not

show any error. However, when the program executes, it stops responding after accepting the inputs from the user (i.e after printf("enter

second vector"); for(i=0;i<10;i++) { scanf("%f",&b_h[i]); } ). After this nothing happens and the program hangs (please see the attached image of the output). Ideally it should display the content of c (i.e. the sum of a and b ).Moreover sometime I even get an error message while running the program saying that there is a break in the program or a new window opens displaying the content of free() that I have used in the program. Please see the various out puts shown below.

Why is this happening even though the compilar is not showing any error?I would be thankful to you for your valuable information in this regard.

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <cuda.h>

#include <cutil.h>

#include <cuda_runtime.h>

#include <malloc.h>

__global__ void vecadd(float*a, float*b, float*c)

{

int i=threadIdx.x;

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

}

int main()

{

float *a_h,*b_h,*b_d,*a_d,*c_h,*c_d;

int i;

const int N=10000;

size_t size= N*sizeof(float);

a_h=(float*)malloc(size);

if(a_h==NULL)

{

printf("Memory allocation failed");

}

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

b_h=(float*)malloc(size);

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

printf("enter first vector");

for(i=0;i<10;i++)

{

scanf("%f",&a_h[i]);

}

printf("enter second vector");

for(i=0;i<10;i++)

{

scanf("%f",&b_h[i]);

}

cudaMemcpy(a_d,a_h,size,cudaMemcpyHostToDevice);

cudaMemcpy(b_d,b_h,size,cudaMemcpyHostToDevice);

vecadd<<<1,10>>>(a_d,b_d,c_d);

cudaMemcpy(c_d,c_h,size,cudaMemcpyHostToDevice);

for(i=0;i<10;i++)

printf("%f\n",c_h[i]);

getch();

}

OUTPUT

============================================================

========

COMPILATION OUTPUT

1>------ Build started: Project: akanksha_vecadd_20july09, Configuration: EmuDebug Win32 ------

1>Compiling…

1>sample.cu

1>c:/Documents and Settings/Administrator/My Documents/Visual Studio 2005/Projects/akanksha_vecadd_20july09/akanksha_vecadd_20july09//sample.cu(51): warning: function “scanf”

1>C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\stdio.h(295): here was declared “deprecated (“This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.”)”

1>c:/Documents and Settings/Administrator/My Documents/Visual Studio 2005/Projects/akanksha_vecadd_20july09/akanksha_vecadd_20july09//sample.cu(59): warning: function “scanf”

1>C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\stdio.h(295): here was declared “deprecated (“This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.”)”

1>c:/Documents and Settings/Administrator/My Documents/Visual Studio 2005/Projects/akanksha_vecadd_20july09/akanksha_vecadd_20july09//sample.cu(67): warning: variable “c_d” is used before its value is set

1>c:/Documents and Settings/Administrator/My Documents/Visual Studio 2005/Projects/akanksha_vecadd_20july09/akanksha_vecadd_20july09//sample.cu(70): warning: variable “c_h” is used before its value is set

1>c:/Documents and Settings/Administrator/My Documents/Visual Studio 2005/Projects/akanksha_vecadd_20july09/akanksha_vecadd_20july09//sample.cu(83): warning: function “getch”

1>C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\conio.h(145): here was declared “deprecated (“The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.”)”

1>tmpxft_000001f4_00000000-3_sample.cudafe1.c

1>tmpxft_000001f4_00000000-7_sample.i

1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\akanksha_vecadd_20july09\akanksha_vecadd_20july09\sample.cu(67) : warning C4700: uninitialized local variable ‘c_d’ used

1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\akanksha_vecadd_20july09\akanksha_vecadd_20july09\sample.cu(70) : warning C4700: uninitialized local variable ‘c_h’ used

1>Build log was saved at “file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\akanksha_vecadd_20july09\akanksha_vecadd_20july09\EmuDebug\BuildLog.htm”

1>akanksha_vecadd_20july09 - 0 error(s), 7 warning(s)

========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

============================================================

=========

OUTPUT FROM DEBUG

‘akanksha_vecadd_20july09.exe’: Loaded ‘C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\akanksha_vecadd_20july09\emudebug\akanksha_vecadd_20july09.exe’, Symbols loaded.

‘akanksha_vecadd_20july09.exe’: Loaded ‘C:\WINDOWS\system32\ntdll.dll’, No symbols loaded.

‘akanksha_vecadd_20july09.exe’: Loaded ‘C:\WINDOWS\system32\kernel32.dll’, No symbols loaded.

‘akanksha_vecadd_20july09.exe’: Loaded ‘C:\CUDA\bin\cudart.dll’, Binary was not built with debug information.

The thread ‘Win32 Thread’ (0x8f0) has exited with code -1073741510 (0xc000013a).

The thread ‘Win32 Thread’ (0x8f4) has exited with code -1073741510 (0xc000013a).

The thread ‘Win32 Thread’ (0x920) has exited with code -1073741510 (0xc000013a).

The thread ‘Win32 Thread’ (0x91c) has exited with code -1073741510 (0xc000013a).

The thread ‘Win32 Thread’ (0x930) has exited with code -1073741510 (0xc000013a).

The thread ‘Win32 Thread’ (0x934) has exited with code -1073741510 (0xc000013a).

The thread ‘Win32 Thread’ (0x8d8) has exited with code -1073741510 (0xc000013a).

The thread ‘Win32 Thread’ (0x8f8) has exited with code -1073741510 (0xc000013a).

The thread ‘Win32 Thread’ (0x900) has exited with code -1073741510 (0xc000013a).

The thread ‘Win32 Thread’ (0x8fc) has exited with code -1073741510 (0xc000013a).

The thread ‘Win32 Thread’ (0x918) has exited with code -1073741510 (0xc000013a).

The program ‘[2260] akanksha_vecadd_20july09.exe: Native’ has exited with code -1073741510 (0xc000013a).
vectAddOUTPUT.bmp (774 KB)

You forgot to allocate both host and device storage for the c-vector + you probably want to use cudaMemcpy(c_h,c_d,size,cudaMemcpyDeviceToHost) instead.

N.

Thanks Nico for the help! :)