use cubecl_macros_internal::cube_op;
use crate::{
CanMaterialize, HasSideEffects, Pure,
dialect::{
matrix::{WgmmaCommitGroupOp, WgmmaWaitGroupOp},
tma::{CommitGroupOp, WaitGroupOp, WaitGroupReadOp},
},
prelude::*,
types::pending::{AsyncGroup, GroupTokenType, WaitUntil},
};
impl AsyncGroup {
pub fn commit_op(self, ctx: &mut Context) -> Ptr<Operation> {
match self {
AsyncGroup::Warpgroup => WgmmaCommitGroupOp::new(ctx).get_operation(),
AsyncGroup::BulkCopy => CommitGroupOp::new(ctx).get_operation(),
}
}
pub fn wait_op(
self,
ctx: &mut Context,
max_pending: usize,
until: WaitUntil,
) -> Ptr<Operation> {
match (self, until) {
(AsyncGroup::Warpgroup, _) => WgmmaWaitGroupOp::new(ctx, max_pending).get_operation(),
(AsyncGroup::BulkCopy, WaitUntil::Released) => {
WaitGroupReadOp::new(ctx, max_pending).get_operation()
}
(AsyncGroup::BulkCopy, WaitUntil::Complete) => {
WaitGroupOp::new(ctx, max_pending).get_operation()
}
}
}
}
fn token_ty(ctx: &Context, group: &AsyncGroup) -> TypeHandle {
GroupTokenType::get(ctx, *group).into()
}
#[cube_op(name = "pending.commit")]
#[result_ty(from_inputs = |ctx, group: &AsyncGroup| token_ty(ctx, group))]
#[op_traits(CanMaterialize, HasSideEffects)]
pub struct CommitOp {
pub group: AsyncGroup,
}
#[cube_op(name = "pending.ready")]
#[result_ty(from_inputs = |ctx, group: &AsyncGroup| token_ty(ctx, group))]
#[op_traits(CanMaterialize, Pure)]
pub struct ReadyOp {
pub group: AsyncGroup,
}
#[cube_op(name = "pending.wait")]
#[result_ty(none)]
#[op_traits(CanMaterialize, HasSideEffects)]
pub struct WaitOp {
pub token: Value,
pub until: WaitUntil,
}