Skip to content

Commit 437ff77

Browse files
committed
Fix queue lock algorithm for ParallelPSOKernel
1 parent 9307b5c commit 437ff77

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

Project.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ version = "1.0.0-DEV"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
8-
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
98
DiffEqGPU = "071ae1c0-96b5-11e9-1965-c90190d839ea"
109
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
1110
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"

src/kernels.jl

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,33 @@ end
4747

4848
particle = @private SPSOParticle{T1, T2} 1
4949

50-
# Initialize cost to be Inf
5150
@inbounds particle[1] = gpu_particles[i]
52-
best_queue[tidx] = SPSOGBest(particle[1].best_position,
53-
convert(T2, Inf))
54-
queue_num[1] = UInt32(0)
51+
@inbounds gbest = gbest_ref[1]
52+
53+
# Initialize cost to be Inf
54+
if tidx == 1
55+
fill!(best_queue, SPSOGBest(gbest.position, convert(typeof(gbest.cost), Inf)))
56+
queue_num[1] = UInt32(0)
57+
end
5558

5659
@synchronize
5760

58-
@inbounds particle[1] = gpu_particles[i]
59-
gbest = @inbounds gbest_ref[1]
60-
@inbounds particle[1] = update_particle_state(particle[1], prob, gbest, w, c1, c2, i, opt)
61+
@inbounds particle[1] = update_particle_state(particle[1],
62+
prob,
63+
gbest,
64+
w,
65+
c1,
66+
c2,
67+
i,
68+
opt)
6169

6270
@synchronize
6371

64-
@inbounds particle[1] = gpu_particles[i]
6572
gbest = @inbounds gbest_ref[1]
6673
if particle[1].best_cost < gbest.cost
6774
queue_idx = @atomic queue_num[1] += UInt32(1)
6875
@inbounds best_queue[queue_idx] = SPSOGBest(particle[1].best_position,
69-
particle[1].best_cost)
76+
particle[1].best_cost)
7077
end
7178

7279
@synchronize
@@ -98,6 +105,7 @@ end
98105
@atomicswap lock[1] = 0
99106
end
100107
end
108+
@inbounds gpu_particles[i] = particle[1]
101109
end
102110

103111
@kernel function update_particle_states!(prob,

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[deps]
2+
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
23
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
34
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
45
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

test/gpu.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ end
3131

3232
sol = solve(prob, ParallelPSOKernel(n_particles; backend), maxiters = 500)
3333

34+
@test prob.f(prob.u0, prob.p) > sol.objective
35+
36+
@test sol.objective < 6e-4
37+
3438
@test sol.retcode == ReturnCode.Default
3539

3640
sol = solve(prob,
3741
ParallelPSOKernel(n_particles; backend, global_update = false),
38-
maxiters = 500)
42+
maxiters = 1000)
3943

4044
@test prob.f(prob.u0, prob.p) > sol.objective
4145

@@ -47,5 +51,5 @@ end
4751

4852
@test prob.f(prob.u0, prob.p) > sol.objective
4953

50-
@test sol.objective < 6e-1
54+
@test sol.objective < 6e-4
5155
end

0 commit comments

Comments
 (0)