Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
GiggleLiu committed Jan 14, 2024
2 parents 0efba65 + 846b354 commit 8ee73dc
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 137 deletions.
21 changes: 21 additions & 0 deletions CITATION.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@article{liu2021tropical,
title={Tropical tensor network for ground states of spin glasses},
author={Liu, Jin-Guo and Wang, Lei and Zhang, Pan},
journal={Physical Review Letters},
volume={126},
number={9},
pages={090506},
year={2021},
publisher={APS}
}

@article{liu2023computing,
title={Computing solution space properties of combinatorial optimization problems via generic tensor networks},
author={Liu, Jin-Guo and Gao, Xun and Cain, Madelyn and Lukin, Mikhail D and Wang, Sheng-Tao},
journal={SIAM Journal on Scientific Computing},
volume={45},
number={3},
pages={A1239--A1270},
year={2023},
publisher={SIAM}
}
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TropicalGEMM"
uuid = "a4ad3063-64a7-4bad-8738-34ed09bc0236"
authors = ["GiggleLiu <[email protected]> and contributors"]
version = "0.1.11"
version = "0.2.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand All @@ -15,7 +15,7 @@ VectorizationBase = "3d5dd08c-fd9d-11e8-17fa-ed2836048c2f"
LoopVectorization = "0.12.4"
Octavian = "0.2.18, 0.3"
PrecompileTools = "1"
TropicalNumbers = "0.2.3, 0.3, 0.4, 0.5"
TropicalNumbers = "0.6"
VectorizationBase = "0.21"
julia = "1"

Expand Down
37 changes: 20 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,33 @@
[![Build Status](https://github.com/TensorBFS/TropicalGEMM.jl/workflows/CI/badge.svg)](https://github.com/TensorBFS/TropicalGEMM.jl/actions)
[![codecov](https://codecov.io/gh/TensorBFS/TropicalGEMM.jl/branch/master/graph/badge.svg?token=8F6PH5Q9PL)](https://codecov.io/gh/TensorBFS/TropicalGEMM.jl)

The fastest tropical matrix multiplication in the world!
The fastest Tropical matrix multiplication in the world! Supported matrix element types include
* max-plus algebra: `Tropical{BlasType}`
* min-plus algebra numbers: `TropicalMinPlus{BlasType}`
* max-times algebra numbers: `TropicalMaxMul{BlasType}`

## See the discussion here

https://github.com/JuliaSIMD/LoopVectorization.jl/issues/201
Please check [`TropicalNumbers.jl`](https://github.com/TensorBFS/TropicalNumbers.jl) for the definitions of these types. The `BlasType` is the storage type, which could be one of `Bool, Float16, Float32, Float64, Int16, Int32, Int64, Int8, UInt16, UInt32, UInt64, UInt8, SIMDTypes.Bit`.

## Get started

Open a Julia REPL and type `]` to enter the `pkg>` mode, and then install related packages with
```julia
pkg> add TropicalNumbers, Octavian, TropicalGEMM, BenchmarkTools
pkg> add TropicalGEMM, BenchmarkTools, TropicalNumbers
```

In a julia REPL, you can try a minimum working example
Loading `TropicalGEMM` module into the workspace affects the `*` on Tropical matrices immediately. The following is a minimum working example
```julia
julia> using TropicalNumbers, Octavian, TropicalGEMM, BenchmarkTools
julia> using TropicalNumbers, BenchmarkTools

julia> a = Tropical.(randn(1000, 1000))
julia> a = Tropical.(randn(1000, 1000));

julia> @benchmark Octavian.matmul_serial($a, $a)
```
julia> @btime $a * $a;
2.588 s (6 allocations: 7.66 MiB)

**Warning:** using TropicalGEMM will overload the `mul!` function for Tropical numbers.
julia> using TropicalGEMM

julia> @btime $a * $a;
66.916 ms (2 allocations: 7.63 MiB)
```

## Benchmarks

Expand All @@ -35,8 +39,7 @@ The benchmark and plotting scripts could be found in the benchmarks folder.
![Float64](benchmarks/benchmark-float64.png)
![Float32](benchmarks/benchmark-float32.png)


## Warnings

It is expected to have an ambiguity error when one uses both `TropicalGEMM` and `CUDA`.
If you see these errors, please include `example/cudapatch.jl` in your project.
## References
1. This package originates from the following issue:
https://github.com/JuliaSIMD/LoopVectorization.jl/issues/201
2. For applications, please check the papers listed in the [CITATION.bib](/CITATION.bib).
2 changes: 1 addition & 1 deletion src/TropicalGEMM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using VectorizationBase: OffsetPrecalc, StaticBool, Bit, static, NativeTypes, In
using VectorizationBase: contiguous_batch_size, contiguous_axis, val_stride_rank, bytestrides, offsets, memory_reference,
vmaximum, fmap, FloatingTypes, IntegerIndex, LazyMulAdd

export Tropical, TropicalF64, TropicalF32
export Tropical, TropicalF64, TropicalF32, TropicalMinPlus, TropicalMinPlusF64, TropicalMinPlusF32, TropicalMaxMul, TropicalMaxMulF64, TropicalMaxMulF32, TropicalMaxPlus, TropicalMaxPlusF64, TropicalMaxPlusF32, BlasSemiringTypes

include("fallbacks.jl")
include("gemm.jl")
Expand Down
10 changes: 6 additions & 4 deletions src/fallbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ end

# For types not nativelly supported, go to fallback.
# Overwrite the `mul!` in LinearAlgebra (also changes the behavior of `*` in Base)!
function LinearAlgebra.mul!(o::MaybeAdjOrTransMat{TO}, a::MaybeAdjOrTransMat{<:Tropical}, b::MaybeAdjOrTransMat{<:Tropical}, α::Number, β::Number) where TO<:Tropical
α = _convert_to_static(TO, α)
β = _convert_to_static(TO, β)
naive_mul!(o, a, b, α, β)
for TT in [:Tropical, :TropicalMinPlus, TropicalMaxMul]
@eval function LinearAlgebra.mul!(o::MaybeAdjOrTransMat{TO}, a::MaybeAdjOrTransMat{<:$TT}, b::MaybeAdjOrTransMat{<:$TT}, α::Number, β::Number) where TO<:$TT
α = _convert_to_static(TO, α)
β = _convert_to_static(TO, β)
naive_mul!(o, a, b, α, β)
end
end

Base.:*(a::T, b::StaticInt{0}) where T<:TropicalTypes = zero(T)
Expand Down
Loading

0 comments on commit 8ee73dc

Please sign in to comment.