23 Commits
v0.2 ... v0.4

Author SHA1 Message Date
a9019291c2 New goal for v0.4 2016-12-13 17:51:48 +01:00
5aa0b333c0 Made a fix, because previously it was not measuring primes. 2016-12-13 16:32:10 +01:00
3110c74174 Added entry about `restrict' keyword. 2016-12-13 16:12:31 +01:00
a5ce845c68 `restrict' keyword must be in declaration. 2016-12-13 16:12:03 +01:00
449fef2994 Add restrict
Add restrict for better pointer optimizations. This is not being applied
to `addToList()' because that function we want to thread later on.
2016-12-13 11:26:12 +01:00
2e9326b5fb Preparing for v0.4
I'm going to be learning OpenCL, after which I will decide whether to
use OpenCL or OpenMP for the development of Indivisible. This mostly
depends on the compatibility of each library with GMP, especially since
I believe GMP already does some of its own threading.
2016-12-12 23:21:22 +01:00
06cb271dba Forgot to set that shit to v0.3 2016-12-12 16:20:21 +01:00
4cbc3fae7d Cache the right .o files. 2016-12-10 17:43:31 +01:00
9b4fa96474 Remove TODO list. 2016-12-10 17:41:40 +01:00
e2aedab3b0 Added new entry. 2016-12-10 17:12:58 +01:00
bc8b48dd29 Updated info for v0.3 2016-12-10 17:12:00 +01:00
dab78093ab Deleted macros 2016-12-10 17:11:21 +01:00
a782bdb57d Organized TODO.md 2016-12-10 15:31:04 +01:00
9157d15383 Added goals for v0.3 2016-12-10 15:30:51 +01:00
f1fd758bfc Added entry for v0.2.1 (which I had previously forgotten). 2016-12-10 14:54:12 +01:00
66c0a5d027 Added a TODO list. 2016-12-10 14:54:00 +01:00
f4ee9872bc Optimize the algorithm to avoid numbers larger than half. 2016-12-10 14:46:51 +01:00
8a42e85d04 Better explanation of error. 2016-12-10 11:51:32 +01:00
dd38b53e31 Fixed leaks. 2016-12-10 11:20:01 +01:00
3c8b9922fb Shorten the long ass `unsigned long long int' to ulli. 2016-12-10 02:20:57 +01:00
cb9e1648e9 No need to use that macro outside a loop. 2016-12-10 02:09:34 +01:00
30703314dd Forgot it for the main loop as well. 2016-12-10 01:15:52 +01:00
4905391c82 Add more optimizations. 2016-12-10 00:50:23 +01:00
7 changed files with 42 additions and 19 deletions

View File

@ -17,4 +17,4 @@ build:
# Cache .o files for faster compiling
cache:
paths:
- "*.o"
- "build/CMakeFiles/indivisible.dir/src/*.o"

View File

@ -9,3 +9,13 @@ Change Log
- Switch to C.
- Uses GNU Multiple Precision library (GMP) to hold prime numbers, allowing for 'infinite' size.
- Add `likely()' and `unlikely()' macros to optimize.
- v0.2.1: Memory Leak Fixes
- Fixed a major memory leak at the end of the program.
- Added more optimizers.
- v0.3: Optimizations
- Algorithm skips half the known primes.
- Removed `likely()' and `unlikely()' macros due to lack of information.
- Improved performance.
- v0.4: Fixed Algorithm
- Fixed algorithm to actually calculate primes.
- Added extra C99 optimizations.

View File

@ -2,15 +2,13 @@
#include <stdlib.h>
#include <stdio.h>
#include "optimizers.h"
/**
* This is the number of elements by which the list expands.
* WARNING: Always use doubles for this number (2^X)
*/
#define BLOCK_SIZE 1024
void initList(List *l) {
void initList(List *restrict l) {
l->list = malloc(sizeof(mpz_t) * BLOCK_SIZE);
if(!l->list) {
fprintf(stderr, "Failed to allocate memory to list!\n");
@ -20,16 +18,18 @@ void initList(List *l) {
l->end = 0;
}
void deInitList(List *l) {
void deInitList(List *restrict l) {
for(ulli i = 0; i < l->size; ++i) {
mpz_clear(l->list[i]);
}
free(l->list);
}
void addToList(List *l, mpz_t n) {
if(l->end == l->size) {
l->size += BLOCK_SIZE;
if(unlikely(l->size == 0)) {
fprintf(stderr,
"size has reached limit of `long long int' type!\n");
if(l->size == 0) {
fprintf(stderr, "`l->size' has overflowed!\n");
exit(1);
}
void *tmp = realloc(l->list, sizeof(mpz_t) * l->size);

View File

@ -2,13 +2,15 @@
#include <gmp.h>
#include <stdbool.h>
#include "types.h"
/**
* @brief An infinitely expanding list type.
*/
typedef struct {
mpz_t *list; //!< The list of elements
unsigned long long int size; //!< How many elements are in the list
unsigned long long int end; //!< The last element of the list (in use)
ulli size; //!< How many elements are in the list
ulli end; //!< The last element of the list (in use)
} List;
/**
@ -18,14 +20,14 @@ typedef struct {
* failure.
* @param[in] l A pointer to a List type to be initialized.
*/
void initList(List *l);
void initList(List *restrict l);
/**
* @brief Deinitialize a List.
* @details Release all memory that has been allocated to the list.
* @param[in] l A pointer to a List type to be deinitialized.
*/
void deInitList(List *l);
void deInitList(List *restrict l);
/**
* @brief Adds a new item to a List type.

View File

@ -5,12 +5,13 @@
#include <gmp.h>
#include "list.h"
#include "types.h"
static bool run;
void leave();
int main(void) {
printf("Indivisible v0.2\n");
puts("Indivisible v0.4\n");
// Quit on ^C by setting `run = false'
run = true;
@ -34,11 +35,19 @@ int main(void) {
printf("\n");
mpz_add_ui(num, num, 1);
// Variable for half `num'
mpz_t halfNum;
mpz_init(halfNum);
do {
// Calculate half of `num'
mpz_fdiv_q_ui(halfNum, num, 2);
// Loop through found primes
for(unsigned long long int i = 0; i < primes.size; ++i) {
for(ulli i = 0; i < primes.end; ++i) {
if(mpz_cmp(primes.list[i], halfNum) >= 0) break;
// If `num' is divisible by a prime then go to the next number
if(mpz_divisible_p(num, primes.list[i]) != 0) goto nextPrime;
if(mpz_divisible_p(num, primes.list[i]) != 0)
goto nextPrime;
}
// `num' is a prime so we add it to the list and print it
@ -54,12 +63,15 @@ nextPrime:
mpz_add_ui(num, num, 2);
} while(run);
// Clear GMP variables
mpz_clear(halfNum);
mpz_clear(num);
// Deinitialize the list
deInitList(&primes);
return 0;
}
void leave() {
printf("Exiting...\n");
puts("Exiting...\n");
run = false;
}

View File

@ -1,3 +0,0 @@
#pragma once
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)

2
src/types.h Normal file
View File

@ -0,0 +1,2 @@
#pragma once
typedef unsigned long long int ulli;