sheet 5
This commit is contained in:
parent
6fb3c9f65d
commit
54e82629a7
5 changed files with 801 additions and 0 deletions
30
Sheet_5/bsp_5_2/Makefile
Normal file
30
Sheet_5/bsp_5_2/Makefile
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#
|
||||
# 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 bsp_5_2_lib.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
|
||||
149
Sheet_5/bsp_5_2/bsp_5_2_lib.cpp
Normal file
149
Sheet_5/bsp_5_2/bsp_5_2_lib.cpp
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
#include "bsp_5_2_lib.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
#include <execution>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#ifdef _OPENMP
|
||||
#include <omp.h>
|
||||
#endif
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
|
||||
|
||||
void meandevvec(vector<int> x, float &a, float &g, float &h, float &d)
|
||||
{
|
||||
a = 0;
|
||||
#pragma omp parallel for default(none) shared(x) reduction(+:a)
|
||||
for(size_t k=1; k<=x.size(); ++k)
|
||||
{
|
||||
a = a+x.at(k-1);
|
||||
}
|
||||
a = a/x.size();
|
||||
|
||||
g = 1;
|
||||
#pragma omp parallel for default(none) shared(x) reduction(*:g)
|
||||
for(size_t k=1; k<=x.size(); ++k)
|
||||
{
|
||||
g = g*pow(x.at(k-1),1.0/x.size());
|
||||
}
|
||||
|
||||
h = 0;
|
||||
#pragma omp parallel for default(none) shared(x) reduction(+:h)
|
||||
for(size_t k=1; k<=x.size(); ++k)
|
||||
{
|
||||
h = h + 1.0/x.at(k-1);
|
||||
}
|
||||
h = x.size()/h;
|
||||
|
||||
d = 0;
|
||||
#pragma omp parallel for default(none) shared(x,a) reduction(+:d)
|
||||
for(size_t k=1; k<=x.size(); ++k)
|
||||
{
|
||||
d = d + pow(x.at(k-1)-a,2);
|
||||
}
|
||||
d = d/x.size();
|
||||
d = sqrt(d);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void minmaxvec(vector<int> &x, int &minv, int &maxv)
|
||||
{
|
||||
minv = numeric_limits<int>::max();
|
||||
maxv = numeric_limits<int>::min();
|
||||
|
||||
#pragma omp parallel for default(none) shared(x) reduction(min:minv) reduction(max:maxv)
|
||||
for (size_t i = 0; i < x.size(); ++i)
|
||||
{
|
||||
minv = min(minv, x[i]);
|
||||
maxv = max(maxv, x[i]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void meandevmaxminvec(std::vector<int> &x, float &a, float &g, float &h, float &d, float &minv, float &maxv)
|
||||
{
|
||||
const size_t n = x.size();
|
||||
|
||||
float sum = reduce(execution::par, x.begin(), x.end());
|
||||
float logsum = transform_reduce(execution::par, x.begin(), x.end(), 0.0, plus<>(), [](float y){ return log(y); });
|
||||
float invsum = transform_reduce(execution::par, x.begin(), x.end(), 0.0, plus<>(), [](float y){ return 1.0/y; });
|
||||
|
||||
a = sum / n;
|
||||
g = exp(logsum / n);
|
||||
h = n / invsum;
|
||||
|
||||
// for calculating the deviation - splitting of the square in the sum
|
||||
float sq_sum = transform_reduce(execution::par, x.begin(), x.end(), 0.0, plus<>(), [](float y){ return y * y; });
|
||||
d = sqrt(sq_sum / n - a*a);
|
||||
|
||||
auto [min_it, max_it] = minmax_element(execution::par, x.begin(), x.end());
|
||||
minv = *min_it;
|
||||
maxv = *max_it;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// for reading a file and writing into a txt-file
|
||||
// [Str10, p.364]
|
||||
void fill_vector(istream& istr, vector<int>& v)
|
||||
{
|
||||
int d=0;
|
||||
while ( istr >> d) v.push_back(d); // Einlesen
|
||||
if (!istr.eof())
|
||||
{ // Fehlerbehandlung
|
||||
cout << " Error handling \n";
|
||||
if ( istr.bad() ) throw runtime_error("Schwerer Fehler in istr");
|
||||
if ( istr.fail() ) // Versuch des Aufraeumens
|
||||
{
|
||||
cout << " Failed in reading all data.\n";
|
||||
istr.clear();
|
||||
}
|
||||
}
|
||||
v.shrink_to_fit(); // C++11
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void read_vector_from_file(const string& file_name, vector<int>& v)
|
||||
{
|
||||
ifstream fin(file_name); // Oeffne das File im ASCII-Modus
|
||||
if( fin.is_open() ) // File gefunden:
|
||||
{
|
||||
v.clear(); // Vektor leeren
|
||||
fill_vector(fin, v);
|
||||
}
|
||||
else // File nicht gefunden:
|
||||
{
|
||||
cout << "\nFile " << file_name << " has not been found.\n\n" ;
|
||||
assert( fin.is_open() && "File not found." ); // exeption handling for the poor programmer
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void write_vector_to_file(const string& file_name, const vector<float>& v)
|
||||
{
|
||||
ofstream fout(file_name); // Oeffne das File im ASCII-Modus
|
||||
if( fout.is_open() )
|
||||
{
|
||||
for (unsigned int k=0; k<v.size(); ++k)
|
||||
{
|
||||
fout << v.at(k) << endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "\nFile " << file_name << " has not been opened.\n\n" ;
|
||||
assert( fout.is_open() && "File not opened." ); // exeption handling for the poor programmer
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
49
Sheet_5/bsp_5_2/bsp_5_2_lib.h
Normal file
49
Sheet_5/bsp_5_2/bsp_5_2_lib.h
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#ifndef BSP_5_2_LIB_H_INCLUDED
|
||||
#define BSP_5_2_LIB_H_INCLUDED
|
||||
|
||||
#include <iostream>
|
||||
#include <omp.h>
|
||||
#include <vector>
|
||||
|
||||
|
||||
/** \brief Funktion zur Berechnung des arithmetischen, geometrischen und harmonischen Mittels und der Standardabweichung aus den Eintraegen eines gegebenen Vektors
|
||||
*
|
||||
* \param[in,out] x Vektor mit Eintraegen
|
||||
* \param[in,out] a arithmetisches Mittel
|
||||
* \param[in,out] g geometrisches Mittel
|
||||
* \param[in,out] h harmonisches Mittel
|
||||
* \param[in,out] d Standardabweichung (deviation)
|
||||
* \return
|
||||
*
|
||||
*/
|
||||
void meandevvec(std::vector<int> x, float &a, float &g, float &h, float &d);
|
||||
|
||||
|
||||
/** Calculating the minimal and maximal element of a given vector with openMP (parallel)
|
||||
* @param[in,out] x vector
|
||||
* @param[in,out] minv minimal entry of x
|
||||
* @param[in,out] maxv maximal entry of x
|
||||
*
|
||||
*/
|
||||
void minmaxvec(std::vector<int> &x, int &minv, int &maxv);
|
||||
|
||||
/** Calculating the arithmetic, geometric and harmonic mean value of a given vector as well as the standard deviation and the minimal and maximal element with execution policies
|
||||
*
|
||||
* \param[in,out] x vector
|
||||
* \param[in,out] a arithmetic mean
|
||||
* \param[in,out] g geometric mean
|
||||
* \param[in,out] h harmonic mean
|
||||
* \param[in,out] d standard deviation
|
||||
* @param[in,out] minv minimal entry of x
|
||||
* @param[in,out] maxv maximal entry of x
|
||||
*
|
||||
*/
|
||||
void meandevmaxminvec(std::vector<int> &x, float &a, float &g, float &h, float &d, float &minv, float &maxv);
|
||||
|
||||
void fill_vector(std::istream& istr, std::vector<int>& v);
|
||||
|
||||
void read_vector_from_file(const std::string& file_name, std::vector<int>& v);
|
||||
|
||||
void write_vector_to_file(const std::string& file_name, const std::vector<float>& v);
|
||||
|
||||
#endif // BSP_5_2_LIB_H_INCLUDED
|
||||
500
Sheet_5/bsp_5_2/data_1.txt
Normal file
500
Sheet_5/bsp_5_2/data_1.txt
Normal file
|
|
@ -0,0 +1,500 @@
|
|||
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
|
||||
73
Sheet_5/bsp_5_2/main.cpp
Normal file
73
Sheet_5/bsp_5_2/main.cpp
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
#include "bsp_5_2_lib.h"
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
// BSP 5_2
|
||||
|
||||
int main()
|
||||
{
|
||||
const string name1("data_1.txt"); // name of input file
|
||||
const string name2("out.txt"); // name of output file
|
||||
vector<int> v;
|
||||
|
||||
read_vector_from_file(name1, v);
|
||||
|
||||
float a, g, h, d, aex, gex, hex, dex, minvex, maxvex, control1(0);
|
||||
int minvp, maxvp, control2(0);
|
||||
|
||||
int NLOOPS = 100000;
|
||||
|
||||
// testing parallel
|
||||
double tstart1 = omp_get_wtime();
|
||||
for(int i=0; i<NLOOPS; ++i)
|
||||
{
|
||||
meandevvec(v,a,g,h,d);
|
||||
minmaxvec(v,minvp,maxvp);
|
||||
control1 += a;
|
||||
control2 += minvp;
|
||||
}
|
||||
double t1all = omp_get_wtime() - tstart1;
|
||||
double t1 = t1all/NLOOPS;
|
||||
|
||||
// testing execution policies
|
||||
double tstart2 = omp_get_wtime();
|
||||
for(int i=0; i<NLOOPS; ++i)
|
||||
{
|
||||
meandevmaxminvec(v,aex,gex,hex,dex,minvex,maxvex);
|
||||
control1 += aex;
|
||||
control2 += minvex;
|
||||
}
|
||||
double t2all = omp_get_wtime() - tstart2;
|
||||
double t2 = t2all/NLOOPS;
|
||||
|
||||
cout << "\nTiming for parallel\n";
|
||||
cout << "Timing NLOOPS: " << t1all << endl;
|
||||
cout << "Timing per it. (sec): " << t1 << endl;
|
||||
|
||||
cout << "\n\nTiming for execution policies\n";
|
||||
cout << "Timing NLOOPS: " << t2all << endl;
|
||||
cout << "Timing per it. (sec): " << t2 << endl;
|
||||
|
||||
|
||||
cout << "\n\nAuswertung (parallel)" << endl;
|
||||
cout << "arithmetisches Mittel: " << a << endl;
|
||||
cout << "geometrisches Mittel: " << g << endl;
|
||||
cout << "harmonisches Mittel: " << h << endl;
|
||||
cout << "standard deviation: " << d << endl;
|
||||
cout << "Minimum: " << minvp << endl;
|
||||
cout << "Maximum: " << maxvp << endl;
|
||||
|
||||
cout << endl;
|
||||
cout << "\nAuswertung (execution policies)" << endl;
|
||||
cout << "arithmetisches Mittel: " << aex << endl;
|
||||
cout << "geometrisches Mittel: " << gex << endl;
|
||||
cout << "harmonisches Mittel: " << hex << endl;
|
||||
cout << "standard deviation: " << dex << endl;
|
||||
cout << "Minimum: " << minvex << endl;
|
||||
cout << "Maximum: " << maxvex << endl;
|
||||
|
||||
vector<float> ve{a,g,h,d,minvp,maxvp};
|
||||
write_vector_to_file(name2, ve);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue