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.

test_cpu.cpp 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include <stdio.h>
  15. #include "cpu.h"
  16. #if defined __ANDROID__ || defined __linux__ || defined __APPLE__
  17. static int test_cpu_set()
  18. {
  19. ncnn::CpuSet set;
  20. if (set.num_enabled() != 0)
  21. {
  22. fprintf(stderr, "By default all cpus should be disabled\n");
  23. return 1;
  24. }
  25. set.enable(0);
  26. if (!set.is_enabled(0))
  27. {
  28. fprintf(stderr, "CpuSet enable doesn't work\n");
  29. return 1;
  30. }
  31. if (set.num_enabled() != 1)
  32. {
  33. fprintf(stderr, "Only one cpu should be enabled\n");
  34. return 1;
  35. }
  36. set.disable(0);
  37. if (set.is_enabled(0))
  38. {
  39. fprintf(stderr, "CpuSet disable doesn't work\n");
  40. return 1;
  41. }
  42. return 0;
  43. }
  44. #else
  45. static int test_cpu_set()
  46. {
  47. return 0;
  48. }
  49. #endif
  50. #if defined __ANDROID__ || defined __linux__
  51. static int test_cpu_info()
  52. {
  53. if (ncnn::get_cpu_count() >= 0 && ncnn::get_little_cpu_count() >= 0 && ncnn::get_big_cpu_count() >= 0)
  54. {
  55. return 0;
  56. }
  57. else
  58. {
  59. fprintf(stderr, "The system cannot have a negative number of processors\n");
  60. return 1;
  61. }
  62. }
  63. static int test_cpu_omp()
  64. {
  65. if (ncnn::get_omp_num_threads() >= 0 && ncnn::get_omp_thread_num() >= 0 && ncnn::get_omp_dynamic() >= 0)
  66. {
  67. return 0;
  68. }
  69. else
  70. {
  71. fprintf(stderr, "The OMP cannot have a negative number of processors\n");
  72. return 1;
  73. }
  74. ncnn::set_omp_num_threads(1);
  75. ncnn::set_omp_dynamic(1);
  76. }
  77. static int test_cpu_powersave()
  78. {
  79. if (ncnn::get_cpu_powersave() >= 0)
  80. {
  81. return 0;
  82. }
  83. else
  84. {
  85. fprintf(stderr, "By default powersave must be zero\n");
  86. return 1;
  87. }
  88. if (ncnn::set_cpu_powersave(-1) == -1 && ncnn::set_cpu_powersave(3) == -1)
  89. {
  90. return 0;
  91. }
  92. else
  93. {
  94. fprintf(stderr, "Set cpu powersave for `-1 < argument < 2` works incorrectly.\n");
  95. return 1;
  96. }
  97. }
  98. #else
  99. #if defined _WIN32
  100. // Check SDK >= Win7
  101. #if _WIN32_WINNT >= _WIN32_WINNT_WIN7 // win7
  102. static int test_cpu_info()
  103. {
  104. int cpucount = ncnn::get_cpu_count();
  105. int bigcpucount = ncnn::get_big_cpu_count();
  106. int littlecpucount = ncnn::get_little_cpu_count();
  107. fprintf(stderr, "cpucount = %d\n", cpucount);
  108. fprintf(stderr, "bigcpucount = %d\n", bigcpucount);
  109. fprintf(stderr, "littlecpucount = %d\n", littlecpucount);
  110. if ((cpucount != bigcpucount + littlecpucount) || (bigcpucount > cpucount) || (littlecpucount > cpucount))
  111. {
  112. fprintf(stderr, "The number of big and little cpus must be less than or equal to the total number of cpus\n");
  113. return -1;
  114. }
  115. return 0;
  116. }
  117. #endif
  118. #else
  119. static int test_cpu_info()
  120. {
  121. return 0;
  122. }
  123. #endif
  124. static int test_cpu_omp()
  125. {
  126. return 0;
  127. }
  128. static int test_cpu_powersave()
  129. {
  130. return 0;
  131. }
  132. #endif
  133. int main()
  134. {
  135. return 0
  136. || test_cpu_set()
  137. || test_cpu_info()
  138. || test_cpu_omp()
  139. || test_cpu_powersave();
  140. }