Starting from:
$35

$29

Project #3 Functional Decomposition Solution




Introduction




This project will use parallelism, not for speeding data compuYou will create a month-by-month simulation in which each agent of where it just has to look at the state of the world around it




You will also get to exercise your creativity by adding an add impacts the state of the other agents and is impacted by them.




Requirements




1.You are creating a month-by-month simulationTheof amountgrainthe-gr grows isfectedaf by the temperature, amount of precipitation, eat Theit. number of graindeer depends on the amount of grain




2.The "state" of the system consists of the following global







int
NowYear;
// 2019 -
2024
int
NowMonth;
// 0 - 11


float
NowPrecip;
// inches
of rain per month
float
NowTemp;
// temperature this month
float
NowHeight;
// grain height in inches
int
NowNumDeer;
// number
of deer in the current population



3.Your basic time step will be one month. Interesting parame







const float GRAIN_GROWS_PER_MONTH =
8.0;


const float ONE_DEER_EATS_PER_MONTH =
0.5;


const float AVG_PRECIP_PER_MONTH =
6.0;
// average
const float AMP_PRECIP_PER_MONTH =
6.0;
// plus or minus
const float RANDOM_PRECIP =
2.0;
// plus or minus noise
const float AVG_TEMP =
50.0;
// average
const float AMP_TEMP =
20.0;
// plus or minus



web.engr.oegonstate.edu/~mjb/cs575/Projects/proj03.html 1/7
29/04/2019 CS 475/575 Project #3




const float RANDOM_TEMP = 10.0; // plus or minus noise




const float MIDTEMP = 40.0;




const float MIDPRECIP = 10.0;




Units of grain growth are inches.




Units of temperature are degrees Fahrenheit (°F).

Units of precipitation are inches.




4.Because you know ahead of time how many threads you will n




parallel sections directive:




omp_set_num_threads( 4 ); // same as # of sections




#pragma omp parallel sections

{

#pragma omp section

{

GrainDeer( );

}




#pragma omp section




{

Grain( );

}




#pragma omp section




{

Watcher( );

}




#pragma omp section




{

MyAgent( ); // your own

}




}// implied barrier -- all functions must return in order // to allow any of them to get past here




5.The temperature and precipitation are a function of the pa







float ang = ( 30.*(float)NowMonth + 15. ) * ( M_PI / 180. );




float temp = AVG_TEMP - AMP_TEMP * cos( ang );




unsigned int seed = 0;

NowTemp = temp + Ranf( &seed, -RANDOM_TEMP, RANDOM_TEMP );




float precip = AVG_PRECIP_PER_MONTH + AMP_PRECIP_PER_MONTH * sin( ang ); NowPrecip = precip + Ranf( &seed, -RANDOM_PRECIP, RANDOM_PRECIP ); if( NowPrecip < 0. )




NowPrecip = 0.;




To keep this simple, a year consists ofThe12firstmonthsday of 30winteda to be JanuaryAsou1. can see, the temperature and precipitation with some randomness added.




6.Starting values are:







// starting date and time:




NowMonth = 0;

NowYear = 2019;




starting state (feel free to change this if you want): NowNumDeer = 1;
NowHeight = 1.;




web.engr.oegonstate.edu/~mjb/cs575/Projects/proj03.html 2/7
29/04/2019 CS 475/575 Project #3




7.In addition to this, you must add in someectlyotherorectlyindirphenomolscon the owthgr of the grain and/or theYourgraindeerchoice ofpopulationthisis.up




8.You are free to tweak the constants to make everything tur




UseofThreads




As shown here, you will spawn, wthenreeyouthreaaddsyour(fourown agent):










TheGrainGrowthandGrainDeerthreads will each compute the next grain deer based on the current set ofTheyglobalwil statecomputevariablestheseinto., loc variablesThey. both then willDoneComputinghitthebarrier.




At that point, both of those threads are done computing using thread should then copy the local variableAll3 threadsintothewillglobalthenverh DoneAssigningbarrier.




At this poinatcher,thethreadW will print the current set of global




count, and then use the new monthemperaturetocomputeandthePrecipitationnewT. GrainGrowthandGrainDeerthreads can't proceed because there is a ch global state variables before theyAll are3threadsdonebeingwillDonePrintingprintedthenhit.barrth.




After spawning the threads, the main parallelrogramshouldsectionstofinishwait. for t




Each thread should return when the year hits 2025 (giving us 6




Remember that this description is for the core part of the pro simulationThat. will involve another thread and some additional in




QuantityInteractions




The Carrying Capacity of the graindeer is the number of inches graindeer exceeds this value at the end of a month, decrease t graindeer is less than this value at the end of a month, incre




Each month you will need to figure out how much the grain grows GRAIN_GROWS_PER_MONTH. If conditions are not good, it won't.




You know how good conditions are by seeing how close you are t (°F) and precipitation (inches).emperatureDothisbyFactorcomputingand aa T



Note that there is a standard math function, exp( x ), to comp







float tempFactor = exp( -SQR( ( NowTemp - MIDTEMP ) / 10. ) );




float precipFactor = exp( -SQR( ( NowPrecip - MIDPRECIP ) / 10. ) );




I like squaring things with another function:







float




SQR( float x )

{

return x*x;

}




You thentempFactoruse andprecipFactor like this:







NowHeight += tempFactor * precipFactor * GRAIN_GROWS_PER_MONTH; NowHeight -= (float)NowNumDeer * ONE_DEER_EATS_PER_MONTH;




Be sure toNowHeightclamp against zero.




StructureoftheSimulation Functions




Each simulation function will have a structure that looks like







while( NowYear < 2025 )




{

compute a temporary next-value for this quantity
based on the current state of the simulation:
. . .




DoneComputing barrier: #pragma omp barrier
. . .




DoneAssigning barrier: #pragma omp barrier
. . .




DonePrinting barrier: #pragma omp barrier
. . .

}




DoingtheBarrierson VisualStudio




You will probably get this error when isualdoing Studio:thistype of proje




'#pragma omp barrioperly' imprnested in a work-sharing construct




The OpenMP specifications says: "All threads in a team must exe means that placing correspondingferentbarriersfunctionsin thedoes,butdifnotsomehowqualifyg are OK with it.

Here is a work-around. Instead of using the




#pragma omp barrier

line, use this:




W aitBarrier( );




You must InitBarrier(callas partn) of your setupn isprocess,thenumberwhereof threa waiting for at. thisPresumablybarrier3beforethisisyou add your own4 afterquantityyou an own quantity.




I'm not particularly proud of thisTocode,make butthisithappen,seems tofirstworkde.




global variables:




omp_lock_t Lock;




int NumInThreadTeam;

int NumAtBarrier;

int NumGone;




Here are the function prototypes:







void InitBarrier( int );




void WaitBarrier( );




Here is the function code:







specify how many threads will be in the barrier:



(also init's the Lock)



void




InitBarrier( int n )

{

NumInThreadTeam = n;

NumAtBarrier = 0;

omp_init_lock( &Lock );

}




// have the calling thread wait here until all the other threads catch up:




void




WaitBarrier( )

{

omp_set_lock( &Lock );

{

NumAtBarrier++;

if( NumAtBarrier == NumInThreadTeam )

{

NumGone = 0;

NumAtBarrier = 0;

let all other threads get back to what they were doing
before this one unlocks, knowing that they might immediately
call WaitBarrier( ) again:
while( NumGone != NumInThreadTeam-1 );

omp_unset_lock( &Lock );

return;

}

}

omp_unset_lock( &Lock );




while( NumAtBarrier != 0 );
//
this
waits
for
the nth thread to
arrive
#pragma omp atomic
//
this
flags
how
many threads have
returned
NumGone++;
}

Random Numbers




How you generate the randomnessAs isexampleupto (whichyou. you are free t




a couple of functions that return a random-given numberlowvaluebetweenand aa hius

the name overloading is a C++-ism, not a C-ism):




#include <stdlib.h




unsigned int seed = 0; // a thread-private variable float x = Ranf( &seed, -1.f, 1.f );




. . .




float




Ranf( unsigned int *seedp, float low, float high )

{

float r = (float) rand_r( seedp ); // 0 - RAND_MAX




return( low + r * ( high - low ) / (float)RAND_MAX );




}




int




Ranf( unsigned int *seedp, int ilow, int ihigh )

{

float low = (float)ilow;

float high = (float)ihigh + 0.9999f;




return (int)( Ranf(seedp, low,high) );




}




Results




Turn in your code and your PDF writeup. Be sure your PDF is a fileYour. writeup will consist of:







1.What your own-choice quantity was and how it fits into the




2.A table showing values for temperature, precipitation,heightofthenumg your own-choice quantity as a function. of month number

3.A graph showing temperature, precipitation,heightnumberoftheofgrain, choice quantity as a function.Note:of monthifyounumberchange the units quantities might fit better on the same set of axes.




cm = inches * 2.54




°C = (5./9.)*(°F-32)




This will make your hgerightsnumbershave andlar your temperatures have




4.A commentary about the patterns in the graph .Whatand whyevidencethey curves proves that your own fectingquantitytheis simactulationallyafcorrect




ExampleOutput




Grading:




Featur Points




Simulate grain growth and graindeer20 population






Simulate your own quantity
20












Table of Results


10
















Graph of Results


20
















Commentary


30
















Potentialotal T


100










































































































































web.engr.oegonstate.edu/~mjb/cs575/Projects/proj03.html 7/7

More products