OpenMP Распараллелить цикл C++

247
05 марта 2018, 00:58
  1. Распараллелить цикл:

    for(i = 2; i < N; i++)
    for(j = 2; i < N; j++)
      A[i, j] = A[i - 2, j] + A[i, j - 2];

Как эффективно распараллелить. Результаты выходят неверные.

 #include <iostream>
#include <omp.h>
#include <iomanip>
using namespace std;

    int main()
    {
         int n=1000;
         int g = 1;
         int **A = new int*[1000];
         for (int i = 0; i < n; i++)
            A[i] = new int[n+1];
         for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                A[i][j] = g;
                A[j][i] = g+2;
               // cout << A[i][j] << " ";
                g++;
            }
           // cout << endl;
        }   
    cout << endl;
    #pragma omp parallel num_threads(4)
    {
        int i1, j1;
        if (omp_get_thread_num() == 0)
            i1 = 0, j1 = 0;
        if (omp_get_thread_num() == 1)
            i1 = 1, j1 = 0;
        if (omp_get_thread_num() == 2)
            i1 = 0, j1 = 1;
        if (omp_get_thread_num() == 3)
            i1 = 1, j1 = 1;
        for (int i = 2 + i1; i < n; i += 2)
        {
            for (int j = 2 + j1; j < n; j += 2)
            {
                A[i][j] = A[i - 2][j] + A[i][j - 2];
            }
        }
    }

    for (int i = n-2; i < n; i++) {
            for (int j = n-2; j < n; j++) {
                cout << A[i][j] << " ";
            }
            cout << endl;
        }
        for (int i = 0; i < n; i++)
            delete [] A[i];
            delete [] A;
    }`
Answer 1
omp_set_num_threads(4);
#pragma omp parallel num_threads(4)
    {
        int r, c;
        if (omp_get_thread_num() == 0)
            r = 0, c = 0;
        if (omp_get_thread_num() == 1)
            r = 1, c = 0;
        if (omp_get_thread_num() == 2)
            r = 0, c = 1;
        if (omp_get_thread_num() == 3)
            r = 1, c = 1;
        for (int i = 2 + c; i < n3; i += 2)
        {
            for (int j = 2 + r; j < n3; j += 2)
            {
                arr1[i][j] = arr1[i - 2][j] + arr1[i][j - 2];
            }
        }
    }
READ ALSO
C++ Error: double free or corruption. Thread. File. Exception. Lambda.

C++ Error: double free or corruption. Thread. File. Exception. Lambda.

Ожидал вывод "Exception opening file" и "Exception get line" каждые 5 секунд

280
Arduino timeout communicating with programmer error

Arduino timeout communicating with programmer error

При загрузке простейшего блинка или hello world на Arduino Mega2560, получаю следующие ошибки:

208
Реализация синглтона через unique_ptr

Реализация синглтона через unique_ptr

Встретил несколько раз в проектном коде реализации синглтонов подобным образом:

215