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.

algo_base.cpp 2.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * \file dnn/src/common/algo_base.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
  10. * implied.
  11. */
  12. #include "src/common/algo_base.h"
  13. #include "src/common/utils.h"
  14. using namespace megdnn;
  15. #define FOREACH_ALGO_ATTRIBUTE(cb) \
  16. cb(DEFAULT) \
  17. cb(REPRODUCIBLE) \
  18. cb(NAIVE) \
  19. cb(USABLE_DEPEND_ON_SHAPE)
  20. namespace {
  21. inline const char* attr_str(const AlgoAttribute& attr) {
  22. #define cb(attr) \
  23. case AlgoAttribute::attr: \
  24. return #attr;
  25. switch (attr) { FOREACH_ALGO_ATTRIBUTE(cb) }
  26. #undef cb
  27. return "UNKNOWN";
  28. }
  29. } // namespace
  30. std::string Algorithm::attribute_str(const Attribute& attr) {
  31. std::string ret;
  32. uint32_t attr_val = static_cast<uint32_t>(attr);
  33. while(attr_val) {
  34. uint32_t mask = ~(attr_val & (attr_val - 1));
  35. Attribute sub_attr = static_cast<Attribute>(mask & attr_val);
  36. if (!ret.empty()) {
  37. ret.append(" | ");
  38. }
  39. ret.append(attr_str(sub_attr));
  40. attr_val = attr_val & (attr_val - 1);
  41. }
  42. if (ret.empty()) {
  43. ret = "DEFAULT";
  44. }
  45. return ret;
  46. }
  47. bool Algorithm::contain_attribute_all(const Attribute& attr) const {
  48. return attr == static_cast<Attribute>(attribute() & attr);
  49. }
  50. bool Algorithm::contain_attribute_any(const Attribute& attr) const {
  51. return static_cast<bool>(attribute() & attr);
  52. }
  53. void Algorithm::check_attribute(const Attribute& positive_attr,
  54. const Attribute& negative_attr) const {
  55. megdnn_assert(contain_attribute_all(positive_attr) &&
  56. !contain_attribute_any(negative_attr),
  57. "require algorithm with attribute(%s) and without "
  58. "attribute(%s), but get"
  59. "algorithm(%s) with attribute(%s) ",
  60. Algorithm::attribute_str(positive_attr).c_str(),
  61. Algorithm::attribute_str(negative_attr).c_str(), name(),
  62. Algorithm::attribute_str(attribute()).c_str());
  63. }
  64. // vim: syntax=cpp.doxygen

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