-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal.c
More file actions
205 lines (172 loc) · 4.73 KB
/
final.c
File metadata and controls
205 lines (172 loc) · 4.73 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Manuel Castro
* Jose Martinez
*/
#include <stdio.h>
#include <omp.h>
#include <stdlib.h>
#include <math.h>
#define DIM 20
#define T 4
float *** crearClusters(int a, int b);
void rellenarVectores (int a, int b, float ***clusters);
float * crearRadios(int a);
float ** crearconsultas(int a);
float distanciaEuclidiana(float ***clusters, float **consultas, int numeroCluster, int vectorConsulta, int elementoCluster);
int ejecutar(int i, int n_clusters, int n_vectores, float ***clusters, float **consultas, float *radios ,int e[]);
int main(){
omp_set_num_threads(T);
int i, j ,k;
int n_clusters,n_vectores, n_consultas;
scanf("%i",&n_clusters);
scanf("%i",&n_vectores);
//matriz clusters[i][0][j] centros de clusters
float ***clusters = crearClusters(n_clusters, n_vectores);
float *radios = crearRadios(n_clusters);
rellenarVectores(n_clusters, n_vectores, clusters);
scanf("%i", &n_consultas);
//matriz consultas vector [i][j][0] = radio
float **consultas = crearconsultas(n_consultas);
int elementosDescartados = 0;
int elementosResultados = 0;
int *e_resultados = (int *)malloc(n_consultas*sizeof(int));
#pragma omp parallel shared (n_consultas, n_clusters, n_vectores, clusters, consultas, radios, e_resultados)
{
#pragma omp for schedule (static, 1)
for (i = 0 ; i<n_consultas; i++)
{
//printf("Hilo %d - Ejecutando for: %d\n", omp_get_thread_num(), i);
elementosDescartados = elementosDescartados + ejecutar(i, n_clusters, n_vectores, clusters, consultas, radios, e_resultados);
}
#pragma omp master
{
for (i =0; i< n_consultas; i++)
{
printf("resultado de consulta %i : %i \n", i, e_resultados[i]);
}
printf("Elementos descartados: %i \n", elementosDescartados);
}
}
}
float *** crearClusters(int a, int b){
float lectura =0;
int i, j ,k;
int n_clusters = a;
int n_vectores = b+1;
float ***clusters;
clusters = (float***)malloc(n_clusters*sizeof(float**));
//asignacion de memoria clusters
for (i = 0; i < n_clusters; i++)
{
clusters[i] = (float**)malloc(n_vectores*sizeof(float*));
for (j = 0; j < n_vectores; j++)
{
clusters[i][j] = (float*)malloc(DIM*sizeof(float));
}
}
// relleno centro de clusters
for (i = 0; i < n_clusters; i++)
{
for (j = 0; j < DIM; j++)
{
scanf("%f",&lectura);
clusters[i][0][j] = lectura;
//printf("%f ",clusters[i][0][j]);
}
}
return clusters;
}
void rellenarVectores (int a, int b, float ***clusters){
float lectura =0;
int i, j ,k;
int n_clusters = a;
int n_vectores = b+1;
//rellenado de vectores de los clusters
for (i = 0; i < n_clusters; i++)
{
for (j = 1; j < n_vectores; j++)
{
for (k = 0;k < DIM;k++)
{
scanf("%f",&lectura);
clusters[i][j][k] = lectura;
}
}
}
}
float * crearRadios(int a){
float lectura =0;
int i, j ,k;
int n_clusters = a;
float *radios;
radios = (float *)malloc(n_clusters*sizeof(float));
//rellenado de los Radios
for (i = 0; i < n_clusters; i++)
{
scanf("%f",&lectura);
radios[i] = lectura;
}
return radios;
}
float ** crearconsultas(int a){
float lectura =0;
int i, j ,k;
int n_consultas = a;
float **consultas;
consultas = (float **)malloc(sizeof(float *)*n_consultas);
//se reserva memoria para vectores consultas
for (i=0; i < n_consultas; i++){
consultas[i] = (float *)malloc(sizeof(float)*DIM+1);
}
for (i =0; i<n_consultas; i++){
for (j=0; j<DIM+1; j++){
scanf("%f", &lectura);
consultas[i][j] = lectura;
}
}
return consultas;
}
float distanciaEuclidiana(float ***clusters, float **consultas, int numeroCluster, int vectorConsulta, int elementoCluster)
{
int i,j,k;
float calculo =0;
float resultado = 0;
for (i=0; i < DIM; i++)
{
calculo = pow ((clusters[numeroCluster][elementoCluster][i]) - (consultas[vectorConsulta][i+1]), 2);
resultado = resultado + calculo;
}
//printf("distancia calculo es: %f \n", sqrt(resultado));
return sqrt(resultado);
}
int ejecutar(int i, int n_clusters, int n_vectores, float ***clusters, float **consultas, float *radios ,int e[])
{
int j, k =0;
int elementosResultados = 0;
int elementosDescartados = 0;
for (j =0; j<n_clusters; j++)
{
if (distanciaEuclidiana(clusters, consultas, j, i, 0) - consultas[i][0] > radios[j])
{
elementosDescartados = elementosDescartados + n_vectores;
continue;
}
else
{
//Cluster no descartado, se calcula distanca de elementos a la consulta
for (k = 1; k<n_vectores+1; k++)
{
if (distanciaEuclidiana(clusters, consultas, j, i, k) <= consultas[i][0] )
{
elementosResultados = elementosResultados +1;
}
}
}
if (distanciaEuclidiana(clusters, consultas, j, i, 0) + consultas[i][0] < radios[j])
{
break;
}
}
e[i] = elementosResultados;
return elementosDescartados;
}