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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
// Copyright (c) 2020 Via Technology Ltd. All Rights Reserved.
//
// 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.

pub use cl3::device::*;

use cl3::types::{
    cl_device_fp_config, cl_device_id, cl_device_partition_property, cl_device_svm_capabilities,
    cl_device_type, cl_int, cl_uint, cl_ulong, cl_name_version,
};
use libc::{intptr_t, size_t};
use std::ffi::CString;

/// A text representation of an OpenCL device type, see:
/// [Device Types](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#device-types-table).
pub fn device_type_text(dev_type: cl_device_type) -> &'static str {
    match dev_type {
        CL_DEVICE_TYPE_DEFAULT => "CL_DEVICE_TYPE_DEFAULT",
        CL_DEVICE_TYPE_CPU => "CL_DEVICE_TYPE_CPU",
        CL_DEVICE_TYPE_GPU => "CL_DEVICE_TYPE_GPU",
        CL_DEVICE_TYPE_ACCELERATOR => "CL_DEVICE_TYPE_ACCELERATOR",
        CL_DEVICE_TYPE_CUSTOM => "CL_DEVICE_TYPE_CUSTOM",
        CL_DEVICE_TYPE_ALL => "CL_DEVICE_TYPE_ALL",

        _ => "COMBINED_DEVICE_TYPE",
    }
}

pub struct SubDevice {
    id: cl_device_id,
}

impl Drop for SubDevice {
    fn drop(&mut self) {
        release_device(self.id).unwrap();
        println!("SubDevice::drop");
    }
}

impl SubDevice {
    pub fn new(id: cl_device_id) -> SubDevice {
        SubDevice { id }
    }

    /// Accessor for the underlying device id.
    pub fn id(&self) -> cl_device_id {
        self.id
    }
}

/// An OpenCL device id and methods to query it.  
/// The query methods calls clGetDeviceInfo with the relevant param_name, see:
/// [Device Queries](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#device-queries-table).
pub struct Device {
    id: cl_device_id,
}

impl Device {
    pub fn new(id: cl_device_id) -> Device {
        Device { id }
    }

    /// Accessor for the underlying device id.
    pub fn id(&self) -> cl_device_id {
        self.id
    }

    /// Create sub-devices by partitioning an OpenCL device.
    ///
    /// * `properties` - the slice of cl_device_partition_property, see
    /// [Subdevice Partition](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#subdevice-partition-table).
    ///
    /// returns a Result containing a vector of available sub-device ids
    /// or the error code from the OpenCL C API function.
    pub fn create_sub_devices(
        &self,
        properties: &[cl_device_partition_property],
    ) -> Result<Vec<cl_device_id>, cl_int> {
        create_sub_devices(self.id, &properties)
    }

    #[cfg(feature = "CL_VERSION_2_1")]
    #[inline]
    pub fn get_device_and_host_timer(&self) -> Result<[cl_ulong; 2], cl_int> {
        get_device_and_host_timer(self.id)
    }

    #[cfg(feature = "CL_VERSION_2_1")]
    #[inline]
    pub fn get_host_timer(&self) -> Result<cl_ulong, cl_int> {
        get_host_timer(self.id)
    }

    /// The OpenCL device type, see
    /// [Device Types](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#device-types-table).  
    pub fn dev_type(&self) -> Result<cl_device_type, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_TYPE)?.to_ulong())
    }

    /// A unique device vendor identifier: a [PCI vendor ID](https://www.pcilookup.com/)
    /// or a Khronos vendor ID if the vendor does not have a PCI vendor ID.  
    pub fn vendor_id(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_VENDOR_ID)?.to_uint())
    }

    /// The number of parallel compute units on the device, minimum 1.  
    pub fn max_compute_units(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_COMPUTE_UNITS)?.to_uint())
    }

    /// Maximum dimensions for global and local work-item IDs, minimum 3
    /// if device is not CL_DEVICE_TYPE_CUSTOM.  
    pub fn max_work_item_dimensions(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS)?.to_uint())
    }

    /// Maximum number of work-items for each dimension of a work-group,
    /// minimum [1, 1, 1] if device is not CL_DEVICE_TYPE_CUSTOM.  
    pub fn max_work_group_size(&self) -> Result<size_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_WORK_GROUP_SIZE)?.to_size())
    }

    pub fn max_work_item_sizes(&self) -> Result<Vec<size_t>, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_WORK_ITEM_SIZES)?.to_vec_size())
    }

    pub fn max_preferred_vector_width_char(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR)?.to_uint())
    }

    pub fn max_preferred_vector_width_short(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT)?.to_uint())
    }

    pub fn max_preferred_vector_width_int(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT)?.to_uint())
    }

    pub fn max_preferred_vector_width_long(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG)?.to_uint())
    }

    pub fn max_preferred_vector_width_float(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT)?.to_uint())
    }

    pub fn max_preferred_vector_width_double(&self) -> Result<cl_uint, cl_int> {
        Ok(
            get_device_info(self.id, DeviceInfo::CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE)?
                .to_uint(),
        )
    }

    pub fn max_clock_frequency(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_CLOCK_FREQUENCY)?.to_uint())
    }

    pub fn address_bits(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_ADDRESS_BITS)?.to_uint())
    }

    pub fn max_read_image_args(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_READ_IMAGE_ARGS)?.to_uint())
    }

    pub fn max_write_image_args(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_WRITE_IMAGE_ARGS)?.to_uint())
    }

    pub fn max_mem_alloc_size(&self) -> Result<cl_ulong, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_MEM_ALLOC_SIZE)?.to_ulong())
    }

    pub fn image2d_max_width(&self) -> Result<size_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_IMAGE2D_MAX_WIDTH)?.to_size())
    }

    pub fn image2d_max_height(&self) -> Result<size_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_IMAGE2D_MAX_HEIGHT)?.to_size())
    }

    pub fn image3d_max_width(&self) -> Result<size_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_IMAGE3D_MAX_WIDTH)?.to_size())
    }

    pub fn image3d_max_height(&self) -> Result<size_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_IMAGE3D_MAX_HEIGHT)?.to_size())
    }

    pub fn image3d_max_depth(&self) -> Result<size_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_IMAGE3D_MAX_DEPTH)?.to_size())
    }

    pub fn image_support(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_IMAGE_SUPPORT)?.to_uint())
    }

    pub fn max_parameter_size(&self) -> Result<size_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_PARAMETER_SIZE)?.to_size())
    }

    pub fn max_device_samples(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_SAMPLERS)?.to_uint())
    }

    pub fn mem_base_addr_align(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MEM_BASE_ADDR_ALIGN)?.to_uint())
    }

    pub fn min_data_type_align_size(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE)?.to_uint())
    }

    pub fn single_fp_config(&self) -> Result<cl_ulong, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_SINGLE_FP_CONFIG)?.to_ulong())
    }

    pub fn global_mem_cache_type(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_GLOBAL_MEM_CACHE_TYPE)?.to_uint())
    }

    pub fn global_mem_cacheline_size(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE)?.to_uint())
    }

    pub fn global_mem_cache_size(&self) -> Result<cl_ulong, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_GLOBAL_MEM_CACHE_SIZE)?.to_ulong())
    }

    pub fn global_mem_size(&self) -> Result<cl_ulong, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_GLOBAL_MEM_SIZE)?.to_ulong())
    }

    pub fn max_constant_buffer_size(&self) -> Result<cl_ulong, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE)?.to_ulong())
    }

    pub fn max_constant_args(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_CONSTANT_ARGS)?.to_uint())
    }

    pub fn local_mem_type(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_LOCAL_MEM_TYPE)?.to_uint())
    }

    pub fn local_mem_size(&self) -> Result<cl_ulong, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_LOCAL_MEM_SIZE)?.to_ulong())
    }

    pub fn error_correction_support(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_ERROR_CORRECTION_SUPPORT)?.to_uint())
    }

    pub fn profiling_timer_resolution(&self) -> Result<size_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PROFILING_TIMER_RESOLUTION)?.to_size())
    }

    pub fn endian_little(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_ENDIAN_LITTLE)?.to_uint())
    }

    pub fn available(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_AVAILABLE)?.to_uint())
    }

    pub fn compiler_available(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_COMPILER_AVAILABLE)?.to_uint())
    }

    pub fn execution_capabilities(&self) -> Result<cl_ulong, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_EXECUTION_CAPABILITIES)?.to_ulong())
    }

    pub fn queue_on_host_properties(&self) -> Result<cl_ulong, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_QUEUE_ON_HOST_PROPERTIES)?.to_ulong())
    }

    pub fn name(&self) -> Result<CString, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_NAME)?
            .to_str()
            .unwrap())
    }

    pub fn vendor(&self) -> Result<CString, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_VENDOR)?
            .to_str()
            .unwrap())
    }

    pub fn driver_version(&self) -> Result<CString, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DRIVER_VERSION)?
            .to_str()
            .unwrap())
    }

    pub fn profile(&self) -> Result<CString, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PROFILE)?
            .to_str()
            .unwrap())
    }

    pub fn version(&self) -> Result<CString, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_VERSION)?
            .to_str()
            .unwrap())
    }

    pub fn extensions(&self) -> Result<CString, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_EXTENSIONS)?
            .to_str()
            .unwrap())
    }

    pub fn platform(&self) -> Result<intptr_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PLATFORM)?.to_ptr())
    }

    pub fn double_fp_config(&self) -> Result<cl_ulong, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_DOUBLE_FP_CONFIG)?.to_ulong())
    }

    pub fn half_fp_config(&self) -> Result<cl_ulong, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_HALF_FP_CONFIG)?.to_ulong())
    }

    pub fn preferred_vector_width_half(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PREFERRED_VECTOR_WIDTH_HALF)?.to_uint())
    }

    // DEPRECATED 2.0
    pub fn host_unified_memory(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_HOST_UNIFIED_MEMORY)?.to_uint())
    }

    pub fn native_vector_width_char(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_NATIVE_VECTOR_WIDTH_CHAR)?.to_uint())
    }

    pub fn native_vector_width_short(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_NATIVE_VECTOR_WIDTH_SHORT)?.to_uint())
    }

    pub fn native_vector_width_int(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_NATIVE_VECTOR_WIDTH_INT)?.to_uint())
    }

    pub fn native_vector_width_long(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_NATIVE_VECTOR_WIDTH_LONG)?.to_uint())
    }

    pub fn native_vector_width_float(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_NATIVE_VECTOR_WIDTH_FLOAT)?.to_uint())
    }

    pub fn native_vector_width_double(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_NATIVE_VECTOR_WIDTH_DOUBLE)?.to_uint())
    }

    pub fn native_vector_width_half(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_NATIVE_VECTOR_WIDTH_HALF)?.to_uint())
    }

    pub fn opencl_c_version(&self) -> Result<CString, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_OPENCL_C_VERSION)?
            .to_str()
            .unwrap())
    }

    pub fn linker_available(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_LINKER_AVAILABLE)?.to_uint())
    }

    pub fn built_in_kernels(&self) -> Result<CString, cl_int> {
        Ok(
            get_device_info(self.id, DeviceInfo::CL_DEVICE_BUILT_IN_KERNELS)?
                .to_str()
                .unwrap(),
        )
    }

    pub fn image_max_buffer_size(&self) -> Result<size_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_IMAGE_MAX_BUFFER_SIZE)?.to_size())
    }

    pub fn image_max_array_size(&self) -> Result<size_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_IMAGE_MAX_ARRAY_SIZE)?.to_size())
    }

    pub fn parent_device(&self) -> Result<intptr_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PARENT_DEVICE)?.to_ptr())
    }

    pub fn partition_max_sub_devices(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PARTITION_MAX_SUB_DEVICES)?.to_uint())
    }
    
    pub fn partition_properties(&self) -> Result<Vec<intptr_t>, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PARTITION_PROPERTIES)?.to_vec_intptr())
    }

    pub fn partition_affinity_domain(&self) -> Result<Vec<cl_ulong>, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PARTITION_AFFINITY_DOMAIN)?.to_vec_ulong())
    }

    pub fn partition_type(&self) -> Result<Vec<intptr_t>, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PARTITION_TYPE)?.to_vec_intptr())
    }

    pub fn reference_count(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_REFERENCE_COUNT)?.to_uint())
    }

    pub fn preferred_interop_user_sync(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PREFERRED_INTEROP_USER_SYNC)?.to_uint())
    }

    pub fn printf_buffer_size(&self) -> Result<size_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PRINTF_BUFFER_SIZE)?.to_size())
    }

    // CL_VERSION_2_0
    pub fn image_pitch_alignment(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_IMAGE_PITCH_ALIGNMENT)?.to_uint())
    }

    pub fn image_base_address_alignment(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_IMAGE_BASE_ADDRESS_ALIGNMENT)?.to_uint())
    }

    pub fn max_read_write_image_args(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_READ_WRITE_IMAGE_ARGS)?.to_uint())
    }

    pub fn max_global_variable_size(&self) -> Result<size_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_GLOBAL_VARIABLE_SIZE)?.to_size())
    }

    pub fn queue_on_device_properties(&self) -> Result<Vec<intptr_t>, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_QUEUE_ON_DEVICE_PROPERTIES)?.to_vec_intptr())
    }

    pub fn queue_on_device_preferred_size(&self) -> Result<size_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_QUEUE_ON_DEVICE_PREFERRED_SIZE)?.to_size())
    }

    pub fn queue_on_device_max_size(&self) -> Result<size_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_QUEUE_ON_DEVICE_MAX_SIZE)?.to_size())
    }

    pub fn max_on_device_queues(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_ON_DEVICE_QUEUES)?.to_uint())
    }

    pub fn max_on_device_events(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_ON_DEVICE_EVENTS)?.to_uint())
    }

    pub fn svm_capabilities(&self) -> Result<cl_device_svm_capabilities, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_SVM_CAPABILITIES)?.to_ulong())
    }

    pub fn global_variable_preferred_total_size(&self) -> Result<size_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_GLOBAL_VARIABLE_PREFERRED_TOTAL_SIZE)?.to_size())
    }

    pub fn max_pipe_args(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_PIPE_ARGS)?.to_uint())
    }

    pub fn pipe_max_active_reservations(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PIPE_MAX_ACTIVE_RESERVATIONS)?.to_uint())
    }

    pub fn pipe_max_packet_size(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PIPE_MAX_PACKET_SIZE)?.to_uint())
    }

    pub fn preferred_platform_atomic_alignment(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PREFERRED_PLATFORM_ATOMIC_ALIGNMENT)?.to_uint())
    }

    pub fn preferred_global_atomic_alignment(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PREFERRED_GLOBAL_ATOMIC_ALIGNMENT)?.to_uint())
    }

    pub fn preferred_local_atomic_alignment(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PREFERRED_LOCAL_ATOMIC_ALIGNMENT)?.to_uint())
    }

    // CL_VERSION_2_1
    pub fn il_version(&self) -> Result<CString, cl_int> {
        Ok(
            get_device_info(self.id, DeviceInfo::CL_DEVICE_IL_VERSION)?
                .to_str()
                .unwrap(),
        )
    }

    pub fn max_num_sub_groups(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_MAX_NUM_SUB_GROUPS)?.to_uint())
    }

    pub fn sub_group_independent_forward_progress(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_SUB_GROUP_INDEPENDENT_FORWARD_PROGRESS)?.to_uint())
    }

    // CL_VERSION_3_0
    pub fn numeric_version(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_NUMERIC_VERSION)?.to_uint())
    }

    pub fn extensions_with_version(&self) -> Result<Vec<cl_name_version>, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_EXTENSIONS_WITH_VERSION)?.to_vec_name_version())
    }

    pub fn ils_with_version(&self) -> Result<Vec<cl_name_version>, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_ILS_WITH_VERSION)?.to_vec_name_version())
    }

    pub fn built_in_kernels_with_version(&self) -> Result<Vec<cl_name_version>, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_BUILT_IN_KERNELS_WITH_VERSION)?.to_vec_name_version())
    }

    pub fn atomic_memory_capabilities(&self) -> Result<cl_ulong, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_ATOMIC_MEMORY_CAPABILITIES)?.to_ulong())
    }

    pub fn atomic_fence_capabilities(&self) -> Result<cl_ulong, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_ATOMIC_FENCE_CAPABILITIES)?.to_ulong())
    }

    pub fn non_uniform_work_group_support(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_NON_UNIFORM_WORK_GROUP_SUPPORT)?.to_uint())
    }

    pub fn opencl_c_all_versions(&self) -> Result<Vec<cl_name_version>, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_OPENCL_C_ALL_VERSIONS)?.to_vec_name_version())
    }

    pub fn preferred_work_group_size_multiple(&self) -> Result<size_t, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PREFERRED_WORK_GROUP_SIZE_MULTIPLE)?.to_size())
    }

    pub fn work_group_collective_functions_support(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_WORK_GROUP_COLLECTIVE_FUNCTIONS_SUPPORT)?.to_uint())
    }

    pub fn generic_address_space_support(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_GENERIC_ADDRESS_SPACE_SUPPORT)?.to_uint())
    }

    pub fn opencl_c_features(&self) -> Result<Vec<cl_name_version>, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_OPENCL_C_FEATURES)?.to_vec_name_version())
    }

    pub fn device_enqueue_capabilities(&self) -> Result<cl_ulong, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_DEVICE_ENQUEUE_CAPABILITIES)?.to_ulong())
    }

    pub fn pipe_support(&self) -> Result<cl_uint, cl_int> {
        Ok(get_device_info(self.id, DeviceInfo::CL_DEVICE_PIPE_SUPPORT)?.to_uint())
    }

    pub fn latest_conformance_version_passed(&self) -> Result<CString, cl_int> {
        Ok(
            get_device_info(self.id, DeviceInfo::CL_DEVICE_LATEST_CONFORMANCE_VERSION_PASSED)?
                .to_str()
                .unwrap(),
        )
    }

    /// Determine if the device supports the given half floating point capability.  
    /// Returns true if the device supports it, false otherwise.
    pub fn supports_half(&self, min_fp_capability: cl_device_fp_config) -> bool {
        if let Ok(fp) = self.half_fp_config() {
            0 < fp & min_fp_capability
        } else {
            false
        }
    }
    /// Determine if the device supports the given double floating point capability.  
    /// Returns true if the device supports it, false otherwise.
    pub fn supports_double(&self, min_fp_capability: cl_device_fp_config) -> bool {
        if let Ok(fp) = self.double_fp_config() {
            0 < fp & min_fp_capability
        } else {
            false
        }
    }

    /// Determine if the device supports SVM and, if so, what kind of SVM.  
    /// Returns zero if the device does not support SVM.
    pub fn svm_mem_capability(&self) -> cl_device_svm_capabilities {
        if let Ok(svm) = self.svm_capabilities() {
            svm
        } else {
            0
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::cl_platform_id;
    use crate::platform::get_platforms;
    use crate::error_codes::error_text;

    #[test]
    fn test_get_devices() {
        let platforms = get_platforms().unwrap();
        println!("Number of platforms: {}", platforms.len());
        assert!(0 < platforms.len());

        for platform in platforms {
            println!("CL_PLATFORM_NAME: {:?}", platform.name().unwrap());

            let devices = platform.get_devices(CL_DEVICE_TYPE_ALL).unwrap();
            for device_id in devices {
                let device = Device::new(device_id);

                println!("\tCL_DEVICE_NAME: {:?}", device.name().unwrap());
                println!("\tCL_DEVICE_TYPE: {:X}", device.dev_type().unwrap());
                println!("\tCL_DEVICE_VENDOR_ID: {:X}", device.vendor_id().unwrap());
                println!("\tCL_DEVICE_VENDOR: {:?}", device.vendor().unwrap());
                println!(
                    "\tCL_DEVICE_OPENCL_C_VERSION: {:?}",
                    device.opencl_c_version().unwrap()
                );
                println!("");
            }
        }
    }

    
    #[test]
    fn test_device_info() {
        let platforms = get_platforms().unwrap();
        assert!(!platforms.is_empty());

        // Choose the first platform
        let platform = &platforms[0];

        let devices = platform.get_devices(CL_DEVICE_TYPE_GPU).unwrap();
        println!("CL_DEVICE_TYPE_GPU count: {}", devices.len());
        assert!(!devices.is_empty());

        // Choose the first device
        let device_id = devices[0];
        let device = Device::new(device_id);

        let value = device.dev_type().unwrap();
        assert_eq!(CL_DEVICE_TYPE_GPU, value);

        let value = device.vendor_id().unwrap();
        println!("CL_DEVICE_VENDOR_ID: {:X}", value);
        assert!(0 < value);

        let vendor_text = match value {
            0x1002 => "AMD",
            0x10DE => "Nvidia",
            0x8086 => "Intel",
            _ => "unknown",
        };
        println!("Device vendor is: {}", vendor_text);

        let value = device.max_compute_units().unwrap();
        println!("CL_DEVICE_MAX_COMPUTE_UNITS: {}", value);
        assert!(0 < value);

        let value = device.max_work_item_dimensions().unwrap();
        println!("CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS: {}", value);
        assert!(0 < value);

        let value = device.max_work_group_size().unwrap();
        println!("CL_DEVICE_MAX_WORK_GROUP_SIZE: {}", value);
        assert!(0 < value);

        let value = device.max_work_item_sizes().unwrap();
        println!("CL_DEVICE_MAX_WORK_ITEM_SIZES: {}", value.len());
        println!("CL_DEVICE_MAX_WORK_ITEM_SIZES: {:?}", value);
        assert!(0 < value.len());

        let value = device.max_preferred_vector_width_char().unwrap();
        println!("CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR: {}", value);
        assert!(0 < value);

        let value = device.max_preferred_vector_width_short().unwrap();
        println!("CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT: {}", value);
        assert!(0 < value);

        let value = device.max_preferred_vector_width_int().unwrap();
        println!("CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT: {}", value);
        assert!(0 < value);

        let value = device.max_preferred_vector_width_long().unwrap();
        println!("CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG: {}", value);
        assert!(0 < value);

        let value = device.max_preferred_vector_width_float().unwrap();
        println!("CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT: {}", value);
        assert!(0 < value);

        let value = device.max_preferred_vector_width_double().unwrap();
        println!("CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE: {}", value);
        assert!(0 < value);

        let value = device.max_clock_frequency().unwrap();
        println!("CL_DEVICE_MAX_CLOCK_FREQUENCY: {}", value);
        assert!(0 < value);

        let value = device.address_bits().unwrap();
        println!("CL_DEVICE_ADDRESS_BITS: {}", value);
        assert!(0 < value);

        let value = device.max_read_image_args().unwrap();
        println!("CL_DEVICE_MAX_READ_IMAGE_ARGS: {}", value);
        assert!(0 < value);

        let value = device.max_write_image_args().unwrap();
        println!("CL_DEVICE_MAX_WRITE_IMAGE_ARGS: {}", value);
        assert!(0 < value);

        let value = device.max_mem_alloc_size().unwrap();
        println!("CL_DEVICE_MAX_MEM_ALLOC_SIZE: {}", value);
        assert!(0 < value);

        let value = device.image2d_max_width().unwrap();
        println!("CL_DEVICE_IMAGE2D_MAX_WIDTH: {}", value);
        assert!(0 < value);

        let value = device.image2d_max_height().unwrap();
        println!("CL_DEVICE_IMAGE2D_MAX_HEIGHT: {}", value);
        assert!(0 < value);

        let value = device.image3d_max_width().unwrap();
        println!("CL_DEVICE_IMAGE3D_MAX_WIDTH: {}", value);
        assert!(0 < value);

        let value = device.image3d_max_height().unwrap();
        println!("CL_DEVICE_IMAGE3D_MAX_HEIGHT: {}", value);
        assert!(0 < value);

        let value = device.image3d_max_depth().unwrap();
        println!("CL_DEVICE_IMAGE3D_MAX_DEPTH: {}", value);
        assert!(0 < value);

        let value = device.image_support().unwrap();
        println!("CL_DEVICE_IMAGE_SUPPORT: {}", value);
        assert!(0 < value);

        let value = device.max_parameter_size().unwrap();
        println!("CL_DEVICE_MAX_PARAMETER_SIZE: {}", value);
        assert!(0 < value);

        let value = device.max_device_samples().unwrap();
        println!("CL_DEVICE_MAX_SAMPLERS: {}", value);
        assert!(0 < value);

        let value = device.mem_base_addr_align().unwrap();
        println!("CL_DEVICE_MEM_BASE_ADDR_ALIGN: {}", value);
        assert!(0 < value);

        let value = device.min_data_type_align_size().unwrap();
        println!("CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE: {}", value);
        assert!(0 < value);
        
        let value = device.single_fp_config().unwrap();
        println!("CL_DEVICE_SINGLE_FP_CONFIG: {:X}", value);
        assert!(0 < value);

        let value = device.global_mem_cache_type().unwrap();
        println!("CL_DEVICE_GLOBAL_MEM_CACHE_TYPE: {:X}", value);
        assert!(0 < value);

        let value = device.global_mem_cacheline_size().unwrap();
        println!("CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE: {}", value);
        assert!(0 < value);

        let value = device.global_mem_cache_size().unwrap();
        println!("CL_DEVICE_GLOBAL_MEM_CACHE_SIZE: {}", value);
        assert!(0 < value);

        let value = device.global_mem_size().unwrap();
        println!("CL_DEVICE_GLOBAL_MEM_SIZE: {}", value);
        assert!(0 < value);
        
        let value = device.max_constant_buffer_size().unwrap();
        println!("CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE: {}", value);
        assert!(0 < value);

        let value = device.max_constant_args().unwrap();
        println!("CL_DEVICE_MAX_CONSTANT_ARGS: {}", value);
        assert!(0 < value);

        let value = device.local_mem_type().unwrap();
        println!("CL_DEVICE_LOCAL_MEM_TYPE: {:X}", value);
        assert!(0 < value);

        let value = device.local_mem_size().unwrap();
        println!("CL_DEVICE_LOCAL_MEM_SIZE: {}", value);
        assert!(0 < value);

        let value = device.error_correction_support().unwrap();
        println!("CL_DEVICE_ERROR_CORRECTION_SUPPORT: {}", value);

        let value = device.profiling_timer_resolution().unwrap();
        println!("CL_DEVICE_PROFILING_TIMER_RESOLUTION: {}", value);
        assert!(0 < value);

        let value = device.endian_little().unwrap();
        println!("CL_DEVICE_ENDIAN_LITTLE: {}", value);
        assert!(0 < value);

        let value = device.available().unwrap();
        println!("CL_DEVICE_AVAILABLE: {}", value);
        assert!(0 < value);

        let value = device.compiler_available().unwrap();
        println!("CL_DEVICE_COMPILER_AVAILABLE: {}", value);
        assert!(0 < value);

        let value = device.execution_capabilities().unwrap();
        println!("CL_DEVICE_EXECUTION_CAPABILITIES: {:X}", value);
        assert!(0 < value);

        // CL_VERSION_2_0
        match device.queue_on_host_properties() {
            Ok(value) => {
                println!("CL_DEVICE_QUEUE_ON_HOST_PROPERTIES: {:X}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_QUEUE_ON_HOST_PROPERTIES: {}", error_text(e))
        };

        let value = device.name().unwrap();
        println!("CL_DEVICE_NAME: {:?}", value);
        assert!(!value.to_bytes().is_empty());

        let value = device.vendor().unwrap();
        println!("CL_DEVICE_VENDOR: {:?}", value);
        assert!(!value.to_bytes().is_empty());

        let value = device.driver_version().unwrap();
        println!("CL_DRIVER_VERSION: {:?}", value);
        assert!(!value.to_bytes().is_empty());

        let value = device.profile().unwrap();
        println!("CL_DEVICE_PROFILE: {:?}", value);
        assert!(!value.to_bytes().is_empty());

        let value = device.version().unwrap();
        println!("CL_DEVICE_VERSION: {:?}", value);
        assert!(!value.to_bytes().is_empty());

        let value = device.extensions().unwrap();
        println!("CL_DEVICE_EXTENSIONS: {:?}", value);
        assert!(!value.to_bytes().is_empty());

        let value = device.platform().unwrap();
        println!("CL_DEVICE_PLATFORM: {:X}", value);
        assert_eq!(platform.id(), value as cl_platform_id);

        // Device may not support double fp precision
        match device.double_fp_config() {
            Ok(value) => {
                println!("CL_DEVICE_DOUBLE_FP_CONFIG: {:X}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_DOUBLE_FP_CONFIG: {}", error_text(e))
        };

        // Device may not support half fp precision
        match device.half_fp_config() {
            Ok(value) => {
                println!("CL_DEVICE_HALF_FP_CONFIG: {:X}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_HALF_FP_CONFIG: {}", error_text(e))
        };

        let value = device.preferred_vector_width_half().unwrap();
        println!("CL_DEVICE_PREFERRED_VECTOR_WIDTH_HALF: {}", value);

        let value = device.host_unified_memory().unwrap();
        println!("CL_DEVICE_HOST_UNIFIED_MEMORY: {}", value);

        let value = device.native_vector_width_char().unwrap();
        println!("CL_DEVICE_NATIVE_VECTOR_WIDTH_CHAR: {}", value);
        assert!(0 < value);

        let value = device.native_vector_width_short().unwrap();
        println!("CL_DEVICE_NATIVE_VECTOR_WIDTH_SHORT: {}", value);
        assert!(0 < value);

        let value = device.native_vector_width_int().unwrap();
        println!("CL_DEVICE_NATIVE_VECTOR_WIDTH_INT: {}", value);
        assert!(0 < value);

        let value = device.native_vector_width_long().unwrap();
        println!("CL_DEVICE_NATIVE_VECTOR_WIDTH_LONG: {}", value);
        assert!(0 < value);

        let value = device.native_vector_width_float().unwrap();
        println!("CL_DEVICE_NATIVE_VECTOR_WIDTH_FLOAT: {}", value);
        assert!(0 < value);

        let value = device.native_vector_width_double().unwrap();
        println!("CL_DEVICE_NATIVE_VECTOR_WIDTH_DOUBLE: {}", value);

        let value = device.native_vector_width_half().unwrap();
        println!("CL_DEVICE_NATIVE_VECTOR_WIDTH_HALF: {}", value);

        let value = device.opencl_c_version().unwrap();
        println!("CL_DEVICE_OPENCL_C_VERSION: {:?}", value);
        assert!(!value.to_bytes().is_empty());

        let value = device.linker_available().unwrap();
        println!("CL_DEVICE_LINKER_AVAILABLE: {}", value);

        let value = device.built_in_kernels().unwrap();
        println!("CL_DEVICE_BUILT_IN_KERNELS: {:?}", value);

        let value = device.image_max_buffer_size().unwrap();
        println!("CL_DEVICE_IMAGE_MAX_BUFFER_SIZE: {}", value);
        assert!(0 < value);

        let value = device.image_max_array_size().unwrap();
        println!("CL_DEVICE_IMAGE_MAX_ARRAY_SIZE: {}", value);
        assert!(0 < value);

        let value = device.parent_device().unwrap();
        println!("CL_DEVICE_PARENT_DEVICE: {:X}", value);
        
        let value = device.partition_max_sub_devices().unwrap();
        println!("CL_DEVICE_PARTITION_MAX_SUB_DEVICES: {}", value);

        let value = device.partition_properties().unwrap();
        println!("CL_DEVICE_PARTITION_PROPERTIES: {:?}", value);
        assert!(0 < value.len());

        let value = device.partition_affinity_domain().unwrap();
        println!("CL_DEVICE_PARTITION_AFFINITY_DOMAIN: {:?}", value);
        assert!(0 < value.len());

        let value = device.partition_type().unwrap();
        println!("CL_DEVICE_PARTITION_TYPE: {:?}", value);
        // assert!(0 < value.len());

        let value = device.reference_count().unwrap();
        println!("CL_DEVICE_REFERENCE_COUNT: {}", value);
        assert!(0 < value);

        let value = device.preferred_interop_user_sync().unwrap();
        println!("CL_DEVICE_PREFERRED_INTEROP_USER_SYNC: {:X}", value);

        let value = device.printf_buffer_size().unwrap();
        println!("CL_DEVICE_PRINTF_BUFFER_SIZE: {}", value);
        assert!(0 < value);

        //////////////////////////////////////////////////////////////////////
        // CL_VERSION_2_0 parameters
        match device.image_pitch_alignment() {
            Ok(value) => {
                println!("CL_DEVICE_IMAGE_PITCH_ALIGNMENT: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_IMAGE_PITCH_ALIGNMENT: {}", error_text(e))
        };

        match device.image_base_address_alignment() {
            Ok(value) => {
                println!("CL_DEVICE_IMAGE_BASE_ADDRESS_ALIGNMENT: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_IMAGE_BASE_ADDRESS_ALIGNMENT: {}", error_text(e))
        };

        match device.max_read_write_image_args() {
            Ok(value) => {
                println!("CL_DEVICE_MAX_READ_WRITE_IMAGE_ARGS: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_MAX_READ_WRITE_IMAGE_ARGS: {}", error_text(e))
        };

        match device.max_global_variable_size() {
            Ok(value) => {
                println!("CL_DEVICE_MAX_GLOBAL_VARIABLE_SIZE: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_MAX_GLOBAL_VARIABLE_SIZE: {}", error_text(e))
        };

        match device.queue_on_device_properties() {
            Ok(value) => {
                println!("CL_DEVICE_QUEUE_ON_DEVICE_PROPERTIES: {:?}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_QUEUE_ON_DEVICE_PROPERTIES: {}", error_text(e))
        };

        match device.queue_on_device_preferred_size() {
            Ok(value) => {
                println!("CL_DEVICE_QUEUE_ON_DEVICE_PREFERRED_SIZE: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_QUEUE_ON_DEVICE_PREFERRED_SIZE: {}", error_text(e))
        };

        match device.queue_on_device_max_size() {
            Ok(value) => {
                println!("CL_DEVICE_QUEUE_ON_DEVICE_MAX_SIZE: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_QUEUE_ON_DEVICE_MAX_SIZE: {}", error_text(e))
        };

        match device.max_on_device_queues() {
            Ok(value) => {
                println!("CL_DEVICE_MAX_ON_DEVICE_QUEUES: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_MAX_ON_DEVICE_QUEUES: {}", error_text(e))
        };

        match device.max_on_device_events() {
            Ok(value) => {
                println!("CL_DEVICE_MAX_ON_DEVICE_EVENTS: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_MAX_ON_DEVICE_EVENTS: {}", error_text(e))
        };

        match device.svm_capabilities() {
            Ok(value) => {
                println!("CL_DEVICE_SVM_CAPABILITIES: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_SVM_CAPABILITIES: {}", error_text(e))
        };

        match device.global_variable_preferred_total_size() {
            Ok(value) => {
                println!("CL_DEVICE_GLOBAL_VARIABLE_PREFERRED_TOTAL_SIZE: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_GLOBAL_VARIABLE_PREFERRED_TOTAL_SIZE: {}", error_text(e))
        };

        match device.max_pipe_args() {
            Ok(value) => {
                println!("CL_DEVICE_MAX_PIPE_ARGS: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_MAX_PIPE_ARGS: {}", error_text(e))
        };
        
        match device.pipe_max_active_reservations() {
            Ok(value) => {
                println!("CL_DEVICE_PIPE_MAX_ACTIVE_RESERVATIONS: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_PIPE_MAX_ACTIVE_RESERVATIONS: {}", error_text(e))
        };

        match device.pipe_max_packet_size() {
            Ok(value) => {
                println!("CL_DEVICE_PIPE_MAX_PACKET_SIZE: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_PIPE_MAX_PACKET_SIZE: {}", error_text(e))
        };

        match device.preferred_platform_atomic_alignment() {
            Ok(value) => {
                println!("CL_DEVICE_PREFERRED_PLATFORM_ATOMIC_ALIGNMENT: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_PREFERRED_PLATFORM_ATOMIC_ALIGNMENT: {}", error_text(e))
        };

        match device.preferred_global_atomic_alignment() {
            Ok(value) => {
                println!("CL_DEVICE_PREFERRED_GLOBAL_ATOMIC_ALIGNMENT: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_PREFERRED_GLOBAL_ATOMIC_ALIGNMENT: {}", error_text(e))
        };

        match device.preferred_local_atomic_alignment() {
            Ok(value) => {
                println!("CL_DEVICE_PREFERRED_LOCAL_ATOMIC_ALIGNMENT: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_PREFERRED_LOCAL_ATOMIC_ALIGNMENT: {}", error_text(e))
        };

        //////////////////////////////////////////////////////////////////////
        // CL_VERSION_2_1 parameters

        match device.il_version() {
            Ok(value) => {
                println!("CL_DEVICE_IL_VERSION: {:?}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_IL_VERSION: {}", error_text(e))
        };

        match device.max_num_sub_groups() {
            Ok(value) => {
                println!("CL_DEVICE_MAX_NUM_SUB_GROUPS: {:?}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_MAX_NUM_SUB_GROUPS: {}", error_text(e))
        };

        match device.sub_group_independent_forward_progress() {
            Ok(value) => {
                println!("CL_DEVICE_SUB_GROUP_INDEPENDENT_FORWARD_PROGRESS: {:?}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_SUB_GROUP_INDEPENDENT_FORWARD_PROGRESS: {}", error_text(e))
        };

        //////////////////////////////////////////////////////////////////////
        // CL_VERSION_3_0 parameters

        match device.numeric_version() {
            Ok(value) => {
                println!("CL_DEVICE_NUMERIC_VERSION: {:X}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_NUMERIC_VERSION: {}", error_text(e))
        };

        match device.extensions_with_version() {
            Ok(value) => {
                println!("CL_DEVICE_EXTENSIONS_WITH_VERSION: {:?}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_EXTENSIONS_WITH_VERSION: {}", error_text(e))
        };

        match device.ils_with_version() {
            Ok(value) => {
                println!("CL_DEVICE_ILS_WITH_VERSION: {:?}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_ILS_WITH_VERSION: {}", error_text(e))
        };

        match device.built_in_kernels_with_version() {
            Ok(value) => {
                println!("CL_DEVICE_BUILT_IN_KERNELS_WITH_VERSION: {:?}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_BUILT_IN_KERNELS_WITH_VERSION: {}", error_text(e))
        };

        match device.atomic_memory_capabilities() {
            Ok(value) => {
                println!("CL_DEVICE_ATOMIC_MEMORY_CAPABILITIES: {:X}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_ATOMIC_MEMORY_CAPABILITIES: {}", error_text(e))
        };

        match device.atomic_fence_capabilities() {
            Ok(value) => {
                println!("CL_DEVICE_ATOMIC_FENCE_CAPABILITIES: {:X}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_ATOMIC_FENCE_CAPABILITIES: {}", error_text(e))
        };

        match device.non_uniform_work_group_support() {
            Ok(value) => {
                println!("CL_DEVICE_NON_UNIFORM_WORK_GROUP_SUPPORT: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_NON_UNIFORM_WORK_GROUP_SUPPORT: {}", error_text(e))
        };

        match device.opencl_c_all_versions() {
            Ok(value) => {
                println!("CL_DEVICE_OPENCL_C_ALL_VERSIONS: {:?}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_OPENCL_C_ALL_VERSIONS: {}", error_text(e))
        };

        match device.preferred_work_group_size_multiple() {
            Ok(value) => {
                println!("CL_DEVICE_PREFERRED_WORK_GROUP_SIZE_MULTIPLE: {}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_PREFERRED_WORK_GROUP_SIZE_MULTIPLE: {}", error_text(e))
        };
        
        match device.work_group_collective_functions_support() {
            Ok(value) => {
                println!("CL_DEVICE_WORK_GROUP_COLLECTIVE_FUNCTIONS_SUPPORT: {:X}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_WORK_GROUP_COLLECTIVE_FUNCTIONS_SUPPORT: {}", error_text(e))
        };

        match device.generic_address_space_support() {
            Ok(value) => {
                println!("CL_DEVICE_GENERIC_ADDRESS_SPACE_SUPPORT: {:X}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_GENERIC_ADDRESS_SPACE_SUPPORT: {}", error_text(e))
        };

        match device.opencl_c_features() {
            Ok(value) => {
                println!("CL_DEVICE_OPENCL_C_FEATURES: {:?}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_OPENCL_C_FEATURES: {}", error_text(e))
        };

        match device.device_enqueue_capabilities() {
            Ok(value) => {
                println!("CL_DEVICE_DEVICE_ENQUEUE_CAPABILITIES: {:X}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_DEVICE_ENQUEUE_CAPABILITIES: {}", error_text(e))
        };

        match device.pipe_support() {
            Ok(value) => {
                println!("CL_DEVICE_PIPE_SUPPORT: {:X}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_PIPE_SUPPORT: {}", error_text(e))
        };

        match device.latest_conformance_version_passed() {
            Ok(value) => {
                println!("CL_DEVICE_LATEST_CONFORMANCE_VERSION_PASSED: {:?}", value)
            }
            Err(e) => println!("OpenCL error, CL_DEVICE_LATEST_CONFORMANCE_VERSION_PASSED: {}", error_text(e))
        };
    }
}