Pushing everything again, accidentally deleted my remote repository
This commit is contained in:
commit
1bee3e8e5b
101 changed files with 9428 additions and 0 deletions
31
ex5/ex5_2/Makefile
Normal file
31
ex5/ex5_2/Makefile
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#
|
||||
# use GNU-Compiler tools
|
||||
COMPILER=GCC_
|
||||
# alternatively from the shell
|
||||
# export COMPILER=GCC_
|
||||
# or, alternatively from the shell
|
||||
# make COMPILER=GCC_
|
||||
|
||||
# use Intel compilers
|
||||
#COMPILER=ICC_
|
||||
|
||||
# use PGI compilers
|
||||
# COMPILER=PGI_
|
||||
|
||||
|
||||
SOURCES = main.cpp mylib.cpp
|
||||
OBJECTS = $(SOURCES:.cpp=.o)
|
||||
|
||||
PROGRAM = main.${COMPILER}
|
||||
|
||||
# uncomment the next to lines for debugging and detailed performance analysis
|
||||
CXXFLAGS += -g
|
||||
LINKFLAGS += -g
|
||||
# do not use -pg with PGI compilers
|
||||
|
||||
ifndef COMPILER
|
||||
COMPILER=GCC_
|
||||
endif
|
||||
|
||||
|
||||
include ../${COMPILER}default.mk
|
||||
501
ex5/ex5_2/data_1.txt
Normal file
501
ex5/ex5_2/data_1.txt
Normal file
|
|
@ -0,0 +1,501 @@
|
|||
141
|
||||
261
|
||||
87
|
||||
430
|
||||
258
|
||||
298
|
||||
425
|
||||
120
|
||||
496
|
||||
707
|
||||
244
|
||||
786
|
||||
75
|
||||
394
|
||||
4
|
||||
221
|
||||
2
|
||||
190
|
||||
143
|
||||
269
|
||||
175
|
||||
139
|
||||
599
|
||||
902
|
||||
940
|
||||
222
|
||||
483
|
||||
377
|
||||
524
|
||||
265
|
||||
69
|
||||
437
|
||||
174
|
||||
27
|
||||
955
|
||||
431
|
||||
962
|
||||
763
|
||||
8
|
||||
681
|
||||
706
|
||||
646
|
||||
553
|
||||
219
|
||||
773
|
||||
229
|
||||
371
|
||||
891
|
||||
857
|
||||
403
|
||||
319
|
||||
609
|
||||
911
|
||||
910
|
||||
592
|
||||
333
|
||||
854
|
||||
443
|
||||
905
|
||||
34
|
||||
533
|
||||
717
|
||||
180
|
||||
337
|
||||
188
|
||||
322
|
||||
404
|
||||
549
|
||||
49
|
||||
553
|
||||
275
|
||||
242
|
||||
244
|
||||
155
|
||||
957
|
||||
936
|
||||
819
|
||||
729
|
||||
176
|
||||
361
|
||||
189
|
||||
2
|
||||
317
|
||||
700
|
||||
626
|
||||
544
|
||||
440
|
||||
288
|
||||
502
|
||||
762
|
||||
763
|
||||
577
|
||||
748
|
||||
646
|
||||
124
|
||||
505
|
||||
348
|
||||
93
|
||||
148
|
||||
199
|
||||
673
|
||||
432
|
||||
695
|
||||
257
|
||||
10
|
||||
533
|
||||
280
|
||||
947
|
||||
907
|
||||
393
|
||||
25
|
||||
672
|
||||
838
|
||||
972
|
||||
57
|
||||
451
|
||||
583
|
||||
687
|
||||
720
|
||||
651
|
||||
727
|
||||
374
|
||||
582
|
||||
117
|
||||
58
|
||||
980
|
||||
285
|
||||
595
|
||||
963
|
||||
186
|
||||
194
|
||||
342
|
||||
933
|
||||
391
|
||||
274
|
||||
152
|
||||
398
|
||||
375
|
||||
132
|
||||
436
|
||||
92
|
||||
615
|
||||
11
|
||||
574
|
||||
790
|
||||
236
|
||||
449
|
||||
570
|
||||
62
|
||||
497
|
||||
643
|
||||
222
|
||||
838
|
||||
972
|
||||
847
|
||||
506
|
||||
279
|
||||
747
|
||||
237
|
||||
958
|
||||
621
|
||||
601
|
||||
173
|
||||
91
|
||||
256
|
||||
859
|
||||
912
|
||||
700
|
||||
726
|
||||
230
|
||||
577
|
||||
811
|
||||
404
|
||||
989
|
||||
90
|
||||
321
|
||||
512
|
||||
61
|
||||
726
|
||||
557
|
||||
530
|
||||
830
|
||||
859
|
||||
790
|
||||
318
|
||||
453
|
||||
753
|
||||
110
|
||||
110
|
||||
270
|
||||
525
|
||||
973
|
||||
711
|
||||
312
|
||||
292
|
||||
851
|
||||
912
|
||||
640
|
||||
256
|
||||
89
|
||||
839
|
||||
585
|
||||
949
|
||||
62
|
||||
585
|
||||
286
|
||||
828
|
||||
191
|
||||
443
|
||||
394
|
||||
827
|
||||
677
|
||||
208
|
||||
319
|
||||
134
|
||||
672
|
||||
571
|
||||
170
|
||||
148
|
||||
477
|
||||
909
|
||||
553
|
||||
33
|
||||
54
|
||||
806
|
||||
452
|
||||
383
|
||||
790
|
||||
365
|
||||
533
|
||||
712
|
||||
872
|
||||
329
|
||||
651
|
||||
975
|
||||
76
|
||||
588
|
||||
414
|
||||
310
|
||||
264
|
||||
759
|
||||
996
|
||||
187
|
||||
782
|
||||
196
|
||||
993
|
||||
803
|
||||
425
|
||||
729
|
||||
499
|
||||
809
|
||||
357
|
||||
74
|
||||
591
|
||||
911
|
||||
194
|
||||
433
|
||||
750
|
||||
40
|
||||
947
|
||||
764
|
||||
559
|
||||
184
|
||||
498
|
||||
518
|
||||
995
|
||||
855
|
||||
963
|
||||
679
|
||||
404
|
||||
935
|
||||
480
|
||||
232
|
||||
397
|
||||
706
|
||||
559
|
||||
757
|
||||
996
|
||||
963
|
||||
536
|
||||
964
|
||||
116
|
||||
52
|
||||
305
|
||||
581
|
||||
531
|
||||
902
|
||||
541
|
||||
432
|
||||
543
|
||||
713
|
||||
17
|
||||
801
|
||||
143
|
||||
479
|
||||
257
|
||||
370
|
||||
662
|
||||
170
|
||||
279
|
||||
199
|
||||
196
|
||||
327
|
||||
881
|
||||
472
|
||||
404
|
||||
180
|
||||
969
|
||||
408
|
||||
845
|
||||
616
|
||||
377
|
||||
878
|
||||
785
|
||||
465
|
||||
814
|
||||
899
|
||||
430
|
||||
335
|
||||
597
|
||||
902
|
||||
703
|
||||
378
|
||||
735
|
||||
955
|
||||
543
|
||||
541
|
||||
312
|
||||
72
|
||||
182
|
||||
93
|
||||
464
|
||||
10
|
||||
916
|
||||
643
|
||||
2
|
||||
31
|
||||
209
|
||||
455
|
||||
128
|
||||
9
|
||||
728
|
||||
355
|
||||
781
|
||||
437
|
||||
437
|
||||
50
|
||||
50
|
||||
92
|
||||
595
|
||||
242
|
||||
842
|
||||
858
|
||||
964
|
||||
489
|
||||
221
|
||||
227
|
||||
537
|
||||
763
|
||||
348
|
||||
462
|
||||
640
|
||||
918
|
||||
162
|
||||
716
|
||||
578
|
||||
434
|
||||
885
|
||||
394
|
||||
179
|
||||
634
|
||||
625
|
||||
328
|
||||
803
|
||||
1000
|
||||
981
|
||||
128
|
||||
233
|
||||
24
|
||||
608
|
||||
111
|
||||
408
|
||||
885
|
||||
549
|
||||
370
|
||||
209
|
||||
441
|
||||
957
|
||||
125
|
||||
471
|
||||
857
|
||||
44
|
||||
692
|
||||
979
|
||||
284
|
||||
134
|
||||
686
|
||||
910
|
||||
611
|
||||
900
|
||||
194
|
||||
755
|
||||
347
|
||||
419
|
||||
156
|
||||
820
|
||||
625
|
||||
739
|
||||
806
|
||||
68
|
||||
951
|
||||
498
|
||||
756
|
||||
743
|
||||
832
|
||||
157
|
||||
458
|
||||
619
|
||||
933
|
||||
836
|
||||
896
|
||||
583
|
||||
583
|
||||
855
|
||||
35
|
||||
886
|
||||
408
|
||||
37
|
||||
747
|
||||
155
|
||||
144
|
||||
606
|
||||
255
|
||||
325
|
||||
402
|
||||
407
|
||||
387
|
||||
610
|
||||
167
|
||||
189
|
||||
95
|
||||
324
|
||||
770
|
||||
235
|
||||
741
|
||||
693
|
||||
825
|
||||
828
|
||||
294
|
||||
310
|
||||
524
|
||||
326
|
||||
832
|
||||
811
|
||||
557
|
||||
263
|
||||
681
|
||||
234
|
||||
457
|
||||
385
|
||||
539
|
||||
992
|
||||
756
|
||||
981
|
||||
235
|
||||
529
|
||||
52
|
||||
757
|
||||
602
|
||||
858
|
||||
989
|
||||
930
|
||||
410
|
||||
1
|
||||
541
|
||||
208
|
||||
220
|
||||
326
|
||||
96
|
||||
748
|
||||
749
|
||||
544
|
||||
339
|
||||
833
|
||||
553
|
||||
958
|
||||
893
|
||||
357
|
||||
547
|
||||
347
|
||||
623
|
||||
797
|
||||
746
|
||||
126
|
||||
823
|
||||
26
|
||||
415
|
||||
732
|
||||
782
|
||||
368
|
||||
|
||||
130
ex5/ex5_2/main.cpp
Normal file
130
ex5/ex5_2/main.cpp
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
#include "mylib.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <omp.h>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
// read vector from file
|
||||
vector<size_t> data_vector = {};
|
||||
|
||||
ifstream input_stream("data_1.txt");
|
||||
|
||||
size_t line;
|
||||
while(input_stream >> line)
|
||||
{
|
||||
data_vector.push_back(line);
|
||||
}
|
||||
data_vector.shrink_to_fit();
|
||||
|
||||
|
||||
|
||||
|
||||
// specify loops
|
||||
size_t NLOOPS = 10000;
|
||||
|
||||
|
||||
|
||||
// ############# Parallelization with openMP #############
|
||||
// calculate arithmetic mean, geometric mean and harmonic mean
|
||||
double am_omp, gm_omp, hm_omp;
|
||||
|
||||
double tstart = omp_get_wtime();
|
||||
|
||||
for (size_t i = 0; i < NLOOPS; ++i)
|
||||
means_omp(data_vector, am_omp, gm_omp, hm_omp);
|
||||
|
||||
double t_means_omp = (omp_get_wtime() - tstart)/NLOOPS;
|
||||
|
||||
// calculate minimum and maximum
|
||||
size_t min, max;
|
||||
|
||||
tstart = omp_get_wtime();
|
||||
|
||||
for (size_t i = 0; i < NLOOPS; ++i)
|
||||
minmax_omp(data_vector, min, max);
|
||||
|
||||
double t_minmax_omp = (omp_get_wtime() - tstart)/NLOOPS;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ############# Parallelization with C++ algorithms #############
|
||||
// calculate arithmetic mean, geometric mean and harmonic mean
|
||||
double am_cpp, gm_cpp, hm_cpp;
|
||||
|
||||
tstart = omp_get_wtime();
|
||||
|
||||
for (size_t i = 0; i < NLOOPS; ++i)
|
||||
means_cpp(data_vector, am_cpp, gm_cpp, hm_cpp);
|
||||
|
||||
double t_means_cpp = (omp_get_wtime() - tstart)/NLOOPS;
|
||||
|
||||
// calculate minimum and maximum
|
||||
size_t min_cpp, max_cpp;
|
||||
|
||||
tstart = omp_get_wtime();
|
||||
|
||||
for (size_t i = 0; i < NLOOPS; ++i)
|
||||
minmax_cpp(data_vector, min_cpp, max_cpp);
|
||||
|
||||
double t_minmax_cpp = (omp_get_wtime() - tstart)/NLOOPS;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// print results
|
||||
cout << "####### OpenMP #######" << endl;
|
||||
cout << "minimum: " << min << endl;
|
||||
cout << "maximum: " << max << endl;
|
||||
cout << "duration: " << t_minmax_omp << endl << endl;
|
||||
|
||||
cout << "arithmetic mean: " << am_omp << endl;
|
||||
cout << "geometric mean: " << gm_omp << endl;
|
||||
cout << "harmonic mean: " << hm_omp << endl;
|
||||
cout << "duration: " << t_means_omp << endl << endl;
|
||||
|
||||
|
||||
cout << "####### C++ #######" << endl;
|
||||
cout << "minimum: " << min_cpp << endl;
|
||||
cout << "maximum: " << max_cpp << endl;
|
||||
cout << "duration: " << t_minmax_cpp << endl << endl;
|
||||
|
||||
cout << "arithmetic mean: " << am_cpp << endl;
|
||||
cout << "geometric mean: " << gm_cpp << endl;
|
||||
cout << "harmonic mean: " << hm_cpp << endl;
|
||||
cout << "duration: " << t_means_cpp << endl << endl;
|
||||
|
||||
|
||||
|
||||
// ####### OpenMP #######
|
||||
// minimum: 1
|
||||
// maximum: 1000
|
||||
// duration: 3.52086e-06
|
||||
|
||||
// arithmetic mean: 498.184
|
||||
// geometric mean: 364.412
|
||||
// harmonic mean: 95.6857
|
||||
// duration: 5.90171e-06
|
||||
|
||||
// ####### C++ #######
|
||||
// minimum: 1
|
||||
// maximum: 1000
|
||||
// duration: 1.76816e-05
|
||||
|
||||
// arithmetic mean: 498.184
|
||||
// geometric mean: 364.412
|
||||
// harmonic mean: 95.6857
|
||||
// duration: 2.35728e-05
|
||||
|
||||
// --> the openMP variant is faster in both cases
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
103
ex5/ex5_2/mylib.cpp
Normal file
103
ex5/ex5_2/mylib.cpp
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
#include "mylib.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <execution>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
#include <omp.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
void means_omp(const std::vector<size_t> numbers, double &am, double &gm, double &hm)
|
||||
{
|
||||
size_t const n = numbers.size();
|
||||
|
||||
am = 0.;
|
||||
gm = 0.;
|
||||
hm = 0.;
|
||||
|
||||
#pragma omp parallel for shared(numbers, n, cout) reduction(+:am, gm, hm)
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
{
|
||||
am += numbers[i];
|
||||
gm += log(numbers[i]);
|
||||
hm += 1.0/numbers[i];
|
||||
|
||||
// #pragma omp critical
|
||||
// {
|
||||
// cout << "Thread number " << omp_get_thread_num() << " processes value " << numbers[i] << endl;
|
||||
// }
|
||||
}
|
||||
|
||||
am /= n;
|
||||
gm = exp(gm/n);
|
||||
hm = n/hm;
|
||||
}
|
||||
|
||||
|
||||
void minmax_omp(const std::vector<size_t> numbers, size_t &global_min, size_t &global_max)
|
||||
{
|
||||
size_t const n = numbers.size();
|
||||
|
||||
global_min = -1; // gives the maximum size_t value
|
||||
global_max = 0;
|
||||
|
||||
#pragma omp parallel shared(numbers, n, global_min, global_max)
|
||||
{
|
||||
const size_t nthreads = omp_get_num_threads();
|
||||
const size_t threadnum = omp_get_thread_num();
|
||||
const size_t chunksize = n/nthreads;
|
||||
|
||||
|
||||
size_t start = threadnum*chunksize;
|
||||
size_t end = start + chunksize;
|
||||
if (threadnum == nthreads - 1)
|
||||
end = n;
|
||||
|
||||
|
||||
size_t local_min = -1;
|
||||
size_t local_max = 0;
|
||||
for (size_t i = start; i < end ; ++i)
|
||||
{
|
||||
if (numbers[i] < local_min)
|
||||
local_min = numbers[i];
|
||||
|
||||
if (numbers[i] > local_max)
|
||||
local_max = numbers[i];
|
||||
}
|
||||
|
||||
#pragma omp critical
|
||||
{
|
||||
if (local_min < global_min)
|
||||
global_min = local_min;
|
||||
|
||||
if (local_max > global_max)
|
||||
global_max = local_max;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void means_cpp(const std::vector<size_t> numbers, double &am, double &gm, double &hm)
|
||||
{
|
||||
size_t const n = numbers.size();
|
||||
|
||||
am = reduce(std::execution::par, numbers.begin(), numbers.end());
|
||||
gm = transform_reduce(std::execution::par, numbers.begin(), numbers.end(), 0.0, plus{}, [] (size_t x) -> double { return log(x); } );
|
||||
hm = transform_reduce(std::execution::par, numbers.begin(), numbers.end(), 0.0, plus{}, [] (size_t x) -> double { return 1.0/x; });
|
||||
|
||||
am /= n;
|
||||
gm = exp(gm/n);
|
||||
hm = n/hm;
|
||||
}
|
||||
|
||||
|
||||
void minmax_cpp(const std::vector<size_t> numbers, size_t &global_min, size_t &global_max)
|
||||
{
|
||||
auto min_it = min_element(std::execution::par, numbers.begin(), numbers.end());
|
||||
auto max_it = max_element(std::execution::par, numbers.begin(), numbers.end());
|
||||
|
||||
global_min = *min_it;
|
||||
global_max = *max_it;
|
||||
}
|
||||
42
ex5/ex5_2/mylib.h
Normal file
42
ex5/ex5_2/mylib.h
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#include <vector>
|
||||
|
||||
/**
|
||||
This function calculates arithmetic mean, geometric mean and harmonic mean of an integer vector.
|
||||
Uses openMP parallelization.
|
||||
@param[in] numbers vector containing integers
|
||||
@param[out] am arithmetic mean
|
||||
@param[out] gm geometric mean
|
||||
@param[out] hm harmonic mean
|
||||
*/
|
||||
void means_omp(const std::vector<size_t> numbers, double &am, double &gm, double &hm);
|
||||
|
||||
|
||||
/**
|
||||
This function calculates the minimum and maximum of a vector.
|
||||
Uses openMP parallelization.
|
||||
@param[in] numbers vector containing integers
|
||||
@param[out] global_min minimum
|
||||
@param[out] global_max maximum
|
||||
*/
|
||||
void minmax_omp(const std::vector<size_t> numbers, size_t &global_min, size_t &global_max);
|
||||
|
||||
|
||||
/**
|
||||
This function calculates arithmetic mean, geometric mean and harmonic mean of an integer vector.
|
||||
Uses C++ parallelization.
|
||||
@param[in] numbers vector containing integers
|
||||
@param[out] am arithmetic mean
|
||||
@param[out] gm geometric mean
|
||||
@param[out] hm harmonic mean
|
||||
*/
|
||||
void means_cpp(const std::vector<size_t> numbers, double &am, double &gm, double &hm);
|
||||
|
||||
|
||||
/**
|
||||
This function calculates the minimum and maximum of a vector.
|
||||
Uses C++ parallelization.
|
||||
@param[in] numbers vector containing integers
|
||||
@param[out] global_min minimum
|
||||
@param[out] global_max maximum
|
||||
*/
|
||||
void minmax_cpp(const std::vector<size_t> numbers, size_t &global_min, size_t &global_max);
|
||||
Loading…
Add table
Add a link
Reference in a new issue