kais implementation
This commit is contained in:
@@ -24,17 +24,20 @@ void gauss_seidel_par(Matrix &phi, int maxNumIter) {
|
||||
|
||||
const double osth = 1. / 4;
|
||||
|
||||
#pragma omp parallel
|
||||
{
|
||||
for (int iter = 0; iter < maxNumIter; ++iter) {
|
||||
#pragma omp for
|
||||
for (int i = 1; i < m - 1; ++i) {
|
||||
int setp = i - omp_get_num_threads();
|
||||
for (int iter = 0; iter < maxNumIter; ++iter) {
|
||||
#pragma omp parallel num_threads(3)
|
||||
{
|
||||
int num_theads = omp_get_num_threads();
|
||||
int chunk = (m - 2) / num_theads;
|
||||
int start = 1 + chunk * omp_get_thread_num();
|
||||
int end = chunk * (omp_get_thread_num() + 1);
|
||||
|
||||
for (int i = start; i <= end; ++i) {
|
||||
for (int j = 1; j < n - 1; ++j) {
|
||||
phi(i, j) = osth * (phi(i + 1, j) + phi(i - 1, j) + phi(i, j + 1) +
|
||||
phi(i, j - 1));
|
||||
#pragma omp barrier
|
||||
}
|
||||
// #pragma omp barrier
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,37 +49,57 @@ void fill_matrix(Matrix &matrix, const int filler) {
|
||||
|
||||
for (int i = 0; i < m; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
matrix(i, j) = filler;
|
||||
if (i == 0 || i == m - 1 || j == 0 || j == n - 1) {
|
||||
matrix(i, j) = filler;
|
||||
} else
|
||||
matrix(i, j) = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool check(Matrix &a, Matrix &b) {
|
||||
void check(const Matrix &a, const Matrix &b) {
|
||||
const int m = a.dim1();
|
||||
const int n = a.dim2();
|
||||
|
||||
for (int i = 0; i < m; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
if (a(i, j) != b(i, j)) {
|
||||
std::cout << "Not equal at (" << i << ", " << j << ")" << std::endl;
|
||||
return false;
|
||||
std::cout << "Not equal at (" << i << ", " << j << "), a: " << a(i, j)
|
||||
<< " != " << b(i, j) << " :b" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void print_matrix(const Matrix &matrix) {
|
||||
const int m = matrix.dim1();
|
||||
const int n = matrix.dim2();
|
||||
|
||||
for (int i = 0; i < m; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
std::cout << matrix(i, j) << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
Matrix first = Matrix(10, 10);
|
||||
Matrix sec = Matrix(10, 10);
|
||||
Matrix first = Matrix(8, 8);
|
||||
Matrix sec = Matrix(8, 8);
|
||||
|
||||
fill_matrix(first, 10);
|
||||
fill_matrix(sec, 10);
|
||||
|
||||
print_matrix(first);
|
||||
print_matrix(sec);
|
||||
|
||||
gauss_seidel(first, 1000);
|
||||
gauss_seidel_par(sec, 1000);
|
||||
|
||||
std::cout << check(first, sec) << std::endl;
|
||||
// check(first, sec);
|
||||
|
||||
print_matrix(first);
|
||||
print_matrix(sec);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user