Skip to content

Commit

Permalink
More speed more better
Browse files Browse the repository at this point in the history
  • Loading branch information
ceesb committed Mar 7, 2018
1 parent 80cf685 commit 4bec87e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 141 deletions.
63 changes: 16 additions & 47 deletions src/incremental-correlation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ maximization(a::IncrementalCPA) = AbsoluteGlobalMaximization()
type IncrementalCorrelation <: PostProcessor
worksplit::WorkSplit
counter::Int
covXY::Dict{Int,IncrementalCovarianceTiled}
meanXinitialized::Bool
meanX::IncrementalMeanVariance
covXY::IncrementalCovarianceTiled
targetOffsets::Vector{Int}
leakages::Vector{Leakage}
targets::Vector{Target}
Expand All @@ -44,7 +43,7 @@ type IncrementalCorrelation <: PostProcessor
end

function IncrementalCorrelation(w::WorkSplit)
return new(w, 0, Dict{Int,IncrementalCovarianceTiled}(), false)
return new(w, 0, false)
end
end

Expand All @@ -63,29 +62,27 @@ function init(c::IncrementalCorrelation, targetOffsets::Vector{Int}, leakages::V
c.targets = targets
#FIXME: broken for attacks with different target output types
#FIXME: broken for leakages that don't fit in an byte
c.hypocache = Vector{UInt8}(length(guesses(targets[1])) * length(leakages))
c.hypocache = Vector{UInt8}(length(guesses(targets[1])) * length(leakages) * length(targets))
c.targetcache = createTargetCache(targets[1])
c.guesses = guesses(targets[1])
end

function reset(c::IncrementalCorrelation)
c.covXY = Dict{Int,IncrementalCovarianceTiled}()
c.meanXinitialized = false
c.counter = 0
end

function helpert(lt::Leakage, hypoidx::Int, outputs::Vector{Out},hypo::Vector{UInt8}) where {Out}
function helpert(lt::Leakage, hypoidx::Int, outputs::Vector{Out},hypo::Vector{UInt8}, offset::Int) where {Out}
nrOfKbVals = length(outputs)
@inbounds for o in 1:nrOfKbVals
hypo[hypoidx+o] = leak(lt, outputs[o])
hypo[offset+hypoidx+o] = leak(lt, outputs[o])
end
end

function toLeakages!(c::IncrementalCorrelation, t::Target{In,Out,Guess}, input::In) where {In,Out,Guess}
function toLeakages!(c::IncrementalCorrelation, t::Target{In,Out,Guess}, input::In, hypo::Vector{UInt8}, offset::Int) where {In,Out,Guess}
kbvals = c.guesses::Vector{Guess}
nrOfKbVals = length(kbvals)
nrOfFuns = length(c.leakages)
nrOfTargets = length(input)
outputs = c.targetcache::Vector{Out}
hypo = c.hypocache::Vector{UInt8}

Expand All @@ -96,33 +93,26 @@ function toLeakages!(c::IncrementalCorrelation, t::Target{In,Out,Guess}, input::
@inbounds for l in 1:nrOfFuns
lt = c.leakages[l]
hypoidx = (l-1)*nrOfKbVals
helpert(lt,hypoidx,outputs,hypo)
helpert(lt,hypoidx,outputs,hypo,offset)
end

return hypo
return offset + nrOfFuns*nrOfKbVals
end

function add(c::IncrementalCorrelation, samples::Vector{S}, data::Vector{D}, traceIdx::Int) where {S,D}
hypo = c.hypocache
if !c.meanXinitialized
c.meanXinitialized = true
c.meanX = IncrementalMeanVariance(length(samples))
for idx in 1:length(c.targetOffsets)
hypo = c.hypocache
c.covXY[idx] = IncrementalCovarianceTiled(c.meanX, IncrementalMeanVariance(length(hypo)))
end
c.covXY = IncrementalCovarianceTiled(length(samples), length(hypo))
c.meanXinitialized = true
end

samplesN::Vector{Float64} = samples .- c.meanX.mean
offset = 0

for idx in 1:length(c.targetOffsets)
val = data[idx]

hypo = toLeakages!(c, c.targets[c.targetOffsets[idx]], val)

add!(c.covXY[idx], samples, hypo, samplesN, false)
offset = toLeakages!(c, c.targets[c.targetOffsets[idx]], data[idx], hypo, offset)
end

add!(c.meanX, samples)
add!(c.covXY, samples, hypo)

c.counter += 1
end
Expand All @@ -144,14 +134,7 @@ end

function merge(this::IncrementalCorrelation, other::IncrementalCorrelation)
this.counter += other.counter
for (idx,cov) in other.covXY
if !haskey(this.covXY, idx)
this.covXY[idx] = cov
else
add!(this.covXY[idx], other.covXY[idx], false)
end
end
add!(this.covXY[1].meanVarX, other.covXY[1].meanVarX)
add!(this.covXY, other.covXY)
end


Expand All @@ -168,22 +151,8 @@ function get(c::IncrementalCorrelation)
end
end

idxes = sort(collect(keys(c.covXY)))

rows = c.covXY[1].numberOfX
cols = sum(x -> c.covXY[x].numberOfY, idxes)
C = Matrix{Float64}(rows, cols)

ystart = 0
yend = 0

for i in idxes
yend += c.covXY[i].numberOfY
C[:,ystart+1:yend] = getCorr(c.covXY[i])
ystart += c.covXY[i].numberOfY
end
C = getCorr(c.covXY)

# C = mapreduce(x -> getCorr(c.covXY[x]), hcat, sort(collect(keys(c.covXY))))
return C
end

Expand Down
28 changes: 26 additions & 2 deletions src/incremental-statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ function add!(this::IncrementalCovariance, other::IncrementalCovariance, minX::I
end
end

const cachechunkmagic = 2^14

function mystrategy(nrX,nrY)
tilesY = min(128,div(nrY,Threads.nthreads()))
tilesX = div(cachechunkmagic,tilesY)
cache = 64
# @show (tilesX,tilesY,cache)
return (tilesX,tilesY,cache)
end

type IncrementalCovarianceTiled
numberOfX::Int
Expand All @@ -191,14 +200,29 @@ type IncrementalCovarianceTiled
cacheCount::Int
cacheMax::Int

function IncrementalCovarianceTiled(numberOfX::Int, numberOfY::Int, tilesizeX::Int=128, tilesizeY::Int=128, caches::Int=32)
function IncrementalCovarianceTiled(numberOfX::Int, numberOfY::Int)
meanVarX = IncrementalMeanVariance(numberOfX)
meanVarY = IncrementalMeanVariance(numberOfY)

tilesizeX, tilesizeY, caches = mystrategy(numberOfX,numberOfY)

IncrementalCovarianceTiled(meanVarX, meanVarY, tilesizeX, tilesizeY, caches)
end

function IncrementalCovarianceTiled(numberOfX::Int, numberOfY::Int, tilesizeX::Int, tilesizeY::Int, caches::Int)
meanVarX = IncrementalMeanVariance(numberOfX)
meanVarY = IncrementalMeanVariance(numberOfY)

IncrementalCovarianceTiled(meanVarX, meanVarY, tilesizeX, tilesizeY, caches)
end

function IncrementalCovarianceTiled(meanVarX::IncrementalMeanVariance, meanVarY::IncrementalMeanVariance, tilesizeX::Int=128, tilesizeY::Int=128, caches::Int=32*Threads.nthreads())
function IncrementalCovarianceTiled(meanVarX::IncrementalMeanVariance, meanVarY::IncrementalMeanVariance)
tilesizeX, tilesizeY, caches = mystrategy(length(meanVarX.mean),length(meanVarY.mean))

IncrementalCovarianceTiled(meanVarX, meanVarY, tilesizeX, tilesizeY, caches)
end

function IncrementalCovarianceTiled(meanVarX::IncrementalMeanVariance, meanVarY::IncrementalMeanVariance, tilesizeX::Int, tilesizeY::Int, caches::Int)
numberOfX = length(meanVarX.mean)
numberOfY = length(meanVarY.mean)
nrTilesX = div(numberOfX+tilesizeX-1, tilesizeX)
Expand Down
92 changes: 0 additions & 92 deletions test/incremental-statistics-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,84 +263,6 @@ function test2tiled()
@test getCorr(covXY) getCorr(covXY1)
end

function speedtest(rows, nrX, nrY)

meanVarX = IncrementalMeanVariance(nrX)
meanVarY = IncrementalMeanVariance(nrY)
covXY = IncrementalCovariance(meanVarX, meanVarY)

for r in 1:rows
x = rand(Float64, nrX)
y = rand(Float64, nrY)
add!(covXY, x, y)
end

# Profile.print(maxdepth=12,combine=true)

return getCorr(covXY)
end

using Base.Threads

function speedtesttiled(rows, nrX, nrY, tilesX, tilesY,cache)

meanVarX = IncrementalMeanVariance(nrX)
meanVarY = IncrementalMeanVariance(nrY)
covXY = IncrementalCovarianceTiled(meanVarX, meanVarY, tilesX, tilesY, cache)

for r in 1:rows
x = rand(Float64, nrX)
y = rand(Float64, nrY)
add!(covXY, x, y)
end

# Profile.print(maxdepth=12,combine=true)

return getCorr(covXY)
end

function dumpasm()
c = Array(Float64,3,5)
x = rand(Float64, 10)
y = rand(Float64, 10)

@code_warntype Trs.updateCov!(c, x, 1, 10, y, 1, 10, 0.9)
@code_native Trs.updateCov!(c, x, 1, 10, y, 1, 10, 0.9)

end

function bla()
nrX = 4
nrY = 5

meanVarX = IncrementalMeanVariance(nrX)
meanVarY = IncrementalMeanVariance(nrY)
covXY = IncrementalCovariance(meanVarX, meanVarY)

add!(covXY, [2.0,2.0,3.0,4.0], [5.0,6.0,7.0,8.0,9.0])

println(covXY.cov)
end

function meanspeedtest1()
rows = 10000
nrX = 512
nrY = 8*256*16

meanVarX = IncrementalMeanVariance(nrX)
meanVarY = IncrementalMeanVariance(nrY)

for r in 1:rows
x = rand(Float64, nrX)
y = rand(Float64, nrY)

normalX = x .- meanVarX.mean
normalY = y .- meanVarY.mean
add!(meanVarX, x, normalX)
add!(meanVarY, y, normalY)
end

end

test1tiled()
test1ntiled()
Expand All @@ -350,17 +272,3 @@ test1()
testmeanadd()
test1n()
test2()

const rows = 500
const nrX = 1500
const nrY = 16*8*256
const tilesX = 128
const tilesY = 128
const cache = 32

# @time speedtest(rows,nrX,nrY)
# @time speedtest(rows,nrX,nrY)
# @time speedtesttiled(rows,nrX,nrY,tilesX,tilesY,cache)
# @time speedtesttiled(rows,nrX,nrY,tilesX,tilesY,cache)

# dumpasm()

0 comments on commit 4bec87e

Please sign in to comment.