Search code examples
c++openmp

Output of the number of iterations processed by each thread


I have written code for parallel matrix processing using OpenMP that should output information about the number of iterations for each thread.

However, the output contains information for only one thread. I just started learning OpenMP and I haven't been able to figure out what the problem is.

int main() {

    int n;
    cout << "Enter matrix range: ";
    cin >> n;

    int** A = new int* [n];
    int** B = new int* [n];
    int* C = new int[n];  
    int* D = new int[n];  

    for (int i = 0; i < n; i++) {
        A[i] = new int[n];
        B[i] = new int[n];
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            A[i][j] = rand() % 100;
            B[i][j] = rand() % 100;
        }
    }

    int thread_num;
    int it_num;
#pragma omp parallel for schedule(static, 4) num_threads(4)
    {
        auto start_time = chrono::high_resolution_clock::now();
        for (int i = 0; i < n; i++) {
            int it_num = 0;
            int minA = A[i][0];
            it_num++;
            for (int j = 1; j < n; j++) {
                if (A[i][j] < minA) {
                    minA = A[i][j];
                    ++it_num;
                }
                
            }
            printf("thread %d processed %d iterations\n", omp_get_thread_num(), it_num);
            C[i] = minA; 
        }
        
        auto end_time = chrono::high_resolution_clock::now();
        auto duration = chrono::duration_cast<chrono::microseconds>(end_time - start_time).count();
        cout << "Time taken for schedule(static, 4): " << duration << " microseconds" << endl;
    }
}

Can you help me understand why the output only contains information for one thread, and how to fix it?


Solution

  • Two solutions.

    1. create an array of private counters, one per thread
    2. Decouple your omp parallel for into a omp parallel and omp for
    3. Between those two lines, let each thread compute its number
    4. In each iteration each thread updates its counter.
    int itcount[nthreads];
    # pragma omp parallel
    {
        int thread_num = omp_get_thread_num();
    #   pragma omp for schedule(guided)
        for (int n = 0; n < N; n++) {
          itcount[thread_num]++;
          // rest of the loop
    

    Second solution: use threadprivate data.