Проблема с распараллеливанием с помощью omp parallel
последовательная программа выдает такие результаты:
Array 'a': 0.20 5.98 3.42 2.49 1.95 1.54 1.20 0.88 0.60 0.33
Array 'b': 25.00 3.14 0.87 0.35 0.06 -0.24 -0.75 -1.47 -1.75 -0.81
Array 'c': -0.44 5.25 2.55 1.39 0.32 -1.58 -6.30 -16.62 -29.40 -24.67
параллельная при тех же входных данных:
Array 'a': 0.20 10.98 5.92 4.16 0.00 0.00 0.00 0.00 0.00 0.00
Array 'b': 25.00 11.25 2.99 0.00 3.12 1.62 1.10 0.87 0.73 0.64
Array 'c': -0.44 10.25 5.05 0.00 0.00 0.00 0.00 0.00 0.00 0.00
функции вычислений и заполнений массивов:
void initFirstArr(std::vector<double> &a, double x, int mode)
{
auto length = a.size();
a[0] = 1.0 / x;
if (mode == 1) {
auto chunk = length / omp_get_max_threads();
if (length % omp_get_max_threads()) {
++chunk;
}
#pragma omp parallel
{
auto thread_num = omp_get_thread_num();
auto start = chunk * thread_num + 1;
auto end = (chunk * (static_cast<unsigned long long>(thread_num) + 1)) + 1;
if (end > length) {
end = length;
}
for (auto i = start; i < end; ++i) {
a[i] = cos(static_cast<double>(i) / x) + (x / static_cast<double>(i)) + (x / i);
}
}
}
else if (mode == 0) {
for (std::size_t i = 1; i < length; ++i) {
a[i] = cos(i / x) + (x / i);
}
}
}
void initSecondArr(std::vector<double>& b, double x, int mode) {
b[0] = x * x;
for (std::size_t i = 1; i < b.size(); ++i) {
b[i] = (b[i - 1] + x) / i;
}
}
void calcFirst(const std::vector<double> &a, const std::vector<double>& b, std::vector<double>& c, int mode) {
auto length = a.size();
if (mode == 1) {
auto chunk = length / omp_get_max_threads();
if (length % omp_get_max_threads()) {
++chunk;
}
#pragma omp parallel
{
auto thread_num = omp_get_thread_num();
auto start = chunk * thread_num;
auto end = chunk * (static_cast<unsigned long long>(thread_num) + 1);
if (end > length) {
end = length;
}
for (auto i = start; i < end; ++i) {
c[i] = a[i] - b[length - i - 1];
}
}
}
else
if (mode == 0) {
for (std::size_t i = 0; i < length; ++i) {
c[i] = a[i] - b[length - i-1];
}
}
}
void calcSecond(const std::vector<double>& a, const std::vector<double>& c, std::vector<double>& b, int mode) {
auto length = a.size();
if (mode == 1) {
auto chunk = length / omp_get_max_threads();
if (length % omp_get_max_threads()) {
++chunk;
}
#pragma omp parallel
{
auto thread_num = omp_get_thread_num();
auto start = chunk * thread_num + 1;
auto end = (chunk * (static_cast<unsigned long long>(thread_num) + 1)) + 1;
if (end > length) end = length;
for (auto i = start; i < end; ++i) {
b[i] = (a[i] * c[i]) / length;
}
}
}
else if (mode == 0) {
for (std::size_t i = 1; i < length; ++i) {
b[i] = (a[i] * c[i]) / length;
}
}
}