[OMP] A newbie question abut assignments and comparissons

Alexander Todorov alexx.todorov at gmail.com
Tue Mar 28 11:58:37 PST 2006


Hello,
I have a parallel project developed with MPI and now I want to add
some OpenMP functionality to it. There are several for loops that can
be parallelised. The project is  a Monte Carlo simulation of baseball
games. Here is some code related to my questions (see below)
//-------------------------------------
	#pragma omp for

	for(int inning = 1; inning <= 9; inning++)

	{
		away->playInning();

		// check results from this inning

		if((inning == 9) && (home->getRuns() > away->getRuns()))	

			break;	//don't play last inning for home team

				//if they have already won the game

				//(i.e. home team has more runs and will

				//bat needlessly)

		else

		{

			//make sure they don't score any extra runs

			//(in the last half inning, you can only

			//score one more run than the other team

			//has -- all extra runs are not needed.
			if(inning == 9)
			{
// QUESTION : Do we need an atomic operation here ?
				#pragma omp atomic

				home->setMaxRuns(away->getRuns() + 1);
			}

			// next team turn

			home->playInning();

		}

	}
//---------------------- cut ---------
void Team::addRuns(int r)

{

	if((runs + r) <= maxRuns)
	{
		#pragma omp atomic

		runs += r;
	} else {
		#pragma omp atomic
		runs = maxRuns;
	}

}

void Team::setMaxRuns(int r)
{
       if(r > 0) maxRuns = r;
}
//-------------------------------------

The `Team::playInning` functions perform a random play for every team
and increase the `runs` variable. `Team::getRuns` function returns
this variable. `Team::setMaxRuns` assigns its parameter to another
variable that is checked in `Team::playInning()`

//-----------------------
void Team::playInning()

{

	string retroHit;		//retrosheet version of the play

	while(outs < 3)

	{

		//don't run any extra atbats if game has been won already

		//(runs can never be greater than maxRuns because addRuns

		//checks this)

		if(runs == maxRuns)

			break;
///------ cut here ----
}
//-------------------------

I am sure that `#pragma omp atomic` is needed when updating the `runs`
variable. Is this also needed before calling `Team::setMaxRuns` in the
`for(int inning = 1...` loop? Is `atomic` needed when comparing
variables (runs == maxRuns) ?

Where can I read more about this issue?

TIA.


More information about the Omp mailing list