Adding a bunch of variables using cuda or cpu
Signed-off-by: Nigel Barink <nigelbarink@hotmail.com>
This commit is contained in:
commit
8857db8abc
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
*.exp
|
||||
*.lib
|
||||
*.exe
|
4
README.md
Normal file
4
README.md
Normal file
@ -0,0 +1,4 @@
|
||||
# Practicing CUDA
|
||||
## Doing some parallel compute on the GPU
|
||||
|
||||
|
31
add.cpp
Normal file
31
add.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
|
||||
void add (int n, float* x, float* y){
|
||||
for(int i = 0; i< n; i++)
|
||||
y[i] = x[i] +y[i];
|
||||
}
|
||||
|
||||
int main (void) {
|
||||
int N = 1<<20;
|
||||
float *x = new float [N];
|
||||
float *y = new float [N];
|
||||
|
||||
for (int i = 0; i<N; i++){
|
||||
x[i] = 1.0f;
|
||||
y[i] = 2.0f;
|
||||
}
|
||||
|
||||
add(N, x, y );
|
||||
|
||||
float maxError= 0.0f;
|
||||
for (int i =0; i < N; i++){
|
||||
maxError = fmax(maxError, fabs(y[i] -3.0f));
|
||||
}
|
||||
std::cout << "Max error: " << maxError << std::endl;
|
||||
|
||||
delete [] x;
|
||||
delete [] y;
|
||||
|
||||
return 0;
|
||||
}
|
35
add_cuda.cu
Normal file
35
add_cuda.cu
Normal file
@ -0,0 +1,35 @@
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
|
||||
__global__
|
||||
void add (int n, float* x, float* y){
|
||||
for(int i = 0; i< n; i++)
|
||||
y[i] = x[i] +y[i];
|
||||
}
|
||||
|
||||
|
||||
int main (void) {
|
||||
int N = 1<<20;
|
||||
float *x, *y;
|
||||
cudaMallocManaged(&x, N*sizeof(float));
|
||||
cudaMallocManaged(&y, N*sizeof(float));
|
||||
|
||||
|
||||
for (int i = 0; i<N; i++){
|
||||
x[i] = 1.0f;
|
||||
y[i] = 2.0f;
|
||||
}
|
||||
|
||||
add<<<1,1>>>(N, x, y );
|
||||
cudaDeviceSynchronize();
|
||||
|
||||
float maxError= 0.0f;
|
||||
for (int i =0; i < N; i++){
|
||||
maxError = fmax(maxError, fabs(y[i] -3.0f));
|
||||
}
|
||||
std::cout << "Max error: " << maxError << std::endl;
|
||||
|
||||
cudaFree(x);
|
||||
cudaFree(y);
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user