1 /** 2 * Copyright 2020 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_ENGINE_CACHE_ARENA_H_ 17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_CACHE_ARENA_H_ 18 19 #include <memory> 20 #include <mutex> 21 #include <vector> 22 #include <string> 23 #include <utility> 24 #include "minddata/dataset/util/arena.h" 25 #include "minddata/dataset/engine/cache/cache_common.h" 26 #include "minddata/dataset/engine/cache/cache_ipc.h" 27 namespace mindspore { 28 namespace dataset { 29 /// This is like a CircularPool but each arena is in shared memory and 30 /// possibly bind to a numa socket. 31 class CachedSharedMemory { 32 public: 33 // Disable copy and assignment constructor 34 CachedSharedMemory(const CachedSharedMemory &) = delete; 35 CachedSharedMemory &operator=(const CachedSharedMemory &) = delete; 36 ~CachedSharedMemory(); 37 38 /// \brief Create an Arena in shared memory 39 /// \param[out] p_ba Pointer to a unique_ptr 40 /// \param shmkey Shared memory key 41 /// \param val_in_GB size of shared memory in gigabyte 42 /// \return Status object 43 static Status CreateArena(std::unique_ptr<CachedSharedMemory> *out, int32_t port, size_t val_in_GB); 44 45 /// \brief This returns where we attach to the shared memory. 46 /// Some gRPC requests will ask for a shared memory block, and 47 /// we can't return the absolute address as this makes no sense 48 /// in the client. So instead we will return an address relative 49 /// to the base address of the shared memory where we attach to. 50 /// \return Base address of the shared memory. SharedMemoryBaseAddr()51 const void *SharedMemoryBaseAddr() const { return shm_.SharedMemoryBaseAddr(); } SharedMemoryBaseAddr()52 void *SharedMemoryBaseAddr() { return shm_.SharedMemoryBaseAddr(); } 53 54 /// \brief Get the shared memory key of the shared memory GetKey()55 SharedMemory::shm_key_t GetKey() const { return shm_.GetKey(); } 56 57 /// \brief Allocate shared memory for a given pipeline 58 Status AllocateSharedMemory(int32_t client_id, size_t sz, void **p); 59 60 /// \brief Deallocate shared memory for a given pipeline 61 void DeallocateSharedMemory(int32_t client_id, void *p); 62 63 private: 64 int32_t shared_memory_sz_in_gb_; 65 int32_t port_; 66 SharedMemory shm_; 67 std::vector<std::unique_ptr<ArenaImpl>> shm_pool_; 68 std::unique_ptr<std::mutex[]> mux_; 69 int32_t num_numa_nodes_; 70 int64_t sub_pool_sz_; 71 /// Private constructor. Not to be called directly. 72 CachedSharedMemory(int32_t port, size_t val_in_GB); 73 Status Init(); 74 }; 75 } // namespace dataset 76 } // namespace mindspore 77 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_CACHE_ARENA_H_ 78