43#include "MagickCore/studio.h"
44#include "MagickCore/accelerate-kernels-private.h"
45#include "MagickCore/artifact.h"
46#include "MagickCore/cache.h"
47#include "MagickCore/cache-private.h"
48#include "MagickCore/color.h"
49#include "MagickCore/compare.h"
50#include "MagickCore/constitute.h"
51#include "MagickCore/configure.h"
52#include "MagickCore/distort.h"
53#include "MagickCore/draw.h"
54#include "MagickCore/effect.h"
55#include "MagickCore/exception.h"
56#include "MagickCore/exception-private.h"
57#include "MagickCore/fx.h"
58#include "MagickCore/gem.h"
59#include "MagickCore/geometry.h"
60#include "MagickCore/image.h"
61#include "MagickCore/image-private.h"
62#include "MagickCore/layer.h"
63#include "MagickCore/locale_.h"
64#include "MagickCore/mime-private.h"
65#include "MagickCore/memory_.h"
66#include "MagickCore/memory-private.h"
67#include "MagickCore/monitor.h"
68#include "MagickCore/montage.h"
69#include "MagickCore/morphology.h"
70#include "MagickCore/nt-base.h"
71#include "MagickCore/nt-base-private.h"
72#include "MagickCore/opencl.h"
73#include "MagickCore/opencl-private.h"
74#include "MagickCore/option.h"
75#include "MagickCore/policy.h"
76#include "MagickCore/property.h"
77#include "MagickCore/quantize.h"
78#include "MagickCore/quantum.h"
79#include "MagickCore/random_.h"
80#include "MagickCore/random-private.h"
81#include "MagickCore/resample.h"
82#include "MagickCore/resource_.h"
83#include "MagickCore/splay-tree.h"
84#include "MagickCore/semaphore.h"
85#include "MagickCore/statistic.h"
86#include "MagickCore/string_.h"
87#include "MagickCore/string-private.h"
88#include "MagickCore/token.h"
89#include "MagickCore/utility.h"
90#include "MagickCore/utility-private.h"
92#if defined(MAGICKCORE_OPENCL_SUPPORT)
93#if defined(MAGICKCORE_LTDL_DELEGATE)
100#define IMAGEMAGICK_PROFILE_FILE "ImagemagickOpenCLDeviceProfile.xml"
126} MagickCLDeviceBenchmark;
132static MagickBooleanType
134 LoadOpenCLLibrary(
void);
137 RelinquishMagickCLDevice(MagickCLDevice);
140 RelinquishMagickCLEnv(MagickCLEnv);
143 BenchmarkOpenCLDevices(MagickCLEnv);
161 *cache_directory_lock;
163static inline MagickBooleanType IsSameOpenCLDevice(MagickCLDevice a,
166 if ((LocaleCompare(a->platform_name,b->platform_name) == 0) &&
167 (LocaleCompare(a->vendor_name,b->vendor_name) == 0) &&
168 (LocaleCompare(a->name,b->name) == 0) &&
169 (LocaleCompare(a->version,b->version) == 0) &&
170 (a->max_clock_frequency == b->max_clock_frequency) &&
171 (a->max_compute_units == b->max_compute_units))
177static inline MagickBooleanType IsBenchmarkedOpenCLDevice(MagickCLDevice a,
178 MagickCLDeviceBenchmark *b)
180 if ((LocaleCompare(a->platform_name,b->platform_name) == 0) &&
181 (LocaleCompare(a->vendor_name,b->vendor_name) == 0) &&
182 (LocaleCompare(a->name,b->name) == 0) &&
183 (LocaleCompare(a->version,b->version) == 0) &&
184 (a->max_clock_frequency == b->max_clock_frequency) &&
185 (a->max_compute_units == b->max_compute_units))
191static inline void RelinquishMagickCLDevices(MagickCLEnv clEnv)
196 if (clEnv->devices != (MagickCLDevice *) NULL)
198 for (i = 0; i < clEnv->number_devices; i++)
199 clEnv->devices[i]=RelinquishMagickCLDevice(clEnv->devices[i]);
200 clEnv->devices=(MagickCLDevice *) RelinquishMagickMemory(clEnv->devices);
202 clEnv->number_devices=0;
205static inline MagickBooleanType MagickCreateDirectory(
const char *path)
210#ifdef MAGICKCORE_WINDOWS_SUPPORT
213 status=mkdir(path,0777);
215 return(status == 0 ? MagickTrue : MagickFalse);
218static inline void InitAccelerateTimer(AccelerateTimer *timer)
221 QueryPerformanceFrequency((LARGE_INTEGER*)&timer->freq);
223 timer->freq=(
long long)1.0E3;
229static inline double ReadAccelerateTimer(AccelerateTimer *timer)
231 return (
double)timer->clocks/(double)timer->freq;
234static inline void StartAccelerateTimer(AccelerateTimer* timer)
237 QueryPerformanceCounter((LARGE_INTEGER*)&timer->start);
242 timer->start=(
long long)s.tv_sec*(
long long)1.0E3+(
long long)s.tv_usec/
247static inline void StopAccelerateTimer(AccelerateTimer *timer)
254 QueryPerformanceCounter((LARGE_INTEGER*)&(n));
259 n=(
long long)s.tv_sec*(
long long)1.0E3+(
long long)s.tv_usec/
267static const char *GetOpenCLCacheDirectory()
269 if (cache_directory == (
char *) NULL)
272 ActivateSemaphoreInfo(&cache_directory_lock);
273 LockSemaphoreInfo(cache_directory_lock);
274 if (cache_directory == (
char *) NULL)
278 path[MagickPathExtent],
288 home=GetEnvironmentValue(
"MAGICK_OPENCL_CACHE_DIR");
289 if (home == (
char *) NULL)
291 home=GetEnvironmentValue(
"XDG_CACHE_HOME");
292#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
293 if (home == (
char *) NULL)
294 home=GetEnvironmentValue(
"LOCALAPPDATA");
295 if (home == (
char *) NULL)
296 home=GetEnvironmentValue(
"APPDATA");
297 if (home == (
char *) NULL)
298 home=GetEnvironmentValue(
"USERPROFILE");
302 if (home != (
char *) NULL)
305 (void) FormatLocaleString(path,MagickPathExtent,
"%s",home);
306 status=GetPathAttributes(path,&attributes);
307 if (status == MagickFalse)
308 status=MagickCreateDirectory(path);
311 if (status != MagickFalse)
313 (void) FormatLocaleString(path,MagickPathExtent,
314 "%s%sImageMagick",home,DirectorySeparator);
316 status=GetPathAttributes(path,&attributes);
317 if (status == MagickFalse)
318 status=MagickCreateDirectory(path);
321 if (status != MagickFalse)
323 temp=(
char*) AcquireCriticalMemory(strlen(path)+1);
324 CopyMagickString(temp,path,strlen(path)+1);
326 home=DestroyString(home);
330 home=GetEnvironmentValue(
"HOME");
331 if (home != (
char *) NULL)
334 (void) FormatLocaleString(path,MagickPathExtent,
"%s%s.cache",
335 home,DirectorySeparator);
336 status=GetPathAttributes(path,&attributes);
337 if (status == MagickFalse)
338 status=MagickCreateDirectory(path);
341 if (status != MagickFalse)
343 (void) FormatLocaleString(path,MagickPathExtent,
344 "%s%s.cache%sImageMagick",home,DirectorySeparator,
346 status=GetPathAttributes(path,&attributes);
347 if (status == MagickFalse)
348 status=MagickCreateDirectory(path);
351 if (status != MagickFalse)
353 temp=(
char*) AcquireCriticalMemory(strlen(path)+1);
354 CopyMagickString(temp,path,strlen(path)+1);
356 home=DestroyString(home);
359 if (temp == (
char *) NULL)
361 temp=AcquireString(
"?");
362 (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),
363 "Cannot use cache directory: \"%s\"",path);
366 (
void) LogMagickEvent(AccelerateEvent,GetMagickModule(),
367 "Using cache directory: \"%s\"",temp);
368 cache_directory=temp;
370 UnlockSemaphoreInfo(cache_directory_lock);
372 if (*cache_directory ==
'?')
373 return((
const char *) NULL);
374 return(cache_directory);
377static void SelectOpenCLDevice(MagickCLEnv clEnv,cl_device_type type)
386 (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),
387 "Selecting device for type: %d",(
int) type);
388 for (i = 0; i < clEnv->number_devices; i++)
389 clEnv->devices[i]->enabled=MagickFalse;
391 for (i = 0; i < clEnv->number_devices; i++)
393 device=clEnv->devices[i];
394 if (device->type != type)
397 device->enabled=MagickTrue;
398 (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),
399 "Selected device: %s",device->name);
400 for (j = i+1; j < clEnv->number_devices; j++)
405 other_device=clEnv->devices[j];
406 if (IsSameOpenCLDevice(device,other_device))
407 other_device->enabled=MagickTrue;
412static size_t StringSignature(
const char*
string)
427 stringLength=(size_t) strlen(
string);
428 signature=stringLength;
429 n=stringLength/
sizeof(size_t);
431 for (i = 0; i < n; i++)
433 if (n *
sizeof(
size_t) != stringLength)
439 for (i = 0; i < 4; i++, j++)
441 if (j < stringLength)
452static void DestroyMagickCLCacheInfo(MagickCLCacheInfo info)
457 for (i=0; i < (ssize_t) info->event_count; i++)
458 openCL_library->clReleaseEvent(info->events[i]);
459 info->events=(cl_event *) RelinquishMagickMemory(info->events);
460 if (info->buffer != (cl_mem) NULL)
461 openCL_library->clReleaseMemObject(info->buffer);
462 RelinquishSemaphoreInfo(&info->events_semaphore);
463 ReleaseOpenCLDevice(info->device);
464 RelinquishMagickMemory(info);
471MagickPrivate cl_mem CreateOpenCLBuffer(MagickCLDevice device,
472 cl_mem_flags flags,
size_t size,
void *host_ptr)
474 return(openCL_library->clCreateBuffer(device->context,flags,size,host_ptr,
478MagickPrivate
void ReleaseOpenCLKernel(cl_kernel kernel)
480 (void) openCL_library->clReleaseKernel(kernel);
483MagickPrivate
void ReleaseOpenCLMemObject(cl_mem memobj)
485 (void) openCL_library->clReleaseMemObject(memobj);
488MagickPrivate
void RetainOpenCLMemObject(cl_mem memobj)
490 (void) openCL_library->clRetainMemObject(memobj);
493MagickPrivate cl_int SetOpenCLKernelArg(cl_kernel kernel,
size_t arg_index,
494 size_t arg_size,
const void *arg_value)
496 return(openCL_library->clSetKernelArg(kernel,(cl_uint) arg_index,arg_size,
528MagickPrivate MagickCLCacheInfo AcquireMagickCLCacheInfo(MagickCLDevice device,
529 Quantum *pixels,
const MagickSizeType length)
537 info=(MagickCLCacheInfo) AcquireCriticalMemory(
sizeof(*info));
538 (void) memset(info,0,
sizeof(*info));
539 LockSemaphoreInfo(openCL_lock);
541 UnlockSemaphoreInfo(openCL_lock);
545 info->events_semaphore=AcquireSemaphoreInfo();
546 info->buffer=openCL_library->clCreateBuffer(device->context,
547 CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,(
size_t) length,(
void *) pixels,
549 if (status == CL_SUCCESS)
551 DestroyMagickCLCacheInfo(info);
552 return((MagickCLCacheInfo) NULL);
574static MagickCLDevice AcquireMagickCLDevice()
579 device=(MagickCLDevice) AcquireMagickMemory(
sizeof(*device));
582 (void) memset(device,0,
sizeof(*device));
583 ActivateSemaphoreInfo(&device->lock);
584 device->score=MAGICKCORE_OPENCL_UNDEFINED_SCORE;
585 device->command_queues_index=-1;
586 device->enabled=MagickTrue;
606static MagickCLEnv AcquireMagickCLEnv(
void)
614 clEnv=(MagickCLEnv) AcquireMagickMemory(
sizeof(*clEnv));
615 if (clEnv != (MagickCLEnv) NULL)
617 (void) memset(clEnv,0,
sizeof(*clEnv));
618 ActivateSemaphoreInfo(&clEnv->lock);
619 clEnv->cpu_score=MAGICKCORE_OPENCL_UNDEFINED_SCORE;
620 clEnv->enabled=MagickFalse;
621 option=getenv(
"MAGICK_OCL_DEVICE");
622 if (option != (
const char *) NULL)
624 if ((IsStringTrue(option) != MagickFalse) ||
625 (strcmp(option,
"GPU") == 0) ||
626 (strcmp(option,
"CPU") == 0))
627 clEnv->enabled=MagickTrue;
656MagickPrivate cl_command_queue AcquireOpenCLCommandQueue(MagickCLDevice device)
661 cl_command_queue_properties
664 assert(device != (MagickCLDevice) NULL);
665 LockSemaphoreInfo(device->lock);
666 if ((device->profile_kernels == MagickFalse) &&
667 (device->command_queues_index >= 0))
669 queue=device->command_queues[device->command_queues_index--];
670 UnlockSemaphoreInfo(device->lock);
674 UnlockSemaphoreInfo(device->lock);
676 if (device->profile_kernels != MagickFalse)
677 properties=CL_QUEUE_PROFILING_ENABLE;
678 queue=openCL_library->clCreateCommandQueue(device->context,
679 device->deviceID,properties,(cl_int *) NULL);
712MagickPrivate cl_kernel AcquireOpenCLKernel(MagickCLDevice device,
713 const char *kernel_name)
718 assert(device != (MagickCLDevice) NULL);
719 (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),
"Using kernel: %s",
721 kernel=openCL_library->clCreateKernel(device->program,kernel_name,
752#if !MAGICKCORE_ZERO_CONFIGURATION_SUPPORT
753static void LoadOpenCLDeviceBenchmark(MagickCLEnv clEnv,
const char *xml)
756 keyword[MagickPathExtent],
762 MagickCLDeviceBenchmark
769 if (xml == (
char *) NULL)
771 device_benchmark=(MagickCLDeviceBenchmark *) NULL;
772 token=AcquireString(xml);
773 extent=strlen(token)+MagickPathExtent;
774 for (q=(
char *) xml; *q !=
'\0'; )
779 (void) GetNextToken(q,&q,extent,token);
782 (void) CopyMagickString(keyword,token,MagickPathExtent);
783 if (LocaleNCompare(keyword,
"<!DOCTYPE",9) == 0)
788 while ((LocaleNCompare(q,
"]>",2) != 0) && (*q !=
'\0'))
789 (void) GetNextToken(q,&q,extent,token);
792 if (LocaleNCompare(keyword,
"<!--",4) == 0)
797 while ((LocaleNCompare(q,
"->",2) != 0) && (*q !=
'\0'))
798 (void) GetNextToken(q,&q,extent,token);
801 if (LocaleCompare(keyword,
"<device") == 0)
806 device_benchmark=(MagickCLDeviceBenchmark *) AcquireQuantumMemory(1,
807 sizeof(*device_benchmark));
808 if (device_benchmark == (MagickCLDeviceBenchmark *) NULL)
810 (void) memset(device_benchmark,0,
sizeof(*device_benchmark));
811 device_benchmark->score=MAGICKCORE_OPENCL_UNDEFINED_SCORE;
814 if (device_benchmark == (MagickCLDeviceBenchmark *) NULL)
816 if (LocaleCompare(keyword,
"/>") == 0)
818 if (device_benchmark->score != MAGICKCORE_OPENCL_UNDEFINED_SCORE)
820 if (LocaleCompare(device_benchmark->name,
"CPU") == 0)
821 clEnv->cpu_score=device_benchmark->score;
830 for (i = 0; i < clEnv->number_devices; i++)
832 device=clEnv->devices[i];
833 if (IsBenchmarkedOpenCLDevice(device,device_benchmark))
834 device->score=device_benchmark->score;
839 device_benchmark->platform_name=(
char *) RelinquishMagickMemory(
840 device_benchmark->platform_name);
841 device_benchmark->vendor_name=(
char *) RelinquishMagickMemory(
842 device_benchmark->vendor_name);
843 device_benchmark->name=(
char *) RelinquishMagickMemory(
844 device_benchmark->name);
845 device_benchmark->version=(
char *) RelinquishMagickMemory(
846 device_benchmark->version);
847 device_benchmark=(MagickCLDeviceBenchmark *) RelinquishMagickMemory(
851 (void) GetNextToken(q,(
const char **) NULL,extent,token);
854 (void) GetNextToken(q,&q,extent,token);
855 (void) GetNextToken(q,&q,extent,token);
861 if (LocaleCompare((
char *) keyword,
"maxClockFrequency") == 0)
863 device_benchmark->max_clock_frequency=StringToInteger(token);
866 if (LocaleCompare((
char *) keyword,
"maxComputeUnits") == 0)
868 device_benchmark->max_compute_units=StringToInteger(token);
876 if (LocaleCompare((
char *) keyword,
"name") == 0)
877 device_benchmark->name=ConstantString(token);
883 if (LocaleCompare((
char *) keyword,
"platform") == 0)
884 device_benchmark->platform_name=ConstantString(token);
890 if (LocaleCompare((
char *) keyword,
"score") == 0)
891 device_benchmark->score=StringToDouble(token,(
char **) NULL);
897 if (LocaleCompare((
char *) keyword,
"vendor") == 0)
898 device_benchmark->vendor_name=ConstantString(token);
899 if (LocaleCompare((
char *) keyword,
"version") == 0)
900 device_benchmark->version=ConstantString(token);
907 token=(
char *) RelinquishMagickMemory(token);
908 device_benchmark=(MagickCLDeviceBenchmark *) RelinquishMagickMemory(
912static MagickBooleanType CanWriteProfileToFile(
const char *filename)
917 profileFile=fopen(filename,
"ab");
919 if (profileFile == (FILE *) NULL)
921 (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),
922 "Unable to save profile to: \"%s\"",filename);
931static MagickBooleanType LoadOpenCLBenchmarks(MagickCLEnv clEnv)
933#if !MAGICKCORE_ZERO_CONFIGURATION_SUPPORT
935 filename[MagickPathExtent];
940 (void) FormatLocaleString(filename,MagickPathExtent,
"%s%s%s",
941 GetOpenCLCacheDirectory(),DirectorySeparator,IMAGEMAGICK_PROFILE_FILE);
947 if (CanWriteProfileToFile(filename) == MagickFalse)
953 for (i = 0; i < clEnv->number_devices; i++)
954 clEnv->devices[i]->score=1.0;
956 SelectOpenCLDevice(clEnv,CL_DEVICE_TYPE_GPU);
959#if !MAGICKCORE_ZERO_CONFIGURATION_SUPPORT
960 option=ConfigureFileToStringInfo(filename);
961 LoadOpenCLDeviceBenchmark(clEnv,(
const char *) GetStringInfoDatum(option));
962 option=DestroyStringInfo(option);
967static void AutoSelectOpenCLDevices(MagickCLEnv clEnv)
981 option=getenv(
"MAGICK_OCL_DEVICE");
982 if (option != (
const char *) NULL)
984 if (strcmp(option,
"GPU") == 0)
985 SelectOpenCLDevice(clEnv,CL_DEVICE_TYPE_GPU);
986 else if (strcmp(option,
"CPU") == 0)
987 SelectOpenCLDevice(clEnv,CL_DEVICE_TYPE_CPU);
990 if (LoadOpenCLBenchmarks(clEnv) == MagickFalse)
993 benchmark=MagickFalse;
994 if (clEnv->cpu_score == MAGICKCORE_OPENCL_UNDEFINED_SCORE)
995 benchmark=MagickTrue;
998 for (i = 0; i < clEnv->number_devices; i++)
1000 if (clEnv->devices[i]->score == MAGICKCORE_OPENCL_UNDEFINED_SCORE)
1002 benchmark=MagickTrue;
1008 if (benchmark != MagickFalse)
1009 BenchmarkOpenCLDevices(clEnv);
1011 best_score=clEnv->cpu_score;
1012 for (i = 0; i < clEnv->number_devices; i++)
1013 best_score=MagickMin(clEnv->devices[i]->score,best_score);
1015 for (i = 0; i < clEnv->number_devices; i++)
1017 if (clEnv->devices[i]->score != best_score)
1018 clEnv->devices[i]->enabled=MagickFalse;
1047static double RunOpenCLBenchmark(MagickBooleanType is_cpu)
1064 exception=AcquireExceptionInfo();
1065 imageInfo=AcquireImageInfo();
1066 CloneString(&imageInfo->size,
"2048x1536");
1067 CopyMagickString(imageInfo->filename,
"xc:none",MagickPathExtent);
1068 inputImage=ReadImage(imageInfo,exception);
1069 if (inputImage == (
Image *) NULL)
1072 InitAccelerateTimer(&timer);
1074 for (i=0; i<=2; i++)
1082 StartAccelerateTimer(&timer);
1084 blurredImage=BlurImage(inputImage,10.0f,3.5f,exception);
1085 unsharpedImage=UnsharpMaskImage(blurredImage,2.0f,2.0f,50.0f,10.0f,
1087 resizedImage=ResizeImage(unsharpedImage,640,480,LanczosFilter,
1094 if (is_cpu == MagickFalse)
1099 cache_info=(
CacheInfo *) resizedImage->cache;
1100 if (cache_info->opencl != (MagickCLCacheInfo) NULL)
1101 openCL_library->clWaitForEvents(cache_info->opencl->event_count,
1102 cache_info->opencl->events);
1106 StopAccelerateTimer(&timer);
1108 if (blurredImage != (
Image *) NULL)
1109 DestroyImage(blurredImage);
1110 if (unsharpedImage != (
Image *) NULL)
1111 DestroyImage(unsharpedImage);
1112 if (resizedImage != (
Image *) NULL)
1113 DestroyImage(resizedImage);
1115 DestroyImage(inputImage);
1116 return(ReadAccelerateTimer(&timer));
1119static void RunDeviceBenchmark(MagickCLEnv clEnv,MagickCLEnv testEnv,
1120 MagickCLDevice device)
1122 testEnv->devices[0]=device;
1123 default_CLEnv=testEnv;
1124 device->score=RunOpenCLBenchmark(MagickFalse);
1125 default_CLEnv=clEnv;
1126 testEnv->devices[0]=(MagickCLDevice) NULL;
1129static void CacheOpenCLBenchmarks(MagickCLEnv clEnv)
1132 filename[MagickPathExtent];
1144 (void) FormatLocaleString(filename,MagickPathExtent,
"%s%s%s",
1145 GetOpenCLCacheDirectory(),DirectorySeparator,
1146 IMAGEMAGICK_PROFILE_FILE);
1148 cache_file=fopen_utf8(filename,
"wb");
1149 if (cache_file == (FILE *) NULL)
1151 fwrite(
"<devices>\n",
sizeof(
char),10,cache_file);
1152 fprintf(cache_file,
" <device name=\"CPU\" score=\"%.4g\"/>\n",
1154 for (i = 0; i < clEnv->number_devices; i++)
1159 device=clEnv->devices[i];
1160 duplicate=MagickFalse;
1161 for (j = 0; j < i; j++)
1163 if (IsSameOpenCLDevice(clEnv->devices[j],device))
1165 duplicate=MagickTrue;
1173 if (device->score != MAGICKCORE_OPENCL_UNDEFINED_SCORE)
1174 fprintf(cache_file,
" <device platform=\"%s\" vendor=\"%s\" name=\"%s\"\
1175 version=\"%s\" maxClockFrequency=\"%d\" maxComputeUnits=\"%d\"\
1176 score=\"%.4g\"/>\n",
1177 device->platform_name,device->vendor_name,device->name,device->version,
1178 (
int)device->max_clock_frequency,(
int)device->max_compute_units,
1181 fwrite(
"</devices>",
sizeof(
char),10,cache_file);
1186static void BenchmarkOpenCLDevices(MagickCLEnv clEnv)
1198 (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),
1199 "Starting benchmark");
1200 testEnv=AcquireMagickCLEnv();
1201 testEnv->library=openCL_library;
1202 testEnv->devices=(MagickCLDevice *) AcquireCriticalMemory(
1203 sizeof(MagickCLDevice));
1204 testEnv->number_devices=1;
1205 testEnv->benchmark_thread_id=GetMagickThreadId();
1206 testEnv->initialized=MagickTrue;
1208 for (i = 0; i < clEnv->number_devices; i++)
1209 clEnv->devices[i]->score=MAGICKCORE_OPENCL_UNDEFINED_SCORE;
1211 for (i = 0; i < clEnv->number_devices; i++)
1213 device=clEnv->devices[i];
1214 if (device->score == MAGICKCORE_OPENCL_UNDEFINED_SCORE)
1215 RunDeviceBenchmark(clEnv,testEnv,device);
1218 for (j = i+1; j < clEnv->number_devices; j++)
1223 other_device=clEnv->devices[j];
1224 if (IsSameOpenCLDevice(device,other_device))
1225 other_device->score=device->score;
1229 testEnv->enabled=MagickFalse;
1230 default_CLEnv=testEnv;
1231 clEnv->cpu_score=RunOpenCLBenchmark(MagickTrue);
1232 default_CLEnv=clEnv;
1234 testEnv=RelinquishMagickCLEnv(testEnv);
1235 CacheOpenCLBenchmarks(clEnv);
1272static void CacheOpenCLKernel(MagickCLDevice device,
char *filename,
1284 status=openCL_library->clGetProgramInfo(device->program,
1285 CL_PROGRAM_BINARY_SIZES,
sizeof(
size_t),&binaryProgramSize,NULL);
1286 if (status != CL_SUCCESS)
1288 binaryProgram=(
unsigned char*) AcquireQuantumMemory(1,binaryProgramSize);
1289 if (binaryProgram == (
unsigned char *) NULL)
1291 (void) ThrowMagickException(exception,GetMagickModule(),
1292 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",filename);
1295 status=openCL_library->clGetProgramInfo(device->program,
1296 CL_PROGRAM_BINARIES,
sizeof(
unsigned char*),&binaryProgram,NULL);
1297 if (status == CL_SUCCESS)
1299 (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),
1300 "Creating cache file: \"%s\"",filename);
1301 (void) BlobToFile(filename,binaryProgram,binaryProgramSize,exception);
1303 binaryProgram=(
unsigned char *) RelinquishMagickMemory(binaryProgram);
1306static MagickBooleanType LoadCachedOpenCLKernels(MagickCLDevice device,
1307 const char *filename)
1322 sans_exception=AcquireExceptionInfo();
1323 binaryProgram=(
unsigned char *) FileToBlob(filename,SIZE_MAX,&length,
1325 sans_exception=DestroyExceptionInfo(sans_exception);
1326 if (binaryProgram == (
unsigned char *) NULL)
1327 return(MagickFalse);
1328 (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),
1329 "Loaded cached kernels: \"%s\"",filename);
1330 device->program=openCL_library->clCreateProgramWithBinary(device->context,1,
1331 &device->deviceID,&length,(
const unsigned char**)&binaryProgram,
1332 &binaryStatus,&status);
1333 binaryProgram=(
unsigned char *) RelinquishMagickMemory(binaryProgram);
1334 return((status != CL_SUCCESS) || (binaryStatus != CL_SUCCESS) ? MagickFalse :
1338static void LogOpenCLBuildFailure(MagickCLDevice device,
const char *kernel,
1342 filename[MagickPathExtent],
1348 (void) FormatLocaleString(filename,MagickPathExtent,
"%s%s%s",
1349 GetOpenCLCacheDirectory(),DirectorySeparator,
"magick_badcl.cl");
1351 (void) remove_utf8(filename);
1352 (void) BlobToFile(filename,kernel,strlen(kernel),exception);
1354 openCL_library->clGetProgramBuildInfo(device->program,device->deviceID,
1355 CL_PROGRAM_BUILD_LOG,0,NULL,&log_size);
1356 log=(
char*)AcquireCriticalMemory(log_size);
1357 openCL_library->clGetProgramBuildInfo(device->program,device->deviceID,
1358 CL_PROGRAM_BUILD_LOG,log_size,log,&log_size);
1360 (void) FormatLocaleString(filename,MagickPathExtent,
"%s%s%s",
1361 GetOpenCLCacheDirectory(),DirectorySeparator,
"magick_badcl.log");
1363 (void) remove_utf8(filename);
1364 (void) BlobToFile(filename,log,log_size,exception);
1365 log=(
char*)RelinquishMagickMemory(log);
1368static MagickBooleanType CompileOpenCLKernel(MagickCLDevice device,
1369 const char *kernel,
const char *options,
size_t signature,
1373 deviceName[MagickPathExtent],
1374 filename[MagickPathExtent],
1386 (void) CopyMagickString(deviceName,device->name,MagickPathExtent);
1389 while (*ptr !=
'\0')
1391 if ((*ptr ==
' ') || (*ptr ==
'\\') || (*ptr ==
'/') || (*ptr ==
':') ||
1392 (*ptr ==
'*') || (*ptr ==
'?') || (*ptr ==
'"') || (*ptr ==
'<') ||
1393 (*ptr ==
'>' || *ptr ==
'|'))
1397 (void) FormatLocaleString(filename,MagickPathExtent,
1398 "%s%s%s_%s_%08x_%.20g.bin",GetOpenCLCacheDirectory(),
1399 DirectorySeparator,
"magick_opencl",deviceName,(
unsigned int) signature,
1400 (
double)
sizeof(
char*)*8);
1401 loaded=LoadCachedOpenCLKernels(device,filename);
1402 if (loaded == MagickFalse)
1405 length=strlen(kernel);
1406 device->program=openCL_library->clCreateProgramWithSource(
1407 device->context,1,&kernel,&length,&status);
1408 if (status != CL_SUCCESS)
1409 return(MagickFalse);
1412 status=openCL_library->clBuildProgram(device->program,1,&device->deviceID,
1414 if (status != CL_SUCCESS)
1416 (void) ThrowMagickException(exception,GetMagickModule(),DelegateWarning,
1417 "clBuildProgram failed.",
"(%d)",(
int)status);
1418 LogOpenCLBuildFailure(device,kernel,exception);
1419 return(MagickFalse);
1423 if (loaded == MagickFalse)
1424 CacheOpenCLKernel(device,filename,exception);
1429static cl_event* CopyOpenCLEvents(MagickCLCacheInfo first,
1430 MagickCLCacheInfo second,cl_uint *event_count)
1441 assert(first != (MagickCLCacheInfo) NULL);
1442 assert(event_count != (cl_uint *) NULL);
1443 events=(cl_event *) NULL;
1444 LockSemaphoreInfo(first->events_semaphore);
1445 if (second != (MagickCLCacheInfo) NULL)
1446 LockSemaphoreInfo(second->events_semaphore);
1447 *event_count=first->event_count;
1448 if (second != (MagickCLCacheInfo) NULL)
1449 *event_count+=second->event_count;
1450 if (*event_count > 0)
1452 events=(cl_event *) AcquireQuantumMemory(*event_count,
sizeof(*events));
1453 if (events == (cl_event *) NULL)
1458 for (i=0; i < first->event_count; i++, j++)
1459 events[j]=first->events[i];
1460 if (second != (MagickCLCacheInfo) NULL)
1462 for (i=0; i < second->event_count; i++, j++)
1463 events[j]=second->events[i];
1467 UnlockSemaphoreInfo(first->events_semaphore);
1468 if (second != (MagickCLCacheInfo) NULL)
1469 UnlockSemaphoreInfo(second->events_semaphore);
1495MagickPrivate MagickCLCacheInfo CopyMagickCLCacheInfo(MagickCLCacheInfo info)
1509 if (info == (MagickCLCacheInfo) NULL)
1510 return((MagickCLCacheInfo) NULL);
1511 events=CopyOpenCLEvents(info,(MagickCLCacheInfo) NULL,&event_count);
1512 if (events != (cl_event *) NULL)
1514 queue=AcquireOpenCLCommandQueue(info->device);
1515 pixels=(Quantum *) openCL_library->clEnqueueMapBuffer(queue,info->buffer,
1516 CL_TRUE,CL_MAP_READ | CL_MAP_WRITE,0,info->length,event_count,events,
1517 (cl_event *) NULL,(cl_int *) NULL);
1518 assert(pixels == info->pixels);
1519 ReleaseOpenCLCommandQueue(info->device,queue);
1520 events=(cl_event *) RelinquishMagickMemory(events);
1522 return(RelinquishMagickCLCacheInfo(info,MagickFalse));
1544MagickPrivate
void DumpOpenCLProfileData()
1546#define OpenCLLog(message) \
1547 fwrite(message,sizeof(char),strlen(message),log); \
1548 fwrite("\n",sizeof(char),1,log);
1552 filename[MagickPathExtent],
1562 if (default_CLEnv == (MagickCLEnv) NULL)
1565 for (i = 0; i < default_CLEnv->number_devices; i++)
1566 if (default_CLEnv->devices[i]->profile_kernels != MagickFalse)
1568 if (i == default_CLEnv->number_devices)
1571 (void) FormatLocaleString(filename,MagickPathExtent,
"%s%s%s",
1572 GetOpenCLCacheDirectory(),DirectorySeparator,
"ImageMagickOpenCL.log");
1574 log=fopen_utf8(filename,
"wb");
1575 if (log == (FILE *) NULL)
1577 for (i = 0; i < default_CLEnv->number_devices; i++)
1582 device=default_CLEnv->devices[i];
1583 if ((device->profile_kernels == MagickFalse) ||
1587 OpenCLLog(
"====================================================");
1588 fprintf(log,
"Device: %s\n",device->name);
1589 fprintf(log,
"Version: %s\n",device->version);
1590 OpenCLLog(
"====================================================");
1591 OpenCLLog(
" average calls min max");
1592 OpenCLLog(
" ------- ----- --- ---");
1599 profile=device->profile_records[j];
1600 (void) CopyMagickString(indent,
" ",
1602 CopyMagickString(indent,profile->kernel_name,MagickMin(strlen(
1603 profile->kernel_name),strlen(indent)));
1604 (void) FormatLocaleString(buf,
sizeof(buf),
"%s %7d %7d %7d %7d",indent,
1605 (
int) (profile->total/profile->count),(
int) profile->count,
1606 (
int) profile->min,(
int) profile->max);
1610 OpenCLLog(
"====================================================");
1611 fwrite(
"\n\n",
sizeof(
char),2,log);
1663static MagickBooleanType RegisterCacheEvent(MagickCLCacheInfo info,
1666 assert(info != (MagickCLCacheInfo) NULL);
1667 assert(event != (cl_event) NULL);
1668 if (openCL_library->clRetainEvent(event) != CL_SUCCESS)
1670 openCL_library->clWaitForEvents(1,&event);
1671 return(MagickFalse);
1673 LockSemaphoreInfo(info->events_semaphore);
1674 if (info->events == (cl_event *) NULL)
1676 info->events=(cl_event *) AcquireMagickMemory(
sizeof(*info->events));
1677 info->event_count=1;
1680 info->events=(cl_event *) ResizeQuantumMemory(info->events,
1681 ++info->event_count,
sizeof(*info->events));
1682 if (info->events == (cl_event *) NULL)
1683 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1684 info->events[info->event_count-1]=event;
1685 UnlockSemaphoreInfo(info->events_semaphore);
1689MagickPrivate MagickBooleanType EnqueueOpenCLKernel(cl_command_queue queue,
1690 cl_kernel kernel,cl_uint work_dim,
const size_t *offset,
const size_t *gsize,
1691 const size_t *lsize,
const Image *input_image,
const Image *output_image,
1708 assert(input_image != (
const Image *) NULL);
1709 input_info=(
CacheInfo *) input_image->cache;
1710 assert(input_info != (
CacheInfo *) NULL);
1711 assert(input_info->opencl != (MagickCLCacheInfo) NULL);
1713 if (output_image == (
const Image *) NULL)
1714 events=CopyOpenCLEvents(input_info->opencl,(MagickCLCacheInfo) NULL,
1718 output_info=(
CacheInfo *) output_image->cache;
1719 assert(output_info != (
CacheInfo *) NULL);
1720 assert(output_info->opencl != (MagickCLCacheInfo) NULL);
1721 events=CopyOpenCLEvents(input_info->opencl,output_info->opencl,
1724 status=openCL_library->clEnqueueNDRangeKernel(queue,kernel,work_dim,offset,
1725 gsize,lsize,event_count,events,&event);
1727 if ((status != CL_SUCCESS) && (event_count > 0))
1729 openCL_library->clFinish(queue);
1730 status=openCL_library->clEnqueueNDRangeKernel(queue,kernel,work_dim,
1731 offset,gsize,lsize,event_count,events,&event);
1733 events=(cl_event *) RelinquishMagickMemory(events);
1734 if (status != CL_SUCCESS)
1736 (void) OpenCLThrowMagickException(input_info->opencl->device,exception,
1737 GetMagickModule(),ResourceLimitWarning,
1738 "clEnqueueNDRangeKernel failed.",
"'%s'",
".");
1739 return(MagickFalse);
1741 if (flush != MagickFalse)
1742 openCL_library->clFlush(queue);
1743 if (RecordProfileData(input_info->opencl->device,kernel,event) == MagickFalse)
1745 if (RegisterCacheEvent(input_info->opencl,event) != MagickFalse)
1748 (void) RegisterCacheEvent(output_info->opencl,event);
1751 openCL_library->clReleaseEvent(event);
1774MagickPrivate MagickCLEnv GetCurrentOpenCLEnv(
void)
1776 if (default_CLEnv != (MagickCLEnv) NULL)
1778 if ((default_CLEnv->benchmark_thread_id != (MagickThreadType) 0) &&
1779 (default_CLEnv->benchmark_thread_id != GetMagickThreadId()))
1780 return((MagickCLEnv) NULL);
1782 return(default_CLEnv);
1785 if (GetOpenCLCacheDirectory() == (
char *) NULL)
1786 return((MagickCLEnv) NULL);
1789 ActivateSemaphoreInfo(&openCL_lock);
1791 LockSemaphoreInfo(openCL_lock);
1792 if (default_CLEnv == (MagickCLEnv) NULL)
1793 default_CLEnv=AcquireMagickCLEnv();
1794 UnlockSemaphoreInfo(openCL_lock);
1796 return(default_CLEnv);
1823MagickExport
double GetOpenCLDeviceBenchmarkScore(
1824 const MagickCLDevice device)
1826 if (device == (MagickCLDevice) NULL)
1827 return(MAGICKCORE_OPENCL_UNDEFINED_SCORE);
1828 return(device->score);
1853MagickExport MagickBooleanType GetOpenCLDeviceEnabled(
1854 const MagickCLDevice device)
1856 if (device == (MagickCLDevice) NULL)
1857 return(MagickFalse);
1858 return(device->enabled);
1883MagickExport
const char *GetOpenCLDeviceName(
const MagickCLDevice device)
1885 if (device == (MagickCLDevice) NULL)
1886 return((
const char *) NULL);
1887 return(device->name);
1912MagickExport
const char *GetOpenCLDeviceVendorName(
const MagickCLDevice device)
1914 if (device == (MagickCLDevice) NULL)
1915 return((
const char *) NULL);
1916 return(device->vendor_name);
1946MagickExport MagickCLDevice *GetOpenCLDevices(
size_t *length,
1952 clEnv=GetCurrentOpenCLEnv();
1953 if (clEnv == (MagickCLEnv) NULL)
1955 if (length != (
size_t *) NULL)
1957 return((MagickCLDevice *) NULL);
1959 InitializeOpenCL(clEnv,exception);
1960 if (length != (
size_t *) NULL)
1961 *length=clEnv->number_devices;
1962 return(clEnv->devices);
1987MagickExport MagickCLDeviceType GetOpenCLDeviceType(
1988 const MagickCLDevice device)
1990 if (device == (MagickCLDevice) NULL)
1991 return(UndefinedCLDeviceType);
1992 if (device->type == CL_DEVICE_TYPE_GPU)
1993 return(GpuCLDeviceType);
1994 if (device->type == CL_DEVICE_TYPE_CPU)
1995 return(CpuCLDeviceType);
1996 return(UndefinedCLDeviceType);
2021MagickExport
const char *GetOpenCLDeviceVersion(
const MagickCLDevice device)
2023 if (device == (MagickCLDevice) NULL)
2024 return((
const char *) NULL);
2025 return(device->version);
2047MagickExport MagickBooleanType GetOpenCLEnabled(
void)
2052 clEnv=GetCurrentOpenCLEnv();
2053 if (clEnv == (MagickCLEnv) NULL)
2054 return(MagickFalse);
2055 return(clEnv->enabled);
2082 const MagickCLDevice device,
size_t *length)
2084 if ((device == (
const MagickCLDevice) NULL) || (device->profile_records ==
2087 if (length != (
size_t *) NULL)
2091 if (length != (
size_t *) NULL)
2094 LockSemaphoreInfo(device->lock);
2097 UnlockSemaphoreInfo(device->lock);
2099 return(device->profile_records);
2130static MagickBooleanType HasOpenCLDevices(MagickCLEnv clEnv,
2134 *accelerateKernelsBuffer,
2135 options[MagickPathExtent];
2147 for (i = 0; i < clEnv->number_devices; i++)
2149 if ((clEnv->devices[i]->enabled != MagickFalse))
2152 if (i == clEnv->number_devices)
2153 return(MagickFalse);
2157 for (i = 0; i < clEnv->number_devices; i++)
2159 if ((clEnv->devices[i]->enabled != MagickFalse) &&
2160 (clEnv->devices[i]->program == (cl_program) NULL))
2166 if (status != MagickFalse)
2170 (void) FormatLocaleString(options,MagickPathExtent,CLOptions,
2171 (
float)QuantumRange,(
float)QuantumScale,(
float)CLCharQuantumScale,
2172 (
float)MagickEpsilon,(
float)MagickPI,(
unsigned int)MaxMap,
2173 (
unsigned int)MAGICKCORE_QUANTUM_DEPTH);
2175 signature=StringSignature(options);
2176 accelerateKernelsBuffer=(
char*) AcquireQuantumMemory(1,
2177 strlen(accelerateKernels)+strlen(accelerateKernels2)+1);
2178 if (accelerateKernelsBuffer == (
char*) NULL)
2179 return(MagickFalse);
2180 (void) FormatLocaleString(accelerateKernelsBuffer,strlen(accelerateKernels)+
2181 strlen(accelerateKernels2)+1,
"%s%s",accelerateKernels,accelerateKernels2);
2182 signature^=StringSignature(accelerateKernelsBuffer);
2185 for (i = 0; i < clEnv->number_devices; i++)
2193 device=clEnv->devices[i];
2194 if ((device->enabled == MagickFalse) ||
2195 (device->program != (cl_program) NULL))
2198 LockSemaphoreInfo(device->lock);
2199 if (device->program != (cl_program) NULL)
2201 UnlockSemaphoreInfo(device->lock);
2204 device_signature=signature;
2205 device_signature^=StringSignature(device->platform_name);
2206 status=CompileOpenCLKernel(device,accelerateKernelsBuffer,options,
2207 device_signature,exception);
2208 UnlockSemaphoreInfo(device->lock);
2209 if (status == MagickFalse)
2212 accelerateKernelsBuffer=(
char *) RelinquishMagickMemory(
2213 accelerateKernelsBuffer);
2241static cl_uint GetOpenCLDeviceCount(MagickCLEnv clEnv,cl_platform_id platform)
2244 version[MagickPathExtent];
2249 if (clEnv->library->clGetPlatformInfo(platform,CL_PLATFORM_VERSION,
2250 MagickPathExtent,version,NULL) != CL_SUCCESS)
2252 if (strncmp(version,
"OpenCL 1.0 ",11) == 0)
2254 if (clEnv->library->clGetDeviceIDs(platform,
2255 CL_DEVICE_TYPE_CPU|CL_DEVICE_TYPE_GPU,0,NULL,&num) != CL_SUCCESS)
2260static inline char *GetOpenCLPlatformString(cl_platform_id platform,
2261 cl_platform_info param_name)
2269 openCL_library->clGetPlatformInfo(platform,param_name,0,NULL,&length);
2270 value=(
char *) AcquireCriticalMemory(length*
sizeof(*value));
2271 openCL_library->clGetPlatformInfo(platform,param_name,length,value,NULL);
2275static inline char *GetOpenCLDeviceString(cl_device_id device,
2276 cl_device_info param_name)
2284 openCL_library->clGetDeviceInfo(device,param_name,0,NULL,&length);
2285 value=(
char *) AcquireCriticalMemory(length*
sizeof(*value));
2286 openCL_library->clGetDeviceInfo(device,param_name,length,value,NULL);
2290static void LoadOpenCLDevices(MagickCLEnv clEnv)
2292 cl_context_properties
2312 if (openCL_library->clGetPlatformIDs(0,NULL,&number_platforms) != CL_SUCCESS)
2314 if (number_platforms == 0)
2316 platforms=(cl_platform_id *) AcquireQuantumMemory(1,number_platforms*
2317 sizeof(cl_platform_id));
2318 if (platforms == (cl_platform_id *) NULL)
2320 if (openCL_library->clGetPlatformIDs(number_platforms,platforms,NULL) != CL_SUCCESS)
2322 platforms=(cl_platform_id *) RelinquishMagickMemory(platforms);
2325 for (i = 0; i < number_platforms; i++)
2327 number_devices=GetOpenCLDeviceCount(clEnv,platforms[i]);
2328 if (number_devices == 0)
2329 platforms[i]=(cl_platform_id) NULL;
2331 clEnv->number_devices+=number_devices;
2333 if (clEnv->number_devices == 0)
2335 platforms=(cl_platform_id *) RelinquishMagickMemory(platforms);
2338 clEnv->devices=(MagickCLDevice *) AcquireQuantumMemory(clEnv->number_devices,
2339 sizeof(MagickCLDevice));
2340 if (clEnv->devices == (MagickCLDevice *) NULL)
2342 RelinquishMagickCLDevices(clEnv);
2343 platforms=(cl_platform_id *) RelinquishMagickMemory(platforms);
2346 (void) memset(clEnv->devices,0,clEnv->number_devices*
sizeof(MagickCLDevice));
2347 devices=(cl_device_id *) AcquireQuantumMemory(clEnv->number_devices,
2348 sizeof(cl_device_id));
2349 if (devices == (cl_device_id *) NULL)
2351 platforms=(cl_platform_id *) RelinquishMagickMemory(platforms);
2352 RelinquishMagickCLDevices(clEnv);
2355 (void) memset(devices,0,clEnv->number_devices*
sizeof(cl_device_id));
2356 clEnv->number_contexts=(size_t) number_platforms;
2357 clEnv->contexts=(cl_context *) AcquireQuantumMemory(clEnv->number_contexts,
2358 sizeof(cl_context));
2359 if (clEnv->contexts == (cl_context *) NULL)
2361 devices=(cl_device_id *) RelinquishMagickMemory(devices);
2362 platforms=(cl_platform_id *) RelinquishMagickMemory(platforms);
2363 RelinquishMagickCLDevices(clEnv);
2366 (void) memset(clEnv->contexts,0,clEnv->number_contexts*
sizeof(cl_context));
2368 for (i = 0; i < number_platforms; i++)
2370 if (platforms[i] == (cl_platform_id) NULL)
2373 status=clEnv->library->clGetDeviceIDs(platforms[i],CL_DEVICE_TYPE_CPU |
2374 CL_DEVICE_TYPE_GPU,(cl_uint) clEnv->number_devices,devices,&number_devices);
2375 if (status != CL_SUCCESS)
2378 properties[0]=CL_CONTEXT_PLATFORM;
2379 properties[1]=(cl_context_properties) platforms[i];
2381 clEnv->contexts[i]=openCL_library->clCreateContext(properties,number_devices,
2382 devices,NULL,NULL,&status);
2383 if (status != CL_SUCCESS)
2386 for (j = 0; j < number_devices; j++,next++)
2391 device=AcquireMagickCLDevice();
2392 if (device == (MagickCLDevice) NULL)
2395 device->context=clEnv->contexts[i];
2396 device->deviceID=devices[j];
2398 device->platform_name=GetOpenCLPlatformString(platforms[i],
2401 device->vendor_name=GetOpenCLPlatformString(platforms[i],
2402 CL_PLATFORM_VENDOR);
2404 device->name=GetOpenCLDeviceString(devices[j],CL_DEVICE_NAME);
2406 device->version=GetOpenCLDeviceString(devices[j],CL_DRIVER_VERSION);
2408 openCL_library->clGetDeviceInfo(devices[j],CL_DEVICE_MAX_CLOCK_FREQUENCY,
2409 sizeof(cl_uint),&device->max_clock_frequency,NULL);
2411 openCL_library->clGetDeviceInfo(devices[j],CL_DEVICE_MAX_COMPUTE_UNITS,
2412 sizeof(cl_uint),&device->max_compute_units,NULL);
2414 openCL_library->clGetDeviceInfo(devices[j],CL_DEVICE_TYPE,
2415 sizeof(cl_device_type),&device->type,NULL);
2417 openCL_library->clGetDeviceInfo(devices[j],CL_DEVICE_LOCAL_MEM_SIZE,
2418 sizeof(cl_ulong),&device->local_memory_size,NULL);
2420 clEnv->devices[next]=device;
2421 (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),
2422 "Found device: %s (%s)",device->name,device->platform_name);
2425 if (next != clEnv->number_devices)
2426 RelinquishMagickCLDevices(clEnv);
2427 platforms=(cl_platform_id *) RelinquishMagickMemory(platforms);
2428 devices=(cl_device_id *) RelinquishMagickMemory(devices);
2431MagickPrivate MagickBooleanType InitializeOpenCL(MagickCLEnv clEnv,
2434 LockSemaphoreInfo(clEnv->lock);
2435 if (clEnv->initialized != MagickFalse)
2437 UnlockSemaphoreInfo(clEnv->lock);
2438 return(HasOpenCLDevices(clEnv,exception));
2440 if (LoadOpenCLLibrary() != MagickFalse)
2442 clEnv->library=openCL_library;
2443 LoadOpenCLDevices(clEnv);
2444 if (clEnv->number_devices > 0)
2445 AutoSelectOpenCLDevices(clEnv);
2447 clEnv->initialized=MagickTrue;
2448 UnlockSemaphoreInfo(clEnv->lock);
2449 return(HasOpenCLDevices(clEnv,exception));
2471void *OsLibraryGetFunctionAddress(
void *library,
const char *functionName)
2473 if ((library == (
void *) NULL) || (functionName == (
const char *) NULL))
2474 return (
void *) NULL;
2475 return lt_dlsym(library,functionName);
2478static MagickBooleanType BindOpenCLFunctions()
2480#ifdef MAGICKCORE_HAVE_OPENCL_CL_H
2481#define BIND(X) openCL_library->X= &X;
2483 (void) memset(openCL_library,0,
sizeof(MagickLibrary));
2484#ifdef MAGICKCORE_WINDOWS_SUPPORT
2485 openCL_library->library=(
void *)lt_dlopen(
"OpenCL.dll");
2487 openCL_library->library=(
void *)lt_dlopen(
"libOpenCL.so");
2490 if ((openCL_library->X=(MAGICKpfn_##X)OsLibraryGetFunctionAddress(openCL_library->library,#X)) == NULL) \
2491 return(MagickFalse);
2494 if (openCL_library->library == (
void*) NULL)
2495 return(MagickFalse);
2497 BIND(clGetPlatformIDs);
2498 BIND(clGetPlatformInfo);
2500 BIND(clGetDeviceIDs);
2501 BIND(clGetDeviceInfo);
2503 BIND(clCreateBuffer);
2504 BIND(clReleaseMemObject);
2505 BIND(clRetainMemObject);
2507 BIND(clCreateContext);
2508 BIND(clReleaseContext);
2510 BIND(clCreateCommandQueue);
2511 BIND(clReleaseCommandQueue);
2515 BIND(clCreateProgramWithSource);
2516 BIND(clCreateProgramWithBinary);
2517 BIND(clReleaseProgram);
2518 BIND(clBuildProgram);
2519 BIND(clGetProgramBuildInfo);
2520 BIND(clGetProgramInfo);
2522 BIND(clCreateKernel);
2523 BIND(clReleaseKernel);
2524 BIND(clSetKernelArg);
2525 BIND(clGetKernelInfo);
2527 BIND(clEnqueueReadBuffer);
2528 BIND(clEnqueueMapBuffer);
2529 BIND(clEnqueueUnmapMemObject);
2530 BIND(clEnqueueNDRangeKernel);
2532 BIND(clGetEventInfo);
2533 BIND(clWaitForEvents);
2534 BIND(clReleaseEvent);
2535 BIND(clRetainEvent);
2536 BIND(clSetEventCallback);
2538 BIND(clGetEventProfilingInfo);
2543static MagickBooleanType LoadOpenCLLibrary(
void)
2545 openCL_library=(MagickLibrary *) AcquireMagickMemory(
sizeof(MagickLibrary));
2546 if (openCL_library == (MagickLibrary *) NULL)
2547 return(MagickFalse);
2549 if (BindOpenCLFunctions() == MagickFalse)
2551 openCL_library=(MagickLibrary *)RelinquishMagickMemory(openCL_library);
2552 return(MagickFalse);
2577MagickPrivate
void OpenCLTerminus()
2579 DumpOpenCLProfileData();
2580 if (cache_directory != (
char *) NULL)
2581 cache_directory=DestroyString(cache_directory);
2583 RelinquishSemaphoreInfo(&cache_directory_lock);
2584 if (default_CLEnv != (MagickCLEnv) NULL)
2585 default_CLEnv=RelinquishMagickCLEnv(default_CLEnv);
2587 RelinquishSemaphoreInfo(&openCL_lock);
2588 if (openCL_library != (MagickLibrary *) NULL)
2590 if (openCL_library->library != (
void *) NULL)
2591 (void) lt_dlclose(openCL_library->library);
2592 openCL_library=(MagickLibrary *) RelinquishMagickMemory(openCL_library);
2635MagickPrivate MagickBooleanType OpenCLThrowMagickException(
2636 MagickCLDevice device,
ExceptionInfo *exception,
const char *module,
2637 const char *function,
const size_t line,
const ExceptionType severity,
2638 const char *tag,
const char *format,...)
2643 assert(device != (MagickCLDevice) NULL);
2645 assert(exception->signature == MagickCoreSignature);
2650 if (device->type == CL_DEVICE_TYPE_CPU)
2654 if (strncmp(device->platform_name,
"Intel",5) == 0)
2655 default_CLEnv->enabled=MagickFalse;
2659#ifdef OPENCLLOG_ENABLED
2663 va_start(operands,format);
2664 status=ThrowMagickExceptionList(exception,module,function,line,severity,tag,
2669 magick_unreferenced(module);
2670 magick_unreferenced(function);
2671 magick_unreferenced(line);
2672 magick_unreferenced(tag);
2673 magick_unreferenced(format);
2705MagickPrivate MagickBooleanType RecordProfileData(MagickCLDevice device,
2706 cl_kernel kernel,cl_event event)
2726 if (device->profile_kernels == MagickFalse)
2727 return(MagickFalse);
2728 status=openCL_library->clWaitForEvents(1,&event);
2729 if (status != CL_SUCCESS)
2730 return(MagickFalse);
2731 status=openCL_library->clGetKernelInfo(kernel,CL_KERNEL_FUNCTION_NAME,0,NULL,
2733 if (status != CL_SUCCESS)
2735 name=(
char *) AcquireQuantumMemory(length,
sizeof(*name));
2736 if (name == (
char *) NULL)
2738 start=end=elapsed=0;
2739 status=openCL_library->clGetKernelInfo(kernel,CL_KERNEL_FUNCTION_NAME,length,
2740 name,(
size_t *) NULL);
2741 status|=openCL_library->clGetEventProfilingInfo(event,
2742 CL_PROFILING_COMMAND_START,
sizeof(cl_ulong),&start,NULL);
2743 status|=openCL_library->clGetEventProfilingInfo(event,
2744 CL_PROFILING_COMMAND_END,
sizeof(cl_ulong),&end,NULL);
2745 if (status != CL_SUCCESS)
2747 name=DestroyString(name);
2753 LockSemaphoreInfo(device->lock);
2760 if (LocaleCompare(device->profile_records[i]->kernel_name,name) == 0)
2762 profile_record=device->profile_records[i];
2769 name=DestroyString(name);
2773 sizeof(*profile_record));
2774 (void) memset(profile_record,0,
sizeof(*profile_record));
2775 profile_record->kernel_name=name;
2777 device->profile_records,(i+2),
sizeof(*device->profile_records));
2779 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
2780 device->profile_records[i]=profile_record;
2783 if ((elapsed < profile_record->min) || (profile_record->count == 0))
2784 profile_record->min=elapsed;
2785 if (elapsed > profile_record->max)
2786 profile_record->max=elapsed;
2787 profile_record->total+=elapsed;
2788 profile_record->count+=1;
2789 UnlockSemaphoreInfo(device->lock);
2818MagickPrivate
void ReleaseOpenCLCommandQueue(MagickCLDevice device,
2819 cl_command_queue queue)
2821 if (queue == (cl_command_queue) NULL)
2824 assert(device != (MagickCLDevice) NULL);
2825 LockSemaphoreInfo(device->lock);
2826 if ((device->profile_kernels != MagickFalse) ||
2827 (device->command_queues_index >= MAGICKCORE_OPENCL_COMMAND_QUEUES-1))
2829 UnlockSemaphoreInfo(device->lock);
2830 openCL_library->clFinish(queue);
2831 (void) openCL_library->clReleaseCommandQueue(queue);
2835 openCL_library->clFlush(queue);
2836 device->command_queues[++device->command_queues_index]=queue;
2837 UnlockSemaphoreInfo(device->lock);
2864MagickPrivate
void ReleaseOpenCLDevice(MagickCLDevice device)
2866 assert(device != (MagickCLDevice) NULL);
2867 LockSemaphoreInfo(openCL_lock);
2868 device->requested--;
2869 UnlockSemaphoreInfo(openCL_lock);
2899static void CL_API_CALL DestroyMagickCLCacheInfoAndPixels(
2900 cl_event magick_unused(event),
2901 cl_int magick_unused(event_command_exec_status),
void *user_data)
2912 magick_unreferenced(event);
2913 magick_unreferenced(event_command_exec_status);
2914 info=(MagickCLCacheInfo) user_data;
2915 for (i=(ssize_t)info->event_count-1; i >= 0; i--)
2923 status=openCL_library->clGetEventInfo(info->events[i],
2924 CL_EVENT_COMMAND_EXECUTION_STATUS,
sizeof(event_status),&event_status,
2926 if ((status == CL_SUCCESS) && (event_status > CL_COMPLETE))
2928 openCL_library->clSetEventCallback(info->events[i],CL_COMPLETE,
2929 &DestroyMagickCLCacheInfoAndPixels,info);
2933 pixels=info->pixels;
2934 RelinquishMagickResource(MemoryResource,info->length);
2935 DestroyMagickCLCacheInfo(info);
2936 (void) RelinquishAlignedMemory(pixels);
2939MagickPrivate MagickCLCacheInfo RelinquishMagickCLCacheInfo(
2940 MagickCLCacheInfo info,
const MagickBooleanType relinquish_pixels)
2942 if (info == (MagickCLCacheInfo) NULL)
2943 return((MagickCLCacheInfo) NULL);
2944 if (relinquish_pixels != MagickFalse)
2945 DestroyMagickCLCacheInfoAndPixels((cl_event) NULL,0,info);
2947 DestroyMagickCLCacheInfo(info);
2948 return((MagickCLCacheInfo) NULL);
2974static MagickCLDevice RelinquishMagickCLDevice(MagickCLDevice device)
2976 if (device == (MagickCLDevice) NULL)
2977 return((MagickCLDevice) NULL);
2979 device->platform_name=(
char *) RelinquishMagickMemory(device->platform_name);
2980 device->vendor_name=(
char *) RelinquishMagickMemory(device->vendor_name);
2981 device->name=(
char *) RelinquishMagickMemory(device->name);
2982 device->version=(
char *) RelinquishMagickMemory(device->version);
2983 if (device->program != (cl_program) NULL)
2984 (void) openCL_library->clReleaseProgram(device->program);
2985 while (device->command_queues_index >= 0)
2986 (void) openCL_library->clReleaseCommandQueue(
2987 device->command_queues[device->command_queues_index--]);
2988 RelinquishSemaphoreInfo(&device->lock);
2989 return((MagickCLDevice) RelinquishMagickMemory(device));
3015static MagickCLEnv RelinquishMagickCLEnv(MagickCLEnv clEnv)
3017 if (clEnv == (MagickCLEnv) NULL)
3018 return((MagickCLEnv) NULL);
3020 RelinquishSemaphoreInfo(&clEnv->lock);
3021 RelinquishMagickCLDevices(clEnv);
3022 if (clEnv->contexts != (cl_context *) NULL)
3027 for (i=0; i < clEnv->number_contexts; i++)
3028 if (clEnv->contexts[i] != (cl_context) NULL)
3029 (void) openCL_library->clReleaseContext(clEnv->contexts[i]);
3030 clEnv->contexts=(cl_context *) RelinquishMagickMemory(clEnv->contexts);
3032 return((MagickCLEnv) RelinquishMagickMemory(clEnv));
3057MagickPrivate MagickCLDevice RequestOpenCLDevice(MagickCLEnv clEnv)
3069 if (clEnv == (MagickCLEnv) NULL)
3070 return((MagickCLDevice) NULL);
3072 if (clEnv->number_devices == 1)
3074 if (clEnv->devices[0]->enabled)
3075 return(clEnv->devices[0]);
3077 return((MagickCLDevice) NULL);
3080 device=(MagickCLDevice) NULL;
3082 LockSemaphoreInfo(openCL_lock);
3083 for (i = 0; i < clEnv->number_devices; i++)
3085 if (clEnv->devices[i]->enabled == MagickFalse)
3088 score=clEnv->devices[i]->score+(clEnv->devices[i]->score*
3089 clEnv->devices[i]->requested);
3090 if ((device == (MagickCLDevice) NULL) || (score < best_score))
3092 device=clEnv->devices[i];
3096 if (device != (MagickCLDevice)NULL)
3097 device->requested++;
3098 UnlockSemaphoreInfo(openCL_lock);
3128MagickExport
void SetOpenCLDeviceEnabled(MagickCLDevice device,
3129 const MagickBooleanType value)
3131 if (device == (MagickCLDevice) NULL)
3133 device->enabled=value;
3163MagickExport
void SetOpenCLKernelProfileEnabled(MagickCLDevice device,
3164 const MagickBooleanType value)
3166 if (device == (MagickCLDevice) NULL)
3168 device->profile_kernels=value;
3193MagickExport MagickBooleanType SetOpenCLEnabled(
const MagickBooleanType value)
3198 clEnv=GetCurrentOpenCLEnv();
3199 if (clEnv == (MagickCLEnv) NULL)
3200 return(MagickFalse);
3201 clEnv->enabled=value;
3202 return(clEnv->enabled);
3207MagickExport
double GetOpenCLDeviceBenchmarkScore(
3208 const MagickCLDevice magick_unused(device))
3210 magick_unreferenced(device);
3214MagickExport MagickBooleanType GetOpenCLDeviceEnabled(
3215 const MagickCLDevice magick_unused(device))
3217 magick_unreferenced(device);
3218 return(MagickFalse);
3221MagickExport
const char *GetOpenCLDeviceName(
3222 const MagickCLDevice magick_unused(device))
3224 magick_unreferenced(device);
3225 return((
const char *) NULL);
3228MagickExport MagickCLDevice *GetOpenCLDevices(
size_t *length,
3231 magick_unreferenced(exception);
3232 if (length != (
size_t *) NULL)
3234 return((MagickCLDevice *) NULL);
3237MagickExport MagickCLDeviceType GetOpenCLDeviceType(
3238 const MagickCLDevice magick_unused(device))
3240 magick_unreferenced(device);
3241 return(UndefinedCLDeviceType);
3245 const MagickCLDevice magick_unused(device),
size_t *length)
3247 magick_unreferenced(device);
3248 if (length != (
size_t *) NULL)
3253MagickExport
const char *GetOpenCLDeviceVersion(
3254 const MagickCLDevice magick_unused(device))
3256 magick_unreferenced(device);
3257 return((
const char *) NULL);
3260MagickExport MagickBooleanType GetOpenCLEnabled(
void)
3262 return(MagickFalse);
3265MagickExport
void SetOpenCLDeviceEnabled(
3266 MagickCLDevice magick_unused(device),
3267 const MagickBooleanType magick_unused(value))
3269 magick_unreferenced(device);
3270 magick_unreferenced(value);
3273MagickExport MagickBooleanType SetOpenCLEnabled(
3274 const MagickBooleanType magick_unused(value))
3276 magick_unreferenced(value);
3277 return(MagickFalse);
3280MagickExport
void SetOpenCLKernelProfileEnabled(
3281 MagickCLDevice magick_unused(device),
3282 const MagickBooleanType magick_unused(value))
3284 magick_unreferenced(device);
3285 magick_unreferenced(value);