-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiniteDiffCPU.cpp
42 lines (37 loc) · 1.42 KB
/
finiteDiffCPU.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
#include <iostream>
#include <chrono>
float f(float t){
return t*t;
}
int main(void) {
auto start = std::chrono::high_resolution_clock::now();
// Declare Vairables host variables
int numElements = 50000; // vector size --- determine whats the max we can do on GPU
// size_t size = numElements*sizeof(float); // total vector size
// we want C <-- A*B so init three vectors on host
float *h_Tp = new float[numElements];
float *h_Tn = new float[numElements];
float *h_dF = new float[numElements];
float e = 0.0001;
auto startL = std::chrono::high_resolution_clock::now();
for (int i = 0; i < numElements; i++)
{
// 0.001 -> 500
h_Tn[i] = i/100 - e;
h_Tp[i] = i/100 + e;
}
for (int i = 0; i < numElements; i++)
{
h_dF[i] = (f(h_Tp[i]) - f(h_Tn[i])) / (2*e);
}
auto stopL = std::chrono::high_resolution_clock::now();
auto durationL = std::chrono::duration_cast<std::chrono::microseconds>(stopL- startL);
// delete vectors
delete[] h_Tn;
delete[] h_Tp;
delete[] h_dF;
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
std::cout<< "Run time for evalulating Finite difference on CPU: " << durationL.count() << " microseconds" << std::endl;
std::cout<< "Total Run time on CPU: " << duration.count() << " microseconds" << std::endl;
}