-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathComputePlatform.cpp
More file actions
214 lines (171 loc) · 5.66 KB
/
ComputePlatform.cpp
File metadata and controls
214 lines (171 loc) · 5.66 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// ComputePlatform.cpp
// ParallelDTM
//
// Created by Thales Sabino on 8/25/16.
// Copyright © 2016 Thales Sabino. All rights reserved.
//
#include "ComputePlatform.h"
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <algorithm>
#ifdef _WIN32
# define STDCALL __stdcall
#else
# define STDCALL
#endif
using namespace std;
void STDCALL OpenCLContextNotify(const char * info, const void *, ::size_t, void*)
{
cout << info << endl;
}
static vector<cl::Platform> GetPlatforms()
{
vector<cl::Platform> TempPlatforms;
cl::Platform::get(&TempPlatforms);
return TempPlatforms;
}
double ComputePlatform::GetEventElapsedTime(cl::Event Event)
{
auto StartTime = Event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
auto EndTime = Event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
auto ElapsedNanoSec = EndTime - StartTime;
auto ElapsedMilliSec = chrono::duration_cast<chrono::milliseconds>(chrono::nanoseconds(ElapsedNanoSec)).count();
return ElapsedMilliSec;
}
int ComputePlatform::GetPlatformCount()
{
return static_cast<int>(GetPlatforms().size());
}
ComputePlatform::ComputePlatform(int PlatformIndex, int NumDevices)
{
auto PlatformList = GetPlatforms();
if(PlatformIndex >= static_cast<int>(PlatformList.size()))
{
throw std::runtime_error("Invalid platform index");
}
Platform = PlatformList.at(PlatformIndex);
Platform.getDevices(CL_DEVICE_TYPE_ALL, &Devices);
if (Devices.empty())
{
throw std::runtime_error("No devices found for platform " + to_string(PlatformIndex));
}
if (NumDevices != -1)
{
while (Devices.size() > NumDevices)
{
Devices.pop_back();
}
}
auto DeviceIsntValidPred = [](cl::Device aDevice)
{
return aDevice.getInfo<CL_DEVICE_DOUBLE_FP_CONFIG>() == 0;
};
Devices.erase(remove_if(Devices.begin(), Devices.end(), DeviceIsntValidPred), Devices.end());
cl_context_properties ContextProperties[3] =
{
CL_CONTEXT_PLATFORM,
(cl_context_properties)(Platform)(),
0
};
Context = cl::Context(Devices, ContextProperties, OpenCLContextNotify);
for (auto aDevice : Devices)
{
CommandQueues.emplace_back(Context, aDevice, CL_QUEUE_PROFILING_ENABLE);
}
}
cl::Program ComputePlatform::CreateProgram(const std::string &SourceFilepath)
{
ifstream KernelFile(SourceFilepath);
if(!KernelFile.is_open())
{
throw std::runtime_error("Error opening " + SourceFilepath);
}
string KernelSource( istreambuf_iterator<char>(KernelFile), (istreambuf_iterator<char>()) );
cl::Program::Sources ProgramSource(1, make_pair(KernelSource.data(), KernelSource.size()));
cl::Program Program(Context, ProgramSource);
try
{
Program.build(Devices);
}
catch(...)
{
for(auto aDevice : Devices)
{
cout << Program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(aDevice) << endl;
}
rethrow_exception(std::current_exception());
}
return Program;
}
cl::CommandQueue ComputePlatform::GetNextCommandQueue()
{
std::unique_lock<std::mutex> Lock(CommandQueuesMutex);
// Round Robin Scheduling
rotate(CommandQueues.begin(), CommandQueues.begin() + 1, CommandQueues.end());
return CommandQueues.back();
}
void ComputePlatform::PrintDeviceName(const std::string& Description, cl::CommandQueue Queue)
{
#if PRINT_DEVICE_NAMES
auto Device = Queue.getInfo<CL_QUEUE_DEVICE>();
auto DeviceName = Device.getInfo<CL_DEVICE_NAME>();
cout << Description << ": " << DeviceName << endl;
#endif
}
void ComputePlatform::RecordEvent(const std::vector<std::string>& Tags, cl::Event Event)
{
if (bProfile)
{
double ElapsedMilliSec = GetEventElapsedTime(Event);
unique_lock<mutex> Lock(RecordEventMutex);
for (auto Tag : Tags)
{
ProfilingMap[Tag] += static_cast<long int>(ElapsedMilliSec);
}
}
}
void ComputePlatform::RecordTime(const std::vector<std::string>& Tags, long int Time)
{
if (bProfile)
{
unique_lock<mutex> Lock(RecordEventMutex);
for (auto Tag : Tags)
{
ProfilingMap[Tag] += Time;
}
}
}
ostream& operator<< (ostream& out, const ComputePlatform& aComputingPlatform)
{
auto Platform = aComputingPlatform.Platform;
out << "Platform" << endl;
out << "\tName : " << Platform.getInfo<CL_PLATFORM_NAME>() << endl;
out << "\tVendor : " << Platform.getInfo<CL_PLATFORM_VENDOR>() << endl;
out << "\tProfile: " << Platform.getInfo<CL_PLATFORM_PROFILE>() << endl;
out << "\tVersion: " << Platform.getInfo<CL_PLATFORM_VERSION>() << endl;
out << endl;
out << "Devices" << endl;
for (auto aDevice : aComputingPlatform.Devices)
{
out << "\tName : " << aDevice.getInfo<CL_DEVICE_NAME>() << endl;
out << "\tVendor : " << aDevice.getInfo<CL_DEVICE_VENDOR>() << endl;
out << "\tGlobal Mem Size: " << aDevice.getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>() / (1024 * 1024) << " Mb" << endl;
out << "\tLocal Mem Size : " << aDevice.getInfo<CL_DEVICE_LOCAL_MEM_SIZE>() / 1024 << " Kb" << endl;
vector<size_t> MaxWorkGroupSize = aDevice.getInfo<CL_DEVICE_MAX_WORK_ITEM_SIZES>();
out << "\tMax Work Item Sizes: ";
for (auto WorkGroupSize : MaxWorkGroupSize)
{
out << WorkGroupSize << " ";
}
out << endl;
out << "\tMax Work Group Size: " << aDevice.getInfo<CL_DEVICE_MAX_WORK_GROUP_SIZE>() << endl;
out << "\tMax Compute Units: " << aDevice.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>() << endl;
out << "\tMax Work Item Dimensions: " << aDevice.getInfo<CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS>() << endl;
out << "\tMem Base Addr Align: " << aDevice.getInfo<CL_DEVICE_MEM_BASE_ADDR_ALIGN>() << endl;
out << endl;
}
out << endl;
return out;
}