1 module dcl.device;
2 
3 import dcl.base;
4 import dcl.platform;
5 
6 ///
7 class CLDevice : CLObject
8 {
9 protected:
10 
11     static CLDevice[cl_device_id] used;
12 
13     ///
14     this( cl_device_id id )
15     {
16         enforce( id !is null, new CLException( "can't create device with null id" ) );
17         enforce( id !in used, new CLException( "can't create existing device" ) );
18         this.id = id;
19         used[id] = this;
20         checkCall!clRetainDevice( id );
21 
22         _platform = reqPlatform;
23     }
24 
25     ///
26     CLPlatform _platform;
27 
28 package:
29     ///
30     cl_device_id id;
31 
32 public:
33 
34     static CLDevice getFromID( cl_device_id id )
35     {
36         if( id is null ) return null;
37         if( id in used ) return used[id];
38         return new CLDevice(id);
39     }
40 
41     CLPlatform platform() @property { return _platform; }
42 
43     ///
44     enum Type
45     {
46         DEFAULT     = CL_DEVICE_TYPE_DEFAULT, ///
47         CPU         = CL_DEVICE_TYPE_CPU, ///
48         GPU         = CL_DEVICE_TYPE_GPU, ///
49         ACCELERATOR = CL_DEVICE_TYPE_ACCELERATOR, ///
50         CUSTOM      = CL_DEVICE_TYPE_CUSTOM, ///
51         ALL         = CL_DEVICE_TYPE_ALL ///
52     }
53 
54     ///
55     enum FPConfig
56     {
57         DENORM           = CL_FP_DENORM,          /// `CL_FP_DENORM`
58         INF_NAN          = CL_FP_INF_NAN,         /// `CL_FP_INF_NAN`
59         ROUND_TO_NEAREST = CL_FP_ROUND_TO_NEAREST,/// `CL_FP_ROUND_TO_NEAREST`
60         ROUND_TO_ZERO    = CL_FP_ROUND_TO_ZERO,   /// `CL_FP_ROUND_TO_ZERO`
61         ROUND_TO_INF     = CL_FP_ROUND_TO_INF,    /// `CL_FP_ROUND_TO_INF`
62         FMA              = CL_FP_FMA              /// `CL_FP_FMA`
63     }
64 
65     ///
66     enum ExecCapabilities
67     {
68         KERNEL        = CL_EXEC_KERNEL, /// `CL_EXEC_KERNEL`
69         NATIVE_KERNEL = CL_EXEC_NATIVE_KERNEL /// `CL_EXEC_NATIVE_KERNEL`
70     }
71 
72     ///
73     enum MemCacheType
74     {
75         NONE             = CL_NONE,            /// `CL_NONE`
76         READ_ONLY_CACHE  = CL_READ_ONLY_CACHE, /// `CL_READ_ONLY_CACHE`
77         READ_WRITE_CACHE = CL_READ_WRITE_CACHE /// `CL_READ_WRITE_CACHE`
78     }
79 
80     enum LocalMemType
81     {
82         LOCAL  = CL_LOCAL, /// `CL_LOCAL`
83         GLOBAL = CL_GLOBAL /// `CL_GLOBAL`
84     }
85 
86     static private enum info_list =
87     [
88         "cl_device_type type:typeMask",
89         "uint vendor_id",
90         "uint max_compute_units",
91         "uint max_work_item_dimensions",
92         "size_t[] max_work_group_size",
93         "size_t[] max_work_item_sizes",
94         "uint preferred_vector_width_char",
95         "uint preferred_vector_width_short",
96         "uint preferred_vector_width_int",
97         "uint preferred_vector_width_long",
98         "uint preferred_vector_width_float",
99         "uint preferred_vector_width_double",
100         "uint preferred_vector_width_half",
101         "uint native_vector_width_char",
102         "uint native_vector_width_short",
103         "uint native_vector_width_int",
104         "uint native_vector_width_long",
105         "uint native_vector_width_float",
106         "uint native_vector_width_double",
107         "uint native_vector_width_half",
108         "uint max_clock_frequency",
109         "uint address_bits",
110         "uint max_read_image_args",
111         "uint max_write_image_args",
112         "ulong max_mem_alloc_size",
113         "size_t image2d_max_width",
114         "size_t image2d_max_height",
115         "size_t image3d_max_width",
116         "size_t image3d_max_height",
117         "size_t image3d_max_depth",
118         "cl_uint:bool image_support",
119         "size_t max_parameter_size",
120         "uint max_samplers",
121         "uint mem_base_addr_align",
122         "cl_device_fp_config single_fp_config",
123         "cl_device_mem_cache_type:MemCacheType global_mem_cache_type",
124         "uint global_mem_cacheline_size",
125         "ulong global_mem_cache_size",
126         "ulong global_mem_size",
127         "ulong max_constant_buffer_size",
128         "uint max_constant_args",
129         "cl_device_local_mem_type:LocalMemType local_mem_type",
130         "ulong local_mem_size",
131         "cl_bool:bool error_correction_support",
132         "size_t profiling_timer_resolution",
133         "cl_bool:bool endian_little",
134         "cl_bool:bool available",
135         "cl_bool:bool compiler_available",
136         "cl_device_exec_capabilities:ExecCapabilities execution_capabilities",
137         "cl_command_queue_properties queue_properties",
138         "string name",
139         "string vendor",
140         "string !driver_version:driver_version",
141         "string profile",
142         "string version:_version",
143         "string extensions",
144         "cl_device_fp_config double_fp_config",
145         "cl_bool:bool host_unified_memory",
146         "string opencl_c_version",
147         "cl_bool:bool linker_available",
148         "string built_in_kernels",
149         "size_t image_max_buffer_size",
150         "size_t image_max_array_size",
151         "cl_device_id:CLDevice parent_device",
152         "uint partition_max_sub_devices",
153         "cl_device_partition_property[] partition_properties",
154         "cl_device_affinity_domain partition_affinity_domain",
155         "cl_device_partition_property[] partition_type",
156         "uint reference_count:refcount",
157         "cl_bool:bool preferred_interop_user_sync",
158         "size_t printf_buffer_size",
159         "cl_platform_id:CLPlatform platform:reqPlatform",
160     ];
161 
162     mixin( infoMixin( "device", info_list ) );
163 
164 protected:
165 
166     override void selfDestroy()
167     {
168         used.remove(id);
169         checkCall!clReleaseDevice( id );
170     }
171 }