@@ -130,14 +130,14 @@ function Base.iterate(p::ProductSplit,state=(first(p),1))
130
130
(el,(p[n+ 1 ],n+ 1 ))
131
131
end
132
132
133
- @inline Base. @propagate_inbounds function _firstlastdim (p:: ProductSplit{<:Any,N} ,dim:: Int ,
133
+ @inline Base. @propagate_inbounds function _firstlastalongdim (p:: ProductSplit{<:Any,N} ,dim:: Int ,
134
134
firstindchild:: Tuple = childindex (p,p. firstind),
135
135
lastindchild:: Tuple = childindex (p,p. lastind)) where {N}
136
136
137
- _firstlastdim (p. iterators,dim,firstindchild,lastindchild)
137
+ _firstlastalongdim (p. iterators,dim,firstindchild,lastindchild)
138
138
end
139
139
140
- @inline function _firstlastdim (iterators:: NTuple{N,<:Any} ,dim:: Int ,
140
+ @inline Base . @propagate_inbounds function _firstlastalongdim (iterators:: NTuple{N,<:Any} ,dim:: Int ,
141
141
firstindchild:: Tuple ,lastindchild:: Tuple ) where {N}
142
142
143
143
@boundscheck (1 <= dim <= N) || throw (BoundsError (iterators,dim))
195
195
firstindchild = childindex (p,p. firstind)
196
196
lastindchild = childindex (p,p. lastind)
197
197
198
- @inbounds first_iter,last_iter = _firstlastdim (p,dim,firstindchild,lastindchild)
198
+ @inbounds first_iter,last_iter = _firstlastalongdim (p,dim,firstindchild,lastindchild)
199
199
200
200
v = last_iter
201
201
227
227
firstindchild = childindex (p,p. firstind)
228
228
lastindchild = childindex (p,p. lastind)
229
229
230
- @inbounds first_iter,last_iter = _firstlastdim (p,dim,firstindchild,lastindchild)
230
+ @inbounds first_iter,last_iter = _firstlastalongdim (p,dim,firstindchild,lastindchild)
231
231
232
232
v = first_iter
233
233
262
262
firstindchild = childindex (p,p. firstind)
263
263
lastindchild = childindex (p,p. lastind)
264
264
265
- @inbounds first_iter,last_iter = _firstlastdim (p,dim,firstindchild,lastindchild)
265
+ @inbounds first_iter,last_iter = _firstlastalongdim (p,dim,firstindchild,lastindchild)
266
266
267
267
v = (first_iter,last_iter)
268
268
# The last index will not roll over so this can be handled easily
278
278
return v
279
279
end
280
280
281
+ _infullrange (val:: T ,p:: ProductSplit{T} ) where {T} = _infullrange (val,p. iterators)
282
+
283
+ function _infullrange (val,t:: Tuple )
284
+ first (val) in first (t) && _infullrange (Base. tail (val),Base. tail (t))
285
+ end
286
+ _infullrange (:: Tuple{} ,:: Tuple{} ) = true
287
+
288
+ struct OrderedTuple{T}
289
+ t :: T
290
+ end
291
+
292
+ function Base.:<= (a:: OrderedTuple{T} ,b:: OrderedTuple{T} ) where {T}
293
+ _le (reverse (a. t),reverse (b. t))
294
+ end
295
+
296
+ function _le (t1:: Tuple ,t2:: Tuple )
297
+ first (t1) < first (t2) || ((first (t1) == first (t2)) & _le (Base. tail (t1),Base. tail (t2)))
298
+ end
299
+ _le (:: Tuple{} ,:: Tuple{} ) = true
300
+
301
+ function Base. in (val:: T ,p:: ProductSplit{T} ) where {T}
302
+ _infullrange (val,p) || return false
303
+
304
+ val_ot = OrderedTuple (val)
305
+ first_iter = OrderedTuple (p[1 ])
306
+ last_iter = OrderedTuple (p[end ])
307
+
308
+ first_iter <= val_ot <= last_iter
309
+ end
310
+
281
311
# ##################################################################################################
282
312
283
313
function worker_rank ()
@@ -287,42 +317,32 @@ function worker_rank()
287
317
myid ()- minimum (workers ())+ 1
288
318
end
289
319
290
- function split_across_processors (num_tasks:: Integer ,num_procs= nworkers (),proc_id= worker_rank ())
291
- if num_procs == 1
292
- return num_tasks
293
- end
294
-
295
- num_tasks_per_process,num_tasks_leftover = div (num_tasks,num_procs),mod (num_tasks,num_procs)
296
-
297
- num_tasks_on_proc = num_tasks_per_process + (proc_id <= mod (num_tasks,num_procs) ? 1 : 0 );
298
- task_start = num_tasks_per_process* (proc_id- 1 ) + min (num_tasks_leftover+ 1 ,proc_id);
320
+ # function split_across_processors_iterators(arr₁::Base.Iterators.ProductIterator,num_procs,proc_id)
299
321
300
- return task_start: (task_start+ num_tasks_on_proc- 1 )
301
- end
322
+ # @assert(proc_id<=num_procs,"processor rank has to be less than number of workers engaged")
302
323
303
- function split_across_processors (arr₁:: Base.Iterators.ProductIterator ,num_procs = nworkers (),proc_id = worker_rank ())
324
+ # num_tasks = length (arr₁);
304
325
305
- @assert (proc_id <= num_procs, " processor rank has to be less than number of workers engaged " )
326
+ # num_tasks_per_process,num_tasks_leftover = div(num_tasks,num_procs),mod(num_tasks,num_procs )
306
327
307
- num_tasks = length (arr₁);
328
+ # num_tasks_on_proc = num_tasks_per_process + (proc_id <= mod(num_tasks,num_procs) ? 1 : 0 );
329
+ # task_start = num_tasks_per_process*(proc_id-1) + min(num_tasks_leftover,proc_id-1) + 1;
308
330
309
- num_tasks_per_process,num_tasks_leftover = div (num_tasks,num_procs),mod (num_tasks,num_procs)
331
+ # Iterators.take(Iterators.drop(arr₁,task_start-1),num_tasks_on_proc)
332
+ # end
310
333
311
- num_tasks_on_proc = num_tasks_per_process + (proc_id <= mod (num_tasks,num_procs) ? 1 : 0 );
312
- task_start = num_tasks_per_process* (proc_id- 1 ) + min (num_tasks_leftover,proc_id- 1 ) + 1 ;
334
+ # function split_product_across_processors_iterators(arrs_tuple,num_procs,proc_id)
335
+ # split_across_processors_iterators(Iterators.product(arrs_tuple...),num_procs,proc_id)
336
+ # end
313
337
314
- Iterators. take (Iterators. drop (arr₁,task_start- 1 ),num_tasks_on_proc)
315
- end
316
-
317
- function split_product_across_processors (arr₁:: AbstractVector ,arr₂:: AbstractVector ,
318
- num_procs:: Integer = nworkers (),proc_id:: Integer = worker_rank ())
319
- # arr₁ will change faster
320
- split_across_processors (Iterators. product (arr₁,arr₂),num_procs,proc_id)
338
+ function split_across_processors (num_tasks:: Integer ,num_procs= nworkers (),proc_id= worker_rank ())
339
+ split_product_across_processors ((1 : num_tasks,),num_procs,proc_id)
321
340
end
322
341
323
- function split_product_across_processors (arrs_tuple:: NTuple ,
342
+ function split_product_across_processors (arrs_tuple:: Tuple ,
324
343
num_procs:: Integer = nworkers (),proc_id:: Integer = worker_rank ())
325
- return split_across_processors (Iterators. product (arrs_tuple... ),num_procs,proc_id)
344
+
345
+ ProductSplit (arrs_tuple,num_procs,proc_id)
326
346
end
327
347
328
348
function get_processor_id_from_split_array (arr₁:: AbstractVector ,arr₂:: AbstractVector ,
@@ -367,9 +387,9 @@ function get_processor_id_from_split_array(arr₁::AbstractVector,arr₂::Abstra
367
387
return proc_id
368
388
end
369
389
370
- function get_processor_id_from_split_array (iter ,val,num_procs)
390
+ function get_processor_id_from_split_array (iterators ,val,num_procs)
371
391
for proc_id in 1 : num_procs
372
- tasks_on_proc = split_across_processors (iter ,num_procs,proc_id)
392
+ tasks_on_proc = split_product_across_processors (iterators ,num_procs,proc_id)
373
393
if val ∈ tasks_on_proc
374
394
return proc_id
375
395
end
0 commit comments