milankl / SoftPosit.jl

A posit arithmetic emulator.
MIT License
47 stars 9 forks source link

Arithmetic operations are not performed #66

Closed alexandrosstamatiou closed 2 years ago

alexandrosstamatiou commented 2 years ago

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 commented 2 years ago

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 https://github.com/milankl/SoftPosit.jl/blob/aa307d50bf4e250d4f49cf999e19f720fc228881/src/arithmetic.jl#L3

milankl commented 2 years ago

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?

alexandrosstamatiou commented 2 years ago

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 commented 2 years ago

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

alexandrosstamatiou commented 2 years ago

Sorry - is this what you meant?

giordano commented 2 years ago

Yup, it looks much better :slightly_smiling_face: Thanks!

milankl commented 2 years ago

As we thought, the problem is not the Julia code but the C code. In the Posit16(x) == Posit16_old(x) the first function calls a conversion that I’ve written in Julia to replace the C version and Posit16_old calls C SoftPosit. However that also means you could, for the time being, redefine all arithmetic operations via back&forth conversion. Like so

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

On 11 May 2022, at 19:33, Mosè Giordano @.***> wrote:

 Yup, it looks much better 🙂 Thanks!

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.

milankl commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago
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 commented 2 years ago

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