Home > Enterprise >  OpenMP parallel iteration over STL unordered_map VS2022
OpenMP parallel iteration over STL unordered_map VS2022

Time:08-21

I am trying to parallelize my sparse square matrix self multiplication. For instance, I want to compute:

    R = A * A

where R and A are sparse matrices. These sparse matrices are represented using 2d unordered_map, where A[i][j] corresponds to the (i,j)-th element. Also i and j are the keys of the corresponding 2d unordered_map. Unfortunately, the OpenMP's #pragma omp parallel for doesn't seem to work in my code below... (this code is part of my smatrix class definition).

    std::pair<int, int> shape;
    std::unordered_map<int, std::unordered_map<int, mpreal>> nzel;

    smatrix smul(void)
    {
        smatrix result(shape);
        #pragma omp parallel for
        for (int row = 0; row < shape.first; row  )
        {
            auto& c = result.nzel[row];
            for (const auto& [k1, v1] : nzel[row])
                for (const auto& [k2, v2] : nzel[k1])
                    c[k2]  = v1 * v2;
        }
        return result;
    }

What is my problem? I am still new to OpenMP, but I seriously need to make my code parallel. Your help is much appreciated.

CodePudding user response:

  1. You don't say how your code "doesn't work".
  2. I'm guessing it doesn't compile: OpenMP needs a random-access iterator, and unordered map is not.
  3. However, you're mapping int to a sparse row. Why not make that an array of sparse rows?
  4. You have a race condition on your output array so the results will be incorrect. You need to rewrite this. Sparse matrix-matrix is not a simple operation.
  • Related