-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqrt.cpp
More file actions
75 lines (58 loc) · 1.95 KB
/
sqrt.cpp
File metadata and controls
75 lines (58 loc) · 1.95 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <chrono>
#include "sqrt.hpp"
#define X_VALUE 25.0f
SQRT::SQRT(CV_OpenCL &opencl, int array_size) : cv_opencl(opencl)
{
this->array_size = array_size;
// Setup Buffers
x_host = (float *)alignedMalloc(sizeof(float)*array_size);
for (int i = 0; i < array_size; i++)
{
x_host[i] = X_VALUE;
}
x_device = alloc<float>(cv_opencl, array_size, x_host);
cv_opencl.cmd_q.enqueueMigrateMemObjects({x_device}, 0);
cv_opencl.cmd_q.finish();
std::cout << "Starting..." << std::endl;
// Start Time.
std::chrono::steady_clock::time_point tick;
tick = std::chrono::steady_clock::now();
compute_sqrt();
cv_opencl.cmd_q.finish();
// Download results
cv_opencl.cmd_q.enqueueMigrateMemObjects({x_device}, CL_MIGRATE_MEM_OBJECT_HOST);
// Stop Time
auto tock = std::chrono::steady_clock::now();
std::chrono::duration<double> time_delta = tock-tick;
std::cout << "Computation Duration: " << time_delta.count() << " s" << std::endl;
// Save duration to file.
{
std::ofstream file("duration.txt");
file << "Computation Duration: " << time_delta.count() << " s" << std::endl;
}
std::cout << "Result: sqrt(" << X_VALUE << ") = " << x_host[0] << std::endl;
// Save Results to file
std::cout << "Saving solution..." << std::endl;
{
std::ofstream file("results.txt");
for (int i = 0; i < array_size; i++)
{
file << x_host[i] << std::endl;
}
}
std::cout << "Saved solution to results.txt..." << std::endl;
std::cout << "Done..." << std::endl;
}
void SQRT::compute_sqrt()
{
cl_int err;
cl::Kernel kernel = cv_opencl.kernels[cv_opencl.compute_sqrt_idx];
int arg_idx = 0;
OCL_CHECK(err, err = kernel.setArg(arg_idx++, x_device));
OCL_CHECK(err, err = kernel.setArg(arg_idx++, sizeof(int), &array_size));
OCL_CHECK(err,
err = cv_opencl.cmd_q.enqueueTask(
kernel
)
);
}