HW2: RC3 - A small exchange optimization
This commit is contained in:
@@ -4,9 +4,9 @@
|
||||
#
|
||||
|
||||
function exchange(localid, remoteid)
|
||||
if verbose
|
||||
println("Exchange local data from $localid with partner $remoteid")
|
||||
end
|
||||
# if verbose
|
||||
# println("Exchange local data from $localid with partner $remoteid")
|
||||
# end
|
||||
nothing # We have all data here ;)
|
||||
end
|
||||
|
||||
@@ -23,9 +23,89 @@ function minmax(data, localid, remoteid, keepsmall)
|
||||
end
|
||||
|
||||
|
||||
function is_bitonic(arr)
|
||||
n = length(arr)
|
||||
if n <= 2
|
||||
return true # Any sequence of length <= 2 is bitonic
|
||||
end
|
||||
|
||||
# State for state machine. 1: inc, -1: dec, 0: z-state
|
||||
state = 0
|
||||
inc_count = 0
|
||||
dec_count = 0
|
||||
ret = false
|
||||
|
||||
for i in 1:n-1
|
||||
# Find the first order
|
||||
if state == 0
|
||||
if arr[i] > arr[i+1]
|
||||
state = -1
|
||||
dec_count += 1
|
||||
elseif arr[i] < arr[i+1]
|
||||
state = 1
|
||||
inc_count += 1
|
||||
end
|
||||
elseif state == -1 # decreasing
|
||||
if arr[i] < arr[i + 1]
|
||||
state = 1
|
||||
inc_count += 1
|
||||
end
|
||||
elseif state == 1 # increasing
|
||||
if arr[i] > arr[i+1]
|
||||
state = -1
|
||||
dec_count += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if inc_count <= 1 && dec_count <= 1
|
||||
ret = true # Sequence is bitonic
|
||||
elseif inc_count == 2 && dec_count == 1
|
||||
ret = (arr[1] >= arr[n])
|
||||
elseif inc_count == 1 && dec_count == 2
|
||||
ret = (arr[1] <= arr[n])
|
||||
end
|
||||
|
||||
ret
|
||||
end
|
||||
|
||||
function is_sort(arr)
|
||||
# State for state machine. 1: inc, -1: dec, 0: z-state
|
||||
state = 0
|
||||
inc_count = 0
|
||||
dec_count = 0
|
||||
|
||||
for i in 1:length(arr)-1
|
||||
# Find the first order
|
||||
if state == 0
|
||||
if arr[i] > arr[i+1]
|
||||
state = -1
|
||||
dec_count += 1
|
||||
elseif arr[i] < arr[i+1]
|
||||
state = 1
|
||||
inc_count += 1
|
||||
end
|
||||
elseif state == -1 # decreasing
|
||||
if arr[i] < arr[i + 1]
|
||||
state = 1
|
||||
inc_count += 1
|
||||
end
|
||||
elseif state == 1 # increasing
|
||||
if arr[i] > arr[i+1]
|
||||
state = -1
|
||||
dec_count += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ret = ((inc_count + dec_count) == 1) ? state : 0
|
||||
ret
|
||||
end
|
||||
|
||||
function sort_network!(data, n, depth)
|
||||
nodes = 0:n-1
|
||||
bitonicFlag = zeros(Int8, size(data, 1))
|
||||
sortFlag = zeros(Int8, size(data, 1))
|
||||
for step = depth-1:-1:0
|
||||
partnerid = nodes .⊻ (1 << step)
|
||||
direction = (nodes .& (1 << depth)) .== 0 .& (nodes .< partnerid)
|
||||
@@ -40,6 +120,13 @@ function sort_network!(data, n, depth)
|
||||
minmax(data, i, partnerid[i+1], keepsmall[i+1])
|
||||
end
|
||||
end
|
||||
if verbose
|
||||
for i in 1:size(data, 1)
|
||||
bitonicFlag[i] = is_bitonic(data[i, :])
|
||||
sortFlag[i] = is_sort(data[i, :])
|
||||
end
|
||||
println("depth: $depth | step: $step | bitonicFlag: $bitonicFlag | sorfFlag: $sortFlag")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user