Skip to content

Commit

Permalink
brain fart
Browse files Browse the repository at this point in the history
  • Loading branch information
araujoms committed Jan 8, 2025
1 parent d18b9ec commit 897434d
Showing 1 changed file with 54 additions and 18 deletions.
72 changes: 54 additions & 18 deletions src/nonlocal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Reference: Araújo, Hirsch, and Quintino, [arXiv:2005.13418](https://arxiv.org/a
"""
function local_bound(G::Array{T,N}; correlation::Bool = N < 4, marg::Bool = true) where {T<:Real,N}
if correlation
return _local_bound_correlation(G; marg)
# return _local_bound_correlation_recursive(G, marg)
#return _local_bound_correlation(G; marg)
return _local_bound_correlation_recursive(G, marg)
else
return _local_bound_probability(G)
end
Expand Down Expand Up @@ -112,6 +112,14 @@ Base.@propagate_inbounds function _local_bound_correlation_recursive(A::Vector{T
return score
end

Base.@propagate_inbounds function _local_bound_correlation_recursive_old(A::Vector{T}, marg, m, tmp, ind, ax) where {T<:Real}
score = marg ? A[1] : abs(A[1])
for x 2:m[1]
score += abs(A[x])
end
return score
end

# Base.@propagate_inbounds function _local_bound_correlation_recursive(
# A::Matrix{T},
# marg = true,
Expand Down Expand Up @@ -146,10 +154,15 @@ Base.@propagate_inbounds function _local_bound_correlation_recursive(
ax = [ones(T, m[i]) for i 2:N],
) where {T<:Real,N}
tmp_end::Array{T,N-1} = tmp[N-1]
offset = similar(tmp_end)
sum!(offset, A)
offset .*= -1
score = typemin(T)
A2 = 2*A
@inbounds for _ 0:2^(m[N]-marg)-1
@views ax[N-1][marg+1:end] .= 2 .* ind[N-1] .- 1
_tensor_contraction!(tmp_end, A, ax[N-1])
@views ax[N-1][marg+1:end] .= ind[N-1]
tmp_end .= offset
_tensor_contraction!(tmp_end, A2, ax[N-1])
@views temp_score = _local_bound_correlation_recursive(tmp_end, marg, m[1:N-1], tmp[1:N-2], ind[1:N-2], ax[1:N-2])
if temp_score > score
score = temp_score
Expand All @@ -159,21 +172,7 @@ Base.@propagate_inbounds function _local_bound_correlation_recursive(
return score
end

#function _tensor_contraction!(tmp, A::Matrix{T}, ax::Vector{T}) where {T<:Real}
# @inbounds mul!(tmp, A, ax)
#end

# among ci/x orders in the loop and in the indexing,
# this is the fastest contraction, hence the enumeration order
#function _tensor_contraction!(tmp, A::Array{T,N}, ax::Vector{T}) where {T<:Real,N}
# tmp .= 0
# @inbounds for x in eachindex(ax), ci in CartesianIndices(tmp)
# tmp[ci] += A[ci, x] * ax[x]
# end
#end

function _tensor_contraction!(tmp, A::Array{T,N}, ax) where {T<:Number,N}
tmp .= 0
@inbounds for x eachindex(ax)
if ax[x] == 1
for ci CartesianIndices(tmp)
Expand All @@ -183,6 +182,43 @@ function _tensor_contraction!(tmp, A::Array{T,N}, ax) where {T<:Number,N}
end
end

Base.@propagate_inbounds function _local_bound_correlation_recursive_old(
A::Array{T,N},
marg = true,
m = size(A),
tmp = [zeros(T, m[1:i]...) for i 1:N-1],
ind = [zeros(Int8, m[i] - marg) for i 2:N],
ax = [ones(T, m[i]) for i 2:N],
) where {T<:Real,N}
tmp_end::Array{T,N-1} = tmp[N-1]
score = typemin(T)
@inbounds for _ 0:2^(m[N]-marg)-1
@views ax[N-1][marg+1:end] .= 2 .* ind[N-1] .- 1
_tensor_contraction_old!(tmp_end, A, ax[N-1])
@views temp_score = _local_bound_correlation_recursive_old(tmp_end, marg, m[1:N-1], tmp[1:N-2], ind[1:N-2], ax[1:N-2])
if temp_score > score
score = temp_score
end
_update_odometer!(ind[N-1], 2)
end
return score
end

function _tensor_contraction_old!(tmp, A::Matrix{T}, ax::Vector{T}) where {T<:Real}
@inbounds mul!(tmp, A, ax)
end

# among ci/x orders in the loop and in the indexing,
# this is the fastest contraction, hence the enumeration order
function _tensor_contraction_old!(tmp, A::Array{T,N}, ax::Vector{T}) where {T<:Real,N}
tmp .= 0
@inbounds for x in eachindex(ax), ci in CartesianIndices(tmp)
tmp[ci] += A[ci, x] * ax[x]
end
end



function _local_bound_probability(G::Array{T,N2}) where {T<:Real,N2}
@assert iseven(N2)
N = N2 ÷ 2
Expand Down

0 comments on commit 897434d

Please sign in to comment.