I am new to CUDA and working on the first exercise which is vector addition
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
// Compute vector sum C = A+B
//CUDA kernel. Each thread performes one pair-wise addition
__global__ void vecAddKernel(float *A, float *B, float *C, int n)
{
//Get our global thread ID
int i = blockDim.x*blockIdx.x+threadIdx.x;
if (i<n) C[i] = A[i] + B[i];
}
int main(int argc, char* argv[])
{
//Size of vectors
int n = 100000;
int size = n * sizeof(float);
//Host input vectors
float *h_A, *h_B;
//Host output vector
float *h_C;
//Device input vectors
float *d_A, *d_B;
//Device output vector
float *d_C;
//Allocate memory for each vector on host
h_A = (float*)malloc(sizeof(size));
h_B = (float*)malloc(sizeof(size));
h_C = (float*)malloc(sizeof(size));
//Allocate memory for each vector on GPU
cudaMalloc( (void **) &d_A, size);
cudaMalloc( (void **) &d_B, size);
cudaMalloc( (void **) &d_C, size);
//Copy host vectors to device
cudaMemcpy(d_A, h_A, size, cudaMemcpyHostToDevice);
cudaMemcpy(d_B, h_B, size, cudaMemcpyHostToDevice);
int blockSize, gridSize;
//Number of threads in each block
blockSize = 1024;//Execute the kernel
vecAddKernel<<<gridSize,blockSize>>>(d_A, d_B, d_C, n);
//Synchronize threads
cudaThreadSynchronize();
//Copy array back to host
cudaMemcpy( h_C, d_C, size, cudaMemcpyDeviceToHost );
//Release device memory
cudaFree(d_A);
cudaFree(d_B);
cudaFree(d_C);
//Release host memory
free(h_A);
free(h_B);
free(h_C);
return 0;
}
The compilation was succeeded, but while running the code I get: `Segmentation fault (core dumped). I do not see where the issue is. I've tried to use nvprof, but it's not helpful in any fashion. Can anyone help me to figure out where I made a mistake?