Parallelized, but needs algorithm optimization.

This commit is contained in:
Nicolás A. Ortega 2017-02-01 19:36:05 +01:00
parent 32076a67cd
commit b9cafadf8e
No known key found for this signature in database
GPG Key ID: 614272579C2070D1
3 changed files with 46 additions and 16 deletions

View File

@ -21,7 +21,7 @@ void deInitList(List *restrict l) {
free(l->list);
}
int addToList(List *l, mpz_t n) {
int addToList(List *restrict l, mpz_t n) {
if(l->end == l->size) {
l->size += BLOCK_SIZE;
void *tmp = realloc(l->list, sizeof(mpz_t) * l->size);

View File

@ -40,4 +40,4 @@ void deInitList(List *restrict l);
* @param[in] n variable to be appended to the list.
* @returns Returns 0 if successful and 1 if failed.
*/
int addToList(List *l, mpz_t n);
int addToList(List *restrict l, mpz_t n);

View File

@ -93,6 +93,19 @@ int main(int argc, char *argv[]) {
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
List primes;
if(initList(&primes) == 1) {
@ -171,17 +184,33 @@ int main(int argc, char *argv[]) {
// Calculate the sqrt(num)
mpz_sqrt(numRoot, num);
bool isPrime = true;
/**
* 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
* 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(mpz_divisible_p(num, primes.list[i]) != 0)
goto nextNum;
if(mpz_divisible_p(num, primes.list[i]) != 0) {
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
}
}
if(isPrime) {
// `num' is a prime so we add it to the list and print it
if(addToList(&primes, num) == 1) {
fprintf(stderr, "Failed to allocate more memory for list.\n");
@ -193,10 +222,11 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Could not print to `stdout'!\n");
printf("\n");
}
}
nextNum:
mpz_add_ui(num, num, 2);
}
mpz_clear(numRoot);
printf("Found %zu primes.\n", primes.end);