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.

sub_cpu_kernel.cc 3.3 kB

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