1 /** 2 * Copyright 2023 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 17 #ifndef MINDSPORE_CORE_UTILS_SYSTEM_IO_HANDLE_H_ 18 #define MINDSPORE_CORE_UTILS_SYSTEM_IO_HANDLE_H_ 19 20 #include <memory> 21 #include <mutex> 22 #include <queue> 23 #include <string> 24 #include <utility> 25 #include "include/backend/visible.h" 26 27 namespace mindspore { 28 namespace device { 29 using AsyncIOToken = size_t; 30 constexpr AsyncIOToken kInvalidAsyncIOToken = 0; 31 32 struct AsyncIOConf { 33 size_t block_size; 34 size_t queue_depth; 35 }; 36 37 class AsyncIO { 38 public: 39 AsyncIO() = default; 40 virtual ~AsyncIO() = default; 41 virtual bool Init(const AsyncIOConf &conf) = 0; 42 virtual bool Read(const std::string &file_name, void *data, size_t byte_num) = 0; 43 virtual bool Write(const std::string &file_name, const void *data, size_t byte_num) = 0; 44 virtual bool ReadAsync(const std::string &file_name, void *data, size_t byte_num, AsyncIOToken *token) = 0; 45 virtual bool WriteAsync(const std::string &file_name, const void *data, size_t byte_num, AsyncIOToken *token) = 0; 46 virtual bool Wait(AsyncIOToken token) = 0; 47 }; 48 49 class BACKEND_EXPORT IOHandle { 50 public: 51 IOHandle() = default; 52 ~IOHandle() = default; 53 bool DeleteSwapFile(const std::string &file_name) const; 54 bool CreateSwapFile(const std::string &file_name) const; 55 void LoadAio(const std::string &aio_shared_lib_name, const std::string &instance_func_name); 56 bool Read(const std::string &file_name, void *data, size_t byte_num) const; 57 bool Write(const std::string &file_name, const void *data, size_t byte_num) const; 58 bool ReadAsync(const std::string &file_name, void *data, size_t byte_num, AsyncIOToken *token) const; 59 bool WriteAsync(const std::string &file_name, const void *data, size_t byte_num, AsyncIOToken *token) const; 60 bool Wait(AsyncIOToken sync_token) const; 61 62 private: 63 bool IsAligned(const void *data, size_t byte_num) const; 64 AsyncIO *aio_{nullptr}; 65 }; 66 using IOHandlePtr = std::shared_ptr<IOHandle>; 67 } // namespace device 68 } // namespace mindspore 69 70 #endif // MINDSPORE_CORE_UTILS_SYSTEM_IO_HANDLE_H_ 71