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
//! Tests various image operations.
//!
//! * TODO: Test fill and copy to buffer.
//!
//! Runs both the core function and the 'standard' method call for each.
use crate::core::{self, DeviceInfo, DeviceInfoResult};
use crate::enums::{
AddressingMode, FilterMode, ImageChannelDataType, ImageChannelOrder, MemObjectType,
};
use crate::flags;
use crate::prm::Int4;
use crate::standard::{Device, Image, Platform, ProQue, Sampler};
use crate::tests;
// const ADDEND: [i32; 4] = [1; 4];
const DIMS: [usize; 3] = [64, 128, 4];
const TEST_ITERS: i32 = 4;
#[test]
fn image_ops() {
#[allow(non_snake_case)]
let ADDEND: Int4 = Int4::new(1, 1, 1, 1);
let src = r#"
#pragma OPENCL EXTENSION cl_khr_3d_image_writes : enable
__kernel void add(
sampler_t sampler_host,
__private int4 addend,
__read_only image3d_t img_src,
__write_only image3d_t img_dst)
{
int4 coord = (int4)(get_global_id(0), get_global_id(1), get_global_id(2), 0);
int4 pixel = read_imagei(img_src, sampler_host, coord);
pixel += addend;
write_imagei(img_dst, coord, pixel);
}
__kernel void fill(
sampler_t sampler_host,
__private int4 pixel,
__write_only image3d_t img)
{
int4 coord = (int4)(get_global_id(0), get_global_id(1), get_global_id(2), 0);
write_imagei(img, coord, pixel);
}
"#;
let platform = Platform::default();
let device = Device::first(platform).unwrap();
// Ensure `cl_khr_3d_image_writes` is available.
match device.info(DeviceInfo::Extensions).unwrap() {
DeviceInfoResult::Extensions(exts) => {
println!("exts: \n {:?}", exts);
if !exts.contains("cl_khr_3d_image_writes") {
println!(
"Skipping 'tests::image_ops': The 'cl_khr_3d_image_writes' is not available."
);
return;
}
}
_ => unreachable!(),
}
let proque = ProQue::builder()
.platform(platform)
.device(device)
.src(src)
.dims(DIMS)
.build()
.unwrap();
let sampler = Sampler::new(
proque.context(),
false,
AddressingMode::None,
FilterMode::Nearest,
)
.unwrap();
let mut vec = vec![0i32; proque.dims().to_len() * 4];
// Source and destination images and a vec to shuffle data:
let img_src = Image::<i32>::builder()
.channel_order(ImageChannelOrder::Rgba)
.channel_data_type(ImageChannelDataType::SignedInt32)
.image_type(MemObjectType::Image3d)
.dims(proque.dims())
.flags(flags::MEM_READ_WRITE | flags::MEM_COPY_HOST_PTR)
.copy_host_slice(&vec)
.queue(proque.queue().clone())
.build()
.unwrap();
let img_dst = Image::<i32>::builder()
.channel_order(ImageChannelOrder::Rgba)
.channel_data_type(ImageChannelDataType::SignedInt32)
.image_type(MemObjectType::Image3d)
.dims(proque.dims())
.flags(flags::MEM_WRITE_ONLY | flags::MEM_COPY_HOST_PTR)
.copy_host_slice(&vec)
.queue(proque.queue().clone())
.build()
.unwrap();
let kernel_add = proque
.kernel_builder("add")
.arg_sampler(&sampler)
.arg(ADDEND)
.arg(&img_src)
.arg(&img_dst)
.build()
.unwrap();
let kernel_fill_src = proque
.kernel_builder("fill")
.arg_sampler(&sampler)
.arg_named::<Int4, _, _>("pixel", &Int4::splat(0))
.arg(&img_src)
.build()
.unwrap();
//========================================================================
//========================================================================
//============================ Warm Up Run ===============================
//========================================================================
//========================================================================
// Make sure that pro_que's dims are correct:
let dims = proque.dims().to_lens().unwrap();
assert_eq!(DIMS, dims);
assert_eq!(
DIMS,
kernel_add.default_global_work_size().to_lens().unwrap()
);
// Verify image and vector lengths:
let len = proque.dims().to_len();
assert_eq!(img_src.dims().to_len(), len);
assert_eq!(img_dst.dims().to_len(), len);
let pixel_element_len = img_src.pixel_element_len();
assert_eq!(vec.len(), len * pixel_element_len);
// KERNEL RUN #1 -- make sure everything's working normally:
unsafe {
kernel_add.enq().unwrap();
}
let mut ttl_runs = 1i32;
// READ AND VERIFY #1 (LINEAR):
img_dst.read(&mut vec).enq().unwrap();
// Verify that the `verify_vec_rect` function isn't letting something slip:
for idx in 0..vec.len() {
assert!(
vec[idx] == ADDEND[0] * ttl_runs,
"vec[{}]: {}",
idx,
vec[idx]
);
}
print!("\n");
// Warm up the verify function:
tests::verify_vec_rect(
[0, 0, 0],
dims,
ADDEND[0] * ttl_runs,
ADDEND[0] * (ttl_runs - 1),
dims,
pixel_element_len,
&vec,
ttl_runs,
true,
)
.unwrap();
//========================================================================
//========================================================================
//======================= Read / Write / Copy ============================
//========================================================================
//========================================================================
for _ in 0..TEST_ITERS {
let (region, origin) = (dims, [0, 0, 0]);
//====================================================================
//=================== `core::enqueue_..._image()` ====================
//====================================================================
// Write to src:
unsafe {
core::enqueue_write_image(
proque.queue(),
&img_src,
true,
origin,
region,
0,
0,
&vec,
None::<core::Event>,
None::<&mut core::Event>,
)
.unwrap();
}
// Add from src to dst:
unsafe {
kernel_add.enq().expect("[FIXME]: HANDLE ME!");
}
ttl_runs += 1;
let (cur_val, old_val) = (ADDEND[0] * ttl_runs, ADDEND[0] * (ttl_runs - 1));
// Read into vec:
unsafe {
core::enqueue_read_image(
proque.queue(),
&img_dst,
true,
origin,
region,
0,
0,
&mut vec,
None::<core::Event>,
None::<&mut core::Event>,
)
.unwrap();
}
// Just to make sure read is complete:
proque.queue().finish().unwrap();
// Verify:
tests::verify_vec_rect(
origin,
region,
cur_val,
old_val,
dims,
pixel_element_len,
&vec,
ttl_runs,
true,
)
.unwrap();
// Run kernel:
ttl_runs += 1;
let (cur_val, old_val) = (ADDEND[0] * ttl_runs, ADDEND[0] * (ttl_runs - 1));
let cur_pixel = Int4::new(cur_val, cur_val, cur_val, cur_val);
unsafe {
kernel_fill_src.set_arg("pixel", cur_pixel).unwrap();
kernel_fill_src.enq().expect("[FIXME]: HANDLE ME!");
}
core::enqueue_copy_image::<_, _>(
proque.queue(),
&img_src,
&img_dst,
origin,
origin,
region,
None::<core::Event>,
None::<&mut core::Event>,
)
.unwrap();
// Read into vec:
unsafe {
core::enqueue_read_image(
proque.queue(),
&img_dst,
true,
origin,
region,
0,
0,
&mut vec,
None::<core::Event>,
None::<&mut core::Event>,
)
.unwrap();
}
// Just to make sure read is complete:
proque.queue().finish().unwrap();
// Verify:
tests::verify_vec_rect(
origin,
region,
cur_val,
old_val,
dims,
pixel_element_len,
&vec,
ttl_runs,
true,
)
.unwrap();
//====================================================================
//========================= `Image::cmd()...` ========================
//====================================================================
// Write to src:
img_src.cmd().write(&vec).enq().unwrap();
// Add from src to dst:
unsafe {
kernel_add.enq().expect("[FIXME]: HANDLE ME!");
}
ttl_runs += 1;
let (cur_val, old_val) = (ADDEND[0] * ttl_runs, ADDEND[0] * (ttl_runs - 1));
// // Read into vec:
img_dst.cmd().read(&mut vec).enq().unwrap();
// Verify:
tests::verify_vec_rect(
origin,
region,
cur_val,
old_val,
dims,
pixel_element_len,
&vec,
ttl_runs,
true,
)
.unwrap();
// Run kernel:
ttl_runs += 1;
let (cur_val, old_val) = (ADDEND[0] * ttl_runs, ADDEND[0] * (ttl_runs - 1));
let cur_pixel = Int4::new(cur_val, cur_val, cur_val, cur_val);
unsafe {
kernel_fill_src.set_arg("pixel", cur_pixel).unwrap();
kernel_fill_src.enq().expect("[FIXME]: HANDLE ME!");
}
img_src.cmd().copy(&img_dst, origin).enq().unwrap();
// Read into vec:
img_dst.cmd().read(&mut vec).enq().unwrap();
// Verify:
tests::verify_vec_rect(
origin,
region,
cur_val,
old_val,
dims,
pixel_element_len,
&vec,
ttl_runs,
true,
)
.unwrap();
}
println!("{} total test runs complete.\n", ttl_runs);
}