kikawet / piFromCoprimes

multithreading pi generator from random coprimes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PiFromCoprimes CI/CD Quality Gate Status

English 🇬🇧/🇺🇲

Inspired in Matt Parker video "Generating π from 1,000 random numbers"

In case you want to run it you can do it in the Repl.it post I created. You don't need to install nothing just click "Run" and watch it go.

Once is over is going to generate a file called PI.txt with 100 digits in case you want it.

If you're going to download the code and run it in your machine you're going to need Boost and C++20.

This program runs in multithreading (5 threads by default) you can change the number of threads in the code and how many iterations is going to do, so far is going to run 2128 (340.282.366.920.938.463.463.374.607.431.768.211.456).

The console is going to print the next approximation after ~4 seconds (the time is going to change if you change the number of threads).

Pseudocode

let numOfThreads = 5
let threadVector[] let math[] // vector to store the partial sum of each thread
let limit = 2128
let refreshRate = 216
for i = 0 up to numOfThreads:
     math[i] = (0,0) // each element contains how much pairs were coprimes and the total number of generated pairs
     threadVector[i].start(i, limit/numOfThreads, refreshRate) // each thread can now start to generate numbers

Once every thread is started the main thread waits 2 seconds and iterates the math vector and prints the partial result according to the math showed in the video.

Castellano 🇪🇸/🇲🇽

Inspirado por el video de Matt Parker "Generating π from 1,000 random numbers"

Si quieres ejecutar el proyecto he creado un post en Repl.it. No hace falta instalarse nada, para ejecutarlo solo darle a ejecutar en la página y disfrutar.

Cuando termine se generará un fichero llamado PI.txt con las 100 primeros digitos calculados.

En caso de quere descargar el proyecto para ejecutarlo en local es necesario tener instalado Boost y C++20.

La aplicación se ejecuta en paralelo (5 hilos por defecto) se puede cambiar el número de hilos dentro del código así como el número total de iteracionesa ejecutar, por defecto se calculan 2128 (340.282.366.920.938.463.463.374.607.431.768.211.456) iteraciones.

Cada ~4 segundos (el tiempo varia en función del número de hilos) se muestra la siguiente estimación con los números generados hasta el momento.

Pseudocódigo

let numDeHilos = 5
let vectorHilos[] let mates[] // vector para almacenar las sumas parciales de cada hilo let limite = 2128
let tasaActualiza = 216
for i = 0 hasta numDeHilos:
     mates[i] = (0,0) // cada elemento almacena cuantos pares de coprimos y el total de pares generados      threadVector[i].start(i, limite/numDeHilos, tasaActualiza) // cada hilo puede empezar a generar números

Cuando todos los hilos se han empezado the hilo principal espera 2 segundos e itera el vector mates y muestra por pantalla el resultado parcial basandose en las equaciones que se muestran en el video.

About

multithreading pi generator from random coprimes

License:MIT License


Languages

Language:C 55.4%Language:CSS 20.1%Language:C++ 7.9%Language:Makefile 7.5%Language:JavaScript 4.4%Language:Awk 2.2%Language:CMake 1.5%Language:Shell 0.6%Language:Dockerfile 0.5%