From 20d1bc9ff007d97305de786ea750f9bf5fa49633 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Tue, 3 Sep 2024 16:56:17 +0200 Subject: [PATCH] fixup! fixup! fixup! add macro to create custom Ops also on aarch64 --- src/operators.jl | 18 ++++++++++++++---- test/test_reduce.jl | 8 +++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/operators.jl b/src/operators.jl index 35ef4ce13..16e2a4737 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -108,24 +108,34 @@ function Op(f, T=Any; iscommutative=false) return op end +""" + @Op(f, T) + +Define a custom operator [`Op`](@ref) using the function `f`. + +""" macro Op(f, T) name_wrapper = gensym(Symbol(f, :_, T, :_wrapper)) name_fptr = gensym(Symbol(f, :_, T, :_ptr)) name_module = gensym(Symbol(f, :_, T, :_module)) - esc(quote + expr = quote module $(name_module) + import ..$f, ..$T $(name_wrapper) = $OpWrapper{typeof($f),$T}($f) $(name_fptr) = @cfunction($(name_wrapper), Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cint}, Ptr{$MPI_Datatype})) function __init__() global $(name_fptr) = @cfunction($(name_wrapper), Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cint}, Ptr{$MPI_Datatype})) end - function $Op(::typeof($f), ::Type{T}; iscommutative=true) - op = $Op($OP_NULL.val, $(name_fptr)) + import MPI: Op + function Op(::typeof($f), ::Type{$T}; iscommutative=true) + op = Op($OP_NULL.val, $(name_fptr)) # int MPI_Op_create(MPI_User_function* user_fn, int commute, MPI_Op* op) $API.MPI_Op_create($(name_fptr), iscommutative, op) finalizer($free, op) end end - end) + end + expr.head = :toplevel + esc(expr) end diff --git a/test/test_reduce.jl b/test/test_reduce.jl index 7e39ac17b..6ea6cd967 100644 --- a/test/test_reduce.jl +++ b/test/test_reduce.jl @@ -59,12 +59,10 @@ if isroot @test sum_mesg == sz .* mesg end -@eval begin - function my_reduce(x, y) - 2x+y-x - end - MPI.@Op(my_reduce, Int) +function my_reduce(x, y) + 2x+y-x end +MPI.@Op(my_reduce, Int) if can_do_closures operators = [MPI.SUM, +, my_reduce, (x,y) -> 2x+y-x]