Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulOxxx1 committed Feb 13, 2018
0 parents commit eda4506
Show file tree
Hide file tree
Showing 46 changed files with 1,360 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app
30 changes: 30 additions & 0 deletions 1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
using namespace std;

struct A {
int i;
A(const int& i): i(i) {}
};

ostream& operator<<(ostream& s, const A& a) {
s << a.i;
return s;
}

template<typename T>
void print(const T& i) {
cout << i << endl;
}

template<typename F, typename S>
void print(const S& a, const F& b) {
cout << a << " " << b << endl;
}

int main(int argc, char const *argv[]) {
int i=42;
print(i);
A a(43);
print(a, a);
return 0;
}
38 changes: 38 additions & 0 deletions 2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <list>
#include <iostream>
using namespace std;

struct A {
int i;
A(const int& i): i(i) {}
};

ostream& operator<<(ostream& s, const A& a) {
s << a.i;
return s;
}

template<typename T>
struct stack {
list<T> v;
void push(const T& i);
const T& top() { return v.back(); }
void pop() { v.pop_back(); }
bool empty() { return v.size()==0; }
unsigned int size() { return v.size(); }
};

template<typename T>
void stack<T>::push(const T& i) { v.push_back(i); }

int main(int argc, char const *argv[]) {
stack<A> s;
s.push(1);
cout << s.top() << endl;
cout << s.size() << endl;
cout << s.empty() << endl;
s.pop();
cout << s.size() << endl;
cout << s.empty() << endl;
return 0;
}
8 changes: 8 additions & 0 deletions 3/A.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <ostream>
using namespace std;
#include "A.h"

ostream& operator<<(ostream& s, const A& a) {
s << a.i;
return s;
}
6 changes: 6 additions & 0 deletions 3/A.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
struct A {
int i;
A(const int& i): i(i) {}
};

ostream& operator<<(ostream& s, const A& a);
17 changes: 17 additions & 0 deletions 3/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
using namespace std;

#include "A.h"
#include "stack.h"

int main(int argc, char const *argv[]) {
stack<A> s;
s.push(1);
cout << s.top() << endl;
cout << s.size() << endl;
cout << s.empty() << endl;
s.pop();
cout << s.size() << endl;
cout << s.empty() << endl;
return 0;
}
2 changes: 2 additions & 0 deletions 3/stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
template<typename T>
void stack<T>::push(const T& i) { v.push_back(i); }
14 changes: 14 additions & 0 deletions 3/stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <list>
using namespace std;

template<typename T>
struct stack {
list<T> v;
void push(const T& i);
const T& top() { return v.back(); }
void pop() { v.pop_back(); }
bool empty() { return v.size()==0; }
unsigned int size() { return v.size(); }
};

#include "stack.cpp"
112 changes: 112 additions & 0 deletions A10_MPI/10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include <iostream>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <mpi.h>
using namespace std;

int main(int argc, char *argv[]) {
// Erwartet die Groesse der Matrix
assert(argc==2);

int size = atoi(argv[1]);
double result = 0;
double** matrix;
double* vector;
double* vector2;

// MPI Zeug
MPI_Init(&argc, &argv);
int id;
int num_procs;
MPI_Status* status;
MPI_Comm_rank(MPI_COMM_WORLD, &id);
MPI_Comm_size(MPI_COMM_WORLD, &num_procs);

assert(num_procs > 1);

int work = 1;

// Hauptprozess
if (id == 0) {
// Matrix und Vektor erstellen
srand(0);
vector = new double[size];
vector2 = new double[size];
for (int i=0;i<size;i++) {
vector[i] = (double)rand()/RAND_MAX*10;
vector2[i] = 0;
}

srand(0);
matrix = new double*[size];
for (int i=0;i<size;i++) {
matrix[i] = new double[size];
for (int j=0;j<size;j++) {
matrix[i][j] = 10 * static_cast<double>(rand()) / RAND_MAX;
}
}

// An die Arbeit
for (int i=1;i<num_procs;i++) {
MPI_Send(vector, size, MPI_DOUBLE, i, 1, MPI_COMM_WORLD);
}
for (int i=0;i<size;i++) {
int target = (i%(num_procs-1))+1;
MPI_Send(&work, 1, MPI_INT, target, 4, MPI_COMM_WORLD);
MPI_Send(matrix[i], size, MPI_DOUBLE, target, 0, MPI_COMM_WORLD);
MPI_Recv(&vector2[i], 1, MPI_DOUBLE, (i%(num_procs-1))+1, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
// int num_workers = num_procs-1;
// for (int i=1;i<=num_workers;i++) {
// int min = ((i-1)/num_workers)*size;
// int max = ( i /num_workers)*size-1;
// for (int j=min;j<=max;j++) {
// MPI_Send(&work, 1, MPI_INT, i, 4, MPI_COMM_WORLD);
// MPI_Send(matrix[j], size, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
// MPI_Send(vector, size, MPI_DOUBLE, i, 1, MPI_COMM_WORLD);
// }
// }

// Arbeiter entlassen
work = 0;
for (int i=1; i<num_procs; i++) {
MPI_Send(&work, 1, MPI_INT, i, 4, MPI_COMM_WORLD);
}

// Vektor Vektor Produkt berechnen
for (int i=0;i<size;i++) {
result += vector2[i]*vector[i];
}

// Ergebnis ausgeben
cout << result << endl;

// Nebenprozesse
} else {
double* a = new double[size];
double* b = new double[size];
double c;
// Vektor empfangen
MPI_Recv(b, size, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD, status);
// Arbeit flag empfangen
MPI_Recv(&work, 1, MPI_INT, 0, 4, MPI_COMM_WORLD, status);
while (work) {
// Matrix Zeile empfangen
MPI_Recv(a, size, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, status);
c = 0;
for (int i=0;i<size;i++) {
c += a[i]*b[i];
}
// Ergebnis senden
MPI_Send(&c, 1, MPI_DOUBLE, 0, 2, MPI_COMM_WORLD);
// Arbeit flag empfangen
MPI_Recv(&work, 1, MPI_INT, 0, 4, MPI_COMM_WORLD, status);
}
delete [] a;
delete [] b;
}

MPI_Finalize();
return 0;
}
9 changes: 9 additions & 0 deletions A10_MPI/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CPPC=g++-7

all: matrix.cpp 10.cpp
g++-7 -fopenmp matrix.cpp 10.cpp -o 10.exe

clean:
rm -f *.exe

.PHONY: all clean
8 changes: 8 additions & 0 deletions A10_MPI/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Anzahl Threads: 2
Laufzeit: 0.365544
Anzahl Threads: 4
Laufzeit: 0.193298
Anzahl Threads: 8
Laufzeit: 0.18129
Anzahl Threads: 16
Laufzeit: 0.182153
75 changes: 75 additions & 0 deletions A10_MPI/matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "matrix.hpp"

double matrix::getP(const vector<double>& v) {
// Vorbereitungen treffen
assert(v.size() == size);
double result = 0;
vector<double> b(size,0);

// MPI Zeug
int id;
int num_procs;
MPI_Status* status;
MPI_Comm_rank(MPI_COMM_WORLD, &id);
MPI_Comm_size(MPI_COMM_WORLD, &num_procs);

// Arbeit senden
int work = 1;
for (int i=0;i<size;i++) {
int target = (i%(num_procs-1))+1;
MPI_Send(&work, 1, MPI_INT, target, 4, MPI_COMM_WORLD);
MPI_Send(&value[i][0], size, MPI_DOUBLE, target, 0, MPI_COMM_WORLD);
MPI_Send(&v[0], size, MPI_DOUBLE, target, 1, MPI_COMM_WORLD);
}

// Ergebnisse sammeln
for (int i=0;i<size;i++) {
MPI_Recv(&b[i], 1, MPI_DOUBLE, (i%(num_procs-1))+1, 2, MPI_COMM_WORLD, status);
}

// Arbeiter entlassen
work = 0;
for (int i=1;i<num_procs;i++) {
MPI_Send(&work, 1, MPI_INT, i, 4, MPI_COMM_WORLD);
}

// Vektor Vektor Produkt berechnen
for (int i=0;i<size;i++) {
result += b[i]*v[i];
}

// Fertig
return result;
}

// Ausgabe einer Matrix
ostream& operator<<(ostream& s, matrix& a) {
for (int i=0;i<a.size;i++) {
for (int j=0;j<a.size;j++) {
s << "[" << a.value[i][j] << "]";
}
s << endl;
}
return s;
}

// Konstruktor (mit zufälligen Einträgen)
matrix::matrix(int n): size(n) {
srand(0);

value = new double*[n];
for (int i=0;i<n;i++) {
value[i] = new double[n];
for (int j=0;j<n;j++) {
value[i][j] = 10 * static_cast<double>(rand()) / RAND_MAX;
}
}
}

// Destruktor
matrix::~matrix() {
for (int i=0;i<size;i++) {
delete value[i];
}
delete value;
}
16 changes: 16 additions & 0 deletions A10_MPI/matrix.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include <iostream>
#include <cassert>
#include <vector>
#include <mpi.h>
using namespace std;

class matrix {
int size;
double** value;
public:
double getP(const vector<double>&);
friend ostream& operator<<(ostream& s, matrix& a);
matrix(int);
~matrix();
};
Loading

0 comments on commit eda4506

Please sign in to comment.