Remove parallelization code to start anew.

This commit is contained in:
Nicolás A. Ortega 2017-02-01 17:18:03 +01:00
parent 9673a4da57
commit 32076a67cd
No known key found for this signature in database
GPG Key ID: 614272579C2070D1

View File

@ -122,7 +122,6 @@ int main(int argc, char *argv[]) {
if(!f_quiet) { if(!f_quiet) {
puts("2\n3"); puts("2\n3");
} }
mpz_add_ui(num, num, 2);
} else { } else {
// Load primes from file // Load primes from file
int err = inputPrimes(file, &primes); int err = inputPrimes(file, &primes);
@ -163,33 +162,14 @@ int main(int argc, char *argv[]) {
goto releaseMemory; goto releaseMemory;
} }
#pragma omp parallel default(shared)
{
// Variable for sqrt of `privNum' // Variable for sqrt of `privNum'
mpz_t numRoot; mpz_t numRoot;
mpz_init(numRoot); mpz_init(numRoot);
mpz_add_ui(num, num, 2);
mpz_t privNum; while(run) {
mpz_init(privNum);
mpz_add_ui(privNum, num, omp_get_thread_num() * 2);
#pragma omp barrier
do {
// Calculate the sqrt(num) // Calculate the sqrt(num)
mpz_sqrt(numRoot, privNum); mpz_sqrt(numRoot, num);
#pragma omp critical
{
mpz_out_str(stdout, base, numRoot);
printf(" - ");
mpz_out_str(stdout, base, privNum);
printf("\n");
}
// Make sure a number larger than numRoot exists
while(mpz_cmp(primes.list[primes.end], numRoot) < 0) {
if(!run)
goto leaveLoop;
}
/** /**
* 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
@ -198,33 +178,26 @@ int main(int argc, char *argv[]) {
*/ */
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(privNum, primes.list[i]) != 0) if(mpz_divisible_p(num, primes.list[i]) != 0)
goto nextNum; goto nextNum;
} }
// `num' is a prime so we add it to the list and print it // `num' is a prime so we add it to the list and print it
#pragma omp critical if(addToList(&primes, num) == 1) {
{
if(addToList(&primes, privNum) == 1) {
fprintf(stderr, "Failed to allocate more memory for list.\n"); fprintf(stderr, "Failed to allocate more memory for list.\n");
exitCode = 1; exitCode = 1;
run = false; run = false;
} }
if(!f_quiet) { if(!f_quiet) {
if(mpz_out_str(stdout, base, privNum) == 0) if(mpz_out_str(stdout, base, num) == 0)
fprintf(stderr, "Could not print to `stdout'!\n"); fprintf(stderr, "Could not print to `stdout'!\n");
printf("\n"); printf("\n");
} }
}
nextNum: nextNum:
mpz_add_ui(privNum, privNum, omp_get_num_threads() * 2); mpz_add_ui(num, num, 2);
} while(run);
leaveLoop:
mpz_clear(privNum);
mpz_clear(numRoot);
#pragma omp barrier
} }
mpz_clear(numRoot);
printf("Found %zu primes.\n", primes.end); printf("Found %zu primes.\n", primes.end);