Skip to content

Commit

Permalink
towards working tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lpawela committed Apr 10, 2024
1 parent a8f7d20 commit 2779ea0
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 130 deletions.
22 changes: 6 additions & 16 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,21 @@ authors = ["Bartłomiej Gardas <[email protected]>", "Dariusz Kurzyk <dkur
version = "1.0.0"

[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Bits = "1654ce90-6ed3-553a-957f-9452c3a40996"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
LabelledGraphs = "605abd48-4d17-4660-b914-d4df33194460"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LinearMaps = "7a12625a-238d-50fd-b39a-03d52299707e"
MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5"
SortingAlgorithms = "a2af1166-a08f-5f64-846c-94a0d3cef48c"
SpinGlassEngine = "0563570f-ea1b-4080-8a64-041ac6565a4e"
SpinGlassNetworks = "b7f6bd3e-55dc-4da6-96a9-ef9dbec6ac19"

[compat]
BenchmarkTools = "1.3"
Bits = "0.2"
CUDA = "3.12"
Distributions = "0.25"
DocStringExtensions = "0.8.6"
Flux = "0.13"
LabelledGraphs = "0.3"
LightGraphs = "1.3"
LinearMaps = "3.8"
MetaGraphs = "0.6, 0.7"
SortingAlgorithms = "1.0"
julia = "1.8, 1.9"
CUDA = "4"
DocStringExtensions = "0.8"
Graphs = "1.9"
LabelledGraphs = "0.4"
julia = "1.9, 1.10"

[extras]
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
Expand Down
24 changes: 12 additions & 12 deletions docs/src/man/details.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ k = 2
energies = CUDA.zeros(2^N);
threadsPerBlock = 2^k
blocksPerGrid = 2^(N-k)
threads = 512
blocks = cld(N, threads)
@cuda blocks=(blocksPerGrid) threads=(threadsPerBlock) SpinGlassExhaustive.kernel_qubo(cu_qubo, energies);
@cuda blocks=blocks threads=threads SpinGlassExhaustive.kernel_qubo(cu_qubo, energies);
states = sortperm(energies);
energies[states];
Expand All @@ -49,10 +49,10 @@ k = 2
energies = CUDA.zeros(2^N);
threadsPerBlock = 2^k
blocksPerGrid = 2^(N-k)
threads = 512
blocks = cld(N, threads)
@cuda blocks=(blocksPerGrid) threads=(threadsPerBlock) SpinGlassExhaustive.kernel(cu_graph, energies);
@cuda blocks=blocks threads=threads SpinGlassExhaustive.kernel(cu_graph, energies);
states = sortperm(energies);
energies[states]
Expand All @@ -70,10 +70,10 @@ energies = CUDA.zeros(2^N);
part_st = CUDA.zeros(2^(N-k));
part_lst = CUDA.zeros(2^(N-k));
threadsPerBlock = 2^k
blocksPerGrid = 2^(N-k)
threads = 512
blocks = cld(N, threads)
@cuda blocks=(blocksPerGrid) threads=(threadsPerBlock) SpinGlassExhaustive.kernel_part(cu_graph, energies, part_lst, part_st);
@cuda blocks=blocks threads=threads SpinGlassExhaustive.kernel_part(cu_graph, energies, part_lst, part_st);
idx = sortperm(part_lst);
states = part_st[idx];
Expand Down Expand Up @@ -109,16 +109,16 @@ lowest_d = CUDA.zeros(Float64, how_many*2);
lowest_states_d = CUDA.zeros(Int64, how_many*2);
k = 2
threadsPerBlock = 2^k
blocksPerGrid = 2^(chunk_size-k)
threads = 512
blocks = cld(chunk_size, threads)
for i in 1:2^(N-chunk_size)
idx = (i-1)*(2^chunk_size) + 1
@cuda blocks=(blocksPerGrid) threads=(threadsPerBlock) SpinGlassExhaustive.kernel_bucket(J_dev, energies_d, idx)
@cuda blocks=blocks threads=threads SpinGlassExhaustive.kernel_bucket(J_dev, energies_d, idx)
states_d = sortperm(energies_d)[1:how_many]
Expand Down
12 changes: 6 additions & 6 deletions examples/experiment.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function exp_exhaustive_search()
graph = generate_random_graph(N)
cu_graph = graph |> cu

ig = SpinGlassEngine.ising_graph(graph_to_dict(cu_graph))
ig = ising_graph(graph_to_dict(cu_graph))

exhaustive_search(ig)
end
Expand All @@ -15,7 +15,7 @@ function exp_bucket_exhaustive_search()
graph = generate_random_graph(N)
cu_graph = graph |> cu

ig = SpinGlassEngine.ising_graph(graph_to_dict(cu_graph))
ig = ising_graph(graph_to_dict(cu_graph))

exhaustive_search_bucket(ig)
end
Expand All @@ -25,7 +25,7 @@ function exp_partial_exhaustive_search()
graph = generate_random_graph(N)
cu_graph = graph |> cu

ig = SpinGlassEngine.ising_graph(graph_to_dict(cu_graph))
ig = ising_graph(graph_to_dict(cu_graph))

partial_exhaustive_search(ig)
end
Expand All @@ -44,7 +44,7 @@ function exp_qubo()
threadsPerBlock::Int64 = 2^k
blocksPerGrid::Int64 = 2^(N-k)

@cuda blocks=(blocksPerGrid) threads=(threadsPerBlock) kernel_qubo(cu_qubo, energies)
@cuda blocks=blocks threads=threads kernel_qubo(cu_qubo, energies)

# sort!(energies)
# or
Expand All @@ -67,7 +67,7 @@ function exp_ising()
threadsPerBlock::Int64 = 2^k
blocksPerGrid::Int64 = 2^(N-k)

@cuda blocks=(blocksPerGrid) threads=(threadsPerBlock) kernel(cu_graph, energies)
@cuda blocks=blocks threads=threads kernel(cu_graph, energies)

sort!(energies)

Expand All @@ -91,7 +91,7 @@ function exp_ising_part()
threadsPerBlock::Int64 = 2^k
blocksPerGrid::Int64 = 2^(N-k)

@cuda blocks=(blocksPerGrid) threads=(threadsPerBlock) kernel_part(cu_graph, energies, part_lst, part_st)
@cuda blocks=blocks threads=threads kernel_part(cu_graph, energies, part_lst, part_st)

sort!(part_lst)

Expand Down
4 changes: 4 additions & 0 deletions src/SpinGlassExhaustive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module SpinGlassExhaustive
using LabelledGraphs
using Bits
using LinearAlgebra
using DocStringExtensions
using CUDA

include("naive.jl")
include("utils.jl")
include("ising.jl")
end
41 changes: 16 additions & 25 deletions src/ising.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
using LabelledGraphs
using SpinGlassNetworks
using Bits
using DocStringExtensions

export energy_qubo, energy, kernel, kernel_qubo, kernel_part, exhaustive_search, partial_exhaustive_search

const IsingGraph = LabelledGraph{MetaGraph{Int64, Float64}, Int64}

struct Spectrum
energies::Vector{Float64}
energies::AbstractVector
states::Vector{Int}
end

Expand Down Expand Up @@ -75,7 +68,9 @@ function kernel(graph, energies)

F = energy(state_code, graph) |> Float32

@inbounds energies[state_code] = F
if state_code <= length(energies)
@inbounds energies[state_code] = F
end

return
end
Expand Down Expand Up @@ -148,22 +143,19 @@ Returns energies and states for provided graph by brute-forece alorithm based on
function exhaustive_search(ig::IsingGraph)
L = SpinGlassNetworks.nv(ig)

σ = CUDA.fill(Int32(-1), L, 2^L)
J = couplings(ig) + SpinGlassNetworks.Diagonal(biases(ig))
J = couplings(ig) + Diagonal(biases(ig))
J_dev = CUDA.CuArray(J)

energies = CUDA.zeros(L)

N = size(J_dev,1)
k = 2

energies = CUDA.zeros(2^N)

threadsPerBlock::Int64 = 2^k
blocksPerGrid::Int64 = 2^(N-k)
threads = 512
blocks = cld(N, threads)

@cuda blocks=(blocksPerGrid) threads=(threadsPerBlock) kernel(J_dev, energies)

@cuda blocks=blocks threads=threads kernel(J_dev, energies)

states = sortperm(energies)

Expand All @@ -181,7 +173,7 @@ function partial_exhaustive_search(ig::IsingGraph)


σ = CUDA.fill(Int32(-1), L, N)
J = couplings(ig) + SpinGlassNetworks.Diagonal(biases(ig))
J = couplings(ig) + Diagonal(biases(ig))
J_dev = CUDA.CuArray(J)

k = 2
Expand All @@ -190,10 +182,10 @@ function partial_exhaustive_search(ig::IsingGraph)
part_st = CUDA.zeros(2^(L-k))
part_lst = CUDA.zeros(2^(L-k))

threadsPerBlock::Int64 = 2^k
blocksPerGrid::Int64 = 2^(L-k)
threads = 512
blocks = cld(L, threads)

@cuda blocks=(blocksPerGrid) threads=(threadsPerBlock) kernel_part(J_dev, energies, part_lst, part_st)
@cuda blocks=blocks threads=threads kernel_part(J_dev, energies, part_lst, part_st)


idx = sortperm(part_lst)
Expand Down Expand Up @@ -255,7 +247,7 @@ function exhaustive_search_bucket(ig::IsingGraph, how_many::Int = 8)
L = SpinGlassNetworks.nv(ig)

σ = CUDA.fill(Int32(-1), L, 2^L)
J = couplings(ig) + SpinGlassNetworks.Diagonal(biases(ig))
J = couplings(ig) + Diagonal(biases(ig))
J_dev = CUDA.CuArray(J)

N = size(J_dev,1)
Expand All @@ -271,17 +263,16 @@ function exhaustive_search_bucket(ig::IsingGraph, how_many::Int = 8)
lowest_d = CUDA.zeros(Float64, how_many*2)
lowest_states_d = CUDA.zeros(Int64, how_many*2)

k = 2
threadsPerBlock = 2^k
blocksPerGrid = 2^(chunk_size-k)
threads = 512
blocks = cld(N, threads)



for i in 1:2^(N-chunk_size)

idx = (i-1)*(2^chunk_size) + 1

@cuda blocks=(blocksPerGrid) threads=(threadsPerBlock) kernel_bucket(J_dev, energies_d, idx)
@cuda blocks=blocks threads=threads kernel_bucket(J_dev, energies_d, idx)

states_d = sortperm(energies_d)[1:how_many]

Expand Down
2 changes: 1 addition & 1 deletion src/naive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function SpinGlassNetworks.brute_force(
::Val{:GPU};
num_states::Int=1
)
L = nv(ig)
L = SpinGlassNetworks.nv(ig)
N = 2^L
σ = CUDA.fill(Int32(-1), L, N)
JJ = couplings(ig)
Expand Down
12 changes: 6 additions & 6 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Distributions
export generate_random_graph
export generate_random_graph, graph_to_dict, graph_to_qubo
"""
$(SIGNATURES)
- `d::Int`: size of random graph.
Returns random array of size d.
"""
function generate_random_graph(d::Int)
graph = rand(Uniform(-5,5),d, d)
graph = 10*(rand(d, d) .- 0.5)
graph = graph * graph'

graph
Expand Down Expand Up @@ -40,17 +39,18 @@ $(SIGNATURES)
- `graph`: graph of the ising model.
Converts Ising model graph to Dict.
"""
function graph_to_dict(graph)
function graph_to_dict(graph::Array)
N = size(graph,1)
graph_dict = Dict()
for i in 1:N
graph_dict[(i, i)] = graph[i,i] #rand(Uniform(-5,5))
graph_dict[(i, i)] = graph[i, i]
for j in i:N
graph_dict[(i, j)] = graph[i,j] #rand(Uniform(-5,5))
graph_dict[(i, j)] = graph[i,j]
end
end
graph_dict
end
graph_to_dict(graph::CuArray) = graph_to_dict(Array(graph))

"""
$(SIGNATURES)
Expand Down
4 changes: 0 additions & 4 deletions test/brute_force.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@

using SpinGlassNetworks
using SpinGlassExhaustive

function bench_cpu(instance::String, max_states::Int=100)
m = 2
n = 2
Expand Down
Loading

0 comments on commit 2779ea0

Please sign in to comment.