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