1 /** 2 * Copyright 2019-2021 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_GLOBAL_CONTEXT_H_ 17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_GLOBAL_CONTEXT_H_ 18 19 #include <memory> 20 #include <mutex> 21 22 #include "include/api/status.h" 23 #include "minddata/dataset/core/config_manager.h" 24 #include "minddata/dataset/include/dataset/constants.h" 25 #include "minddata/dataset/util/allocator.h" 26 27 namespace mindspore { 28 namespace dataset { 29 // forward declare 30 class MemoryPool; 31 class Tensor; 32 class CVTensor; 33 class DeviceTensor; 34 35 using TensorAlloc = Allocator<Tensor>; // An allocator for Tensors 36 using CVTensorAlloc = Allocator<CVTensor>; // An allocator CVTensors 37 using DeviceTensorAlloc = Allocator<DeviceTensor>; // An allocator for Device_Tensors 38 using IntAlloc = Allocator<dsize_t>; 39 40 class GlobalContext { 41 // some consts for pool config 42 static constexpr int kArenaSize = 128; 43 static constexpr int kMaxSize = -1; 44 static constexpr bool kInitArena = true; 45 46 public: 47 // Singleton pattern. This method either: 48 // - creates the single version of the GlobalContext for the first time and returns it 49 // OR 50 // - returns the already existing single instance of the GlobalContext 51 // @return the single global context 52 static GlobalContext *Instance(); 53 54 // Destructor 55 ~GlobalContext() = default; 56 57 // A print method typically used for debugging 58 // @param out - The output stream to write output to 59 void Print(std::ostream &out) const; 60 61 // << Stream output operator overload 62 // @notes This allows you to write the debug print info using stream operators 63 // @param out - reference to the output stream being overloaded 64 // @param g_c - reference to the GlobalContext to display 65 // @return - the output stream must be returned 66 friend std::ostream &operator<<(std::ostream &out, const GlobalContext &g_c) { 67 g_c.Print(out); 68 return out; 69 } 70 71 // Getter method 72 // @return the client config as raw const pointer config_manager()73 static std::shared_ptr<ConfigManager> config_manager() { return Instance()->config_manager_; } 74 75 // Getter method 76 // @return the mem pool mem_pool()77 std::shared_ptr<MemoryPool> mem_pool() const { return mem_pool_; } 78 79 // Getter method 80 // @return the tensor allocator as raw pointer tensor_allocator()81 const TensorAlloc *tensor_allocator() const { return tensor_allocator_.get(); } 82 83 // Getter method 84 // @return the CVTensor allocator as raw pointer cv_tensor_allocator()85 const CVTensorAlloc *cv_tensor_allocator() const { return cv_tensor_allocator_.get(); } 86 87 // Getter method 88 // @return the DeviceTensor allocator as raw pointer device_tensor_allocator()89 const DeviceTensorAlloc *device_tensor_allocator() const { return device_tensor_allocator_.get(); } 90 91 // Getter method 92 // @return the integer allocator as raw pointer int_allocator()93 const IntAlloc *int_allocator() const { return int_allocator_.get(); } 94 95 private: 96 // Constructor. 97 // @note Singleton. Instantiation flows through instance() 98 // @return This is a constructor. 99 GlobalContext() = default; 100 101 Status Init(); 102 103 static std::once_flag init_instance_flag_; 104 static std::unique_ptr<GlobalContext> global_context_; // The instance of the singleton (global) 105 std::shared_ptr<MemoryPool> mem_pool_; // A global memory pool 106 std::shared_ptr<ConfigManager> config_manager_; // The configs 107 std::unique_ptr<TensorAlloc> tensor_allocator_; // An allocator for Tensors 108 std::unique_ptr<CVTensorAlloc> cv_tensor_allocator_; // An allocator for CV Tensors 109 std::unique_ptr<DeviceTensorAlloc> device_tensor_allocator_; // An allocator for Device Tensors 110 std::unique_ptr<IntAlloc> int_allocator_; // An allocator for ints 111 }; 112 } // namespace dataset 113 } // namespace mindspore 114 115 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_GLOBAL_CONTEXT_H_ 116