use cubecl::{
TestRuntime,
prelude::ComputeClient,
server::{self, LaunchError, ServerError},
};
use crate::ExecutionOutcome;
pub fn launch_and_capture_outcome<F>(
client: &ComputeClient<TestRuntime>,
launch: F,
) -> ExecutionOutcome
where
F: FnOnce(&ComputeClient<TestRuntime>) -> ExecutionOutcome,
{
let outcome = flush_compile_error(client).unwrap_or_else(|| launch(client));
match outcome {
ExecutionOutcome::Executed => {
flush_compile_error(client).unwrap_or(ExecutionOutcome::Executed)
}
other => other,
}
}
pub fn flush_compile_error(client: &ComputeClient<TestRuntime>) -> Option<ExecutionOutcome> {
match client.flush() {
Ok(_) => None,
Err(ServerError::ServerUnhealthy { errors, .. }) => {
for error in errors.iter() {
if let server::ServerError::Launch(LaunchError::TooManyResources(_))
| server::ServerError::Launch(LaunchError::CompilationError(_)) = error
{
return Some(ExecutionOutcome::CompileError(format!("{errors:?}")));
}
}
None
}
Err(err) => Some(ExecutionOutcome::CompileError(format!("{err:?}"))),
}
}