Hello…
Could anyone help me to compile and run this program (MatrixAdd.cu) successfully…!
#include<stdio.h>
#include<stdlib.h>
const int N = 1024;
const int blocksize = 16;
global void add_matrix(floata, float b, float* c, int N)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
int j = blockIdx.y * blockDim.y + threadIdx.y;
int index = i + j*N;
if ( i < N && j < N)
c[index]= a[index] + b[index];
}
int main()
{
float a = new float[NN];
float b = new float[NN];
float c = new float[NN];
for ( int i=0; i< N*N; ++i)
{
a[i] = 1.0f;
b[i]= 3.5f;
}
float *ad, bd, cd;
const int size = NNsizeof(float);
cudaMalloc((void**)&ad, size);
cudaMalloc((void**)&bd, size);
cudaMalloc((void**)&cd, size);
cudaMemcpy(ad, a, size, cudaMemcpyHostToDevice)
cudaMemcpy(bd, b, size, cudaMemcpyHostToDevice)
dim3 dimBlock(blocksize, blocksize);
dim3 dimGrid(N/dimBlock.x, N/dimBlock.y);
add_matrix<<<dimGrid, dimBlock>>>(ad, bd, cd, N);
cudaMemcpy(c, cd, size, cudaMemcpyDeviceToHost);
cudaFree(ad);
cudaFree(bd);
cudaFree(cd);
delete a;
delete b;
delete c;
return EXIT_SUCCESS;
}
AND I HAVE GOT THE FOLLOWING ERRORS:-
MatrixAdd.cu(9): error: this declaration has no storage class or type specifier
MatrixAdd.cu(9): error: expected a “;”
MatrixAdd.cu(46): warning: parsing restarts here after previous syntax error
MatrixAdd.cu(47): error: identifier “dimBlock” is undefined
MatrixAdd.cu(48): error: this declaration has no storage class or type specifier
MatrixAdd.cu(48): error: expected a “;”
MatrixAdd.cu(50): error: this declaration has no storage class or type specifier
MatrixAdd.cu(50): error: declaration is incompatible with “cudaError_t cudaMemcpy(void *, const void *, size_t, cudaMemcpyKind)”
/usr/local/cuda/bin/…/include/cuda_runtime_api.h(120): here
MatrixAdd.cu(50): error: identifier “c” is undefined
MatrixAdd.cu(50): error: expected a “)”
MatrixAdd.cu(52): error: this declaration has no storage class or type specifier
MatrixAdd.cu(52): error: declaration is incompatible with “cudaError_t cudaFree(void *)”
/usr/local/cuda/bin/…/include/cuda_runtime_api.h(106): here
MatrixAdd.cu(52): error: identifier “ad” is undefined
MatrixAdd.cu(53): error: this declaration has no storage class or type specifier
MatrixAdd.cu(53): error: variable “cudaFree” has already been defined
MatrixAdd.cu(53): error: identifier “bd” is undefined
MatrixAdd.cu(54): error: this declaration has no storage class or type specifier
MatrixAdd.cu(54): error: variable “cudaFree” has already been defined
MatrixAdd.cu(54): error: identifier “cd” is undefined
MatrixAdd.cu(56): error: expected a declaration
MatrixAdd.cu(57): error: expected a declaration
MatrixAdd.cu(58): error: expected a declaration
MatrixAdd.cu(61): error: expected a declaration
MatrixAdd.cu(63): error: expected a declaration
MatrixAdd.cu(7): warning: variable “blocksize” was declared but never referenced
23 errors detected in the compilation of “/tmp/tmpxft_000049d3_00000000-4_MatrixAdd.cpp1.ii”.
Thank you for help!