40 Commits
v0.2 ... v0.6

Author SHA1 Message Date
af79d206d3 Added entry for v0.6 2016-12-14 20:12:46 +01:00
4b034ce5e3 Print more info about the file writing. 2016-12-14 19:34:50 +01:00
f146dbf11c Able to save found primes to a file afterwards. 2016-12-14 19:28:28 +01:00
656fee720e Allow user to choose base. 2016-12-14 19:06:33 +01:00
5e45656e1a Forgot that from the switch to C. 2016-12-14 17:58:12 +01:00
c522196d66 Argument parsing! 2016-12-14 17:52:52 +01:00
b5dcadce19 Added new goal for v0.5 2016-12-14 17:31:36 +01:00
003b94dcdb Added some more useful docs and stuff. 2016-12-14 16:14:58 +01:00
fcee95da17 Fixed something from the description 2016-12-14 16:14:37 +01:00
43620ba2d3 Enable documentation. 2016-12-14 16:08:11 +01:00
9f1160242a Removed warning (no longer necessary). 2016-12-14 15:56:28 +01:00
b414bff9dc Minor optimization. 2016-12-14 15:55:39 +01:00
d8c81b172b Print some fun information at the end. 2016-12-14 15:29:06 +01:00
c2f0fb0ffd Defining changes for v0.5 2016-12-14 15:05:24 +01:00
5bbac132bc Use size_t, which is better for arrays of very large sizes. 2016-12-14 14:13:32 +01:00
2a3e97f4bc Prepare version number so I don't forget later. 2016-12-13 18:09:05 +01:00
06d5ddb0cc Fixed problem with the algorithm.
If it's equal to half then we want to check if it's divisible, since if
it's half then it is NOT prime.
2016-12-13 18:05:02 +01:00
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
9 changed files with 2656 additions and 38 deletions

3
.gitignore vendored
View File

@ -2,5 +2,8 @@
build/*
!build/.keep
# Ignore documentation
doc/
# Ignore vim temporary files
*.sw[a-z]

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,21 @@ 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.
- v0.5: Minor Changes
- Use `size_t' instead of `unsigned long long int'.
- Minor optimizations to the algorithm.
- Added commandline argument parsing.
- v0.6: User Control
- Allow user to choose base in which the prime numbers are printed.
- Give option for primes to be saved to a file upon exit.
- Free memory and leave instead of emergency exit.

View File

@ -24,10 +24,10 @@ set(SRCS
# Define the C flags.
set(CMAKE_C_FLAGS "-std=gnu99 -Wall -Wextra -Werror -Wfatal-errors -Wmissing-declarations -pedantic-errors")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -g -O0")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O3")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS} -g -O3")
set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS} -Os")
set(CMAKE_C_FLAGS_DEBUG "-g -O0")
set(CMAKE_C_FLAGS_RELEASE "-O3")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-g -O3")
set(CMAKE_C_FLAGS_MINSIZEREL "-Os")
if(NOT CMAKE_BUILD_TYPE MATCHES Debug AND NOT CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
add_definitions(-DNDEBUG)

2489
Doxyfile Normal file

File diff suppressed because it is too large Load Diff

View File

@ -2,15 +2,12 @@
#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 +17,18 @@ void initList(List *l) {
l->end = 0;
}
void deInitList(List *l) {
void deInitList(List *restrict l) {
for(size_t 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

@ -1,3 +1,10 @@
/**
* @file list.h
* @author Deathsbreed <deathsbreed@themusicinnoise.net>
* @brief Code responsible for List management.
* @details Code responsible for the definition and management of the
* List object.
*/
#pragma once
#include <gmp.h>
#include <stdbool.h>
@ -7,25 +14,24 @@
*/
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)
size_t size; //!< How many elements are in the list
size_t end; //!< The last element of the list (in use)
} List;
/**
* @brief Initialize a List.
* @details Initialize the list and its variables, allocating memory
* to the pointer array inside. Returns true on success and false on
* failure.
* to the pointer array inside.
* @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

@ -2,20 +2,76 @@
#include <stdlib.h>
#include <signal.h>
#include <stdbool.h>
#include <unistd.h>
#include <gmp.h>
#include "list.h"
#define VERSION "v0.6"
static bool run;
void printUsage(char *progName);
void leave();
int main(void) {
printf("Indivisible v0.2\n");
int main(int argc, char *argv[]) {
bool f_help = false,
f_version = false,
f_quiet = false;
int base = 10;
char *file = NULL;
int c;
while((c = getopt(argc, argv, "hvqb:f:")) != -1) {
switch(c) {
case 'h':
f_help = true;
break;
case 'v':
f_version = true;
break;
case 'q':
f_quiet = true;
break;
case 'b':
base = atoi(optarg);
if(base < 2 || base > 62) {
fprintf(stderr,
"Invalid base `%d'. Base must be between 2 and 62\n",
base);
exit(1);
}
break;
case 'f':
file = optarg;
break;
default:
printUsage(argv[0]);
exit(1);
}
}
if(f_help) {
printUsage(argv[0]);
puts(" -h print this help information");
puts(" -v print version number of program");
puts(" -q quiet mode");
puts(" -b <base> base in which to print primes between 2 and 62");
puts(" -f <file> file to save primes to");
return 0;
} else if(f_version) {
printf("Indivisible %s\n", VERSION);
return 0;
}
// Quit on ^C by setting `run = false'
run = true;
signal(SIGINT, leave);
if(f_quiet) {
puts("Use Ctrl+C (SIGINT) to exit.");
}
// Primes we've found
List primes;
initList(&primes);
@ -27,39 +83,89 @@ int main(void) {
// Add 2, a known prime to this list
mpz_set_ui(num, 2);
addToList(&primes, num);
if(mpz_out_str(stdout, 10, num) == 0) {
fprintf(stderr, "Could not print to `stdout'!\n");
exit(1);
if(!f_quiet) {
if(mpz_out_str(stdout, base, num) == 0) {
fprintf(stderr, "Could not print to `stdout'!\n");
goto releaseMemory;
}
printf("\n");
}
printf("\n");
mpz_add_ui(num, num, 1);
// Variable for half `num'
mpz_t halfNum;
mpz_init(halfNum);
do {
// Loop through found primes
for(unsigned long long int i = 0; i < primes.size; ++i) {
// Calculate half of `num'
mpz_fdiv_q_ui(halfNum, num, 2);
/**
* Loop through primes we've found until we get to half of the number
* we're analyzing
*/
for(size_t i = 0; mpz_cmp(primes.list[i], halfNum) < 0; ++i) {
// 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
addToList(&primes, num);
if(mpz_out_str(stdout, 10, num) == 0) {
fprintf(stderr, "Could not print to `stdout'!\n");
exit(1);
if(!f_quiet) {
if(mpz_out_str(stdout, base, num) == 0) {
fprintf(stderr, "Could not print to `stdout'!\n");
goto releaseMemory;
}
printf("\n");
}
printf("\n");
nextPrime:
// Add 2 (skip even numbers since they're all divisible by 2)
mpz_add_ui(num, num, 2);
} while(run);
printf("Found %zu primes.\n", primes.end);
// Clear GMP variables
mpz_clear(halfNum);
mpz_clear(num);
if(file != NULL) {
FILE *outFile = fopen(file, "w+");
if(outFile == NULL) {
fprintf(stderr, "Failed create file `%s'.\n", file);
goto releaseMemory;
}
printf("Writing primes to `%s'...\n", file);
puts("0%");
for(size_t i = 0; i < primes.end; ++i) {
if(mpz_out_str(outFile, base, primes.list[i]) == 0) {
fprintf(stderr, "Error occurred while writing to file `%s'.\n", file);
goto releaseMemory;
}
fprintf(outFile, "\n");
if(i == primes.end / 4) puts("25%");
else if(i == primes.end / 2) puts("50%");
else if(i == primes.end * 3 / 4) puts("75%");
}
if(fclose(outFile) != 0) {
fprintf(stderr, "Failed to close file `%s'.\n", file);
goto releaseMemory;
}
puts("100%");
puts("Finished writing primes.");
}
releaseMemory:
puts("Clearing memory...");
// Deinitialize the list
deInitList(&primes);
puts("Exit successful.");
return 0;
}
void leave() {
printf("Exiting...\n");
run = false;
void printUsage(char *progName) {
printf("%s [options...]\n", progName);
}
void leave() { run = false; }

View File

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