-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit eda4506
Showing
46 changed files
with
1,360 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
Oops, something went wrong.