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_PS_PS_CACHE_PS_CACHE_FACTORY_H_ 17 #define MINDSPORE_CCSRC_PS_PS_CACHE_PS_CACHE_FACTORY_H_ 18 19 #include <functional> 20 #include <map> 21 #include <memory> 22 #include <string> 23 #include <utility> 24 #include "ps/ps_cache/ps_cache_basic.h" 25 #include "utils/ms_utils.h" 26 27 namespace mindspore { 28 namespace ps { 29 using PsCacheCreator = std::function<std::shared_ptr<PsCacheBasic>()>; 30 class PsCacheFactory { 31 public: 32 static PsCacheFactory &Get(); 33 void Register(const std::string &device_name, PsCacheCreator &&ps_cache_creator); 34 std::shared_ptr<PsCacheBasic> ps_cache(const std::string &device_name); 35 36 private: 37 PsCacheFactory() = default; 38 ~PsCacheFactory() = default; 39 DISABLE_COPY_AND_ASSIGN(PsCacheFactory) 40 std::map<std::string, PsCacheCreator> ps_cache_creators_; 41 }; 42 43 class PsCacheRegistrar { 44 public: PsCacheRegistrar(const std::string & device_name,PsCacheCreator && ps_cache_creator)45 PsCacheRegistrar(const std::string &device_name, PsCacheCreator &&ps_cache_creator) { 46 PsCacheFactory::Get().Register(device_name, std::move(ps_cache_creator)); 47 } 48 ~PsCacheRegistrar() = default; 49 }; 50 51 #define MS_REG_PS_CACHE(DEVICE_NAME, PS_CACHE_CLASS) \ 52 static const PsCacheRegistrar g_ps_cache_registrar__##DEVICE_NAME##_##_reg( \ 53 DEVICE_NAME, []() { return std::make_shared<PS_CACHE_CLASS>(); }); 54 } // namespace ps 55 } // namespace mindspore 56 57 #endif // MINDSPORE_CCSRC_PS_PS_CACHE_PS_CACHE_FACTORY_H_ 58