Home > other >  is there a way to increment a loop by a power in parallel when using openmp
is there a way to increment a loop by a power in parallel when using openmp

Time:10-18

int d = 0;
while (d < log2(n))
{
    #pragma omp parallel
        #pragma omp for
        for (int i = 0; i < n; i = i   pow(2, d   1))
        {
            ScanNumbers[i   pow(2, d   1) - 1] = ScanNumbers[i   pow(2, d) - 1]   ScanNumbers[i   pow(2, d   1) - 1];
        }
    PrintVector(ScanNumbers);
    d  ;
}

ScanNumbers is a vector of ints. n is the size of ScanNumbers

CodePudding user response:

d is a constant inside the inner loop, so it can be replaced by a simple integer without having to call pow on every iteration.

    while (n >> (d 1))  // d < log2(n)
    {
        const int d_pow = 1 << (d 1); // pow(2,d 1)
        #pragma omp parallel
        #pragma omp for
        for (int i = 0; i < n; i = i   d_pow)
        {
            ScanNumbers[i   (1 << (d   1)) - 1] = ScanNumbers[i   (1 << d) - 1]   ScanNumbers[i   (1 << (d   1)) - 1];
        }
        PrintVector(ScanNumbers);
        d  ;
    }

also don't use log2 and pow for base2 arithmetic, as pointed by @user4581301 this causes inacuracy and it's also computationally expensive as opposed to shifts.

Edit: just added 1 to fix the integer math,

CodePudding user response:

while (d < log2(n))
{
#pragma omp parallel
    #pragma omp for
    for (int i = 0; i < n; i = i   (int)(pow(2, d   1)))
    {
        ScanNumbers[i   pow(2, d   1) - 1] = ScanNumbers[i   pow(2, d) - 1]   ScanNumbers[i   pow(2, d   1) - 1];
    }
    
    PrintVector(ScanNumbers);
    d  ;
}
  • Related