'parallel programming multiplying two arrays of numbers

I have the following C++ code that multiply two array elements of a large size count

double* pA1 = { large array };
double* pA2 = { large array };
for(register int r = mm; r <= count; ++r)
{
    lg += *pA1-- * *pA2--;  
}

Is there a way that I can implement parallelism for the code?



Solution 1:[1]

I wanted to answer my own question. Looks like we can use openMP like the following. However, the speed gains is not that much (2x). My computer has 16 cores.

// need to use compile flag /openmp
double dot_prod_parallel(double* v1, double* v2, int dim)
{
    TimeMeasureHelper helper;

    double sum = 0.;
    int i;
# pragma omp parallel shared(sum)
    {
        int num = omp_get_num_threads();
        int id = omp_get_thread_num();
        printf("I am thread #  % d of % d.\n", id, num);

        double priv_sum = 0.;
# pragma omp for
        for (i = 0; i < dim; i++)
        {
            priv_sum += v1[i] * v2[i];
        }

#pragma omp critical
        {
            cout << "priv_sum = " << priv_sum << endl;
            sum += priv_sum;
        }
    }
    return sum;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user1998863