This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomp.c
292 lines (251 loc) · 6.45 KB
/
omp.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include <omp.h>
#include <unistd.h>
#include <time.h>
int N ; // nombres de tableaux
int K ; // tailles des tableaux
float** bin; // tableau qui stocke tout les bin
void verif_tri(float** bloc);
void fusion(float tableau[],int deb1,int fin1,int fin2);
void tri_fusion_bis(float tableau[],int deb,int fin);
void tri_fusion(float tableau[],int longueur);
void create_marks_csv(char *filename,int N,int K, float time,int nb_threads );
void init_bin(); // genere les tableaux
void generator(float bin[]);
void affiche(float bin[]);
void tri_merge(float bin1[],float bin2[]);
float maximum(float a,float b);
float minimum(float a , float b);
void tri_parallel (float** bloc,int nb_threads);
int main(int argc, char const *argv[])
{
int nb_threads;
double t1,t2;
double time_spent;
srand((unsigned int)time(NULL));
N=atoi(argv[1]);
K=atoi(argv[2]);
printf("Base de données d'entiers de %d x %d éléments\n",N,K );
printf("Entrez le nombre de threads qui vont exécuté le tri parallél \n");
scanf("\n%d",&nb_threads);
init_bin();
printf("Generation du tableau de bloc ");
for (int i = 0; i < N; i++)
generator(bin[i]);
printf(" FINI\n");
/*
for (int i = 0; i < N; i++){
printf("affichage bin [%d] ",i );
affiche(bin[i]);
}
*/
printf("TRI parallél");
t1=omp_get_wtime();
tri_parallel(bin,nb_threads);
t2=omp_get_wtime();
time_spent = (double)(t2-t1);
printf("FINI \n");
printf("Affichage aprés le tri parallél\n");
for (int i = 0; i < N; ++i){
printf("affichage bin [%d] ",i );
affiche(bin[i]);
}
printf("///////////////////////////////////////////////////////\n");
printf("Le temps d'execution du tri parallél est %f\n",time_spent );
printf("///////////////////////////////////////////////////////\n");
verif_tri(bin);
create_marks_csv("lesvaleurs.csv",N,K,time_spent,nb_threads);
return 0;
}
void init_bin(){
bin=(float**)malloc(N*sizeof(float*));
for (int i = 0; i < N; i++)
bin[i]=(float*)malloc(K*sizeof(float));
}
void generator(float tab[]){
float coef = 1000; //coeffecient pour agrandir les floats
for (int i = 0; i < K; ++i)
tab[i]=((float)rand()/(float)(RAND_MAX))*coef ;
}
void affiche(float tab[]){
printf("\n");
for (int i = 0; i < K; ++i)
printf("| %f | \n",tab[i] );
printf("\n");
}
void tri_merge(float *bin1, float *bin2)
{
float* Tab;
Tab=(float*)malloc(sizeof(float)*2*K);
int c_bin1=0;
int c_bin2=0;
for(int i=0;i<2*K;i++)
{
if(c_bin1>=K)
{
Tab[i]=bin2[c_bin2];
c_bin2++;
}
else if(c_bin2>=K)
{
Tab[i]=bin1[c_bin1];
c_bin1++;
}
else if((bin1[c_bin1]<bin2[c_bin2]))
{
Tab[i]=bin1[c_bin1];
c_bin1++;
}
else if((bin1[c_bin1]>=bin2[c_bin2]))
{
Tab[i]=bin2[c_bin2];
c_bin2++;
}
}
for(int i=0;i<K;i++)
{
bin1[i]=Tab[i];
}
for(int j=K;j<2*K;j++)
{
bin2[j-K]=Tab[j];
}
}
void fusion(float tableau[],int deb1,int fin1,int fin2)
{
float *table1;
int deb2=fin1+1;
int compt1=deb1;
int compt2=deb2;
int i;
table1=malloc((fin1-deb1+1)*sizeof(float));
//on recopie les éléments du début du tableau
for(i=deb1;i<=fin1;i++)
{
table1[i-deb1]=tableau[i];
}
for(i=deb1;i<=fin2;i++)
{
if (compt1==deb2) //c'est que tous les éléments du premier tableau ont été utilisés
{
break; //tous les éléments ont donc été classés
}
else if (compt2==(fin2+1)) //c'est que tous les éléments du second tableau ont été utilisés
{
tableau[i]=table1[compt1-deb1]; //on ajoute les éléments restants du premier tableau
compt1++;
}
else if (table1[compt1-deb1]<tableau[compt2])
{
tableau[i]=table1[compt1-deb1]; //on ajoute un élément du premier tableau
compt1++;
}
else
{
tableau[i]=tableau[compt2]; //on ajoute un élément du second tableau
compt2++;
}
}
free(table1);
}
void tri_fusion_bis(float tableau[],int deb,int fin)
{
if (deb!=fin)
{
int milieu=(fin+deb)/2;
tri_fusion_bis(tableau,deb,milieu);
tri_fusion_bis(tableau,milieu+1,fin);
fusion(tableau,deb,milieu,fin);
}
}
void tri_fusion(float tableau[],int longueur)
{
if (longueur>0)
{
tri_fusion_bis(tableau,0,longueur-1);
}
}
float minimum(float a , float b)
{
if (a>b)
return b ;
else
return a ;
}
float maximum(float a , float b)
{
if (a>b)
return a ;
else
return b ;
}
void tri_parallel(float** bloc,int nb_threads)
{
omp_set_num_threads(nb_threads);
int i,j,b1,b2,k ;
int min , max ;
#pragma omp parallel for schedule (dynamic)
for (i=0;i<N;i++)
tri_fusion(bloc[i],K);
for (j=0;j<=N;j++)
{
k=1+(j%2) ; // Traitement des blocs deux à deux
#pragma omp parallel for private(b1,b2,min,max) schedule (static)
for (i=0;i<=N/2+1;i++)
{
b1=(1+(k+(2*i)))%N;
b2=(1+(k+(2*i)+1))%N;
min=minimum(b1,b2);
max=maximum(b1,b2) ;
#pragma omp critical
{
tri_merge(bloc[min],bloc[max]);
}
}
}
}
void verif_tri(float **bloc)
{
float * tab = (float*)malloc(K*N*sizeof(float));
int b=1 ;//initialement vrai
int i,j,k;
k=0;
i=0 ;
j=0 ;
for (i=0;i<N;i++)
for(j=0;j<K;j++)
{
tab[k]=bloc[i][j];
k++;
}
printf("k= %d \n",k );
i=0;
while ((b==1)&&(i<k-1))
{
if (tab[i]>tab[i+1])
{
b=0;
printf("Probléme au niveau de la ligne %d",i);
}
else
{
i++;
}
}
if (b==1)
printf("Tri parallél fonctionnel à 100/100 \n ");
}
void create_marks_csv(char *filename,int N,int K, float time ,int nb_threads){
FILE *fp;
if( access( filename, F_OK ) != -1 ) {
fp=fopen(filename,"a+");
fprintf(fp, "\n%d,%d,%f,%d,%f", N, K, time, nb_threads , time/omp_get_num_procs());
} else {
fp=fopen(filename,"a+");
fprintf(fp,"N, K, time, nb_threads , charge du programme");
fprintf(fp, "\n%d,%d,%f,%d,%f", N, K, time, nb_threads,time/omp_get_num_procs());
}
fclose(fp);
}