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,68 +162,42 @@ int main(int argc, char *argv[]) {
goto releaseMemory; goto releaseMemory;
} }
#pragma omp parallel default(shared) // Variable for sqrt of `privNum'
{ mpz_t numRoot;
// Variable for sqrt of `privNum' mpz_init(numRoot);
mpz_t numRoot; mpz_add_ui(num, num, 2);
mpz_init(numRoot);
mpz_t privNum; while(run) {
mpz_init(privNum); // Calculate the sqrt(num)
mpz_add_ui(privNum, num, omp_get_thread_num() * 2); mpz_sqrt(numRoot, num);
#pragma omp barrier
do { /**
// Calculate the sqrt(num) * Loop through primes we've found until we get to the sqrt of the
mpz_sqrt(numRoot, privNum); * number we're analyzing. Also, skip `2' since we're not testing even
#pragma omp critical * numbers.
{ */
mpz_out_str(stdout, base, numRoot); for(size_t i = 1; mpz_cmp(primes.list[i], numRoot) <= 0; ++i) {
printf(" - "); // If `num' is divisible by a prime then go to the next number
mpz_out_str(stdout, base, privNum); if(mpz_divisible_p(num, primes.list[i]) != 0)
printf("\n"); goto nextNum;
} }
// Make sure a number larger than numRoot exists // `num' is a prime so we add it to the list and print it
while(mpz_cmp(primes.list[primes.end], numRoot) < 0) { if(addToList(&primes, num) == 1) {
if(!run) fprintf(stderr, "Failed to allocate more memory for list.\n");
goto leaveLoop; exitCode = 1;
} run = false;
}
/** if(!f_quiet) {
* Loop through primes we've found until we get to the sqrt of the if(mpz_out_str(stdout, base, num) == 0)
* number we're analyzing. Also, skip `2' since we're not testing even fprintf(stderr, "Could not print to `stdout'!\n");
* numbers. printf("\n");
*/ }
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(privNum, primes.list[i]) != 0)
goto nextNum;
}
// `num' is a prime so we add it to the list and print it
#pragma omp critical
{
if(addToList(&primes, privNum) == 1) {
fprintf(stderr, "Failed to allocate more memory for list.\n");
exitCode = 1;
run = false;
}
if(!f_quiet) {
if(mpz_out_str(stdout, base, privNum) == 0)
fprintf(stderr, "Could not print to `stdout'!\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);