forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opencl.c
3415 lines (2925 loc) · 121 KB
/
opencl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of darktable,
Copyright (C) 2010-2022 darktable developers.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_OPENCL
#include "common/opencl.h"
#include "common/bilateralcl.h"
#include "common/darktable.h"
#include "common/dlopencl.h"
#include "common/dwt.h"
#include "common/file_location.h"
#include "common/gaussian.h"
#include "common/guided_filter.h"
#include "common/heal.h"
#include "common/interpolation.h"
#include "common/locallaplaciancl.h"
#include "common/nvidia_gpus.h"
#include "common/opencl_drivers_blacklist.h"
#include "common/tea.h"
#include "control/conf.h"
#include "control/control.h"
#include "develop/blend.h"
#include "develop/pixelpipe.h"
#include <assert.h>
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <errno.h>
#include <libgen.h>
#include <sys/stat.h>
#include <zlib.h>
static const char *dt_opencl_get_vendor_by_id(unsigned int id);
static float dt_opencl_benchmark_gpu(const int devid, const size_t width, const size_t height, const int count, const float sigma);
static float dt_opencl_benchmark_cpu(const size_t width, const size_t height, const int count, const float sigma);
static char *_ascii_str_canonical(const char *in, char *out, int maxlen);
/** parse a single token of priority string and store priorities in priority_list */
static void dt_opencl_priority_parse(dt_opencl_t *cl, char *configstr, int *priority_list, int *mandatory);
/** parse a complete priority string */
static void dt_opencl_priorities_parse(dt_opencl_t *cl, const char *configstr);
/** set device priorities according to config string */
static void dt_opencl_update_priorities(const char *configstr);
/** read scheduling profile for config variables */
static dt_opencl_scheduling_profile_t dt_opencl_get_scheduling_profile(void);
/** adjust opencl subsystem according to scheduling profile */
static void dt_opencl_apply_scheduling_profile(dt_opencl_scheduling_profile_t profile);
/** set opencl specific synchronization timeout */
static void dt_opencl_set_synchronization_timeout(int value);
const char *cl_errstr(cl_int error)
{
switch(error)
{
case CL_SUCCESS: return "CL_SUCCESS";
case CL_DEVICE_NOT_FOUND: return "CL_DEVICE_NOT_FOUND";
case CL_DEVICE_NOT_AVAILABLE: return "CL_DEVICE_NOT_AVAILABLE";
case CL_COMPILER_NOT_AVAILABLE: return "CL_COMPILER_NOT_AVAILABLE";
case CL_MEM_OBJECT_ALLOCATION_FAILURE: return "CL_MEM_OBJECT_ALLOCATION_FAILURE";
case CL_OUT_OF_RESOURCES: return "CL_OUT_OF_RESOURCES";
case CL_OUT_OF_HOST_MEMORY: return "CL_OUT_OF_HOST_MEMORY";
case CL_PROFILING_INFO_NOT_AVAILABLE: return "CL_PROFILING_INFO_NOT_AVAILABLE";
case CL_MEM_COPY_OVERLAP: return "CL_MEM_COPY_OVERLAP";
case CL_IMAGE_FORMAT_MISMATCH: return "CL_IMAGE_FORMAT_MISMATCH";
case CL_IMAGE_FORMAT_NOT_SUPPORTED: return "CL_IMAGE_FORMAT_NOT_SUPPORTED";
case CL_BUILD_PROGRAM_FAILURE: return "CL_BUILD_PROGRAM_FAILURE";
case CL_MAP_FAILURE: return "CL_MAP_FAILURE";
case CL_MISALIGNED_SUB_BUFFER_OFFSET: return "CL_MISALIGNED_SUB_BUFFER_OFFSET";
case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST: return "CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST";
case CL_COMPILE_PROGRAM_FAILURE: return "CL_COMPILE_PROGRAM_FAILURE";
case CL_LINKER_NOT_AVAILABLE: return "CL_LINKER_NOT_AVAILABLE";
case CL_LINK_PROGRAM_FAILURE: return "CL_LINK_PROGRAM_FAILURE";
case CL_DEVICE_PARTITION_FAILED: return "CL_DEVICE_PARTITION_FAILED";
case CL_KERNEL_ARG_INFO_NOT_AVAILABLE: return "CL_KERNEL_ARG_INFO_NOT_AVAILABLE";
case CL_INVALID_VALUE: return "CL_INVALID_VALUE";
case CL_INVALID_DEVICE_TYPE: return "CL_INVALID_DEVICE_TYPE";
case CL_INVALID_PLATFORM: return "CL_INVALID_PLATFORM";
case CL_INVALID_DEVICE: return "CL_INVALID_DEVICE";
case CL_INVALID_CONTEXT: return "CL_INVALID_CONTEXT";
case CL_INVALID_QUEUE_PROPERTIES: return "CL_INVALID_QUEUE_PROPERTIES";
case CL_INVALID_COMMAND_QUEUE: return "CL_INVALID_COMMAND_QUEUE";
case CL_INVALID_HOST_PTR: return "CL_INVALID_HOST_PTR";
case CL_INVALID_MEM_OBJECT: return "CL_INVALID_MEM_OBJECT";
case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: return "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR";
case CL_INVALID_IMAGE_SIZE: return "CL_INVALID_IMAGE_SIZE";
case CL_INVALID_SAMPLER: return "CL_INVALID_SAMPLER";
case CL_INVALID_BINARY: return "CL_INVALID_BINARY";
case CL_INVALID_BUILD_OPTIONS: return "CL_INVALID_BUILD_OPTIONS";
case CL_INVALID_PROGRAM: return "CL_INVALID_PROGRAM";
case CL_INVALID_PROGRAM_EXECUTABLE: return "CL_INVALID_PROGRAM_EXECUTABLE";
case CL_INVALID_KERNEL_NAME: return "CL_INVALID_KERNEL_NAME";
case CL_INVALID_KERNEL_DEFINITION: return "CL_INVALID_KERNEL_DEFINITION";
case CL_INVALID_KERNEL: return "CL_INVALID_KERNEL";
case CL_INVALID_ARG_INDEX: return "CL_INVALID_ARG_INDEX";
case CL_INVALID_ARG_VALUE: return "CL_INVALID_ARG_VALUE";
case CL_INVALID_ARG_SIZE: return "CL_INVALID_ARG_SIZE";
case CL_INVALID_KERNEL_ARGS: return "CL_INVALID_KERNEL_ARGS";
case CL_INVALID_WORK_DIMENSION: return "CL_INVALID_WORK_DIMENSION";
case CL_INVALID_WORK_GROUP_SIZE: return"CL_INVALID_WORK_GROUP_SIZE";
case CL_INVALID_WORK_ITEM_SIZE: return"CL_INVALID_WORK_ITEM_SIZE";
case CL_INVALID_GLOBAL_OFFSET: return"CL_INVALID_GLOBAL_OFFSET";
case CL_INVALID_EVENT_WAIT_LIST: return"CL_INVALID_EVENT_WAIT_LIST";
case CL_INVALID_EVENT: return"CL_INVALID_EVENT";
case CL_INVALID_OPERATION: return"CL_INVALID_OPERATION";
case CL_INVALID_GL_OBJECT: return"CL_INVALID_GL_OBJECT";
case CL_INVALID_BUFFER_SIZE: return"CL_INVALID_BUFFER_SIZE";
case CL_INVALID_MIP_LEVEL: return"CL_INVALID_MIP_LEVEL";
case CL_INVALID_GLOBAL_WORK_SIZE: return"CL_INVALID_GLOBAL_WORK_SIZE";
case CL_INVALID_PROPERTY: return"CL_INVALID_PROPERTY";
case CL_INVALID_IMAGE_DESCRIPTOR: return"CL_INVALID_IMAGE_DESCRIPTOR";
case CL_INVALID_COMPILER_OPTIONS: return"CL_INVALID_COMPILER_OPTIONS";
case CL_INVALID_LINKER_OPTIONS: return"CL_INVALID_LINKER_OPTIONS";
case CL_INVALID_DEVICE_PARTITION_COUNT: return"CL_INVALID_DEVICE_PARTITION_COUNT";
case DT_OPENCL_DEFAULT_ERROR: return "DT_OPENCL_DEFAULT_ERROR";
case DT_OPENCL_SYSMEM_ALLOCATION: return "DT_OPENCL_SYSMEM_ALLOCATION";
case DT_OPENCL_PROCESS_CL: return "DT_OPENCL_PROCESS_CL";
default: return "Unknown OpenCL error";
}
}
static inline void _check_clmem_err(const int devid, const cl_int err)
{
if((err == CL_MEM_OBJECT_ALLOCATION_FAILURE) || (err == CL_OUT_OF_RESOURCES))
darktable.opencl->dev[devid].runtime_error |= DT_OPENCL_TUNE_MEMSIZE;
}
int dt_opencl_get_device_info(dt_opencl_t *cl, cl_device_id device, cl_device_info param_name, void **param_value,
size_t *param_value_size)
{
*param_value_size = SIZE_MAX;
// 1. figure out how much memory is needed
cl_int err = (cl->dlocl->symbols->dt_clGetDeviceInfo)(device, param_name, 0, NULL, param_value_size);
if(err != CL_SUCCESS)
{
dt_print(DT_DEBUG_OPENCL,
"[dt_opencl_get_device_info] could not query the actual size in bytes of info %d: %s\n", param_name, cl_errstr(err));
goto error;
}
// 2. did we /actually/ get the size?
if(*param_value_size == SIZE_MAX || *param_value_size == 0)
{
// both of these sizes make no sense. either i failed to parse spec, or opencl implementation bug?
dt_print(DT_DEBUG_OPENCL,
"[dt_opencl_get_device_info] ERROR: no size returned, or zero size returned for data %d: %zu\n",
param_name, *param_value_size);
err = CL_INVALID_VALUE; // FIXME: anything better?
goto error;
}
// 3. make sure that *param_value points to big-enough memory block
{
void *ptr = realloc(*param_value, *param_value_size);
if(!ptr)
{
dt_print(DT_DEBUG_OPENCL,
"[dt_opencl_get_device_info] memory allocation failed! tried to allocate %zu bytes for data %d: %s",
*param_value_size, param_name, cl_errstr(err));
err = CL_OUT_OF_HOST_MEMORY;
goto error;
}
// allocation succeeded, update pointer.
*param_value = ptr;
}
// 4. actually get the value
err = (cl->dlocl->symbols->dt_clGetDeviceInfo)(device, param_name, *param_value_size, *param_value, NULL);
if(err != CL_SUCCESS)
{
dt_print(DT_DEBUG_OPENCL, "[dt_opencl_get_device_info] could not query info %d: %s\n", param_name, cl_errstr(err));
goto error;
}
return CL_SUCCESS;
error:
free(*param_value);
*param_value = NULL;
*param_value_size = 0;
return err;
}
int dt_opencl_avoid_atomics(const int devid)
{
dt_opencl_t *cl = darktable.opencl;
return (!cl->inited || devid < 0) ? 0 : cl->dev[devid].avoid_atomics;
}
int dt_opencl_micro_nap(const int devid)
{
dt_opencl_t *cl = darktable.opencl;
return (!cl->inited || devid < 0) ? 0 : cl->dev[devid].micro_nap;
}
gboolean dt_opencl_use_pinned_memory(const int devid)
{
dt_opencl_t *cl = darktable.opencl;
if(!cl->inited || devid < 0) return FALSE;
return (cl->dev[devid].tuneactive & DT_OPENCL_TUNE_PINNED);
}
void dt_opencl_write_device_config(const int devid)
{
if(devid < 0) return;
dt_opencl_t *cl = darktable.opencl;
gchar key[256] = { 0 };
gchar dat[512] = { 0 };
g_snprintf(key, 254, "%s%s", DT_CLDEVICE_HEAD, cl->dev[devid].cname);
g_snprintf(dat, 510, "%i %i %i %i %i %i %i %i %f %.3f",
cl->dev[devid].avoid_atomics,
cl->dev[devid].micro_nap,
cl->dev[devid].pinned_memory & (DT_OPENCL_PINNING_ON | DT_OPENCL_PINNING_DISABLED),
cl->dev[devid].clroundup_wd,
cl->dev[devid].clroundup_ht,
cl->dev[devid].event_handles,
cl->dev[devid].asyncmode & 1,
cl->dev[devid].disabled & 1,
cl->dev[devid].benchmark,
cl->dev[devid].advantage);
dt_vprint(DT_DEBUG_OPENCL, "[dt_opencl_write_device_config] writing data '%s' for '%s'\n", dat, key);
dt_conf_set_string(key, dat);
// Also take care of extended device data, these are not only device specific but also depend on the devid
// to support systems with two similar cards.
g_snprintf(key, 254, "%s%s_id%i", DT_CLDEVICE_HEAD, cl->dev[devid].cname, devid);
g_snprintf(dat, 510, "%i", cl->dev[devid].forced_headroom);
dt_vprint(DT_DEBUG_OPENCL, "[dt_opencl_write_device_config] writing data '%s' for '%s'\n", dat, key);
dt_conf_set_string(key, dat);
}
gboolean dt_opencl_read_device_config(const int devid)
{
if(devid < 0) return FALSE;
dt_opencl_t *cl = darktable.opencl;
gchar key[256] = { 0 };
g_snprintf(key, 254, "%s%s", DT_CLDEVICE_HEAD, cl->dev[devid].cname);
const gboolean existing_device = dt_conf_key_not_empty(key);
gboolean safety_ok = TRUE;
if(existing_device)
{
const gchar *dat = dt_conf_get_string_const(key);
int avoid_atomics;
int micro_nap;
int pinned_memory;
int wd;
int ht;
int event_handles;
int asyncmode;
int disabled;
float benchmark;
float advantage;
sscanf(dat, "%i %i %i %i %i %i %i %i %f %f",
&avoid_atomics, µ_nap, &pinned_memory, &wd, &ht, &event_handles, &asyncmode, &disabled, &benchmark, &advantage);
// some rudimentary safety checking if string seems to be ok
safety_ok = (wd > 1) && (wd < 513) && (ht > 1) && (ht < 513);
if(safety_ok)
{
cl->dev[devid].avoid_atomics = avoid_atomics;
cl->dev[devid].micro_nap = micro_nap;
cl->dev[devid].pinned_memory = pinned_memory;
cl->dev[devid].clroundup_wd = wd;
cl->dev[devid].clroundup_ht = ht;
cl->dev[devid].event_handles = event_handles;
cl->dev[devid].asyncmode = asyncmode;
cl->dev[devid].disabled = disabled;
cl->dev[devid].benchmark = benchmark;
cl->dev[devid].advantage = advantage;
}
else // if there is something wrong with the found conf key reset to defaults
{
dt_print(DT_DEBUG_OPENCL, "[dt_opencl_read_device_config] malformed data '%s' for '%s'\n", dat, key);
}
}
// do some safety housekeeping
cl->dev[devid].avoid_atomics &= 1;
cl->dev[devid].pinned_memory &= (DT_OPENCL_PINNING_ON | DT_OPENCL_PINNING_DISABLED);
if((cl->dev[devid].micro_nap < 0) || (cl->dev[devid].micro_nap > 1000000))
cl->dev[devid].micro_nap = 250;
if((cl->dev[devid].clroundup_wd < 2) || (cl->dev[devid].clroundup_wd > 512))
cl->dev[devid].clroundup_wd = 16;
if((cl->dev[devid].clroundup_ht < 2) || (cl->dev[devid].clroundup_ht > 512))
cl->dev[devid].clroundup_ht = 16;
if(cl->dev[devid].event_handles < 0)
cl->dev[devid].event_handles = 0x40000000;
cl->dev[devid].benchmark = fminf(1e6, fmaxf(0.0f, cl->dev[devid].benchmark));
cl->dev[devid].use_events = (cl->dev[devid].event_handles != 0) ? 1 : 0;
cl->dev[devid].asyncmode &= 1;
cl->dev[devid].disabled &= 1;
cl->dev[devid].advantage = fmaxf(0.0f, cl->dev[devid].advantage);
// Also take care of extended device data, these are not only device specific but also depend on the devid
g_snprintf(key, 254, "%s%s_id%i", DT_CLDEVICE_HEAD, cl->dev[devid].cname, devid);
if(dt_conf_key_not_empty(key))
{
const gchar *dat = dt_conf_get_string_const(key);
int forced_headroom;
sscanf(dat, "%i", &forced_headroom);
if(forced_headroom > 0) cl->dev[devid].forced_headroom = forced_headroom;
}
else // this is used if updating to 4.0 or fresh installs; see commenting _opencl_get_unused_device_mem()
cl->dev[devid].forced_headroom = 400;
dt_opencl_write_device_config(devid);
return !existing_device || !safety_ok;
}
static float dt_opencl_device_perfgain(const int devid)
{
dt_opencl_t *cl = darktable.opencl;
const float tcpu = cl->cpubenchmark;
const float tgpu = cl->dev[devid].benchmark;
if((tcpu < 1e-8) || (tgpu < 1e-8)) return 1.0f;
return (tcpu / tgpu);
}
// returns 0 if all ok or an error if we failed to init this device
static int dt_opencl_device_init(dt_opencl_t *cl, const int dev, cl_device_id *devices, const int k)
{
int res;
cl_int err;
memset(cl->dev[dev].program, 0x0, sizeof(cl_program) * DT_OPENCL_MAX_PROGRAMS);
memset(cl->dev[dev].program_used, 0x0, sizeof(int) * DT_OPENCL_MAX_PROGRAMS);
memset(cl->dev[dev].kernel, 0x0, sizeof(cl_kernel) * DT_OPENCL_MAX_KERNELS);
memset(cl->dev[dev].kernel_used, 0x0, sizeof(int) * DT_OPENCL_MAX_KERNELS);
cl->dev[dev].eventlist = NULL;
cl->dev[dev].eventtags = NULL;
cl->dev[dev].numevents = 0;
cl->dev[dev].eventsconsolidated = 0;
cl->dev[dev].maxevents = 0;
cl->dev[dev].maxeventslot = 0;
cl->dev[dev].lostevents = 0;
cl->dev[dev].totalevents = 0;
cl->dev[dev].totalsuccess = 0;
cl->dev[dev].totallost = 0;
cl->dev[dev].summary = CL_COMPLETE;
cl->dev[dev].used_global_mem = 0;
cl->dev[dev].nvidia_sm_20 = 0;
cl->dev[dev].vendor = NULL;
cl->dev[dev].name = NULL;
cl->dev[dev].cname = NULL;
cl->dev[dev].options = NULL;
cl->dev[dev].memory_in_use = 0;
cl->dev[dev].peak_memory = 0;
cl->dev[dev].used_available = 0;
// setting sane/conservative defaults at first
cl->dev[dev].avoid_atomics = 0;
cl->dev[dev].micro_nap = 250;
cl->dev[dev].pinned_memory = DT_OPENCL_PINNING_OFF;
cl->dev[dev].clroundup_wd = 16;
cl->dev[dev].clroundup_ht = 16;
cl->dev[dev].benchmark = 0.0f;
cl->dev[dev].advantage = 0.0f;
cl->dev[dev].use_events = 1;
cl->dev[dev].event_handles = 128;
cl->dev[dev].asyncmode = 0;
cl->dev[dev].disabled = 0;
cl->dev[dev].forced_headroom = 0;
cl->dev[dev].tuneactive = 0;
cl->dev[dev].runtime_error = 0;
cl_device_id devid = cl->dev[dev].devid = devices[k];
char *infostr = NULL;
size_t infostr_size;
char *cname = NULL;
size_t cname_size;
char *vendor = NULL;
size_t vendor_size;
char *driverversion = NULL;
size_t driverversion_size;
char *deviceversion = NULL;
size_t deviceversion_size;
size_t infoint;
size_t *infointtab = NULL;
cl_device_type type;
cl_bool image_support = 0;
cl_bool device_available = 0;
cl_uint vendor_id = 0;
cl_bool little_endian = 0;
cl_platform_id platform_id = 0;
char *dtcache = calloc(PATH_MAX, sizeof(char));
char *cachedir = calloc(PATH_MAX, sizeof(char));
char *devname = calloc(DT_OPENCL_CBUFFSIZE, sizeof(char));
char *drvversion = calloc(DT_OPENCL_CBUFFSIZE, sizeof(char));
char *platform_name = calloc(DT_OPENCL_CBUFFSIZE, sizeof(char));
char *platform_vendor = calloc(DT_OPENCL_CBUFFSIZE, sizeof(char));
char kerneldir[PATH_MAX] = { 0 };
char *filename = calloc(PATH_MAX, sizeof(char));
char *confentry = calloc(PATH_MAX, sizeof(char));
char *binname = calloc(PATH_MAX, sizeof(char));
dt_print_nts(DT_DEBUG_OPENCL, "\n[dt_opencl_device_init]\n");
// test GPU availability, vendor, memory, image support etc:
(cl->dlocl->symbols->dt_clGetDeviceInfo)(devid, CL_DEVICE_AVAILABLE, sizeof(cl_bool), &device_available, NULL);
err = dt_opencl_get_device_info(cl, devid, CL_DEVICE_VENDOR, (void **)&vendor, &vendor_size);
if(err != CL_SUCCESS)
{
dt_print_nts(DT_DEBUG_OPENCL, " *** could not get vendor name of device %d: %s\n", k, cl_errstr(err));
res = -1;
goto end;
}
(cl->dlocl->symbols->dt_clGetDeviceInfo)(devid, CL_DEVICE_VENDOR_ID, sizeof(cl_uint), &vendor_id, NULL);
err = dt_opencl_get_device_info(cl, devid, CL_DEVICE_NAME, (void **)&infostr, &infostr_size);
if(err != CL_SUCCESS)
{
dt_print_nts(DT_DEBUG_OPENCL, " *** could not get device name of device %d: %s\n", k, cl_errstr(err));
res = -1;
goto end;
}
// get the canonical device name
cname_size = infostr_size;
cname = malloc(cname_size);
_ascii_str_canonical(infostr, cname, cname_size);
cl->dev[dev].name = strdup(infostr);
cl->dev[dev].cname = strdup(cname);
// take every detected device into account of checksum
cl->crc = crc32(cl->crc, (const unsigned char *)infostr, strlen(infostr));
err = (cl->dlocl->symbols->dt_clGetDeviceInfo)(devid, CL_DEVICE_PLATFORM, sizeof(cl_platform_id), &platform_id, NULL);
if(err != CL_SUCCESS)
{
g_strlcpy(platform_vendor, "no platform id", DT_OPENCL_CBUFFSIZE);
g_strlcpy(platform_name, "no platform id", DT_OPENCL_CBUFFSIZE);
dt_print_nts(DT_DEBUG_OPENCL, " *** could not get platform id for device `%s' : %s\n", cl->dev[dev].name, cl_errstr(err));
}
else
{
err = (cl->dlocl->symbols->dt_clGetPlatformInfo)(platform_id, CL_PLATFORM_NAME, DT_OPENCL_CBUFFSIZE, platform_name, NULL);
if(err != CL_SUCCESS)
{
dt_print_nts(DT_DEBUG_OPENCL, " *** could not get platform name for device `%s' : %s\n", cl->dev[dev].name, cl_errstr(err));
g_strlcpy(platform_name, "???", DT_OPENCL_CBUFFSIZE);
}
err = (cl->dlocl->symbols->dt_clGetPlatformInfo)(platform_id, CL_PLATFORM_VENDOR, DT_OPENCL_CBUFFSIZE, platform_vendor, NULL);
if(err != CL_SUCCESS)
{
dt_print_nts(DT_DEBUG_OPENCL, " *** could not get platform vendor for device `%s' : %s\n", cl->dev[dev].name, cl_errstr(err));
g_strlcpy(platform_vendor, "???", DT_OPENCL_CBUFFSIZE);
}
}
const gboolean newdevice = dt_opencl_read_device_config(dev);
dt_print_nts(DT_DEBUG_OPENCL, " DEVICE: %d: '%s'%s\n", k, infostr, (newdevice) ? ", NEW" : "" );
dt_print_nts(DT_DEBUG_OPENCL, " CANONICAL NAME: %s\n", cname);
dt_print_nts(DT_DEBUG_OPENCL, " PLATFORM NAME & VENDOR: %s, %s\n", platform_name, platform_vendor);
err = dt_opencl_get_device_info(cl, devid, CL_DRIVER_VERSION, (void **)&driverversion, &driverversion_size);
if(err != CL_SUCCESS)
{
dt_print_nts(DT_DEBUG_OPENCL, " *** driver version not available *** %s\n", cl_errstr(err));
res = -1;
cl->dev[dev].disabled |= 1;
goto end;
}
err = dt_opencl_get_device_info(cl, devid, CL_DEVICE_VERSION, (void **)&deviceversion, &deviceversion_size);
if(err != CL_SUCCESS)
{
dt_print_nts(DT_DEBUG_OPENCL, " *** device version not available *** %s\n", cl_errstr(err));
res = -1;
cl->dev[dev].disabled |= 1;
goto end;
}
(cl->dlocl->symbols->dt_clGetDeviceInfo)(devid, CL_DEVICE_TYPE, sizeof(cl_device_type), &type, NULL);
(cl->dlocl->symbols->dt_clGetDeviceInfo)(devid, CL_DEVICE_IMAGE_SUPPORT, sizeof(cl_bool), &image_support, NULL);
(cl->dlocl->symbols->dt_clGetDeviceInfo)(devid, CL_DEVICE_IMAGE2D_MAX_HEIGHT, sizeof(size_t),
&(cl->dev[dev].max_image_height), NULL);
(cl->dlocl->symbols->dt_clGetDeviceInfo)(devid, CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof(size_t),
&(cl->dev[dev].max_image_width), NULL);
(cl->dlocl->symbols->dt_clGetDeviceInfo)(devid, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(cl_ulong),
&(cl->dev[dev].max_mem_alloc), NULL);
(cl->dlocl->symbols->dt_clGetDeviceInfo)(devid, CL_DEVICE_ENDIAN_LITTLE, sizeof(cl_bool), &little_endian, NULL);
cl->dev[dev].cltype = (unsigned int)type;
if(!strncasecmp(vendor, "NVIDIA", 6))
{
// very lame attempt to detect support for atomic float add in global memory.
// we need compute model sm_20, but let's try for all nvidia devices :(
cl->dev[dev].nvidia_sm_20 = dt_nvidia_gpu_supports_sm_20(infostr);
}
const gboolean is_cpu_device = (type & CL_DEVICE_TYPE_CPU) == CL_DEVICE_TYPE_CPU;
// micro_nap can be made less conservative on current systems at least if not on-CPU
if(newdevice)
cl->dev[dev].micro_nap = (is_cpu_device) ? 1000 : 250;
dt_print_nts(DT_DEBUG_OPENCL, " DRIVER VERSION: %s\n", driverversion);
dt_print_nts(DT_DEBUG_OPENCL, " DEVICE VERSION: %s%s\n", deviceversion,
cl->dev[dev].nvidia_sm_20 ? ", SM_20 SUPPORT" : "");
dt_print_nts(DT_DEBUG_OPENCL, " DEVICE_TYPE: %s%s%s\n",
((type & CL_DEVICE_TYPE_CPU) == CL_DEVICE_TYPE_CPU) ? "CPU" : "",
((type & CL_DEVICE_TYPE_GPU) == CL_DEVICE_TYPE_GPU) ? "GPU" : "",
(type & CL_DEVICE_TYPE_ACCELERATOR) ? ", Accelerator" : "" );
if(is_cpu_device && newdevice)
{
dt_print_nts(DT_DEBUG_OPENCL, " *** discarding new device as emulated by CPU ***\n");
cl->dev[dev].disabled |= 1;
res = -1;
goto end;
}
if(!device_available)
{
dt_print_nts(DT_DEBUG_OPENCL, " *** device is not available ***\n");
res = -1;
goto end;
}
if(!image_support)
{
dt_print_nts(DT_DEBUG_OPENCL, " *** The OpenCL driver doesn't provide image support. See also 'clinfo' output ***\n");
res = -1;
cl->dev[dev].disabled |= 1;
goto end;
}
if(!little_endian)
{
dt_print_nts(DT_DEBUG_OPENCL, " *** device is not little endian ***\n");
res = -1;
cl->dev[dev].disabled |= 1;
goto end;
}
(cl->dlocl->symbols->dt_clGetDeviceInfo)(devid, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(cl_ulong),
&(cl->dev[dev].max_global_mem), NULL);
if(cl->dev[dev].max_global_mem < (uint64_t)512ul * 1024ul * 1024ul)
{
dt_print_nts(DT_DEBUG_OPENCL, " *** insufficient global memory (%" PRIu64 "MB) ***\n",
cl->dev[dev].max_global_mem / 1024 / 1024);
res = -1;
cl->dev[dev].disabled |= 1;
goto end;
}
cl->dev[dev].vendor = strdup(dt_opencl_get_vendor_by_id(vendor_id));
const gboolean is_blacklisted = dt_opencl_check_driver_blacklist(deviceversion);
// disable device for now if this is the first time detected and blacklisted too.
if(newdevice && is_blacklisted)
{
// To keep installations we look for the old blacklist conf key
const gboolean old_blacklist = dt_conf_get_bool("opencl_disable_drivers_blacklist");
cl->dev[dev].disabled |= (old_blacklist) ? 0 : 1;
if(cl->dev[dev].disabled)
dt_print_nts(DT_DEBUG_OPENCL, " *** new device is blacklisted ***\n");
res = -1;
goto end;
}
dt_print_nts(DT_DEBUG_OPENCL, " GLOBAL MEM SIZE: %.0f MB\n", (double)cl->dev[dev].max_global_mem / 1024.0 / 1024.0);
dt_print_nts(DT_DEBUG_OPENCL, " MAX MEM ALLOC: %.0f MB\n", (double)cl->dev[dev].max_mem_alloc / 1024.0 / 1024.0);
dt_print_nts(DT_DEBUG_OPENCL, " MAX IMAGE SIZE: %zd x %zd\n", cl->dev[dev].max_image_width, cl->dev[dev].max_image_height);
(cl->dlocl->symbols->dt_clGetDeviceInfo)(devid, CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(infoint), &infoint, NULL);
dt_print_nts(DT_DEBUG_OPENCL, " MAX WORK GROUP SIZE: %zu\n", infoint);
(cl->dlocl->symbols->dt_clGetDeviceInfo)(devid, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(infoint), &infoint, NULL);
dt_print_nts(DT_DEBUG_OPENCL, " MAX WORK ITEM DIMENSIONS: %zu\n", infoint);
size_t infointtab_size;
err = dt_opencl_get_device_info(cl, devid, CL_DEVICE_MAX_WORK_ITEM_SIZES, (void **)&infointtab, &infointtab_size);
if(err == CL_SUCCESS)
{
dt_print_nts(DT_DEBUG_OPENCL, " MAX WORK ITEM SIZES: [ ");
for(size_t i = 0; i < infoint; i++) dt_print_nts(DT_DEBUG_OPENCL, "%zu ", infointtab[i]);
free(infointtab);
infointtab = NULL;
dt_print_nts(DT_DEBUG_OPENCL, "]\n");
}
else
{
dt_print_nts(DT_DEBUG_OPENCL, " *** could not get maximum work item sizes ***\n");
res = -1;
cl->dev[dev].disabled |= 1;
goto end;
}
dt_sys_resources_t *resrc = &darktable.dtresources;
const gboolean pinning = (cl->dev[dev].pinned_memory & DT_OPENCL_PINNING_ON) || (resrc->tunemode & DT_OPENCL_TUNE_PINNED);
const gboolean tuning = resrc->tunemode & DT_OPENCL_TUNE_MEMSIZE;
dt_print_nts(DT_DEBUG_OPENCL, " ASYNC PIXELPIPE: %s\n", (cl->dev[dev].asyncmode) ? "YES" : "NO");
dt_print_nts(DT_DEBUG_OPENCL, " PINNED MEMORY TRANSFER: %s\n", pinning ? "WANTED" : "NO");
dt_print_nts(DT_DEBUG_OPENCL, " MEMORY TUNING: %s\n", tuning ? "WANTED" : "NO");
dt_print_nts(DT_DEBUG_OPENCL, " FORCED HEADROOM: %i\n", cl->dev[dev].forced_headroom);
dt_print_nts(DT_DEBUG_OPENCL, " AVOID ATOMICS: %s\n", (cl->dev[dev].avoid_atomics) ? "YES" : "NO");
dt_print_nts(DT_DEBUG_OPENCL, " MICRO NAP: %i\n", cl->dev[dev].micro_nap);
dt_print_nts(DT_DEBUG_OPENCL, " ROUNDUP WIDTH: %i\n", cl->dev[dev].clroundup_wd);
dt_print_nts(DT_DEBUG_OPENCL, " ROUNDUP HEIGHT: %i\n", cl->dev[dev].clroundup_ht);
dt_print_nts(DT_DEBUG_OPENCL, " CHECK EVENT HANDLES: %i\n", cl->dev[dev].event_handles);
if(cl->dev[dev].benchmark > 0.0f)
dt_print_nts(DT_DEBUG_OPENCL, " PERFORMANCE: %.3f\n", dt_opencl_device_perfgain(dev));
dt_print_nts(DT_DEBUG_OPENCL, " TILING ADVANTAGE: %.3f\n", cl->dev[dev].advantage);
dt_print_nts(DT_DEBUG_OPENCL, " DEFAULT DEVICE: %s\n", (type & CL_DEVICE_TYPE_DEFAULT) ? "YES" : "NO");
if(cl->dev[dev].disabled)
{
dt_print_nts(DT_DEBUG_OPENCL, " *** marked as disabled ***\n");
res = -1;
goto end;
}
dt_pthread_mutex_init(&cl->dev[dev].lock, NULL);
cl->dev[dev].context = (cl->dlocl->symbols->dt_clCreateContext)(0, 1, &devid, NULL, NULL, &err);
if(err != CL_SUCCESS)
{
dt_print_nts(DT_DEBUG_OPENCL, " *** could not create context *** %s\n", cl_errstr(err));
res = -1;
goto end;
}
// create a command queue for first device the context reported
cl->dev[dev].cmd_queue = (cl->dlocl->symbols->dt_clCreateCommandQueue)(
cl->dev[dev].context, devid, (darktable.unmuted & DT_DEBUG_PERF) ? CL_QUEUE_PROFILING_ENABLE : 0, &err);
if(err != CL_SUCCESS)
{
dt_print_nts(DT_DEBUG_OPENCL, " *** could not create command queue *** %s\n", cl_errstr(err));
res = -1;
goto end;
}
double tstart, tend, tdiff;
dt_loc_get_user_cache_dir(dtcache, PATH_MAX * sizeof(char));
int len = MIN(strlen(infostr),1024 * sizeof(char));;
int j = 0;
// remove non-alphanumeric chars from device name
for(int i = 0; i < len; i++)
if(isalnum(infostr[i])) devname[j++] = infostr[i];
devname[j] = 0;
len = MIN(strlen(driverversion), 1024 * sizeof(char));
j = 0;
// remove non-alphanumeric chars from driver version
for(int i = 0; i < len; i++)
if(isalnum(driverversion[i])) drvversion[j++] = driverversion[i];
drvversion[j] = 0;
snprintf(cachedir, PATH_MAX * sizeof(char), "%s" G_DIR_SEPARATOR_S "cached_kernels_for_%s_%s", dtcache, devname, drvversion);
if(g_mkdir_with_parents(cachedir, 0700) == -1)
{
dt_print_nts(DT_DEBUG_OPENCL, " *** failed to create kernel directory `%s' ***\n", cachedir);
res = -1;
goto end;
}
dt_loc_get_kerneldir(kerneldir, sizeof(kerneldir));
dt_print_nts(DT_DEBUG_OPENCL, " KERNEL DIRECTORY: %s\n", kerneldir);
snprintf(filename, PATH_MAX * sizeof(char), "%s" G_DIR_SEPARATOR_S "programs.conf", kerneldir);
char *escapedkerneldir = NULL;
#ifndef __APPLE__
escapedkerneldir = g_strdup_printf("\"%s\"", kerneldir);
#else
escapedkerneldir = dt_util_str_replace(kerneldir, " ", "\\ ");
#endif
gchar* compile_option_name_cname = g_strdup_printf("%s%s_building", DT_CLDEVICE_HEAD, cl->dev[dev].cname);
const char* compile_opt = NULL;
if(dt_conf_key_exists(compile_option_name_cname))
compile_opt = dt_conf_get_string_const(compile_option_name_cname);
else
{
switch(vendor_id)
{
case DT_OPENCL_VENDOR_AMD:
compile_opt = DT_OPENCL_DEFAULT_COMPILE_AMD;
break;
case DT_OPENCL_VENDOR_NVIDIA:
compile_opt = DT_OPENCL_DEFAULT_COMPILE_NVIDIA;
break;
case DT_OPENCL_VENDOR_INTEL:
compile_opt = DT_OPENCL_DEFAULT_COMPILE_INTEL;
break;
default:
compile_opt = DT_OPENCL_DEFAULT_COMPILE;
}
}
gchar *my_option = g_strdup(compile_opt);
dt_conf_set_string(compile_option_name_cname, my_option);
cl->dev[dev].options = g_strdup_printf("-w %s %s -D%s=1 -I%s",
my_option,
(cl->dev[dev].nvidia_sm_20 ? " -DNVIDIA_SM_20=1" : ""),
dt_opencl_get_vendor_by_id(vendor_id), escapedkerneldir);
dt_print_nts(DT_DEBUG_OPENCL, " CL COMPILER OPTION: %s\n", my_option);
g_free(compile_option_name_cname);
g_free(my_option);
g_free(escapedkerneldir);
escapedkerneldir = NULL;
const char *clincludes[DT_OPENCL_MAX_INCLUDES] = { "rgb_norms.h", "noise_generator.h", "color_conversion.h", "colorspaces.cl", "colorspace.h", "common.h", NULL };
char *includemd5[DT_OPENCL_MAX_INCLUDES] = { NULL };
dt_opencl_md5sum(clincludes, includemd5);
if(newdevice) // so far the device seems to be ok. Make sure to write&export the conf database to
{
dt_opencl_write_device_config(dev);
dt_conf_save(darktable.conf);
}
// now load all darktable cl kernels.
// TODO: compile as a job?
tstart = dt_get_wtime();
FILE *f = g_fopen(filename, "rb");
if(f)
{
while(!feof(f))
{
int prog = -1;
gchar *confline_pattern = g_strdup_printf("%%%zu[^\n]\n", PATH_MAX * sizeof(char) - 1);
int rd = fscanf(f, confline_pattern, confentry);
g_free(confline_pattern);
if(rd != 1) continue;
// remove comments:
size_t end = strlen(confentry);
for(size_t pos = 0; pos < end; pos++)
if(confentry[pos] == '#')
{
confentry[pos] = '\0';
for(int l = pos - 1; l >= 0; l--)
{
if(confentry[l] == ' ')
confentry[l] = '\0';
else
break;
}
break;
}
if(confentry[0] == '\0') continue;
const char *programname = NULL, *programnumber = NULL;
gchar **tokens = g_strsplit_set(confentry, " \t", 2);
if(tokens)
{
programname = tokens[0];
if(tokens[0])
programnumber = tokens[1]; // if the 0st wasn't NULL then we have at least the terminating NULL in [1]
}
prog = programnumber ? strtol(programnumber, NULL, 10) : -1;
if(!programname || programname[0] == '\0' || prog < 0)
{
dt_print(DT_DEBUG_OPENCL, "[dt_opencl_device_init] malformed entry in programs.conf `%s'; ignoring it!\n", confentry);
continue;
}
snprintf(filename, PATH_MAX * sizeof(char), "%s" G_DIR_SEPARATOR_S "%s", kerneldir, programname);
snprintf(binname, PATH_MAX * sizeof(char), "%s" G_DIR_SEPARATOR_S "%s.bin", cachedir, programname);
dt_vprint(DT_DEBUG_OPENCL, "[dt_opencl_device_init] testing program `%s' ..\n", programname);
int loaded_cached;
char md5sum[33];
if(dt_opencl_load_program(dev, prog, filename, binname, cachedir, md5sum, includemd5, &loaded_cached)
&& dt_opencl_build_program(dev, prog, binname, cachedir, md5sum, loaded_cached) != CL_SUCCESS)
{
dt_print(DT_DEBUG_OPENCL, "[dt_opencl_device_init] failed to compile program `%s'!\n", programname);
fclose(f);
g_strfreev(tokens);
res = -1;
goto end;
}
g_strfreev(tokens);
}
fclose(f);
tend = dt_get_wtime();
tdiff = tend - tstart;
dt_print_nts(DT_DEBUG_OPENCL, " KERNEL LOADING TIME: %2.4lf sec\n", tdiff);
}
else
{
dt_print_nts(DT_DEBUG_OPENCL, "[dt_opencl_device_init] could not open `%s'!\n", filename);
res = -1;
goto end;
}
for(int n = 0; n < DT_OPENCL_MAX_INCLUDES; n++) g_free(includemd5[n]);
res = 0;
end:
// we always write the device config to keep track of disabled devices
dt_opencl_write_device_config(dev);
free(infostr);
free(cname);
free(vendor);
free(driverversion);
free(deviceversion);
free(dtcache);
free(cachedir);
free(devname);
free(drvversion);
free(platform_name);
free(platform_vendor);
free(filename);
free(confentry);
free(binname);
return res;
}
void dt_opencl_init(dt_opencl_t *cl, const gboolean exclude_opencl, const gboolean print_statistics)
{
dt_pthread_mutex_init(&cl->lock, NULL);
cl->inited = 0;
cl->enabled = 0;
cl->stopped = 0;
cl->error_count = 0;
cl->print_statistics = print_statistics;
// work-around to fix a bug in some AMD OpenCL compilers, which would fail parsing certain numerical
// constants if locale is different from "C".
// we save the current locale, set locale to "C", and restore the previous setting after OpenCL is
// initialized
char *locale = strdup(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, "C");
cl->crc = 5781;
cl->dlocl = NULL;
cl->dev_priority_image = NULL;
cl->dev_priority_preview = NULL;
cl->dev_priority_preview2 = NULL;
cl->dev_priority_export = NULL;
cl->dev_priority_thumbnail = NULL;
cl_platform_id *all_platforms = NULL;
cl_uint *all_num_devices = NULL;
char *platform_name = calloc(DT_OPENCL_CBUFFSIZE, sizeof(char));
char *platform_vendor = calloc(DT_OPENCL_CBUFFSIZE, sizeof(char));
cl->cpubenchmark = dt_conf_get_float("dt_cpubenchmark");
if(cl->cpubenchmark <= 0.0f)
{
cl->cpubenchmark = dt_opencl_benchmark_cpu(1024, 1024, 5, 100.0f);
dt_conf_set_float("dt_cpubenchmark", cl->cpubenchmark);
}
if(exclude_opencl)
{
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] do not try to find and use an opencl runtime library due to "
"explicit user request\n");
goto finally;
}
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] opencl related configuration options:\n");
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] opencl: %s\n", dt_conf_get_bool("opencl") ? "ON" : "OFF" );
const char *str = dt_conf_get_string_const("opencl_scheduling_profile");
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] opencl_scheduling_profile: '%s'\n", str);
// look for explicit definition of opencl_runtime library in preferences
const char *library = dt_conf_get_string_const("opencl_library");
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] opencl_library: '%s'\n", (strlen(library) == 0) ? "default path" : library);
str = dt_conf_get_string_const("opencl_device_priority");
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] opencl_device_priority: '%s'\n", str);
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] opencl_mandatory_timeout: %d\n",
dt_conf_get_int("opencl_mandatory_timeout"));
// dynamically load opencl runtime
if((cl->dlocl = dt_dlopencl_init(library)) == NULL)
{
dt_print_nts(DT_DEBUG_OPENCL,
"[opencl_init] no working opencl library found. Continue with opencl disabled\n");
goto finally;
}
else
{
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] opencl library '%s' found on your system and loaded\n",
cl->dlocl->library);
}
cl_int err;
all_platforms = malloc(sizeof(cl_platform_id) * DT_OPENCL_MAX_PLATFORMS);
all_num_devices = malloc(sizeof(cl_uint) * DT_OPENCL_MAX_PLATFORMS);
cl_uint num_platforms = DT_OPENCL_MAX_PLATFORMS;
err = (cl->dlocl->symbols->dt_clGetPlatformIDs)(DT_OPENCL_MAX_PLATFORMS, all_platforms, &num_platforms);
if(err != CL_SUCCESS)
{
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] could not get platforms: %s\n", cl_errstr(err));
goto finally;
}
if(num_platforms == 0)
{
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] no opencl platform available\n");
goto finally;
}
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] found %d platform%s\n", num_platforms,
num_platforms > 1 ? "s" : "");
for(int n = 0; n < num_platforms; n++)
{
cl_platform_id platform = all_platforms[n];
// get the number of GPU devices available to the platforms
// the other common option is CL_DEVICE_TYPE_GPU/CPU (but the latter doesn't work with the nvidia drivers)
err = (cl->dlocl->symbols->dt_clGetDeviceIDs)(platform, CL_DEVICE_TYPE_ALL, 0, NULL, &(all_num_devices[n]));
if(err != CL_SUCCESS)
{
cl_int errv = (cl->dlocl->symbols->dt_clGetPlatformInfo)(platform, CL_PLATFORM_VENDOR, DT_OPENCL_CBUFFSIZE, platform_vendor, NULL);
cl_int errn = (cl->dlocl->symbols->dt_clGetPlatformInfo)(platform, CL_PLATFORM_NAME, DT_OPENCL_CBUFFSIZE, platform_name, NULL);
if((errn == CL_SUCCESS) && (errv == CL_SUCCESS))
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] no devices found for %s (vendor) - %s (name)\n", platform_vendor, platform_name);
else
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] no devices found for unknown platform\n");
all_num_devices[n] = 0;
}
else
{
char profile[64] = { 0 };
size_t profile_size;
err = (cl->dlocl->symbols->dt_clGetPlatformInfo)(platform, CL_PLATFORM_PROFILE, 64, profile, &profile_size);
if(err != CL_SUCCESS)
{
all_num_devices[n] = 0;
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] could not get profile: %s\n", cl_errstr(err));
}
else
{
// fprintf(stderr, "%s\n", profile);
if(strcmp("FULL_PROFILE", profile) != 0)
{
all_num_devices[n] = 0;
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] platform %i is not FULL_PROFILE\n", n);
}
}
}
}
cl_uint num_devices = 0;
for(int n = 0; n < num_platforms; n++) num_devices += all_num_devices[n];
// create the device list
cl_device_id *devices = 0;
if(num_devices)
{
cl->dev = (dt_opencl_device_t *)malloc(sizeof(dt_opencl_device_t) * num_devices);
devices = (cl_device_id *)malloc(sizeof(cl_device_id) * num_devices);
if(!cl->dev || !devices)
{
free(cl->dev);
cl->dev = NULL;
free(devices);
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] could not allocate memory\n");
goto finally;
}
}
cl_device_id *devs = devices;
for(int n = 0; n < num_platforms; n++)
{
if(all_num_devices[n])
{
cl_platform_id platform = all_platforms[n];
err = (cl->dlocl->symbols->dt_clGetDeviceIDs)(platform, CL_DEVICE_TYPE_ALL, all_num_devices[n], devs,