Compare commits
55 Commits
Author | SHA1 | Date | |
---|---|---|---|
8b35c5aea3 | |||
1264edc8c8 | |||
63aa8e14cb | |||
f2eb3e869e | |||
a695cce709 | |||
096cb2eb16 | |||
1783b16024 | |||
c602b5fe1b | |||
6b2411e860 | |||
6c2f96416b | |||
fa3f2dd2b1 | |||
591ee92971 | |||
803c6f9e06 | |||
4390fca3ef | |||
2629c12f1a | |||
af79d206d3 | |||
4b034ce5e3 | |||
f146dbf11c | |||
656fee720e | |||
5e45656e1a | |||
c522196d66 | |||
b5dcadce19 | |||
003b94dcdb | |||
fcee95da17 | |||
43620ba2d3 | |||
9f1160242a | |||
b414bff9dc | |||
d8c81b172b | |||
c2f0fb0ffd | |||
5bbac132bc | |||
2a3e97f4bc | |||
06d5ddb0cc | |||
a9019291c2 | |||
5aa0b333c0 | |||
3110c74174 | |||
a5ce845c68 | |||
449fef2994 | |||
2e9326b5fb | |||
06cb271dba | |||
4cbc3fae7d | |||
9b4fa96474 | |||
e2aedab3b0 | |||
bc8b48dd29 | |||
dab78093ab | |||
a782bdb57d | |||
9157d15383 | |||
f1fd758bfc | |||
66c0a5d027 | |||
f4ee9872bc | |||
8a42e85d04 | |||
dd38b53e31 | |||
3c8b9922fb | |||
cb9e1648e9 | |||
30703314dd | |||
4905391c82 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -2,5 +2,8 @@
|
||||
build/*
|
||||
!build/.keep
|
||||
|
||||
# Ignore documentation
|
||||
doc/
|
||||
|
||||
# Ignore vim temporary files
|
||||
*.sw[a-z]
|
||||
|
@ -4,7 +4,7 @@ build:
|
||||
stage: build
|
||||
# Install dependencies
|
||||
before_script:
|
||||
- apt update && apt -y install cmake libgmp-dev
|
||||
- apt update && apt -y install cmake libgmp-dev libgomp1
|
||||
# Build the project
|
||||
script:
|
||||
- cd build/
|
||||
@ -17,4 +17,4 @@ build:
|
||||
# Cache .o files for faster compiling
|
||||
cache:
|
||||
paths:
|
||||
- "*.o"
|
||||
- "build/CMakeFiles/indivisible.dir/src/*.o"
|
||||
|
21
CHANGELOG
21
CHANGELOG
@ -9,3 +9,24 @@ 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.
|
||||
- v0.7: Data Saving
|
||||
- Allow user to save found primes to be loaded later.
|
||||
- User can save and read primes in raw output.
|
||||
|
@ -14,20 +14,22 @@ set(CMAKE_MODULE_PATH
|
||||
${CMAKE_SOURCE_DIR}/cmake/)
|
||||
|
||||
find_package(GMP REQUIRED)
|
||||
find_package(OpenMP REQUIRED)
|
||||
|
||||
include_directories(
|
||||
${GMP_INCLUDE_DIR})
|
||||
|
||||
set(SRCS
|
||||
src/main.c
|
||||
src/files.c
|
||||
src/list.c)
|
||||
|
||||
# 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 "-std=gnu99 ${OpenMP_C_FLAGS} -Wall -Wextra -Werror -Wfatal-errors -Wmissing-declarations -pedantic-errors")
|
||||
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)
|
||||
|
@ -9,6 +9,7 @@ Building
|
||||
There are multiple dependencies to install before compiling the project:
|
||||
- CMake
|
||||
- GMP
|
||||
- OpenMP
|
||||
|
||||
Once the dependencies are installed you can compile by running the following from the root directory of the project:
|
||||
```bash
|
||||
|
61
src/files.c
Normal file
61
src/files.c
Normal file
@ -0,0 +1,61 @@
|
||||
#include "files.h"
|
||||
#include <stdio.h>
|
||||
#include <gmp.h>
|
||||
#include <assert.h>
|
||||
|
||||
int inputPrimes(char *file, List *list) {
|
||||
// Assert safeguards
|
||||
assert(file != NULL);
|
||||
assert(list != NULL);
|
||||
|
||||
FILE *pFile = fopen(file, "r");
|
||||
if(pFile == NULL) return 1;
|
||||
mpz_t n;
|
||||
mpz_init(n);
|
||||
while(mpz_inp_raw(n, pFile) != 0) addToList(list, n);
|
||||
if(fclose(pFile) != 0) return 2;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int outputPrimes(char *file, List *list) {
|
||||
// Assert safeguards
|
||||
assert(file != NULL);
|
||||
assert(list != NULL);
|
||||
|
||||
FILE *oFile = fopen(file, "w");
|
||||
if(oFile == NULL) return 1;
|
||||
printf("Saving primes to `%s'...\n", file);
|
||||
puts("0%");
|
||||
for(size_t i = 0; i < list->end; ++i) {
|
||||
if(mpz_out_raw(oFile, list->list[i]) == 0) return 3;
|
||||
if(i == list->end / 4) puts("25%");
|
||||
else if(i == list->end / 2) puts("50%");
|
||||
else if(i == list->end * 3 / 4) puts("75%");
|
||||
}
|
||||
puts("100%");
|
||||
if(fclose(oFile) != 0) return 2;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int exportPrimes(char *file, List *list, int base) {
|
||||
// Assert safeguards
|
||||
assert(file != NULL);
|
||||
assert(list != NULL);
|
||||
assert(list->list != NULL);
|
||||
|
||||
FILE *eFile = fopen(file, "w");
|
||||
if(eFile == NULL) return 1;
|
||||
printf("Exporting primes to `%s'...\n", file);
|
||||
puts("0%");
|
||||
for(size_t i = 0; i < list->end; ++i) {
|
||||
if(mpz_out_str(eFile, base, list->list[i]) == 0) return 3;
|
||||
fprintf(eFile, "\n");
|
||||
if(i == list->end / 4) puts("25%");
|
||||
else if(i == list->end / 2) puts("50%");
|
||||
else if(i == list->end * 3 / 4) puts("75%");
|
||||
}
|
||||
puts("100%");
|
||||
if(fclose(eFile) != 0) return 2;
|
||||
puts("Finished exporting primes.");
|
||||
return 0;
|
||||
}
|
36
src/files.h
Normal file
36
src/files.h
Normal file
@ -0,0 +1,36 @@
|
||||
/**
|
||||
* @file files.h
|
||||
* @author Deathsbreed <deathsbreed@themusicinnoise.net>
|
||||
* @brief Functions to deal with file I/O of primes.
|
||||
* @details Functions that input, output, and export primes from/to files.
|
||||
*/
|
||||
#pragma once
|
||||
#include "list.h"
|
||||
|
||||
/**
|
||||
* @brief Load primes from an Indivisible file into a List.
|
||||
* @param file File to input primes from.
|
||||
* @param list List to load primes into.
|
||||
* @returns If 0 then load was successful, if 1 then failed to open,
|
||||
* if 2 failed to close.
|
||||
*/
|
||||
int inputPrimes(char *file, List *list);
|
||||
|
||||
/**
|
||||
* @brief Output primes from a List into an Indivisible file.
|
||||
* @param file File to output primes to.
|
||||
* @param list List to read primes from.
|
||||
* @returns If 0 then load was successful, if 1 then failed to open,
|
||||
* if 2 failed to close, if 3 failed when writing.
|
||||
*/
|
||||
int outputPrimes(char *file, List *list);
|
||||
|
||||
/**
|
||||
* @brief Export primes from a List to a plain text file.
|
||||
* @param file File to output primes as plain text to.
|
||||
* @param list List to read primes from.
|
||||
* @param base The base in which the primes will be written.
|
||||
* @returns If 0 then load was successful, if 1 then failed to open,
|
||||
* if 2 failed to close, if 3 failed when writing.
|
||||
*/
|
||||
int exportPrimes(char *file, List *list, int base);
|
17
src/list.c
17
src/list.c
@ -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,24 +17,22 @@ 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");
|
||||
exit(1);
|
||||
}
|
||||
void *tmp = realloc(l->list, sizeof(mpz_t) * l->size);
|
||||
if(!tmp) {
|
||||
fprintf(stderr, "Failed to allocate more memory to list!\n");
|
||||
exit(1);
|
||||
}
|
||||
l->list = (mpz_t*)tmp;
|
||||
l->list = tmp;
|
||||
}
|
||||
mpz_init(l->list[l->end]);
|
||||
mpz_set(l->list[l->end++], n);
|
||||
|
18
src/list.h
18
src/list.h
@ -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.
|
||||
|
201
src/main.c
201
src/main.c
@ -2,20 +2,95 @@
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
#include <gmp.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "list.h"
|
||||
#include "files.h"
|
||||
|
||||
#define VERSION "v0.7"
|
||||
|
||||
static bool run;
|
||||
|
||||
void printUsage(char *progName);
|
||||
void leave();
|
||||
|
||||
int main(void) {
|
||||
printf("Indivisible v0.2\n");
|
||||
int main(int argc, char *argv[]) {
|
||||
// Variables for argument parsing
|
||||
bool f_help = false,
|
||||
f_version = false,
|
||||
f_quiet = false;
|
||||
int base = 10;
|
||||
char *file = NULL;
|
||||
char *efile = NULL;
|
||||
|
||||
// Parse commandline arguments
|
||||
int c;
|
||||
while((c = getopt(argc, argv, "hvqb:f:e:")) != -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;
|
||||
case 'e':
|
||||
efile = optarg;
|
||||
break;
|
||||
default:
|
||||
printUsage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Act based on which flags were used
|
||||
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 (default 10)");
|
||||
puts(" -f <file> file in/from which primes are stored and read from in raw format");
|
||||
puts(" -e <file> export input file to plain text format");
|
||||
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(efile != NULL && file == NULL) {
|
||||
fprintf(stderr, "There must be an input file to export! Use `-h' for help.\n");
|
||||
return 1;
|
||||
}
|
||||
bool newFile = true;
|
||||
if(file != NULL) {
|
||||
struct stat s;
|
||||
if(stat(file, &s) == 0) newFile = false;
|
||||
}
|
||||
|
||||
int exitCode = 0;
|
||||
|
||||
// Primes we've found
|
||||
List primes;
|
||||
initList(&primes);
|
||||
@ -24,42 +99,126 @@ int main(void) {
|
||||
mpz_t num;
|
||||
mpz_init(num);
|
||||
|
||||
// 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);
|
||||
// Variable for half `num'
|
||||
mpz_t halfNum;
|
||||
mpz_init(halfNum);
|
||||
|
||||
if(efile == NULL) puts("Use Ctrl+C to exit.");
|
||||
|
||||
if(newFile) {
|
||||
// Add 2, a known prime to this list
|
||||
mpz_set_ui(num, 2);
|
||||
addToList(&primes, num);
|
||||
if(!f_quiet) {
|
||||
if(mpz_out_str(stdout, base, num) == 0) {
|
||||
fprintf(stderr, "Could not print to `stdout'!\n");
|
||||
exitCode = 1;
|
||||
goto releaseMemory;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
mpz_add_ui(num, num, 1);
|
||||
} else {
|
||||
// Load primes from file
|
||||
int err = inputPrimes(file, &primes);
|
||||
|
||||
if(err == 0) {
|
||||
printf("Loaded %zu primes.\n", primes.end);
|
||||
} else {
|
||||
if(err == 1)
|
||||
fprintf(stderr, "Failed to open Indivisible file `%s'.\n", file);
|
||||
else if(err == 2)
|
||||
fprintf(stderr, "Failed to close Indivisible file `%s'.\n", file);
|
||||
exitCode = 1;
|
||||
goto releaseMemory;
|
||||
}
|
||||
/**
|
||||
* Yes, I realize there's a -1 here, I don't know why but it won't
|
||||
* work if it's not there, so don't change it unless necessary.
|
||||
*/
|
||||
mpz_set(num, primes.list[primes.end-1]);
|
||||
}
|
||||
|
||||
if(efile != NULL) {
|
||||
int err = exportPrimes(efile, &primes, base);
|
||||
|
||||
if(err == 0) {
|
||||
puts("Successfully exported primes.");
|
||||
} else {
|
||||
if(err == 1)
|
||||
fprintf(stderr, "Failed to open/create plain text file `%s'.\n", efile);
|
||||
else if(err == 2)
|
||||
fprintf(stderr, "Failed to close plain text file `%s'.\n", efile);
|
||||
else if(err == 3)
|
||||
fprintf(stderr, "Failed to write prime to plain text file `%s'.\n", efile);
|
||||
exitCode = 1;
|
||||
}
|
||||
goto releaseMemory;
|
||||
}
|
||||
printf("\n");
|
||||
mpz_add_ui(num, num, 1);
|
||||
|
||||
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");
|
||||
exitCode = 1;
|
||||
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);
|
||||
|
||||
if(file != NULL) {
|
||||
int err = outputPrimes(file, &primes);
|
||||
if(err == 0) {
|
||||
puts("Successfully saved primes.");
|
||||
} else {
|
||||
if(err == 1)
|
||||
fprintf(stderr, "Failed to open/create file `%s'.\n", file);
|
||||
else if(err == 2)
|
||||
fprintf(stderr, "Failed to close file `%s'.\n", file);
|
||||
else if(err == 3)
|
||||
fprintf(stderr, "Failed while writing a prime to `%s'.\n", file);
|
||||
exitCode = 1;
|
||||
goto releaseMemory;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Clear GMP variables
|
||||
mpz_clear(halfNum);
|
||||
mpz_clear(num);
|
||||
|
||||
releaseMemory:
|
||||
puts("Clearing memory...");
|
||||
// Deinitialize the list
|
||||
deInitList(&primes);
|
||||
return 0;
|
||||
|
||||
puts("Exit successful.");
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
void leave() {
|
||||
printf("Exiting...\n");
|
||||
run = false;
|
||||
void printUsage(char *progName) {
|
||||
printf("%s [OPTIONS]\n", progName);
|
||||
}
|
||||
|
||||
void leave() { run = false; }
|
||||
|
@ -1,3 +0,0 @@
|
||||
#pragma once
|
||||
#define likely(x) __builtin_expect(!!(x), 1)
|
||||
#define unlikely(x) __builtin_expect(!!(x), 0)
|
Reference in New Issue
Block a user