Article View: comp.lang.forth
Article #34Re: Pairwise dot product
From: peter
Date: Thu, 08 Oct 2026 21:23
Date: Thu, 08 Oct 2026 21:23
108 lines
5721 bytes
5721 bytes
On Thu, 08 Oct 2026 15:55:13 GMT anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote: > 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 > > 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. > > I have not looked at why lxf and sf64 perform word on PAIRWISE-V* than > on NAIVE-V*. > > - anton I tested this on lxf64 running on a ryzen 9950x I got cycles instructions pairwise 789,294,432 3,628,952,994 naive 2,510,536,091 6,512,423,204 First run with naive I got 35,476,814,233 cycles and 54,012,383,854 instr. I saw that you checked for F*+ . I have that as a library call, obviously clang does not use a fma instruction for it but instead compiles a call to a libray function. Redefining it as f* f+ gave the above result. I would guess that lxf is limited by the use of the x87 fpus small stack But I need to confirm that BR Peter
Technical Details
Message-ID:
<20261008232336.0000698f@tin.it>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-mailReferences:
<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: peter <peter.noreply@tin.it> Newsgroups: comp.lang.forth Subject: Re: Pairwise dot product Date: Thu, 8 Oct 2026 23:23:36 +0200 Organization: A noiseless patient Spider Lines: 108 Message-ID: <20261008232336.0000698f@tin.it> References: <2026Oct8.175513@mips.complang.tuwien.ac.at> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Injection-Date: Thu, 08 Oct 2026 21:23:47 +0000 (UTC) Injection-Info: dont-email.me; logging-data="4179027"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Ftb5PlEgYWm3Hi2uyFXgTTVIk/lNt7sk="; posting-host="dd63b567af524a9d4c3f6459d93aa223" Cancel-Lock: sha1:dGAM587Fo2WHoKXI5k3LgTwv40A= sha256:M+KPjgbkyhPEbI/WKv+LMey6jckRAB3hqC/2x12yUNg= sha1:lttMbZVmm3TXsCbvvbkSADpoM00= sha256:kG/HkuqDtDsU+Z6gnI5rHdDeblWnC9mKt5LvhnKzFHo= X-Newsreader: Claws Mail 4.4.0 (GTK 3.24.51; x86_64-w64-mingw32)