🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.forth
Article #62

Re: Pairwise dot product

#62
From: peter
Date: Mon, 12 Oct 2026 09:11
125 lines
6756 bytes
On Fri, 09 Oct 2026 06:32:42 GMT
anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:

> Paul Rubin <no.email@nospam.invalid> writes:
> >anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
> >> Not sure what you mean with "AVX intrinsics" in the context of Forth;
> >
> >Well, your v* primitive is written in C, so it could potentially use AVX
> >vector operations.  _mm512_fmadd_pd to do pointwise multiply-adds into a
> >YMM register and _mm512_reduce_add_pd to sum the pointwise results at
> >the end, if I have it right.
> 
> You mean AVX "p"arrallel instructions rather than "s"calar
> instructions used by the generated code, in order to vectorize the code.
> 
> Actually, if V* used unit stride and was compiled with -ffast-math
> (which allows the compiler to pretend that the associative law holds
> for FP addition and multiplication, among other things), gcc would
> vectorize V* and, with -march=x86-64-v3 use the AVX instructions
> corresponding to these intrinsics, so one gets the result without
> using intrinsics.
> 
> But given that the stride can be anything, vectorizing V* is not
> profitable, so gcc does not do it.  One could special-case unit stride
> to get at least that case vectorized, but the benchmark I used does
> not use unit stride.
> 
> Even in the vectorized case, the dependency between the FP additions
> would be a problem; the compiler could unroll the loop and (thanks to
> -ffast-math) reassociate the additions from
> 
> ... f* f+ ... f* f+ ... f* f+
> 
> where every F+ depends on the one before to
> 
> ... f* ... f* f+ ... f* f+ f+
> 
> i.e., only the last f+ depends on the previous iteration, while the
> other two are in a new dependence chain.  And if unrolling by a factor
> of 3 is not enough, use more unrolling until the loop execution time
> is dominated by resource consumption rather than latency, but my
> impression of the gcc- and clang-generated code is that the versions I
> looked at are not doing that.
> 
> You can call the BLAS routine DDOT of libopenblas or some other
> well-optimized BLAS library and probably get close to optimal code.
> 
> - anton

I remembered that when you compared pairwise sum some time back that 
I tested some avx-512 variants with great results. I got interested
again and did some avx-512 variants of fdot. I had to change the
example to use stride=1 and 2 separate vectors instead of 2 interleaved
as in your examples. The calling interface got also simpler
( v1-addr v2-addr v-len -- r: fdot)

The result was that using avx-512 got almost the expected 8 times
improvement. Further unrolling did not show significant gains.
All tests run on a zen5 ryzen 9950x

Here are the results, same test data, same number of iterations.

                     fdot1         fdot8          fdot16          fdot32
cycles       1,004,957,412   155,618,582     138,800,039     148,334,303
instructions 3,504,229,271   441,949,102     317,079,085     254,819,091
 
 fdot1 uses xmm registers 1 double at a time
 fdot8 uses zmm registers 8 doubles at a time
fdot16 uses zmm unrolled 2 times
fdot32 uses zmm unrolled 4 times

The actual vector multiplication uses them in sequence
: V*1 fdot1 ;
: V*8 fdot8 fdot1 f+ ;
: V*16 fdot16 v*8 f+ ;
: V*32 fdot32 v*16 f+ ;

I run the benchmark in wsl under windows 11
as they run very quick, around 30ms, the timings are not that
stable and the CPU reaches only 4.5Ghz 
Increasing the number of 10x gives more stable output
and a CPU that reaches 5.3Ghz.

I found this blog post that explains the zen5 improvements interesting
https://www.numberworld.org/blogs/2024_8_7_zen5_avx512_teardown/

Below is the disassembly of the fdot16. As lxf64 do not generate avx-512
code I coded them in assembler directly.

seea fdot16
0x428B10  C4C17B1145F8     vmovsd qword ptr [r13-0x8], xmm0
0x428B16  4D8D6DF8         lea r13, [r13-0x8]
0x428B1A  62F1FD4857C0     vxorpd zmm0, zmm0, zmm0
0x428B20  488B4D00         mov rcx, qword ptr [rbp]
0x428B24  488B5508         mov rdx, qword ptr [rbp+0x8]
0x428B28  4809DB           or rbx, rbx
0x428B2B  7464             jz 0x428B91
0x428B2D  4889D8           mov rax, rbx
0x428B30  4883E0F0         and rax, 0xFFFFFFFFFFFFFFF0
0x428B34  4829C3           sub rbx, rax
0x428B37  62F1FD481009     vmovupd zmm1, zmmword ptr [rcx]
0x428B3D  62F1FD48105101   vmovupd zmm2, zmmword ptr [rcx+0x40]
0x428B44  62F1F548590A     vmulpd zmm1, zmm1, zmmword ptr [rdx]
0x428B4A  62F1ED48595201   vmulpd zmm2, zmm2, zmmword ptr [rdx+0x40]
0x428B51  4881C180000000   add rcx, 0x80
0x428B58  4881C280000000   add rdx, 0x80
0x428B5F  62F1F54858CA     vaddpd zmm1, zmm1, zmm2
0x428B65  62F1FD4858C1     vaddpd zmm0, zmm0, zmm1
0x428B6B  4883E810         sub rax, 0x10
0x428B6F  75C6             jnz 0x428B37
0x428B71  62F3FD481BC101   vextractf64x4 ymm1, zmm0, 0x1
0x428B78  C5F558D0         vaddpd ymm2, ymm1, ymm0
0x428B7C  62F3FD2819D301   vextractf64x2 xmm3, ymm2, 0x1
0x428B83  C5E558E2         vaddpd ymm4, ymm3, ymm2
0x428B87  C4E37905EC01     vpermilpd xmm5, xmm4, 0x1
0x428B8D  C5DB58C5         vaddsd xmm0, xmm4, xmm5
0x428B91  48894D00         mov qword ptr [rbp], rcx
0x428B95  48895508         mov qword ptr [rbp+0x8], rdx
0x428B99  C5F877           vzeroupper
0x428B9C  C3               ret
141 bytes, 30 instructions

BR
Peter
Technical Details
Message-ID: <20261012111148.000018b2@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-mail
References: <2026Oct8.175513@mips.complang.tuwien.ac.at> <87ld882afn.fsf@nightsong.com> <2026Oct9.002856@mips.complang.tuwien.ac.at> <87h5iv36p4.fsf@nightsong.com> <2026Oct9.083242@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: Mon, 12 Oct 2026 11:11:48 +0200
Organization: A noiseless patient Spider
Lines: 125
Message-ID: <20261012111148.000018b2@tin.it>
References: <2026Oct8.175513@mips.complang.tuwien.ac.at>
	<87ld882afn.fsf@nightsong.com>
	<2026Oct9.002856@mips.complang.tuwien.ac.at>
	<87h5iv36p4.fsf@nightsong.com>
	<2026Oct9.083242@mips.complang.tuwien.ac.at>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 12 Oct 2026 09:11:59 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3519062"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX183ZPZpaA49qfuj382jcyKnUDGQCB/WRYg="; posting-host="7a703e040d3f802379e38c80c745765d"
Cancel-Lock: sha1:b5IUV/RREIpmVUWSu9EJoR6z+QM= sha256:o9gnLQj4Qz3bjTawS5V9J8GPMotwsUBXJqRVgv4sk2k=
	sha1:nCVYSpJ1jA2uIZ3cmWFtphJgD8A= sha256:0nsLkI4V4aRL15zVJTAl0cFnAo8FiNg5IJ/CW+3lm3c=
X-Newsreader: Claws Mail 4.4.0 (GTK 3.24.51; x86_64-w64-mingw32)