| 1 |
/* |
| 2 |
* Advanced Simulation Library <http://asl.org.il> |
| 3 |
* |
| 4 |
* Copyright 2015 Avtech Scientific <http://avtechscientific.com> |
| 5 |
* |
| 6 |
* |
| 7 |
* This file is part of Advanced Simulation Library (ASL). |
| 8 |
* |
| 9 |
* ASL is free software: you can redistribute it and/or modify it |
| 10 |
* under the terms of the GNU Affero General Public License as |
| 11 |
* published by the Free Software Foundation, version 3 of the License. |
| 12 |
* |
| 13 |
* ASL is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Affero General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Affero General Public License |
| 19 |
* along with ASL. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
|
| 24 |
#include "aclHardware.h" |
| 25 |
#include "../aslUtilities.h" |
| 26 |
#include "Kernels/aclKernel.h" |
| 27 |
#include <fstream> |
| 28 |
|
| 29 |
|
| 30 |
using namespace std; |
| 31 |
using namespace asl; |
| 32 |
|
| 33 |
namespace acl |
| 34 |
{ |
| 35 |
vector<string> clExtension({"cl_khr_fp64", |
| 36 |
"cl_khr_int64_base_atomics", |
| 37 |
"cl_khr_int64_extended_atomics", |
| 38 |
"CL_KHR_gl_sharing"}); |
| 39 |
|
| 40 |
// order of elements in TYPE and TYPE_SIZE |
| 41 |
// has to correspond to the order of elements of TypeID |
| 42 |
// don't change strings to "cl_..."!!! |
| 43 |
const vector<string> TYPE({"int", "uint", "float", "double", "long"}); |
| 44 |
const vector<unsigned char> TYPE_SIZE({sizeof(cl_int), sizeof(cl_uint), sizeof(cl_float), sizeof(cl_double), sizeof(cl_long)}); |
| 45 |
const vector<TypeID> TYPE_SELECT({TYPE_INT, TYPE_INT, TYPE_INT, TYPE_LONG, TYPE_LONG}); |
| 46 |
|
| 47 |
Hardware hardware; |
| 48 |
|
| 49 |
|
| 50 |
Hardware::Hardware(): |
| 51 |
devicesInfo("") |
| 52 |
{ |
| 53 |
// Scan all available platforms and their devices |
| 54 |
vector<cl::Platform> platforms; |
| 55 |
vector<cl::Device> devices; |
| 56 |
cl_context_properties cps[3]; |
| 57 |
cl::Context context; |
| 58 |
|
| 59 |
cl_int status = 0; |
| 60 |
status = cl::Platform::get(&platforms); |
| 61 |
errorMessage(status, "acl::Platform::get()"); |
| 62 |
|
| 63 |
if (platforms.size() > 0) |
| 64 |
{ |
| 65 |
for (unsigned int i = 0; i < platforms.size(); ++i) |
| 66 |
{ |
| 67 |
// Probably not needed, since getDevices() seems to call clear() internally |
| 68 |
devices.clear(); |
| 69 |
status = platforms[i].getDevices(CL_DEVICE_TYPE_ALL, &devices); |
| 70 |
errorMessage(status, "acl::Platform::getDevices()"); |
| 71 |
devicesInfo += "Platform: " + platforms[i].getInfo<CL_PLATFORM_VENDOR>() |
| 72 |
+ "\nNumber of devices: " + numToStr(devices.size()) + "\n"; |
| 73 |
|
| 74 |
cps[0] = CL_CONTEXT_PLATFORM; |
| 75 |
cps[1] = (cl_context_properties)(platforms[i])(); |
| 76 |
cps[2] = 0; |
| 77 |
|
| 78 |
for (unsigned int j = 0; j < devices.size(); ++j) |
| 79 |
{ |
| 80 |
// Create an OpenCL context for the current device |
| 81 |
context = cl::Context(vector<cl::Device>(1, devices[j]), cps, NULL, NULL, &status); |
| 82 |
errorMessage(status, "acl::Context::Context()"); |
| 83 |
|
| 84 |
// Create an OpenCL command queue for current context and device |
| 85 |
queues.push_back(CommandQueue(new cl::CommandQueue(context, devices[j], 0, &status))); |
| 86 |
errorMessage(status, "acl::CommandQueue::CommandQueue()"); |
| 87 |
|
| 88 |
devicesInfo += "\t" + devices[j].getInfo<CL_DEVICE_NAME>() + "\n"; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// Set first found queue as default queue |
| 94 |
defaultQueue = queues.front(); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
void Hardware::setDefaultQueue(const std::string & platform, |
| 99 |
const std::string & device) |
| 100 |
{ |
| 101 |
defaultQueue = nullptr; |
| 102 |
|
| 103 |
for (unsigned int i(0); i < queues.size(); ++i) |
| 104 |
{ |
| 105 |
if ((platform == getPlatformVendor(queues[i])) && |
| 106 |
(device == getDeviceName(queues[i]))) |
| 107 |
{ |
| 108 |
// Choose requested device on requested platform |
| 109 |
defaultQueue = queues[i]; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// Warn if requested combination of platform and device was not found |
| 114 |
if (defaultQueue.get() == nullptr) |
| 115 |
{ |
| 116 |
// Choose first available device |
| 117 |
defaultQueue = queues.front(); |
| 118 |
warningMessage("Requested combination of platform(" + platform + ") and device(" + device + ") not found! Using:\n" + getDefaultDeviceInfo()); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
std::string Hardware::getDefaultDeviceInfo() |
| 124 |
{ |
| 125 |
string defaultDeviceInfo("platform = " + getPlatformVendor(defaultQueue) |
| 126 |
+ "\ndevice = " + getDeviceName(defaultQueue)); |
| 127 |
return defaultDeviceInfo; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
string Hardware::getDevicesInfo() |
| 132 |
{ |
| 133 |
return devicesInfo; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
string getPlatformVendor(const CommandQueue & queue) |
| 138 |
{ |
| 139 |
cl_context_properties cps = getContext(queue).getInfo<CL_CONTEXT_PROPERTIES>()[1]; |
| 140 |
return (cl::Platform((cl_platform_id)cps)).getInfo<CL_PLATFORM_VENDOR>(); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
string getDeviceName(const CommandQueue & queue) |
| 145 |
{ |
| 146 |
return getDevice(queue).getInfo<CL_DEVICE_NAME>(); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
string getDeviceVersion(const CommandQueue & queue) |
| 151 |
{ |
| 152 |
return getDevice(queue).getInfo<CL_DEVICE_VERSION>(); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
cl_device_type getDeviceType(const CommandQueue & queue) |
| 157 |
{ |
| 158 |
return getDevice(queue).getInfo<CL_DEVICE_TYPE>(); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
string getDriverVersion(const CommandQueue & queue) |
| 163 |
{ |
| 164 |
return getDevice(queue).getInfo<CL_DRIVER_VERSION>(); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
cl_uint getNComputeUnits(const CommandQueue & queue) |
| 169 |
{ |
| 170 |
return getDevice(queue).getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>(); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
cl::Device getDevice(const CommandQueue & queue) |
| 175 |
{ |
| 176 |
return queue->getInfo<CL_QUEUE_DEVICE>(); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
cl::Context getContext(const CommandQueue & queue) |
| 181 |
{ |
| 182 |
return queue->getInfo<CL_QUEUE_CONTEXT>(); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
unsigned int getAlignment(const CommandQueue & queue) |
| 187 |
{ |
| 188 |
return getDevice(queue).getInfo<CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE>(); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
size_t getMaxItemSize(const CommandQueue & queue) |
| 193 |
{ |
| 194 |
return getDevice(queue).getInfo<CL_DEVICE_MAX_WORK_ITEM_SIZES>()[0]; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
cl_uint getVectorWidth(const CommandQueue & queue, const TypeID typeID) |
| 199 |
{ |
| 200 |
cl_uint width; |
| 201 |
|
| 202 |
switch (typeID) |
| 203 |
{ |
| 204 |
case TYPE_INT: |
| 205 |
width = getDevice(queue).getInfo<CL_DEVICE_NATIVE_VECTOR_WIDTH_INT>(); |
| 206 |
break; |
| 207 |
case TYPE_UINT: |
| 208 |
// same width as in TYPE_INT |
| 209 |
width = getDevice(queue).getInfo<CL_DEVICE_NATIVE_VECTOR_WIDTH_INT>(); |
| 210 |
break; |
| 211 |
case TYPE_FLOAT: |
| 212 |
width = getDevice(queue).getInfo<CL_DEVICE_NATIVE_VECTOR_WIDTH_FLOAT>(); |
| 213 |
break; |
| 214 |
case TYPE_LONG: |
| 215 |
width = getDevice(queue).getInfo<CL_DEVICE_NATIVE_VECTOR_WIDTH_LONG>(); |
| 216 |
break; |
| 217 |
default: |
| 218 |
width = getDevice(queue).getInfo<CL_DEVICE_NATIVE_VECTOR_WIDTH_DOUBLE>(); |
| 219 |
break; |
| 220 |
} |
| 221 |
|
| 222 |
return width; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
bool extensionAvailable(const CommandQueue & queue, const Extension extension) |
| 227 |
{ |
| 228 |
string availableExtensions = getDevice(queue).getInfo<CL_DEVICE_EXTENSIONS>(); |
| 229 |
return (availableExtensions.find(clExtension[extension]) != string::npos); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
cl_device_local_mem_type getLocalMemoryType(const CommandQueue & queue) |
| 234 |
{ |
| 235 |
return getDevice(queue).getInfo<CL_DEVICE_LOCAL_MEM_TYPE>(); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
cl_ulong getLocalMemorySize(const CommandQueue & queue) |
| 240 |
{ |
| 241 |
return getDevice(queue).getInfo<CL_DEVICE_LOCAL_MEM_SIZE>(); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
cl_ulong getKernelLocalMemSize(const Kernel & kernel) |
| 246 |
{ |
| 247 |
return kernel.getKernel().getWorkGroupInfo<CL_KERNEL_LOCAL_MEM_SIZE>(getDevice(kernel.getQueue())); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
cl_ulong getKernelPrivateMemSize(const Kernel & kernel) |
| 252 |
{ |
| 253 |
return kernel.getKernel().getWorkGroupInfo<CL_KERNEL_PRIVATE_MEM_SIZE >(getDevice(kernel.getQueue())); |
| 254 |
// return kernel.getKernel().getWorkGroupInfo<CL_KERNEL_WORK_GROUP_SIZE >(getDevice(kernel.getQueue())); |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
cl_device_fp_config doublePrecisionSupport(const CommandQueue & queue) |
| 260 |
{ |
| 261 |
return getDevice(queue).getInfo<CL_DEVICE_DOUBLE_FP_CONFIG>(); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
} // namespace acl |