kmp.h 174 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512
  1. /*! \file */
  2. /*
  3. * kmp.h -- KPTS runtime header file.
  4. */
  5. //===----------------------------------------------------------------------===//
  6. //
  7. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  8. // See https://llvm.org/LICENSE.txt for license information.
  9. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef KMP_H
  13. #define KMP_H
  14. #include "kmp_config.h"
  15. /* #define BUILD_PARALLEL_ORDERED 1 */
  16. /* This fix replaces gettimeofday with clock_gettime for better scalability on
  17. the Altix. Requires user code to be linked with -lrt. */
  18. //#define FIX_SGI_CLOCK
  19. /* Defines for OpenMP 3.0 tasking and auto scheduling */
  20. #ifndef KMP_STATIC_STEAL_ENABLED
  21. #define KMP_STATIC_STEAL_ENABLED 1
  22. #endif
  23. #define TASK_CURRENT_NOT_QUEUED 0
  24. #define TASK_CURRENT_QUEUED 1
  25. #ifdef BUILD_TIED_TASK_STACK
  26. #define TASK_STACK_EMPTY 0 // entries when the stack is empty
  27. #define TASK_STACK_BLOCK_BITS 5 // Used in TASK_STACK_SIZE and TASK_STACK_MASK
  28. // Number of entries in each task stack array
  29. #define TASK_STACK_BLOCK_SIZE (1 << TASK_STACK_BLOCK_BITS)
  30. // Mask for determining index into stack block
  31. #define TASK_STACK_INDEX_MASK (TASK_STACK_BLOCK_SIZE - 1)
  32. #endif // BUILD_TIED_TASK_STACK
  33. #define TASK_NOT_PUSHED 1
  34. #define TASK_SUCCESSFULLY_PUSHED 0
  35. #define TASK_TIED 1
  36. #define TASK_UNTIED 0
  37. #define TASK_EXPLICIT 1
  38. #define TASK_IMPLICIT 0
  39. #define TASK_PROXY 1
  40. #define TASK_FULL 0
  41. #define TASK_DETACHABLE 1
  42. #define TASK_UNDETACHABLE 0
  43. #define KMP_CANCEL_THREADS
  44. #define KMP_THREAD_ATTR
  45. // Android does not have pthread_cancel. Undefine KMP_CANCEL_THREADS if being
  46. // built on Android
  47. #if defined(__ANDROID__)
  48. #undef KMP_CANCEL_THREADS
  49. #endif
  50. #include <signal.h>
  51. #include <stdarg.h>
  52. #include <stddef.h>
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include <limits>
  57. #include <type_traits>
  58. /* include <ctype.h> don't use; problems with /MD on Windows* OS NT due to bad
  59. Microsoft library. Some macros provided below to replace these functions */
  60. #ifndef __ABSOFT_WIN
  61. #include <sys/types.h>
  62. #endif
  63. #include <limits.h>
  64. #include <time.h>
  65. #include <errno.h>
  66. #include "kmp_os.h"
  67. #include "kmp_safe_c_api.h"
  68. #if KMP_STATS_ENABLED
  69. class kmp_stats_list;
  70. #endif
  71. #if KMP_USE_HIER_SCHED
  72. // Only include hierarchical scheduling if affinity is supported
  73. #undef KMP_USE_HIER_SCHED
  74. #define KMP_USE_HIER_SCHED KMP_AFFINITY_SUPPORTED
  75. #endif
  76. #if KMP_USE_HWLOC && KMP_AFFINITY_SUPPORTED
  77. #include "hwloc.h"
  78. #ifndef HWLOC_OBJ_NUMANODE
  79. #define HWLOC_OBJ_NUMANODE HWLOC_OBJ_NODE
  80. #endif
  81. #ifndef HWLOC_OBJ_PACKAGE
  82. #define HWLOC_OBJ_PACKAGE HWLOC_OBJ_SOCKET
  83. #endif
  84. #endif
  85. #if KMP_ARCH_X86 || KMP_ARCH_X86_64
  86. #include <xmmintrin.h>
  87. #endif
  88. // The below has to be defined before including "kmp_barrier.h".
  89. #define KMP_INTERNAL_MALLOC(sz) malloc(sz)
  90. #define KMP_INTERNAL_FREE(p) free(p)
  91. #define KMP_INTERNAL_REALLOC(p, sz) realloc((p), (sz))
  92. #define KMP_INTERNAL_CALLOC(n, sz) calloc((n), (sz))
  93. #include "kmp_debug.h"
  94. #include "kmp_lock.h"
  95. #include "kmp_version.h"
  96. #include "kmp_barrier.h"
  97. #if USE_DEBUGGER
  98. #error #include "kmp_debugger.h"
  99. #endif
  100. #include "kmp_i18n.h"
  101. #define KMP_HANDLE_SIGNALS (KMP_OS_UNIX || KMP_OS_WINDOWS)
  102. #include "kmp_wrapper_malloc.h"
  103. #if KMP_OS_UNIX
  104. #include <unistd.h>
  105. #if !defined NSIG && defined _NSIG
  106. #define NSIG _NSIG
  107. #endif
  108. #endif
  109. #if KMP_OS_LINUX
  110. #pragma weak clock_gettime
  111. #endif
  112. #if OMPT_SUPPORT
  113. #include "ompt-internal.h"
  114. #endif
  115. #if OMPD_SUPPORT
  116. #error #include "ompd-specific.h"
  117. #endif
  118. #ifndef UNLIKELY
  119. #define UNLIKELY(x) (x)
  120. #endif
  121. // Affinity format function
  122. #include "kmp_str.h"
  123. // 0 - no fast memory allocation, alignment: 8-byte on x86, 16-byte on x64.
  124. // 3 - fast allocation using sync, non-sync free lists of any size, non-self
  125. // free lists of limited size.
  126. #ifndef USE_FAST_MEMORY
  127. #define USE_FAST_MEMORY 3
  128. #endif
  129. #ifndef KMP_NESTED_HOT_TEAMS
  130. #define KMP_NESTED_HOT_TEAMS 0
  131. #define USE_NESTED_HOT_ARG(x)
  132. #else
  133. #if KMP_NESTED_HOT_TEAMS
  134. #define USE_NESTED_HOT_ARG(x) , x
  135. #else
  136. #define USE_NESTED_HOT_ARG(x)
  137. #endif
  138. #endif
  139. // Assume using BGET compare_exchange instruction instead of lock by default.
  140. #ifndef USE_CMP_XCHG_FOR_BGET
  141. #define USE_CMP_XCHG_FOR_BGET 1
  142. #endif
  143. // Test to see if queuing lock is better than bootstrap lock for bget
  144. // #ifndef USE_QUEUING_LOCK_FOR_BGET
  145. // #define USE_QUEUING_LOCK_FOR_BGET
  146. // #endif
  147. #define KMP_NSEC_PER_SEC 1000000000L
  148. #define KMP_USEC_PER_SEC 1000000L
  149. /*!
  150. @ingroup BASIC_TYPES
  151. @{
  152. */
  153. /*!
  154. Values for bit flags used in the ident_t to describe the fields.
  155. */
  156. enum {
  157. /*! Use trampoline for internal microtasks */
  158. KMP_IDENT_IMB = 0x01,
  159. /*! Use c-style ident structure */
  160. KMP_IDENT_KMPC = 0x02,
  161. /* 0x04 is no longer used */
  162. /*! Entry point generated by auto-parallelization */
  163. KMP_IDENT_AUTOPAR = 0x08,
  164. /*! Compiler generates atomic reduction option for kmpc_reduce* */
  165. KMP_IDENT_ATOMIC_REDUCE = 0x10,
  166. /*! To mark a 'barrier' directive in user code */
  167. KMP_IDENT_BARRIER_EXPL = 0x20,
  168. /*! To Mark implicit barriers. */
  169. KMP_IDENT_BARRIER_IMPL = 0x0040,
  170. KMP_IDENT_BARRIER_IMPL_MASK = 0x01C0,
  171. KMP_IDENT_BARRIER_IMPL_FOR = 0x0040,
  172. KMP_IDENT_BARRIER_IMPL_SECTIONS = 0x00C0,
  173. KMP_IDENT_BARRIER_IMPL_SINGLE = 0x0140,
  174. KMP_IDENT_BARRIER_IMPL_WORKSHARE = 0x01C0,
  175. /*! To mark a static loop in OMPT callbacks */
  176. KMP_IDENT_WORK_LOOP = 0x200,
  177. /*! To mark a sections directive in OMPT callbacks */
  178. KMP_IDENT_WORK_SECTIONS = 0x400,
  179. /*! To mark a distribute construct in OMPT callbacks */
  180. KMP_IDENT_WORK_DISTRIBUTE = 0x800,
  181. /*! Atomic hint; bottom four bits as omp_sync_hint_t. Top four reserved and
  182. not currently used. If one day we need more bits, then we can use
  183. an invalid combination of hints to mean that another, larger field
  184. should be used in a different flag. */
  185. KMP_IDENT_ATOMIC_HINT_MASK = 0xFF0000,
  186. KMP_IDENT_ATOMIC_HINT_UNCONTENDED = 0x010000,
  187. KMP_IDENT_ATOMIC_HINT_CONTENDED = 0x020000,
  188. KMP_IDENT_ATOMIC_HINT_NONSPECULATIVE = 0x040000,
  189. KMP_IDENT_ATOMIC_HINT_SPECULATIVE = 0x080000,
  190. KMP_IDENT_OPENMP_SPEC_VERSION_MASK = 0xFF000000
  191. };
  192. /*!
  193. * The ident structure that describes a source location.
  194. */
  195. typedef struct ident {
  196. kmp_int32 reserved_1; /**< might be used in Fortran; see above */
  197. kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags; KMP_IDENT_KMPC
  198. identifies this union member */
  199. kmp_int32 reserved_2; /**< not really used in Fortran any more; see above */
  200. #if USE_ITT_BUILD
  201. /* but currently used for storing region-specific ITT */
  202. /* contextual information. */
  203. #endif /* USE_ITT_BUILD */
  204. kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for C++ */
  205. char const *psource; /**< String describing the source location.
  206. The string is composed of semi-colon separated fields
  207. which describe the source file, the function and a pair
  208. of line numbers that delimit the construct. */
  209. // Returns the OpenMP version in form major*10+minor (e.g., 50 for 5.0)
  210. kmp_int32 get_openmp_version() {
  211. return (((flags & KMP_IDENT_OPENMP_SPEC_VERSION_MASK) >> 24) & 0xFF);
  212. }
  213. } ident_t;
  214. /*!
  215. @}
  216. */
  217. // Some forward declarations.
  218. typedef union kmp_team kmp_team_t;
  219. typedef struct kmp_taskdata kmp_taskdata_t;
  220. typedef union kmp_task_team kmp_task_team_t;
  221. typedef union kmp_team kmp_team_p;
  222. typedef union kmp_info kmp_info_p;
  223. typedef union kmp_root kmp_root_p;
  224. template <bool C = false, bool S = true> class kmp_flag_32;
  225. template <bool C = false, bool S = true> class kmp_flag_64;
  226. template <bool C = false, bool S = true> class kmp_atomic_flag_64;
  227. class kmp_flag_oncore;
  228. #ifdef __cplusplus
  229. extern "C" {
  230. #endif
  231. /* ------------------------------------------------------------------------ */
  232. /* Pack two 32-bit signed integers into a 64-bit signed integer */
  233. /* ToDo: Fix word ordering for big-endian machines. */
  234. #define KMP_PACK_64(HIGH_32, LOW_32) \
  235. ((kmp_int64)((((kmp_uint64)(HIGH_32)) << 32) | (kmp_uint64)(LOW_32)))
  236. // Generic string manipulation macros. Assume that _x is of type char *
  237. #define SKIP_WS(_x) \
  238. { \
  239. while (*(_x) == ' ' || *(_x) == '\t') \
  240. (_x)++; \
  241. }
  242. #define SKIP_DIGITS(_x) \
  243. { \
  244. while (*(_x) >= '0' && *(_x) <= '9') \
  245. (_x)++; \
  246. }
  247. #define SKIP_TOKEN(_x) \
  248. { \
  249. while ((*(_x) >= '0' && *(_x) <= '9') || (*(_x) >= 'a' && *(_x) <= 'z') || \
  250. (*(_x) >= 'A' && *(_x) <= 'Z') || *(_x) == '_') \
  251. (_x)++; \
  252. }
  253. #define SKIP_TO(_x, _c) \
  254. { \
  255. while (*(_x) != '\0' && *(_x) != (_c)) \
  256. (_x)++; \
  257. }
  258. /* ------------------------------------------------------------------------ */
  259. #define KMP_MAX(x, y) ((x) > (y) ? (x) : (y))
  260. #define KMP_MIN(x, y) ((x) < (y) ? (x) : (y))
  261. /* ------------------------------------------------------------------------ */
  262. /* Enumeration types */
  263. enum kmp_state_timer {
  264. ts_stop,
  265. ts_start,
  266. ts_pause,
  267. ts_last_state
  268. };
  269. enum dynamic_mode {
  270. dynamic_default,
  271. #ifdef USE_LOAD_BALANCE
  272. dynamic_load_balance,
  273. #endif /* USE_LOAD_BALANCE */
  274. dynamic_random,
  275. dynamic_thread_limit,
  276. dynamic_max
  277. };
  278. /* external schedule constants, duplicate enum omp_sched in omp.h in order to
  279. * not include it here */
  280. #ifndef KMP_SCHED_TYPE_DEFINED
  281. #define KMP_SCHED_TYPE_DEFINED
  282. typedef enum kmp_sched {
  283. kmp_sched_lower = 0, // lower and upper bounds are for routine parameter check
  284. // Note: need to adjust __kmp_sch_map global array in case enum is changed
  285. kmp_sched_static = 1, // mapped to kmp_sch_static_chunked (33)
  286. kmp_sched_dynamic = 2, // mapped to kmp_sch_dynamic_chunked (35)
  287. kmp_sched_guided = 3, // mapped to kmp_sch_guided_chunked (36)
  288. kmp_sched_auto = 4, // mapped to kmp_sch_auto (38)
  289. kmp_sched_upper_std = 5, // upper bound for standard schedules
  290. kmp_sched_lower_ext = 100, // lower bound of Intel extension schedules
  291. kmp_sched_trapezoidal = 101, // mapped to kmp_sch_trapezoidal (39)
  292. #if KMP_STATIC_STEAL_ENABLED
  293. kmp_sched_static_steal = 102, // mapped to kmp_sch_static_steal (44)
  294. #endif
  295. kmp_sched_upper,
  296. kmp_sched_default = kmp_sched_static, // default scheduling
  297. kmp_sched_monotonic = 0x80000000
  298. } kmp_sched_t;
  299. #endif
  300. /*!
  301. @ingroup WORK_SHARING
  302. * Describes the loop schedule to be used for a parallel for loop.
  303. */
  304. enum sched_type : kmp_int32 {
  305. kmp_sch_lower = 32, /**< lower bound for unordered values */
  306. kmp_sch_static_chunked = 33,
  307. kmp_sch_static = 34, /**< static unspecialized */
  308. kmp_sch_dynamic_chunked = 35,
  309. kmp_sch_guided_chunked = 36, /**< guided unspecialized */
  310. kmp_sch_runtime = 37,
  311. kmp_sch_auto = 38, /**< auto */
  312. kmp_sch_trapezoidal = 39,
  313. /* accessible only through KMP_SCHEDULE environment variable */
  314. kmp_sch_static_greedy = 40,
  315. kmp_sch_static_balanced = 41,
  316. /* accessible only through KMP_SCHEDULE environment variable */
  317. kmp_sch_guided_iterative_chunked = 42,
  318. kmp_sch_guided_analytical_chunked = 43,
  319. /* accessible only through KMP_SCHEDULE environment variable */
  320. kmp_sch_static_steal = 44,
  321. /* static with chunk adjustment (e.g., simd) */
  322. kmp_sch_static_balanced_chunked = 45,
  323. kmp_sch_guided_simd = 46, /**< guided with chunk adjustment */
  324. kmp_sch_runtime_simd = 47, /**< runtime with chunk adjustment */
  325. /* accessible only through KMP_SCHEDULE environment variable */
  326. kmp_sch_upper, /**< upper bound for unordered values */
  327. kmp_ord_lower = 64, /**< lower bound for ordered values, must be power of 2 */
  328. kmp_ord_static_chunked = 65,
  329. kmp_ord_static = 66, /**< ordered static unspecialized */
  330. kmp_ord_dynamic_chunked = 67,
  331. kmp_ord_guided_chunked = 68,
  332. kmp_ord_runtime = 69,
  333. kmp_ord_auto = 70, /**< ordered auto */
  334. kmp_ord_trapezoidal = 71,
  335. kmp_ord_upper, /**< upper bound for ordered values */
  336. /* Schedules for Distribute construct */
  337. kmp_distribute_static_chunked = 91, /**< distribute static chunked */
  338. kmp_distribute_static = 92, /**< distribute static unspecialized */
  339. /* For the "nomerge" versions, kmp_dispatch_next*() will always return a
  340. single iteration/chunk, even if the loop is serialized. For the schedule
  341. types listed above, the entire iteration vector is returned if the loop is
  342. serialized. This doesn't work for gcc/gcomp sections. */
  343. kmp_nm_lower = 160, /**< lower bound for nomerge values */
  344. kmp_nm_static_chunked =
  345. (kmp_sch_static_chunked - kmp_sch_lower + kmp_nm_lower),
  346. kmp_nm_static = 162, /**< static unspecialized */
  347. kmp_nm_dynamic_chunked = 163,
  348. kmp_nm_guided_chunked = 164, /**< guided unspecialized */
  349. kmp_nm_runtime = 165,
  350. kmp_nm_auto = 166, /**< auto */
  351. kmp_nm_trapezoidal = 167,
  352. /* accessible only through KMP_SCHEDULE environment variable */
  353. kmp_nm_static_greedy = 168,
  354. kmp_nm_static_balanced = 169,
  355. /* accessible only through KMP_SCHEDULE environment variable */
  356. kmp_nm_guided_iterative_chunked = 170,
  357. kmp_nm_guided_analytical_chunked = 171,
  358. kmp_nm_static_steal =
  359. 172, /* accessible only through OMP_SCHEDULE environment variable */
  360. kmp_nm_ord_static_chunked = 193,
  361. kmp_nm_ord_static = 194, /**< ordered static unspecialized */
  362. kmp_nm_ord_dynamic_chunked = 195,
  363. kmp_nm_ord_guided_chunked = 196,
  364. kmp_nm_ord_runtime = 197,
  365. kmp_nm_ord_auto = 198, /**< auto */
  366. kmp_nm_ord_trapezoidal = 199,
  367. kmp_nm_upper, /**< upper bound for nomerge values */
  368. /* Support for OpenMP 4.5 monotonic and nonmonotonic schedule modifiers. Since
  369. we need to distinguish the three possible cases (no modifier, monotonic
  370. modifier, nonmonotonic modifier), we need separate bits for each modifier.
  371. The absence of monotonic does not imply nonmonotonic, especially since 4.5
  372. says that the behaviour of the "no modifier" case is implementation defined
  373. in 4.5, but will become "nonmonotonic" in 5.0.
  374. Since we're passing a full 32 bit value, we can use a couple of high bits
  375. for these flags; out of paranoia we avoid the sign bit.
  376. These modifiers can be or-ed into non-static schedules by the compiler to
  377. pass the additional information. They will be stripped early in the
  378. processing in __kmp_dispatch_init when setting up schedules, so most of the
  379. code won't ever see schedules with these bits set. */
  380. kmp_sch_modifier_monotonic =
  381. (1 << 29), /**< Set if the monotonic schedule modifier was present */
  382. kmp_sch_modifier_nonmonotonic =
  383. (1 << 30), /**< Set if the nonmonotonic schedule modifier was present */
  384. #define SCHEDULE_WITHOUT_MODIFIERS(s) \
  385. (enum sched_type)( \
  386. (s) & ~(kmp_sch_modifier_nonmonotonic | kmp_sch_modifier_monotonic))
  387. #define SCHEDULE_HAS_MONOTONIC(s) (((s)&kmp_sch_modifier_monotonic) != 0)
  388. #define SCHEDULE_HAS_NONMONOTONIC(s) (((s)&kmp_sch_modifier_nonmonotonic) != 0)
  389. #define SCHEDULE_HAS_NO_MODIFIERS(s) \
  390. (((s) & (kmp_sch_modifier_nonmonotonic | kmp_sch_modifier_monotonic)) == 0)
  391. #define SCHEDULE_GET_MODIFIERS(s) \
  392. ((enum sched_type)( \
  393. (s) & (kmp_sch_modifier_nonmonotonic | kmp_sch_modifier_monotonic)))
  394. #define SCHEDULE_SET_MODIFIERS(s, m) \
  395. (s = (enum sched_type)((kmp_int32)s | (kmp_int32)m))
  396. #define SCHEDULE_NONMONOTONIC 0
  397. #define SCHEDULE_MONOTONIC 1
  398. kmp_sch_default = kmp_sch_static /**< default scheduling algorithm */
  399. };
  400. // Apply modifiers on internal kind to standard kind
  401. static inline void
  402. __kmp_sched_apply_mods_stdkind(kmp_sched_t *kind,
  403. enum sched_type internal_kind) {
  404. if (SCHEDULE_HAS_MONOTONIC(internal_kind)) {
  405. *kind = (kmp_sched_t)((int)*kind | (int)kmp_sched_monotonic);
  406. }
  407. }
  408. // Apply modifiers on standard kind to internal kind
  409. static inline void
  410. __kmp_sched_apply_mods_intkind(kmp_sched_t kind,
  411. enum sched_type *internal_kind) {
  412. if ((int)kind & (int)kmp_sched_monotonic) {
  413. *internal_kind = (enum sched_type)((int)*internal_kind |
  414. (int)kmp_sch_modifier_monotonic);
  415. }
  416. }
  417. // Get standard schedule without modifiers
  418. static inline kmp_sched_t __kmp_sched_without_mods(kmp_sched_t kind) {
  419. return (kmp_sched_t)((int)kind & ~((int)kmp_sched_monotonic));
  420. }
  421. /* Type to keep runtime schedule set via OMP_SCHEDULE or omp_set_schedule() */
  422. typedef union kmp_r_sched {
  423. struct {
  424. enum sched_type r_sched_type;
  425. int chunk;
  426. };
  427. kmp_int64 sched;
  428. } kmp_r_sched_t;
  429. extern enum sched_type __kmp_sch_map[]; // map OMP 3.0 schedule types with our
  430. // internal schedule types
  431. enum library_type {
  432. library_none,
  433. library_serial,
  434. library_turnaround,
  435. library_throughput
  436. };
  437. #if KMP_OS_LINUX
  438. enum clock_function_type {
  439. clock_function_gettimeofday,
  440. clock_function_clock_gettime
  441. };
  442. #endif /* KMP_OS_LINUX */
  443. #if KMP_MIC_SUPPORTED
  444. enum mic_type { non_mic, mic1, mic2, mic3, dummy };
  445. #endif
  446. /* -- fast reduction stuff ------------------------------------------------ */
  447. #undef KMP_FAST_REDUCTION_BARRIER
  448. #define KMP_FAST_REDUCTION_BARRIER 1
  449. #undef KMP_FAST_REDUCTION_CORE_DUO
  450. #if KMP_ARCH_X86 || KMP_ARCH_X86_64
  451. #define KMP_FAST_REDUCTION_CORE_DUO 1
  452. #endif
  453. enum _reduction_method {
  454. reduction_method_not_defined = 0,
  455. critical_reduce_block = (1 << 8),
  456. atomic_reduce_block = (2 << 8),
  457. tree_reduce_block = (3 << 8),
  458. empty_reduce_block = (4 << 8)
  459. };
  460. // Description of the packed_reduction_method variable:
  461. // The packed_reduction_method variable consists of two enum types variables
  462. // that are packed together into 0-th byte and 1-st byte:
  463. // 0: (packed_reduction_method & 0x000000FF) is a 'enum barrier_type' value of
  464. // barrier that will be used in fast reduction: bs_plain_barrier or
  465. // bs_reduction_barrier
  466. // 1: (packed_reduction_method & 0x0000FF00) is a reduction method that will
  467. // be used in fast reduction;
  468. // Reduction method is of 'enum _reduction_method' type and it's defined the way
  469. // so that the bits of 0-th byte are empty, so no need to execute a shift
  470. // instruction while packing/unpacking
  471. #if KMP_FAST_REDUCTION_BARRIER
  472. #define PACK_REDUCTION_METHOD_AND_BARRIER(reduction_method, barrier_type) \
  473. ((reduction_method) | (barrier_type))
  474. #define UNPACK_REDUCTION_METHOD(packed_reduction_method) \
  475. ((enum _reduction_method)((packed_reduction_method) & (0x0000FF00)))
  476. #define UNPACK_REDUCTION_BARRIER(packed_reduction_method) \
  477. ((enum barrier_type)((packed_reduction_method) & (0x000000FF)))
  478. #else
  479. #define PACK_REDUCTION_METHOD_AND_BARRIER(reduction_method, barrier_type) \
  480. (reduction_method)
  481. #define UNPACK_REDUCTION_METHOD(packed_reduction_method) \
  482. (packed_reduction_method)
  483. #define UNPACK_REDUCTION_BARRIER(packed_reduction_method) (bs_plain_barrier)
  484. #endif
  485. #define TEST_REDUCTION_METHOD(packed_reduction_method, which_reduction_block) \
  486. ((UNPACK_REDUCTION_METHOD(packed_reduction_method)) == \
  487. (which_reduction_block))
  488. #if KMP_FAST_REDUCTION_BARRIER
  489. #define TREE_REDUCE_BLOCK_WITH_REDUCTION_BARRIER \
  490. (PACK_REDUCTION_METHOD_AND_BARRIER(tree_reduce_block, bs_reduction_barrier))
  491. #define TREE_REDUCE_BLOCK_WITH_PLAIN_BARRIER \
  492. (PACK_REDUCTION_METHOD_AND_BARRIER(tree_reduce_block, bs_plain_barrier))
  493. #endif
  494. typedef int PACKED_REDUCTION_METHOD_T;
  495. /* -- end of fast reduction stuff ----------------------------------------- */
  496. #if KMP_OS_WINDOWS
  497. #define USE_CBLKDATA
  498. #if KMP_MSVC_COMPAT
  499. #pragma warning(push)
  500. #pragma warning(disable : 271 310)
  501. #endif
  502. #include <windows.h>
  503. #if KMP_MSVC_COMPAT
  504. #pragma warning(pop)
  505. #endif
  506. #endif
  507. #if KMP_OS_UNIX
  508. #include <dlfcn.h>
  509. #include <pthread.h>
  510. #endif
  511. enum kmp_hw_t : int {
  512. KMP_HW_UNKNOWN = -1,
  513. KMP_HW_SOCKET = 0,
  514. KMP_HW_PROC_GROUP,
  515. KMP_HW_NUMA,
  516. KMP_HW_DIE,
  517. KMP_HW_LLC,
  518. KMP_HW_L3,
  519. KMP_HW_TILE,
  520. KMP_HW_MODULE,
  521. KMP_HW_L2,
  522. KMP_HW_L1,
  523. KMP_HW_CORE,
  524. KMP_HW_THREAD,
  525. KMP_HW_LAST
  526. };
  527. typedef enum kmp_hw_core_type_t {
  528. KMP_HW_CORE_TYPE_UNKNOWN = 0x0,
  529. #if KMP_ARCH_X86 || KMP_ARCH_X86_64
  530. KMP_HW_CORE_TYPE_ATOM = 0x20,
  531. KMP_HW_CORE_TYPE_CORE = 0x40,
  532. KMP_HW_MAX_NUM_CORE_TYPES = 3,
  533. #else
  534. KMP_HW_MAX_NUM_CORE_TYPES = 1,
  535. #endif
  536. } kmp_hw_core_type_t;
  537. #define KMP_HW_MAX_NUM_CORE_EFFS 8
  538. #define KMP_DEBUG_ASSERT_VALID_HW_TYPE(type) \
  539. KMP_DEBUG_ASSERT(type >= (kmp_hw_t)0 && type < KMP_HW_LAST)
  540. #define KMP_ASSERT_VALID_HW_TYPE(type) \
  541. KMP_ASSERT(type >= (kmp_hw_t)0 && type < KMP_HW_LAST)
  542. #define KMP_FOREACH_HW_TYPE(type) \
  543. for (kmp_hw_t type = (kmp_hw_t)0; type < KMP_HW_LAST; \
  544. type = (kmp_hw_t)((int)type + 1))
  545. const char *__kmp_hw_get_keyword(kmp_hw_t type, bool plural = false);
  546. const char *__kmp_hw_get_catalog_string(kmp_hw_t type, bool plural = false);
  547. const char *__kmp_hw_get_core_type_string(kmp_hw_core_type_t type);
  548. /* Only Linux* OS and Windows* OS support thread affinity. */
  549. #if KMP_AFFINITY_SUPPORTED
  550. // GROUP_AFFINITY is already defined for _MSC_VER>=1600 (VS2010 and later).
  551. #if KMP_OS_WINDOWS
  552. #if _MSC_VER < 1600 && KMP_MSVC_COMPAT
  553. typedef struct GROUP_AFFINITY {
  554. KAFFINITY Mask;
  555. WORD Group;
  556. WORD Reserved[3];
  557. } GROUP_AFFINITY;
  558. #endif /* _MSC_VER < 1600 */
  559. #if KMP_GROUP_AFFINITY
  560. extern int __kmp_num_proc_groups;
  561. #else
  562. static const int __kmp_num_proc_groups = 1;
  563. #endif /* KMP_GROUP_AFFINITY */
  564. typedef DWORD (*kmp_GetActiveProcessorCount_t)(WORD);
  565. extern kmp_GetActiveProcessorCount_t __kmp_GetActiveProcessorCount;
  566. typedef WORD (*kmp_GetActiveProcessorGroupCount_t)(void);
  567. extern kmp_GetActiveProcessorGroupCount_t __kmp_GetActiveProcessorGroupCount;
  568. typedef BOOL (*kmp_GetThreadGroupAffinity_t)(HANDLE, GROUP_AFFINITY *);
  569. extern kmp_GetThreadGroupAffinity_t __kmp_GetThreadGroupAffinity;
  570. typedef BOOL (*kmp_SetThreadGroupAffinity_t)(HANDLE, const GROUP_AFFINITY *,
  571. GROUP_AFFINITY *);
  572. extern kmp_SetThreadGroupAffinity_t __kmp_SetThreadGroupAffinity;
  573. #endif /* KMP_OS_WINDOWS */
  574. #if KMP_USE_HWLOC
  575. extern hwloc_topology_t __kmp_hwloc_topology;
  576. extern int __kmp_hwloc_error;
  577. #endif
  578. extern size_t __kmp_affin_mask_size;
  579. #define KMP_AFFINITY_CAPABLE() (__kmp_affin_mask_size > 0)
  580. #define KMP_AFFINITY_DISABLE() (__kmp_affin_mask_size = 0)
  581. #define KMP_AFFINITY_ENABLE(mask_size) (__kmp_affin_mask_size = mask_size)
  582. #define KMP_CPU_SET_ITERATE(i, mask) \
  583. for (i = (mask)->begin(); (int)i != (mask)->end(); i = (mask)->next(i))
  584. #define KMP_CPU_SET(i, mask) (mask)->set(i)
  585. #define KMP_CPU_ISSET(i, mask) (mask)->is_set(i)
  586. #define KMP_CPU_CLR(i, mask) (mask)->clear(i)
  587. #define KMP_CPU_ZERO(mask) (mask)->zero()
  588. #define KMP_CPU_COPY(dest, src) (dest)->copy(src)
  589. #define KMP_CPU_AND(dest, src) (dest)->bitwise_and(src)
  590. #define KMP_CPU_COMPLEMENT(max_bit_number, mask) (mask)->bitwise_not()
  591. #define KMP_CPU_UNION(dest, src) (dest)->bitwise_or(src)
  592. #define KMP_CPU_ALLOC(ptr) (ptr = __kmp_affinity_dispatch->allocate_mask())
  593. #define KMP_CPU_FREE(ptr) __kmp_affinity_dispatch->deallocate_mask(ptr)
  594. #define KMP_CPU_ALLOC_ON_STACK(ptr) KMP_CPU_ALLOC(ptr)
  595. #define KMP_CPU_FREE_FROM_STACK(ptr) KMP_CPU_FREE(ptr)
  596. #define KMP_CPU_INTERNAL_ALLOC(ptr) KMP_CPU_ALLOC(ptr)
  597. #define KMP_CPU_INTERNAL_FREE(ptr) KMP_CPU_FREE(ptr)
  598. #define KMP_CPU_INDEX(arr, i) __kmp_affinity_dispatch->index_mask_array(arr, i)
  599. #define KMP_CPU_ALLOC_ARRAY(arr, n) \
  600. (arr = __kmp_affinity_dispatch->allocate_mask_array(n))
  601. #define KMP_CPU_FREE_ARRAY(arr, n) \
  602. __kmp_affinity_dispatch->deallocate_mask_array(arr)
  603. #define KMP_CPU_INTERNAL_ALLOC_ARRAY(arr, n) KMP_CPU_ALLOC_ARRAY(arr, n)
  604. #define KMP_CPU_INTERNAL_FREE_ARRAY(arr, n) KMP_CPU_FREE_ARRAY(arr, n)
  605. #define __kmp_get_system_affinity(mask, abort_bool) \
  606. (mask)->get_system_affinity(abort_bool)
  607. #define __kmp_set_system_affinity(mask, abort_bool) \
  608. (mask)->set_system_affinity(abort_bool)
  609. #define __kmp_get_proc_group(mask) (mask)->get_proc_group()
  610. class KMPAffinity {
  611. public:
  612. class Mask {
  613. public:
  614. void *operator new(size_t n);
  615. void operator delete(void *p);
  616. void *operator new[](size_t n);
  617. void operator delete[](void *p);
  618. virtual ~Mask() {}
  619. // Set bit i to 1
  620. virtual void set(int i) {}
  621. // Return bit i
  622. virtual bool is_set(int i) const { return false; }
  623. // Set bit i to 0
  624. virtual void clear(int i) {}
  625. // Zero out entire mask
  626. virtual void zero() {}
  627. // Copy src into this mask
  628. virtual void copy(const Mask *src) {}
  629. // this &= rhs
  630. virtual void bitwise_and(const Mask *rhs) {}
  631. // this |= rhs
  632. virtual void bitwise_or(const Mask *rhs) {}
  633. // this = ~this
  634. virtual void bitwise_not() {}
  635. // API for iterating over an affinity mask
  636. // for (int i = mask->begin(); i != mask->end(); i = mask->next(i))
  637. virtual int begin() const { return 0; }
  638. virtual int end() const { return 0; }
  639. virtual int next(int previous) const { return 0; }
  640. #if KMP_OS_WINDOWS
  641. virtual int set_process_affinity(bool abort_on_error) const { return -1; }
  642. #endif
  643. // Set the system's affinity to this affinity mask's value
  644. virtual int set_system_affinity(bool abort_on_error) const { return -1; }
  645. // Set this affinity mask to the current system affinity
  646. virtual int get_system_affinity(bool abort_on_error) { return -1; }
  647. // Only 1 DWORD in the mask should have any procs set.
  648. // Return the appropriate index, or -1 for an invalid mask.
  649. virtual int get_proc_group() const { return -1; }
  650. };
  651. void *operator new(size_t n);
  652. void operator delete(void *p);
  653. // Need virtual destructor
  654. virtual ~KMPAffinity() = default;
  655. // Determine if affinity is capable
  656. virtual void determine_capable(const char *env_var) {}
  657. // Bind the current thread to os proc
  658. virtual void bind_thread(int proc) {}
  659. // Factory functions to allocate/deallocate a mask
  660. virtual Mask *allocate_mask() { return nullptr; }
  661. virtual void deallocate_mask(Mask *m) {}
  662. virtual Mask *allocate_mask_array(int num) { return nullptr; }
  663. virtual void deallocate_mask_array(Mask *m) {}
  664. virtual Mask *index_mask_array(Mask *m, int index) { return nullptr; }
  665. static void pick_api();
  666. static void destroy_api();
  667. enum api_type {
  668. NATIVE_OS
  669. #if KMP_USE_HWLOC
  670. ,
  671. HWLOC
  672. #endif
  673. };
  674. virtual api_type get_api_type() const {
  675. KMP_ASSERT(0);
  676. return NATIVE_OS;
  677. }
  678. private:
  679. static bool picked_api;
  680. };
  681. typedef KMPAffinity::Mask kmp_affin_mask_t;
  682. extern KMPAffinity *__kmp_affinity_dispatch;
  683. // Declare local char buffers with this size for printing debug and info
  684. // messages, using __kmp_affinity_print_mask().
  685. #define KMP_AFFIN_MASK_PRINT_LEN 1024
  686. enum affinity_type {
  687. affinity_none = 0,
  688. affinity_physical,
  689. affinity_logical,
  690. affinity_compact,
  691. affinity_scatter,
  692. affinity_explicit,
  693. affinity_balanced,
  694. affinity_disabled, // not used outsize the env var parser
  695. affinity_default
  696. };
  697. enum affinity_top_method {
  698. affinity_top_method_all = 0, // try all (supported) methods, in order
  699. #if KMP_ARCH_X86 || KMP_ARCH_X86_64
  700. affinity_top_method_apicid,
  701. affinity_top_method_x2apicid,
  702. affinity_top_method_x2apicid_1f,
  703. #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
  704. affinity_top_method_cpuinfo, // KMP_CPUINFO_FILE is usable on Windows* OS, too
  705. #if KMP_GROUP_AFFINITY
  706. affinity_top_method_group,
  707. #endif /* KMP_GROUP_AFFINITY */
  708. affinity_top_method_flat,
  709. #if KMP_USE_HWLOC
  710. affinity_top_method_hwloc,
  711. #endif
  712. affinity_top_method_default
  713. };
  714. #define affinity_respect_mask_default (-1)
  715. extern enum affinity_type __kmp_affinity_type; /* Affinity type */
  716. extern kmp_hw_t __kmp_affinity_gran; /* Affinity granularity */
  717. extern int __kmp_affinity_gran_levels; /* corresponding int value */
  718. extern int __kmp_affinity_dups; /* Affinity duplicate masks */
  719. extern enum affinity_top_method __kmp_affinity_top_method;
  720. extern int __kmp_affinity_compact; /* Affinity 'compact' value */
  721. extern int __kmp_affinity_offset; /* Affinity offset value */
  722. extern int __kmp_affinity_verbose; /* Was verbose specified for KMP_AFFINITY? */
  723. extern int __kmp_affinity_warnings; /* KMP_AFFINITY warnings enabled ? */
  724. extern int __kmp_affinity_respect_mask; // Respect process' init affinity mask?
  725. extern char *__kmp_affinity_proclist; /* proc ID list */
  726. extern kmp_affin_mask_t *__kmp_affinity_masks;
  727. extern unsigned __kmp_affinity_num_masks;
  728. extern void __kmp_affinity_bind_thread(int which);
  729. extern kmp_affin_mask_t *__kmp_affin_fullMask;
  730. extern kmp_affin_mask_t *__kmp_affin_origMask;
  731. extern char *__kmp_cpuinfo_file;
  732. extern bool __kmp_affin_reset;
  733. #endif /* KMP_AFFINITY_SUPPORTED */
  734. // This needs to be kept in sync with the values in omp.h !!!
  735. typedef enum kmp_proc_bind_t {
  736. proc_bind_false = 0,
  737. proc_bind_true,
  738. proc_bind_primary,
  739. proc_bind_close,
  740. proc_bind_spread,
  741. proc_bind_intel, // use KMP_AFFINITY interface
  742. proc_bind_default
  743. } kmp_proc_bind_t;
  744. typedef struct kmp_nested_proc_bind_t {
  745. kmp_proc_bind_t *bind_types;
  746. int size;
  747. int used;
  748. } kmp_nested_proc_bind_t;
  749. extern kmp_nested_proc_bind_t __kmp_nested_proc_bind;
  750. extern kmp_proc_bind_t __kmp_teams_proc_bind;
  751. extern int __kmp_display_affinity;
  752. extern char *__kmp_affinity_format;
  753. static const size_t KMP_AFFINITY_FORMAT_SIZE = 512;
  754. #if OMPT_SUPPORT
  755. extern int __kmp_tool;
  756. extern char *__kmp_tool_libraries;
  757. #endif // OMPT_SUPPORT
  758. #if KMP_AFFINITY_SUPPORTED
  759. #define KMP_PLACE_ALL (-1)
  760. #define KMP_PLACE_UNDEFINED (-2)
  761. // Is KMP_AFFINITY is being used instead of OMP_PROC_BIND/OMP_PLACES?
  762. #define KMP_AFFINITY_NON_PROC_BIND \
  763. ((__kmp_nested_proc_bind.bind_types[0] == proc_bind_false || \
  764. __kmp_nested_proc_bind.bind_types[0] == proc_bind_intel) && \
  765. (__kmp_affinity_num_masks > 0 || __kmp_affinity_type == affinity_balanced))
  766. #endif /* KMP_AFFINITY_SUPPORTED */
  767. extern int __kmp_affinity_num_places;
  768. typedef enum kmp_cancel_kind_t {
  769. cancel_noreq = 0,
  770. cancel_parallel = 1,
  771. cancel_loop = 2,
  772. cancel_sections = 3,
  773. cancel_taskgroup = 4
  774. } kmp_cancel_kind_t;
  775. // KMP_HW_SUBSET support:
  776. typedef struct kmp_hws_item {
  777. int num;
  778. int offset;
  779. } kmp_hws_item_t;
  780. extern kmp_hws_item_t __kmp_hws_socket;
  781. extern kmp_hws_item_t __kmp_hws_die;
  782. extern kmp_hws_item_t __kmp_hws_node;
  783. extern kmp_hws_item_t __kmp_hws_tile;
  784. extern kmp_hws_item_t __kmp_hws_core;
  785. extern kmp_hws_item_t __kmp_hws_proc;
  786. extern int __kmp_hws_requested;
  787. extern int __kmp_hws_abs_flag; // absolute or per-item number requested
  788. /* ------------------------------------------------------------------------ */
  789. #define KMP_PAD(type, sz) \
  790. (sizeof(type) + (sz - ((sizeof(type) - 1) % (sz)) - 1))
  791. // We need to avoid using -1 as a GTID as +1 is added to the gtid
  792. // when storing it in a lock, and the value 0 is reserved.
  793. #define KMP_GTID_DNE (-2) /* Does not exist */
  794. #define KMP_GTID_SHUTDOWN (-3) /* Library is shutting down */
  795. #define KMP_GTID_MONITOR (-4) /* Monitor thread ID */
  796. #define KMP_GTID_UNKNOWN (-5) /* Is not known */
  797. #define KMP_GTID_MIN (-6) /* Minimal gtid for low bound check in DEBUG */
  798. /* OpenMP 5.0 Memory Management support */
  799. #ifndef __OMP_H
  800. // Duplicate type definitions from omp.h
  801. typedef uintptr_t omp_uintptr_t;
  802. typedef enum {
  803. omp_atk_sync_hint = 1,
  804. omp_atk_alignment = 2,
  805. omp_atk_access = 3,
  806. omp_atk_pool_size = 4,
  807. omp_atk_fallback = 5,
  808. omp_atk_fb_data = 6,
  809. omp_atk_pinned = 7,
  810. omp_atk_partition = 8
  811. } omp_alloctrait_key_t;
  812. typedef enum {
  813. omp_atv_false = 0,
  814. omp_atv_true = 1,
  815. omp_atv_contended = 3,
  816. omp_atv_uncontended = 4,
  817. omp_atv_serialized = 5,
  818. omp_atv_sequential = omp_atv_serialized, // (deprecated)
  819. omp_atv_private = 6,
  820. omp_atv_all = 7,
  821. omp_atv_thread = 8,
  822. omp_atv_pteam = 9,
  823. omp_atv_cgroup = 10,
  824. omp_atv_default_mem_fb = 11,
  825. omp_atv_null_fb = 12,
  826. omp_atv_abort_fb = 13,
  827. omp_atv_allocator_fb = 14,
  828. omp_atv_environment = 15,
  829. omp_atv_nearest = 16,
  830. omp_atv_blocked = 17,
  831. omp_atv_interleaved = 18
  832. } omp_alloctrait_value_t;
  833. #define omp_atv_default ((omp_uintptr_t)-1)
  834. typedef void *omp_memspace_handle_t;
  835. extern omp_memspace_handle_t const omp_default_mem_space;
  836. extern omp_memspace_handle_t const omp_large_cap_mem_space;
  837. extern omp_memspace_handle_t const omp_const_mem_space;
  838. extern omp_memspace_handle_t const omp_high_bw_mem_space;
  839. extern omp_memspace_handle_t const omp_low_lat_mem_space;
  840. extern omp_memspace_handle_t const llvm_omp_target_host_mem_space;
  841. extern omp_memspace_handle_t const llvm_omp_target_shared_mem_space;
  842. extern omp_memspace_handle_t const llvm_omp_target_device_mem_space;
  843. typedef struct {
  844. omp_alloctrait_key_t key;
  845. omp_uintptr_t value;
  846. } omp_alloctrait_t;
  847. typedef void *omp_allocator_handle_t;
  848. extern omp_allocator_handle_t const omp_null_allocator;
  849. extern omp_allocator_handle_t const omp_default_mem_alloc;
  850. extern omp_allocator_handle_t const omp_large_cap_mem_alloc;
  851. extern omp_allocator_handle_t const omp_const_mem_alloc;
  852. extern omp_allocator_handle_t const omp_high_bw_mem_alloc;
  853. extern omp_allocator_handle_t const omp_low_lat_mem_alloc;
  854. extern omp_allocator_handle_t const omp_cgroup_mem_alloc;
  855. extern omp_allocator_handle_t const omp_pteam_mem_alloc;
  856. extern omp_allocator_handle_t const omp_thread_mem_alloc;
  857. extern omp_allocator_handle_t const llvm_omp_target_host_mem_alloc;
  858. extern omp_allocator_handle_t const llvm_omp_target_shared_mem_alloc;
  859. extern omp_allocator_handle_t const llvm_omp_target_device_mem_alloc;
  860. extern omp_allocator_handle_t const kmp_max_mem_alloc;
  861. extern omp_allocator_handle_t __kmp_def_allocator;
  862. // end of duplicate type definitions from omp.h
  863. #endif
  864. extern int __kmp_memkind_available;
  865. typedef omp_memspace_handle_t kmp_memspace_t; // placeholder
  866. typedef struct kmp_allocator_t {
  867. omp_memspace_handle_t memspace;
  868. void **memkind; // pointer to memkind
  869. size_t alignment;
  870. omp_alloctrait_value_t fb;
  871. kmp_allocator_t *fb_data;
  872. kmp_uint64 pool_size;
  873. kmp_uint64 pool_used;
  874. } kmp_allocator_t;
  875. extern omp_allocator_handle_t __kmpc_init_allocator(int gtid,
  876. omp_memspace_handle_t,
  877. int ntraits,
  878. omp_alloctrait_t traits[]);
  879. extern void __kmpc_destroy_allocator(int gtid, omp_allocator_handle_t al);
  880. extern void __kmpc_set_default_allocator(int gtid, omp_allocator_handle_t al);
  881. extern omp_allocator_handle_t __kmpc_get_default_allocator(int gtid);
  882. // external interfaces, may be used by compiler
  883. extern void *__kmpc_alloc(int gtid, size_t sz, omp_allocator_handle_t al);
  884. extern void *__kmpc_aligned_alloc(int gtid, size_t align, size_t sz,
  885. omp_allocator_handle_t al);
  886. extern void *__kmpc_calloc(int gtid, size_t nmemb, size_t sz,
  887. omp_allocator_handle_t al);
  888. extern void *__kmpc_realloc(int gtid, void *ptr, size_t sz,
  889. omp_allocator_handle_t al,
  890. omp_allocator_handle_t free_al);
  891. extern void __kmpc_free(int gtid, void *ptr, omp_allocator_handle_t al);
  892. // internal interfaces, contain real implementation
  893. extern void *__kmp_alloc(int gtid, size_t align, size_t sz,
  894. omp_allocator_handle_t al);
  895. extern void *__kmp_calloc(int gtid, size_t align, size_t nmemb, size_t sz,
  896. omp_allocator_handle_t al);
  897. extern void *__kmp_realloc(int gtid, void *ptr, size_t sz,
  898. omp_allocator_handle_t al,
  899. omp_allocator_handle_t free_al);
  900. extern void ___kmpc_free(int gtid, void *ptr, omp_allocator_handle_t al);
  901. extern void __kmp_init_memkind();
  902. extern void __kmp_fini_memkind();
  903. extern void __kmp_init_target_mem();
  904. /* ------------------------------------------------------------------------ */
  905. #define KMP_UINT64_MAX \
  906. (~((kmp_uint64)1 << ((sizeof(kmp_uint64) * (1 << 3)) - 1)))
  907. #define KMP_MIN_NTH 1
  908. #ifndef KMP_MAX_NTH
  909. #if defined(PTHREAD_THREADS_MAX) && PTHREAD_THREADS_MAX < INT_MAX
  910. #define KMP_MAX_NTH PTHREAD_THREADS_MAX
  911. #else
  912. #define KMP_MAX_NTH INT_MAX
  913. #endif
  914. #endif /* KMP_MAX_NTH */
  915. #ifdef PTHREAD_STACK_MIN
  916. #define KMP_MIN_STKSIZE PTHREAD_STACK_MIN
  917. #else
  918. #define KMP_MIN_STKSIZE ((size_t)(32 * 1024))
  919. #endif
  920. #define KMP_MAX_STKSIZE (~((size_t)1 << ((sizeof(size_t) * (1 << 3)) - 1)))
  921. #if KMP_ARCH_X86
  922. #define KMP_DEFAULT_STKSIZE ((size_t)(2 * 1024 * 1024))
  923. #elif KMP_ARCH_X86_64
  924. #define KMP_DEFAULT_STKSIZE ((size_t)(4 * 1024 * 1024))
  925. #define KMP_BACKUP_STKSIZE ((size_t)(2 * 1024 * 1024))
  926. #else
  927. #define KMP_DEFAULT_STKSIZE ((size_t)(1024 * 1024))
  928. #endif
  929. #define KMP_DEFAULT_MALLOC_POOL_INCR ((size_t)(1024 * 1024))
  930. #define KMP_MIN_MALLOC_POOL_INCR ((size_t)(4 * 1024))
  931. #define KMP_MAX_MALLOC_POOL_INCR \
  932. (~((size_t)1 << ((sizeof(size_t) * (1 << 3)) - 1)))
  933. #define KMP_MIN_STKOFFSET (0)
  934. #define KMP_MAX_STKOFFSET KMP_MAX_STKSIZE
  935. #if KMP_OS_DARWIN
  936. #define KMP_DEFAULT_STKOFFSET KMP_MIN_STKOFFSET
  937. #else
  938. #define KMP_DEFAULT_STKOFFSET CACHE_LINE
  939. #endif
  940. #define KMP_MIN_STKPADDING (0)
  941. #define KMP_MAX_STKPADDING (2 * 1024 * 1024)
  942. #define KMP_BLOCKTIME_MULTIPLIER \
  943. (1000) /* number of blocktime units per second */
  944. #define KMP_MIN_BLOCKTIME (0)
  945. #define KMP_MAX_BLOCKTIME \
  946. (INT_MAX) /* Must be this for "infinite" setting the work */
  947. /* __kmp_blocktime is in milliseconds */
  948. #define KMP_DEFAULT_BLOCKTIME (__kmp_is_hybrid_cpu() ? (0) : (200))
  949. #if KMP_USE_MONITOR
  950. #define KMP_DEFAULT_MONITOR_STKSIZE ((size_t)(64 * 1024))
  951. #define KMP_MIN_MONITOR_WAKEUPS (1) // min times monitor wakes up per second
  952. #define KMP_MAX_MONITOR_WAKEUPS (1000) // max times monitor can wake up per sec
  953. /* Calculate new number of monitor wakeups for a specific block time based on
  954. previous monitor_wakeups. Only allow increasing number of wakeups */
  955. #define KMP_WAKEUPS_FROM_BLOCKTIME(blocktime, monitor_wakeups) \
  956. (((blocktime) == KMP_MAX_BLOCKTIME) ? (monitor_wakeups) \
  957. : ((blocktime) == KMP_MIN_BLOCKTIME) ? KMP_MAX_MONITOR_WAKEUPS \
  958. : ((monitor_wakeups) > (KMP_BLOCKTIME_MULTIPLIER / (blocktime))) \
  959. ? (monitor_wakeups) \
  960. : (KMP_BLOCKTIME_MULTIPLIER) / (blocktime))
  961. /* Calculate number of intervals for a specific block time based on
  962. monitor_wakeups */
  963. #define KMP_INTERVALS_FROM_BLOCKTIME(blocktime, monitor_wakeups) \
  964. (((blocktime) + (KMP_BLOCKTIME_MULTIPLIER / (monitor_wakeups)) - 1) / \
  965. (KMP_BLOCKTIME_MULTIPLIER / (monitor_wakeups)))
  966. #else
  967. #define KMP_BLOCKTIME(team, tid) \
  968. (get__bt_set(team, tid) ? get__blocktime(team, tid) : __kmp_dflt_blocktime)
  969. #if KMP_OS_UNIX && (KMP_ARCH_X86 || KMP_ARCH_X86_64)
  970. // HW TSC is used to reduce overhead (clock tick instead of nanosecond).
  971. extern kmp_uint64 __kmp_ticks_per_msec;
  972. #if KMP_COMPILER_ICC || KMP_COMPILER_ICX
  973. #define KMP_NOW() ((kmp_uint64)_rdtsc())
  974. #else
  975. #define KMP_NOW() __kmp_hardware_timestamp()
  976. #endif
  977. #define KMP_NOW_MSEC() (KMP_NOW() / __kmp_ticks_per_msec)
  978. #define KMP_BLOCKTIME_INTERVAL(team, tid) \
  979. (KMP_BLOCKTIME(team, tid) * __kmp_ticks_per_msec)
  980. #define KMP_BLOCKING(goal, count) ((goal) > KMP_NOW())
  981. #else
  982. // System time is retrieved sporadically while blocking.
  983. extern kmp_uint64 __kmp_now_nsec();
  984. #define KMP_NOW() __kmp_now_nsec()
  985. #define KMP_NOW_MSEC() (KMP_NOW() / KMP_USEC_PER_SEC)
  986. #define KMP_BLOCKTIME_INTERVAL(team, tid) \
  987. (KMP_BLOCKTIME(team, tid) * KMP_USEC_PER_SEC)
  988. #define KMP_BLOCKING(goal, count) ((count) % 1000 != 0 || (goal) > KMP_NOW())
  989. #endif
  990. #endif // KMP_USE_MONITOR
  991. #define KMP_MIN_STATSCOLS 40
  992. #define KMP_MAX_STATSCOLS 4096
  993. #define KMP_DEFAULT_STATSCOLS 80
  994. #define KMP_MIN_INTERVAL 0
  995. #define KMP_MAX_INTERVAL (INT_MAX - 1)
  996. #define KMP_DEFAULT_INTERVAL 0
  997. #define KMP_MIN_CHUNK 1
  998. #define KMP_MAX_CHUNK (INT_MAX - 1)
  999. #define KMP_DEFAULT_CHUNK 1
  1000. #define KMP_MIN_DISP_NUM_BUFF 1
  1001. #define KMP_DFLT_DISP_NUM_BUFF 7
  1002. #define KMP_MAX_DISP_NUM_BUFF 4096
  1003. #define KMP_MAX_ORDERED 8
  1004. #define KMP_MAX_FIELDS 32
  1005. #define KMP_MAX_BRANCH_BITS 31
  1006. #define KMP_MAX_ACTIVE_LEVELS_LIMIT INT_MAX
  1007. #define KMP_MAX_DEFAULT_DEVICE_LIMIT INT_MAX
  1008. #define KMP_MAX_TASK_PRIORITY_LIMIT INT_MAX
  1009. /* Minimum number of threads before switch to TLS gtid (experimentally
  1010. determined) */
  1011. /* josh TODO: what about OS X* tuning? */
  1012. #if KMP_ARCH_X86 || KMP_ARCH_X86_64
  1013. #define KMP_TLS_GTID_MIN 5
  1014. #else
  1015. #define KMP_TLS_GTID_MIN INT_MAX
  1016. #endif
  1017. #define KMP_MASTER_TID(tid) (0 == (tid))
  1018. #define KMP_WORKER_TID(tid) (0 != (tid))
  1019. #define KMP_MASTER_GTID(gtid) (0 == __kmp_tid_from_gtid((gtid)))
  1020. #define KMP_WORKER_GTID(gtid) (0 != __kmp_tid_from_gtid((gtid)))
  1021. #define KMP_INITIAL_GTID(gtid) (0 == (gtid))
  1022. #ifndef TRUE
  1023. #define FALSE 0
  1024. #define TRUE (!FALSE)
  1025. #endif
  1026. /* NOTE: all of the following constants must be even */
  1027. #if KMP_OS_WINDOWS
  1028. #define KMP_INIT_WAIT 64U /* initial number of spin-tests */
  1029. #define KMP_NEXT_WAIT 32U /* susequent number of spin-tests */
  1030. #elif KMP_OS_LINUX
  1031. #define KMP_INIT_WAIT 1024U /* initial number of spin-tests */
  1032. #define KMP_NEXT_WAIT 512U /* susequent number of spin-tests */
  1033. #elif KMP_OS_DARWIN
  1034. /* TODO: tune for KMP_OS_DARWIN */
  1035. #define KMP_INIT_WAIT 1024U /* initial number of spin-tests */
  1036. #define KMP_NEXT_WAIT 512U /* susequent number of spin-tests */
  1037. #elif KMP_OS_DRAGONFLY
  1038. /* TODO: tune for KMP_OS_DRAGONFLY */
  1039. #define KMP_INIT_WAIT 1024U /* initial number of spin-tests */
  1040. #define KMP_NEXT_WAIT 512U /* susequent number of spin-tests */
  1041. #elif KMP_OS_FREEBSD
  1042. /* TODO: tune for KMP_OS_FREEBSD */
  1043. #define KMP_INIT_WAIT 1024U /* initial number of spin-tests */
  1044. #define KMP_NEXT_WAIT 512U /* susequent number of spin-tests */
  1045. #elif KMP_OS_NETBSD
  1046. /* TODO: tune for KMP_OS_NETBSD */
  1047. #define KMP_INIT_WAIT 1024U /* initial number of spin-tests */
  1048. #define KMP_NEXT_WAIT 512U /* susequent number of spin-tests */
  1049. #elif KMP_OS_HURD
  1050. /* TODO: tune for KMP_OS_HURD */
  1051. #define KMP_INIT_WAIT 1024U /* initial number of spin-tests */
  1052. #define KMP_NEXT_WAIT 512U /* susequent number of spin-tests */
  1053. #elif KMP_OS_OPENBSD
  1054. /* TODO: tune for KMP_OS_OPENBSD */
  1055. #define KMP_INIT_WAIT 1024U /* initial number of spin-tests */
  1056. #define KMP_NEXT_WAIT 512U /* susequent number of spin-tests */
  1057. #endif
  1058. #if KMP_ARCH_X86 || KMP_ARCH_X86_64
  1059. typedef struct kmp_cpuid {
  1060. kmp_uint32 eax;
  1061. kmp_uint32 ebx;
  1062. kmp_uint32 ecx;
  1063. kmp_uint32 edx;
  1064. } kmp_cpuid_t;
  1065. typedef struct kmp_cpuinfo_flags_t {
  1066. unsigned sse2 : 1; // 0 if SSE2 instructions are not supported, 1 otherwise.
  1067. unsigned rtm : 1; // 0 if RTM instructions are not supported, 1 otherwise.
  1068. unsigned hybrid : 1;
  1069. unsigned reserved : 29; // Ensure size of 32 bits
  1070. } kmp_cpuinfo_flags_t;
  1071. typedef struct kmp_cpuinfo {
  1072. int initialized; // If 0, other fields are not initialized.
  1073. int signature; // CPUID(1).EAX
  1074. int family; // CPUID(1).EAX[27:20]+CPUID(1).EAX[11:8] (Extended Family+Family)
  1075. int model; // ( CPUID(1).EAX[19:16] << 4 ) + CPUID(1).EAX[7:4] ( ( Extended
  1076. // Model << 4 ) + Model)
  1077. int stepping; // CPUID(1).EAX[3:0] ( Stepping )
  1078. kmp_cpuinfo_flags_t flags;
  1079. int apic_id;
  1080. int physical_id;
  1081. int logical_id;
  1082. kmp_uint64 frequency; // Nominal CPU frequency in Hz.
  1083. char name[3 * sizeof(kmp_cpuid_t)]; // CPUID(0x80000002,0x80000003,0x80000004)
  1084. } kmp_cpuinfo_t;
  1085. extern void __kmp_query_cpuid(kmp_cpuinfo_t *p);
  1086. #if KMP_OS_UNIX
  1087. // subleaf is only needed for cache and topology discovery and can be set to
  1088. // zero in most cases
  1089. static inline void __kmp_x86_cpuid(int leaf, int subleaf, struct kmp_cpuid *p) {
  1090. __asm__ __volatile__("cpuid"
  1091. : "=a"(p->eax), "=b"(p->ebx), "=c"(p->ecx), "=d"(p->edx)
  1092. : "a"(leaf), "c"(subleaf));
  1093. }
  1094. // Load p into FPU control word
  1095. static inline void __kmp_load_x87_fpu_control_word(const kmp_int16 *p) {
  1096. __asm__ __volatile__("fldcw %0" : : "m"(*p));
  1097. }
  1098. // Store FPU control word into p
  1099. static inline void __kmp_store_x87_fpu_control_word(kmp_int16 *p) {
  1100. __asm__ __volatile__("fstcw %0" : "=m"(*p));
  1101. }
  1102. static inline void __kmp_clear_x87_fpu_status_word() {
  1103. #if KMP_MIC
  1104. // 32-bit protected mode x87 FPU state
  1105. struct x87_fpu_state {
  1106. unsigned cw;
  1107. unsigned sw;
  1108. unsigned tw;
  1109. unsigned fip;
  1110. unsigned fips;
  1111. unsigned fdp;
  1112. unsigned fds;
  1113. };
  1114. struct x87_fpu_state fpu_state = {0, 0, 0, 0, 0, 0, 0};
  1115. __asm__ __volatile__("fstenv %0\n\t" // store FP env
  1116. "andw $0x7f00, %1\n\t" // clear 0-7,15 bits of FP SW
  1117. "fldenv %0\n\t" // load FP env back
  1118. : "+m"(fpu_state), "+m"(fpu_state.sw));
  1119. #else
  1120. __asm__ __volatile__("fnclex");
  1121. #endif // KMP_MIC
  1122. }
  1123. #if __SSE__
  1124. static inline void __kmp_load_mxcsr(const kmp_uint32 *p) { _mm_setcsr(*p); }
  1125. static inline void __kmp_store_mxcsr(kmp_uint32 *p) { *p = _mm_getcsr(); }
  1126. #else
  1127. static inline void __kmp_load_mxcsr(const kmp_uint32 *p) {}
  1128. static inline void __kmp_store_mxcsr(kmp_uint32 *p) { *p = 0; }
  1129. #endif
  1130. #else
  1131. // Windows still has these as external functions in assembly file
  1132. extern void __kmp_x86_cpuid(int mode, int mode2, struct kmp_cpuid *p);
  1133. extern void __kmp_load_x87_fpu_control_word(const kmp_int16 *p);
  1134. extern void __kmp_store_x87_fpu_control_word(kmp_int16 *p);
  1135. extern void __kmp_clear_x87_fpu_status_word();
  1136. static inline void __kmp_load_mxcsr(const kmp_uint32 *p) { _mm_setcsr(*p); }
  1137. static inline void __kmp_store_mxcsr(kmp_uint32 *p) { *p = _mm_getcsr(); }
  1138. #endif // KMP_OS_UNIX
  1139. #define KMP_X86_MXCSR_MASK 0xffffffc0 /* ignore status flags (6 lsb) */
  1140. // User-level Monitor/Mwait
  1141. #if KMP_HAVE_UMWAIT
  1142. // We always try for UMWAIT first
  1143. #if KMP_HAVE_WAITPKG_INTRINSICS
  1144. #if KMP_HAVE_IMMINTRIN_H
  1145. #include <immintrin.h>
  1146. #elif KMP_HAVE_INTRIN_H
  1147. #include <intrin.h>
  1148. #endif
  1149. #endif // KMP_HAVE_WAITPKG_INTRINSICS
  1150. KMP_ATTRIBUTE_TARGET_WAITPKG
  1151. static inline int __kmp_tpause(uint32_t hint, uint64_t counter) {
  1152. #if !KMP_HAVE_WAITPKG_INTRINSICS
  1153. uint32_t timeHi = uint32_t(counter >> 32);
  1154. uint32_t timeLo = uint32_t(counter & 0xffffffff);
  1155. char flag;
  1156. __asm__ volatile("#tpause\n.byte 0x66, 0x0F, 0xAE, 0xF1\n"
  1157. "setb %0"
  1158. // The "=q" restraint means any register accessible as rl
  1159. // in 32-bit mode: a, b, c, and d;
  1160. // in 64-bit mode: any integer register
  1161. : "=q"(flag)
  1162. : "a"(timeLo), "d"(timeHi), "c"(hint)
  1163. :);
  1164. return flag;
  1165. #else
  1166. return _tpause(hint, counter);
  1167. #endif
  1168. }
  1169. KMP_ATTRIBUTE_TARGET_WAITPKG
  1170. static inline void __kmp_umonitor(void *cacheline) {
  1171. #if !KMP_HAVE_WAITPKG_INTRINSICS
  1172. __asm__ volatile("# umonitor\n.byte 0xF3, 0x0F, 0xAE, 0x01 "
  1173. :
  1174. : "a"(cacheline)
  1175. :);
  1176. #else
  1177. _umonitor(cacheline);
  1178. #endif
  1179. }
  1180. KMP_ATTRIBUTE_TARGET_WAITPKG
  1181. static inline int __kmp_umwait(uint32_t hint, uint64_t counter) {
  1182. #if !KMP_HAVE_WAITPKG_INTRINSICS
  1183. uint32_t timeHi = uint32_t(counter >> 32);
  1184. uint32_t timeLo = uint32_t(counter & 0xffffffff);
  1185. char flag;
  1186. __asm__ volatile("#umwait\n.byte 0xF2, 0x0F, 0xAE, 0xF1\n"
  1187. "setb %0"
  1188. // The "=q" restraint means any register accessible as rl
  1189. // in 32-bit mode: a, b, c, and d;
  1190. // in 64-bit mode: any integer register
  1191. : "=q"(flag)
  1192. : "a"(timeLo), "d"(timeHi), "c"(hint)
  1193. :);
  1194. return flag;
  1195. #else
  1196. return _umwait(hint, counter);
  1197. #endif
  1198. }
  1199. #elif KMP_HAVE_MWAIT
  1200. #if KMP_OS_UNIX
  1201. #include <pmmintrin.h>
  1202. #else
  1203. #include <intrin.h>
  1204. #endif
  1205. #if KMP_OS_UNIX
  1206. __attribute__((target("sse3")))
  1207. #endif
  1208. static inline void
  1209. __kmp_mm_monitor(void *cacheline, unsigned extensions, unsigned hints) {
  1210. _mm_monitor(cacheline, extensions, hints);
  1211. }
  1212. #if KMP_OS_UNIX
  1213. __attribute__((target("sse3")))
  1214. #endif
  1215. static inline void
  1216. __kmp_mm_mwait(unsigned extensions, unsigned hints) {
  1217. _mm_mwait(extensions, hints);
  1218. }
  1219. #endif // KMP_HAVE_UMWAIT
  1220. #if KMP_ARCH_X86
  1221. extern void __kmp_x86_pause(void);
  1222. #elif KMP_MIC
  1223. // Performance testing on KNC (C0QS-7120 P/A/X/D, 61-core, 16 GB Memory) showed
  1224. // regression after removal of extra PAUSE from spin loops. Changing
  1225. // the delay from 100 to 300 showed even better performance than double PAUSE
  1226. // on Spec OMP2001 and LCPC tasking tests, no regressions on EPCC.
  1227. static inline void __kmp_x86_pause(void) { _mm_delay_32(300); }
  1228. #else
  1229. static inline void __kmp_x86_pause(void) { _mm_pause(); }
  1230. #endif
  1231. #define KMP_CPU_PAUSE() __kmp_x86_pause()
  1232. #elif KMP_ARCH_PPC64
  1233. #define KMP_PPC64_PRI_LOW() __asm__ volatile("or 1, 1, 1")
  1234. #define KMP_PPC64_PRI_MED() __asm__ volatile("or 2, 2, 2")
  1235. #define KMP_PPC64_PRI_LOC_MB() __asm__ volatile("" : : : "memory")
  1236. #define KMP_CPU_PAUSE() \
  1237. do { \
  1238. KMP_PPC64_PRI_LOW(); \
  1239. KMP_PPC64_PRI_MED(); \
  1240. KMP_PPC64_PRI_LOC_MB(); \
  1241. } while (0)
  1242. #else
  1243. #define KMP_CPU_PAUSE() /* nothing to do */
  1244. #endif
  1245. #define KMP_INIT_YIELD(count) \
  1246. { (count) = __kmp_yield_init; }
  1247. #define KMP_INIT_BACKOFF(time) \
  1248. { (time) = __kmp_pause_init; }
  1249. #define KMP_OVERSUBSCRIBED \
  1250. (TCR_4(__kmp_nth) > (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc))
  1251. #define KMP_TRY_YIELD \
  1252. ((__kmp_use_yield == 1) || (__kmp_use_yield == 2 && (KMP_OVERSUBSCRIBED)))
  1253. #define KMP_TRY_YIELD_OVERSUB \
  1254. ((__kmp_use_yield == 1 || __kmp_use_yield == 2) && (KMP_OVERSUBSCRIBED))
  1255. #define KMP_YIELD(cond) \
  1256. { \
  1257. KMP_CPU_PAUSE(); \
  1258. if ((cond) && (KMP_TRY_YIELD)) \
  1259. __kmp_yield(); \
  1260. }
  1261. #define KMP_YIELD_OVERSUB() \
  1262. { \
  1263. KMP_CPU_PAUSE(); \
  1264. if ((KMP_TRY_YIELD_OVERSUB)) \
  1265. __kmp_yield(); \
  1266. }
  1267. // Note the decrement of 2 in the following Macros. With KMP_LIBRARY=turnaround,
  1268. // there should be no yielding since initial value from KMP_INIT_YIELD() is odd.
  1269. #define KMP_YIELD_SPIN(count) \
  1270. { \
  1271. KMP_CPU_PAUSE(); \
  1272. if (KMP_TRY_YIELD) { \
  1273. (count) -= 2; \
  1274. if (!(count)) { \
  1275. __kmp_yield(); \
  1276. (count) = __kmp_yield_next; \
  1277. } \
  1278. } \
  1279. }
  1280. // If TPAUSE is available & enabled, use it. If oversubscribed, use the slower
  1281. // (C0.2) state, which improves performance of other SMT threads on the same
  1282. // core, otherwise, use the fast (C0.1) default state, or whatever the user has
  1283. // requested. Uses a timed TPAUSE, and exponential backoff. If TPAUSE isn't
  1284. // available, fall back to the regular CPU pause and yield combination.
  1285. #if KMP_HAVE_UMWAIT
  1286. #define KMP_YIELD_OVERSUB_ELSE_SPIN(count, time) \
  1287. { \
  1288. if (__kmp_tpause_enabled) { \
  1289. if (KMP_OVERSUBSCRIBED) { \
  1290. __kmp_tpause(0, (time)); \
  1291. } else { \
  1292. __kmp_tpause(__kmp_tpause_hint, (time)); \
  1293. } \
  1294. (time) *= 2; \
  1295. } else { \
  1296. KMP_CPU_PAUSE(); \
  1297. if ((KMP_TRY_YIELD_OVERSUB)) { \
  1298. __kmp_yield(); \
  1299. } else if (__kmp_use_yield == 1) { \
  1300. (count) -= 2; \
  1301. if (!(count)) { \
  1302. __kmp_yield(); \
  1303. (count) = __kmp_yield_next; \
  1304. } \
  1305. } \
  1306. } \
  1307. }
  1308. #else
  1309. #define KMP_YIELD_OVERSUB_ELSE_SPIN(count, time) \
  1310. { \
  1311. KMP_CPU_PAUSE(); \
  1312. if ((KMP_TRY_YIELD_OVERSUB)) \
  1313. __kmp_yield(); \
  1314. else if (__kmp_use_yield == 1) { \
  1315. (count) -= 2; \
  1316. if (!(count)) { \
  1317. __kmp_yield(); \
  1318. (count) = __kmp_yield_next; \
  1319. } \
  1320. } \
  1321. }
  1322. #endif // KMP_HAVE_UMWAIT
  1323. /* ------------------------------------------------------------------------ */
  1324. /* Support datatypes for the orphaned construct nesting checks. */
  1325. /* ------------------------------------------------------------------------ */
  1326. /* When adding to this enum, add its corresponding string in cons_text_c[]
  1327. * array in kmp_error.cpp */
  1328. enum cons_type {
  1329. ct_none,
  1330. ct_parallel,
  1331. ct_pdo,
  1332. ct_pdo_ordered,
  1333. ct_psections,
  1334. ct_psingle,
  1335. ct_critical,
  1336. ct_ordered_in_parallel,
  1337. ct_ordered_in_pdo,
  1338. ct_master,
  1339. ct_reduce,
  1340. ct_barrier,
  1341. ct_masked
  1342. };
  1343. #define IS_CONS_TYPE_ORDERED(ct) ((ct) == ct_pdo_ordered)
  1344. struct cons_data {
  1345. ident_t const *ident;
  1346. enum cons_type type;
  1347. int prev;
  1348. kmp_user_lock_p
  1349. name; /* address exclusively for critical section name comparison */
  1350. };
  1351. struct cons_header {
  1352. int p_top, w_top, s_top;
  1353. int stack_size, stack_top;
  1354. struct cons_data *stack_data;
  1355. };
  1356. struct kmp_region_info {
  1357. char *text;
  1358. int offset[KMP_MAX_FIELDS];
  1359. int length[KMP_MAX_FIELDS];
  1360. };
  1361. /* ---------------------------------------------------------------------- */
  1362. /* ---------------------------------------------------------------------- */
  1363. #if KMP_OS_WINDOWS
  1364. typedef HANDLE kmp_thread_t;
  1365. typedef DWORD kmp_key_t;
  1366. #endif /* KMP_OS_WINDOWS */
  1367. #if KMP_OS_UNIX
  1368. typedef pthread_t kmp_thread_t;
  1369. typedef pthread_key_t kmp_key_t;
  1370. #endif
  1371. extern kmp_key_t __kmp_gtid_threadprivate_key;
  1372. typedef struct kmp_sys_info {
  1373. long maxrss; /* the maximum resident set size utilized (in kilobytes) */
  1374. long minflt; /* the number of page faults serviced without any I/O */
  1375. long majflt; /* the number of page faults serviced that required I/O */
  1376. long nswap; /* the number of times a process was "swapped" out of memory */
  1377. long inblock; /* the number of times the file system had to perform input */
  1378. long oublock; /* the number of times the file system had to perform output */
  1379. long nvcsw; /* the number of times a context switch was voluntarily */
  1380. long nivcsw; /* the number of times a context switch was forced */
  1381. } kmp_sys_info_t;
  1382. #if USE_ITT_BUILD
  1383. // We cannot include "kmp_itt.h" due to circular dependency. Declare the only
  1384. // required type here. Later we will check the type meets requirements.
  1385. typedef int kmp_itt_mark_t;
  1386. #define KMP_ITT_DEBUG 0
  1387. #endif /* USE_ITT_BUILD */
  1388. typedef kmp_int32 kmp_critical_name[8];
  1389. /*!
  1390. @ingroup PARALLEL
  1391. The type for a microtask which gets passed to @ref __kmpc_fork_call().
  1392. The arguments to the outlined function are
  1393. @param global_tid the global thread identity of the thread executing the
  1394. function.
  1395. @param bound_tid the local identity of the thread executing the function
  1396. @param ... pointers to shared variables accessed by the function.
  1397. */
  1398. typedef void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid, ...);
  1399. typedef void (*kmpc_micro_bound)(kmp_int32 *bound_tid, kmp_int32 *bound_nth,
  1400. ...);
  1401. /*!
  1402. @ingroup THREADPRIVATE
  1403. @{
  1404. */
  1405. /* ---------------------------------------------------------------------------
  1406. */
  1407. /* Threadprivate initialization/finalization function declarations */
  1408. /* for non-array objects: __kmpc_threadprivate_register() */
  1409. /*!
  1410. Pointer to the constructor function.
  1411. The first argument is the <tt>this</tt> pointer
  1412. */
  1413. typedef void *(*kmpc_ctor)(void *);
  1414. /*!
  1415. Pointer to the destructor function.
  1416. The first argument is the <tt>this</tt> pointer
  1417. */
  1418. typedef void (*kmpc_dtor)(
  1419. void * /*, size_t */); /* 2nd arg: magic number for KCC unused by Intel
  1420. compiler */
  1421. /*!
  1422. Pointer to an alternate constructor.
  1423. The first argument is the <tt>this</tt> pointer.
  1424. */
  1425. typedef void *(*kmpc_cctor)(void *, void *);
  1426. /* for array objects: __kmpc_threadprivate_register_vec() */
  1427. /* First arg: "this" pointer */
  1428. /* Last arg: number of array elements */
  1429. /*!
  1430. Array constructor.
  1431. First argument is the <tt>this</tt> pointer
  1432. Second argument the number of array elements.
  1433. */
  1434. typedef void *(*kmpc_ctor_vec)(void *, size_t);
  1435. /*!
  1436. Pointer to the array destructor function.
  1437. The first argument is the <tt>this</tt> pointer
  1438. Second argument the number of array elements.
  1439. */
  1440. typedef void (*kmpc_dtor_vec)(void *, size_t);
  1441. /*!
  1442. Array constructor.
  1443. First argument is the <tt>this</tt> pointer
  1444. Third argument the number of array elements.
  1445. */
  1446. typedef void *(*kmpc_cctor_vec)(void *, void *,
  1447. size_t); /* function unused by compiler */
  1448. /*!
  1449. @}
  1450. */
  1451. /* keeps tracked of threadprivate cache allocations for cleanup later */
  1452. typedef struct kmp_cached_addr {
  1453. void **addr; /* address of allocated cache */
  1454. void ***compiler_cache; /* pointer to compiler's cache */
  1455. void *data; /* pointer to global data */
  1456. struct kmp_cached_addr *next; /* pointer to next cached address */
  1457. } kmp_cached_addr_t;
  1458. struct private_data {
  1459. struct private_data *next; /* The next descriptor in the list */
  1460. void *data; /* The data buffer for this descriptor */
  1461. int more; /* The repeat count for this descriptor */
  1462. size_t size; /* The data size for this descriptor */
  1463. };
  1464. struct private_common {
  1465. struct private_common *next;
  1466. struct private_common *link;
  1467. void *gbl_addr;
  1468. void *par_addr; /* par_addr == gbl_addr for PRIMARY thread */
  1469. size_t cmn_size;
  1470. };
  1471. struct shared_common {
  1472. struct shared_common *next;
  1473. struct private_data *pod_init;
  1474. void *obj_init;
  1475. void *gbl_addr;
  1476. union {
  1477. kmpc_ctor ctor;
  1478. kmpc_ctor_vec ctorv;
  1479. } ct;
  1480. union {
  1481. kmpc_cctor cctor;
  1482. kmpc_cctor_vec cctorv;
  1483. } cct;
  1484. union {
  1485. kmpc_dtor dtor;
  1486. kmpc_dtor_vec dtorv;
  1487. } dt;
  1488. size_t vec_len;
  1489. int is_vec;
  1490. size_t cmn_size;
  1491. };
  1492. #define KMP_HASH_TABLE_LOG2 9 /* log2 of the hash table size */
  1493. #define KMP_HASH_TABLE_SIZE \
  1494. (1 << KMP_HASH_TABLE_LOG2) /* size of the hash table */
  1495. #define KMP_HASH_SHIFT 3 /* throw away this many low bits from the address */
  1496. #define KMP_HASH(x) \
  1497. ((((kmp_uintptr_t)x) >> KMP_HASH_SHIFT) & (KMP_HASH_TABLE_SIZE - 1))
  1498. struct common_table {
  1499. struct private_common *data[KMP_HASH_TABLE_SIZE];
  1500. };
  1501. struct shared_table {
  1502. struct shared_common *data[KMP_HASH_TABLE_SIZE];
  1503. };
  1504. /* ------------------------------------------------------------------------ */
  1505. #if KMP_USE_HIER_SCHED
  1506. // Shared barrier data that exists inside a single unit of the scheduling
  1507. // hierarchy
  1508. typedef struct kmp_hier_private_bdata_t {
  1509. kmp_int32 num_active;
  1510. kmp_uint64 index;
  1511. kmp_uint64 wait_val[2];
  1512. } kmp_hier_private_bdata_t;
  1513. #endif
  1514. typedef struct kmp_sched_flags {
  1515. unsigned ordered : 1;
  1516. unsigned nomerge : 1;
  1517. unsigned contains_last : 1;
  1518. #if KMP_USE_HIER_SCHED
  1519. unsigned use_hier : 1;
  1520. unsigned unused : 28;
  1521. #else
  1522. unsigned unused : 29;
  1523. #endif
  1524. } kmp_sched_flags_t;
  1525. KMP_BUILD_ASSERT(sizeof(kmp_sched_flags_t) == 4);
  1526. #if KMP_STATIC_STEAL_ENABLED
  1527. typedef struct KMP_ALIGN_CACHE dispatch_private_info32 {
  1528. kmp_int32 count;
  1529. kmp_int32 ub;
  1530. /* Adding KMP_ALIGN_CACHE here doesn't help / can hurt performance */
  1531. kmp_int32 lb;
  1532. kmp_int32 st;
  1533. kmp_int32 tc;
  1534. kmp_lock_t *steal_lock; // lock used for chunk stealing
  1535. // KMP_ALIGN(32) ensures (if the KMP_ALIGN macro is turned on)
  1536. // a) parm3 is properly aligned and
  1537. // b) all parm1-4 are on the same cache line.
  1538. // Because of parm1-4 are used together, performance seems to be better
  1539. // if they are on the same cache line (not measured though).
  1540. struct KMP_ALIGN(32) { // AC: changed 16 to 32 in order to simplify template
  1541. kmp_int32 parm1; // structures in kmp_dispatch.cpp. This should
  1542. kmp_int32 parm2; // make no real change at least while padding is off.
  1543. kmp_int32 parm3;
  1544. kmp_int32 parm4;
  1545. };
  1546. kmp_uint32 ordered_lower;
  1547. kmp_uint32 ordered_upper;
  1548. #if KMP_OS_WINDOWS
  1549. kmp_int32 last_upper;
  1550. #endif /* KMP_OS_WINDOWS */
  1551. } dispatch_private_info32_t;
  1552. typedef struct KMP_ALIGN_CACHE dispatch_private_info64 {
  1553. kmp_int64 count; // current chunk number for static & static-steal scheduling
  1554. kmp_int64 ub; /* upper-bound */
  1555. /* Adding KMP_ALIGN_CACHE here doesn't help / can hurt performance */
  1556. kmp_int64 lb; /* lower-bound */
  1557. kmp_int64 st; /* stride */
  1558. kmp_int64 tc; /* trip count (number of iterations) */
  1559. kmp_lock_t *steal_lock; // lock used for chunk stealing
  1560. /* parm[1-4] are used in different ways by different scheduling algorithms */
  1561. // KMP_ALIGN( 32 ) ensures ( if the KMP_ALIGN macro is turned on )
  1562. // a) parm3 is properly aligned and
  1563. // b) all parm1-4 are in the same cache line.
  1564. // Because of parm1-4 are used together, performance seems to be better
  1565. // if they are in the same line (not measured though).
  1566. struct KMP_ALIGN(32) {
  1567. kmp_int64 parm1;
  1568. kmp_int64 parm2;
  1569. kmp_int64 parm3;
  1570. kmp_int64 parm4;
  1571. };
  1572. kmp_uint64 ordered_lower;
  1573. kmp_uint64 ordered_upper;
  1574. #if KMP_OS_WINDOWS
  1575. kmp_int64 last_upper;
  1576. #endif /* KMP_OS_WINDOWS */
  1577. } dispatch_private_info64_t;
  1578. #else /* KMP_STATIC_STEAL_ENABLED */
  1579. typedef struct KMP_ALIGN_CACHE dispatch_private_info32 {
  1580. kmp_int32 lb;
  1581. kmp_int32 ub;
  1582. kmp_int32 st;
  1583. kmp_int32 tc;
  1584. kmp_int32 parm1;
  1585. kmp_int32 parm2;
  1586. kmp_int32 parm3;
  1587. kmp_int32 parm4;
  1588. kmp_int32 count;
  1589. kmp_uint32 ordered_lower;
  1590. kmp_uint32 ordered_upper;
  1591. #if KMP_OS_WINDOWS
  1592. kmp_int32 last_upper;
  1593. #endif /* KMP_OS_WINDOWS */
  1594. } dispatch_private_info32_t;
  1595. typedef struct KMP_ALIGN_CACHE dispatch_private_info64 {
  1596. kmp_int64 lb; /* lower-bound */
  1597. kmp_int64 ub; /* upper-bound */
  1598. kmp_int64 st; /* stride */
  1599. kmp_int64 tc; /* trip count (number of iterations) */
  1600. /* parm[1-4] are used in different ways by different scheduling algorithms */
  1601. kmp_int64 parm1;
  1602. kmp_int64 parm2;
  1603. kmp_int64 parm3;
  1604. kmp_int64 parm4;
  1605. kmp_int64 count; /* current chunk number for static scheduling */
  1606. kmp_uint64 ordered_lower;
  1607. kmp_uint64 ordered_upper;
  1608. #if KMP_OS_WINDOWS
  1609. kmp_int64 last_upper;
  1610. #endif /* KMP_OS_WINDOWS */
  1611. } dispatch_private_info64_t;
  1612. #endif /* KMP_STATIC_STEAL_ENABLED */
  1613. typedef struct KMP_ALIGN_CACHE dispatch_private_info {
  1614. union private_info {
  1615. dispatch_private_info32_t p32;
  1616. dispatch_private_info64_t p64;
  1617. } u;
  1618. enum sched_type schedule; /* scheduling algorithm */
  1619. kmp_sched_flags_t flags; /* flags (e.g., ordered, nomerge, etc.) */
  1620. std::atomic<kmp_uint32> steal_flag; // static_steal only, state of a buffer
  1621. kmp_int32 ordered_bumped;
  1622. // Stack of buffers for nest of serial regions
  1623. struct dispatch_private_info *next;
  1624. kmp_int32 type_size; /* the size of types in private_info */
  1625. #if KMP_USE_HIER_SCHED
  1626. kmp_int32 hier_id;
  1627. void *parent; /* hierarchical scheduling parent pointer */
  1628. #endif
  1629. enum cons_type pushed_ws;
  1630. } dispatch_private_info_t;
  1631. typedef struct dispatch_shared_info32 {
  1632. /* chunk index under dynamic, number of idle threads under static-steal;
  1633. iteration index otherwise */
  1634. volatile kmp_uint32 iteration;
  1635. volatile kmp_int32 num_done;
  1636. volatile kmp_uint32 ordered_iteration;
  1637. // Dummy to retain the structure size after making ordered_iteration scalar
  1638. kmp_int32 ordered_dummy[KMP_MAX_ORDERED - 1];
  1639. } dispatch_shared_info32_t;
  1640. typedef struct dispatch_shared_info64 {
  1641. /* chunk index under dynamic, number of idle threads under static-steal;
  1642. iteration index otherwise */
  1643. volatile kmp_uint64 iteration;
  1644. volatile kmp_int64 num_done;
  1645. volatile kmp_uint64 ordered_iteration;
  1646. // Dummy to retain the structure size after making ordered_iteration scalar
  1647. kmp_int64 ordered_dummy[KMP_MAX_ORDERED - 3];
  1648. } dispatch_shared_info64_t;
  1649. typedef struct dispatch_shared_info {
  1650. union shared_info {
  1651. dispatch_shared_info32_t s32;
  1652. dispatch_shared_info64_t s64;
  1653. } u;
  1654. volatile kmp_uint32 buffer_index;
  1655. volatile kmp_int32 doacross_buf_idx; // teamwise index
  1656. volatile kmp_uint32 *doacross_flags; // shared array of iteration flags (0/1)
  1657. kmp_int32 doacross_num_done; // count finished threads
  1658. #if KMP_USE_HIER_SCHED
  1659. void *hier;
  1660. #endif
  1661. #if KMP_USE_HWLOC
  1662. // When linking with libhwloc, the ORDERED EPCC test slows down on big
  1663. // machines (> 48 cores). Performance analysis showed that a cache thrash
  1664. // was occurring and this padding helps alleviate the problem.
  1665. char padding[64];
  1666. #endif
  1667. } dispatch_shared_info_t;
  1668. typedef struct kmp_disp {
  1669. /* Vector for ORDERED SECTION */
  1670. void (*th_deo_fcn)(int *gtid, int *cid, ident_t *);
  1671. /* Vector for END ORDERED SECTION */
  1672. void (*th_dxo_fcn)(int *gtid, int *cid, ident_t *);
  1673. dispatch_shared_info_t *th_dispatch_sh_current;
  1674. dispatch_private_info_t *th_dispatch_pr_current;
  1675. dispatch_private_info_t *th_disp_buffer;
  1676. kmp_uint32 th_disp_index;
  1677. kmp_int32 th_doacross_buf_idx; // thread's doacross buffer index
  1678. volatile kmp_uint32 *th_doacross_flags; // pointer to shared array of flags
  1679. kmp_int64 *th_doacross_info; // info on loop bounds
  1680. #if KMP_USE_INTERNODE_ALIGNMENT
  1681. char more_padding[INTERNODE_CACHE_LINE];
  1682. #endif
  1683. } kmp_disp_t;
  1684. /* ------------------------------------------------------------------------ */
  1685. /* Barrier stuff */
  1686. /* constants for barrier state update */
  1687. #define KMP_INIT_BARRIER_STATE 0 /* should probably start from zero */
  1688. #define KMP_BARRIER_SLEEP_BIT 0 /* bit used for suspend/sleep part of state */
  1689. #define KMP_BARRIER_UNUSED_BIT 1 // bit that must never be set for valid state
  1690. #define KMP_BARRIER_BUMP_BIT 2 /* lsb used for bump of go/arrived state */
  1691. #define KMP_BARRIER_SLEEP_STATE (1 << KMP_BARRIER_SLEEP_BIT)
  1692. #define KMP_BARRIER_UNUSED_STATE (1 << KMP_BARRIER_UNUSED_BIT)
  1693. #define KMP_BARRIER_STATE_BUMP (1 << KMP_BARRIER_BUMP_BIT)
  1694. #if (KMP_BARRIER_SLEEP_BIT >= KMP_BARRIER_BUMP_BIT)
  1695. #error "Barrier sleep bit must be smaller than barrier bump bit"
  1696. #endif
  1697. #if (KMP_BARRIER_UNUSED_BIT >= KMP_BARRIER_BUMP_BIT)
  1698. #error "Barrier unused bit must be smaller than barrier bump bit"
  1699. #endif
  1700. // Constants for release barrier wait state: currently, hierarchical only
  1701. #define KMP_BARRIER_NOT_WAITING 0 // Normal state; worker not in wait_sleep
  1702. #define KMP_BARRIER_OWN_FLAG \
  1703. 1 // Normal state; worker waiting on own b_go flag in release
  1704. #define KMP_BARRIER_PARENT_FLAG \
  1705. 2 // Special state; worker waiting on parent's b_go flag in release
  1706. #define KMP_BARRIER_SWITCH_TO_OWN_FLAG \
  1707. 3 // Special state; tells worker to shift from parent to own b_go
  1708. #define KMP_BARRIER_SWITCHING \
  1709. 4 // Special state; worker resets appropriate flag on wake-up
  1710. #define KMP_NOT_SAFE_TO_REAP \
  1711. 0 // Thread th_reap_state: not safe to reap (tasking)
  1712. #define KMP_SAFE_TO_REAP 1 // Thread th_reap_state: safe to reap (not tasking)
  1713. // The flag_type describes the storage used for the flag.
  1714. enum flag_type {
  1715. flag32, /**< atomic 32 bit flags */
  1716. flag64, /**< 64 bit flags */
  1717. atomic_flag64, /**< atomic 64 bit flags */
  1718. flag_oncore, /**< special 64-bit flag for on-core barrier (hierarchical) */
  1719. flag_unset
  1720. };
  1721. enum barrier_type {
  1722. bs_plain_barrier = 0, /* 0, All non-fork/join barriers (except reduction
  1723. barriers if enabled) */
  1724. bs_forkjoin_barrier, /* 1, All fork/join (parallel region) barriers */
  1725. #if KMP_FAST_REDUCTION_BARRIER
  1726. bs_reduction_barrier, /* 2, All barriers that are used in reduction */
  1727. #endif // KMP_FAST_REDUCTION_BARRIER
  1728. bs_last_barrier /* Just a placeholder to mark the end */
  1729. };
  1730. // to work with reduction barriers just like with plain barriers
  1731. #if !KMP_FAST_REDUCTION_BARRIER
  1732. #define bs_reduction_barrier bs_plain_barrier
  1733. #endif // KMP_FAST_REDUCTION_BARRIER
  1734. typedef enum kmp_bar_pat { /* Barrier communication patterns */
  1735. bp_linear_bar =
  1736. 0, /* Single level (degenerate) tree */
  1737. bp_tree_bar =
  1738. 1, /* Balanced tree with branching factor 2^n */
  1739. bp_hyper_bar = 2, /* Hypercube-embedded tree with min
  1740. branching factor 2^n */
  1741. bp_hierarchical_bar = 3, /* Machine hierarchy tree */
  1742. bp_dist_bar = 4, /* Distributed barrier */
  1743. bp_last_bar /* Placeholder to mark the end */
  1744. } kmp_bar_pat_e;
  1745. #define KMP_BARRIER_ICV_PUSH 1
  1746. /* Record for holding the values of the internal controls stack records */
  1747. typedef struct kmp_internal_control {
  1748. int serial_nesting_level; /* corresponds to the value of the
  1749. th_team_serialized field */
  1750. kmp_int8 dynamic; /* internal control for dynamic adjustment of threads (per
  1751. thread) */
  1752. kmp_int8
  1753. bt_set; /* internal control for whether blocktime is explicitly set */
  1754. int blocktime; /* internal control for blocktime */
  1755. #if KMP_USE_MONITOR
  1756. int bt_intervals; /* internal control for blocktime intervals */
  1757. #endif
  1758. int nproc; /* internal control for #threads for next parallel region (per
  1759. thread) */
  1760. int thread_limit; /* internal control for thread-limit-var */
  1761. int max_active_levels; /* internal control for max_active_levels */
  1762. kmp_r_sched_t
  1763. sched; /* internal control for runtime schedule {sched,chunk} pair */
  1764. kmp_proc_bind_t proc_bind; /* internal control for affinity */
  1765. kmp_int32 default_device; /* internal control for default device */
  1766. struct kmp_internal_control *next;
  1767. } kmp_internal_control_t;
  1768. static inline void copy_icvs(kmp_internal_control_t *dst,
  1769. kmp_internal_control_t *src) {
  1770. *dst = *src;
  1771. }
  1772. /* Thread barrier needs volatile barrier fields */
  1773. typedef struct KMP_ALIGN_CACHE kmp_bstate {
  1774. // th_fixed_icvs is aligned by virtue of kmp_bstate being aligned (and all
  1775. // uses of it). It is not explicitly aligned below, because we *don't* want
  1776. // it to be padded -- instead, we fit b_go into the same cache line with
  1777. // th_fixed_icvs, enabling NGO cache lines stores in the hierarchical barrier.
  1778. kmp_internal_control_t th_fixed_icvs; // Initial ICVs for the thread
  1779. // Tuck b_go into end of th_fixed_icvs cache line, so it can be stored with
  1780. // same NGO store
  1781. volatile kmp_uint64 b_go; // STATE => task should proceed (hierarchical)
  1782. KMP_ALIGN_CACHE volatile kmp_uint64
  1783. b_arrived; // STATE => task reached synch point.
  1784. kmp_uint32 *skip_per_level;
  1785. kmp_uint32 my_level;
  1786. kmp_int32 parent_tid;
  1787. kmp_int32 old_tid;
  1788. kmp_uint32 depth;
  1789. struct kmp_bstate *parent_bar;
  1790. kmp_team_t *team;
  1791. kmp_uint64 leaf_state;
  1792. kmp_uint32 nproc;
  1793. kmp_uint8 base_leaf_kids;
  1794. kmp_uint8 leaf_kids;
  1795. kmp_uint8 offset;
  1796. kmp_uint8 wait_flag;
  1797. kmp_uint8 use_oncore_barrier;
  1798. #if USE_DEBUGGER
  1799. // The following field is intended for the debugger solely. Only the worker
  1800. // thread itself accesses this field: the worker increases it by 1 when it
  1801. // arrives to a barrier.
  1802. KMP_ALIGN_CACHE kmp_uint b_worker_arrived;
  1803. #endif /* USE_DEBUGGER */
  1804. } kmp_bstate_t;
  1805. union KMP_ALIGN_CACHE kmp_barrier_union {
  1806. double b_align; /* use worst case alignment */
  1807. char b_pad[KMP_PAD(kmp_bstate_t, CACHE_LINE)];
  1808. kmp_bstate_t bb;
  1809. };
  1810. typedef union kmp_barrier_union kmp_balign_t;
  1811. /* Team barrier needs only non-volatile arrived counter */
  1812. union KMP_ALIGN_CACHE kmp_barrier_team_union {
  1813. double b_align; /* use worst case alignment */
  1814. char b_pad[CACHE_LINE];
  1815. struct {
  1816. kmp_uint64 b_arrived; /* STATE => task reached synch point. */
  1817. #if USE_DEBUGGER
  1818. // The following two fields are indended for the debugger solely. Only
  1819. // primary thread of the team accesses these fields: the first one is
  1820. // increased by 1 when the primary thread arrives to a barrier, the second
  1821. // one is increased by one when all the threads arrived.
  1822. kmp_uint b_master_arrived;
  1823. kmp_uint b_team_arrived;
  1824. #endif
  1825. };
  1826. };
  1827. typedef union kmp_barrier_team_union kmp_balign_team_t;
  1828. /* Padding for Linux* OS pthreads condition variables and mutexes used to signal
  1829. threads when a condition changes. This is to workaround an NPTL bug where
  1830. padding was added to pthread_cond_t which caused the initialization routine
  1831. to write outside of the structure if compiled on pre-NPTL threads. */
  1832. #if KMP_OS_WINDOWS
  1833. typedef struct kmp_win32_mutex {
  1834. /* The Lock */
  1835. CRITICAL_SECTION cs;
  1836. } kmp_win32_mutex_t;
  1837. typedef struct kmp_win32_cond {
  1838. /* Count of the number of waiters. */
  1839. int waiters_count_;
  1840. /* Serialize access to <waiters_count_> */
  1841. kmp_win32_mutex_t waiters_count_lock_;
  1842. /* Number of threads to release via a <cond_broadcast> or a <cond_signal> */
  1843. int release_count_;
  1844. /* Keeps track of the current "generation" so that we don't allow */
  1845. /* one thread to steal all the "releases" from the broadcast. */
  1846. int wait_generation_count_;
  1847. /* A manual-reset event that's used to block and release waiting threads. */
  1848. HANDLE event_;
  1849. } kmp_win32_cond_t;
  1850. #endif
  1851. #if KMP_OS_UNIX
  1852. union KMP_ALIGN_CACHE kmp_cond_union {
  1853. double c_align;
  1854. char c_pad[CACHE_LINE];
  1855. pthread_cond_t c_cond;
  1856. };
  1857. typedef union kmp_cond_union kmp_cond_align_t;
  1858. union KMP_ALIGN_CACHE kmp_mutex_union {
  1859. double m_align;
  1860. char m_pad[CACHE_LINE];
  1861. pthread_mutex_t m_mutex;
  1862. };
  1863. typedef union kmp_mutex_union kmp_mutex_align_t;
  1864. #endif /* KMP_OS_UNIX */
  1865. typedef struct kmp_desc_base {
  1866. void *ds_stackbase;
  1867. size_t ds_stacksize;
  1868. int ds_stackgrow;
  1869. kmp_thread_t ds_thread;
  1870. volatile int ds_tid;
  1871. int ds_gtid;
  1872. #if KMP_OS_WINDOWS
  1873. volatile int ds_alive;
  1874. DWORD ds_thread_id;
  1875. /* ds_thread keeps thread handle on Windows* OS. It is enough for RTL purposes.
  1876. However, debugger support (libomp_db) cannot work with handles, because they
  1877. uncomparable. For example, debugger requests info about thread with handle h.
  1878. h is valid within debugger process, and meaningless within debugee process.
  1879. Even if h is duped by call to DuplicateHandle(), so the result h' is valid
  1880. within debugee process, but it is a *new* handle which does *not* equal to
  1881. any other handle in debugee... The only way to compare handles is convert
  1882. them to system-wide ids. GetThreadId() function is available only in
  1883. Longhorn and Server 2003. :-( In contrast, GetCurrentThreadId() is available
  1884. on all Windows* OS flavours (including Windows* 95). Thus, we have to get
  1885. thread id by call to GetCurrentThreadId() from within the thread and save it
  1886. to let libomp_db identify threads. */
  1887. #endif /* KMP_OS_WINDOWS */
  1888. } kmp_desc_base_t;
  1889. typedef union KMP_ALIGN_CACHE kmp_desc {
  1890. double ds_align; /* use worst case alignment */
  1891. char ds_pad[KMP_PAD(kmp_desc_base_t, CACHE_LINE)];
  1892. kmp_desc_base_t ds;
  1893. } kmp_desc_t;
  1894. typedef struct kmp_local {
  1895. volatile int this_construct; /* count of single's encountered by thread */
  1896. void *reduce_data;
  1897. #if KMP_USE_BGET
  1898. void *bget_data;
  1899. void *bget_list;
  1900. #if !USE_CMP_XCHG_FOR_BGET
  1901. #ifdef USE_QUEUING_LOCK_FOR_BGET
  1902. kmp_lock_t bget_lock; /* Lock for accessing bget free list */
  1903. #else
  1904. kmp_bootstrap_lock_t bget_lock; // Lock for accessing bget free list. Must be
  1905. // bootstrap lock so we can use it at library
  1906. // shutdown.
  1907. #endif /* USE_LOCK_FOR_BGET */
  1908. #endif /* ! USE_CMP_XCHG_FOR_BGET */
  1909. #endif /* KMP_USE_BGET */
  1910. PACKED_REDUCTION_METHOD_T
  1911. packed_reduction_method; /* stored by __kmpc_reduce*(), used by
  1912. __kmpc_end_reduce*() */
  1913. } kmp_local_t;
  1914. #define KMP_CHECK_UPDATE(a, b) \
  1915. if ((a) != (b)) \
  1916. (a) = (b)
  1917. #define KMP_CHECK_UPDATE_SYNC(a, b) \
  1918. if ((a) != (b)) \
  1919. TCW_SYNC_PTR((a), (b))
  1920. #define get__blocktime(xteam, xtid) \
  1921. ((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.blocktime)
  1922. #define get__bt_set(xteam, xtid) \
  1923. ((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.bt_set)
  1924. #if KMP_USE_MONITOR
  1925. #define get__bt_intervals(xteam, xtid) \
  1926. ((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.bt_intervals)
  1927. #endif
  1928. #define get__dynamic_2(xteam, xtid) \
  1929. ((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.dynamic)
  1930. #define get__nproc_2(xteam, xtid) \
  1931. ((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.nproc)
  1932. #define get__sched_2(xteam, xtid) \
  1933. ((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.sched)
  1934. #define set__blocktime_team(xteam, xtid, xval) \
  1935. (((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.blocktime) = \
  1936. (xval))
  1937. #if KMP_USE_MONITOR
  1938. #define set__bt_intervals_team(xteam, xtid, xval) \
  1939. (((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.bt_intervals) = \
  1940. (xval))
  1941. #endif
  1942. #define set__bt_set_team(xteam, xtid, xval) \
  1943. (((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.bt_set) = (xval))
  1944. #define set__dynamic(xthread, xval) \
  1945. (((xthread)->th.th_current_task->td_icvs.dynamic) = (xval))
  1946. #define get__dynamic(xthread) \
  1947. (((xthread)->th.th_current_task->td_icvs.dynamic) ? (FTN_TRUE) : (FTN_FALSE))
  1948. #define set__nproc(xthread, xval) \
  1949. (((xthread)->th.th_current_task->td_icvs.nproc) = (xval))
  1950. #define set__thread_limit(xthread, xval) \
  1951. (((xthread)->th.th_current_task->td_icvs.thread_limit) = (xval))
  1952. #define set__max_active_levels(xthread, xval) \
  1953. (((xthread)->th.th_current_task->td_icvs.max_active_levels) = (xval))
  1954. #define get__max_active_levels(xthread) \
  1955. ((xthread)->th.th_current_task->td_icvs.max_active_levels)
  1956. #define set__sched(xthread, xval) \
  1957. (((xthread)->th.th_current_task->td_icvs.sched) = (xval))
  1958. #define set__proc_bind(xthread, xval) \
  1959. (((xthread)->th.th_current_task->td_icvs.proc_bind) = (xval))
  1960. #define get__proc_bind(xthread) \
  1961. ((xthread)->th.th_current_task->td_icvs.proc_bind)
  1962. // OpenMP tasking data structures
  1963. typedef enum kmp_tasking_mode {
  1964. tskm_immediate_exec = 0,
  1965. tskm_extra_barrier = 1,
  1966. tskm_task_teams = 2,
  1967. tskm_max = 2
  1968. } kmp_tasking_mode_t;
  1969. extern kmp_tasking_mode_t
  1970. __kmp_tasking_mode; /* determines how/when to execute tasks */
  1971. extern int __kmp_task_stealing_constraint;
  1972. extern int __kmp_enable_task_throttling;
  1973. extern kmp_int32 __kmp_default_device; // Set via OMP_DEFAULT_DEVICE if
  1974. // specified, defaults to 0 otherwise
  1975. // Set via OMP_MAX_TASK_PRIORITY if specified, defaults to 0 otherwise
  1976. extern kmp_int32 __kmp_max_task_priority;
  1977. // Set via KMP_TASKLOOP_MIN_TASKS if specified, defaults to 0 otherwise
  1978. extern kmp_uint64 __kmp_taskloop_min_tasks;
  1979. /* NOTE: kmp_taskdata_t and kmp_task_t structures allocated in single block with
  1980. taskdata first */
  1981. #define KMP_TASK_TO_TASKDATA(task) (((kmp_taskdata_t *)task) - 1)
  1982. #define KMP_TASKDATA_TO_TASK(taskdata) (kmp_task_t *)(taskdata + 1)
  1983. // The tt_found_tasks flag is a signal to all threads in the team that tasks
  1984. // were spawned and queued since the previous barrier release.
  1985. #define KMP_TASKING_ENABLED(task_team) \
  1986. (TRUE == TCR_SYNC_4((task_team)->tt.tt_found_tasks))
  1987. /*!
  1988. @ingroup BASIC_TYPES
  1989. @{
  1990. */
  1991. /*!
  1992. */
  1993. typedef kmp_int32 (*kmp_routine_entry_t)(kmp_int32, void *);
  1994. typedef union kmp_cmplrdata {
  1995. kmp_int32 priority; /**< priority specified by user for the task */
  1996. kmp_routine_entry_t
  1997. destructors; /* pointer to function to invoke deconstructors of
  1998. firstprivate C++ objects */
  1999. /* future data */
  2000. } kmp_cmplrdata_t;
  2001. /* sizeof_kmp_task_t passed as arg to kmpc_omp_task call */
  2002. /*!
  2003. */
  2004. typedef struct kmp_task { /* GEH: Shouldn't this be aligned somehow? */
  2005. void *shareds; /**< pointer to block of pointers to shared vars */
  2006. kmp_routine_entry_t
  2007. routine; /**< pointer to routine to call for executing task */
  2008. kmp_int32 part_id; /**< part id for the task */
  2009. kmp_cmplrdata_t
  2010. data1; /* Two known optional additions: destructors and priority */
  2011. kmp_cmplrdata_t data2; /* Process destructors first, priority second */
  2012. /* future data */
  2013. /* private vars */
  2014. } kmp_task_t;
  2015. /*!
  2016. @}
  2017. */
  2018. typedef struct kmp_taskgroup {
  2019. std::atomic<kmp_int32> count; // number of allocated and incomplete tasks
  2020. std::atomic<kmp_int32>
  2021. cancel_request; // request for cancellation of this taskgroup
  2022. struct kmp_taskgroup *parent; // parent taskgroup
  2023. // Block of data to perform task reduction
  2024. void *reduce_data; // reduction related info
  2025. kmp_int32 reduce_num_data; // number of data items to reduce
  2026. uintptr_t *gomp_data; // gomp reduction data
  2027. } kmp_taskgroup_t;
  2028. // forward declarations
  2029. typedef union kmp_depnode kmp_depnode_t;
  2030. typedef struct kmp_depnode_list kmp_depnode_list_t;
  2031. typedef struct kmp_dephash_entry kmp_dephash_entry_t;
  2032. // macros for checking dep flag as an integer
  2033. #define KMP_DEP_IN 0x1
  2034. #define KMP_DEP_OUT 0x2
  2035. #define KMP_DEP_INOUT 0x3
  2036. #define KMP_DEP_MTX 0x4
  2037. #define KMP_DEP_SET 0x8
  2038. #define KMP_DEP_ALL 0x80
  2039. // Compiler sends us this info:
  2040. typedef struct kmp_depend_info {
  2041. kmp_intptr_t base_addr;
  2042. size_t len;
  2043. union {
  2044. kmp_uint8 flag; // flag as an unsigned char
  2045. struct { // flag as a set of 8 bits
  2046. unsigned in : 1;
  2047. unsigned out : 1;
  2048. unsigned mtx : 1;
  2049. unsigned set : 1;
  2050. unsigned unused : 3;
  2051. unsigned all : 1;
  2052. } flags;
  2053. };
  2054. } kmp_depend_info_t;
  2055. // Internal structures to work with task dependencies:
  2056. struct kmp_depnode_list {
  2057. kmp_depnode_t *node;
  2058. kmp_depnode_list_t *next;
  2059. };
  2060. // Max number of mutexinoutset dependencies per node
  2061. #define MAX_MTX_DEPS 4
  2062. typedef struct kmp_base_depnode {
  2063. kmp_depnode_list_t *successors; /* used under lock */
  2064. kmp_task_t *task; /* non-NULL if depnode is active, used under lock */
  2065. kmp_lock_t *mtx_locks[MAX_MTX_DEPS]; /* lock mutexinoutset dependent tasks */
  2066. kmp_int32 mtx_num_locks; /* number of locks in mtx_locks array */
  2067. kmp_lock_t lock; /* guards shared fields: task, successors */
  2068. #if KMP_SUPPORT_GRAPH_OUTPUT
  2069. kmp_uint32 id;
  2070. #endif
  2071. std::atomic<kmp_int32> npredecessors;
  2072. std::atomic<kmp_int32> nrefs;
  2073. } kmp_base_depnode_t;
  2074. union KMP_ALIGN_CACHE kmp_depnode {
  2075. double dn_align; /* use worst case alignment */
  2076. char dn_pad[KMP_PAD(kmp_base_depnode_t, CACHE_LINE)];
  2077. kmp_base_depnode_t dn;
  2078. };
  2079. struct kmp_dephash_entry {
  2080. kmp_intptr_t addr;
  2081. kmp_depnode_t *last_out;
  2082. kmp_depnode_list_t *last_set;
  2083. kmp_depnode_list_t *prev_set;
  2084. kmp_uint8 last_flag;
  2085. kmp_lock_t *mtx_lock; /* is referenced by depnodes w/mutexinoutset dep */
  2086. kmp_dephash_entry_t *next_in_bucket;
  2087. };
  2088. typedef struct kmp_dephash {
  2089. kmp_dephash_entry_t **buckets;
  2090. size_t size;
  2091. kmp_depnode_t *last_all;
  2092. size_t generation;
  2093. kmp_uint32 nelements;
  2094. kmp_uint32 nconflicts;
  2095. } kmp_dephash_t;
  2096. typedef struct kmp_task_affinity_info {
  2097. kmp_intptr_t base_addr;
  2098. size_t len;
  2099. struct {
  2100. bool flag1 : 1;
  2101. bool flag2 : 1;
  2102. kmp_int32 reserved : 30;
  2103. } flags;
  2104. } kmp_task_affinity_info_t;
  2105. typedef enum kmp_event_type_t {
  2106. KMP_EVENT_UNINITIALIZED = 0,
  2107. KMP_EVENT_ALLOW_COMPLETION = 1
  2108. } kmp_event_type_t;
  2109. typedef struct {
  2110. kmp_event_type_t type;
  2111. kmp_tas_lock_t lock;
  2112. union {
  2113. kmp_task_t *task;
  2114. } ed;
  2115. } kmp_event_t;
  2116. #ifdef BUILD_TIED_TASK_STACK
  2117. /* Tied Task stack definitions */
  2118. typedef struct kmp_stack_block {
  2119. kmp_taskdata_t *sb_block[TASK_STACK_BLOCK_SIZE];
  2120. struct kmp_stack_block *sb_next;
  2121. struct kmp_stack_block *sb_prev;
  2122. } kmp_stack_block_t;
  2123. typedef struct kmp_task_stack {
  2124. kmp_stack_block_t ts_first_block; // first block of stack entries
  2125. kmp_taskdata_t **ts_top; // pointer to the top of stack
  2126. kmp_int32 ts_entries; // number of entries on the stack
  2127. } kmp_task_stack_t;
  2128. #endif // BUILD_TIED_TASK_STACK
  2129. typedef struct kmp_tasking_flags { /* Total struct must be exactly 32 bits */
  2130. /* Compiler flags */ /* Total compiler flags must be 16 bits */
  2131. unsigned tiedness : 1; /* task is either tied (1) or untied (0) */
  2132. unsigned final : 1; /* task is final(1) so execute immediately */
  2133. unsigned merged_if0 : 1; /* no __kmpc_task_{begin/complete}_if0 calls in if0
  2134. code path */
  2135. unsigned destructors_thunk : 1; /* set if the compiler creates a thunk to
  2136. invoke destructors from the runtime */
  2137. unsigned proxy : 1; /* task is a proxy task (it will be executed outside the
  2138. context of the RTL) */
  2139. unsigned priority_specified : 1; /* set if the compiler provides priority
  2140. setting for the task */
  2141. unsigned detachable : 1; /* 1 == can detach */
  2142. unsigned hidden_helper : 1; /* 1 == hidden helper task */
  2143. unsigned reserved : 8; /* reserved for compiler use */
  2144. /* Library flags */ /* Total library flags must be 16 bits */
  2145. unsigned tasktype : 1; /* task is either explicit(1) or implicit (0) */
  2146. unsigned task_serial : 1; // task is executed immediately (1) or deferred (0)
  2147. unsigned tasking_ser : 1; // all tasks in team are either executed immediately
  2148. // (1) or may be deferred (0)
  2149. unsigned team_serial : 1; // entire team is serial (1) [1 thread] or parallel
  2150. // (0) [>= 2 threads]
  2151. /* If either team_serial or tasking_ser is set, task team may be NULL */
  2152. /* Task State Flags: */
  2153. unsigned started : 1; /* 1==started, 0==not started */
  2154. unsigned executing : 1; /* 1==executing, 0==not executing */
  2155. unsigned complete : 1; /* 1==complete, 0==not complete */
  2156. unsigned freed : 1; /* 1==freed, 0==allocated */
  2157. unsigned native : 1; /* 1==gcc-compiled task, 0==intel */
  2158. unsigned reserved31 : 7; /* reserved for library use */
  2159. } kmp_tasking_flags_t;
  2160. struct kmp_taskdata { /* aligned during dynamic allocation */
  2161. kmp_int32 td_task_id; /* id, assigned by debugger */
  2162. kmp_tasking_flags_t td_flags; /* task flags */
  2163. kmp_team_t *td_team; /* team for this task */
  2164. kmp_info_p *td_alloc_thread; /* thread that allocated data structures */
  2165. /* Currently not used except for perhaps IDB */
  2166. kmp_taskdata_t *td_parent; /* parent task */
  2167. kmp_int32 td_level; /* task nesting level */
  2168. std::atomic<kmp_int32> td_untied_count; // untied task active parts counter
  2169. ident_t *td_ident; /* task identifier */
  2170. // Taskwait data.
  2171. ident_t *td_taskwait_ident;
  2172. kmp_uint32 td_taskwait_counter;
  2173. kmp_int32 td_taskwait_thread; /* gtid + 1 of thread encountered taskwait */
  2174. KMP_ALIGN_CACHE kmp_internal_control_t
  2175. td_icvs; /* Internal control variables for the task */
  2176. KMP_ALIGN_CACHE std::atomic<kmp_int32>
  2177. td_allocated_child_tasks; /* Child tasks (+ current task) not yet
  2178. deallocated */
  2179. std::atomic<kmp_int32>
  2180. td_incomplete_child_tasks; /* Child tasks not yet complete */
  2181. kmp_taskgroup_t
  2182. *td_taskgroup; // Each task keeps pointer to its current taskgroup
  2183. kmp_dephash_t
  2184. *td_dephash; // Dependencies for children tasks are tracked from here
  2185. kmp_depnode_t
  2186. *td_depnode; // Pointer to graph node if this task has dependencies
  2187. kmp_task_team_t *td_task_team;
  2188. size_t td_size_alloc; // Size of task structure, including shareds etc.
  2189. #if defined(KMP_GOMP_COMPAT)
  2190. // 4 or 8 byte integers for the loop bounds in GOMP_taskloop
  2191. kmp_int32 td_size_loop_bounds;
  2192. #endif
  2193. kmp_taskdata_t *td_last_tied; // keep tied task for task scheduling constraint
  2194. #if defined(KMP_GOMP_COMPAT)
  2195. // GOMP sends in a copy function for copy constructors
  2196. void (*td_copy_func)(void *, void *);
  2197. #endif
  2198. kmp_event_t td_allow_completion_event;
  2199. #if OMPT_SUPPORT
  2200. ompt_task_info_t ompt_task_info;
  2201. #endif
  2202. }; // struct kmp_taskdata
  2203. // Make sure padding above worked
  2204. KMP_BUILD_ASSERT(sizeof(kmp_taskdata_t) % sizeof(void *) == 0);
  2205. // Data for task team but per thread
  2206. typedef struct kmp_base_thread_data {
  2207. kmp_info_p *td_thr; // Pointer back to thread info
  2208. // Used only in __kmp_execute_tasks_template, maybe not avail until task is
  2209. // queued?
  2210. kmp_bootstrap_lock_t td_deque_lock; // Lock for accessing deque
  2211. kmp_taskdata_t *
  2212. *td_deque; // Deque of tasks encountered by td_thr, dynamically allocated
  2213. kmp_int32 td_deque_size; // Size of deck
  2214. kmp_uint32 td_deque_head; // Head of deque (will wrap)
  2215. kmp_uint32 td_deque_tail; // Tail of deque (will wrap)
  2216. kmp_int32 td_deque_ntasks; // Number of tasks in deque
  2217. // GEH: shouldn't this be volatile since used in while-spin?
  2218. kmp_int32 td_deque_last_stolen; // Thread number of last successful steal
  2219. #ifdef BUILD_TIED_TASK_STACK
  2220. kmp_task_stack_t td_susp_tied_tasks; // Stack of suspended tied tasks for task
  2221. // scheduling constraint
  2222. #endif // BUILD_TIED_TASK_STACK
  2223. } kmp_base_thread_data_t;
  2224. #define TASK_DEQUE_BITS 8 // Used solely to define INITIAL_TASK_DEQUE_SIZE
  2225. #define INITIAL_TASK_DEQUE_SIZE (1 << TASK_DEQUE_BITS)
  2226. #define TASK_DEQUE_SIZE(td) ((td).td_deque_size)
  2227. #define TASK_DEQUE_MASK(td) ((td).td_deque_size - 1)
  2228. typedef union KMP_ALIGN_CACHE kmp_thread_data {
  2229. kmp_base_thread_data_t td;
  2230. double td_align; /* use worst case alignment */
  2231. char td_pad[KMP_PAD(kmp_base_thread_data_t, CACHE_LINE)];
  2232. } kmp_thread_data_t;
  2233. typedef struct kmp_task_pri {
  2234. kmp_thread_data_t td;
  2235. kmp_int32 priority;
  2236. kmp_task_pri *next;
  2237. } kmp_task_pri_t;
  2238. // Data for task teams which are used when tasking is enabled for the team
  2239. typedef struct kmp_base_task_team {
  2240. kmp_bootstrap_lock_t
  2241. tt_threads_lock; /* Lock used to allocate per-thread part of task team */
  2242. /* must be bootstrap lock since used at library shutdown*/
  2243. // TODO: check performance vs kmp_tas_lock_t
  2244. kmp_bootstrap_lock_t tt_task_pri_lock; /* Lock to access priority tasks */
  2245. kmp_task_pri_t *tt_task_pri_list;
  2246. kmp_task_team_t *tt_next; /* For linking the task team free list */
  2247. kmp_thread_data_t
  2248. *tt_threads_data; /* Array of per-thread structures for task team */
  2249. /* Data survives task team deallocation */
  2250. kmp_int32 tt_found_tasks; /* Have we found tasks and queued them while
  2251. executing this team? */
  2252. /* TRUE means tt_threads_data is set up and initialized */
  2253. kmp_int32 tt_nproc; /* #threads in team */
  2254. kmp_int32 tt_max_threads; // # entries allocated for threads_data array
  2255. kmp_int32 tt_found_proxy_tasks; // found proxy tasks since last barrier
  2256. kmp_int32 tt_untied_task_encountered;
  2257. std::atomic<kmp_int32> tt_num_task_pri; // number of priority tasks enqueued
  2258. // There is hidden helper thread encountered in this task team so that we must
  2259. // wait when waiting on task team
  2260. kmp_int32 tt_hidden_helper_task_encountered;
  2261. KMP_ALIGN_CACHE
  2262. std::atomic<kmp_int32> tt_unfinished_threads; /* #threads still active */
  2263. KMP_ALIGN_CACHE
  2264. volatile kmp_uint32
  2265. tt_active; /* is the team still actively executing tasks */
  2266. } kmp_base_task_team_t;
  2267. union KMP_ALIGN_CACHE kmp_task_team {
  2268. kmp_base_task_team_t tt;
  2269. double tt_align; /* use worst case alignment */
  2270. char tt_pad[KMP_PAD(kmp_base_task_team_t, CACHE_LINE)];
  2271. };
  2272. #if (USE_FAST_MEMORY == 3) || (USE_FAST_MEMORY == 5)
  2273. // Free lists keep same-size free memory slots for fast memory allocation
  2274. // routines
  2275. typedef struct kmp_free_list {
  2276. void *th_free_list_self; // Self-allocated tasks free list
  2277. void *th_free_list_sync; // Self-allocated tasks stolen/returned by other
  2278. // threads
  2279. void *th_free_list_other; // Non-self free list (to be returned to owner's
  2280. // sync list)
  2281. } kmp_free_list_t;
  2282. #endif
  2283. #if KMP_NESTED_HOT_TEAMS
  2284. // Hot teams array keeps hot teams and their sizes for given thread. Hot teams
  2285. // are not put in teams pool, and they don't put threads in threads pool.
  2286. typedef struct kmp_hot_team_ptr {
  2287. kmp_team_p *hot_team; // pointer to hot_team of given nesting level
  2288. kmp_int32 hot_team_nth; // number of threads allocated for the hot_team
  2289. } kmp_hot_team_ptr_t;
  2290. #endif
  2291. typedef struct kmp_teams_size {
  2292. kmp_int32 nteams; // number of teams in a league
  2293. kmp_int32 nth; // number of threads in each team of the league
  2294. } kmp_teams_size_t;
  2295. // This struct stores a thread that acts as a "root" for a contention
  2296. // group. Contention groups are rooted at kmp_root threads, but also at
  2297. // each primary thread of each team created in the teams construct.
  2298. // This struct therefore also stores a thread_limit associated with
  2299. // that contention group, and a counter to track the number of threads
  2300. // active in that contention group. Each thread has a list of these: CG
  2301. // root threads have an entry in their list in which cg_root refers to
  2302. // the thread itself, whereas other workers in the CG will have a
  2303. // single entry where cg_root is same as the entry containing their CG
  2304. // root. When a thread encounters a teams construct, it will add a new
  2305. // entry to the front of its list, because it now roots a new CG.
  2306. typedef struct kmp_cg_root {
  2307. kmp_info_p *cg_root; // "root" thread for a contention group
  2308. // The CG root's limit comes from OMP_THREAD_LIMIT for root threads, or
  2309. // thread_limit clause for teams primary threads
  2310. kmp_int32 cg_thread_limit;
  2311. kmp_int32 cg_nthreads; // Count of active threads in CG rooted at cg_root
  2312. struct kmp_cg_root *up; // pointer to higher level CG root in list
  2313. } kmp_cg_root_t;
  2314. // OpenMP thread data structures
  2315. typedef struct KMP_ALIGN_CACHE kmp_base_info {
  2316. /* Start with the readonly data which is cache aligned and padded. This is
  2317. written before the thread starts working by the primary thread. Uber
  2318. masters may update themselves later. Usage does not consider serialized
  2319. regions. */
  2320. kmp_desc_t th_info;
  2321. kmp_team_p *th_team; /* team we belong to */
  2322. kmp_root_p *th_root; /* pointer to root of task hierarchy */
  2323. kmp_info_p *th_next_pool; /* next available thread in the pool */
  2324. kmp_disp_t *th_dispatch; /* thread's dispatch data */
  2325. int th_in_pool; /* in thread pool (32 bits for TCR/TCW) */
  2326. /* The following are cached from the team info structure */
  2327. /* TODO use these in more places as determined to be needed via profiling */
  2328. int th_team_nproc; /* number of threads in a team */
  2329. kmp_info_p *th_team_master; /* the team's primary thread */
  2330. int th_team_serialized; /* team is serialized */
  2331. microtask_t th_teams_microtask; /* save entry address for teams construct */
  2332. int th_teams_level; /* save initial level of teams construct */
  2333. /* it is 0 on device but may be any on host */
  2334. /* The blocktime info is copied from the team struct to the thread struct */
  2335. /* at the start of a barrier, and the values stored in the team are used */
  2336. /* at points in the code where the team struct is no longer guaranteed */
  2337. /* to exist (from the POV of worker threads). */
  2338. #if KMP_USE_MONITOR
  2339. int th_team_bt_intervals;
  2340. int th_team_bt_set;
  2341. #else
  2342. kmp_uint64 th_team_bt_intervals;
  2343. #endif
  2344. #if KMP_AFFINITY_SUPPORTED
  2345. kmp_affin_mask_t *th_affin_mask; /* thread's current affinity mask */
  2346. #endif
  2347. omp_allocator_handle_t th_def_allocator; /* default allocator */
  2348. /* The data set by the primary thread at reinit, then R/W by the worker */
  2349. KMP_ALIGN_CACHE int
  2350. th_set_nproc; /* if > 0, then only use this request for the next fork */
  2351. #if KMP_NESTED_HOT_TEAMS
  2352. kmp_hot_team_ptr_t *th_hot_teams; /* array of hot teams */
  2353. #endif
  2354. kmp_proc_bind_t
  2355. th_set_proc_bind; /* if != proc_bind_default, use request for next fork */
  2356. kmp_teams_size_t
  2357. th_teams_size; /* number of teams/threads in teams construct */
  2358. #if KMP_AFFINITY_SUPPORTED
  2359. int th_current_place; /* place currently bound to */
  2360. int th_new_place; /* place to bind to in par reg */
  2361. int th_first_place; /* first place in partition */
  2362. int th_last_place; /* last place in partition */
  2363. #endif
  2364. int th_prev_level; /* previous level for affinity format */
  2365. int th_prev_num_threads; /* previous num_threads for affinity format */
  2366. #if USE_ITT_BUILD
  2367. kmp_uint64 th_bar_arrive_time; /* arrival to barrier timestamp */
  2368. kmp_uint64 th_bar_min_time; /* minimum arrival time at the barrier */
  2369. kmp_uint64 th_frame_time; /* frame timestamp */
  2370. #endif /* USE_ITT_BUILD */
  2371. kmp_local_t th_local;
  2372. struct private_common *th_pri_head;
  2373. /* Now the data only used by the worker (after initial allocation) */
  2374. /* TODO the first serial team should actually be stored in the info_t
  2375. structure. this will help reduce initial allocation overhead */
  2376. KMP_ALIGN_CACHE kmp_team_p
  2377. *th_serial_team; /*serialized team held in reserve*/
  2378. #if OMPT_SUPPORT
  2379. ompt_thread_info_t ompt_thread_info;
  2380. #endif
  2381. /* The following are also read by the primary thread during reinit */
  2382. struct common_table *th_pri_common;
  2383. volatile kmp_uint32 th_spin_here; /* thread-local location for spinning */
  2384. /* while awaiting queuing lock acquire */
  2385. volatile void *th_sleep_loc; // this points at a kmp_flag<T>
  2386. flag_type th_sleep_loc_type; // enum type of flag stored in th_sleep_loc
  2387. ident_t *th_ident;
  2388. unsigned th_x; // Random number generator data
  2389. unsigned th_a; // Random number generator data
  2390. /* Tasking-related data for the thread */
  2391. kmp_task_team_t *th_task_team; // Task team struct
  2392. kmp_taskdata_t *th_current_task; // Innermost Task being executed
  2393. kmp_uint8 th_task_state; // alternating 0/1 for task team identification
  2394. kmp_uint8 *th_task_state_memo_stack; // Stack holding memos of th_task_state
  2395. // at nested levels
  2396. kmp_uint32 th_task_state_top; // Top element of th_task_state_memo_stack
  2397. kmp_uint32 th_task_state_stack_sz; // Size of th_task_state_memo_stack
  2398. kmp_uint32 th_reap_state; // Non-zero indicates thread is not
  2399. // tasking, thus safe to reap
  2400. /* More stuff for keeping track of active/sleeping threads (this part is
  2401. written by the worker thread) */
  2402. kmp_uint8 th_active_in_pool; // included in count of #active threads in pool
  2403. int th_active; // ! sleeping; 32 bits for TCR/TCW
  2404. std::atomic<kmp_uint32> th_used_in_team; // Flag indicating use in team
  2405. // 0 = not used in team; 1 = used in team;
  2406. // 2 = transitioning to not used in team; 3 = transitioning to used in team
  2407. struct cons_header *th_cons; // used for consistency check
  2408. #if KMP_USE_HIER_SCHED
  2409. // used for hierarchical scheduling
  2410. kmp_hier_private_bdata_t *th_hier_bar_data;
  2411. #endif
  2412. /* Add the syncronizing data which is cache aligned and padded. */
  2413. KMP_ALIGN_CACHE kmp_balign_t th_bar[bs_last_barrier];
  2414. KMP_ALIGN_CACHE volatile kmp_int32
  2415. th_next_waiting; /* gtid+1 of next thread on lock wait queue, 0 if none */
  2416. #if (USE_FAST_MEMORY == 3) || (USE_FAST_MEMORY == 5)
  2417. #define NUM_LISTS 4
  2418. kmp_free_list_t th_free_lists[NUM_LISTS]; // Free lists for fast memory
  2419. // allocation routines
  2420. #endif
  2421. #if KMP_OS_WINDOWS
  2422. kmp_win32_cond_t th_suspend_cv;
  2423. kmp_win32_mutex_t th_suspend_mx;
  2424. std::atomic<int> th_suspend_init;
  2425. #endif
  2426. #if KMP_OS_UNIX
  2427. kmp_cond_align_t th_suspend_cv;
  2428. kmp_mutex_align_t th_suspend_mx;
  2429. std::atomic<int> th_suspend_init_count;
  2430. #endif
  2431. #if USE_ITT_BUILD
  2432. kmp_itt_mark_t th_itt_mark_single;
  2433. // alignment ???
  2434. #endif /* USE_ITT_BUILD */
  2435. #if KMP_STATS_ENABLED
  2436. kmp_stats_list *th_stats;
  2437. #endif
  2438. #if KMP_OS_UNIX
  2439. std::atomic<bool> th_blocking;
  2440. #endif
  2441. kmp_cg_root_t *th_cg_roots; // list of cg_roots associated with this thread
  2442. } kmp_base_info_t;
  2443. typedef union KMP_ALIGN_CACHE kmp_info {
  2444. double th_align; /* use worst case alignment */
  2445. char th_pad[KMP_PAD(kmp_base_info_t, CACHE_LINE)];
  2446. kmp_base_info_t th;
  2447. } kmp_info_t;
  2448. // OpenMP thread team data structures
  2449. typedef struct kmp_base_data {
  2450. volatile kmp_uint32 t_value;
  2451. } kmp_base_data_t;
  2452. typedef union KMP_ALIGN_CACHE kmp_sleep_team {
  2453. double dt_align; /* use worst case alignment */
  2454. char dt_pad[KMP_PAD(kmp_base_data_t, CACHE_LINE)];
  2455. kmp_base_data_t dt;
  2456. } kmp_sleep_team_t;
  2457. typedef union KMP_ALIGN_CACHE kmp_ordered_team {
  2458. double dt_align; /* use worst case alignment */
  2459. char dt_pad[KMP_PAD(kmp_base_data_t, CACHE_LINE)];
  2460. kmp_base_data_t dt;
  2461. } kmp_ordered_team_t;
  2462. typedef int (*launch_t)(int gtid);
  2463. /* Minimum number of ARGV entries to malloc if necessary */
  2464. #define KMP_MIN_MALLOC_ARGV_ENTRIES 100
  2465. // Set up how many argv pointers will fit in cache lines containing
  2466. // t_inline_argv. Historically, we have supported at least 96 bytes. Using a
  2467. // larger value for more space between the primary write/worker read section and
  2468. // read/write by all section seems to buy more performance on EPCC PARALLEL.
  2469. #if KMP_ARCH_X86 || KMP_ARCH_X86_64
  2470. #define KMP_INLINE_ARGV_BYTES \
  2471. (4 * CACHE_LINE - \
  2472. ((3 * KMP_PTR_SKIP + 2 * sizeof(int) + 2 * sizeof(kmp_int8) + \
  2473. sizeof(kmp_int16) + sizeof(kmp_uint32)) % \
  2474. CACHE_LINE))
  2475. #else
  2476. #define KMP_INLINE_ARGV_BYTES \
  2477. (2 * CACHE_LINE - ((3 * KMP_PTR_SKIP + 2 * sizeof(int)) % CACHE_LINE))
  2478. #endif
  2479. #define KMP_INLINE_ARGV_ENTRIES (int)(KMP_INLINE_ARGV_BYTES / KMP_PTR_SKIP)
  2480. typedef struct KMP_ALIGN_CACHE kmp_base_team {
  2481. // Synchronization Data
  2482. // ---------------------------------------------------------------------------
  2483. KMP_ALIGN_CACHE kmp_ordered_team_t t_ordered;
  2484. kmp_balign_team_t t_bar[bs_last_barrier];
  2485. std::atomic<int> t_construct; // count of single directive encountered by team
  2486. char pad[sizeof(kmp_lock_t)]; // padding to maintain performance on big iron
  2487. // [0] - parallel / [1] - worksharing task reduction data shared by taskgroups
  2488. std::atomic<void *> t_tg_reduce_data[2]; // to support task modifier
  2489. std::atomic<int> t_tg_fini_counter[2]; // sync end of task reductions
  2490. // Primary thread only
  2491. // ---------------------------------------------------------------------------
  2492. KMP_ALIGN_CACHE int t_master_tid; // tid of primary thread in parent team
  2493. int t_master_this_cons; // "this_construct" single counter of primary thread
  2494. // in parent team
  2495. ident_t *t_ident; // if volatile, have to change too much other crud to
  2496. // volatile too
  2497. kmp_team_p *t_parent; // parent team
  2498. kmp_team_p *t_next_pool; // next free team in the team pool
  2499. kmp_disp_t *t_dispatch; // thread's dispatch data
  2500. kmp_task_team_t *t_task_team[2]; // Task team struct; switch between 2
  2501. kmp_proc_bind_t t_proc_bind; // bind type for par region
  2502. #if USE_ITT_BUILD
  2503. kmp_uint64 t_region_time; // region begin timestamp
  2504. #endif /* USE_ITT_BUILD */
  2505. // Primary thread write, workers read
  2506. // --------------------------------------------------------------------------
  2507. KMP_ALIGN_CACHE void **t_argv;
  2508. int t_argc;
  2509. int t_nproc; // number of threads in team
  2510. microtask_t t_pkfn;
  2511. launch_t t_invoke; // procedure to launch the microtask
  2512. #if OMPT_SUPPORT
  2513. ompt_team_info_t ompt_team_info;
  2514. ompt_lw_taskteam_t *ompt_serialized_team_info;
  2515. #endif
  2516. #if KMP_ARCH_X86 || KMP_ARCH_X86_64
  2517. kmp_int8 t_fp_control_saved;
  2518. kmp_int8 t_pad2b;
  2519. kmp_int16 t_x87_fpu_control_word; // FP control regs
  2520. kmp_uint32 t_mxcsr;
  2521. #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
  2522. void *t_inline_argv[KMP_INLINE_ARGV_ENTRIES];
  2523. KMP_ALIGN_CACHE kmp_info_t **t_threads;
  2524. kmp_taskdata_t
  2525. *t_implicit_task_taskdata; // Taskdata for the thread's implicit task
  2526. int t_level; // nested parallel level
  2527. KMP_ALIGN_CACHE int t_max_argc;
  2528. int t_max_nproc; // max threads this team can handle (dynamically expandable)
  2529. int t_serialized; // levels deep of serialized teams
  2530. dispatch_shared_info_t *t_disp_buffer; // buffers for dispatch system
  2531. int t_id; // team's id, assigned by debugger.
  2532. int t_active_level; // nested active parallel level
  2533. kmp_r_sched_t t_sched; // run-time schedule for the team
  2534. #if KMP_AFFINITY_SUPPORTED
  2535. int t_first_place; // first & last place in parent thread's partition.
  2536. int t_last_place; // Restore these values to primary thread after par region.
  2537. #endif // KMP_AFFINITY_SUPPORTED
  2538. int t_display_affinity;
  2539. int t_size_changed; // team size was changed?: 0: no, 1: yes, -1: changed via
  2540. // omp_set_num_threads() call
  2541. omp_allocator_handle_t t_def_allocator; /* default allocator */
  2542. // Read/write by workers as well
  2543. #if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
  2544. // Using CACHE_LINE=64 reduces memory footprint, but causes a big perf
  2545. // regression of epcc 'parallel' and 'barrier' on fxe256lin01. This extra
  2546. // padding serves to fix the performance of epcc 'parallel' and 'barrier' when
  2547. // CACHE_LINE=64. TODO: investigate more and get rid if this padding.
  2548. char dummy_padding[1024];
  2549. #endif
  2550. // Internal control stack for additional nested teams.
  2551. KMP_ALIGN_CACHE kmp_internal_control_t *t_control_stack_top;
  2552. // for SERIALIZED teams nested 2 or more levels deep
  2553. // typed flag to store request state of cancellation
  2554. std::atomic<kmp_int32> t_cancel_request;
  2555. int t_master_active; // save on fork, restore on join
  2556. void *t_copypriv_data; // team specific pointer to copyprivate data array
  2557. #if KMP_OS_WINDOWS
  2558. std::atomic<kmp_uint32> t_copyin_counter;
  2559. #endif
  2560. #if USE_ITT_BUILD
  2561. void *t_stack_id; // team specific stack stitching id (for ittnotify)
  2562. #endif /* USE_ITT_BUILD */
  2563. distributedBarrier *b; // Distributed barrier data associated with team
  2564. } kmp_base_team_t;
  2565. union KMP_ALIGN_CACHE kmp_team {
  2566. kmp_base_team_t t;
  2567. double t_align; /* use worst case alignment */
  2568. char t_pad[KMP_PAD(kmp_base_team_t, CACHE_LINE)];
  2569. };
  2570. typedef union KMP_ALIGN_CACHE kmp_time_global {
  2571. double dt_align; /* use worst case alignment */
  2572. char dt_pad[KMP_PAD(kmp_base_data_t, CACHE_LINE)];
  2573. kmp_base_data_t dt;
  2574. } kmp_time_global_t;
  2575. typedef struct kmp_base_global {
  2576. /* cache-aligned */
  2577. kmp_time_global_t g_time;
  2578. /* non cache-aligned */
  2579. volatile int g_abort;
  2580. volatile int g_done;
  2581. int g_dynamic;
  2582. enum dynamic_mode g_dynamic_mode;
  2583. } kmp_base_global_t;
  2584. typedef union KMP_ALIGN_CACHE kmp_global {
  2585. kmp_base_global_t g;
  2586. double g_align; /* use worst case alignment */
  2587. char g_pad[KMP_PAD(kmp_base_global_t, CACHE_LINE)];
  2588. } kmp_global_t;
  2589. typedef struct kmp_base_root {
  2590. // TODO: GEH - combine r_active with r_in_parallel then r_active ==
  2591. // (r_in_parallel>= 0)
  2592. // TODO: GEH - then replace r_active with t_active_levels if we can to reduce
  2593. // the synch overhead or keeping r_active
  2594. volatile int r_active; /* TRUE if some region in a nest has > 1 thread */
  2595. // keeps a count of active parallel regions per root
  2596. std::atomic<int> r_in_parallel;
  2597. // GEH: This is misnamed, should be r_active_levels
  2598. kmp_team_t *r_root_team;
  2599. kmp_team_t *r_hot_team;
  2600. kmp_info_t *r_uber_thread;
  2601. kmp_lock_t r_begin_lock;
  2602. volatile int r_begin;
  2603. int r_blocktime; /* blocktime for this root and descendants */
  2604. #if KMP_AFFINITY_SUPPORTED
  2605. int r_affinity_assigned;
  2606. #endif // KMP_AFFINITY_SUPPORTED
  2607. } kmp_base_root_t;
  2608. typedef union KMP_ALIGN_CACHE kmp_root {
  2609. kmp_base_root_t r;
  2610. double r_align; /* use worst case alignment */
  2611. char r_pad[KMP_PAD(kmp_base_root_t, CACHE_LINE)];
  2612. } kmp_root_t;
  2613. struct fortran_inx_info {
  2614. kmp_int32 data;
  2615. };
  2616. // This list type exists to hold old __kmp_threads arrays so that
  2617. // old references to them may complete while reallocation takes place when
  2618. // expanding the array. The items in this list are kept alive until library
  2619. // shutdown.
  2620. typedef struct kmp_old_threads_list_t {
  2621. kmp_info_t **threads;
  2622. struct kmp_old_threads_list_t *next;
  2623. } kmp_old_threads_list_t;
  2624. /* ------------------------------------------------------------------------ */
  2625. extern int __kmp_settings;
  2626. extern int __kmp_duplicate_library_ok;
  2627. #if USE_ITT_BUILD
  2628. extern int __kmp_forkjoin_frames;
  2629. extern int __kmp_forkjoin_frames_mode;
  2630. #endif
  2631. extern PACKED_REDUCTION_METHOD_T __kmp_force_reduction_method;
  2632. extern int __kmp_determ_red;
  2633. #ifdef KMP_DEBUG
  2634. extern int kmp_a_debug;
  2635. extern int kmp_b_debug;
  2636. extern int kmp_c_debug;
  2637. extern int kmp_d_debug;
  2638. extern int kmp_e_debug;
  2639. extern int kmp_f_debug;
  2640. #endif /* KMP_DEBUG */
  2641. /* For debug information logging using rotating buffer */
  2642. #define KMP_DEBUG_BUF_LINES_INIT 512
  2643. #define KMP_DEBUG_BUF_LINES_MIN 1
  2644. #define KMP_DEBUG_BUF_CHARS_INIT 128
  2645. #define KMP_DEBUG_BUF_CHARS_MIN 2
  2646. extern int
  2647. __kmp_debug_buf; /* TRUE means use buffer, FALSE means print to stderr */
  2648. extern int __kmp_debug_buf_lines; /* How many lines of debug stored in buffer */
  2649. extern int
  2650. __kmp_debug_buf_chars; /* How many characters allowed per line in buffer */
  2651. extern int __kmp_debug_buf_atomic; /* TRUE means use atomic update of buffer
  2652. entry pointer */
  2653. extern char *__kmp_debug_buffer; /* Debug buffer itself */
  2654. extern std::atomic<int> __kmp_debug_count; /* Counter for number of lines
  2655. printed in buffer so far */
  2656. extern int __kmp_debug_buf_warn_chars; /* Keep track of char increase
  2657. recommended in warnings */
  2658. /* end rotating debug buffer */
  2659. #ifdef KMP_DEBUG
  2660. extern int __kmp_par_range; /* +1 => only go par for constructs in range */
  2661. #define KMP_PAR_RANGE_ROUTINE_LEN 1024
  2662. extern char __kmp_par_range_routine[KMP_PAR_RANGE_ROUTINE_LEN];
  2663. #define KMP_PAR_RANGE_FILENAME_LEN 1024
  2664. extern char __kmp_par_range_filename[KMP_PAR_RANGE_FILENAME_LEN];
  2665. extern int __kmp_par_range_lb;
  2666. extern int __kmp_par_range_ub;
  2667. #endif
  2668. /* For printing out dynamic storage map for threads and teams */
  2669. extern int
  2670. __kmp_storage_map; /* True means print storage map for threads and teams */
  2671. extern int __kmp_storage_map_verbose; /* True means storage map includes
  2672. placement info */
  2673. extern int __kmp_storage_map_verbose_specified;
  2674. #if KMP_ARCH_X86 || KMP_ARCH_X86_64
  2675. extern kmp_cpuinfo_t __kmp_cpuinfo;
  2676. static inline bool __kmp_is_hybrid_cpu() { return __kmp_cpuinfo.flags.hybrid; }
  2677. #elif KMP_OS_DARWIN && KMP_ARCH_AARCH64
  2678. static inline bool __kmp_is_hybrid_cpu() { return true; }
  2679. #else
  2680. static inline bool __kmp_is_hybrid_cpu() { return false; }
  2681. #endif
  2682. extern volatile int __kmp_init_serial;
  2683. extern volatile int __kmp_init_gtid;
  2684. extern volatile int __kmp_init_common;
  2685. extern volatile int __kmp_need_register_serial;
  2686. extern volatile int __kmp_init_middle;
  2687. extern volatile int __kmp_init_parallel;
  2688. #if KMP_USE_MONITOR
  2689. extern volatile int __kmp_init_monitor;
  2690. #endif
  2691. extern volatile int __kmp_init_user_locks;
  2692. extern volatile int __kmp_init_hidden_helper_threads;
  2693. extern int __kmp_init_counter;
  2694. extern int __kmp_root_counter;
  2695. extern int __kmp_version;
  2696. /* list of address of allocated caches for commons */
  2697. extern kmp_cached_addr_t *__kmp_threadpriv_cache_list;
  2698. /* Barrier algorithm types and options */
  2699. extern kmp_uint32 __kmp_barrier_gather_bb_dflt;
  2700. extern kmp_uint32 __kmp_barrier_release_bb_dflt;
  2701. extern kmp_bar_pat_e __kmp_barrier_gather_pat_dflt;
  2702. extern kmp_bar_pat_e __kmp_barrier_release_pat_dflt;
  2703. extern kmp_uint32 __kmp_barrier_gather_branch_bits[bs_last_barrier];
  2704. extern kmp_uint32 __kmp_barrier_release_branch_bits[bs_last_barrier];
  2705. extern kmp_bar_pat_e __kmp_barrier_gather_pattern[bs_last_barrier];
  2706. extern kmp_bar_pat_e __kmp_barrier_release_pattern[bs_last_barrier];
  2707. extern char const *__kmp_barrier_branch_bit_env_name[bs_last_barrier];
  2708. extern char const *__kmp_barrier_pattern_env_name[bs_last_barrier];
  2709. extern char const *__kmp_barrier_type_name[bs_last_barrier];
  2710. extern char const *__kmp_barrier_pattern_name[bp_last_bar];
  2711. /* Global Locks */
  2712. extern kmp_bootstrap_lock_t __kmp_initz_lock; /* control initialization */
  2713. extern kmp_bootstrap_lock_t __kmp_forkjoin_lock; /* control fork/join access */
  2714. extern kmp_bootstrap_lock_t __kmp_task_team_lock;
  2715. extern kmp_bootstrap_lock_t
  2716. __kmp_exit_lock; /* exit() is not always thread-safe */
  2717. #if KMP_USE_MONITOR
  2718. extern kmp_bootstrap_lock_t
  2719. __kmp_monitor_lock; /* control monitor thread creation */
  2720. #endif
  2721. extern kmp_bootstrap_lock_t
  2722. __kmp_tp_cached_lock; /* used for the hack to allow threadprivate cache and
  2723. __kmp_threads expansion to co-exist */
  2724. extern kmp_lock_t __kmp_global_lock; /* control OS/global access */
  2725. extern kmp_queuing_lock_t __kmp_dispatch_lock; /* control dispatch access */
  2726. extern kmp_lock_t __kmp_debug_lock; /* control I/O access for KMP_DEBUG */
  2727. extern enum library_type __kmp_library;
  2728. extern enum sched_type __kmp_sched; /* default runtime scheduling */
  2729. extern enum sched_type __kmp_static; /* default static scheduling method */
  2730. extern enum sched_type __kmp_guided; /* default guided scheduling method */
  2731. extern enum sched_type __kmp_auto; /* default auto scheduling method */
  2732. extern int __kmp_chunk; /* default runtime chunk size */
  2733. extern int __kmp_force_monotonic; /* whether monotonic scheduling forced */
  2734. extern size_t __kmp_stksize; /* stack size per thread */
  2735. #if KMP_USE_MONITOR
  2736. extern size_t __kmp_monitor_stksize; /* stack size for monitor thread */
  2737. #endif
  2738. extern size_t __kmp_stkoffset; /* stack offset per thread */
  2739. extern int __kmp_stkpadding; /* Should we pad root thread(s) stack */
  2740. extern size_t
  2741. __kmp_malloc_pool_incr; /* incremental size of pool for kmp_malloc() */
  2742. extern int __kmp_env_stksize; /* was KMP_STACKSIZE specified? */
  2743. extern int __kmp_env_blocktime; /* was KMP_BLOCKTIME specified? */
  2744. extern int __kmp_env_checks; /* was KMP_CHECKS specified? */
  2745. extern int __kmp_env_consistency_check; // was KMP_CONSISTENCY_CHECK specified?
  2746. extern int __kmp_generate_warnings; /* should we issue warnings? */
  2747. extern int __kmp_reserve_warn; /* have we issued reserve_threads warning? */
  2748. #ifdef DEBUG_SUSPEND
  2749. extern int __kmp_suspend_count; /* count inside __kmp_suspend_template() */
  2750. #endif
  2751. extern kmp_int32 __kmp_use_yield;
  2752. extern kmp_int32 __kmp_use_yield_exp_set;
  2753. extern kmp_uint32 __kmp_yield_init;
  2754. extern kmp_uint32 __kmp_yield_next;
  2755. extern kmp_uint64 __kmp_pause_init;
  2756. /* ------------------------------------------------------------------------- */
  2757. extern int __kmp_allThreadsSpecified;
  2758. extern size_t __kmp_align_alloc;
  2759. /* following data protected by initialization routines */
  2760. extern int __kmp_xproc; /* number of processors in the system */
  2761. extern int __kmp_avail_proc; /* number of processors available to the process */
  2762. extern size_t __kmp_sys_min_stksize; /* system-defined minimum stack size */
  2763. extern int __kmp_sys_max_nth; /* system-imposed maximum number of threads */
  2764. // maximum total number of concurrently-existing threads on device
  2765. extern int __kmp_max_nth;
  2766. // maximum total number of concurrently-existing threads in a contention group
  2767. extern int __kmp_cg_max_nth;
  2768. extern int __kmp_teams_max_nth; // max threads used in a teams construct
  2769. extern int __kmp_threads_capacity; /* capacity of the arrays __kmp_threads and
  2770. __kmp_root */
  2771. extern int __kmp_dflt_team_nth; /* default number of threads in a parallel
  2772. region a la OMP_NUM_THREADS */
  2773. extern int __kmp_dflt_team_nth_ub; /* upper bound on "" determined at serial
  2774. initialization */
  2775. extern int __kmp_tp_capacity; /* capacity of __kmp_threads if threadprivate is
  2776. used (fixed) */
  2777. extern int __kmp_tp_cached; /* whether threadprivate cache has been created
  2778. (__kmpc_threadprivate_cached()) */
  2779. extern int __kmp_dflt_blocktime; /* number of milliseconds to wait before
  2780. blocking (env setting) */
  2781. extern bool __kmp_wpolicy_passive; /* explicitly set passive wait policy */
  2782. #if KMP_USE_MONITOR
  2783. extern int
  2784. __kmp_monitor_wakeups; /* number of times monitor wakes up per second */
  2785. extern int __kmp_bt_intervals; /* number of monitor timestamp intervals before
  2786. blocking */
  2787. #endif
  2788. #ifdef KMP_ADJUST_BLOCKTIME
  2789. extern int __kmp_zero_bt; /* whether blocktime has been forced to zero */
  2790. #endif /* KMP_ADJUST_BLOCKTIME */
  2791. #ifdef KMP_DFLT_NTH_CORES
  2792. extern int __kmp_ncores; /* Total number of cores for threads placement */
  2793. #endif
  2794. /* Number of millisecs to delay on abort for Intel(R) VTune(TM) tools */
  2795. extern int __kmp_abort_delay;
  2796. extern int __kmp_need_register_atfork_specified;
  2797. extern int __kmp_need_register_atfork; /* At initialization, call pthread_atfork
  2798. to install fork handler */
  2799. extern int __kmp_gtid_mode; /* Method of getting gtid, values:
  2800. 0 - not set, will be set at runtime
  2801. 1 - using stack search
  2802. 2 - dynamic TLS (pthread_getspecific(Linux* OS/OS
  2803. X*) or TlsGetValue(Windows* OS))
  2804. 3 - static TLS (__declspec(thread) __kmp_gtid),
  2805. Linux* OS .so only. */
  2806. extern int
  2807. __kmp_adjust_gtid_mode; /* If true, adjust method based on #threads */
  2808. #ifdef KMP_TDATA_GTID
  2809. extern KMP_THREAD_LOCAL int __kmp_gtid;
  2810. #endif
  2811. extern int __kmp_tls_gtid_min; /* #threads below which use sp search for gtid */
  2812. extern int __kmp_foreign_tp; // If true, separate TP var for each foreign thread
  2813. #if KMP_ARCH_X86 || KMP_ARCH_X86_64
  2814. extern int __kmp_inherit_fp_control; // copy fp creg(s) parent->workers at fork
  2815. extern kmp_int16 __kmp_init_x87_fpu_control_word; // init thread's FP ctrl reg
  2816. extern kmp_uint32 __kmp_init_mxcsr; /* init thread's mxscr */
  2817. #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
  2818. // max_active_levels for nested parallelism enabled by default via
  2819. // OMP_MAX_ACTIVE_LEVELS, OMP_NESTED, OMP_NUM_THREADS, and OMP_PROC_BIND
  2820. extern int __kmp_dflt_max_active_levels;
  2821. // Indicates whether value of __kmp_dflt_max_active_levels was already
  2822. // explicitly set by OMP_MAX_ACTIVE_LEVELS or OMP_NESTED=false
  2823. extern bool __kmp_dflt_max_active_levels_set;
  2824. extern int __kmp_dispatch_num_buffers; /* max possible dynamic loops in
  2825. concurrent execution per team */
  2826. #if KMP_NESTED_HOT_TEAMS
  2827. extern int __kmp_hot_teams_mode;
  2828. extern int __kmp_hot_teams_max_level;
  2829. #endif
  2830. #if KMP_OS_LINUX
  2831. extern enum clock_function_type __kmp_clock_function;
  2832. extern int __kmp_clock_function_param;
  2833. #endif /* KMP_OS_LINUX */
  2834. #if KMP_MIC_SUPPORTED
  2835. extern enum mic_type __kmp_mic_type;
  2836. #endif
  2837. #ifdef USE_LOAD_BALANCE
  2838. extern double __kmp_load_balance_interval; // load balance algorithm interval
  2839. #endif /* USE_LOAD_BALANCE */
  2840. // OpenMP 3.1 - Nested num threads array
  2841. typedef struct kmp_nested_nthreads_t {
  2842. int *nth;
  2843. int size;
  2844. int used;
  2845. } kmp_nested_nthreads_t;
  2846. extern kmp_nested_nthreads_t __kmp_nested_nth;
  2847. #if KMP_USE_ADAPTIVE_LOCKS
  2848. // Parameters for the speculative lock backoff system.
  2849. struct kmp_adaptive_backoff_params_t {
  2850. // Number of soft retries before it counts as a hard retry.
  2851. kmp_uint32 max_soft_retries;
  2852. // Badness is a bit mask : 0,1,3,7,15,... on each hard failure we move one to
  2853. // the right
  2854. kmp_uint32 max_badness;
  2855. };
  2856. extern kmp_adaptive_backoff_params_t __kmp_adaptive_backoff_params;
  2857. #if KMP_DEBUG_ADAPTIVE_LOCKS
  2858. extern const char *__kmp_speculative_statsfile;
  2859. #endif
  2860. #endif // KMP_USE_ADAPTIVE_LOCKS
  2861. extern int __kmp_display_env; /* TRUE or FALSE */
  2862. extern int __kmp_display_env_verbose; /* TRUE if OMP_DISPLAY_ENV=VERBOSE */
  2863. extern int __kmp_omp_cancellation; /* TRUE or FALSE */
  2864. extern int __kmp_nteams;
  2865. extern int __kmp_teams_thread_limit;
  2866. /* ------------------------------------------------------------------------- */
  2867. /* the following are protected by the fork/join lock */
  2868. /* write: lock read: anytime */
  2869. extern kmp_info_t **__kmp_threads; /* Descriptors for the threads */
  2870. /* Holds old arrays of __kmp_threads until library shutdown */
  2871. extern kmp_old_threads_list_t *__kmp_old_threads_list;
  2872. /* read/write: lock */
  2873. extern volatile kmp_team_t *__kmp_team_pool;
  2874. extern volatile kmp_info_t *__kmp_thread_pool;
  2875. extern kmp_info_t *__kmp_thread_pool_insert_pt;
  2876. // total num threads reachable from some root thread including all root threads
  2877. extern volatile int __kmp_nth;
  2878. /* total number of threads reachable from some root thread including all root
  2879. threads, and those in the thread pool */
  2880. extern volatile int __kmp_all_nth;
  2881. extern std::atomic<int> __kmp_thread_pool_active_nth;
  2882. extern kmp_root_t **__kmp_root; /* root of thread hierarchy */
  2883. /* end data protected by fork/join lock */
  2884. /* ------------------------------------------------------------------------- */
  2885. #define __kmp_get_gtid() __kmp_get_global_thread_id()
  2886. #define __kmp_entry_gtid() __kmp_get_global_thread_id_reg()
  2887. #define __kmp_get_tid() (__kmp_tid_from_gtid(__kmp_get_gtid()))
  2888. #define __kmp_get_team() (__kmp_threads[(__kmp_get_gtid())]->th.th_team)
  2889. #define __kmp_get_thread() (__kmp_thread_from_gtid(__kmp_get_gtid()))
  2890. // AT: Which way is correct?
  2891. // AT: 1. nproc = __kmp_threads[ ( gtid ) ] -> th.th_team -> t.t_nproc;
  2892. // AT: 2. nproc = __kmp_threads[ ( gtid ) ] -> th.th_team_nproc;
  2893. #define __kmp_get_team_num_threads(gtid) \
  2894. (__kmp_threads[(gtid)]->th.th_team->t.t_nproc)
  2895. static inline bool KMP_UBER_GTID(int gtid) {
  2896. KMP_DEBUG_ASSERT(gtid >= KMP_GTID_MIN);
  2897. KMP_DEBUG_ASSERT(gtid < __kmp_threads_capacity);
  2898. return (gtid >= 0 && __kmp_root[gtid] && __kmp_threads[gtid] &&
  2899. __kmp_threads[gtid] == __kmp_root[gtid]->r.r_uber_thread);
  2900. }
  2901. static inline int __kmp_tid_from_gtid(int gtid) {
  2902. KMP_DEBUG_ASSERT(gtid >= 0);
  2903. return __kmp_threads[gtid]->th.th_info.ds.ds_tid;
  2904. }
  2905. static inline int __kmp_gtid_from_tid(int tid, const kmp_team_t *team) {
  2906. KMP_DEBUG_ASSERT(tid >= 0 && team);
  2907. return team->t.t_threads[tid]->th.th_info.ds.ds_gtid;
  2908. }
  2909. static inline int __kmp_gtid_from_thread(const kmp_info_t *thr) {
  2910. KMP_DEBUG_ASSERT(thr);
  2911. return thr->th.th_info.ds.ds_gtid;
  2912. }
  2913. static inline kmp_info_t *__kmp_thread_from_gtid(int gtid) {
  2914. KMP_DEBUG_ASSERT(gtid >= 0);
  2915. return __kmp_threads[gtid];
  2916. }
  2917. static inline kmp_team_t *__kmp_team_from_gtid(int gtid) {
  2918. KMP_DEBUG_ASSERT(gtid >= 0);
  2919. return __kmp_threads[gtid]->th.th_team;
  2920. }
  2921. static inline void __kmp_assert_valid_gtid(kmp_int32 gtid) {
  2922. if (UNLIKELY(gtid < 0 || gtid >= __kmp_threads_capacity))
  2923. KMP_FATAL(ThreadIdentInvalid);
  2924. }
  2925. #if KMP_HAVE_MWAIT || KMP_HAVE_UMWAIT
  2926. extern int __kmp_user_level_mwait; // TRUE or FALSE; from KMP_USER_LEVEL_MWAIT
  2927. extern int __kmp_umwait_enabled; // Runtime check if user-level mwait enabled
  2928. extern int __kmp_mwait_enabled; // Runtime check if ring3 mwait is enabled
  2929. extern int __kmp_mwait_hints; // Hints to pass in to mwait
  2930. #endif
  2931. #if KMP_HAVE_UMWAIT
  2932. extern int __kmp_waitpkg_enabled; // Runtime check if waitpkg exists
  2933. extern int __kmp_tpause_state; // 0 (default), 1=C0.1, 2=C0.2; from KMP_TPAUSE
  2934. extern int __kmp_tpause_hint; // 1=C0.1 (default), 0=C0.2; from KMP_TPAUSE
  2935. extern int __kmp_tpause_enabled; // 0 (default), 1 (KMP_TPAUSE is non-zero)
  2936. #endif
  2937. /* ------------------------------------------------------------------------- */
  2938. extern kmp_global_t __kmp_global; /* global status */
  2939. extern kmp_info_t __kmp_monitor;
  2940. // For Debugging Support Library
  2941. extern std::atomic<kmp_int32> __kmp_team_counter;
  2942. // For Debugging Support Library
  2943. extern std::atomic<kmp_int32> __kmp_task_counter;
  2944. #if USE_DEBUGGER
  2945. #define _KMP_GEN_ID(counter) \
  2946. (__kmp_debugging ? KMP_ATOMIC_INC(&counter) + 1 : ~0)
  2947. #else
  2948. #define _KMP_GEN_ID(counter) (~0)
  2949. #endif /* USE_DEBUGGER */
  2950. #define KMP_GEN_TASK_ID() _KMP_GEN_ID(__kmp_task_counter)
  2951. #define KMP_GEN_TEAM_ID() _KMP_GEN_ID(__kmp_team_counter)
  2952. /* ------------------------------------------------------------------------ */
  2953. extern void __kmp_print_storage_map_gtid(int gtid, void *p1, void *p2,
  2954. size_t size, char const *format, ...);
  2955. extern void __kmp_serial_initialize(void);
  2956. extern void __kmp_middle_initialize(void);
  2957. extern void __kmp_parallel_initialize(void);
  2958. extern void __kmp_internal_begin(void);
  2959. extern void __kmp_internal_end_library(int gtid);
  2960. extern void __kmp_internal_end_thread(int gtid);
  2961. extern void __kmp_internal_end_atexit(void);
  2962. extern void __kmp_internal_end_dtor(void);
  2963. extern void __kmp_internal_end_dest(void *);
  2964. extern int __kmp_register_root(int initial_thread);
  2965. extern void __kmp_unregister_root(int gtid);
  2966. extern void __kmp_unregister_library(void); // called by __kmp_internal_end()
  2967. extern int __kmp_ignore_mppbeg(void);
  2968. extern int __kmp_ignore_mppend(void);
  2969. extern int __kmp_enter_single(int gtid, ident_t *id_ref, int push_ws);
  2970. extern void __kmp_exit_single(int gtid);
  2971. extern void __kmp_parallel_deo(int *gtid_ref, int *cid_ref, ident_t *loc_ref);
  2972. extern void __kmp_parallel_dxo(int *gtid_ref, int *cid_ref, ident_t *loc_ref);
  2973. #ifdef USE_LOAD_BALANCE
  2974. extern int __kmp_get_load_balance(int);
  2975. #endif
  2976. extern int __kmp_get_global_thread_id(void);
  2977. extern int __kmp_get_global_thread_id_reg(void);
  2978. extern void __kmp_exit_thread(int exit_status);
  2979. extern void __kmp_abort(char const *format, ...);
  2980. extern void __kmp_abort_thread(void);
  2981. KMP_NORETURN extern void __kmp_abort_process(void);
  2982. extern void __kmp_warn(char const *format, ...);
  2983. extern void __kmp_set_num_threads(int new_nth, int gtid);
  2984. // Returns current thread (pointer to kmp_info_t). Current thread *must* be
  2985. // registered.
  2986. static inline kmp_info_t *__kmp_entry_thread() {
  2987. int gtid = __kmp_entry_gtid();
  2988. return __kmp_threads[gtid];
  2989. }
  2990. extern void __kmp_set_max_active_levels(int gtid, int new_max_active_levels);
  2991. extern int __kmp_get_max_active_levels(int gtid);
  2992. extern int __kmp_get_ancestor_thread_num(int gtid, int level);
  2993. extern int __kmp_get_team_size(int gtid, int level);
  2994. extern void __kmp_set_schedule(int gtid, kmp_sched_t new_sched, int chunk);
  2995. extern void __kmp_get_schedule(int gtid, kmp_sched_t *sched, int *chunk);
  2996. extern unsigned short __kmp_get_random(kmp_info_t *thread);
  2997. extern void __kmp_init_random(kmp_info_t *thread);
  2998. extern kmp_r_sched_t __kmp_get_schedule_global(void);
  2999. extern void __kmp_adjust_num_threads(int new_nproc);
  3000. extern void __kmp_check_stksize(size_t *val);
  3001. extern void *___kmp_allocate(size_t size KMP_SRC_LOC_DECL);
  3002. extern void *___kmp_page_allocate(size_t size KMP_SRC_LOC_DECL);
  3003. extern void ___kmp_free(void *ptr KMP_SRC_LOC_DECL);
  3004. #define __kmp_allocate(size) ___kmp_allocate((size)KMP_SRC_LOC_CURR)
  3005. #define __kmp_page_allocate(size) ___kmp_page_allocate((size)KMP_SRC_LOC_CURR)
  3006. #define __kmp_free(ptr) ___kmp_free((ptr)KMP_SRC_LOC_CURR)
  3007. #if USE_FAST_MEMORY
  3008. extern void *___kmp_fast_allocate(kmp_info_t *this_thr,
  3009. size_t size KMP_SRC_LOC_DECL);
  3010. extern void ___kmp_fast_free(kmp_info_t *this_thr, void *ptr KMP_SRC_LOC_DECL);
  3011. extern void __kmp_free_fast_memory(kmp_info_t *this_thr);
  3012. extern void __kmp_initialize_fast_memory(kmp_info_t *this_thr);
  3013. #define __kmp_fast_allocate(this_thr, size) \
  3014. ___kmp_fast_allocate((this_thr), (size)KMP_SRC_LOC_CURR)
  3015. #define __kmp_fast_free(this_thr, ptr) \
  3016. ___kmp_fast_free((this_thr), (ptr)KMP_SRC_LOC_CURR)
  3017. #endif
  3018. extern void *___kmp_thread_malloc(kmp_info_t *th, size_t size KMP_SRC_LOC_DECL);
  3019. extern void *___kmp_thread_calloc(kmp_info_t *th, size_t nelem,
  3020. size_t elsize KMP_SRC_LOC_DECL);
  3021. extern void *___kmp_thread_realloc(kmp_info_t *th, void *ptr,
  3022. size_t size KMP_SRC_LOC_DECL);
  3023. extern void ___kmp_thread_free(kmp_info_t *th, void *ptr KMP_SRC_LOC_DECL);
  3024. #define __kmp_thread_malloc(th, size) \
  3025. ___kmp_thread_malloc((th), (size)KMP_SRC_LOC_CURR)
  3026. #define __kmp_thread_calloc(th, nelem, elsize) \
  3027. ___kmp_thread_calloc((th), (nelem), (elsize)KMP_SRC_LOC_CURR)
  3028. #define __kmp_thread_realloc(th, ptr, size) \
  3029. ___kmp_thread_realloc((th), (ptr), (size)KMP_SRC_LOC_CURR)
  3030. #define __kmp_thread_free(th, ptr) \
  3031. ___kmp_thread_free((th), (ptr)KMP_SRC_LOC_CURR)
  3032. extern void __kmp_push_num_threads(ident_t *loc, int gtid, int num_threads);
  3033. extern void __kmp_push_proc_bind(ident_t *loc, int gtid,
  3034. kmp_proc_bind_t proc_bind);
  3035. extern void __kmp_push_num_teams(ident_t *loc, int gtid, int num_teams,
  3036. int num_threads);
  3037. extern void __kmp_push_num_teams_51(ident_t *loc, int gtid, int num_teams_lb,
  3038. int num_teams_ub, int num_threads);
  3039. extern void __kmp_yield();
  3040. extern void __kmpc_dispatch_init_4(ident_t *loc, kmp_int32 gtid,
  3041. enum sched_type schedule, kmp_int32 lb,
  3042. kmp_int32 ub, kmp_int32 st, kmp_int32 chunk);
  3043. extern void __kmpc_dispatch_init_4u(ident_t *loc, kmp_int32 gtid,
  3044. enum sched_type schedule, kmp_uint32 lb,
  3045. kmp_uint32 ub, kmp_int32 st,
  3046. kmp_int32 chunk);
  3047. extern void __kmpc_dispatch_init_8(ident_t *loc, kmp_int32 gtid,
  3048. enum sched_type schedule, kmp_int64 lb,
  3049. kmp_int64 ub, kmp_int64 st, kmp_int64 chunk);
  3050. extern void __kmpc_dispatch_init_8u(ident_t *loc, kmp_int32 gtid,
  3051. enum sched_type schedule, kmp_uint64 lb,
  3052. kmp_uint64 ub, kmp_int64 st,
  3053. kmp_int64 chunk);
  3054. extern int __kmpc_dispatch_next_4(ident_t *loc, kmp_int32 gtid,
  3055. kmp_int32 *p_last, kmp_int32 *p_lb,
  3056. kmp_int32 *p_ub, kmp_int32 *p_st);
  3057. extern int __kmpc_dispatch_next_4u(ident_t *loc, kmp_int32 gtid,
  3058. kmp_int32 *p_last, kmp_uint32 *p_lb,
  3059. kmp_uint32 *p_ub, kmp_int32 *p_st);
  3060. extern int __kmpc_dispatch_next_8(ident_t *loc, kmp_int32 gtid,
  3061. kmp_int32 *p_last, kmp_int64 *p_lb,
  3062. kmp_int64 *p_ub, kmp_int64 *p_st);
  3063. extern int __kmpc_dispatch_next_8u(ident_t *loc, kmp_int32 gtid,
  3064. kmp_int32 *p_last, kmp_uint64 *p_lb,
  3065. kmp_uint64 *p_ub, kmp_int64 *p_st);
  3066. extern void __kmpc_dispatch_fini_4(ident_t *loc, kmp_int32 gtid);
  3067. extern void __kmpc_dispatch_fini_8(ident_t *loc, kmp_int32 gtid);
  3068. extern void __kmpc_dispatch_fini_4u(ident_t *loc, kmp_int32 gtid);
  3069. extern void __kmpc_dispatch_fini_8u(ident_t *loc, kmp_int32 gtid);
  3070. #ifdef KMP_GOMP_COMPAT
  3071. extern void __kmp_aux_dispatch_init_4(ident_t *loc, kmp_int32 gtid,
  3072. enum sched_type schedule, kmp_int32 lb,
  3073. kmp_int32 ub, kmp_int32 st,
  3074. kmp_int32 chunk, int push_ws);
  3075. extern void __kmp_aux_dispatch_init_4u(ident_t *loc, kmp_int32 gtid,
  3076. enum sched_type schedule, kmp_uint32 lb,
  3077. kmp_uint32 ub, kmp_int32 st,
  3078. kmp_int32 chunk, int push_ws);
  3079. extern void __kmp_aux_dispatch_init_8(ident_t *loc, kmp_int32 gtid,
  3080. enum sched_type schedule, kmp_int64 lb,
  3081. kmp_int64 ub, kmp_int64 st,
  3082. kmp_int64 chunk, int push_ws);
  3083. extern void __kmp_aux_dispatch_init_8u(ident_t *loc, kmp_int32 gtid,
  3084. enum sched_type schedule, kmp_uint64 lb,
  3085. kmp_uint64 ub, kmp_int64 st,
  3086. kmp_int64 chunk, int push_ws);
  3087. extern void __kmp_aux_dispatch_fini_chunk_4(ident_t *loc, kmp_int32 gtid);
  3088. extern void __kmp_aux_dispatch_fini_chunk_8(ident_t *loc, kmp_int32 gtid);
  3089. extern void __kmp_aux_dispatch_fini_chunk_4u(ident_t *loc, kmp_int32 gtid);
  3090. extern void __kmp_aux_dispatch_fini_chunk_8u(ident_t *loc, kmp_int32 gtid);
  3091. #endif /* KMP_GOMP_COMPAT */
  3092. extern kmp_uint32 __kmp_eq_4(kmp_uint32 value, kmp_uint32 checker);
  3093. extern kmp_uint32 __kmp_neq_4(kmp_uint32 value, kmp_uint32 checker);
  3094. extern kmp_uint32 __kmp_lt_4(kmp_uint32 value, kmp_uint32 checker);
  3095. extern kmp_uint32 __kmp_ge_4(kmp_uint32 value, kmp_uint32 checker);
  3096. extern kmp_uint32 __kmp_le_4(kmp_uint32 value, kmp_uint32 checker);
  3097. extern kmp_uint32 __kmp_wait_4(kmp_uint32 volatile *spinner, kmp_uint32 checker,
  3098. kmp_uint32 (*pred)(kmp_uint32, kmp_uint32),
  3099. void *obj);
  3100. extern void __kmp_wait_4_ptr(void *spinner, kmp_uint32 checker,
  3101. kmp_uint32 (*pred)(void *, kmp_uint32), void *obj);
  3102. extern void __kmp_wait_64(kmp_info_t *this_thr, kmp_flag_64<> *flag,
  3103. int final_spin
  3104. #if USE_ITT_BUILD
  3105. ,
  3106. void *itt_sync_obj
  3107. #endif
  3108. );
  3109. extern void __kmp_release_64(kmp_flag_64<> *flag);
  3110. extern void __kmp_infinite_loop(void);
  3111. extern void __kmp_cleanup(void);
  3112. #if KMP_HANDLE_SIGNALS
  3113. extern int __kmp_handle_signals;
  3114. extern void __kmp_install_signals(int parallel_init);
  3115. extern void __kmp_remove_signals(void);
  3116. #endif
  3117. extern void __kmp_clear_system_time(void);
  3118. extern void __kmp_read_system_time(double *delta);
  3119. extern void __kmp_check_stack_overlap(kmp_info_t *thr);
  3120. extern void __kmp_expand_host_name(char *buffer, size_t size);
  3121. extern void __kmp_expand_file_name(char *result, size_t rlen, char *pattern);
  3122. #if KMP_ARCH_X86 || KMP_ARCH_X86_64 || (KMP_OS_WINDOWS && KMP_ARCH_AARCH64)
  3123. extern void
  3124. __kmp_initialize_system_tick(void); /* Initialize timer tick value */
  3125. #endif
  3126. extern void
  3127. __kmp_runtime_initialize(void); /* machine specific initialization */
  3128. extern void __kmp_runtime_destroy(void);
  3129. #if KMP_AFFINITY_SUPPORTED
  3130. extern char *__kmp_affinity_print_mask(char *buf, int buf_len,
  3131. kmp_affin_mask_t *mask);
  3132. extern kmp_str_buf_t *__kmp_affinity_str_buf_mask(kmp_str_buf_t *buf,
  3133. kmp_affin_mask_t *mask);
  3134. extern void __kmp_affinity_initialize(void);
  3135. extern void __kmp_affinity_uninitialize(void);
  3136. extern void __kmp_affinity_set_init_mask(
  3137. int gtid, int isa_root); /* set affinity according to KMP_AFFINITY */
  3138. extern void __kmp_affinity_set_place(int gtid);
  3139. extern void __kmp_affinity_determine_capable(const char *env_var);
  3140. extern int __kmp_aux_set_affinity(void **mask);
  3141. extern int __kmp_aux_get_affinity(void **mask);
  3142. extern int __kmp_aux_get_affinity_max_proc();
  3143. extern int __kmp_aux_set_affinity_mask_proc(int proc, void **mask);
  3144. extern int __kmp_aux_unset_affinity_mask_proc(int proc, void **mask);
  3145. extern int __kmp_aux_get_affinity_mask_proc(int proc, void **mask);
  3146. extern void __kmp_balanced_affinity(kmp_info_t *th, int team_size);
  3147. #if KMP_OS_LINUX || KMP_OS_FREEBSD
  3148. extern int kmp_set_thread_affinity_mask_initial(void);
  3149. #endif
  3150. static inline void __kmp_assign_root_init_mask() {
  3151. int gtid = __kmp_entry_gtid();
  3152. kmp_root_t *r = __kmp_threads[gtid]->th.th_root;
  3153. if (r->r.r_uber_thread == __kmp_threads[gtid] && !r->r.r_affinity_assigned) {
  3154. __kmp_affinity_set_init_mask(gtid, TRUE);
  3155. r->r.r_affinity_assigned = TRUE;
  3156. }
  3157. }
  3158. static inline void __kmp_reset_root_init_mask(int gtid) {
  3159. kmp_info_t *th = __kmp_threads[gtid];
  3160. kmp_root_t *r = th->th.th_root;
  3161. if (r->r.r_uber_thread == th && r->r.r_affinity_assigned) {
  3162. __kmp_set_system_affinity(__kmp_affin_origMask, FALSE);
  3163. KMP_CPU_COPY(th->th.th_affin_mask, __kmp_affin_origMask);
  3164. r->r.r_affinity_assigned = FALSE;
  3165. }
  3166. }
  3167. #else /* KMP_AFFINITY_SUPPORTED */
  3168. #define __kmp_assign_root_init_mask() /* Nothing */
  3169. static inline void __kmp_reset_root_init_mask(int gtid) {}
  3170. #endif /* KMP_AFFINITY_SUPPORTED */
  3171. // No need for KMP_AFFINITY_SUPPORTED guard as only one field in the
  3172. // format string is for affinity, so platforms that do not support
  3173. // affinity can still use the other fields, e.g., %n for num_threads
  3174. extern size_t __kmp_aux_capture_affinity(int gtid, const char *format,
  3175. kmp_str_buf_t *buffer);
  3176. extern void __kmp_aux_display_affinity(int gtid, const char *format);
  3177. extern void __kmp_cleanup_hierarchy();
  3178. extern void __kmp_get_hierarchy(kmp_uint32 nproc, kmp_bstate_t *thr_bar);
  3179. #if KMP_USE_FUTEX
  3180. extern int __kmp_futex_determine_capable(void);
  3181. #endif // KMP_USE_FUTEX
  3182. extern void __kmp_gtid_set_specific(int gtid);
  3183. extern int __kmp_gtid_get_specific(void);
  3184. extern double __kmp_read_cpu_time(void);
  3185. extern int __kmp_read_system_info(struct kmp_sys_info *info);
  3186. #if KMP_USE_MONITOR
  3187. extern void __kmp_create_monitor(kmp_info_t *th);
  3188. #endif
  3189. extern void *__kmp_launch_thread(kmp_info_t *thr);
  3190. extern void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size);
  3191. #if KMP_OS_WINDOWS
  3192. extern int __kmp_still_running(kmp_info_t *th);
  3193. extern int __kmp_is_thread_alive(kmp_info_t *th, DWORD *exit_val);
  3194. extern void __kmp_free_handle(kmp_thread_t tHandle);
  3195. #endif
  3196. #if KMP_USE_MONITOR
  3197. extern void __kmp_reap_monitor(kmp_info_t *th);
  3198. #endif
  3199. extern void __kmp_reap_worker(kmp_info_t *th);
  3200. extern void __kmp_terminate_thread(int gtid);
  3201. extern int __kmp_try_suspend_mx(kmp_info_t *th);
  3202. extern void __kmp_lock_suspend_mx(kmp_info_t *th);
  3203. extern void __kmp_unlock_suspend_mx(kmp_info_t *th);
  3204. extern void __kmp_elapsed(double *);
  3205. extern void __kmp_elapsed_tick(double *);
  3206. extern void __kmp_enable(int old_state);
  3207. extern void __kmp_disable(int *old_state);
  3208. extern void __kmp_thread_sleep(int millis);
  3209. extern void __kmp_common_initialize(void);
  3210. extern void __kmp_common_destroy(void);
  3211. extern void __kmp_common_destroy_gtid(int gtid);
  3212. #if KMP_OS_UNIX
  3213. extern void __kmp_register_atfork(void);
  3214. #endif
  3215. extern void __kmp_suspend_initialize(void);
  3216. extern void __kmp_suspend_initialize_thread(kmp_info_t *th);
  3217. extern void __kmp_suspend_uninitialize_thread(kmp_info_t *th);
  3218. extern kmp_info_t *__kmp_allocate_thread(kmp_root_t *root, kmp_team_t *team,
  3219. int tid);
  3220. extern kmp_team_t *
  3221. __kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc,
  3222. #if OMPT_SUPPORT
  3223. ompt_data_t ompt_parallel_data,
  3224. #endif
  3225. kmp_proc_bind_t proc_bind, kmp_internal_control_t *new_icvs,
  3226. int argc USE_NESTED_HOT_ARG(kmp_info_t *thr));
  3227. extern void __kmp_free_thread(kmp_info_t *);
  3228. extern void __kmp_free_team(kmp_root_t *,
  3229. kmp_team_t *USE_NESTED_HOT_ARG(kmp_info_t *));
  3230. extern kmp_team_t *__kmp_reap_team(kmp_team_t *);
  3231. /* ------------------------------------------------------------------------ */
  3232. extern void __kmp_initialize_bget(kmp_info_t *th);
  3233. extern void __kmp_finalize_bget(kmp_info_t *th);
  3234. KMP_EXPORT void *kmpc_malloc(size_t size);
  3235. KMP_EXPORT void *kmpc_aligned_malloc(size_t size, size_t alignment);
  3236. KMP_EXPORT void *kmpc_calloc(size_t nelem, size_t elsize);
  3237. KMP_EXPORT void *kmpc_realloc(void *ptr, size_t size);
  3238. KMP_EXPORT void kmpc_free(void *ptr);
  3239. /* declarations for internal use */
  3240. extern int __kmp_barrier(enum barrier_type bt, int gtid, int is_split,
  3241. size_t reduce_size, void *reduce_data,
  3242. void (*reduce)(void *, void *));
  3243. extern void __kmp_end_split_barrier(enum barrier_type bt, int gtid);
  3244. extern int __kmp_barrier_gomp_cancel(int gtid);
  3245. /*!
  3246. * Tell the fork call which compiler generated the fork call, and therefore how
  3247. * to deal with the call.
  3248. */
  3249. enum fork_context_e {
  3250. fork_context_gnu, /**< Called from GNU generated code, so must not invoke the
  3251. microtask internally. */
  3252. fork_context_intel, /**< Called from Intel generated code. */
  3253. fork_context_last
  3254. };
  3255. extern int __kmp_fork_call(ident_t *loc, int gtid,
  3256. enum fork_context_e fork_context, kmp_int32 argc,
  3257. microtask_t microtask, launch_t invoker,
  3258. kmp_va_list ap);
  3259. extern void __kmp_join_call(ident_t *loc, int gtid
  3260. #if OMPT_SUPPORT
  3261. ,
  3262. enum fork_context_e fork_context
  3263. #endif
  3264. ,
  3265. int exit_teams = 0);
  3266. extern void __kmp_serialized_parallel(ident_t *id, kmp_int32 gtid);
  3267. extern void __kmp_internal_fork(ident_t *id, int gtid, kmp_team_t *team);
  3268. extern void __kmp_internal_join(ident_t *id, int gtid, kmp_team_t *team);
  3269. extern int __kmp_invoke_task_func(int gtid);
  3270. extern void __kmp_run_before_invoked_task(int gtid, int tid,
  3271. kmp_info_t *this_thr,
  3272. kmp_team_t *team);
  3273. extern void __kmp_run_after_invoked_task(int gtid, int tid,
  3274. kmp_info_t *this_thr,
  3275. kmp_team_t *team);
  3276. // should never have been exported
  3277. KMP_EXPORT int __kmpc_invoke_task_func(int gtid);
  3278. extern int __kmp_invoke_teams_master(int gtid);
  3279. extern void __kmp_teams_master(int gtid);
  3280. extern int __kmp_aux_get_team_num();
  3281. extern int __kmp_aux_get_num_teams();
  3282. extern void __kmp_save_internal_controls(kmp_info_t *thread);
  3283. extern void __kmp_user_set_library(enum library_type arg);
  3284. extern void __kmp_aux_set_library(enum library_type arg);
  3285. extern void __kmp_aux_set_stacksize(size_t arg);
  3286. extern void __kmp_aux_set_blocktime(int arg, kmp_info_t *thread, int tid);
  3287. extern void __kmp_aux_set_defaults(char const *str, size_t len);
  3288. /* Functions called from __kmp_aux_env_initialize() in kmp_settings.cpp */
  3289. void kmpc_set_blocktime(int arg);
  3290. void ompc_set_nested(int flag);
  3291. void ompc_set_dynamic(int flag);
  3292. void ompc_set_num_threads(int arg);
  3293. extern void __kmp_push_current_task_to_thread(kmp_info_t *this_thr,
  3294. kmp_team_t *team, int tid);
  3295. extern void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr);
  3296. extern kmp_task_t *__kmp_task_alloc(ident_t *loc_ref, kmp_int32 gtid,
  3297. kmp_tasking_flags_t *flags,
  3298. size_t sizeof_kmp_task_t,
  3299. size_t sizeof_shareds,
  3300. kmp_routine_entry_t task_entry);
  3301. extern void __kmp_init_implicit_task(ident_t *loc_ref, kmp_info_t *this_thr,
  3302. kmp_team_t *team, int tid,
  3303. int set_curr_task);
  3304. extern void __kmp_finish_implicit_task(kmp_info_t *this_thr);
  3305. extern void __kmp_free_implicit_task(kmp_info_t *this_thr);
  3306. extern kmp_event_t *__kmpc_task_allow_completion_event(ident_t *loc_ref,
  3307. int gtid,
  3308. kmp_task_t *task);
  3309. extern void __kmp_fulfill_event(kmp_event_t *event);
  3310. extern void __kmp_free_task_team(kmp_info_t *thread,
  3311. kmp_task_team_t *task_team);
  3312. extern void __kmp_reap_task_teams(void);
  3313. extern void __kmp_wait_to_unref_task_teams(void);
  3314. extern void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team,
  3315. int always);
  3316. extern void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team);
  3317. extern void __kmp_task_team_wait(kmp_info_t *this_thr, kmp_team_t *team
  3318. #if USE_ITT_BUILD
  3319. ,
  3320. void *itt_sync_obj
  3321. #endif /* USE_ITT_BUILD */
  3322. ,
  3323. int wait = 1);
  3324. extern void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
  3325. int gtid);
  3326. extern int __kmp_is_address_mapped(void *addr);
  3327. extern kmp_uint64 __kmp_hardware_timestamp(void);
  3328. #if KMP_OS_UNIX
  3329. extern int __kmp_read_from_file(char const *path, char const *format, ...);
  3330. #endif
  3331. /* ------------------------------------------------------------------------ */
  3332. //
  3333. // Assembly routines that have no compiler intrinsic replacement
  3334. //
  3335. extern int __kmp_invoke_microtask(microtask_t pkfn, int gtid, int npr, int argc,
  3336. void *argv[]
  3337. #if OMPT_SUPPORT
  3338. ,
  3339. void **exit_frame_ptr
  3340. #endif
  3341. );
  3342. /* ------------------------------------------------------------------------ */
  3343. KMP_EXPORT void __kmpc_begin(ident_t *, kmp_int32 flags);
  3344. KMP_EXPORT void __kmpc_end(ident_t *);
  3345. KMP_EXPORT void __kmpc_threadprivate_register_vec(ident_t *, void *data,
  3346. kmpc_ctor_vec ctor,
  3347. kmpc_cctor_vec cctor,
  3348. kmpc_dtor_vec dtor,
  3349. size_t vector_length);
  3350. KMP_EXPORT void __kmpc_threadprivate_register(ident_t *, void *data,
  3351. kmpc_ctor ctor, kmpc_cctor cctor,
  3352. kmpc_dtor dtor);
  3353. KMP_EXPORT void *__kmpc_threadprivate(ident_t *, kmp_int32 global_tid,
  3354. void *data, size_t size);
  3355. KMP_EXPORT kmp_int32 __kmpc_global_thread_num(ident_t *);
  3356. KMP_EXPORT kmp_int32 __kmpc_global_num_threads(ident_t *);
  3357. KMP_EXPORT kmp_int32 __kmpc_bound_thread_num(ident_t *);
  3358. KMP_EXPORT kmp_int32 __kmpc_bound_num_threads(ident_t *);
  3359. KMP_EXPORT kmp_int32 __kmpc_ok_to_fork(ident_t *);
  3360. KMP_EXPORT void __kmpc_fork_call(ident_t *, kmp_int32 nargs,
  3361. kmpc_micro microtask, ...);
  3362. KMP_EXPORT void __kmpc_serialized_parallel(ident_t *, kmp_int32 global_tid);
  3363. KMP_EXPORT void __kmpc_end_serialized_parallel(ident_t *, kmp_int32 global_tid);
  3364. KMP_EXPORT void __kmpc_flush(ident_t *);
  3365. KMP_EXPORT void __kmpc_barrier(ident_t *, kmp_int32 global_tid);
  3366. KMP_EXPORT kmp_int32 __kmpc_master(ident_t *, kmp_int32 global_tid);
  3367. KMP_EXPORT void __kmpc_end_master(ident_t *, kmp_int32 global_tid);
  3368. KMP_EXPORT kmp_int32 __kmpc_masked(ident_t *, kmp_int32 global_tid,
  3369. kmp_int32 filter);
  3370. KMP_EXPORT void __kmpc_end_masked(ident_t *, kmp_int32 global_tid);
  3371. KMP_EXPORT void __kmpc_ordered(ident_t *, kmp_int32 global_tid);
  3372. KMP_EXPORT void __kmpc_end_ordered(ident_t *, kmp_int32 global_tid);
  3373. KMP_EXPORT void __kmpc_critical(ident_t *, kmp_int32 global_tid,
  3374. kmp_critical_name *);
  3375. KMP_EXPORT void __kmpc_end_critical(ident_t *, kmp_int32 global_tid,
  3376. kmp_critical_name *);
  3377. KMP_EXPORT void __kmpc_critical_with_hint(ident_t *, kmp_int32 global_tid,
  3378. kmp_critical_name *, uint32_t hint);
  3379. KMP_EXPORT kmp_int32 __kmpc_barrier_master(ident_t *, kmp_int32 global_tid);
  3380. KMP_EXPORT void __kmpc_end_barrier_master(ident_t *, kmp_int32 global_tid);
  3381. KMP_EXPORT kmp_int32 __kmpc_barrier_master_nowait(ident_t *,
  3382. kmp_int32 global_tid);
  3383. KMP_EXPORT kmp_int32 __kmpc_single(ident_t *, kmp_int32 global_tid);
  3384. KMP_EXPORT void __kmpc_end_single(ident_t *, kmp_int32 global_tid);
  3385. KMP_EXPORT kmp_int32 __kmpc_sections_init(ident_t *loc, kmp_int32 global_tid);
  3386. KMP_EXPORT kmp_int32 __kmpc_next_section(ident_t *loc, kmp_int32 global_tid,
  3387. kmp_int32 numberOfSections);
  3388. KMP_EXPORT void __kmpc_end_sections(ident_t *loc, kmp_int32 global_tid);
  3389. KMP_EXPORT void KMPC_FOR_STATIC_INIT(ident_t *loc, kmp_int32 global_tid,
  3390. kmp_int32 schedtype, kmp_int32 *plastiter,
  3391. kmp_int *plower, kmp_int *pupper,
  3392. kmp_int *pstride, kmp_int incr,
  3393. kmp_int chunk);
  3394. KMP_EXPORT void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid);
  3395. KMP_EXPORT void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid,
  3396. size_t cpy_size, void *cpy_data,
  3397. void (*cpy_func)(void *, void *),
  3398. kmp_int32 didit);
  3399. KMP_EXPORT void *__kmpc_copyprivate_light(ident_t *loc, kmp_int32 gtid,
  3400. void *cpy_data);
  3401. extern void KMPC_SET_NUM_THREADS(int arg);
  3402. extern void KMPC_SET_DYNAMIC(int flag);
  3403. extern void KMPC_SET_NESTED(int flag);
  3404. /* OMP 3.0 tasking interface routines */
  3405. KMP_EXPORT kmp_int32 __kmpc_omp_task(ident_t *loc_ref, kmp_int32 gtid,
  3406. kmp_task_t *new_task);
  3407. KMP_EXPORT kmp_task_t *__kmpc_omp_task_alloc(ident_t *loc_ref, kmp_int32 gtid,
  3408. kmp_int32 flags,
  3409. size_t sizeof_kmp_task_t,
  3410. size_t sizeof_shareds,
  3411. kmp_routine_entry_t task_entry);
  3412. KMP_EXPORT kmp_task_t *__kmpc_omp_target_task_alloc(
  3413. ident_t *loc_ref, kmp_int32 gtid, kmp_int32 flags, size_t sizeof_kmp_task_t,
  3414. size_t sizeof_shareds, kmp_routine_entry_t task_entry, kmp_int64 device_id);
  3415. KMP_EXPORT void __kmpc_omp_task_begin_if0(ident_t *loc_ref, kmp_int32 gtid,
  3416. kmp_task_t *task);
  3417. KMP_EXPORT void __kmpc_omp_task_complete_if0(ident_t *loc_ref, kmp_int32 gtid,
  3418. kmp_task_t *task);
  3419. KMP_EXPORT kmp_int32 __kmpc_omp_task_parts(ident_t *loc_ref, kmp_int32 gtid,
  3420. kmp_task_t *new_task);
  3421. KMP_EXPORT kmp_int32 __kmpc_omp_taskwait(ident_t *loc_ref, kmp_int32 gtid);
  3422. KMP_EXPORT kmp_int32 __kmpc_omp_taskyield(ident_t *loc_ref, kmp_int32 gtid,
  3423. int end_part);
  3424. #if TASK_UNUSED
  3425. void __kmpc_omp_task_begin(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task);
  3426. void __kmpc_omp_task_complete(ident_t *loc_ref, kmp_int32 gtid,
  3427. kmp_task_t *task);
  3428. #endif // TASK_UNUSED
  3429. /* ------------------------------------------------------------------------ */
  3430. KMP_EXPORT void __kmpc_taskgroup(ident_t *loc, int gtid);
  3431. KMP_EXPORT void __kmpc_end_taskgroup(ident_t *loc, int gtid);
  3432. KMP_EXPORT kmp_int32 __kmpc_omp_task_with_deps(
  3433. ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *new_task, kmp_int32 ndeps,
  3434. kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias,
  3435. kmp_depend_info_t *noalias_dep_list);
  3436. KMP_EXPORT void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32 gtid,
  3437. kmp_int32 ndeps,
  3438. kmp_depend_info_t *dep_list,
  3439. kmp_int32 ndeps_noalias,
  3440. kmp_depend_info_t *noalias_dep_list);
  3441. extern kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
  3442. bool serialize_immediate);
  3443. KMP_EXPORT kmp_int32 __kmpc_cancel(ident_t *loc_ref, kmp_int32 gtid,
  3444. kmp_int32 cncl_kind);
  3445. KMP_EXPORT kmp_int32 __kmpc_cancellationpoint(ident_t *loc_ref, kmp_int32 gtid,
  3446. kmp_int32 cncl_kind);
  3447. KMP_EXPORT kmp_int32 __kmpc_cancel_barrier(ident_t *loc_ref, kmp_int32 gtid);
  3448. KMP_EXPORT int __kmp_get_cancellation_status(int cancel_kind);
  3449. KMP_EXPORT void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask);
  3450. KMP_EXPORT void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask);
  3451. KMP_EXPORT void __kmpc_taskloop(ident_t *loc, kmp_int32 gtid, kmp_task_t *task,
  3452. kmp_int32 if_val, kmp_uint64 *lb,
  3453. kmp_uint64 *ub, kmp_int64 st, kmp_int32 nogroup,
  3454. kmp_int32 sched, kmp_uint64 grainsize,
  3455. void *task_dup);
  3456. KMP_EXPORT void __kmpc_taskloop_5(ident_t *loc, kmp_int32 gtid,
  3457. kmp_task_t *task, kmp_int32 if_val,
  3458. kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st,
  3459. kmp_int32 nogroup, kmp_int32 sched,
  3460. kmp_uint64 grainsize, kmp_int32 modifier,
  3461. void *task_dup);
  3462. KMP_EXPORT void *__kmpc_task_reduction_init(int gtid, int num_data, void *data);
  3463. KMP_EXPORT void *__kmpc_taskred_init(int gtid, int num_data, void *data);
  3464. KMP_EXPORT void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void *d);
  3465. KMP_EXPORT void *__kmpc_task_reduction_modifier_init(ident_t *loc, int gtid,
  3466. int is_ws, int num,
  3467. void *data);
  3468. KMP_EXPORT void *__kmpc_taskred_modifier_init(ident_t *loc, int gtid, int is_ws,
  3469. int num, void *data);
  3470. KMP_EXPORT void __kmpc_task_reduction_modifier_fini(ident_t *loc, int gtid,
  3471. int is_ws);
  3472. KMP_EXPORT kmp_int32 __kmpc_omp_reg_task_with_affinity(
  3473. ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *new_task, kmp_int32 naffins,
  3474. kmp_task_affinity_info_t *affin_list);
  3475. KMP_EXPORT void __kmp_set_num_teams(int num_teams);
  3476. KMP_EXPORT int __kmp_get_max_teams(void);
  3477. KMP_EXPORT void __kmp_set_teams_thread_limit(int limit);
  3478. KMP_EXPORT int __kmp_get_teams_thread_limit(void);
  3479. /* Lock interface routines (fast versions with gtid passed in) */
  3480. KMP_EXPORT void __kmpc_init_lock(ident_t *loc, kmp_int32 gtid,
  3481. void **user_lock);
  3482. KMP_EXPORT void __kmpc_init_nest_lock(ident_t *loc, kmp_int32 gtid,
  3483. void **user_lock);
  3484. KMP_EXPORT void __kmpc_destroy_lock(ident_t *loc, kmp_int32 gtid,
  3485. void **user_lock);
  3486. KMP_EXPORT void __kmpc_destroy_nest_lock(ident_t *loc, kmp_int32 gtid,
  3487. void **user_lock);
  3488. KMP_EXPORT void __kmpc_set_lock(ident_t *loc, kmp_int32 gtid, void **user_lock);
  3489. KMP_EXPORT void __kmpc_set_nest_lock(ident_t *loc, kmp_int32 gtid,
  3490. void **user_lock);
  3491. KMP_EXPORT void __kmpc_unset_lock(ident_t *loc, kmp_int32 gtid,
  3492. void **user_lock);
  3493. KMP_EXPORT void __kmpc_unset_nest_lock(ident_t *loc, kmp_int32 gtid,
  3494. void **user_lock);
  3495. KMP_EXPORT int __kmpc_test_lock(ident_t *loc, kmp_int32 gtid, void **user_lock);
  3496. KMP_EXPORT int __kmpc_test_nest_lock(ident_t *loc, kmp_int32 gtid,
  3497. void **user_lock);
  3498. KMP_EXPORT void __kmpc_init_lock_with_hint(ident_t *loc, kmp_int32 gtid,
  3499. void **user_lock, uintptr_t hint);
  3500. KMP_EXPORT void __kmpc_init_nest_lock_with_hint(ident_t *loc, kmp_int32 gtid,
  3501. void **user_lock,
  3502. uintptr_t hint);
  3503. /* Interface to fast scalable reduce methods routines */
  3504. KMP_EXPORT kmp_int32 __kmpc_reduce_nowait(
  3505. ident_t *loc, kmp_int32 global_tid, kmp_int32 num_vars, size_t reduce_size,
  3506. void *reduce_data, void (*reduce_func)(void *lhs_data, void *rhs_data),
  3507. kmp_critical_name *lck);
  3508. KMP_EXPORT void __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid,
  3509. kmp_critical_name *lck);
  3510. KMP_EXPORT kmp_int32 __kmpc_reduce(
  3511. ident_t *loc, kmp_int32 global_tid, kmp_int32 num_vars, size_t reduce_size,
  3512. void *reduce_data, void (*reduce_func)(void *lhs_data, void *rhs_data),
  3513. kmp_critical_name *lck);
  3514. KMP_EXPORT void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid,
  3515. kmp_critical_name *lck);
  3516. /* Internal fast reduction routines */
  3517. extern PACKED_REDUCTION_METHOD_T __kmp_determine_reduction_method(
  3518. ident_t *loc, kmp_int32 global_tid, kmp_int32 num_vars, size_t reduce_size,
  3519. void *reduce_data, void (*reduce_func)(void *lhs_data, void *rhs_data),
  3520. kmp_critical_name *lck);
  3521. // this function is for testing set/get/determine reduce method
  3522. KMP_EXPORT kmp_int32 __kmp_get_reduce_method(void);
  3523. KMP_EXPORT kmp_uint64 __kmpc_get_taskid();
  3524. KMP_EXPORT kmp_uint64 __kmpc_get_parent_taskid();
  3525. // C++ port
  3526. // missing 'extern "C"' declarations
  3527. KMP_EXPORT kmp_int32 __kmpc_in_parallel(ident_t *loc);
  3528. KMP_EXPORT void __kmpc_pop_num_threads(ident_t *loc, kmp_int32 global_tid);
  3529. KMP_EXPORT void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
  3530. kmp_int32 num_threads);
  3531. KMP_EXPORT void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid,
  3532. int proc_bind);
  3533. KMP_EXPORT void __kmpc_push_num_teams(ident_t *loc, kmp_int32 global_tid,
  3534. kmp_int32 num_teams,
  3535. kmp_int32 num_threads);
  3536. /* Function for OpenMP 5.1 num_teams clause */
  3537. KMP_EXPORT void __kmpc_push_num_teams_51(ident_t *loc, kmp_int32 global_tid,
  3538. kmp_int32 num_teams_lb,
  3539. kmp_int32 num_teams_ub,
  3540. kmp_int32 num_threads);
  3541. KMP_EXPORT void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc,
  3542. kmpc_micro microtask, ...);
  3543. struct kmp_dim { // loop bounds info casted to kmp_int64
  3544. kmp_int64 lo; // lower
  3545. kmp_int64 up; // upper
  3546. kmp_int64 st; // stride
  3547. };
  3548. KMP_EXPORT void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid,
  3549. kmp_int32 num_dims,
  3550. const struct kmp_dim *dims);
  3551. KMP_EXPORT void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid,
  3552. const kmp_int64 *vec);
  3553. KMP_EXPORT void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid,
  3554. const kmp_int64 *vec);
  3555. KMP_EXPORT void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid);
  3556. KMP_EXPORT void *__kmpc_threadprivate_cached(ident_t *loc, kmp_int32 global_tid,
  3557. void *data, size_t size,
  3558. void ***cache);
  3559. // Symbols for MS mutual detection.
  3560. extern int _You_must_link_with_exactly_one_OpenMP_library;
  3561. extern int _You_must_link_with_Intel_OpenMP_library;
  3562. #if KMP_OS_WINDOWS && (KMP_VERSION_MAJOR > 4)
  3563. extern int _You_must_link_with_Microsoft_OpenMP_library;
  3564. #endif
  3565. // The routines below are not exported.
  3566. // Consider making them 'static' in corresponding source files.
  3567. void kmp_threadprivate_insert_private_data(int gtid, void *pc_addr,
  3568. void *data_addr, size_t pc_size);
  3569. struct private_common *kmp_threadprivate_insert(int gtid, void *pc_addr,
  3570. void *data_addr,
  3571. size_t pc_size);
  3572. void __kmp_threadprivate_resize_cache(int newCapacity);
  3573. void __kmp_cleanup_threadprivate_caches();
  3574. // ompc_, kmpc_ entries moved from omp.h.
  3575. #if KMP_OS_WINDOWS
  3576. #define KMPC_CONVENTION __cdecl
  3577. #else
  3578. #define KMPC_CONVENTION
  3579. #endif
  3580. #ifndef __OMP_H
  3581. typedef enum omp_sched_t {
  3582. omp_sched_static = 1,
  3583. omp_sched_dynamic = 2,
  3584. omp_sched_guided = 3,
  3585. omp_sched_auto = 4
  3586. } omp_sched_t;
  3587. typedef void *kmp_affinity_mask_t;
  3588. #endif
  3589. KMP_EXPORT void KMPC_CONVENTION ompc_set_max_active_levels(int);
  3590. KMP_EXPORT void KMPC_CONVENTION ompc_set_schedule(omp_sched_t, int);
  3591. KMP_EXPORT int KMPC_CONVENTION ompc_get_ancestor_thread_num(int);
  3592. KMP_EXPORT int KMPC_CONVENTION ompc_get_team_size(int);
  3593. KMP_EXPORT int KMPC_CONVENTION
  3594. kmpc_set_affinity_mask_proc(int, kmp_affinity_mask_t *);
  3595. KMP_EXPORT int KMPC_CONVENTION
  3596. kmpc_unset_affinity_mask_proc(int, kmp_affinity_mask_t *);
  3597. KMP_EXPORT int KMPC_CONVENTION
  3598. kmpc_get_affinity_mask_proc(int, kmp_affinity_mask_t *);
  3599. KMP_EXPORT void KMPC_CONVENTION kmpc_set_stacksize(int);
  3600. KMP_EXPORT void KMPC_CONVENTION kmpc_set_stacksize_s(size_t);
  3601. KMP_EXPORT void KMPC_CONVENTION kmpc_set_library(int);
  3602. KMP_EXPORT void KMPC_CONVENTION kmpc_set_defaults(char const *);
  3603. KMP_EXPORT void KMPC_CONVENTION kmpc_set_disp_num_buffers(int);
  3604. void KMP_EXPAND_NAME(ompc_set_affinity_format)(char const *format);
  3605. size_t KMP_EXPAND_NAME(ompc_get_affinity_format)(char *buffer, size_t size);
  3606. void KMP_EXPAND_NAME(ompc_display_affinity)(char const *format);
  3607. size_t KMP_EXPAND_NAME(ompc_capture_affinity)(char *buffer, size_t buf_size,
  3608. char const *format);
  3609. enum kmp_target_offload_kind {
  3610. tgt_disabled = 0,
  3611. tgt_default = 1,
  3612. tgt_mandatory = 2
  3613. };
  3614. typedef enum kmp_target_offload_kind kmp_target_offload_kind_t;
  3615. // Set via OMP_TARGET_OFFLOAD if specified, defaults to tgt_default otherwise
  3616. extern kmp_target_offload_kind_t __kmp_target_offload;
  3617. extern int __kmpc_get_target_offload();
  3618. // Constants used in libomptarget
  3619. #define KMP_DEVICE_DEFAULT -1 // This is libomptarget's default device.
  3620. #define KMP_DEVICE_ALL -11 // This is libomptarget's "all devices".
  3621. // OMP Pause Resource
  3622. // The following enum is used both to set the status in __kmp_pause_status, and
  3623. // as the internal equivalent of the externally-visible omp_pause_resource_t.
  3624. typedef enum kmp_pause_status_t {
  3625. kmp_not_paused = 0, // status is not paused, or, requesting resume
  3626. kmp_soft_paused = 1, // status is soft-paused, or, requesting soft pause
  3627. kmp_hard_paused = 2 // status is hard-paused, or, requesting hard pause
  3628. } kmp_pause_status_t;
  3629. // This stores the pause state of the runtime
  3630. extern kmp_pause_status_t __kmp_pause_status;
  3631. extern int __kmpc_pause_resource(kmp_pause_status_t level);
  3632. extern int __kmp_pause_resource(kmp_pause_status_t level);
  3633. // Soft resume sets __kmp_pause_status, and wakes up all threads.
  3634. extern void __kmp_resume_if_soft_paused();
  3635. // Hard resume simply resets the status to not paused. Library will appear to
  3636. // be uninitialized after hard pause. Let OMP constructs trigger required
  3637. // initializations.
  3638. static inline void __kmp_resume_if_hard_paused() {
  3639. if (__kmp_pause_status == kmp_hard_paused) {
  3640. __kmp_pause_status = kmp_not_paused;
  3641. }
  3642. }
  3643. extern void __kmp_omp_display_env(int verbose);
  3644. // 1: it is initializing hidden helper team
  3645. extern volatile int __kmp_init_hidden_helper;
  3646. // 1: the hidden helper team is done
  3647. extern volatile int __kmp_hidden_helper_team_done;
  3648. // 1: enable hidden helper task
  3649. extern kmp_int32 __kmp_enable_hidden_helper;
  3650. // Main thread of hidden helper team
  3651. extern kmp_info_t *__kmp_hidden_helper_main_thread;
  3652. // Descriptors for the hidden helper threads
  3653. extern kmp_info_t **__kmp_hidden_helper_threads;
  3654. // Number of hidden helper threads
  3655. extern kmp_int32 __kmp_hidden_helper_threads_num;
  3656. // Number of hidden helper tasks that have not been executed yet
  3657. extern std::atomic<kmp_int32> __kmp_unexecuted_hidden_helper_tasks;
  3658. extern void __kmp_hidden_helper_initialize();
  3659. extern void __kmp_hidden_helper_threads_initz_routine();
  3660. extern void __kmp_do_initialize_hidden_helper_threads();
  3661. extern void __kmp_hidden_helper_threads_initz_wait();
  3662. extern void __kmp_hidden_helper_initz_release();
  3663. extern void __kmp_hidden_helper_threads_deinitz_wait();
  3664. extern void __kmp_hidden_helper_threads_deinitz_release();
  3665. extern void __kmp_hidden_helper_main_thread_wait();
  3666. extern void __kmp_hidden_helper_worker_thread_wait();
  3667. extern void __kmp_hidden_helper_worker_thread_signal();
  3668. extern void __kmp_hidden_helper_main_thread_release();
  3669. // Check whether a given thread is a hidden helper thread
  3670. #define KMP_HIDDEN_HELPER_THREAD(gtid) \
  3671. ((gtid) >= 1 && (gtid) <= __kmp_hidden_helper_threads_num)
  3672. #define KMP_HIDDEN_HELPER_WORKER_THREAD(gtid) \
  3673. ((gtid) > 1 && (gtid) <= __kmp_hidden_helper_threads_num)
  3674. #define KMP_HIDDEN_HELPER_TEAM(team) \
  3675. (team->t.t_threads[0] == __kmp_hidden_helper_main_thread)
  3676. // Map a gtid to a hidden helper thread. The first hidden helper thread, a.k.a
  3677. // main thread, is skipped.
  3678. #define KMP_GTID_TO_SHADOW_GTID(gtid) \
  3679. ((gtid) % (__kmp_hidden_helper_threads_num - 1) + 2)
  3680. // Return the adjusted gtid value by subtracting from gtid the number
  3681. // of hidden helper threads. This adjusted value is the gtid the thread would
  3682. // have received if there were no hidden helper threads.
  3683. static inline int __kmp_adjust_gtid_for_hidden_helpers(int gtid) {
  3684. int adjusted_gtid = gtid;
  3685. if (__kmp_hidden_helper_threads_num > 0 && gtid > 0 &&
  3686. gtid - __kmp_hidden_helper_threads_num >= 0) {
  3687. adjusted_gtid -= __kmp_hidden_helper_threads_num;
  3688. }
  3689. return adjusted_gtid;
  3690. }
  3691. // Support for error directive
  3692. typedef enum kmp_severity_t {
  3693. severity_warning = 1,
  3694. severity_fatal = 2
  3695. } kmp_severity_t;
  3696. extern void __kmpc_error(ident_t *loc, int severity, const char *message);
  3697. // Support for scope directive
  3698. KMP_EXPORT void __kmpc_scope(ident_t *loc, kmp_int32 gtid, void *reserved);
  3699. KMP_EXPORT void __kmpc_end_scope(ident_t *loc, kmp_int32 gtid, void *reserved);
  3700. #ifdef __cplusplus
  3701. }
  3702. #endif
  3703. template <bool C, bool S>
  3704. extern void __kmp_suspend_32(int th_gtid, kmp_flag_32<C, S> *flag);
  3705. template <bool C, bool S>
  3706. extern void __kmp_suspend_64(int th_gtid, kmp_flag_64<C, S> *flag);
  3707. template <bool C, bool S>
  3708. extern void __kmp_atomic_suspend_64(int th_gtid,
  3709. kmp_atomic_flag_64<C, S> *flag);
  3710. extern void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag);
  3711. #if KMP_HAVE_MWAIT || KMP_HAVE_UMWAIT
  3712. template <bool C, bool S>
  3713. extern void __kmp_mwait_32(int th_gtid, kmp_flag_32<C, S> *flag);
  3714. template <bool C, bool S>
  3715. extern void __kmp_mwait_64(int th_gtid, kmp_flag_64<C, S> *flag);
  3716. template <bool C, bool S>
  3717. extern void __kmp_atomic_mwait_64(int th_gtid, kmp_atomic_flag_64<C, S> *flag);
  3718. extern void __kmp_mwait_oncore(int th_gtid, kmp_flag_oncore *flag);
  3719. #endif
  3720. template <bool C, bool S>
  3721. extern void __kmp_resume_32(int target_gtid, kmp_flag_32<C, S> *flag);
  3722. template <bool C, bool S>
  3723. extern void __kmp_resume_64(int target_gtid, kmp_flag_64<C, S> *flag);
  3724. template <bool C, bool S>
  3725. extern void __kmp_atomic_resume_64(int target_gtid,
  3726. kmp_atomic_flag_64<C, S> *flag);
  3727. extern void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag);
  3728. template <bool C, bool S>
  3729. int __kmp_execute_tasks_32(kmp_info_t *thread, kmp_int32 gtid,
  3730. kmp_flag_32<C, S> *flag, int final_spin,
  3731. int *thread_finished,
  3732. #if USE_ITT_BUILD
  3733. void *itt_sync_obj,
  3734. #endif /* USE_ITT_BUILD */
  3735. kmp_int32 is_constrained);
  3736. template <bool C, bool S>
  3737. int __kmp_execute_tasks_64(kmp_info_t *thread, kmp_int32 gtid,
  3738. kmp_flag_64<C, S> *flag, int final_spin,
  3739. int *thread_finished,
  3740. #if USE_ITT_BUILD
  3741. void *itt_sync_obj,
  3742. #endif /* USE_ITT_BUILD */
  3743. kmp_int32 is_constrained);
  3744. template <bool C, bool S>
  3745. int __kmp_atomic_execute_tasks_64(kmp_info_t *thread, kmp_int32 gtid,
  3746. kmp_atomic_flag_64<C, S> *flag,
  3747. int final_spin, int *thread_finished,
  3748. #if USE_ITT_BUILD
  3749. void *itt_sync_obj,
  3750. #endif /* USE_ITT_BUILD */
  3751. kmp_int32 is_constrained);
  3752. int __kmp_execute_tasks_oncore(kmp_info_t *thread, kmp_int32 gtid,
  3753. kmp_flag_oncore *flag, int final_spin,
  3754. int *thread_finished,
  3755. #if USE_ITT_BUILD
  3756. void *itt_sync_obj,
  3757. #endif /* USE_ITT_BUILD */
  3758. kmp_int32 is_constrained);
  3759. extern int __kmp_nesting_mode;
  3760. extern int __kmp_nesting_mode_nlevels;
  3761. extern int *__kmp_nesting_nth_level;
  3762. extern void __kmp_init_nesting_mode();
  3763. extern void __kmp_set_nesting_mode_threads();
  3764. /// This class safely opens and closes a C-style FILE* object using RAII
  3765. /// semantics. There are also methods which allow using stdout or stderr as
  3766. /// the underlying FILE* object. With the implicit conversion operator to
  3767. /// FILE*, an object with this type can be used in any function which takes
  3768. /// a FILE* object e.g., fprintf().
  3769. /// No close method is needed at use sites.
  3770. class kmp_safe_raii_file_t {
  3771. FILE *f;
  3772. void close() {
  3773. if (f && f != stdout && f != stderr) {
  3774. fclose(f);
  3775. f = nullptr;
  3776. }
  3777. }
  3778. public:
  3779. kmp_safe_raii_file_t() : f(nullptr) {}
  3780. kmp_safe_raii_file_t(const char *filename, const char *mode,
  3781. const char *env_var = nullptr)
  3782. : f(nullptr) {
  3783. open(filename, mode, env_var);
  3784. }
  3785. ~kmp_safe_raii_file_t() { close(); }
  3786. /// Open filename using mode. This is automatically closed in the destructor.
  3787. /// The env_var parameter indicates the environment variable the filename
  3788. /// came from if != nullptr.
  3789. void open(const char *filename, const char *mode,
  3790. const char *env_var = nullptr) {
  3791. KMP_ASSERT(!f);
  3792. f = fopen(filename, mode);
  3793. if (!f) {
  3794. int code = errno;
  3795. if (env_var) {
  3796. __kmp_fatal(KMP_MSG(CantOpenFileForReading, filename), KMP_ERR(code),
  3797. KMP_HNT(CheckEnvVar, env_var, filename), __kmp_msg_null);
  3798. } else {
  3799. __kmp_fatal(KMP_MSG(CantOpenFileForReading, filename), KMP_ERR(code),
  3800. __kmp_msg_null);
  3801. }
  3802. }
  3803. }
  3804. /// Instead of erroring out, return non-zero when
  3805. /// unsuccessful fopen() for any reason
  3806. int try_open(const char *filename, const char *mode) {
  3807. KMP_ASSERT(!f);
  3808. f = fopen(filename, mode);
  3809. if (!f)
  3810. return errno;
  3811. return 0;
  3812. }
  3813. /// Set the FILE* object to stdout and output there
  3814. /// No open call should happen before this call.
  3815. void set_stdout() {
  3816. KMP_ASSERT(!f);
  3817. f = stdout;
  3818. }
  3819. /// Set the FILE* object to stderr and output there
  3820. /// No open call should happen before this call.
  3821. void set_stderr() {
  3822. KMP_ASSERT(!f);
  3823. f = stderr;
  3824. }
  3825. operator bool() { return bool(f); }
  3826. operator FILE *() { return f; }
  3827. };
  3828. template <typename SourceType, typename TargetType,
  3829. bool isSourceSmaller = (sizeof(SourceType) < sizeof(TargetType)),
  3830. bool isSourceEqual = (sizeof(SourceType) == sizeof(TargetType)),
  3831. bool isSourceSigned = std::is_signed<SourceType>::value,
  3832. bool isTargetSigned = std::is_signed<TargetType>::value>
  3833. struct kmp_convert {};
  3834. // Both types are signed; Source smaller
  3835. template <typename SourceType, typename TargetType>
  3836. struct kmp_convert<SourceType, TargetType, true, false, true, true> {
  3837. static TargetType to(SourceType src) { return (TargetType)src; }
  3838. };
  3839. // Source equal
  3840. template <typename SourceType, typename TargetType>
  3841. struct kmp_convert<SourceType, TargetType, false, true, true, true> {
  3842. static TargetType to(SourceType src) { return src; }
  3843. };
  3844. // Source bigger
  3845. template <typename SourceType, typename TargetType>
  3846. struct kmp_convert<SourceType, TargetType, false, false, true, true> {
  3847. static TargetType to(SourceType src) {
  3848. KMP_ASSERT(src <= static_cast<SourceType>(
  3849. (std::numeric_limits<TargetType>::max)()));
  3850. KMP_ASSERT(src >= static_cast<SourceType>(
  3851. (std::numeric_limits<TargetType>::min)()));
  3852. return (TargetType)src;
  3853. }
  3854. };
  3855. // Source signed, Target unsigned
  3856. // Source smaller
  3857. template <typename SourceType, typename TargetType>
  3858. struct kmp_convert<SourceType, TargetType, true, false, true, false> {
  3859. static TargetType to(SourceType src) {
  3860. KMP_ASSERT(src >= 0);
  3861. return (TargetType)src;
  3862. }
  3863. };
  3864. // Source equal
  3865. template <typename SourceType, typename TargetType>
  3866. struct kmp_convert<SourceType, TargetType, false, true, true, false> {
  3867. static TargetType to(SourceType src) {
  3868. KMP_ASSERT(src >= 0);
  3869. return (TargetType)src;
  3870. }
  3871. };
  3872. // Source bigger
  3873. template <typename SourceType, typename TargetType>
  3874. struct kmp_convert<SourceType, TargetType, false, false, true, false> {
  3875. static TargetType to(SourceType src) {
  3876. KMP_ASSERT(src >= 0);
  3877. KMP_ASSERT(src <= static_cast<SourceType>(
  3878. (std::numeric_limits<TargetType>::max)()));
  3879. return (TargetType)src;
  3880. }
  3881. };
  3882. // Source unsigned, Target signed
  3883. // Source smaller
  3884. template <typename SourceType, typename TargetType>
  3885. struct kmp_convert<SourceType, TargetType, true, false, false, true> {
  3886. static TargetType to(SourceType src) { return (TargetType)src; }
  3887. };
  3888. // Source equal
  3889. template <typename SourceType, typename TargetType>
  3890. struct kmp_convert<SourceType, TargetType, false, true, false, true> {
  3891. static TargetType to(SourceType src) {
  3892. KMP_ASSERT(src <= static_cast<SourceType>(
  3893. (std::numeric_limits<TargetType>::max)()));
  3894. return (TargetType)src;
  3895. }
  3896. };
  3897. // Source bigger
  3898. template <typename SourceType, typename TargetType>
  3899. struct kmp_convert<SourceType, TargetType, false, false, false, true> {
  3900. static TargetType to(SourceType src) {
  3901. KMP_ASSERT(src <= static_cast<SourceType>(
  3902. (std::numeric_limits<TargetType>::max)()));
  3903. return (TargetType)src;
  3904. }
  3905. };
  3906. // Source unsigned, Target unsigned
  3907. // Source smaller
  3908. template <typename SourceType, typename TargetType>
  3909. struct kmp_convert<SourceType, TargetType, true, false, false, false> {
  3910. static TargetType to(SourceType src) { return (TargetType)src; }
  3911. };
  3912. // Source equal
  3913. template <typename SourceType, typename TargetType>
  3914. struct kmp_convert<SourceType, TargetType, false, true, false, false> {
  3915. static TargetType to(SourceType src) { return src; }
  3916. };
  3917. // Source bigger
  3918. template <typename SourceType, typename TargetType>
  3919. struct kmp_convert<SourceType, TargetType, false, false, false, false> {
  3920. static TargetType to(SourceType src) {
  3921. KMP_ASSERT(src <= static_cast<SourceType>(
  3922. (std::numeric_limits<TargetType>::max)()));
  3923. return (TargetType)src;
  3924. }
  3925. };
  3926. template <typename T1, typename T2>
  3927. static inline void __kmp_type_convert(T1 src, T2 *dest) {
  3928. *dest = kmp_convert<T1, T2>::to(src);
  3929. }
  3930. #endif /* KMP_H */