compute_session_pool.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2017 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // =============================================================================
  15. #ifndef NLP_SAFT_OPENSOURCE_DRAGNN_CORE_COMPUTE_SESSION_POOL_H_
  16. #define NLP_SAFT_OPENSOURCE_DRAGNN_CORE_COMPUTE_SESSION_POOL_H_
  17. #include <memory>
  18. #include "dragnn/core/compute_session.h"
  19. #include "dragnn/protos/spec.pb.h"
  20. #include "tensorflow/core/platform/mutex.h"
  21. namespace syntaxnet {
  22. namespace dragnn {
  23. // This pool creates and manages the reuse of ComputeSession objects.
  24. class ComputeSessionPool {
  25. public:
  26. // Create a ComputeSessionPool that creates ComputeSessions for the given
  27. // MasterSpec and hyperparameters.
  28. ComputeSessionPool(const MasterSpec &master_spec,
  29. const GridPoint &hyperparams);
  30. virtual ~ComputeSessionPool();
  31. // Get a ComputeSession. This function will attempt to use an already-created
  32. // ComputeSession, but if none are available a new one will be created.
  33. std::unique_ptr<ComputeSession> GetSession();
  34. // Returns a ComputeSession to the backing pool.
  35. void ReturnSession(std::unique_ptr<ComputeSession> session);
  36. // Returns the count of outstanding unique sessions.
  37. int num_outstanding_sessions() {
  38. tensorflow::mutex_lock lock(lock_);
  39. return num_unique_sessions_ - sessions_.size();
  40. }
  41. private:
  42. friend class ComputeSessionImplTestPoolAccessor;
  43. friend class ComputeSessionPoolTestPoolAccessor;
  44. // This is a creational injection setter. It should be used for tests
  45. // where we want our ComputeSessionPool to prepare and return
  46. // MockComputeSessions instead of actual ComputeSessionImpls.
  47. void SetComputeSessionBuilder(
  48. std::function<std::unique_ptr<ComputeSession>()> session_builder);
  49. // This injector will cause ComputeSessions built in this pool to use the
  50. // passed function to create Components. This is useful when you want a
  51. // ComputeSession to create MockComponents instead of real ones.
  52. void SetComponentBuilder(
  53. std::function<std::unique_ptr<Component>(const string &component_name,
  54. const string &backend_type)>
  55. component_builder);
  56. // The MasterSpec that will be used to initialize ComputeSessions from this
  57. // pool.
  58. const MasterSpec master_spec_;
  59. // The hyperparameters that will be used to initialize ComputeSessions from
  60. // this pool.
  61. const GridPoint hyperparams_;
  62. // The function that is used to create ComputeSessions.
  63. std::function<std::unique_ptr<ComputeSession>()> session_builder_;
  64. // The function passed to ComputeSessions that will be used by that session
  65. // to create components.
  66. std::function<std::unique_ptr<Component>(const string &component_name,
  67. const string &backend_type)>
  68. component_builder_;
  69. // ComputeSessions that are not currently being used. These sessions are not
  70. // reset until they are requested by another thread.
  71. std::vector<std::unique_ptr<ComputeSession>> sessions_;
  72. // Count of the number of unique ComputeSession objects that have been
  73. // created. Used to assign IDs to new Sessions.
  74. int num_unique_sessions_;
  75. // Mutex that protects accesses to all members of this object.
  76. tensorflow::mutex lock_;
  77. };
  78. } // namespace dragnn
  79. } // namespace syntaxnet
  80. #endif // NLP_SAFT_OPENSOURCE_DRAGNN_CORE_COMPUTE_SESSION_POOL_H_