Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
142 lines (110 loc) · 5.34 KB

File metadata and controls

142 lines (110 loc) · 5.34 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <vector>
#include "Utils.h"
void print_help() {
std::cerr << "Application usage:" << std::endl;
std::cerr << " -p : select platform " << std::endl;
std::cerr << " -d : select device" << std::endl;
std::cerr << " -l : list all platforms and devices" << std::endl;
std::cerr << " -h : print this message" << std::endl;
}
int main(int argc, char** argv) {
//Part 1 - handle command line options such as device selection, verbosity, etc.
int platform_id = 0;
int device_id = 0;
for (int i = 1; i < argc; i++) {
if ((strcmp(argv[i], "-p") == 0) && (i < (argc - 1))) { platform_id = atoi(argv[++i]); }
else if ((strcmp(argv[i], "-d") == 0) && (i < (argc - 1))) { device_id = atoi(argv[++i]); }
else if (strcmp(argv[i], "-l") == 0) { std::cout << ListPlatformsDevices() << std::endl; }
else if (strcmp(argv[i], "-h") == 0) { print_help(); return 0; }
}
//detect any potential exceptions
try {
//Part 2 - host operations
//2.1 Select computing devices
cl::Context context = GetContext(platform_id, device_id);
//display the selected device
std::cout << "Running on " << GetPlatformName(platform_id) << ", " << GetDeviceName(platform_id, device_id) << std::endl;
//create a queue to which we will push commands for the device
cl::CommandQueue queue(context, CL_QUEUE_PROFILING_ENABLE);
//2.2 Load & build the device code
cl::Program::Sources sources;
AddSources(sources, "kernels/my_kernels.cl");
cl::Program program(context, sources);
//build and debug the kernel code
try {
program.build();
}
catch (const cl::Error& err) {
std::cout << "Build Status: " << program.getBuildInfo<CL_PROGRAM_BUILD_STATUS>(context.getInfo<CL_CONTEXT_DEVICES>()[0]) << std::endl;
std::cout << "Build Options:\t" << program.getBuildInfo<CL_PROGRAM_BUILD_OPTIONS>(context.getInfo<CL_CONTEXT_DEVICES>()[0]) << std::endl;
std::cout << "Build Log:\t " << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(context.getInfo<CL_CONTEXT_DEVICES>()[0]) << std::endl;
throw err;
}
//Part 3 - memory allocation
//host - input
std::vector<int> A = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 10 }; //C++11 allows this type of initialisation
std::vector<int> B = { 0, 1, 2, 0, 1, 2, 0, 1, 2, 0 };
//std::vector<int> A(100000000);
//std::vector<int> B(100000000);
size_t vector_elements = A.size();//number of elements
size_t vector_size = A.size() * sizeof(int);//size in bytes
//host - output
std::vector<int> C(vector_elements);
//device - buffers
cl::Buffer buffer_A(context, CL_MEM_READ_WRITE, vector_size);
cl::Buffer buffer_B(context, CL_MEM_READ_WRITE, vector_size);
cl::Buffer buffer_C(context, CL_MEM_READ_WRITE, vector_size);
//Part 4 - device operations
// Tut 1
//Enable profiling for the queue:
//cl::CommandQueue queue(context, CL_QUEUE_PROFILING_ENABLE);
//Create an eventand attach it to a queue command responsible for the kernel launch :
cl::Event prof_event;
//4.1 Copy arrays A and B to device memory
//cl::Event A_event;
//cl::Event B_event;
//queue.enqueueWriteBuffer(buffer_A, CL_TRUE, 0, vector_size, &A[0], NULL, &A_event);
//queue.enqueueWriteBuffer(buffer_B, CL_TRUE, 0, vector_size, &B[0], NULL, &B_event);
queue.enqueueWriteBuffer(buffer_A, CL_TRUE, 0, vector_size, &A[0]);
queue.enqueueWriteBuffer(buffer_B, CL_TRUE, 0, vector_size, &B[0]);
//4.2 Setup and execute the kernel (i.e. device code)
cl::Kernel kernel_add = cl::Kernel(program, "add");
kernel_add.setArg(0, buffer_C);
kernel_add.setArg(1, buffer_B);
kernel_add.setArg(2, buffer_C);
cl::Kernel kernel_mult = cl::Kernel(program, "mult");
kernel_mult.setArg(0, buffer_A);
kernel_mult.setArg(1, buffer_B);
kernel_mult.setArg(2, buffer_C);
cl::Kernel kernel_mult_add = cl::Kernel(program, "avg_filter");
kernel_mult_add.setArg(0, buffer_A);
//kernel_mult_add.setArg(1, buffer_B);
kernel_mult_add.setArg(1, buffer_C);
//clGetDeviceInfo(CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE)
//queue.enqueueNDRangeKernel(kernel_add, cl::NullRange, cl::NDRange(vector_elements), cl::NullRange, NULL, &prof_event);
//queue.enqueueNDRangeKernel(kernel_add, cl::NullRange, cl::NDRange(vector_elements), cl::NullRange, NULL, &prof_event);
//queue.enqueueNDRangeKernel(kernel_mult, cl::NullRange, cl::NDRange(vector_elements), cl::NullRange);
//queue.enqueueNDRangeKernel(kernel_add, cl::NullRange, cl::NDRange(vector_elements), cl::NullRange);
queue.enqueueNDRangeKernel(kernel_mult_add, cl::NullRange, cl::NDRange(vector_elements), cl::NullRange);
//4.3 Copy the result from device to host
queue.enqueueReadBuffer(buffer_C, CL_TRUE, 0, vector_size, &C[0]);
std::cout << "A = " << A << std::endl;
//std::cout << "B = " << B << std::endl;
std::cout << "C = " << C << std::endl;
//std::cout << "Kernel execution time [ns]:" <<
// prof_event.getProfilingInfo<CL_PROFILING_COMMAND_END>() -
// prof_event.getProfilingInfo<CL_PROFILING_COMMAND_START>() << std::endl;
//std::cout << GetFullProfilingInfo(prof_event, ProfilingResolution::PROF_US)
// << std::endl;
//std::cout << GetFullProfilingInfo(A_event, ProfilingResolution::PROF_US)
// << std::endl;
//cl::Event sum_event = A_event + B_event;
//std::cout << GetFullProfilingInfo(sum_event, ProfilingResolution::PROF_US)
// << std::endl;
}
catch (cl::Error err) {
std::cerr << "ERROR: " << err.what() << ", " << getErrorString(err.err()) << std::endl;
}
return 0;
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.