-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest01.cpp
More file actions
56 lines (40 loc) · 1.12 KB
/
test01.cpp
File metadata and controls
56 lines (40 loc) · 1.12 KB
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
50
51
52
53
54
55
56
#include <iostream>
#include <cmath>
#include <ctime>
#include <ratio>
#include <chrono>
/*
Compile: g++ --std=c++14 test01.cpp -o test01
Run: ./test01
*/
// function to add the elements of two arrays
static void add(int n, float *x, float *y){
for (int i = 0; i < n; i++)
y[i] = x[i] + y[i];
}
int main(void){
using namespace std::chrono;
int N = 1<<24; // 1M elements
float *x = new float[N];
float *y = new float[N];
// initialize x and y arrays on the host
for (int i = 0; i < N; i++) {
x[i] = 1.0f;
y[i] = 2.0f;
}
high_resolution_clock::time_point t1 = high_resolution_clock::now();
// Run kernel on 1M elements on the CPU
add(N, x, y);
high_resolution_clock::time_point t2 = high_resolution_clock::now();
// 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));
duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
std::cout << "Max error: " << maxError << "\n";
std::cout << "Duration: " << time_span.count() * 1000 << " ms\n";
// Free memory
delete [] x;
delete [] y;
return 0;
}