Hi all. I've been learning programming from the web for almost 2 years now and this'll be my first post ever on DaniWeb (or any forum ever). I have no experience with parallel programming but read about the simplicity of working with OpenMP, and that's what I need. Something that will make my code parallel with little to no effort on my part so I can get my work done asap.

I've got a program that works perfectly fine in serial, so I hoped I could just drop "#pragma omp parallel for" on top of a for loop and solve all my problems. Unfortunately, as soon as I did that, my code suddenly produced the runtime error "Expression: vector subscript out of range". Here's the code (lines that trigger the problem have //----->PROBLEM next to them):

#pragma omp parallel
    {
        #pragma omp parallel for
        for (i = 0; i < pars[1]; i++) // vector <int> pars(some size), pars[1] > 0
        {
            for (j = 0; j < pars[1]; j++)
            {
                if (YOB[i][0] > YOB[j][0]) // compare two doubles in 2D vector (irrelevant)
                {
                    dist = L2norm(depop[i],depop[j]); // double dist (irrelevant)
                    beta = (beta0-betamin) * exp(-gama*dist*dist) + betamin; // double beta (irrelevant)
                    if (beta > 0.9) 
                    {
                        continue;
                    }

                    //update each component of position vector
                    for (d = 0; d < pars[0]; d++)
                    {
                        dom = domain(d,pars[3]); // vector <double> dom(2) (irrelevant)

                        tempop[i][d] = tempop[i][d]*(1-beta) + depop[j][d]*beta 
                            + arfa*(ran2(&idum)-0.5)*(dom[1]-dom[0]); //----->PROBLEM

                        //ENFORCE DOMAIN
                        if (tempop[i][d] > dom[1]) //----->PROBLEM
                            tempop[i][d] = dom[1]; //----->PROBLEM

                        if (tempop[i][d] < dom[0]) //----->PROBLEM
                            tempop[i][d] = dom[0]; //----->PROBLEM
                    }//end d
                }//end if - compare objective functions
                else
                {
                    if ( (i != bi) && (i != j) )
                    {
                        //randomly update each component of position vector
                        for (d = 0; d < pars[0]; d++)
                        {
                            dom = domain(d,pars[3]);

                            tempop[i][d] += arfa*arfa*(ran2(&idum)-0.5)*(dom[1]-dom[0]); //----->PROBLEM

                            //ENFORCE DOMAIN
                            if (tempop[i][d] > dom[1]) //----->PROBLEM
                                tempop[i][d] = dom[1]; //----->PROBLEM

                            if (tempop[i][d] < dom[0]) //----->PROBLEM
                                tempop[i][d] = dom[0]; //----->PROBLEM
                        }
                    }
                }
            }//end j
        }//end i
    }//end pragma omp parallel

Recall, this program works perfectly fine as soon as I comment out the OpenMP code.

For the "pragma" commands, I've tried all sorts of possible combinations:

  • over the outer for loop
    pragma... for
    pragma... for private (i)
    pragma... for firstprivate(j) lastprivate(i)

  • over the inner for loop (with nothing on the outer loop)
    pragma... for private (j)

This bug appears to occur constistently at the end of the loop (the very last iteration). I understand that loop-carried dependencies can be a problem. Unfortunately, the equation is what it is. But I absolutely must parallelize this code because that inner loop is making my program run way too slow. Thank you for your help!

I'm not sure, but I'm inclined to think that the indices i and j should be declared within the for-loop statement. Otherwise, each parallel thread will be using the same variable, which is not going to go well. I would try this:

#pragma omp parallel
    {
        #pragma omp parallel for
        for (std::size_t i = 0; i < pars[1]; i++) // vector <int> pars(some size), pars[1] > 0
        {
            for (std::size_t j = 0; j < pars[1]; j++)
            {

            //...

            }//end j
        }//end i
    }//end pragma omp parallel

I did what you suggested and it no longer produces that error. Thank you for your help!

This question has already been answered. Start a new discussion instead.