| 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 "aclKernel.h" |
| 25 |
#include "../aclHardware.h" |
| 26 |
#include <acl/aclElementBase.h> |
| 27 |
#include <iostream> |
| 28 |
#include "../../aslUtilities.h" |
| 29 |
#include <algorithm> |
| 30 |
|
| 31 |
using namespace std; |
| 32 |
using namespace asl; |
| 33 |
|
| 34 |
|
| 35 |
namespace acl |
| 36 |
{ |
| 37 |
|
| 38 |
unsigned int Kernel::kernelNum = 0; |
| 39 |
|
| 40 |
Kernel::Kernel(const KernelConfiguration kernelConfig_): |
| 41 |
groupsNumber(0), |
| 42 |
kernelConfig(kernelConfig_), |
| 43 |
kernelSource("") |
| 44 |
{ |
| 45 |
id = kernelNum; |
| 46 |
++kernelNum; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
cl_uint Kernel::detectVectorWidth() |
| 51 |
{ |
| 52 |
vector<cl_uint> widths; |
| 53 |
|
| 54 |
// get all possible vector widths for this queue |
| 55 |
for (unsigned int i = 0; i < TYPE.size(); ++i) |
| 56 |
widths.push_back(getVectorWidth(queue, (TypeID)i)); |
| 57 |
|
| 58 |
cl_uint absoluteMin = *min_element(widths.begin(), widths.end()); |
| 59 |
cl_uint min = *max_element(widths.begin(), widths.end()); |
| 60 |
|
| 61 |
unsigned int i = 0; |
| 62 |
while ((min > absoluteMin) && (i < arguments.size())) |
| 63 |
{ |
| 64 |
if (min > widths[arguments[i]->getTypeID()]) |
| 65 |
min = widths[arguments[i]->getTypeID()]; |
| 66 |
++i; |
| 67 |
} |
| 68 |
|
| 69 |
i = 0; |
| 70 |
while ((min > absoluteMin) && (i < localDeclarations.size())) |
| 71 |
{ |
| 72 |
if (min > widths[localDeclarations[i]->getTypeID()]) |
| 73 |
min = widths[localDeclarations[i]->getTypeID()]; |
| 74 |
++i; |
| 75 |
} |
| 76 |
|
| 77 |
// Workaround for the case when double vector width is 0. |
| 78 |
// The code still works, however with acceleration |
| 79 |
// only for types other than double. |
| 80 |
min = min == 0 ? 1 : min; |
| 81 |
return min; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
// Enables double extension only if there are doubles in the source, |
| 86 |
// enables right extension (workaround for the incomplete AMD cl_amd_fp64) |
| 87 |
void enableDoubleExtension(string & kernelSource, const CommandQueue & queue) |
| 88 |
{ |
| 89 |
if (kernelSource.find(TYPE[TYPE_DOUBLE]) != string::npos) |
| 90 |
{ |
| 91 |
string availableExtensions = getDevice(queue).getInfo<CL_DEVICE_EXTENSIONS>(); |
| 92 |
|
| 93 |
// workaround for the incomplete AMD cl_amd_fp64. |
| 94 |
// once AMD implements this extension completely |
| 95 |
// only the else-block should stay |
| 96 |
if (availableExtensions.find("cl_amd_fp64") != string::npos) |
| 97 |
{ |
| 98 |
kernelSource = "#pragma OPENCL EXTENSION cl_amd_fp64 : enable\n" + kernelSource; |
| 99 |
} |
| 100 |
else |
| 101 |
{ |
| 102 |
kernelSource = "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n" + kernelSource; |
| 103 |
} |
| 104 |
} |
| 105 |
else |
| 106 |
{ |
| 107 |
string availableExtensions = getDevice(queue).getInfo<CL_DEVICE_EXTENSIONS>(); |
| 108 |
|
| 109 |
// workaround for the incomplete AMD cl_amd_fp64. |
| 110 |
// once AMD implements this extension completely |
| 111 |
// only the else-block should stay |
| 112 |
if (availableExtensions.find("cl_amd_fp64") != string::npos) |
| 113 |
{ |
| 114 |
kernelSource = "#pragma OPENCL EXTENSION cl_amd_fp64 : disable\n" + kernelSource; |
| 115 |
} |
| 116 |
else |
| 117 |
{ |
| 118 |
kernelSource = "#pragma OPENCL EXTENSION cl_khr_fp64 : disable\n" + kernelSource; |
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
void Kernel::generateExtensions() |
| 126 |
{ |
| 127 |
kernelSource = "\n" + kernelSource; |
| 128 |
for (unsigned int i = 0; i < kernelConfig.extensions.size(); i++) |
| 129 |
{ |
| 130 |
kernelSource = "#pragma OPENCL EXTENSION " + kernelConfig.extensions[i] |
| 131 |
+ " : enable\n" + kernelSource; |
| 132 |
} |
| 133 |
enableDoubleExtension(kernelSource, queue); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
void Kernel::generateArguments() |
| 138 |
{ |
| 139 |
for (unsigned int i = 0; i < arguments.size(); i++) |
| 140 |
{ |
| 141 |
kernelSource += arguments[i]->getTypeSignature(kernelConfig) + ",\n "; |
| 142 |
for (unsigned int j = 0; j < numToStr(id).size(); ++j) |
| 143 |
{ |
| 144 |
kernelSource += " "; |
| 145 |
} |
| 146 |
} |
| 147 |
// drop all after the ',' of last parameter |
| 148 |
kernelSource.erase(kernelSource.size() - 25 - numToStr(id).size()); |
| 149 |
kernelSource += ")\n{\n\t"; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
void Kernel::generateIndex() |
| 154 |
{ |
| 155 |
if (kernelConfig.local) |
| 156 |
{ |
| 157 |
kernelSource += "uint " + INDEX + " = get_local_id(0);\n\t"; |
| 158 |
kernelSource += "uint groupID = get_group_id(0);\n"; |
| 159 |
} |
| 160 |
else |
| 161 |
{ |
| 162 |
if ((kernelConfig.vectorWidth > 1) && (kernelConfig.unaligned)) |
| 163 |
{ |
| 164 |
kernelSource += "uint " + INDEX + " = " |
| 165 |
+ numToStr(kernelConfig.vectorWidth) |
| 166 |
+ " * get_global_id(0);\n"; |
| 167 |
} |
| 168 |
else |
| 169 |
{ |
| 170 |
kernelSource += "uint " + INDEX + " = get_global_id(0);\n"; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
void Kernel::generateLocalDeclarations() |
| 177 |
{ |
| 178 |
for (unsigned int i = 0; i < localDeclarations.size(); i++) |
| 179 |
{ |
| 180 |
kernelSource += "\t" + localDeclarations[i]->getLocalDeclaration(kernelConfig) + ";\n"; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
void Kernel::generateExpressions() |
| 186 |
{ |
| 187 |
for (unsigned int i = 0; i < expression.size(); i++) |
| 188 |
{ |
| 189 |
kernelSource += "\t" + expression[i]->str(kernelConfig) + ";\n"; |
| 190 |
} |
| 191 |
|
| 192 |
kernelSource += "}"; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
void Kernel::generateKernelSource() |
| 197 |
{ |
| 198 |
kernelSource = "__kernel void compute_" + numToStr(id) + "("; |
| 199 |
|
| 200 |
filterDeclarations(); |
| 201 |
|
| 202 |
generateArguments(); |
| 203 |
|
| 204 |
generateIndex(); |
| 205 |
|
| 206 |
generateLocalDeclarations(); |
| 207 |
|
| 208 |
generateExpressions(); |
| 209 |
|
| 210 |
// enable extensions in the last step after |
| 211 |
// the kernel source is available for analysis |
| 212 |
generateExtensions(); |
| 213 |
|
| 214 |
regenerateKernelSource = false; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
void Kernel::buildKernel() |
| 219 |
{ |
| 220 |
cl_int status = 0; |
| 221 |
|
| 222 |
cl::Program::Sources source(1, make_pair(kernelSource.data(), kernelSource.size())); |
| 223 |
|
| 224 |
cl::Program program = cl::Program(getContext(queue), source, &status); |
| 225 |
errorMessage(status, "Program::Program()"); |
| 226 |
status = program.build(std::vector<cl::Device>(1, getDevice(queue))); |
| 227 |
|
| 228 |
if (status == CL_BUILD_PROGRAM_FAILURE) |
| 229 |
{ |
| 230 |
string str = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(getDevice(queue)); |
| 231 |
|
| 232 |
cout << " \n\t\t\tBUILD LOG\n"; |
| 233 |
cout << " ************************************************\n"; |
| 234 |
cout << str << endl; |
| 235 |
cout << " ************************************************\n"; |
| 236 |
cout << " \n\t\t\tKERNEL SOURCE CODE\n"; |
| 237 |
cout << " ------------------------------------------------\n"; |
| 238 |
cout << kernelSource << endl; |
| 239 |
cout << " ------------------------------------------------\n"; |
| 240 |
} |
| 241 |
|
| 242 |
errorMessage(status, "Program::build()"); |
| 243 |
|
| 244 |
string kernelName; |
| 245 |
kernelName = "compute_" + numToStr(id); |
| 246 |
kernel = cl::Kernel(program, kernelName.c_str(), &status); |
| 247 |
errorMessage(status, "Kernel() - kernelBase_" + numToStr(id)); |
| 248 |
// cout<<" "<<kernelName<<": privMem="<< getKernelPrivateMemSize(*this)<<endl; |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
void Kernel::setKernelArguments() |
| 254 |
{ |
| 255 |
for (unsigned int i = 0; i < arguments.size(); ++i) |
| 256 |
{ |
| 257 |
arguments[i]->setAsArgument(kernel, i); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
void Kernel::updateKernelConfiguration() |
| 263 |
{ |
| 264 |
// if it is a simd kernel - detect proper vector width |
| 265 |
if (kernelConfig.vectorWidth > 1) |
| 266 |
kernelConfig.vectorWidth = detectVectorWidth(); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
void Kernel::setup() |
| 271 |
{ |
| 272 |
if (size == 0) |
| 273 |
errorMessage("Kernel::setup() - kernel's size is 0; add proper expression"); |
| 274 |
|
| 275 |
if (kernelConfig.local && (size > getMaxItemSize(queue))) |
| 276 |
errorMessage("Kernel::setup() - requested group size is larger than supported by device"); |
| 277 |
|
| 278 |
if (kernelConfig.local && (groupsNumber == 0)) |
| 279 |
errorMessage("Kernel::setup() - groups number was not set"); |
| 280 |
|
| 281 |
updateKernelConfiguration(); |
| 282 |
generateKernelSource(); |
| 283 |
buildKernel(); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
inline unsigned int paddingElementsSIMD(unsigned int size, unsigned int vectorWidth); |
| 288 |
|
| 289 |
void Kernel::compute() |
| 290 |
{ |
| 291 |
if (regenerateKernelSource) |
| 292 |
setup(); |
| 293 |
|
| 294 |
cl_int status = 0; |
| 295 |
cl::Event event; |
| 296 |
|
| 297 |
setKernelArguments(); |
| 298 |
|
| 299 |
size_t paddedSize = (size + paddingElementsSIMD(size, kernelConfig.vectorWidth)) / kernelConfig.vectorWidth; |
| 300 |
// if 4th argument is cl::NullRange - OpenCL implementation |
| 301 |
// will determine how to break the global work-items into |
| 302 |
// appropriate work-group instances |
| 303 |
status = queue->enqueueNDRangeKernel(kernel, |
| 304 |
cl::NullRange, |
| 305 |
kernelConfig.local ? cl::NDRange(groupsNumber * paddedSize) : cl::NDRange(paddedSize), |
| 306 |
kernelConfig.local ? cl::NDRange(paddedSize) : cl::NullRange, |
| 307 |
NULL, |
| 308 |
&event); |
| 309 |
errorMessage(status, "CommandQueue::enqueueNDRangeKernel() - kernel"); |
| 310 |
|
| 311 |
status = event.wait(); |
| 312 |
errorMessage(status, "Event::wait() - event"); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
void Kernel::setGroupsNumber(unsigned int n) |
| 317 |
{ |
| 318 |
groupsNumber = n; |
| 319 |
} |
| 320 |
|
| 321 |
unsigned int Kernel::getGroupsNumber() |
| 322 |
{ |
| 323 |
return groupsNumber; |
| 324 |
} |
| 325 |
|
| 326 |
string Kernel::getKernelSource() |
| 327 |
{ |
| 328 |
return kernelSource; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
unsigned int Kernel::getKernelID() |
| 333 |
{ |
| 334 |
return id; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
const cl::Kernel & Kernel::getKernel() const |
| 339 |
{ |
| 340 |
return kernel; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
void Kernel::clear() |
| 345 |
{ |
| 346 |
kernelSource = "__kernel void compute_" + numToStr(id) + "("; |
| 347 |
regenerateKernelSource = true; |
| 348 |
expression.clear(); |
| 349 |
localDeclarations.clear(); |
| 350 |
arguments.clear(); |
| 351 |
size = 0; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
inline unsigned int paddingElementsSIMD(unsigned int size, unsigned int vectorWidth) |
| 356 |
{ |
| 357 |
// second modulo is added in order to make padding = 0 in the case |
| 358 |
// that size is divisible by vectorWidth |
| 359 |
return (vectorWidth - (size % vectorWidth)) % vectorWidth; |
| 360 |
} |
| 361 |
|
| 362 |
} // namespace acl |