-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvecadd_cpu.cpp
More file actions
61 lines (49 loc) · 1.51 KB
/
Copy pathvecadd_cpu.cpp
File metadata and controls
61 lines (49 loc) · 1.51 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
57
58
59
60
61
/*
* Coding Project #3: Vector Addition in Parrallel, CPU only version.
* Emanuel Francis
* CSC 656
*/
#include <iostream>
#include <math.h>
#include <chrono>
// function to add the elements of two arrays
void add(int n, float *x, float *y)
{
// n arithmetic operations
// 3n bytes written/read
for (int i = 0; i < n; i++)
{
y[i] = x[i] + y[i];
}
}
int main(void)
{
int N = 1 << 24;
float *x = new float[N];
float *y = new float[N];
// initalize x and y arrays on the host
for (int i = 0; i < N; i++)
{
x[i] = 1.0f;
y[i] = 2.0f;
}
// insert your timer code here
std::chrono::time_point<std::chrono::high_resolution_clock> start_time = std::chrono::high_resolution_clock::now();
// Run kernel on 1M elements on the CPU
add(N, x, y);
// insert your end timer code here, and print out elapsed time for this problem size
std::chrono::time_point<std::chrono::high_resolution_clock> end_time = std::chrono::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));
}
std::cout << "Max error " << maxError << std::endl;
std::chrono::duration<double> elapsed = end_time - start_time;
std::cout << "Elapsed time is : " << elapsed.count() << " " << std::endl;
// Free Memory
delete[] x;
delete[] y;
return 0;
}