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.

index.cpp 2.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * \file dnn/test/common/index.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 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/index.h"
  12. #include "test/common/utils.h"
  13. namespace megdnn {
  14. namespace test {
  15. Index::Index(TensorLayout layout, size_t linear):
  16. m_layout(layout),
  17. m_linear(linear)
  18. {
  19. linear_to_array();
  20. array_to_offset();
  21. }
  22. Index::Index(TensorLayout layout, TensorShape array):
  23. m_layout(layout),
  24. m_array(array)
  25. {
  26. array_to_linear();
  27. array_to_offset();
  28. }
  29. void Index::linear_to_array()
  30. {
  31. auto linear = m_linear;
  32. auto &array = m_array;
  33. array.ndim = m_layout.ndim;
  34. for (size_t j = m_layout.ndim; j > 0; --j) {
  35. size_t i = j-1;
  36. array[i] = linear % m_layout[i];
  37. linear /= m_layout[i];
  38. }
  39. megdnn_assert(linear == 0);
  40. }
  41. void Index::array_to_linear()
  42. {
  43. auto &linear = m_linear;
  44. megdnn_assert(m_array.ndim == m_layout.ndim);
  45. linear = 0;
  46. for (size_t i = 0; i < m_array.ndim; ++i) {
  47. megdnn_assert(m_array[i] < m_layout[i]);
  48. linear = linear * m_layout[i] + m_array[i];
  49. }
  50. }
  51. void Index::array_to_offset()
  52. {
  53. auto &offset = m_offset;
  54. megdnn_assert(m_array.ndim == m_layout.ndim);
  55. offset = 0;
  56. for (size_t i = 0; i < m_array.ndim; ++i) {
  57. megdnn_assert(m_array[i] < m_layout[i]);
  58. offset += m_array[i] * m_layout.stride[i];
  59. }
  60. }
  61. std::string Index::to_string() const
  62. {
  63. std::string res = "";
  64. res.append("{");
  65. res.append("array=");
  66. res.append(m_array.to_string());
  67. res.append(",linear=");
  68. res.append(std::to_string(m_linear));
  69. res.append(",offset=");
  70. res.append(std::to_string(m_offset));
  71. res.append("}");
  72. return res;
  73. }
  74. } // namespace test
  75. } // namespace megdnn
  76. // vim: syntax=cpp.doxygen

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台

Contributors (1)