@@ -67,10 +67,16 @@ function recurrence_matrix(x::Vector_or_SSSet, y::Vector_or_SSSet, metric::Metri
67
67
# multiple threads pushing to the same `Array` (`Array`s are not atomic).
68
68
rowvals = [Vector {Int} () for _ in 1 : Threads. nthreads ()]
69
69
colvals = [Vector {Int} () for _ in 1 : Threads. nthreads ()]
70
+ # Channel to manage `Array`s to be used in each iteration
71
+ nbuffers = Threads. nthreads ()
72
+ threadchannel = Channel {Int} (nbuffers) # for rows and columns
73
+ for i in 1 : nbuffers
74
+ put! (threadchannel, i)
75
+ end
70
76
71
77
# This is the same logic as the serial function, but parallelized.
72
78
Threads. @threads for j in eachindex (y)
73
- threadn = Threads . threadid ( )
79
+ threadn = take! (threadchannel )
74
80
nzcol = 0
75
81
for i in eachindex (x)
76
82
@inbounds if evaluate (metric, x[i], y[j]) ≤ ( (ε isa Real) ? ε : ε[j] )
@@ -79,9 +85,12 @@ function recurrence_matrix(x::Vector_or_SSSet, y::Vector_or_SSSet, metric::Metri
79
85
end
80
86
end
81
87
append! (colvals[threadn], fill (j, (nzcol,)))
88
+ put! (threadchannel, threadn)
82
89
end
83
- finalrows = vcat (rowvals... ) # merge into one array
84
- finalcols = vcat (colvals... ) # merge into one array
90
+ close (threadchannel)
91
+
92
+ finalrows = reduce (vcat, rowvals) # merge into one array
93
+ finalcols = reduce (vcat, colvals) # merge into one array
85
94
nzvals = fill (true , (length (finalrows),))
86
95
return sparse (finalrows, finalcols, nzvals, length (x), length (y))
87
96
end
@@ -93,10 +102,16 @@ function recurrence_matrix(x::Vector_or_SSSet, metric::Metric, ε, ::Val{true})
93
102
# multiple threads pushing to the same `Array` (`Array`s are not atomic).
94
103
rowvals = [Vector {Int} () for _ in 1 : Threads. nthreads ()]
95
104
colvals = [Vector {Int} () for _ in 1 : Threads. nthreads ()]
105
+ # Channel to manage `Array`s to be used in each iteration
106
+ nbuffers = Threads. nthreads ()
107
+ threadchannel = Channel {Int} (nbuffers) # for rows and columns
108
+ for i in 1 : nbuffers
109
+ put! (threadchannel, i)
110
+ end
96
111
97
112
# This is the same logic as the serial function, but parallelized.
98
113
Threads. @threads for k in partition_indices (length (x))
99
- threadn = Threads . threadid ( )
114
+ threadn = take! (threadchannel )
100
115
for j in k
101
116
nzcol = 0
102
117
for i in 1 : j
@@ -107,9 +122,12 @@ function recurrence_matrix(x::Vector_or_SSSet, metric::Metric, ε, ::Val{true})
107
122
end
108
123
append! (colvals[threadn], fill (j, (nzcol,)))
109
124
end
125
+ put! (threadchannel, threadn)
110
126
end
111
- finalrows = vcat (rowvals... ) # merge into one array
112
- finalcols = vcat (colvals... ) # merge into one array
127
+ close (threadchannel)
128
+
129
+ finalrows = reduce (vcat, rowvals) # merge into one array
130
+ finalcols = reduce (vcat, colvals) # merge into one array
113
131
nzvals = fill (true , (length (finalrows),))
114
132
return Symmetric (sparse (finalrows, finalcols, nzvals, length (x), length (x)), :U )
115
133
end
0 commit comments