Program takes longer in Parallel

hi, i made my mandlebrot parallel with OpenMP i used QT

here's .pro to make it work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#-------------------------------------------------
#
# Project created by QtCreator 2014-09-06T16:00:46
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Defualt
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    +=

QMAKE_CXXFLAGS += -fopenmp
LIBS += -fopenmp


here's the function that make's the image:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
void MainWindow::MakeImage()
{
    QTime myTimer;
    myTimer.start();
    #pragma omp parallel
    {
        for(unsigned y=0; y<ImageHeight; ++y)
        {
            double c_im = MaxIm - y*Im_factor;
            for(unsigned x=0; x<ImageWidth; ++x)
            {
                double c_re = MinRe + x*Re_factor;

                double Z_re = c_re, Z_im = c_im;

                unsigned NumIter=MaxIterations;

                bool isInside = true;
                for(unsigned n=0; n<MaxIterations; ++n)
                {
                    double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
                    if(Z_re2 + Z_im2 > 4)
                    {
                        isInside = false;
                        NumIter = n;
                        break;
                    }
                    Z_im = 2*Z_re*Z_im + c_im;
                    Z_re = Z_re2 - Z_im2 + c_re;
                }
                /*if(isInside)
                    result->setPixel(x,y,qRgb(200,0,0));
                else
                {
                    result->setPixel(x,y,qRgb(NumIter%256,0,255 * (NumIter < MaxIterations)));
                }*/

                if(isInside)
                        result->setPixel(x,y,qRgb(0, 0, 0)); // black
                    else
                    {
                        double z = sqrt(x * x + y * y);
                        int brightness = 256. * log2(1.75 + NumIter - log2(log2(z))) / log2(double(MaxIterations));
                        result->setPixel(x,y,qRgb(brightness, brightness, 255));
                    }

                //END
            }
        }
    }

    qDebug() << "Rendering took " << myTimer.elapsed() << "ms";
}


in system monitor ( i use ubuntu 14.10 lts 64-bit ) i see all my cores around 99~

with parallel it takes 950 940ms~ without parallel it takes 800ms !
Last edited on
What are you parallelizing? Isn't #pragma omp parallel in the wrong place?
i want to parallize the function that makes the image so its faster, where should i put it then ?! :o
Step back and think about what you want to run concurrently.

Clearly running the whole function concurrently won't help, so what parts of the function do you what to run concurrently?
well, im not sure what part is taking long ! i just wanted to speed up my app, (making the image render faster ! ) i assume its this part

:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
for(unsigned n=0; n<MaxIterations; ++n)
                {
                    double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
                    if(Z_re2 + Z_im2 > 4)
                    {
                        isInside = false;
                        NumIter = n;
                        break;
                    }
                    Z_im = 2*Z_re*Z_im + c_im;
                    Z_re = Z_re2 - Z_im2 + c_re;
                }
                /*if(isInside)
                    result->setPixel(x,y,qRgb(200,0,0));
                else
                {
                    result->setPixel(x,y,qRgb(NumIter%256,0,255 * (NumIter < MaxIterations)));
                }*/

                if(isInside)
                        result->setPixel(x,y,qRgb(0, 0, 0)); // black
                    else
                    {
                        double z = sqrt(x * x + y * y);
                        int brightness = 256. * log2(1.75 + NumIter - log2(log2(z))) / log2(double(MaxIterations));
                        result->setPixel(x,y,qRgb(brightness, brightness, 255));
                    }


maybe i should only make that part minus the part that it colors the Qimage !

wait, i tought i can make a single task to be done by 4cores so its faster, i can only run 4 diffrent tasks on 4 diffrent cores ? :(
Last edited on
You can run the same code on different threads, ideally operating on different data. You can't hand-off your code to some mysterious process that will make it run 4x faster. You have to specify how your code and data will get distributed among the threads.
so i should, like make it lets say core0 runs 1/4 of for core1 runs other 1/4 of for loop? how can i do this?! what should i consider to decide what part should what thread do ?! :o im really confused today, :)
i don't understand can you please tell me what i should do !?
Last edited on
I was trying to get you to realize that the code that needs to be run concurrently is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
bool isInside = true;
for (unsigned n=0; n<MaxIterations; ++n)
{
	double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
	if (Z_re2 + Z_im2 > 4)
	{
		isInside = false;
		NumIter = n;
		break;
	}
	Z_im = 2*Z_re*Z_im + c_im;
	Z_re = Z_re2 - Z_im2 + c_re;
}

if (isInside)
{
	result->setPixel(x,y,qRgb(0, 0, 0)); // black
}
else
{
	double z = sqrt(x * x + y * y);
	int brightness = 256. * log2(1.75 + NumIter - log2(log2(z))) / log2(double(MaxIterations));
	result->setPixel(x,y,qRgb(brightness, brightness, 255));
}


You just need to pass in the correct (x, y, z) as in:
1
2
3
4
5
6
7
8
for(unsigned y=0; y<ImageHeight; ++y)
{
	double c_im = MaxIm - y*Im_factor;
	for(unsigned x=0; x<ImageWidth; ++x)
	{
		double c_re = MinRe + x*Re_factor;
		double Z_re = c_re, Z_im = c_im;
		unsigned NumIter=MaxIterations;


Do you understand, or have I lost you?
im sorry if am annoying you by not understanding :(

i actually think i need to read a tutorial on OpenMP :|

and also as i understood that the part need to be paralleled is the first code you posted!

i will try to figure it out , thanks for helping me

it soudns stupid but i tought it will be simple as using #pragma parralel !

at first i knew its mroe complicated, but my firend told me its simple as that, and i started looking at examples,

let me read a tutorial first
Last edited on
Topic archived. No new replies allowed.