简体   繁体   中英

OpenMP/C++ How to increment a variable in parallel for?

I have one problem, big problem =. I Have two image (using GDIplus) and I want to compare pixel-pixel. when pixelA = pixelB, the variable cont should be incremented.

today, I compare two equal image, my return should be 100%, but this return is 70%.

why? how can i resolve this?

see

    #pragma omp parallel for schedule(dynamic) 
    for (int x = 0; x < height; x++){
        for (int y = 0; y < width; y++){
            int luma01 = 0, luma02 = 0;


            Gdiplus::Color pixelColorImage01;
            Gdiplus::Color pixelColorImage02;

                myImage01->GetPixel(x, y, &pixelColorImage01);
                luma01 = pixelColorImage01.GetRed() + pixelColorImage01.GetGreen() + pixelColorImage01.GetBlue();
                myImage02->GetPixel(x, y, &pixelColorImage02);
                luma02 = pixelColorImage02.GetRed() + pixelColorImage02.GetGreen() + pixelColorImage02.GetBlue();

                #pragma omp critical
                if (luma01 == luma02){
                    cont++;
                }
            }
        }

percentage of equality between images

thanks =)

Before you parallelize your solution make sure you can solve it sequentially. In this case that means comment out the #pragma and debug that first.

First,

for (int x = 0; x < height; x++){
    for (int y = 0; y < width; y++){
        ...
        myImage01->GetPixel(x, y, &pixelColorImage01);

You transposed width and height , so you'll get a wrong answer for any image that's not square.

Second, your pixel equality metric is subject to collisions. Since you add up the individual colors' luminosities then compare that sum, it will think that, for example, an all red pixel is equal to an all blue one.
Do something like this instead:

if (red1 == red2 && green1 == green2 && blue1 == blue2)
    cont++;

As for your parallelization, it's technically correct but will give you terrible performance. You put a critical section around the if , so that means if all the workers are constantly trying to acquire that lock. In other words, you've got parallel workers but each one has to wait for all the others. In other words, you've serialized your parallel code. To solve this problem look up OpenMP reducers.

I'm happy for your answers! Adam, i switched the position of matriz. and now, I compare individual the colors! Now, i have two little problems. when I ran code:

see result of my code: http://postimg.org/image/8c1ophkz9/

Using #pragma omp parallel for schedule(dynamic,1024) reduction(+:cont) Parallel time (0.763 seconds) (100% of similarity) Sequential time (0.702 seconds) (100% of similarity)

Using #pragma omp parallel for schedule(dynamic) reduction(+:cont) Parallel time (0.113 seconds) (66% of similarity) Sequential time (0.703 seconds) (100% of similarity)

Image1 is equal image2. I have many colision yet in my code =\\ i need reduce time in parallel code. i dont understand why o parallel code is more slow than sequential code =\\

Parallel code

#pragma omp parallel for schedule(dynamic) reduction(+:cont)
                for (int x = 0; x < width; x++){
                    for (int y = 0; y < height; y++){

                        Gdiplus::Color pixelColorImage01;
                        Gdiplus::Color pixelColorImage02;

                        myImage01->GetPixel(x, y, &pixelColorImage01);
                        myImage02->GetPixel(x, y, &pixelColorImage02);
                        cont += (pixelColorImage01.GetRed() == pixelColorImage02.GetRed() && pixelColorImage01.GetGreen() == pixelColorImage02.GetGreen() && pixelColorImage01.GetBlue() == pixelColorImage02.GetBlue());


                    }

                }

Sequential code

        for (int x = 0; x < width; x++){
        for (int y = 0; y < height; y++){

            Gdiplus::Color pixelColorImage01;
            Gdiplus::Color pixelColorImage02;

            myImage01->GetPixel(x, y, &pixelColorImage01);
            myImage02->GetPixel(x, y, &pixelColorImage02);
            cont += (pixelColorImage01.GetRed() == pixelColorImage02.GetRed() && pixelColorImage01.GetGreen() == pixelColorImage02.GetGreen() && pixelColorImage01.GetBlue() == pixelColorImage02.GetBlue());


        }

    }

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