The source code and dockerfile for the GSW2024 AI Lab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

97 lines
2.7 KiB

2 months ago
  1. #include <cstdio>
  2. #include <stdlib.h>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <sparsepp/spp_timer.h>
  6. #include <sparsepp/spp_memory.h>
  7. #include <sparsepp/spp.h>
  8. using spp::sparse_hash_map;
  9. using namespace std;
  10. class FileSerializer
  11. {
  12. public:
  13. // serialize basic types to FILE
  14. // -----------------------------
  15. template <class T>
  16. bool operator()(FILE *fp, const T& value)
  17. {
  18. return fwrite((const void *)&value, sizeof(value), 1, fp) == 1;
  19. }
  20. template <class T>
  21. bool operator()(FILE *fp, T* value)
  22. {
  23. return fread((void *)value, sizeof(*value), 1, fp) == 1;
  24. }
  25. // serialize std::string to FILE
  26. // -----------------------------
  27. bool operator()(FILE *fp, const string& value)
  28. {
  29. const size_t size = value.size();
  30. return (*this)(fp, size) && fwrite(value.c_str(), size, 1, fp) == 1;
  31. }
  32. bool operator()(FILE *fp, string* value)
  33. {
  34. size_t size;
  35. if (!(*this)(fp, &size))
  36. return false;
  37. char* buf = new char[size];
  38. if (fread(buf, size, 1, fp) != 1)
  39. {
  40. delete [] buf;
  41. return false;
  42. }
  43. new (value) string(buf, (size_t)size);
  44. delete[] buf;
  45. return true;
  46. }
  47. // serialize std::pair<const A, B> to FILE - needed for maps
  48. // ---------------------------------------------------------
  49. template <class A, class B>
  50. bool operator()(FILE *fp, const std::pair<const A, B>& value)
  51. {
  52. return (*this)(fp, value.first) && (*this)(fp, value.second);
  53. }
  54. template <class A, class B>
  55. bool operator()(FILE *fp, std::pair<const A, B> *value)
  56. {
  57. return (*this)(fp, (A *)&value->first) && (*this)(fp, &value->second);
  58. }
  59. };
  60. float _to_gb(uint64_t m) { return (float)((double)m / (1024 * 1024 * 1024)); }
  61. int main(int, char* [])
  62. {
  63. sparse_hash_map<string, int> age;
  64. for (size_t i=0; i<10000000; ++i)
  65. {
  66. char buff[20];
  67. sprintf(buff, "%zu", i);
  68. age.insert(std::make_pair(std::string(buff), i));
  69. }
  70. printf("before serialize(): mem_usage %4.1f GB\n", _to_gb(spp::GetProcessMemoryUsed()));
  71. // serialize age hash_map to "ages.dmp" file
  72. FILE *out = fopen("ages.dmp", "wb");
  73. age.serialize(FileSerializer(), out);
  74. fclose(out);
  75. printf("before clear(): mem_usage %4.1f GB\n", _to_gb(spp::GetProcessMemoryUsed()));
  76. age.clear();
  77. printf("after clear(): mem_usage %4.1f GB\n", _to_gb(spp::GetProcessMemoryUsed()));
  78. // read from "ages.dmp" file into age_read hash_map
  79. FILE *input = fopen("ages.dmp", "rb");
  80. age.unserialize(FileSerializer(), input);
  81. fclose(input);
  82. printf("after unserialize(): mem_usage %4.1f GB\n", _to_gb(spp::GetProcessMemoryUsed()));
  83. }