kmp_runtime.cpp 324 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183
  1. /*
  2. * kmp_runtime.cpp -- KPTS runtime support library
  3. */
  4. //===----------------------------------------------------------------------===//
  5. //
  6. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  7. // See https://llvm.org/LICENSE.txt for license information.
  8. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #include "kmp.h"
  12. #include "kmp_affinity.h"
  13. #include "kmp_atomic.h"
  14. #include "kmp_environment.h"
  15. #include "kmp_error.h"
  16. #include "kmp_i18n.h"
  17. #include "kmp_io.h"
  18. #include "kmp_itt.h"
  19. #include "kmp_settings.h"
  20. #include "kmp_stats.h"
  21. #include "kmp_str.h"
  22. #include "kmp_wait_release.h"
  23. #include "kmp_wrapper_getpid.h"
  24. #include "kmp_dispatch.h"
  25. #if KMP_USE_HIER_SCHED
  26. #error #include "kmp_dispatch_hier.h"
  27. #endif
  28. #if OMPT_SUPPORT
  29. #include "ompt-specific.h"
  30. #endif
  31. #if OMPD_SUPPORT
  32. #error #include "ompd-specific.h"
  33. #endif
  34. #if OMP_PROFILING_SUPPORT
  35. #error #include "llvm/Support/TimeProfiler.h"
  36. static char *ProfileTraceFile = nullptr;
  37. #endif
  38. /* these are temporary issues to be dealt with */
  39. #define KMP_USE_PRCTL 0
  40. #if KMP_OS_WINDOWS
  41. #include <process.h>
  42. #endif
  43. #if KMP_OS_WINDOWS
  44. // windows does not need include files as it doesn't use shared memory
  45. #else
  46. #include <sys/mman.h>
  47. #include <sys/stat.h>
  48. #include <fcntl.h>
  49. #define SHM_SIZE 1024
  50. #endif
  51. #if defined(KMP_GOMP_COMPAT)
  52. char const __kmp_version_alt_comp[] =
  53. KMP_VERSION_PREFIX "alternative compiler support: yes";
  54. #endif /* defined(KMP_GOMP_COMPAT) */
  55. char const __kmp_version_omp_api[] =
  56. KMP_VERSION_PREFIX "API version: 5.0 (201611)";
  57. #ifdef KMP_DEBUG
  58. char const __kmp_version_lock[] =
  59. KMP_VERSION_PREFIX "lock type: run time selectable";
  60. #endif /* KMP_DEBUG */
  61. #define KMP_MIN(x, y) ((x) < (y) ? (x) : (y))
  62. /* ------------------------------------------------------------------------ */
  63. #if KMP_USE_MONITOR
  64. kmp_info_t __kmp_monitor = {0};
  65. #endif
  66. /* Forward declarations */
  67. void __kmp_cleanup(void);
  68. static void __kmp_initialize_info(kmp_info_t *, kmp_team_t *, int tid,
  69. int gtid);
  70. static void __kmp_initialize_team(kmp_team_t *team, int new_nproc,
  71. kmp_internal_control_t *new_icvs,
  72. ident_t *loc);
  73. #if KMP_AFFINITY_SUPPORTED
  74. static void __kmp_partition_places(kmp_team_t *team,
  75. int update_master_only = 0);
  76. #endif
  77. static void __kmp_do_serial_initialize(void);
  78. void __kmp_fork_barrier(int gtid, int tid);
  79. void __kmp_join_barrier(int gtid);
  80. void __kmp_setup_icv_copy(kmp_team_t *team, int new_nproc,
  81. kmp_internal_control_t *new_icvs, ident_t *loc);
  82. #ifdef USE_LOAD_BALANCE
  83. static int __kmp_load_balance_nproc(kmp_root_t *root, int set_nproc);
  84. #endif
  85. static int __kmp_expand_threads(int nNeed);
  86. #if KMP_OS_WINDOWS
  87. static int __kmp_unregister_root_other_thread(int gtid);
  88. #endif
  89. static void __kmp_reap_thread(kmp_info_t *thread, int is_root);
  90. kmp_info_t *__kmp_thread_pool_insert_pt = NULL;
  91. void __kmp_resize_dist_barrier(kmp_team_t *team, int old_nthreads,
  92. int new_nthreads);
  93. void __kmp_add_threads_to_team(kmp_team_t *team, int new_nthreads);
  94. /* Calculate the identifier of the current thread */
  95. /* fast (and somewhat portable) way to get unique identifier of executing
  96. thread. Returns KMP_GTID_DNE if we haven't been assigned a gtid. */
  97. int __kmp_get_global_thread_id() {
  98. int i;
  99. kmp_info_t **other_threads;
  100. size_t stack_data;
  101. char *stack_addr;
  102. size_t stack_size;
  103. char *stack_base;
  104. KA_TRACE(
  105. 1000,
  106. ("*** __kmp_get_global_thread_id: entering, nproc=%d all_nproc=%d\n",
  107. __kmp_nth, __kmp_all_nth));
  108. /* JPH - to handle the case where __kmpc_end(0) is called immediately prior to
  109. a parallel region, made it return KMP_GTID_DNE to force serial_initialize
  110. by caller. Had to handle KMP_GTID_DNE at all call-sites, or else guarantee
  111. __kmp_init_gtid for this to work. */
  112. if (!TCR_4(__kmp_init_gtid))
  113. return KMP_GTID_DNE;
  114. #ifdef KMP_TDATA_GTID
  115. if (TCR_4(__kmp_gtid_mode) >= 3) {
  116. KA_TRACE(1000, ("*** __kmp_get_global_thread_id: using TDATA\n"));
  117. return __kmp_gtid;
  118. }
  119. #endif
  120. if (TCR_4(__kmp_gtid_mode) >= 2) {
  121. KA_TRACE(1000, ("*** __kmp_get_global_thread_id: using keyed TLS\n"));
  122. return __kmp_gtid_get_specific();
  123. }
  124. KA_TRACE(1000, ("*** __kmp_get_global_thread_id: using internal alg.\n"));
  125. stack_addr = (char *)&stack_data;
  126. other_threads = __kmp_threads;
  127. /* ATT: The code below is a source of potential bugs due to unsynchronized
  128. access to __kmp_threads array. For example:
  129. 1. Current thread loads other_threads[i] to thr and checks it, it is
  130. non-NULL.
  131. 2. Current thread is suspended by OS.
  132. 3. Another thread unregisters and finishes (debug versions of free()
  133. may fill memory with something like 0xEF).
  134. 4. Current thread is resumed.
  135. 5. Current thread reads junk from *thr.
  136. TODO: Fix it. --ln */
  137. for (i = 0; i < __kmp_threads_capacity; i++) {
  138. kmp_info_t *thr = (kmp_info_t *)TCR_SYNC_PTR(other_threads[i]);
  139. if (!thr)
  140. continue;
  141. stack_size = (size_t)TCR_PTR(thr->th.th_info.ds.ds_stacksize);
  142. stack_base = (char *)TCR_PTR(thr->th.th_info.ds.ds_stackbase);
  143. /* stack grows down -- search through all of the active threads */
  144. if (stack_addr <= stack_base) {
  145. size_t stack_diff = stack_base - stack_addr;
  146. if (stack_diff <= stack_size) {
  147. /* The only way we can be closer than the allocated */
  148. /* stack size is if we are running on this thread. */
  149. KMP_DEBUG_ASSERT(__kmp_gtid_get_specific() == i);
  150. return i;
  151. }
  152. }
  153. }
  154. /* get specific to try and determine our gtid */
  155. KA_TRACE(1000,
  156. ("*** __kmp_get_global_thread_id: internal alg. failed to find "
  157. "thread, using TLS\n"));
  158. i = __kmp_gtid_get_specific();
  159. /*fprintf( stderr, "=== %d\n", i ); */ /* GROO */
  160. /* if we havn't been assigned a gtid, then return code */
  161. if (i < 0)
  162. return i;
  163. /* dynamically updated stack window for uber threads to avoid get_specific
  164. call */
  165. if (!TCR_4(other_threads[i]->th.th_info.ds.ds_stackgrow)) {
  166. KMP_FATAL(StackOverflow, i);
  167. }
  168. stack_base = (char *)other_threads[i]->th.th_info.ds.ds_stackbase;
  169. if (stack_addr > stack_base) {
  170. TCW_PTR(other_threads[i]->th.th_info.ds.ds_stackbase, stack_addr);
  171. TCW_PTR(other_threads[i]->th.th_info.ds.ds_stacksize,
  172. other_threads[i]->th.th_info.ds.ds_stacksize + stack_addr -
  173. stack_base);
  174. } else {
  175. TCW_PTR(other_threads[i]->th.th_info.ds.ds_stacksize,
  176. stack_base - stack_addr);
  177. }
  178. /* Reprint stack bounds for ubermaster since they have been refined */
  179. if (__kmp_storage_map) {
  180. char *stack_end = (char *)other_threads[i]->th.th_info.ds.ds_stackbase;
  181. char *stack_beg = stack_end - other_threads[i]->th.th_info.ds.ds_stacksize;
  182. __kmp_print_storage_map_gtid(i, stack_beg, stack_end,
  183. other_threads[i]->th.th_info.ds.ds_stacksize,
  184. "th_%d stack (refinement)", i);
  185. }
  186. return i;
  187. }
  188. int __kmp_get_global_thread_id_reg() {
  189. int gtid;
  190. if (!__kmp_init_serial) {
  191. gtid = KMP_GTID_DNE;
  192. } else
  193. #ifdef KMP_TDATA_GTID
  194. if (TCR_4(__kmp_gtid_mode) >= 3) {
  195. KA_TRACE(1000, ("*** __kmp_get_global_thread_id_reg: using TDATA\n"));
  196. gtid = __kmp_gtid;
  197. } else
  198. #endif
  199. if (TCR_4(__kmp_gtid_mode) >= 2) {
  200. KA_TRACE(1000, ("*** __kmp_get_global_thread_id_reg: using keyed TLS\n"));
  201. gtid = __kmp_gtid_get_specific();
  202. } else {
  203. KA_TRACE(1000,
  204. ("*** __kmp_get_global_thread_id_reg: using internal alg.\n"));
  205. gtid = __kmp_get_global_thread_id();
  206. }
  207. /* we must be a new uber master sibling thread */
  208. if (gtid == KMP_GTID_DNE) {
  209. KA_TRACE(10,
  210. ("__kmp_get_global_thread_id_reg: Encountered new root thread. "
  211. "Registering a new gtid.\n"));
  212. __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
  213. if (!__kmp_init_serial) {
  214. __kmp_do_serial_initialize();
  215. gtid = __kmp_gtid_get_specific();
  216. } else {
  217. gtid = __kmp_register_root(FALSE);
  218. }
  219. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  220. /*__kmp_printf( "+++ %d\n", gtid ); */ /* GROO */
  221. }
  222. KMP_DEBUG_ASSERT(gtid >= 0);
  223. return gtid;
  224. }
  225. /* caller must hold forkjoin_lock */
  226. void __kmp_check_stack_overlap(kmp_info_t *th) {
  227. int f;
  228. char *stack_beg = NULL;
  229. char *stack_end = NULL;
  230. int gtid;
  231. KA_TRACE(10, ("__kmp_check_stack_overlap: called\n"));
  232. if (__kmp_storage_map) {
  233. stack_end = (char *)th->th.th_info.ds.ds_stackbase;
  234. stack_beg = stack_end - th->th.th_info.ds.ds_stacksize;
  235. gtid = __kmp_gtid_from_thread(th);
  236. if (gtid == KMP_GTID_MONITOR) {
  237. __kmp_print_storage_map_gtid(
  238. gtid, stack_beg, stack_end, th->th.th_info.ds.ds_stacksize,
  239. "th_%s stack (%s)", "mon",
  240. (th->th.th_info.ds.ds_stackgrow) ? "initial" : "actual");
  241. } else {
  242. __kmp_print_storage_map_gtid(
  243. gtid, stack_beg, stack_end, th->th.th_info.ds.ds_stacksize,
  244. "th_%d stack (%s)", gtid,
  245. (th->th.th_info.ds.ds_stackgrow) ? "initial" : "actual");
  246. }
  247. }
  248. /* No point in checking ubermaster threads since they use refinement and
  249. * cannot overlap */
  250. gtid = __kmp_gtid_from_thread(th);
  251. if (__kmp_env_checks == TRUE && !KMP_UBER_GTID(gtid)) {
  252. KA_TRACE(10,
  253. ("__kmp_check_stack_overlap: performing extensive checking\n"));
  254. if (stack_beg == NULL) {
  255. stack_end = (char *)th->th.th_info.ds.ds_stackbase;
  256. stack_beg = stack_end - th->th.th_info.ds.ds_stacksize;
  257. }
  258. for (f = 0; f < __kmp_threads_capacity; f++) {
  259. kmp_info_t *f_th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[f]);
  260. if (f_th && f_th != th) {
  261. char *other_stack_end =
  262. (char *)TCR_PTR(f_th->th.th_info.ds.ds_stackbase);
  263. char *other_stack_beg =
  264. other_stack_end - (size_t)TCR_PTR(f_th->th.th_info.ds.ds_stacksize);
  265. if ((stack_beg > other_stack_beg && stack_beg < other_stack_end) ||
  266. (stack_end > other_stack_beg && stack_end < other_stack_end)) {
  267. /* Print the other stack values before the abort */
  268. if (__kmp_storage_map)
  269. __kmp_print_storage_map_gtid(
  270. -1, other_stack_beg, other_stack_end,
  271. (size_t)TCR_PTR(f_th->th.th_info.ds.ds_stacksize),
  272. "th_%d stack (overlapped)", __kmp_gtid_from_thread(f_th));
  273. __kmp_fatal(KMP_MSG(StackOverlap), KMP_HNT(ChangeStackLimit),
  274. __kmp_msg_null);
  275. }
  276. }
  277. }
  278. }
  279. KA_TRACE(10, ("__kmp_check_stack_overlap: returning\n"));
  280. }
  281. /* ------------------------------------------------------------------------ */
  282. void __kmp_infinite_loop(void) {
  283. static int done = FALSE;
  284. while (!done) {
  285. KMP_YIELD(TRUE);
  286. }
  287. }
  288. #define MAX_MESSAGE 512
  289. void __kmp_print_storage_map_gtid(int gtid, void *p1, void *p2, size_t size,
  290. char const *format, ...) {
  291. char buffer[MAX_MESSAGE];
  292. va_list ap;
  293. va_start(ap, format);
  294. KMP_SNPRINTF(buffer, sizeof(buffer), "OMP storage map: %p %p%8lu %s\n", p1,
  295. p2, (unsigned long)size, format);
  296. __kmp_acquire_bootstrap_lock(&__kmp_stdio_lock);
  297. __kmp_vprintf(kmp_err, buffer, ap);
  298. #if KMP_PRINT_DATA_PLACEMENT
  299. int node;
  300. if (gtid >= 0) {
  301. if (p1 <= p2 && (char *)p2 - (char *)p1 == size) {
  302. if (__kmp_storage_map_verbose) {
  303. node = __kmp_get_host_node(p1);
  304. if (node < 0) /* doesn't work, so don't try this next time */
  305. __kmp_storage_map_verbose = FALSE;
  306. else {
  307. char *last;
  308. int lastNode;
  309. int localProc = __kmp_get_cpu_from_gtid(gtid);
  310. const int page_size = KMP_GET_PAGE_SIZE();
  311. p1 = (void *)((size_t)p1 & ~((size_t)page_size - 1));
  312. p2 = (void *)(((size_t)p2 - 1) & ~((size_t)page_size - 1));
  313. if (localProc >= 0)
  314. __kmp_printf_no_lock(" GTID %d localNode %d\n", gtid,
  315. localProc >> 1);
  316. else
  317. __kmp_printf_no_lock(" GTID %d\n", gtid);
  318. #if KMP_USE_PRCTL
  319. /* The more elaborate format is disabled for now because of the prctl
  320. * hanging bug. */
  321. do {
  322. last = p1;
  323. lastNode = node;
  324. /* This loop collates adjacent pages with the same host node. */
  325. do {
  326. (char *)p1 += page_size;
  327. } while (p1 <= p2 && (node = __kmp_get_host_node(p1)) == lastNode);
  328. __kmp_printf_no_lock(" %p-%p memNode %d\n", last, (char *)p1 - 1,
  329. lastNode);
  330. } while (p1 <= p2);
  331. #else
  332. __kmp_printf_no_lock(" %p-%p memNode %d\n", p1,
  333. (char *)p1 + (page_size - 1),
  334. __kmp_get_host_node(p1));
  335. if (p1 < p2) {
  336. __kmp_printf_no_lock(" %p-%p memNode %d\n", p2,
  337. (char *)p2 + (page_size - 1),
  338. __kmp_get_host_node(p2));
  339. }
  340. #endif
  341. }
  342. }
  343. } else
  344. __kmp_printf_no_lock(" %s\n", KMP_I18N_STR(StorageMapWarning));
  345. }
  346. #endif /* KMP_PRINT_DATA_PLACEMENT */
  347. __kmp_release_bootstrap_lock(&__kmp_stdio_lock);
  348. }
  349. void __kmp_warn(char const *format, ...) {
  350. char buffer[MAX_MESSAGE];
  351. va_list ap;
  352. if (__kmp_generate_warnings == kmp_warnings_off) {
  353. return;
  354. }
  355. va_start(ap, format);
  356. KMP_SNPRINTF(buffer, sizeof(buffer), "OMP warning: %s\n", format);
  357. __kmp_acquire_bootstrap_lock(&__kmp_stdio_lock);
  358. __kmp_vprintf(kmp_err, buffer, ap);
  359. __kmp_release_bootstrap_lock(&__kmp_stdio_lock);
  360. va_end(ap);
  361. }
  362. void __kmp_abort_process() {
  363. // Later threads may stall here, but that's ok because abort() will kill them.
  364. __kmp_acquire_bootstrap_lock(&__kmp_exit_lock);
  365. if (__kmp_debug_buf) {
  366. __kmp_dump_debug_buffer();
  367. }
  368. if (KMP_OS_WINDOWS) {
  369. // Let other threads know of abnormal termination and prevent deadlock
  370. // if abort happened during library initialization or shutdown
  371. __kmp_global.g.g_abort = SIGABRT;
  372. /* On Windows* OS by default abort() causes pop-up error box, which stalls
  373. nightly testing. Unfortunately, we cannot reliably suppress pop-up error
  374. boxes. _set_abort_behavior() works well, but this function is not
  375. available in VS7 (this is not problem for DLL, but it is a problem for
  376. static OpenMP RTL). SetErrorMode (and so, timelimit utility) does not
  377. help, at least in some versions of MS C RTL.
  378. It seems following sequence is the only way to simulate abort() and
  379. avoid pop-up error box. */
  380. raise(SIGABRT);
  381. _exit(3); // Just in case, if signal ignored, exit anyway.
  382. } else {
  383. __kmp_unregister_library();
  384. abort();
  385. }
  386. __kmp_infinite_loop();
  387. __kmp_release_bootstrap_lock(&__kmp_exit_lock);
  388. } // __kmp_abort_process
  389. void __kmp_abort_thread(void) {
  390. // TODO: Eliminate g_abort global variable and this function.
  391. // In case of abort just call abort(), it will kill all the threads.
  392. __kmp_infinite_loop();
  393. } // __kmp_abort_thread
  394. /* Print out the storage map for the major kmp_info_t thread data structures
  395. that are allocated together. */
  396. static void __kmp_print_thread_storage_map(kmp_info_t *thr, int gtid) {
  397. __kmp_print_storage_map_gtid(gtid, thr, thr + 1, sizeof(kmp_info_t), "th_%d",
  398. gtid);
  399. __kmp_print_storage_map_gtid(gtid, &thr->th.th_info, &thr->th.th_team,
  400. sizeof(kmp_desc_t), "th_%d.th_info", gtid);
  401. __kmp_print_storage_map_gtid(gtid, &thr->th.th_local, &thr->th.th_pri_head,
  402. sizeof(kmp_local_t), "th_%d.th_local", gtid);
  403. __kmp_print_storage_map_gtid(
  404. gtid, &thr->th.th_bar[0], &thr->th.th_bar[bs_last_barrier],
  405. sizeof(kmp_balign_t) * bs_last_barrier, "th_%d.th_bar", gtid);
  406. __kmp_print_storage_map_gtid(gtid, &thr->th.th_bar[bs_plain_barrier],
  407. &thr->th.th_bar[bs_plain_barrier + 1],
  408. sizeof(kmp_balign_t), "th_%d.th_bar[plain]",
  409. gtid);
  410. __kmp_print_storage_map_gtid(gtid, &thr->th.th_bar[bs_forkjoin_barrier],
  411. &thr->th.th_bar[bs_forkjoin_barrier + 1],
  412. sizeof(kmp_balign_t), "th_%d.th_bar[forkjoin]",
  413. gtid);
  414. #if KMP_FAST_REDUCTION_BARRIER
  415. __kmp_print_storage_map_gtid(gtid, &thr->th.th_bar[bs_reduction_barrier],
  416. &thr->th.th_bar[bs_reduction_barrier + 1],
  417. sizeof(kmp_balign_t), "th_%d.th_bar[reduction]",
  418. gtid);
  419. #endif // KMP_FAST_REDUCTION_BARRIER
  420. }
  421. /* Print out the storage map for the major kmp_team_t team data structures
  422. that are allocated together. */
  423. static void __kmp_print_team_storage_map(const char *header, kmp_team_t *team,
  424. int team_id, int num_thr) {
  425. int num_disp_buff = team->t.t_max_nproc > 1 ? __kmp_dispatch_num_buffers : 2;
  426. __kmp_print_storage_map_gtid(-1, team, team + 1, sizeof(kmp_team_t), "%s_%d",
  427. header, team_id);
  428. __kmp_print_storage_map_gtid(-1, &team->t.t_bar[0],
  429. &team->t.t_bar[bs_last_barrier],
  430. sizeof(kmp_balign_team_t) * bs_last_barrier,
  431. "%s_%d.t_bar", header, team_id);
  432. __kmp_print_storage_map_gtid(-1, &team->t.t_bar[bs_plain_barrier],
  433. &team->t.t_bar[bs_plain_barrier + 1],
  434. sizeof(kmp_balign_team_t), "%s_%d.t_bar[plain]",
  435. header, team_id);
  436. __kmp_print_storage_map_gtid(-1, &team->t.t_bar[bs_forkjoin_barrier],
  437. &team->t.t_bar[bs_forkjoin_barrier + 1],
  438. sizeof(kmp_balign_team_t),
  439. "%s_%d.t_bar[forkjoin]", header, team_id);
  440. #if KMP_FAST_REDUCTION_BARRIER
  441. __kmp_print_storage_map_gtid(-1, &team->t.t_bar[bs_reduction_barrier],
  442. &team->t.t_bar[bs_reduction_barrier + 1],
  443. sizeof(kmp_balign_team_t),
  444. "%s_%d.t_bar[reduction]", header, team_id);
  445. #endif // KMP_FAST_REDUCTION_BARRIER
  446. __kmp_print_storage_map_gtid(
  447. -1, &team->t.t_dispatch[0], &team->t.t_dispatch[num_thr],
  448. sizeof(kmp_disp_t) * num_thr, "%s_%d.t_dispatch", header, team_id);
  449. __kmp_print_storage_map_gtid(
  450. -1, &team->t.t_threads[0], &team->t.t_threads[num_thr],
  451. sizeof(kmp_info_t *) * num_thr, "%s_%d.t_threads", header, team_id);
  452. __kmp_print_storage_map_gtid(-1, &team->t.t_disp_buffer[0],
  453. &team->t.t_disp_buffer[num_disp_buff],
  454. sizeof(dispatch_shared_info_t) * num_disp_buff,
  455. "%s_%d.t_disp_buffer", header, team_id);
  456. }
  457. static void __kmp_init_allocator() {
  458. __kmp_init_memkind();
  459. __kmp_init_target_mem();
  460. }
  461. static void __kmp_fini_allocator() { __kmp_fini_memkind(); }
  462. /* ------------------------------------------------------------------------ */
  463. #if KMP_DYNAMIC_LIB
  464. #if KMP_OS_WINDOWS
  465. BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpReserved) {
  466. //__kmp_acquire_bootstrap_lock( &__kmp_initz_lock );
  467. switch (fdwReason) {
  468. case DLL_PROCESS_ATTACH:
  469. KA_TRACE(10, ("DllMain: PROCESS_ATTACH\n"));
  470. return TRUE;
  471. case DLL_PROCESS_DETACH:
  472. KA_TRACE(10, ("DllMain: PROCESS_DETACH T#%d\n", __kmp_gtid_get_specific()));
  473. // According to Windows* documentation for DllMain entry point:
  474. // for DLL_PROCESS_DETACH, lpReserved is used for telling the difference:
  475. // lpReserved == NULL when FreeLibrary() is called,
  476. // lpReserved != NULL when the process is terminated.
  477. // When FreeLibrary() is called, worker threads remain alive. So the
  478. // runtime's state is consistent and executing proper shutdown is OK.
  479. // When the process is terminated, worker threads have exited or been
  480. // forcefully terminated by the OS and only the shutdown thread remains.
  481. // This can leave the runtime in an inconsistent state.
  482. // Hence, only attempt proper cleanup when FreeLibrary() is called.
  483. // Otherwise, rely on OS to reclaim resources.
  484. if (lpReserved == NULL)
  485. __kmp_internal_end_library(__kmp_gtid_get_specific());
  486. return TRUE;
  487. case DLL_THREAD_ATTACH:
  488. KA_TRACE(10, ("DllMain: THREAD_ATTACH\n"));
  489. /* if we want to register new siblings all the time here call
  490. * __kmp_get_gtid(); */
  491. return TRUE;
  492. case DLL_THREAD_DETACH:
  493. KA_TRACE(10, ("DllMain: THREAD_DETACH T#%d\n", __kmp_gtid_get_specific()));
  494. __kmp_internal_end_thread(__kmp_gtid_get_specific());
  495. return TRUE;
  496. }
  497. return TRUE;
  498. }
  499. #endif /* KMP_OS_WINDOWS */
  500. #endif /* KMP_DYNAMIC_LIB */
  501. /* __kmp_parallel_deo -- Wait until it's our turn. */
  502. void __kmp_parallel_deo(int *gtid_ref, int *cid_ref, ident_t *loc_ref) {
  503. int gtid = *gtid_ref;
  504. #ifdef BUILD_PARALLEL_ORDERED
  505. kmp_team_t *team = __kmp_team_from_gtid(gtid);
  506. #endif /* BUILD_PARALLEL_ORDERED */
  507. if (__kmp_env_consistency_check) {
  508. if (__kmp_threads[gtid]->th.th_root->r.r_active)
  509. #if KMP_USE_DYNAMIC_LOCK
  510. __kmp_push_sync(gtid, ct_ordered_in_parallel, loc_ref, NULL, 0);
  511. #else
  512. __kmp_push_sync(gtid, ct_ordered_in_parallel, loc_ref, NULL);
  513. #endif
  514. }
  515. #ifdef BUILD_PARALLEL_ORDERED
  516. if (!team->t.t_serialized) {
  517. KMP_MB();
  518. KMP_WAIT(&team->t.t_ordered.dt.t_value, __kmp_tid_from_gtid(gtid), KMP_EQ,
  519. NULL);
  520. KMP_MB();
  521. }
  522. #endif /* BUILD_PARALLEL_ORDERED */
  523. }
  524. /* __kmp_parallel_dxo -- Signal the next task. */
  525. void __kmp_parallel_dxo(int *gtid_ref, int *cid_ref, ident_t *loc_ref) {
  526. int gtid = *gtid_ref;
  527. #ifdef BUILD_PARALLEL_ORDERED
  528. int tid = __kmp_tid_from_gtid(gtid);
  529. kmp_team_t *team = __kmp_team_from_gtid(gtid);
  530. #endif /* BUILD_PARALLEL_ORDERED */
  531. if (__kmp_env_consistency_check) {
  532. if (__kmp_threads[gtid]->th.th_root->r.r_active)
  533. __kmp_pop_sync(gtid, ct_ordered_in_parallel, loc_ref);
  534. }
  535. #ifdef BUILD_PARALLEL_ORDERED
  536. if (!team->t.t_serialized) {
  537. KMP_MB(); /* Flush all pending memory write invalidates. */
  538. /* use the tid of the next thread in this team */
  539. /* TODO replace with general release procedure */
  540. team->t.t_ordered.dt.t_value = ((tid + 1) % team->t.t_nproc);
  541. KMP_MB(); /* Flush all pending memory write invalidates. */
  542. }
  543. #endif /* BUILD_PARALLEL_ORDERED */
  544. }
  545. /* ------------------------------------------------------------------------ */
  546. /* The BARRIER for a SINGLE process section is always explicit */
  547. int __kmp_enter_single(int gtid, ident_t *id_ref, int push_ws) {
  548. int status;
  549. kmp_info_t *th;
  550. kmp_team_t *team;
  551. if (!TCR_4(__kmp_init_parallel))
  552. __kmp_parallel_initialize();
  553. __kmp_resume_if_soft_paused();
  554. th = __kmp_threads[gtid];
  555. team = th->th.th_team;
  556. status = 0;
  557. th->th.th_ident = id_ref;
  558. if (team->t.t_serialized) {
  559. status = 1;
  560. } else {
  561. kmp_int32 old_this = th->th.th_local.this_construct;
  562. ++th->th.th_local.this_construct;
  563. /* try to set team count to thread count--success means thread got the
  564. single block */
  565. /* TODO: Should this be acquire or release? */
  566. if (team->t.t_construct == old_this) {
  567. status = __kmp_atomic_compare_store_acq(&team->t.t_construct, old_this,
  568. th->th.th_local.this_construct);
  569. }
  570. #if USE_ITT_BUILD
  571. if (__itt_metadata_add_ptr && __kmp_forkjoin_frames_mode == 3 &&
  572. KMP_MASTER_GTID(gtid) && th->th.th_teams_microtask == NULL &&
  573. team->t.t_active_level == 1) {
  574. // Only report metadata by primary thread of active team at level 1
  575. __kmp_itt_metadata_single(id_ref);
  576. }
  577. #endif /* USE_ITT_BUILD */
  578. }
  579. if (__kmp_env_consistency_check) {
  580. if (status && push_ws) {
  581. __kmp_push_workshare(gtid, ct_psingle, id_ref);
  582. } else {
  583. __kmp_check_workshare(gtid, ct_psingle, id_ref);
  584. }
  585. }
  586. #if USE_ITT_BUILD
  587. if (status) {
  588. __kmp_itt_single_start(gtid);
  589. }
  590. #endif /* USE_ITT_BUILD */
  591. return status;
  592. }
  593. void __kmp_exit_single(int gtid) {
  594. #if USE_ITT_BUILD
  595. __kmp_itt_single_end(gtid);
  596. #endif /* USE_ITT_BUILD */
  597. if (__kmp_env_consistency_check)
  598. __kmp_pop_workshare(gtid, ct_psingle, NULL);
  599. }
  600. /* determine if we can go parallel or must use a serialized parallel region and
  601. * how many threads we can use
  602. * set_nproc is the number of threads requested for the team
  603. * returns 0 if we should serialize or only use one thread,
  604. * otherwise the number of threads to use
  605. * The forkjoin lock is held by the caller. */
  606. static int __kmp_reserve_threads(kmp_root_t *root, kmp_team_t *parent_team,
  607. int master_tid, int set_nthreads,
  608. int enter_teams) {
  609. int capacity;
  610. int new_nthreads;
  611. KMP_DEBUG_ASSERT(__kmp_init_serial);
  612. KMP_DEBUG_ASSERT(root && parent_team);
  613. kmp_info_t *this_thr = parent_team->t.t_threads[master_tid];
  614. // If dyn-var is set, dynamically adjust the number of desired threads,
  615. // according to the method specified by dynamic_mode.
  616. new_nthreads = set_nthreads;
  617. if (!get__dynamic_2(parent_team, master_tid)) {
  618. ;
  619. }
  620. #ifdef USE_LOAD_BALANCE
  621. else if (__kmp_global.g.g_dynamic_mode == dynamic_load_balance) {
  622. new_nthreads = __kmp_load_balance_nproc(root, set_nthreads);
  623. if (new_nthreads == 1) {
  624. KC_TRACE(10, ("__kmp_reserve_threads: T#%d load balance reduced "
  625. "reservation to 1 thread\n",
  626. master_tid));
  627. return 1;
  628. }
  629. if (new_nthreads < set_nthreads) {
  630. KC_TRACE(10, ("__kmp_reserve_threads: T#%d load balance reduced "
  631. "reservation to %d threads\n",
  632. master_tid, new_nthreads));
  633. }
  634. }
  635. #endif /* USE_LOAD_BALANCE */
  636. else if (__kmp_global.g.g_dynamic_mode == dynamic_thread_limit) {
  637. new_nthreads = __kmp_avail_proc - __kmp_nth +
  638. (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc);
  639. if (new_nthreads <= 1) {
  640. KC_TRACE(10, ("__kmp_reserve_threads: T#%d thread limit reduced "
  641. "reservation to 1 thread\n",
  642. master_tid));
  643. return 1;
  644. }
  645. if (new_nthreads < set_nthreads) {
  646. KC_TRACE(10, ("__kmp_reserve_threads: T#%d thread limit reduced "
  647. "reservation to %d threads\n",
  648. master_tid, new_nthreads));
  649. } else {
  650. new_nthreads = set_nthreads;
  651. }
  652. } else if (__kmp_global.g.g_dynamic_mode == dynamic_random) {
  653. if (set_nthreads > 2) {
  654. new_nthreads = __kmp_get_random(parent_team->t.t_threads[master_tid]);
  655. new_nthreads = (new_nthreads % set_nthreads) + 1;
  656. if (new_nthreads == 1) {
  657. KC_TRACE(10, ("__kmp_reserve_threads: T#%d dynamic random reduced "
  658. "reservation to 1 thread\n",
  659. master_tid));
  660. return 1;
  661. }
  662. if (new_nthreads < set_nthreads) {
  663. KC_TRACE(10, ("__kmp_reserve_threads: T#%d dynamic random reduced "
  664. "reservation to %d threads\n",
  665. master_tid, new_nthreads));
  666. }
  667. }
  668. } else {
  669. KMP_ASSERT(0);
  670. }
  671. // Respect KMP_ALL_THREADS/KMP_DEVICE_THREAD_LIMIT.
  672. if (__kmp_nth + new_nthreads -
  673. (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) >
  674. __kmp_max_nth) {
  675. int tl_nthreads = __kmp_max_nth - __kmp_nth +
  676. (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc);
  677. if (tl_nthreads <= 0) {
  678. tl_nthreads = 1;
  679. }
  680. // If dyn-var is false, emit a 1-time warning.
  681. if (!get__dynamic_2(parent_team, master_tid) && (!__kmp_reserve_warn)) {
  682. __kmp_reserve_warn = 1;
  683. __kmp_msg(kmp_ms_warning,
  684. KMP_MSG(CantFormThrTeam, set_nthreads, tl_nthreads),
  685. KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null);
  686. }
  687. if (tl_nthreads == 1) {
  688. KC_TRACE(10, ("__kmp_reserve_threads: T#%d KMP_DEVICE_THREAD_LIMIT "
  689. "reduced reservation to 1 thread\n",
  690. master_tid));
  691. return 1;
  692. }
  693. KC_TRACE(10, ("__kmp_reserve_threads: T#%d KMP_DEVICE_THREAD_LIMIT reduced "
  694. "reservation to %d threads\n",
  695. master_tid, tl_nthreads));
  696. new_nthreads = tl_nthreads;
  697. }
  698. // Respect OMP_THREAD_LIMIT
  699. int cg_nthreads = this_thr->th.th_cg_roots->cg_nthreads;
  700. int max_cg_threads = this_thr->th.th_cg_roots->cg_thread_limit;
  701. if (cg_nthreads + new_nthreads -
  702. (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) >
  703. max_cg_threads) {
  704. int tl_nthreads = max_cg_threads - cg_nthreads +
  705. (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc);
  706. if (tl_nthreads <= 0) {
  707. tl_nthreads = 1;
  708. }
  709. // If dyn-var is false, emit a 1-time warning.
  710. if (!get__dynamic_2(parent_team, master_tid) && (!__kmp_reserve_warn)) {
  711. __kmp_reserve_warn = 1;
  712. __kmp_msg(kmp_ms_warning,
  713. KMP_MSG(CantFormThrTeam, set_nthreads, tl_nthreads),
  714. KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null);
  715. }
  716. if (tl_nthreads == 1) {
  717. KC_TRACE(10, ("__kmp_reserve_threads: T#%d OMP_THREAD_LIMIT "
  718. "reduced reservation to 1 thread\n",
  719. master_tid));
  720. return 1;
  721. }
  722. KC_TRACE(10, ("__kmp_reserve_threads: T#%d OMP_THREAD_LIMIT reduced "
  723. "reservation to %d threads\n",
  724. master_tid, tl_nthreads));
  725. new_nthreads = tl_nthreads;
  726. }
  727. // Check if the threads array is large enough, or needs expanding.
  728. // See comment in __kmp_register_root() about the adjustment if
  729. // __kmp_threads[0] == NULL.
  730. capacity = __kmp_threads_capacity;
  731. if (TCR_PTR(__kmp_threads[0]) == NULL) {
  732. --capacity;
  733. }
  734. // If it is not for initializing the hidden helper team, we need to take
  735. // __kmp_hidden_helper_threads_num out of the capacity because it is included
  736. // in __kmp_threads_capacity.
  737. if (__kmp_enable_hidden_helper && !TCR_4(__kmp_init_hidden_helper_threads)) {
  738. capacity -= __kmp_hidden_helper_threads_num;
  739. }
  740. if (__kmp_nth + new_nthreads -
  741. (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) >
  742. capacity) {
  743. // Expand the threads array.
  744. int slotsRequired = __kmp_nth + new_nthreads -
  745. (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) -
  746. capacity;
  747. int slotsAdded = __kmp_expand_threads(slotsRequired);
  748. if (slotsAdded < slotsRequired) {
  749. // The threads array was not expanded enough.
  750. new_nthreads -= (slotsRequired - slotsAdded);
  751. KMP_ASSERT(new_nthreads >= 1);
  752. // If dyn-var is false, emit a 1-time warning.
  753. if (!get__dynamic_2(parent_team, master_tid) && (!__kmp_reserve_warn)) {
  754. __kmp_reserve_warn = 1;
  755. if (__kmp_tp_cached) {
  756. __kmp_msg(kmp_ms_warning,
  757. KMP_MSG(CantFormThrTeam, set_nthreads, new_nthreads),
  758. KMP_HNT(Set_ALL_THREADPRIVATE, __kmp_tp_capacity),
  759. KMP_HNT(PossibleSystemLimitOnThreads), __kmp_msg_null);
  760. } else {
  761. __kmp_msg(kmp_ms_warning,
  762. KMP_MSG(CantFormThrTeam, set_nthreads, new_nthreads),
  763. KMP_HNT(SystemLimitOnThreads), __kmp_msg_null);
  764. }
  765. }
  766. }
  767. }
  768. #ifdef KMP_DEBUG
  769. if (new_nthreads == 1) {
  770. KC_TRACE(10,
  771. ("__kmp_reserve_threads: T#%d serializing team after reclaiming "
  772. "dead roots and rechecking; requested %d threads\n",
  773. __kmp_get_gtid(), set_nthreads));
  774. } else {
  775. KC_TRACE(10, ("__kmp_reserve_threads: T#%d allocating %d threads; requested"
  776. " %d threads\n",
  777. __kmp_get_gtid(), new_nthreads, set_nthreads));
  778. }
  779. #endif // KMP_DEBUG
  780. return new_nthreads;
  781. }
  782. /* Allocate threads from the thread pool and assign them to the new team. We are
  783. assured that there are enough threads available, because we checked on that
  784. earlier within critical section forkjoin */
  785. static void __kmp_fork_team_threads(kmp_root_t *root, kmp_team_t *team,
  786. kmp_info_t *master_th, int master_gtid,
  787. int fork_teams_workers) {
  788. int i;
  789. int use_hot_team;
  790. KA_TRACE(10, ("__kmp_fork_team_threads: new_nprocs = %d\n", team->t.t_nproc));
  791. KMP_DEBUG_ASSERT(master_gtid == __kmp_get_gtid());
  792. KMP_MB();
  793. /* first, let's setup the primary thread */
  794. master_th->th.th_info.ds.ds_tid = 0;
  795. master_th->th.th_team = team;
  796. master_th->th.th_team_nproc = team->t.t_nproc;
  797. master_th->th.th_team_master = master_th;
  798. master_th->th.th_team_serialized = FALSE;
  799. master_th->th.th_dispatch = &team->t.t_dispatch[0];
  800. /* make sure we are not the optimized hot team */
  801. #if KMP_NESTED_HOT_TEAMS
  802. use_hot_team = 0;
  803. kmp_hot_team_ptr_t *hot_teams = master_th->th.th_hot_teams;
  804. if (hot_teams) { // hot teams array is not allocated if
  805. // KMP_HOT_TEAMS_MAX_LEVEL=0
  806. int level = team->t.t_active_level - 1; // index in array of hot teams
  807. if (master_th->th.th_teams_microtask) { // are we inside the teams?
  808. if (master_th->th.th_teams_size.nteams > 1) {
  809. ++level; // level was not increased in teams construct for
  810. // team_of_masters
  811. }
  812. if (team->t.t_pkfn != (microtask_t)__kmp_teams_master &&
  813. master_th->th.th_teams_level == team->t.t_level) {
  814. ++level; // level was not increased in teams construct for
  815. // team_of_workers before the parallel
  816. } // team->t.t_level will be increased inside parallel
  817. }
  818. if (level < __kmp_hot_teams_max_level) {
  819. if (hot_teams[level].hot_team) {
  820. // hot team has already been allocated for given level
  821. KMP_DEBUG_ASSERT(hot_teams[level].hot_team == team);
  822. use_hot_team = 1; // the team is ready to use
  823. } else {
  824. use_hot_team = 0; // AC: threads are not allocated yet
  825. hot_teams[level].hot_team = team; // remember new hot team
  826. hot_teams[level].hot_team_nth = team->t.t_nproc;
  827. }
  828. } else {
  829. use_hot_team = 0;
  830. }
  831. }
  832. #else
  833. use_hot_team = team == root->r.r_hot_team;
  834. #endif
  835. if (!use_hot_team) {
  836. /* install the primary thread */
  837. team->t.t_threads[0] = master_th;
  838. __kmp_initialize_info(master_th, team, 0, master_gtid);
  839. /* now, install the worker threads */
  840. for (i = 1; i < team->t.t_nproc; i++) {
  841. /* fork or reallocate a new thread and install it in team */
  842. kmp_info_t *thr = __kmp_allocate_thread(root, team, i);
  843. team->t.t_threads[i] = thr;
  844. KMP_DEBUG_ASSERT(thr);
  845. KMP_DEBUG_ASSERT(thr->th.th_team == team);
  846. /* align team and thread arrived states */
  847. KA_TRACE(20, ("__kmp_fork_team_threads: T#%d(%d:%d) init arrived "
  848. "T#%d(%d:%d) join =%llu, plain=%llu\n",
  849. __kmp_gtid_from_tid(0, team), team->t.t_id, 0,
  850. __kmp_gtid_from_tid(i, team), team->t.t_id, i,
  851. team->t.t_bar[bs_forkjoin_barrier].b_arrived,
  852. team->t.t_bar[bs_plain_barrier].b_arrived));
  853. thr->th.th_teams_microtask = master_th->th.th_teams_microtask;
  854. thr->th.th_teams_level = master_th->th.th_teams_level;
  855. thr->th.th_teams_size = master_th->th.th_teams_size;
  856. { // Initialize threads' barrier data.
  857. int b;
  858. kmp_balign_t *balign = team->t.t_threads[i]->th.th_bar;
  859. for (b = 0; b < bs_last_barrier; ++b) {
  860. balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived;
  861. KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG);
  862. #if USE_DEBUGGER
  863. balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived;
  864. #endif
  865. }
  866. }
  867. }
  868. #if KMP_AFFINITY_SUPPORTED
  869. // Do not partition the places list for teams construct workers who
  870. // haven't actually been forked to do real work yet. This partitioning
  871. // will take place in the parallel region nested within the teams construct.
  872. if (!fork_teams_workers) {
  873. __kmp_partition_places(team);
  874. }
  875. #endif
  876. }
  877. if (__kmp_display_affinity && team->t.t_display_affinity != 1) {
  878. for (i = 0; i < team->t.t_nproc; i++) {
  879. kmp_info_t *thr = team->t.t_threads[i];
  880. if (thr->th.th_prev_num_threads != team->t.t_nproc ||
  881. thr->th.th_prev_level != team->t.t_level) {
  882. team->t.t_display_affinity = 1;
  883. break;
  884. }
  885. }
  886. }
  887. KMP_MB();
  888. }
  889. #if KMP_ARCH_X86 || KMP_ARCH_X86_64
  890. // Propagate any changes to the floating point control registers out to the team
  891. // We try to avoid unnecessary writes to the relevant cache line in the team
  892. // structure, so we don't make changes unless they are needed.
  893. inline static void propagateFPControl(kmp_team_t *team) {
  894. if (__kmp_inherit_fp_control) {
  895. kmp_int16 x87_fpu_control_word;
  896. kmp_uint32 mxcsr;
  897. // Get primary thread's values of FPU control flags (both X87 and vector)
  898. __kmp_store_x87_fpu_control_word(&x87_fpu_control_word);
  899. __kmp_store_mxcsr(&mxcsr);
  900. mxcsr &= KMP_X86_MXCSR_MASK;
  901. // There is no point looking at t_fp_control_saved here.
  902. // If it is TRUE, we still have to update the values if they are different
  903. // from those we now have. If it is FALSE we didn't save anything yet, but
  904. // our objective is the same. We have to ensure that the values in the team
  905. // are the same as those we have.
  906. // So, this code achieves what we need whether or not t_fp_control_saved is
  907. // true. By checking whether the value needs updating we avoid unnecessary
  908. // writes that would put the cache-line into a written state, causing all
  909. // threads in the team to have to read it again.
  910. KMP_CHECK_UPDATE(team->t.t_x87_fpu_control_word, x87_fpu_control_word);
  911. KMP_CHECK_UPDATE(team->t.t_mxcsr, mxcsr);
  912. // Although we don't use this value, other code in the runtime wants to know
  913. // whether it should restore them. So we must ensure it is correct.
  914. KMP_CHECK_UPDATE(team->t.t_fp_control_saved, TRUE);
  915. } else {
  916. // Similarly here. Don't write to this cache-line in the team structure
  917. // unless we have to.
  918. KMP_CHECK_UPDATE(team->t.t_fp_control_saved, FALSE);
  919. }
  920. }
  921. // Do the opposite, setting the hardware registers to the updated values from
  922. // the team.
  923. inline static void updateHWFPControl(kmp_team_t *team) {
  924. if (__kmp_inherit_fp_control && team->t.t_fp_control_saved) {
  925. // Only reset the fp control regs if they have been changed in the team.
  926. // the parallel region that we are exiting.
  927. kmp_int16 x87_fpu_control_word;
  928. kmp_uint32 mxcsr;
  929. __kmp_store_x87_fpu_control_word(&x87_fpu_control_word);
  930. __kmp_store_mxcsr(&mxcsr);
  931. mxcsr &= KMP_X86_MXCSR_MASK;
  932. if (team->t.t_x87_fpu_control_word != x87_fpu_control_word) {
  933. __kmp_clear_x87_fpu_status_word();
  934. __kmp_load_x87_fpu_control_word(&team->t.t_x87_fpu_control_word);
  935. }
  936. if (team->t.t_mxcsr != mxcsr) {
  937. __kmp_load_mxcsr(&team->t.t_mxcsr);
  938. }
  939. }
  940. }
  941. #else
  942. #define propagateFPControl(x) ((void)0)
  943. #define updateHWFPControl(x) ((void)0)
  944. #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
  945. static void __kmp_alloc_argv_entries(int argc, kmp_team_t *team,
  946. int realloc); // forward declaration
  947. /* Run a parallel region that has been serialized, so runs only in a team of the
  948. single primary thread. */
  949. void __kmp_serialized_parallel(ident_t *loc, kmp_int32 global_tid) {
  950. kmp_info_t *this_thr;
  951. kmp_team_t *serial_team;
  952. KC_TRACE(10, ("__kmpc_serialized_parallel: called by T#%d\n", global_tid));
  953. /* Skip all this code for autopar serialized loops since it results in
  954. unacceptable overhead */
  955. if (loc != NULL && (loc->flags & KMP_IDENT_AUTOPAR))
  956. return;
  957. if (!TCR_4(__kmp_init_parallel))
  958. __kmp_parallel_initialize();
  959. __kmp_resume_if_soft_paused();
  960. this_thr = __kmp_threads[global_tid];
  961. serial_team = this_thr->th.th_serial_team;
  962. /* utilize the serialized team held by this thread */
  963. KMP_DEBUG_ASSERT(serial_team);
  964. KMP_MB();
  965. if (__kmp_tasking_mode != tskm_immediate_exec) {
  966. KMP_DEBUG_ASSERT(
  967. this_thr->th.th_task_team ==
  968. this_thr->th.th_team->t.t_task_team[this_thr->th.th_task_state]);
  969. KMP_DEBUG_ASSERT(serial_team->t.t_task_team[this_thr->th.th_task_state] ==
  970. NULL);
  971. KA_TRACE(20, ("__kmpc_serialized_parallel: T#%d pushing task_team %p / "
  972. "team %p, new task_team = NULL\n",
  973. global_tid, this_thr->th.th_task_team, this_thr->th.th_team));
  974. this_thr->th.th_task_team = NULL;
  975. }
  976. kmp_proc_bind_t proc_bind = this_thr->th.th_set_proc_bind;
  977. if (this_thr->th.th_current_task->td_icvs.proc_bind == proc_bind_false) {
  978. proc_bind = proc_bind_false;
  979. } else if (proc_bind == proc_bind_default) {
  980. // No proc_bind clause was specified, so use the current value
  981. // of proc-bind-var for this parallel region.
  982. proc_bind = this_thr->th.th_current_task->td_icvs.proc_bind;
  983. }
  984. // Reset for next parallel region
  985. this_thr->th.th_set_proc_bind = proc_bind_default;
  986. #if OMPT_SUPPORT
  987. ompt_data_t ompt_parallel_data = ompt_data_none;
  988. void *codeptr = OMPT_LOAD_RETURN_ADDRESS(global_tid);
  989. if (ompt_enabled.enabled &&
  990. this_thr->th.ompt_thread_info.state != ompt_state_overhead) {
  991. ompt_task_info_t *parent_task_info;
  992. parent_task_info = OMPT_CUR_TASK_INFO(this_thr);
  993. parent_task_info->frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
  994. if (ompt_enabled.ompt_callback_parallel_begin) {
  995. int team_size = 1;
  996. ompt_callbacks.ompt_callback(ompt_callback_parallel_begin)(
  997. &(parent_task_info->task_data), &(parent_task_info->frame),
  998. &ompt_parallel_data, team_size,
  999. ompt_parallel_invoker_program | ompt_parallel_team, codeptr);
  1000. }
  1001. }
  1002. #endif // OMPT_SUPPORT
  1003. if (this_thr->th.th_team != serial_team) {
  1004. // Nested level will be an index in the nested nthreads array
  1005. int level = this_thr->th.th_team->t.t_level;
  1006. if (serial_team->t.t_serialized) {
  1007. /* this serial team was already used
  1008. TODO increase performance by making this locks more specific */
  1009. kmp_team_t *new_team;
  1010. __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
  1011. new_team =
  1012. __kmp_allocate_team(this_thr->th.th_root, 1, 1,
  1013. #if OMPT_SUPPORT
  1014. ompt_parallel_data,
  1015. #endif
  1016. proc_bind, &this_thr->th.th_current_task->td_icvs,
  1017. 0 USE_NESTED_HOT_ARG(NULL));
  1018. __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
  1019. KMP_ASSERT(new_team);
  1020. /* setup new serialized team and install it */
  1021. new_team->t.t_threads[0] = this_thr;
  1022. new_team->t.t_parent = this_thr->th.th_team;
  1023. serial_team = new_team;
  1024. this_thr->th.th_serial_team = serial_team;
  1025. KF_TRACE(
  1026. 10,
  1027. ("__kmpc_serialized_parallel: T#%d allocated new serial team %p\n",
  1028. global_tid, serial_team));
  1029. /* TODO the above breaks the requirement that if we run out of resources,
  1030. then we can still guarantee that serialized teams are ok, since we may
  1031. need to allocate a new one */
  1032. } else {
  1033. KF_TRACE(
  1034. 10,
  1035. ("__kmpc_serialized_parallel: T#%d reusing cached serial team %p\n",
  1036. global_tid, serial_team));
  1037. }
  1038. /* we have to initialize this serial team */
  1039. KMP_DEBUG_ASSERT(serial_team->t.t_threads);
  1040. KMP_DEBUG_ASSERT(serial_team->t.t_threads[0] == this_thr);
  1041. KMP_DEBUG_ASSERT(this_thr->th.th_team != serial_team);
  1042. serial_team->t.t_ident = loc;
  1043. serial_team->t.t_serialized = 1;
  1044. serial_team->t.t_nproc = 1;
  1045. serial_team->t.t_parent = this_thr->th.th_team;
  1046. serial_team->t.t_sched.sched = this_thr->th.th_team->t.t_sched.sched;
  1047. this_thr->th.th_team = serial_team;
  1048. serial_team->t.t_master_tid = this_thr->th.th_info.ds.ds_tid;
  1049. KF_TRACE(10, ("__kmpc_serialized_parallel: T#%d curtask=%p\n", global_tid,
  1050. this_thr->th.th_current_task));
  1051. KMP_ASSERT(this_thr->th.th_current_task->td_flags.executing == 1);
  1052. this_thr->th.th_current_task->td_flags.executing = 0;
  1053. __kmp_push_current_task_to_thread(this_thr, serial_team, 0);
  1054. /* TODO: GEH: do ICVs work for nested serialized teams? Don't we need an
  1055. implicit task for each serialized task represented by
  1056. team->t.t_serialized? */
  1057. copy_icvs(&this_thr->th.th_current_task->td_icvs,
  1058. &this_thr->th.th_current_task->td_parent->td_icvs);
  1059. // Thread value exists in the nested nthreads array for the next nested
  1060. // level
  1061. if (__kmp_nested_nth.used && (level + 1 < __kmp_nested_nth.used)) {
  1062. this_thr->th.th_current_task->td_icvs.nproc =
  1063. __kmp_nested_nth.nth[level + 1];
  1064. }
  1065. if (__kmp_nested_proc_bind.used &&
  1066. (level + 1 < __kmp_nested_proc_bind.used)) {
  1067. this_thr->th.th_current_task->td_icvs.proc_bind =
  1068. __kmp_nested_proc_bind.bind_types[level + 1];
  1069. }
  1070. #if USE_DEBUGGER
  1071. serial_team->t.t_pkfn = (microtask_t)(~0); // For the debugger.
  1072. #endif
  1073. this_thr->th.th_info.ds.ds_tid = 0;
  1074. /* set thread cache values */
  1075. this_thr->th.th_team_nproc = 1;
  1076. this_thr->th.th_team_master = this_thr;
  1077. this_thr->th.th_team_serialized = 1;
  1078. serial_team->t.t_level = serial_team->t.t_parent->t.t_level + 1;
  1079. serial_team->t.t_active_level = serial_team->t.t_parent->t.t_active_level;
  1080. serial_team->t.t_def_allocator = this_thr->th.th_def_allocator; // save
  1081. propagateFPControl(serial_team);
  1082. /* check if we need to allocate dispatch buffers stack */
  1083. KMP_DEBUG_ASSERT(serial_team->t.t_dispatch);
  1084. if (!serial_team->t.t_dispatch->th_disp_buffer) {
  1085. serial_team->t.t_dispatch->th_disp_buffer =
  1086. (dispatch_private_info_t *)__kmp_allocate(
  1087. sizeof(dispatch_private_info_t));
  1088. }
  1089. this_thr->th.th_dispatch = serial_team->t.t_dispatch;
  1090. KMP_MB();
  1091. } else {
  1092. /* this serialized team is already being used,
  1093. * that's fine, just add another nested level */
  1094. KMP_DEBUG_ASSERT(this_thr->th.th_team == serial_team);
  1095. KMP_DEBUG_ASSERT(serial_team->t.t_threads);
  1096. KMP_DEBUG_ASSERT(serial_team->t.t_threads[0] == this_thr);
  1097. ++serial_team->t.t_serialized;
  1098. this_thr->th.th_team_serialized = serial_team->t.t_serialized;
  1099. // Nested level will be an index in the nested nthreads array
  1100. int level = this_thr->th.th_team->t.t_level;
  1101. // Thread value exists in the nested nthreads array for the next nested
  1102. // level
  1103. if (__kmp_nested_nth.used && (level + 1 < __kmp_nested_nth.used)) {
  1104. this_thr->th.th_current_task->td_icvs.nproc =
  1105. __kmp_nested_nth.nth[level + 1];
  1106. }
  1107. serial_team->t.t_level++;
  1108. KF_TRACE(10, ("__kmpc_serialized_parallel: T#%d increasing nesting level "
  1109. "of serial team %p to %d\n",
  1110. global_tid, serial_team, serial_team->t.t_level));
  1111. /* allocate/push dispatch buffers stack */
  1112. KMP_DEBUG_ASSERT(serial_team->t.t_dispatch);
  1113. {
  1114. dispatch_private_info_t *disp_buffer =
  1115. (dispatch_private_info_t *)__kmp_allocate(
  1116. sizeof(dispatch_private_info_t));
  1117. disp_buffer->next = serial_team->t.t_dispatch->th_disp_buffer;
  1118. serial_team->t.t_dispatch->th_disp_buffer = disp_buffer;
  1119. }
  1120. this_thr->th.th_dispatch = serial_team->t.t_dispatch;
  1121. KMP_MB();
  1122. }
  1123. KMP_CHECK_UPDATE(serial_team->t.t_cancel_request, cancel_noreq);
  1124. // Perform the display affinity functionality for
  1125. // serialized parallel regions
  1126. if (__kmp_display_affinity) {
  1127. if (this_thr->th.th_prev_level != serial_team->t.t_level ||
  1128. this_thr->th.th_prev_num_threads != 1) {
  1129. // NULL means use the affinity-format-var ICV
  1130. __kmp_aux_display_affinity(global_tid, NULL);
  1131. this_thr->th.th_prev_level = serial_team->t.t_level;
  1132. this_thr->th.th_prev_num_threads = 1;
  1133. }
  1134. }
  1135. if (__kmp_env_consistency_check)
  1136. __kmp_push_parallel(global_tid, NULL);
  1137. #if OMPT_SUPPORT
  1138. serial_team->t.ompt_team_info.master_return_address = codeptr;
  1139. if (ompt_enabled.enabled &&
  1140. this_thr->th.ompt_thread_info.state != ompt_state_overhead) {
  1141. OMPT_CUR_TASK_INFO(this_thr)->frame.exit_frame.ptr =
  1142. OMPT_GET_FRAME_ADDRESS(0);
  1143. ompt_lw_taskteam_t lw_taskteam;
  1144. __ompt_lw_taskteam_init(&lw_taskteam, this_thr, global_tid,
  1145. &ompt_parallel_data, codeptr);
  1146. __ompt_lw_taskteam_link(&lw_taskteam, this_thr, 1);
  1147. // don't use lw_taskteam after linking. content was swaped
  1148. /* OMPT implicit task begin */
  1149. if (ompt_enabled.ompt_callback_implicit_task) {
  1150. ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
  1151. ompt_scope_begin, OMPT_CUR_TEAM_DATA(this_thr),
  1152. OMPT_CUR_TASK_DATA(this_thr), 1, __kmp_tid_from_gtid(global_tid),
  1153. ompt_task_implicit); // TODO: Can this be ompt_task_initial?
  1154. OMPT_CUR_TASK_INFO(this_thr)->thread_num =
  1155. __kmp_tid_from_gtid(global_tid);
  1156. }
  1157. /* OMPT state */
  1158. this_thr->th.ompt_thread_info.state = ompt_state_work_parallel;
  1159. OMPT_CUR_TASK_INFO(this_thr)->frame.exit_frame.ptr =
  1160. OMPT_GET_FRAME_ADDRESS(0);
  1161. }
  1162. #endif
  1163. }
  1164. /* most of the work for a fork */
  1165. /* return true if we really went parallel, false if serialized */
  1166. int __kmp_fork_call(ident_t *loc, int gtid,
  1167. enum fork_context_e call_context, // Intel, GNU, ...
  1168. kmp_int32 argc, microtask_t microtask, launch_t invoker,
  1169. kmp_va_list ap) {
  1170. void **argv;
  1171. int i;
  1172. int master_tid;
  1173. int master_this_cons;
  1174. kmp_team_t *team;
  1175. kmp_team_t *parent_team;
  1176. kmp_info_t *master_th;
  1177. kmp_root_t *root;
  1178. int nthreads;
  1179. int master_active;
  1180. int master_set_numthreads;
  1181. int level;
  1182. int active_level;
  1183. int teams_level;
  1184. #if KMP_NESTED_HOT_TEAMS
  1185. kmp_hot_team_ptr_t **p_hot_teams;
  1186. #endif
  1187. { // KMP_TIME_BLOCK
  1188. KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(KMP_fork_call);
  1189. KMP_COUNT_VALUE(OMP_PARALLEL_args, argc);
  1190. KA_TRACE(20, ("__kmp_fork_call: enter T#%d\n", gtid));
  1191. if (__kmp_stkpadding > 0 && __kmp_root[gtid] != NULL) {
  1192. /* Some systems prefer the stack for the root thread(s) to start with */
  1193. /* some gap from the parent stack to prevent false sharing. */
  1194. void *dummy = KMP_ALLOCA(__kmp_stkpadding);
  1195. /* These 2 lines below are so this does not get optimized out */
  1196. if (__kmp_stkpadding > KMP_MAX_STKPADDING)
  1197. __kmp_stkpadding += (short)((kmp_int64)dummy);
  1198. }
  1199. /* initialize if needed */
  1200. KMP_DEBUG_ASSERT(
  1201. __kmp_init_serial); // AC: potentially unsafe, not in sync with shutdown
  1202. if (!TCR_4(__kmp_init_parallel))
  1203. __kmp_parallel_initialize();
  1204. __kmp_resume_if_soft_paused();
  1205. /* setup current data */
  1206. master_th = __kmp_threads[gtid]; // AC: potentially unsafe, not in sync with
  1207. // shutdown
  1208. parent_team = master_th->th.th_team;
  1209. master_tid = master_th->th.th_info.ds.ds_tid;
  1210. master_this_cons = master_th->th.th_local.this_construct;
  1211. root = master_th->th.th_root;
  1212. master_active = root->r.r_active;
  1213. master_set_numthreads = master_th->th.th_set_nproc;
  1214. #if OMPT_SUPPORT
  1215. ompt_data_t ompt_parallel_data = ompt_data_none;
  1216. ompt_data_t *parent_task_data;
  1217. ompt_frame_t *ompt_frame;
  1218. ompt_data_t *implicit_task_data;
  1219. void *return_address = NULL;
  1220. if (ompt_enabled.enabled) {
  1221. __ompt_get_task_info_internal(0, NULL, &parent_task_data, &ompt_frame,
  1222. NULL, NULL);
  1223. return_address = OMPT_LOAD_RETURN_ADDRESS(gtid);
  1224. }
  1225. #endif
  1226. // Assign affinity to root thread if it hasn't happened yet
  1227. __kmp_assign_root_init_mask();
  1228. // Nested level will be an index in the nested nthreads array
  1229. level = parent_team->t.t_level;
  1230. // used to launch non-serial teams even if nested is not allowed
  1231. active_level = parent_team->t.t_active_level;
  1232. // needed to check nesting inside the teams
  1233. teams_level = master_th->th.th_teams_level;
  1234. #if KMP_NESTED_HOT_TEAMS
  1235. p_hot_teams = &master_th->th.th_hot_teams;
  1236. if (*p_hot_teams == NULL && __kmp_hot_teams_max_level > 0) {
  1237. *p_hot_teams = (kmp_hot_team_ptr_t *)__kmp_allocate(
  1238. sizeof(kmp_hot_team_ptr_t) * __kmp_hot_teams_max_level);
  1239. (*p_hot_teams)[0].hot_team = root->r.r_hot_team;
  1240. // it is either actual or not needed (when active_level > 0)
  1241. (*p_hot_teams)[0].hot_team_nth = 1;
  1242. }
  1243. #endif
  1244. #if OMPT_SUPPORT
  1245. if (ompt_enabled.enabled) {
  1246. if (ompt_enabled.ompt_callback_parallel_begin) {
  1247. int team_size = master_set_numthreads
  1248. ? master_set_numthreads
  1249. : get__nproc_2(parent_team, master_tid);
  1250. int flags = OMPT_INVOKER(call_context) |
  1251. ((microtask == (microtask_t)__kmp_teams_master)
  1252. ? ompt_parallel_league
  1253. : ompt_parallel_team);
  1254. ompt_callbacks.ompt_callback(ompt_callback_parallel_begin)(
  1255. parent_task_data, ompt_frame, &ompt_parallel_data, team_size, flags,
  1256. return_address);
  1257. }
  1258. master_th->th.ompt_thread_info.state = ompt_state_overhead;
  1259. }
  1260. #endif
  1261. master_th->th.th_ident = loc;
  1262. if (master_th->th.th_teams_microtask && ap &&
  1263. microtask != (microtask_t)__kmp_teams_master && level == teams_level) {
  1264. // AC: This is start of parallel that is nested inside teams construct.
  1265. // The team is actual (hot), all workers are ready at the fork barrier.
  1266. // No lock needed to initialize the team a bit, then free workers.
  1267. parent_team->t.t_ident = loc;
  1268. __kmp_alloc_argv_entries(argc, parent_team, TRUE);
  1269. parent_team->t.t_argc = argc;
  1270. argv = (void **)parent_team->t.t_argv;
  1271. for (i = argc - 1; i >= 0; --i)
  1272. *argv++ = va_arg(kmp_va_deref(ap), void *);
  1273. // Increment our nested depth levels, but not increase the serialization
  1274. if (parent_team == master_th->th.th_serial_team) {
  1275. // AC: we are in serialized parallel
  1276. __kmpc_serialized_parallel(loc, gtid);
  1277. KMP_DEBUG_ASSERT(parent_team->t.t_serialized > 1);
  1278. if (call_context == fork_context_gnu) {
  1279. // AC: need to decrement t_serialized for enquiry functions to work
  1280. // correctly, will restore at join time
  1281. parent_team->t.t_serialized--;
  1282. return TRUE;
  1283. }
  1284. #if OMPD_SUPPORT
  1285. parent_team->t.t_pkfn = microtask;
  1286. #endif
  1287. #if OMPT_SUPPORT
  1288. void *dummy;
  1289. void **exit_frame_p;
  1290. ompt_lw_taskteam_t lw_taskteam;
  1291. if (ompt_enabled.enabled) {
  1292. __ompt_lw_taskteam_init(&lw_taskteam, master_th, gtid,
  1293. &ompt_parallel_data, return_address);
  1294. exit_frame_p = &(lw_taskteam.ompt_task_info.frame.exit_frame.ptr);
  1295. __ompt_lw_taskteam_link(&lw_taskteam, master_th, 0);
  1296. // don't use lw_taskteam after linking. content was swaped
  1297. /* OMPT implicit task begin */
  1298. implicit_task_data = OMPT_CUR_TASK_DATA(master_th);
  1299. if (ompt_enabled.ompt_callback_implicit_task) {
  1300. OMPT_CUR_TASK_INFO(master_th)->thread_num =
  1301. __kmp_tid_from_gtid(gtid);
  1302. ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
  1303. ompt_scope_begin, OMPT_CUR_TEAM_DATA(master_th),
  1304. implicit_task_data, 1,
  1305. OMPT_CUR_TASK_INFO(master_th)->thread_num, ompt_task_implicit);
  1306. }
  1307. /* OMPT state */
  1308. master_th->th.ompt_thread_info.state = ompt_state_work_parallel;
  1309. } else {
  1310. exit_frame_p = &dummy;
  1311. }
  1312. #endif
  1313. // AC: need to decrement t_serialized for enquiry functions to work
  1314. // correctly, will restore at join time
  1315. parent_team->t.t_serialized--;
  1316. {
  1317. KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
  1318. KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
  1319. __kmp_invoke_microtask(microtask, gtid, 0, argc, parent_team->t.t_argv
  1320. #if OMPT_SUPPORT
  1321. ,
  1322. exit_frame_p
  1323. #endif
  1324. );
  1325. }
  1326. #if OMPT_SUPPORT
  1327. if (ompt_enabled.enabled) {
  1328. *exit_frame_p = NULL;
  1329. OMPT_CUR_TASK_INFO(master_th)->frame.exit_frame = ompt_data_none;
  1330. if (ompt_enabled.ompt_callback_implicit_task) {
  1331. ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
  1332. ompt_scope_end, NULL, implicit_task_data, 1,
  1333. OMPT_CUR_TASK_INFO(master_th)->thread_num, ompt_task_implicit);
  1334. }
  1335. ompt_parallel_data = *OMPT_CUR_TEAM_DATA(master_th);
  1336. __ompt_lw_taskteam_unlink(master_th);
  1337. if (ompt_enabled.ompt_callback_parallel_end) {
  1338. ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
  1339. &ompt_parallel_data, OMPT_CUR_TASK_DATA(master_th),
  1340. OMPT_INVOKER(call_context) | ompt_parallel_team,
  1341. return_address);
  1342. }
  1343. master_th->th.ompt_thread_info.state = ompt_state_overhead;
  1344. }
  1345. #endif
  1346. return TRUE;
  1347. }
  1348. parent_team->t.t_pkfn = microtask;
  1349. parent_team->t.t_invoke = invoker;
  1350. KMP_ATOMIC_INC(&root->r.r_in_parallel);
  1351. parent_team->t.t_active_level++;
  1352. parent_team->t.t_level++;
  1353. parent_team->t.t_def_allocator = master_th->th.th_def_allocator; // save
  1354. #if OMPT_SUPPORT
  1355. if (ompt_enabled.enabled) {
  1356. ompt_lw_taskteam_t lw_taskteam;
  1357. __ompt_lw_taskteam_init(&lw_taskteam, master_th, gtid,
  1358. &ompt_parallel_data, return_address);
  1359. __ompt_lw_taskteam_link(&lw_taskteam, master_th, 1, true);
  1360. }
  1361. #endif
  1362. /* Change number of threads in the team if requested */
  1363. if (master_set_numthreads) { // The parallel has num_threads clause
  1364. if (master_set_numthreads <= master_th->th.th_teams_size.nth) {
  1365. // AC: only can reduce number of threads dynamically, can't increase
  1366. kmp_info_t **other_threads = parent_team->t.t_threads;
  1367. // NOTE: if using distributed barrier, we need to run this code block
  1368. // even when the team size appears not to have changed from the max.
  1369. int old_proc = master_th->th.th_teams_size.nth;
  1370. if (__kmp_barrier_release_pattern[bs_forkjoin_barrier] ==
  1371. bp_dist_bar) {
  1372. __kmp_resize_dist_barrier(parent_team, old_proc,
  1373. master_set_numthreads);
  1374. __kmp_add_threads_to_team(parent_team, master_set_numthreads);
  1375. }
  1376. parent_team->t.t_nproc = master_set_numthreads;
  1377. for (i = 0; i < master_set_numthreads; ++i) {
  1378. other_threads[i]->th.th_team_nproc = master_set_numthreads;
  1379. }
  1380. }
  1381. // Keep extra threads hot in the team for possible next parallels
  1382. master_th->th.th_set_nproc = 0;
  1383. }
  1384. #if USE_DEBUGGER
  1385. if (__kmp_debugging) { // Let debugger override number of threads.
  1386. int nth = __kmp_omp_num_threads(loc);
  1387. if (nth > 0) { // 0 means debugger doesn't want to change num threads
  1388. master_set_numthreads = nth;
  1389. }
  1390. }
  1391. #endif
  1392. // Figure out the proc_bind policy for the nested parallel within teams
  1393. kmp_proc_bind_t proc_bind = master_th->th.th_set_proc_bind;
  1394. // proc_bind_default means don't update
  1395. kmp_proc_bind_t proc_bind_icv = proc_bind_default;
  1396. if (master_th->th.th_current_task->td_icvs.proc_bind == proc_bind_false) {
  1397. proc_bind = proc_bind_false;
  1398. } else {
  1399. // No proc_bind clause specified; use current proc-bind-var
  1400. if (proc_bind == proc_bind_default) {
  1401. proc_bind = master_th->th.th_current_task->td_icvs.proc_bind;
  1402. }
  1403. /* else: The proc_bind policy was specified explicitly on parallel
  1404. clause.
  1405. This overrides proc-bind-var for this parallel region, but does not
  1406. change proc-bind-var. */
  1407. // Figure the value of proc-bind-var for the child threads.
  1408. if ((level + 1 < __kmp_nested_proc_bind.used) &&
  1409. (__kmp_nested_proc_bind.bind_types[level + 1] !=
  1410. master_th->th.th_current_task->td_icvs.proc_bind)) {
  1411. proc_bind_icv = __kmp_nested_proc_bind.bind_types[level + 1];
  1412. }
  1413. }
  1414. KMP_CHECK_UPDATE(parent_team->t.t_proc_bind, proc_bind);
  1415. // Need to change the bind-var ICV to correct value for each implicit task
  1416. if (proc_bind_icv != proc_bind_default &&
  1417. master_th->th.th_current_task->td_icvs.proc_bind != proc_bind_icv) {
  1418. kmp_info_t **other_threads = parent_team->t.t_threads;
  1419. for (i = 0; i < master_th->th.th_team_nproc; ++i) {
  1420. other_threads[i]->th.th_current_task->td_icvs.proc_bind =
  1421. proc_bind_icv;
  1422. }
  1423. }
  1424. // Reset for next parallel region
  1425. master_th->th.th_set_proc_bind = proc_bind_default;
  1426. #if USE_ITT_BUILD && USE_ITT_NOTIFY
  1427. if (((__itt_frame_submit_v3_ptr && __itt_get_timestamp_ptr) ||
  1428. KMP_ITT_DEBUG) &&
  1429. __kmp_forkjoin_frames_mode == 3 &&
  1430. parent_team->t.t_active_level == 1 // only report frames at level 1
  1431. && master_th->th.th_teams_size.nteams == 1) {
  1432. kmp_uint64 tmp_time = __itt_get_timestamp();
  1433. master_th->th.th_frame_time = tmp_time;
  1434. parent_team->t.t_region_time = tmp_time;
  1435. }
  1436. if (__itt_stack_caller_create_ptr) {
  1437. KMP_DEBUG_ASSERT(parent_team->t.t_stack_id == NULL);
  1438. // create new stack stitching id before entering fork barrier
  1439. parent_team->t.t_stack_id = __kmp_itt_stack_caller_create();
  1440. }
  1441. #endif /* USE_ITT_BUILD && USE_ITT_NOTIFY */
  1442. #if KMP_AFFINITY_SUPPORTED
  1443. __kmp_partition_places(parent_team);
  1444. #endif
  1445. KF_TRACE(10, ("__kmp_fork_call: before internal fork: root=%p, team=%p, "
  1446. "master_th=%p, gtid=%d\n",
  1447. root, parent_team, master_th, gtid));
  1448. __kmp_internal_fork(loc, gtid, parent_team);
  1449. KF_TRACE(10, ("__kmp_fork_call: after internal fork: root=%p, team=%p, "
  1450. "master_th=%p, gtid=%d\n",
  1451. root, parent_team, master_th, gtid));
  1452. if (call_context == fork_context_gnu)
  1453. return TRUE;
  1454. /* Invoke microtask for PRIMARY thread */
  1455. KA_TRACE(20, ("__kmp_fork_call: T#%d(%d:0) invoke microtask = %p\n", gtid,
  1456. parent_team->t.t_id, parent_team->t.t_pkfn));
  1457. if (!parent_team->t.t_invoke(gtid)) {
  1458. KMP_ASSERT2(0, "cannot invoke microtask for PRIMARY thread");
  1459. }
  1460. KA_TRACE(20, ("__kmp_fork_call: T#%d(%d:0) done microtask = %p\n", gtid,
  1461. parent_team->t.t_id, parent_team->t.t_pkfn));
  1462. KMP_MB(); /* Flush all pending memory write invalidates. */
  1463. KA_TRACE(20, ("__kmp_fork_call: parallel exit T#%d\n", gtid));
  1464. return TRUE;
  1465. } // Parallel closely nested in teams construct
  1466. #if KMP_DEBUG
  1467. if (__kmp_tasking_mode != tskm_immediate_exec) {
  1468. KMP_DEBUG_ASSERT(master_th->th.th_task_team ==
  1469. parent_team->t.t_task_team[master_th->th.th_task_state]);
  1470. }
  1471. #endif
  1472. // Need this to happen before we determine the number of threads, not while
  1473. // we are allocating the team
  1474. //__kmp_push_current_task_to_thread(master_th, parent_team, 0);
  1475. int enter_teams = 0;
  1476. if (parent_team->t.t_active_level >=
  1477. master_th->th.th_current_task->td_icvs.max_active_levels) {
  1478. nthreads = 1;
  1479. } else {
  1480. enter_teams = ((ap == NULL && active_level == 0) ||
  1481. (ap && teams_level > 0 && teams_level == level));
  1482. nthreads = master_set_numthreads
  1483. ? master_set_numthreads
  1484. // TODO: get nproc directly from current task
  1485. : get__nproc_2(parent_team, master_tid);
  1486. // Check if we need to take forkjoin lock? (no need for serialized
  1487. // parallel out of teams construct). This code moved here from
  1488. // __kmp_reserve_threads() to speedup nested serialized parallels.
  1489. if (nthreads > 1) {
  1490. if ((get__max_active_levels(master_th) == 1 &&
  1491. (root->r.r_in_parallel && !enter_teams)) ||
  1492. (__kmp_library == library_serial)) {
  1493. KC_TRACE(10, ("__kmp_fork_call: T#%d serializing team; requested %d"
  1494. " threads\n",
  1495. gtid, nthreads));
  1496. nthreads = 1;
  1497. }
  1498. }
  1499. if (nthreads > 1) {
  1500. /* determine how many new threads we can use */
  1501. __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
  1502. /* AC: If we execute teams from parallel region (on host), then teams
  1503. should be created but each can only have 1 thread if nesting is
  1504. disabled. If teams called from serial region, then teams and their
  1505. threads should be created regardless of the nesting setting. */
  1506. nthreads = __kmp_reserve_threads(root, parent_team, master_tid,
  1507. nthreads, enter_teams);
  1508. if (nthreads == 1) {
  1509. // Free lock for single thread execution here; for multi-thread
  1510. // execution it will be freed later after team of threads created
  1511. // and initialized
  1512. __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
  1513. }
  1514. }
  1515. }
  1516. KMP_DEBUG_ASSERT(nthreads > 0);
  1517. // If we temporarily changed the set number of threads then restore it now
  1518. master_th->th.th_set_nproc = 0;
  1519. /* create a serialized parallel region? */
  1520. if (nthreads == 1) {
  1521. /* josh todo: hypothetical question: what do we do for OS X*? */
  1522. #if KMP_OS_LINUX && \
  1523. (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)
  1524. void *args[argc];
  1525. #else
  1526. void **args = (void **)KMP_ALLOCA(argc * sizeof(void *));
  1527. #endif /* KMP_OS_LINUX && ( KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || \
  1528. KMP_ARCH_AARCH64) */
  1529. KA_TRACE(20,
  1530. ("__kmp_fork_call: T#%d serializing parallel region\n", gtid));
  1531. __kmpc_serialized_parallel(loc, gtid);
  1532. #if OMPD_SUPPORT
  1533. master_th->th.th_serial_team->t.t_pkfn = microtask;
  1534. #endif
  1535. if (call_context == fork_context_intel) {
  1536. /* TODO this sucks, use the compiler itself to pass args! :) */
  1537. master_th->th.th_serial_team->t.t_ident = loc;
  1538. if (!ap) {
  1539. // revert change made in __kmpc_serialized_parallel()
  1540. master_th->th.th_serial_team->t.t_level--;
  1541. // Get args from parent team for teams construct
  1542. #if OMPT_SUPPORT
  1543. void *dummy;
  1544. void **exit_frame_p;
  1545. ompt_task_info_t *task_info;
  1546. ompt_lw_taskteam_t lw_taskteam;
  1547. if (ompt_enabled.enabled) {
  1548. __ompt_lw_taskteam_init(&lw_taskteam, master_th, gtid,
  1549. &ompt_parallel_data, return_address);
  1550. __ompt_lw_taskteam_link(&lw_taskteam, master_th, 0);
  1551. // don't use lw_taskteam after linking. content was swaped
  1552. task_info = OMPT_CUR_TASK_INFO(master_th);
  1553. exit_frame_p = &(task_info->frame.exit_frame.ptr);
  1554. if (ompt_enabled.ompt_callback_implicit_task) {
  1555. OMPT_CUR_TASK_INFO(master_th)->thread_num =
  1556. __kmp_tid_from_gtid(gtid);
  1557. ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
  1558. ompt_scope_begin, OMPT_CUR_TEAM_DATA(master_th),
  1559. &(task_info->task_data), 1,
  1560. OMPT_CUR_TASK_INFO(master_th)->thread_num,
  1561. ompt_task_implicit);
  1562. }
  1563. /* OMPT state */
  1564. master_th->th.ompt_thread_info.state = ompt_state_work_parallel;
  1565. } else {
  1566. exit_frame_p = &dummy;
  1567. }
  1568. #endif
  1569. {
  1570. KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
  1571. KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
  1572. __kmp_invoke_microtask(microtask, gtid, 0, argc,
  1573. parent_team->t.t_argv
  1574. #if OMPT_SUPPORT
  1575. ,
  1576. exit_frame_p
  1577. #endif
  1578. );
  1579. }
  1580. #if OMPT_SUPPORT
  1581. if (ompt_enabled.enabled) {
  1582. *exit_frame_p = NULL;
  1583. if (ompt_enabled.ompt_callback_implicit_task) {
  1584. ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
  1585. ompt_scope_end, NULL, &(task_info->task_data), 1,
  1586. OMPT_CUR_TASK_INFO(master_th)->thread_num,
  1587. ompt_task_implicit);
  1588. }
  1589. ompt_parallel_data = *OMPT_CUR_TEAM_DATA(master_th);
  1590. __ompt_lw_taskteam_unlink(master_th);
  1591. if (ompt_enabled.ompt_callback_parallel_end) {
  1592. ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
  1593. &ompt_parallel_data, parent_task_data,
  1594. OMPT_INVOKER(call_context) | ompt_parallel_team,
  1595. return_address);
  1596. }
  1597. master_th->th.ompt_thread_info.state = ompt_state_overhead;
  1598. }
  1599. #endif
  1600. } else if (microtask == (microtask_t)__kmp_teams_master) {
  1601. KMP_DEBUG_ASSERT(master_th->th.th_team ==
  1602. master_th->th.th_serial_team);
  1603. team = master_th->th.th_team;
  1604. // team->t.t_pkfn = microtask;
  1605. team->t.t_invoke = invoker;
  1606. __kmp_alloc_argv_entries(argc, team, TRUE);
  1607. team->t.t_argc = argc;
  1608. argv = (void **)team->t.t_argv;
  1609. if (ap) {
  1610. for (i = argc - 1; i >= 0; --i)
  1611. *argv++ = va_arg(kmp_va_deref(ap), void *);
  1612. } else {
  1613. for (i = 0; i < argc; ++i)
  1614. // Get args from parent team for teams construct
  1615. argv[i] = parent_team->t.t_argv[i];
  1616. }
  1617. // AC: revert change made in __kmpc_serialized_parallel()
  1618. // because initial code in teams should have level=0
  1619. team->t.t_level--;
  1620. // AC: call special invoker for outer "parallel" of teams construct
  1621. invoker(gtid);
  1622. #if OMPT_SUPPORT
  1623. if (ompt_enabled.enabled) {
  1624. ompt_task_info_t *task_info = OMPT_CUR_TASK_INFO(master_th);
  1625. if (ompt_enabled.ompt_callback_implicit_task) {
  1626. ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
  1627. ompt_scope_end, NULL, &(task_info->task_data), 0,
  1628. OMPT_CUR_TASK_INFO(master_th)->thread_num, ompt_task_initial);
  1629. }
  1630. if (ompt_enabled.ompt_callback_parallel_end) {
  1631. ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
  1632. &ompt_parallel_data, parent_task_data,
  1633. OMPT_INVOKER(call_context) | ompt_parallel_league,
  1634. return_address);
  1635. }
  1636. master_th->th.ompt_thread_info.state = ompt_state_overhead;
  1637. }
  1638. #endif
  1639. } else {
  1640. argv = args;
  1641. for (i = argc - 1; i >= 0; --i)
  1642. *argv++ = va_arg(kmp_va_deref(ap), void *);
  1643. KMP_MB();
  1644. #if OMPT_SUPPORT
  1645. void *dummy;
  1646. void **exit_frame_p;
  1647. ompt_task_info_t *task_info;
  1648. ompt_lw_taskteam_t lw_taskteam;
  1649. if (ompt_enabled.enabled) {
  1650. __ompt_lw_taskteam_init(&lw_taskteam, master_th, gtid,
  1651. &ompt_parallel_data, return_address);
  1652. __ompt_lw_taskteam_link(&lw_taskteam, master_th, 0);
  1653. // don't use lw_taskteam after linking. content was swaped
  1654. task_info = OMPT_CUR_TASK_INFO(master_th);
  1655. exit_frame_p = &(task_info->frame.exit_frame.ptr);
  1656. /* OMPT implicit task begin */
  1657. implicit_task_data = OMPT_CUR_TASK_DATA(master_th);
  1658. if (ompt_enabled.ompt_callback_implicit_task) {
  1659. ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
  1660. ompt_scope_begin, OMPT_CUR_TEAM_DATA(master_th),
  1661. implicit_task_data, 1, __kmp_tid_from_gtid(gtid),
  1662. ompt_task_implicit);
  1663. OMPT_CUR_TASK_INFO(master_th)->thread_num =
  1664. __kmp_tid_from_gtid(gtid);
  1665. }
  1666. /* OMPT state */
  1667. master_th->th.ompt_thread_info.state = ompt_state_work_parallel;
  1668. } else {
  1669. exit_frame_p = &dummy;
  1670. }
  1671. #endif
  1672. {
  1673. KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
  1674. KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
  1675. __kmp_invoke_microtask(microtask, gtid, 0, argc, args
  1676. #if OMPT_SUPPORT
  1677. ,
  1678. exit_frame_p
  1679. #endif
  1680. );
  1681. }
  1682. #if OMPT_SUPPORT
  1683. if (ompt_enabled.enabled) {
  1684. *exit_frame_p = NULL;
  1685. if (ompt_enabled.ompt_callback_implicit_task) {
  1686. ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
  1687. ompt_scope_end, NULL, &(task_info->task_data), 1,
  1688. OMPT_CUR_TASK_INFO(master_th)->thread_num,
  1689. ompt_task_implicit);
  1690. }
  1691. ompt_parallel_data = *OMPT_CUR_TEAM_DATA(master_th);
  1692. __ompt_lw_taskteam_unlink(master_th);
  1693. if (ompt_enabled.ompt_callback_parallel_end) {
  1694. ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
  1695. &ompt_parallel_data, parent_task_data,
  1696. OMPT_INVOKER(call_context) | ompt_parallel_team,
  1697. return_address);
  1698. }
  1699. master_th->th.ompt_thread_info.state = ompt_state_overhead;
  1700. }
  1701. #endif
  1702. }
  1703. } else if (call_context == fork_context_gnu) {
  1704. #if OMPT_SUPPORT
  1705. ompt_lw_taskteam_t lwt;
  1706. __ompt_lw_taskteam_init(&lwt, master_th, gtid, &ompt_parallel_data,
  1707. return_address);
  1708. lwt.ompt_task_info.frame.exit_frame = ompt_data_none;
  1709. __ompt_lw_taskteam_link(&lwt, master_th, 1);
  1710. // don't use lw_taskteam after linking. content was swaped
  1711. #endif
  1712. // we were called from GNU native code
  1713. KA_TRACE(20, ("__kmp_fork_call: T#%d serial exit\n", gtid));
  1714. return FALSE;
  1715. } else {
  1716. KMP_ASSERT2(call_context < fork_context_last,
  1717. "__kmp_fork_call: unknown fork_context parameter");
  1718. }
  1719. KA_TRACE(20, ("__kmp_fork_call: T#%d serial exit\n", gtid));
  1720. KMP_MB();
  1721. return FALSE;
  1722. } // if (nthreads == 1)
  1723. // GEH: only modify the executing flag in the case when not serialized
  1724. // serialized case is handled in kmpc_serialized_parallel
  1725. KF_TRACE(10, ("__kmp_fork_call: parent_team_aclevel=%d, master_th=%p, "
  1726. "curtask=%p, curtask_max_aclevel=%d\n",
  1727. parent_team->t.t_active_level, master_th,
  1728. master_th->th.th_current_task,
  1729. master_th->th.th_current_task->td_icvs.max_active_levels));
  1730. // TODO: GEH - cannot do this assertion because root thread not set up as
  1731. // executing
  1732. // KMP_ASSERT( master_th->th.th_current_task->td_flags.executing == 1 );
  1733. master_th->th.th_current_task->td_flags.executing = 0;
  1734. if (!master_th->th.th_teams_microtask || level > teams_level) {
  1735. /* Increment our nested depth level */
  1736. KMP_ATOMIC_INC(&root->r.r_in_parallel);
  1737. }
  1738. // See if we need to make a copy of the ICVs.
  1739. int nthreads_icv = master_th->th.th_current_task->td_icvs.nproc;
  1740. if ((level + 1 < __kmp_nested_nth.used) &&
  1741. (__kmp_nested_nth.nth[level + 1] != nthreads_icv)) {
  1742. nthreads_icv = __kmp_nested_nth.nth[level + 1];
  1743. } else {
  1744. nthreads_icv = 0; // don't update
  1745. }
  1746. // Figure out the proc_bind_policy for the new team.
  1747. kmp_proc_bind_t proc_bind = master_th->th.th_set_proc_bind;
  1748. // proc_bind_default means don't update
  1749. kmp_proc_bind_t proc_bind_icv = proc_bind_default;
  1750. if (master_th->th.th_current_task->td_icvs.proc_bind == proc_bind_false) {
  1751. proc_bind = proc_bind_false;
  1752. } else {
  1753. // No proc_bind clause specified; use current proc-bind-var for this
  1754. // parallel region
  1755. if (proc_bind == proc_bind_default) {
  1756. proc_bind = master_th->th.th_current_task->td_icvs.proc_bind;
  1757. }
  1758. // Have teams construct take proc_bind value from KMP_TEAMS_PROC_BIND
  1759. if (master_th->th.th_teams_microtask &&
  1760. microtask == (microtask_t)__kmp_teams_master) {
  1761. proc_bind = __kmp_teams_proc_bind;
  1762. }
  1763. /* else: The proc_bind policy was specified explicitly on parallel clause.
  1764. This overrides proc-bind-var for this parallel region, but does not
  1765. change proc-bind-var. */
  1766. // Figure the value of proc-bind-var for the child threads.
  1767. if ((level + 1 < __kmp_nested_proc_bind.used) &&
  1768. (__kmp_nested_proc_bind.bind_types[level + 1] !=
  1769. master_th->th.th_current_task->td_icvs.proc_bind)) {
  1770. // Do not modify the proc bind icv for the two teams construct forks
  1771. // They just let the proc bind icv pass through
  1772. if (!master_th->th.th_teams_microtask ||
  1773. !(microtask == (microtask_t)__kmp_teams_master || ap == NULL))
  1774. proc_bind_icv = __kmp_nested_proc_bind.bind_types[level + 1];
  1775. }
  1776. }
  1777. // Reset for next parallel region
  1778. master_th->th.th_set_proc_bind = proc_bind_default;
  1779. if ((nthreads_icv > 0) || (proc_bind_icv != proc_bind_default)) {
  1780. kmp_internal_control_t new_icvs;
  1781. copy_icvs(&new_icvs, &master_th->th.th_current_task->td_icvs);
  1782. new_icvs.next = NULL;
  1783. if (nthreads_icv > 0) {
  1784. new_icvs.nproc = nthreads_icv;
  1785. }
  1786. if (proc_bind_icv != proc_bind_default) {
  1787. new_icvs.proc_bind = proc_bind_icv;
  1788. }
  1789. /* allocate a new parallel team */
  1790. KF_TRACE(10, ("__kmp_fork_call: before __kmp_allocate_team\n"));
  1791. team = __kmp_allocate_team(root, nthreads, nthreads,
  1792. #if OMPT_SUPPORT
  1793. ompt_parallel_data,
  1794. #endif
  1795. proc_bind, &new_icvs,
  1796. argc USE_NESTED_HOT_ARG(master_th));
  1797. if (__kmp_barrier_release_pattern[bs_forkjoin_barrier] == bp_dist_bar)
  1798. copy_icvs((kmp_internal_control_t *)team->t.b->team_icvs, &new_icvs);
  1799. } else {
  1800. /* allocate a new parallel team */
  1801. KF_TRACE(10, ("__kmp_fork_call: before __kmp_allocate_team\n"));
  1802. team = __kmp_allocate_team(root, nthreads, nthreads,
  1803. #if OMPT_SUPPORT
  1804. ompt_parallel_data,
  1805. #endif
  1806. proc_bind,
  1807. &master_th->th.th_current_task->td_icvs,
  1808. argc USE_NESTED_HOT_ARG(master_th));
  1809. if (__kmp_barrier_release_pattern[bs_forkjoin_barrier] == bp_dist_bar)
  1810. copy_icvs((kmp_internal_control_t *)team->t.b->team_icvs,
  1811. &master_th->th.th_current_task->td_icvs);
  1812. }
  1813. KF_TRACE(
  1814. 10, ("__kmp_fork_call: after __kmp_allocate_team - team = %p\n", team));
  1815. /* setup the new team */
  1816. KMP_CHECK_UPDATE(team->t.t_master_tid, master_tid);
  1817. KMP_CHECK_UPDATE(team->t.t_master_this_cons, master_this_cons);
  1818. KMP_CHECK_UPDATE(team->t.t_ident, loc);
  1819. KMP_CHECK_UPDATE(team->t.t_parent, parent_team);
  1820. KMP_CHECK_UPDATE_SYNC(team->t.t_pkfn, microtask);
  1821. #if OMPT_SUPPORT
  1822. KMP_CHECK_UPDATE_SYNC(team->t.ompt_team_info.master_return_address,
  1823. return_address);
  1824. #endif
  1825. KMP_CHECK_UPDATE(team->t.t_invoke, invoker); // TODO move to root, maybe
  1826. // TODO: parent_team->t.t_level == INT_MAX ???
  1827. if (!master_th->th.th_teams_microtask || level > teams_level) {
  1828. int new_level = parent_team->t.t_level + 1;
  1829. KMP_CHECK_UPDATE(team->t.t_level, new_level);
  1830. new_level = parent_team->t.t_active_level + 1;
  1831. KMP_CHECK_UPDATE(team->t.t_active_level, new_level);
  1832. } else {
  1833. // AC: Do not increase parallel level at start of the teams construct
  1834. int new_level = parent_team->t.t_level;
  1835. KMP_CHECK_UPDATE(team->t.t_level, new_level);
  1836. new_level = parent_team->t.t_active_level;
  1837. KMP_CHECK_UPDATE(team->t.t_active_level, new_level);
  1838. }
  1839. kmp_r_sched_t new_sched = get__sched_2(parent_team, master_tid);
  1840. // set primary thread's schedule as new run-time schedule
  1841. KMP_CHECK_UPDATE(team->t.t_sched.sched, new_sched.sched);
  1842. KMP_CHECK_UPDATE(team->t.t_cancel_request, cancel_noreq);
  1843. KMP_CHECK_UPDATE(team->t.t_def_allocator, master_th->th.th_def_allocator);
  1844. // Update the floating point rounding in the team if required.
  1845. propagateFPControl(team);
  1846. #if OMPD_SUPPORT
  1847. if (ompd_state & OMPD_ENABLE_BP)
  1848. ompd_bp_parallel_begin();
  1849. #endif
  1850. if (__kmp_tasking_mode != tskm_immediate_exec) {
  1851. // Set primary thread's task team to team's task team. Unless this is hot
  1852. // team, it should be NULL.
  1853. KMP_DEBUG_ASSERT(master_th->th.th_task_team ==
  1854. parent_team->t.t_task_team[master_th->th.th_task_state]);
  1855. KA_TRACE(20, ("__kmp_fork_call: Primary T#%d pushing task_team %p / team "
  1856. "%p, new task_team %p / team %p\n",
  1857. __kmp_gtid_from_thread(master_th),
  1858. master_th->th.th_task_team, parent_team,
  1859. team->t.t_task_team[master_th->th.th_task_state], team));
  1860. if (active_level || master_th->th.th_task_team) {
  1861. // Take a memo of primary thread's task_state
  1862. KMP_DEBUG_ASSERT(master_th->th.th_task_state_memo_stack);
  1863. if (master_th->th.th_task_state_top >=
  1864. master_th->th.th_task_state_stack_sz) { // increase size
  1865. kmp_uint32 new_size = 2 * master_th->th.th_task_state_stack_sz;
  1866. kmp_uint8 *old_stack, *new_stack;
  1867. kmp_uint32 i;
  1868. new_stack = (kmp_uint8 *)__kmp_allocate(new_size);
  1869. for (i = 0; i < master_th->th.th_task_state_stack_sz; ++i) {
  1870. new_stack[i] = master_th->th.th_task_state_memo_stack[i];
  1871. }
  1872. for (i = master_th->th.th_task_state_stack_sz; i < new_size;
  1873. ++i) { // zero-init rest of stack
  1874. new_stack[i] = 0;
  1875. }
  1876. old_stack = master_th->th.th_task_state_memo_stack;
  1877. master_th->th.th_task_state_memo_stack = new_stack;
  1878. master_th->th.th_task_state_stack_sz = new_size;
  1879. __kmp_free(old_stack);
  1880. }
  1881. // Store primary thread's task_state on stack
  1882. master_th->th
  1883. .th_task_state_memo_stack[master_th->th.th_task_state_top] =
  1884. master_th->th.th_task_state;
  1885. master_th->th.th_task_state_top++;
  1886. #if KMP_NESTED_HOT_TEAMS
  1887. if (master_th->th.th_hot_teams &&
  1888. active_level < __kmp_hot_teams_max_level &&
  1889. team == master_th->th.th_hot_teams[active_level].hot_team) {
  1890. // Restore primary thread's nested state if nested hot team
  1891. master_th->th.th_task_state =
  1892. master_th->th
  1893. .th_task_state_memo_stack[master_th->th.th_task_state_top];
  1894. } else {
  1895. #endif
  1896. master_th->th.th_task_state = 0;
  1897. #if KMP_NESTED_HOT_TEAMS
  1898. }
  1899. #endif
  1900. }
  1901. #if !KMP_NESTED_HOT_TEAMS
  1902. KMP_DEBUG_ASSERT((master_th->th.th_task_team == NULL) ||
  1903. (team == root->r.r_hot_team));
  1904. #endif
  1905. }
  1906. KA_TRACE(
  1907. 20,
  1908. ("__kmp_fork_call: T#%d(%d:%d)->(%d:0) created a team of %d threads\n",
  1909. gtid, parent_team->t.t_id, team->t.t_master_tid, team->t.t_id,
  1910. team->t.t_nproc));
  1911. KMP_DEBUG_ASSERT(team != root->r.r_hot_team ||
  1912. (team->t.t_master_tid == 0 &&
  1913. (team->t.t_parent == root->r.r_root_team ||
  1914. team->t.t_parent->t.t_serialized)));
  1915. KMP_MB();
  1916. /* now, setup the arguments */
  1917. argv = (void **)team->t.t_argv;
  1918. if (ap) {
  1919. for (i = argc - 1; i >= 0; --i) {
  1920. void *new_argv = va_arg(kmp_va_deref(ap), void *);
  1921. KMP_CHECK_UPDATE(*argv, new_argv);
  1922. argv++;
  1923. }
  1924. } else {
  1925. for (i = 0; i < argc; ++i) {
  1926. // Get args from parent team for teams construct
  1927. KMP_CHECK_UPDATE(argv[i], team->t.t_parent->t.t_argv[i]);
  1928. }
  1929. }
  1930. /* now actually fork the threads */
  1931. KMP_CHECK_UPDATE(team->t.t_master_active, master_active);
  1932. if (!root->r.r_active) // Only do assignment if it prevents cache ping-pong
  1933. root->r.r_active = TRUE;
  1934. __kmp_fork_team_threads(root, team, master_th, gtid, !ap);
  1935. __kmp_setup_icv_copy(team, nthreads,
  1936. &master_th->th.th_current_task->td_icvs, loc);
  1937. #if OMPT_SUPPORT
  1938. master_th->th.ompt_thread_info.state = ompt_state_work_parallel;
  1939. #endif
  1940. __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
  1941. #if USE_ITT_BUILD
  1942. if (team->t.t_active_level == 1 // only report frames at level 1
  1943. && !master_th->th.th_teams_microtask) { // not in teams construct
  1944. #if USE_ITT_NOTIFY
  1945. if ((__itt_frame_submit_v3_ptr || KMP_ITT_DEBUG) &&
  1946. (__kmp_forkjoin_frames_mode == 3 ||
  1947. __kmp_forkjoin_frames_mode == 1)) {
  1948. kmp_uint64 tmp_time = 0;
  1949. if (__itt_get_timestamp_ptr)
  1950. tmp_time = __itt_get_timestamp();
  1951. // Internal fork - report frame begin
  1952. master_th->th.th_frame_time = tmp_time;
  1953. if (__kmp_forkjoin_frames_mode == 3)
  1954. team->t.t_region_time = tmp_time;
  1955. } else
  1956. // only one notification scheme (either "submit" or "forking/joined", not both)
  1957. #endif /* USE_ITT_NOTIFY */
  1958. if ((__itt_frame_begin_v3_ptr || KMP_ITT_DEBUG) &&
  1959. __kmp_forkjoin_frames && !__kmp_forkjoin_frames_mode) {
  1960. // Mark start of "parallel" region for Intel(R) VTune(TM) analyzer.
  1961. __kmp_itt_region_forking(gtid, team->t.t_nproc, 0);
  1962. }
  1963. }
  1964. #endif /* USE_ITT_BUILD */
  1965. /* now go on and do the work */
  1966. KMP_DEBUG_ASSERT(team == __kmp_threads[gtid]->th.th_team);
  1967. KMP_MB();
  1968. KF_TRACE(10,
  1969. ("__kmp_internal_fork : root=%p, team=%p, master_th=%p, gtid=%d\n",
  1970. root, team, master_th, gtid));
  1971. #if USE_ITT_BUILD
  1972. if (__itt_stack_caller_create_ptr) {
  1973. // create new stack stitching id before entering fork barrier
  1974. if (!enter_teams) {
  1975. KMP_DEBUG_ASSERT(team->t.t_stack_id == NULL);
  1976. team->t.t_stack_id = __kmp_itt_stack_caller_create();
  1977. } else if (parent_team->t.t_serialized) {
  1978. // keep stack stitching id in the serialized parent_team;
  1979. // current team will be used for parallel inside the teams;
  1980. // if parent_team is active, then it already keeps stack stitching id
  1981. // for the league of teams
  1982. KMP_DEBUG_ASSERT(parent_team->t.t_stack_id == NULL);
  1983. parent_team->t.t_stack_id = __kmp_itt_stack_caller_create();
  1984. }
  1985. }
  1986. #endif /* USE_ITT_BUILD */
  1987. // AC: skip __kmp_internal_fork at teams construct, let only primary
  1988. // threads execute
  1989. if (ap) {
  1990. __kmp_internal_fork(loc, gtid, team);
  1991. KF_TRACE(10, ("__kmp_internal_fork : after : root=%p, team=%p, "
  1992. "master_th=%p, gtid=%d\n",
  1993. root, team, master_th, gtid));
  1994. }
  1995. if (call_context == fork_context_gnu) {
  1996. KA_TRACE(20, ("__kmp_fork_call: parallel exit T#%d\n", gtid));
  1997. return TRUE;
  1998. }
  1999. /* Invoke microtask for PRIMARY thread */
  2000. KA_TRACE(20, ("__kmp_fork_call: T#%d(%d:0) invoke microtask = %p\n", gtid,
  2001. team->t.t_id, team->t.t_pkfn));
  2002. } // END of timer KMP_fork_call block
  2003. #if KMP_STATS_ENABLED
  2004. // If beginning a teams construct, then change thread state
  2005. stats_state_e previous_state = KMP_GET_THREAD_STATE();
  2006. if (!ap) {
  2007. KMP_SET_THREAD_STATE(stats_state_e::TEAMS_REGION);
  2008. }
  2009. #endif
  2010. if (!team->t.t_invoke(gtid)) {
  2011. KMP_ASSERT2(0, "cannot invoke microtask for PRIMARY thread");
  2012. }
  2013. #if KMP_STATS_ENABLED
  2014. // If was beginning of a teams construct, then reset thread state
  2015. if (!ap) {
  2016. KMP_SET_THREAD_STATE(previous_state);
  2017. }
  2018. #endif
  2019. KA_TRACE(20, ("__kmp_fork_call: T#%d(%d:0) done microtask = %p\n", gtid,
  2020. team->t.t_id, team->t.t_pkfn));
  2021. KMP_MB(); /* Flush all pending memory write invalidates. */
  2022. KA_TRACE(20, ("__kmp_fork_call: parallel exit T#%d\n", gtid));
  2023. #if OMPT_SUPPORT
  2024. if (ompt_enabled.enabled) {
  2025. master_th->th.ompt_thread_info.state = ompt_state_overhead;
  2026. }
  2027. #endif
  2028. return TRUE;
  2029. }
  2030. #if OMPT_SUPPORT
  2031. static inline void __kmp_join_restore_state(kmp_info_t *thread,
  2032. kmp_team_t *team) {
  2033. // restore state outside the region
  2034. thread->th.ompt_thread_info.state =
  2035. ((team->t.t_serialized) ? ompt_state_work_serial
  2036. : ompt_state_work_parallel);
  2037. }
  2038. static inline void __kmp_join_ompt(int gtid, kmp_info_t *thread,
  2039. kmp_team_t *team, ompt_data_t *parallel_data,
  2040. int flags, void *codeptr) {
  2041. ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
  2042. if (ompt_enabled.ompt_callback_parallel_end) {
  2043. ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
  2044. parallel_data, &(task_info->task_data), flags, codeptr);
  2045. }
  2046. task_info->frame.enter_frame = ompt_data_none;
  2047. __kmp_join_restore_state(thread, team);
  2048. }
  2049. #endif
  2050. void __kmp_join_call(ident_t *loc, int gtid
  2051. #if OMPT_SUPPORT
  2052. ,
  2053. enum fork_context_e fork_context
  2054. #endif
  2055. ,
  2056. int exit_teams) {
  2057. KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(KMP_join_call);
  2058. kmp_team_t *team;
  2059. kmp_team_t *parent_team;
  2060. kmp_info_t *master_th;
  2061. kmp_root_t *root;
  2062. int master_active;
  2063. KA_TRACE(20, ("__kmp_join_call: enter T#%d\n", gtid));
  2064. /* setup current data */
  2065. master_th = __kmp_threads[gtid];
  2066. root = master_th->th.th_root;
  2067. team = master_th->th.th_team;
  2068. parent_team = team->t.t_parent;
  2069. master_th->th.th_ident = loc;
  2070. #if OMPT_SUPPORT
  2071. void *team_microtask = (void *)team->t.t_pkfn;
  2072. // For GOMP interface with serialized parallel, need the
  2073. // __kmpc_end_serialized_parallel to call hooks for OMPT end-implicit-task
  2074. // and end-parallel events.
  2075. if (ompt_enabled.enabled &&
  2076. !(team->t.t_serialized && fork_context == fork_context_gnu)) {
  2077. master_th->th.ompt_thread_info.state = ompt_state_overhead;
  2078. }
  2079. #endif
  2080. #if KMP_DEBUG
  2081. if (__kmp_tasking_mode != tskm_immediate_exec && !exit_teams) {
  2082. KA_TRACE(20, ("__kmp_join_call: T#%d, old team = %p old task_team = %p, "
  2083. "th_task_team = %p\n",
  2084. __kmp_gtid_from_thread(master_th), team,
  2085. team->t.t_task_team[master_th->th.th_task_state],
  2086. master_th->th.th_task_team));
  2087. KMP_DEBUG_ASSERT(master_th->th.th_task_team ==
  2088. team->t.t_task_team[master_th->th.th_task_state]);
  2089. }
  2090. #endif
  2091. if (team->t.t_serialized) {
  2092. if (master_th->th.th_teams_microtask) {
  2093. // We are in teams construct
  2094. int level = team->t.t_level;
  2095. int tlevel = master_th->th.th_teams_level;
  2096. if (level == tlevel) {
  2097. // AC: we haven't incremented it earlier at start of teams construct,
  2098. // so do it here - at the end of teams construct
  2099. team->t.t_level++;
  2100. } else if (level == tlevel + 1) {
  2101. // AC: we are exiting parallel inside teams, need to increment
  2102. // serialization in order to restore it in the next call to
  2103. // __kmpc_end_serialized_parallel
  2104. team->t.t_serialized++;
  2105. }
  2106. }
  2107. __kmpc_end_serialized_parallel(loc, gtid);
  2108. #if OMPT_SUPPORT
  2109. if (ompt_enabled.enabled) {
  2110. __kmp_join_restore_state(master_th, parent_team);
  2111. }
  2112. #endif
  2113. return;
  2114. }
  2115. master_active = team->t.t_master_active;
  2116. if (!exit_teams) {
  2117. // AC: No barrier for internal teams at exit from teams construct.
  2118. // But there is barrier for external team (league).
  2119. __kmp_internal_join(loc, gtid, team);
  2120. #if USE_ITT_BUILD
  2121. if (__itt_stack_caller_create_ptr) {
  2122. KMP_DEBUG_ASSERT(team->t.t_stack_id != NULL);
  2123. // destroy the stack stitching id after join barrier
  2124. __kmp_itt_stack_caller_destroy((__itt_caller)team->t.t_stack_id);
  2125. team->t.t_stack_id = NULL;
  2126. }
  2127. #endif
  2128. } else {
  2129. master_th->th.th_task_state =
  2130. 0; // AC: no tasking in teams (out of any parallel)
  2131. #if USE_ITT_BUILD
  2132. if (__itt_stack_caller_create_ptr && parent_team->t.t_serialized) {
  2133. KMP_DEBUG_ASSERT(parent_team->t.t_stack_id != NULL);
  2134. // destroy the stack stitching id on exit from the teams construct
  2135. // if parent_team is active, then the id will be destroyed later on
  2136. // by master of the league of teams
  2137. __kmp_itt_stack_caller_destroy((__itt_caller)parent_team->t.t_stack_id);
  2138. parent_team->t.t_stack_id = NULL;
  2139. }
  2140. #endif
  2141. if (team->t.t_nproc > 1 &&
  2142. __kmp_barrier_gather_pattern[bs_forkjoin_barrier] == bp_dist_bar) {
  2143. team->t.b->update_num_threads(team->t.t_nproc);
  2144. __kmp_add_threads_to_team(team, team->t.t_nproc);
  2145. }
  2146. }
  2147. KMP_MB();
  2148. #if OMPT_SUPPORT
  2149. ompt_data_t *parallel_data = &(team->t.ompt_team_info.parallel_data);
  2150. void *codeptr = team->t.ompt_team_info.master_return_address;
  2151. #endif
  2152. #if USE_ITT_BUILD
  2153. // Mark end of "parallel" region for Intel(R) VTune(TM) analyzer.
  2154. if (team->t.t_active_level == 1 &&
  2155. (!master_th->th.th_teams_microtask || /* not in teams construct */
  2156. master_th->th.th_teams_size.nteams == 1)) {
  2157. master_th->th.th_ident = loc;
  2158. // only one notification scheme (either "submit" or "forking/joined", not
  2159. // both)
  2160. if ((__itt_frame_submit_v3_ptr || KMP_ITT_DEBUG) &&
  2161. __kmp_forkjoin_frames_mode == 3)
  2162. __kmp_itt_frame_submit(gtid, team->t.t_region_time,
  2163. master_th->th.th_frame_time, 0, loc,
  2164. master_th->th.th_team_nproc, 1);
  2165. else if ((__itt_frame_end_v3_ptr || KMP_ITT_DEBUG) &&
  2166. !__kmp_forkjoin_frames_mode && __kmp_forkjoin_frames)
  2167. __kmp_itt_region_joined(gtid);
  2168. } // active_level == 1
  2169. #endif /* USE_ITT_BUILD */
  2170. #if KMP_AFFINITY_SUPPORTED
  2171. if (!exit_teams) {
  2172. // Restore master thread's partition.
  2173. master_th->th.th_first_place = team->t.t_first_place;
  2174. master_th->th.th_last_place = team->t.t_last_place;
  2175. }
  2176. #endif // KMP_AFFINITY_SUPPORTED
  2177. if (master_th->th.th_teams_microtask && !exit_teams &&
  2178. team->t.t_pkfn != (microtask_t)__kmp_teams_master &&
  2179. team->t.t_level == master_th->th.th_teams_level + 1) {
  2180. // AC: We need to leave the team structure intact at the end of parallel
  2181. // inside the teams construct, so that at the next parallel same (hot) team
  2182. // works, only adjust nesting levels
  2183. #if OMPT_SUPPORT
  2184. ompt_data_t ompt_parallel_data = ompt_data_none;
  2185. if (ompt_enabled.enabled) {
  2186. ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
  2187. if (ompt_enabled.ompt_callback_implicit_task) {
  2188. int ompt_team_size = team->t.t_nproc;
  2189. ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
  2190. ompt_scope_end, NULL, &(task_info->task_data), ompt_team_size,
  2191. OMPT_CUR_TASK_INFO(master_th)->thread_num, ompt_task_implicit);
  2192. }
  2193. task_info->frame.exit_frame = ompt_data_none;
  2194. task_info->task_data = ompt_data_none;
  2195. ompt_parallel_data = *OMPT_CUR_TEAM_DATA(master_th);
  2196. __ompt_lw_taskteam_unlink(master_th);
  2197. }
  2198. #endif
  2199. /* Decrement our nested depth level */
  2200. team->t.t_level--;
  2201. team->t.t_active_level--;
  2202. KMP_ATOMIC_DEC(&root->r.r_in_parallel);
  2203. // Restore number of threads in the team if needed. This code relies on
  2204. // the proper adjustment of th_teams_size.nth after the fork in
  2205. // __kmp_teams_master on each teams primary thread in the case that
  2206. // __kmp_reserve_threads reduced it.
  2207. if (master_th->th.th_team_nproc < master_th->th.th_teams_size.nth) {
  2208. int old_num = master_th->th.th_team_nproc;
  2209. int new_num = master_th->th.th_teams_size.nth;
  2210. kmp_info_t **other_threads = team->t.t_threads;
  2211. team->t.t_nproc = new_num;
  2212. for (int i = 0; i < old_num; ++i) {
  2213. other_threads[i]->th.th_team_nproc = new_num;
  2214. }
  2215. // Adjust states of non-used threads of the team
  2216. for (int i = old_num; i < new_num; ++i) {
  2217. // Re-initialize thread's barrier data.
  2218. KMP_DEBUG_ASSERT(other_threads[i]);
  2219. kmp_balign_t *balign = other_threads[i]->th.th_bar;
  2220. for (int b = 0; b < bs_last_barrier; ++b) {
  2221. balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived;
  2222. KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG);
  2223. #if USE_DEBUGGER
  2224. balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived;
  2225. #endif
  2226. }
  2227. if (__kmp_tasking_mode != tskm_immediate_exec) {
  2228. // Synchronize thread's task state
  2229. other_threads[i]->th.th_task_state = master_th->th.th_task_state;
  2230. }
  2231. }
  2232. }
  2233. #if OMPT_SUPPORT
  2234. if (ompt_enabled.enabled) {
  2235. __kmp_join_ompt(gtid, master_th, parent_team, &ompt_parallel_data,
  2236. OMPT_INVOKER(fork_context) | ompt_parallel_team, codeptr);
  2237. }
  2238. #endif
  2239. return;
  2240. }
  2241. /* do cleanup and restore the parent team */
  2242. master_th->th.th_info.ds.ds_tid = team->t.t_master_tid;
  2243. master_th->th.th_local.this_construct = team->t.t_master_this_cons;
  2244. master_th->th.th_dispatch = &parent_team->t.t_dispatch[team->t.t_master_tid];
  2245. /* jc: The following lock has instructions with REL and ACQ semantics,
  2246. separating the parallel user code called in this parallel region
  2247. from the serial user code called after this function returns. */
  2248. __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
  2249. if (!master_th->th.th_teams_microtask ||
  2250. team->t.t_level > master_th->th.th_teams_level) {
  2251. /* Decrement our nested depth level */
  2252. KMP_ATOMIC_DEC(&root->r.r_in_parallel);
  2253. }
  2254. KMP_DEBUG_ASSERT(root->r.r_in_parallel >= 0);
  2255. #if OMPT_SUPPORT
  2256. if (ompt_enabled.enabled) {
  2257. ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
  2258. if (ompt_enabled.ompt_callback_implicit_task) {
  2259. int flags = (team_microtask == (void *)__kmp_teams_master)
  2260. ? ompt_task_initial
  2261. : ompt_task_implicit;
  2262. int ompt_team_size = (flags == ompt_task_initial) ? 0 : team->t.t_nproc;
  2263. ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
  2264. ompt_scope_end, NULL, &(task_info->task_data), ompt_team_size,
  2265. OMPT_CUR_TASK_INFO(master_th)->thread_num, flags);
  2266. }
  2267. task_info->frame.exit_frame = ompt_data_none;
  2268. task_info->task_data = ompt_data_none;
  2269. }
  2270. #endif
  2271. KF_TRACE(10, ("__kmp_join_call1: T#%d, this_thread=%p team=%p\n", 0,
  2272. master_th, team));
  2273. __kmp_pop_current_task_from_thread(master_th);
  2274. master_th->th.th_def_allocator = team->t.t_def_allocator;
  2275. #if OMPD_SUPPORT
  2276. if (ompd_state & OMPD_ENABLE_BP)
  2277. ompd_bp_parallel_end();
  2278. #endif
  2279. updateHWFPControl(team);
  2280. if (root->r.r_active != master_active)
  2281. root->r.r_active = master_active;
  2282. __kmp_free_team(root, team USE_NESTED_HOT_ARG(
  2283. master_th)); // this will free worker threads
  2284. /* this race was fun to find. make sure the following is in the critical
  2285. region otherwise assertions may fail occasionally since the old team may be
  2286. reallocated and the hierarchy appears inconsistent. it is actually safe to
  2287. run and won't cause any bugs, but will cause those assertion failures. it's
  2288. only one deref&assign so might as well put this in the critical region */
  2289. master_th->th.th_team = parent_team;
  2290. master_th->th.th_team_nproc = parent_team->t.t_nproc;
  2291. master_th->th.th_team_master = parent_team->t.t_threads[0];
  2292. master_th->th.th_team_serialized = parent_team->t.t_serialized;
  2293. /* restore serialized team, if need be */
  2294. if (parent_team->t.t_serialized &&
  2295. parent_team != master_th->th.th_serial_team &&
  2296. parent_team != root->r.r_root_team) {
  2297. __kmp_free_team(root,
  2298. master_th->th.th_serial_team USE_NESTED_HOT_ARG(NULL));
  2299. master_th->th.th_serial_team = parent_team;
  2300. }
  2301. if (__kmp_tasking_mode != tskm_immediate_exec) {
  2302. if (master_th->th.th_task_state_top >
  2303. 0) { // Restore task state from memo stack
  2304. KMP_DEBUG_ASSERT(master_th->th.th_task_state_memo_stack);
  2305. // Remember primary thread's state if we re-use this nested hot team
  2306. master_th->th.th_task_state_memo_stack[master_th->th.th_task_state_top] =
  2307. master_th->th.th_task_state;
  2308. --master_th->th.th_task_state_top; // pop
  2309. // Now restore state at this level
  2310. master_th->th.th_task_state =
  2311. master_th->th
  2312. .th_task_state_memo_stack[master_th->th.th_task_state_top];
  2313. }
  2314. // Copy the task team from the parent team to the primary thread
  2315. master_th->th.th_task_team =
  2316. parent_team->t.t_task_team[master_th->th.th_task_state];
  2317. KA_TRACE(20,
  2318. ("__kmp_join_call: Primary T#%d restoring task_team %p, team %p\n",
  2319. __kmp_gtid_from_thread(master_th), master_th->th.th_task_team,
  2320. parent_team));
  2321. }
  2322. // TODO: GEH - cannot do this assertion because root thread not set up as
  2323. // executing
  2324. // KMP_ASSERT( master_th->th.th_current_task->td_flags.executing == 0 );
  2325. master_th->th.th_current_task->td_flags.executing = 1;
  2326. __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
  2327. #if KMP_AFFINITY_SUPPORTED
  2328. if (master_th->th.th_team->t.t_level == 0 && __kmp_affin_reset) {
  2329. __kmp_reset_root_init_mask(gtid);
  2330. }
  2331. #endif
  2332. #if OMPT_SUPPORT
  2333. int flags =
  2334. OMPT_INVOKER(fork_context) |
  2335. ((team_microtask == (void *)__kmp_teams_master) ? ompt_parallel_league
  2336. : ompt_parallel_team);
  2337. if (ompt_enabled.enabled) {
  2338. __kmp_join_ompt(gtid, master_th, parent_team, parallel_data, flags,
  2339. codeptr);
  2340. }
  2341. #endif
  2342. KMP_MB();
  2343. KA_TRACE(20, ("__kmp_join_call: exit T#%d\n", gtid));
  2344. }
  2345. /* Check whether we should push an internal control record onto the
  2346. serial team stack. If so, do it. */
  2347. void __kmp_save_internal_controls(kmp_info_t *thread) {
  2348. if (thread->th.th_team != thread->th.th_serial_team) {
  2349. return;
  2350. }
  2351. if (thread->th.th_team->t.t_serialized > 1) {
  2352. int push = 0;
  2353. if (thread->th.th_team->t.t_control_stack_top == NULL) {
  2354. push = 1;
  2355. } else {
  2356. if (thread->th.th_team->t.t_control_stack_top->serial_nesting_level !=
  2357. thread->th.th_team->t.t_serialized) {
  2358. push = 1;
  2359. }
  2360. }
  2361. if (push) { /* push a record on the serial team's stack */
  2362. kmp_internal_control_t *control =
  2363. (kmp_internal_control_t *)__kmp_allocate(
  2364. sizeof(kmp_internal_control_t));
  2365. copy_icvs(control, &thread->th.th_current_task->td_icvs);
  2366. control->serial_nesting_level = thread->th.th_team->t.t_serialized;
  2367. control->next = thread->th.th_team->t.t_control_stack_top;
  2368. thread->th.th_team->t.t_control_stack_top = control;
  2369. }
  2370. }
  2371. }
  2372. /* Changes set_nproc */
  2373. void __kmp_set_num_threads(int new_nth, int gtid) {
  2374. kmp_info_t *thread;
  2375. kmp_root_t *root;
  2376. KF_TRACE(10, ("__kmp_set_num_threads: new __kmp_nth = %d\n", new_nth));
  2377. KMP_DEBUG_ASSERT(__kmp_init_serial);
  2378. if (new_nth < 1)
  2379. new_nth = 1;
  2380. else if (new_nth > __kmp_max_nth)
  2381. new_nth = __kmp_max_nth;
  2382. KMP_COUNT_VALUE(OMP_set_numthreads, new_nth);
  2383. thread = __kmp_threads[gtid];
  2384. if (thread->th.th_current_task->td_icvs.nproc == new_nth)
  2385. return; // nothing to do
  2386. __kmp_save_internal_controls(thread);
  2387. set__nproc(thread, new_nth);
  2388. // If this omp_set_num_threads() call will cause the hot team size to be
  2389. // reduced (in the absence of a num_threads clause), then reduce it now,
  2390. // rather than waiting for the next parallel region.
  2391. root = thread->th.th_root;
  2392. if (__kmp_init_parallel && (!root->r.r_active) &&
  2393. (root->r.r_hot_team->t.t_nproc > new_nth)
  2394. #if KMP_NESTED_HOT_TEAMS
  2395. && __kmp_hot_teams_max_level && !__kmp_hot_teams_mode
  2396. #endif
  2397. ) {
  2398. kmp_team_t *hot_team = root->r.r_hot_team;
  2399. int f;
  2400. __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
  2401. if (__kmp_barrier_release_pattern[bs_forkjoin_barrier] == bp_dist_bar) {
  2402. __kmp_resize_dist_barrier(hot_team, hot_team->t.t_nproc, new_nth);
  2403. }
  2404. // Release the extra threads we don't need any more.
  2405. for (f = new_nth; f < hot_team->t.t_nproc; f++) {
  2406. KMP_DEBUG_ASSERT(hot_team->t.t_threads[f] != NULL);
  2407. if (__kmp_tasking_mode != tskm_immediate_exec) {
  2408. // When decreasing team size, threads no longer in the team should unref
  2409. // task team.
  2410. hot_team->t.t_threads[f]->th.th_task_team = NULL;
  2411. }
  2412. __kmp_free_thread(hot_team->t.t_threads[f]);
  2413. hot_team->t.t_threads[f] = NULL;
  2414. }
  2415. hot_team->t.t_nproc = new_nth;
  2416. #if KMP_NESTED_HOT_TEAMS
  2417. if (thread->th.th_hot_teams) {
  2418. KMP_DEBUG_ASSERT(hot_team == thread->th.th_hot_teams[0].hot_team);
  2419. thread->th.th_hot_teams[0].hot_team_nth = new_nth;
  2420. }
  2421. #endif
  2422. if (__kmp_barrier_release_pattern[bs_forkjoin_barrier] == bp_dist_bar) {
  2423. hot_team->t.b->update_num_threads(new_nth);
  2424. __kmp_add_threads_to_team(hot_team, new_nth);
  2425. }
  2426. __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
  2427. // Update the t_nproc field in the threads that are still active.
  2428. for (f = 0; f < new_nth; f++) {
  2429. KMP_DEBUG_ASSERT(hot_team->t.t_threads[f] != NULL);
  2430. hot_team->t.t_threads[f]->th.th_team_nproc = new_nth;
  2431. }
  2432. // Special flag in case omp_set_num_threads() call
  2433. hot_team->t.t_size_changed = -1;
  2434. }
  2435. }
  2436. /* Changes max_active_levels */
  2437. void __kmp_set_max_active_levels(int gtid, int max_active_levels) {
  2438. kmp_info_t *thread;
  2439. KF_TRACE(10, ("__kmp_set_max_active_levels: new max_active_levels for thread "
  2440. "%d = (%d)\n",
  2441. gtid, max_active_levels));
  2442. KMP_DEBUG_ASSERT(__kmp_init_serial);
  2443. // validate max_active_levels
  2444. if (max_active_levels < 0) {
  2445. KMP_WARNING(ActiveLevelsNegative, max_active_levels);
  2446. // We ignore this call if the user has specified a negative value.
  2447. // The current setting won't be changed. The last valid setting will be
  2448. // used. A warning will be issued (if warnings are allowed as controlled by
  2449. // the KMP_WARNINGS env var).
  2450. KF_TRACE(10, ("__kmp_set_max_active_levels: the call is ignored: new "
  2451. "max_active_levels for thread %d = (%d)\n",
  2452. gtid, max_active_levels));
  2453. return;
  2454. }
  2455. if (max_active_levels <= KMP_MAX_ACTIVE_LEVELS_LIMIT) {
  2456. // it's OK, the max_active_levels is within the valid range: [ 0;
  2457. // KMP_MAX_ACTIVE_LEVELS_LIMIT ]
  2458. // We allow a zero value. (implementation defined behavior)
  2459. } else {
  2460. KMP_WARNING(ActiveLevelsExceedLimit, max_active_levels,
  2461. KMP_MAX_ACTIVE_LEVELS_LIMIT);
  2462. max_active_levels = KMP_MAX_ACTIVE_LEVELS_LIMIT;
  2463. // Current upper limit is MAX_INT. (implementation defined behavior)
  2464. // If the input exceeds the upper limit, we correct the input to be the
  2465. // upper limit. (implementation defined behavior)
  2466. // Actually, the flow should never get here until we use MAX_INT limit.
  2467. }
  2468. KF_TRACE(10, ("__kmp_set_max_active_levels: after validation: new "
  2469. "max_active_levels for thread %d = (%d)\n",
  2470. gtid, max_active_levels));
  2471. thread = __kmp_threads[gtid];
  2472. __kmp_save_internal_controls(thread);
  2473. set__max_active_levels(thread, max_active_levels);
  2474. }
  2475. /* Gets max_active_levels */
  2476. int __kmp_get_max_active_levels(int gtid) {
  2477. kmp_info_t *thread;
  2478. KF_TRACE(10, ("__kmp_get_max_active_levels: thread %d\n", gtid));
  2479. KMP_DEBUG_ASSERT(__kmp_init_serial);
  2480. thread = __kmp_threads[gtid];
  2481. KMP_DEBUG_ASSERT(thread->th.th_current_task);
  2482. KF_TRACE(10, ("__kmp_get_max_active_levels: thread %d, curtask=%p, "
  2483. "curtask_maxaclevel=%d\n",
  2484. gtid, thread->th.th_current_task,
  2485. thread->th.th_current_task->td_icvs.max_active_levels));
  2486. return thread->th.th_current_task->td_icvs.max_active_levels;
  2487. }
  2488. // nteams-var per-device ICV
  2489. void __kmp_set_num_teams(int num_teams) {
  2490. if (num_teams > 0)
  2491. __kmp_nteams = num_teams;
  2492. }
  2493. int __kmp_get_max_teams(void) { return __kmp_nteams; }
  2494. // teams-thread-limit-var per-device ICV
  2495. void __kmp_set_teams_thread_limit(int limit) {
  2496. if (limit > 0)
  2497. __kmp_teams_thread_limit = limit;
  2498. }
  2499. int __kmp_get_teams_thread_limit(void) { return __kmp_teams_thread_limit; }
  2500. KMP_BUILD_ASSERT(sizeof(kmp_sched_t) == sizeof(int));
  2501. KMP_BUILD_ASSERT(sizeof(enum sched_type) == sizeof(int));
  2502. /* Changes def_sched_var ICV values (run-time schedule kind and chunk) */
  2503. void __kmp_set_schedule(int gtid, kmp_sched_t kind, int chunk) {
  2504. kmp_info_t *thread;
  2505. kmp_sched_t orig_kind;
  2506. // kmp_team_t *team;
  2507. KF_TRACE(10, ("__kmp_set_schedule: new schedule for thread %d = (%d, %d)\n",
  2508. gtid, (int)kind, chunk));
  2509. KMP_DEBUG_ASSERT(__kmp_init_serial);
  2510. // Check if the kind parameter is valid, correct if needed.
  2511. // Valid parameters should fit in one of two intervals - standard or extended:
  2512. // <lower>, <valid>, <upper_std>, <lower_ext>, <valid>, <upper>
  2513. // 2008-01-25: 0, 1 - 4, 5, 100, 101 - 102, 103
  2514. orig_kind = kind;
  2515. kind = __kmp_sched_without_mods(kind);
  2516. if (kind <= kmp_sched_lower || kind >= kmp_sched_upper ||
  2517. (kind <= kmp_sched_lower_ext && kind >= kmp_sched_upper_std)) {
  2518. // TODO: Hint needs attention in case we change the default schedule.
  2519. __kmp_msg(kmp_ms_warning, KMP_MSG(ScheduleKindOutOfRange, kind),
  2520. KMP_HNT(DefaultScheduleKindUsed, "static, no chunk"),
  2521. __kmp_msg_null);
  2522. kind = kmp_sched_default;
  2523. chunk = 0; // ignore chunk value in case of bad kind
  2524. }
  2525. thread = __kmp_threads[gtid];
  2526. __kmp_save_internal_controls(thread);
  2527. if (kind < kmp_sched_upper_std) {
  2528. if (kind == kmp_sched_static && chunk < KMP_DEFAULT_CHUNK) {
  2529. // differ static chunked vs. unchunked: chunk should be invalid to
  2530. // indicate unchunked schedule (which is the default)
  2531. thread->th.th_current_task->td_icvs.sched.r_sched_type = kmp_sch_static;
  2532. } else {
  2533. thread->th.th_current_task->td_icvs.sched.r_sched_type =
  2534. __kmp_sch_map[kind - kmp_sched_lower - 1];
  2535. }
  2536. } else {
  2537. // __kmp_sch_map[ kind - kmp_sched_lower_ext + kmp_sched_upper_std -
  2538. // kmp_sched_lower - 2 ];
  2539. thread->th.th_current_task->td_icvs.sched.r_sched_type =
  2540. __kmp_sch_map[kind - kmp_sched_lower_ext + kmp_sched_upper_std -
  2541. kmp_sched_lower - 2];
  2542. }
  2543. __kmp_sched_apply_mods_intkind(
  2544. orig_kind, &(thread->th.th_current_task->td_icvs.sched.r_sched_type));
  2545. if (kind == kmp_sched_auto || chunk < 1) {
  2546. // ignore parameter chunk for schedule auto
  2547. thread->th.th_current_task->td_icvs.sched.chunk = KMP_DEFAULT_CHUNK;
  2548. } else {
  2549. thread->th.th_current_task->td_icvs.sched.chunk = chunk;
  2550. }
  2551. }
  2552. /* Gets def_sched_var ICV values */
  2553. void __kmp_get_schedule(int gtid, kmp_sched_t *kind, int *chunk) {
  2554. kmp_info_t *thread;
  2555. enum sched_type th_type;
  2556. KF_TRACE(10, ("__kmp_get_schedule: thread %d\n", gtid));
  2557. KMP_DEBUG_ASSERT(__kmp_init_serial);
  2558. thread = __kmp_threads[gtid];
  2559. th_type = thread->th.th_current_task->td_icvs.sched.r_sched_type;
  2560. switch (SCHEDULE_WITHOUT_MODIFIERS(th_type)) {
  2561. case kmp_sch_static:
  2562. case kmp_sch_static_greedy:
  2563. case kmp_sch_static_balanced:
  2564. *kind = kmp_sched_static;
  2565. __kmp_sched_apply_mods_stdkind(kind, th_type);
  2566. *chunk = 0; // chunk was not set, try to show this fact via zero value
  2567. return;
  2568. case kmp_sch_static_chunked:
  2569. *kind = kmp_sched_static;
  2570. break;
  2571. case kmp_sch_dynamic_chunked:
  2572. *kind = kmp_sched_dynamic;
  2573. break;
  2574. case kmp_sch_guided_chunked:
  2575. case kmp_sch_guided_iterative_chunked:
  2576. case kmp_sch_guided_analytical_chunked:
  2577. *kind = kmp_sched_guided;
  2578. break;
  2579. case kmp_sch_auto:
  2580. *kind = kmp_sched_auto;
  2581. break;
  2582. case kmp_sch_trapezoidal:
  2583. *kind = kmp_sched_trapezoidal;
  2584. break;
  2585. #if KMP_STATIC_STEAL_ENABLED
  2586. case kmp_sch_static_steal:
  2587. *kind = kmp_sched_static_steal;
  2588. break;
  2589. #endif
  2590. default:
  2591. KMP_FATAL(UnknownSchedulingType, th_type);
  2592. }
  2593. __kmp_sched_apply_mods_stdkind(kind, th_type);
  2594. *chunk = thread->th.th_current_task->td_icvs.sched.chunk;
  2595. }
  2596. int __kmp_get_ancestor_thread_num(int gtid, int level) {
  2597. int ii, dd;
  2598. kmp_team_t *team;
  2599. kmp_info_t *thr;
  2600. KF_TRACE(10, ("__kmp_get_ancestor_thread_num: thread %d %d\n", gtid, level));
  2601. KMP_DEBUG_ASSERT(__kmp_init_serial);
  2602. // validate level
  2603. if (level == 0)
  2604. return 0;
  2605. if (level < 0)
  2606. return -1;
  2607. thr = __kmp_threads[gtid];
  2608. team = thr->th.th_team;
  2609. ii = team->t.t_level;
  2610. if (level > ii)
  2611. return -1;
  2612. if (thr->th.th_teams_microtask) {
  2613. // AC: we are in teams region where multiple nested teams have same level
  2614. int tlevel = thr->th.th_teams_level; // the level of the teams construct
  2615. if (level <=
  2616. tlevel) { // otherwise usual algorithm works (will not touch the teams)
  2617. KMP_DEBUG_ASSERT(ii >= tlevel);
  2618. // AC: As we need to pass by the teams league, we need to artificially
  2619. // increase ii
  2620. if (ii == tlevel) {
  2621. ii += 2; // three teams have same level
  2622. } else {
  2623. ii++; // two teams have same level
  2624. }
  2625. }
  2626. }
  2627. if (ii == level)
  2628. return __kmp_tid_from_gtid(gtid);
  2629. dd = team->t.t_serialized;
  2630. level++;
  2631. while (ii > level) {
  2632. for (dd = team->t.t_serialized; (dd > 0) && (ii > level); dd--, ii--) {
  2633. }
  2634. if ((team->t.t_serialized) && (!dd)) {
  2635. team = team->t.t_parent;
  2636. continue;
  2637. }
  2638. if (ii > level) {
  2639. team = team->t.t_parent;
  2640. dd = team->t.t_serialized;
  2641. ii--;
  2642. }
  2643. }
  2644. return (dd > 1) ? (0) : (team->t.t_master_tid);
  2645. }
  2646. int __kmp_get_team_size(int gtid, int level) {
  2647. int ii, dd;
  2648. kmp_team_t *team;
  2649. kmp_info_t *thr;
  2650. KF_TRACE(10, ("__kmp_get_team_size: thread %d %d\n", gtid, level));
  2651. KMP_DEBUG_ASSERT(__kmp_init_serial);
  2652. // validate level
  2653. if (level == 0)
  2654. return 1;
  2655. if (level < 0)
  2656. return -1;
  2657. thr = __kmp_threads[gtid];
  2658. team = thr->th.th_team;
  2659. ii = team->t.t_level;
  2660. if (level > ii)
  2661. return -1;
  2662. if (thr->th.th_teams_microtask) {
  2663. // AC: we are in teams region where multiple nested teams have same level
  2664. int tlevel = thr->th.th_teams_level; // the level of the teams construct
  2665. if (level <=
  2666. tlevel) { // otherwise usual algorithm works (will not touch the teams)
  2667. KMP_DEBUG_ASSERT(ii >= tlevel);
  2668. // AC: As we need to pass by the teams league, we need to artificially
  2669. // increase ii
  2670. if (ii == tlevel) {
  2671. ii += 2; // three teams have same level
  2672. } else {
  2673. ii++; // two teams have same level
  2674. }
  2675. }
  2676. }
  2677. while (ii > level) {
  2678. for (dd = team->t.t_serialized; (dd > 0) && (ii > level); dd--, ii--) {
  2679. }
  2680. if (team->t.t_serialized && (!dd)) {
  2681. team = team->t.t_parent;
  2682. continue;
  2683. }
  2684. if (ii > level) {
  2685. team = team->t.t_parent;
  2686. ii--;
  2687. }
  2688. }
  2689. return team->t.t_nproc;
  2690. }
  2691. kmp_r_sched_t __kmp_get_schedule_global() {
  2692. // This routine created because pairs (__kmp_sched, __kmp_chunk) and
  2693. // (__kmp_static, __kmp_guided) may be changed by kmp_set_defaults
  2694. // independently. So one can get the updated schedule here.
  2695. kmp_r_sched_t r_sched;
  2696. // create schedule from 4 globals: __kmp_sched, __kmp_chunk, __kmp_static,
  2697. // __kmp_guided. __kmp_sched should keep original value, so that user can set
  2698. // KMP_SCHEDULE multiple times, and thus have different run-time schedules in
  2699. // different roots (even in OMP 2.5)
  2700. enum sched_type s = SCHEDULE_WITHOUT_MODIFIERS(__kmp_sched);
  2701. enum sched_type sched_modifiers = SCHEDULE_GET_MODIFIERS(__kmp_sched);
  2702. if (s == kmp_sch_static) {
  2703. // replace STATIC with more detailed schedule (balanced or greedy)
  2704. r_sched.r_sched_type = __kmp_static;
  2705. } else if (s == kmp_sch_guided_chunked) {
  2706. // replace GUIDED with more detailed schedule (iterative or analytical)
  2707. r_sched.r_sched_type = __kmp_guided;
  2708. } else { // (STATIC_CHUNKED), or (DYNAMIC_CHUNKED), or other
  2709. r_sched.r_sched_type = __kmp_sched;
  2710. }
  2711. SCHEDULE_SET_MODIFIERS(r_sched.r_sched_type, sched_modifiers);
  2712. if (__kmp_chunk < KMP_DEFAULT_CHUNK) {
  2713. // __kmp_chunk may be wrong here (if it was not ever set)
  2714. r_sched.chunk = KMP_DEFAULT_CHUNK;
  2715. } else {
  2716. r_sched.chunk = __kmp_chunk;
  2717. }
  2718. return r_sched;
  2719. }
  2720. /* Allocate (realloc == FALSE) * or reallocate (realloc == TRUE)
  2721. at least argc number of *t_argv entries for the requested team. */
  2722. static void __kmp_alloc_argv_entries(int argc, kmp_team_t *team, int realloc) {
  2723. KMP_DEBUG_ASSERT(team);
  2724. if (!realloc || argc > team->t.t_max_argc) {
  2725. KA_TRACE(100, ("__kmp_alloc_argv_entries: team %d: needed entries=%d, "
  2726. "current entries=%d\n",
  2727. team->t.t_id, argc, (realloc) ? team->t.t_max_argc : 0));
  2728. /* if previously allocated heap space for args, free them */
  2729. if (realloc && team->t.t_argv != &team->t.t_inline_argv[0])
  2730. __kmp_free((void *)team->t.t_argv);
  2731. if (argc <= KMP_INLINE_ARGV_ENTRIES) {
  2732. /* use unused space in the cache line for arguments */
  2733. team->t.t_max_argc = KMP_INLINE_ARGV_ENTRIES;
  2734. KA_TRACE(100, ("__kmp_alloc_argv_entries: team %d: inline allocate %d "
  2735. "argv entries\n",
  2736. team->t.t_id, team->t.t_max_argc));
  2737. team->t.t_argv = &team->t.t_inline_argv[0];
  2738. if (__kmp_storage_map) {
  2739. __kmp_print_storage_map_gtid(
  2740. -1, &team->t.t_inline_argv[0],
  2741. &team->t.t_inline_argv[KMP_INLINE_ARGV_ENTRIES],
  2742. (sizeof(void *) * KMP_INLINE_ARGV_ENTRIES), "team_%d.t_inline_argv",
  2743. team->t.t_id);
  2744. }
  2745. } else {
  2746. /* allocate space for arguments in the heap */
  2747. team->t.t_max_argc = (argc <= (KMP_MIN_MALLOC_ARGV_ENTRIES >> 1))
  2748. ? KMP_MIN_MALLOC_ARGV_ENTRIES
  2749. : 2 * argc;
  2750. KA_TRACE(100, ("__kmp_alloc_argv_entries: team %d: dynamic allocate %d "
  2751. "argv entries\n",
  2752. team->t.t_id, team->t.t_max_argc));
  2753. team->t.t_argv =
  2754. (void **)__kmp_page_allocate(sizeof(void *) * team->t.t_max_argc);
  2755. if (__kmp_storage_map) {
  2756. __kmp_print_storage_map_gtid(-1, &team->t.t_argv[0],
  2757. &team->t.t_argv[team->t.t_max_argc],
  2758. sizeof(void *) * team->t.t_max_argc,
  2759. "team_%d.t_argv", team->t.t_id);
  2760. }
  2761. }
  2762. }
  2763. }
  2764. static void __kmp_allocate_team_arrays(kmp_team_t *team, int max_nth) {
  2765. int i;
  2766. int num_disp_buff = max_nth > 1 ? __kmp_dispatch_num_buffers : 2;
  2767. team->t.t_threads =
  2768. (kmp_info_t **)__kmp_allocate(sizeof(kmp_info_t *) * max_nth);
  2769. team->t.t_disp_buffer = (dispatch_shared_info_t *)__kmp_allocate(
  2770. sizeof(dispatch_shared_info_t) * num_disp_buff);
  2771. team->t.t_dispatch =
  2772. (kmp_disp_t *)__kmp_allocate(sizeof(kmp_disp_t) * max_nth);
  2773. team->t.t_implicit_task_taskdata =
  2774. (kmp_taskdata_t *)__kmp_allocate(sizeof(kmp_taskdata_t) * max_nth);
  2775. team->t.t_max_nproc = max_nth;
  2776. /* setup dispatch buffers */
  2777. for (i = 0; i < num_disp_buff; ++i) {
  2778. team->t.t_disp_buffer[i].buffer_index = i;
  2779. team->t.t_disp_buffer[i].doacross_buf_idx = i;
  2780. }
  2781. }
  2782. static void __kmp_free_team_arrays(kmp_team_t *team) {
  2783. /* Note: this does not free the threads in t_threads (__kmp_free_threads) */
  2784. int i;
  2785. for (i = 0; i < team->t.t_max_nproc; ++i) {
  2786. if (team->t.t_dispatch[i].th_disp_buffer != NULL) {
  2787. __kmp_free(team->t.t_dispatch[i].th_disp_buffer);
  2788. team->t.t_dispatch[i].th_disp_buffer = NULL;
  2789. }
  2790. }
  2791. #if KMP_USE_HIER_SCHED
  2792. __kmp_dispatch_free_hierarchies(team);
  2793. #endif
  2794. __kmp_free(team->t.t_threads);
  2795. __kmp_free(team->t.t_disp_buffer);
  2796. __kmp_free(team->t.t_dispatch);
  2797. __kmp_free(team->t.t_implicit_task_taskdata);
  2798. team->t.t_threads = NULL;
  2799. team->t.t_disp_buffer = NULL;
  2800. team->t.t_dispatch = NULL;
  2801. team->t.t_implicit_task_taskdata = 0;
  2802. }
  2803. static void __kmp_reallocate_team_arrays(kmp_team_t *team, int max_nth) {
  2804. kmp_info_t **oldThreads = team->t.t_threads;
  2805. __kmp_free(team->t.t_disp_buffer);
  2806. __kmp_free(team->t.t_dispatch);
  2807. __kmp_free(team->t.t_implicit_task_taskdata);
  2808. __kmp_allocate_team_arrays(team, max_nth);
  2809. KMP_MEMCPY(team->t.t_threads, oldThreads,
  2810. team->t.t_nproc * sizeof(kmp_info_t *));
  2811. __kmp_free(oldThreads);
  2812. }
  2813. static kmp_internal_control_t __kmp_get_global_icvs(void) {
  2814. kmp_r_sched_t r_sched =
  2815. __kmp_get_schedule_global(); // get current state of scheduling globals
  2816. KMP_DEBUG_ASSERT(__kmp_nested_proc_bind.used > 0);
  2817. kmp_internal_control_t g_icvs = {
  2818. 0, // int serial_nesting_level; //corresponds to value of th_team_serialized
  2819. (kmp_int8)__kmp_global.g.g_dynamic, // internal control for dynamic
  2820. // adjustment of threads (per thread)
  2821. (kmp_int8)__kmp_env_blocktime, // int bt_set; //internal control for
  2822. // whether blocktime is explicitly set
  2823. __kmp_dflt_blocktime, // int blocktime; //internal control for blocktime
  2824. #if KMP_USE_MONITOR
  2825. __kmp_bt_intervals, // int bt_intervals; //internal control for blocktime
  2826. // intervals
  2827. #endif
  2828. __kmp_dflt_team_nth, // int nproc; //internal control for # of threads for
  2829. // next parallel region (per thread)
  2830. // (use a max ub on value if __kmp_parallel_initialize not called yet)
  2831. __kmp_cg_max_nth, // int thread_limit;
  2832. __kmp_dflt_max_active_levels, // int max_active_levels; //internal control
  2833. // for max_active_levels
  2834. r_sched, // kmp_r_sched_t sched; //internal control for runtime schedule
  2835. // {sched,chunk} pair
  2836. __kmp_nested_proc_bind.bind_types[0],
  2837. __kmp_default_device,
  2838. NULL // struct kmp_internal_control *next;
  2839. };
  2840. return g_icvs;
  2841. }
  2842. static kmp_internal_control_t __kmp_get_x_global_icvs(const kmp_team_t *team) {
  2843. kmp_internal_control_t gx_icvs;
  2844. gx_icvs.serial_nesting_level =
  2845. 0; // probably =team->t.t_serial like in save_inter_controls
  2846. copy_icvs(&gx_icvs, &team->t.t_threads[0]->th.th_current_task->td_icvs);
  2847. gx_icvs.next = NULL;
  2848. return gx_icvs;
  2849. }
  2850. static void __kmp_initialize_root(kmp_root_t *root) {
  2851. int f;
  2852. kmp_team_t *root_team;
  2853. kmp_team_t *hot_team;
  2854. int hot_team_max_nth;
  2855. kmp_r_sched_t r_sched =
  2856. __kmp_get_schedule_global(); // get current state of scheduling globals
  2857. kmp_internal_control_t r_icvs = __kmp_get_global_icvs();
  2858. KMP_DEBUG_ASSERT(root);
  2859. KMP_ASSERT(!root->r.r_begin);
  2860. /* setup the root state structure */
  2861. __kmp_init_lock(&root->r.r_begin_lock);
  2862. root->r.r_begin = FALSE;
  2863. root->r.r_active = FALSE;
  2864. root->r.r_in_parallel = 0;
  2865. root->r.r_blocktime = __kmp_dflt_blocktime;
  2866. #if KMP_AFFINITY_SUPPORTED
  2867. root->r.r_affinity_assigned = FALSE;
  2868. #endif
  2869. /* setup the root team for this task */
  2870. /* allocate the root team structure */
  2871. KF_TRACE(10, ("__kmp_initialize_root: before root_team\n"));
  2872. root_team =
  2873. __kmp_allocate_team(root,
  2874. 1, // new_nproc
  2875. 1, // max_nproc
  2876. #if OMPT_SUPPORT
  2877. ompt_data_none, // root parallel id
  2878. #endif
  2879. __kmp_nested_proc_bind.bind_types[0], &r_icvs,
  2880. 0 // argc
  2881. USE_NESTED_HOT_ARG(NULL) // primary thread is unknown
  2882. );
  2883. #if USE_DEBUGGER
  2884. // Non-NULL value should be assigned to make the debugger display the root
  2885. // team.
  2886. TCW_SYNC_PTR(root_team->t.t_pkfn, (microtask_t)(~0));
  2887. #endif
  2888. KF_TRACE(10, ("__kmp_initialize_root: after root_team = %p\n", root_team));
  2889. root->r.r_root_team = root_team;
  2890. root_team->t.t_control_stack_top = NULL;
  2891. /* initialize root team */
  2892. root_team->t.t_threads[0] = NULL;
  2893. root_team->t.t_nproc = 1;
  2894. root_team->t.t_serialized = 1;
  2895. // TODO???: root_team->t.t_max_active_levels = __kmp_dflt_max_active_levels;
  2896. root_team->t.t_sched.sched = r_sched.sched;
  2897. KA_TRACE(
  2898. 20,
  2899. ("__kmp_initialize_root: init root team %d arrived: join=%u, plain=%u\n",
  2900. root_team->t.t_id, KMP_INIT_BARRIER_STATE, KMP_INIT_BARRIER_STATE));
  2901. /* setup the hot team for this task */
  2902. /* allocate the hot team structure */
  2903. KF_TRACE(10, ("__kmp_initialize_root: before hot_team\n"));
  2904. hot_team =
  2905. __kmp_allocate_team(root,
  2906. 1, // new_nproc
  2907. __kmp_dflt_team_nth_ub * 2, // max_nproc
  2908. #if OMPT_SUPPORT
  2909. ompt_data_none, // root parallel id
  2910. #endif
  2911. __kmp_nested_proc_bind.bind_types[0], &r_icvs,
  2912. 0 // argc
  2913. USE_NESTED_HOT_ARG(NULL) // primary thread is unknown
  2914. );
  2915. KF_TRACE(10, ("__kmp_initialize_root: after hot_team = %p\n", hot_team));
  2916. root->r.r_hot_team = hot_team;
  2917. root_team->t.t_control_stack_top = NULL;
  2918. /* first-time initialization */
  2919. hot_team->t.t_parent = root_team;
  2920. /* initialize hot team */
  2921. hot_team_max_nth = hot_team->t.t_max_nproc;
  2922. for (f = 0; f < hot_team_max_nth; ++f) {
  2923. hot_team->t.t_threads[f] = NULL;
  2924. }
  2925. hot_team->t.t_nproc = 1;
  2926. // TODO???: hot_team->t.t_max_active_levels = __kmp_dflt_max_active_levels;
  2927. hot_team->t.t_sched.sched = r_sched.sched;
  2928. hot_team->t.t_size_changed = 0;
  2929. }
  2930. #ifdef KMP_DEBUG
  2931. typedef struct kmp_team_list_item {
  2932. kmp_team_p const *entry;
  2933. struct kmp_team_list_item *next;
  2934. } kmp_team_list_item_t;
  2935. typedef kmp_team_list_item_t *kmp_team_list_t;
  2936. static void __kmp_print_structure_team_accum( // Add team to list of teams.
  2937. kmp_team_list_t list, // List of teams.
  2938. kmp_team_p const *team // Team to add.
  2939. ) {
  2940. // List must terminate with item where both entry and next are NULL.
  2941. // Team is added to the list only once.
  2942. // List is sorted in ascending order by team id.
  2943. // Team id is *not* a key.
  2944. kmp_team_list_t l;
  2945. KMP_DEBUG_ASSERT(list != NULL);
  2946. if (team == NULL) {
  2947. return;
  2948. }
  2949. __kmp_print_structure_team_accum(list, team->t.t_parent);
  2950. __kmp_print_structure_team_accum(list, team->t.t_next_pool);
  2951. // Search list for the team.
  2952. l = list;
  2953. while (l->next != NULL && l->entry != team) {
  2954. l = l->next;
  2955. }
  2956. if (l->next != NULL) {
  2957. return; // Team has been added before, exit.
  2958. }
  2959. // Team is not found. Search list again for insertion point.
  2960. l = list;
  2961. while (l->next != NULL && l->entry->t.t_id <= team->t.t_id) {
  2962. l = l->next;
  2963. }
  2964. // Insert team.
  2965. {
  2966. kmp_team_list_item_t *item = (kmp_team_list_item_t *)KMP_INTERNAL_MALLOC(
  2967. sizeof(kmp_team_list_item_t));
  2968. *item = *l;
  2969. l->entry = team;
  2970. l->next = item;
  2971. }
  2972. }
  2973. static void __kmp_print_structure_team(char const *title, kmp_team_p const *team
  2974. ) {
  2975. __kmp_printf("%s", title);
  2976. if (team != NULL) {
  2977. __kmp_printf("%2x %p\n", team->t.t_id, team);
  2978. } else {
  2979. __kmp_printf(" - (nil)\n");
  2980. }
  2981. }
  2982. static void __kmp_print_structure_thread(char const *title,
  2983. kmp_info_p const *thread) {
  2984. __kmp_printf("%s", title);
  2985. if (thread != NULL) {
  2986. __kmp_printf("%2d %p\n", thread->th.th_info.ds.ds_gtid, thread);
  2987. } else {
  2988. __kmp_printf(" - (nil)\n");
  2989. }
  2990. }
  2991. void __kmp_print_structure(void) {
  2992. kmp_team_list_t list;
  2993. // Initialize list of teams.
  2994. list =
  2995. (kmp_team_list_item_t *)KMP_INTERNAL_MALLOC(sizeof(kmp_team_list_item_t));
  2996. list->entry = NULL;
  2997. list->next = NULL;
  2998. __kmp_printf("\n------------------------------\nGlobal Thread "
  2999. "Table\n------------------------------\n");
  3000. {
  3001. int gtid;
  3002. for (gtid = 0; gtid < __kmp_threads_capacity; ++gtid) {
  3003. __kmp_printf("%2d", gtid);
  3004. if (__kmp_threads != NULL) {
  3005. __kmp_printf(" %p", __kmp_threads[gtid]);
  3006. }
  3007. if (__kmp_root != NULL) {
  3008. __kmp_printf(" %p", __kmp_root[gtid]);
  3009. }
  3010. __kmp_printf("\n");
  3011. }
  3012. }
  3013. // Print out __kmp_threads array.
  3014. __kmp_printf("\n------------------------------\nThreads\n--------------------"
  3015. "----------\n");
  3016. if (__kmp_threads != NULL) {
  3017. int gtid;
  3018. for (gtid = 0; gtid < __kmp_threads_capacity; ++gtid) {
  3019. kmp_info_t const *thread = __kmp_threads[gtid];
  3020. if (thread != NULL) {
  3021. __kmp_printf("GTID %2d %p:\n", gtid, thread);
  3022. __kmp_printf(" Our Root: %p\n", thread->th.th_root);
  3023. __kmp_print_structure_team(" Our Team: ", thread->th.th_team);
  3024. __kmp_print_structure_team(" Serial Team: ",
  3025. thread->th.th_serial_team);
  3026. __kmp_printf(" Threads: %2d\n", thread->th.th_team_nproc);
  3027. __kmp_print_structure_thread(" Primary: ",
  3028. thread->th.th_team_master);
  3029. __kmp_printf(" Serialized?: %2d\n", thread->th.th_team_serialized);
  3030. __kmp_printf(" Set NProc: %2d\n", thread->th.th_set_nproc);
  3031. __kmp_printf(" Set Proc Bind: %2d\n", thread->th.th_set_proc_bind);
  3032. __kmp_print_structure_thread(" Next in pool: ",
  3033. thread->th.th_next_pool);
  3034. __kmp_printf("\n");
  3035. __kmp_print_structure_team_accum(list, thread->th.th_team);
  3036. __kmp_print_structure_team_accum(list, thread->th.th_serial_team);
  3037. }
  3038. }
  3039. } else {
  3040. __kmp_printf("Threads array is not allocated.\n");
  3041. }
  3042. // Print out __kmp_root array.
  3043. __kmp_printf("\n------------------------------\nUbers\n----------------------"
  3044. "--------\n");
  3045. if (__kmp_root != NULL) {
  3046. int gtid;
  3047. for (gtid = 0; gtid < __kmp_threads_capacity; ++gtid) {
  3048. kmp_root_t const *root = __kmp_root[gtid];
  3049. if (root != NULL) {
  3050. __kmp_printf("GTID %2d %p:\n", gtid, root);
  3051. __kmp_print_structure_team(" Root Team: ", root->r.r_root_team);
  3052. __kmp_print_structure_team(" Hot Team: ", root->r.r_hot_team);
  3053. __kmp_print_structure_thread(" Uber Thread: ",
  3054. root->r.r_uber_thread);
  3055. __kmp_printf(" Active?: %2d\n", root->r.r_active);
  3056. __kmp_printf(" In Parallel: %2d\n",
  3057. KMP_ATOMIC_LD_RLX(&root->r.r_in_parallel));
  3058. __kmp_printf("\n");
  3059. __kmp_print_structure_team_accum(list, root->r.r_root_team);
  3060. __kmp_print_structure_team_accum(list, root->r.r_hot_team);
  3061. }
  3062. }
  3063. } else {
  3064. __kmp_printf("Ubers array is not allocated.\n");
  3065. }
  3066. __kmp_printf("\n------------------------------\nTeams\n----------------------"
  3067. "--------\n");
  3068. while (list->next != NULL) {
  3069. kmp_team_p const *team = list->entry;
  3070. int i;
  3071. __kmp_printf("Team %2x %p:\n", team->t.t_id, team);
  3072. __kmp_print_structure_team(" Parent Team: ", team->t.t_parent);
  3073. __kmp_printf(" Primary TID: %2d\n", team->t.t_master_tid);
  3074. __kmp_printf(" Max threads: %2d\n", team->t.t_max_nproc);
  3075. __kmp_printf(" Levels of serial: %2d\n", team->t.t_serialized);
  3076. __kmp_printf(" Number threads: %2d\n", team->t.t_nproc);
  3077. for (i = 0; i < team->t.t_nproc; ++i) {
  3078. __kmp_printf(" Thread %2d: ", i);
  3079. __kmp_print_structure_thread("", team->t.t_threads[i]);
  3080. }
  3081. __kmp_print_structure_team(" Next in pool: ", team->t.t_next_pool);
  3082. __kmp_printf("\n");
  3083. list = list->next;
  3084. }
  3085. // Print out __kmp_thread_pool and __kmp_team_pool.
  3086. __kmp_printf("\n------------------------------\nPools\n----------------------"
  3087. "--------\n");
  3088. __kmp_print_structure_thread("Thread pool: ",
  3089. CCAST(kmp_info_t *, __kmp_thread_pool));
  3090. __kmp_print_structure_team("Team pool: ",
  3091. CCAST(kmp_team_t *, __kmp_team_pool));
  3092. __kmp_printf("\n");
  3093. // Free team list.
  3094. while (list != NULL) {
  3095. kmp_team_list_item_t *item = list;
  3096. list = list->next;
  3097. KMP_INTERNAL_FREE(item);
  3098. }
  3099. }
  3100. #endif
  3101. //---------------------------------------------------------------------------
  3102. // Stuff for per-thread fast random number generator
  3103. // Table of primes
  3104. static const unsigned __kmp_primes[] = {
  3105. 0x9e3779b1, 0xffe6cc59, 0x2109f6dd, 0x43977ab5, 0xba5703f5, 0xb495a877,
  3106. 0xe1626741, 0x79695e6b, 0xbc98c09f, 0xd5bee2b3, 0x287488f9, 0x3af18231,
  3107. 0x9677cd4d, 0xbe3a6929, 0xadc6a877, 0xdcf0674b, 0xbe4d6fe9, 0x5f15e201,
  3108. 0x99afc3fd, 0xf3f16801, 0xe222cfff, 0x24ba5fdb, 0x0620452d, 0x79f149e3,
  3109. 0xc8b93f49, 0x972702cd, 0xb07dd827, 0x6c97d5ed, 0x085a3d61, 0x46eb5ea7,
  3110. 0x3d9910ed, 0x2e687b5b, 0x29609227, 0x6eb081f1, 0x0954c4e1, 0x9d114db9,
  3111. 0x542acfa9, 0xb3e6bd7b, 0x0742d917, 0xe9f3ffa7, 0x54581edb, 0xf2480f45,
  3112. 0x0bb9288f, 0xef1affc7, 0x85fa0ca7, 0x3ccc14db, 0xe6baf34b, 0x343377f7,
  3113. 0x5ca19031, 0xe6d9293b, 0xf0a9f391, 0x5d2e980b, 0xfc411073, 0xc3749363,
  3114. 0xb892d829, 0x3549366b, 0x629750ad, 0xb98294e5, 0x892d9483, 0xc235baf3,
  3115. 0x3d2402a3, 0x6bdef3c9, 0xbec333cd, 0x40c9520f};
  3116. //---------------------------------------------------------------------------
  3117. // __kmp_get_random: Get a random number using a linear congruential method.
  3118. unsigned short __kmp_get_random(kmp_info_t *thread) {
  3119. unsigned x = thread->th.th_x;
  3120. unsigned short r = (unsigned short)(x >> 16);
  3121. thread->th.th_x = x * thread->th.th_a + 1;
  3122. KA_TRACE(30, ("__kmp_get_random: THREAD: %d, RETURN: %u\n",
  3123. thread->th.th_info.ds.ds_tid, r));
  3124. return r;
  3125. }
  3126. //--------------------------------------------------------
  3127. // __kmp_init_random: Initialize a random number generator
  3128. void __kmp_init_random(kmp_info_t *thread) {
  3129. unsigned seed = thread->th.th_info.ds.ds_tid;
  3130. thread->th.th_a =
  3131. __kmp_primes[seed % (sizeof(__kmp_primes) / sizeof(__kmp_primes[0]))];
  3132. thread->th.th_x = (seed + 1) * thread->th.th_a + 1;
  3133. KA_TRACE(30,
  3134. ("__kmp_init_random: THREAD: %u; A: %u\n", seed, thread->th.th_a));
  3135. }
  3136. #if KMP_OS_WINDOWS
  3137. /* reclaim array entries for root threads that are already dead, returns number
  3138. * reclaimed */
  3139. static int __kmp_reclaim_dead_roots(void) {
  3140. int i, r = 0;
  3141. for (i = 0; i < __kmp_threads_capacity; ++i) {
  3142. if (KMP_UBER_GTID(i) &&
  3143. !__kmp_still_running((kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[i])) &&
  3144. !__kmp_root[i]
  3145. ->r.r_active) { // AC: reclaim only roots died in non-active state
  3146. r += __kmp_unregister_root_other_thread(i);
  3147. }
  3148. }
  3149. return r;
  3150. }
  3151. #endif
  3152. /* This function attempts to create free entries in __kmp_threads and
  3153. __kmp_root, and returns the number of free entries generated.
  3154. For Windows* OS static library, the first mechanism used is to reclaim array
  3155. entries for root threads that are already dead.
  3156. On all platforms, expansion is attempted on the arrays __kmp_threads_ and
  3157. __kmp_root, with appropriate update to __kmp_threads_capacity. Array
  3158. capacity is increased by doubling with clipping to __kmp_tp_capacity, if
  3159. threadprivate cache array has been created. Synchronization with
  3160. __kmpc_threadprivate_cached is done using __kmp_tp_cached_lock.
  3161. After any dead root reclamation, if the clipping value allows array expansion
  3162. to result in the generation of a total of nNeed free slots, the function does
  3163. that expansion. If not, nothing is done beyond the possible initial root
  3164. thread reclamation.
  3165. If any argument is negative, the behavior is undefined. */
  3166. static int __kmp_expand_threads(int nNeed) {
  3167. int added = 0;
  3168. int minimumRequiredCapacity;
  3169. int newCapacity;
  3170. kmp_info_t **newThreads;
  3171. kmp_root_t **newRoot;
  3172. // All calls to __kmp_expand_threads should be under __kmp_forkjoin_lock, so
  3173. // resizing __kmp_threads does not need additional protection if foreign
  3174. // threads are present
  3175. #if KMP_OS_WINDOWS && !KMP_DYNAMIC_LIB
  3176. /* only for Windows static library */
  3177. /* reclaim array entries for root threads that are already dead */
  3178. added = __kmp_reclaim_dead_roots();
  3179. if (nNeed) {
  3180. nNeed -= added;
  3181. if (nNeed < 0)
  3182. nNeed = 0;
  3183. }
  3184. #endif
  3185. if (nNeed <= 0)
  3186. return added;
  3187. // Note that __kmp_threads_capacity is not bounded by __kmp_max_nth. If
  3188. // __kmp_max_nth is set to some value less than __kmp_sys_max_nth by the
  3189. // user via KMP_DEVICE_THREAD_LIMIT, then __kmp_threads_capacity may become
  3190. // > __kmp_max_nth in one of two ways:
  3191. //
  3192. // 1) The initialization thread (gtid = 0) exits. __kmp_threads[0]
  3193. // may not be reused by another thread, so we may need to increase
  3194. // __kmp_threads_capacity to __kmp_max_nth + 1.
  3195. //
  3196. // 2) New foreign root(s) are encountered. We always register new foreign
  3197. // roots. This may cause a smaller # of threads to be allocated at
  3198. // subsequent parallel regions, but the worker threads hang around (and
  3199. // eventually go to sleep) and need slots in the __kmp_threads[] array.
  3200. //
  3201. // Anyway, that is the reason for moving the check to see if
  3202. // __kmp_max_nth was exceeded into __kmp_reserve_threads()
  3203. // instead of having it performed here. -BB
  3204. KMP_DEBUG_ASSERT(__kmp_sys_max_nth >= __kmp_threads_capacity);
  3205. /* compute expansion headroom to check if we can expand */
  3206. if (__kmp_sys_max_nth - __kmp_threads_capacity < nNeed) {
  3207. /* possible expansion too small -- give up */
  3208. return added;
  3209. }
  3210. minimumRequiredCapacity = __kmp_threads_capacity + nNeed;
  3211. newCapacity = __kmp_threads_capacity;
  3212. do {
  3213. newCapacity = newCapacity <= (__kmp_sys_max_nth >> 1) ? (newCapacity << 1)
  3214. : __kmp_sys_max_nth;
  3215. } while (newCapacity < minimumRequiredCapacity);
  3216. newThreads = (kmp_info_t **)__kmp_allocate(
  3217. (sizeof(kmp_info_t *) + sizeof(kmp_root_t *)) * newCapacity + CACHE_LINE);
  3218. newRoot =
  3219. (kmp_root_t **)((char *)newThreads + sizeof(kmp_info_t *) * newCapacity);
  3220. KMP_MEMCPY(newThreads, __kmp_threads,
  3221. __kmp_threads_capacity * sizeof(kmp_info_t *));
  3222. KMP_MEMCPY(newRoot, __kmp_root,
  3223. __kmp_threads_capacity * sizeof(kmp_root_t *));
  3224. // Put old __kmp_threads array on a list. Any ongoing references to the old
  3225. // list will be valid. This list is cleaned up at library shutdown.
  3226. kmp_old_threads_list_t *node =
  3227. (kmp_old_threads_list_t *)__kmp_allocate(sizeof(kmp_old_threads_list_t));
  3228. node->threads = __kmp_threads;
  3229. node->next = __kmp_old_threads_list;
  3230. __kmp_old_threads_list = node;
  3231. *(kmp_info_t * *volatile *)&__kmp_threads = newThreads;
  3232. *(kmp_root_t * *volatile *)&__kmp_root = newRoot;
  3233. added += newCapacity - __kmp_threads_capacity;
  3234. *(volatile int *)&__kmp_threads_capacity = newCapacity;
  3235. if (newCapacity > __kmp_tp_capacity) {
  3236. __kmp_acquire_bootstrap_lock(&__kmp_tp_cached_lock);
  3237. if (__kmp_tp_cached && newCapacity > __kmp_tp_capacity) {
  3238. __kmp_threadprivate_resize_cache(newCapacity);
  3239. } else { // increase __kmp_tp_capacity to correspond with kmp_threads size
  3240. *(volatile int *)&__kmp_tp_capacity = newCapacity;
  3241. }
  3242. __kmp_release_bootstrap_lock(&__kmp_tp_cached_lock);
  3243. }
  3244. return added;
  3245. }
  3246. /* Register the current thread as a root thread and obtain our gtid. We must
  3247. have the __kmp_initz_lock held at this point. Argument TRUE only if are the
  3248. thread that calls from __kmp_do_serial_initialize() */
  3249. int __kmp_register_root(int initial_thread) {
  3250. kmp_info_t *root_thread;
  3251. kmp_root_t *root;
  3252. int gtid;
  3253. int capacity;
  3254. __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
  3255. KA_TRACE(20, ("__kmp_register_root: entered\n"));
  3256. KMP_MB();
  3257. /* 2007-03-02:
  3258. If initial thread did not invoke OpenMP RTL yet, and this thread is not an
  3259. initial one, "__kmp_all_nth >= __kmp_threads_capacity" condition does not
  3260. work as expected -- it may return false (that means there is at least one
  3261. empty slot in __kmp_threads array), but it is possible the only free slot
  3262. is #0, which is reserved for initial thread and so cannot be used for this
  3263. one. Following code workarounds this bug.
  3264. However, right solution seems to be not reserving slot #0 for initial
  3265. thread because:
  3266. (1) there is no magic in slot #0,
  3267. (2) we cannot detect initial thread reliably (the first thread which does
  3268. serial initialization may be not a real initial thread).
  3269. */
  3270. capacity = __kmp_threads_capacity;
  3271. if (!initial_thread && TCR_PTR(__kmp_threads[0]) == NULL) {
  3272. --capacity;
  3273. }
  3274. // If it is not for initializing the hidden helper team, we need to take
  3275. // __kmp_hidden_helper_threads_num out of the capacity because it is included
  3276. // in __kmp_threads_capacity.
  3277. if (__kmp_enable_hidden_helper && !TCR_4(__kmp_init_hidden_helper_threads)) {
  3278. capacity -= __kmp_hidden_helper_threads_num;
  3279. }
  3280. /* see if there are too many threads */
  3281. if (__kmp_all_nth >= capacity && !__kmp_expand_threads(1)) {
  3282. if (__kmp_tp_cached) {
  3283. __kmp_fatal(KMP_MSG(CantRegisterNewThread),
  3284. KMP_HNT(Set_ALL_THREADPRIVATE, __kmp_tp_capacity),
  3285. KMP_HNT(PossibleSystemLimitOnThreads), __kmp_msg_null);
  3286. } else {
  3287. __kmp_fatal(KMP_MSG(CantRegisterNewThread), KMP_HNT(SystemLimitOnThreads),
  3288. __kmp_msg_null);
  3289. }
  3290. }
  3291. // When hidden helper task is enabled, __kmp_threads is organized as follows:
  3292. // 0: initial thread, also a regular OpenMP thread.
  3293. // [1, __kmp_hidden_helper_threads_num]: slots for hidden helper threads.
  3294. // [__kmp_hidden_helper_threads_num + 1, __kmp_threads_capacity): slots for
  3295. // regular OpenMP threads.
  3296. if (TCR_4(__kmp_init_hidden_helper_threads)) {
  3297. // Find an available thread slot for hidden helper thread. Slots for hidden
  3298. // helper threads start from 1 to __kmp_hidden_helper_threads_num.
  3299. for (gtid = 1; TCR_PTR(__kmp_threads[gtid]) != NULL &&
  3300. gtid <= __kmp_hidden_helper_threads_num;
  3301. gtid++)
  3302. ;
  3303. KMP_ASSERT(gtid <= __kmp_hidden_helper_threads_num);
  3304. KA_TRACE(1, ("__kmp_register_root: found slot in threads array for "
  3305. "hidden helper thread: T#%d\n",
  3306. gtid));
  3307. } else {
  3308. /* find an available thread slot */
  3309. // Don't reassign the zero slot since we need that to only be used by
  3310. // initial thread. Slots for hidden helper threads should also be skipped.
  3311. if (initial_thread && TCR_PTR(__kmp_threads[0]) == NULL) {
  3312. gtid = 0;
  3313. } else {
  3314. for (gtid = __kmp_hidden_helper_threads_num + 1;
  3315. TCR_PTR(__kmp_threads[gtid]) != NULL; gtid++)
  3316. ;
  3317. }
  3318. KA_TRACE(
  3319. 1, ("__kmp_register_root: found slot in threads array: T#%d\n", gtid));
  3320. KMP_ASSERT(gtid < __kmp_threads_capacity);
  3321. }
  3322. /* update global accounting */
  3323. __kmp_all_nth++;
  3324. TCW_4(__kmp_nth, __kmp_nth + 1);
  3325. // if __kmp_adjust_gtid_mode is set, then we use method #1 (sp search) for low
  3326. // numbers of procs, and method #2 (keyed API call) for higher numbers.
  3327. if (__kmp_adjust_gtid_mode) {
  3328. if (__kmp_all_nth >= __kmp_tls_gtid_min) {
  3329. if (TCR_4(__kmp_gtid_mode) != 2) {
  3330. TCW_4(__kmp_gtid_mode, 2);
  3331. }
  3332. } else {
  3333. if (TCR_4(__kmp_gtid_mode) != 1) {
  3334. TCW_4(__kmp_gtid_mode, 1);
  3335. }
  3336. }
  3337. }
  3338. #ifdef KMP_ADJUST_BLOCKTIME
  3339. /* Adjust blocktime to zero if necessary */
  3340. /* Middle initialization might not have occurred yet */
  3341. if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
  3342. if (__kmp_nth > __kmp_avail_proc) {
  3343. __kmp_zero_bt = TRUE;
  3344. }
  3345. }
  3346. #endif /* KMP_ADJUST_BLOCKTIME */
  3347. /* setup this new hierarchy */
  3348. if (!(root = __kmp_root[gtid])) {
  3349. root = __kmp_root[gtid] = (kmp_root_t *)__kmp_allocate(sizeof(kmp_root_t));
  3350. KMP_DEBUG_ASSERT(!root->r.r_root_team);
  3351. }
  3352. #if KMP_STATS_ENABLED
  3353. // Initialize stats as soon as possible (right after gtid assignment).
  3354. __kmp_stats_thread_ptr = __kmp_stats_list->push_back(gtid);
  3355. __kmp_stats_thread_ptr->startLife();
  3356. KMP_SET_THREAD_STATE(SERIAL_REGION);
  3357. KMP_INIT_PARTITIONED_TIMERS(OMP_serial);
  3358. #endif
  3359. __kmp_initialize_root(root);
  3360. /* setup new root thread structure */
  3361. if (root->r.r_uber_thread) {
  3362. root_thread = root->r.r_uber_thread;
  3363. } else {
  3364. root_thread = (kmp_info_t *)__kmp_allocate(sizeof(kmp_info_t));
  3365. if (__kmp_storage_map) {
  3366. __kmp_print_thread_storage_map(root_thread, gtid);
  3367. }
  3368. root_thread->th.th_info.ds.ds_gtid = gtid;
  3369. #if OMPT_SUPPORT
  3370. root_thread->th.ompt_thread_info.thread_data = ompt_data_none;
  3371. #endif
  3372. root_thread->th.th_root = root;
  3373. if (__kmp_env_consistency_check) {
  3374. root_thread->th.th_cons = __kmp_allocate_cons_stack(gtid);
  3375. }
  3376. #if USE_FAST_MEMORY
  3377. __kmp_initialize_fast_memory(root_thread);
  3378. #endif /* USE_FAST_MEMORY */
  3379. #if KMP_USE_BGET
  3380. KMP_DEBUG_ASSERT(root_thread->th.th_local.bget_data == NULL);
  3381. __kmp_initialize_bget(root_thread);
  3382. #endif
  3383. __kmp_init_random(root_thread); // Initialize random number generator
  3384. }
  3385. /* setup the serial team held in reserve by the root thread */
  3386. if (!root_thread->th.th_serial_team) {
  3387. kmp_internal_control_t r_icvs = __kmp_get_global_icvs();
  3388. KF_TRACE(10, ("__kmp_register_root: before serial_team\n"));
  3389. root_thread->th.th_serial_team = __kmp_allocate_team(
  3390. root, 1, 1,
  3391. #if OMPT_SUPPORT
  3392. ompt_data_none, // root parallel id
  3393. #endif
  3394. proc_bind_default, &r_icvs, 0 USE_NESTED_HOT_ARG(NULL));
  3395. }
  3396. KMP_ASSERT(root_thread->th.th_serial_team);
  3397. KF_TRACE(10, ("__kmp_register_root: after serial_team = %p\n",
  3398. root_thread->th.th_serial_team));
  3399. /* drop root_thread into place */
  3400. TCW_SYNC_PTR(__kmp_threads[gtid], root_thread);
  3401. root->r.r_root_team->t.t_threads[0] = root_thread;
  3402. root->r.r_hot_team->t.t_threads[0] = root_thread;
  3403. root_thread->th.th_serial_team->t.t_threads[0] = root_thread;
  3404. // AC: the team created in reserve, not for execution (it is unused for now).
  3405. root_thread->th.th_serial_team->t.t_serialized = 0;
  3406. root->r.r_uber_thread = root_thread;
  3407. /* initialize the thread, get it ready to go */
  3408. __kmp_initialize_info(root_thread, root->r.r_root_team, 0, gtid);
  3409. TCW_4(__kmp_init_gtid, TRUE);
  3410. /* prepare the primary thread for get_gtid() */
  3411. __kmp_gtid_set_specific(gtid);
  3412. #if USE_ITT_BUILD
  3413. __kmp_itt_thread_name(gtid);
  3414. #endif /* USE_ITT_BUILD */
  3415. #ifdef KMP_TDATA_GTID
  3416. __kmp_gtid = gtid;
  3417. #endif
  3418. __kmp_create_worker(gtid, root_thread, __kmp_stksize);
  3419. KMP_DEBUG_ASSERT(__kmp_gtid_get_specific() == gtid);
  3420. KA_TRACE(20, ("__kmp_register_root: T#%d init T#%d(%d:%d) arrived: join=%u, "
  3421. "plain=%u\n",
  3422. gtid, __kmp_gtid_from_tid(0, root->r.r_hot_team),
  3423. root->r.r_hot_team->t.t_id, 0, KMP_INIT_BARRIER_STATE,
  3424. KMP_INIT_BARRIER_STATE));
  3425. { // Initialize barrier data.
  3426. int b;
  3427. for (b = 0; b < bs_last_barrier; ++b) {
  3428. root_thread->th.th_bar[b].bb.b_arrived = KMP_INIT_BARRIER_STATE;
  3429. #if USE_DEBUGGER
  3430. root_thread->th.th_bar[b].bb.b_worker_arrived = 0;
  3431. #endif
  3432. }
  3433. }
  3434. KMP_DEBUG_ASSERT(root->r.r_hot_team->t.t_bar[bs_forkjoin_barrier].b_arrived ==
  3435. KMP_INIT_BARRIER_STATE);
  3436. #if KMP_AFFINITY_SUPPORTED
  3437. root_thread->th.th_current_place = KMP_PLACE_UNDEFINED;
  3438. root_thread->th.th_new_place = KMP_PLACE_UNDEFINED;
  3439. root_thread->th.th_first_place = KMP_PLACE_UNDEFINED;
  3440. root_thread->th.th_last_place = KMP_PLACE_UNDEFINED;
  3441. #endif /* KMP_AFFINITY_SUPPORTED */
  3442. root_thread->th.th_def_allocator = __kmp_def_allocator;
  3443. root_thread->th.th_prev_level = 0;
  3444. root_thread->th.th_prev_num_threads = 1;
  3445. kmp_cg_root_t *tmp = (kmp_cg_root_t *)__kmp_allocate(sizeof(kmp_cg_root_t));
  3446. tmp->cg_root = root_thread;
  3447. tmp->cg_thread_limit = __kmp_cg_max_nth;
  3448. tmp->cg_nthreads = 1;
  3449. KA_TRACE(100, ("__kmp_register_root: Thread %p created node %p with"
  3450. " cg_nthreads init to 1\n",
  3451. root_thread, tmp));
  3452. tmp->up = NULL;
  3453. root_thread->th.th_cg_roots = tmp;
  3454. __kmp_root_counter++;
  3455. #if OMPT_SUPPORT
  3456. if (!initial_thread && ompt_enabled.enabled) {
  3457. kmp_info_t *root_thread = ompt_get_thread();
  3458. ompt_set_thread_state(root_thread, ompt_state_overhead);
  3459. if (ompt_enabled.ompt_callback_thread_begin) {
  3460. ompt_callbacks.ompt_callback(ompt_callback_thread_begin)(
  3461. ompt_thread_initial, __ompt_get_thread_data_internal());
  3462. }
  3463. ompt_data_t *task_data;
  3464. ompt_data_t *parallel_data;
  3465. __ompt_get_task_info_internal(0, NULL, &task_data, NULL, &parallel_data,
  3466. NULL);
  3467. if (ompt_enabled.ompt_callback_implicit_task) {
  3468. ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
  3469. ompt_scope_begin, parallel_data, task_data, 1, 1, ompt_task_initial);
  3470. }
  3471. ompt_set_thread_state(root_thread, ompt_state_work_serial);
  3472. }
  3473. #endif
  3474. #if OMPD_SUPPORT
  3475. if (ompd_state & OMPD_ENABLE_BP)
  3476. ompd_bp_thread_begin();
  3477. #endif
  3478. KMP_MB();
  3479. __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
  3480. return gtid;
  3481. }
  3482. #if KMP_NESTED_HOT_TEAMS
  3483. static int __kmp_free_hot_teams(kmp_root_t *root, kmp_info_t *thr, int level,
  3484. const int max_level) {
  3485. int i, n, nth;
  3486. kmp_hot_team_ptr_t *hot_teams = thr->th.th_hot_teams;
  3487. if (!hot_teams || !hot_teams[level].hot_team) {
  3488. return 0;
  3489. }
  3490. KMP_DEBUG_ASSERT(level < max_level);
  3491. kmp_team_t *team = hot_teams[level].hot_team;
  3492. nth = hot_teams[level].hot_team_nth;
  3493. n = nth - 1; // primary thread is not freed
  3494. if (level < max_level - 1) {
  3495. for (i = 0; i < nth; ++i) {
  3496. kmp_info_t *th = team->t.t_threads[i];
  3497. n += __kmp_free_hot_teams(root, th, level + 1, max_level);
  3498. if (i > 0 && th->th.th_hot_teams) {
  3499. __kmp_free(th->th.th_hot_teams);
  3500. th->th.th_hot_teams = NULL;
  3501. }
  3502. }
  3503. }
  3504. __kmp_free_team(root, team, NULL);
  3505. return n;
  3506. }
  3507. #endif
  3508. // Resets a root thread and clear its root and hot teams.
  3509. // Returns the number of __kmp_threads entries directly and indirectly freed.
  3510. static int __kmp_reset_root(int gtid, kmp_root_t *root) {
  3511. kmp_team_t *root_team = root->r.r_root_team;
  3512. kmp_team_t *hot_team = root->r.r_hot_team;
  3513. int n = hot_team->t.t_nproc;
  3514. int i;
  3515. KMP_DEBUG_ASSERT(!root->r.r_active);
  3516. root->r.r_root_team = NULL;
  3517. root->r.r_hot_team = NULL;
  3518. // __kmp_free_team() does not free hot teams, so we have to clear r_hot_team
  3519. // before call to __kmp_free_team().
  3520. __kmp_free_team(root, root_team USE_NESTED_HOT_ARG(NULL));
  3521. #if KMP_NESTED_HOT_TEAMS
  3522. if (__kmp_hot_teams_max_level >
  3523. 0) { // need to free nested hot teams and their threads if any
  3524. for (i = 0; i < hot_team->t.t_nproc; ++i) {
  3525. kmp_info_t *th = hot_team->t.t_threads[i];
  3526. if (__kmp_hot_teams_max_level > 1) {
  3527. n += __kmp_free_hot_teams(root, th, 1, __kmp_hot_teams_max_level);
  3528. }
  3529. if (th->th.th_hot_teams) {
  3530. __kmp_free(th->th.th_hot_teams);
  3531. th->th.th_hot_teams = NULL;
  3532. }
  3533. }
  3534. }
  3535. #endif
  3536. __kmp_free_team(root, hot_team USE_NESTED_HOT_ARG(NULL));
  3537. // Before we can reap the thread, we need to make certain that all other
  3538. // threads in the teams that had this root as ancestor have stopped trying to
  3539. // steal tasks.
  3540. if (__kmp_tasking_mode != tskm_immediate_exec) {
  3541. __kmp_wait_to_unref_task_teams();
  3542. }
  3543. #if KMP_OS_WINDOWS
  3544. /* Close Handle of root duplicated in __kmp_create_worker (tr #62919) */
  3545. KA_TRACE(
  3546. 10, ("__kmp_reset_root: free handle, th = %p, handle = %" KMP_UINTPTR_SPEC
  3547. "\n",
  3548. (LPVOID) & (root->r.r_uber_thread->th),
  3549. root->r.r_uber_thread->th.th_info.ds.ds_thread));
  3550. __kmp_free_handle(root->r.r_uber_thread->th.th_info.ds.ds_thread);
  3551. #endif /* KMP_OS_WINDOWS */
  3552. #if OMPD_SUPPORT
  3553. if (ompd_state & OMPD_ENABLE_BP)
  3554. ompd_bp_thread_end();
  3555. #endif
  3556. #if OMPT_SUPPORT
  3557. ompt_data_t *task_data;
  3558. ompt_data_t *parallel_data;
  3559. __ompt_get_task_info_internal(0, NULL, &task_data, NULL, &parallel_data,
  3560. NULL);
  3561. if (ompt_enabled.ompt_callback_implicit_task) {
  3562. ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
  3563. ompt_scope_end, parallel_data, task_data, 0, 1, ompt_task_initial);
  3564. }
  3565. if (ompt_enabled.ompt_callback_thread_end) {
  3566. ompt_callbacks.ompt_callback(ompt_callback_thread_end)(
  3567. &(root->r.r_uber_thread->th.ompt_thread_info.thread_data));
  3568. }
  3569. #endif
  3570. TCW_4(__kmp_nth,
  3571. __kmp_nth - 1); // __kmp_reap_thread will decrement __kmp_all_nth.
  3572. i = root->r.r_uber_thread->th.th_cg_roots->cg_nthreads--;
  3573. KA_TRACE(100, ("__kmp_reset_root: Thread %p decrement cg_nthreads on node %p"
  3574. " to %d\n",
  3575. root->r.r_uber_thread, root->r.r_uber_thread->th.th_cg_roots,
  3576. root->r.r_uber_thread->th.th_cg_roots->cg_nthreads));
  3577. if (i == 1) {
  3578. // need to free contention group structure
  3579. KMP_DEBUG_ASSERT(root->r.r_uber_thread ==
  3580. root->r.r_uber_thread->th.th_cg_roots->cg_root);
  3581. KMP_DEBUG_ASSERT(root->r.r_uber_thread->th.th_cg_roots->up == NULL);
  3582. __kmp_free(root->r.r_uber_thread->th.th_cg_roots);
  3583. root->r.r_uber_thread->th.th_cg_roots = NULL;
  3584. }
  3585. __kmp_reap_thread(root->r.r_uber_thread, 1);
  3586. // We canot put root thread to __kmp_thread_pool, so we have to reap it
  3587. // instead of freeing.
  3588. root->r.r_uber_thread = NULL;
  3589. /* mark root as no longer in use */
  3590. root->r.r_begin = FALSE;
  3591. return n;
  3592. }
  3593. void __kmp_unregister_root_current_thread(int gtid) {
  3594. KA_TRACE(1, ("__kmp_unregister_root_current_thread: enter T#%d\n", gtid));
  3595. /* this lock should be ok, since unregister_root_current_thread is never
  3596. called during an abort, only during a normal close. furthermore, if you
  3597. have the forkjoin lock, you should never try to get the initz lock */
  3598. __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
  3599. if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) {
  3600. KC_TRACE(10, ("__kmp_unregister_root_current_thread: already finished, "
  3601. "exiting T#%d\n",
  3602. gtid));
  3603. __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
  3604. return;
  3605. }
  3606. kmp_root_t *root = __kmp_root[gtid];
  3607. KMP_DEBUG_ASSERT(__kmp_threads && __kmp_threads[gtid]);
  3608. KMP_ASSERT(KMP_UBER_GTID(gtid));
  3609. KMP_ASSERT(root == __kmp_threads[gtid]->th.th_root);
  3610. KMP_ASSERT(root->r.r_active == FALSE);
  3611. KMP_MB();
  3612. kmp_info_t *thread = __kmp_threads[gtid];
  3613. kmp_team_t *team = thread->th.th_team;
  3614. kmp_task_team_t *task_team = thread->th.th_task_team;
  3615. // we need to wait for the proxy tasks before finishing the thread
  3616. if (task_team != NULL && (task_team->tt.tt_found_proxy_tasks ||
  3617. task_team->tt.tt_hidden_helper_task_encountered)) {
  3618. #if OMPT_SUPPORT
  3619. // the runtime is shutting down so we won't report any events
  3620. thread->th.ompt_thread_info.state = ompt_state_undefined;
  3621. #endif
  3622. __kmp_task_team_wait(thread, team USE_ITT_BUILD_ARG(NULL));
  3623. }
  3624. __kmp_reset_root(gtid, root);
  3625. KMP_MB();
  3626. KC_TRACE(10,
  3627. ("__kmp_unregister_root_current_thread: T#%d unregistered\n", gtid));
  3628. __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
  3629. }
  3630. #if KMP_OS_WINDOWS
  3631. /* __kmp_forkjoin_lock must be already held
  3632. Unregisters a root thread that is not the current thread. Returns the number
  3633. of __kmp_threads entries freed as a result. */
  3634. static int __kmp_unregister_root_other_thread(int gtid) {
  3635. kmp_root_t *root = __kmp_root[gtid];
  3636. int r;
  3637. KA_TRACE(1, ("__kmp_unregister_root_other_thread: enter T#%d\n", gtid));
  3638. KMP_DEBUG_ASSERT(__kmp_threads && __kmp_threads[gtid]);
  3639. KMP_ASSERT(KMP_UBER_GTID(gtid));
  3640. KMP_ASSERT(root == __kmp_threads[gtid]->th.th_root);
  3641. KMP_ASSERT(root->r.r_active == FALSE);
  3642. r = __kmp_reset_root(gtid, root);
  3643. KC_TRACE(10,
  3644. ("__kmp_unregister_root_other_thread: T#%d unregistered\n", gtid));
  3645. return r;
  3646. }
  3647. #endif
  3648. #if KMP_DEBUG
  3649. void __kmp_task_info() {
  3650. kmp_int32 gtid = __kmp_entry_gtid();
  3651. kmp_int32 tid = __kmp_tid_from_gtid(gtid);
  3652. kmp_info_t *this_thr = __kmp_threads[gtid];
  3653. kmp_team_t *steam = this_thr->th.th_serial_team;
  3654. kmp_team_t *team = this_thr->th.th_team;
  3655. __kmp_printf(
  3656. "__kmp_task_info: gtid=%d tid=%d t_thread=%p team=%p steam=%p curtask=%p "
  3657. "ptask=%p\n",
  3658. gtid, tid, this_thr, team, steam, this_thr->th.th_current_task,
  3659. team->t.t_implicit_task_taskdata[tid].td_parent);
  3660. }
  3661. #endif // KMP_DEBUG
  3662. /* TODO optimize with one big memclr, take out what isn't needed, split
  3663. responsibility to workers as much as possible, and delay initialization of
  3664. features as much as possible */
  3665. static void __kmp_initialize_info(kmp_info_t *this_thr, kmp_team_t *team,
  3666. int tid, int gtid) {
  3667. /* this_thr->th.th_info.ds.ds_gtid is setup in
  3668. kmp_allocate_thread/create_worker.
  3669. this_thr->th.th_serial_team is setup in __kmp_allocate_thread */
  3670. KMP_DEBUG_ASSERT(this_thr != NULL);
  3671. KMP_DEBUG_ASSERT(this_thr->th.th_serial_team);
  3672. KMP_DEBUG_ASSERT(team);
  3673. KMP_DEBUG_ASSERT(team->t.t_threads);
  3674. KMP_DEBUG_ASSERT(team->t.t_dispatch);
  3675. kmp_info_t *master = team->t.t_threads[0];
  3676. KMP_DEBUG_ASSERT(master);
  3677. KMP_DEBUG_ASSERT(master->th.th_root);
  3678. KMP_MB();
  3679. TCW_SYNC_PTR(this_thr->th.th_team, team);
  3680. this_thr->th.th_info.ds.ds_tid = tid;
  3681. this_thr->th.th_set_nproc = 0;
  3682. if (__kmp_tasking_mode != tskm_immediate_exec)
  3683. // When tasking is possible, threads are not safe to reap until they are
  3684. // done tasking; this will be set when tasking code is exited in wait
  3685. this_thr->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
  3686. else // no tasking --> always safe to reap
  3687. this_thr->th.th_reap_state = KMP_SAFE_TO_REAP;
  3688. this_thr->th.th_set_proc_bind = proc_bind_default;
  3689. #if KMP_AFFINITY_SUPPORTED
  3690. this_thr->th.th_new_place = this_thr->th.th_current_place;
  3691. #endif
  3692. this_thr->th.th_root = master->th.th_root;
  3693. /* setup the thread's cache of the team structure */
  3694. this_thr->th.th_team_nproc = team->t.t_nproc;
  3695. this_thr->th.th_team_master = master;
  3696. this_thr->th.th_team_serialized = team->t.t_serialized;
  3697. KMP_DEBUG_ASSERT(team->t.t_implicit_task_taskdata);
  3698. KF_TRACE(10, ("__kmp_initialize_info1: T#%d:%d this_thread=%p curtask=%p\n",
  3699. tid, gtid, this_thr, this_thr->th.th_current_task));
  3700. __kmp_init_implicit_task(this_thr->th.th_team_master->th.th_ident, this_thr,
  3701. team, tid, TRUE);
  3702. KF_TRACE(10, ("__kmp_initialize_info2: T#%d:%d this_thread=%p curtask=%p\n",
  3703. tid, gtid, this_thr, this_thr->th.th_current_task));
  3704. // TODO: Initialize ICVs from parent; GEH - isn't that already done in
  3705. // __kmp_initialize_team()?
  3706. /* TODO no worksharing in speculative threads */
  3707. this_thr->th.th_dispatch = &team->t.t_dispatch[tid];
  3708. this_thr->th.th_local.this_construct = 0;
  3709. if (!this_thr->th.th_pri_common) {
  3710. this_thr->th.th_pri_common =
  3711. (struct common_table *)__kmp_allocate(sizeof(struct common_table));
  3712. if (__kmp_storage_map) {
  3713. __kmp_print_storage_map_gtid(
  3714. gtid, this_thr->th.th_pri_common, this_thr->th.th_pri_common + 1,
  3715. sizeof(struct common_table), "th_%d.th_pri_common\n", gtid);
  3716. }
  3717. this_thr->th.th_pri_head = NULL;
  3718. }
  3719. if (this_thr != master && // Primary thread's CG root is initialized elsewhere
  3720. this_thr->th.th_cg_roots != master->th.th_cg_roots) { // CG root not set
  3721. // Make new thread's CG root same as primary thread's
  3722. KMP_DEBUG_ASSERT(master->th.th_cg_roots);
  3723. kmp_cg_root_t *tmp = this_thr->th.th_cg_roots;
  3724. if (tmp) {
  3725. // worker changes CG, need to check if old CG should be freed
  3726. int i = tmp->cg_nthreads--;
  3727. KA_TRACE(100, ("__kmp_initialize_info: Thread %p decrement cg_nthreads"
  3728. " on node %p of thread %p to %d\n",
  3729. this_thr, tmp, tmp->cg_root, tmp->cg_nthreads));
  3730. if (i == 1) {
  3731. __kmp_free(tmp); // last thread left CG --> free it
  3732. }
  3733. }
  3734. this_thr->th.th_cg_roots = master->th.th_cg_roots;
  3735. // Increment new thread's CG root's counter to add the new thread
  3736. this_thr->th.th_cg_roots->cg_nthreads++;
  3737. KA_TRACE(100, ("__kmp_initialize_info: Thread %p increment cg_nthreads on"
  3738. " node %p of thread %p to %d\n",
  3739. this_thr, this_thr->th.th_cg_roots,
  3740. this_thr->th.th_cg_roots->cg_root,
  3741. this_thr->th.th_cg_roots->cg_nthreads));
  3742. this_thr->th.th_current_task->td_icvs.thread_limit =
  3743. this_thr->th.th_cg_roots->cg_thread_limit;
  3744. }
  3745. /* Initialize dynamic dispatch */
  3746. {
  3747. volatile kmp_disp_t *dispatch = this_thr->th.th_dispatch;
  3748. // Use team max_nproc since this will never change for the team.
  3749. size_t disp_size =
  3750. sizeof(dispatch_private_info_t) *
  3751. (team->t.t_max_nproc == 1 ? 1 : __kmp_dispatch_num_buffers);
  3752. KD_TRACE(10, ("__kmp_initialize_info: T#%d max_nproc: %d\n", gtid,
  3753. team->t.t_max_nproc));
  3754. KMP_ASSERT(dispatch);
  3755. KMP_DEBUG_ASSERT(team->t.t_dispatch);
  3756. KMP_DEBUG_ASSERT(dispatch == &team->t.t_dispatch[tid]);
  3757. dispatch->th_disp_index = 0;
  3758. dispatch->th_doacross_buf_idx = 0;
  3759. if (!dispatch->th_disp_buffer) {
  3760. dispatch->th_disp_buffer =
  3761. (dispatch_private_info_t *)__kmp_allocate(disp_size);
  3762. if (__kmp_storage_map) {
  3763. __kmp_print_storage_map_gtid(
  3764. gtid, &dispatch->th_disp_buffer[0],
  3765. &dispatch->th_disp_buffer[team->t.t_max_nproc == 1
  3766. ? 1
  3767. : __kmp_dispatch_num_buffers],
  3768. disp_size,
  3769. "th_%d.th_dispatch.th_disp_buffer "
  3770. "(team_%d.t_dispatch[%d].th_disp_buffer)",
  3771. gtid, team->t.t_id, gtid);
  3772. }
  3773. } else {
  3774. memset(&dispatch->th_disp_buffer[0], '\0', disp_size);
  3775. }
  3776. dispatch->th_dispatch_pr_current = 0;
  3777. dispatch->th_dispatch_sh_current = 0;
  3778. dispatch->th_deo_fcn = 0; /* ORDERED */
  3779. dispatch->th_dxo_fcn = 0; /* END ORDERED */
  3780. }
  3781. this_thr->th.th_next_pool = NULL;
  3782. if (!this_thr->th.th_task_state_memo_stack) {
  3783. size_t i;
  3784. this_thr->th.th_task_state_memo_stack =
  3785. (kmp_uint8 *)__kmp_allocate(4 * sizeof(kmp_uint8));
  3786. this_thr->th.th_task_state_top = 0;
  3787. this_thr->th.th_task_state_stack_sz = 4;
  3788. for (i = 0; i < this_thr->th.th_task_state_stack_sz;
  3789. ++i) // zero init the stack
  3790. this_thr->th.th_task_state_memo_stack[i] = 0;
  3791. }
  3792. KMP_DEBUG_ASSERT(!this_thr->th.th_spin_here);
  3793. KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);
  3794. KMP_MB();
  3795. }
  3796. /* allocate a new thread for the requesting team. this is only called from
  3797. within a forkjoin critical section. we will first try to get an available
  3798. thread from the thread pool. if none is available, we will fork a new one
  3799. assuming we are able to create a new one. this should be assured, as the
  3800. caller should check on this first. */
  3801. kmp_info_t *__kmp_allocate_thread(kmp_root_t *root, kmp_team_t *team,
  3802. int new_tid) {
  3803. kmp_team_t *serial_team;
  3804. kmp_info_t *new_thr;
  3805. int new_gtid;
  3806. KA_TRACE(20, ("__kmp_allocate_thread: T#%d\n", __kmp_get_gtid()));
  3807. KMP_DEBUG_ASSERT(root && team);
  3808. #if !KMP_NESTED_HOT_TEAMS
  3809. KMP_DEBUG_ASSERT(KMP_MASTER_GTID(__kmp_get_gtid()));
  3810. #endif
  3811. KMP_MB();
  3812. /* first, try to get one from the thread pool */
  3813. if (__kmp_thread_pool) {
  3814. new_thr = CCAST(kmp_info_t *, __kmp_thread_pool);
  3815. __kmp_thread_pool = (volatile kmp_info_t *)new_thr->th.th_next_pool;
  3816. if (new_thr == __kmp_thread_pool_insert_pt) {
  3817. __kmp_thread_pool_insert_pt = NULL;
  3818. }
  3819. TCW_4(new_thr->th.th_in_pool, FALSE);
  3820. __kmp_suspend_initialize_thread(new_thr);
  3821. __kmp_lock_suspend_mx(new_thr);
  3822. if (new_thr->th.th_active_in_pool == TRUE) {
  3823. KMP_DEBUG_ASSERT(new_thr->th.th_active == TRUE);
  3824. KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth);
  3825. new_thr->th.th_active_in_pool = FALSE;
  3826. }
  3827. __kmp_unlock_suspend_mx(new_thr);
  3828. KA_TRACE(20, ("__kmp_allocate_thread: T#%d using thread T#%d\n",
  3829. __kmp_get_gtid(), new_thr->th.th_info.ds.ds_gtid));
  3830. KMP_ASSERT(!new_thr->th.th_team);
  3831. KMP_DEBUG_ASSERT(__kmp_nth < __kmp_threads_capacity);
  3832. /* setup the thread structure */
  3833. __kmp_initialize_info(new_thr, team, new_tid,
  3834. new_thr->th.th_info.ds.ds_gtid);
  3835. KMP_DEBUG_ASSERT(new_thr->th.th_serial_team);
  3836. TCW_4(__kmp_nth, __kmp_nth + 1);
  3837. new_thr->th.th_task_state = 0;
  3838. new_thr->th.th_task_state_top = 0;
  3839. new_thr->th.th_task_state_stack_sz = 4;
  3840. if (__kmp_barrier_gather_pattern[bs_forkjoin_barrier] == bp_dist_bar) {
  3841. // Make sure pool thread has transitioned to waiting on own thread struct
  3842. KMP_DEBUG_ASSERT(new_thr->th.th_used_in_team.load() == 0);
  3843. // Thread activated in __kmp_allocate_team when increasing team size
  3844. }
  3845. #ifdef KMP_ADJUST_BLOCKTIME
  3846. /* Adjust blocktime back to zero if necessary */
  3847. /* Middle initialization might not have occurred yet */
  3848. if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
  3849. if (__kmp_nth > __kmp_avail_proc) {
  3850. __kmp_zero_bt = TRUE;
  3851. }
  3852. }
  3853. #endif /* KMP_ADJUST_BLOCKTIME */
  3854. #if KMP_DEBUG
  3855. // If thread entered pool via __kmp_free_thread, wait_flag should !=
  3856. // KMP_BARRIER_PARENT_FLAG.
  3857. int b;
  3858. kmp_balign_t *balign = new_thr->th.th_bar;
  3859. for (b = 0; b < bs_last_barrier; ++b)
  3860. KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG);
  3861. #endif
  3862. KF_TRACE(10, ("__kmp_allocate_thread: T#%d using thread %p T#%d\n",
  3863. __kmp_get_gtid(), new_thr, new_thr->th.th_info.ds.ds_gtid));
  3864. KMP_MB();
  3865. return new_thr;
  3866. }
  3867. /* no, well fork a new one */
  3868. KMP_ASSERT(__kmp_nth == __kmp_all_nth);
  3869. KMP_ASSERT(__kmp_all_nth < __kmp_threads_capacity);
  3870. #if KMP_USE_MONITOR
  3871. // If this is the first worker thread the RTL is creating, then also
  3872. // launch the monitor thread. We try to do this as early as possible.
  3873. if (!TCR_4(__kmp_init_monitor)) {
  3874. __kmp_acquire_bootstrap_lock(&__kmp_monitor_lock);
  3875. if (!TCR_4(__kmp_init_monitor)) {
  3876. KF_TRACE(10, ("before __kmp_create_monitor\n"));
  3877. TCW_4(__kmp_init_monitor, 1);
  3878. __kmp_create_monitor(&__kmp_monitor);
  3879. KF_TRACE(10, ("after __kmp_create_monitor\n"));
  3880. #if KMP_OS_WINDOWS
  3881. // AC: wait until monitor has started. This is a fix for CQ232808.
  3882. // The reason is that if the library is loaded/unloaded in a loop with
  3883. // small (parallel) work in between, then there is high probability that
  3884. // monitor thread started after the library shutdown. At shutdown it is
  3885. // too late to cope with the problem, because when the primary thread is
  3886. // in DllMain (process detach) the monitor has no chances to start (it is
  3887. // blocked), and primary thread has no means to inform the monitor that
  3888. // the library has gone, because all the memory which the monitor can
  3889. // access is going to be released/reset.
  3890. while (TCR_4(__kmp_init_monitor) < 2) {
  3891. KMP_YIELD(TRUE);
  3892. }
  3893. KF_TRACE(10, ("after monitor thread has started\n"));
  3894. #endif
  3895. }
  3896. __kmp_release_bootstrap_lock(&__kmp_monitor_lock);
  3897. }
  3898. #endif
  3899. KMP_MB();
  3900. {
  3901. int new_start_gtid = TCR_4(__kmp_init_hidden_helper_threads)
  3902. ? 1
  3903. : __kmp_hidden_helper_threads_num + 1;
  3904. for (new_gtid = new_start_gtid; TCR_PTR(__kmp_threads[new_gtid]) != NULL;
  3905. ++new_gtid) {
  3906. KMP_DEBUG_ASSERT(new_gtid < __kmp_threads_capacity);
  3907. }
  3908. if (TCR_4(__kmp_init_hidden_helper_threads)) {
  3909. KMP_DEBUG_ASSERT(new_gtid <= __kmp_hidden_helper_threads_num);
  3910. }
  3911. }
  3912. /* allocate space for it. */
  3913. new_thr = (kmp_info_t *)__kmp_allocate(sizeof(kmp_info_t));
  3914. TCW_SYNC_PTR(__kmp_threads[new_gtid], new_thr);
  3915. #if USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG
  3916. // suppress race conditions detection on synchronization flags in debug mode
  3917. // this helps to analyze library internals eliminating false positives
  3918. __itt_suppress_mark_range(
  3919. __itt_suppress_range, __itt_suppress_threading_errors,
  3920. &new_thr->th.th_sleep_loc, sizeof(new_thr->th.th_sleep_loc));
  3921. __itt_suppress_mark_range(
  3922. __itt_suppress_range, __itt_suppress_threading_errors,
  3923. &new_thr->th.th_reap_state, sizeof(new_thr->th.th_reap_state));
  3924. #if KMP_OS_WINDOWS
  3925. __itt_suppress_mark_range(
  3926. __itt_suppress_range, __itt_suppress_threading_errors,
  3927. &new_thr->th.th_suspend_init, sizeof(new_thr->th.th_suspend_init));
  3928. #else
  3929. __itt_suppress_mark_range(__itt_suppress_range,
  3930. __itt_suppress_threading_errors,
  3931. &new_thr->th.th_suspend_init_count,
  3932. sizeof(new_thr->th.th_suspend_init_count));
  3933. #endif
  3934. // TODO: check if we need to also suppress b_arrived flags
  3935. __itt_suppress_mark_range(__itt_suppress_range,
  3936. __itt_suppress_threading_errors,
  3937. CCAST(kmp_uint64 *, &new_thr->th.th_bar[0].bb.b_go),
  3938. sizeof(new_thr->th.th_bar[0].bb.b_go));
  3939. __itt_suppress_mark_range(__itt_suppress_range,
  3940. __itt_suppress_threading_errors,
  3941. CCAST(kmp_uint64 *, &new_thr->th.th_bar[1].bb.b_go),
  3942. sizeof(new_thr->th.th_bar[1].bb.b_go));
  3943. __itt_suppress_mark_range(__itt_suppress_range,
  3944. __itt_suppress_threading_errors,
  3945. CCAST(kmp_uint64 *, &new_thr->th.th_bar[2].bb.b_go),
  3946. sizeof(new_thr->th.th_bar[2].bb.b_go));
  3947. #endif /* USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG */
  3948. if (__kmp_storage_map) {
  3949. __kmp_print_thread_storage_map(new_thr, new_gtid);
  3950. }
  3951. // add the reserve serialized team, initialized from the team's primary thread
  3952. {
  3953. kmp_internal_control_t r_icvs = __kmp_get_x_global_icvs(team);
  3954. KF_TRACE(10, ("__kmp_allocate_thread: before th_serial/serial_team\n"));
  3955. new_thr->th.th_serial_team = serial_team =
  3956. (kmp_team_t *)__kmp_allocate_team(root, 1, 1,
  3957. #if OMPT_SUPPORT
  3958. ompt_data_none, // root parallel id
  3959. #endif
  3960. proc_bind_default, &r_icvs,
  3961. 0 USE_NESTED_HOT_ARG(NULL));
  3962. }
  3963. KMP_ASSERT(serial_team);
  3964. serial_team->t.t_serialized = 0; // AC: the team created in reserve, not for
  3965. // execution (it is unused for now).
  3966. serial_team->t.t_threads[0] = new_thr;
  3967. KF_TRACE(10,
  3968. ("__kmp_allocate_thread: after th_serial/serial_team : new_thr=%p\n",
  3969. new_thr));
  3970. /* setup the thread structures */
  3971. __kmp_initialize_info(new_thr, team, new_tid, new_gtid);
  3972. #if USE_FAST_MEMORY
  3973. __kmp_initialize_fast_memory(new_thr);
  3974. #endif /* USE_FAST_MEMORY */
  3975. #if KMP_USE_BGET
  3976. KMP_DEBUG_ASSERT(new_thr->th.th_local.bget_data == NULL);
  3977. __kmp_initialize_bget(new_thr);
  3978. #endif
  3979. __kmp_init_random(new_thr); // Initialize random number generator
  3980. /* Initialize these only once when thread is grabbed for a team allocation */
  3981. KA_TRACE(20,
  3982. ("__kmp_allocate_thread: T#%d init go fork=%u, plain=%u\n",
  3983. __kmp_get_gtid(), KMP_INIT_BARRIER_STATE, KMP_INIT_BARRIER_STATE));
  3984. int b;
  3985. kmp_balign_t *balign = new_thr->th.th_bar;
  3986. for (b = 0; b < bs_last_barrier; ++b) {
  3987. balign[b].bb.b_go = KMP_INIT_BARRIER_STATE;
  3988. balign[b].bb.team = NULL;
  3989. balign[b].bb.wait_flag = KMP_BARRIER_NOT_WAITING;
  3990. balign[b].bb.use_oncore_barrier = 0;
  3991. }
  3992. TCW_PTR(new_thr->th.th_sleep_loc, NULL);
  3993. new_thr->th.th_sleep_loc_type = flag_unset;
  3994. new_thr->th.th_spin_here = FALSE;
  3995. new_thr->th.th_next_waiting = 0;
  3996. #if KMP_OS_UNIX
  3997. new_thr->th.th_blocking = false;
  3998. #endif
  3999. #if KMP_AFFINITY_SUPPORTED
  4000. new_thr->th.th_current_place = KMP_PLACE_UNDEFINED;
  4001. new_thr->th.th_new_place = KMP_PLACE_UNDEFINED;
  4002. new_thr->th.th_first_place = KMP_PLACE_UNDEFINED;
  4003. new_thr->th.th_last_place = KMP_PLACE_UNDEFINED;
  4004. #endif
  4005. new_thr->th.th_def_allocator = __kmp_def_allocator;
  4006. new_thr->th.th_prev_level = 0;
  4007. new_thr->th.th_prev_num_threads = 1;
  4008. TCW_4(new_thr->th.th_in_pool, FALSE);
  4009. new_thr->th.th_active_in_pool = FALSE;
  4010. TCW_4(new_thr->th.th_active, TRUE);
  4011. /* adjust the global counters */
  4012. __kmp_all_nth++;
  4013. __kmp_nth++;
  4014. // if __kmp_adjust_gtid_mode is set, then we use method #1 (sp search) for low
  4015. // numbers of procs, and method #2 (keyed API call) for higher numbers.
  4016. if (__kmp_adjust_gtid_mode) {
  4017. if (__kmp_all_nth >= __kmp_tls_gtid_min) {
  4018. if (TCR_4(__kmp_gtid_mode) != 2) {
  4019. TCW_4(__kmp_gtid_mode, 2);
  4020. }
  4021. } else {
  4022. if (TCR_4(__kmp_gtid_mode) != 1) {
  4023. TCW_4(__kmp_gtid_mode, 1);
  4024. }
  4025. }
  4026. }
  4027. #ifdef KMP_ADJUST_BLOCKTIME
  4028. /* Adjust blocktime back to zero if necessary */
  4029. /* Middle initialization might not have occurred yet */
  4030. if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
  4031. if (__kmp_nth > __kmp_avail_proc) {
  4032. __kmp_zero_bt = TRUE;
  4033. }
  4034. }
  4035. #endif /* KMP_ADJUST_BLOCKTIME */
  4036. /* actually fork it and create the new worker thread */
  4037. KF_TRACE(
  4038. 10, ("__kmp_allocate_thread: before __kmp_create_worker: %p\n", new_thr));
  4039. __kmp_create_worker(new_gtid, new_thr, __kmp_stksize);
  4040. KF_TRACE(10,
  4041. ("__kmp_allocate_thread: after __kmp_create_worker: %p\n", new_thr));
  4042. KA_TRACE(20, ("__kmp_allocate_thread: T#%d forked T#%d\n", __kmp_get_gtid(),
  4043. new_gtid));
  4044. KMP_MB();
  4045. return new_thr;
  4046. }
  4047. /* Reinitialize team for reuse.
  4048. The hot team code calls this case at every fork barrier, so EPCC barrier
  4049. test are extremely sensitive to changes in it, esp. writes to the team
  4050. struct, which cause a cache invalidation in all threads.
  4051. IF YOU TOUCH THIS ROUTINE, RUN EPCC C SYNCBENCH ON A BIG-IRON MACHINE!!! */
  4052. static void __kmp_reinitialize_team(kmp_team_t *team,
  4053. kmp_internal_control_t *new_icvs,
  4054. ident_t *loc) {
  4055. KF_TRACE(10, ("__kmp_reinitialize_team: enter this_thread=%p team=%p\n",
  4056. team->t.t_threads[0], team));
  4057. KMP_DEBUG_ASSERT(team && new_icvs);
  4058. KMP_DEBUG_ASSERT((!TCR_4(__kmp_init_parallel)) || new_icvs->nproc);
  4059. KMP_CHECK_UPDATE(team->t.t_ident, loc);
  4060. KMP_CHECK_UPDATE(team->t.t_id, KMP_GEN_TEAM_ID());
  4061. // Copy ICVs to the primary thread's implicit taskdata
  4062. __kmp_init_implicit_task(loc, team->t.t_threads[0], team, 0, FALSE);
  4063. copy_icvs(&team->t.t_implicit_task_taskdata[0].td_icvs, new_icvs);
  4064. KF_TRACE(10, ("__kmp_reinitialize_team: exit this_thread=%p team=%p\n",
  4065. team->t.t_threads[0], team));
  4066. }
  4067. /* Initialize the team data structure.
  4068. This assumes the t_threads and t_max_nproc are already set.
  4069. Also, we don't touch the arguments */
  4070. static void __kmp_initialize_team(kmp_team_t *team, int new_nproc,
  4071. kmp_internal_control_t *new_icvs,
  4072. ident_t *loc) {
  4073. KF_TRACE(10, ("__kmp_initialize_team: enter: team=%p\n", team));
  4074. /* verify */
  4075. KMP_DEBUG_ASSERT(team);
  4076. KMP_DEBUG_ASSERT(new_nproc <= team->t.t_max_nproc);
  4077. KMP_DEBUG_ASSERT(team->t.t_threads);
  4078. KMP_MB();
  4079. team->t.t_master_tid = 0; /* not needed */
  4080. /* team->t.t_master_bar; not needed */
  4081. team->t.t_serialized = new_nproc > 1 ? 0 : 1;
  4082. team->t.t_nproc = new_nproc;
  4083. /* team->t.t_parent = NULL; TODO not needed & would mess up hot team */
  4084. team->t.t_next_pool = NULL;
  4085. /* memset( team->t.t_threads, 0, sizeof(kmp_info_t*)*new_nproc ); would mess
  4086. * up hot team */
  4087. TCW_SYNC_PTR(team->t.t_pkfn, NULL); /* not needed */
  4088. team->t.t_invoke = NULL; /* not needed */
  4089. // TODO???: team->t.t_max_active_levels = new_max_active_levels;
  4090. team->t.t_sched.sched = new_icvs->sched.sched;
  4091. #if KMP_ARCH_X86 || KMP_ARCH_X86_64
  4092. team->t.t_fp_control_saved = FALSE; /* not needed */
  4093. team->t.t_x87_fpu_control_word = 0; /* not needed */
  4094. team->t.t_mxcsr = 0; /* not needed */
  4095. #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
  4096. team->t.t_construct = 0;
  4097. team->t.t_ordered.dt.t_value = 0;
  4098. team->t.t_master_active = FALSE;
  4099. #ifdef KMP_DEBUG
  4100. team->t.t_copypriv_data = NULL; /* not necessary, but nice for debugging */
  4101. #endif
  4102. #if KMP_OS_WINDOWS
  4103. team->t.t_copyin_counter = 0; /* for barrier-free copyin implementation */
  4104. #endif
  4105. team->t.t_control_stack_top = NULL;
  4106. __kmp_reinitialize_team(team, new_icvs, loc);
  4107. KMP_MB();
  4108. KF_TRACE(10, ("__kmp_initialize_team: exit: team=%p\n", team));
  4109. }
  4110. #if (KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED
  4111. /* Sets full mask for thread and returns old mask, no changes to structures. */
  4112. static void
  4113. __kmp_set_thread_affinity_mask_full_tmp(kmp_affin_mask_t *old_mask) {
  4114. if (KMP_AFFINITY_CAPABLE()) {
  4115. int status;
  4116. if (old_mask != NULL) {
  4117. status = __kmp_get_system_affinity(old_mask, TRUE);
  4118. int error = errno;
  4119. if (status != 0) {
  4120. __kmp_fatal(KMP_MSG(ChangeThreadAffMaskError), KMP_ERR(error),
  4121. __kmp_msg_null);
  4122. }
  4123. }
  4124. __kmp_set_system_affinity(__kmp_affin_fullMask, TRUE);
  4125. }
  4126. }
  4127. #endif
  4128. #if KMP_AFFINITY_SUPPORTED
  4129. // __kmp_partition_places() is the heart of the OpenMP 4.0 affinity mechanism.
  4130. // It calculates the worker + primary thread's partition based upon the parent
  4131. // thread's partition, and binds each worker to a thread in their partition.
  4132. // The primary thread's partition should already include its current binding.
  4133. static void __kmp_partition_places(kmp_team_t *team, int update_master_only) {
  4134. // Do not partition places for the hidden helper team
  4135. if (KMP_HIDDEN_HELPER_TEAM(team))
  4136. return;
  4137. // Copy the primary thread's place partition to the team struct
  4138. kmp_info_t *master_th = team->t.t_threads[0];
  4139. KMP_DEBUG_ASSERT(master_th != NULL);
  4140. kmp_proc_bind_t proc_bind = team->t.t_proc_bind;
  4141. int first_place = master_th->th.th_first_place;
  4142. int last_place = master_th->th.th_last_place;
  4143. int masters_place = master_th->th.th_current_place;
  4144. team->t.t_first_place = first_place;
  4145. team->t.t_last_place = last_place;
  4146. KA_TRACE(20, ("__kmp_partition_places: enter: proc_bind = %d T#%d(%d:0) "
  4147. "bound to place %d partition = [%d,%d]\n",
  4148. proc_bind, __kmp_gtid_from_thread(team->t.t_threads[0]),
  4149. team->t.t_id, masters_place, first_place, last_place));
  4150. switch (proc_bind) {
  4151. case proc_bind_default:
  4152. // Serial teams might have the proc_bind policy set to proc_bind_default.
  4153. // Not an issue -- we don't rebind primary thread for any proc_bind policy.
  4154. KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
  4155. break;
  4156. case proc_bind_primary: {
  4157. int f;
  4158. int n_th = team->t.t_nproc;
  4159. for (f = 1; f < n_th; f++) {
  4160. kmp_info_t *th = team->t.t_threads[f];
  4161. KMP_DEBUG_ASSERT(th != NULL);
  4162. th->th.th_first_place = first_place;
  4163. th->th.th_last_place = last_place;
  4164. th->th.th_new_place = masters_place;
  4165. if (__kmp_display_affinity && masters_place != th->th.th_current_place &&
  4166. team->t.t_display_affinity != 1) {
  4167. team->t.t_display_affinity = 1;
  4168. }
  4169. KA_TRACE(100, ("__kmp_partition_places: primary: T#%d(%d:%d) place %d "
  4170. "partition = [%d,%d]\n",
  4171. __kmp_gtid_from_thread(team->t.t_threads[f]), team->t.t_id,
  4172. f, masters_place, first_place, last_place));
  4173. }
  4174. } break;
  4175. case proc_bind_close: {
  4176. int f;
  4177. int n_th = team->t.t_nproc;
  4178. int n_places;
  4179. if (first_place <= last_place) {
  4180. n_places = last_place - first_place + 1;
  4181. } else {
  4182. n_places = __kmp_affinity_num_masks - first_place + last_place + 1;
  4183. }
  4184. if (n_th <= n_places) {
  4185. int place = masters_place;
  4186. for (f = 1; f < n_th; f++) {
  4187. kmp_info_t *th = team->t.t_threads[f];
  4188. KMP_DEBUG_ASSERT(th != NULL);
  4189. if (place == last_place) {
  4190. place = first_place;
  4191. } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
  4192. place = 0;
  4193. } else {
  4194. place++;
  4195. }
  4196. th->th.th_first_place = first_place;
  4197. th->th.th_last_place = last_place;
  4198. th->th.th_new_place = place;
  4199. if (__kmp_display_affinity && place != th->th.th_current_place &&
  4200. team->t.t_display_affinity != 1) {
  4201. team->t.t_display_affinity = 1;
  4202. }
  4203. KA_TRACE(100, ("__kmp_partition_places: close: T#%d(%d:%d) place %d "
  4204. "partition = [%d,%d]\n",
  4205. __kmp_gtid_from_thread(team->t.t_threads[f]),
  4206. team->t.t_id, f, place, first_place, last_place));
  4207. }
  4208. } else {
  4209. int S, rem, gap, s_count;
  4210. S = n_th / n_places;
  4211. s_count = 0;
  4212. rem = n_th - (S * n_places);
  4213. gap = rem > 0 ? n_places / rem : n_places;
  4214. int place = masters_place;
  4215. int gap_ct = gap;
  4216. for (f = 0; f < n_th; f++) {
  4217. kmp_info_t *th = team->t.t_threads[f];
  4218. KMP_DEBUG_ASSERT(th != NULL);
  4219. th->th.th_first_place = first_place;
  4220. th->th.th_last_place = last_place;
  4221. th->th.th_new_place = place;
  4222. if (__kmp_display_affinity && place != th->th.th_current_place &&
  4223. team->t.t_display_affinity != 1) {
  4224. team->t.t_display_affinity = 1;
  4225. }
  4226. s_count++;
  4227. if ((s_count == S) && rem && (gap_ct == gap)) {
  4228. // do nothing, add an extra thread to place on next iteration
  4229. } else if ((s_count == S + 1) && rem && (gap_ct == gap)) {
  4230. // we added an extra thread to this place; move to next place
  4231. if (place == last_place) {
  4232. place = first_place;
  4233. } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
  4234. place = 0;
  4235. } else {
  4236. place++;
  4237. }
  4238. s_count = 0;
  4239. gap_ct = 1;
  4240. rem--;
  4241. } else if (s_count == S) { // place full; don't add extra
  4242. if (place == last_place) {
  4243. place = first_place;
  4244. } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
  4245. place = 0;
  4246. } else {
  4247. place++;
  4248. }
  4249. gap_ct++;
  4250. s_count = 0;
  4251. }
  4252. KA_TRACE(100,
  4253. ("__kmp_partition_places: close: T#%d(%d:%d) place %d "
  4254. "partition = [%d,%d]\n",
  4255. __kmp_gtid_from_thread(team->t.t_threads[f]), team->t.t_id, f,
  4256. th->th.th_new_place, first_place, last_place));
  4257. }
  4258. KMP_DEBUG_ASSERT(place == masters_place);
  4259. }
  4260. } break;
  4261. case proc_bind_spread: {
  4262. int f;
  4263. int n_th = team->t.t_nproc;
  4264. int n_places;
  4265. int thidx;
  4266. if (first_place <= last_place) {
  4267. n_places = last_place - first_place + 1;
  4268. } else {
  4269. n_places = __kmp_affinity_num_masks - first_place + last_place + 1;
  4270. }
  4271. if (n_th <= n_places) {
  4272. int place = -1;
  4273. if (n_places != static_cast<int>(__kmp_affinity_num_masks)) {
  4274. int S = n_places / n_th;
  4275. int s_count, rem, gap, gap_ct;
  4276. place = masters_place;
  4277. rem = n_places - n_th * S;
  4278. gap = rem ? n_th / rem : 1;
  4279. gap_ct = gap;
  4280. thidx = n_th;
  4281. if (update_master_only == 1)
  4282. thidx = 1;
  4283. for (f = 0; f < thidx; f++) {
  4284. kmp_info_t *th = team->t.t_threads[f];
  4285. KMP_DEBUG_ASSERT(th != NULL);
  4286. th->th.th_first_place = place;
  4287. th->th.th_new_place = place;
  4288. if (__kmp_display_affinity && place != th->th.th_current_place &&
  4289. team->t.t_display_affinity != 1) {
  4290. team->t.t_display_affinity = 1;
  4291. }
  4292. s_count = 1;
  4293. while (s_count < S) {
  4294. if (place == last_place) {
  4295. place = first_place;
  4296. } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
  4297. place = 0;
  4298. } else {
  4299. place++;
  4300. }
  4301. s_count++;
  4302. }
  4303. if (rem && (gap_ct == gap)) {
  4304. if (place == last_place) {
  4305. place = first_place;
  4306. } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
  4307. place = 0;
  4308. } else {
  4309. place++;
  4310. }
  4311. rem--;
  4312. gap_ct = 0;
  4313. }
  4314. th->th.th_last_place = place;
  4315. gap_ct++;
  4316. if (place == last_place) {
  4317. place = first_place;
  4318. } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
  4319. place = 0;
  4320. } else {
  4321. place++;
  4322. }
  4323. KA_TRACE(100,
  4324. ("__kmp_partition_places: spread: T#%d(%d:%d) place %d "
  4325. "partition = [%d,%d], __kmp_affinity_num_masks: %u\n",
  4326. __kmp_gtid_from_thread(team->t.t_threads[f]), team->t.t_id,
  4327. f, th->th.th_new_place, th->th.th_first_place,
  4328. th->th.th_last_place, __kmp_affinity_num_masks));
  4329. }
  4330. } else {
  4331. /* Having uniform space of available computation places I can create
  4332. T partitions of round(P/T) size and put threads into the first
  4333. place of each partition. */
  4334. double current = static_cast<double>(masters_place);
  4335. double spacing =
  4336. (static_cast<double>(n_places + 1) / static_cast<double>(n_th));
  4337. int first, last;
  4338. kmp_info_t *th;
  4339. thidx = n_th + 1;
  4340. if (update_master_only == 1)
  4341. thidx = 1;
  4342. for (f = 0; f < thidx; f++) {
  4343. first = static_cast<int>(current);
  4344. last = static_cast<int>(current + spacing) - 1;
  4345. KMP_DEBUG_ASSERT(last >= first);
  4346. if (first >= n_places) {
  4347. if (masters_place) {
  4348. first -= n_places;
  4349. last -= n_places;
  4350. if (first == (masters_place + 1)) {
  4351. KMP_DEBUG_ASSERT(f == n_th);
  4352. first--;
  4353. }
  4354. if (last == masters_place) {
  4355. KMP_DEBUG_ASSERT(f == (n_th - 1));
  4356. last--;
  4357. }
  4358. } else {
  4359. KMP_DEBUG_ASSERT(f == n_th);
  4360. first = 0;
  4361. last = 0;
  4362. }
  4363. }
  4364. if (last >= n_places) {
  4365. last = (n_places - 1);
  4366. }
  4367. place = first;
  4368. current += spacing;
  4369. if (f < n_th) {
  4370. KMP_DEBUG_ASSERT(0 <= first);
  4371. KMP_DEBUG_ASSERT(n_places > first);
  4372. KMP_DEBUG_ASSERT(0 <= last);
  4373. KMP_DEBUG_ASSERT(n_places > last);
  4374. KMP_DEBUG_ASSERT(last_place >= first_place);
  4375. th = team->t.t_threads[f];
  4376. KMP_DEBUG_ASSERT(th);
  4377. th->th.th_first_place = first;
  4378. th->th.th_new_place = place;
  4379. th->th.th_last_place = last;
  4380. if (__kmp_display_affinity && place != th->th.th_current_place &&
  4381. team->t.t_display_affinity != 1) {
  4382. team->t.t_display_affinity = 1;
  4383. }
  4384. KA_TRACE(100,
  4385. ("__kmp_partition_places: spread: T#%d(%d:%d) place %d "
  4386. "partition = [%d,%d], spacing = %.4f\n",
  4387. __kmp_gtid_from_thread(team->t.t_threads[f]),
  4388. team->t.t_id, f, th->th.th_new_place,
  4389. th->th.th_first_place, th->th.th_last_place, spacing));
  4390. }
  4391. }
  4392. }
  4393. KMP_DEBUG_ASSERT(update_master_only || place == masters_place);
  4394. } else {
  4395. int S, rem, gap, s_count;
  4396. S = n_th / n_places;
  4397. s_count = 0;
  4398. rem = n_th - (S * n_places);
  4399. gap = rem > 0 ? n_places / rem : n_places;
  4400. int place = masters_place;
  4401. int gap_ct = gap;
  4402. thidx = n_th;
  4403. if (update_master_only == 1)
  4404. thidx = 1;
  4405. for (f = 0; f < thidx; f++) {
  4406. kmp_info_t *th = team->t.t_threads[f];
  4407. KMP_DEBUG_ASSERT(th != NULL);
  4408. th->th.th_first_place = place;
  4409. th->th.th_last_place = place;
  4410. th->th.th_new_place = place;
  4411. if (__kmp_display_affinity && place != th->th.th_current_place &&
  4412. team->t.t_display_affinity != 1) {
  4413. team->t.t_display_affinity = 1;
  4414. }
  4415. s_count++;
  4416. if ((s_count == S) && rem && (gap_ct == gap)) {
  4417. // do nothing, add an extra thread to place on next iteration
  4418. } else if ((s_count == S + 1) && rem && (gap_ct == gap)) {
  4419. // we added an extra thread to this place; move on to next place
  4420. if (place == last_place) {
  4421. place = first_place;
  4422. } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
  4423. place = 0;
  4424. } else {
  4425. place++;
  4426. }
  4427. s_count = 0;
  4428. gap_ct = 1;
  4429. rem--;
  4430. } else if (s_count == S) { // place is full; don't add extra thread
  4431. if (place == last_place) {
  4432. place = first_place;
  4433. } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
  4434. place = 0;
  4435. } else {
  4436. place++;
  4437. }
  4438. gap_ct++;
  4439. s_count = 0;
  4440. }
  4441. KA_TRACE(100, ("__kmp_partition_places: spread: T#%d(%d:%d) place %d "
  4442. "partition = [%d,%d]\n",
  4443. __kmp_gtid_from_thread(team->t.t_threads[f]),
  4444. team->t.t_id, f, th->th.th_new_place,
  4445. th->th.th_first_place, th->th.th_last_place));
  4446. }
  4447. KMP_DEBUG_ASSERT(update_master_only || place == masters_place);
  4448. }
  4449. } break;
  4450. default:
  4451. break;
  4452. }
  4453. KA_TRACE(20, ("__kmp_partition_places: exit T#%d\n", team->t.t_id));
  4454. }
  4455. #endif // KMP_AFFINITY_SUPPORTED
  4456. /* allocate a new team data structure to use. take one off of the free pool if
  4457. available */
  4458. kmp_team_t *
  4459. __kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc,
  4460. #if OMPT_SUPPORT
  4461. ompt_data_t ompt_parallel_data,
  4462. #endif
  4463. kmp_proc_bind_t new_proc_bind,
  4464. kmp_internal_control_t *new_icvs,
  4465. int argc USE_NESTED_HOT_ARG(kmp_info_t *master)) {
  4466. KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(KMP_allocate_team);
  4467. int f;
  4468. kmp_team_t *team;
  4469. int use_hot_team = !root->r.r_active;
  4470. int level = 0;
  4471. int do_place_partition = 1;
  4472. KA_TRACE(20, ("__kmp_allocate_team: called\n"));
  4473. KMP_DEBUG_ASSERT(new_nproc >= 1 && argc >= 0);
  4474. KMP_DEBUG_ASSERT(max_nproc >= new_nproc);
  4475. KMP_MB();
  4476. #if KMP_NESTED_HOT_TEAMS
  4477. kmp_hot_team_ptr_t *hot_teams;
  4478. if (master) {
  4479. team = master->th.th_team;
  4480. level = team->t.t_active_level;
  4481. if (master->th.th_teams_microtask) { // in teams construct?
  4482. if (master->th.th_teams_size.nteams > 1 &&
  4483. ( // #teams > 1
  4484. team->t.t_pkfn ==
  4485. (microtask_t)__kmp_teams_master || // inner fork of the teams
  4486. master->th.th_teams_level <
  4487. team->t.t_level)) { // or nested parallel inside the teams
  4488. ++level; // not increment if #teams==1, or for outer fork of the teams;
  4489. // increment otherwise
  4490. }
  4491. // Do not perform the place partition if inner fork of the teams
  4492. // Wait until nested parallel region encountered inside teams construct
  4493. if ((master->th.th_teams_size.nteams == 1 &&
  4494. master->th.th_teams_level >= team->t.t_level) ||
  4495. (team->t.t_pkfn == (microtask_t)__kmp_teams_master))
  4496. do_place_partition = 0;
  4497. }
  4498. hot_teams = master->th.th_hot_teams;
  4499. if (level < __kmp_hot_teams_max_level && hot_teams &&
  4500. hot_teams[level].hot_team) {
  4501. // hot team has already been allocated for given level
  4502. use_hot_team = 1;
  4503. } else {
  4504. use_hot_team = 0;
  4505. }
  4506. } else {
  4507. // check we won't access uninitialized hot_teams, just in case
  4508. KMP_DEBUG_ASSERT(new_nproc == 1);
  4509. }
  4510. #endif
  4511. // Optimization to use a "hot" team
  4512. if (use_hot_team && new_nproc > 1) {
  4513. KMP_DEBUG_ASSERT(new_nproc <= max_nproc);
  4514. #if KMP_NESTED_HOT_TEAMS
  4515. team = hot_teams[level].hot_team;
  4516. #else
  4517. team = root->r.r_hot_team;
  4518. #endif
  4519. #if KMP_DEBUG
  4520. if (__kmp_tasking_mode != tskm_immediate_exec) {
  4521. KA_TRACE(20, ("__kmp_allocate_team: hot team task_team[0] = %p "
  4522. "task_team[1] = %p before reinit\n",
  4523. team->t.t_task_team[0], team->t.t_task_team[1]));
  4524. }
  4525. #endif
  4526. if (team->t.t_nproc != new_nproc &&
  4527. __kmp_barrier_release_pattern[bs_forkjoin_barrier] == bp_dist_bar) {
  4528. // Distributed barrier may need a resize
  4529. int old_nthr = team->t.t_nproc;
  4530. __kmp_resize_dist_barrier(team, old_nthr, new_nproc);
  4531. }
  4532. // If not doing the place partition, then reset the team's proc bind
  4533. // to indicate that partitioning of all threads still needs to take place
  4534. if (do_place_partition == 0)
  4535. team->t.t_proc_bind = proc_bind_default;
  4536. // Has the number of threads changed?
  4537. /* Let's assume the most common case is that the number of threads is
  4538. unchanged, and put that case first. */
  4539. if (team->t.t_nproc == new_nproc) { // Check changes in number of threads
  4540. KA_TRACE(20, ("__kmp_allocate_team: reusing hot team\n"));
  4541. // This case can mean that omp_set_num_threads() was called and the hot
  4542. // team size was already reduced, so we check the special flag
  4543. if (team->t.t_size_changed == -1) {
  4544. team->t.t_size_changed = 1;
  4545. } else {
  4546. KMP_CHECK_UPDATE(team->t.t_size_changed, 0);
  4547. }
  4548. // TODO???: team->t.t_max_active_levels = new_max_active_levels;
  4549. kmp_r_sched_t new_sched = new_icvs->sched;
  4550. // set primary thread's schedule as new run-time schedule
  4551. KMP_CHECK_UPDATE(team->t.t_sched.sched, new_sched.sched);
  4552. __kmp_reinitialize_team(team, new_icvs,
  4553. root->r.r_uber_thread->th.th_ident);
  4554. KF_TRACE(10, ("__kmp_allocate_team2: T#%d, this_thread=%p team=%p\n", 0,
  4555. team->t.t_threads[0], team));
  4556. __kmp_push_current_task_to_thread(team->t.t_threads[0], team, 0);
  4557. #if KMP_AFFINITY_SUPPORTED
  4558. if ((team->t.t_size_changed == 0) &&
  4559. (team->t.t_proc_bind == new_proc_bind)) {
  4560. if (new_proc_bind == proc_bind_spread) {
  4561. if (do_place_partition) {
  4562. // add flag to update only master for spread
  4563. __kmp_partition_places(team, 1);
  4564. }
  4565. }
  4566. KA_TRACE(200, ("__kmp_allocate_team: reusing hot team #%d bindings: "
  4567. "proc_bind = %d, partition = [%d,%d]\n",
  4568. team->t.t_id, new_proc_bind, team->t.t_first_place,
  4569. team->t.t_last_place));
  4570. } else {
  4571. if (do_place_partition) {
  4572. KMP_CHECK_UPDATE(team->t.t_proc_bind, new_proc_bind);
  4573. __kmp_partition_places(team);
  4574. }
  4575. }
  4576. #else
  4577. KMP_CHECK_UPDATE(team->t.t_proc_bind, new_proc_bind);
  4578. #endif /* KMP_AFFINITY_SUPPORTED */
  4579. } else if (team->t.t_nproc > new_nproc) {
  4580. KA_TRACE(20,
  4581. ("__kmp_allocate_team: decreasing hot team thread count to %d\n",
  4582. new_nproc));
  4583. team->t.t_size_changed = 1;
  4584. if (__kmp_barrier_release_pattern[bs_forkjoin_barrier] == bp_dist_bar) {
  4585. // Barrier size already reduced earlier in this function
  4586. // Activate team threads via th_used_in_team
  4587. __kmp_add_threads_to_team(team, new_nproc);
  4588. }
  4589. #if KMP_NESTED_HOT_TEAMS
  4590. if (__kmp_hot_teams_mode == 0) {
  4591. // AC: saved number of threads should correspond to team's value in this
  4592. // mode, can be bigger in mode 1, when hot team has threads in reserve
  4593. KMP_DEBUG_ASSERT(hot_teams[level].hot_team_nth == team->t.t_nproc);
  4594. hot_teams[level].hot_team_nth = new_nproc;
  4595. #endif // KMP_NESTED_HOT_TEAMS
  4596. /* release the extra threads we don't need any more */
  4597. for (f = new_nproc; f < team->t.t_nproc; f++) {
  4598. KMP_DEBUG_ASSERT(team->t.t_threads[f]);
  4599. if (__kmp_tasking_mode != tskm_immediate_exec) {
  4600. // When decreasing team size, threads no longer in the team should
  4601. // unref task team.
  4602. team->t.t_threads[f]->th.th_task_team = NULL;
  4603. }
  4604. __kmp_free_thread(team->t.t_threads[f]);
  4605. team->t.t_threads[f] = NULL;
  4606. }
  4607. #if KMP_NESTED_HOT_TEAMS
  4608. } // (__kmp_hot_teams_mode == 0)
  4609. else {
  4610. // When keeping extra threads in team, switch threads to wait on own
  4611. // b_go flag
  4612. for (f = new_nproc; f < team->t.t_nproc; ++f) {
  4613. KMP_DEBUG_ASSERT(team->t.t_threads[f]);
  4614. kmp_balign_t *balign = team->t.t_threads[f]->th.th_bar;
  4615. for (int b = 0; b < bs_last_barrier; ++b) {
  4616. if (balign[b].bb.wait_flag == KMP_BARRIER_PARENT_FLAG) {
  4617. balign[b].bb.wait_flag = KMP_BARRIER_SWITCH_TO_OWN_FLAG;
  4618. }
  4619. KMP_CHECK_UPDATE(balign[b].bb.leaf_kids, 0);
  4620. }
  4621. }
  4622. }
  4623. #endif // KMP_NESTED_HOT_TEAMS
  4624. team->t.t_nproc = new_nproc;
  4625. // TODO???: team->t.t_max_active_levels = new_max_active_levels;
  4626. KMP_CHECK_UPDATE(team->t.t_sched.sched, new_icvs->sched.sched);
  4627. __kmp_reinitialize_team(team, new_icvs,
  4628. root->r.r_uber_thread->th.th_ident);
  4629. // Update remaining threads
  4630. for (f = 0; f < new_nproc; ++f) {
  4631. team->t.t_threads[f]->th.th_team_nproc = new_nproc;
  4632. }
  4633. // restore the current task state of the primary thread: should be the
  4634. // implicit task
  4635. KF_TRACE(10, ("__kmp_allocate_team: T#%d, this_thread=%p team=%p\n", 0,
  4636. team->t.t_threads[0], team));
  4637. __kmp_push_current_task_to_thread(team->t.t_threads[0], team, 0);
  4638. #ifdef KMP_DEBUG
  4639. for (f = 0; f < team->t.t_nproc; f++) {
  4640. KMP_DEBUG_ASSERT(team->t.t_threads[f] &&
  4641. team->t.t_threads[f]->th.th_team_nproc ==
  4642. team->t.t_nproc);
  4643. }
  4644. #endif
  4645. if (do_place_partition) {
  4646. KMP_CHECK_UPDATE(team->t.t_proc_bind, new_proc_bind);
  4647. #if KMP_AFFINITY_SUPPORTED
  4648. __kmp_partition_places(team);
  4649. #endif
  4650. }
  4651. } else { // team->t.t_nproc < new_nproc
  4652. #if (KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED
  4653. kmp_affin_mask_t *old_mask;
  4654. if (KMP_AFFINITY_CAPABLE()) {
  4655. KMP_CPU_ALLOC(old_mask);
  4656. }
  4657. #endif
  4658. KA_TRACE(20,
  4659. ("__kmp_allocate_team: increasing hot team thread count to %d\n",
  4660. new_nproc));
  4661. int old_nproc = team->t.t_nproc; // save old value and use to update only
  4662. team->t.t_size_changed = 1;
  4663. #if KMP_NESTED_HOT_TEAMS
  4664. int avail_threads = hot_teams[level].hot_team_nth;
  4665. if (new_nproc < avail_threads)
  4666. avail_threads = new_nproc;
  4667. kmp_info_t **other_threads = team->t.t_threads;
  4668. for (f = team->t.t_nproc; f < avail_threads; ++f) {
  4669. // Adjust barrier data of reserved threads (if any) of the team
  4670. // Other data will be set in __kmp_initialize_info() below.
  4671. int b;
  4672. kmp_balign_t *balign = other_threads[f]->th.th_bar;
  4673. for (b = 0; b < bs_last_barrier; ++b) {
  4674. balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived;
  4675. KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG);
  4676. #if USE_DEBUGGER
  4677. balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived;
  4678. #endif
  4679. }
  4680. }
  4681. if (hot_teams[level].hot_team_nth >= new_nproc) {
  4682. // we have all needed threads in reserve, no need to allocate any
  4683. // this only possible in mode 1, cannot have reserved threads in mode 0
  4684. KMP_DEBUG_ASSERT(__kmp_hot_teams_mode == 1);
  4685. team->t.t_nproc = new_nproc; // just get reserved threads involved
  4686. } else {
  4687. // We may have some threads in reserve, but not enough;
  4688. // get reserved threads involved if any.
  4689. team->t.t_nproc = hot_teams[level].hot_team_nth;
  4690. hot_teams[level].hot_team_nth = new_nproc; // adjust hot team max size
  4691. #endif // KMP_NESTED_HOT_TEAMS
  4692. if (team->t.t_max_nproc < new_nproc) {
  4693. /* reallocate larger arrays */
  4694. __kmp_reallocate_team_arrays(team, new_nproc);
  4695. __kmp_reinitialize_team(team, new_icvs, NULL);
  4696. }
  4697. #if (KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED
  4698. /* Temporarily set full mask for primary thread before creation of
  4699. workers. The reason is that workers inherit the affinity from the
  4700. primary thread, so if a lot of workers are created on the single
  4701. core quickly, they don't get a chance to set their own affinity for
  4702. a long time. */
  4703. __kmp_set_thread_affinity_mask_full_tmp(old_mask);
  4704. #endif
  4705. /* allocate new threads for the hot team */
  4706. for (f = team->t.t_nproc; f < new_nproc; f++) {
  4707. kmp_info_t *new_worker = __kmp_allocate_thread(root, team, f);
  4708. KMP_DEBUG_ASSERT(new_worker);
  4709. team->t.t_threads[f] = new_worker;
  4710. KA_TRACE(20,
  4711. ("__kmp_allocate_team: team %d init T#%d arrived: "
  4712. "join=%llu, plain=%llu\n",
  4713. team->t.t_id, __kmp_gtid_from_tid(f, team), team->t.t_id, f,
  4714. team->t.t_bar[bs_forkjoin_barrier].b_arrived,
  4715. team->t.t_bar[bs_plain_barrier].b_arrived));
  4716. { // Initialize barrier data for new threads.
  4717. int b;
  4718. kmp_balign_t *balign = new_worker->th.th_bar;
  4719. for (b = 0; b < bs_last_barrier; ++b) {
  4720. balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived;
  4721. KMP_DEBUG_ASSERT(balign[b].bb.wait_flag !=
  4722. KMP_BARRIER_PARENT_FLAG);
  4723. #if USE_DEBUGGER
  4724. balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived;
  4725. #endif
  4726. }
  4727. }
  4728. }
  4729. #if (KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED
  4730. if (KMP_AFFINITY_CAPABLE()) {
  4731. /* Restore initial primary thread's affinity mask */
  4732. __kmp_set_system_affinity(old_mask, TRUE);
  4733. KMP_CPU_FREE(old_mask);
  4734. }
  4735. #endif
  4736. #if KMP_NESTED_HOT_TEAMS
  4737. } // end of check of t_nproc vs. new_nproc vs. hot_team_nth
  4738. #endif // KMP_NESTED_HOT_TEAMS
  4739. if (__kmp_barrier_release_pattern[bs_forkjoin_barrier] == bp_dist_bar) {
  4740. // Barrier size already increased earlier in this function
  4741. // Activate team threads via th_used_in_team
  4742. __kmp_add_threads_to_team(team, new_nproc);
  4743. }
  4744. /* make sure everyone is syncronized */
  4745. // new threads below
  4746. __kmp_initialize_team(team, new_nproc, new_icvs,
  4747. root->r.r_uber_thread->th.th_ident);
  4748. /* reinitialize the threads */
  4749. KMP_DEBUG_ASSERT(team->t.t_nproc == new_nproc);
  4750. for (f = 0; f < team->t.t_nproc; ++f)
  4751. __kmp_initialize_info(team->t.t_threads[f], team, f,
  4752. __kmp_gtid_from_tid(f, team));
  4753. if (level) { // set th_task_state for new threads in nested hot team
  4754. // __kmp_initialize_info() no longer zeroes th_task_state, so we should
  4755. // only need to set the th_task_state for the new threads. th_task_state
  4756. // for primary thread will not be accurate until after this in
  4757. // __kmp_fork_call(), so we look to the primary thread's memo_stack to
  4758. // get the correct value.
  4759. for (f = old_nproc; f < team->t.t_nproc; ++f)
  4760. team->t.t_threads[f]->th.th_task_state =
  4761. team->t.t_threads[0]->th.th_task_state_memo_stack[level];
  4762. } else { // set th_task_state for new threads in non-nested hot team
  4763. // copy primary thread's state
  4764. kmp_uint8 old_state = team->t.t_threads[0]->th.th_task_state;
  4765. for (f = old_nproc; f < team->t.t_nproc; ++f)
  4766. team->t.t_threads[f]->th.th_task_state = old_state;
  4767. }
  4768. #ifdef KMP_DEBUG
  4769. for (f = 0; f < team->t.t_nproc; ++f) {
  4770. KMP_DEBUG_ASSERT(team->t.t_threads[f] &&
  4771. team->t.t_threads[f]->th.th_team_nproc ==
  4772. team->t.t_nproc);
  4773. }
  4774. #endif
  4775. if (do_place_partition) {
  4776. KMP_CHECK_UPDATE(team->t.t_proc_bind, new_proc_bind);
  4777. #if KMP_AFFINITY_SUPPORTED
  4778. __kmp_partition_places(team);
  4779. #endif
  4780. }
  4781. } // Check changes in number of threads
  4782. kmp_info_t *master = team->t.t_threads[0];
  4783. if (master->th.th_teams_microtask) {
  4784. for (f = 1; f < new_nproc; ++f) {
  4785. // propagate teams construct specific info to workers
  4786. kmp_info_t *thr = team->t.t_threads[f];
  4787. thr->th.th_teams_microtask = master->th.th_teams_microtask;
  4788. thr->th.th_teams_level = master->th.th_teams_level;
  4789. thr->th.th_teams_size = master->th.th_teams_size;
  4790. }
  4791. }
  4792. #if KMP_NESTED_HOT_TEAMS
  4793. if (level) {
  4794. // Sync barrier state for nested hot teams, not needed for outermost hot
  4795. // team.
  4796. for (f = 1; f < new_nproc; ++f) {
  4797. kmp_info_t *thr = team->t.t_threads[f];
  4798. int b;
  4799. kmp_balign_t *balign = thr->th.th_bar;
  4800. for (b = 0; b < bs_last_barrier; ++b) {
  4801. balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived;
  4802. KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG);
  4803. #if USE_DEBUGGER
  4804. balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived;
  4805. #endif
  4806. }
  4807. }
  4808. }
  4809. #endif // KMP_NESTED_HOT_TEAMS
  4810. /* reallocate space for arguments if necessary */
  4811. __kmp_alloc_argv_entries(argc, team, TRUE);
  4812. KMP_CHECK_UPDATE(team->t.t_argc, argc);
  4813. // The hot team re-uses the previous task team,
  4814. // if untouched during the previous release->gather phase.
  4815. KF_TRACE(10, (" hot_team = %p\n", team));
  4816. #if KMP_DEBUG
  4817. if (__kmp_tasking_mode != tskm_immediate_exec) {
  4818. KA_TRACE(20, ("__kmp_allocate_team: hot team task_team[0] = %p "
  4819. "task_team[1] = %p after reinit\n",
  4820. team->t.t_task_team[0], team->t.t_task_team[1]));
  4821. }
  4822. #endif
  4823. #if OMPT_SUPPORT
  4824. __ompt_team_assign_id(team, ompt_parallel_data);
  4825. #endif
  4826. KMP_MB();
  4827. return team;
  4828. }
  4829. /* next, let's try to take one from the team pool */
  4830. KMP_MB();
  4831. for (team = CCAST(kmp_team_t *, __kmp_team_pool); (team);) {
  4832. /* TODO: consider resizing undersized teams instead of reaping them, now
  4833. that we have a resizing mechanism */
  4834. if (team->t.t_max_nproc >= max_nproc) {
  4835. /* take this team from the team pool */
  4836. __kmp_team_pool = team->t.t_next_pool;
  4837. if (max_nproc > 1 &&
  4838. __kmp_barrier_gather_pattern[bs_forkjoin_barrier] == bp_dist_bar) {
  4839. if (!team->t.b) { // Allocate barrier structure
  4840. team->t.b = distributedBarrier::allocate(__kmp_dflt_team_nth_ub);
  4841. }
  4842. }
  4843. /* setup the team for fresh use */
  4844. __kmp_initialize_team(team, new_nproc, new_icvs, NULL);
  4845. KA_TRACE(20, ("__kmp_allocate_team: setting task_team[0] %p and "
  4846. "task_team[1] %p to NULL\n",
  4847. &team->t.t_task_team[0], &team->t.t_task_team[1]));
  4848. team->t.t_task_team[0] = NULL;
  4849. team->t.t_task_team[1] = NULL;
  4850. /* reallocate space for arguments if necessary */
  4851. __kmp_alloc_argv_entries(argc, team, TRUE);
  4852. KMP_CHECK_UPDATE(team->t.t_argc, argc);
  4853. KA_TRACE(
  4854. 20, ("__kmp_allocate_team: team %d init arrived: join=%u, plain=%u\n",
  4855. team->t.t_id, KMP_INIT_BARRIER_STATE, KMP_INIT_BARRIER_STATE));
  4856. { // Initialize barrier data.
  4857. int b;
  4858. for (b = 0; b < bs_last_barrier; ++b) {
  4859. team->t.t_bar[b].b_arrived = KMP_INIT_BARRIER_STATE;
  4860. #if USE_DEBUGGER
  4861. team->t.t_bar[b].b_master_arrived = 0;
  4862. team->t.t_bar[b].b_team_arrived = 0;
  4863. #endif
  4864. }
  4865. }
  4866. team->t.t_proc_bind = new_proc_bind;
  4867. KA_TRACE(20, ("__kmp_allocate_team: using team from pool %d.\n",
  4868. team->t.t_id));
  4869. #if OMPT_SUPPORT
  4870. __ompt_team_assign_id(team, ompt_parallel_data);
  4871. #endif
  4872. KMP_MB();
  4873. return team;
  4874. }
  4875. /* reap team if it is too small, then loop back and check the next one */
  4876. // not sure if this is wise, but, will be redone during the hot-teams
  4877. // rewrite.
  4878. /* TODO: Use technique to find the right size hot-team, don't reap them */
  4879. team = __kmp_reap_team(team);
  4880. __kmp_team_pool = team;
  4881. }
  4882. /* nothing available in the pool, no matter, make a new team! */
  4883. KMP_MB();
  4884. team = (kmp_team_t *)__kmp_allocate(sizeof(kmp_team_t));
  4885. /* and set it up */
  4886. team->t.t_max_nproc = max_nproc;
  4887. if (max_nproc > 1 &&
  4888. __kmp_barrier_gather_pattern[bs_forkjoin_barrier] == bp_dist_bar) {
  4889. // Allocate barrier structure
  4890. team->t.b = distributedBarrier::allocate(__kmp_dflt_team_nth_ub);
  4891. }
  4892. /* NOTE well, for some reason allocating one big buffer and dividing it up
  4893. seems to really hurt performance a lot on the P4, so, let's not use this */
  4894. __kmp_allocate_team_arrays(team, max_nproc);
  4895. KA_TRACE(20, ("__kmp_allocate_team: making a new team\n"));
  4896. __kmp_initialize_team(team, new_nproc, new_icvs, NULL);
  4897. KA_TRACE(20, ("__kmp_allocate_team: setting task_team[0] %p and task_team[1] "
  4898. "%p to NULL\n",
  4899. &team->t.t_task_team[0], &team->t.t_task_team[1]));
  4900. team->t.t_task_team[0] = NULL; // to be removed, as __kmp_allocate zeroes
  4901. // memory, no need to duplicate
  4902. team->t.t_task_team[1] = NULL; // to be removed, as __kmp_allocate zeroes
  4903. // memory, no need to duplicate
  4904. if (__kmp_storage_map) {
  4905. __kmp_print_team_storage_map("team", team, team->t.t_id, new_nproc);
  4906. }
  4907. /* allocate space for arguments */
  4908. __kmp_alloc_argv_entries(argc, team, FALSE);
  4909. team->t.t_argc = argc;
  4910. KA_TRACE(20,
  4911. ("__kmp_allocate_team: team %d init arrived: join=%u, plain=%u\n",
  4912. team->t.t_id, KMP_INIT_BARRIER_STATE, KMP_INIT_BARRIER_STATE));
  4913. { // Initialize barrier data.
  4914. int b;
  4915. for (b = 0; b < bs_last_barrier; ++b) {
  4916. team->t.t_bar[b].b_arrived = KMP_INIT_BARRIER_STATE;
  4917. #if USE_DEBUGGER
  4918. team->t.t_bar[b].b_master_arrived = 0;
  4919. team->t.t_bar[b].b_team_arrived = 0;
  4920. #endif
  4921. }
  4922. }
  4923. team->t.t_proc_bind = new_proc_bind;
  4924. #if OMPT_SUPPORT
  4925. __ompt_team_assign_id(team, ompt_parallel_data);
  4926. team->t.ompt_serialized_team_info = NULL;
  4927. #endif
  4928. KMP_MB();
  4929. KA_TRACE(20, ("__kmp_allocate_team: done creating a new team %d.\n",
  4930. team->t.t_id));
  4931. return team;
  4932. }
  4933. /* TODO implement hot-teams at all levels */
  4934. /* TODO implement lazy thread release on demand (disband request) */
  4935. /* free the team. return it to the team pool. release all the threads
  4936. * associated with it */
  4937. void __kmp_free_team(kmp_root_t *root,
  4938. kmp_team_t *team USE_NESTED_HOT_ARG(kmp_info_t *master)) {
  4939. int f;
  4940. KA_TRACE(20, ("__kmp_free_team: T#%d freeing team %d\n", __kmp_get_gtid(),
  4941. team->t.t_id));
  4942. /* verify state */
  4943. KMP_DEBUG_ASSERT(root);
  4944. KMP_DEBUG_ASSERT(team);
  4945. KMP_DEBUG_ASSERT(team->t.t_nproc <= team->t.t_max_nproc);
  4946. KMP_DEBUG_ASSERT(team->t.t_threads);
  4947. int use_hot_team = team == root->r.r_hot_team;
  4948. #if KMP_NESTED_HOT_TEAMS
  4949. int level;
  4950. if (master) {
  4951. level = team->t.t_active_level - 1;
  4952. if (master->th.th_teams_microtask) { // in teams construct?
  4953. if (master->th.th_teams_size.nteams > 1) {
  4954. ++level; // level was not increased in teams construct for
  4955. // team_of_masters
  4956. }
  4957. if (team->t.t_pkfn != (microtask_t)__kmp_teams_master &&
  4958. master->th.th_teams_level == team->t.t_level) {
  4959. ++level; // level was not increased in teams construct for
  4960. // team_of_workers before the parallel
  4961. } // team->t.t_level will be increased inside parallel
  4962. }
  4963. #if KMP_DEBUG
  4964. kmp_hot_team_ptr_t *hot_teams = master->th.th_hot_teams;
  4965. #endif
  4966. if (level < __kmp_hot_teams_max_level) {
  4967. KMP_DEBUG_ASSERT(team == hot_teams[level].hot_team);
  4968. use_hot_team = 1;
  4969. }
  4970. }
  4971. #endif // KMP_NESTED_HOT_TEAMS
  4972. /* team is done working */
  4973. TCW_SYNC_PTR(team->t.t_pkfn,
  4974. NULL); // Important for Debugging Support Library.
  4975. #if KMP_OS_WINDOWS
  4976. team->t.t_copyin_counter = 0; // init counter for possible reuse
  4977. #endif
  4978. // Do not reset pointer to parent team to NULL for hot teams.
  4979. /* if we are non-hot team, release our threads */
  4980. if (!use_hot_team) {
  4981. if (__kmp_tasking_mode != tskm_immediate_exec) {
  4982. // Wait for threads to reach reapable state
  4983. for (f = 1; f < team->t.t_nproc; ++f) {
  4984. KMP_DEBUG_ASSERT(team->t.t_threads[f]);
  4985. kmp_info_t *th = team->t.t_threads[f];
  4986. volatile kmp_uint32 *state = &th->th.th_reap_state;
  4987. while (*state != KMP_SAFE_TO_REAP) {
  4988. #if KMP_OS_WINDOWS
  4989. // On Windows a thread can be killed at any time, check this
  4990. DWORD ecode;
  4991. if (!__kmp_is_thread_alive(th, &ecode)) {
  4992. *state = KMP_SAFE_TO_REAP; // reset the flag for dead thread
  4993. break;
  4994. }
  4995. #endif
  4996. // first check if thread is sleeping
  4997. kmp_flag_64<> fl(&th->th.th_bar[bs_forkjoin_barrier].bb.b_go, th);
  4998. if (fl.is_sleeping())
  4999. fl.resume(__kmp_gtid_from_thread(th));
  5000. KMP_CPU_PAUSE();
  5001. }
  5002. }
  5003. // Delete task teams
  5004. int tt_idx;
  5005. for (tt_idx = 0; tt_idx < 2; ++tt_idx) {
  5006. kmp_task_team_t *task_team = team->t.t_task_team[tt_idx];
  5007. if (task_team != NULL) {
  5008. for (f = 0; f < team->t.t_nproc; ++f) { // threads unref task teams
  5009. KMP_DEBUG_ASSERT(team->t.t_threads[f]);
  5010. team->t.t_threads[f]->th.th_task_team = NULL;
  5011. }
  5012. KA_TRACE(
  5013. 20,
  5014. ("__kmp_free_team: T#%d deactivating task_team %p on team %d\n",
  5015. __kmp_get_gtid(), task_team, team->t.t_id));
  5016. #if KMP_NESTED_HOT_TEAMS
  5017. __kmp_free_task_team(master, task_team);
  5018. #endif
  5019. team->t.t_task_team[tt_idx] = NULL;
  5020. }
  5021. }
  5022. }
  5023. // Reset pointer to parent team only for non-hot teams.
  5024. team->t.t_parent = NULL;
  5025. team->t.t_level = 0;
  5026. team->t.t_active_level = 0;
  5027. /* free the worker threads */
  5028. for (f = 1; f < team->t.t_nproc; ++f) {
  5029. KMP_DEBUG_ASSERT(team->t.t_threads[f]);
  5030. if (__kmp_barrier_gather_pattern[bs_forkjoin_barrier] == bp_dist_bar) {
  5031. KMP_COMPARE_AND_STORE_ACQ32(&(team->t.t_threads[f]->th.th_used_in_team),
  5032. 1, 2);
  5033. }
  5034. __kmp_free_thread(team->t.t_threads[f]);
  5035. }
  5036. if (__kmp_barrier_gather_pattern[bs_forkjoin_barrier] == bp_dist_bar) {
  5037. if (team->t.b) {
  5038. // wake up thread at old location
  5039. team->t.b->go_release();
  5040. if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
  5041. for (f = 1; f < team->t.t_nproc; ++f) {
  5042. if (team->t.b->sleep[f].sleep) {
  5043. __kmp_atomic_resume_64(
  5044. team->t.t_threads[f]->th.th_info.ds.ds_gtid,
  5045. (kmp_atomic_flag_64<> *)NULL);
  5046. }
  5047. }
  5048. }
  5049. // Wait for threads to be removed from team
  5050. for (int f = 1; f < team->t.t_nproc; ++f) {
  5051. while (team->t.t_threads[f]->th.th_used_in_team.load() != 0)
  5052. KMP_CPU_PAUSE();
  5053. }
  5054. }
  5055. }
  5056. for (f = 1; f < team->t.t_nproc; ++f) {
  5057. team->t.t_threads[f] = NULL;
  5058. }
  5059. if (team->t.t_max_nproc > 1 &&
  5060. __kmp_barrier_gather_pattern[bs_forkjoin_barrier] == bp_dist_bar) {
  5061. distributedBarrier::deallocate(team->t.b);
  5062. team->t.b = NULL;
  5063. }
  5064. /* put the team back in the team pool */
  5065. /* TODO limit size of team pool, call reap_team if pool too large */
  5066. team->t.t_next_pool = CCAST(kmp_team_t *, __kmp_team_pool);
  5067. __kmp_team_pool = (volatile kmp_team_t *)team;
  5068. } else { // Check if team was created for primary threads in teams construct
  5069. // See if first worker is a CG root
  5070. KMP_DEBUG_ASSERT(team->t.t_threads[1] &&
  5071. team->t.t_threads[1]->th.th_cg_roots);
  5072. if (team->t.t_threads[1]->th.th_cg_roots->cg_root == team->t.t_threads[1]) {
  5073. // Clean up the CG root nodes on workers so that this team can be re-used
  5074. for (f = 1; f < team->t.t_nproc; ++f) {
  5075. kmp_info_t *thr = team->t.t_threads[f];
  5076. KMP_DEBUG_ASSERT(thr && thr->th.th_cg_roots &&
  5077. thr->th.th_cg_roots->cg_root == thr);
  5078. // Pop current CG root off list
  5079. kmp_cg_root_t *tmp = thr->th.th_cg_roots;
  5080. thr->th.th_cg_roots = tmp->up;
  5081. KA_TRACE(100, ("__kmp_free_team: Thread %p popping node %p and moving"
  5082. " up to node %p. cg_nthreads was %d\n",
  5083. thr, tmp, thr->th.th_cg_roots, tmp->cg_nthreads));
  5084. int i = tmp->cg_nthreads--;
  5085. if (i == 1) {
  5086. __kmp_free(tmp); // free CG if we are the last thread in it
  5087. }
  5088. // Restore current task's thread_limit from CG root
  5089. if (thr->th.th_cg_roots)
  5090. thr->th.th_current_task->td_icvs.thread_limit =
  5091. thr->th.th_cg_roots->cg_thread_limit;
  5092. }
  5093. }
  5094. }
  5095. KMP_MB();
  5096. }
  5097. /* reap the team. destroy it, reclaim all its resources and free its memory */
  5098. kmp_team_t *__kmp_reap_team(kmp_team_t *team) {
  5099. kmp_team_t *next_pool = team->t.t_next_pool;
  5100. KMP_DEBUG_ASSERT(team);
  5101. KMP_DEBUG_ASSERT(team->t.t_dispatch);
  5102. KMP_DEBUG_ASSERT(team->t.t_disp_buffer);
  5103. KMP_DEBUG_ASSERT(team->t.t_threads);
  5104. KMP_DEBUG_ASSERT(team->t.t_argv);
  5105. /* TODO clean the threads that are a part of this? */
  5106. /* free stuff */
  5107. __kmp_free_team_arrays(team);
  5108. if (team->t.t_argv != &team->t.t_inline_argv[0])
  5109. __kmp_free((void *)team->t.t_argv);
  5110. __kmp_free(team);
  5111. KMP_MB();
  5112. return next_pool;
  5113. }
  5114. // Free the thread. Don't reap it, just place it on the pool of available
  5115. // threads.
  5116. //
  5117. // Changes for Quad issue 527845: We need a predictable OMP tid <-> gtid
  5118. // binding for the affinity mechanism to be useful.
  5119. //
  5120. // Now, we always keep the free list (__kmp_thread_pool) sorted by gtid.
  5121. // However, we want to avoid a potential performance problem by always
  5122. // scanning through the list to find the correct point at which to insert
  5123. // the thread (potential N**2 behavior). To do this we keep track of the
  5124. // last place a thread struct was inserted (__kmp_thread_pool_insert_pt).
  5125. // With single-level parallelism, threads will always be added to the tail
  5126. // of the list, kept track of by __kmp_thread_pool_insert_pt. With nested
  5127. // parallelism, all bets are off and we may need to scan through the entire
  5128. // free list.
  5129. //
  5130. // This change also has a potentially large performance benefit, for some
  5131. // applications. Previously, as threads were freed from the hot team, they
  5132. // would be placed back on the free list in inverse order. If the hot team
  5133. // grew back to it's original size, then the freed thread would be placed
  5134. // back on the hot team in reverse order. This could cause bad cache
  5135. // locality problems on programs where the size of the hot team regularly
  5136. // grew and shrunk.
  5137. //
  5138. // Now, for single-level parallelism, the OMP tid is always == gtid.
  5139. void __kmp_free_thread(kmp_info_t *this_th) {
  5140. int gtid;
  5141. kmp_info_t **scan;
  5142. KA_TRACE(20, ("__kmp_free_thread: T#%d putting T#%d back on free pool.\n",
  5143. __kmp_get_gtid(), this_th->th.th_info.ds.ds_gtid));
  5144. KMP_DEBUG_ASSERT(this_th);
  5145. // When moving thread to pool, switch thread to wait on own b_go flag, and
  5146. // uninitialized (NULL team).
  5147. int b;
  5148. kmp_balign_t *balign = this_th->th.th_bar;
  5149. for (b = 0; b < bs_last_barrier; ++b) {
  5150. if (balign[b].bb.wait_flag == KMP_BARRIER_PARENT_FLAG)
  5151. balign[b].bb.wait_flag = KMP_BARRIER_SWITCH_TO_OWN_FLAG;
  5152. balign[b].bb.team = NULL;
  5153. balign[b].bb.leaf_kids = 0;
  5154. }
  5155. this_th->th.th_task_state = 0;
  5156. this_th->th.th_reap_state = KMP_SAFE_TO_REAP;
  5157. /* put thread back on the free pool */
  5158. TCW_PTR(this_th->th.th_team, NULL);
  5159. TCW_PTR(this_th->th.th_root, NULL);
  5160. TCW_PTR(this_th->th.th_dispatch, NULL); /* NOT NEEDED */
  5161. while (this_th->th.th_cg_roots) {
  5162. this_th->th.th_cg_roots->cg_nthreads--;
  5163. KA_TRACE(100, ("__kmp_free_thread: Thread %p decrement cg_nthreads on node"
  5164. " %p of thread %p to %d\n",
  5165. this_th, this_th->th.th_cg_roots,
  5166. this_th->th.th_cg_roots->cg_root,
  5167. this_th->th.th_cg_roots->cg_nthreads));
  5168. kmp_cg_root_t *tmp = this_th->th.th_cg_roots;
  5169. if (tmp->cg_root == this_th) { // Thread is a cg_root
  5170. KMP_DEBUG_ASSERT(tmp->cg_nthreads == 0);
  5171. KA_TRACE(
  5172. 5, ("__kmp_free_thread: Thread %p freeing node %p\n", this_th, tmp));
  5173. this_th->th.th_cg_roots = tmp->up;
  5174. __kmp_free(tmp);
  5175. } else { // Worker thread
  5176. if (tmp->cg_nthreads == 0) { // last thread leaves contention group
  5177. __kmp_free(tmp);
  5178. }
  5179. this_th->th.th_cg_roots = NULL;
  5180. break;
  5181. }
  5182. }
  5183. /* If the implicit task assigned to this thread can be used by other threads
  5184. * -> multiple threads can share the data and try to free the task at
  5185. * __kmp_reap_thread at exit. This duplicate use of the task data can happen
  5186. * with higher probability when hot team is disabled but can occurs even when
  5187. * the hot team is enabled */
  5188. __kmp_free_implicit_task(this_th);
  5189. this_th->th.th_current_task = NULL;
  5190. // If the __kmp_thread_pool_insert_pt is already past the new insert
  5191. // point, then we need to re-scan the entire list.
  5192. gtid = this_th->th.th_info.ds.ds_gtid;
  5193. if (__kmp_thread_pool_insert_pt != NULL) {
  5194. KMP_DEBUG_ASSERT(__kmp_thread_pool != NULL);
  5195. if (__kmp_thread_pool_insert_pt->th.th_info.ds.ds_gtid > gtid) {
  5196. __kmp_thread_pool_insert_pt = NULL;
  5197. }
  5198. }
  5199. // Scan down the list to find the place to insert the thread.
  5200. // scan is the address of a link in the list, possibly the address of
  5201. // __kmp_thread_pool itself.
  5202. //
  5203. // In the absence of nested parallelism, the for loop will have 0 iterations.
  5204. if (__kmp_thread_pool_insert_pt != NULL) {
  5205. scan = &(__kmp_thread_pool_insert_pt->th.th_next_pool);
  5206. } else {
  5207. scan = CCAST(kmp_info_t **, &__kmp_thread_pool);
  5208. }
  5209. for (; (*scan != NULL) && ((*scan)->th.th_info.ds.ds_gtid < gtid);
  5210. scan = &((*scan)->th.th_next_pool))
  5211. ;
  5212. // Insert the new element on the list, and set __kmp_thread_pool_insert_pt
  5213. // to its address.
  5214. TCW_PTR(this_th->th.th_next_pool, *scan);
  5215. __kmp_thread_pool_insert_pt = *scan = this_th;
  5216. KMP_DEBUG_ASSERT((this_th->th.th_next_pool == NULL) ||
  5217. (this_th->th.th_info.ds.ds_gtid <
  5218. this_th->th.th_next_pool->th.th_info.ds.ds_gtid));
  5219. TCW_4(this_th->th.th_in_pool, TRUE);
  5220. __kmp_suspend_initialize_thread(this_th);
  5221. __kmp_lock_suspend_mx(this_th);
  5222. if (this_th->th.th_active == TRUE) {
  5223. KMP_ATOMIC_INC(&__kmp_thread_pool_active_nth);
  5224. this_th->th.th_active_in_pool = TRUE;
  5225. }
  5226. #if KMP_DEBUG
  5227. else {
  5228. KMP_DEBUG_ASSERT(this_th->th.th_active_in_pool == FALSE);
  5229. }
  5230. #endif
  5231. __kmp_unlock_suspend_mx(this_th);
  5232. TCW_4(__kmp_nth, __kmp_nth - 1);
  5233. #ifdef KMP_ADJUST_BLOCKTIME
  5234. /* Adjust blocktime back to user setting or default if necessary */
  5235. /* Middle initialization might never have occurred */
  5236. if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
  5237. KMP_DEBUG_ASSERT(__kmp_avail_proc > 0);
  5238. if (__kmp_nth <= __kmp_avail_proc) {
  5239. __kmp_zero_bt = FALSE;
  5240. }
  5241. }
  5242. #endif /* KMP_ADJUST_BLOCKTIME */
  5243. KMP_MB();
  5244. }
  5245. /* ------------------------------------------------------------------------ */
  5246. void *__kmp_launch_thread(kmp_info_t *this_thr) {
  5247. #if OMP_PROFILING_SUPPORT
  5248. ProfileTraceFile = getenv("LIBOMPTARGET_PROFILE");
  5249. // TODO: add a configuration option for time granularity
  5250. if (ProfileTraceFile)
  5251. llvm::timeTraceProfilerInitialize(500 /* us */, "libomptarget");
  5252. #endif
  5253. int gtid = this_thr->th.th_info.ds.ds_gtid;
  5254. /* void *stack_data;*/
  5255. kmp_team_t **volatile pteam;
  5256. KMP_MB();
  5257. KA_TRACE(10, ("__kmp_launch_thread: T#%d start\n", gtid));
  5258. if (__kmp_env_consistency_check) {
  5259. this_thr->th.th_cons = __kmp_allocate_cons_stack(gtid); // ATT: Memory leak?
  5260. }
  5261. #if OMPD_SUPPORT
  5262. if (ompd_state & OMPD_ENABLE_BP)
  5263. ompd_bp_thread_begin();
  5264. #endif
  5265. #if OMPT_SUPPORT
  5266. ompt_data_t *thread_data = nullptr;
  5267. if (ompt_enabled.enabled) {
  5268. thread_data = &(this_thr->th.ompt_thread_info.thread_data);
  5269. *thread_data = ompt_data_none;
  5270. this_thr->th.ompt_thread_info.state = ompt_state_overhead;
  5271. this_thr->th.ompt_thread_info.wait_id = 0;
  5272. this_thr->th.ompt_thread_info.idle_frame = OMPT_GET_FRAME_ADDRESS(0);
  5273. this_thr->th.ompt_thread_info.parallel_flags = 0;
  5274. if (ompt_enabled.ompt_callback_thread_begin) {
  5275. ompt_callbacks.ompt_callback(ompt_callback_thread_begin)(
  5276. ompt_thread_worker, thread_data);
  5277. }
  5278. this_thr->th.ompt_thread_info.state = ompt_state_idle;
  5279. }
  5280. #endif
  5281. /* This is the place where threads wait for work */
  5282. while (!TCR_4(__kmp_global.g.g_done)) {
  5283. KMP_DEBUG_ASSERT(this_thr == __kmp_threads[gtid]);
  5284. KMP_MB();
  5285. /* wait for work to do */
  5286. KA_TRACE(20, ("__kmp_launch_thread: T#%d waiting for work\n", gtid));
  5287. /* No tid yet since not part of a team */
  5288. __kmp_fork_barrier(gtid, KMP_GTID_DNE);
  5289. #if OMPT_SUPPORT
  5290. if (ompt_enabled.enabled) {
  5291. this_thr->th.ompt_thread_info.state = ompt_state_overhead;
  5292. }
  5293. #endif
  5294. pteam = &this_thr->th.th_team;
  5295. /* have we been allocated? */
  5296. if (TCR_SYNC_PTR(*pteam) && !TCR_4(__kmp_global.g.g_done)) {
  5297. /* we were just woken up, so run our new task */
  5298. if (TCR_SYNC_PTR((*pteam)->t.t_pkfn) != NULL) {
  5299. int rc;
  5300. KA_TRACE(20,
  5301. ("__kmp_launch_thread: T#%d(%d:%d) invoke microtask = %p\n",
  5302. gtid, (*pteam)->t.t_id, __kmp_tid_from_gtid(gtid),
  5303. (*pteam)->t.t_pkfn));
  5304. updateHWFPControl(*pteam);
  5305. #if OMPT_SUPPORT
  5306. if (ompt_enabled.enabled) {
  5307. this_thr->th.ompt_thread_info.state = ompt_state_work_parallel;
  5308. }
  5309. #endif
  5310. rc = (*pteam)->t.t_invoke(gtid);
  5311. KMP_ASSERT(rc);
  5312. KMP_MB();
  5313. KA_TRACE(20, ("__kmp_launch_thread: T#%d(%d:%d) done microtask = %p\n",
  5314. gtid, (*pteam)->t.t_id, __kmp_tid_from_gtid(gtid),
  5315. (*pteam)->t.t_pkfn));
  5316. }
  5317. #if OMPT_SUPPORT
  5318. if (ompt_enabled.enabled) {
  5319. /* no frame set while outside task */
  5320. __ompt_get_task_info_object(0)->frame.exit_frame = ompt_data_none;
  5321. this_thr->th.ompt_thread_info.state = ompt_state_overhead;
  5322. }
  5323. #endif
  5324. /* join barrier after parallel region */
  5325. __kmp_join_barrier(gtid);
  5326. }
  5327. }
  5328. TCR_SYNC_PTR((intptr_t)__kmp_global.g.g_done);
  5329. #if OMPD_SUPPORT
  5330. if (ompd_state & OMPD_ENABLE_BP)
  5331. ompd_bp_thread_end();
  5332. #endif
  5333. #if OMPT_SUPPORT
  5334. if (ompt_enabled.ompt_callback_thread_end) {
  5335. ompt_callbacks.ompt_callback(ompt_callback_thread_end)(thread_data);
  5336. }
  5337. #endif
  5338. this_thr->th.th_task_team = NULL;
  5339. /* run the destructors for the threadprivate data for this thread */
  5340. __kmp_common_destroy_gtid(gtid);
  5341. KA_TRACE(10, ("__kmp_launch_thread: T#%d done\n", gtid));
  5342. KMP_MB();
  5343. #if OMP_PROFILING_SUPPORT
  5344. llvm::timeTraceProfilerFinishThread();
  5345. #endif
  5346. return this_thr;
  5347. }
  5348. /* ------------------------------------------------------------------------ */
  5349. void __kmp_internal_end_dest(void *specific_gtid) {
  5350. // Make sure no significant bits are lost
  5351. int gtid;
  5352. __kmp_type_convert((kmp_intptr_t)specific_gtid - 1, &gtid);
  5353. KA_TRACE(30, ("__kmp_internal_end_dest: T#%d\n", gtid));
  5354. /* NOTE: the gtid is stored as gitd+1 in the thread-local-storage
  5355. * this is because 0 is reserved for the nothing-stored case */
  5356. __kmp_internal_end_thread(gtid);
  5357. }
  5358. #if KMP_OS_UNIX && KMP_DYNAMIC_LIB
  5359. __attribute__((destructor)) void __kmp_internal_end_dtor(void) {
  5360. __kmp_internal_end_atexit();
  5361. }
  5362. #endif
  5363. /* [Windows] josh: when the atexit handler is called, there may still be more
  5364. than one thread alive */
  5365. void __kmp_internal_end_atexit(void) {
  5366. KA_TRACE(30, ("__kmp_internal_end_atexit\n"));
  5367. /* [Windows]
  5368. josh: ideally, we want to completely shutdown the library in this atexit
  5369. handler, but stat code that depends on thread specific data for gtid fails
  5370. because that data becomes unavailable at some point during the shutdown, so
  5371. we call __kmp_internal_end_thread instead. We should eventually remove the
  5372. dependency on __kmp_get_specific_gtid in the stat code and use
  5373. __kmp_internal_end_library to cleanly shutdown the library.
  5374. // TODO: Can some of this comment about GVS be removed?
  5375. I suspect that the offending stat code is executed when the calling thread
  5376. tries to clean up a dead root thread's data structures, resulting in GVS
  5377. code trying to close the GVS structures for that thread, but since the stat
  5378. code uses __kmp_get_specific_gtid to get the gtid with the assumption that
  5379. the calling thread is cleaning up itself instead of another thread, it get
  5380. confused. This happens because allowing a thread to unregister and cleanup
  5381. another thread is a recent modification for addressing an issue.
  5382. Based on the current design (20050722), a thread may end up
  5383. trying to unregister another thread only if thread death does not trigger
  5384. the calling of __kmp_internal_end_thread. For Linux* OS, there is the
  5385. thread specific data destructor function to detect thread death. For
  5386. Windows dynamic, there is DllMain(THREAD_DETACH). For Windows static, there
  5387. is nothing. Thus, the workaround is applicable only for Windows static
  5388. stat library. */
  5389. __kmp_internal_end_library(-1);
  5390. #if KMP_OS_WINDOWS
  5391. __kmp_close_console();
  5392. #endif
  5393. }
  5394. static void __kmp_reap_thread(kmp_info_t *thread, int is_root) {
  5395. // It is assumed __kmp_forkjoin_lock is acquired.
  5396. int gtid;
  5397. KMP_DEBUG_ASSERT(thread != NULL);
  5398. gtid = thread->th.th_info.ds.ds_gtid;
  5399. if (!is_root) {
  5400. if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
  5401. /* Assume the threads are at the fork barrier here */
  5402. KA_TRACE(
  5403. 20, ("__kmp_reap_thread: releasing T#%d from fork barrier for reap\n",
  5404. gtid));
  5405. if (__kmp_barrier_gather_pattern[bs_forkjoin_barrier] == bp_dist_bar) {
  5406. while (
  5407. !KMP_COMPARE_AND_STORE_ACQ32(&(thread->th.th_used_in_team), 0, 3))
  5408. KMP_CPU_PAUSE();
  5409. __kmp_resume_32(gtid, (kmp_flag_32<false, false> *)NULL);
  5410. } else {
  5411. /* Need release fence here to prevent seg faults for tree forkjoin
  5412. barrier (GEH) */
  5413. kmp_flag_64<> flag(&thread->th.th_bar[bs_forkjoin_barrier].bb.b_go,
  5414. thread);
  5415. __kmp_release_64(&flag);
  5416. }
  5417. }
  5418. // Terminate OS thread.
  5419. __kmp_reap_worker(thread);
  5420. // The thread was killed asynchronously. If it was actively
  5421. // spinning in the thread pool, decrement the global count.
  5422. //
  5423. // There is a small timing hole here - if the worker thread was just waking
  5424. // up after sleeping in the pool, had reset it's th_active_in_pool flag but
  5425. // not decremented the global counter __kmp_thread_pool_active_nth yet, then
  5426. // the global counter might not get updated.
  5427. //
  5428. // Currently, this can only happen as the library is unloaded,
  5429. // so there are no harmful side effects.
  5430. if (thread->th.th_active_in_pool) {
  5431. thread->th.th_active_in_pool = FALSE;
  5432. KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth);
  5433. KMP_DEBUG_ASSERT(__kmp_thread_pool_active_nth >= 0);
  5434. }
  5435. }
  5436. __kmp_free_implicit_task(thread);
  5437. // Free the fast memory for tasking
  5438. #if USE_FAST_MEMORY
  5439. __kmp_free_fast_memory(thread);
  5440. #endif /* USE_FAST_MEMORY */
  5441. __kmp_suspend_uninitialize_thread(thread);
  5442. KMP_DEBUG_ASSERT(__kmp_threads[gtid] == thread);
  5443. TCW_SYNC_PTR(__kmp_threads[gtid], NULL);
  5444. --__kmp_all_nth;
  5445. // __kmp_nth was decremented when thread is added to the pool.
  5446. #ifdef KMP_ADJUST_BLOCKTIME
  5447. /* Adjust blocktime back to user setting or default if necessary */
  5448. /* Middle initialization might never have occurred */
  5449. if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
  5450. KMP_DEBUG_ASSERT(__kmp_avail_proc > 0);
  5451. if (__kmp_nth <= __kmp_avail_proc) {
  5452. __kmp_zero_bt = FALSE;
  5453. }
  5454. }
  5455. #endif /* KMP_ADJUST_BLOCKTIME */
  5456. /* free the memory being used */
  5457. if (__kmp_env_consistency_check) {
  5458. if (thread->th.th_cons) {
  5459. __kmp_free_cons_stack(thread->th.th_cons);
  5460. thread->th.th_cons = NULL;
  5461. }
  5462. }
  5463. if (thread->th.th_pri_common != NULL) {
  5464. __kmp_free(thread->th.th_pri_common);
  5465. thread->th.th_pri_common = NULL;
  5466. }
  5467. if (thread->th.th_task_state_memo_stack != NULL) {
  5468. __kmp_free(thread->th.th_task_state_memo_stack);
  5469. thread->th.th_task_state_memo_stack = NULL;
  5470. }
  5471. #if KMP_USE_BGET
  5472. if (thread->th.th_local.bget_data != NULL) {
  5473. __kmp_finalize_bget(thread);
  5474. }
  5475. #endif
  5476. #if KMP_AFFINITY_SUPPORTED
  5477. if (thread->th.th_affin_mask != NULL) {
  5478. KMP_CPU_FREE(thread->th.th_affin_mask);
  5479. thread->th.th_affin_mask = NULL;
  5480. }
  5481. #endif /* KMP_AFFINITY_SUPPORTED */
  5482. #if KMP_USE_HIER_SCHED
  5483. if (thread->th.th_hier_bar_data != NULL) {
  5484. __kmp_free(thread->th.th_hier_bar_data);
  5485. thread->th.th_hier_bar_data = NULL;
  5486. }
  5487. #endif
  5488. __kmp_reap_team(thread->th.th_serial_team);
  5489. thread->th.th_serial_team = NULL;
  5490. __kmp_free(thread);
  5491. KMP_MB();
  5492. } // __kmp_reap_thread
  5493. static void __kmp_itthash_clean(kmp_info_t *th) {
  5494. #if USE_ITT_NOTIFY
  5495. if (__kmp_itt_region_domains.count > 0) {
  5496. for (int i = 0; i < KMP_MAX_FRAME_DOMAINS; ++i) {
  5497. kmp_itthash_entry_t *bucket = __kmp_itt_region_domains.buckets[i];
  5498. while (bucket) {
  5499. kmp_itthash_entry_t *next = bucket->next_in_bucket;
  5500. __kmp_thread_free(th, bucket);
  5501. bucket = next;
  5502. }
  5503. }
  5504. }
  5505. if (__kmp_itt_barrier_domains.count > 0) {
  5506. for (int i = 0; i < KMP_MAX_FRAME_DOMAINS; ++i) {
  5507. kmp_itthash_entry_t *bucket = __kmp_itt_barrier_domains.buckets[i];
  5508. while (bucket) {
  5509. kmp_itthash_entry_t *next = bucket->next_in_bucket;
  5510. __kmp_thread_free(th, bucket);
  5511. bucket = next;
  5512. }
  5513. }
  5514. }
  5515. #endif
  5516. }
  5517. static void __kmp_internal_end(void) {
  5518. int i;
  5519. /* First, unregister the library */
  5520. __kmp_unregister_library();
  5521. #if KMP_OS_WINDOWS
  5522. /* In Win static library, we can't tell when a root actually dies, so we
  5523. reclaim the data structures for any root threads that have died but not
  5524. unregistered themselves, in order to shut down cleanly.
  5525. In Win dynamic library we also can't tell when a thread dies. */
  5526. __kmp_reclaim_dead_roots(); // AC: moved here to always clean resources of
  5527. // dead roots
  5528. #endif
  5529. for (i = 0; i < __kmp_threads_capacity; i++)
  5530. if (__kmp_root[i])
  5531. if (__kmp_root[i]->r.r_active)
  5532. break;
  5533. KMP_MB(); /* Flush all pending memory write invalidates. */
  5534. TCW_SYNC_4(__kmp_global.g.g_done, TRUE);
  5535. if (i < __kmp_threads_capacity) {
  5536. #if KMP_USE_MONITOR
  5537. // 2009-09-08 (lev): Other alive roots found. Why do we kill the monitor??
  5538. KMP_MB(); /* Flush all pending memory write invalidates. */
  5539. // Need to check that monitor was initialized before reaping it. If we are
  5540. // called form __kmp_atfork_child (which sets __kmp_init_parallel = 0), then
  5541. // __kmp_monitor will appear to contain valid data, but it is only valid in
  5542. // the parent process, not the child.
  5543. // New behavior (201008): instead of keying off of the flag
  5544. // __kmp_init_parallel, the monitor thread creation is keyed off
  5545. // of the new flag __kmp_init_monitor.
  5546. __kmp_acquire_bootstrap_lock(&__kmp_monitor_lock);
  5547. if (TCR_4(__kmp_init_monitor)) {
  5548. __kmp_reap_monitor(&__kmp_monitor);
  5549. TCW_4(__kmp_init_monitor, 0);
  5550. }
  5551. __kmp_release_bootstrap_lock(&__kmp_monitor_lock);
  5552. KA_TRACE(10, ("__kmp_internal_end: monitor reaped\n"));
  5553. #endif // KMP_USE_MONITOR
  5554. } else {
  5555. /* TODO move this to cleanup code */
  5556. #ifdef KMP_DEBUG
  5557. /* make sure that everything has properly ended */
  5558. for (i = 0; i < __kmp_threads_capacity; i++) {
  5559. if (__kmp_root[i]) {
  5560. // KMP_ASSERT( ! KMP_UBER_GTID( i ) ); // AC:
  5561. // there can be uber threads alive here
  5562. KMP_ASSERT(!__kmp_root[i]->r.r_active); // TODO: can they be active?
  5563. }
  5564. }
  5565. #endif
  5566. KMP_MB();
  5567. // Reap the worker threads.
  5568. // This is valid for now, but be careful if threads are reaped sooner.
  5569. while (__kmp_thread_pool != NULL) { // Loop thru all the thread in the pool.
  5570. // Get the next thread from the pool.
  5571. kmp_info_t *thread = CCAST(kmp_info_t *, __kmp_thread_pool);
  5572. __kmp_thread_pool = thread->th.th_next_pool;
  5573. // Reap it.
  5574. KMP_DEBUG_ASSERT(thread->th.th_reap_state == KMP_SAFE_TO_REAP);
  5575. thread->th.th_next_pool = NULL;
  5576. thread->th.th_in_pool = FALSE;
  5577. __kmp_reap_thread(thread, 0);
  5578. }
  5579. __kmp_thread_pool_insert_pt = NULL;
  5580. // Reap teams.
  5581. while (__kmp_team_pool != NULL) { // Loop thru all the teams in the pool.
  5582. // Get the next team from the pool.
  5583. kmp_team_t *team = CCAST(kmp_team_t *, __kmp_team_pool);
  5584. __kmp_team_pool = team->t.t_next_pool;
  5585. // Reap it.
  5586. team->t.t_next_pool = NULL;
  5587. __kmp_reap_team(team);
  5588. }
  5589. __kmp_reap_task_teams();
  5590. #if KMP_OS_UNIX
  5591. // Threads that are not reaped should not access any resources since they
  5592. // are going to be deallocated soon, so the shutdown sequence should wait
  5593. // until all threads either exit the final spin-waiting loop or begin
  5594. // sleeping after the given blocktime.
  5595. for (i = 0; i < __kmp_threads_capacity; i++) {
  5596. kmp_info_t *thr = __kmp_threads[i];
  5597. while (thr && KMP_ATOMIC_LD_ACQ(&thr->th.th_blocking))
  5598. KMP_CPU_PAUSE();
  5599. }
  5600. #endif
  5601. for (i = 0; i < __kmp_threads_capacity; ++i) {
  5602. // TBD: Add some checking...
  5603. // Something like KMP_DEBUG_ASSERT( __kmp_thread[ i ] == NULL );
  5604. }
  5605. /* Make sure all threadprivate destructors get run by joining with all
  5606. worker threads before resetting this flag */
  5607. TCW_SYNC_4(__kmp_init_common, FALSE);
  5608. KA_TRACE(10, ("__kmp_internal_end: all workers reaped\n"));
  5609. KMP_MB();
  5610. #if KMP_USE_MONITOR
  5611. // See note above: One of the possible fixes for CQ138434 / CQ140126
  5612. //
  5613. // FIXME: push both code fragments down and CSE them?
  5614. // push them into __kmp_cleanup() ?
  5615. __kmp_acquire_bootstrap_lock(&__kmp_monitor_lock);
  5616. if (TCR_4(__kmp_init_monitor)) {
  5617. __kmp_reap_monitor(&__kmp_monitor);
  5618. TCW_4(__kmp_init_monitor, 0);
  5619. }
  5620. __kmp_release_bootstrap_lock(&__kmp_monitor_lock);
  5621. KA_TRACE(10, ("__kmp_internal_end: monitor reaped\n"));
  5622. #endif
  5623. } /* else !__kmp_global.t_active */
  5624. TCW_4(__kmp_init_gtid, FALSE);
  5625. KMP_MB(); /* Flush all pending memory write invalidates. */
  5626. __kmp_cleanup();
  5627. #if OMPT_SUPPORT
  5628. ompt_fini();
  5629. #endif
  5630. }
  5631. void __kmp_internal_end_library(int gtid_req) {
  5632. /* if we have already cleaned up, don't try again, it wouldn't be pretty */
  5633. /* this shouldn't be a race condition because __kmp_internal_end() is the
  5634. only place to clear __kmp_serial_init */
  5635. /* we'll check this later too, after we get the lock */
  5636. // 2009-09-06: We do not set g_abort without setting g_done. This check looks
  5637. // redundant, because the next check will work in any case.
  5638. if (__kmp_global.g.g_abort) {
  5639. KA_TRACE(11, ("__kmp_internal_end_library: abort, exiting\n"));
  5640. /* TODO abort? */
  5641. return;
  5642. }
  5643. if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) {
  5644. KA_TRACE(10, ("__kmp_internal_end_library: already finished\n"));
  5645. return;
  5646. }
  5647. // If hidden helper team has been initialized, we need to deinit it
  5648. if (TCR_4(__kmp_init_hidden_helper) &&
  5649. !TCR_4(__kmp_hidden_helper_team_done)) {
  5650. TCW_SYNC_4(__kmp_hidden_helper_team_done, TRUE);
  5651. // First release the main thread to let it continue its work
  5652. __kmp_hidden_helper_main_thread_release();
  5653. // Wait until the hidden helper team has been destroyed
  5654. __kmp_hidden_helper_threads_deinitz_wait();
  5655. }
  5656. KMP_MB(); /* Flush all pending memory write invalidates. */
  5657. /* find out who we are and what we should do */
  5658. {
  5659. int gtid = (gtid_req >= 0) ? gtid_req : __kmp_gtid_get_specific();
  5660. KA_TRACE(
  5661. 10, ("__kmp_internal_end_library: enter T#%d (%d)\n", gtid, gtid_req));
  5662. if (gtid == KMP_GTID_SHUTDOWN) {
  5663. KA_TRACE(10, ("__kmp_internal_end_library: !__kmp_init_runtime, system "
  5664. "already shutdown\n"));
  5665. return;
  5666. } else if (gtid == KMP_GTID_MONITOR) {
  5667. KA_TRACE(10, ("__kmp_internal_end_library: monitor thread, gtid not "
  5668. "registered, or system shutdown\n"));
  5669. return;
  5670. } else if (gtid == KMP_GTID_DNE) {
  5671. KA_TRACE(10, ("__kmp_internal_end_library: gtid not registered or system "
  5672. "shutdown\n"));
  5673. /* we don't know who we are, but we may still shutdown the library */
  5674. } else if (KMP_UBER_GTID(gtid)) {
  5675. /* unregister ourselves as an uber thread. gtid is no longer valid */
  5676. if (__kmp_root[gtid]->r.r_active) {
  5677. __kmp_global.g.g_abort = -1;
  5678. TCW_SYNC_4(__kmp_global.g.g_done, TRUE);
  5679. __kmp_unregister_library();
  5680. KA_TRACE(10,
  5681. ("__kmp_internal_end_library: root still active, abort T#%d\n",
  5682. gtid));
  5683. return;
  5684. } else {
  5685. __kmp_itthash_clean(__kmp_threads[gtid]);
  5686. KA_TRACE(
  5687. 10,
  5688. ("__kmp_internal_end_library: unregistering sibling T#%d\n", gtid));
  5689. __kmp_unregister_root_current_thread(gtid);
  5690. }
  5691. } else {
  5692. /* worker threads may call this function through the atexit handler, if they
  5693. * call exit() */
  5694. /* For now, skip the usual subsequent processing and just dump the debug buffer.
  5695. TODO: do a thorough shutdown instead */
  5696. #ifdef DUMP_DEBUG_ON_EXIT
  5697. if (__kmp_debug_buf)
  5698. __kmp_dump_debug_buffer();
  5699. #endif
  5700. // added unregister library call here when we switch to shm linux
  5701. // if we don't, it will leave lots of files in /dev/shm
  5702. // cleanup shared memory file before exiting.
  5703. __kmp_unregister_library();
  5704. return;
  5705. }
  5706. }
  5707. /* synchronize the termination process */
  5708. __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
  5709. /* have we already finished */
  5710. if (__kmp_global.g.g_abort) {
  5711. KA_TRACE(10, ("__kmp_internal_end_library: abort, exiting\n"));
  5712. /* TODO abort? */
  5713. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  5714. return;
  5715. }
  5716. if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) {
  5717. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  5718. return;
  5719. }
  5720. /* We need this lock to enforce mutex between this reading of
  5721. __kmp_threads_capacity and the writing by __kmp_register_root.
  5722. Alternatively, we can use a counter of roots that is atomically updated by
  5723. __kmp_get_global_thread_id_reg, __kmp_do_serial_initialize and
  5724. __kmp_internal_end_*. */
  5725. __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
  5726. /* now we can safely conduct the actual termination */
  5727. __kmp_internal_end();
  5728. __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
  5729. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  5730. KA_TRACE(10, ("__kmp_internal_end_library: exit\n"));
  5731. #ifdef DUMP_DEBUG_ON_EXIT
  5732. if (__kmp_debug_buf)
  5733. __kmp_dump_debug_buffer();
  5734. #endif
  5735. #if KMP_OS_WINDOWS
  5736. __kmp_close_console();
  5737. #endif
  5738. __kmp_fini_allocator();
  5739. } // __kmp_internal_end_library
  5740. void __kmp_internal_end_thread(int gtid_req) {
  5741. int i;
  5742. /* if we have already cleaned up, don't try again, it wouldn't be pretty */
  5743. /* this shouldn't be a race condition because __kmp_internal_end() is the
  5744. * only place to clear __kmp_serial_init */
  5745. /* we'll check this later too, after we get the lock */
  5746. // 2009-09-06: We do not set g_abort without setting g_done. This check looks
  5747. // redundant, because the next check will work in any case.
  5748. if (__kmp_global.g.g_abort) {
  5749. KA_TRACE(11, ("__kmp_internal_end_thread: abort, exiting\n"));
  5750. /* TODO abort? */
  5751. return;
  5752. }
  5753. if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) {
  5754. KA_TRACE(10, ("__kmp_internal_end_thread: already finished\n"));
  5755. return;
  5756. }
  5757. // If hidden helper team has been initialized, we need to deinit it
  5758. if (TCR_4(__kmp_init_hidden_helper) &&
  5759. !TCR_4(__kmp_hidden_helper_team_done)) {
  5760. TCW_SYNC_4(__kmp_hidden_helper_team_done, TRUE);
  5761. // First release the main thread to let it continue its work
  5762. __kmp_hidden_helper_main_thread_release();
  5763. // Wait until the hidden helper team has been destroyed
  5764. __kmp_hidden_helper_threads_deinitz_wait();
  5765. }
  5766. KMP_MB(); /* Flush all pending memory write invalidates. */
  5767. /* find out who we are and what we should do */
  5768. {
  5769. int gtid = (gtid_req >= 0) ? gtid_req : __kmp_gtid_get_specific();
  5770. KA_TRACE(10,
  5771. ("__kmp_internal_end_thread: enter T#%d (%d)\n", gtid, gtid_req));
  5772. if (gtid == KMP_GTID_SHUTDOWN) {
  5773. KA_TRACE(10, ("__kmp_internal_end_thread: !__kmp_init_runtime, system "
  5774. "already shutdown\n"));
  5775. return;
  5776. } else if (gtid == KMP_GTID_MONITOR) {
  5777. KA_TRACE(10, ("__kmp_internal_end_thread: monitor thread, gtid not "
  5778. "registered, or system shutdown\n"));
  5779. return;
  5780. } else if (gtid == KMP_GTID_DNE) {
  5781. KA_TRACE(10, ("__kmp_internal_end_thread: gtid not registered or system "
  5782. "shutdown\n"));
  5783. return;
  5784. /* we don't know who we are */
  5785. } else if (KMP_UBER_GTID(gtid)) {
  5786. /* unregister ourselves as an uber thread. gtid is no longer valid */
  5787. if (__kmp_root[gtid]->r.r_active) {
  5788. __kmp_global.g.g_abort = -1;
  5789. TCW_SYNC_4(__kmp_global.g.g_done, TRUE);
  5790. KA_TRACE(10,
  5791. ("__kmp_internal_end_thread: root still active, abort T#%d\n",
  5792. gtid));
  5793. return;
  5794. } else {
  5795. KA_TRACE(10, ("__kmp_internal_end_thread: unregistering sibling T#%d\n",
  5796. gtid));
  5797. __kmp_unregister_root_current_thread(gtid);
  5798. }
  5799. } else {
  5800. /* just a worker thread, let's leave */
  5801. KA_TRACE(10, ("__kmp_internal_end_thread: worker thread T#%d\n", gtid));
  5802. if (gtid >= 0) {
  5803. __kmp_threads[gtid]->th.th_task_team = NULL;
  5804. }
  5805. KA_TRACE(10,
  5806. ("__kmp_internal_end_thread: worker thread done, exiting T#%d\n",
  5807. gtid));
  5808. return;
  5809. }
  5810. }
  5811. #if KMP_DYNAMIC_LIB
  5812. if (__kmp_pause_status != kmp_hard_paused)
  5813. // AC: lets not shutdown the dynamic library at the exit of uber thread,
  5814. // because we will better shutdown later in the library destructor.
  5815. {
  5816. KA_TRACE(10, ("__kmp_internal_end_thread: exiting T#%d\n", gtid_req));
  5817. return;
  5818. }
  5819. #endif
  5820. /* synchronize the termination process */
  5821. __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
  5822. /* have we already finished */
  5823. if (__kmp_global.g.g_abort) {
  5824. KA_TRACE(10, ("__kmp_internal_end_thread: abort, exiting\n"));
  5825. /* TODO abort? */
  5826. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  5827. return;
  5828. }
  5829. if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) {
  5830. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  5831. return;
  5832. }
  5833. /* We need this lock to enforce mutex between this reading of
  5834. __kmp_threads_capacity and the writing by __kmp_register_root.
  5835. Alternatively, we can use a counter of roots that is atomically updated by
  5836. __kmp_get_global_thread_id_reg, __kmp_do_serial_initialize and
  5837. __kmp_internal_end_*. */
  5838. /* should we finish the run-time? are all siblings done? */
  5839. __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
  5840. for (i = 0; i < __kmp_threads_capacity; ++i) {
  5841. if (KMP_UBER_GTID(i)) {
  5842. KA_TRACE(
  5843. 10,
  5844. ("__kmp_internal_end_thread: remaining sibling task: gtid==%d\n", i));
  5845. __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
  5846. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  5847. return;
  5848. }
  5849. }
  5850. /* now we can safely conduct the actual termination */
  5851. __kmp_internal_end();
  5852. __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
  5853. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  5854. KA_TRACE(10, ("__kmp_internal_end_thread: exit T#%d\n", gtid_req));
  5855. #ifdef DUMP_DEBUG_ON_EXIT
  5856. if (__kmp_debug_buf)
  5857. __kmp_dump_debug_buffer();
  5858. #endif
  5859. } // __kmp_internal_end_thread
  5860. // -----------------------------------------------------------------------------
  5861. // Library registration stuff.
  5862. static long __kmp_registration_flag = 0;
  5863. // Random value used to indicate library initialization.
  5864. static char *__kmp_registration_str = NULL;
  5865. // Value to be saved in env var __KMP_REGISTERED_LIB_<pid>.
  5866. static inline char *__kmp_reg_status_name() {
  5867. /* On RHEL 3u5 if linked statically, getpid() returns different values in
  5868. each thread. If registration and unregistration go in different threads
  5869. (omp_misc_other_root_exit.cpp test case), the name of registered_lib_env
  5870. env var can not be found, because the name will contain different pid. */
  5871. // macOS* complains about name being too long with additional getuid()
  5872. #if KMP_OS_UNIX && !KMP_OS_DARWIN && KMP_DYNAMIC_LIB
  5873. return __kmp_str_format("__KMP_REGISTERED_LIB_%d_%d", (int)getpid(),
  5874. (int)getuid());
  5875. #else
  5876. return __kmp_str_format("__KMP_REGISTERED_LIB_%d", (int)getpid());
  5877. #endif
  5878. } // __kmp_reg_status_get
  5879. void __kmp_register_library_startup(void) {
  5880. char *name = __kmp_reg_status_name(); // Name of the environment variable.
  5881. int done = 0;
  5882. union {
  5883. double dtime;
  5884. long ltime;
  5885. } time;
  5886. #if KMP_ARCH_X86 || KMP_ARCH_X86_64
  5887. __kmp_initialize_system_tick();
  5888. #endif
  5889. __kmp_read_system_time(&time.dtime);
  5890. __kmp_registration_flag = 0xCAFE0000L | (time.ltime & 0x0000FFFFL);
  5891. __kmp_registration_str =
  5892. __kmp_str_format("%p-%lx-%s", &__kmp_registration_flag,
  5893. __kmp_registration_flag, KMP_LIBRARY_FILE);
  5894. KA_TRACE(50, ("__kmp_register_library_startup: %s=\"%s\"\n", name,
  5895. __kmp_registration_str));
  5896. while (!done) {
  5897. char *value = NULL; // Actual value of the environment variable.
  5898. #if defined(KMP_USE_SHM)
  5899. char *shm_name = __kmp_str_format("/%s", name);
  5900. int shm_preexist = 0;
  5901. char *data1;
  5902. int fd1 = shm_open(shm_name, O_CREAT | O_EXCL | O_RDWR, 0666);
  5903. if ((fd1 == -1) && (errno == EEXIST)) {
  5904. // file didn't open because it already exists.
  5905. // try opening existing file
  5906. fd1 = shm_open(shm_name, O_RDWR, 0666);
  5907. if (fd1 == -1) { // file didn't open
  5908. // error out here
  5909. __kmp_fatal(KMP_MSG(FunctionError, "Can't open SHM"), KMP_ERR(0),
  5910. __kmp_msg_null);
  5911. } else {
  5912. // able to open existing file
  5913. shm_preexist = 1;
  5914. }
  5915. } else if (fd1 == -1) { // SHM didn't open; it was due to error other than
  5916. // already exists.
  5917. // error out here.
  5918. __kmp_fatal(KMP_MSG(FunctionError, "Can't open SHM2"), KMP_ERR(errno),
  5919. __kmp_msg_null);
  5920. }
  5921. if (shm_preexist == 0) {
  5922. // we created SHM now set size
  5923. if (ftruncate(fd1, SHM_SIZE) == -1) {
  5924. // error occured setting size;
  5925. __kmp_fatal(KMP_MSG(FunctionError, "Can't set size of SHM"),
  5926. KMP_ERR(errno), __kmp_msg_null);
  5927. }
  5928. }
  5929. data1 =
  5930. (char *)mmap(0, SHM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0);
  5931. if (data1 == MAP_FAILED) {
  5932. // failed to map shared memory
  5933. __kmp_fatal(KMP_MSG(FunctionError, "Can't map SHM"), KMP_ERR(errno),
  5934. __kmp_msg_null);
  5935. }
  5936. if (shm_preexist == 0) { // set data to SHM, set value
  5937. KMP_STRCPY_S(data1, SHM_SIZE, __kmp_registration_str);
  5938. }
  5939. // Read value from either what we just wrote or existing file.
  5940. value = __kmp_str_format("%s", data1); // read value from SHM
  5941. munmap(data1, SHM_SIZE);
  5942. close(fd1);
  5943. #else // Windows and unix with static library
  5944. // Set environment variable, but do not overwrite if it is exist.
  5945. __kmp_env_set(name, __kmp_registration_str, 0);
  5946. // read value to see if it got set
  5947. value = __kmp_env_get(name);
  5948. #endif
  5949. if (value != NULL && strcmp(value, __kmp_registration_str) == 0) {
  5950. done = 1; // Ok, environment variable set successfully, exit the loop.
  5951. } else {
  5952. // Oops. Write failed. Another copy of OpenMP RTL is in memory.
  5953. // Check whether it alive or dead.
  5954. int neighbor = 0; // 0 -- unknown status, 1 -- alive, 2 -- dead.
  5955. char *tail = value;
  5956. char *flag_addr_str = NULL;
  5957. char *flag_val_str = NULL;
  5958. char const *file_name = NULL;
  5959. __kmp_str_split(tail, '-', &flag_addr_str, &tail);
  5960. __kmp_str_split(tail, '-', &flag_val_str, &tail);
  5961. file_name = tail;
  5962. if (tail != NULL) {
  5963. unsigned long *flag_addr = 0;
  5964. unsigned long flag_val = 0;
  5965. KMP_SSCANF(flag_addr_str, "%p", RCAST(void **, &flag_addr));
  5966. KMP_SSCANF(flag_val_str, "%lx", &flag_val);
  5967. if (flag_addr != 0 && flag_val != 0 && strcmp(file_name, "") != 0) {
  5968. // First, check whether environment-encoded address is mapped into
  5969. // addr space.
  5970. // If so, dereference it to see if it still has the right value.
  5971. if (__kmp_is_address_mapped(flag_addr) && *flag_addr == flag_val) {
  5972. neighbor = 1;
  5973. } else {
  5974. // If not, then we know the other copy of the library is no longer
  5975. // running.
  5976. neighbor = 2;
  5977. }
  5978. }
  5979. }
  5980. switch (neighbor) {
  5981. case 0: // Cannot parse environment variable -- neighbor status unknown.
  5982. // Assume it is the incompatible format of future version of the
  5983. // library. Assume the other library is alive.
  5984. // WARN( ... ); // TODO: Issue a warning.
  5985. file_name = "unknown library";
  5986. KMP_FALLTHROUGH();
  5987. // Attention! Falling to the next case. That's intentional.
  5988. case 1: { // Neighbor is alive.
  5989. // Check it is allowed.
  5990. char *duplicate_ok = __kmp_env_get("KMP_DUPLICATE_LIB_OK");
  5991. if (!__kmp_str_match_true(duplicate_ok)) {
  5992. // That's not allowed. Issue fatal error.
  5993. __kmp_fatal(KMP_MSG(DuplicateLibrary, KMP_LIBRARY_FILE, file_name),
  5994. KMP_HNT(DuplicateLibrary), __kmp_msg_null);
  5995. }
  5996. KMP_INTERNAL_FREE(duplicate_ok);
  5997. __kmp_duplicate_library_ok = 1;
  5998. done = 1; // Exit the loop.
  5999. } break;
  6000. case 2: { // Neighbor is dead.
  6001. #if defined(KMP_USE_SHM)
  6002. // close shared memory.
  6003. shm_unlink(shm_name); // this removes file in /dev/shm
  6004. #else
  6005. // Clear the variable and try to register library again.
  6006. __kmp_env_unset(name);
  6007. #endif
  6008. } break;
  6009. default: {
  6010. KMP_DEBUG_ASSERT(0);
  6011. } break;
  6012. }
  6013. }
  6014. KMP_INTERNAL_FREE((void *)value);
  6015. #if defined(KMP_USE_SHM)
  6016. KMP_INTERNAL_FREE((void *)shm_name);
  6017. #endif
  6018. } // while
  6019. KMP_INTERNAL_FREE((void *)name);
  6020. } // func __kmp_register_library_startup
  6021. void __kmp_unregister_library(void) {
  6022. char *name = __kmp_reg_status_name();
  6023. char *value = NULL;
  6024. #if defined(KMP_USE_SHM)
  6025. char *shm_name = __kmp_str_format("/%s", name);
  6026. int fd1 = shm_open(shm_name, O_RDONLY, 0666);
  6027. if (fd1 == -1) {
  6028. // file did not open. return.
  6029. return;
  6030. }
  6031. char *data1 = (char *)mmap(0, SHM_SIZE, PROT_READ, MAP_SHARED, fd1, 0);
  6032. if (data1 != MAP_FAILED) {
  6033. value = __kmp_str_format("%s", data1); // read value from SHM
  6034. munmap(data1, SHM_SIZE);
  6035. }
  6036. close(fd1);
  6037. #else
  6038. value = __kmp_env_get(name);
  6039. #endif
  6040. KMP_DEBUG_ASSERT(__kmp_registration_flag != 0);
  6041. KMP_DEBUG_ASSERT(__kmp_registration_str != NULL);
  6042. if (value != NULL && strcmp(value, __kmp_registration_str) == 0) {
  6043. // Ok, this is our variable. Delete it.
  6044. #if defined(KMP_USE_SHM)
  6045. shm_unlink(shm_name); // this removes file in /dev/shm
  6046. #else
  6047. __kmp_env_unset(name);
  6048. #endif
  6049. }
  6050. #if defined(KMP_USE_SHM)
  6051. KMP_INTERNAL_FREE(shm_name);
  6052. #endif
  6053. KMP_INTERNAL_FREE(__kmp_registration_str);
  6054. KMP_INTERNAL_FREE(value);
  6055. KMP_INTERNAL_FREE(name);
  6056. __kmp_registration_flag = 0;
  6057. __kmp_registration_str = NULL;
  6058. } // __kmp_unregister_library
  6059. // End of Library registration stuff.
  6060. // -----------------------------------------------------------------------------
  6061. #if KMP_MIC_SUPPORTED
  6062. static void __kmp_check_mic_type() {
  6063. kmp_cpuid_t cpuid_state = {0};
  6064. kmp_cpuid_t *cs_p = &cpuid_state;
  6065. __kmp_x86_cpuid(1, 0, cs_p);
  6066. // We don't support mic1 at the moment
  6067. if ((cs_p->eax & 0xff0) == 0xB10) {
  6068. __kmp_mic_type = mic2;
  6069. } else if ((cs_p->eax & 0xf0ff0) == 0x50670) {
  6070. __kmp_mic_type = mic3;
  6071. } else {
  6072. __kmp_mic_type = non_mic;
  6073. }
  6074. }
  6075. #endif /* KMP_MIC_SUPPORTED */
  6076. #if KMP_HAVE_UMWAIT
  6077. static void __kmp_user_level_mwait_init() {
  6078. struct kmp_cpuid buf;
  6079. __kmp_x86_cpuid(7, 0, &buf);
  6080. __kmp_waitpkg_enabled = ((buf.ecx >> 5) & 1);
  6081. __kmp_umwait_enabled = __kmp_waitpkg_enabled && __kmp_user_level_mwait;
  6082. __kmp_tpause_enabled = __kmp_waitpkg_enabled && (__kmp_tpause_state > 0);
  6083. KF_TRACE(30, ("__kmp_user_level_mwait_init: __kmp_umwait_enabled = %d\n",
  6084. __kmp_umwait_enabled));
  6085. }
  6086. #elif KMP_HAVE_MWAIT
  6087. #ifndef AT_INTELPHIUSERMWAIT
  6088. // Spurious, non-existent value that should always fail to return anything.
  6089. // Will be replaced with the correct value when we know that.
  6090. #define AT_INTELPHIUSERMWAIT 10000
  6091. #endif
  6092. // getauxval() function is available in RHEL7 and SLES12. If a system with an
  6093. // earlier OS is used to build the RTL, we'll use the following internal
  6094. // function when the entry is not found.
  6095. unsigned long getauxval(unsigned long) KMP_WEAK_ATTRIBUTE_EXTERNAL;
  6096. unsigned long getauxval(unsigned long) { return 0; }
  6097. static void __kmp_user_level_mwait_init() {
  6098. // When getauxval() and correct value of AT_INTELPHIUSERMWAIT are available
  6099. // use them to find if the user-level mwait is enabled. Otherwise, forcibly
  6100. // set __kmp_mwait_enabled=TRUE on Intel MIC if the environment variable
  6101. // KMP_USER_LEVEL_MWAIT was set to TRUE.
  6102. if (__kmp_mic_type == mic3) {
  6103. unsigned long res = getauxval(AT_INTELPHIUSERMWAIT);
  6104. if ((res & 0x1) || __kmp_user_level_mwait) {
  6105. __kmp_mwait_enabled = TRUE;
  6106. if (__kmp_user_level_mwait) {
  6107. KMP_INFORM(EnvMwaitWarn);
  6108. }
  6109. } else {
  6110. __kmp_mwait_enabled = FALSE;
  6111. }
  6112. }
  6113. KF_TRACE(30, ("__kmp_user_level_mwait_init: __kmp_mic_type = %d, "
  6114. "__kmp_mwait_enabled = %d\n",
  6115. __kmp_mic_type, __kmp_mwait_enabled));
  6116. }
  6117. #endif /* KMP_HAVE_UMWAIT */
  6118. static void __kmp_do_serial_initialize(void) {
  6119. int i, gtid;
  6120. size_t size;
  6121. KA_TRACE(10, ("__kmp_do_serial_initialize: enter\n"));
  6122. KMP_DEBUG_ASSERT(sizeof(kmp_int32) == 4);
  6123. KMP_DEBUG_ASSERT(sizeof(kmp_uint32) == 4);
  6124. KMP_DEBUG_ASSERT(sizeof(kmp_int64) == 8);
  6125. KMP_DEBUG_ASSERT(sizeof(kmp_uint64) == 8);
  6126. KMP_DEBUG_ASSERT(sizeof(kmp_intptr_t) == sizeof(void *));
  6127. #if OMPT_SUPPORT
  6128. ompt_pre_init();
  6129. #endif
  6130. #if OMPD_SUPPORT
  6131. __kmp_env_dump();
  6132. ompd_init();
  6133. #endif
  6134. __kmp_validate_locks();
  6135. /* Initialize internal memory allocator */
  6136. __kmp_init_allocator();
  6137. /* Register the library startup via an environment variable or via mapped
  6138. shared memory file and check to see whether another copy of the library is
  6139. already registered. Since forked child process is often terminated, we
  6140. postpone the registration till middle initialization in the child */
  6141. if (__kmp_need_register_serial)
  6142. __kmp_register_library_startup();
  6143. /* TODO reinitialization of library */
  6144. if (TCR_4(__kmp_global.g.g_done)) {
  6145. KA_TRACE(10, ("__kmp_do_serial_initialize: reinitialization of library\n"));
  6146. }
  6147. __kmp_global.g.g_abort = 0;
  6148. TCW_SYNC_4(__kmp_global.g.g_done, FALSE);
  6149. /* initialize the locks */
  6150. #if KMP_USE_ADAPTIVE_LOCKS
  6151. #if KMP_DEBUG_ADAPTIVE_LOCKS
  6152. __kmp_init_speculative_stats();
  6153. #endif
  6154. #endif
  6155. #if KMP_STATS_ENABLED
  6156. __kmp_stats_init();
  6157. #endif
  6158. __kmp_init_lock(&__kmp_global_lock);
  6159. __kmp_init_queuing_lock(&__kmp_dispatch_lock);
  6160. __kmp_init_lock(&__kmp_debug_lock);
  6161. __kmp_init_atomic_lock(&__kmp_atomic_lock);
  6162. __kmp_init_atomic_lock(&__kmp_atomic_lock_1i);
  6163. __kmp_init_atomic_lock(&__kmp_atomic_lock_2i);
  6164. __kmp_init_atomic_lock(&__kmp_atomic_lock_4i);
  6165. __kmp_init_atomic_lock(&__kmp_atomic_lock_4r);
  6166. __kmp_init_atomic_lock(&__kmp_atomic_lock_8i);
  6167. __kmp_init_atomic_lock(&__kmp_atomic_lock_8r);
  6168. __kmp_init_atomic_lock(&__kmp_atomic_lock_8c);
  6169. __kmp_init_atomic_lock(&__kmp_atomic_lock_10r);
  6170. __kmp_init_atomic_lock(&__kmp_atomic_lock_16r);
  6171. __kmp_init_atomic_lock(&__kmp_atomic_lock_16c);
  6172. __kmp_init_atomic_lock(&__kmp_atomic_lock_20c);
  6173. __kmp_init_atomic_lock(&__kmp_atomic_lock_32c);
  6174. __kmp_init_bootstrap_lock(&__kmp_forkjoin_lock);
  6175. __kmp_init_bootstrap_lock(&__kmp_exit_lock);
  6176. #if KMP_USE_MONITOR
  6177. __kmp_init_bootstrap_lock(&__kmp_monitor_lock);
  6178. #endif
  6179. __kmp_init_bootstrap_lock(&__kmp_tp_cached_lock);
  6180. /* conduct initialization and initial setup of configuration */
  6181. __kmp_runtime_initialize();
  6182. #if KMP_MIC_SUPPORTED
  6183. __kmp_check_mic_type();
  6184. #endif
  6185. // Some global variable initialization moved here from kmp_env_initialize()
  6186. #ifdef KMP_DEBUG
  6187. kmp_diag = 0;
  6188. #endif
  6189. __kmp_abort_delay = 0;
  6190. // From __kmp_init_dflt_team_nth()
  6191. /* assume the entire machine will be used */
  6192. __kmp_dflt_team_nth_ub = __kmp_xproc;
  6193. if (__kmp_dflt_team_nth_ub < KMP_MIN_NTH) {
  6194. __kmp_dflt_team_nth_ub = KMP_MIN_NTH;
  6195. }
  6196. if (__kmp_dflt_team_nth_ub > __kmp_sys_max_nth) {
  6197. __kmp_dflt_team_nth_ub = __kmp_sys_max_nth;
  6198. }
  6199. __kmp_max_nth = __kmp_sys_max_nth;
  6200. __kmp_cg_max_nth = __kmp_sys_max_nth;
  6201. __kmp_teams_max_nth = __kmp_xproc; // set a "reasonable" default
  6202. if (__kmp_teams_max_nth > __kmp_sys_max_nth) {
  6203. __kmp_teams_max_nth = __kmp_sys_max_nth;
  6204. }
  6205. // Three vars below moved here from __kmp_env_initialize() "KMP_BLOCKTIME"
  6206. // part
  6207. __kmp_dflt_blocktime = KMP_DEFAULT_BLOCKTIME;
  6208. #if KMP_USE_MONITOR
  6209. __kmp_monitor_wakeups =
  6210. KMP_WAKEUPS_FROM_BLOCKTIME(__kmp_dflt_blocktime, __kmp_monitor_wakeups);
  6211. __kmp_bt_intervals =
  6212. KMP_INTERVALS_FROM_BLOCKTIME(__kmp_dflt_blocktime, __kmp_monitor_wakeups);
  6213. #endif
  6214. // From "KMP_LIBRARY" part of __kmp_env_initialize()
  6215. __kmp_library = library_throughput;
  6216. // From KMP_SCHEDULE initialization
  6217. __kmp_static = kmp_sch_static_balanced;
  6218. // AC: do not use analytical here, because it is non-monotonous
  6219. //__kmp_guided = kmp_sch_guided_iterative_chunked;
  6220. //__kmp_auto = kmp_sch_guided_analytical_chunked; // AC: it is the default, no
  6221. // need to repeat assignment
  6222. // Barrier initialization. Moved here from __kmp_env_initialize() Barrier branch
  6223. // bit control and barrier method control parts
  6224. #if KMP_FAST_REDUCTION_BARRIER
  6225. #define kmp_reduction_barrier_gather_bb ((int)1)
  6226. #define kmp_reduction_barrier_release_bb ((int)1)
  6227. #define kmp_reduction_barrier_gather_pat __kmp_barrier_gather_pat_dflt
  6228. #define kmp_reduction_barrier_release_pat __kmp_barrier_release_pat_dflt
  6229. #endif // KMP_FAST_REDUCTION_BARRIER
  6230. for (i = bs_plain_barrier; i < bs_last_barrier; i++) {
  6231. __kmp_barrier_gather_branch_bits[i] = __kmp_barrier_gather_bb_dflt;
  6232. __kmp_barrier_release_branch_bits[i] = __kmp_barrier_release_bb_dflt;
  6233. __kmp_barrier_gather_pattern[i] = __kmp_barrier_gather_pat_dflt;
  6234. __kmp_barrier_release_pattern[i] = __kmp_barrier_release_pat_dflt;
  6235. #if KMP_FAST_REDUCTION_BARRIER
  6236. if (i == bs_reduction_barrier) { // tested and confirmed on ALTIX only (
  6237. // lin_64 ): hyper,1
  6238. __kmp_barrier_gather_branch_bits[i] = kmp_reduction_barrier_gather_bb;
  6239. __kmp_barrier_release_branch_bits[i] = kmp_reduction_barrier_release_bb;
  6240. __kmp_barrier_gather_pattern[i] = kmp_reduction_barrier_gather_pat;
  6241. __kmp_barrier_release_pattern[i] = kmp_reduction_barrier_release_pat;
  6242. }
  6243. #endif // KMP_FAST_REDUCTION_BARRIER
  6244. }
  6245. #if KMP_FAST_REDUCTION_BARRIER
  6246. #undef kmp_reduction_barrier_release_pat
  6247. #undef kmp_reduction_barrier_gather_pat
  6248. #undef kmp_reduction_barrier_release_bb
  6249. #undef kmp_reduction_barrier_gather_bb
  6250. #endif // KMP_FAST_REDUCTION_BARRIER
  6251. #if KMP_MIC_SUPPORTED
  6252. if (__kmp_mic_type == mic2) { // KNC
  6253. // AC: plane=3,2, forkjoin=2,1 are optimal for 240 threads on KNC
  6254. __kmp_barrier_gather_branch_bits[bs_plain_barrier] = 3; // plain gather
  6255. __kmp_barrier_release_branch_bits[bs_forkjoin_barrier] =
  6256. 1; // forkjoin release
  6257. __kmp_barrier_gather_pattern[bs_forkjoin_barrier] = bp_hierarchical_bar;
  6258. __kmp_barrier_release_pattern[bs_forkjoin_barrier] = bp_hierarchical_bar;
  6259. }
  6260. #if KMP_FAST_REDUCTION_BARRIER
  6261. if (__kmp_mic_type == mic2) { // KNC
  6262. __kmp_barrier_gather_pattern[bs_reduction_barrier] = bp_hierarchical_bar;
  6263. __kmp_barrier_release_pattern[bs_reduction_barrier] = bp_hierarchical_bar;
  6264. }
  6265. #endif // KMP_FAST_REDUCTION_BARRIER
  6266. #endif // KMP_MIC_SUPPORTED
  6267. // From KMP_CHECKS initialization
  6268. #ifdef KMP_DEBUG
  6269. __kmp_env_checks = TRUE; /* development versions have the extra checks */
  6270. #else
  6271. __kmp_env_checks = FALSE; /* port versions do not have the extra checks */
  6272. #endif
  6273. // From "KMP_FOREIGN_THREADS_THREADPRIVATE" initialization
  6274. __kmp_foreign_tp = TRUE;
  6275. __kmp_global.g.g_dynamic = FALSE;
  6276. __kmp_global.g.g_dynamic_mode = dynamic_default;
  6277. __kmp_init_nesting_mode();
  6278. __kmp_env_initialize(NULL);
  6279. #if KMP_HAVE_MWAIT || KMP_HAVE_UMWAIT
  6280. __kmp_user_level_mwait_init();
  6281. #endif
  6282. // Print all messages in message catalog for testing purposes.
  6283. #ifdef KMP_DEBUG
  6284. char const *val = __kmp_env_get("KMP_DUMP_CATALOG");
  6285. if (__kmp_str_match_true(val)) {
  6286. kmp_str_buf_t buffer;
  6287. __kmp_str_buf_init(&buffer);
  6288. __kmp_i18n_dump_catalog(&buffer);
  6289. __kmp_printf("%s", buffer.str);
  6290. __kmp_str_buf_free(&buffer);
  6291. }
  6292. __kmp_env_free(&val);
  6293. #endif
  6294. __kmp_threads_capacity =
  6295. __kmp_initial_threads_capacity(__kmp_dflt_team_nth_ub);
  6296. // Moved here from __kmp_env_initialize() "KMP_ALL_THREADPRIVATE" part
  6297. __kmp_tp_capacity = __kmp_default_tp_capacity(
  6298. __kmp_dflt_team_nth_ub, __kmp_max_nth, __kmp_allThreadsSpecified);
  6299. // If the library is shut down properly, both pools must be NULL. Just in
  6300. // case, set them to NULL -- some memory may leak, but subsequent code will
  6301. // work even if pools are not freed.
  6302. KMP_DEBUG_ASSERT(__kmp_thread_pool == NULL);
  6303. KMP_DEBUG_ASSERT(__kmp_thread_pool_insert_pt == NULL);
  6304. KMP_DEBUG_ASSERT(__kmp_team_pool == NULL);
  6305. __kmp_thread_pool = NULL;
  6306. __kmp_thread_pool_insert_pt = NULL;
  6307. __kmp_team_pool = NULL;
  6308. /* Allocate all of the variable sized records */
  6309. /* NOTE: __kmp_threads_capacity entries are allocated, but the arrays are
  6310. * expandable */
  6311. /* Since allocation is cache-aligned, just add extra padding at the end */
  6312. size =
  6313. (sizeof(kmp_info_t *) + sizeof(kmp_root_t *)) * __kmp_threads_capacity +
  6314. CACHE_LINE;
  6315. __kmp_threads = (kmp_info_t **)__kmp_allocate(size);
  6316. __kmp_root = (kmp_root_t **)((char *)__kmp_threads +
  6317. sizeof(kmp_info_t *) * __kmp_threads_capacity);
  6318. /* init thread counts */
  6319. KMP_DEBUG_ASSERT(__kmp_all_nth ==
  6320. 0); // Asserts fail if the library is reinitializing and
  6321. KMP_DEBUG_ASSERT(__kmp_nth == 0); // something was wrong in termination.
  6322. __kmp_all_nth = 0;
  6323. __kmp_nth = 0;
  6324. /* setup the uber master thread and hierarchy */
  6325. gtid = __kmp_register_root(TRUE);
  6326. KA_TRACE(10, ("__kmp_do_serial_initialize T#%d\n", gtid));
  6327. KMP_ASSERT(KMP_UBER_GTID(gtid));
  6328. KMP_ASSERT(KMP_INITIAL_GTID(gtid));
  6329. KMP_MB(); /* Flush all pending memory write invalidates. */
  6330. __kmp_common_initialize();
  6331. #if KMP_OS_UNIX
  6332. /* invoke the child fork handler */
  6333. __kmp_register_atfork();
  6334. #endif
  6335. #if !KMP_DYNAMIC_LIB
  6336. {
  6337. /* Invoke the exit handler when the program finishes, only for static
  6338. library. For dynamic library, we already have _fini and DllMain. */
  6339. int rc = atexit(__kmp_internal_end_atexit);
  6340. if (rc != 0) {
  6341. __kmp_fatal(KMP_MSG(FunctionError, "atexit()"), KMP_ERR(rc),
  6342. __kmp_msg_null);
  6343. }
  6344. }
  6345. #endif
  6346. #if KMP_HANDLE_SIGNALS
  6347. #if KMP_OS_UNIX
  6348. /* NOTE: make sure that this is called before the user installs their own
  6349. signal handlers so that the user handlers are called first. this way they
  6350. can return false, not call our handler, avoid terminating the library, and
  6351. continue execution where they left off. */
  6352. __kmp_install_signals(FALSE);
  6353. #endif /* KMP_OS_UNIX */
  6354. #if KMP_OS_WINDOWS
  6355. __kmp_install_signals(TRUE);
  6356. #endif /* KMP_OS_WINDOWS */
  6357. #endif
  6358. /* we have finished the serial initialization */
  6359. __kmp_init_counter++;
  6360. __kmp_init_serial = TRUE;
  6361. if (__kmp_settings) {
  6362. __kmp_env_print();
  6363. }
  6364. if (__kmp_display_env || __kmp_display_env_verbose) {
  6365. __kmp_env_print_2();
  6366. }
  6367. #if OMPT_SUPPORT
  6368. ompt_post_init();
  6369. #endif
  6370. KMP_MB();
  6371. KA_TRACE(10, ("__kmp_do_serial_initialize: exit\n"));
  6372. }
  6373. void __kmp_serial_initialize(void) {
  6374. if (__kmp_init_serial) {
  6375. return;
  6376. }
  6377. __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
  6378. if (__kmp_init_serial) {
  6379. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  6380. return;
  6381. }
  6382. __kmp_do_serial_initialize();
  6383. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  6384. }
  6385. static void __kmp_do_middle_initialize(void) {
  6386. int i, j;
  6387. int prev_dflt_team_nth;
  6388. if (!__kmp_init_serial) {
  6389. __kmp_do_serial_initialize();
  6390. }
  6391. KA_TRACE(10, ("__kmp_middle_initialize: enter\n"));
  6392. if (UNLIKELY(!__kmp_need_register_serial)) {
  6393. // We are in a forked child process. The registration was skipped during
  6394. // serial initialization in __kmp_atfork_child handler. Do it here.
  6395. __kmp_register_library_startup();
  6396. }
  6397. // Save the previous value for the __kmp_dflt_team_nth so that
  6398. // we can avoid some reinitialization if it hasn't changed.
  6399. prev_dflt_team_nth = __kmp_dflt_team_nth;
  6400. #if KMP_AFFINITY_SUPPORTED
  6401. // __kmp_affinity_initialize() will try to set __kmp_ncores to the
  6402. // number of cores on the machine.
  6403. __kmp_affinity_initialize();
  6404. #endif /* KMP_AFFINITY_SUPPORTED */
  6405. KMP_ASSERT(__kmp_xproc > 0);
  6406. if (__kmp_avail_proc == 0) {
  6407. __kmp_avail_proc = __kmp_xproc;
  6408. }
  6409. // If there were empty places in num_threads list (OMP_NUM_THREADS=,,2,3),
  6410. // correct them now
  6411. j = 0;
  6412. while ((j < __kmp_nested_nth.used) && !__kmp_nested_nth.nth[j]) {
  6413. __kmp_nested_nth.nth[j] = __kmp_dflt_team_nth = __kmp_dflt_team_nth_ub =
  6414. __kmp_avail_proc;
  6415. j++;
  6416. }
  6417. if (__kmp_dflt_team_nth == 0) {
  6418. #ifdef KMP_DFLT_NTH_CORES
  6419. // Default #threads = #cores
  6420. __kmp_dflt_team_nth = __kmp_ncores;
  6421. KA_TRACE(20, ("__kmp_middle_initialize: setting __kmp_dflt_team_nth = "
  6422. "__kmp_ncores (%d)\n",
  6423. __kmp_dflt_team_nth));
  6424. #else
  6425. // Default #threads = #available OS procs
  6426. __kmp_dflt_team_nth = __kmp_avail_proc;
  6427. KA_TRACE(20, ("__kmp_middle_initialize: setting __kmp_dflt_team_nth = "
  6428. "__kmp_avail_proc(%d)\n",
  6429. __kmp_dflt_team_nth));
  6430. #endif /* KMP_DFLT_NTH_CORES */
  6431. }
  6432. if (__kmp_dflt_team_nth < KMP_MIN_NTH) {
  6433. __kmp_dflt_team_nth = KMP_MIN_NTH;
  6434. }
  6435. if (__kmp_dflt_team_nth > __kmp_sys_max_nth) {
  6436. __kmp_dflt_team_nth = __kmp_sys_max_nth;
  6437. }
  6438. if (__kmp_nesting_mode > 0)
  6439. __kmp_set_nesting_mode_threads();
  6440. // There's no harm in continuing if the following check fails,
  6441. // but it indicates an error in the previous logic.
  6442. KMP_DEBUG_ASSERT(__kmp_dflt_team_nth <= __kmp_dflt_team_nth_ub);
  6443. if (__kmp_dflt_team_nth != prev_dflt_team_nth) {
  6444. // Run through the __kmp_threads array and set the num threads icv for each
  6445. // root thread that is currently registered with the RTL (which has not
  6446. // already explicitly set its nthreads-var with a call to
  6447. // omp_set_num_threads()).
  6448. for (i = 0; i < __kmp_threads_capacity; i++) {
  6449. kmp_info_t *thread = __kmp_threads[i];
  6450. if (thread == NULL)
  6451. continue;
  6452. if (thread->th.th_current_task->td_icvs.nproc != 0)
  6453. continue;
  6454. set__nproc(__kmp_threads[i], __kmp_dflt_team_nth);
  6455. }
  6456. }
  6457. KA_TRACE(
  6458. 20,
  6459. ("__kmp_middle_initialize: final value for __kmp_dflt_team_nth = %d\n",
  6460. __kmp_dflt_team_nth));
  6461. #ifdef KMP_ADJUST_BLOCKTIME
  6462. /* Adjust blocktime to zero if necessary now that __kmp_avail_proc is set */
  6463. if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
  6464. KMP_DEBUG_ASSERT(__kmp_avail_proc > 0);
  6465. if (__kmp_nth > __kmp_avail_proc) {
  6466. __kmp_zero_bt = TRUE;
  6467. }
  6468. }
  6469. #endif /* KMP_ADJUST_BLOCKTIME */
  6470. /* we have finished middle initialization */
  6471. TCW_SYNC_4(__kmp_init_middle, TRUE);
  6472. KA_TRACE(10, ("__kmp_do_middle_initialize: exit\n"));
  6473. }
  6474. void __kmp_middle_initialize(void) {
  6475. if (__kmp_init_middle) {
  6476. return;
  6477. }
  6478. __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
  6479. if (__kmp_init_middle) {
  6480. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  6481. return;
  6482. }
  6483. __kmp_do_middle_initialize();
  6484. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  6485. }
  6486. void __kmp_parallel_initialize(void) {
  6487. int gtid = __kmp_entry_gtid(); // this might be a new root
  6488. /* synchronize parallel initialization (for sibling) */
  6489. if (TCR_4(__kmp_init_parallel))
  6490. return;
  6491. __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
  6492. if (TCR_4(__kmp_init_parallel)) {
  6493. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  6494. return;
  6495. }
  6496. /* TODO reinitialization after we have already shut down */
  6497. if (TCR_4(__kmp_global.g.g_done)) {
  6498. KA_TRACE(
  6499. 10,
  6500. ("__kmp_parallel_initialize: attempt to init while shutting down\n"));
  6501. __kmp_infinite_loop();
  6502. }
  6503. /* jc: The lock __kmp_initz_lock is already held, so calling
  6504. __kmp_serial_initialize would cause a deadlock. So we call
  6505. __kmp_do_serial_initialize directly. */
  6506. if (!__kmp_init_middle) {
  6507. __kmp_do_middle_initialize();
  6508. }
  6509. __kmp_assign_root_init_mask();
  6510. __kmp_resume_if_hard_paused();
  6511. /* begin initialization */
  6512. KA_TRACE(10, ("__kmp_parallel_initialize: enter\n"));
  6513. KMP_ASSERT(KMP_UBER_GTID(gtid));
  6514. #if KMP_ARCH_X86 || KMP_ARCH_X86_64
  6515. // Save the FP control regs.
  6516. // Worker threads will set theirs to these values at thread startup.
  6517. __kmp_store_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word);
  6518. __kmp_store_mxcsr(&__kmp_init_mxcsr);
  6519. __kmp_init_mxcsr &= KMP_X86_MXCSR_MASK;
  6520. #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
  6521. #if KMP_OS_UNIX
  6522. #if KMP_HANDLE_SIGNALS
  6523. /* must be after __kmp_serial_initialize */
  6524. __kmp_install_signals(TRUE);
  6525. #endif
  6526. #endif
  6527. __kmp_suspend_initialize();
  6528. #if defined(USE_LOAD_BALANCE)
  6529. if (__kmp_global.g.g_dynamic_mode == dynamic_default) {
  6530. __kmp_global.g.g_dynamic_mode = dynamic_load_balance;
  6531. }
  6532. #else
  6533. if (__kmp_global.g.g_dynamic_mode == dynamic_default) {
  6534. __kmp_global.g.g_dynamic_mode = dynamic_thread_limit;
  6535. }
  6536. #endif
  6537. if (__kmp_version) {
  6538. __kmp_print_version_2();
  6539. }
  6540. /* we have finished parallel initialization */
  6541. TCW_SYNC_4(__kmp_init_parallel, TRUE);
  6542. KMP_MB();
  6543. KA_TRACE(10, ("__kmp_parallel_initialize: exit\n"));
  6544. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  6545. }
  6546. void __kmp_hidden_helper_initialize() {
  6547. if (TCR_4(__kmp_init_hidden_helper))
  6548. return;
  6549. // __kmp_parallel_initialize is required before we initialize hidden helper
  6550. if (!TCR_4(__kmp_init_parallel))
  6551. __kmp_parallel_initialize();
  6552. // Double check. Note that this double check should not be placed before
  6553. // __kmp_parallel_initialize as it will cause dead lock.
  6554. __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
  6555. if (TCR_4(__kmp_init_hidden_helper)) {
  6556. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  6557. return;
  6558. }
  6559. // Set the count of hidden helper tasks to be executed to zero
  6560. KMP_ATOMIC_ST_REL(&__kmp_unexecuted_hidden_helper_tasks, 0);
  6561. // Set the global variable indicating that we're initializing hidden helper
  6562. // team/threads
  6563. TCW_SYNC_4(__kmp_init_hidden_helper_threads, TRUE);
  6564. // Platform independent initialization
  6565. __kmp_do_initialize_hidden_helper_threads();
  6566. // Wait here for the finish of initialization of hidden helper teams
  6567. __kmp_hidden_helper_threads_initz_wait();
  6568. // We have finished hidden helper initialization
  6569. TCW_SYNC_4(__kmp_init_hidden_helper, TRUE);
  6570. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  6571. }
  6572. /* ------------------------------------------------------------------------ */
  6573. void __kmp_run_before_invoked_task(int gtid, int tid, kmp_info_t *this_thr,
  6574. kmp_team_t *team) {
  6575. kmp_disp_t *dispatch;
  6576. KMP_MB();
  6577. /* none of the threads have encountered any constructs, yet. */
  6578. this_thr->th.th_local.this_construct = 0;
  6579. #if KMP_CACHE_MANAGE
  6580. KMP_CACHE_PREFETCH(&this_thr->th.th_bar[bs_forkjoin_barrier].bb.b_arrived);
  6581. #endif /* KMP_CACHE_MANAGE */
  6582. dispatch = (kmp_disp_t *)TCR_PTR(this_thr->th.th_dispatch);
  6583. KMP_DEBUG_ASSERT(dispatch);
  6584. KMP_DEBUG_ASSERT(team->t.t_dispatch);
  6585. // KMP_DEBUG_ASSERT( this_thr->th.th_dispatch == &team->t.t_dispatch[
  6586. // this_thr->th.th_info.ds.ds_tid ] );
  6587. dispatch->th_disp_index = 0; /* reset the dispatch buffer counter */
  6588. dispatch->th_doacross_buf_idx = 0; // reset doacross dispatch buffer counter
  6589. if (__kmp_env_consistency_check)
  6590. __kmp_push_parallel(gtid, team->t.t_ident);
  6591. KMP_MB(); /* Flush all pending memory write invalidates. */
  6592. }
  6593. void __kmp_run_after_invoked_task(int gtid, int tid, kmp_info_t *this_thr,
  6594. kmp_team_t *team) {
  6595. if (__kmp_env_consistency_check)
  6596. __kmp_pop_parallel(gtid, team->t.t_ident);
  6597. __kmp_finish_implicit_task(this_thr);
  6598. }
  6599. int __kmp_invoke_task_func(int gtid) {
  6600. int rc;
  6601. int tid = __kmp_tid_from_gtid(gtid);
  6602. kmp_info_t *this_thr = __kmp_threads[gtid];
  6603. kmp_team_t *team = this_thr->th.th_team;
  6604. __kmp_run_before_invoked_task(gtid, tid, this_thr, team);
  6605. #if USE_ITT_BUILD
  6606. if (__itt_stack_caller_create_ptr) {
  6607. // inform ittnotify about entering user's code
  6608. if (team->t.t_stack_id != NULL) {
  6609. __kmp_itt_stack_callee_enter((__itt_caller)team->t.t_stack_id);
  6610. } else {
  6611. KMP_DEBUG_ASSERT(team->t.t_parent->t.t_stack_id != NULL);
  6612. __kmp_itt_stack_callee_enter(
  6613. (__itt_caller)team->t.t_parent->t.t_stack_id);
  6614. }
  6615. }
  6616. #endif /* USE_ITT_BUILD */
  6617. #if INCLUDE_SSC_MARKS
  6618. SSC_MARK_INVOKING();
  6619. #endif
  6620. #if OMPT_SUPPORT
  6621. void *dummy;
  6622. void **exit_frame_p;
  6623. ompt_data_t *my_task_data;
  6624. ompt_data_t *my_parallel_data;
  6625. int ompt_team_size;
  6626. if (ompt_enabled.enabled) {
  6627. exit_frame_p = &(team->t.t_implicit_task_taskdata[tid]
  6628. .ompt_task_info.frame.exit_frame.ptr);
  6629. } else {
  6630. exit_frame_p = &dummy;
  6631. }
  6632. my_task_data =
  6633. &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data);
  6634. my_parallel_data = &(team->t.ompt_team_info.parallel_data);
  6635. if (ompt_enabled.ompt_callback_implicit_task) {
  6636. ompt_team_size = team->t.t_nproc;
  6637. ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
  6638. ompt_scope_begin, my_parallel_data, my_task_data, ompt_team_size,
  6639. __kmp_tid_from_gtid(gtid), ompt_task_implicit);
  6640. OMPT_CUR_TASK_INFO(this_thr)->thread_num = __kmp_tid_from_gtid(gtid);
  6641. }
  6642. #endif
  6643. #if KMP_STATS_ENABLED
  6644. stats_state_e previous_state = KMP_GET_THREAD_STATE();
  6645. if (previous_state == stats_state_e::TEAMS_REGION) {
  6646. KMP_PUSH_PARTITIONED_TIMER(OMP_teams);
  6647. } else {
  6648. KMP_PUSH_PARTITIONED_TIMER(OMP_parallel);
  6649. }
  6650. KMP_SET_THREAD_STATE(IMPLICIT_TASK);
  6651. #endif
  6652. rc = __kmp_invoke_microtask((microtask_t)TCR_SYNC_PTR(team->t.t_pkfn), gtid,
  6653. tid, (int)team->t.t_argc, (void **)team->t.t_argv
  6654. #if OMPT_SUPPORT
  6655. ,
  6656. exit_frame_p
  6657. #endif
  6658. );
  6659. #if OMPT_SUPPORT
  6660. *exit_frame_p = NULL;
  6661. this_thr->th.ompt_thread_info.parallel_flags |= ompt_parallel_team;
  6662. #endif
  6663. #if KMP_STATS_ENABLED
  6664. if (previous_state == stats_state_e::TEAMS_REGION) {
  6665. KMP_SET_THREAD_STATE(previous_state);
  6666. }
  6667. KMP_POP_PARTITIONED_TIMER();
  6668. #endif
  6669. #if USE_ITT_BUILD
  6670. if (__itt_stack_caller_create_ptr) {
  6671. // inform ittnotify about leaving user's code
  6672. if (team->t.t_stack_id != NULL) {
  6673. __kmp_itt_stack_callee_leave((__itt_caller)team->t.t_stack_id);
  6674. } else {
  6675. KMP_DEBUG_ASSERT(team->t.t_parent->t.t_stack_id != NULL);
  6676. __kmp_itt_stack_callee_leave(
  6677. (__itt_caller)team->t.t_parent->t.t_stack_id);
  6678. }
  6679. }
  6680. #endif /* USE_ITT_BUILD */
  6681. __kmp_run_after_invoked_task(gtid, tid, this_thr, team);
  6682. return rc;
  6683. }
  6684. void __kmp_teams_master(int gtid) {
  6685. // This routine is called by all primary threads in teams construct
  6686. kmp_info_t *thr = __kmp_threads[gtid];
  6687. kmp_team_t *team = thr->th.th_team;
  6688. ident_t *loc = team->t.t_ident;
  6689. thr->th.th_set_nproc = thr->th.th_teams_size.nth;
  6690. KMP_DEBUG_ASSERT(thr->th.th_teams_microtask);
  6691. KMP_DEBUG_ASSERT(thr->th.th_set_nproc);
  6692. KA_TRACE(20, ("__kmp_teams_master: T#%d, Tid %d, microtask %p\n", gtid,
  6693. __kmp_tid_from_gtid(gtid), thr->th.th_teams_microtask));
  6694. // This thread is a new CG root. Set up the proper variables.
  6695. kmp_cg_root_t *tmp = (kmp_cg_root_t *)__kmp_allocate(sizeof(kmp_cg_root_t));
  6696. tmp->cg_root = thr; // Make thr the CG root
  6697. // Init to thread limit stored when league primary threads were forked
  6698. tmp->cg_thread_limit = thr->th.th_current_task->td_icvs.thread_limit;
  6699. tmp->cg_nthreads = 1; // Init counter to one active thread, this one
  6700. KA_TRACE(100, ("__kmp_teams_master: Thread %p created node %p and init"
  6701. " cg_nthreads to 1\n",
  6702. thr, tmp));
  6703. tmp->up = thr->th.th_cg_roots;
  6704. thr->th.th_cg_roots = tmp;
  6705. // Launch league of teams now, but not let workers execute
  6706. // (they hang on fork barrier until next parallel)
  6707. #if INCLUDE_SSC_MARKS
  6708. SSC_MARK_FORKING();
  6709. #endif
  6710. __kmp_fork_call(loc, gtid, fork_context_intel, team->t.t_argc,
  6711. (microtask_t)thr->th.th_teams_microtask, // "wrapped" task
  6712. VOLATILE_CAST(launch_t) __kmp_invoke_task_func, NULL);
  6713. #if INCLUDE_SSC_MARKS
  6714. SSC_MARK_JOINING();
  6715. #endif
  6716. // If the team size was reduced from the limit, set it to the new size
  6717. if (thr->th.th_team_nproc < thr->th.th_teams_size.nth)
  6718. thr->th.th_teams_size.nth = thr->th.th_team_nproc;
  6719. // AC: last parameter "1" eliminates join barrier which won't work because
  6720. // worker threads are in a fork barrier waiting for more parallel regions
  6721. __kmp_join_call(loc, gtid
  6722. #if OMPT_SUPPORT
  6723. ,
  6724. fork_context_intel
  6725. #endif
  6726. ,
  6727. 1);
  6728. }
  6729. int __kmp_invoke_teams_master(int gtid) {
  6730. kmp_info_t *this_thr = __kmp_threads[gtid];
  6731. kmp_team_t *team = this_thr->th.th_team;
  6732. #if KMP_DEBUG
  6733. if (!__kmp_threads[gtid]->th.th_team->t.t_serialized)
  6734. KMP_DEBUG_ASSERT((void *)__kmp_threads[gtid]->th.th_team->t.t_pkfn ==
  6735. (void *)__kmp_teams_master);
  6736. #endif
  6737. __kmp_run_before_invoked_task(gtid, 0, this_thr, team);
  6738. #if OMPT_SUPPORT
  6739. int tid = __kmp_tid_from_gtid(gtid);
  6740. ompt_data_t *task_data =
  6741. &team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data;
  6742. ompt_data_t *parallel_data = &team->t.ompt_team_info.parallel_data;
  6743. if (ompt_enabled.ompt_callback_implicit_task) {
  6744. ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
  6745. ompt_scope_begin, parallel_data, task_data, team->t.t_nproc, tid,
  6746. ompt_task_initial);
  6747. OMPT_CUR_TASK_INFO(this_thr)->thread_num = tid;
  6748. }
  6749. #endif
  6750. __kmp_teams_master(gtid);
  6751. #if OMPT_SUPPORT
  6752. this_thr->th.ompt_thread_info.parallel_flags |= ompt_parallel_league;
  6753. #endif
  6754. __kmp_run_after_invoked_task(gtid, 0, this_thr, team);
  6755. return 1;
  6756. }
  6757. /* this sets the requested number of threads for the next parallel region
  6758. encountered by this team. since this should be enclosed in the forkjoin
  6759. critical section it should avoid race conditions with asymmetrical nested
  6760. parallelism */
  6761. void __kmp_push_num_threads(ident_t *id, int gtid, int num_threads) {
  6762. kmp_info_t *thr = __kmp_threads[gtid];
  6763. if (num_threads > 0)
  6764. thr->th.th_set_nproc = num_threads;
  6765. }
  6766. static void __kmp_push_thread_limit(kmp_info_t *thr, int num_teams,
  6767. int num_threads) {
  6768. KMP_DEBUG_ASSERT(thr);
  6769. // Remember the number of threads for inner parallel regions
  6770. if (!TCR_4(__kmp_init_middle))
  6771. __kmp_middle_initialize(); // get internal globals calculated
  6772. __kmp_assign_root_init_mask();
  6773. KMP_DEBUG_ASSERT(__kmp_avail_proc);
  6774. KMP_DEBUG_ASSERT(__kmp_dflt_team_nth);
  6775. if (num_threads == 0) {
  6776. if (__kmp_teams_thread_limit > 0) {
  6777. num_threads = __kmp_teams_thread_limit;
  6778. } else {
  6779. num_threads = __kmp_avail_proc / num_teams;
  6780. }
  6781. // adjust num_threads w/o warning as it is not user setting
  6782. // num_threads = min(num_threads, nthreads-var, thread-limit-var)
  6783. // no thread_limit clause specified - do not change thread-limit-var ICV
  6784. if (num_threads > __kmp_dflt_team_nth) {
  6785. num_threads = __kmp_dflt_team_nth; // honor nthreads-var ICV
  6786. }
  6787. if (num_threads > thr->th.th_current_task->td_icvs.thread_limit) {
  6788. num_threads = thr->th.th_current_task->td_icvs.thread_limit;
  6789. } // prevent team size to exceed thread-limit-var
  6790. if (num_teams * num_threads > __kmp_teams_max_nth) {
  6791. num_threads = __kmp_teams_max_nth / num_teams;
  6792. }
  6793. if (num_threads == 0) {
  6794. num_threads = 1;
  6795. }
  6796. } else {
  6797. if (num_threads < 0) {
  6798. __kmp_msg(kmp_ms_warning, KMP_MSG(CantFormThrTeam, num_threads, 1),
  6799. __kmp_msg_null);
  6800. num_threads = 1;
  6801. }
  6802. // This thread will be the primary thread of the league primary threads
  6803. // Store new thread limit; old limit is saved in th_cg_roots list
  6804. thr->th.th_current_task->td_icvs.thread_limit = num_threads;
  6805. // num_threads = min(num_threads, nthreads-var)
  6806. if (num_threads > __kmp_dflt_team_nth) {
  6807. num_threads = __kmp_dflt_team_nth; // honor nthreads-var ICV
  6808. }
  6809. if (num_teams * num_threads > __kmp_teams_max_nth) {
  6810. int new_threads = __kmp_teams_max_nth / num_teams;
  6811. if (new_threads == 0) {
  6812. new_threads = 1;
  6813. }
  6814. if (new_threads != num_threads) {
  6815. if (!__kmp_reserve_warn) { // user asked for too many threads
  6816. __kmp_reserve_warn = 1; // conflicts with KMP_TEAMS_THREAD_LIMIT
  6817. __kmp_msg(kmp_ms_warning,
  6818. KMP_MSG(CantFormThrTeam, num_threads, new_threads),
  6819. KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null);
  6820. }
  6821. }
  6822. num_threads = new_threads;
  6823. }
  6824. }
  6825. thr->th.th_teams_size.nth = num_threads;
  6826. }
  6827. /* this sets the requested number of teams for the teams region and/or
  6828. the number of threads for the next parallel region encountered */
  6829. void __kmp_push_num_teams(ident_t *id, int gtid, int num_teams,
  6830. int num_threads) {
  6831. kmp_info_t *thr = __kmp_threads[gtid];
  6832. if (num_teams < 0) {
  6833. // OpenMP specification requires requested values to be positive,
  6834. // but people can send us any value, so we'd better check
  6835. __kmp_msg(kmp_ms_warning, KMP_MSG(NumTeamsNotPositive, num_teams, 1),
  6836. __kmp_msg_null);
  6837. num_teams = 1;
  6838. }
  6839. if (num_teams == 0) {
  6840. if (__kmp_nteams > 0) {
  6841. num_teams = __kmp_nteams;
  6842. } else {
  6843. num_teams = 1; // default number of teams is 1.
  6844. }
  6845. }
  6846. if (num_teams > __kmp_teams_max_nth) { // if too many teams requested?
  6847. if (!__kmp_reserve_warn) {
  6848. __kmp_reserve_warn = 1;
  6849. __kmp_msg(kmp_ms_warning,
  6850. KMP_MSG(CantFormThrTeam, num_teams, __kmp_teams_max_nth),
  6851. KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null);
  6852. }
  6853. num_teams = __kmp_teams_max_nth;
  6854. }
  6855. // Set number of teams (number of threads in the outer "parallel" of the
  6856. // teams)
  6857. thr->th.th_set_nproc = thr->th.th_teams_size.nteams = num_teams;
  6858. __kmp_push_thread_limit(thr, num_teams, num_threads);
  6859. }
  6860. /* This sets the requested number of teams for the teams region and/or
  6861. the number of threads for the next parallel region encountered */
  6862. void __kmp_push_num_teams_51(ident_t *id, int gtid, int num_teams_lb,
  6863. int num_teams_ub, int num_threads) {
  6864. kmp_info_t *thr = __kmp_threads[gtid];
  6865. KMP_DEBUG_ASSERT(num_teams_lb >= 0 && num_teams_ub >= 0);
  6866. KMP_DEBUG_ASSERT(num_teams_ub >= num_teams_lb);
  6867. KMP_DEBUG_ASSERT(num_threads >= 0);
  6868. if (num_teams_lb > num_teams_ub) {
  6869. __kmp_fatal(KMP_MSG(FailedToCreateTeam, num_teams_lb, num_teams_ub),
  6870. KMP_HNT(SetNewBound, __kmp_teams_max_nth), __kmp_msg_null);
  6871. }
  6872. int num_teams = 1; // defalt number of teams is 1.
  6873. if (num_teams_lb == 0 && num_teams_ub > 0)
  6874. num_teams_lb = num_teams_ub;
  6875. if (num_teams_lb == 0 && num_teams_ub == 0) { // no num_teams clause
  6876. num_teams = (__kmp_nteams > 0) ? __kmp_nteams : num_teams;
  6877. if (num_teams > __kmp_teams_max_nth) {
  6878. if (!__kmp_reserve_warn) {
  6879. __kmp_reserve_warn = 1;
  6880. __kmp_msg(kmp_ms_warning,
  6881. KMP_MSG(CantFormThrTeam, num_teams, __kmp_teams_max_nth),
  6882. KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null);
  6883. }
  6884. num_teams = __kmp_teams_max_nth;
  6885. }
  6886. } else if (num_teams_lb == num_teams_ub) { // requires exact number of teams
  6887. num_teams = num_teams_ub;
  6888. } else { // num_teams_lb <= num_teams <= num_teams_ub
  6889. if (num_threads <= 0) {
  6890. if (num_teams_ub > __kmp_teams_max_nth) {
  6891. num_teams = num_teams_lb;
  6892. } else {
  6893. num_teams = num_teams_ub;
  6894. }
  6895. } else {
  6896. num_teams = (num_threads > __kmp_teams_max_nth)
  6897. ? num_teams
  6898. : __kmp_teams_max_nth / num_threads;
  6899. if (num_teams < num_teams_lb) {
  6900. num_teams = num_teams_lb;
  6901. } else if (num_teams > num_teams_ub) {
  6902. num_teams = num_teams_ub;
  6903. }
  6904. }
  6905. }
  6906. // Set number of teams (number of threads in the outer "parallel" of the
  6907. // teams)
  6908. thr->th.th_set_nproc = thr->th.th_teams_size.nteams = num_teams;
  6909. __kmp_push_thread_limit(thr, num_teams, num_threads);
  6910. }
  6911. // Set the proc_bind var to use in the following parallel region.
  6912. void __kmp_push_proc_bind(ident_t *id, int gtid, kmp_proc_bind_t proc_bind) {
  6913. kmp_info_t *thr = __kmp_threads[gtid];
  6914. thr->th.th_set_proc_bind = proc_bind;
  6915. }
  6916. /* Launch the worker threads into the microtask. */
  6917. void __kmp_internal_fork(ident_t *id, int gtid, kmp_team_t *team) {
  6918. kmp_info_t *this_thr = __kmp_threads[gtid];
  6919. #ifdef KMP_DEBUG
  6920. int f;
  6921. #endif /* KMP_DEBUG */
  6922. KMP_DEBUG_ASSERT(team);
  6923. KMP_DEBUG_ASSERT(this_thr->th.th_team == team);
  6924. KMP_ASSERT(KMP_MASTER_GTID(gtid));
  6925. KMP_MB(); /* Flush all pending memory write invalidates. */
  6926. team->t.t_construct = 0; /* no single directives seen yet */
  6927. team->t.t_ordered.dt.t_value =
  6928. 0; /* thread 0 enters the ordered section first */
  6929. /* Reset the identifiers on the dispatch buffer */
  6930. KMP_DEBUG_ASSERT(team->t.t_disp_buffer);
  6931. if (team->t.t_max_nproc > 1) {
  6932. int i;
  6933. for (i = 0; i < __kmp_dispatch_num_buffers; ++i) {
  6934. team->t.t_disp_buffer[i].buffer_index = i;
  6935. team->t.t_disp_buffer[i].doacross_buf_idx = i;
  6936. }
  6937. } else {
  6938. team->t.t_disp_buffer[0].buffer_index = 0;
  6939. team->t.t_disp_buffer[0].doacross_buf_idx = 0;
  6940. }
  6941. KMP_MB(); /* Flush all pending memory write invalidates. */
  6942. KMP_ASSERT(this_thr->th.th_team == team);
  6943. #ifdef KMP_DEBUG
  6944. for (f = 0; f < team->t.t_nproc; f++) {
  6945. KMP_DEBUG_ASSERT(team->t.t_threads[f] &&
  6946. team->t.t_threads[f]->th.th_team_nproc == team->t.t_nproc);
  6947. }
  6948. #endif /* KMP_DEBUG */
  6949. /* release the worker threads so they may begin working */
  6950. __kmp_fork_barrier(gtid, 0);
  6951. }
  6952. void __kmp_internal_join(ident_t *id, int gtid, kmp_team_t *team) {
  6953. kmp_info_t *this_thr = __kmp_threads[gtid];
  6954. KMP_DEBUG_ASSERT(team);
  6955. KMP_DEBUG_ASSERT(this_thr->th.th_team == team);
  6956. KMP_ASSERT(KMP_MASTER_GTID(gtid));
  6957. KMP_MB(); /* Flush all pending memory write invalidates. */
  6958. /* Join barrier after fork */
  6959. #ifdef KMP_DEBUG
  6960. if (__kmp_threads[gtid] &&
  6961. __kmp_threads[gtid]->th.th_team_nproc != team->t.t_nproc) {
  6962. __kmp_printf("GTID: %d, __kmp_threads[%d]=%p\n", gtid, gtid,
  6963. __kmp_threads[gtid]);
  6964. __kmp_printf("__kmp_threads[%d]->th.th_team_nproc=%d, TEAM: %p, "
  6965. "team->t.t_nproc=%d\n",
  6966. gtid, __kmp_threads[gtid]->th.th_team_nproc, team,
  6967. team->t.t_nproc);
  6968. __kmp_print_structure();
  6969. }
  6970. KMP_DEBUG_ASSERT(__kmp_threads[gtid] &&
  6971. __kmp_threads[gtid]->th.th_team_nproc == team->t.t_nproc);
  6972. #endif /* KMP_DEBUG */
  6973. __kmp_join_barrier(gtid); /* wait for everyone */
  6974. #if OMPT_SUPPORT
  6975. if (ompt_enabled.enabled &&
  6976. this_thr->th.ompt_thread_info.state == ompt_state_wait_barrier_implicit) {
  6977. int ds_tid = this_thr->th.th_info.ds.ds_tid;
  6978. ompt_data_t *task_data = OMPT_CUR_TASK_DATA(this_thr);
  6979. this_thr->th.ompt_thread_info.state = ompt_state_overhead;
  6980. #if OMPT_OPTIONAL
  6981. void *codeptr = NULL;
  6982. if (KMP_MASTER_TID(ds_tid) &&
  6983. (ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait) ||
  6984. ompt_callbacks.ompt_callback(ompt_callback_sync_region)))
  6985. codeptr = OMPT_CUR_TEAM_INFO(this_thr)->master_return_address;
  6986. if (ompt_enabled.ompt_callback_sync_region_wait) {
  6987. ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
  6988. ompt_sync_region_barrier_implicit, ompt_scope_end, NULL, task_data,
  6989. codeptr);
  6990. }
  6991. if (ompt_enabled.ompt_callback_sync_region) {
  6992. ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
  6993. ompt_sync_region_barrier_implicit, ompt_scope_end, NULL, task_data,
  6994. codeptr);
  6995. }
  6996. #endif
  6997. if (!KMP_MASTER_TID(ds_tid) && ompt_enabled.ompt_callback_implicit_task) {
  6998. ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
  6999. ompt_scope_end, NULL, task_data, 0, ds_tid,
  7000. ompt_task_implicit); // TODO: Can this be ompt_task_initial?
  7001. }
  7002. }
  7003. #endif
  7004. KMP_MB(); /* Flush all pending memory write invalidates. */
  7005. KMP_ASSERT(this_thr->th.th_team == team);
  7006. }
  7007. /* ------------------------------------------------------------------------ */
  7008. #ifdef USE_LOAD_BALANCE
  7009. // Return the worker threads actively spinning in the hot team, if we
  7010. // are at the outermost level of parallelism. Otherwise, return 0.
  7011. static int __kmp_active_hot_team_nproc(kmp_root_t *root) {
  7012. int i;
  7013. int retval;
  7014. kmp_team_t *hot_team;
  7015. if (root->r.r_active) {
  7016. return 0;
  7017. }
  7018. hot_team = root->r.r_hot_team;
  7019. if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
  7020. return hot_team->t.t_nproc - 1; // Don't count primary thread
  7021. }
  7022. // Skip the primary thread - it is accounted for elsewhere.
  7023. retval = 0;
  7024. for (i = 1; i < hot_team->t.t_nproc; i++) {
  7025. if (hot_team->t.t_threads[i]->th.th_active) {
  7026. retval++;
  7027. }
  7028. }
  7029. return retval;
  7030. }
  7031. // Perform an automatic adjustment to the number of
  7032. // threads used by the next parallel region.
  7033. static int __kmp_load_balance_nproc(kmp_root_t *root, int set_nproc) {
  7034. int retval;
  7035. int pool_active;
  7036. int hot_team_active;
  7037. int team_curr_active;
  7038. int system_active;
  7039. KB_TRACE(20, ("__kmp_load_balance_nproc: called root:%p set_nproc:%d\n", root,
  7040. set_nproc));
  7041. KMP_DEBUG_ASSERT(root);
  7042. KMP_DEBUG_ASSERT(root->r.r_root_team->t.t_threads[0]
  7043. ->th.th_current_task->td_icvs.dynamic == TRUE);
  7044. KMP_DEBUG_ASSERT(set_nproc > 1);
  7045. if (set_nproc == 1) {
  7046. KB_TRACE(20, ("__kmp_load_balance_nproc: serial execution.\n"));
  7047. return 1;
  7048. }
  7049. // Threads that are active in the thread pool, active in the hot team for this
  7050. // particular root (if we are at the outer par level), and the currently
  7051. // executing thread (to become the primary thread) are available to add to the
  7052. // new team, but are currently contributing to the system load, and must be
  7053. // accounted for.
  7054. pool_active = __kmp_thread_pool_active_nth;
  7055. hot_team_active = __kmp_active_hot_team_nproc(root);
  7056. team_curr_active = pool_active + hot_team_active + 1;
  7057. // Check the system load.
  7058. system_active = __kmp_get_load_balance(__kmp_avail_proc + team_curr_active);
  7059. KB_TRACE(30, ("__kmp_load_balance_nproc: system active = %d pool active = %d "
  7060. "hot team active = %d\n",
  7061. system_active, pool_active, hot_team_active));
  7062. if (system_active < 0) {
  7063. // There was an error reading the necessary info from /proc, so use the
  7064. // thread limit algorithm instead. Once we set __kmp_global.g.g_dynamic_mode
  7065. // = dynamic_thread_limit, we shouldn't wind up getting back here.
  7066. __kmp_global.g.g_dynamic_mode = dynamic_thread_limit;
  7067. KMP_WARNING(CantLoadBalUsing, "KMP_DYNAMIC_MODE=thread limit");
  7068. // Make this call behave like the thread limit algorithm.
  7069. retval = __kmp_avail_proc - __kmp_nth +
  7070. (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc);
  7071. if (retval > set_nproc) {
  7072. retval = set_nproc;
  7073. }
  7074. if (retval < KMP_MIN_NTH) {
  7075. retval = KMP_MIN_NTH;
  7076. }
  7077. KB_TRACE(20, ("__kmp_load_balance_nproc: thread limit exit. retval:%d\n",
  7078. retval));
  7079. return retval;
  7080. }
  7081. // There is a slight delay in the load balance algorithm in detecting new
  7082. // running procs. The real system load at this instant should be at least as
  7083. // large as the #active omp thread that are available to add to the team.
  7084. if (system_active < team_curr_active) {
  7085. system_active = team_curr_active;
  7086. }
  7087. retval = __kmp_avail_proc - system_active + team_curr_active;
  7088. if (retval > set_nproc) {
  7089. retval = set_nproc;
  7090. }
  7091. if (retval < KMP_MIN_NTH) {
  7092. retval = KMP_MIN_NTH;
  7093. }
  7094. KB_TRACE(20, ("__kmp_load_balance_nproc: exit. retval:%d\n", retval));
  7095. return retval;
  7096. } // __kmp_load_balance_nproc()
  7097. #endif /* USE_LOAD_BALANCE */
  7098. /* ------------------------------------------------------------------------ */
  7099. /* NOTE: this is called with the __kmp_init_lock held */
  7100. void __kmp_cleanup(void) {
  7101. int f;
  7102. KA_TRACE(10, ("__kmp_cleanup: enter\n"));
  7103. if (TCR_4(__kmp_init_parallel)) {
  7104. #if KMP_HANDLE_SIGNALS
  7105. __kmp_remove_signals();
  7106. #endif
  7107. TCW_4(__kmp_init_parallel, FALSE);
  7108. }
  7109. if (TCR_4(__kmp_init_middle)) {
  7110. #if KMP_AFFINITY_SUPPORTED
  7111. __kmp_affinity_uninitialize();
  7112. #endif /* KMP_AFFINITY_SUPPORTED */
  7113. __kmp_cleanup_hierarchy();
  7114. TCW_4(__kmp_init_middle, FALSE);
  7115. }
  7116. KA_TRACE(10, ("__kmp_cleanup: go serial cleanup\n"));
  7117. if (__kmp_init_serial) {
  7118. __kmp_runtime_destroy();
  7119. __kmp_init_serial = FALSE;
  7120. }
  7121. __kmp_cleanup_threadprivate_caches();
  7122. for (f = 0; f < __kmp_threads_capacity; f++) {
  7123. if (__kmp_root[f] != NULL) {
  7124. __kmp_free(__kmp_root[f]);
  7125. __kmp_root[f] = NULL;
  7126. }
  7127. }
  7128. __kmp_free(__kmp_threads);
  7129. // __kmp_threads and __kmp_root were allocated at once, as single block, so
  7130. // there is no need in freeing __kmp_root.
  7131. __kmp_threads = NULL;
  7132. __kmp_root = NULL;
  7133. __kmp_threads_capacity = 0;
  7134. // Free old __kmp_threads arrays if they exist.
  7135. kmp_old_threads_list_t *ptr = __kmp_old_threads_list;
  7136. while (ptr) {
  7137. kmp_old_threads_list_t *next = ptr->next;
  7138. __kmp_free(ptr->threads);
  7139. __kmp_free(ptr);
  7140. ptr = next;
  7141. }
  7142. #if KMP_USE_DYNAMIC_LOCK
  7143. __kmp_cleanup_indirect_user_locks();
  7144. #else
  7145. __kmp_cleanup_user_locks();
  7146. #endif
  7147. #if OMPD_SUPPORT
  7148. if (ompd_state) {
  7149. __kmp_free(ompd_env_block);
  7150. ompd_env_block = NULL;
  7151. ompd_env_block_size = 0;
  7152. }
  7153. #endif
  7154. #if KMP_AFFINITY_SUPPORTED
  7155. KMP_INTERNAL_FREE(CCAST(char *, __kmp_cpuinfo_file));
  7156. __kmp_cpuinfo_file = NULL;
  7157. #endif /* KMP_AFFINITY_SUPPORTED */
  7158. #if KMP_USE_ADAPTIVE_LOCKS
  7159. #if KMP_DEBUG_ADAPTIVE_LOCKS
  7160. __kmp_print_speculative_stats();
  7161. #endif
  7162. #endif
  7163. KMP_INTERNAL_FREE(__kmp_nested_nth.nth);
  7164. __kmp_nested_nth.nth = NULL;
  7165. __kmp_nested_nth.size = 0;
  7166. __kmp_nested_nth.used = 0;
  7167. KMP_INTERNAL_FREE(__kmp_nested_proc_bind.bind_types);
  7168. __kmp_nested_proc_bind.bind_types = NULL;
  7169. __kmp_nested_proc_bind.size = 0;
  7170. __kmp_nested_proc_bind.used = 0;
  7171. if (__kmp_affinity_format) {
  7172. KMP_INTERNAL_FREE(__kmp_affinity_format);
  7173. __kmp_affinity_format = NULL;
  7174. }
  7175. __kmp_i18n_catclose();
  7176. #if KMP_USE_HIER_SCHED
  7177. __kmp_hier_scheds.deallocate();
  7178. #endif
  7179. #if KMP_STATS_ENABLED
  7180. __kmp_stats_fini();
  7181. #endif
  7182. KA_TRACE(10, ("__kmp_cleanup: exit\n"));
  7183. }
  7184. /* ------------------------------------------------------------------------ */
  7185. int __kmp_ignore_mppbeg(void) {
  7186. char *env;
  7187. if ((env = getenv("KMP_IGNORE_MPPBEG")) != NULL) {
  7188. if (__kmp_str_match_false(env))
  7189. return FALSE;
  7190. }
  7191. // By default __kmpc_begin() is no-op.
  7192. return TRUE;
  7193. }
  7194. int __kmp_ignore_mppend(void) {
  7195. char *env;
  7196. if ((env = getenv("KMP_IGNORE_MPPEND")) != NULL) {
  7197. if (__kmp_str_match_false(env))
  7198. return FALSE;
  7199. }
  7200. // By default __kmpc_end() is no-op.
  7201. return TRUE;
  7202. }
  7203. void __kmp_internal_begin(void) {
  7204. int gtid;
  7205. kmp_root_t *root;
  7206. /* this is a very important step as it will register new sibling threads
  7207. and assign these new uber threads a new gtid */
  7208. gtid = __kmp_entry_gtid();
  7209. root = __kmp_threads[gtid]->th.th_root;
  7210. KMP_ASSERT(KMP_UBER_GTID(gtid));
  7211. if (root->r.r_begin)
  7212. return;
  7213. __kmp_acquire_lock(&root->r.r_begin_lock, gtid);
  7214. if (root->r.r_begin) {
  7215. __kmp_release_lock(&root->r.r_begin_lock, gtid);
  7216. return;
  7217. }
  7218. root->r.r_begin = TRUE;
  7219. __kmp_release_lock(&root->r.r_begin_lock, gtid);
  7220. }
  7221. /* ------------------------------------------------------------------------ */
  7222. void __kmp_user_set_library(enum library_type arg) {
  7223. int gtid;
  7224. kmp_root_t *root;
  7225. kmp_info_t *thread;
  7226. /* first, make sure we are initialized so we can get our gtid */
  7227. gtid = __kmp_entry_gtid();
  7228. thread = __kmp_threads[gtid];
  7229. root = thread->th.th_root;
  7230. KA_TRACE(20, ("__kmp_user_set_library: enter T#%d, arg: %d, %d\n", gtid, arg,
  7231. library_serial));
  7232. if (root->r.r_in_parallel) { /* Must be called in serial section of top-level
  7233. thread */
  7234. KMP_WARNING(SetLibraryIncorrectCall);
  7235. return;
  7236. }
  7237. switch (arg) {
  7238. case library_serial:
  7239. thread->th.th_set_nproc = 0;
  7240. set__nproc(thread, 1);
  7241. break;
  7242. case library_turnaround:
  7243. thread->th.th_set_nproc = 0;
  7244. set__nproc(thread, __kmp_dflt_team_nth ? __kmp_dflt_team_nth
  7245. : __kmp_dflt_team_nth_ub);
  7246. break;
  7247. case library_throughput:
  7248. thread->th.th_set_nproc = 0;
  7249. set__nproc(thread, __kmp_dflt_team_nth ? __kmp_dflt_team_nth
  7250. : __kmp_dflt_team_nth_ub);
  7251. break;
  7252. default:
  7253. KMP_FATAL(UnknownLibraryType, arg);
  7254. }
  7255. __kmp_aux_set_library(arg);
  7256. }
  7257. void __kmp_aux_set_stacksize(size_t arg) {
  7258. if (!__kmp_init_serial)
  7259. __kmp_serial_initialize();
  7260. #if KMP_OS_DARWIN
  7261. if (arg & (0x1000 - 1)) {
  7262. arg &= ~(0x1000 - 1);
  7263. if (arg + 0x1000) /* check for overflow if we round up */
  7264. arg += 0x1000;
  7265. }
  7266. #endif
  7267. __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
  7268. /* only change the default stacksize before the first parallel region */
  7269. if (!TCR_4(__kmp_init_parallel)) {
  7270. size_t value = arg; /* argument is in bytes */
  7271. if (value < __kmp_sys_min_stksize)
  7272. value = __kmp_sys_min_stksize;
  7273. else if (value > KMP_MAX_STKSIZE)
  7274. value = KMP_MAX_STKSIZE;
  7275. __kmp_stksize = value;
  7276. __kmp_env_stksize = TRUE; /* was KMP_STACKSIZE specified? */
  7277. }
  7278. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  7279. }
  7280. /* set the behaviour of the runtime library */
  7281. /* TODO this can cause some odd behaviour with sibling parallelism... */
  7282. void __kmp_aux_set_library(enum library_type arg) {
  7283. __kmp_library = arg;
  7284. switch (__kmp_library) {
  7285. case library_serial: {
  7286. KMP_INFORM(LibraryIsSerial);
  7287. } break;
  7288. case library_turnaround:
  7289. if (__kmp_use_yield == 1 && !__kmp_use_yield_exp_set)
  7290. __kmp_use_yield = 2; // only yield when oversubscribed
  7291. break;
  7292. case library_throughput:
  7293. if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME)
  7294. __kmp_dflt_blocktime = KMP_DEFAULT_BLOCKTIME;
  7295. break;
  7296. default:
  7297. KMP_FATAL(UnknownLibraryType, arg);
  7298. }
  7299. }
  7300. /* Getting team information common for all team API */
  7301. // Returns NULL if not in teams construct
  7302. static kmp_team_t *__kmp_aux_get_team_info(int &teams_serialized) {
  7303. kmp_info_t *thr = __kmp_entry_thread();
  7304. teams_serialized = 0;
  7305. if (thr->th.th_teams_microtask) {
  7306. kmp_team_t *team = thr->th.th_team;
  7307. int tlevel = thr->th.th_teams_level; // the level of the teams construct
  7308. int ii = team->t.t_level;
  7309. teams_serialized = team->t.t_serialized;
  7310. int level = tlevel + 1;
  7311. KMP_DEBUG_ASSERT(ii >= tlevel);
  7312. while (ii > level) {
  7313. for (teams_serialized = team->t.t_serialized;
  7314. (teams_serialized > 0) && (ii > level); teams_serialized--, ii--) {
  7315. }
  7316. if (team->t.t_serialized && (!teams_serialized)) {
  7317. team = team->t.t_parent;
  7318. continue;
  7319. }
  7320. if (ii > level) {
  7321. team = team->t.t_parent;
  7322. ii--;
  7323. }
  7324. }
  7325. return team;
  7326. }
  7327. return NULL;
  7328. }
  7329. int __kmp_aux_get_team_num() {
  7330. int serialized;
  7331. kmp_team_t *team = __kmp_aux_get_team_info(serialized);
  7332. if (team) {
  7333. if (serialized > 1) {
  7334. return 0; // teams region is serialized ( 1 team of 1 thread ).
  7335. } else {
  7336. return team->t.t_master_tid;
  7337. }
  7338. }
  7339. return 0;
  7340. }
  7341. int __kmp_aux_get_num_teams() {
  7342. int serialized;
  7343. kmp_team_t *team = __kmp_aux_get_team_info(serialized);
  7344. if (team) {
  7345. if (serialized > 1) {
  7346. return 1;
  7347. } else {
  7348. return team->t.t_parent->t.t_nproc;
  7349. }
  7350. }
  7351. return 1;
  7352. }
  7353. /* ------------------------------------------------------------------------ */
  7354. /*
  7355. * Affinity Format Parser
  7356. *
  7357. * Field is in form of: %[[[0].]size]type
  7358. * % and type are required (%% means print a literal '%')
  7359. * type is either single char or long name surrounded by {},
  7360. * e.g., N or {num_threads}
  7361. * 0 => leading zeros
  7362. * . => right justified when size is specified
  7363. * by default output is left justified
  7364. * size is the *minimum* field length
  7365. * All other characters are printed as is
  7366. *
  7367. * Available field types:
  7368. * L {thread_level} - omp_get_level()
  7369. * n {thread_num} - omp_get_thread_num()
  7370. * h {host} - name of host machine
  7371. * P {process_id} - process id (integer)
  7372. * T {thread_identifier} - native thread identifier (integer)
  7373. * N {num_threads} - omp_get_num_threads()
  7374. * A {ancestor_tnum} - omp_get_ancestor_thread_num(omp_get_level()-1)
  7375. * a {thread_affinity} - comma separated list of integers or integer ranges
  7376. * (values of affinity mask)
  7377. *
  7378. * Implementation-specific field types can be added
  7379. * If a type is unknown, print "undefined"
  7380. */
  7381. // Structure holding the short name, long name, and corresponding data type
  7382. // for snprintf. A table of these will represent the entire valid keyword
  7383. // field types.
  7384. typedef struct kmp_affinity_format_field_t {
  7385. char short_name; // from spec e.g., L -> thread level
  7386. const char *long_name; // from spec thread_level -> thread level
  7387. char field_format; // data type for snprintf (typically 'd' or 's'
  7388. // for integer or string)
  7389. } kmp_affinity_format_field_t;
  7390. static const kmp_affinity_format_field_t __kmp_affinity_format_table[] = {
  7391. #if KMP_AFFINITY_SUPPORTED
  7392. {'A', "thread_affinity", 's'},
  7393. #endif
  7394. {'t', "team_num", 'd'},
  7395. {'T', "num_teams", 'd'},
  7396. {'L', "nesting_level", 'd'},
  7397. {'n', "thread_num", 'd'},
  7398. {'N', "num_threads", 'd'},
  7399. {'a', "ancestor_tnum", 'd'},
  7400. {'H', "host", 's'},
  7401. {'P', "process_id", 'd'},
  7402. {'i', "native_thread_id", 'd'}};
  7403. // Return the number of characters it takes to hold field
  7404. static int __kmp_aux_capture_affinity_field(int gtid, const kmp_info_t *th,
  7405. const char **ptr,
  7406. kmp_str_buf_t *field_buffer) {
  7407. int rc, format_index, field_value;
  7408. const char *width_left, *width_right;
  7409. bool pad_zeros, right_justify, parse_long_name, found_valid_name;
  7410. static const int FORMAT_SIZE = 20;
  7411. char format[FORMAT_SIZE] = {0};
  7412. char absolute_short_name = 0;
  7413. KMP_DEBUG_ASSERT(gtid >= 0);
  7414. KMP_DEBUG_ASSERT(th);
  7415. KMP_DEBUG_ASSERT(**ptr == '%');
  7416. KMP_DEBUG_ASSERT(field_buffer);
  7417. __kmp_str_buf_clear(field_buffer);
  7418. // Skip the initial %
  7419. (*ptr)++;
  7420. // Check for %% first
  7421. if (**ptr == '%') {
  7422. __kmp_str_buf_cat(field_buffer, "%", 1);
  7423. (*ptr)++; // skip over the second %
  7424. return 1;
  7425. }
  7426. // Parse field modifiers if they are present
  7427. pad_zeros = false;
  7428. if (**ptr == '0') {
  7429. pad_zeros = true;
  7430. (*ptr)++; // skip over 0
  7431. }
  7432. right_justify = false;
  7433. if (**ptr == '.') {
  7434. right_justify = true;
  7435. (*ptr)++; // skip over .
  7436. }
  7437. // Parse width of field: [width_left, width_right)
  7438. width_left = width_right = NULL;
  7439. if (**ptr >= '0' && **ptr <= '9') {
  7440. width_left = *ptr;
  7441. SKIP_DIGITS(*ptr);
  7442. width_right = *ptr;
  7443. }
  7444. // Create the format for KMP_SNPRINTF based on flags parsed above
  7445. format_index = 0;
  7446. format[format_index++] = '%';
  7447. if (!right_justify)
  7448. format[format_index++] = '-';
  7449. if (pad_zeros)
  7450. format[format_index++] = '0';
  7451. if (width_left && width_right) {
  7452. int i = 0;
  7453. // Only allow 8 digit number widths.
  7454. // This also prevents overflowing format variable
  7455. while (i < 8 && width_left < width_right) {
  7456. format[format_index++] = *width_left;
  7457. width_left++;
  7458. i++;
  7459. }
  7460. }
  7461. // Parse a name (long or short)
  7462. // Canonicalize the name into absolute_short_name
  7463. found_valid_name = false;
  7464. parse_long_name = (**ptr == '{');
  7465. if (parse_long_name)
  7466. (*ptr)++; // skip initial left brace
  7467. for (size_t i = 0; i < sizeof(__kmp_affinity_format_table) /
  7468. sizeof(__kmp_affinity_format_table[0]);
  7469. ++i) {
  7470. char short_name = __kmp_affinity_format_table[i].short_name;
  7471. const char *long_name = __kmp_affinity_format_table[i].long_name;
  7472. char field_format = __kmp_affinity_format_table[i].field_format;
  7473. if (parse_long_name) {
  7474. size_t length = KMP_STRLEN(long_name);
  7475. if (strncmp(*ptr, long_name, length) == 0) {
  7476. found_valid_name = true;
  7477. (*ptr) += length; // skip the long name
  7478. }
  7479. } else if (**ptr == short_name) {
  7480. found_valid_name = true;
  7481. (*ptr)++; // skip the short name
  7482. }
  7483. if (found_valid_name) {
  7484. format[format_index++] = field_format;
  7485. format[format_index++] = '\0';
  7486. absolute_short_name = short_name;
  7487. break;
  7488. }
  7489. }
  7490. if (parse_long_name) {
  7491. if (**ptr != '}') {
  7492. absolute_short_name = 0;
  7493. } else {
  7494. (*ptr)++; // skip over the right brace
  7495. }
  7496. }
  7497. // Attempt to fill the buffer with the requested
  7498. // value using snprintf within __kmp_str_buf_print()
  7499. switch (absolute_short_name) {
  7500. case 't':
  7501. rc = __kmp_str_buf_print(field_buffer, format, __kmp_aux_get_team_num());
  7502. break;
  7503. case 'T':
  7504. rc = __kmp_str_buf_print(field_buffer, format, __kmp_aux_get_num_teams());
  7505. break;
  7506. case 'L':
  7507. rc = __kmp_str_buf_print(field_buffer, format, th->th.th_team->t.t_level);
  7508. break;
  7509. case 'n':
  7510. rc = __kmp_str_buf_print(field_buffer, format, __kmp_tid_from_gtid(gtid));
  7511. break;
  7512. case 'H': {
  7513. static const int BUFFER_SIZE = 256;
  7514. char buf[BUFFER_SIZE];
  7515. __kmp_expand_host_name(buf, BUFFER_SIZE);
  7516. rc = __kmp_str_buf_print(field_buffer, format, buf);
  7517. } break;
  7518. case 'P':
  7519. rc = __kmp_str_buf_print(field_buffer, format, getpid());
  7520. break;
  7521. case 'i':
  7522. rc = __kmp_str_buf_print(field_buffer, format, __kmp_gettid());
  7523. break;
  7524. case 'N':
  7525. rc = __kmp_str_buf_print(field_buffer, format, th->th.th_team->t.t_nproc);
  7526. break;
  7527. case 'a':
  7528. field_value =
  7529. __kmp_get_ancestor_thread_num(gtid, th->th.th_team->t.t_level - 1);
  7530. rc = __kmp_str_buf_print(field_buffer, format, field_value);
  7531. break;
  7532. #if KMP_AFFINITY_SUPPORTED
  7533. case 'A': {
  7534. kmp_str_buf_t buf;
  7535. __kmp_str_buf_init(&buf);
  7536. __kmp_affinity_str_buf_mask(&buf, th->th.th_affin_mask);
  7537. rc = __kmp_str_buf_print(field_buffer, format, buf.str);
  7538. __kmp_str_buf_free(&buf);
  7539. } break;
  7540. #endif
  7541. default:
  7542. // According to spec, If an implementation does not have info for field
  7543. // type, then "undefined" is printed
  7544. rc = __kmp_str_buf_print(field_buffer, "%s", "undefined");
  7545. // Skip the field
  7546. if (parse_long_name) {
  7547. SKIP_TOKEN(*ptr);
  7548. if (**ptr == '}')
  7549. (*ptr)++;
  7550. } else {
  7551. (*ptr)++;
  7552. }
  7553. }
  7554. KMP_ASSERT(format_index <= FORMAT_SIZE);
  7555. return rc;
  7556. }
  7557. /*
  7558. * Return number of characters needed to hold the affinity string
  7559. * (not including null byte character)
  7560. * The resultant string is printed to buffer, which the caller can then
  7561. * handle afterwards
  7562. */
  7563. size_t __kmp_aux_capture_affinity(int gtid, const char *format,
  7564. kmp_str_buf_t *buffer) {
  7565. const char *parse_ptr;
  7566. size_t retval;
  7567. const kmp_info_t *th;
  7568. kmp_str_buf_t field;
  7569. KMP_DEBUG_ASSERT(buffer);
  7570. KMP_DEBUG_ASSERT(gtid >= 0);
  7571. __kmp_str_buf_init(&field);
  7572. __kmp_str_buf_clear(buffer);
  7573. th = __kmp_threads[gtid];
  7574. retval = 0;
  7575. // If format is NULL or zero-length string, then we use
  7576. // affinity-format-var ICV
  7577. parse_ptr = format;
  7578. if (parse_ptr == NULL || *parse_ptr == '\0') {
  7579. parse_ptr = __kmp_affinity_format;
  7580. }
  7581. KMP_DEBUG_ASSERT(parse_ptr);
  7582. while (*parse_ptr != '\0') {
  7583. // Parse a field
  7584. if (*parse_ptr == '%') {
  7585. // Put field in the buffer
  7586. int rc = __kmp_aux_capture_affinity_field(gtid, th, &parse_ptr, &field);
  7587. __kmp_str_buf_catbuf(buffer, &field);
  7588. retval += rc;
  7589. } else {
  7590. // Put literal character in buffer
  7591. __kmp_str_buf_cat(buffer, parse_ptr, 1);
  7592. retval++;
  7593. parse_ptr++;
  7594. }
  7595. }
  7596. __kmp_str_buf_free(&field);
  7597. return retval;
  7598. }
  7599. // Displays the affinity string to stdout
  7600. void __kmp_aux_display_affinity(int gtid, const char *format) {
  7601. kmp_str_buf_t buf;
  7602. __kmp_str_buf_init(&buf);
  7603. __kmp_aux_capture_affinity(gtid, format, &buf);
  7604. __kmp_fprintf(kmp_out, "%s" KMP_END_OF_LINE, buf.str);
  7605. __kmp_str_buf_free(&buf);
  7606. }
  7607. /* ------------------------------------------------------------------------ */
  7608. void __kmp_aux_set_blocktime(int arg, kmp_info_t *thread, int tid) {
  7609. int blocktime = arg; /* argument is in milliseconds */
  7610. #if KMP_USE_MONITOR
  7611. int bt_intervals;
  7612. #endif
  7613. kmp_int8 bt_set;
  7614. __kmp_save_internal_controls(thread);
  7615. /* Normalize and set blocktime for the teams */
  7616. if (blocktime < KMP_MIN_BLOCKTIME)
  7617. blocktime = KMP_MIN_BLOCKTIME;
  7618. else if (blocktime > KMP_MAX_BLOCKTIME)
  7619. blocktime = KMP_MAX_BLOCKTIME;
  7620. set__blocktime_team(thread->th.th_team, tid, blocktime);
  7621. set__blocktime_team(thread->th.th_serial_team, 0, blocktime);
  7622. #if KMP_USE_MONITOR
  7623. /* Calculate and set blocktime intervals for the teams */
  7624. bt_intervals = KMP_INTERVALS_FROM_BLOCKTIME(blocktime, __kmp_monitor_wakeups);
  7625. set__bt_intervals_team(thread->th.th_team, tid, bt_intervals);
  7626. set__bt_intervals_team(thread->th.th_serial_team, 0, bt_intervals);
  7627. #endif
  7628. /* Set whether blocktime has been set to "TRUE" */
  7629. bt_set = TRUE;
  7630. set__bt_set_team(thread->th.th_team, tid, bt_set);
  7631. set__bt_set_team(thread->th.th_serial_team, 0, bt_set);
  7632. #if KMP_USE_MONITOR
  7633. KF_TRACE(10, ("kmp_set_blocktime: T#%d(%d:%d), blocktime=%d, "
  7634. "bt_intervals=%d, monitor_updates=%d\n",
  7635. __kmp_gtid_from_tid(tid, thread->th.th_team),
  7636. thread->th.th_team->t.t_id, tid, blocktime, bt_intervals,
  7637. __kmp_monitor_wakeups));
  7638. #else
  7639. KF_TRACE(10, ("kmp_set_blocktime: T#%d(%d:%d), blocktime=%d\n",
  7640. __kmp_gtid_from_tid(tid, thread->th.th_team),
  7641. thread->th.th_team->t.t_id, tid, blocktime));
  7642. #endif
  7643. }
  7644. void __kmp_aux_set_defaults(char const *str, size_t len) {
  7645. if (!__kmp_init_serial) {
  7646. __kmp_serial_initialize();
  7647. }
  7648. __kmp_env_initialize(str);
  7649. if (__kmp_settings || __kmp_display_env || __kmp_display_env_verbose) {
  7650. __kmp_env_print();
  7651. }
  7652. } // __kmp_aux_set_defaults
  7653. /* ------------------------------------------------------------------------ */
  7654. /* internal fast reduction routines */
  7655. PACKED_REDUCTION_METHOD_T
  7656. __kmp_determine_reduction_method(
  7657. ident_t *loc, kmp_int32 global_tid, kmp_int32 num_vars, size_t reduce_size,
  7658. void *reduce_data, void (*reduce_func)(void *lhs_data, void *rhs_data),
  7659. kmp_critical_name *lck) {
  7660. // Default reduction method: critical construct ( lck != NULL, like in current
  7661. // PAROPT )
  7662. // If ( reduce_data!=NULL && reduce_func!=NULL ): the tree-reduction method
  7663. // can be selected by RTL
  7664. // If loc->flags contains KMP_IDENT_ATOMIC_REDUCE, the atomic reduce method
  7665. // can be selected by RTL
  7666. // Finally, it's up to OpenMP RTL to make a decision on which method to select
  7667. // among generated by PAROPT.
  7668. PACKED_REDUCTION_METHOD_T retval;
  7669. int team_size;
  7670. KMP_DEBUG_ASSERT(loc); // it would be nice to test ( loc != 0 )
  7671. KMP_DEBUG_ASSERT(lck); // it would be nice to test ( lck != 0 )
  7672. #define FAST_REDUCTION_ATOMIC_METHOD_GENERATED \
  7673. (loc && \
  7674. ((loc->flags & (KMP_IDENT_ATOMIC_REDUCE)) == (KMP_IDENT_ATOMIC_REDUCE)))
  7675. #define FAST_REDUCTION_TREE_METHOD_GENERATED ((reduce_data) && (reduce_func))
  7676. retval = critical_reduce_block;
  7677. // another choice of getting a team size (with 1 dynamic deference) is slower
  7678. team_size = __kmp_get_team_num_threads(global_tid);
  7679. if (team_size == 1) {
  7680. retval = empty_reduce_block;
  7681. } else {
  7682. int atomic_available = FAST_REDUCTION_ATOMIC_METHOD_GENERATED;
  7683. #if KMP_ARCH_X86_64 || KMP_ARCH_PPC64 || KMP_ARCH_AARCH64 || \
  7684. KMP_ARCH_MIPS64 || KMP_ARCH_RISCV64
  7685. #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
  7686. KMP_OS_OPENBSD || KMP_OS_WINDOWS || KMP_OS_DARWIN || KMP_OS_HURD
  7687. int teamsize_cutoff = 4;
  7688. #if KMP_MIC_SUPPORTED
  7689. if (__kmp_mic_type != non_mic) {
  7690. teamsize_cutoff = 8;
  7691. }
  7692. #endif
  7693. int tree_available = FAST_REDUCTION_TREE_METHOD_GENERATED;
  7694. if (tree_available) {
  7695. if (team_size <= teamsize_cutoff) {
  7696. if (atomic_available) {
  7697. retval = atomic_reduce_block;
  7698. }
  7699. } else {
  7700. retval = TREE_REDUCE_BLOCK_WITH_REDUCTION_BARRIER;
  7701. }
  7702. } else if (atomic_available) {
  7703. retval = atomic_reduce_block;
  7704. }
  7705. #else
  7706. #error "Unknown or unsupported OS"
  7707. #endif // KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD ||
  7708. // KMP_OS_OPENBSD || KMP_OS_WINDOWS || KMP_OS_DARWIN || KMP_OS_HURD
  7709. #elif KMP_ARCH_X86 || KMP_ARCH_ARM || KMP_ARCH_AARCH || KMP_ARCH_MIPS
  7710. #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_WINDOWS || KMP_OS_HURD
  7711. // basic tuning
  7712. if (atomic_available) {
  7713. if (num_vars <= 2) { // && ( team_size <= 8 ) due to false-sharing ???
  7714. retval = atomic_reduce_block;
  7715. }
  7716. } // otherwise: use critical section
  7717. #elif KMP_OS_DARWIN
  7718. int tree_available = FAST_REDUCTION_TREE_METHOD_GENERATED;
  7719. if (atomic_available && (num_vars <= 3)) {
  7720. retval = atomic_reduce_block;
  7721. } else if (tree_available) {
  7722. if ((reduce_size > (9 * sizeof(kmp_real64))) &&
  7723. (reduce_size < (2000 * sizeof(kmp_real64)))) {
  7724. retval = TREE_REDUCE_BLOCK_WITH_PLAIN_BARRIER;
  7725. }
  7726. } // otherwise: use critical section
  7727. #else
  7728. #error "Unknown or unsupported OS"
  7729. #endif
  7730. #else
  7731. #error "Unknown or unsupported architecture"
  7732. #endif
  7733. }
  7734. // KMP_FORCE_REDUCTION
  7735. // If the team is serialized (team_size == 1), ignore the forced reduction
  7736. // method and stay with the unsynchronized method (empty_reduce_block)
  7737. if (__kmp_force_reduction_method != reduction_method_not_defined &&
  7738. team_size != 1) {
  7739. PACKED_REDUCTION_METHOD_T forced_retval = critical_reduce_block;
  7740. int atomic_available, tree_available;
  7741. switch ((forced_retval = __kmp_force_reduction_method)) {
  7742. case critical_reduce_block:
  7743. KMP_ASSERT(lck); // lck should be != 0
  7744. break;
  7745. case atomic_reduce_block:
  7746. atomic_available = FAST_REDUCTION_ATOMIC_METHOD_GENERATED;
  7747. if (!atomic_available) {
  7748. KMP_WARNING(RedMethodNotSupported, "atomic");
  7749. forced_retval = critical_reduce_block;
  7750. }
  7751. break;
  7752. case tree_reduce_block:
  7753. tree_available = FAST_REDUCTION_TREE_METHOD_GENERATED;
  7754. if (!tree_available) {
  7755. KMP_WARNING(RedMethodNotSupported, "tree");
  7756. forced_retval = critical_reduce_block;
  7757. } else {
  7758. #if KMP_FAST_REDUCTION_BARRIER
  7759. forced_retval = TREE_REDUCE_BLOCK_WITH_REDUCTION_BARRIER;
  7760. #endif
  7761. }
  7762. break;
  7763. default:
  7764. KMP_ASSERT(0); // "unsupported method specified"
  7765. }
  7766. retval = forced_retval;
  7767. }
  7768. KA_TRACE(10, ("reduction method selected=%08x\n", retval));
  7769. #undef FAST_REDUCTION_TREE_METHOD_GENERATED
  7770. #undef FAST_REDUCTION_ATOMIC_METHOD_GENERATED
  7771. return (retval);
  7772. }
  7773. // this function is for testing set/get/determine reduce method
  7774. kmp_int32 __kmp_get_reduce_method(void) {
  7775. return ((__kmp_entry_thread()->th.th_local.packed_reduction_method) >> 8);
  7776. }
  7777. // Soft pause sets up threads to ignore blocktime and just go to sleep.
  7778. // Spin-wait code checks __kmp_pause_status and reacts accordingly.
  7779. void __kmp_soft_pause() { __kmp_pause_status = kmp_soft_paused; }
  7780. // Hard pause shuts down the runtime completely. Resume happens naturally when
  7781. // OpenMP is used subsequently.
  7782. void __kmp_hard_pause() {
  7783. __kmp_pause_status = kmp_hard_paused;
  7784. __kmp_internal_end_thread(-1);
  7785. }
  7786. // Soft resume sets __kmp_pause_status, and wakes up all threads.
  7787. void __kmp_resume_if_soft_paused() {
  7788. if (__kmp_pause_status == kmp_soft_paused) {
  7789. __kmp_pause_status = kmp_not_paused;
  7790. for (int gtid = 1; gtid < __kmp_threads_capacity; ++gtid) {
  7791. kmp_info_t *thread = __kmp_threads[gtid];
  7792. if (thread) { // Wake it if sleeping
  7793. kmp_flag_64<> fl(&thread->th.th_bar[bs_forkjoin_barrier].bb.b_go,
  7794. thread);
  7795. if (fl.is_sleeping())
  7796. fl.resume(gtid);
  7797. else if (__kmp_try_suspend_mx(thread)) { // got suspend lock
  7798. __kmp_unlock_suspend_mx(thread); // unlock it; it won't sleep
  7799. } else { // thread holds the lock and may sleep soon
  7800. do { // until either the thread sleeps, or we can get the lock
  7801. if (fl.is_sleeping()) {
  7802. fl.resume(gtid);
  7803. break;
  7804. } else if (__kmp_try_suspend_mx(thread)) {
  7805. __kmp_unlock_suspend_mx(thread);
  7806. break;
  7807. }
  7808. } while (1);
  7809. }
  7810. }
  7811. }
  7812. }
  7813. }
  7814. // This function is called via __kmpc_pause_resource. Returns 0 if successful.
  7815. // TODO: add warning messages
  7816. int __kmp_pause_resource(kmp_pause_status_t level) {
  7817. if (level == kmp_not_paused) { // requesting resume
  7818. if (__kmp_pause_status == kmp_not_paused) {
  7819. // error message about runtime not being paused, so can't resume
  7820. return 1;
  7821. } else {
  7822. KMP_DEBUG_ASSERT(__kmp_pause_status == kmp_soft_paused ||
  7823. __kmp_pause_status == kmp_hard_paused);
  7824. __kmp_pause_status = kmp_not_paused;
  7825. return 0;
  7826. }
  7827. } else if (level == kmp_soft_paused) { // requesting soft pause
  7828. if (__kmp_pause_status != kmp_not_paused) {
  7829. // error message about already being paused
  7830. return 1;
  7831. } else {
  7832. __kmp_soft_pause();
  7833. return 0;
  7834. }
  7835. } else if (level == kmp_hard_paused) { // requesting hard pause
  7836. if (__kmp_pause_status != kmp_not_paused) {
  7837. // error message about already being paused
  7838. return 1;
  7839. } else {
  7840. __kmp_hard_pause();
  7841. return 0;
  7842. }
  7843. } else {
  7844. // error message about invalid level
  7845. return 1;
  7846. }
  7847. }
  7848. void __kmp_omp_display_env(int verbose) {
  7849. __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
  7850. if (__kmp_init_serial == 0)
  7851. __kmp_do_serial_initialize();
  7852. __kmp_display_env_impl(!verbose, verbose);
  7853. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  7854. }
  7855. // The team size is changing, so distributed barrier must be modified
  7856. void __kmp_resize_dist_barrier(kmp_team_t *team, int old_nthreads,
  7857. int new_nthreads) {
  7858. KMP_DEBUG_ASSERT(__kmp_barrier_release_pattern[bs_forkjoin_barrier] ==
  7859. bp_dist_bar);
  7860. kmp_info_t **other_threads = team->t.t_threads;
  7861. // We want all the workers to stop waiting on the barrier while we adjust the
  7862. // size of the team.
  7863. for (int f = 1; f < old_nthreads; ++f) {
  7864. KMP_DEBUG_ASSERT(other_threads[f] != NULL);
  7865. // Ignore threads that are already inactive or not present in the team
  7866. if (team->t.t_threads[f]->th.th_used_in_team.load() == 0) {
  7867. // teams construct causes thread_limit to get passed in, and some of
  7868. // those could be inactive; just ignore them
  7869. continue;
  7870. }
  7871. // If thread is transitioning still to in_use state, wait for it
  7872. if (team->t.t_threads[f]->th.th_used_in_team.load() == 3) {
  7873. while (team->t.t_threads[f]->th.th_used_in_team.load() == 3)
  7874. KMP_CPU_PAUSE();
  7875. }
  7876. // The thread should be in_use now
  7877. KMP_DEBUG_ASSERT(team->t.t_threads[f]->th.th_used_in_team.load() == 1);
  7878. // Transition to unused state
  7879. team->t.t_threads[f]->th.th_used_in_team.store(2);
  7880. KMP_DEBUG_ASSERT(team->t.t_threads[f]->th.th_used_in_team.load() == 2);
  7881. }
  7882. // Release all the workers
  7883. team->t.b->go_release();
  7884. KMP_MFENCE();
  7885. // Workers should see transition status 2 and move to 0; but may need to be
  7886. // woken up first
  7887. int count = old_nthreads - 1;
  7888. while (count > 0) {
  7889. count = old_nthreads - 1;
  7890. for (int f = 1; f < old_nthreads; ++f) {
  7891. if (other_threads[f]->th.th_used_in_team.load() != 0) {
  7892. if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) { // Wake up the workers
  7893. kmp_atomic_flag_64<> *flag = (kmp_atomic_flag_64<> *)CCAST(
  7894. void *, other_threads[f]->th.th_sleep_loc);
  7895. __kmp_atomic_resume_64(other_threads[f]->th.th_info.ds.ds_gtid, flag);
  7896. }
  7897. } else {
  7898. KMP_DEBUG_ASSERT(team->t.t_threads[f]->th.th_used_in_team.load() == 0);
  7899. count--;
  7900. }
  7901. }
  7902. }
  7903. // Now update the barrier size
  7904. team->t.b->update_num_threads(new_nthreads);
  7905. team->t.b->go_reset();
  7906. }
  7907. void __kmp_add_threads_to_team(kmp_team_t *team, int new_nthreads) {
  7908. // Add the threads back to the team
  7909. KMP_DEBUG_ASSERT(team);
  7910. // Threads were paused and pointed at th_used_in_team temporarily during a
  7911. // resize of the team. We're going to set th_used_in_team to 3 to indicate to
  7912. // the thread that it should transition itself back into the team. Then, if
  7913. // blocktime isn't infinite, the thread could be sleeping, so we send a resume
  7914. // to wake it up.
  7915. for (int f = 1; f < new_nthreads; ++f) {
  7916. KMP_DEBUG_ASSERT(team->t.t_threads[f]);
  7917. KMP_COMPARE_AND_STORE_ACQ32(&(team->t.t_threads[f]->th.th_used_in_team), 0,
  7918. 3);
  7919. if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) { // Wake up sleeping threads
  7920. __kmp_resume_32(team->t.t_threads[f]->th.th_info.ds.ds_gtid,
  7921. (kmp_flag_32<false, false> *)NULL);
  7922. }
  7923. }
  7924. // The threads should be transitioning to the team; when they are done, they
  7925. // should have set th_used_in_team to 1. This loop forces master to wait until
  7926. // all threads have moved into the team and are waiting in the barrier.
  7927. int count = new_nthreads - 1;
  7928. while (count > 0) {
  7929. count = new_nthreads - 1;
  7930. for (int f = 1; f < new_nthreads; ++f) {
  7931. if (team->t.t_threads[f]->th.th_used_in_team.load() == 1) {
  7932. count--;
  7933. }
  7934. }
  7935. }
  7936. }
  7937. // Globals and functions for hidden helper task
  7938. kmp_info_t **__kmp_hidden_helper_threads;
  7939. kmp_info_t *__kmp_hidden_helper_main_thread;
  7940. std::atomic<kmp_int32> __kmp_unexecuted_hidden_helper_tasks;
  7941. #if KMP_OS_LINUX
  7942. kmp_int32 __kmp_hidden_helper_threads_num = 8;
  7943. kmp_int32 __kmp_enable_hidden_helper = TRUE;
  7944. #else
  7945. kmp_int32 __kmp_hidden_helper_threads_num = 0;
  7946. kmp_int32 __kmp_enable_hidden_helper = FALSE;
  7947. #endif
  7948. namespace {
  7949. std::atomic<kmp_int32> __kmp_hit_hidden_helper_threads_num;
  7950. void __kmp_hidden_helper_wrapper_fn(int *gtid, int *, ...) {
  7951. // This is an explicit synchronization on all hidden helper threads in case
  7952. // that when a regular thread pushes a hidden helper task to one hidden
  7953. // helper thread, the thread has not been awaken once since they're released
  7954. // by the main thread after creating the team.
  7955. KMP_ATOMIC_INC(&__kmp_hit_hidden_helper_threads_num);
  7956. while (KMP_ATOMIC_LD_ACQ(&__kmp_hit_hidden_helper_threads_num) !=
  7957. __kmp_hidden_helper_threads_num)
  7958. ;
  7959. // If main thread, then wait for signal
  7960. if (__kmpc_master(nullptr, *gtid)) {
  7961. // First, unset the initial state and release the initial thread
  7962. TCW_4(__kmp_init_hidden_helper_threads, FALSE);
  7963. __kmp_hidden_helper_initz_release();
  7964. __kmp_hidden_helper_main_thread_wait();
  7965. // Now wake up all worker threads
  7966. for (int i = 1; i < __kmp_hit_hidden_helper_threads_num; ++i) {
  7967. __kmp_hidden_helper_worker_thread_signal();
  7968. }
  7969. }
  7970. }
  7971. } // namespace
  7972. void __kmp_hidden_helper_threads_initz_routine() {
  7973. // Create a new root for hidden helper team/threads
  7974. const int gtid = __kmp_register_root(TRUE);
  7975. __kmp_hidden_helper_main_thread = __kmp_threads[gtid];
  7976. __kmp_hidden_helper_threads = &__kmp_threads[gtid];
  7977. __kmp_hidden_helper_main_thread->th.th_set_nproc =
  7978. __kmp_hidden_helper_threads_num;
  7979. KMP_ATOMIC_ST_REL(&__kmp_hit_hidden_helper_threads_num, 0);
  7980. __kmpc_fork_call(nullptr, 0, __kmp_hidden_helper_wrapper_fn);
  7981. // Set the initialization flag to FALSE
  7982. TCW_SYNC_4(__kmp_init_hidden_helper, FALSE);
  7983. __kmp_hidden_helper_threads_deinitz_release();
  7984. }
  7985. /* Nesting Mode:
  7986. Set via KMP_NESTING_MODE, which takes an integer.
  7987. Note: we skip duplicate topology levels, and skip levels with only
  7988. one entity.
  7989. KMP_NESTING_MODE=0 is the default, and doesn't use nesting mode.
  7990. KMP_NESTING_MODE=1 sets as many nesting levels as there are distinct levels
  7991. in the topology, and initializes the number of threads at each of those
  7992. levels to the number of entities at each level, respectively, below the
  7993. entity at the parent level.
  7994. KMP_NESTING_MODE=N, where N>1, attempts to create up to N nesting levels,
  7995. but starts with nesting OFF -- max-active-levels-var is 1 -- and requires
  7996. the user to turn nesting on explicitly. This is an even more experimental
  7997. option to this experimental feature, and may change or go away in the
  7998. future.
  7999. */
  8000. // Allocate space to store nesting levels
  8001. void __kmp_init_nesting_mode() {
  8002. int levels = KMP_HW_LAST;
  8003. __kmp_nesting_mode_nlevels = levels;
  8004. __kmp_nesting_nth_level = (int *)KMP_INTERNAL_MALLOC(levels * sizeof(int));
  8005. for (int i = 0; i < levels; ++i)
  8006. __kmp_nesting_nth_level[i] = 0;
  8007. if (__kmp_nested_nth.size < levels) {
  8008. __kmp_nested_nth.nth =
  8009. (int *)KMP_INTERNAL_REALLOC(__kmp_nested_nth.nth, levels * sizeof(int));
  8010. __kmp_nested_nth.size = levels;
  8011. }
  8012. }
  8013. // Set # threads for top levels of nesting; must be called after topology set
  8014. void __kmp_set_nesting_mode_threads() {
  8015. kmp_info_t *thread = __kmp_threads[__kmp_entry_gtid()];
  8016. if (__kmp_nesting_mode == 1)
  8017. __kmp_nesting_mode_nlevels = KMP_MAX_ACTIVE_LEVELS_LIMIT;
  8018. else if (__kmp_nesting_mode > 1)
  8019. __kmp_nesting_mode_nlevels = __kmp_nesting_mode;
  8020. if (__kmp_topology) { // use topology info
  8021. int loc, hw_level;
  8022. for (loc = 0, hw_level = 0; hw_level < __kmp_topology->get_depth() &&
  8023. loc < __kmp_nesting_mode_nlevels;
  8024. loc++, hw_level++) {
  8025. __kmp_nesting_nth_level[loc] = __kmp_topology->get_ratio(hw_level);
  8026. if (__kmp_nesting_nth_level[loc] == 1)
  8027. loc--;
  8028. }
  8029. // Make sure all cores are used
  8030. if (__kmp_nesting_mode > 1 && loc > 1) {
  8031. int core_level = __kmp_topology->get_level(KMP_HW_CORE);
  8032. int num_cores = __kmp_topology->get_count(core_level);
  8033. int upper_levels = 1;
  8034. for (int level = 0; level < loc - 1; ++level)
  8035. upper_levels *= __kmp_nesting_nth_level[level];
  8036. if (upper_levels * __kmp_nesting_nth_level[loc - 1] < num_cores)
  8037. __kmp_nesting_nth_level[loc - 1] =
  8038. num_cores / __kmp_nesting_nth_level[loc - 2];
  8039. }
  8040. __kmp_nesting_mode_nlevels = loc;
  8041. __kmp_nested_nth.used = __kmp_nesting_mode_nlevels;
  8042. } else { // no topology info available; provide a reasonable guesstimation
  8043. if (__kmp_avail_proc >= 4) {
  8044. __kmp_nesting_nth_level[0] = __kmp_avail_proc / 2;
  8045. __kmp_nesting_nth_level[1] = 2;
  8046. __kmp_nesting_mode_nlevels = 2;
  8047. } else {
  8048. __kmp_nesting_nth_level[0] = __kmp_avail_proc;
  8049. __kmp_nesting_mode_nlevels = 1;
  8050. }
  8051. __kmp_nested_nth.used = __kmp_nesting_mode_nlevels;
  8052. }
  8053. for (int i = 0; i < __kmp_nesting_mode_nlevels; ++i) {
  8054. __kmp_nested_nth.nth[i] = __kmp_nesting_nth_level[i];
  8055. }
  8056. set__nproc(thread, __kmp_nesting_nth_level[0]);
  8057. if (__kmp_nesting_mode > 1 && __kmp_nesting_mode_nlevels > __kmp_nesting_mode)
  8058. __kmp_nesting_mode_nlevels = __kmp_nesting_mode;
  8059. if (get__max_active_levels(thread) > 1) {
  8060. // if max levels was set, set nesting mode levels to same
  8061. __kmp_nesting_mode_nlevels = get__max_active_levels(thread);
  8062. }
  8063. if (__kmp_nesting_mode == 1) // turn on nesting for this case only
  8064. set__max_active_levels(thread, __kmp_nesting_mode_nlevels);
  8065. }