Parallelized, but needs algorithm optimization.
This commit is contained in:
parent
32076a67cd
commit
b9cafadf8e
@ -21,7 +21,7 @@ void deInitList(List *restrict l) {
|
|||||||
free(l->list);
|
free(l->list);
|
||||||
}
|
}
|
||||||
|
|
||||||
int addToList(List *l, mpz_t n) {
|
int addToList(List *restrict l, mpz_t n) {
|
||||||
if(l->end == l->size) {
|
if(l->end == l->size) {
|
||||||
l->size += BLOCK_SIZE;
|
l->size += BLOCK_SIZE;
|
||||||
void *tmp = realloc(l->list, sizeof(mpz_t) * l->size);
|
void *tmp = realloc(l->list, sizeof(mpz_t) * l->size);
|
||||||
|
@ -40,4 +40,4 @@ void deInitList(List *restrict l);
|
|||||||
* @param[in] n variable to be appended to the list.
|
* @param[in] n variable to be appended to the list.
|
||||||
* @returns Returns 0 if successful and 1 if failed.
|
* @returns Returns 0 if successful and 1 if failed.
|
||||||
*/
|
*/
|
||||||
int addToList(List *l, mpz_t n);
|
int addToList(List *restrict l, mpz_t n);
|
||||||
|
58
src/main.c
58
src/main.c
@ -93,6 +93,19 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
int exitCode = 0;
|
int exitCode = 0;
|
||||||
|
|
||||||
|
if(!omp_get_cancellation()) {
|
||||||
|
puts("Warning: the OpenMP cancellation (`OMP_CANCELLATION') environment variable is not enabled.");
|
||||||
|
char cont = '\0';
|
||||||
|
while(true) {
|
||||||
|
printf("Continue? (y/n) ");
|
||||||
|
scanf("%c", &cont);
|
||||||
|
if(cont == 'y' || cont == 'Y')
|
||||||
|
break;
|
||||||
|
else if(cont == 'n' || cont == 'N')
|
||||||
|
return exitCode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Primes we've found
|
// Primes we've found
|
||||||
List primes;
|
List primes;
|
||||||
if(initList(&primes) == 1) {
|
if(initList(&primes) == 1) {
|
||||||
@ -171,32 +184,49 @@ int main(int argc, char *argv[]) {
|
|||||||
// Calculate the sqrt(num)
|
// Calculate the sqrt(num)
|
||||||
mpz_sqrt(numRoot, num);
|
mpz_sqrt(numRoot, num);
|
||||||
|
|
||||||
|
bool isPrime = true;
|
||||||
/**
|
/**
|
||||||
* Loop through primes we've found until we get to the sqrt of the
|
* Loop through primes we've found until we get to the sqrt of the
|
||||||
* number we're analyzing. Also, skip `2' since we're not testing even
|
* number we're analyzing. Also, skip `2' since we're not testing even
|
||||||
* numbers.
|
* numbers.
|
||||||
*/
|
*/
|
||||||
for(size_t i = 1; mpz_cmp(primes.list[i], numRoot) <= 0; ++i) {
|
/*for(size_t i = 1; mpz_cmp(primes.list[i], numRoot) <= 0; ++i) {
|
||||||
// If `num' is divisible by a prime then go to the next number
|
// If `num' is divisible by a prime then go to the next number
|
||||||
if(mpz_divisible_p(num, primes.list[i]) != 0)
|
if(mpz_divisible_p(num, primes.list[i]) != 0) {
|
||||||
goto nextNum;
|
isPrime = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
#pragma omp parallel
|
||||||
|
{
|
||||||
|
#pragma omp for
|
||||||
|
for(size_t i = 1; i < primes.end; ++i) {
|
||||||
|
if(mpz_divisible_p(num, primes.list[i])) {
|
||||||
|
#pragma omp atomic write
|
||||||
|
isPrime = false;
|
||||||
|
#pragma omp cancel for
|
||||||
|
}
|
||||||
|
#pragma omp cancellation point for
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// `num' is a prime so we add it to the list and print it
|
if(isPrime) {
|
||||||
if(addToList(&primes, num) == 1) {
|
// `num' is a prime so we add it to the list and print it
|
||||||
fprintf(stderr, "Failed to allocate more memory for list.\n");
|
if(addToList(&primes, num) == 1) {
|
||||||
exitCode = 1;
|
fprintf(stderr, "Failed to allocate more memory for list.\n");
|
||||||
run = false;
|
exitCode = 1;
|
||||||
}
|
run = false;
|
||||||
if(!f_quiet) {
|
}
|
||||||
if(mpz_out_str(stdout, base, num) == 0)
|
if(!f_quiet) {
|
||||||
fprintf(stderr, "Could not print to `stdout'!\n");
|
if(mpz_out_str(stdout, base, num) == 0)
|
||||||
printf("\n");
|
fprintf(stderr, "Could not print to `stdout'!\n");
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
nextNum:
|
|
||||||
mpz_add_ui(num, num, 2);
|
mpz_add_ui(num, num, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
mpz_clear(numRoot);
|
mpz_clear(numRoot);
|
||||||
|
|
||||||
printf("Found %zu primes.\n", primes.end);
|
printf("Found %zu primes.\n", primes.end);
|
||||||
|
Loading…
Reference in New Issue
Block a user