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.

query_blocksize.cpp 1.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * \file dnn/src/cuda/query_blocksize.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 "./query_blocksize.cuh"
  12. #include "src/cuda/utils.h"
  13. #include <mutex>
  14. #include <unordered_map>
  15. using namespace megdnn;
  16. using namespace cuda;
  17. namespace {
  18. size_t hash_pair_combine(size_t x, size_t y) {
  19. return y + 0x9e3779b9 + (x << 6) + (x >> 2);
  20. }
  21. //! stupid committee has no pair hash. Let's do it for them
  22. struct pairhash {
  23. public:
  24. template <typename T, typename U>
  25. size_t operator()(const std::pair<T, U>& x) const {
  26. return hash_pair_combine(std::hash<T>{}(x.first),
  27. std::hash<U>{}(x.second));
  28. }
  29. };
  30. } // anonymous namespace
  31. LaunchConfig cuda::query_launch_config_for_kernel(const void* kern,
  32. const SmemGetter& smem) {
  33. static std::mutex mtx;
  34. static std::unordered_map<std::pair<int, const void*>, LaunchConfig,
  35. pairhash>
  36. cache;
  37. std::lock_guard<std::mutex> _lock{mtx};
  38. int device = -1;
  39. cuda_check(cudaGetDevice(&device));
  40. auto ins = cache.insert({{device, kern}, LaunchConfig{}});
  41. if (ins.second) {
  42. ins.first->second =
  43. detail::query_launch_config_for_kernel_uncached(kern, smem);
  44. }
  45. return ins.first->second;
  46. }
  47. // vim: ft=cpp syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

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