ParallelJobList.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __PARALLELJOBLIST_H__
  21. #define __PARALLELJOBLIST_H__
  22. struct CellSpursJob128;
  23. class idColor;
  24. typedef void ( * jobRun_t )( void * );
  25. enum jobSyncType_t {
  26. SYNC_NONE,
  27. SYNC_SIGNAL,
  28. SYNC_SYNCHRONIZE
  29. };
  30. // NOTE: keep in sync with jobNames[]
  31. enum jobListId_t {
  32. JOBLIST_RENDERER_FRONTEND = 0,
  33. JOBLIST_RENDERER_BACKEND = 1,
  34. JOBLIST_UTILITY = 9, // won't print over-time warnings
  35. MAX_JOBLISTS = 32 // the editor may cause quite a few to be allocated
  36. };
  37. compile_time_assert( CONST_ISPOWEROFTWO( MAX_JOBLISTS ) );
  38. enum jobListPriority_t {
  39. JOBLIST_PRIORITY_NONE,
  40. JOBLIST_PRIORITY_LOW,
  41. JOBLIST_PRIORITY_MEDIUM,
  42. JOBLIST_PRIORITY_HIGH
  43. };
  44. enum jobListParallelism_t {
  45. JOBLIST_PARALLELISM_DEFAULT = -1, // use "jobs_numThreads" number of threads
  46. JOBLIST_PARALLELISM_MAX_CORES = -2, // use a thread for each logical core (includes hyperthreads)
  47. JOBLIST_PARALLELISM_MAX_THREADS = -3 // use the maximum number of job threads, which can help if there is IO to overlap
  48. };
  49. #define assert_spu_local_store( ptr )
  50. #define assert_not_spu_local_store( ptr )
  51. /*
  52. ================================================
  53. idParallelJobList
  54. A job should be at least a couple of 1000 clock cycles in
  55. order to outweigh any job switching overhead. On the other
  56. hand a job should consume no more than a couple of
  57. 100,000 clock cycles to maintain a good load balance over
  58. multiple processing units.
  59. ================================================
  60. */
  61. class idParallelJobList {
  62. friend class idParallelJobManagerLocal;
  63. public:
  64. void AddJob( jobRun_t function, void * data );
  65. CellSpursJob128 * AddJobSPURS();
  66. void InsertSyncPoint( jobSyncType_t syncType );
  67. // Submit the jobs in this list.
  68. void Submit( idParallelJobList * waitForJobList = NULL, int parallelism = JOBLIST_PARALLELISM_DEFAULT );
  69. // Wait for the jobs in this list to finish. Will spin in place if any jobs are not done.
  70. void Wait();
  71. // Try to wait for the jobs in this list to finish but either way return immediately. Returns true if all jobs are done.
  72. bool TryWait();
  73. // returns true if the job list has been submitted.
  74. bool IsSubmitted() const;
  75. // Get the number of jobs executed in this job list.
  76. unsigned int GetNumExecutedJobs() const;
  77. // Get the number of sync points.
  78. unsigned int GetNumSyncs() const;
  79. // Time at which the job list was submitted.
  80. uint64 GetSubmitTimeMicroSec() const;
  81. // Time at which execution of this job list started.
  82. uint64 GetStartTimeMicroSec() const;
  83. // Time at which all jobs in the list were executed.
  84. uint64 GetFinishTimeMicroSec() const;
  85. // Time the host thread waited for this job list to finish.
  86. uint64 GetWaitTimeMicroSec() const;
  87. // Get the total time all units spent processing this job list.
  88. uint64 GetTotalProcessingTimeMicroSec() const;
  89. // Get the total time all units wasted while processing this job list.
  90. uint64 GetTotalWastedTimeMicroSec() const;
  91. // Time the given unit spent processing this job list.
  92. uint64 GetUnitProcessingTimeMicroSec( int unit ) const;
  93. // Time the given unit wasted while processing this job list.
  94. uint64 GetUnitWastedTimeMicroSec( int unit ) const;
  95. // Get the job list ID
  96. jobListId_t GetId() const;
  97. // Get the color for profiling.
  98. const idColor * GetColor() const { return this->color; }
  99. private:
  100. class idParallelJobList_Threads * jobListThreads;
  101. const idColor * color;
  102. idParallelJobList( jobListId_t id, jobListPriority_t priority, unsigned int maxJobs, unsigned int maxSyncs, const idColor * color );
  103. ~idParallelJobList();
  104. };
  105. /*
  106. ================================================
  107. idParallelJobManager
  108. This is the only interface through which job lists
  109. should be allocated or freed.
  110. ================================================
  111. */
  112. class idParallelJobManager {
  113. public:
  114. virtual ~idParallelJobManager() {}
  115. virtual void Init() = 0;
  116. virtual void Shutdown() = 0;
  117. virtual idParallelJobList * AllocJobList( jobListId_t id, jobListPriority_t priority, unsigned int maxJobs, unsigned int maxSyncs, const idColor * color ) = 0;
  118. virtual void FreeJobList( idParallelJobList * jobList ) = 0;
  119. virtual int GetNumJobLists() const = 0;
  120. virtual int GetNumFreeJobLists() const = 0;
  121. virtual idParallelJobList * GetJobList( int index ) = 0;
  122. virtual int GetNumProcessingUnits() = 0;
  123. virtual void WaitForAllJobLists() = 0;
  124. };
  125. extern idParallelJobManager * parallelJobManager;
  126. // jobRun_t functions can have the debug name associated with them
  127. // by explicitly calling this, or using the REGISTER_PARALLEL_JOB()
  128. // static variable macro.
  129. void RegisterJob( jobRun_t function, const char * name );
  130. /*
  131. ================================================
  132. idParallelJobRegistration
  133. ================================================
  134. */
  135. class idParallelJobRegistration {
  136. public:
  137. idParallelJobRegistration( jobRun_t function, const char * name );
  138. };
  139. #define REGISTER_PARALLEL_JOB( function, name ) static idParallelJobRegistration register_##function( (jobRun_t) function, name )
  140. #endif // !__PARALLELJOBLIST_H__