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

@ -25,7 +25,8 @@ int get_number(FILE *fp, char *buffer, uint8_t *out_int)
{ {
perror("found inavlid character in the number!"); perror("found inavlid character in the number!");
return -1; return -1;
} else }
else
out_int[L - i - 1] = buffer[i] - 0x30; out_int[L - i - 1] = buffer[i] - 0x30;
return 0; return 0;
@ -48,7 +49,8 @@ int add_numbers(uint8_t *a, uint8_t *b, uint8_t N)
{ {
carry = 1; carry = 1;
c[i] -= 10; c[i] -= 10;
} else }
else
carry = 0; carry = 0;
// printf("= %d, %d\n", carry, c[i]); // printf("= %d, %d\n", carry, c[i]);
} }
@ -63,7 +65,8 @@ int add_numbers(uint8_t *a, uint8_t *b, uint8_t N)
{ {
carry = 1; carry = 1;
c[i] -= 10; c[i] -= 10;
} else }
else
carry = 0; carry = 0;
// printf("= %d, %d\n", carry, c[i]); // printf("= %d, %d\n", carry, c[i]);
} }
@ -98,10 +101,12 @@ int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print)
return 0; return 0;
} }
#define N 10
#define N2 (N + 10)
int main(void) int main(void)
{ {
const char N = 50, N2 = N+10; /* length of numbers */ // const char N = 50, N2 = N+10; /* length of numbers */
char txt_buffer[N + 5]; /* temporary buffer */ char txt_buffer[N + 5]; /* temporary buffer */
uint8_t number[N]; /* array to store digits of a large number */ 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 uint8_t sum[N2]; /* array to store the sum of the large numbers. For
@ -118,7 +123,8 @@ int main(void)
int count = 0; int count = 0;
get_number(fp, txt_buffer, sum); /* 0 + = first_number = first_number */ get_number(fp, txt_buffer, sum); /* 0 + = first_number = first_number */
do { do
{
count++; count++;
if (get_number(fp, txt_buffer, number) != 0) if (get_number(fp, txt_buffer, number) != 0)
break; break;

View File

@ -46,10 +46,11 @@ int main(int argc, char **argv)
* *
* Automatically detects for OPENMP using the _OPENMP macro. * Automatically detects for OPENMP using the _OPENMP macro.
**/ **/
long long i;
#ifdef _OPENMP #ifdef _OPENMP
#pragma omp parallel for shared(max_len, max_len_num) schedule(guided) #pragma omp parallel for shared(max_len, max_len_num) schedule(guided)
#endif #endif
for (long long i = 1; i < MAX_NUM; i++) for (i = 1; i < MAX_NUM; i++)
{ {
long long L = collatz(i); long long L = collatz(i);
if (L > max_len) if (L > max_len)

View File

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

View File

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

View File

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

View File

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

View File

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