🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.forth
Article #36

Re: Pairwise dot product

#36
From: Anton Ertl
Date: Thu, 08 Oct 2026 21:52
107 lines
6129 bytes
anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>I discussed summing up an array with pairwise addition earlier
><2025Jul16.132504@mips.complang.tuwien.ac.at>.  The benefits compared
>to the straightforward algorithm are:
>
>* Better performance, because the straightforward algorithm is limited
>  by the dependence chains of the F+ computations.
>
>* Lower rounding errors in the typical case.
>
>The dot-product of two vectors is a primitive in linear algebra, and
>also has such a chain of F+ if implemented natively.  Implementing the
>dot product with pairwise addition should have similar benefits.
>
>Gforth already has a word for the dot product:
>
>|'v*' ( f-addr1 nstride1 f-addr2 nstride2 ucount - r ) gforth-0.5 "v-star"
>|   dot-product: r=v1*v2.  The first element of v1 is at f_addr1, the
>|next at f_addr1+nstride1 and so on (similar for v2).  Both vectors have
>|ucount elements.
>
>This word is implemented as C function, which allows us to compare the
>performance of the various Forth systems to C.
>
>I have implemented variants of v* in Forth, a straightforward one and
>one with pairwise addition.  The code can be found in
>
>https://www.complang.tuwien.ac.at/forth/programs/pairwise-fdot.4th
>
>Note that if your Forth system only supports an 8-deep FP stack, you
>must not call PAIRWISE-V* unless the FP stack is empty.
>
>I have measured the performance for 10000 dot products of vectors of
>50000 elements (so the straightforward algorithm has a minimum
>run-time of 0.5G times the latency of F+.
>
>Here are the results (measured on Zen4 (Ryzen 8700G)):
>
>   gforth-fast gforth-fast-v3     lxf            sf64          vfx64
>                            cycles:u
> 1_543_071_551  2_044_522_195                                             v*
> 6_095_056_325  6_544_418_067  3_221_307_358  4_579_918_381 3_425_243_416 st
> 4_350_367_901  4_371_526_269 10_527_316_831 10_630_319_712 1_203_870_244 pw
>
>                           instructions:u
> 3_618_080_286  3_117_767_051                                             v*
>11_618_280_866 11_617_967_567  9_516_121_645 17_010_999_668 6_074_024_083 st
>10_164_410_950 10_164_097_601 14_265_222_850 16_962_950_438 3_823_084_163 pw

I looked at the lxf and sf64 pairwise words, and found that they
contained calls to some replacement words for primitives in Gforth.
So I changed these replacements into macros.  The result is as follows:

   gforth-fast gforth-fast-v3     lxf            sf64          vfx64
                            cycles:u
 1_539_448_712  2_039_584_263                                             v*
 6_089_465_716  6_543_141_374  3_219_098_562  4_712_813_408 3_424_493_885 st
 4_381_413_861  4_373_619_348  4_365_848_521 11_983_572_054 1_204_079_565 pw
                                                                            
                           instructions:u
 3_618_136_380  3_117_823_273                                             v*
11_618_336_625 11_618_023_393  9_509_945_380 16_011_408_567 6_072_662_430 st
10_164_466_634 10_164_153_289  8_266_865_885 15_963_359_138 3_821_722_609 pw

These changes help lxf pairwise a lot, but for SwiftForth they reduce
the executed instructions, but increase the cycles.

>The Forth versions are:
>gforth-fast    current development version
>gforth-fast-v3 current development version compiled with -march=x86-64-v3
>                   this makes use of fma instructions
>lxf 1.6-982-823
>sf64 SwiftForth 4.0.0-RC89
>vfx64 5.43
>
>The versions are:
>tag word        Explanation
>v*  v*          Gforth primitive implemented in C
>st  naive-v*    Straightforward implementation
>pw  pairwise-v*
>
>VFX64 shows that pairwise addition can be faster than even the C
>implementation of the straightforward algorithm (v* in gforth-fast).
>VFX also executes slightly fewer instructions than V* on Gforth-fast:
>V* has loop overhead, whereas PAIRWISE-V* uses base steps in the
>recursion for vectors with up to 64 elements, resulting in less
>overhead.
>
>With FMA instructions V* executes fewer instructions (6 per iteration
>instead of 7), but more cycles (4 per iteration instead of 3).  My
>explanation is that the FMA instruction has 4 cycles of latency on
>Zen4, while fadd has 3 cycles of latency.  Interestingly, in
>gforth-fast-v3 NAIVE-V* takes the same number of instructions and one
>cycle per iteration more than in gforth-fast, but I have not looked at
>why that is.
>
>In gforth-fast the PAIRWISE-V* is quite a bit faster than NAIVE-V*.
>It also takes a little fewer instructions, probably because of the
>unfolding.  But V* is still a lot faster, so our code generation
>should be improved.

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: https://forth-standard.org/
EuroForth 2026 CFP: http://www.euroforth.org/ef26/cfp.html
Technical Details
Message-ID: <2026Oct8.235214@mips.complang.tuwien.ac.at>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <2026Oct8.175513@mips.complang.tuwien.ac.at>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: Pairwise dot product
Date: Thu, 08 Oct 2026 21:52:14 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 107
Message-ID: <2026Oct8.235214@mips.complang.tuwien.ac.at>
References: <2026Oct8.175513@mips.complang.tuwien.ac.at>
Injection-Date: Thu, 08 Oct 2026 22:28:50 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="257990"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/MJcsZVeKksoxUVmylH0jS"; posting-host="4440b373708cb595d29900b8ff51a766"
Cancel-Lock: sha1:L9vdv3QGpDdQ0Pph8B0lpS33HL8= sha256:3HMaPVMZY9/256/YOvgBkU7IPyti/sYD+OAC+eJHeYY=
	sha1:IlS/T9nfjMPSBuV63uEM39xR70c= sha256:MLGllEoks5PtBdw7d4U1UVMOVq6dh7HlIU/xWlOs438=
X-newsreader: xrn 10.11