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.

subscalar_cpu_kernel.cc 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 <thread>
  17. #include "kernel/cpu/subscalar_cpu_kernel.h"
  18. #include "device/cpu/cpu_device_address.h"
  19. namespace mindspore {
  20. namespace kernel {
  21. void SubscalarCPUKernel::InitKernel(const CNodePtr &kernel_node) {
  22. offset_ = AnfAlgo::GetNodeAttr<int>(kernel_node, "input_y");
  23. MS_LOG(DEBUG) << "offset: " << offset_;
  24. }
  25. void sub_task(int *in_addr, int *out_addr, size_t lens, int offset) {
  26. for (size_t i = 0; i < lens; i++) {
  27. out_addr[i] = in_addr[i] - offset;
  28. }
  29. }
  30. bool SubscalarCPUKernel::Launch(const std::vector<kernel::AddressPtr> &inputs,
  31. const std::vector<kernel::AddressPtr> & /*workspace*/,
  32. const std::vector<kernel::AddressPtr> &outputs) {
  33. #if defined(_WIN32) || defined(_WIN64)
  34. auto start_time = std::chrono::steady_clock::now();
  35. #else
  36. struct timeval start_time, end_time;
  37. (void)gettimeofday(&start_time, nullptr);
  38. #endif
  39. auto input_addr = reinterpret_cast<int *>(inputs[0]->addr);
  40. auto output_addr = reinterpret_cast<int *>(outputs[0]->addr);
  41. auto lens = inputs[0]->size / sizeof(int);
  42. if (lens < 10000) {
  43. for (size_t i = 0; i < lens; i++) {
  44. output_addr[i] = input_addr[i] - offset_;
  45. }
  46. } else {
  47. size_t thread_num = 4;
  48. std::thread threads[4];
  49. size_t process_lens = (lens + thread_num - 1) / thread_num;
  50. size_t process_offset = 0;
  51. for (size_t i = 0; i < thread_num; i++) {
  52. threads[i] =
  53. std::thread(sub_task, input_addr + process_offset, output_addr + process_offset, process_lens, offset_);
  54. if (process_offset + process_lens > lens) {
  55. process_lens = lens - process_offset;
  56. process_offset = lens;
  57. } else {
  58. process_offset += process_lens;
  59. }
  60. }
  61. for (size_t i = 0; i < thread_num; i++) {
  62. threads[i].join();
  63. }
  64. }
  65. #if defined(_WIN32) || defined(_WIN64)
  66. auto end_time = std::chrono::steady_clock::now();
  67. std::chrono::duration<double, std::ratio<1, 1000000>> cost = end_time - start_time;
  68. MS_LOG(INFO) << "SubscaleCPUKernel, used time: " << cost.count() << " us";
  69. #else
  70. (void)gettimeofday(&end_time, nullptr);
  71. uint64_t time = 1000000 * static_cast<uint64_t>(end_time.tv_sec - start_time.tv_sec);
  72. time += static_cast<uint64_t>(end_time.tv_usec - start_time.tv_usec);
  73. MS_LOG(INFO) << "SubscalarCPUKernel, used time: " << time << " us";
  74. #endif
  75. return true;
  76. }
  77. } // namespace kernel
  78. } // namespace mindspore