1use std::fmt;
4
5use crate::api::{call, new_args};
6use crate::buffer::Buffer;
7use crate::client::{Client, await_event};
8use crate::error::{Error, Result};
9use crate::sys;
10
11pub struct LoadedExecutable {
13 client: Client,
14 raw: *mut sys::PJRT_LoadedExecutable,
15 num_outputs: usize,
16}
17
18unsafe impl Send for LoadedExecutable {}
20unsafe impl Sync for LoadedExecutable {}
22
23impl Drop for LoadedExecutable {
24 fn drop(&mut self) {
25 let mut args = new_args!(sys::PJRT_LoadedExecutable_Destroy_Args);
26 args.executable = self.raw;
27 let _ = call!(
30 self.client.inner.api,
31 PJRT_LoadedExecutable_Destroy,
32 &mut args
33 );
34 }
35}
36
37impl LoadedExecutable {
38 pub(crate) unsafe fn from_raw(
45 client: Client,
46 raw: *mut sys::PJRT_LoadedExecutable,
47 ) -> Result<Self> {
48 let api = client.inner.api;
49 let mut exe = Self {
51 client,
52 raw,
53 num_outputs: 0,
54 };
55
56 let mut get = new_args!(sys::PJRT_LoadedExecutable_GetExecutable_Args);
57 get.loaded_executable = raw;
58 call!(api, PJRT_LoadedExecutable_GetExecutable, &mut get)?;
60
61 let mut num = new_args!(sys::PJRT_Executable_NumOutputs_Args);
62 num.executable = get.executable;
63 let result = call!(api, PJRT_Executable_NumOutputs, &mut num);
65
66 let mut destroy = new_args!(sys::PJRT_Executable_Destroy_Args);
68 destroy.executable = get.executable;
69 call!(api, PJRT_Executable_Destroy, &mut destroy)?;
71
72 result?;
73 exe.num_outputs = num.num_outputs;
74 Ok(exe)
75 }
76
77 pub fn num_outputs(&self) -> usize {
79 self.num_outputs
80 }
81
82 pub fn execute(&self, args: &[&Buffer]) -> Result<Vec<Buffer>> {
92 let (outputs, done) = self.execute_with(args, &ExecuteOptions::default())?;
93 done.wait()?;
94 Ok(outputs)
95 }
96
97 pub fn execute_with(
111 &self,
112 args: &[&Buffer],
113 options: &ExecuteOptions,
114 ) -> Result<(Vec<Buffer>, Event)> {
115 let api = self.client.inner.api;
116 if args.iter().any(|b| !b.belongs_to(&self.client)) {
117 return Err(Error::HostBuffer(String::from(
118 "an argument belongs to another client",
119 )));
120 }
121 let arg_list: Vec<*mut sys::PJRT_Buffer> = args.iter().map(|b| b.raw).collect();
122 let arg_lists = [arg_list.as_ptr()];
123 let mut out_list: Vec<*mut sys::PJRT_Buffer> = vec![std::ptr::null_mut(); self.num_outputs];
124 let out_lists = [out_list.as_mut_ptr()];
125 let mut events = [std::ptr::null_mut::<sys::PJRT_Event>()];
126
127 let mut raw_options = new_args!(sys::PJRT_ExecuteOptions);
128 raw_options.non_donatable_input_indices = options.non_donatable.as_ptr();
129 raw_options.num_non_donatable_input_indices = options.non_donatable.len();
130 let mut exec = new_args!(sys::PJRT_LoadedExecutable_Execute_Args);
131 exec.executable = self.raw;
132 exec.options = &mut raw_options;
133 exec.argument_lists = arg_lists.as_ptr();
134 exec.num_devices = 1;
135 exec.num_args = arg_list.len();
136 exec.output_lists = out_lists.as_ptr();
137 exec.device_complete_events = events.as_mut_ptr();
138 call!(api, PJRT_LoadedExecutable_Execute, &mut exec)?;
143
144 let outputs: Vec<Buffer> = out_list
147 .iter()
148 .filter(|raw| !raw.is_null())
149 .map(|&raw| unsafe { Buffer::from_raw(self.client.clone(), raw) })
151 .collect();
152 let event = Event {
153 client: self.client.clone(),
154 raw: events[0],
155 };
156 if outputs.len() != self.num_outputs {
157 return Err(Error::Pjrt {
158 code: crate::ErrorCode::Internal,
159 message: String::from("execution returned a null buffer"),
160 });
161 }
162 Ok((outputs, event))
163 }
164}
165
166#[derive(Clone, Debug, Default)]
168pub struct ExecuteOptions {
169 pub non_donatable: Vec<i64>,
172}
173
174pub struct Event {
176 client: Client,
177 raw: *mut sys::PJRT_Event,
178}
179
180unsafe impl Send for Event {}
182
183impl Event {
184 pub fn wait(mut self) -> Result<()> {
190 let raw = std::mem::replace(&mut self.raw, std::ptr::null_mut());
191 unsafe { await_event(self.client.inner.api, raw) }
194 }
195}
196
197impl Drop for Event {
198 fn drop(&mut self) {
199 if self.raw.is_null() {
200 return;
201 }
202 let mut args = new_args!(sys::PJRT_Event_Destroy_Args);
203 args.event = self.raw;
204 let _ = call!(self.client.inner.api, PJRT_Event_Destroy, &mut args);
207 }
208}
209
210impl fmt::Debug for Event {
211 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212 f.debug_struct("Event").finish_non_exhaustive()
213 }
214}
215
216impl fmt::Debug for LoadedExecutable {
217 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
218 f.debug_struct("LoadedExecutable")
219 .field("num_outputs", &self.num_outputs)
220 .finish_non_exhaustive()
221 }
222}