aboutsummaryrefslogtreecommitdiff
path: root/test_conformance/api/test_queries.cpp
blob: f07401077ee874833a0e6420445d2755f1c96ba0 (plain)
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
//
// Copyright (c) 2017 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "testBase.h"
#include "harness/imageHelpers.h"
#include "harness/propertyHelpers.h"
#include <stdlib.h>
#include <ctype.h>
#include <algorithm>
#include <vector>

int test_get_platform_info(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
{
    cl_platform_id platform;
    cl_int error;
    char buffer[ 16384 ];
    size_t length;

    // Get the platform to use
    error = clGetPlatformIDs(1, &platform, NULL);
    test_error( error, "Unable to get platform" );

    // Platform profile should either be FULL_PROFILE or EMBEDDED_PROFILE
    error = clGetPlatformInfo(platform,  CL_PLATFORM_PROFILE, sizeof( buffer ), buffer, &length );
    test_error( error, "Unable to get platform profile string" );

    log_info("Returned CL_PLATFORM_PROFILE %s.\n", buffer);

    if( strcmp( buffer, "FULL_PROFILE" ) != 0 && strcmp( buffer, "EMBEDDED_PROFILE" ) != 0 )
    {
        log_error( "ERROR: Returned platform profile string is not a valid string by OpenCL 1.2! (Returned: %s)\n", buffer );
        return -1;
    }
    if( strlen( buffer )+1 != length )
    {
        log_error( "ERROR: Returned length of profile string is incorrect (actual length: %d, returned length: %d)\n",
                  (int)strlen( buffer )+1, (int)length );
        return -1;
    }

    // Check just length return
    error = clGetPlatformInfo(platform,  CL_PLATFORM_PROFILE, 0, NULL, &length );
    test_error( error, "Unable to get platform profile length" );
    if( strlen( (char *)buffer )+1 != length )
    {
        log_error( "ERROR: Returned length of profile string is incorrect (actual length: %d, returned length: %d)\n",
                  (int)strlen( (char *)buffer )+1, (int)length );
        return -1;
    }


    // Platform version should fit the regex "OpenCL *[0-9]+\.[0-9]+"
    error = clGetPlatformInfo(platform,  CL_PLATFORM_VERSION, sizeof( buffer ), buffer, &length );
    test_error( error, "Unable to get platform version string" );

    log_info("Returned CL_PLATFORM_VERSION %s.\n", buffer);

    if( memcmp( buffer, "OpenCL ", strlen( "OpenCL " ) ) != 0 )
    {
        log_error( "ERROR: Initial part of platform version string does not match required format! (returned: %s)\n", (char *)buffer );
        return -1;
    }
    char *p1 = (char *)buffer + strlen( "OpenCL " );
    while( *p1 == ' ' )
        p1++;
    char *p2 = p1;
    while( isdigit( *p2 ) )
        p2++;
    if( *p2 != '.' )
    {
        log_error( "ERROR: Numeric part of platform version string does not match required format! (returned: %s)\n", (char *)buffer );
        return -1;
    }
    char *p3 = p2 + 1;
    while( isdigit( *p3 ) )
        p3++;
    if( *p3 != ' ' )
    {
        log_error( "ERROR: space expected after minor version number! (returned: %s)\n", (char *)buffer );
        return -1;
    }
    *p2 = ' '; // Put in a space for atoi below.
    p2++;

    // make sure it is null terminated
    for( ; p3 != buffer + length; p3++ )
        if( *p3 == '\0' )
            break;
    if( p3 == buffer + length )
    {
        log_error( "ERROR: platform version string is not NUL terminated!\n" );
        return -1;
    }

    int major = atoi( p1 );
    int minor = atoi( p2 );
    int minor_revision = 2;
    if( major * 10 + minor < 10 + minor_revision )
    {
        log_error( "ERROR: OpenCL profile version returned is less than 1.%d!\n", minor_revision );
        return -1;
    }

    // Sanity checks on the returned values
    if( length != strlen( (char *)buffer ) + 1)
    {
        log_error( "ERROR: Returned length of version string does not match actual length (actual: %d, returned: %d)\n", (int)strlen( (char *)buffer )+1, (int)length );
        return -1;
    }

    // Check just length
    error = clGetPlatformInfo(platform,  CL_PLATFORM_VERSION, 0, NULL, &length );
    test_error( error, "Unable to get platform version length" );
    if( length != strlen( (char *)buffer )+1 )
    {
        log_error( "ERROR: Returned length of version string does not match actual length (actual: %d, returned: %d)\n", (int)strlen( buffer )+1, (int)length );
        return -1;
    }

    return 0;
}

template <typename T>
int sampler_param_test(cl_sampler sampler, cl_sampler_info param_name,
                       T expected, const char *name)
{
    size_t size;
    T val;
    int error = clGetSamplerInfo(sampler, param_name, sizeof(val), &val, &size);
    test_error(error, "Unable to get sampler info");
    if (val != expected)
    {
        test_fail("ERROR: Sampler %s did not validate!\n", name);
    }
    if (size != sizeof(val))
    {
        test_fail("ERROR: Returned size of sampler %s does not validate! "
                  "(expected %d, got %d)\n",
                  name, (int)sizeof(val), (int)size);
    }
    return 0;
}

static cl_int normalized_coord_values[] = { CL_TRUE, CL_FALSE };
static cl_addressing_mode addressing_mode_values[] = {
    CL_ADDRESS_NONE, CL_ADDRESS_CLAMP_TO_EDGE, CL_ADDRESS_CLAMP,
    CL_ADDRESS_REPEAT, CL_ADDRESS_MIRRORED_REPEAT
};
static cl_filter_mode filter_mode_values[] = { CL_FILTER_NEAREST,
                                               CL_FILTER_LINEAR };

int test_sampler_params(cl_device_id deviceID, cl_context context,
                        bool is_compatibility, size_t norm_coord_num,
                        size_t addr_mod_num, size_t filt_mod_num)
{
    cl_uint refCount;
    size_t size;
    int error;

    clSamplerWrapper sampler;
    cl_sampler_properties properties[] = {
        CL_SAMPLER_NORMALIZED_COORDS,
        normalized_coord_values[norm_coord_num],
        CL_SAMPLER_ADDRESSING_MODE,
        addressing_mode_values[addr_mod_num],
        CL_SAMPLER_FILTER_MODE,
        filter_mode_values[filt_mod_num],
        0
    };

    if (is_compatibility)
    {
        sampler =
            clCreateSampler(context, normalized_coord_values[norm_coord_num],
                            addressing_mode_values[addr_mod_num],
                            filter_mode_values[filt_mod_num], &error);
        test_error(error, "Unable to create sampler to test with");
    }
    else
    {
        sampler = clCreateSamplerWithProperties(context, properties, &error);
        test_error(error, "Unable to create sampler to test with");
    }

    error = clGetSamplerInfo(sampler, CL_SAMPLER_REFERENCE_COUNT,
                             sizeof(refCount), &refCount, &size);
    test_error(error, "Unable to get sampler ref count");
    test_assert_error(size == sizeof(refCount),
                      "Returned size of sampler refcount does not validate!\n");

    error = sampler_param_test(sampler, CL_SAMPLER_CONTEXT, context, "context");
    test_error(error, "param checking failed");

    error = sampler_param_test(sampler, CL_SAMPLER_ADDRESSING_MODE,
                               addressing_mode_values[addr_mod_num],
                               "addressing mode");
    test_error(error, "param checking failed");

    error = sampler_param_test(sampler, CL_SAMPLER_FILTER_MODE,
                               filter_mode_values[filt_mod_num], "filter mode");
    test_error(error, "param checking failed");

    error = sampler_param_test(sampler, CL_SAMPLER_NORMALIZED_COORDS,
                               normalized_coord_values[norm_coord_num],
                               "normalized coords");
    test_error(error, "param checking failed");

    Version version = get_device_cl_version(deviceID);
    if (version >= Version(3, 0))
    {
        std::vector<cl_sampler_properties> test_properties(
            properties, properties + ARRAY_SIZE(properties));

        std::vector<cl_sampler_properties> check_properties;
        size_t set_size;

        error = clGetSamplerInfo(sampler, CL_SAMPLER_PROPERTIES, 0, NULL,
                                 &set_size);
        test_error(
            error,
            "clGetSamplerInfo failed asking for CL_SAMPLER_PROPERTIES size.");

        if (is_compatibility)
        {
            if (set_size != 0)
            {
                log_error(
                    "ERROR: CL_SAMPLER_PROPERTIES size is %d, expected 0\n",
                    set_size);
                return TEST_FAIL;
            }
        }
        else
        {
            if (set_size
                != test_properties.size() * sizeof(cl_sampler_properties))
            {
                log_error(
                    "ERROR: CL_SAMPLER_PROPERTIES size is %d, expected %d.\n",
                    set_size,
                    test_properties.size() * sizeof(cl_sampler_properties));
                return TEST_FAIL;
            }

            cl_uint number_of_props = set_size / sizeof(cl_sampler_properties);
            check_properties.resize(number_of_props);
            error = clGetSamplerInfo(sampler, CL_SAMPLER_PROPERTIES, set_size,
                                     check_properties.data(), 0);
            test_error(
                error,
                "clGetSamplerInfo failed asking for CL_SAMPLER_PROPERTIES.");

            error = compareProperties(check_properties, test_properties);
            test_error(error, "checkProperties mismatch.");
        }
    }
    return 0;
}

int get_sampler_info_params(cl_device_id deviceID, cl_context context,
                            bool is_compatibility)
{
    for (size_t norm_coord_num = 0;
         norm_coord_num < ARRAY_SIZE(normalized_coord_values); norm_coord_num++)
    {
        for (size_t addr_mod_num = 0;
             addr_mod_num < ARRAY_SIZE(addressing_mode_values); addr_mod_num++)
        {
            if ((normalized_coord_values[norm_coord_num] == CL_FALSE)
                && ((addressing_mode_values[addr_mod_num] == CL_ADDRESS_REPEAT)
                    || (addressing_mode_values[addr_mod_num]
                        == CL_ADDRESS_MIRRORED_REPEAT)))
            {
                continue;
            }
            for (size_t filt_mod_num = 0;
                 filt_mod_num < ARRAY_SIZE(filter_mode_values); filt_mod_num++)
            {
                int err = test_sampler_params(deviceID, context,
                                              is_compatibility, norm_coord_num,
                                              addr_mod_num, filt_mod_num);
                test_error(err, "testing clGetSamplerInfo params failed");
            }
        }
    }
    return 0;
}
int test_get_sampler_info(cl_device_id deviceID, cl_context context,
                          cl_command_queue queue, int num_elements)
{
    int error;
    PASSIVE_REQUIRE_IMAGE_SUPPORT(deviceID)

    error = get_sampler_info_params(deviceID, context, false);
    test_error(error, "Test Failed");

    return 0;
}

int test_get_sampler_info_compatibility(cl_device_id deviceID,
                                        cl_context context,
                                        cl_command_queue queue,
                                        int num_elements)
{
    int error;
    PASSIVE_REQUIRE_IMAGE_SUPPORT(deviceID)

    error = get_sampler_info_params(deviceID, context, true);
    test_error(error, "Test Failed");

    return 0;
}

template <typename T>
int command_queue_param_test(cl_command_queue queue,
                             cl_command_queue_info param_name, T expected,
                             const char *name)
{
    size_t size;
    T val;
    int error =
        clGetCommandQueueInfo(queue, param_name, sizeof(val), &val, &size);
    test_error(error, "Unable to get command queue info");
    if (val != expected)
    {
        test_fail("ERROR: Command queue %s did not validate!\n", name);
    }
    if (size != sizeof(val))
    {
        test_fail("ERROR: Returned size of command queue %s does not validate! "
                  "(expected %d, got %d)\n",
                  name, (int)sizeof(val), (int)size);
    }
    return 0;
}

int check_get_command_queue_info_params(cl_device_id deviceID,
                                        cl_context context,
                                        bool is_compatibility)
{
    const cl_command_queue_properties host_optional[] = {
        CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE,
        CL_QUEUE_PROFILING_ENABLE | CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE
    };

    const cl_command_queue_properties device_required[] = {
        CL_QUEUE_ON_DEVICE | CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE,
        CL_QUEUE_PROFILING_ENABLE | CL_QUEUE_ON_DEVICE
            | CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE,
        CL_QUEUE_ON_DEVICE | CL_QUEUE_ON_DEVICE_DEFAULT
            | CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE,
        CL_QUEUE_PROFILING_ENABLE | CL_QUEUE_ON_DEVICE
            | CL_QUEUE_ON_DEVICE_DEFAULT
            | CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE
    };

    const size_t host_optional_size = ARRAY_SIZE(host_optional);
    const size_t device_required_size = ARRAY_SIZE(device_required);

    Version version = get_device_cl_version(deviceID);

    const cl_device_info host_queue_query = version >= Version(2, 0)
        ? CL_DEVICE_QUEUE_ON_HOST_PROPERTIES
        : CL_DEVICE_QUEUE_PROPERTIES;

    cl_queue_properties host_queue_props = 0;
    int error =
        clGetDeviceInfo(deviceID, host_queue_query, sizeof(host_queue_props),
                        &host_queue_props, NULL);
    test_error(error, "clGetDeviceInfo failed");
    log_info("CL_DEVICE_QUEUE_ON_HOST_PROPERTIES is %d\n", host_queue_props);

    cl_queue_properties device_queue_props = 0;
    if (version >= Version(2, 0))
    {
        error = clGetDeviceInfo(deviceID, CL_DEVICE_QUEUE_ON_DEVICE_PROPERTIES,
                                sizeof(device_queue_props), &device_queue_props,
                                NULL);
        test_error(error, "clGetDeviceInfo failed");
        log_info("CL_DEVICE_QUEUE_ON_DEVICE_PROPERTIES is %d\n",
                 device_queue_props);
    }

    bool out_of_order_supported =
        host_queue_props & CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE;

    bool on_device_supported =
        (version >= Version(2, 0) && version < Version(3, 0))
        || (version >= Version(3, 0) && device_queue_props != 0);

    // test device queues if the device and the API under test support it
    bool test_on_device = on_device_supported && !is_compatibility;

    std::vector<cl_queue_properties> queue_props{ 0,
                                                  CL_QUEUE_PROFILING_ENABLE };

    if (out_of_order_supported)
    {
        queue_props.insert(queue_props.end(), &host_optional[0],
                           &host_optional[host_optional_size]);
    };

    cl_queue_properties queue_props_arg[] = { CL_QUEUE_PROPERTIES, 0, 0 };

    if (test_on_device)
    {
        queue_props.insert(queue_props.end(), &device_required[0],
                           &device_required[device_required_size]);
    };

    for (cl_queue_properties props : queue_props)
    {

        queue_props_arg[1] = props;

        clCommandQueueWrapper queue;
        if (is_compatibility)
        {
            queue = clCreateCommandQueue(context, deviceID, props, &error);
            test_error(error, "Unable to create command queue to test with");
        }
        else
        {
            queue = clCreateCommandQueueWithProperties(context, deviceID,
                                                       queue_props_arg, &error);
            test_error(error, "Unable to create command queue to test with");
        }

        cl_uint refCount;
        size_t size;
        error = clGetCommandQueueInfo(queue, CL_QUEUE_REFERENCE_COUNT,
                                      sizeof(refCount), &refCount, &size);
        test_error(error, "Unable to get command queue reference count");
        test_assert_error(size == sizeof(refCount),
                          "Returned size of command queue reference count does "
                          "not validate!\n");

        error = command_queue_param_test(queue, CL_QUEUE_CONTEXT, context,
                                         "context");
        test_error(error, "param checking failed");

        error = command_queue_param_test(queue, CL_QUEUE_DEVICE, deviceID,
                                         "deviceID");
        test_error(error, "param checking failed");

        error = command_queue_param_test(queue, CL_QUEUE_PROPERTIES,
                                         queue_props_arg[1], "properties");
        test_error(error, "param checking failed");
    }
    return 0;
}

int test_get_command_queue_info(cl_device_id deviceID, cl_context context,
                                cl_command_queue ignoreQueue, int num_elements)
{
    int error = check_get_command_queue_info_params(deviceID, context, false);
    test_error(error, "Test Failed");
    return 0;
}

int test_get_command_queue_info_compatibility(cl_device_id deviceID,
                                              cl_context context,
                                              cl_command_queue ignoreQueue,
                                              int num_elements)
{
    int error = check_get_command_queue_info_params(deviceID, context, true);
    test_error(error, "Test Failed");
    return 0;
}

int test_get_context_info(cl_device_id deviceID, cl_context context, cl_command_queue ignoreQueue, int num_elements)
{
    int error;
    size_t size;
    cl_context_properties props;

    error = clGetContextInfo( context, CL_CONTEXT_PROPERTIES, sizeof( props ), &props, &size );
    test_error( error, "Unable to get context props" );

    if (size == 0) {
        // Valid size
        return 0;
    } else if (size == sizeof(cl_context_properties)) {
        // Data must be NULL
        if (props != 0) {
            log_error("ERROR: Returned properties is no NULL.\n");
            return -1;
        }
        // Valid data and size
        return 0;
    }
    // Size was not 0 or 1
    log_error( "ERROR: Returned size of context props is not valid! (expected 0 or %d, got %d)\n",
              (int)sizeof(cl_context_properties), (int)size );
    return -1;
}

#define TEST_MEM_OBJECT_PARAM( mem, paramName, val, expected, name, type, cast )    \
error = clGetMemObjectInfo( mem, paramName, sizeof( val ), &val, &size );        \
test_error( error, "Unable to get mem object " name );                            \
if( val != expected )                                                                \
{                                                                                    \
log_error( "ERROR: Mem object " name " did not validate! (expected " type ", got " type ")\n", (cast)(expected), (cast)val );    \
return -1;                                                                        \
}            \
if( size != sizeof( val ) )                \
{                                        \
log_error( "ERROR: Returned size of mem object " name " does not validate! (expected %d, got %d)\n", (int)sizeof( val ), (int)size );    \
return -1;    \
}

void CL_CALLBACK mem_obj_destructor_callback( cl_mem, void *data )
{
    free( data );
}

#define TEST_DEVICE_PARAM( device, paramName, val, name, type, cast )    \
error = clGetDeviceInfo( device, paramName, sizeof( val ), &val, &size );        \
test_error( error, "Unable to get device " name );                            \
if( size != sizeof( val ) )                \
{                                        \
log_error( "ERROR: Returned size of device " name " does not validate! (expected %d, got %d)\n", (int)sizeof( val ), (int)size );    \
return -1;    \
}                \
log_info( "\tReported device " name " : " type "\n", (cast)val );

#define TEST_DEVICE_PARAM_MEM( device, paramName, val, name, type, div )    \
error = clGetDeviceInfo( device, paramName, sizeof( val ), &val, &size );        \
test_error( error, "Unable to get device " name );                            \
if( size != sizeof( val ) )                \
{                                        \
log_error( "ERROR: Returned size of device " name " does not validate! (expected %d, got %d)\n", (int)sizeof( val ), (int)size );    \
return -1;    \
}                \
log_info( "\tReported device " name " : " type "\n", (int)( val / div ) );

int test_get_device_info(cl_device_id deviceID, cl_context context, cl_command_queue ignoreQueue, int num_elements)
{
    int error;
    size_t size;

    cl_uint vendorID;
    TEST_DEVICE_PARAM( deviceID, CL_DEVICE_VENDOR_ID, vendorID, "vendor ID", "0x%08x", int )

    char extensions[ 10240 ];
    error = clGetDeviceInfo( deviceID, CL_DEVICE_EXTENSIONS, sizeof( extensions ), &extensions, &size );
    test_error( error, "Unable to get device extensions" );
    if( size != strlen( extensions ) + 1 )
    {
        log_error( "ERROR: Returned size of device extensions does not validate! (expected %d, got %d)\n", (int)( strlen( extensions ) + 1 ), (int)size );
        return -1;
    }
    log_info( "\tReported device extensions: %s \n", extensions );

    cl_uint preferred;
    TEST_DEVICE_PARAM( deviceID, CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, preferred, "preferred vector char width", "%d", int )
    TEST_DEVICE_PARAM( deviceID, CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT, preferred, "preferred vector short width", "%d", int )
    TEST_DEVICE_PARAM( deviceID, CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, preferred, "preferred vector int width", "%d", int )
    TEST_DEVICE_PARAM( deviceID, CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG, preferred, "preferred vector long width", "%d", int )
    TEST_DEVICE_PARAM( deviceID, CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT, preferred, "preferred vector float width", "%d", int )
    TEST_DEVICE_PARAM( deviceID, CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE, preferred, "preferred vector double width", "%d", int )

    // Note that even if cl_khr_fp64, the preferred width for double can be non-zero.  For example, vendors
    // extensions can support double but may not support cl_khr_fp64, which implies math library support.

    cl_uint baseAddrAlign;
    TEST_DEVICE_PARAM(deviceID, CL_DEVICE_MEM_BASE_ADDR_ALIGN, baseAddrAlign,
                      "base address alignment", "%d bits", int)

    cl_uint maxDataAlign;
    TEST_DEVICE_PARAM( deviceID, CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE, maxDataAlign, "min data type alignment", "%d bytes", int )

    cl_device_mem_cache_type cacheType;
    error = clGetDeviceInfo( deviceID, CL_DEVICE_GLOBAL_MEM_CACHE_TYPE, sizeof( cacheType ), &cacheType, &size );
    test_error( error, "Unable to get device global mem cache type" );
    if( size != sizeof( cacheType ) )
    {
        log_error( "ERROR: Returned size of device global mem cache type does not validate! (expected %d, got %d)\n", (int)sizeof( cacheType ), (int)size );
        return -1;
    }
    const char *cacheTypeName = ( cacheType == CL_NONE ) ? "CL_NONE" : ( cacheType == CL_READ_ONLY_CACHE ) ? "CL_READ_ONLY_CACHE" : ( cacheType == CL_READ_WRITE_CACHE ) ? "CL_READ_WRITE_CACHE" : "<unknown>";
    log_info( "\tReported device global mem cache type: %s \n", cacheTypeName );

    cl_uint cachelineSize;
    TEST_DEVICE_PARAM( deviceID, CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE, cachelineSize, "global mem cacheline size", "%d bytes", int )

    cl_ulong cacheSize;
    TEST_DEVICE_PARAM_MEM( deviceID, CL_DEVICE_GLOBAL_MEM_CACHE_SIZE, cacheSize, "global mem cache size", "%d KB", 1024 )

    cl_ulong memSize;
    TEST_DEVICE_PARAM_MEM( deviceID, CL_DEVICE_GLOBAL_MEM_SIZE, memSize, "global mem size", "%d MB", ( 1024 * 1024 ) )

    cl_device_local_mem_type localMemType;
    error = clGetDeviceInfo( deviceID, CL_DEVICE_LOCAL_MEM_TYPE, sizeof( localMemType ), &localMemType, &size );
    test_error( error, "Unable to get device local mem type" );
    if( size != sizeof( cacheType ) )
    {
        log_error( "ERROR: Returned size of device local mem type does not validate! (expected %d, got %d)\n", (int)sizeof( localMemType ), (int)size );
        return -1;
    }
    const char *localMemTypeName = ( localMemType == CL_LOCAL ) ? "CL_LOCAL" : ( cacheType == CL_GLOBAL ) ? "CL_GLOBAL" : "<unknown>";
    log_info( "\tReported device local mem type: %s \n", localMemTypeName );


    cl_bool errSupport;
    TEST_DEVICE_PARAM( deviceID, CL_DEVICE_ERROR_CORRECTION_SUPPORT, errSupport, "error correction support", "%d", int )

    size_t timerResolution;
    TEST_DEVICE_PARAM( deviceID, CL_DEVICE_PROFILING_TIMER_RESOLUTION, timerResolution, "profiling timer resolution", "%ld nanoseconds", long )

    cl_bool endian;
    TEST_DEVICE_PARAM( deviceID, CL_DEVICE_ENDIAN_LITTLE, endian, "little endian flag", "%d", int )

    cl_bool avail;
    TEST_DEVICE_PARAM( deviceID, CL_DEVICE_AVAILABLE, avail, "available flag", "%d", int )

    cl_bool compilerAvail;
    TEST_DEVICE_PARAM( deviceID, CL_DEVICE_COMPILER_AVAILABLE, compilerAvail, "compiler available flag", "%d", int )

    char profile[ 1024 ];
    error = clGetDeviceInfo( deviceID, CL_DEVICE_PROFILE, sizeof( profile ), &profile, &size );
    test_error( error, "Unable to get device profile" );
    if( size != strlen( profile ) + 1 )
    {
        log_error( "ERROR: Returned size of device profile does not validate! (expected %d, got %d)\n", (int)( strlen( profile ) + 1 ), (int)size );
        return -1;
    }
    if( strcmp( profile, "FULL_PROFILE" ) != 0 && strcmp( profile, "EMBEDDED_PROFILE" ) != 0 )
    {
        log_error( "ERROR: Returned profile of device not FULL or EMBEDDED as required by OpenCL 1.2! (Returned %s)\n", profile );
        return -1;
    }
    log_info( "\tReported device profile: %s \n", profile );

    if (strcmp(profile, "FULL_PROFILE") == 0 && compilerAvail != CL_TRUE)
    {
        log_error("ERROR: Returned profile of device is FULL , but "
                  "CL_DEVICE_COMPILER_AVAILABLE is not CL_TRUE as required by "
                  "OpenCL 1.2!");
        return -1;
    }

    return 0;
}




static const char *sample_compile_size[2] = {
    "__kernel void sample_test(__global int *src, __global int *dst)\n"
    "{\n"
    "    int  tid = get_global_id(0);\n"
    "     dst[tid] = src[tid];\n"
    "\n"
    "}\n",
    "__kernel __attribute__((reqd_work_group_size(%d,%d,%d))) void sample_test(__global int *src, __global int *dst)\n"
    "{\n"
    "    int  tid = get_global_id(0);\n"
    "     dst[tid] = src[tid];\n"
    "\n"
    "}\n" };

int test_kernel_required_group_size(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
{
    int error;
    size_t realSize;
    size_t kernel_max_workgroup_size;
    size_t global[] = {64,14,10};
    size_t local[] = {0,0,0};

    cl_uint max_dimensions;

    error = clGetDeviceInfo(deviceID, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(max_dimensions), &max_dimensions, NULL);
    test_error(error,  "clGetDeviceInfo failed for CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS");
    log_info("Device reported CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS = %d.\n", (int)max_dimensions);

    {
        clProgramWrapper program;
        clKernelWrapper kernel;

        error = create_single_kernel_helper( context, &program, &kernel, 1, &sample_compile_size[ 0 ], "sample_test" );
        if( error != 0 )
            return error;

        error = clGetKernelWorkGroupInfo(kernel, deviceID, CL_KERNEL_WORK_GROUP_SIZE, sizeof(kernel_max_workgroup_size), &kernel_max_workgroup_size, NULL);
        test_error( error, "clGetKernelWorkGroupInfo failed for CL_KERNEL_WORK_GROUP_SIZE");
        log_info("The CL_KERNEL_WORK_GROUP_SIZE for the kernel is %d.\n", (int)kernel_max_workgroup_size);

        size_t size[ 3 ];
        error = clGetKernelWorkGroupInfo( kernel, deviceID, CL_KERNEL_COMPILE_WORK_GROUP_SIZE, sizeof( size ), size, &realSize );
        test_error( error, "Unable to get work group info" );

        if( size[ 0 ] != 0 || size[ 1 ] != 0 || size[ 2 ] != 0 )
        {
            log_error( "ERROR: Nonzero compile work group size returned for nonspecified size! (returned %d,%d,%d)\n", (int)size[0], (int)size[1], (int)size[2] );
            return -1;
        }

        if( realSize != sizeof( size ) )
        {
            log_error( "ERROR: Returned size of compile work group size not valid! (Expected %d, got %d)\n", (int)sizeof( size ), (int)realSize );
            return -1;
        }

        // Determine some local dimensions to use for the test.
        if (max_dimensions == 1) {
            error = get_max_common_work_group_size(context, kernel, global[0], &local[0]);
            test_error( error, "get_max_common_work_group_size failed");
            log_info("For global dimension %d, kernel will require local dimension %d.\n", (int)global[0], (int)local[0]);
        } else if (max_dimensions == 2) {
            error = get_max_common_2D_work_group_size(context, kernel, global, local);
            test_error( error, "get_max_common_2D_work_group_size failed");
            log_info("For global dimension %d x %d, kernel will require local dimension %d x %d.\n", (int)global[0], (int)global[1], (int)local[0], (int)local[1]);
        } else {
            error = get_max_common_3D_work_group_size(context, kernel, global, local);
            test_error( error, "get_max_common_3D_work_group_size failed");
            log_info("For global dimension %d x %d x %d, kernel will require local dimension %d x %d x %d.\n",
                     (int)global[0], (int)global[1], (int)global[2], (int)local[0], (int)local[1], (int)local[2]);
        }
    }


    {
        clProgramWrapper program;
        clKernelWrapper kernel;
        clMemWrapper in, out;
        //char source[1024];
        char *source = (char*)malloc(1024);
        source[0] = '\0';

        sprintf(source, sample_compile_size[1], local[0], local[1], local[2]);

        error = create_single_kernel_helper( context, &program, &kernel, 1, (const char**)&source, "sample_test" );
        if( error != 0 )
            return error;

        size_t size[ 3 ];
        error = clGetKernelWorkGroupInfo( kernel, deviceID, CL_KERNEL_COMPILE_WORK_GROUP_SIZE, sizeof( size ), size, &realSize );
        test_error( error, "Unable to get work group info" );

        if( size[ 0 ] != local[0] || size[ 1 ] != local[1] || size[ 2 ] != local[2] )
        {
            log_error( "ERROR: Incorrect compile work group size returned for specified size! (returned %d,%d,%d, expected %d,%d,%d)\n",
                      (int)size[0], (int)size[1], (int)size[2], (int)local[0], (int)local[1], (int)local[2]);
            return -1;
        }

        // Verify that the kernel will only execute with that size.
        in = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(cl_int)*global[0], NULL, &error);
        test_error(error, "clCreateBuffer failed");
        out = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(cl_int)*global[0], NULL, &error);
        test_error(error, "clCreateBuffer failed");

        error = clSetKernelArg(kernel, 0, sizeof(in), &in);
        test_error(error, "clSetKernelArg failed");
        error = clSetKernelArg(kernel, 1, sizeof(out), &out);
        test_error(error, "clSetKernelArg failed");

        error = clEnqueueNDRangeKernel(queue, kernel, 3, NULL, global, local, 0, NULL, NULL);
        test_error(error, "clEnqueueNDRangeKernel failed");

        error = clFinish(queue);
        test_error(error, "clFinish failed");

        log_info("kernel_required_group_size may report spurious ERRORS in the conformance log.\n");

        local[0]++;
        error = clEnqueueNDRangeKernel(queue, kernel, 3, NULL, global, local, 0, NULL, NULL);
        if (error != CL_INVALID_WORK_GROUP_SIZE) {
            log_error("Incorrect error returned for executing a kernel with the wrong required local work group size. (used %d,%d,%d, required %d,%d,%d)\n",
                      (int)local[0], (int)local[1], (int)local[2], (int)local[0]-1, (int)local[1], (int)local[2] );
            print_error(error, "Expected: CL_INVALID_WORK_GROUP_SIZE.");
            return -1;
        }

        error = clFinish(queue);
        test_error(error, "clFinish failed");

        if (max_dimensions == 1) {
            free(source);
            return 0;
        }

        local[0]--; local[1]++;
        error = clEnqueueNDRangeKernel(queue, kernel, 3, NULL, global, local, 0, NULL, NULL);
        if (error != CL_INVALID_WORK_GROUP_SIZE) {
            log_error("Incorrect error returned for executing a kernel with the wrong required local work group size. (used %d,%d,%d, required %d,%d,%d)\n",
                      (int)local[0], (int)local[1], (int)local[2], (int)local[0]-1, (int)local[1], (int)local[2]);
            print_error(error, "Expected: CL_INVALID_WORK_GROUP_SIZE.");
            return -1;
        }

        error = clFinish(queue);
        test_error(error, "clFinish failed");

        if (max_dimensions == 2) {
            free(source);
            return 0;
        }

        local[1]--; local[2]++;
        error = clEnqueueNDRangeKernel(queue, kernel, 3, NULL, global, local, 0, NULL, NULL);
        if (error != CL_INVALID_WORK_GROUP_SIZE) {
            log_error("Incorrect error returned for executing a kernel with the wrong required local work group size. (used %d,%d,%d, required %d,%d,%d)\n",
                      (int)local[0], (int)local[1], (int)local[2], (int)local[0]-1, (int)local[1], (int)local[2]);
            print_error(error, "Expected: CL_INVALID_WORK_GROUP_SIZE.");
            return -1;
        }

        error = clFinish(queue);
        test_error(error, "clFinish failed");
        free(source);
    }

    return 0;
}