简体   繁体   中英

Paralleling a loop in C++ with OpenMP

The solution that this parallelized returns doesn't match with the solution that it should be return, like the non-parallelized one.

    double angle=(PI/180)*atoi(extra);
    unsigned int xf;
    unsigned int yf;
    int j;    
    #pragma omp parallel for private (j,xf,yf)        
    for(int i=0;i<width;i++){
        for(j=0;j<height;j++){  
            xf=(unsigned int)ceil(((cos(angle)*(j-(((double)height)/2.0)))-(sin(angle)*(i-(((double)width)/2.0))))+(((double)height)/2.0));
            yf=(unsigned int)ceil(((sin(angle)*(j-(((double)height)/2.0)))+(cos(angle)*(i-(((double)width)/2.0))))+(((double)width)/2.0));
            if(xf<(unsigned int)height && xf>=0 && yf>=0 && yf<(unsigned int)width){
                  matrixRed2[yf][xf]=matrixRed[i][j];
                  matrixGreen2[yf][xf]=matrixGreen[i][j];
                  matrixBlue2[yf][xf]=matrixBlue[i][j];
            }
        }
    }

I don't see it necessary for j, to be outside the loop since it just contain the temporary value before assigning to array (which have element independent). Moreover, two for loop can be collapsed. Edited: each xf, yf is not guarantee to be unique with the formula from i, j. So keep the sequential one, Or you can try to check to keep the largest (i, j) if there race condition in (xf, yf). I'm not sure OpenMP can work on Visual Studio or not, since the supported version is very old (2.0 meanwhile the latest is 4.5).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM