You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

workspace_pool.cc 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Copyright 2019 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. #include "src/runtime/workspace_pool.h"
  17. #include <malloc.h>
  18. #include <algorithm>
  19. #include "common/mslog.h"
  20. namespace mindspore {
  21. namespace predict {
  22. static constexpr size_t kWorkspacePageSize = 4096;
  23. static constexpr int kTempAllocaAlignment = 64;
  24. WorkspacePool *WorkspacePool::GetInstance() {
  25. static WorkspacePool instance;
  26. return &instance;
  27. }
  28. void *WorkspacePool::AllocWorkSpaceMem(size_t size) {
  29. size_t nbytes = (size + (kWorkspacePageSize - 1)) / kWorkspacePageSize * kWorkspacePageSize;
  30. if (nbytes == 0) {
  31. nbytes = kWorkspacePageSize;
  32. }
  33. std::pair<size_t, void *> alloc;
  34. // fist alloc
  35. if (freeList.empty()) {
  36. alloc.first = nbytes;
  37. alloc.second = memalign(kTempAllocaAlignment, nbytes);
  38. } else if (freeList.size() == 1) { // one element
  39. alloc = *(freeList.begin());
  40. freeList.erase(freeList.begin());
  41. if (alloc.first < nbytes) {
  42. free(alloc.second);
  43. alloc.first = nbytes;
  44. alloc.second = memalign(kTempAllocaAlignment, nbytes);
  45. }
  46. } else {
  47. if ((*(freeList.begin())).first >= nbytes) {
  48. auto iter = freeList.begin();
  49. for (; iter != freeList.end(); ++iter) {
  50. if ((*iter).first < size) {
  51. alloc = *(--iter);
  52. freeList.erase(iter);
  53. break;
  54. }
  55. }
  56. if (iter == freeList.end()) {
  57. alloc = *(freeList.rbegin());
  58. freeList.erase(--freeList.end());
  59. }
  60. } else {
  61. alloc = *(freeList.begin());
  62. freeList.erase(freeList.begin());
  63. free(alloc.second);
  64. alloc.first = nbytes;
  65. alloc.second = memalign(kTempAllocaAlignment, nbytes);
  66. }
  67. }
  68. allocList.emplace_back(alloc);
  69. return alloc.second;
  70. }
  71. void WorkspacePool::FreeWorkSpaceMem(void *ptr) {
  72. if (ptr == nullptr) {
  73. return;
  74. }
  75. std::pair<size_t, void *> alloc;
  76. if (allocList.empty()) {
  77. MS_LOGE("no mem have been alloc");
  78. return;
  79. } else if (allocList.back().second == ptr) {
  80. alloc = allocList.back();
  81. allocList.pop_back();
  82. } else {
  83. auto iter = allocList.begin();
  84. for (; iter != allocList.end(); ++iter) {
  85. if ((*iter).second == ptr) {
  86. alloc = *iter;
  87. allocList.erase(iter);
  88. break;
  89. }
  90. }
  91. if (iter == allocList.end()) {
  92. MS_LOGE("no value ptr have been alloc");
  93. return;
  94. }
  95. }
  96. freeList.insert(alloc);
  97. }
  98. WorkspacePool::~WorkspacePool() {
  99. for (auto &a : allocList) {
  100. free(a.second);
  101. }
  102. allocList.clear();
  103. for (auto &f : freeList) {
  104. free(f.second);
  105. }
  106. freeList.clear();
  107. }
  108. } // namespace predict
  109. } // namespace mindspore