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_wrapper.cpp 1.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * \file dnn/test/common/workspace_wrapper.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "test/common/workspace_wrapper.h"
  12. #include "test/common/utils.h"
  13. namespace megdnn {
  14. namespace test {
  15. WorkspaceWrapper::WorkspaceWrapper() : WorkspaceWrapper(nullptr, 0) {}
  16. WorkspaceWrapper::WorkspaceWrapper(Handle* handle, size_t size_in_bytes)
  17. : m_handle(handle) {
  18. m_workspace.size = size_in_bytes;
  19. if (m_workspace.size > 0) {
  20. m_workspace.raw_ptr =
  21. static_cast<dt_byte*>(megdnn_malloc(handle, size_in_bytes));
  22. } else {
  23. m_workspace.raw_ptr = nullptr;
  24. }
  25. }
  26. void WorkspaceWrapper::update(size_t size_in_bytes) {
  27. megdnn_assert(this->valid());
  28. if (size_in_bytes > m_workspace.size) {
  29. // free workspace
  30. if (m_workspace.size > 0) {
  31. megdnn_free(m_handle, m_workspace.raw_ptr);
  32. m_workspace.raw_ptr = nullptr;
  33. }
  34. // alloc new workspace
  35. m_workspace.size = size_in_bytes;
  36. if (m_workspace.size > 0) {
  37. m_workspace.raw_ptr =
  38. static_cast<dt_byte*>(megdnn_malloc(m_handle, size_in_bytes));
  39. } else {
  40. m_workspace.raw_ptr = nullptr;
  41. }
  42. }
  43. }
  44. WorkspaceWrapper::~WorkspaceWrapper() {
  45. if (m_workspace.size > 0) {
  46. megdnn_free(m_handle, m_workspace.raw_ptr);
  47. m_workspace.raw_ptr = nullptr;
  48. }
  49. }
  50. } // namespace test
  51. } // namespace megdnn
  52. // vim: syntax=cpp.doxygen