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
pub use cl3::program::*;
use super::context::Context;
use super::Result;
#[allow(unused_imports)]
use cl3::error_codes::CL_BUILD_PROGRAM_FAILURE;
#[allow(unused_imports)]
use cl3::ext;
#[allow(unused_imports)]
use cl3::types::{cl_context, cl_device_id, cl_int, cl_program, cl_uchar, cl_uint, CL_FALSE};
#[allow(unused_imports)]
use libc::{c_void, intptr_t, size_t};
#[allow(unused_imports)]
use std::ffi::{CStr, CString};
use std::ptr;
use std::result;
pub const CL_SINGLE_RECISION_CONSTANT: &str = "-cl-single-precision-constant ";
pub const CL_DENORMS_ARE_ZERO: &str = "-cl-denorms-are-zero ";
pub const CL_FP_CORRECTLY_ROUNDED_DIVIDE_SQRT: &str = "-cl-fp32-correctly-rounded-divide-sqrt ";
pub const CL_OPT_DISABLE: &str = "-cl-opt-disable ";
pub const CL_STRICT_ALIASING: &str = "-cl-strict-aliasing ";
pub const CL_UNIFORM_WORK_GROUP_SIZE: &str = "-cl-uniform-work-group-size ";
pub const CL_NO_SUBGROUP_INFO: &str = "-cl-no-subgroup-ifp ";
pub const CL_MAD_ENABLE: &str = "-cl-mad-enable ";
pub const CL_NO_SIGNED_ZEROS: &str = "-cl-no-signed-zeros ";
pub const CL_UNSAFE_MATH_OPTIMIZATIONS: &str = "-cl-unsafe-math-optimizations ";
pub const CL_FINITE_MATH_ONLY: &str = "-cl-finite-math-only ";
pub const CL_FAST_RELAXED_MATH: &str = "-cl-fast-relaxed-math ";
pub const CL_STD_2_0: &str = "-cl-std=CL2.0 ";
pub const CL_STD_3_0: &str = "-cl-std=CL3.0 ";
pub const CL_KERNEL_ARG_INFO: &str = "-cl-kernel-arg-info ";
pub const DEBUG_OPTION: &str = "-g ";
pub const BUILD_OPTION_X_SPIR: &str = "-x spir ";
pub const BUILD_OPTION_SPIR_STD_1_2: &str = "-spir-std=1.2 ";
pub const CREATE_LIBRARY: &str = "-create-library ";
pub const ENABLE_LINK_OPTIONS: &str = "-enable-link-options ";
#[derive(Debug)]
pub struct Program {
program: cl_program,
kernel_names: String,
}
impl Drop for Program {
fn drop(&mut self) {
release_program(self.program).expect("Error: clReleaseProgram");
}
}
impl Program {
fn new(program: cl_program, kernel_names: &str) -> Program {
Program {
program,
kernel_names: kernel_names.to_owned(),
}
}
pub fn get(&self) -> cl_program {
self.program
}
pub fn kernel_names(&self) -> &str {
&self.kernel_names
}
pub fn create_from_sources(context: &Context, sources: &[&str]) -> Result<Program> {
Ok(Program::new(
create_program_with_source(context.get(), sources)?,
"",
))
}
pub fn create_from_source(context: &Context, src: &str) -> Result<Program> {
let sources = [src];
Ok(Program::new(
create_program_with_source(context.get(), &sources)?,
"",
))
}
pub fn create_from_binary(
context: &Context,
devices: &[cl_device_id],
binaries: &[&[u8]],
) -> Result<Program> {
Ok(Program::new(
create_program_with_binary(context.get(), devices, binaries)?,
"",
))
}
#[cfg(feature = "CL_VERSION_1_2")]
pub fn create_from_builtin_kernels(
context: &Context,
devices: &[cl_device_id],
kernel_names: &str,
) -> Result<Program> {
let c_names = CString::new(kernel_names)
.expect("Program::create_from_builtin_kernels, invalid kernel_names");
Ok(Program::new(
create_program_with_builtin_kernels(context.get(), devices, &c_names)?,
kernel_names,
))
}
#[cfg(feature = "CL_VERSION_2_1")]
pub fn create_from_il(context: &Context, il: &[u8]) -> Result<Program> {
Ok(Program::new(
create_program_with_il(context.get(), &il)?,
"",
))
}
#[cfg(feature = "cl_khr_il_program")]
pub fn create_from_il_khr(context: &Context, il: &[u8]) -> Result<Program> {
Ok(Program::new(
ext::create_program_with_il_khr(context.get(), &il)?,
"",
))
}
pub fn build(&mut self, devices: &[cl_device_id], options: &str) -> Result<()> {
let c_options = CString::new(options).expect("Program::build, invalid options");
build_program(self.program, devices, &c_options, None, ptr::null_mut())?;
self.kernel_names = self.get_kernel_names()?;
Ok(())
}
pub fn create_and_build_from_sources(
context: &Context,
sources: &[&str],
options: &str,
) -> result::Result<Program, String> {
let mut program =
Program::create_from_sources(context, sources).map_err(|e| e.to_string())?;
match program.build(context.devices(), options) {
Ok(_) => Ok(program),
Err(e) => {
if CL_BUILD_PROGRAM_FAILURE == e.0 {
let log = program
.get_build_log(context.devices()[0])
.map_err(|e| e.to_string())?;
Err(e.to_string() + ", build log: " + &log)
} else {
Err(e.to_string())
}
}
}
}
pub fn create_and_build_from_source(
context: &Context,
src: &str,
options: &str,
) -> result::Result<Program, String> {
let sources = [src];
Program::create_and_build_from_sources(context, &sources, options)
}
pub fn create_and_build_from_binary(
context: &Context,
binaries: &[&[u8]],
options: &str,
) -> Result<Program> {
let mut program = Program::create_from_binary(context, context.devices(), binaries)?;
program.build(context.devices(), options)?;
Ok(program)
}
#[cfg(feature = "CL_VERSION_2_1")]
pub fn create_and_build_from_il(
context: &Context,
il: &[u8],
options: &str,
) -> Result<Program> {
let mut program = Program::create_from_il(&context, il)?;
program.build(context.devices(), options)?;
Ok(program)
}
#[cfg(feature = "CL_VERSION_1_2")]
pub fn compile(
&mut self,
devices: &[cl_device_id],
options: &str,
input_headers: &[cl_program],
header_include_names: &[&CStr],
) -> Result<()> {
let c_options = CString::new(options).expect("Program::compile, invalid options");
Ok(compile_program(
self.program,
devices,
&c_options,
input_headers,
header_include_names,
None,
ptr::null_mut(),
)?)
}
#[cfg(feature = "CL_VERSION_1_2")]
pub fn link(
&mut self,
devices: &[cl_device_id],
options: &str,
input_programs: &[cl_program],
) -> Result<()> {
let c_options = CString::new(options).expect("Program::link, invalid options");
self.program = link_program(
self.program,
devices,
&c_options,
input_programs,
None,
ptr::null_mut(),
)?;
self.kernel_names = self.get_kernel_names()?;
Ok(())
}
#[cfg(feature = "CL_VERSION_2_2")]
pub fn set_release_callback(
&self,
pfn_notify: Option<extern "C" fn(program: cl_program, user_data: *mut c_void)>,
user_data: *mut c_void,
) -> Result<()> {
Ok(set_program_release_callback(
self.program,
pfn_notify,
user_data,
)?)
}
#[cfg(feature = "CL_VERSION_2_2")]
pub fn set_specialization_constant(
&self,
spec_id: cl_uint,
spec_size: size_t,
spec_value: *const c_void,
) -> Result<()> {
Ok(set_program_specialization_constant(
self.program,
spec_id,
spec_size,
spec_value,
)?)
}
pub fn get_reference_count(&self) -> Result<cl_uint> {
Ok(get_program_info(self.program, ProgramInfo::CL_PROGRAM_REFERENCE_COUNT)?.to_uint())
}
pub fn get_context(&self) -> Result<cl_context> {
Ok(get_program_info(self.program, ProgramInfo::CL_PROGRAM_CONTEXT)?.to_ptr() as cl_context)
}
pub fn get_num_devices(&self) -> Result<cl_uint> {
Ok(get_program_info(self.program, ProgramInfo::CL_PROGRAM_NUM_DEVICES)?.to_uint())
}
pub fn get_devices(&self) -> Result<Vec<intptr_t>> {
Ok(get_program_info(self.program, ProgramInfo::CL_PROGRAM_DEVICES)?.to_vec_intptr())
}
pub fn get_source(&self) -> Result<String> {
Ok(get_program_info(self.program, ProgramInfo::CL_PROGRAM_SOURCE)?.to_string())
}
pub fn get_binary_sizes(&self) -> Result<Vec<size_t>> {
Ok(get_program_info(self.program, ProgramInfo::CL_PROGRAM_BINARY_SIZES)?.to_vec_size())
}
pub fn get_binaries(&self) -> Result<Vec<Vec<cl_uchar>>> {
Ok(get_program_info(self.program, ProgramInfo::CL_PROGRAM_BINARIES)?.to_vec_vec_uchar())
}
pub fn get_num_kernels(&self) -> Result<size_t> {
Ok(get_program_info(self.program, ProgramInfo::CL_PROGRAM_NUM_KERNELS)?.to_size())
}
pub fn get_kernel_names(&self) -> Result<String> {
Ok(get_program_info(self.program, ProgramInfo::CL_PROGRAM_KERNEL_NAMES)?.to_string())
}
pub fn get_program_il(&self) -> Result<String> {
Ok(get_program_info(self.program, ProgramInfo::CL_PROGRAM_IL)?.to_string())
}
pub fn get_program_scope_global_ctors_present(&self) -> Result<bool> {
Ok(get_program_info(
self.program,
ProgramInfo::CL_PROGRAM_SCOPE_GLOBAL_CTORS_PRESENT,
)?
.to_uint()
!= CL_FALSE)
}
pub fn get_program_scope_global_dtors_present(&self) -> Result<bool> {
Ok(get_program_info(
self.program,
ProgramInfo::CL_PROGRAM_SCOPE_GLOBAL_DTORS_PRESENT,
)?
.to_uint()
!= CL_FALSE)
}
pub fn get_build_status(&self, device: cl_device_id) -> Result<cl_int> {
Ok(get_program_build_info(
self.program,
device,
ProgramBuildInfo::CL_PROGRAM_BUILD_STATUS,
)?
.to_int())
}
pub fn get_build_options(&self, device: cl_device_id) -> Result<String> {
Ok(get_program_build_info(
self.program,
device,
ProgramBuildInfo::CL_PROGRAM_BUILD_OPTIONS,
)?
.to_string())
}
pub fn get_build_log(&self, device: cl_device_id) -> Result<String> {
Ok(
get_program_build_info(self.program, device, ProgramBuildInfo::CL_PROGRAM_BUILD_LOG)?
.to_string(),
)
}
pub fn get_build_binary_type(&self, device: cl_device_id) -> Result<cl_uint> {
Ok(get_program_build_info(
self.program,
device,
ProgramBuildInfo::CL_PROGRAM_BINARY_TYPE,
)?
.to_uint())
}
pub fn get_build_global_variable_total_size(&self, device: cl_device_id) -> Result<size_t> {
Ok(get_program_build_info(
self.program,
device,
ProgramBuildInfo::CL_PROGRAM_BUILD_GLOBAL_VARIABLE_TOTAL_SIZE,
)?
.to_size())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::context::Context;
use crate::device::Device;
use crate::platform::get_platforms;
use cl3::device::CL_DEVICE_TYPE_GPU;
use std::collections::HashSet;
const PROGRAM_SOURCE: &str = r#"
kernel void add(global float* buffer, float scalar) {
buffer[get_global_id(0)] += scalar;
}
kernel void subtract(global float* buffer, float scalar) {
buffer[get_global_id(0)] -= scalar;
}
"#;
#[test]
fn test_create_and_build_from_source() {
let platforms = get_platforms().unwrap();
assert!(0 < platforms.len());
let platform = &platforms[0];
let devices = platform.get_devices(CL_DEVICE_TYPE_GPU).unwrap();
assert!(0 < devices.len());
let device = Device::new(devices[0]);
let context = Context::from_device(&device).unwrap();
let program =
Program::create_and_build_from_source(&context, PROGRAM_SOURCE, CL_DENORMS_ARE_ZERO)
.expect("Program::create_and_build_from_source failed");
let names: HashSet<&str> = program.kernel_names().split(';').collect();
println!("OpenCL Program kernel_names len: {}", names.len());
println!("OpenCL Program kernel_names: {:?}", names);
let value = program.get_reference_count().unwrap();
println!("program.get_reference_count(): {}", value);
assert_eq!(1, value);
let value = program.get_context().unwrap();
assert!(context.get() == value);
let value = program.get_num_devices().unwrap();
println!("program.get_num_devices(): {}", value);
assert_eq!(1, value);
let value = program.get_devices().unwrap();
assert!(device.id() == value[0] as cl_device_id);
let value = program.get_source().unwrap();
println!("program.get_source(): {}", value);
assert!(!value.is_empty());
let value = program.get_binary_sizes().unwrap();
println!("program.get_binary_sizes(): {:?}", value);
assert!(0 < value[0]);
let value = program.get_binaries().unwrap();
assert!(!value[0].is_empty());
let value = program.get_num_kernels().unwrap();
println!("program.get_num_kernels(): {}", value);
assert_eq!(2, value);
let value = program.get_build_status(device.id()).unwrap();
println!("program.get_build_status(): {}", value);
assert!(CL_BUILD_SUCCESS == value);
let value = program.get_build_options(device.id()).unwrap();
println!("program.get_build_options(): {}", value);
assert!(!value.is_empty());
let value = program.get_build_log(device.id()).unwrap();
println!("program.get_build_log(): {}", value);
let value = program.get_build_binary_type(device.id()).unwrap();
println!("program.get_build_binary_type(): {}", value);
assert_eq!(CL_PROGRAM_BINARY_TYPE_EXECUTABLE as u32, value);
match program.get_build_global_variable_total_size(device.id()) {
Ok(value) => println!("program.get_build_global_variable_total_size(): {}", value),
Err(e) => println!(
"OpenCL error, program.get_build_global_variable_total_size(): {}",
e
),
};
}
}