LCOV - code coverage report
Current view: top level - nntrainer/tensor - swap_device.h (source / functions) Coverage Total Hit
Test: coverage_filtered.info Lines: 0.0 % 14 0
Test Date: 2025-12-14 20:38:17 Functions: 0.0 % 4 0

            Line data    Source code
       1              : // SPDX-License-Identifier: Apache-2.0
       2              : /**
       3              :  * Copyright (C) 2022 Jiho Chu <jiho.chu@samsung.com>
       4              :  *
       5              :  * @file   swap_device.cpp
       6              :  * @date   01 July 2022
       7              :  * @see    https://github.com/nnstreamer/nntrainer
       8              :  * @author Jiho Chu <jiho.chu@samsung.com>
       9              :  * @bug    No known bugs except for NYI items
      10              :  * @brief  Swap device class
      11              :  *
      12              :  */
      13              : 
      14              : #ifndef __SWAP_DEVICE_H__
      15              : #define __SWAP_DEVICE_H__
      16              : 
      17              : #include <common.h>
      18              : #include <fcntl.h>
      19              : #include <filesystem>
      20              : #include <map>
      21              : #include <memory>
      22              : #include <mutex>
      23              : #include <nntrainer_error.h>
      24              : #include <string>
      25              : #include <sys/stat.h>
      26              : #include <sys/types.h>
      27              : #include <system_error>
      28              : #include <utility>
      29              : #include <vector>
      30              : #if defined(_WIN32)
      31              : #include "utils/mman_windows.h"
      32              : #include <io.h>
      33              : #define O_SYNC 0UL
      34              : #else
      35              : #include <sys/mman.h>
      36              : #include <unistd.h>
      37              : #endif
      38              : 
      39              : #if defined(_WIN32)
      40              : using ssize_t = std::make_signed_t<size_t>;
      41              : #endif
      42              : 
      43              : namespace nntrainer {
      44              : /**
      45              :  * @class   SwapDevice
      46              :  * @brief   A device used to storing data with long access time
      47              :  */
      48              : class SwapDevice {
      49              : public:
      50              :   /**
      51              :    * @brief SwapDevice default constructor
      52              :    *
      53              :    */
      54            0 :   explicit SwapDevice(const std::string &name) :
      55            0 :     dev_path(std::filesystem::path(std::filesystem::current_path())
      56              :                .append(name)
      57              :                .string()),
      58            0 :     fd(-1),
      59            0 :     execution_mode(ml::train::ExecutionMode::TRAIN) {}
      60              : 
      61              :   /**
      62              :    * @brief SwapDevice default constructor
      63              :    *
      64              :    */
      65            0 :   explicit SwapDevice(const std::string &path, const std::string &name) :
      66            0 :     dev_path(std::filesystem::path(path).append(name).string()),
      67            0 :     fd(-1),
      68            0 :     execution_mode(ml::train::ExecutionMode::TRAIN) {}
      69              : 
      70              :   /**
      71              :    * @brief SwapDevice destructor
      72              :    *
      73              :    */
      74            0 :   virtual ~SwapDevice() = default;
      75              : 
      76              :   /**
      77              :    * @brief Start device
      78              :    *
      79              :    * @param size The size of requested swap device space
      80              :    * @param writeable Writeable flag
      81              :    *
      82              :    */
      83              :   void start(size_t size, ml::train::ExecutionMode _execution_mode =
      84              :                             ml::train::ExecutionMode::TRAIN);
      85              : 
      86              :   /**
      87              :    * @brief Allocate and get data
      88              :    *
      89              :    * @param offset Requested offset of swap device file
      90              :    * @param size Requested size.
      91              :    * @param memory_ptr memory ptr that allocate FSU data (mapping)
      92              :    * @param alloc_only only allocate buffer without reading data
      93              :    *
      94              :    * @return The pointer of the swap space
      95              :    *
      96              :    */
      97              :   void *getBuffer(off_t offset, size_t size, void *memory_ptr, unsigned int id,
      98              :                   bool alloc_only = false);
      99              : 
     100              :   /**
     101              :    * @brief Deallocate and put data
     102              :    *
     103              :    * @param ptr The pointer obtained from getBuffer
     104              :    * @param dealloc_only only deallocate buffer without writing data
     105              :    */
     106              :   void putBuffer(void *ptr, bool dealloc_only = false);
     107              : 
     108              :   /**
     109              :    * @brief Close device
     110              :    *
     111              :    */
     112              :   void finish();
     113              : 
     114              :   /**
     115              :    * @brief Check device is operating
     116              :    *
     117              :    * @return Device operating status
     118              :    *
     119              :    */
     120            0 :   bool isOperating() const { return (fd >= 0); }
     121              : 
     122              :   /**
     123              :    * @brief Get device path
     124              :    *
     125              :    * @return Device path
     126              :    *
     127              :    */
     128            0 :   const std::string getDevicePath() const { return dev_path; }
     129              : 
     130              :   /**
     131              :    * @brief set FSU weight path
     132              :    *
     133              :    * @param path FSU weight file path
     134              :    */
     135            0 :   void setFsuWeightPath(std::string file_path) { dev_path = file_path; }
     136              : 
     137              :   /**
     138              :    * @brief set weight file offset for FSU loading
     139              :    *
     140              :    * @param offsets weight file offset
     141              :    */
     142              :   void setWeightOffset(std::vector<std::pair<size_t, size_t>> offsets) {
     143            0 :     weight_offset = offsets;
     144            0 :   }
     145              : 
     146              : private:
     147              :   mutable std::mutex mutex;
     148              :   std::string dev_path; /**< device path */
     149              :   int fd;               /**< device file description */
     150              :   std::vector<std::pair<size_t, size_t>> weight_offset;
     151              :   ml::train::ExecutionMode execution_mode;
     152              : #ifdef USE_MMAP
     153              :   std::map<void *, std::tuple<void *, size_t, off_t, ssize_t>>
     154              :     mapped; /**< <pointer, <orig_pointer, size, offset, origianl size>> */
     155              : #else
     156              :   std::map<void *, std::pair<off_t, ssize_t>>
     157              :     allocated; /**< <pointer, <offset, size>> */
     158              : #endif
     159              : };
     160              : } // namespace nntrainer
     161              : 
     162              : #endif /** __SWAP_DEVICE_H__ */
        

Generated by: LCOV version 2.0-1