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.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2020 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include <stdio.h>
  4. #include "cpu.h"
  5. #if defined __ANDROID__ || defined __linux__ || defined __APPLE__
  6. static int test_cpu_set()
  7. {
  8. ncnn::CpuSet set;
  9. if (set.num_enabled() != 0)
  10. {
  11. fprintf(stderr, "By default all cpus should be disabled\n");
  12. return 1;
  13. }
  14. set.enable(0);
  15. if (!set.is_enabled(0))
  16. {
  17. fprintf(stderr, "CpuSet enable doesn't work\n");
  18. return 1;
  19. }
  20. if (set.num_enabled() != 1)
  21. {
  22. fprintf(stderr, "Only one cpu should be enabled\n");
  23. return 1;
  24. }
  25. set.disable(0);
  26. if (set.is_enabled(0))
  27. {
  28. fprintf(stderr, "CpuSet disable doesn't work\n");
  29. return 1;
  30. }
  31. return 0;
  32. }
  33. #else
  34. static int test_cpu_set()
  35. {
  36. return 0;
  37. }
  38. #endif
  39. #if defined __ANDROID__ || defined __linux__
  40. static int test_cpu_info()
  41. {
  42. if (ncnn::get_cpu_count() >= 0 && ncnn::get_little_cpu_count() >= 0 && ncnn::get_big_cpu_count() >= 0)
  43. {
  44. return 0;
  45. }
  46. else
  47. {
  48. fprintf(stderr, "The system cannot have a negative number of processors\n");
  49. return 1;
  50. }
  51. }
  52. static int test_cpu_omp()
  53. {
  54. if (ncnn::get_omp_num_threads() >= 0 && ncnn::get_omp_thread_num() >= 0 && ncnn::get_omp_dynamic() >= 0)
  55. {
  56. return 0;
  57. }
  58. else
  59. {
  60. fprintf(stderr, "The OMP cannot have a negative number of processors\n");
  61. return 1;
  62. }
  63. ncnn::set_omp_num_threads(1);
  64. ncnn::set_omp_dynamic(1);
  65. }
  66. static int test_cpu_powersave()
  67. {
  68. if (ncnn::get_cpu_powersave() >= 0)
  69. {
  70. return 0;
  71. }
  72. else
  73. {
  74. fprintf(stderr, "By default powersave must be zero\n");
  75. return 1;
  76. }
  77. if (ncnn::set_cpu_powersave(-1) == -1 && ncnn::set_cpu_powersave(3) == -1)
  78. {
  79. return 0;
  80. }
  81. else
  82. {
  83. fprintf(stderr, "Set cpu powersave for `-1 < argument < 2` works incorrectly.\n");
  84. return 1;
  85. }
  86. }
  87. #else
  88. #if defined _WIN32
  89. // Check SDK >= WinXP
  90. #if _WIN32_WINNT >= _WIN32_WINNT_WINXP // winxp
  91. static int test_cpu_info()
  92. {
  93. int cpucount = ncnn::get_cpu_count();
  94. int bigcpucount = ncnn::get_big_cpu_count();
  95. int littlecpucount = ncnn::get_little_cpu_count();
  96. fprintf(stderr, "cpucount = %d\n", cpucount);
  97. fprintf(stderr, "bigcpucount = %d\n", bigcpucount);
  98. fprintf(stderr, "littlecpucount = %d\n", littlecpucount);
  99. if ((cpucount != bigcpucount + littlecpucount) || (bigcpucount > cpucount) || (littlecpucount > cpucount))
  100. {
  101. fprintf(stderr, "The number of big and little cpus must be less than or equal to the total number of cpus\n");
  102. return -1;
  103. }
  104. return 0;
  105. }
  106. #endif
  107. #else
  108. static int test_cpu_info()
  109. {
  110. return 0;
  111. }
  112. #endif
  113. static int test_cpu_omp()
  114. {
  115. return 0;
  116. }
  117. static int test_cpu_powersave()
  118. {
  119. return 0;
  120. }
  121. #endif
  122. int main()
  123. {
  124. return 0
  125. || test_cpu_set()
  126. || test_cpu_info()
  127. || test_cpu_omp()
  128. || test_cpu_powersave();
  129. }