-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathtest_openclhpp.cpp
More file actions
4629 lines (3988 loc) · 151 KB
/
test_openclhpp.cpp
File metadata and controls
4629 lines (3988 loc) · 151 KB
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
#define CL_HPP_UNIT_TEST_ENABLE
// We want to support all versions
#define CL_HPP_MINIMUM_OPENCL_VERSION 100
# include <CL/opencl.hpp>
# define TEST_RVALUE_REFERENCES
# define VECTOR_CLASS cl::vector
# define STRING_CLASS cl::string
extern "C"
{
#include <unity.h>
#include <cmock.h>
#include "Mockcl.h"
#include "Mockcl_ext.h"
#include "Mockcl_gl.h"
#include <string.h>
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
/// Creates fake IDs that are easy to identify
static inline cl_platform_id make_platform_id(int index)
{
return (cl_platform_id) (size_t) (0x1a1a1a1a + index);
}
static inline cl_context make_context(int index)
{
return (cl_context) (size_t) (0xcccccccc + index);
}
static inline cl_device_id make_device_id(int index)
{
return (cl_device_id) (size_t) (0xdededede + index);
}
static inline cl_mem make_mem(int index)
{
return (cl_mem) (size_t) (0x33333333 + index);
}
static inline cl_command_queue make_command_queue(int index)
{
return (cl_command_queue) (size_t) (0xc0c0c0c0 + index);
}
static inline cl_kernel make_kernel(int index)
{
return (cl_kernel) (size_t) (0xcececece + index);
}
static inline cl_program make_program(int index)
{
return (cl_program)(size_t)(0xcfcfcfcf + index);
}
static inline cl_command_buffer_khr make_command_buffer_khr(int index)
{
return (cl_command_buffer_khr)(size_t)(0x8f8f8f8f + index);
}
static inline cl_event make_event(int index)
{
return (cl_event)(size_t)(0xd0d0d0d0 + index);
}
#if defined(cl_khr_semaphore)
static inline cl_semaphore_khr make_semaphore_khr(int index)
{
return (cl_semaphore_khr)(size_t)(0xa0b0c0e0 + index);
}
#endif
/* Pools of pre-allocated wrapped objects for tests.
*/
static const int POOL_MAX = 5;
static cl::Platform platformPool[POOL_MAX];
static cl::Context contextPool[POOL_MAX];
static cl::CommandQueue commandQueuePool[POOL_MAX];
static cl::Buffer bufferPool[POOL_MAX];
static cl::Image2D image2DPool[POOL_MAX];
static cl::Image3D image3DPool[POOL_MAX];
static cl::Kernel kernelPool[POOL_MAX];
static cl::Program programPool[POOL_MAX];
#if defined(cl_khr_command_buffer)
static cl::CommandBufferKhr commandBufferKhrPool[POOL_MAX];
#endif
#if defined(cl_khr_semaphore)
static cl::Semaphore semaphorePool[POOL_MAX];
#endif
static cl::Device devicePool[POOL_MAX];
/****************************************************************************
* Stub functions shared by multiple tests
****************************************************************************/
/**
* Stub implementation of clGetCommandQueueInfo that returns the first context.
*/
static cl_int clGetCommandQueueInfo_context(
cl_command_queue id,
cl_command_queue_info param_name,
size_t param_value_size,
void *param_value,
size_t *param_value_size_ret,
int num_calls)
{
(void) id;
(void) num_calls;
TEST_ASSERT_EQUAL_HEX(CL_QUEUE_CONTEXT, param_name);
TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_context));
if (param_value_size_ret != nullptr)
*param_value_size_ret = sizeof(cl_context);
if (param_value != nullptr)
*(cl_context *)param_value = make_context(0);
return CL_SUCCESS;
}
/**
* Stub implementation of clGetDeviceInfo that just returns the first platform.
*/
static cl_int clGetDeviceInfo_platform(
cl_device_id id,
cl_device_info param_name,
size_t param_value_size,
void *param_value,
size_t *param_value_size_ret,
int num_calls)
{
(void) id;
(void) num_calls;
TEST_ASSERT_EQUAL_HEX(CL_DEVICE_PLATFORM, param_name);
TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_platform_id));
if (param_value_size_ret != nullptr)
*param_value_size_ret = sizeof(cl_platform_id);
if (param_value != nullptr)
*(cl_platform_id *) param_value = make_platform_id(0);
return CL_SUCCESS;
}
/**
* Stub implementation of clGetContextInfo that just returns the first device.
*/
static cl_int clGetContextInfo_device(
cl_context id,
cl_context_info param_name,
size_t param_value_size,
void *param_value,
size_t *param_value_size_ret,
int num_calls)
{
(void) id;
(void) num_calls;
TEST_ASSERT_EQUAL_HEX(CL_CONTEXT_DEVICES, param_name);
TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_device_id));
if (param_value_size_ret != nullptr)
*param_value_size_ret = sizeof(cl_device_id);
if (param_value != nullptr)
*(cl_device_id *) param_value = make_device_id(0);
return CL_SUCCESS;
}
/**
* Stub implementation of clGetPlatformInfo that returns a specific version.
* It also checks that the id is the zeroth platform.
*/
static cl_int clGetPlatformInfo_version(
cl_platform_id id,
cl_platform_info param_name,
size_t param_value_size,
void *param_value,
size_t *param_value_size_ret,
const char *version)
{
size_t bytes = strlen(version) + 1;
TEST_ASSERT_NOT_NULL(id);
TEST_ASSERT_EQUAL_PTR(make_platform_id(0), id);
TEST_ASSERT_EQUAL_HEX(CL_PLATFORM_VERSION, param_name);
TEST_ASSERT(param_value == nullptr || param_value_size >= bytes);
if (param_value_size_ret != nullptr)
*param_value_size_ret = bytes;
if (param_value != nullptr)
strcpy((char *) param_value, version);
return CL_SUCCESS;
}
/**
* A stub for clGetPlatformInfo that will only support querying
* CL_PLATFORM_VERSION, and will return version 1.1.
*/
static cl_int clGetPlatformInfo_version_1_1(
cl_platform_id id,
cl_platform_info param_name,
size_t param_value_size,
void *param_value,
size_t *param_value_size_ret,
int num_calls)
{
(void) num_calls;
return clGetPlatformInfo_version(
id, param_name, param_value_size, param_value,
param_value_size_ret, "OpenCL 1.1 Mock");
}
/**
* A stub for clGetPlatformInfo that will only support querying
* CL_PLATFORM_VERSION, and will return version 1.2.
*/
static cl_int clGetPlatformInfo_version_1_2(
cl_platform_id id,
cl_platform_info param_name,
size_t param_value_size,
void *param_value,
size_t *param_value_size_ret,
int num_calls)
{
(void) num_calls;
return clGetPlatformInfo_version(
id, param_name, param_value_size, param_value,
param_value_size_ret, "OpenCL 1.2 Mock");
}
/**
* A stub for clGetPlatformInfo that will only support querying
* CL_PLATFORM_VERSION, and will return version 2.0.
*/
static cl_int clGetPlatformInfo_version_2_0(
cl_platform_id id,
cl_platform_info param_name,
size_t param_value_size,
void *param_value,
size_t *param_value_size_ret,
int num_calls)
{
(void) num_calls;
return clGetPlatformInfo_version(
id, param_name, param_value_size, param_value,
param_value_size_ret, "OpenCL 2.0 Mock");
}
#if CL_HPP_TARGET_OPENCL_VERSION >= 300
/**
* A stub for clGetPlatformInfo that will only support querying
* CL_PLATFORM_VERSION, and will return version 3.0.
*/
static cl_int clGetPlatformInfo_version_3_0(
cl_platform_id id,
cl_platform_info param_name,
size_t param_value_size,
void *param_value,
size_t *param_value_size_ret,
int num_calls)
{
(void) num_calls;
return clGetPlatformInfo_version(
id, param_name, param_value_size, param_value,
param_value_size_ret, "OpenCL 3.0 Mock");
}
#endif
/* Simulated reference counts. The table points to memory held by the caller.
* This makes things simpler in the common case of only one object to be
* reference counted.
*/
class RefcountTable
{
private:
size_t n; // number of objects
void * const *objects; // object IDs
int *refcounts; // current refcounts
size_t find(void *object)
{
size_t idx = 0;
while (idx < n && objects[idx] != object)
idx++;
TEST_ASSERT(idx < n);
TEST_ASSERT(refcounts[idx] > 0); // otherwise object has been destroyed
return idx;
}
public:
RefcountTable() : n(0), objects(nullptr), refcounts(nullptr) {}
void init(size_t n, void * const *objects, int *refcounts)
{
this->n = n;
this->objects = objects;
this->refcounts = refcounts;
}
void reset()
{
init(0, nullptr, nullptr);
}
cl_int retain(void *object)
{
size_t idx = find(object);
++refcounts[idx];
return CL_SUCCESS;
}
cl_int release(void *object)
{
size_t idx = find(object);
--refcounts[idx];
return CL_SUCCESS;
}
};
/* Stubs for retain/release calls that track reference counts. The stubs
* check that the reference count never becomes negative and that a zero
* reference count is never incremented.
*
* Use the prepareRefcount* calls to set up the global variables first.
*/
#define MAKE_REFCOUNT_STUBS(cl_type, retainfunc, releasefunc, table) \
static RefcountTable table; \
static cl_int retainfunc ## _refcount(cl_type object, int num_calls) \
{ \
(void) num_calls; \
return table.retain(object); \
} \
static cl_int releasefunc ## _refcount(cl_type object, int num_calls) \
{ \
(void) num_calls; \
return table.release(object); \
} \
static void prepare_ ## table(size_t n, cl_type const *objects, int *refcounts) \
{ \
table.init(n, (void * const *) objects, refcounts); \
retainfunc ## _StubWithCallback(retainfunc ## _refcount); \
releasefunc ## _StubWithCallback(releasefunc ## _refcount); \
}
MAKE_REFCOUNT_STUBS(cl_program, clRetainProgram, clReleaseProgram, programRefcounts)
MAKE_REFCOUNT_STUBS(cl_command_queue, clRetainCommandQueue, clReleaseCommandQueue, commandQueueRefcounts)
MAKE_REFCOUNT_STUBS(cl_device_id, clRetainDevice, clReleaseDevice, deviceRefcounts)
MAKE_REFCOUNT_STUBS(cl_context, clRetainContext, clReleaseContext, contextRefcounts)
MAKE_REFCOUNT_STUBS(cl_mem, clRetainMemObject, clReleaseMemObject, memRefcounts)
// Deactivated because unused for now.
#if defined(cl_khr_command_buffer) && 0
MAKE_REFCOUNT_STUBS(cl_command_buffer_khr, clRetainCommandBufferKHR, clReleaseCommandBufferKHR, commandBufferKhrRefcounts)
#endif
/* The indirection through MAKE_MOVE_TESTS2 with a prefix parameter is to
* prevent the simple-minded parser from Unity from identifying tests from the
* macro value.
*/
#ifdef TEST_RVALUE_REFERENCES
#define MAKE_MOVE_TESTS2(prefix, type, makeFunc, releaseFunc, pool) \
void prefix ## MoveAssign ## type ## NonNull(void) \
{ \
releaseFunc ## _ExpectAndReturn(makeFunc(0), CL_SUCCESS); \
pool[0] = std::move(pool[1]); \
TEST_ASSERT_EQUAL_PTR(makeFunc(1), pool[0]()); \
TEST_ASSERT_NULL(pool[1]()); \
} \
\
void prefix ## MoveAssign ## type ## Null(void) \
{ \
pool[0]() = nullptr; \
pool[0] = std::move(pool[1]); \
TEST_ASSERT_EQUAL_PTR(makeFunc(1), pool[0]()); \
TEST_ASSERT_NULL(pool[1]()); \
} \
\
void prefix ## MoveConstruct ## type ## NonNull(void) \
{ \
cl::type tmp(std::move(pool[0])); \
TEST_ASSERT_EQUAL_PTR(makeFunc(0), tmp()); \
TEST_ASSERT_NULL(pool[0]()); \
tmp() = nullptr; \
} \
\
void prefix ## MoveConstruct ## type ## Null(void) \
{ \
cl::type empty; \
cl::type tmp(std::move(empty)); \
TEST_ASSERT_NULL(tmp()); \
TEST_ASSERT_NULL(empty()); \
}
#else
#define MAKE_MOVE_TESTS2(prefix, type, makeFunc, releaseFunc, pool) \
void prefix ## MoveAssign ## type ## NonNull(void) {} \
void prefix ## MoveAssign ## type ## Null(void) {} \
void prefix ## MoveConstruct ## type ## NonNull(void) {} \
void prefix ## MoveConstruct ## type ## Null(void) {}
#endif // !TEST_RVALUE_REFERENCES
#define MAKE_MOVE_TESTS(type, makeFunc, releaseFunc, pool) \
MAKE_MOVE_TESTS2(test, type, makeFunc, releaseFunc, pool)
void setUp(void)
{
/* init extensions addresses with mocked functions */
#if defined(cl_khr_command_buffer)
cl::pfn_clCreateCommandBufferKHR = ::clCreateCommandBufferKHR;
cl::pfn_clFinalizeCommandBufferKHR = ::clFinalizeCommandBufferKHR;
cl::pfn_clRetainCommandBufferKHR = ::clRetainCommandBufferKHR;
cl::pfn_clReleaseCommandBufferKHR = ::clReleaseCommandBufferKHR;
cl::pfn_clGetCommandBufferInfoKHR = ::clGetCommandBufferInfoKHR;
#endif
#if defined(cl_khr_semaphore)
cl::pfn_clCreateSemaphoreWithPropertiesKHR = ::clCreateSemaphoreWithPropertiesKHR;
cl::pfn_clReleaseSemaphoreKHR = ::clReleaseSemaphoreKHR;
cl::pfn_clRetainSemaphoreKHR = ::clRetainSemaphoreKHR;
cl::pfn_clEnqueueWaitSemaphoresKHR = ::clEnqueueWaitSemaphoresKHR;
cl::pfn_clEnqueueSignalSemaphoresKHR = ::clEnqueueSignalSemaphoresKHR;
cl::pfn_clGetSemaphoreInfoKHR = ::clGetSemaphoreInfoKHR;
#endif
#if defined(cl_khr_external_semaphore)
cl::pfn_clGetSemaphoreHandleForTypeKHR = ::clGetSemaphoreHandleForTypeKHR;
#endif
#ifdef cl_khr_external_memory
cl::pfn_clEnqueueAcquireExternalMemObjectsKHR = ::clEnqueueAcquireExternalMemObjectsKHR;
cl::pfn_clEnqueueReleaseExternalMemObjectsKHR = ::clEnqueueReleaseExternalMemObjectsKHR;
#endif
#if defined(cl_ext_image_requirements_info)
cl::pfn_clGetImageRequirementsInfoEXT = ::clGetImageRequirementsInfoEXT;
#endif
#if defined(cl_ext_device_fission)
cl::pfn_clCreateSubDevicesEXT = ::clCreateSubDevicesEXT;
#endif
/* We reach directly into the objects rather than using assignment to
* avoid the reference counting functions from being called.
*/
for (int i = 0; i < POOL_MAX; i++)
{
platformPool[i]() = make_platform_id(i);
contextPool[i]() = make_context(i);
commandQueuePool[i]() = make_command_queue(i);
bufferPool[i]() = make_mem(i);
image2DPool[i]() = make_mem(i);
image3DPool[i]() = make_mem(i);
kernelPool[i]() = make_kernel(i);
programPool[i]() = make_program(i);
#if defined(cl_khr_command_buffer)
commandBufferKhrPool[i]() = make_command_buffer_khr(i);
#endif
#if defined(cl_khr_semaphore)
semaphorePool[i]() = make_semaphore_khr(i);
#endif
devicePool[i]() = make_device_id(i);
}
programRefcounts.reset();
deviceRefcounts.reset();
contextRefcounts.reset();
memRefcounts.reset();
}
void tearDown(void)
{
/* Wipe out the internal state to avoid a release call being made */
for (int i = 0; i < POOL_MAX; i++)
{
platformPool[i]() = nullptr;
contextPool[i]() = nullptr;
commandQueuePool[i]() = nullptr;
bufferPool[i]() = nullptr;
image2DPool[i]() = nullptr;
image3DPool[i]() = nullptr;
kernelPool[i]() = nullptr;
programPool[i]() = nullptr;
devicePool[i]() = nullptr;
#if defined(cl_khr_command_buffer)
commandBufferKhrPool[i]() = nullptr;
#endif
#if defined(cl_khr_semaphore)
semaphorePool[i]() = nullptr;
#endif
}
#if defined(cl_khr_command_buffer)
cl::pfn_clCreateCommandBufferKHR = nullptr;
cl::pfn_clFinalizeCommandBufferKHR = nullptr;
cl::pfn_clRetainCommandBufferKHR = nullptr;
cl::pfn_clReleaseCommandBufferKHR = nullptr;
cl::pfn_clGetCommandBufferInfoKHR = nullptr;
#endif
#if defined(cl_khr_semaphore)
cl::pfn_clCreateSemaphoreWithPropertiesKHR = nullptr;
cl::pfn_clReleaseSemaphoreKHR = nullptr;
cl::pfn_clRetainSemaphoreKHR = nullptr;
cl::pfn_clEnqueueWaitSemaphoresKHR = nullptr;
cl::pfn_clEnqueueSignalSemaphoresKHR = nullptr;
cl::pfn_clGetSemaphoreInfoKHR = nullptr;
#endif
#ifdef cl_khr_external_memory
cl::pfn_clEnqueueAcquireExternalMemObjectsKHR = nullptr;
cl::pfn_clEnqueueReleaseExternalMemObjectsKHR = nullptr;
#endif
}
/****************************************************************************
* Tests for cl::Context
****************************************************************************/
void testCopyContextNonNull(void)
{
clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
clRetainContext_ExpectAndReturn(make_context(1), CL_SUCCESS);
contextPool[0] = contextPool[1];
TEST_ASSERT_EQUAL_PTR(make_context(1), contextPool[0]());
}
void testMoveAssignContextNonNull(void);
void testMoveAssignContextNull(void);
void testMoveConstructContextNonNull(void);
void testMoveConstructContextNull(void);
MAKE_MOVE_TESTS(Context, make_context, clReleaseContext, contextPool)
/// Stub for querying CL_CONTEXT_DEVICES that returns two devices
static cl_int clGetContextInfo_testContextGetDevices(
cl_context context,
cl_context_info param_name,
size_t param_value_size,
void *param_value,
size_t *param_value_size_ret,
int num_calls)
{
(void) num_calls;
TEST_ASSERT_EQUAL_PTR(make_context(0), context);
TEST_ASSERT_EQUAL_HEX(CL_CONTEXT_DEVICES, param_name);
TEST_ASSERT(param_value == nullptr || param_value_size >= 2 * sizeof(cl_device_id));
if (param_value_size_ret != nullptr)
*param_value_size_ret = 2 * sizeof(cl_device_id);
if (param_value != nullptr)
{
cl_device_id *devices = (cl_device_id *) param_value;
devices[0] = make_device_id(0);
devices[1] = make_device_id(1);
}
return CL_SUCCESS;
}
/// Test that queried devices are not refcounted
void testContextGetDevices1_1(void)
{
clGetContextInfo_StubWithCallback(clGetContextInfo_testContextGetDevices);
clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
VECTOR_CLASS<cl::Device> devices = contextPool[0].getInfo<CL_CONTEXT_DEVICES>();
TEST_ASSERT_EQUAL(2, devices.size());
TEST_ASSERT_EQUAL_PTR(make_device_id(0), devices[0]());
TEST_ASSERT_EQUAL_PTR(make_device_id(1), devices[1]());
}
/// Test that queried devices are correctly refcounted
void testContextGetDevices1_2(void)
{
clGetContextInfo_StubWithCallback(clGetContextInfo_testContextGetDevices);
clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
clRetainDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
clRetainDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
VECTOR_CLASS<cl::Device> devices = contextPool[0].getInfo<CL_CONTEXT_DEVICES>();
TEST_ASSERT_EQUAL(2, devices.size());
TEST_ASSERT_EQUAL_PTR(make_device_id(0), devices[0]());
TEST_ASSERT_EQUAL_PTR(make_device_id(1), devices[1]());
// Prevent release in the destructor
devices[0]() = nullptr;
devices[1]() = nullptr;
}
#if !defined(__APPLE__) && !defined(__MACOS)
// This is used to get a list of all platforms, so expect two calls
// First, return to say we have two platforms
// Then return the two platform id_s
static cl_int clGetPlatformIDs_testContextFromType(
cl_uint num_entries,
cl_platform_id *platforms,
cl_uint *num_platforms,
int num_calls)
{
if (num_calls == 0)
{
TEST_ASSERT_NULL(platforms);
TEST_ASSERT_NOT_NULL(num_platforms);
*num_platforms = 2;
return CL_SUCCESS;
}
else if (num_calls == 1)
{
TEST_ASSERT_NOT_NULL(platforms);
TEST_ASSERT_EQUAL(2, num_entries);
platforms[0] = make_platform_id(0);
platforms[1] = make_platform_id(1);
return CL_SUCCESS;
}
else
{
TEST_FAIL_MESSAGE("clGetPlatformIDs called too many times");
return CL_INVALID_VALUE;
}
}
#endif
#if !defined(__APPLE__) && !defined(__MACOS)
// Expect three calls to this
// 1. Platform 1, we have no GPUs
// 2. Platform 2, we have two GPUs
// 3. Here are the two cl_device_id's
static cl_int clGetDeviceIDs_testContextFromType(
cl_platform_id platform,
cl_device_type device_type,
cl_uint num_entries,
cl_device_id *devices,
cl_uint *num_devices,
int num_calls)
{
if (num_calls == 0)
{
TEST_ASSERT_EQUAL_PTR(make_platform_id(0), platform);
TEST_ASSERT_EQUAL(CL_DEVICE_TYPE_GPU, device_type);
TEST_ASSERT_NOT_NULL(num_devices);
return CL_DEVICE_NOT_FOUND;
}
else if (num_calls == 1)
{
TEST_ASSERT_EQUAL_PTR(make_platform_id(1), platform);
TEST_ASSERT_EQUAL(CL_DEVICE_TYPE_GPU, device_type);
TEST_ASSERT_NOT_NULL(num_devices);
*num_devices = 2;
return CL_SUCCESS;
}
else if (num_calls == 2)
{
TEST_ASSERT_EQUAL_PTR(make_platform_id(1), platform);
TEST_ASSERT_EQUAL(CL_DEVICE_TYPE_GPU, device_type);
TEST_ASSERT_EQUAL(2, num_entries);
TEST_ASSERT_NOT_NULL(devices);
devices[0] = make_device_id(0);
devices[1] = make_device_id(1);
return CL_SUCCESS;
}
else
{
TEST_FAIL_MESSAGE("clGetDeviceIDs called too many times");
return CL_INVALID_VALUE;
}
}
#endif
// Stub for clCreateContextFromType
// - expect platform 1 with GPUs and non-null properties
static cl_context clCreateContextFromType_testContextFromType(
const cl_context_properties *properties,
cl_device_type device_type,
void (CL_CALLBACK *pfn_notify) (const char *errinfo,
const void *private_info,
size_t cb,
void *user_data),
void *user_data,
cl_int *errcode_ret,
int num_calls)
{
(void) pfn_notify;
(void) user_data;
(void) num_calls;
TEST_ASSERT_EQUAL(CL_DEVICE_TYPE_GPU, device_type);
#if !defined(__APPLE__) && !defined(__MACOS)
TEST_ASSERT_NOT_NULL(properties);
TEST_ASSERT_EQUAL(CL_CONTEXT_PLATFORM, properties[0]);
TEST_ASSERT_EQUAL(make_platform_id(1), properties[1]);
#else
(void) properties;
#endif
if (errcode_ret)
*errcode_ret = CL_SUCCESS;
return make_context(0);
}
void testContextFromType(void)
{
#if !defined(__APPLE__) && !defined(__MACOS)
clGetPlatformIDs_StubWithCallback(clGetPlatformIDs_testContextFromType);
clGetDeviceIDs_StubWithCallback(clGetDeviceIDs_testContextFromType);
// The opencl.hpp header will perform an extra retain here to be consistent
// with other APIs retaining runtime-owned objects before releasing them
clRetainDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
clRetainDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
// End of scope of vector of devices within constructor
clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
clReleaseDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
#endif
clCreateContextFromType_StubWithCallback(clCreateContextFromType_testContextFromType);
cl::Context context(CL_DEVICE_TYPE_GPU);
TEST_ASSERT_EQUAL_PTR(make_context(0), context());
clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
}
void testContextFromTypeNonNullProperties(void)
{
clCreateContextFromType_StubWithCallback(clCreateContextFromType_testContextFromType);
const cl_context_properties props[] = {
CL_CONTEXT_PLATFORM, (cl_context_properties)make_platform_id(1), 0 };
cl::Context context(CL_DEVICE_TYPE_GPU, props);
TEST_ASSERT_EQUAL_PTR(make_context(0), context());
clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
}
static cl_context clCreateContext_testContextNonNullProperties(
const cl_context_properties* properties,
cl_uint num_devices,
const cl_device_id* devices,
void (CL_CALLBACK *pfn_notify) (const char *errinfo, const void *private_info, size_t cb, void *user_data),
void *user_data,
cl_int *errcode_ret,
int num_calls)
{
(void) pfn_notify;
(void) user_data;
(void) num_calls;
TEST_ASSERT_NOT_NULL(properties);
TEST_ASSERT_GREATER_THAN(0, num_devices);
for (int i = 0; i < (int)num_devices; i++) {
TEST_ASSERT_EQUAL(make_device_id(i), devices[i]);
}
if (errcode_ret != nullptr)
*errcode_ret = CL_SUCCESS;
return make_context(0);
}
void testContextWithDeviceNonNullProperties(void)
{
clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
clCreateContext_StubWithCallback(clCreateContext_testContextNonNullProperties);
clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
const cl_context_properties props[] = {
CL_CONTEXT_PLATFORM, (cl_context_properties)make_platform_id(0), 0 };
cl::Device device = cl::Device(make_device_id(0));
cl::Context context(device, props);
TEST_ASSERT_EQUAL_PTR(make_context(0), context());
}
void testContextWithDevicesNonNullProperties(void)
{
clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
clCreateContext_StubWithCallback(clCreateContext_testContextNonNullProperties);
clReleaseContext_ExpectAndReturn(make_context(0), CL_SUCCESS);
const cl_context_properties props[] = {
CL_CONTEXT_PLATFORM, (cl_context_properties)make_platform_id(0), 0 };
cl::Device device0 = cl::Device(make_device_id(0));
cl::Device device1 = cl::Device(make_device_id(1));
cl::Context context({device0, device1}, props);
TEST_ASSERT_EQUAL_PTR(make_context(0), context());
}
/****************************************************************************
* Tests for cl::CommandQueue
****************************************************************************/
void testMoveAssignCommandQueueNonNull(void);
void testMoveAssignCommandQueueNull(void);
void testMoveConstructCommandQueueNonNull(void);
void testMoveConstructCommandQueueNull(void);
MAKE_MOVE_TESTS(CommandQueue, make_command_queue, clReleaseCommandQueue, commandQueuePool)
// Stub for clGetCommandQueueInfo that returns context 0
static cl_int clGetCommandQueueInfo_testCommandQueueGetContext(
cl_command_queue command_queue,
cl_command_queue_info param_name,
size_t param_value_size,
void *param_value,
size_t *param_value_size_ret,
int num_calls)
{
(void) num_calls;
TEST_ASSERT_EQUAL_PTR(make_command_queue(0), command_queue);
TEST_ASSERT_EQUAL_HEX(CL_QUEUE_CONTEXT, param_name);
TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_context));
if (param_value_size_ret != nullptr)
*param_value_size_ret = sizeof(cl_context);
if (param_value != nullptr)
*(cl_context *) param_value = make_context(0);
return CL_SUCCESS;
}
void testCommandQueueGetContext(void)
{
cl_context expected = make_context(0);
int refcount = 1;
clGetCommandQueueInfo_StubWithCallback(clGetCommandQueueInfo_testCommandQueueGetContext);
prepare_contextRefcounts(1, &expected, &refcount);
cl::Context ctx = commandQueuePool[0].getInfo<CL_QUEUE_CONTEXT>();
TEST_ASSERT_EQUAL_PTR(expected, ctx());
TEST_ASSERT_EQUAL(2, refcount);
ctx() = nullptr;
}
// Stub for clGetCommandQueueInfo that returns device 0
static cl_int clGetCommandQueueInfo_testCommandQueueGetDevice(
cl_command_queue command_queue,
cl_command_queue_info param_name,
size_t param_value_size,
void *param_value,
size_t *param_value_size_ret,
int num_calls)
{
(void) num_calls;
TEST_ASSERT_EQUAL_PTR(make_command_queue(0), command_queue);
TEST_ASSERT_EQUAL_HEX(CL_QUEUE_DEVICE, param_name);
TEST_ASSERT(param_value == nullptr || param_value_size >= sizeof(cl_device_id));
if (param_value_size_ret != nullptr)
*param_value_size_ret = sizeof(cl_device_id);
if (param_value != nullptr)
*(cl_device_id *) param_value = make_device_id(0);
return CL_SUCCESS;
}
void testCommandQueueGetDevice1_1(void)
{
cl_device_id expected = make_device_id(0);
clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
clGetCommandQueueInfo_StubWithCallback(clGetCommandQueueInfo_testCommandQueueGetDevice);
cl::Device device = commandQueuePool[0].getInfo<CL_QUEUE_DEVICE>();
TEST_ASSERT_EQUAL_PTR(expected, device());
device() = nullptr;
}
void testCommandQueueGetDevice1_2(void)
{
cl_device_id expected = make_device_id(0);
int refcount = 1;
clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
clGetCommandQueueInfo_StubWithCallback(clGetCommandQueueInfo_testCommandQueueGetDevice);
prepare_deviceRefcounts(1, &expected, &refcount);
cl::Device device = commandQueuePool[0].getInfo<CL_QUEUE_DEVICE>();
TEST_ASSERT_EQUAL_PTR(expected, device());
TEST_ASSERT_EQUAL(2, refcount);
device() = nullptr;
}
#if CL_HPP_TARGET_OPENCL_VERSION < 200
// stub for clCreateCommandQueue - returns queue zero
static cl_command_queue clCreateCommandQueue_testCommandQueueFromSpecifiedContext(
cl_context context,
cl_device_id device,
cl_command_queue_properties properties,
cl_int *errcode_ret,
int num_calls)
{
(void) num_calls;
TEST_ASSERT_EQUAL_PTR(make_context(0), context);
TEST_ASSERT_EQUAL_PTR(make_device_id(0), device);
TEST_ASSERT(properties == 0);
if (errcode_ret != nullptr)
*errcode_ret = CL_SUCCESS;
return make_command_queue(0);
}
#else
// stub for clCreateCommandQueueWithProperties - returns queue zero
static cl_command_queue clCreateCommandQueueWithProperties_testCommandQueueFromSpecifiedContext(
cl_context context,
cl_device_id device,
const cl_queue_properties *properties,
cl_int *errcode_ret,
int num_calls)
{
(void)num_calls;
TEST_ASSERT_EQUAL_PTR(make_context(0), context);
TEST_ASSERT_EQUAL_PTR(make_device_id(0), device);
TEST_ASSERT(properties[0] == CL_QUEUE_PROPERTIES);
TEST_ASSERT(properties[1] == 0);
if (errcode_ret != nullptr)
*errcode_ret = CL_SUCCESS;
return make_command_queue(0);
}
#endif // #if CL_HPP_TARGET_OPENCL_VERSION < 200
void testCommandQueueFromSpecifiedContext(void)
{
cl_command_queue expected = make_command_queue(0);
cl_context expected_context = make_context(0);
cl_device_id expected_device = make_device_id(0);
int context_refcount = 1;
int device_refcount = 1;
prepare_contextRefcounts(1, &expected_context, &context_refcount);
prepare_deviceRefcounts(1, &expected_device, &device_refcount);
// This is the context we will pass in to test
cl::Context context = contextPool[0];
// Assumes the context contains the fi rst device
clGetContextInfo_StubWithCallback(clGetContextInfo_device);
#if CL_HPP_TARGET_OPENCL_VERSION >= 200
clCreateCommandQueueWithProperties_StubWithCallback(clCreateCommandQueueWithProperties_testCommandQueueFromSpecifiedContext);
#else // #if CL_HPP_TARGET_OPENCL_VERSION >= 200
clCreateCommandQueue_StubWithCallback(clCreateCommandQueue_testCommandQueueFromSpecifiedContext);
#endif // #if CL_HPP_TARGET_OPENCL_VERSION >= 200
clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
#if CL_HPP_TARGET_OPENCL_VERSION >= 200
clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_2_0);
#else // #if CL_HPP_TARGET_OPENCL_VERSION >= 200
clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
#endif // #if CL_HPP_TARGET_OPENCL_VERSION >= 200
clReleaseCommandQueue_ExpectAndReturn(expected, CL_SUCCESS);
cl::CommandQueue queue(context);
TEST_ASSERT_EQUAL_PTR(expected, queue());
// Context not destroyed yet
TEST_ASSERT_EQUAL(2, context_refcount);
// Device object destroyed at end of scope
TEST_ASSERT_EQUAL(1, device_refcount);
}
/****************************************************************************
* Tests for cl::Device
****************************************************************************/
void testCopyDeviceNonNull1_1(void)
{
clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
cl::Device d0(make_device_id(0));
cl::Device d1(make_device_id(1));
d0 = d1;
}
void testCopyDeviceNonNull1_2(void)
{
clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_2);
clReleaseDevice_ExpectAndReturn(make_device_id(0), CL_SUCCESS);
clRetainDevice_ExpectAndReturn(make_device_id(1), CL_SUCCESS);
cl::Device d0(make_device_id(0));
cl::Device d1(make_device_id(1));
d0 = d1;
// Prevent destructor from interfering with the test
d0() = nullptr;
d1() = nullptr;
}
void testCopyDeviceFromNull1_1(void)
{
clGetDeviceInfo_StubWithCallback(clGetDeviceInfo_platform);
clGetPlatformInfo_StubWithCallback(clGetPlatformInfo_version_1_1);
// No other calls expected
cl::Device d(make_device_id(0));
d = cl::Device();
}
void testCopyDeviceFromNull1_2(void)
{