Skip to content
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

Arithmetic operations are not performed #66

Closed
alexandrosstamatiou opened this issue May 11, 2022 · 12 comments
Closed

Arithmetic operations are not performed #66

alexandrosstamatiou opened this issue May 11, 2022 · 12 comments
Labels
bug Something isn't working

Comments

@alexandrosstamatiou
Copy link

Hi again,
I'm sorry, but there is another issue which I thought to mention in a different post. After resolving the method errors following your suggestions in the previous Issue post, the computation ran but wouldn't terminate! Many potential reasons of course, but I discovered that simple arithmetic operations of Posit16 values don't work on my Mac (M1 processor):

julia> a = Posit16(1.0);
julia> a + a
Posit16(1.0)

The same lines on the Windows machine give

julia> a = Posit16(1.0);
julia> a + a
Posit16(2.0)

Any ideas why this would happen? Recall that I installed the via Main to get SoftPosit.jl working on the Mac...

@giordano
Copy link
Contributor

I'd have to debug it a bit and don't really have the time now, but this is likely an upstream bug in https://gitlab.com/cerlane/SoftPosit, since the operation you're doing is

+(x::Posit16,y::Posit16) = ccall((:p16_add, SoftPositPath), Posit16, (Posit16,Posit16),x,y)

@milankl
Copy link
Owner

milankl commented May 11, 2022

Thanks Mosè for looking into this. Indeed, while I've been trying to replace piece by piece of C-SoftPosit with pure Julia implementations, all arithmetic operations are still using a ccall. Alexandros, could you run the unit tests on your M1 with #main by doing

julia>] test SoftPosit

and post the test summaries here?

@milankl milankl added the bug Something isn't working label May 11, 2022
@alexandrosstamatiou
Copy link
Author

alexandrosstamatiou commented May 11, 2022

Thank you very much, indeed, @giordano.
See below for the test output:

     Testing SoftPosit
      Status `/private/var/folders/ft/_gwtkwb96yn4wmjn991hcjy00000gn/T/jl_8Hnpby/Project.toml`
  [0775deef] SoftPosit v0.4.0 `https://github.com/milankl/SoftPosit.jl#main`
  [f9aa12f2] SoftPosit_jll v0.4.2+0
  [8dfed614] Test `@stdlib/Test`
      Status `/private/var/folders/ft/_gwtkwb96yn4wmjn991hcjy00000gn/T/jl_8Hnpby/Manifest.toml`
  [692b3bcd] JLLWrappers v1.4.1
  [21216c6a] Preferences v1.3.0
  [0775deef] SoftPosit v0.4.0 `https://github.com/milankl/SoftPosit.jl#main`
  [f9aa12f2] SoftPosit_jll v0.4.2+0
  [0dad84c5] ArgTools `@stdlib/ArgTools`
  [56f22d72] Artifacts `@stdlib/Artifacts`
  [2a0f44e3] Base64 `@stdlib/Base64`
  [ade2ca70] Dates `@stdlib/Dates`
  [f43a241f] Downloads `@stdlib/Downloads`
  [b77e0a4c] InteractiveUtils `@stdlib/InteractiveUtils`
  [b27032c2] LibCURL `@stdlib/LibCURL`
  [76f85450] LibGit2 `@stdlib/LibGit2`
  [8f399da3] Libdl `@stdlib/Libdl`
  [56ddb016] Logging `@stdlib/Logging`
  [d6f4376e] Markdown `@stdlib/Markdown`
  [ca575930] NetworkOptions `@stdlib/NetworkOptions`
  [44cfe95a] Pkg `@stdlib/Pkg`
  [de0858da] Printf `@stdlib/Printf`
  [3fa0cd96] REPL `@stdlib/REPL`
  [9a3f8284] Random `@stdlib/Random`
  [ea8e919c] SHA `@stdlib/SHA`
  [9e88b42a] Serialization `@stdlib/Serialization`
  [6462fe0b] Sockets `@stdlib/Sockets`
  [fa267f1f] TOML `@stdlib/TOML`
  [a4e569a6] Tar `@stdlib/Tar`
  [8dfed614] Test `@stdlib/Test`
  [cf7118a7] UUIDs `@stdlib/UUIDs`
  [4ec0a83e] Unicode `@stdlib/Unicode`
  [deac9b47] LibCURL_jll `@stdlib/LibCURL_jll`
  [29816b5a] LibSSH2_jll `@stdlib/LibSSH2_jll`
  [c8ffd9c3] MbedTLS_jll `@stdlib/MbedTLS_jll`
  [14a3606d] MozillaCACerts_jll `@stdlib/MozillaCACerts_jll`
  [83775a58] Zlib_jll `@stdlib/Zlib_jll`
  [8e850ede] nghttp2_jll `@stdlib/nghttp2_jll`
  [3f19e933] p7zip_jll `@stdlib/p7zip_jll`
     Testing Running tests...
Test Summary: | Pass  Broken  Total
Conversions   |    9       2     11
0,±1,±2,±4: Test Failed at /Users/alexandrosstamatiou/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:41
  Expression: Posit16(f) == Posit16_old(f)
   Evaluated: Posit16(-4.0) == Posit16(0.0)
Stacktrace:
 [1] macro expansion
   @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Test/src/Test.jl:445 [inlined]
 [2] macro expansion
   @ ~/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:41 [inlined]
 [3] macro expansion
   @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Test/src/Test.jl:1283 [inlined]
 [4] top-level scope
   @ ~/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:40
0,±1,±2,±4: Test Failed at /Users/alexandrosstamatiou/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:41
  Expression: Posit16(f) == Posit16_old(f)
   Evaluated: Posit16(-2.0) == Posit16(0.0)
Stacktrace:
 [1] macro expansion
   @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Test/src/Test.jl:445 [inlined]
 [2] macro expansion
   @ ~/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:41 [inlined]
 [3] macro expansion
   @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Test/src/Test.jl:1283 [inlined]
 [4] top-level scope
   @ ~/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:40
0,±1,±2,±4: Test Failed at /Users/alexandrosstamatiou/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:41
  Expression: Posit16(f) == Posit16_old(f)
   Evaluated: Posit16(-1.0) == Posit16(0.0)
Stacktrace:
 [1] macro expansion
   @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Test/src/Test.jl:445 [inlined]
 [2] macro expansion
   @ ~/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:41 [inlined]
 [3] macro expansion
   @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Test/src/Test.jl:1283 [inlined]
 [4] top-level scope
   @ ~/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:40
0,±1,±2,±4: Test Failed at /Users/alexandrosstamatiou/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:41
  Expression: Posit16(f) == Posit16_old(f)
   Evaluated: Posit16(1.0) == Posit16(0.0)
Stacktrace:
 [1] macro expansion
   @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Test/src/Test.jl:445 [inlined]
 [2] macro expansion
   @ ~/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:41 [inlined]
 [3] macro expansion
   @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Test/src/Test.jl:1283 [inlined]
 [4] top-level scope
   @ ~/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:40
0,±1,±2,±4: Test Failed at /Users/alexandrosstamatiou/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:41
  Expression: Posit16(f) == Posit16_old(f)
   Evaluated: Posit16(2.0) == Posit16(0.0)
Stacktrace:
 [1] macro expansion
   @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Test/src/Test.jl:445 [inlined]
 [2] macro expansion
   @ ~/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:41 [inlined]
 [3] macro expansion
   @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Test/src/Test.jl:1283 [inlined]
 [4] top-level scope
   @ ~/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:40
0,±1,±2,±4: Test Failed at /Users/alexandrosstamatiou/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:41
  Expression: Posit16(f) == Posit16_old(f)
   Evaluated: Posit16(4.0) == Posit16(0.0)
Stacktrace:
 [1] macro expansion
   @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Test/src/Test.jl:445 [inlined]
 [2] macro expansion
   @ ~/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:41 [inlined]
 [3] macro expansion
   @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Test/src/Test.jl:1283 [inlined]
 [4] top-level scope
   @ ~/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:40
Test Summary: | Pass  Fail  Total
0,±1,±2,±4    |    8     6     14
ERROR: LoadError: Some tests did not pass: 8 passed, 6 failed, 0 errored, 0 broken.
in expression starting at /Users/alexandrosstamatiou/.julia/packages/SoftPosit/hat2g/test/test_conversions.jl:39
in expression starting at /Users/alexandrosstamatiou/.julia/packages/SoftPosit/hat2g/test/runtests.jl:4
ERROR: Package SoftPosit errored during testing

@giordano
Copy link
Contributor

@alexandrosstamatiou could you please quote all your code blocks with triple backquotes? That'd make reading these long blocks much easier. Thanks.

@alexandrosstamatiou
Copy link
Author

Sorry - is this what you meant?

@giordano
Copy link
Contributor

Yup, it looks much better 🙂 Thanks!

@milankl
Copy link
Owner

milankl commented May 12, 2022 via email

@milankl
Copy link
Owner

milankl commented May 16, 2022

At the moment, defining the arithmetic operations as conversions with Float32 is even faster

julia> using SoftPosit, BenchmarkTools

julia> A,B = Posit16.(rand(1000,1000)),Posit16.(rand(1000,1000));

julia> @btime +($A,$B);
  25.558 ms (2 allocations: 1.91 MiB)

julia> Base.:(+)(x::Posit16,y::Posit16) = Posit16(Float32(x)+Float32(y))

julia> @btime +($A,$B);
  18.911 ms (2 allocations: 1.91 MiB)

Probably because I tried to squeeze some performance out of the Posit16-Float32 conversion in Julia in #55 but + still calls the C code that turned out wasn't always the fastest.

@milankl
Copy link
Owner

milankl commented May 16, 2022

Also meaning that I'd be happy to accept a PR that replaces ccall-arithmetic operations by pure Julia ones (whether via conversion or if anyone wants to write a posit addition from scratch, feel free 😄 )

@milankl
Copy link
Owner

milankl commented Jun 10, 2022

upcoming v0.5 will be independent of C-SoftPosit. I overhauled the conversions and all arithmetics now use those to instead of calling specific posit-arithmetic functions. At the moment this is, despite the detour via float arithmetic, even faster than C-SoftPosit and the conversions are much faster (see #69). Feel free to report here whether #68 (branch mk/conversions) still fails on Apple M1, but given the removal of that C code it should work.

@giordano
Copy link
Contributor

julia> using SoftPosit

julia> a = Posit16(1.0);

julia> a + a
Posit16(2.0)

julia> versioninfo()
Julia Version 1.9.0-DEV.732
Commit ada860fe7d* (2022-06-10 15:07 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin21.5.0)
  CPU: 8 × Apple M1
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, apple-m1)
  Threads: 4 on 4 virtual cores

julia> pkgversion(SoftPosit)
v"0.5.0"

@milankl
Copy link
Owner

milankl commented Jul 26, 2022

Given that SoftPosit.jl v0.5 seems to work on M1, I'm closing this.

@milankl milankl closed this as completed Jul 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants