deepspeed_aio_utils.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (c) Microsoft Corporation.
  2. // SPDX-License-Identifier: Apache-2.0
  3. // DeepSpeed Team
  4. /*
  5. Functionality for swapping optimizer tensors to/from (NVMe) storage devices.
  6. */
  7. #pragma once
  8. #include <assert.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <fcntl.h>
  12. #include <libaio.h>
  13. #include <sys/mman.h>
  14. #include <sys/stat.h>
  15. #include <sys/types.h>
  16. #include <unistd.h>
  17. #include <deepspeed_aio_types.h>
  18. #include <cstring>
  19. #include <fstream>
  20. #include <iostream>
  21. #include <memory>
  22. #include <string>
  23. #include <vector>
  24. struct io_xfer_ctxt {
  25. const int _fd;
  26. const long long int _base_offset;
  27. const void* _mem_buffer;
  28. const long long int _num_bytes;
  29. io_xfer_ctxt(const int fd,
  30. const long long int file_offset,
  31. const long long int num_bytes,
  32. const void* buffer);
  33. };
  34. struct io_prep_context {
  35. const bool _read_op;
  36. const std::unique_ptr<io_xfer_ctxt>& _xfer_ctxt;
  37. const size_t _block_size;
  38. const std::vector<struct iocb*>* _iocbs;
  39. io_prep_context(const bool read_op,
  40. const std::unique_ptr<io_xfer_ctxt>& xfer_ctxt,
  41. const size_t block_size,
  42. const std::vector<struct iocb*>* iocbs);
  43. void prep_iocbs(const int n_iocbs,
  44. const size_t num_bytes,
  45. const void* start_buffer,
  46. const long long int start_offset);
  47. };
  48. struct io_prep_generator {
  49. const bool _read_op;
  50. const std::unique_ptr<io_xfer_ctxt>& _xfer_ctxt;
  51. const size_t _block_size;
  52. long long int _remaining_bytes;
  53. long long int _num_io_blocks;
  54. long long int _remaining_io_blocks;
  55. long long int _next_iocb_index;
  56. io_prep_generator(const bool read_op,
  57. const std::unique_ptr<io_xfer_ctxt>& xfer_ctxt,
  58. const size_t block_size);
  59. int prep_iocbs(const int n_iocbs, std::vector<struct iocb*>* iocbs);
  60. };
  61. void* ds_page_aligned_alloc(const size_t size, const bool lock = false);
  62. int get_file_size(const char* filename, long long int& size);