Generating a Germain Prime efficiently, using Python and an 8-core CPU.

I have spent extensive time, as well as previous blog postings, exploring the subject of how to generate a Germain prime (number), in the Python (3.5) programming language, but taking advantage of parallel computing to some slight degree.

Before I go any further with this subject, I’d like to point out that generally, for production-ready applications, Python is not the best language to use. The main reason is the fact that Python is an interpreted language, even though many modern interpreted languages are compiled into bytecode before being interpreted. This makes a Python script slower by nature, than very well-written C or even C++. But what I aim to do is to use Lego-Blocks to explore this exercise, yet, to use tools which would be used professionally.

The main way I am generating prime numbers is, to start with a pseudo-random, 512-bit number (just as a default, the user can specify different bit-lengths), and then to attempt to divide this number by a list of known, definite primes, that by now only extend to 4096 (exclusively, of course), in an attempt to disprove that number prime. In about 1/8 of all cases, the number survives this test, after which I undertake a more robust, Miller-Rabin approach to try disproving it prime 192 times, probabilistically. If the number has survived these tests, my program assumes it to be prime.

Even though I’ve never been told this, the probability of a non-prime Candidate surviving even a single Miller-Rabin Test, or Witness, is very small, smaller than 1/8. This could be due to the fact that the Witness in question is raised to a high, odd exponent, in its 512-bit modulus etc., after which it would be squared some number of times. Because the first Candidate in the modulus of 4 is 3, that one actually gets squared a subsequent total of zero times. And after each exponentiation, the result could be any number in the modulus, with the possible exception of zero. It needs to become either (1) or (n-1) in the modulus of (n), for the Candidate to survive the test. (:1)

Further, there is no need for the numbers that get used as witnesses, which are pseudo-random, to be of the same, high, cryptographic quality of pseudo-random, as the candidate is, which is being tested.

But there is a sub-category of prime numbers which have recently been of greater interest to me, which is known as the Germain prime numbers, such that the Totient of that main candidate, divided by two, should also be prime. And so, if the density of prime numbers roughly equal to (n) is (1 / log(n)), and if we can assume a uniform distribution of random numbers, then the probability of finding a Germain prime is roughly (1 / (log (n))2), assuming that our code was inefficient enough actually to test all numbers. The efficiency can be boosted by making sure that the random number modulo 4 equals 3.

But one early difficulty I had with this project was, that if I needed to start with a new pseudo-random number for each candidate, on my Linux computers, I’d actually break ‘/dev/urandom’ ! Therefore, the slightly better approach which I needed to take next was, to make my initial random number the pseudo-random one, and then just to keep incrementing it by 4, until the code plodded into a Germain prime.

Even when all this is incorporated into the solution I find that with Python, I need the added advantage of parallel computing. Thus, I next learned about the GIL – The Global Interpreter Lock – and the resulting pitfalls of multi-threaded Python, which is not concurrent. Multi-threading under Python tells the O/S to allocate CPU cores as usual, but then only allows 1 core to be running at any one time! But, even under those constraints, I found that I was benefiting from the fact that my early code was able to test at least 2 candidates for primality simultaneously, those being the actual candidate, as well as its Totient divided by 2. And as soon as either candidate was disproved prime, testing on the other could be stopped quickly. This reduced the necessary number of computations dramatically, to make up for the slowness of multi-threaded Python, and I felt that I was on the right track.

The only hurdle which remained was, how to convert my code into multi-processing code, no longer merely multi-threaded, while keeping the ability for two processes, then, to send each other a shutdown-command, as soon as the present process disproved its number to be prime.

(Updated 6/01/2019, 17h35 … )

Continue reading Generating a Germain Prime efficiently, using Python and an 8-core CPU.

How SDL Accelerates Video Output under Linux.

What we might know about the Linux, X-server, is that it offers pure X-protocol to render such features efficiently to the display, as Text with Fonts, Simple GUI-elements, and small Bitmaps such as Icons… But then, when it’s needed to send moving pictures to the display, we need extensions, which serious Linux-users take for granted. One such extension is the Shared-Memory extension.

Its premise is that the X-server shares a region of RAM with the client application, into which the client-application can draw pixels, which the X-server then transfers to Graphics Memory.

For moving pictures, this offers one way in which they can also be ~accelerated~, because that memory-region stays mapped, even when the client-application redraws it many times.

But this extension does not make significant use of the GPU, only of the CPU.

And so there exists something called SDL, which stands for Simple Direct Media Layer. And one valid question we may ask ourselves about this protocol, is how it achieves a speed improvement, if it’s only installed on Linux systems as a set of user-space libraries, not drivers.

(Updated 10/06/2017 : )

Continue reading How SDL Accelerates Video Output under Linux.

I have now installed ‘xine’ on my Linux tablet.

In this earlier posting, I had written that I’ve installed Linux on an older tablet of mine, that being my Samsung Galaxy Tab S, First Generation, with only 16GB of storage.

In order to do so, I used the (non-rooted) applications from Google Play, ‘GNURoot’ and ‘XSDL’.

One feature which the author of ‘XSDL’ pointed out, is the fact that we may download a shared library to run under Linux, which when preloaded, makes the shared-memory extension available, for the purpose of running one application. By default pure X-server protocol does not have this, even though any half-decent Linux system has shared memory extension, X-Video extension, and beyond that, ‘vdpau‘, to allow fast video playback.

One Linux application which I had been using this way, was ‘gnome-mplayer’ , for which I had also written a shell-script, that preloads the shared-memory library. The video-player application was launching and running fine, but I’m no longer convinced that it was ever benefiting from shared memory. More specifically, we can set in the preferences of the player application, to use ‘X11′ as its video output-mode, and ‘pulseaudio’ as its audio output-mode.

Literally, selecting X11 in this way, does not mean shared memory as the output-mode, although the player could have bee negotiating with the (fake) X-server over this parameter…

So. To make sure I’d be obtaining the full benefit of shared memory, when playing back video-streams more seriously, I next proceeded to install ‘xine-ui’. It is highly-configurable, in that we can choose shared memory video-output explicitly.

Continue reading I have now installed ‘xine’ on my Linux tablet.

Continuing to Troubleshoot my Graphics Chip

The computer named ‘Phoenix’ has now been running for 6 days and 19 hours. It is approaching the point, where recently it used to crash. I have suspected that my graphics chip might be the problem, and that more specifically, this might have happened, because the graphics chip is only supposed to take 128MB of shared RAM according to the BIOS, but according to which it has been allocated 256MB, by the Linux software. It is an outdated graphics chip, which I have written about here.

Under Linux we have numerous tools that give us information about our machines, including the ‘‘ here:


I have already written that I cannot take the 70MB of shared RAM as an exact figure, of how much VRAM the chip is taking, which is actually system memory in this case, because there could be other processes using shared memory in some way. Yet, this is still an approximate estimate, and, since the amount being indicated is 70MB, the total amount of shared memory taken by my graphics chip, cannot be greater than 71MB.

This basically rules out one hypothesis for what might have been happening. Under normal use, the chip will not need more than 128MB.

Now, the fact that I have installed a new case fan, may or may not protect me from future crashes, since my other main hypothesis was, ‘Something could have been overheating on the motherboard.’

(Edit 02/14/2017 : )

And this is what my shared memory looked like today, 8 days and 23 hours after the latest reboot:


( 59+ MB – Stable )