Changed for loop in stencil.hpp to double loop, changed main script structure
This commit is contained in:
63
stencil.hpp
63
stencil.hpp
@ -138,7 +138,7 @@ void Stencil<T>::stdthread(
|
||||
for (auto result : *ResultsVector) {
|
||||
Task<T> *task = result.get();
|
||||
int niter = Iterations;
|
||||
int delta = (task->Rows * task->Cols) / MaxThreads;
|
||||
int delta = task->Rows / MaxThreads;
|
||||
|
||||
std::vector<std::vector<T>> *arena = new std::vector<std::vector<T>>(0);
|
||||
*arena = *(task->VectorData); // copy all from VectorData
|
||||
@ -146,9 +146,30 @@ void Stencil<T>::stdthread(
|
||||
while (niter > 0) {
|
||||
for (int l = 0; l < MaxThreads - 1; ++l) {
|
||||
thread_pool.addJob([&, l, delta] {
|
||||
for (int k = l * delta; k < (l + 1) * delta; ++k) {
|
||||
int x = k / task->Cols;
|
||||
int y = k % task->Cols;
|
||||
for (int x = l * delta; x < (l+1) * delta; ++x) {
|
||||
for (int y = 0; y < task->Cols; ++y) {
|
||||
if (x < Borders[1] || x >= task->Rows - Borders[3] ||
|
||||
y < Borders[0] || y >= task->Cols - Borders[2]) {
|
||||
continue;
|
||||
}
|
||||
std::vector<T> n;
|
||||
n.resize(Neighborhood.size());
|
||||
std::transform(
|
||||
Neighborhood.begin(), Neighborhood.end(),
|
||||
n.begin(),
|
||||
[&task, x, y](std::pair<int, int> e) {
|
||||
return (*task->VectorData)[x + e.second]
|
||||
[y + e.first];
|
||||
});
|
||||
(*arena)[x][y] = Convolution(n);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
thread_pool.addJob([&, delta, MaxThreads] {
|
||||
for (int x = (MaxThreads - 1) * delta;
|
||||
x < task->Rows; ++x) {
|
||||
for (int y = 0; y < task->Cols; ++y) {
|
||||
if (x < Borders[1] || x >= task->Rows - Borders[3] ||
|
||||
y < Borders[0] || y >= task->Cols - Borders[2]) {
|
||||
continue;
|
||||
@ -158,31 +179,11 @@ void Stencil<T>::stdthread(
|
||||
std::transform(
|
||||
Neighborhood.begin(), Neighborhood.end(), n.begin(),
|
||||
[&task, x, y](std::pair<int, int> e) {
|
||||
return (*task->VectorData)[x + e.second]
|
||||
[y + e.first];
|
||||
return (
|
||||
*task->VectorData)[x + e.second][y + e.first];
|
||||
});
|
||||
(*arena)[x][y] = Convolution(n);
|
||||
}
|
||||
});
|
||||
}
|
||||
thread_pool.addJob([&, delta, MaxThreads] {
|
||||
for (int k = (MaxThreads - 1) * delta;
|
||||
k < task->Cols * task->Rows; ++k) {
|
||||
int x = k / task->Cols;
|
||||
int y = k % task->Cols;
|
||||
if (x < Borders[1] || x >= task->Rows - Borders[3] ||
|
||||
y < Borders[0] || y >= task->Cols - Borders[2]) {
|
||||
continue;
|
||||
}
|
||||
std::vector<T> n;
|
||||
n.resize(Neighborhood.size());
|
||||
std::transform(
|
||||
Neighborhood.begin(), Neighborhood.end(), n.begin(),
|
||||
[&task, x, y](std::pair<int, int> e) {
|
||||
return (
|
||||
*task->VectorData)[x + e.second][y + e.first];
|
||||
});
|
||||
(*arena)[x][y] = Convolution(n);
|
||||
}
|
||||
});
|
||||
thread_pool.waitEnd();
|
||||
@ -250,13 +251,12 @@ template <typename T> Task<T> *Stencil<T>::svc_helper(Task<T> *task) {
|
||||
|
||||
while (niter > 0) {
|
||||
parallel_for(
|
||||
0, task->Rows * task->Cols,
|
||||
[&](const int k) {
|
||||
int x = k / task->Cols;
|
||||
int y = k % task->Cols;
|
||||
0, task->Rows,
|
||||
[&](const int x) {
|
||||
for (int y = 0; y < task->Cols; ++y) {
|
||||
if (x < Borders[1] || x >= task->Rows - Borders[3] ||
|
||||
y < Borders[0] || y >= task->Cols - Borders[2]) {
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
std::vector<T> n;
|
||||
n.resize(Neighborhood.size());
|
||||
@ -266,6 +266,7 @@ template <typename T> Task<T> *Stencil<T>::svc_helper(Task<T> *task) {
|
||||
return (*task->VectorData)[x + e.second][y + e.first];
|
||||
});
|
||||
(*arena)[x][y] = Convolution(n);
|
||||
}
|
||||
},
|
||||
MaxWorkers);
|
||||
std::swap(task->VectorData, arena);
|
||||
|
||||
Reference in New Issue
Block a user