cristy | dbba821 | 2013-07-19 14:53:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % OOO PPPP EEEEE N N CCCC L % |
| 7 | % O O P P E NN N C L % |
| 8 | % O O PPPP EEE N N N C L % |
| 9 | % O O P E N NN C L % |
| 10 | % OOO P EEEEE N N CCCC LLLLL % |
| 11 | % % |
| 12 | % % |
| 13 | % MagickCore OpenCL Methods % |
| 14 | % % |
| 15 | % Software Design % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
cristy | dbba821 | 2013-07-19 14:53:50 +0000 | [diff] [blame] | 17 | % March 2000 % |
| 18 | % % |
| 19 | % % |
Cristy | 7ce65e7 | 2015-12-12 18:03:16 -0500 | [diff] [blame] | 20 | % Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization % |
cristy | dbba821 | 2013-07-19 14:53:50 +0000 | [diff] [blame] | 21 | % dedicated to making software imaging solutions freely available. % |
| 22 | % % |
| 23 | % You may not use this file except in compliance with the License. You may % |
| 24 | % obtain a copy of the License at % |
| 25 | % % |
| 26 | % http://www.imagemagick.org/script/license.php % |
| 27 | % % |
| 28 | % Unless required by applicable law or agreed to in writing, software % |
| 29 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 30 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 31 | % See the License for the specific language governing permissions and % |
| 32 | % limitations under the License. % |
| 33 | % % |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | % |
| 36 | % |
| 37 | % |
| 38 | */ |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 39 | |
cristy | dbba821 | 2013-07-19 14:53:50 +0000 | [diff] [blame] | 40 | /* |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 41 | Include declarations. |
cristy | dbba821 | 2013-07-19 14:53:50 +0000 | [diff] [blame] | 42 | */ |
| 43 | #include "MagickCore/studio.h" |
| 44 | #include "MagickCore/artifact.h" |
| 45 | #include "MagickCore/cache.h" |
| 46 | #include "MagickCore/color.h" |
| 47 | #include "MagickCore/compare.h" |
| 48 | #include "MagickCore/constitute.h" |
| 49 | #include "MagickCore/distort.h" |
| 50 | #include "MagickCore/draw.h" |
| 51 | #include "MagickCore/effect.h" |
| 52 | #include "MagickCore/exception.h" |
| 53 | #include "MagickCore/exception-private.h" |
| 54 | #include "MagickCore/fx.h" |
| 55 | #include "MagickCore/gem.h" |
| 56 | #include "MagickCore/geometry.h" |
| 57 | #include "MagickCore/image.h" |
| 58 | #include "MagickCore/image-private.h" |
| 59 | #include "MagickCore/layer.h" |
| 60 | #include "MagickCore/mime-private.h" |
| 61 | #include "MagickCore/memory_.h" |
| 62 | #include "MagickCore/monitor.h" |
| 63 | #include "MagickCore/montage.h" |
| 64 | #include "MagickCore/morphology.h" |
cristy | d116555 | 2013-11-24 20:10:57 +0000 | [diff] [blame] | 65 | #include "MagickCore/nt-base.h" |
cristy | 1e37e8f | 2014-02-21 17:05:37 +0000 | [diff] [blame] | 66 | #include "MagickCore/nt-base-private.h" |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 67 | #include "MagickCore/opencl.h" |
| 68 | #include "MagickCore/opencl-private.h" |
cristy | dbba821 | 2013-07-19 14:53:50 +0000 | [diff] [blame] | 69 | #include "MagickCore/option.h" |
| 70 | #include "MagickCore/policy.h" |
| 71 | #include "MagickCore/property.h" |
| 72 | #include "MagickCore/quantize.h" |
| 73 | #include "MagickCore/quantum.h" |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 74 | #include "MagickCore/random_.h" |
| 75 | #include "MagickCore/random-private.h" |
cristy | dbba821 | 2013-07-19 14:53:50 +0000 | [diff] [blame] | 76 | #include "MagickCore/resample.h" |
| 77 | #include "MagickCore/resource_.h" |
| 78 | #include "MagickCore/splay-tree.h" |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 79 | #include "MagickCore/semaphore.h" |
cristy | dbba821 | 2013-07-19 14:53:50 +0000 | [diff] [blame] | 80 | #include "MagickCore/statistic.h" |
| 81 | #include "MagickCore/string_.h" |
| 82 | #include "MagickCore/token.h" |
| 83 | #include "MagickCore/utility.h" |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 84 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 85 | #ifdef MAGICKCORE_CLPERFMARKER |
| 86 | #include "CLPerfMarker.h" |
| 87 | #endif |
| 88 | |
| 89 | |
| 90 | #if defined(MAGICKCORE_OPENCL_SUPPORT) |
| 91 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 92 | #ifdef MAGICKCORE_HAVE_OPENCL_CL_H |
| 93 | #define MAGICKCORE_OPENCL_MACOSX 1 |
| 94 | #endif |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 95 | |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 96 | #define NUM_CL_RAND_GENERATORS 1024 /* number of random number generators running in parallel */ |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 97 | #define PROFILE_OCL_KERNELS 0 |
| 98 | |
| 99 | typedef struct |
| 100 | { |
| 101 | cl_ulong min; |
| 102 | cl_ulong max; |
| 103 | cl_ulong total; |
| 104 | cl_ulong count; |
| 105 | } KernelProfileRecord; |
| 106 | |
| 107 | static const char *kernelNames[] = { |
| 108 | "AddNoise", |
| 109 | "BlurRow", |
| 110 | "BlurColumn", |
| 111 | "Composite", |
| 112 | "ComputeFunction", |
| 113 | "Contrast", |
| 114 | "ContrastStretch", |
| 115 | "Convolve", |
| 116 | "Equalize", |
| 117 | "GrayScale", |
| 118 | "Histogram", |
| 119 | "HullPass1", |
| 120 | "HullPass2", |
| 121 | "LocalContrastBlurRow", |
| 122 | "LocalContrastBlurApplyColumn", |
| 123 | "Modulate", |
| 124 | "MotionBlur", |
| 125 | "RandomNumberGenerator", |
| 126 | "ResizeHorizontal", |
| 127 | "ResizeVertical", |
| 128 | "RotationalBlur", |
| 129 | "UnsharpMaskBlurColumn", |
| 130 | "UnsharpMask", |
| 131 | "NONE" }; |
| 132 | |
| 133 | KernelProfileRecord |
| 134 | profileRecords[KERNEL_COUNT]; |
| 135 | |
| 136 | typedef struct _AccelerateTimer { |
| 137 | long long _freq; |
| 138 | long long _clocks; |
| 139 | long long _start; |
| 140 | } AccelerateTimer; |
| 141 | |
| 142 | void startAccelerateTimer(AccelerateTimer* timer) { |
| 143 | #ifdef _WIN32 |
| 144 | QueryPerformanceCounter((LARGE_INTEGER*)&timer->_start); |
| 145 | |
| 146 | |
| 147 | #else |
| 148 | struct timeval s; |
| 149 | gettimeofday(&s, 0); |
| 150 | timer->_start = (long long)s.tv_sec * (long long)1.0E3 + (long long)s.tv_usec / (long long)1.0E3; |
| 151 | #endif |
| 152 | } |
| 153 | |
| 154 | void stopAccelerateTimer(AccelerateTimer* timer) { |
| 155 | long long n=0; |
| 156 | #ifdef _WIN32 |
| 157 | QueryPerformanceCounter((LARGE_INTEGER*)&(n)); |
| 158 | #else |
| 159 | struct timeval s; |
| 160 | gettimeofday(&s, 0); |
| 161 | n = (long long)s.tv_sec * (long long)1.0E3+ (long long)s.tv_usec / (long long)1.0E3; |
| 162 | #endif |
| 163 | n -= timer->_start; |
| 164 | timer->_start = 0; |
| 165 | timer->_clocks += n; |
| 166 | } |
| 167 | |
| 168 | void resetAccelerateTimer(AccelerateTimer* timer) { |
| 169 | timer->_clocks = 0; |
| 170 | timer->_start = 0; |
| 171 | } |
| 172 | |
| 173 | void initAccelerateTimer(AccelerateTimer* timer) { |
| 174 | #ifdef _WIN32 |
| 175 | QueryPerformanceFrequency((LARGE_INTEGER*)&timer->_freq); |
| 176 | #else |
| 177 | timer->_freq = (long long)1.0E3; |
| 178 | #endif |
| 179 | resetAccelerateTimer(timer); |
| 180 | } |
| 181 | |
| 182 | double readAccelerateTimer(AccelerateTimer* timer) { |
| 183 | return (double)timer->_clocks/(double)timer->_freq; |
| 184 | }; |
| 185 | |
dirk | 0fe53dc | 2015-11-14 23:52:55 +0100 | [diff] [blame] | 186 | MagickPrivate void RecordProfileData(MagickCLEnv clEnv, ProfiledKernels kernel, cl_event event) |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 187 | { |
| 188 | #if PROFILE_OCL_KERNELS |
| 189 | cl_int status; |
| 190 | cl_ulong start = 0; |
| 191 | cl_ulong end = 0; |
| 192 | cl_ulong elapsed = 0; |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 193 | clEnv->library->clWaitForEvents(1, &event); |
| 194 | status = clEnv->library->clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &start, NULL); |
| 195 | status &= clEnv->library->clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &end, NULL); |
| 196 | if (status == CL_SUCCESS) { |
| 197 | start /= 1000; // usecs |
| 198 | end /= 1000; // usecs |
| 199 | elapsed = end - start; |
dirk | 0fe53dc | 2015-11-14 23:52:55 +0100 | [diff] [blame] | 200 | /* we can use the commandQueuesLock to make the code below thread safe */ |
| 201 | LockSemaphoreInfo(clEnv->commandQueuesLock); |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 202 | if ((elapsed < profileRecords[kernel].min) || (profileRecords[kernel].count == 0)) |
| 203 | profileRecords[kernel].min = elapsed; |
| 204 | if (elapsed > profileRecords[kernel].max) |
| 205 | profileRecords[kernel].max = elapsed; |
| 206 | profileRecords[kernel].total += elapsed; |
| 207 | profileRecords[kernel].count += 1; |
dirk | 0fe53dc | 2015-11-14 23:52:55 +0100 | [diff] [blame] | 208 | UnlockSemaphoreInfo(clEnv->commandQueuesLock); |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 209 | } |
| 210 | #endif |
| 211 | } |
| 212 | |
| 213 | void DumpProfileData() |
| 214 | { |
| 215 | #if PROFILE_OCL_KERNELS |
| 216 | int i; |
| 217 | |
| 218 | OpenCLLog("===================================================="); |
| 219 | |
| 220 | // Write out the device info to the profile |
| 221 | if (0 == 1) |
| 222 | { |
| 223 | MagickCLEnv clEnv; |
| 224 | char buff[2048]; |
| 225 | cl_int status; |
| 226 | |
| 227 | clEnv = GetDefaultOpenCLEnv(); |
| 228 | |
| 229 | status = clEnv->library->clGetDeviceInfo(clEnv->device, CL_DEVICE_VENDOR, 2048, buff, NULL); |
| 230 | OpenCLLog(buff); |
| 231 | |
| 232 | status = clEnv->library->clGetDeviceInfo(clEnv->device, CL_DEVICE_NAME, 2048, buff, NULL); |
| 233 | OpenCLLog(buff); |
| 234 | |
| 235 | status = clEnv->library->clGetDeviceInfo(clEnv->device, CL_DRIVER_VERSION, 2048, buff, NULL); |
| 236 | OpenCLLog(buff); |
| 237 | } |
| 238 | |
| 239 | OpenCLLog("===================================================="); |
| 240 | OpenCLLog(" ave\tcalls \tmin -> max"); |
| 241 | OpenCLLog(" ---\t----- \t----------"); |
| 242 | for (i = 0; i < KERNEL_COUNT; ++i) { |
| 243 | char buf[4096]; |
| 244 | char indent[160]; |
| 245 | strcpy(indent, " "); |
| 246 | strncpy(indent, kernelNames[i], min(strlen(kernelNames[i]), strlen(indent) - 1)); |
| 247 | sprintf(buf, "%s%d\t(%d calls) \t%d -> %d", indent, profileRecords[i].count > 0 ? (profileRecords[i].total / profileRecords[i].count) : 0, profileRecords[i].count, profileRecords[i].min, profileRecords[i].max); |
| 248 | //printf("%s%d\t(%d calls) \t%d -> %d\n", indent, profileRecords[i].count > 0 ? (profileRecords[i].total / profileRecords[i].count) : 0, profileRecords[i].count, profileRecords[i].min, profileRecords[i].max); |
| 249 | OpenCLLog(buf); |
| 250 | } |
| 251 | OpenCLLog("===================================================="); |
| 252 | #endif |
| 253 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 254 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 255 | /* |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 256 | * |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 257 | * Dynamic library loading functions |
| 258 | * |
| 259 | */ |
| 260 | #ifdef MAGICKCORE_WINDOWS_SUPPORT |
| 261 | #else |
| 262 | #include <dlfcn.h> |
| 263 | #endif |
| 264 | |
| 265 | // dynamically load a library. returns NULL on failure |
| 266 | void *OsLibraryLoad(const char *libraryName) |
| 267 | { |
| 268 | #ifdef MAGICKCORE_WINDOWS_SUPPORT |
| 269 | return (void *)LoadLibraryA(libraryName); |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 270 | #else |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 271 | return (void *)dlopen(libraryName, RTLD_NOW); |
| 272 | #endif |
| 273 | } |
| 274 | |
| 275 | // get a function pointer from a loaded library. returns NULL on failure. |
| 276 | void *OsLibraryGetFunctionAddress(void *library, const char *functionName) |
| 277 | { |
| 278 | #ifdef MAGICKCORE_WINDOWS_SUPPORT |
| 279 | if (!library || !functionName) |
| 280 | { |
| 281 | return NULL; |
| 282 | } |
| 283 | return (void *) GetProcAddress( (HMODULE)library, functionName); |
| 284 | #else |
| 285 | if (!library || !functionName) |
| 286 | { |
| 287 | return NULL; |
| 288 | } |
| 289 | return (void *)dlsym(library, functionName); |
| 290 | #endif |
| 291 | } |
| 292 | |
| 293 | // unload a library. |
| 294 | void OsLibraryUnload(void *library) |
| 295 | { |
| 296 | #ifdef MAGICKCORE_WINDOWS_SUPPORT |
| 297 | FreeLibrary( (HMODULE)library); |
| 298 | #else |
| 299 | dlclose(library); |
| 300 | #endif |
| 301 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 302 | |
| 303 | |
| 304 | /* |
| 305 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 306 | % % |
| 307 | % % |
| 308 | % % |
| 309 | + A c q u i r e M a g i c k O p e n C L E n v % |
| 310 | % % |
| 311 | % % |
| 312 | % % |
| 313 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 314 | % |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 315 | % AcquireMagickOpenCLEnv() allocates the MagickCLEnv structure |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 316 | % |
| 317 | */ |
| 318 | |
dirk | 69838ee | 2016-01-10 18:22:20 +0100 | [diff] [blame] | 319 | MagickExport MagickCLEnv AcquireMagickOpenCLEnv(void) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 320 | { |
| 321 | MagickCLEnv clEnv; |
| 322 | clEnv = (MagickCLEnv) AcquireMagickMemory(sizeof(struct _MagickCLEnv)); |
| 323 | if (clEnv != NULL) |
| 324 | { |
| 325 | memset(clEnv, 0, sizeof(struct _MagickCLEnv)); |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 326 | clEnv->commandQueuesPos=-1; |
cristy | 04b11db | 2014-02-16 15:10:39 +0000 | [diff] [blame] | 327 | ActivateSemaphoreInfo(&clEnv->lock); |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 328 | ActivateSemaphoreInfo(&clEnv->commandQueuesLock); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 329 | } |
| 330 | return clEnv; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | /* |
| 335 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 336 | % % |
| 337 | % % |
| 338 | % % |
| 339 | + R e l i n q u i s h M a g i c k O p e n C L E n v % |
| 340 | % % |
| 341 | % % |
| 342 | % % |
| 343 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 344 | % |
| 345 | % RelinquishMagickOpenCLEnv() destroy the MagickCLEnv structure |
| 346 | % |
| 347 | % The format of the RelinquishMagickOpenCLEnv method is: |
| 348 | % |
| 349 | % MagickBooleanType RelinquishMagickOpenCLEnv(MagickCLEnv clEnv) |
| 350 | % |
| 351 | % A description of each parameter follows: |
| 352 | % |
| 353 | % o clEnv: MagickCLEnv structure to destroy |
| 354 | % |
| 355 | */ |
| 356 | |
| 357 | MagickExport MagickBooleanType RelinquishMagickOpenCLEnv(MagickCLEnv clEnv) |
| 358 | { |
cristy | f432c63 | 2014-12-07 15:11:28 +0000 | [diff] [blame] | 359 | if (clEnv != (MagickCLEnv) NULL) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 360 | { |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 361 | while (clEnv->commandQueuesPos >= 0) |
| 362 | { |
| 363 | clEnv->library->clReleaseCommandQueue( |
| 364 | clEnv->commandQueues[clEnv->commandQueuesPos--]); |
| 365 | } |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 366 | RelinquishSemaphoreInfo(&clEnv->lock); |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 367 | RelinquishSemaphoreInfo(&clEnv->commandQueuesLock); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 368 | RelinquishMagickMemory(clEnv); |
| 369 | return MagickTrue; |
| 370 | } |
| 371 | return MagickFalse; |
| 372 | } |
| 373 | |
| 374 | |
| 375 | /* |
| 376 | * Default OpenCL environment |
| 377 | */ |
| 378 | MagickCLEnv defaultCLEnv; |
| 379 | SemaphoreInfo* defaultCLEnvLock; |
| 380 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 381 | /* |
| 382 | * OpenCL library |
| 383 | */ |
| 384 | MagickLibrary * OpenCLLib; |
| 385 | SemaphoreInfo* OpenCLLibLock; |
| 386 | |
| 387 | |
| 388 | static MagickBooleanType bindOpenCLFunctions(void* library) |
| 389 | { |
| 390 | #ifdef MAGICKCORE_OPENCL_MACOSX |
| 391 | #define BIND(X) OpenCLLib->X= &X; |
| 392 | #else |
| 393 | #define BIND(X)\ |
| 394 | if ((OpenCLLib->X=(MAGICKpfn_##X)OsLibraryGetFunctionAddress(library,#X)) == NULL)\ |
| 395 | return MagickFalse; |
| 396 | #endif |
| 397 | |
| 398 | BIND(clGetPlatformIDs); |
| 399 | BIND(clGetPlatformInfo); |
| 400 | |
| 401 | BIND(clGetDeviceIDs); |
| 402 | BIND(clGetDeviceInfo); |
| 403 | |
| 404 | BIND(clCreateContext); |
| 405 | |
| 406 | BIND(clCreateBuffer); |
| 407 | BIND(clReleaseMemObject); |
| 408 | |
| 409 | BIND(clCreateProgramWithSource); |
| 410 | BIND(clCreateProgramWithBinary); |
| 411 | BIND(clBuildProgram); |
| 412 | BIND(clGetProgramInfo); |
| 413 | BIND(clGetProgramBuildInfo); |
| 414 | |
| 415 | BIND(clCreateKernel); |
| 416 | BIND(clReleaseKernel); |
| 417 | BIND(clSetKernelArg); |
| 418 | |
| 419 | BIND(clFlush); |
| 420 | BIND(clFinish); |
| 421 | |
| 422 | BIND(clEnqueueNDRangeKernel); |
| 423 | BIND(clEnqueueReadBuffer); |
| 424 | BIND(clEnqueueMapBuffer); |
| 425 | BIND(clEnqueueUnmapMemObject); |
| 426 | |
| 427 | BIND(clCreateCommandQueue); |
| 428 | BIND(clReleaseCommandQueue); |
| 429 | |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 430 | BIND(clGetEventProfilingInfo); |
| 431 | BIND(clWaitForEvents); |
| 432 | BIND(clReleaseEvent); |
| 433 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 434 | return MagickTrue; |
| 435 | } |
| 436 | |
| 437 | MagickLibrary * GetOpenCLLib() |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 438 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 439 | if (OpenCLLib == NULL) |
| 440 | { |
| 441 | if (OpenCLLibLock == NULL) |
| 442 | { |
| 443 | ActivateSemaphoreInfo(&OpenCLLibLock); |
| 444 | } |
| 445 | |
| 446 | LockSemaphoreInfo(OpenCLLibLock); |
| 447 | |
| 448 | OpenCLLib = (MagickLibrary *) AcquireMagickMemory (sizeof (MagickLibrary)); |
| 449 | |
| 450 | if (OpenCLLib != NULL) |
| 451 | { |
| 452 | MagickBooleanType status = MagickFalse; |
| 453 | void * library = NULL; |
| 454 | |
| 455 | #ifdef MAGICKCORE_OPENCL_MACOSX |
| 456 | status = bindOpenCLFunctions(library); |
| 457 | #else |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 458 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 459 | memset(OpenCLLib, 0, sizeof(MagickLibrary)); |
| 460 | #ifdef MAGICKCORE_WINDOWS_SUPPORT |
| 461 | library = OsLibraryLoad("OpenCL.dll"); |
| 462 | #else |
| 463 | library = OsLibraryLoad("libOpenCL.so"); |
| 464 | #endif |
| 465 | if (library) |
| 466 | status = bindOpenCLFunctions(library); |
| 467 | |
| 468 | if (status==MagickTrue) |
| 469 | OpenCLLib->base=library; |
| 470 | else |
| 471 | OpenCLLib=(MagickLibrary *)RelinquishMagickMemory(OpenCLLib); |
| 472 | #endif |
| 473 | } |
| 474 | |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 475 | UnlockSemaphoreInfo(OpenCLLibLock); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 476 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 477 | |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 478 | |
| 479 | return OpenCLLib; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 480 | } |
| 481 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 482 | |
| 483 | /* |
| 484 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 485 | % % |
| 486 | % % |
| 487 | % % |
| 488 | + G e t D e f a u l t O p e n C L E n v % |
| 489 | % % |
| 490 | % % |
| 491 | % % |
| 492 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 493 | % |
| 494 | % GetDefaultOpenCLEnv() returns the default OpenCL env |
| 495 | % |
| 496 | % The format of the GetDefaultOpenCLEnv method is: |
| 497 | % |
| 498 | % MagickCLEnv GetDefaultOpenCLEnv() |
| 499 | % |
| 500 | % A description of each parameter follows: |
| 501 | % |
| 502 | % o exception: return any errors or warnings. |
| 503 | % |
| 504 | */ |
| 505 | |
dirk | 69838ee | 2016-01-10 18:22:20 +0100 | [diff] [blame] | 506 | MagickExport MagickCLEnv GetDefaultOpenCLEnv(void) |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 507 | { |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 508 | if (defaultCLEnv == NULL) |
| 509 | { |
| 510 | if (defaultCLEnvLock == NULL) |
| 511 | { |
cristy | 04b11db | 2014-02-16 15:10:39 +0000 | [diff] [blame] | 512 | ActivateSemaphoreInfo(&defaultCLEnvLock); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 513 | } |
| 514 | LockSemaphoreInfo(defaultCLEnvLock); |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 515 | if (defaultCLEnv == NULL) |
| 516 | defaultCLEnv = AcquireMagickOpenCLEnv(); |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 517 | UnlockSemaphoreInfo(defaultCLEnvLock); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 518 | } |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 519 | return defaultCLEnv; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | static void LockDefaultOpenCLEnv() { |
| 523 | if (defaultCLEnvLock == NULL) |
| 524 | { |
cristy | 04b11db | 2014-02-16 15:10:39 +0000 | [diff] [blame] | 525 | ActivateSemaphoreInfo(&defaultCLEnvLock); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 526 | } |
| 527 | LockSemaphoreInfo(defaultCLEnvLock); |
| 528 | } |
| 529 | |
| 530 | static void UnlockDefaultOpenCLEnv() { |
| 531 | if (defaultCLEnvLock == NULL) |
| 532 | { |
cristy | 04b11db | 2014-02-16 15:10:39 +0000 | [diff] [blame] | 533 | ActivateSemaphoreInfo(&defaultCLEnvLock); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 534 | } |
| 535 | else |
| 536 | UnlockSemaphoreInfo(defaultCLEnvLock); |
| 537 | } |
| 538 | |
| 539 | |
| 540 | /* |
| 541 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 542 | % % |
| 543 | % % |
| 544 | % % |
| 545 | + S e t D e f a u l t O p e n C L E n v % |
| 546 | % % |
| 547 | % % |
| 548 | % % |
| 549 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 550 | % |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 551 | % SetDefaultOpenCLEnv() sets the new OpenCL environment as default |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 552 | % and returns the old OpenCL environment |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 553 | % |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 554 | % The format of the SetDefaultOpenCLEnv() method is: |
| 555 | % |
| 556 | % MagickCLEnv SetDefaultOpenCLEnv(MagickCLEnv clEnv) |
| 557 | % |
| 558 | % A description of each parameter follows: |
| 559 | % |
| 560 | % o clEnv: the new default OpenCL environment. |
| 561 | % |
| 562 | */ |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 563 | MagickExport MagickCLEnv SetDefaultOpenCLEnv(MagickCLEnv clEnv) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 564 | { |
| 565 | MagickCLEnv oldEnv; |
| 566 | LockDefaultOpenCLEnv(); |
| 567 | oldEnv = defaultCLEnv; |
| 568 | defaultCLEnv = clEnv; |
| 569 | UnlockDefaultOpenCLEnv(); |
| 570 | return oldEnv; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 571 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 572 | |
| 573 | |
| 574 | |
| 575 | /* |
| 576 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 577 | % % |
| 578 | % % |
| 579 | % % |
| 580 | + S e t M a g i c k O p e n C L E n v P a r a m % |
| 581 | % % |
| 582 | % % |
| 583 | % % |
| 584 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 585 | % |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 586 | % SetMagickOpenCLEnvParam() sets the parameters in the OpenCL environment |
| 587 | % |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 588 | % The format of the SetMagickOpenCLEnvParam() method is: |
| 589 | % |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 590 | % MagickBooleanType SetMagickOpenCLEnvParam(MagickCLEnv clEnv, |
| 591 | % MagickOpenCLEnvParam param, size_t dataSize, void* data, |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 592 | % ExceptionInfo* exception) |
| 593 | % |
| 594 | % A description of each parameter follows: |
| 595 | % |
| 596 | % o clEnv: the OpenCL environment. |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 597 | % |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 598 | % o param: the parameter to be set. |
| 599 | % |
| 600 | % o dataSize: the data size of the parameter value. |
| 601 | % |
| 602 | % o data: the pointer to the new parameter value |
| 603 | % |
| 604 | % o exception: return any errors or warnings |
| 605 | % |
| 606 | */ |
| 607 | |
| 608 | static MagickBooleanType SetMagickOpenCLEnvParamInternal(MagickCLEnv clEnv, MagickOpenCLEnvParam param |
| 609 | , size_t dataSize, void* data, ExceptionInfo* exception) |
| 610 | { |
| 611 | MagickBooleanType status = MagickFalse; |
| 612 | |
| 613 | if (clEnv == NULL |
| 614 | || data == NULL) |
| 615 | goto cleanup; |
| 616 | |
| 617 | switch(param) |
| 618 | { |
| 619 | case MAGICK_OPENCL_ENV_PARAM_DEVICE: |
| 620 | if (dataSize != sizeof(clEnv->device)) |
| 621 | goto cleanup; |
| 622 | clEnv->device = *((cl_device_id*)data); |
| 623 | clEnv->OpenCLInitialized = MagickFalse; |
| 624 | status = MagickTrue; |
| 625 | break; |
| 626 | |
| 627 | case MAGICK_OPENCL_ENV_PARAM_OPENCL_DISABLED: |
| 628 | if (dataSize != sizeof(clEnv->OpenCLDisabled)) |
| 629 | goto cleanup; |
| 630 | clEnv->OpenCLDisabled = *((MagickBooleanType*)data); |
| 631 | clEnv->OpenCLInitialized = MagickFalse; |
| 632 | status = MagickTrue; |
| 633 | break; |
| 634 | |
| 635 | case MAGICK_OPENCL_ENV_PARAM_OPENCL_INITIALIZED: |
| 636 | (void) ThrowMagickException(exception, GetMagickModule(), ModuleWarning, "SetMagickOpenCLEnvParm cannot modify the OpenCL initialization state.", "'%s'", "."); |
| 637 | break; |
| 638 | |
dirk | 20932d3 | 2013-12-12 06:16:19 +0000 | [diff] [blame] | 639 | case MAGICK_OPENCL_ENV_PARAM_PROGRAM_CACHE_DISABLED: |
| 640 | if (dataSize != sizeof(clEnv->disableProgramCache)) |
| 641 | goto cleanup; |
| 642 | clEnv->disableProgramCache = *((MagickBooleanType*)data); |
| 643 | clEnv->OpenCLInitialized = MagickFalse; |
| 644 | status = MagickTrue; |
| 645 | break; |
| 646 | |
| 647 | case MAGICK_OPENCL_ENV_PARAM_REGENERATE_PROFILE: |
| 648 | if (dataSize != sizeof(clEnv->regenerateProfile)) |
| 649 | goto cleanup; |
| 650 | clEnv->regenerateProfile = *((MagickBooleanType*)data); |
| 651 | clEnv->OpenCLInitialized = MagickFalse; |
| 652 | status = MagickTrue; |
| 653 | break; |
| 654 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 655 | default: |
| 656 | goto cleanup; |
| 657 | }; |
| 658 | |
| 659 | cleanup: |
| 660 | return status; |
| 661 | } |
| 662 | |
| 663 | MagickExport |
| 664 | MagickBooleanType SetMagickOpenCLEnvParam(MagickCLEnv clEnv, MagickOpenCLEnvParam param |
| 665 | , size_t dataSize, void* data, ExceptionInfo* exception) { |
| 666 | MagickBooleanType status = MagickFalse; |
| 667 | if (clEnv!=NULL) { |
| 668 | LockSemaphoreInfo(clEnv->lock); |
| 669 | status = SetMagickOpenCLEnvParamInternal(clEnv,param,dataSize,data,exception); |
| 670 | UnlockSemaphoreInfo(clEnv->lock); |
| 671 | } |
| 672 | return status; |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 677 | % % |
| 678 | % % |
| 679 | % % |
| 680 | + G e t M a g i c k O p e n C L E n v P a r a m % |
| 681 | % % |
| 682 | % % |
| 683 | % % |
| 684 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 685 | % |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 686 | % GetMagickOpenCLEnvParam() gets the parameters in the OpenCL environment |
| 687 | % |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 688 | % The format of the GetMagickOpenCLEnvParam() method is: |
| 689 | % |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 690 | % MagickBooleanType GetMagickOpenCLEnvParam(MagickCLEnv clEnv, |
| 691 | % MagickOpenCLEnvParam param, size_t dataSize, void* data, |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 692 | % ExceptionInfo* exception) |
| 693 | % |
| 694 | % A description of each parameter follows: |
| 695 | % |
| 696 | % o clEnv: the OpenCL environment. |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 697 | % |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 698 | % o param: the parameter to be returned. |
| 699 | % |
| 700 | % o dataSize: the data size of the parameter value. |
| 701 | % |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 702 | % o data: the location where the returned parameter value will be stored |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 703 | % |
| 704 | % o exception: return any errors or warnings |
| 705 | % |
| 706 | */ |
| 707 | |
| 708 | MagickExport |
| 709 | MagickBooleanType GetMagickOpenCLEnvParam(MagickCLEnv clEnv, MagickOpenCLEnvParam param |
| 710 | , size_t dataSize, void* data, ExceptionInfo* exception) |
| 711 | { |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 712 | MagickBooleanType |
dirk | 5dcb762 | 2013-12-01 10:43:43 +0000 | [diff] [blame] | 713 | status; |
| 714 | |
| 715 | magick_unreferenced(exception); |
| 716 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 717 | status = MagickFalse; |
| 718 | |
| 719 | if (clEnv == NULL |
| 720 | || data == NULL) |
| 721 | goto cleanup; |
| 722 | |
| 723 | switch(param) |
| 724 | { |
| 725 | case MAGICK_OPENCL_ENV_PARAM_DEVICE: |
| 726 | if (dataSize != sizeof(cl_device_id)) |
| 727 | goto cleanup; |
| 728 | *((cl_device_id*)data) = clEnv->device; |
| 729 | status = MagickTrue; |
| 730 | break; |
| 731 | |
| 732 | case MAGICK_OPENCL_ENV_PARAM_OPENCL_DISABLED: |
| 733 | if (dataSize != sizeof(clEnv->OpenCLDisabled)) |
| 734 | goto cleanup; |
| 735 | *((MagickBooleanType*)data) = clEnv->OpenCLDisabled; |
| 736 | status = MagickTrue; |
| 737 | break; |
| 738 | |
| 739 | case MAGICK_OPENCL_ENV_PARAM_OPENCL_INITIALIZED: |
| 740 | if (dataSize != sizeof(clEnv->OpenCLDisabled)) |
| 741 | goto cleanup; |
| 742 | *((MagickBooleanType*)data) = clEnv->OpenCLInitialized; |
| 743 | status = MagickTrue; |
| 744 | break; |
| 745 | |
dirk | 20932d3 | 2013-12-12 06:16:19 +0000 | [diff] [blame] | 746 | case MAGICK_OPENCL_ENV_PARAM_PROGRAM_CACHE_DISABLED: |
| 747 | if (dataSize != sizeof(clEnv->disableProgramCache)) |
| 748 | goto cleanup; |
| 749 | *((MagickBooleanType*)data) = clEnv->disableProgramCache; |
| 750 | status = MagickTrue; |
| 751 | break; |
| 752 | |
| 753 | case MAGICK_OPENCL_ENV_PARAM_REGENERATE_PROFILE: |
| 754 | if (dataSize != sizeof(clEnv->regenerateProfile)) |
| 755 | goto cleanup; |
| 756 | *((MagickBooleanType*)data) = clEnv->regenerateProfile; |
| 757 | status = MagickTrue; |
| 758 | break; |
| 759 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 760 | default: |
| 761 | goto cleanup; |
| 762 | }; |
| 763 | |
| 764 | cleanup: |
| 765 | return status; |
| 766 | } |
| 767 | |
| 768 | |
| 769 | /* |
| 770 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 771 | % % |
| 772 | % % |
| 773 | % % |
| 774 | + G e t O p e n C L C o n t e x t % |
| 775 | % % |
| 776 | % % |
| 777 | % % |
| 778 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 779 | % |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 780 | % GetOpenCLContext() returns the OpenCL context |
| 781 | % |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 782 | % The format of the GetOpenCLContext() method is: |
| 783 | % |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 784 | % cl_context GetOpenCLContext(MagickCLEnv clEnv) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 785 | % |
| 786 | % A description of each parameter follows: |
| 787 | % |
| 788 | % o clEnv: OpenCL environment |
| 789 | % |
| 790 | */ |
| 791 | |
cristy | 7b6514c | 2013-12-10 23:13:13 +0000 | [diff] [blame] | 792 | MagickPrivate |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 793 | cl_context GetOpenCLContext(MagickCLEnv clEnv) { |
| 794 | if (clEnv == NULL) |
| 795 | return NULL; |
| 796 | else |
| 797 | return clEnv->context; |
| 798 | } |
| 799 | |
| 800 | static char* getBinaryCLProgramName(MagickCLEnv clEnv, MagickOpenCLProgram prog, unsigned int signature) |
| 801 | { |
| 802 | char* name; |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 803 | char* ptr; |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 804 | char path[MagickPathExtent]; |
| 805 | char deviceName[MagickPathExtent]; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 806 | const char* prefix = "magick_opencl"; |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 807 | clEnv->library->clGetDeviceInfo(clEnv->device, CL_DEVICE_NAME, MagickPathExtent, deviceName, NULL); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 808 | ptr=deviceName; |
| 809 | /* strip out illegal characters for file names */ |
| 810 | while (*ptr != '\0') |
| 811 | { |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 812 | if ( *ptr == ' ' || *ptr == '\\' || *ptr == '/' || *ptr == ':' || *ptr == '*' |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 813 | || *ptr == '?' || *ptr == '"' || *ptr == '<' || *ptr == '>' || *ptr == '|') |
| 814 | { |
| 815 | *ptr = '_'; |
| 816 | } |
| 817 | ptr++; |
| 818 | } |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 819 | (void) FormatLocaleString(path,MagickPathExtent,"%s%s%s_%s_%02d_%08x_%.20g.bin", |
dirk | d091dc3 | 2013-12-11 12:26:40 +0000 | [diff] [blame] | 820 | GetOpenCLCachedFilesDirectory(),DirectorySeparator,prefix,deviceName, |
dirk | 584cf81 | 2013-12-12 07:59:15 +0000 | [diff] [blame] | 821 | (unsigned int) prog,signature,(double) sizeof(char*)*8); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 822 | name = (char*)AcquireMagickMemory(strlen(path)+1); |
| 823 | CopyMagickString(name,path,strlen(path)+1); |
| 824 | return name; |
| 825 | } |
| 826 | |
| 827 | static MagickBooleanType saveBinaryCLProgram(MagickCLEnv clEnv, MagickOpenCLProgram prog, unsigned int signature, ExceptionInfo* exception) |
| 828 | { |
| 829 | MagickBooleanType saveSuccessful; |
| 830 | cl_int clStatus; |
| 831 | size_t binaryProgramSize; |
| 832 | unsigned char* binaryProgram; |
| 833 | char* binaryFileName; |
| 834 | FILE* fileHandle; |
| 835 | |
| 836 | #ifdef MAGICKCORE_CLPERFMARKER |
| 837 | clBeginPerfMarkerAMD(__FUNCTION__,""); |
| 838 | #endif |
| 839 | |
| 840 | binaryProgram = NULL; |
| 841 | binaryFileName = NULL; |
| 842 | fileHandle = NULL; |
| 843 | saveSuccessful = MagickFalse; |
| 844 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 845 | clStatus = clEnv->library->clGetProgramInfo(clEnv->programs[prog], CL_PROGRAM_BINARY_SIZES, sizeof(size_t), &binaryProgramSize, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 846 | if (clStatus != CL_SUCCESS) |
| 847 | { |
| 848 | (void) ThrowMagickException(exception, GetMagickModule(), ModuleFatalError, "clGetProgramInfo failed.", "'%s'", "."); |
| 849 | goto cleanup; |
| 850 | } |
| 851 | |
| 852 | binaryProgram = (unsigned char*) AcquireMagickMemory(binaryProgramSize); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 853 | clStatus = clEnv->library->clGetProgramInfo(clEnv->programs[prog], CL_PROGRAM_BINARIES, sizeof(char*), &binaryProgram, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 854 | if (clStatus != CL_SUCCESS) |
| 855 | { |
| 856 | (void) ThrowMagickException(exception, GetMagickModule(), ModuleFatalError, "clGetProgramInfo failed.", "'%s'", "."); |
| 857 | goto cleanup; |
| 858 | } |
| 859 | |
| 860 | binaryFileName = getBinaryCLProgramName(clEnv, prog, signature); |
| 861 | fileHandle = fopen(binaryFileName, "wb"); |
| 862 | if (fileHandle != NULL) |
| 863 | { |
| 864 | fwrite(binaryProgram, sizeof(char), binaryProgramSize, fileHandle); |
| 865 | saveSuccessful = MagickTrue; |
| 866 | } |
| 867 | else |
| 868 | { |
| 869 | (void) ThrowMagickException(exception, GetMagickModule(), DelegateWarning, |
| 870 | "Saving binary kernel failed.", "'%s'", "."); |
| 871 | } |
| 872 | |
| 873 | cleanup: |
| 874 | if (fileHandle != NULL) |
| 875 | fclose(fileHandle); |
| 876 | if (binaryProgram != NULL) |
| 877 | RelinquishMagickMemory(binaryProgram); |
| 878 | if (binaryFileName != NULL) |
| 879 | free(binaryFileName); |
| 880 | |
| 881 | #ifdef MAGICKCORE_CLPERFMARKER |
| 882 | clEndPerfMarkerAMD(); |
| 883 | #endif |
| 884 | |
| 885 | return saveSuccessful; |
| 886 | } |
| 887 | |
dirk | 5dcb762 | 2013-12-01 10:43:43 +0000 | [diff] [blame] | 888 | static MagickBooleanType loadBinaryCLProgram(MagickCLEnv clEnv, MagickOpenCLProgram prog, unsigned int signature) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 889 | { |
| 890 | MagickBooleanType loadSuccessful; |
| 891 | unsigned char* binaryProgram; |
| 892 | char* binaryFileName; |
| 893 | FILE* fileHandle; |
| 894 | |
| 895 | #ifdef MAGICKCORE_CLPERFMARKER |
| 896 | clBeginPerfMarkerAMD(__FUNCTION__,""); |
| 897 | #endif |
| 898 | |
| 899 | binaryProgram = NULL; |
| 900 | binaryFileName = NULL; |
| 901 | fileHandle = NULL; |
| 902 | loadSuccessful = MagickFalse; |
| 903 | |
| 904 | binaryFileName = getBinaryCLProgramName(clEnv, prog, signature); |
| 905 | fileHandle = fopen(binaryFileName, "rb"); |
| 906 | if (fileHandle != NULL) |
| 907 | { |
| 908 | int b_error; |
| 909 | size_t length; |
| 910 | cl_int clStatus; |
| 911 | cl_int clBinaryStatus; |
| 912 | |
| 913 | b_error = 0 ; |
| 914 | length = 0; |
| 915 | b_error |= fseek( fileHandle, 0, SEEK_END ) < 0; |
| 916 | b_error |= ( length = ftell( fileHandle ) ) <= 0; |
| 917 | b_error |= fseek( fileHandle, 0, SEEK_SET ) < 0; |
| 918 | if( b_error ) |
| 919 | goto cleanup; |
| 920 | |
| 921 | binaryProgram = (unsigned char*)AcquireMagickMemory(length); |
| 922 | if (binaryProgram == NULL) |
| 923 | goto cleanup; |
| 924 | |
| 925 | memset(binaryProgram, 0, length); |
| 926 | b_error |= fread(binaryProgram, 1, length, fileHandle) != length; |
| 927 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 928 | clEnv->programs[prog] = clEnv->library->clCreateProgramWithBinary(clEnv->context, 1, &clEnv->device, &length, (const unsigned char**)&binaryProgram, &clBinaryStatus, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 929 | if (clStatus != CL_SUCCESS |
| 930 | || clBinaryStatus != CL_SUCCESS) |
| 931 | goto cleanup; |
| 932 | |
| 933 | loadSuccessful = MagickTrue; |
| 934 | } |
| 935 | |
| 936 | cleanup: |
| 937 | if (fileHandle != NULL) |
| 938 | fclose(fileHandle); |
| 939 | if (binaryFileName != NULL) |
| 940 | free(binaryFileName); |
| 941 | if (binaryProgram != NULL) |
| 942 | RelinquishMagickMemory(binaryProgram); |
| 943 | |
| 944 | #ifdef MAGICKCORE_CLPERFMARKER |
| 945 | clEndPerfMarkerAMD(); |
| 946 | #endif |
| 947 | |
| 948 | return loadSuccessful; |
| 949 | } |
| 950 | |
| 951 | static unsigned int stringSignature(const char* string) |
| 952 | { |
| 953 | unsigned int stringLength; |
| 954 | unsigned int n,i,j; |
| 955 | unsigned int signature; |
| 956 | union |
| 957 | { |
| 958 | const char* s; |
| 959 | const unsigned int* u; |
| 960 | }p; |
| 961 | |
| 962 | #ifdef MAGICKCORE_CLPERFMARKER |
| 963 | clBeginPerfMarkerAMD(__FUNCTION__,""); |
| 964 | #endif |
| 965 | |
dirk | b0d783f | 2014-08-31 10:48:05 +0000 | [diff] [blame] | 966 | stringLength = (unsigned int) strlen(string); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 967 | signature = stringLength; |
| 968 | n = stringLength/sizeof(unsigned int); |
| 969 | p.s = string; |
| 970 | for (i = 0; i < n; i++) |
| 971 | { |
| 972 | signature^=p.u[i]; |
| 973 | } |
| 974 | if (n * sizeof(unsigned int) != stringLength) |
| 975 | { |
| 976 | char padded[4]; |
| 977 | j = n * sizeof(unsigned int); |
| 978 | for (i = 0; i < 4; i++,j++) |
| 979 | { |
| 980 | if (j < stringLength) |
| 981 | padded[i] = p.s[j]; |
| 982 | else |
| 983 | padded[i] = 0; |
| 984 | } |
| 985 | p.s = padded; |
| 986 | signature^=p.u[0]; |
| 987 | } |
| 988 | |
| 989 | #ifdef MAGICKCORE_CLPERFMARKER |
| 990 | clEndPerfMarkerAMD(); |
| 991 | #endif |
| 992 | |
| 993 | return signature; |
| 994 | } |
| 995 | |
| 996 | /* OpenCL kernels for accelerate.c */ |
| 997 | extern const char *accelerateKernels, *accelerateKernels2; |
| 998 | |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 999 | static MagickBooleanType CompileOpenCLKernels(MagickCLEnv clEnv, ExceptionInfo* exception) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1000 | { |
| 1001 | MagickBooleanType status = MagickFalse; |
| 1002 | cl_int clStatus; |
| 1003 | unsigned int i; |
| 1004 | char* accelerateKernelsBuffer = NULL; |
| 1005 | |
| 1006 | /* The index of the program strings in this array has to match the value of the enum MagickOpenCLProgram */ |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 1007 | const char* MagickOpenCLProgramStrings[MAGICK_OPENCL_NUM_PROGRAMS]; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1008 | |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1009 | char options[MagickPathExtent]; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1010 | unsigned int optionsSignature; |
| 1011 | |
| 1012 | #ifdef MAGICKCORE_CLPERFMARKER |
| 1013 | clBeginPerfMarkerAMD(__FUNCTION__,""); |
| 1014 | #endif |
| 1015 | |
| 1016 | /* Get additional options */ |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1017 | (void) FormatLocaleString(options, MagickPathExtent, CLOptions, (float)QuantumRange, |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1018 | (float)QuantumScale, (float)CLCharQuantumScale, (float)MagickEpsilon, (float)MagickPI, (unsigned int)MaxMap, (unsigned int)MAGICKCORE_QUANTUM_DEPTH); |
| 1019 | |
| 1020 | /* |
| 1021 | if (getenv("MAGICK_OCL_DEF")) |
| 1022 | { |
| 1023 | strcat(options," "); |
| 1024 | strcat(options,getenv("MAGICK_OCL_DEF")); |
| 1025 | } |
| 1026 | */ |
| 1027 | |
| 1028 | /* |
| 1029 | if (getenv("MAGICK_OCL_BUILD")) |
| 1030 | printf("options: %s\n", options); |
| 1031 | */ |
| 1032 | |
| 1033 | optionsSignature = stringSignature(options); |
| 1034 | |
| 1035 | /* get all the OpenCL program strings here */ |
| 1036 | accelerateKernelsBuffer = (char*) AcquireMagickMemory(strlen(accelerateKernels)+strlen(accelerateKernels2)+1); |
| 1037 | sprintf(accelerateKernelsBuffer,"%s%s",accelerateKernels,accelerateKernels2); |
| 1038 | MagickOpenCLProgramStrings[MAGICK_OPENCL_ACCELERATE] = accelerateKernelsBuffer; |
| 1039 | |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 1040 | for (i = 0; i < MAGICK_OPENCL_NUM_PROGRAMS; i++) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1041 | { |
| 1042 | MagickBooleanType loadSuccessful = MagickFalse; |
| 1043 | unsigned int programSignature = stringSignature(MagickOpenCLProgramStrings[i]) ^ optionsSignature; |
| 1044 | |
| 1045 | /* try to load the binary first */ |
dirk | 20932d3 | 2013-12-12 06:16:19 +0000 | [diff] [blame] | 1046 | if (clEnv->disableProgramCache != MagickTrue |
| 1047 | && !getenv("MAGICK_OCL_REC")) |
dirk | 5dcb762 | 2013-12-01 10:43:43 +0000 | [diff] [blame] | 1048 | loadSuccessful = loadBinaryCLProgram(clEnv, (MagickOpenCLProgram)i, programSignature); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1049 | |
| 1050 | if (loadSuccessful == MagickFalse) |
| 1051 | { |
| 1052 | /* Binary CL program unavailable, compile the program from source */ |
| 1053 | size_t programLength = strlen(MagickOpenCLProgramStrings[i]); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1054 | clEnv->programs[i] = clEnv->library->clCreateProgramWithSource(clEnv->context, 1, &(MagickOpenCLProgramStrings[i]), &programLength, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1055 | if (clStatus!=CL_SUCCESS) |
| 1056 | { |
| 1057 | (void) ThrowMagickException(exception, GetMagickModule(), DelegateWarning, |
| 1058 | "clCreateProgramWithSource failed.", "(%d)", (int)clStatus); |
| 1059 | |
| 1060 | goto cleanup; |
| 1061 | } |
| 1062 | } |
| 1063 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1064 | clStatus = clEnv->library->clBuildProgram(clEnv->programs[i], 1, &clEnv->device, options, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1065 | if (clStatus!=CL_SUCCESS) |
| 1066 | { |
| 1067 | (void) ThrowMagickException(exception, GetMagickModule(), DelegateWarning, |
| 1068 | "clBuildProgram failed.", "(%d)", (int)clStatus); |
| 1069 | |
| 1070 | if (loadSuccessful == MagickFalse) |
| 1071 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1072 | char path[MagickPathExtent]; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1073 | FILE* fileHandle; |
| 1074 | |
| 1075 | /* dump the source into a file */ |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1076 | (void) FormatLocaleString(path,MagickPathExtent,"%s%s%s" |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1077 | ,GetOpenCLCachedFilesDirectory() |
| 1078 | ,DirectorySeparator,"magick_badcl.cl"); |
| 1079 | fileHandle = fopen(path, "wb"); |
| 1080 | if (fileHandle != NULL) |
| 1081 | { |
| 1082 | fwrite(MagickOpenCLProgramStrings[i], sizeof(char), strlen(MagickOpenCLProgramStrings[i]), fileHandle); |
| 1083 | fclose(fileHandle); |
| 1084 | } |
| 1085 | |
| 1086 | /* dump the build log */ |
| 1087 | { |
| 1088 | char* log; |
| 1089 | size_t logSize; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1090 | clEnv->library->clGetProgramBuildInfo(clEnv->programs[i], clEnv->device, CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1091 | log = (char*)AcquireMagickMemory(logSize); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1092 | clEnv->library->clGetProgramBuildInfo(clEnv->programs[i], clEnv->device, CL_PROGRAM_BUILD_LOG, logSize, log, &logSize); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1093 | |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1094 | (void) FormatLocaleString(path,MagickPathExtent,"%s%s%s" |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1095 | ,GetOpenCLCachedFilesDirectory() |
| 1096 | ,DirectorySeparator,"magick_badcl_build.log"); |
| 1097 | fileHandle = fopen(path, "wb"); |
| 1098 | if (fileHandle != NULL) |
| 1099 | { |
| 1100 | const char* buildOptionsTitle = "build options: "; |
| 1101 | fwrite(buildOptionsTitle, sizeof(char), strlen(buildOptionsTitle), fileHandle); |
| 1102 | fwrite(options, sizeof(char), strlen(options), fileHandle); |
| 1103 | fwrite("\n",sizeof(char), 1, fileHandle); |
| 1104 | fwrite(log, sizeof(char), logSize, fileHandle); |
| 1105 | fclose(fileHandle); |
| 1106 | } |
| 1107 | RelinquishMagickMemory(log); |
| 1108 | } |
| 1109 | } |
| 1110 | goto cleanup; |
| 1111 | } |
| 1112 | |
| 1113 | if (loadSuccessful == MagickFalse) |
| 1114 | { |
| 1115 | /* Save the binary to a file to avoid re-compilation of the kernels in the future */ |
| 1116 | saveBinaryCLProgram(clEnv, (MagickOpenCLProgram)i, programSignature, exception); |
| 1117 | } |
| 1118 | |
| 1119 | } |
| 1120 | status = MagickTrue; |
| 1121 | |
| 1122 | cleanup: |
| 1123 | |
| 1124 | if (accelerateKernelsBuffer!=NULL) RelinquishMagickMemory(accelerateKernelsBuffer); |
| 1125 | |
| 1126 | #ifdef MAGICKCORE_CLPERFMARKER |
| 1127 | clEndPerfMarkerAMD(); |
| 1128 | #endif |
| 1129 | |
| 1130 | return status; |
| 1131 | } |
| 1132 | |
| 1133 | static MagickBooleanType InitOpenCLPlatformDevice(MagickCLEnv clEnv, ExceptionInfo* exception) { |
| 1134 | int i,j; |
| 1135 | cl_int status; |
| 1136 | cl_uint numPlatforms = 0; |
| 1137 | cl_platform_id *platforms = NULL; |
| 1138 | char* MAGICK_OCL_DEVICE = NULL; |
| 1139 | MagickBooleanType OpenCLAvailable = MagickFalse; |
| 1140 | |
| 1141 | #ifdef MAGICKCORE_CLPERFMARKER |
| 1142 | clBeginPerfMarkerAMD(__FUNCTION__,""); |
| 1143 | #endif |
| 1144 | |
| 1145 | /* check if there's an environment variable overriding the device selection */ |
| 1146 | MAGICK_OCL_DEVICE = getenv("MAGICK_OCL_DEVICE"); |
| 1147 | if (MAGICK_OCL_DEVICE != NULL) |
| 1148 | { |
| 1149 | if (strcmp(MAGICK_OCL_DEVICE, "CPU") == 0) |
| 1150 | { |
| 1151 | clEnv->deviceType = CL_DEVICE_TYPE_CPU; |
| 1152 | } |
| 1153 | else if (strcmp(MAGICK_OCL_DEVICE, "GPU") == 0) |
| 1154 | { |
| 1155 | clEnv->deviceType = CL_DEVICE_TYPE_GPU; |
| 1156 | } |
| 1157 | else if (strcmp(MAGICK_OCL_DEVICE, "OFF") == 0) |
| 1158 | { |
| 1159 | /* OpenCL disabled */ |
| 1160 | goto cleanup; |
| 1161 | } |
| 1162 | } |
| 1163 | else if (clEnv->deviceType == 0) { |
| 1164 | clEnv->deviceType = CL_DEVICE_TYPE_ALL; |
| 1165 | } |
| 1166 | |
| 1167 | if (clEnv->device != NULL) |
| 1168 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1169 | status = clEnv->library->clGetDeviceInfo(clEnv->device, CL_DEVICE_PLATFORM, sizeof(cl_platform_id), &clEnv->platform, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1170 | if (status != CL_SUCCESS) { |
| 1171 | (void) ThrowMagickException(exception, GetMagickModule(), DelegateWarning, |
| 1172 | "Failed to get OpenCL platform from the selected device.", "(%d)", status); |
| 1173 | } |
| 1174 | goto cleanup; |
| 1175 | } |
| 1176 | else if (clEnv->platform != NULL) |
| 1177 | { |
| 1178 | numPlatforms = 1; |
| 1179 | platforms = (cl_platform_id *) AcquireMagickMemory(numPlatforms * sizeof(cl_platform_id)); |
| 1180 | if (platforms == (cl_platform_id *) NULL) |
| 1181 | { |
| 1182 | (void) ThrowMagickException(exception, GetMagickModule(), ResourceLimitError, |
| 1183 | "AcquireMagickMemory failed.","."); |
| 1184 | goto cleanup; |
| 1185 | } |
| 1186 | platforms[0] = clEnv->platform; |
| 1187 | } |
| 1188 | else |
| 1189 | { |
| 1190 | clEnv->device = NULL; |
| 1191 | |
| 1192 | /* Get the number of OpenCL platforms available */ |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1193 | status = clEnv->library->clGetPlatformIDs(0, NULL, &numPlatforms); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1194 | if (status != CL_SUCCESS) |
| 1195 | { |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 1196 | (void) ThrowMagickException(exception, GetMagickModule(), DelegateWarning, |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1197 | "clGetplatformIDs failed.", "(%d)", status); |
| 1198 | goto cleanup; |
| 1199 | } |
| 1200 | |
| 1201 | /* No OpenCL available, just leave */ |
| 1202 | if (numPlatforms == 0) { |
| 1203 | goto cleanup; |
| 1204 | } |
| 1205 | |
| 1206 | platforms = (cl_platform_id *) AcquireMagickMemory(numPlatforms * sizeof(cl_platform_id)); |
| 1207 | if (platforms == (cl_platform_id *) NULL) |
| 1208 | { |
| 1209 | (void) ThrowMagickException(exception, GetMagickModule(), ResourceLimitError, |
| 1210 | "AcquireMagickMemory failed.","."); |
| 1211 | goto cleanup; |
| 1212 | } |
| 1213 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1214 | status = clEnv->library->clGetPlatformIDs(numPlatforms, platforms, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1215 | if (status != CL_SUCCESS) |
| 1216 | { |
| 1217 | (void) ThrowMagickException(exception, GetMagickModule(), DelegateWarning, |
| 1218 | "clGetPlatformIDs failed.", "(%d)", status); |
| 1219 | goto cleanup; |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | /* Device selection */ |
| 1224 | clEnv->device = NULL; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 1225 | for (j = 0; j < 2; j++) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1226 | { |
| 1227 | |
| 1228 | cl_device_type deviceType; |
| 1229 | if (clEnv->deviceType == CL_DEVICE_TYPE_ALL) |
| 1230 | { |
| 1231 | if (j == 0) |
| 1232 | deviceType = CL_DEVICE_TYPE_GPU; |
| 1233 | else |
| 1234 | deviceType = CL_DEVICE_TYPE_CPU; |
| 1235 | } |
| 1236 | else if (j == 1) |
| 1237 | { |
| 1238 | break; |
| 1239 | } |
| 1240 | else |
| 1241 | deviceType = clEnv->deviceType; |
| 1242 | |
| 1243 | for (i = 0; i < numPlatforms; i++) |
| 1244 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1245 | char version[MagickPathExtent]; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1246 | cl_uint numDevices; |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1247 | status = clEnv->library->clGetPlatformInfo(clEnv->platform, CL_PLATFORM_VERSION, MagickPathExtent, version, NULL); |
dirk | afb5e94 | 2014-07-11 18:20:52 +0000 | [diff] [blame] | 1248 | if (status != CL_SUCCESS) |
| 1249 | { |
| 1250 | (void) ThrowMagickException(exception, GetMagickModule(), DelegateWarning, |
| 1251 | "clGetPlatformInfo failed.", "(%d)", status); |
| 1252 | goto cleanup; |
| 1253 | } |
| 1254 | if (strncmp(version,"OpenCL 1.0 ",11) == 0) |
| 1255 | continue; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1256 | status = clEnv->library->clGetDeviceIDs(platforms[i], deviceType, 1, &(clEnv->device), &numDevices); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1257 | if (status != CL_SUCCESS) |
| 1258 | { |
| 1259 | (void) ThrowMagickException(exception, GetMagickModule(), DelegateWarning, |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1260 | "clGetDeviceIDs failed.", "(%d)", status); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1261 | goto cleanup; |
| 1262 | } |
| 1263 | if (clEnv->device != NULL) |
| 1264 | { |
| 1265 | clEnv->platform = platforms[i]; |
| 1266 | goto cleanup; |
| 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | cleanup: |
| 1272 | if (platforms!=NULL) |
| 1273 | RelinquishMagickMemory(platforms); |
| 1274 | |
| 1275 | OpenCLAvailable = (clEnv->platform!=NULL |
| 1276 | && clEnv->device!=NULL)?MagickTrue:MagickFalse; |
| 1277 | |
| 1278 | #ifdef MAGICKCORE_CLPERFMARKER |
| 1279 | clEndPerfMarkerAMD(); |
| 1280 | #endif |
| 1281 | |
| 1282 | return OpenCLAvailable; |
| 1283 | } |
| 1284 | |
| 1285 | static MagickBooleanType EnableOpenCLInternal(MagickCLEnv clEnv) { |
cristy | cd8b331 | 2013-12-22 01:51:11 +0000 | [diff] [blame] | 1286 | if (clEnv->OpenCLInitialized != MagickFalse |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1287 | && clEnv->platform != NULL |
| 1288 | && clEnv->device != NULL) { |
| 1289 | clEnv->OpenCLDisabled = MagickFalse; |
| 1290 | return MagickTrue; |
| 1291 | } |
| 1292 | clEnv->OpenCLDisabled = MagickTrue; |
| 1293 | return MagickFalse; |
| 1294 | } |
| 1295 | |
| 1296 | |
| 1297 | static MagickBooleanType autoSelectDevice(MagickCLEnv clEnv, ExceptionInfo* exception); |
| 1298 | /* |
| 1299 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1300 | % % |
| 1301 | % % |
| 1302 | % % |
| 1303 | + I n i t O p e n C L E n v % |
| 1304 | % % |
| 1305 | % % |
| 1306 | % % |
| 1307 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1308 | % |
| 1309 | % InitOpenCLEnv() initialize the OpenCL environment |
| 1310 | % |
| 1311 | % The format of the RelinquishMagickOpenCLEnv method is: |
| 1312 | % |
| 1313 | % MagickBooleanType InitOpenCLEnv(MagickCLEnv clEnv, ExceptionInfo* exception) |
| 1314 | % |
| 1315 | % A description of each parameter follows: |
| 1316 | % |
| 1317 | % o clEnv: OpenCL environment structure |
| 1318 | % |
| 1319 | % o exception: return any errors or warnings. |
| 1320 | % |
| 1321 | */ |
| 1322 | |
| 1323 | MagickExport |
| 1324 | MagickBooleanType InitOpenCLEnvInternal(MagickCLEnv clEnv, ExceptionInfo* exception) { |
| 1325 | MagickBooleanType status = MagickTrue; |
| 1326 | cl_int clStatus; |
| 1327 | cl_context_properties cps[3]; |
| 1328 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1329 | #ifdef MAGICKCORE_CLPERFMARKER |
| 1330 | { |
| 1331 | int status = clInitializePerfMarkerAMD(); |
| 1332 | if (status == AP_SUCCESS) { |
| 1333 | //printf("PerfMarker successfully initialized\n"); |
| 1334 | } |
| 1335 | } |
| 1336 | #endif |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1337 | clEnv->OpenCLInitialized = MagickTrue; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1338 | |
| 1339 | /* check and init the global lib */ |
| 1340 | OpenCLLib=GetOpenCLLib(); |
| 1341 | if (OpenCLLib) |
| 1342 | { |
| 1343 | clEnv->library=OpenCLLib; |
| 1344 | } |
| 1345 | else |
| 1346 | { |
| 1347 | /* turn off opencl */ |
| 1348 | MagickBooleanType flag; |
| 1349 | flag = MagickTrue; |
| 1350 | SetMagickOpenCLEnvParamInternal(clEnv, MAGICK_OPENCL_ENV_PARAM_OPENCL_DISABLED |
| 1351 | , sizeof(MagickBooleanType), &flag, exception); |
| 1352 | } |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 1353 | |
cristy | cd8b331 | 2013-12-22 01:51:11 +0000 | [diff] [blame] | 1354 | if (clEnv->OpenCLDisabled != MagickFalse) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1355 | goto cleanup; |
| 1356 | |
| 1357 | clEnv->OpenCLDisabled = MagickTrue; |
| 1358 | /* setup the OpenCL platform and device */ |
| 1359 | status = InitOpenCLPlatformDevice(clEnv, exception); |
| 1360 | if (status == MagickFalse) { |
| 1361 | /* No OpenCL device available */ |
| 1362 | goto cleanup; |
| 1363 | } |
| 1364 | |
| 1365 | /* create an OpenCL context */ |
| 1366 | cps[0] = CL_CONTEXT_PLATFORM; |
| 1367 | cps[1] = (cl_context_properties)clEnv->platform; |
| 1368 | cps[2] = 0; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1369 | clEnv->context = clEnv->library->clCreateContext(cps, 1, &(clEnv->device), NULL, NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1370 | if (clStatus != CL_SUCCESS) |
| 1371 | { |
| 1372 | (void) ThrowMagickException(exception, GetMagickModule(), DelegateWarning, |
| 1373 | "clCreateContext failed.", "(%d)", clStatus); |
| 1374 | status = MagickFalse; |
| 1375 | goto cleanup; |
| 1376 | } |
| 1377 | |
| 1378 | status = CompileOpenCLKernels(clEnv, exception); |
| 1379 | if (status == MagickFalse) { |
| 1380 | (void) ThrowMagickException(exception, GetMagickModule(), DelegateWarning, |
| 1381 | "clCreateCommandQueue failed.", "(%d)", status); |
| 1382 | |
| 1383 | status = MagickFalse; |
| 1384 | goto cleanup; |
| 1385 | } |
| 1386 | |
| 1387 | status = EnableOpenCLInternal(clEnv); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1388 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1389 | cleanup: |
| 1390 | return status; |
| 1391 | } |
| 1392 | |
| 1393 | |
| 1394 | MagickExport |
| 1395 | MagickBooleanType InitOpenCLEnv(MagickCLEnv clEnv, ExceptionInfo* exception) { |
| 1396 | MagickBooleanType status = MagickFalse; |
| 1397 | |
| 1398 | if (clEnv == NULL) |
| 1399 | return MagickFalse; |
| 1400 | |
| 1401 | #ifdef MAGICKCORE_CLPERFMARKER |
| 1402 | clBeginPerfMarkerAMD(__FUNCTION__,""); |
| 1403 | #endif |
| 1404 | |
| 1405 | LockSemaphoreInfo(clEnv->lock); |
| 1406 | if (clEnv->OpenCLInitialized == MagickFalse) { |
| 1407 | if (clEnv->device==NULL |
| 1408 | && clEnv->OpenCLDisabled == MagickFalse) |
| 1409 | status = autoSelectDevice(clEnv, exception); |
| 1410 | else |
| 1411 | status = InitOpenCLEnvInternal(clEnv, exception); |
| 1412 | } |
| 1413 | UnlockSemaphoreInfo(clEnv->lock); |
| 1414 | |
| 1415 | #ifdef MAGICKCORE_CLPERFMARKER |
| 1416 | clEndPerfMarkerAMD(); |
| 1417 | #endif |
| 1418 | return status; |
| 1419 | } |
| 1420 | |
| 1421 | |
| 1422 | /* |
| 1423 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1424 | % % |
| 1425 | % % |
| 1426 | % % |
| 1427 | + A c q u i r e O p e n C L C o m m a n d Q u e u e % |
| 1428 | % % |
| 1429 | % % |
| 1430 | % % |
| 1431 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1432 | % |
| 1433 | % AcquireOpenCLCommandQueue() acquires an OpenCL command queue |
| 1434 | % |
| 1435 | % The format of the AcquireOpenCLCommandQueue method is: |
| 1436 | % |
| 1437 | % cl_command_queue AcquireOpenCLCommandQueue(MagickCLEnv clEnv) |
| 1438 | % |
| 1439 | % A description of each parameter follows: |
| 1440 | % |
| 1441 | % o clEnv: the OpenCL environment. |
| 1442 | % |
| 1443 | */ |
| 1444 | |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 1445 | MagickPrivate cl_command_queue AcquireOpenCLCommandQueue(MagickCLEnv clEnv) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1446 | { |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 1447 | cl_command_queue |
| 1448 | queue; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1449 | |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 1450 | cl_command_queue_properties |
| 1451 | properties; |
| 1452 | |
| 1453 | if (clEnv == (MagickCLEnv) NULL) |
| 1454 | return (cl_command_queue) NULL; |
| 1455 | LockSemaphoreInfo(clEnv->commandQueuesLock); |
| 1456 | if (clEnv->commandQueuesPos >= 0) { |
| 1457 | queue=clEnv->commandQueues[clEnv->commandQueuesPos--]; |
| 1458 | UnlockSemaphoreInfo(clEnv->commandQueuesLock); |
| 1459 | } |
| 1460 | else { |
| 1461 | UnlockSemaphoreInfo(clEnv->commandQueuesLock); |
| 1462 | properties=0; |
| 1463 | #if PROFILE_OCL_KERNELS |
| 1464 | properties=CL_QUEUE_PROFILING_ENABLE; |
| 1465 | #endif |
| 1466 | queue=clEnv->library->clCreateCommandQueue(clEnv->context,clEnv->device, |
| 1467 | properties,NULL); |
| 1468 | } |
| 1469 | return(queue); |
| 1470 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1471 | |
| 1472 | /* |
| 1473 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1474 | % % |
| 1475 | % % |
| 1476 | % % |
| 1477 | + R e l i n q u i s h O p e n C L C o m m a n d Q u e u e % |
| 1478 | % % |
| 1479 | % % |
| 1480 | % % |
| 1481 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1482 | % |
| 1483 | % RelinquishOpenCLCommandQueue() releases the OpenCL command queue |
| 1484 | % |
| 1485 | % The format of the RelinquishOpenCLCommandQueue method is: |
| 1486 | % |
| 1487 | % MagickBooleanType RelinquishOpenCLCommandQueue(MagickCLEnv clEnv, |
| 1488 | % cl_command_queue queue) |
| 1489 | % |
| 1490 | % A description of each parameter follows: |
| 1491 | % |
| 1492 | % o clEnv: the OpenCL environment. |
| 1493 | % |
| 1494 | % o queue: the OpenCL queue to be released. |
| 1495 | % |
| 1496 | % |
| 1497 | */ |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 1498 | |
| 1499 | MagickPrivate MagickBooleanType RelinquishOpenCLCommandQueue(MagickCLEnv clEnv, |
| 1500 | cl_command_queue queue) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1501 | { |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 1502 | MagickBooleanType |
| 1503 | status; |
| 1504 | |
| 1505 | if (clEnv == NULL) |
| 1506 | return(MagickFalse); |
| 1507 | |
| 1508 | LockSemaphoreInfo(clEnv->commandQueuesLock); |
| 1509 | |
| 1510 | if (clEnv->commandQueuesPos >= MAX_COMMAND_QUEUES) |
| 1511 | status=(clEnv->library->clReleaseCommandQueue(queue) == CL_SUCCESS) ? |
| 1512 | MagickTrue : MagickFalse; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1513 | else |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 1514 | { |
| 1515 | clEnv->commandQueues[++clEnv->commandQueuesPos]=queue; |
| 1516 | status=MagickTrue; |
| 1517 | } |
| 1518 | |
| 1519 | UnlockSemaphoreInfo(clEnv->commandQueuesLock); |
| 1520 | |
| 1521 | return(status); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1524 | /* |
| 1525 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1526 | % % |
| 1527 | % % |
| 1528 | % % |
| 1529 | + A c q u i r e O p e n C L K e r n e l % |
| 1530 | % % |
| 1531 | % % |
| 1532 | % % |
| 1533 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1534 | % |
| 1535 | % AcquireOpenCLKernel() acquires an OpenCL kernel |
| 1536 | % |
| 1537 | % The format of the AcquireOpenCLKernel method is: |
| 1538 | % |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 1539 | % cl_kernel AcquireOpenCLKernel(MagickCLEnv clEnv, |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1540 | % MagickOpenCLProgram program, const char* kernelName) |
| 1541 | % |
| 1542 | % A description of each parameter follows: |
| 1543 | % |
| 1544 | % o clEnv: the OpenCL environment. |
| 1545 | % |
| 1546 | % o program: the OpenCL program module that the kernel belongs to. |
| 1547 | % |
| 1548 | % o kernelName: the name of the kernel |
| 1549 | % |
| 1550 | */ |
| 1551 | |
cristy | 7b6514c | 2013-12-10 23:13:13 +0000 | [diff] [blame] | 1552 | MagickPrivate |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1553 | cl_kernel AcquireOpenCLKernel(MagickCLEnv clEnv, MagickOpenCLProgram program, const char* kernelName) |
| 1554 | { |
| 1555 | cl_int clStatus; |
| 1556 | cl_kernel kernel = NULL; |
| 1557 | if (clEnv != NULL && kernelName!=NULL) |
| 1558 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1559 | kernel = clEnv->library->clCreateKernel(clEnv->programs[program], kernelName, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1560 | } |
| 1561 | return kernel; |
| 1562 | } |
| 1563 | |
| 1564 | |
| 1565 | /* |
| 1566 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1567 | % % |
| 1568 | % % |
| 1569 | % % |
| 1570 | + R e l i n q u i s h O p e n C L K e r n e l % |
| 1571 | % % |
| 1572 | % % |
| 1573 | % % |
| 1574 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1575 | % |
| 1576 | % RelinquishOpenCLKernel() releases an OpenCL kernel |
| 1577 | % |
| 1578 | % The format of the RelinquishOpenCLKernel method is: |
| 1579 | % |
| 1580 | % MagickBooleanType RelinquishOpenCLKernel(MagickCLEnv clEnv, |
| 1581 | % cl_kernel kernel) |
| 1582 | % |
| 1583 | % A description of each parameter follows: |
| 1584 | % |
| 1585 | % o clEnv: the OpenCL environment. |
| 1586 | % |
| 1587 | % o kernel: the OpenCL kernel object to be released. |
| 1588 | % |
| 1589 | % |
| 1590 | */ |
| 1591 | |
cristy | 7b6514c | 2013-12-10 23:13:13 +0000 | [diff] [blame] | 1592 | MagickPrivate |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1593 | MagickBooleanType RelinquishOpenCLKernel(MagickCLEnv clEnv, cl_kernel kernel) |
| 1594 | { |
| 1595 | MagickBooleanType status = MagickFalse; |
| 1596 | if (clEnv != NULL && kernel != NULL) |
| 1597 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1598 | status = ((clEnv->library->clReleaseKernel(kernel) == CL_SUCCESS)?MagickTrue:MagickFalse); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1599 | } |
| 1600 | return status; |
| 1601 | } |
| 1602 | |
| 1603 | /* |
| 1604 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1605 | % % |
| 1606 | % % |
| 1607 | % % |
| 1608 | + G e t O p e n C L D e v i c e L o c a l M e m o r y S i z e % |
| 1609 | % % |
| 1610 | % % |
| 1611 | % % |
| 1612 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1613 | % |
| 1614 | % GetOpenCLDeviceLocalMemorySize() returns local memory size of the device |
| 1615 | % |
| 1616 | % The format of the GetOpenCLDeviceLocalMemorySize method is: |
| 1617 | % |
| 1618 | % unsigned long GetOpenCLDeviceLocalMemorySize(MagickCLEnv clEnv) |
| 1619 | % |
| 1620 | % A description of each parameter follows: |
| 1621 | % |
| 1622 | % o clEnv: the OpenCL environment. |
| 1623 | % |
| 1624 | % |
| 1625 | */ |
| 1626 | |
cristy | 7b6514c | 2013-12-10 23:13:13 +0000 | [diff] [blame] | 1627 | MagickPrivate |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1628 | unsigned long GetOpenCLDeviceLocalMemorySize(MagickCLEnv clEnv) |
| 1629 | { |
| 1630 | cl_ulong localMemorySize; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1631 | clEnv->library->clGetDeviceInfo(clEnv->device, CL_DEVICE_LOCAL_MEM_SIZE, sizeof(cl_ulong), &localMemorySize, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1632 | return (unsigned long)localMemorySize; |
| 1633 | } |
| 1634 | |
cristy | 7b6514c | 2013-12-10 23:13:13 +0000 | [diff] [blame] | 1635 | MagickPrivate |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1636 | unsigned long GetOpenCLDeviceMaxMemAllocSize(MagickCLEnv clEnv) |
| 1637 | { |
| 1638 | cl_ulong maxMemAllocSize; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1639 | clEnv->library->clGetDeviceInfo(clEnv->device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(cl_ulong), &maxMemAllocSize, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1640 | return (unsigned long)maxMemAllocSize; |
| 1641 | } |
| 1642 | |
| 1643 | |
| 1644 | /* |
| 1645 | Beginning of the OpenCL device selection infrastructure |
| 1646 | */ |
| 1647 | |
| 1648 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1649 | typedef enum { |
| 1650 | DS_SUCCESS = 0 |
| 1651 | ,DS_INVALID_PROFILE = 1000 |
| 1652 | ,DS_MEMORY_ERROR |
| 1653 | ,DS_INVALID_PERF_EVALUATOR_TYPE |
| 1654 | ,DS_INVALID_PERF_EVALUATOR |
| 1655 | ,DS_PERF_EVALUATOR_ERROR |
| 1656 | ,DS_FILE_ERROR |
| 1657 | ,DS_UNKNOWN_DEVICE_TYPE |
| 1658 | ,DS_PROFILE_FILE_ERROR |
| 1659 | ,DS_SCORE_SERIALIZER_ERROR |
| 1660 | ,DS_SCORE_DESERIALIZER_ERROR |
| 1661 | } ds_status; |
| 1662 | |
| 1663 | /* device type */ |
| 1664 | typedef enum { |
| 1665 | DS_DEVICE_NATIVE_CPU = 0 |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 1666 | ,DS_DEVICE_OPENCL_DEVICE |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1667 | } ds_device_type; |
| 1668 | |
| 1669 | |
| 1670 | typedef struct { |
| 1671 | ds_device_type type; |
dirk | b05dcc9 | 2014-08-27 15:30:53 +0000 | [diff] [blame] | 1672 | cl_device_type oclDeviceType; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1673 | cl_device_id oclDeviceID; |
| 1674 | char* oclDeviceName; |
| 1675 | char* oclDriverVersion; |
| 1676 | cl_uint oclMaxClockFrequency; |
| 1677 | cl_uint oclMaxComputeUnits; |
| 1678 | void* score; /* a pointer to the score data, the content/format is application defined */ |
| 1679 | } ds_device; |
| 1680 | |
| 1681 | typedef struct { |
| 1682 | unsigned int numDevices; |
| 1683 | ds_device* devices; |
| 1684 | const char* version; |
| 1685 | } ds_profile; |
| 1686 | |
| 1687 | /* deallocate memory used by score */ |
| 1688 | typedef ds_status (*ds_score_release)(void* score); |
| 1689 | |
| 1690 | static ds_status releaseDeviceResource(ds_device* device, ds_score_release sr) { |
| 1691 | ds_status status = DS_SUCCESS; |
| 1692 | if (device) { |
| 1693 | if (device->oclDeviceName) free(device->oclDeviceName); |
| 1694 | if (device->oclDriverVersion) free(device->oclDriverVersion); |
| 1695 | if (device->score) status = sr(device->score); |
| 1696 | } |
| 1697 | return status; |
| 1698 | } |
| 1699 | |
| 1700 | static ds_status releaseDSProfile(ds_profile* profile, ds_score_release sr) { |
| 1701 | ds_status status = DS_SUCCESS; |
| 1702 | if (profile!=NULL) { |
| 1703 | if (profile->devices!=NULL && sr!=NULL) { |
| 1704 | unsigned int i; |
| 1705 | for (i = 0; i < profile->numDevices; i++) { |
| 1706 | status = releaseDeviceResource(profile->devices+i,sr); |
| 1707 | if (status != DS_SUCCESS) |
| 1708 | break; |
| 1709 | } |
| 1710 | free(profile->devices); |
| 1711 | } |
| 1712 | free(profile); |
| 1713 | } |
| 1714 | return status; |
| 1715 | } |
| 1716 | |
| 1717 | |
| 1718 | static ds_status initDSProfile(ds_profile** p, const char* version) { |
| 1719 | int numDevices = 0; |
| 1720 | cl_uint numPlatforms = 0; |
| 1721 | cl_platform_id* platforms = NULL; |
| 1722 | cl_device_id* devices = NULL; |
| 1723 | ds_status status = DS_SUCCESS; |
| 1724 | ds_profile* profile = NULL; |
| 1725 | unsigned int next = 0; |
| 1726 | unsigned int i; |
| 1727 | |
| 1728 | if (p == NULL) |
| 1729 | return DS_INVALID_PROFILE; |
| 1730 | |
| 1731 | profile = (ds_profile*)malloc(sizeof(ds_profile)); |
| 1732 | if (profile == NULL) |
| 1733 | return DS_MEMORY_ERROR; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 1734 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1735 | memset(profile, 0, sizeof(ds_profile)); |
| 1736 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1737 | OpenCLLib->clGetPlatformIDs(0, NULL, &numPlatforms); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1738 | if (numPlatforms > 0) { |
| 1739 | platforms = (cl_platform_id*)malloc(numPlatforms*sizeof(cl_platform_id)); |
| 1740 | if (platforms == NULL) { |
| 1741 | status = DS_MEMORY_ERROR; |
| 1742 | goto cleanup; |
| 1743 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1744 | OpenCLLib->clGetPlatformIDs(numPlatforms, platforms, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1745 | for (i = 0; i < (unsigned int)numPlatforms; i++) { |
| 1746 | cl_uint num; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1747 | if (OpenCLLib->clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_CPU | CL_DEVICE_TYPE_GPU, 0, NULL, &num) == CL_SUCCESS) |
dirk | 20932d3 | 2013-12-12 06:16:19 +0000 | [diff] [blame] | 1748 | numDevices+=num; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | profile->numDevices = numDevices+1; /* +1 to numDevices to include the native CPU */ |
| 1753 | |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 1754 | profile->devices = (ds_device*)malloc(profile->numDevices*sizeof(ds_device)); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1755 | if (profile->devices == NULL) { |
| 1756 | profile->numDevices = 0; |
| 1757 | status = DS_MEMORY_ERROR; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 1758 | goto cleanup; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1759 | } |
| 1760 | memset(profile->devices, 0, profile->numDevices*sizeof(ds_device)); |
| 1761 | |
| 1762 | if (numDevices > 0) { |
| 1763 | devices = (cl_device_id*)malloc(numDevices*sizeof(cl_device_id)); |
| 1764 | if (devices == NULL) { |
| 1765 | status = DS_MEMORY_ERROR; |
| 1766 | goto cleanup; |
| 1767 | } |
| 1768 | for (i = 0; i < (unsigned int)numPlatforms; i++) { |
| 1769 | cl_uint num; |
| 1770 | |
| 1771 | int d; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 1772 | for (d = 0; d < 2; d++) { |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1773 | unsigned int j; |
| 1774 | cl_device_type deviceType; |
| 1775 | switch(d) { |
| 1776 | case 0: |
| 1777 | deviceType = CL_DEVICE_TYPE_GPU; |
| 1778 | break; |
| 1779 | case 1: |
| 1780 | deviceType = CL_DEVICE_TYPE_CPU; |
| 1781 | break; |
| 1782 | default: |
| 1783 | continue; |
| 1784 | break; |
| 1785 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1786 | if (OpenCLLib->clGetDeviceIDs(platforms[i], deviceType, numDevices, devices, &num) != CL_SUCCESS) |
dirk | 7b1bb39 | 2013-12-10 22:36:32 +0000 | [diff] [blame] | 1787 | continue; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1788 | for (j = 0; j < num; j++, next++) { |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1789 | size_t length; |
| 1790 | |
| 1791 | profile->devices[next].type = DS_DEVICE_OPENCL_DEVICE; |
| 1792 | profile->devices[next].oclDeviceID = devices[j]; |
| 1793 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1794 | OpenCLLib->clGetDeviceInfo(profile->devices[next].oclDeviceID, CL_DEVICE_NAME |
dirk | 7b1bb39 | 2013-12-10 22:36:32 +0000 | [diff] [blame] | 1795 | , 0, NULL, &length); |
| 1796 | profile->devices[next].oclDeviceName = (char*)malloc(sizeof(char)*length); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1797 | OpenCLLib->clGetDeviceInfo(profile->devices[next].oclDeviceID, CL_DEVICE_NAME |
dirk | 7b1bb39 | 2013-12-10 22:36:32 +0000 | [diff] [blame] | 1798 | , length, profile->devices[next].oclDeviceName, NULL); |
| 1799 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1800 | OpenCLLib->clGetDeviceInfo(profile->devices[next].oclDeviceID, CL_DRIVER_VERSION |
dirk | 7b1bb39 | 2013-12-10 22:36:32 +0000 | [diff] [blame] | 1801 | , 0, NULL, &length); |
| 1802 | profile->devices[next].oclDriverVersion = (char*)malloc(sizeof(char)*length); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1803 | OpenCLLib->clGetDeviceInfo(profile->devices[next].oclDeviceID, CL_DRIVER_VERSION |
dirk | e3c5f89 | 2013-12-10 06:04:40 +0000 | [diff] [blame] | 1804 | , length, profile->devices[next].oclDriverVersion, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1805 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1806 | OpenCLLib->clGetDeviceInfo(profile->devices[next].oclDeviceID, CL_DEVICE_MAX_CLOCK_FREQUENCY |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1807 | , sizeof(cl_uint), &profile->devices[next].oclMaxClockFrequency, NULL); |
| 1808 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1809 | OpenCLLib->clGetDeviceInfo(profile->devices[next].oclDeviceID, CL_DEVICE_MAX_COMPUTE_UNITS |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1810 | , sizeof(cl_uint), &profile->devices[next].oclMaxComputeUnits, NULL); |
dirk | b05dcc9 | 2014-08-27 15:30:53 +0000 | [diff] [blame] | 1811 | |
| 1812 | OpenCLLib->clGetDeviceInfo(profile->devices[next].oclDeviceID, CL_DEVICE_TYPE |
| 1813 | , sizeof(cl_device_type), &profile->devices[next].oclDeviceType, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1814 | } |
| 1815 | } |
| 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | profile->devices[next].type = DS_DEVICE_NATIVE_CPU; |
| 1820 | profile->version = version; |
| 1821 | |
| 1822 | cleanup: |
| 1823 | if (platforms) free(platforms); |
| 1824 | if (devices) free(devices); |
| 1825 | if (status == DS_SUCCESS) { |
| 1826 | *p = profile; |
| 1827 | } |
| 1828 | else { |
| 1829 | if (profile) { |
| 1830 | if (profile->devices) |
| 1831 | free(profile->devices); |
| 1832 | free(profile); |
| 1833 | } |
| 1834 | } |
| 1835 | return status; |
| 1836 | } |
| 1837 | |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 1838 | /* Pointer to a function that calculates the score of a device (ex: device->score) |
| 1839 | update the data size of score. The encoding and the format of the score data |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 1840 | is implementation defined. The function should return DS_SUCCESS if there's no error to be reported. |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1841 | */ |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 1842 | typedef ds_status (*ds_perf_evaluator)(ds_device* device, void* data); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1843 | |
| 1844 | typedef enum { |
| 1845 | DS_EVALUATE_ALL |
| 1846 | ,DS_EVALUATE_NEW_ONLY |
| 1847 | } ds_evaluation_type; |
| 1848 | |
| 1849 | static ds_status profileDevices(ds_profile* profile, const ds_evaluation_type type |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 1850 | ,ds_perf_evaluator evaluator, void* evaluatorData, unsigned int* numUpdates) { |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1851 | ds_status status = DS_SUCCESS; |
| 1852 | unsigned int i; |
| 1853 | unsigned int updates = 0; |
| 1854 | |
| 1855 | if (profile == NULL) { |
| 1856 | return DS_INVALID_PROFILE; |
| 1857 | } |
| 1858 | if (evaluator == NULL) { |
| 1859 | return DS_INVALID_PERF_EVALUATOR; |
| 1860 | } |
| 1861 | |
| 1862 | for (i = 0; i < profile->numDevices; i++) { |
| 1863 | ds_status evaluatorStatus; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 1864 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1865 | switch (type) { |
| 1866 | case DS_EVALUATE_NEW_ONLY: |
| 1867 | if (profile->devices[i].score != NULL) |
| 1868 | break; |
| 1869 | /* else fall through */ |
| 1870 | case DS_EVALUATE_ALL: |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 1871 | evaluatorStatus = evaluator(profile->devices+i,evaluatorData); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1872 | if (evaluatorStatus != DS_SUCCESS) { |
| 1873 | status = evaluatorStatus; |
| 1874 | return status; |
| 1875 | } |
| 1876 | updates++; |
| 1877 | break; |
| 1878 | default: |
| 1879 | return DS_INVALID_PERF_EVALUATOR_TYPE; |
| 1880 | break; |
| 1881 | }; |
| 1882 | } |
| 1883 | if (numUpdates) |
| 1884 | *numUpdates = updates; |
| 1885 | return status; |
| 1886 | } |
| 1887 | |
| 1888 | |
| 1889 | #define DS_TAG_VERSION "<version>" |
| 1890 | #define DS_TAG_VERSION_END "</version>" |
| 1891 | #define DS_TAG_DEVICE "<device>" |
| 1892 | #define DS_TAG_DEVICE_END "</device>" |
| 1893 | #define DS_TAG_SCORE "<score>" |
| 1894 | #define DS_TAG_SCORE_END "</score>" |
| 1895 | #define DS_TAG_DEVICE_TYPE "<type>" |
| 1896 | #define DS_TAG_DEVICE_TYPE_END "</type>" |
| 1897 | #define DS_TAG_DEVICE_NAME "<name>" |
| 1898 | #define DS_TAG_DEVICE_NAME_END "</name>" |
| 1899 | #define DS_TAG_DEVICE_DRIVER_VERSION "<driver>" |
| 1900 | #define DS_TAG_DEVICE_DRIVER_VERSION_END "</driver>" |
| 1901 | #define DS_TAG_DEVICE_MAX_COMPUTE_UNITS "<max cu>" |
| 1902 | #define DS_TAG_DEVICE_MAX_COMPUTE_UNITS_END "</max cu>" |
| 1903 | #define DS_TAG_DEVICE_MAX_CLOCK_FREQ "<max clock>" |
| 1904 | #define DS_TAG_DEVICE_MAX_CLOCK_FREQ_END "</max clock>" |
| 1905 | |
| 1906 | #define DS_DEVICE_NATIVE_CPU_STRING "native_cpu" |
| 1907 | |
| 1908 | |
| 1909 | |
| 1910 | typedef ds_status (*ds_score_serializer)(ds_device* device, void** serializedScore, unsigned int* serializedScoreSize); |
| 1911 | static ds_status writeProfileToFile(ds_profile* profile, ds_score_serializer serializer, const char* file) { |
| 1912 | ds_status status = DS_SUCCESS; |
| 1913 | FILE* profileFile = NULL; |
| 1914 | |
| 1915 | |
| 1916 | if (profile == NULL) |
| 1917 | return DS_INVALID_PROFILE; |
| 1918 | |
| 1919 | profileFile = fopen(file, "wb"); |
| 1920 | if (profileFile==NULL) { |
| 1921 | status = DS_FILE_ERROR; |
| 1922 | } |
| 1923 | else { |
| 1924 | unsigned int i; |
| 1925 | |
| 1926 | /* write version string */ |
| 1927 | fwrite(DS_TAG_VERSION, sizeof(char), strlen(DS_TAG_VERSION), profileFile); |
| 1928 | fwrite(profile->version, sizeof(char), strlen(profile->version), profileFile); |
| 1929 | fwrite(DS_TAG_VERSION_END, sizeof(char), strlen(DS_TAG_VERSION_END), profileFile); |
| 1930 | fwrite("\n", sizeof(char), 1, profileFile); |
| 1931 | |
| 1932 | for (i = 0; i < profile->numDevices && status == DS_SUCCESS; i++) { |
| 1933 | void* serializedScore; |
| 1934 | unsigned int serializedScoreSize; |
| 1935 | |
| 1936 | fwrite(DS_TAG_DEVICE, sizeof(char), strlen(DS_TAG_DEVICE), profileFile); |
| 1937 | |
| 1938 | fwrite(DS_TAG_DEVICE_TYPE, sizeof(char), strlen(DS_TAG_DEVICE_TYPE), profileFile); |
| 1939 | fwrite(&profile->devices[i].type,sizeof(ds_device_type),1, profileFile); |
| 1940 | fwrite(DS_TAG_DEVICE_TYPE_END, sizeof(char), strlen(DS_TAG_DEVICE_TYPE_END), profileFile); |
| 1941 | |
| 1942 | switch(profile->devices[i].type) { |
| 1943 | case DS_DEVICE_NATIVE_CPU: |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 1944 | { |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1945 | /* There's no need to emit a device name for the native CPU device. */ |
| 1946 | /* |
| 1947 | fwrite(DS_TAG_DEVICE_NAME, sizeof(char), strlen(DS_TAG_DEVICE_NAME), profileFile); |
| 1948 | fwrite(DS_DEVICE_NATIVE_CPU_STRING,sizeof(char),strlen(DS_DEVICE_NATIVE_CPU_STRING), profileFile); |
| 1949 | fwrite(DS_TAG_DEVICE_NAME_END, sizeof(char), strlen(DS_TAG_DEVICE_NAME_END), profileFile); |
| 1950 | */ |
| 1951 | } |
| 1952 | break; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 1953 | case DS_DEVICE_OPENCL_DEVICE: |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1954 | { |
| 1955 | char tmp[16]; |
| 1956 | |
| 1957 | fwrite(DS_TAG_DEVICE_NAME, sizeof(char), strlen(DS_TAG_DEVICE_NAME), profileFile); |
| 1958 | fwrite(profile->devices[i].oclDeviceName,sizeof(char),strlen(profile->devices[i].oclDeviceName), profileFile); |
| 1959 | fwrite(DS_TAG_DEVICE_NAME_END, sizeof(char), strlen(DS_TAG_DEVICE_NAME_END), profileFile); |
| 1960 | |
| 1961 | fwrite(DS_TAG_DEVICE_DRIVER_VERSION, sizeof(char), strlen(DS_TAG_DEVICE_DRIVER_VERSION), profileFile); |
| 1962 | fwrite(profile->devices[i].oclDriverVersion,sizeof(char),strlen(profile->devices[i].oclDriverVersion), profileFile); |
| 1963 | fwrite(DS_TAG_DEVICE_DRIVER_VERSION_END, sizeof(char), strlen(DS_TAG_DEVICE_DRIVER_VERSION_END), profileFile); |
| 1964 | |
| 1965 | fwrite(DS_TAG_DEVICE_MAX_COMPUTE_UNITS, sizeof(char), strlen(DS_TAG_DEVICE_MAX_COMPUTE_UNITS), profileFile); |
| 1966 | sprintf(tmp,"%d",profile->devices[i].oclMaxComputeUnits); |
| 1967 | fwrite(tmp,sizeof(char),strlen(tmp), profileFile); |
| 1968 | fwrite(DS_TAG_DEVICE_MAX_COMPUTE_UNITS_END, sizeof(char), strlen(DS_TAG_DEVICE_MAX_COMPUTE_UNITS_END), profileFile); |
| 1969 | |
| 1970 | fwrite(DS_TAG_DEVICE_MAX_CLOCK_FREQ, sizeof(char), strlen(DS_TAG_DEVICE_MAX_CLOCK_FREQ), profileFile); |
| 1971 | sprintf(tmp,"%d",profile->devices[i].oclMaxClockFrequency); |
| 1972 | fwrite(tmp,sizeof(char),strlen(tmp), profileFile); |
| 1973 | fwrite(DS_TAG_DEVICE_MAX_CLOCK_FREQ_END, sizeof(char), strlen(DS_TAG_DEVICE_MAX_CLOCK_FREQ_END), profileFile); |
| 1974 | } |
| 1975 | break; |
| 1976 | default: |
| 1977 | status = DS_UNKNOWN_DEVICE_TYPE; |
| 1978 | break; |
| 1979 | }; |
| 1980 | |
| 1981 | fwrite(DS_TAG_SCORE, sizeof(char), strlen(DS_TAG_SCORE), profileFile); |
| 1982 | status = serializer(profile->devices+i, &serializedScore, &serializedScoreSize); |
| 1983 | if (status == DS_SUCCESS && serializedScore!=NULL && serializedScoreSize > 0) { |
| 1984 | fwrite(serializedScore, sizeof(char), serializedScoreSize, profileFile); |
| 1985 | free(serializedScore); |
| 1986 | } |
| 1987 | fwrite(DS_TAG_SCORE_END, sizeof(char), strlen(DS_TAG_SCORE_END), profileFile); |
| 1988 | fwrite(DS_TAG_DEVICE_END, sizeof(char), strlen(DS_TAG_DEVICE_END), profileFile); |
| 1989 | fwrite("\n",sizeof(char),1,profileFile); |
| 1990 | } |
| 1991 | fclose(profileFile); |
| 1992 | } |
| 1993 | return status; |
| 1994 | } |
| 1995 | |
| 1996 | |
| 1997 | static ds_status readProFile(const char* fileName, char** content, size_t* contentSize) { |
| 1998 | ds_status status = DS_SUCCESS; |
| 1999 | FILE * input = NULL; |
| 2000 | size_t size = 0; |
| 2001 | size_t rsize = 0; |
| 2002 | char* binary = NULL; |
| 2003 | |
| 2004 | *contentSize = 0; |
| 2005 | *content = NULL; |
| 2006 | |
| 2007 | input = fopen(fileName, "rb"); |
| 2008 | if(input == NULL) { |
| 2009 | return DS_FILE_ERROR; |
| 2010 | } |
| 2011 | |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2012 | fseek(input, 0L, SEEK_END); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2013 | size = ftell(input); |
| 2014 | rewind(input); |
| 2015 | binary = (char*)malloc(size); |
| 2016 | if(binary == NULL) { |
| 2017 | status = DS_FILE_ERROR; |
| 2018 | goto cleanup; |
| 2019 | } |
| 2020 | rsize = fread(binary, sizeof(char), size, input); |
| 2021 | if (rsize!=size |
| 2022 | || ferror(input)) { |
| 2023 | status = DS_FILE_ERROR; |
| 2024 | goto cleanup; |
| 2025 | } |
| 2026 | *contentSize = size; |
| 2027 | *content = binary; |
| 2028 | |
| 2029 | cleanup: |
| 2030 | if (input != NULL) fclose(input); |
| 2031 | if (status != DS_SUCCESS |
| 2032 | && binary != NULL) { |
| 2033 | free(binary); |
| 2034 | *content = NULL; |
| 2035 | *contentSize = 0; |
| 2036 | } |
| 2037 | return status; |
| 2038 | } |
| 2039 | |
| 2040 | |
| 2041 | static const char* findString(const char* contentStart, const char* contentEnd, const char* string) { |
| 2042 | size_t stringLength; |
| 2043 | const char* currentPosition; |
| 2044 | const char* found; |
| 2045 | found = NULL; |
| 2046 | stringLength = strlen(string); |
| 2047 | currentPosition = contentStart; |
| 2048 | for(currentPosition = contentStart; currentPosition < contentEnd; currentPosition++) { |
| 2049 | if (*currentPosition == string[0]) { |
| 2050 | if (currentPosition+stringLength < contentEnd) { |
| 2051 | if (strncmp(currentPosition, string, stringLength) == 0) { |
| 2052 | found = currentPosition; |
| 2053 | break; |
| 2054 | } |
| 2055 | } |
| 2056 | } |
| 2057 | } |
| 2058 | return found; |
| 2059 | } |
| 2060 | |
| 2061 | |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2062 | typedef ds_status (*ds_score_deserializer)(ds_device* device, const unsigned char* serializedScore, unsigned int serializedScoreSize); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2063 | static ds_status readProfileFromFile(ds_profile* profile, ds_score_deserializer deserializer, const char* file) { |
| 2064 | |
| 2065 | ds_status status = DS_SUCCESS; |
| 2066 | char* contentStart = NULL; |
| 2067 | const char* contentEnd = NULL; |
| 2068 | size_t contentSize; |
| 2069 | |
| 2070 | if (profile==NULL) |
| 2071 | return DS_INVALID_PROFILE; |
| 2072 | |
| 2073 | status = readProFile(file, &contentStart, &contentSize); |
| 2074 | if (status == DS_SUCCESS) { |
| 2075 | const char* currentPosition; |
| 2076 | const char* dataStart; |
| 2077 | const char* dataEnd; |
| 2078 | size_t versionStringLength; |
| 2079 | |
| 2080 | contentEnd = contentStart + contentSize; |
| 2081 | currentPosition = contentStart; |
| 2082 | |
| 2083 | |
| 2084 | /* parse the version string */ |
| 2085 | dataStart = findString(currentPosition, contentEnd, DS_TAG_VERSION); |
| 2086 | if (dataStart == NULL) { |
| 2087 | status = DS_PROFILE_FILE_ERROR; |
| 2088 | goto cleanup; |
| 2089 | } |
| 2090 | dataStart += strlen(DS_TAG_VERSION); |
| 2091 | |
| 2092 | dataEnd = findString(dataStart, contentEnd, DS_TAG_VERSION_END); |
| 2093 | if (dataEnd==NULL) { |
| 2094 | status = DS_PROFILE_FILE_ERROR; |
| 2095 | goto cleanup; |
| 2096 | } |
| 2097 | |
| 2098 | versionStringLength = strlen(profile->version); |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2099 | if (versionStringLength!=(size_t)(dataEnd-dataStart) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2100 | || strncmp(profile->version, dataStart, versionStringLength)!=(int)0) { |
| 2101 | /* version mismatch */ |
| 2102 | status = DS_PROFILE_FILE_ERROR; |
| 2103 | goto cleanup; |
| 2104 | } |
| 2105 | currentPosition = dataEnd+strlen(DS_TAG_VERSION_END); |
| 2106 | |
| 2107 | /* parse the device information */ |
dirk | 5dcb762 | 2013-12-01 10:43:43 +0000 | [diff] [blame] | 2108 | DisableMSCWarning(4127) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2109 | while (1) { |
dirk | 5dcb762 | 2013-12-01 10:43:43 +0000 | [diff] [blame] | 2110 | RestoreMSCWarning |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2111 | unsigned int i; |
| 2112 | |
| 2113 | const char* deviceTypeStart; |
| 2114 | const char* deviceTypeEnd; |
| 2115 | ds_device_type deviceType; |
| 2116 | |
| 2117 | const char* deviceNameStart; |
| 2118 | const char* deviceNameEnd; |
| 2119 | |
| 2120 | const char* deviceScoreStart; |
| 2121 | const char* deviceScoreEnd; |
| 2122 | |
| 2123 | const char* deviceDriverStart; |
| 2124 | const char* deviceDriverEnd; |
| 2125 | |
| 2126 | const char* tmpStart; |
| 2127 | const char* tmpEnd; |
| 2128 | char tmp[16]; |
| 2129 | |
| 2130 | cl_uint maxClockFrequency; |
| 2131 | cl_uint maxComputeUnits; |
| 2132 | |
| 2133 | dataStart = findString(currentPosition, contentEnd, DS_TAG_DEVICE); |
| 2134 | if (dataStart==NULL) { |
| 2135 | /* nothing useful remain, quit...*/ |
| 2136 | break; |
| 2137 | } |
| 2138 | dataStart+=strlen(DS_TAG_DEVICE); |
| 2139 | dataEnd = findString(dataStart, contentEnd, DS_TAG_DEVICE_END); |
| 2140 | if (dataEnd==NULL) { |
| 2141 | status = DS_PROFILE_FILE_ERROR; |
| 2142 | goto cleanup; |
| 2143 | } |
| 2144 | |
| 2145 | /* parse the device type */ |
| 2146 | deviceTypeStart = findString(dataStart, contentEnd, DS_TAG_DEVICE_TYPE); |
| 2147 | if (deviceTypeStart==NULL) { |
| 2148 | status = DS_PROFILE_FILE_ERROR; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2149 | goto cleanup; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2150 | } |
| 2151 | deviceTypeStart+=strlen(DS_TAG_DEVICE_TYPE); |
| 2152 | deviceTypeEnd = findString(deviceTypeStart, contentEnd, DS_TAG_DEVICE_TYPE_END); |
| 2153 | if (deviceTypeEnd==NULL) { |
| 2154 | status = DS_PROFILE_FILE_ERROR; |
| 2155 | goto cleanup; |
| 2156 | } |
| 2157 | memcpy(&deviceType, deviceTypeStart, sizeof(ds_device_type)); |
| 2158 | |
| 2159 | |
| 2160 | /* parse the device name */ |
| 2161 | if (deviceType == DS_DEVICE_OPENCL_DEVICE) { |
| 2162 | |
| 2163 | deviceNameStart = findString(dataStart, contentEnd, DS_TAG_DEVICE_NAME); |
| 2164 | if (deviceNameStart==NULL) { |
| 2165 | status = DS_PROFILE_FILE_ERROR; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2166 | goto cleanup; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2167 | } |
| 2168 | deviceNameStart+=strlen(DS_TAG_DEVICE_NAME); |
| 2169 | deviceNameEnd = findString(deviceNameStart, contentEnd, DS_TAG_DEVICE_NAME_END); |
| 2170 | if (deviceNameEnd==NULL) { |
| 2171 | status = DS_PROFILE_FILE_ERROR; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2172 | goto cleanup; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | |
| 2176 | deviceDriverStart = findString(dataStart, contentEnd, DS_TAG_DEVICE_DRIVER_VERSION); |
| 2177 | if (deviceDriverStart==NULL) { |
| 2178 | status = DS_PROFILE_FILE_ERROR; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2179 | goto cleanup; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2180 | } |
| 2181 | deviceDriverStart+=strlen(DS_TAG_DEVICE_DRIVER_VERSION); |
| 2182 | deviceDriverEnd = findString(deviceDriverStart, contentEnd, DS_TAG_DEVICE_DRIVER_VERSION_END); |
| 2183 | if (deviceDriverEnd ==NULL) { |
| 2184 | status = DS_PROFILE_FILE_ERROR; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2185 | goto cleanup; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
| 2188 | |
| 2189 | tmpStart = findString(dataStart, contentEnd, DS_TAG_DEVICE_MAX_COMPUTE_UNITS); |
| 2190 | if (tmpStart==NULL) { |
| 2191 | status = DS_PROFILE_FILE_ERROR; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2192 | goto cleanup; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2193 | } |
| 2194 | tmpStart+=strlen(DS_TAG_DEVICE_MAX_COMPUTE_UNITS); |
| 2195 | tmpEnd = findString(tmpStart, contentEnd, DS_TAG_DEVICE_MAX_COMPUTE_UNITS_END); |
| 2196 | if (tmpEnd ==NULL) { |
| 2197 | status = DS_PROFILE_FILE_ERROR; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2198 | goto cleanup; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2199 | } |
| 2200 | memcpy(tmp,tmpStart,tmpEnd-tmpStart); |
| 2201 | tmp[tmpEnd-tmpStart] = '\0'; |
cristy | 79d0531 | 2014-12-25 18:13:29 +0000 | [diff] [blame] | 2202 | maxComputeUnits = strtol(tmp,(char **) NULL,10); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2203 | |
| 2204 | |
| 2205 | tmpStart = findString(dataStart, contentEnd, DS_TAG_DEVICE_MAX_CLOCK_FREQ); |
| 2206 | if (tmpStart==NULL) { |
| 2207 | status = DS_PROFILE_FILE_ERROR; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2208 | goto cleanup; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2209 | } |
| 2210 | tmpStart+=strlen(DS_TAG_DEVICE_MAX_CLOCK_FREQ); |
| 2211 | tmpEnd = findString(tmpStart, contentEnd, DS_TAG_DEVICE_MAX_CLOCK_FREQ_END); |
| 2212 | if (tmpEnd ==NULL) { |
| 2213 | status = DS_PROFILE_FILE_ERROR; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2214 | goto cleanup; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2215 | } |
| 2216 | memcpy(tmp,tmpStart,tmpEnd-tmpStart); |
| 2217 | tmp[tmpEnd-tmpStart] = '\0'; |
cristy | 79d0531 | 2014-12-25 18:13:29 +0000 | [diff] [blame] | 2218 | maxClockFrequency = strtol(tmp,(char **) NULL,10); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2219 | |
| 2220 | |
| 2221 | /* check if this device is on the system */ |
| 2222 | for (i = 0; i < profile->numDevices; i++) { |
| 2223 | if (profile->devices[i].type == DS_DEVICE_OPENCL_DEVICE) { |
| 2224 | size_t actualDeviceNameLength; |
| 2225 | size_t driverVersionLength; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2226 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2227 | actualDeviceNameLength = strlen(profile->devices[i].oclDeviceName); |
| 2228 | driverVersionLength = strlen(profile->devices[i].oclDriverVersion); |
dirk | 5dcb762 | 2013-12-01 10:43:43 +0000 | [diff] [blame] | 2229 | if (actualDeviceNameLength == (size_t)(deviceNameEnd - deviceNameStart) |
| 2230 | && driverVersionLength == (size_t)(deviceDriverEnd - deviceDriverStart) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2231 | && maxComputeUnits == profile->devices[i].oclMaxComputeUnits |
| 2232 | && maxClockFrequency == profile->devices[i].oclMaxClockFrequency |
| 2233 | && strncmp(profile->devices[i].oclDeviceName, deviceNameStart, actualDeviceNameLength)==(int)0 |
| 2234 | && strncmp(profile->devices[i].oclDriverVersion, deviceDriverStart, driverVersionLength)==(int)0) { |
| 2235 | |
| 2236 | deviceScoreStart = findString(dataStart, contentEnd, DS_TAG_SCORE); |
| 2237 | if (deviceNameStart==NULL) { |
| 2238 | status = DS_PROFILE_FILE_ERROR; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2239 | goto cleanup; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2240 | } |
| 2241 | deviceScoreStart+=strlen(DS_TAG_SCORE); |
| 2242 | deviceScoreEnd = findString(deviceScoreStart, contentEnd, DS_TAG_SCORE_END); |
| 2243 | status = deserializer(profile->devices+i, (const unsigned char*)deviceScoreStart, deviceScoreEnd-deviceScoreStart); |
| 2244 | if (status != DS_SUCCESS) { |
| 2245 | goto cleanup; |
| 2246 | } |
| 2247 | } |
| 2248 | } |
| 2249 | } |
| 2250 | |
| 2251 | } |
| 2252 | else if (deviceType == DS_DEVICE_NATIVE_CPU) { |
| 2253 | for (i = 0; i < profile->numDevices; i++) { |
| 2254 | if (profile->devices[i].type == DS_DEVICE_NATIVE_CPU) { |
| 2255 | deviceScoreStart = findString(dataStart, contentEnd, DS_TAG_SCORE); |
| 2256 | if (deviceScoreStart==NULL) { |
| 2257 | status = DS_PROFILE_FILE_ERROR; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2258 | goto cleanup; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2259 | } |
| 2260 | deviceScoreStart+=strlen(DS_TAG_SCORE); |
| 2261 | deviceScoreEnd = findString(deviceScoreStart, contentEnd, DS_TAG_SCORE_END); |
| 2262 | status = deserializer(profile->devices+i, (const unsigned char*)deviceScoreStart, deviceScoreEnd-deviceScoreStart); |
| 2263 | if (status != DS_SUCCESS) { |
| 2264 | goto cleanup; |
| 2265 | } |
| 2266 | } |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | /* skip over the current one to find the next device */ |
| 2271 | currentPosition = dataEnd+strlen(DS_TAG_DEVICE_END); |
| 2272 | } |
| 2273 | } |
| 2274 | cleanup: |
| 2275 | if (contentStart!=NULL) free(contentStart); |
| 2276 | return status; |
| 2277 | } |
| 2278 | |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2279 | |
| 2280 | #if 0 |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2281 | static ds_status getNumDeviceWithEmptyScore(ds_profile* profile, unsigned int* num) { |
| 2282 | unsigned int i; |
| 2283 | if (profile == NULL || num==NULL) |
| 2284 | return DS_MEMORY_ERROR; |
| 2285 | *num=0; |
| 2286 | for (i = 0; i < profile->numDevices; i++) { |
| 2287 | if (profile->devices[i].score == NULL) { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2288 | (*num)++; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2289 | } |
| 2290 | } |
| 2291 | return DS_SUCCESS; |
| 2292 | } |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2293 | #endif |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2294 | |
| 2295 | /* |
| 2296 | End of the OpenCL device selection infrastructure |
| 2297 | */ |
| 2298 | |
| 2299 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2300 | typedef double AccelerateScoreType; |
| 2301 | |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2302 | static ds_status AcceleratePerfEvaluator(ds_device *device, |
| 2303 | void *magick_unused(data)) |
| 2304 | { |
| 2305 | #define ACCELERATE_PERF_DIMEN "2048x1536" |
| 2306 | #define NUM_ITER 2 |
| 2307 | #define ReturnStatus(status) \ |
| 2308 | { \ |
| 2309 | if (clEnv!=NULL) \ |
| 2310 | RelinquishMagickOpenCLEnv(clEnv); \ |
| 2311 | if (oldClEnv!=NULL) \ |
| 2312 | defaultCLEnv = oldClEnv; \ |
| 2313 | return status; \ |
| 2314 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2315 | |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2316 | AccelerateTimer |
| 2317 | timer; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2318 | |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2319 | ExceptionInfo |
| 2320 | *exception=NULL; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2321 | |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2322 | MagickCLEnv |
| 2323 | clEnv=NULL, |
| 2324 | oldClEnv=NULL; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2325 | |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2326 | magick_unreferenced(data); |
| 2327 | |
| 2328 | if (device == NULL) |
| 2329 | ReturnStatus(DS_PERF_EVALUATOR_ERROR); |
| 2330 | |
| 2331 | clEnv=AcquireMagickOpenCLEnv(); |
| 2332 | exception=AcquireExceptionInfo(); |
| 2333 | |
| 2334 | if (device->type == DS_DEVICE_NATIVE_CPU) |
| 2335 | { |
| 2336 | /* CPU device */ |
| 2337 | MagickBooleanType flag=MagickTrue; |
| 2338 | SetMagickOpenCLEnvParamInternal(clEnv, |
| 2339 | MAGICK_OPENCL_ENV_PARAM_OPENCL_DISABLED,sizeof(MagickBooleanType), |
| 2340 | &flag,exception); |
| 2341 | } |
| 2342 | else if (device->type == DS_DEVICE_OPENCL_DEVICE) |
| 2343 | { |
| 2344 | /* OpenCL device */ |
| 2345 | SetMagickOpenCLEnvParamInternal(clEnv,MAGICK_OPENCL_ENV_PARAM_DEVICE, |
| 2346 | sizeof(cl_device_id),&device->oclDeviceID,exception); |
| 2347 | } |
| 2348 | else |
| 2349 | ReturnStatus(DS_PERF_EVALUATOR_ERROR); |
| 2350 | |
dirk | 20932d3 | 2013-12-12 06:16:19 +0000 | [diff] [blame] | 2351 | /* recompile the OpenCL kernels if it needs to */ |
| 2352 | clEnv->disableProgramCache = defaultCLEnv->disableProgramCache; |
| 2353 | |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2354 | InitOpenCLEnvInternal(clEnv,exception); |
| 2355 | oldClEnv=defaultCLEnv; |
| 2356 | defaultCLEnv=clEnv; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2357 | |
| 2358 | /* microbenchmark */ |
| 2359 | { |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2360 | Image |
| 2361 | *inputImage; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2362 | |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2363 | ImageInfo |
| 2364 | *imageInfo; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2365 | |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2366 | int |
| 2367 | i; |
| 2368 | |
| 2369 | imageInfo=AcquireImageInfo(); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2370 | CloneString(&imageInfo->size,ACCELERATE_PERF_DIMEN); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 2371 | CopyMagickString(imageInfo->filename,"xc:none",MagickPathExtent); |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2372 | inputImage=ReadImage(imageInfo,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2373 | |
| 2374 | initAccelerateTimer(&timer); |
| 2375 | |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2376 | for (i=0; i<=NUM_ITER; i++) |
| 2377 | { |
| 2378 | Image |
| 2379 | *bluredImage, |
| 2380 | *resizedImage, |
| 2381 | *unsharpedImage; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2382 | |
| 2383 | if (i > 0) |
| 2384 | startAccelerateTimer(&timer); |
| 2385 | |
| 2386 | #ifdef MAGICKCORE_CLPERFMARKER |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2387 | clBeginPerfMarkerAMD("PerfEvaluatorRegion",""); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2388 | #endif |
| 2389 | |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2390 | bluredImage=BlurImage(inputImage,10.0f,3.5f,exception); |
| 2391 | unsharpedImage=UnsharpMaskImage(bluredImage,2.0f,2.0f,50.0f,10.0f, |
| 2392 | exception); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2393 | resizedImage=ResizeImage(unsharpedImage,640,480,LanczosFilter, |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2394 | exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2395 | |
| 2396 | #ifdef MAGICKCORE_CLPERFMARKER |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2397 | clEndPerfMarkerAMD(); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2398 | #endif |
| 2399 | |
| 2400 | if (i > 0) |
| 2401 | stopAccelerateTimer(&timer); |
| 2402 | |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2403 | if (bluredImage) |
| 2404 | DestroyImage(bluredImage); |
| 2405 | if (unsharpedImage) |
| 2406 | DestroyImage(unsharpedImage); |
| 2407 | if (resizedImage) |
| 2408 | DestroyImage(resizedImage); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2409 | } |
| 2410 | DestroyImage(inputImage); |
| 2411 | } |
| 2412 | /* end of microbenchmark */ |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2413 | |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2414 | if (device->score == NULL) |
| 2415 | device->score=malloc(sizeof(AccelerateScoreType)); |
| 2416 | *(AccelerateScoreType*)device->score=readAccelerateTimer(&timer); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2417 | |
dirk | 22624f1 | 2013-12-01 17:16:37 +0000 | [diff] [blame] | 2418 | ReturnStatus(DS_SUCCESS); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2421 | ds_status AccelerateScoreSerializer(ds_device* device, void** serializedScore, unsigned int* serializedScoreSize) { |
| 2422 | if (device |
| 2423 | && device->score) { |
| 2424 | /* generate a string from the score */ |
| 2425 | char* s = (char*)malloc(sizeof(char)*256); |
| 2426 | sprintf(s,"%.4f",*((AccelerateScoreType*)device->score)); |
| 2427 | *serializedScore = (void*)s; |
dirk | b0d783f | 2014-08-31 10:48:05 +0000 | [diff] [blame] | 2428 | *serializedScoreSize = (unsigned int) strlen(s); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2429 | return DS_SUCCESS; |
| 2430 | } |
| 2431 | else { |
| 2432 | return DS_SCORE_SERIALIZER_ERROR; |
| 2433 | } |
| 2434 | } |
| 2435 | |
| 2436 | ds_status AccelerateScoreDeserializer(ds_device* device, const unsigned char* serializedScore, unsigned int serializedScoreSize) { |
| 2437 | if (device) { |
| 2438 | /* convert the string back to an int */ |
| 2439 | char* s = (char*)malloc(serializedScoreSize+1); |
| 2440 | memcpy(s, serializedScore, serializedScoreSize); |
| 2441 | s[serializedScoreSize] = (char)'\0'; |
| 2442 | device->score = malloc(sizeof(AccelerateScoreType)); |
cristy | 79d0531 | 2014-12-25 18:13:29 +0000 | [diff] [blame] | 2443 | *((AccelerateScoreType*)device->score) = (AccelerateScoreType) |
dirk | 53dd0e4 | 2014-12-26 03:01:48 +0000 | [diff] [blame] | 2444 | strtod(s,(char **) NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2445 | free(s); |
| 2446 | return DS_SUCCESS; |
| 2447 | } |
| 2448 | else { |
| 2449 | return DS_SCORE_DESERIALIZER_ERROR; |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | ds_status AccelerateScoreRelease(void* score) { |
| 2454 | if (score!=NULL) { |
| 2455 | free(score); |
| 2456 | } |
| 2457 | return DS_SUCCESS; |
| 2458 | } |
| 2459 | |
dirk | 1e3b22a | 2014-08-28 05:36:18 +0000 | [diff] [blame] | 2460 | ds_status canWriteProfileToFile(const char *path) |
dirk | b05dcc9 | 2014-08-27 15:30:53 +0000 | [diff] [blame] | 2461 | { |
cristy | b515e68 | 2014-10-18 00:26:13 +0000 | [diff] [blame] | 2462 | FILE* profileFile = fopen(path, "ab"); |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2463 | |
dirk | b05dcc9 | 2014-08-27 15:30:53 +0000 | [diff] [blame] | 2464 | if (profileFile==NULL) |
| 2465 | return DS_FILE_ERROR; |
| 2466 | |
| 2467 | fclose(profileFile); |
| 2468 | return DS_SUCCESS; |
| 2469 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2470 | |
| 2471 | #define IMAGEMAGICK_PROFILE_VERSION "ImageMagick Device Selection v0.9" |
| 2472 | #define IMAGEMAGICK_PROFILE_FILE "ImagemagickOpenCLDeviceProfile" |
| 2473 | static MagickBooleanType autoSelectDevice(MagickCLEnv clEnv, ExceptionInfo* exception) { |
| 2474 | |
| 2475 | MagickBooleanType mStatus = MagickFalse; |
| 2476 | ds_status status; |
| 2477 | ds_profile* profile; |
| 2478 | unsigned int numDeviceProfiled = 0; |
| 2479 | unsigned int i; |
| 2480 | unsigned int bestDeviceIndex; |
| 2481 | AccelerateScoreType bestScore; |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 2482 | char path[MagickPathExtent]; |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2483 | MagickBooleanType flag; |
dirk | 20932d3 | 2013-12-12 06:16:19 +0000 | [diff] [blame] | 2484 | ds_evaluation_type profileType; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2485 | |
| 2486 | LockDefaultOpenCLEnv(); |
| 2487 | |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2488 | /* Initially, just set OpenCL to off */ |
| 2489 | flag = MagickTrue; |
| 2490 | SetMagickOpenCLEnvParamInternal(clEnv, MAGICK_OPENCL_ENV_PARAM_OPENCL_DISABLED |
| 2491 | , sizeof(MagickBooleanType), &flag, exception); |
| 2492 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2493 | /* check and init the global lib */ |
| 2494 | OpenCLLib=GetOpenCLLib(); |
| 2495 | if (OpenCLLib==NULL) |
| 2496 | { |
| 2497 | mStatus=InitOpenCLEnvInternal(clEnv, exception); |
| 2498 | goto cleanup; |
| 2499 | } |
| 2500 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2501 | status = initDSProfile(&profile, IMAGEMAGICK_PROFILE_VERSION); |
| 2502 | if (status!=DS_SUCCESS) { |
| 2503 | (void) ThrowMagickException(exception, GetMagickModule(), ModuleFatalError, "Error when initializing the profile", "'%s'", "."); |
| 2504 | goto cleanup; |
| 2505 | } |
| 2506 | |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 2507 | (void) FormatLocaleString(path,MagickPathExtent,"%s%s%s" |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2508 | ,GetOpenCLCachedFilesDirectory() |
| 2509 | ,DirectorySeparator,IMAGEMAGICK_PROFILE_FILE); |
| 2510 | |
dirk | b05dcc9 | 2014-08-27 15:30:53 +0000 | [diff] [blame] | 2511 | if (canWriteProfileToFile(path) != DS_SUCCESS) { |
| 2512 | /* We can not write out a device profile, so don't run the benchmark */ |
| 2513 | /* select the first GPU device */ |
dirk | 20932d3 | 2013-12-12 06:16:19 +0000 | [diff] [blame] | 2514 | |
dirk | b05dcc9 | 2014-08-27 15:30:53 +0000 | [diff] [blame] | 2515 | bestDeviceIndex = 0; |
| 2516 | for (i = 1; i < profile->numDevices; i++) { |
| 2517 | if ((profile->devices[i].type == DS_DEVICE_OPENCL_DEVICE) && (profile->devices[i].oclDeviceType == CL_DEVICE_TYPE_GPU)) { |
| 2518 | bestDeviceIndex = i; |
| 2519 | break; |
| 2520 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2521 | } |
| 2522 | } |
dirk | b05dcc9 | 2014-08-27 15:30:53 +0000 | [diff] [blame] | 2523 | else { |
| 2524 | if (clEnv->regenerateProfile != MagickFalse) { |
| 2525 | profileType = DS_EVALUATE_ALL; |
| 2526 | } |
| 2527 | else { |
| 2528 | readProfileFromFile(profile, AccelerateScoreDeserializer, path); |
| 2529 | profileType = DS_EVALUATE_NEW_ONLY; |
| 2530 | } |
| 2531 | status = profileDevices(profile, profileType, AcceleratePerfEvaluator, NULL, &numDeviceProfiled); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2532 | |
dirk | b05dcc9 | 2014-08-27 15:30:53 +0000 | [diff] [blame] | 2533 | if (status!=DS_SUCCESS) { |
| 2534 | (void) ThrowMagickException(exception, GetMagickModule(), ModuleFatalError, "Error when initializing the profile", "'%s'", "."); |
| 2535 | goto cleanup; |
| 2536 | } |
| 2537 | if (numDeviceProfiled > 0) { |
| 2538 | status = writeProfileToFile(profile, AccelerateScoreSerializer, path); |
| 2539 | if (status!=DS_SUCCESS) { |
| 2540 | (void) ThrowMagickException(exception, GetMagickModule(), ModuleWarning, "Error when saving the profile into a file", "'%s'", "."); |
| 2541 | } |
| 2542 | } |
| 2543 | |
| 2544 | /* pick the best device */ |
| 2545 | bestDeviceIndex = 0; |
| 2546 | bestScore = *(AccelerateScoreType*)profile->devices[bestDeviceIndex].score; |
| 2547 | for (i = 1; i < profile->numDevices; i++) { |
| 2548 | AccelerateScoreType score = *(AccelerateScoreType*)profile->devices[i].score; |
| 2549 | if (score < bestScore) { |
| 2550 | bestDeviceIndex = i; |
| 2551 | bestScore = score; |
| 2552 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2553 | } |
| 2554 | } |
| 2555 | |
| 2556 | /* set up clEnv with the best device */ |
| 2557 | if (profile->devices[bestDeviceIndex].type == DS_DEVICE_NATIVE_CPU) { |
| 2558 | /* CPU device */ |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2559 | flag = MagickTrue; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2560 | SetMagickOpenCLEnvParamInternal(clEnv, MAGICK_OPENCL_ENV_PARAM_OPENCL_DISABLED |
| 2561 | , sizeof(MagickBooleanType), &flag, exception); |
| 2562 | } |
| 2563 | else if (profile->devices[bestDeviceIndex].type == DS_DEVICE_OPENCL_DEVICE) { |
| 2564 | /* OpenCL device */ |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2565 | flag = MagickFalse; |
| 2566 | SetMagickOpenCLEnvParamInternal(clEnv, MAGICK_OPENCL_ENV_PARAM_OPENCL_DISABLED |
| 2567 | , sizeof(MagickBooleanType), &flag, exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2568 | SetMagickOpenCLEnvParamInternal(clEnv, MAGICK_OPENCL_ENV_PARAM_DEVICE |
| 2569 | , sizeof(cl_device_id), &profile->devices[bestDeviceIndex].oclDeviceID,exception); |
| 2570 | } |
| 2571 | else { |
| 2572 | status = DS_PERF_EVALUATOR_ERROR; |
| 2573 | goto cleanup; |
| 2574 | } |
dirk | 6b57c96 | 2013-11-30 19:14:02 +0000 | [diff] [blame] | 2575 | mStatus=InitOpenCLEnvInternal(clEnv, exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2576 | |
| 2577 | status = releaseDSProfile(profile, AccelerateScoreRelease); |
| 2578 | if (status!=DS_SUCCESS) { |
| 2579 | (void) ThrowMagickException(exception, GetMagickModule(), ModuleWarning, "Error when releasing the profile", "'%s'", "."); |
| 2580 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2581 | |
| 2582 | cleanup: |
| 2583 | |
| 2584 | UnlockDefaultOpenCLEnv(); |
| 2585 | return mStatus; |
| 2586 | } |
| 2587 | |
| 2588 | |
| 2589 | /* |
| 2590 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2591 | % % |
| 2592 | % % |
| 2593 | % % |
| 2594 | + I n i t I m a g e M a g i c k O p e n C L % |
| 2595 | % % |
| 2596 | % % |
| 2597 | % % |
| 2598 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2599 | % |
| 2600 | % InitImageMagickOpenCL() provides a simplified interface to initialize |
| 2601 | % the OpenCL environtment in ImageMagick |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2602 | % |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2603 | % The format of the InitImageMagickOpenCL() method is: |
| 2604 | % |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2605 | % MagickBooleanType InitImageMagickOpenCL(ImageMagickOpenCLMode mode, |
| 2606 | % void* userSelectedDevice, |
| 2607 | % void* selectedDevice) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2608 | % |
| 2609 | % A description of each parameter follows: |
| 2610 | % |
| 2611 | % o mode: OpenCL mode in ImageMagick, could be off,auto,user |
| 2612 | % |
| 2613 | % o userSelectedDevice: when in user mode, a pointer to the selected |
| 2614 | % cl_device_id |
| 2615 | % |
| 2616 | % o selectedDevice: a pointer to cl_device_id where the selected |
| 2617 | % cl_device_id by ImageMagick could be returned |
| 2618 | % |
| 2619 | % o exception: exception |
| 2620 | % |
| 2621 | */ |
dirk | ed7eb1e | 2013-12-04 05:53:08 +0000 | [diff] [blame] | 2622 | MagickExport MagickBooleanType InitImageMagickOpenCL( |
| 2623 | ImageMagickOpenCLMode mode,void *userSelectedDevice,void *selectedDevice, |
| 2624 | ExceptionInfo *exception) |
| 2625 | { |
dirk | cec9dd6 | 2014-04-08 22:59:41 +0000 | [diff] [blame] | 2626 | MagickBooleanType status = MagickFalse; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2627 | MagickCLEnv clEnv = NULL; |
| 2628 | MagickBooleanType flag; |
| 2629 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2630 | clEnv = GetDefaultOpenCLEnv(); |
| 2631 | if (clEnv!=NULL) { |
| 2632 | switch(mode) { |
| 2633 | |
| 2634 | case MAGICK_OPENCL_OFF: |
| 2635 | flag = MagickTrue; |
| 2636 | SetMagickOpenCLEnvParam(clEnv, MAGICK_OPENCL_ENV_PARAM_OPENCL_DISABLED |
| 2637 | , sizeof(MagickBooleanType), &flag, exception); |
| 2638 | status = InitOpenCLEnv(clEnv, exception); |
| 2639 | |
| 2640 | if (selectedDevice) |
| 2641 | *(cl_device_id*)selectedDevice = NULL; |
| 2642 | break; |
| 2643 | |
| 2644 | case MAGICK_OPENCL_DEVICE_SELECT_USER: |
| 2645 | |
| 2646 | if (userSelectedDevice == NULL) |
| 2647 | return MagickFalse; |
| 2648 | |
| 2649 | flag = MagickFalse; |
| 2650 | SetMagickOpenCLEnvParam(clEnv, MAGICK_OPENCL_ENV_PARAM_OPENCL_DISABLED |
| 2651 | , sizeof(MagickBooleanType), &flag, exception); |
| 2652 | |
| 2653 | SetMagickOpenCLEnvParam(clEnv, MAGICK_OPENCL_ENV_PARAM_DEVICE |
| 2654 | , sizeof(cl_device_id), userSelectedDevice,exception); |
| 2655 | |
| 2656 | status = InitOpenCLEnv(clEnv, exception); |
| 2657 | if (selectedDevice) { |
| 2658 | GetMagickOpenCLEnvParam(clEnv, MAGICK_OPENCL_ENV_PARAM_DEVICE |
| 2659 | , sizeof(cl_device_id), selectedDevice, exception); |
| 2660 | } |
| 2661 | break; |
| 2662 | |
dirk | 20932d3 | 2013-12-12 06:16:19 +0000 | [diff] [blame] | 2663 | case MAGICK_OPENCL_DEVICE_SELECT_AUTO_CLEAR_CACHE: |
| 2664 | flag = MagickTrue; |
| 2665 | SetMagickOpenCLEnvParam(clEnv, MAGICK_OPENCL_ENV_PARAM_PROGRAM_CACHE_DISABLED |
| 2666 | , sizeof(MagickBooleanType), &flag, exception); |
| 2667 | flag = MagickTrue; |
| 2668 | SetMagickOpenCLEnvParam(clEnv, MAGICK_OPENCL_ENV_PARAM_REGENERATE_PROFILE |
| 2669 | , sizeof(MagickBooleanType), &flag, exception); |
| 2670 | |
| 2671 | /* fall through here!! */ |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2672 | case MAGICK_OPENCL_DEVICE_SELECT_AUTO: |
| 2673 | default: |
| 2674 | { |
| 2675 | cl_device_id d = NULL; |
| 2676 | flag = MagickFalse; |
| 2677 | SetMagickOpenCLEnvParam(clEnv, MAGICK_OPENCL_ENV_PARAM_OPENCL_DISABLED |
| 2678 | , sizeof(MagickBooleanType), &flag, exception); |
| 2679 | SetMagickOpenCLEnvParam(clEnv, MAGICK_OPENCL_ENV_PARAM_DEVICE |
| 2680 | , sizeof(cl_device_id), &d,exception); |
| 2681 | status = InitOpenCLEnv(clEnv, exception); |
| 2682 | if (selectedDevice) { |
| 2683 | GetMagickOpenCLEnvParam(clEnv, MAGICK_OPENCL_ENV_PARAM_DEVICE |
| 2684 | , sizeof(cl_device_id), selectedDevice, exception); |
| 2685 | } |
| 2686 | } |
| 2687 | break; |
| 2688 | }; |
| 2689 | } |
| 2690 | return status; |
| 2691 | } |
| 2692 | |
| 2693 | |
dirk | 20932d3 | 2013-12-12 06:16:19 +0000 | [diff] [blame] | 2694 | MagickPrivate |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2695 | MagickBooleanType OpenCLThrowMagickException(ExceptionInfo *exception, |
| 2696 | const char *module,const char *function,const size_t line, |
| 2697 | const ExceptionType severity,const char *tag,const char *format,...) { |
| 2698 | MagickBooleanType |
| 2699 | status; |
| 2700 | |
| 2701 | MagickCLEnv clEnv; |
| 2702 | |
| 2703 | status = MagickTrue; |
| 2704 | |
| 2705 | clEnv = GetDefaultOpenCLEnv(); |
| 2706 | |
| 2707 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 2708 | assert(exception->signature == MagickCoreSignature); |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2709 | |
| 2710 | if (severity!=0) { |
| 2711 | cl_device_type dType; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2712 | clEnv->library->clGetDeviceInfo(clEnv->device,CL_DEVICE_TYPE ,sizeof(cl_device_type),&dType,NULL); |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2713 | if (dType == CL_DEVICE_TYPE_CPU) { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 2714 | char buffer[MagickPathExtent]; |
| 2715 | clEnv->library->clGetPlatformInfo(clEnv->platform, CL_PLATFORM_NAME, MagickPathExtent, buffer, NULL); |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2716 | |
| 2717 | /* Workaround for Intel OpenCL CPU runtime bug */ |
| 2718 | /* Turn off OpenCL when a problem is detected! */ |
| 2719 | if (strncmp(buffer, "Intel",5) == 0) { |
| 2720 | |
| 2721 | InitImageMagickOpenCL(MAGICK_OPENCL_OFF, NULL, NULL, exception); |
| 2722 | } |
| 2723 | } |
| 2724 | } |
| 2725 | |
| 2726 | #ifdef OPENCLLOG_ENABLED |
| 2727 | { |
| 2728 | va_list |
| 2729 | operands; |
| 2730 | va_start(operands,format); |
| 2731 | status=ThrowMagickExceptionList(exception,module,function,line,severity,tag, format,operands); |
| 2732 | va_end(operands); |
| 2733 | } |
| 2734 | #else |
| 2735 | magick_unreferenced(module); |
| 2736 | magick_unreferenced(function); |
| 2737 | magick_unreferenced(line); |
| 2738 | magick_unreferenced(tag); |
| 2739 | magick_unreferenced(format); |
| 2740 | #endif |
| 2741 | |
| 2742 | return(status); |
| 2743 | } |
| 2744 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2745 | MagickPrivate cl_mem GetAndLockRandSeedBuffer(MagickCLEnv clEnv) |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2746 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2747 | LockSemaphoreInfo(clEnv->lock); |
| 2748 | if (clEnv->seedsLock == NULL) |
| 2749 | { |
| 2750 | ActivateSemaphoreInfo(&clEnv->seedsLock); |
| 2751 | } |
| 2752 | LockSemaphoreInfo(clEnv->seedsLock); |
| 2753 | |
| 2754 | if (clEnv->seeds == NULL) |
| 2755 | { |
| 2756 | cl_int clStatus; |
| 2757 | clEnv->numGenerators = NUM_CL_RAND_GENERATORS; |
| 2758 | clEnv->seeds = clEnv->library->clCreateBuffer(clEnv->context, CL_MEM_READ_WRITE, |
| 2759 | clEnv->numGenerators*4*sizeof(unsigned int), |
| 2760 | NULL, &clStatus); |
| 2761 | if (clStatus != CL_SUCCESS) |
| 2762 | { |
| 2763 | clEnv->seeds = NULL; |
| 2764 | } |
| 2765 | else |
| 2766 | { |
| 2767 | unsigned int i; |
| 2768 | cl_command_queue queue = NULL; |
| 2769 | unsigned int *seeds; |
| 2770 | |
| 2771 | queue = AcquireOpenCLCommandQueue(clEnv); |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2772 | seeds = (unsigned int*) clEnv->library->clEnqueueMapBuffer(queue, clEnv->seeds, CL_TRUE, |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2773 | CL_MAP_WRITE, 0, |
| 2774 | clEnv->numGenerators*4 |
| 2775 | *sizeof(unsigned int), |
| 2776 | 0, NULL, NULL, &clStatus); |
| 2777 | if (clStatus!=CL_SUCCESS) |
| 2778 | { |
| 2779 | clEnv->library->clReleaseMemObject(clEnv->seeds); |
| 2780 | goto cleanup; |
| 2781 | } |
| 2782 | |
| 2783 | for (i = 0; i < clEnv->numGenerators; i++) { |
| 2784 | RandomInfo* randomInfo = AcquireRandomInfo(); |
| 2785 | const unsigned long* s = GetRandomInfoSeed(randomInfo); |
| 2786 | if (i == 0) |
| 2787 | clEnv->randNormalize = GetRandomInfoNormalize(randomInfo); |
| 2788 | |
| 2789 | seeds[i*4] = (unsigned int) s[0]; |
| 2790 | seeds[i*4+1] = (unsigned int) 0x50a7f451; |
| 2791 | seeds[i*4+2] = (unsigned int) 0x5365417e; |
| 2792 | seeds[i*4+3] = (unsigned int) 0xc3a4171a; |
| 2793 | |
| 2794 | randomInfo = DestroyRandomInfo(randomInfo); |
| 2795 | } |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2796 | clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, clEnv->seeds, seeds, 0, |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2797 | NULL, NULL); |
| 2798 | clEnv->library->clFinish(queue); |
| 2799 | cleanup: |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2800 | if (queue != NULL) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2801 | RelinquishOpenCLCommandQueue(clEnv, queue); |
| 2802 | } |
| 2803 | } |
| 2804 | UnlockSemaphoreInfo(clEnv->lock); |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2805 | return clEnv->seeds; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | MagickPrivate void UnlockRandSeedBuffer(MagickCLEnv clEnv) { |
| 2809 | if (clEnv->seedsLock == NULL) |
| 2810 | { |
| 2811 | ActivateSemaphoreInfo(&clEnv->seedsLock); |
| 2812 | } |
| 2813 | else |
| 2814 | UnlockSemaphoreInfo(clEnv->seedsLock); |
| 2815 | } |
| 2816 | |
| 2817 | MagickPrivate unsigned int GetNumRandGenerators(MagickCLEnv clEnv) |
| 2818 | { |
| 2819 | return clEnv->numGenerators; |
| 2820 | } |
| 2821 | |
| 2822 | |
| 2823 | MagickPrivate float GetRandNormalize(MagickCLEnv clEnv) |
| 2824 | { |
| 2825 | return clEnv->randNormalize; |
| 2826 | } |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2827 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2828 | #else |
| 2829 | |
| 2830 | struct _MagickCLEnv { |
| 2831 | MagickBooleanType OpenCLInitialized; /* whether OpenCL environment is initialized. */ |
| 2832 | }; |
| 2833 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2834 | MagickExport MagickCLEnv AcquireMagickOpenCLEnv() |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2835 | { |
| 2836 | return NULL; |
| 2837 | } |
| 2838 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2839 | MagickExport MagickBooleanType RelinquishMagickOpenCLEnv( |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2840 | MagickCLEnv magick_unused(clEnv)) |
| 2841 | { |
| 2842 | magick_unreferenced(clEnv); |
| 2843 | |
| 2844 | return MagickFalse; |
| 2845 | } |
| 2846 | |
| 2847 | /* |
| 2848 | * Return the OpenCL environment |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2849 | */ |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 2850 | MagickExport MagickCLEnv GetDefaultOpenCLEnv() |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2851 | { |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2852 | return (MagickCLEnv) NULL; |
| 2853 | } |
| 2854 | |
| 2855 | MagickExport MagickCLEnv SetDefaultOpenCLEnv( |
| 2856 | MagickCLEnv magick_unused(clEnv)) |
| 2857 | { |
| 2858 | magick_unreferenced(clEnv); |
| 2859 | |
| 2860 | return (MagickCLEnv) NULL; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2861 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2862 | |
| 2863 | MagickExport MagickBooleanType SetMagickOpenCLEnvParam( |
| 2864 | MagickCLEnv magick_unused(clEnv),MagickOpenCLEnvParam magick_unused(param), |
| 2865 | size_t magick_unused(dataSize),void *magick_unused(data), |
| 2866 | ExceptionInfo *magick_unused(exception)) |
| 2867 | { |
| 2868 | magick_unreferenced(clEnv); |
| 2869 | magick_unreferenced(param); |
| 2870 | magick_unreferenced(dataSize); |
| 2871 | magick_unreferenced(data); |
| 2872 | magick_unreferenced(exception); |
| 2873 | |
| 2874 | return MagickFalse; |
| 2875 | } |
| 2876 | |
| 2877 | MagickExport MagickBooleanType GetMagickOpenCLEnvParam( |
| 2878 | MagickCLEnv magick_unused(clEnv),MagickOpenCLEnvParam magick_unused(param), |
| 2879 | size_t magick_unused(dataSize),void *magick_unused(data), |
| 2880 | ExceptionInfo *magick_unused(exception)) |
| 2881 | { |
| 2882 | magick_unreferenced(clEnv); |
| 2883 | magick_unreferenced(param); |
| 2884 | magick_unreferenced(dataSize); |
| 2885 | magick_unreferenced(data); |
| 2886 | magick_unreferenced(exception); |
| 2887 | |
| 2888 | return MagickFalse; |
| 2889 | } |
| 2890 | |
| 2891 | MagickExport MagickBooleanType InitOpenCLEnv(MagickCLEnv magick_unused(clEnv), |
| 2892 | ExceptionInfo *magick_unused(exception)) |
| 2893 | { |
| 2894 | magick_unreferenced(clEnv); |
| 2895 | magick_unreferenced(exception); |
| 2896 | |
| 2897 | return MagickFalse; |
| 2898 | } |
| 2899 | |
cristy | 7b6514c | 2013-12-10 23:13:13 +0000 | [diff] [blame] | 2900 | MagickPrivate cl_command_queue AcquireOpenCLCommandQueue( |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2901 | MagickCLEnv magick_unused(clEnv)) |
| 2902 | { |
| 2903 | magick_unreferenced(clEnv); |
| 2904 | |
| 2905 | return (cl_command_queue) NULL; |
| 2906 | } |
| 2907 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2908 | MagickPrivate MagickBooleanType RelinquishCommandQueue( |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2909 | MagickCLEnv magick_unused(clEnv),cl_command_queue magick_unused(queue)) |
| 2910 | { |
| 2911 | magick_unreferenced(clEnv); |
| 2912 | magick_unreferenced(queue); |
| 2913 | |
| 2914 | return MagickFalse; |
| 2915 | } |
| 2916 | |
cristy | 7b6514c | 2013-12-10 23:13:13 +0000 | [diff] [blame] | 2917 | MagickPrivate cl_kernel AcquireOpenCLKernel( |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2918 | MagickCLEnv magick_unused(clEnv),MagickOpenCLProgram magick_unused(program), |
| 2919 | const char *magick_unused(kernelName)) |
| 2920 | { |
| 2921 | magick_unreferenced(clEnv); |
| 2922 | magick_unreferenced(program); |
| 2923 | magick_unreferenced(kernelName); |
| 2924 | |
cristy | f432c63 | 2014-12-07 15:11:28 +0000 | [diff] [blame] | 2925 | return (cl_kernel) NULL; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
cristy | 7b6514c | 2013-12-10 23:13:13 +0000 | [diff] [blame] | 2928 | MagickPrivate MagickBooleanType RelinquishOpenCLKernel( |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2929 | MagickCLEnv magick_unused(clEnv),cl_kernel magick_unused(kernel)) |
| 2930 | { |
| 2931 | magick_unreferenced(clEnv); |
| 2932 | magick_unreferenced(kernel); |
| 2933 | |
| 2934 | return MagickFalse; |
| 2935 | } |
| 2936 | |
cristy | 7b6514c | 2013-12-10 23:13:13 +0000 | [diff] [blame] | 2937 | MagickPrivate unsigned long GetOpenCLDeviceLocalMemorySize( |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2938 | MagickCLEnv magick_unused(clEnv)) |
| 2939 | { |
| 2940 | magick_unreferenced(clEnv); |
| 2941 | |
| 2942 | return 0; |
| 2943 | } |
| 2944 | |
dirk | ed7eb1e | 2013-12-04 05:53:08 +0000 | [diff] [blame] | 2945 | MagickExport MagickBooleanType InitImageMagickOpenCL( |
| 2946 | ImageMagickOpenCLMode magick_unused(mode), |
| 2947 | void *magick_unused(userSelectedDevice),void *magick_unused(selectedDevice), |
| 2948 | ExceptionInfo *magick_unused(exception)) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2949 | { |
| 2950 | magick_unreferenced(mode); |
| 2951 | magick_unreferenced(userSelectedDevice); |
| 2952 | magick_unreferenced(selectedDevice); |
| 2953 | magick_unreferenced(exception); |
| 2954 | return MagickFalse; |
| 2955 | } |
| 2956 | |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2957 | |
dirk | 20932d3 | 2013-12-12 06:16:19 +0000 | [diff] [blame] | 2958 | MagickPrivate |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2959 | MagickBooleanType OpenCLThrowMagickException(ExceptionInfo *exception, |
| 2960 | const char *module,const char *function,const size_t line, |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 2961 | const ExceptionType severity,const char *tag,const char *format,...) |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2962 | { |
| 2963 | magick_unreferenced(exception); |
| 2964 | magick_unreferenced(module); |
| 2965 | magick_unreferenced(function); |
| 2966 | magick_unreferenced(line); |
| 2967 | magick_unreferenced(severity); |
| 2968 | magick_unreferenced(tag); |
| 2969 | magick_unreferenced(format); |
| 2970 | return(MagickFalse); |
| 2971 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2972 | |
| 2973 | |
| 2974 | MagickPrivate cl_mem GetAndLockRandSeedBuffer(MagickCLEnv clEnv) |
| 2975 | { |
| 2976 | magick_unreferenced(clEnv); |
| 2977 | return NULL; |
| 2978 | } |
| 2979 | |
| 2980 | |
| 2981 | MagickPrivate void UnlockRandSeedBuffer(MagickCLEnv clEnv) |
| 2982 | { |
| 2983 | magick_unreferenced(clEnv); |
| 2984 | } |
| 2985 | |
| 2986 | MagickPrivate unsigned int GetNumRandGenerators(MagickCLEnv clEnv) |
| 2987 | { |
| 2988 | magick_unreferenced(clEnv); |
| 2989 | return 0; |
| 2990 | } |
| 2991 | |
| 2992 | MagickPrivate float GetRandNormalize(MagickCLEnv clEnv) |
| 2993 | { |
| 2994 | magick_unreferenced(clEnv); |
| 2995 | return 0.0f; |
| 2996 | } |
| 2997 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2998 | #endif /* MAGICKCORE_OPENCL_SUPPORT */ |
| 2999 | |
| 3000 | char* openclCachedFilesDirectory; |
| 3001 | SemaphoreInfo* openclCachedFilesDirectoryLock; |
| 3002 | |
cristy | 7b6514c | 2013-12-10 23:13:13 +0000 | [diff] [blame] | 3003 | MagickPrivate |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3004 | const char* GetOpenCLCachedFilesDirectory() { |
| 3005 | if (openclCachedFilesDirectory == NULL) { |
| 3006 | if (openclCachedFilesDirectoryLock == NULL) |
| 3007 | { |
cristy | 04b11db | 2014-02-16 15:10:39 +0000 | [diff] [blame] | 3008 | ActivateSemaphoreInfo(&openclCachedFilesDirectoryLock); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3009 | } |
| 3010 | LockSemaphoreInfo(openclCachedFilesDirectoryLock); |
| 3011 | if (openclCachedFilesDirectory == NULL) { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 3012 | char path[MagickPathExtent]; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3013 | char *home = NULL; |
| 3014 | char *temp = NULL; |
| 3015 | struct stat attributes; |
| 3016 | MagickBooleanType status; |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 3017 | int mkdirStatus = 0; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3018 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3019 | |
| 3020 | |
cristy | a45be69 | 2014-07-24 10:12:11 +0000 | [diff] [blame] | 3021 | home=GetEnvironmentValue("MAGICK_OPENCL_CACHE_DIR"); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3022 | if (home == (char *) NULL) |
| 3023 | { |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 3024 | home=GetEnvironmentValue("XDG_CACHE_HOME"); |
| 3025 | if (home == (char *) NULL) |
| 3026 | home=GetEnvironmentValue("LOCALAPPDATA"); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3027 | if (home == (char *) NULL) |
| 3028 | home=GetEnvironmentValue("APPDATA"); |
| 3029 | if (home == (char *) NULL) |
| 3030 | home=GetEnvironmentValue("USERPROFILE"); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3031 | } |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 3032 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3033 | if (home != (char *) NULL) |
| 3034 | { |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 3035 | /* first check if $HOME exists */ |
| 3036 | (void) FormatLocaleString(path,MagickPathExtent,"%s",home); |
cristy | cba9793 | 2014-03-05 22:52:17 +0000 | [diff] [blame] | 3037 | status=GetPathAttributes(path,&attributes); |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 3038 | if (status == MagickFalse) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3039 | { |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 3040 | |
cristy | cba9793 | 2014-03-05 22:52:17 +0000 | [diff] [blame] | 3041 | #ifdef MAGICKCORE_WINDOWS_SUPPORT |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3042 | mkdirStatus = mkdir(path); |
cristy | cba9793 | 2014-03-05 22:52:17 +0000 | [diff] [blame] | 3043 | #else |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3044 | mkdirStatus = mkdir(path, 0777); |
cristy | cba9793 | 2014-03-05 22:52:17 +0000 | [diff] [blame] | 3045 | #endif |
| 3046 | } |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 3047 | |
| 3048 | /* first check if $HOME/ImageMagick exists */ |
| 3049 | if (mkdirStatus==0) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3050 | { |
Cristy | 9799a84 | 2015-10-04 19:44:36 -0400 | [diff] [blame] | 3051 | (void) FormatLocaleString(path,MagickPathExtent, |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 3052 | "%s%sImageMagick",home,DirectorySeparator); |
| 3053 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3054 | status=GetPathAttributes(path,&attributes); |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 3055 | if (status == MagickFalse) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3056 | { |
| 3057 | #ifdef MAGICKCORE_WINDOWS_SUPPORT |
| 3058 | mkdirStatus = mkdir(path); |
| 3059 | #else |
| 3060 | mkdirStatus = mkdir(path, 0777); |
| 3061 | #endif |
| 3062 | } |
| 3063 | } |
| 3064 | |
| 3065 | if (mkdirStatus==0) |
| 3066 | { |
| 3067 | temp = (char*)AcquireMagickMemory(strlen(path)+1); |
| 3068 | CopyMagickString(temp,path,strlen(path)+1); |
| 3069 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3070 | home=DestroyString(home); |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 3071 | } else { |
| 3072 | home=GetEnvironmentValue("HOME"); |
| 3073 | if (home != (char *) NULL) |
| 3074 | { |
| 3075 | /* first check if $HOME/.cache exists */ |
| 3076 | (void) FormatLocaleString(path,MagickPathExtent,"%s%s.cache", |
| 3077 | home,DirectorySeparator); |
| 3078 | status=GetPathAttributes(path,&attributes); |
| 3079 | if (status == MagickFalse) |
| 3080 | { |
| 3081 | |
| 3082 | #ifdef MAGICKCORE_WINDOWS_SUPPORT |
| 3083 | mkdirStatus = mkdir(path); |
| 3084 | #else |
| 3085 | mkdirStatus = mkdir(path, 0777); |
| 3086 | #endif |
| 3087 | } |
| 3088 | |
| 3089 | /* first check if $HOME/.cache/ImageMagick exists */ |
| 3090 | if (mkdirStatus==0) |
| 3091 | { |
| 3092 | (void) FormatLocaleString(path,MagickPathExtent, |
| 3093 | "%s%s.cache%sImageMagick",home,DirectorySeparator, |
| 3094 | DirectorySeparator); |
| 3095 | |
| 3096 | status=GetPathAttributes(path,&attributes); |
| 3097 | if (status == MagickFalse) |
| 3098 | { |
| 3099 | #ifdef MAGICKCORE_WINDOWS_SUPPORT |
| 3100 | mkdirStatus = mkdir(path); |
| 3101 | #else |
| 3102 | mkdirStatus = mkdir(path, 0777); |
| 3103 | #endif |
| 3104 | } |
| 3105 | } |
| 3106 | |
| 3107 | if (mkdirStatus==0) |
| 3108 | { |
| 3109 | temp = (char*)AcquireMagickMemory(strlen(path)+1); |
| 3110 | CopyMagickString(temp,path,strlen(path)+1); |
| 3111 | } |
| 3112 | home=DestroyString(home); |
| 3113 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3114 | } |
| 3115 | openclCachedFilesDirectory = temp; |
| 3116 | } |
Cristy | 1dd96da | 2015-10-06 07:52:01 -0400 | [diff] [blame] | 3117 | UnlockSemaphoreInfo(openclCachedFilesDirectoryLock); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3118 | } |
| 3119 | return openclCachedFilesDirectory; |
| 3120 | } |
| 3121 | |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 3122 | /* create a function for OpenCL log */ |
cristy | 7b6514c | 2013-12-10 23:13:13 +0000 | [diff] [blame] | 3123 | MagickPrivate |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3124 | void OpenCLLog(const char* message) { |
| 3125 | |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 3126 | #ifdef OPENCLLOG_ENABLED |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3127 | #define OPENCL_LOG_FILE "ImageMagickOpenCL.log" |
| 3128 | |
| 3129 | FILE* log; |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 3130 | if (getenv("MAGICK_OCL_LOG")) |
| 3131 | { |
| 3132 | if (message) { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 3133 | char path[MagickPathExtent]; |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 3134 | unsigned long allocSize; |
| 3135 | |
| 3136 | MagickCLEnv clEnv; |
| 3137 | |
| 3138 | clEnv = GetDefaultOpenCLEnv(); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3139 | |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 3140 | /* dump the source into a file */ |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 3141 | (void) FormatLocaleString(path,MagickPathExtent,"%s%s%s" |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 3142 | ,GetOpenCLCachedFilesDirectory() |
| 3143 | ,DirectorySeparator,OPENCL_LOG_FILE); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3144 | |
| 3145 | |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 3146 | log = fopen(path, "ab"); |
| 3147 | fwrite(message, sizeof(char), strlen(message), log); |
| 3148 | fwrite("\n", sizeof(char), 1, log); |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 3149 | |
| 3150 | if (clEnv->OpenCLInitialized && !clEnv->OpenCLDisabled) |
| 3151 | { |
| 3152 | allocSize = GetOpenCLDeviceMaxMemAllocSize(clEnv); |
Cristy | 4cf12fd | 2015-09-05 21:20:57 -0400 | [diff] [blame] | 3153 | fprintf(log, "Devic Max Memory Alloc Size: %lu\n", allocSize); |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 3156 | fclose(log); |
| 3157 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3158 | } |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 3159 | #else |
| 3160 | magick_unreferenced(message); |
| 3161 | #endif |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3162 | } |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 3163 | |
| 3164 | MagickPrivate void OpenCLTerminus() |
| 3165 | { |
dirk | f6c70c3 | 2015-11-14 22:58:17 +0100 | [diff] [blame] | 3166 | #if MAGICKCORE_OPENCL_SUPPORT |
dirk | 251cf8e | 2015-11-14 23:17:52 +0100 | [diff] [blame] | 3167 | DumpProfileData(); |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 3168 | if (openclCachedFilesDirectory != (char *) NULL) |
| 3169 | openclCachedFilesDirectory=DestroyString(openclCachedFilesDirectory); |
| 3170 | if (openclCachedFilesDirectoryLock != (SemaphoreInfo*)NULL) |
| 3171 | RelinquishSemaphoreInfo(&openclCachedFilesDirectoryLock); |
| 3172 | if (defaultCLEnv != (MagickCLEnv) NULL) |
| 3173 | { |
| 3174 | (void) RelinquishMagickOpenCLEnv(defaultCLEnv); |
| 3175 | defaultCLEnv=(MagickCLEnv)NULL; |
| 3176 | } |
| 3177 | if (defaultCLEnvLock != (SemaphoreInfo*) NULL) |
| 3178 | RelinquishSemaphoreInfo(&defaultCLEnvLock); |
| 3179 | if (OpenCLLib != (MagickLibrary *)NULL) |
| 3180 | OpenCLLib=(MagickLibrary *)RelinquishMagickMemory(OpenCLLib); |
| 3181 | if (OpenCLLibLock != (SemaphoreInfo*)NULL) |
| 3182 | RelinquishSemaphoreInfo(&OpenCLLibLock); |
dirk | f6c70c3 | 2015-11-14 22:58:17 +0100 | [diff] [blame] | 3183 | #endif |
dirk | 9973174 | 2015-11-14 22:54:38 +0100 | [diff] [blame] | 3184 | } |