6161 val pareduceBreakExn: (int * int) -> 'a -> (('a -> exn) * int * 'a -> 'a) -> ('a * 'a -> 'a) -> 'a
6262 val reducem: ('a * 'a -> 'a) -> 'a -> (int * int) -> (int -> 'a) -> 'a
6363 val parform: (int * int) -> (int -> unit) -> unit
64+ val seqLoop: (int * int) -> (int -> unit) -> unit
65+ val seqReduce: ('a * 'a -> 'a) -> 'a -> (int * int) -> (int -> 'a) -> 'a
6466end =
6567struct
6668 type idx = LoopIndex.t
@@ -147,6 +149,26 @@ struct
147149
148150 fun __inline_always__ parform (lo: int, hi: int) (f: int -> unit) : unit =
149151 reducem (fn _ => ()) () (lo, hi) f
152+
153+
154+ fun __inline_always__ seqLoop (lo: int, hi: int) (f: int -> unit) : unit =
155+ let
156+ fun loop (i: idx, j: idx) : unit =
157+ if LoopIndex.equal (i, j) then ()
158+ else (__inline_always__ f (LoopIndex.toInt i); loop (LoopIndex.increment i, j))
159+ in
160+ loop (LoopIndex.fromInt (Int.min (lo, hi)), LoopIndex.fromInt hi)
161+ end
162+
163+
164+ fun __inline_always__ seqReduce (combine: 'a * 'a -> 'a) (zero: 'a) (lo: int, hi: int) (f: int -> 'a) : 'a =
165+ let
166+ fun loop (acc: 'a) (i: idx, j: idx) : 'a =
167+ if LoopIndex.equal (i, j) then acc
168+ else loop (__inline_always__ combine (acc, __inline_always__ f (LoopIndex.toInt i))) (LoopIndex.increment i, j)
169+ in
170+ loop zero (LoopIndex.fromInt (Int.min (lo, hi)), LoopIndex.fromInt hi)
171+ end
150172end
151173
152174
170192 val parfor: int -> (int * int) -> (int -> unit) -> unit
171193 val alloc: int -> 'a array
172194
195+ val seqLoop: (int * int) -> (int -> unit) -> unit
196+ val seqReduce: ('a * 'a -> 'a) -> 'a -> (int * int) -> (int -> 'a) -> 'a
197+
173198 val idleTimeSoFar: unit -> Time.time
174199 val workTimeSoFar: unit -> Time.time
175200 val maxForkDepthSoFar: unit -> int
@@ -280,7 +305,47 @@ struct
280305 val fInt16 = Unrolled16.parform
281306 val fInt32 = Unrolled32.parform
282307 val fInt64 = Unrolled64.parform
283- val fIntInf = Unrolled64.parform
308+ val fIntInf = Unrolled64.parform
309+ end )
310+
311+ structure SeqLoop =
312+ Int_ChooseFromInt (struct
313+ type 'a t = (int * int) -> (int -> unit) -> unit
314+ val fInt8 = Loops8.seqLoop
315+ val fInt16 = Loops16.seqLoop
316+ val fInt32 = Loops32.seqLoop
317+ val fInt64 = Loops64.seqLoop
318+ val fIntInf = LoopsInt.seqLoop
319+ end )
320+
321+ structure SeqReduce =
322+ Int_ChooseFromInt (struct
323+ type 'a t = ('a * 'a -> 'a) -> 'a -> (int * int) -> (int -> 'a) -> 'a
324+ val fInt8 = Loops8.seqReduce
325+ val fInt16 = Loops16.seqReduce
326+ val fInt32 = Loops32.seqReduce
327+ val fInt64 = Loops64.seqReduce
328+ val fIntInf = LoopsInt.seqReduce
329+ end )
330+
331+ structure UnrolledSeqLoop =
332+ Int_ChooseFromInt (struct
333+ type 'a t = (int * int) -> (int -> unit) -> unit
334+ val fInt8 = Unrolled8.seqLoop
335+ val fInt16 = Unrolled16.seqLoop
336+ val fInt32 = Unrolled32.seqLoop
337+ val fInt64 = Unrolled64.seqLoop
338+ val fIntInf = Unrolled64.seqLoop
339+ end )
340+
341+ structure UnrolledSeqReduce =
342+ Int_ChooseFromInt (struct
343+ type 'a t = ('a * 'a -> 'a) -> 'a -> (int * int) -> (int -> 'a) -> 'a
344+ val fInt8 = Unrolled8.seqReduce
345+ val fInt16 = Unrolled16.seqReduce
346+ val fInt32 = Unrolled32.seqReduce
347+ val fInt64 = Unrolled64.seqReduce
348+ val fIntInf = Unrolled64.seqReduce
284349 end )
285350
286351 local
@@ -314,6 +379,22 @@ struct
314379 in
315380 primSporkChoose (__inline_always__ loopBody, __inline_always__ unrolledImpl, __inline_always__ regularImpl)
316381 end
382+
383+ fun __inline_always__ unifiedSeqLoop (lo: int, hi: int) (f: int -> unit) : unit =
384+ let
385+ fun __inline_always__ regularImpl () = __inline_always__ SeqLoop.f (lo, hi) f
386+ fun __inline_always__ unrolledImpl () = __inline_always__ UnrolledSeqLoop.f (lo, hi) f
387+ in
388+ Scheduler.primLoopChoose (__inline_always__ f, __inline_always__ unrolledImpl, __inline_always__ regularImpl)
389+ end
390+
391+ fun __inline_always__ unifiedSeqReduce (combine: 'a * 'a -> 'a) (zero: 'a) (lo: int, hi: int) (f: int -> 'a) : 'a =
392+ let
393+ fun __inline_always__ regularImpl () = __inline_always__ SeqReduce.f combine zero (lo, hi) f
394+ fun __inline_always__ unrolledImpl () = __inline_always__ UnrolledSeqReduce.f combine zero (lo, hi) f
395+ in
396+ Scheduler.primLoopChoose (__inline_always__ f, __inline_always__ unrolledImpl, __inline_always__ regularImpl)
397+ end
317398 in
318399 val reducem = __inline_always__ unifiedReducem
319400 val reduce = __inline_always__ unifiedReducem
@@ -322,6 +403,8 @@ struct
322403 val parformDefault = __inline_always__ Parform.f
323404 val pareduce = __inline_always__ unifiedPareduce
324405 val parfor = __inline_always__ ForkJoin0.parfor
406+ val seqLoop = __inline_always__ unifiedSeqLoop
407+ val seqReduce = __inline_always__ unifiedSeqReduce
325408 end
326409
327410 val pareduceBreakExn = __inline_always__ PareduceBreakExn.f
0 commit comments