mersenneforum.org  

Go Back   mersenneforum.org > Great Internet Mersenne Prime Search > Software

Reply
 
Thread Tools
Old 2021-07-17, 16:22   #1
moytrage
 
Jul 2021

3·11 Posts
Default NTT faster than FFT?

As far as I know Prime95 uses Fast Fourier Transform to multiply (square) large numbers, right?

Just for fun, recently I decided to implement from scratch both Fast Fourier Transform (FFT) and Number Theoretical Transform (NTT) for numbers multiplication. In C++ (and using SIMD instructions).

According to my time measurements NTT appeared 10-15 times faster than FFT for doing multiplications of numbers around 16 MegaBytes in size.

Both NTT and FFT code I tried to optimize as much as possible. Also tested that both algorithms gave correct results, by doing naive multiplication.

FFT was tweaked in such a way that error is around 0.07-0.1. NTT of cause gives no error as it is precise. Chinese Reminder Theorem was used at final step of NTT multiplication.

Does anyone know if there was any trial to use NTT in Prime95 engine instead of FFT? Was it successful and/or gave any speed or other benefits?
moytrage is offline   Reply With Quote
Old 2021-07-17, 18:01   #2
kriesel
 
kriesel's Avatar
 
"TF79LL86GIMPS96gpu17"
Mar 2017
US midwest

17×349 Posts
Default

Welcome.
If you're feeling brave, show some source code. The accomplished code smiths may have some helpful comments.

How do your multiplication times compare to GWNUM /prime95 timings for same number size, processor, number of cores, etc.?

I assume George did some sort of analysis if not actual experimentation with alternate code for prime95. His code is much faster than GMP multiple precision routines that avoid use of floating point because of error concerns. Prime95 uses the IBDWT and multiple instruction-set-tailored code paths.
Preda experimented with DP, SP, M31, and M61 back in gpuowl V1.9. M61 was substantially slower than DP on AMD GPUs but gave slightly higher maximum exponent for the same fft length. SP and M31 were not useful; the usable bits/word were too small. See https://www.mersenneforum.org/showpo...35&postcount=2 including the first attachment.

There's a lot of GIMPS computing reference info at https://www.mersenneforum.org/showthread.php?t=24607.

Last fiddled with by kriesel on 2021-07-17 at 18:09
kriesel is online now   Reply With Quote
Old 2021-07-17, 18:13   #3
Viliam Furik
 
Viliam Furik's Avatar
 
"Viliam Furík"
Jul 2018
Martin, Slovakia

13068 Posts
Default

Quote:
Originally Posted by moytrage View Post
As far as I know Prime95 uses Fast Fourier Transform to multiply (square) large numbers, right?
Well yes, but actually no. It uses something called IBDWT - Irrational Base Discrete Weighted Transform. But it is similar.

Quote:
Originally Posted by moytrage View Post
According to my time measurements NTT appeared 10-15 times faster than FFT for doing multiplications of numbers around 16 MegaBytes in size.
16 MB in size, does that mean that a similar-sized Mersenne number is 2^134217728-1 (if Megabyte = 1024*1024 bytes, sometimes called Mebibyte) or 2^128000000-1 (if Megabyte is 1000*1000 bytes)?

What times did you get with your code, and what times do you get with Prime95 at similar exponents?
Viliam Furik is offline   Reply With Quote
Old 2021-07-17, 18:20   #4
moytrage
 
Jul 2021

3·11 Posts
Default

@kriesel Thanks for quick reply!

Right now my code is quite dirty draft. Is not very well suited for usage or reading.

Also didn't do comarison to current's Prime95 code. Sure Prime95's FFT is much faster than mine.

I just wanted to create a Proof of Concept about comparison of FFT and NTT for myself.

Actually I quite accidentally found out that NTT was much faster. Before implementing I didn't even expect this.

Just to make comparison of 2 my algos fair I made as much optimizations as possible and used SIMD instructions. Actually only FFT is currently using AVX1 SIMD in my code. So FFT is even in better position. Still NTT is 10-15 times faster than FFT.

Actually by posting this Thread first of all I wanted to find out if anyone knows if there were any efforts or comparisons to NTT implementations? Maybe there were already such exepriments and they didn't show such good improvement of NTT compared to FFT.

I can do speed comparisons if somebody can point out to me how to do that inside gwnum. If there is a ready made function that does multiplication of two large numbers. I.e. if there is some imaginary function `mult(char const * a, char const * b, int size, char * c)` that is when given two arrays of bytes computes resulting multiplication of those two. I can call such function to compare to my NTT.

Just of curiosity I compared my NTT implementation to some random optimized NTT inside GitHub like this one https://github.com/Mysticial/ProtoNT...aster/Binaries , this implementation is said to be a part of Y-Cruncher, a famous one. So my implementation is 5-7% slower than this, hence I can conclude that mine is quite fast.
moytrage is offline   Reply With Quote
Old 2021-07-17, 18:27   #5
Viliam Furik
 
Viliam Furik's Avatar
 
"Viliam Furík"
Jul 2018
Martin, Slovakia

2×5×71 Posts
Default

I think the easiest way to do a speed comparison is to find a similar size exponent of a Mersenne number, and run its PRP test in Prime95 for a few iterations. The reported time per iteration is your result for Prime95, and thus gwnum.

Make sure your code is using the same number of cores as Prime95.
Viliam Furik is offline   Reply With Quote
Old 2021-07-17, 18:32   #6
moytrage
 
Jul 2021

3·11 Posts
Default

Quote:
Originally Posted by Viliam Furik View Post
What times did you get with your code, and what times do you get with Prime95 at similar exponents?
Right now I re-measured timings of my NTT multiplying two large integers of size 2^25 bytes each (2^22 uint-64 words each). For my measurement to be fair I also measured this NTT https://github.com/Mysticial/ProtoNT...aster/Binaries which is a part of Y-Cruncher.

So for this two numbers each 2^25 bytes I get 9.8 seconds on my NTT and 9.1 seconds on Y-Cruncher's NTT on my old 1Ghz laptop, these two a measured single-threaded.

If somebody can point me to how I can do multiply using `gwnum` lib or how to use Prime95 to test one multiplication of given size, then I can do measurements on my laptop.

But just to tell in advance - I actually wanted to tell not that my implementation is fast but wanted to stress out that NTT is maybe faster compared to FFT.
moytrage is offline   Reply With Quote
Old 2021-07-17, 18:52   #7
Viliam Furik
 
Viliam Furik's Avatar
 
"Viliam Furík"
Jul 2018
Martin, Slovakia

10110001102 Posts
Default

Quote:
Originally Posted by moytrage View Post
If somebody can point me to how I can do multiply using `gwnum` lib or how to use Prime95 to test one multiplication of given size, then I can do measurements on my laptop.
If "two large integers of size 2^25 bytes each (2^22 uint-64 words each)" are each about the same size as 2^268435399-1, you can use Prime95 with this worktodo line:

Code:
PRP=N/A,1,2,268435399,-1,85,0
Let it run until you get a few print-outs in the worker window, and tell us the average of the numbers it says after "ms/iter:". 1 iteration is a little bit more than 1 multiplication.

If you are testing your NTT and y-cruncher's NTT on one core, one thread, set it in the Prime95 in the upper menu Test -> Worker Windows (number of workers = 1, CPU cores to use = 1).

Last fiddled with by Viliam Furik on 2021-07-17 at 19:00 Reason: Tf level too low + correction
Viliam Furik is offline   Reply With Quote
Old 2021-07-17, 18:59   #8
paulunderwood
 
paulunderwood's Avatar
 
Sep 2002
Database er0rr

11×359 Posts
Default

Quote:
Originally Posted by Viliam Furik View Post
1 iteration is 1 multiplication.
Not quite true. 1 iteration is 1 multiplication plus 1 mod reduction. Of course mod reduction is fast for Mersenne numbers but not entirely insignificant.
paulunderwood is online now   Reply With Quote
Old 2021-07-17, 19:01   #9
Viliam Furik
 
Viliam Furik's Avatar
 
"Viliam Furík"
Jul 2018
Martin, Slovakia

2×5×71 Posts
Default

Quote:
Originally Posted by paulunderwood View Post
Not quite true. 1 iteration is 1 multiplication plus 1 mod reduction. Of course mod reduction is fast for Mersenne numbers but not entirely insignificant.
Corrected, thanks!
Viliam Furik is offline   Reply With Quote
Old 2021-07-17, 19:14   #10
moytrage
 
Jul 2021

3·11 Posts
Default

Quote:
Originally Posted by Viliam Furik View Post
Let it run until you get a few print-outs in the worker window.
Started your Prime95 speed test, all as you suggested. But already running for 15 minutes and still there is no print-out in worker's window.

Can you tell approximately how often print out to worker window is done for exponent of this size at 1 thread (1 core of 1Ghz)?
moytrage is offline   Reply With Quote
Old 2021-07-17, 19:44   #11
kriesel
 
kriesel's Avatar
 
"TF79LL86GIMPS96gpu17"
Mar 2017
US midwest

17·349 Posts
Default

Quote:
Originally Posted by moytrage View Post
Right now I re-measured timings of my NTT multiplying two large integers of size 2^25 bytes each (2^22 uint-64 words each). For my measurement to be fair I also measured this NTT https://github.com/Mysticial/ProtoNT...aster/Binaries which is a part of Y-Cruncher.

So for this two numbers each 2^25 bytes I get 9.8 seconds on my NTT and 9.1 seconds on Y-Cruncher's NTT on my old 1Ghz laptop, these two a measured single-threaded.

If somebody can point me to how I can do multiply using `gwnum` lib or how to use Prime95 to test one multiplication of given size, then I can do measurements on my laptop.

But just to tell in advance - I actually wanted to tell not that my implementation is fast but wanted to stress out that NTT is maybe faster compared to FFT.
Based on the conclusions of several other researchers and excellent coders in the area, "NTT is maybe faster compared to FFT" seems an inexplicable result. For a summary of the current state of the art see https://www.mersenneforum.org/showpo...21&postcount=7.

To run prime95 for comparison timings: In the reference info link I gave you earlier, scroll down to the thread that is specific to prime95. Read and follow the how to get started info in that thread's first post. Create worktodo entries manually. Set workers to one, cores to one. Set prime95 to print out interim residues and timing every 100 iterations. This timing will include the error detection's slight overhead. Prime95 can be configured for fraction of a second resolution on time stamp of screen output also. There are several good documentation files included in the package, recommended reading. Try to replicate in prime95, with timing, while nothing else runs, the 100-iteration residues for larger known Mersenne primes in the attachment of https://www.mersenneforum.org/showpo...83&postcount=5.
Then attempt to compute the same type 1 PRP sequence with a=3 using your FFT or NTT code, with output of timing and least significant 64 bits of the 100-iteration results, on the same exponents, iterations, and hardware.

It's not clear to me how you avoid issues with carry propagation or loss, apparently using no guard bits. Perhaps I misunderstood your reference to 225 byte operands (32MB each) in 222 8-byte words each.

As a point of reference, from one of the benchmark attachments in that prime95 reference thread, on a dual-core E8200 Core2Duo, in older prime95 v29.4b8, 32M 64-bit words fft length, max ~596M exponent, I get ~2.53 iter/sec. If I pessimistically scale that down 2:1 for core count and 2.66:1 for clock rate, I extrapolate 1/2.53 * 2 *2.66 = 2.1 seconds*core-GHz/iter. Same hardware, scaled pessimistically to 4M DPwords fft length, 1 core, 1 GHz, max ~79M exponent, ~23.5 iter/sec; 1/23.5*2*2.66 ~0.226 sec*core-GHz/iter.

The E8200 is SSE2 at best as I recall; https://ark.intel.com/content/www/us...3-mhz-fsb.html does not say. (Prime95 is usually more memory bandwidth limited than cpu limited.)

Last fiddled with by kriesel on 2021-07-17 at 19:45
kriesel is online now   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
PRP on gpu is faster that on cpu indomit Information & Answers 4 2020-10-07 10:50
faster than LL? paulunderwood Miscellaneous Math 13 2016-08-02 00:05
My CPU is getting faster and faster ;-) lidocorc Software 2 2008-11-08 09:26
Faster way to do LLT? 1260 Miscellaneous Math 23 2005-09-04 07:12
Faster than LL? clowns789 Miscellaneous Math 3 2004-05-27 23:39

All times are UTC. The time now is 14:12.


Wed Dec 8 14:12:16 UTC 2021 up 138 days, 8:41, 1 user, load averages: 1.59, 1.52, 1.55

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.