forked from csc-training/summerschool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy2d.cpp
49 lines (40 loc) · 1.21 KB
/
copy2d.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <hip/hip_runtime.h>
#include <stdio.h>
#include <math.h>
// TODO: add a device kernel that copies all elements of a vector
// using GPU threads in a 2D grid
int main(void)
{
int i, j;
const int n = 600;
const int m = 400;
const int size = n * m;
double x[size], y[size], y_ref[size];
double *x_, *y_;
// initialise data
for (i=0; i < size; i++) {
x[i] = (double) i / 1000.0;
y[i] = 0.0;
}
// copy reference values (C ordered)
for (i=0; i < n; i++) {
for (j=0; j < m; j++) {
y_ref[i * m + j] = x[i * m + j];
}
}
// TODO: allocate vectors x_ and y_ on the GPU
// TODO: copy initial values from CPU to GPU (x -> x_ and y -> y_)
// TODO: define grid dimensions (use 2D grid!)
// TODO: launch the device kernel
hipLaunchKernelGGL(...);
// TODO: copy results back to CPU (y_ -> y)
// confirm that results are correct
double error = 0.0;
for (i=0; i < size; i++) {
error += abs(y_ref[i] - y[i]);
}
printf("total error: %f\n", error);
printf(" reference: %f at (42,42)\n", y_ref[42 * m + 42]);
printf(" result: %f at (42,42)\n", y[42 * m + 42]);
return 0;
}