-
Notifications
You must be signed in to change notification settings - Fork 0
/
primos_parallel.c
110 lines (90 loc) · 2.79 KB
/
primos_parallel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//////////////////// Constantes y variables globales //////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <omp.h>
static int gProgress = 0,
gPrimesFound = 0; // Acumulador de número de primos encontrados
long globalPrimes[1000000]; // Para almacenar los primos encontrados
int CLstart, CLend;
///////////////////////////////////////////////////////////////////////////////
//
// Imprime portcentaje de avance
//
///////////////////////////////////////////////////////////////////////////////
void ShowProgress( int val, int range )
{
int percentDone = 0;
gProgress++;
percentDone = (int)((float)gProgress/(float)range *200.0f + 0.5f);
if( percentDone % 10 == 0 )
printf("\b\b\b\b%3d%%", percentDone);
}
///////////////////////////////////////////////////////////////////////////////
//
// Busca factorres. Devuelve "0" si los encuentra
//
///////////////////////////////////////////////////////////////////////////////
int TestForPrime(int val)
{
int limit, factor = 3;
limit = (long)(sqrtf((float)val)+0.5f);
#pragma omp parallel shared(factor)
while((factor <= limit) && (val % factor))
#pragma omp critical
factor = factor + 1;
return (factor > limit);
}
void FindPrimes(int start, int end)
{
// start siempre es non
int i, range = end - start + 1;
#pragma omp parallel for private(i) shared(gPrimesFound)
for( i = start; i <= end; i += 2 )
{
if( TestForPrime(i) )
#pragma omp critical
globalPrimes[gPrimesFound++] = i;
ShowProgress(i, range);
}
}
int main(int argc, char **argv)
{
int start, end,i;
clock_t before, after;
int numThreads = 4;
// Set number of threads.
omp_set_num_threads(numThreads);
if( argc == 3 )
{
CLstart = atoi(argv[1]);
CLend = atoi(argv[2]);
}
else
{
printf("Uso:- %s <rango inicio> <rango fin>\n", argv[0]);
exit(-1);
}
if (CLstart > CLend) {
printf("Valor inicial debe ser menor o igual al valor final\n");
exit(-1);
}
if (CLstart < 1 && CLend < 2) {
printf("El rango de búsqueda debe estar formado por enteros positivos\n");
exit(-1);
}
start = CLstart;
end = CLend;
if (CLstart < 2)
start = 2;
if (start <= 2)
globalPrimes[gPrimesFound++] = 2;
if((start % 2) == 0 )
start = start + 1; // Asegurar que iniciamos con un número impar
before = clock();
FindPrimes(start, end);
after = clock();
printf("\n\nSe encontraron %8d primos entre %6d y %6d en %7.2f secs\n",
gPrimesFound, CLstart, CLend, (float)(after - before)/ CLOCKS_PER_SEC);
}