problem with different results each run C++

General OpenMP discussion

problem with different results each run C++

Postby gdarmon » Fri Feb 28, 2014 4:01 am

This is an algorithm I'm trying to optimize. I was trying to use openMP without any success.

I know the code is long but most of it is params init

Please try to explain why I need to change any line of code. I want to learn. I also wish to understand if using openMP is the best way for matrix based algorithms.

Code: Select all

    void CAlgo::runAlgo()
    {
 
             
    }
Last edited by gdarmon on Sun Mar 23, 2014 1:26 pm, edited 1 time in total.
gdarmon
 
Posts: 8
Joined: Wed Feb 26, 2014 1:01 pm

Re: problem with different results each run C++

Postby MarkB » Fri Feb 28, 2014 4:59 am

For a start, you need to make the debug arrays shared instead of firstprivate. Otherwise, each thread gets a private copy, assigns to it and then it goes out of scope at the end of the parallel loop: the subsequent writes are of the original, uninitialised arrays.

Most likely you don't want to make everything firstprivate. Variables which are read-only inside the parallel loop, or arrays where different loop iterations assign to different parts of the array should be shared. Temporary variables which are assigned and then used in every iteration should be private.

You have lot of variables to deal with in your loop, and the consequences of getting the data-attribute scoping wrong are unpleasant (race conditions and hence non-deterministic behaviour). It may help to declare as many variables as possible inside the scope of the parallel loop: these will then be automatically private to each thread. Then you only have to deal with a (hopefully) much shorter list of variables which are in scope at the parallel for directive.
MarkB
 
Posts: 434
Joined: Thu Jan 08, 2009 10:12 am

Re: problem with different results each run C++

Postby gdarmon » Fri Mar 14, 2014 12:23 pm

Code: Select all
void Algo::runAlgo()
{
   
}


so this my refactored code i'm still having the same problem. different outputs
Last edited by gdarmon on Sun Mar 23, 2014 1:27 pm, edited 1 time in total.
gdarmon
 
Posts: 8
Joined: Wed Feb 26, 2014 1:01 pm

Re: problem with different results each run C++

Postby MarkB » Mon Mar 17, 2014 4:52 am

I'm suspicious of the way Ry is used. All the threads get the same initial value (m_RyInitial) and each thread then increments it at the end of the loop. This means the value of Ry for a given value of the parallel loop index will depend on the number of threads - is that what you intend?

Do you get the correct results if you run with one OpenMP thread?
MarkB
 
Posts: 434
Joined: Thu Jan 08, 2009 10:12 am

Re: problem with different results each run C++

Postby gdarmon » Wed Mar 19, 2014 2:04 am

Hey Mark,
yes i get same result it i set it to 1 thread.
i have done some research and i think like you that Ry is the criminal here. what i have done is changed the usage of Ry in the function
Code: Select all
SettingSigmaN(eta, Rx, Ry, RxSqr

to
Code: Select all
SettingSigmaN(eta, Rx, m_RyInitial + row, RxSqr


since both row and Ry where getting incremented in the end of each loops. and indeed my results where changed however the results are still not good.
gdarmon
 
Posts: 8
Joined: Wed Feb 26, 2014 1:01 pm

Re: problem with different results each run C++

Postby MarkB » Wed Mar 19, 2014 2:11 am

Do any of the functions called inside the parallel loop modify any implicitly shared variables (i.e. global variables, or local variables which are declared static)?
MarkB
 
Posts: 434
Joined: Thu Jan 08, 2009 10:12 am

Re: problem with different results each run C++

Postby gdarmon » Thu Mar 20, 2014 11:14 am

there are not static variables, however i'm using a lot of variables which are global to the class not specific to the function inside the class.
can you recommend of a way to debug? if i print stuff into files? is there a way which i will be able to figure out what is going on ?
gdarmon
 
Posts: 8
Joined: Wed Feb 26, 2014 1:01 pm

Re: problem with different results each run C++

Postby MarkB » Fri Mar 21, 2014 3:08 am

gdarmon wrote:however i'm using a lot of variables which are global to the class not specific to the function inside the class.
can you recommend of a way to debug?


It's probably reasonable to inspect the code first. You are looking for any global variables which are being modified by multiple threads inside the parallel loop. Read-only variables are not a problem, nor are arrays which are indexed by the parallel loop iterator.
MarkB
 
Posts: 434
Joined: Thu Jan 08, 2009 10:12 am


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot] and 15 guests