-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tests using other real types #591
Comments
As far as I can tell,
|
This will require some changes because of Julia's automatic type promotion for mixed-precision floating point arithmetic. For example, this flux calculation includes multiplication with For example,
The machine code for e.g. the
|
Thanks for your input, @stillyslalom! Sure, that's part of the process. The first step will be to fix everything such that we can run simulations using plain |
Of course, it would be a great PR to
|
Right now, I hesitate a bit to use rational constants. Unless (something like) JuliaLang/julia#41258 is merged, there can and will be surprising problems. For example, the case of one half reported by @stillyslalom above is fine julia> f(x) = x * 1//2
f (generic function with 1 method)
julia> @code_native debuginfo=:none syntax=:intel f(1.5)
.text
movabs rax, offset .rodata.cst8
vmulsd xmm0, xmm0, qword ptr [rax]
ret
nop but other rational constant will be problematic, e.g. julia> f(x) = x * 1//3
f (generic function with 1 method)
julia> @code_native debuginfo=:none syntax=:intel f(1.5)
.text
sub rsp, 40
vmovsd qword ptr [rsp + 8], xmm0
movabs rax, offset divgcd
lea rdi, [rsp + 16]
mov esi, 1
mov edx, 3
call rax
mov rax, qword ptr [rsp + 24]
test rax, rax
js L54
mov rcx, qword ptr [rsp + 16]
jmp L66
L54:
neg rax
js L91
xor ecx, ecx
sub rcx, qword ptr [rsp + 16]
L66:
vcvtsi2sd xmm0, xmm1, rcx
vcvtsi2sd xmm1, xmm1, rax
vdivsd xmm0, xmm0, xmm1
vmulsd xmm0, xmm0, qword ptr [rsp + 8]
add rsp, 40
ret
L91:
movabs rax, offset jl_system_image_data
mov qword ptr [rsp + 32], rax
movabs rax, offset jl_invoke
movabs rdi, offset jl_system_image_data
lea rsi, [rsp + 32]
movabs rcx, 140365791294448
mov edx, 1
call rax
ud2
nop word ptr cs:[rax + rax] I don't really like the (only?) ways to get good code using something like julia> f(x::T) where {T} = x * (T(1) / T(3))
f (generic function with 1 method)
julia> @code_native debuginfo=:none syntax=:intel f(1.5)
.text
movabs rax, offset .rodata.cst8
vmulsd xmm0, xmm0, qword ptr [rax]
ret
nop
julia> f(x) = x * Base.unsafe_rational(Int, 1, 3)
f (generic function with 1 method)
julia> @code_native debuginfo=:none syntax=:intel f(1.5)
.text
movabs rax, offset .rodata.cst8
vmulsd xmm0, xmm0, qword ptr [rax]
ret
nop since that's considerably less readable, I think. |
I agree. I could live with writing fractions using double |
Now that JuliaLang/julia#41258 is merged, we might be able to use this form of providing constants in Julia v1.8 (or whenever the change is included in a new release). Edit: I guess it's v1.8 - it's already available in the nightly builds julia> f(x) = x * 1//2
f (generic function with 1 method)
julia> @code_native debuginfo=:none f(1.5)
.text
.file "f"
.section .rodata.cst8,"aM",@progbits,8
.p2align 3 # -- Begin function julia_f_54
.LCPI0_0:
.quad 0x3fe0000000000000 # double 0.5
.text
.globl julia_f_54
.p2align 4, 0x90
.type julia_f_54,@function
julia_f_54: # @julia_f_54
.cfi_startproc
# %bb.0: # %top
movabsq $.LCPI0_0, %rax
vmulsd (%rax), %xmm0, %xmm0
retq
.Lfunc_end0:
.size julia_f_54, .Lfunc_end0-julia_f_54
.cfi_endproc
# -- End function
.section ".note.GNU-stack","",@progbits
julia> f(x) = x * 2//3
f (generic function with 1 method)
julia> @code_native debuginfo=:none f(1.5)
.text
.file "f"
.section .rodata.cst8,"aM",@progbits,8
.p2align 3 # -- Begin function julia_f_109
.LCPI0_0:
.quad 0x3fe5555555555555 # double 0.66666666666666663
.text
.globl julia_f_109
.p2align 4, 0x90
.type julia_f_109,@function
julia_f_109: # @julia_f_109
.cfi_startproc
# %bb.0: # %top
movabsq $.LCPI0_0, %rax
vmulsd (%rax), %xmm0, %xmm0
retq
.Lfunc_end0:
.size julia_f_109, .Lfunc_end0-julia_f_109
.cfi_endproc
# -- End function
.section ".note.GNU-stack","",@progbits
julia> versioninfo()
Julia Version 1.8.0-DEV.1125
Commit fa8be6fda5 (2021-12-12 19:26 UTC) |
Our current code doesn't work well with julia> surface_flux_kernel1 = @cuda launch = false surface_flux_kernel1!(surface_flux_arr, interfaces_u, orientations, equations, surface_flux)
ERROR: InvalidIRError: compiling MethodInstance for surface_flux_kernel1!(::CuDeviceArray{Float32, 5, 1}, ::CuDeviceArray{Float32, 5, 1}, ::CuDeviceVector{Int32, 1}, ::CompressibleEulerEquations3D{Float32}, ::FluxLaxFriedrichs{typeof(max_abs_speed_naive)}) resulted in invalid LLVM IR
Reason: unsupported dynamic function invocation (call to __throw_rational_argerror_typemin(T) @ Base rational.jl:20)
Stacktrace:
[1] checked_den
@ ./rational.jl:24
[2] Rational
@ ./rational.jl:36
[3] Rational
@ ./rational.jl:39
[4] //
@ ./rational.jl:62
[5] flux
@ /tmp/trixi_cuda/trixi/src/equations/compressible_euler_3d.jl:379
[6] flux_central
@ /tmp/trixi_cuda/trixi/src/equations/numerical_fluxes.jl:20
... Thus, it seems to be better to use
Any thoughts, @trixi-framework/developers? |
Result from a local discussion: Maybe a macro? And maybe with the option to be disabled via Preferences.jl? |
After thinking more about it, I tend to prefer an explicit solution where we write |
We have discussed this again and agreed to proceed as follows:
Trixi.jl/src/solvers/dgsem_tree/indicators.jl Lines 116 to 118 in b1a84a6
|
I just came across DispatchDoctor.jl, which provides tools to guarantee type stability without any overhead. Is it worth thinking about using this package? |
Could be worth looking at. I'm just a bit concerned about "should" in
from the README - and the warning that it will increase precompile time quite a bit |
There is some discussion on the "should" part in this discourse thread. |
I would see it mostly useful for testing/CI. But that would require wrapping basically everything in Another aspect to consider is that we will still need tests to ensure that we return the correct type, e.g., for I don't want to be pessimistic - just discuss and list pro/contra arguments. It's definitely an interesting package and has been on my TODO list of things to look into more deeply. |
Please check the task box once you see something is already finished :) @ranocha Also, please change to improve |
We should check whether everything works using
Float32
Float64
(current default)It would also be nice to see the impact on performance.
Tasks
0.5 *
or1/2 *
to0.5f0 *
oftype
andconvert
in initial conditions, source terms etc.ln_mean
and other math.jl functions forFloat32
(adapt auxiliary math functions forFloat32
#2048)solve
)TreeMesh
, hardocded toFloat64
Trixi.jl/src/meshes/tree_mesh.jl
Lines 102 to 104 in 345f53a
StructuredMesh
UnstructuredMesh2D
P4estMesh
T8codeMesh
, hardcoded toFloat64
Trixi.jl/src/meshes/t8code_mesh.jl
Line 34 in 345f53a
DGMulti
, hardcoded in several placesTrixi.jl/src/solvers/dgmulti/shock_capturing.jl
Line 32 in 345f53a
ln_mean
)StepsizeCallback
AnalysisCallback
SaveSolutionCallback
Float32
#2048The text was updated successfully, but these errors were encountered: