-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
106 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ Creole (creole) | |
Crystal (cr) | ||
CSS (css) | ||
CSV (csv) | ||
Cuda (cu) | ||
Cython (pyx,pxi,pxd) | ||
D (d) | ||
Dart (dart) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <iostream> | ||
#include <math.h> | ||
// Kernel function to add the elements of two arrays | ||
__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; | ||
|
||
// Allocate Unified Memory – accessible from CPU or GPU | ||
cudaMallocManaged(&x, N*sizeof(float)); | ||
cudaMallocManaged(&y, N*sizeof(float)); | ||
|
||
// initialize x and y arrays on the host | ||
for (int i = 0; i < N; i++) { | ||
x[i] = 1.0f; | ||
y[i] = 2.0f; | ||
} | ||
|
||
// Run kernel on 1M elements on the GPU | ||
add<<<1, 1>>>(N, x, y); | ||
|
||
// Wait for GPU to finish before accessing on host | ||
cudaDeviceSynchronize(); | ||
|
||
// Check for errors (all values should be 3.0f) | ||
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; | ||
|
||
// Free memory | ||
cudaFree(x); | ||
cudaFree(y); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters