opencl_api/api/
pipe.rs

1/*
2 * pipe.rs - Pipe API wrappers (Part of OpenCL Runtime Layer).
3 *
4 * Copyright 2020-2021 Naman Bishnoi
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18//!
19//! A pipe is a memory object that stores data organized as a FIFO.
20//!
21//! Pipe objects can only be accessed using built-in functions that read from and write to a pipe.
22//! Pipe objects are not accessible from the host. A pipe object encapsulates the following information:
23//! - Packet size in bytes
24//! - Maximum capacity in packets
25//! - Information about the number of packets currently in the pipe
26//! - Data packets
27//!
28use crate::objects::bitfields::MemFlags;
29use crate::objects::enums::{ParamValue, Size};
30use crate::objects::functions::status_update;
31use crate::objects::structs::{PipeInfo, StatusCode};
32use crate::objects::traits::GetSetGo;
33use crate::objects::types::{APIResult, ContextPtr, MemPtr};
34use crate::{gen_param_value, size_getter};
35use libc::c_void;
36use opencl_heads::ffi;
37use opencl_heads::ffi::clGetPipeInfo;
38use opencl_heads::types::*;
39use std::ptr;
40
41pub fn create_pipe(
42    context: &ContextPtr,
43    flags: MemFlags,
44    pipe_packet_size: cl_uint,
45    pipe_max_packets: cl_uint,
46    properties: *const cl_pipe_properties,
47) -> APIResult<MemPtr> {
48    let fn_name = "clCreatePipe";
49    let mut status_code = StatusCode::INVALID_VALUE;
50    let mem_ptr = unsafe {
51        ffi::clCreatePipe(
52            context.unwrap(),
53            flags.get(),
54            pipe_packet_size,
55            pipe_max_packets,
56            properties,
57            &mut status_code,
58        )
59    };
60    status_update(status_code, fn_name, MemPtr::from_ptr(mem_ptr, fn_name)?)
61}
62
63pub fn get_pipe_info(pipe: &MemPtr, param_name: cl_pipe_info) -> APIResult<ParamValue> {
64    let pipe = pipe.unwrap();
65    size_getter!(get_pipe_info_size, clGetPipeInfo);
66    match param_name {
67        PipeInfo::PACKET_SIZE | PipeInfo::MAX_PACKETS => {
68            let param_value = gen_param_value!(clGetPipeInfo, u32, pipe, param_name);
69            Ok(ParamValue::UInt(param_value))
70        }
71        PipeInfo::PROPERTIES => {
72            let size = get_pipe_info_size(pipe, param_name)?;
73            let param_value = gen_param_value!(clGetPipeInfo, isize, pipe, param_name, size);
74            Ok(ParamValue::ArrCPtr(param_value))
75        }
76        _ => status_update(40404, "clGetPipeInfo", ParamValue::default()),
77    }
78}
79
80// TODO: Add unit tests for this file.