move openmp loop variable outside

This commit is contained in:
Krishna Vedala 2020-04-23 20:17:32 -04:00
parent bf1c367a62
commit dee56f7781
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7
7 changed files with 48 additions and 36 deletions

View File

@ -11,22 +11,23 @@
int get_number(FILE *fp, char *buffer, uint8_t *out_int)
{
long l = fscanf(fp, "%s\n", buffer);
if (!l)
if (!l)
{
perror("Error reading line.");
perror("Error reading line.");
return -1;
}
// printf("Number: %s\t length: %ld, %ld\n", buffer, strlen(buffer), l);
long L = strlen(buffer);
for (int i = 0 ; i < L; i++)
for (int i = 0; i < L; i++)
if (buffer[i] < 0x30 || buffer[i] > 0x39)
{
perror("found inavlid character in the number!");
return -1;
} else
out_int[L-i-1] = buffer[i] - 0x30;
}
else
out_int[L - i - 1] = buffer[i] - 0x30;
return 0;
}
@ -44,18 +45,19 @@ int add_numbers(uint8_t *a, uint8_t *b, uint8_t N)
{
// printf("\t%d + %d + %d ", a[i], b[i], carry);
c[i] = carry + a[i] + b[i];
if (c[i] > 9) /* check for carry */
if (c[i] > 9) /* check for carry */
{
carry = 1;
c[i] -= 10;
} else
}
else
carry = 0;
// printf("= %d, %d\n", carry, c[i]);
}
for (int i = N; i < N+10; i++)
for (int i = N; i < N + 10; i++)
{
if(carry == 0)
if (carry == 0)
break;
// printf("\t0 + %d + %d ", b[i], carry);
c[i] = carry + c[i];
@ -63,7 +65,8 @@ int add_numbers(uint8_t *a, uint8_t *b, uint8_t N)
{
carry = 1;
c[i] -= 10;
} else
}
else
carry = 0;
// printf("= %d, %d\n", carry, c[i]);
}
@ -77,7 +80,7 @@ int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print)
/* skip all initial zeros */
while (number[start_pos] == 0)
start_pos --;
start_pos--;
/* if end_pos < 0, print all digits */
if (num_digits_to_print < 0)
@ -89,37 +92,40 @@ int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print)
fprintf(stderr, "invalid number of digits argumet!\n");
return -1;
}
for (int i = start_pos; i >= end_pos ; i--)
for (int i = start_pos; i >= end_pos; i--)
putchar(number[i] + 0x30);
putchar('\n');
return 0;
}
#define N 10
#define N2 (N + 10)
int main(void)
{
const char N = 50, N2 = N+10; /* length of numbers */
char txt_buffer[N+5]; /* temporary buffer */
uint8_t number[N]; /* array to store digits of a large number */
uint8_t sum[N2]; /* array to store the sum of the large numbers. For
// const char N = 50, N2 = N+10; /* length of numbers */
char txt_buffer[N + 5]; /* temporary buffer */
uint8_t number[N]; /* array to store digits of a large number */
uint8_t sum[N2]; /* array to store the sum of the large numbers. For
safety, we make it twice the length of a number. */
memset(sum, 0, sizeof(sum)); /* initialize sum array with 0 */
memset(sum, 0, sizeof(sum)); /* initialize sum array with 0 */
FILE *fp = fopen("num.txt", "rt"); /* open text file to read */
if(!fp)
FILE *fp = fopen("num.txt", "rt"); /* open text file to read */
if (!fp)
{
perror("Unable to open file 'num.txt'.");
return -1;
}
int count = 0;
get_number(fp, txt_buffer, sum); /* 0 + = first_number = first_number */
do {
count ++;
get_number(fp, txt_buffer, sum); /* 0 + = first_number = first_number */
do
{
count++;
if (get_number(fp, txt_buffer, number) != 0)
break;
add_numbers(number, sum, N);
@ -131,6 +137,6 @@ int main(void)
printf("first 10 digits: \t");
print_number(sum, N2, 10);
fclose(fp); /* close file */
fclose(fp); /* close file */
return 0;
}

View File

@ -36,7 +36,7 @@ int main(int argc, char **argv)
printf("Maximum number: %lld\n", MAX_NUM);
}
/**
/**
* Since the computational values for each iteration step are independent,
* we can compute them in parallel. However, the maximum values should be
* updated in synchrony so that we do not get into a "race condition".
@ -46,10 +46,11 @@ int main(int argc, char **argv)
*
* Automatically detects for OPENMP using the _OPENMP macro.
**/
long long i;
#ifdef _OPENMP
#pragma omp parallel for shared(max_len, max_len_num) schedule(guided)
#endif
for (long long i = 1; i < MAX_NUM; i++)
for (i = 1; i < MAX_NUM; i++)
{
long long L = collatz(i);
if (L > max_len)

View File

@ -52,12 +52,12 @@ int main(int argc, char **argv)
char *flags = (char *)calloc(MAX_N, sizeof(char));
clock_t start_time = clock();
unsigned int i;
#ifdef _OPENMP
#pragma omp for schedule(runtime)
#endif
/* there are no such numbers till 10. Lets search from there on */
for (unsigned int i = 10; i < MAX_N; i++)
for (i = 10; i < MAX_N; i++)
{
if (flags[i] != 0)
/* already processed, skip */

View File

@ -110,14 +110,16 @@ int main(int argc, char **argv)
long sum_score = 0;
clock_t start_time = clock();
unsigned int i;
#ifdef _OPENMP
#pragma omp parallel for schedule(runtime) reduction(+ \
: sum_score)
#endif
#ifdef DEBUG
for (unsigned int i = 935; i < 940; i++)
for (i = 935; i < 940; i++)
#else
for (unsigned int i = 0; i < COUNT; i++)
for (i = 0; i < COUNT; i++)
#endif
{
unsigned int score = 0;

View File

@ -92,12 +92,13 @@ int main(int argc, char **argv)
#endif
double total_duration = 0;
unsigned long i;
function_timer *timer = new_timer();
#ifdef _OPENMP
#pragma omp parallel for reduction(+ \
: sum) schedule(runtime)
#endif
for (unsigned long i = 1; i <= MAX_N; i++)
for (i = 1; i <= MAX_N; i++)
{
start_timer(timer);
if (!is_sum_of_abundant(i))

View File

@ -119,11 +119,12 @@ int main(int argc, char **argv)
clock_t start_time = clock();
/* Loop to set abundant flags */
/* Loop to set abundant flags */
unsigned long N;
#ifdef _OPENMP
#pragma omp for schedule(runtime)
#endif
for (unsigned long N = 1; N <= MAX_N; N++)
for (N = 1; N <= MAX_N; N++)
{
char ret = get_perfect_number(N);
if (ret == 1)

View File

@ -19,10 +19,11 @@ int main(int argc, char *argv[])
unsigned short max_digits = 0, max_idx_number = 0;
clock_t start_time = clock();
unsigned short deno;
#ifdef _OPENMP
#pragma omp for
#endif
for (unsigned short deno = 2; deno < MAX_DENO; deno++)
for (deno = 2; deno < MAX_DENO; deno++)
{
unsigned short remainders[MAX_LEN];
unsigned short rem = 1, *rem_ptr = remainders;