mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
move openmp loop variable outside
This commit is contained in:
parent
bf1c367a62
commit
dee56f7781
@ -20,13 +20,14 @@ int get_number(FILE *fp, char *buffer, uint8_t *out_int)
|
|||||||
|
|
||||||
long L = strlen(buffer);
|
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)
|
if (buffer[i] < 0x30 || buffer[i] > 0x39)
|
||||||
{
|
{
|
||||||
perror("found inavlid character in the number!");
|
perror("found inavlid character in the number!");
|
||||||
return -1;
|
return -1;
|
||||||
} else
|
}
|
||||||
out_int[L-i-1] = buffer[i] - 0x30;
|
else
|
||||||
|
out_int[L - i - 1] = buffer[i] - 0x30;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -48,14 +49,15 @@ 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]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = N; i < N+10; i++)
|
for (int i = N; i < N + 10; i++)
|
||||||
{
|
{
|
||||||
if(carry == 0)
|
if (carry == 0)
|
||||||
break;
|
break;
|
||||||
// printf("\t0 + %d + %d ", b[i], carry);
|
// printf("\t0 + %d + %d ", b[i], carry);
|
||||||
c[i] = carry + c[i];
|
c[i] = 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]);
|
||||||
}
|
}
|
||||||
@ -77,7 +80,7 @@ int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print)
|
|||||||
|
|
||||||
/* skip all initial zeros */
|
/* skip all initial zeros */
|
||||||
while (number[start_pos] == 0)
|
while (number[start_pos] == 0)
|
||||||
start_pos --;
|
start_pos--;
|
||||||
|
|
||||||
/* if end_pos < 0, print all digits */
|
/* if end_pos < 0, print all digits */
|
||||||
if (num_digits_to_print < 0)
|
if (num_digits_to_print < 0)
|
||||||
@ -90,7 +93,7 @@ int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print)
|
|||||||
return -1;
|
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(number[i] + 0x30);
|
||||||
|
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
@ -98,11 +101,13 @@ 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
|
||||||
safety, we make it twice the length of a number. */
|
safety, we make it twice the length of a number. */
|
||||||
@ -110,7 +115,7 @@ int main(void)
|
|||||||
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 */
|
FILE *fp = fopen("num.txt", "rt"); /* open text file to read */
|
||||||
if(!fp)
|
if (!fp)
|
||||||
{
|
{
|
||||||
perror("Unable to open file 'num.txt'.");
|
perror("Unable to open file 'num.txt'.");
|
||||||
return -1;
|
return -1;
|
||||||
@ -118,8 +123,9 @@ 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;
|
||||||
add_numbers(number, sum, N);
|
add_numbers(number, sum, N);
|
||||||
|
@ -36,7 +36,7 @@ int main(int argc, char **argv)
|
|||||||
printf("Maximum number: %lld\n", MAX_NUM);
|
printf("Maximum number: %lld\n", MAX_NUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Since the computational values for each iteration step are independent,
|
* Since the computational values for each iteration step are independent,
|
||||||
* we can compute them in parallel. However, the maximum values should be
|
* 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".
|
* 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.
|
* 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)
|
||||||
|
@ -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 */
|
||||||
|
@ -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;
|
||||||
|
@ -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))
|
||||||
|
@ -119,11 +119,12 @@ 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)
|
||||||
|
@ -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;
|
||||||
|
Loading…
Reference in New Issue
Block a user