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.

bench.h 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #ifdef __CYGWIN32__
  5. #include <sys/time.h>
  6. #elif defined(__APPLE__)
  7. #include <mach/mach_time.h>
  8. #endif
  9. #include "common.h"
  10. #if defined(__WIN32__) || defined(__WIN64__)
  11. #ifndef DELTA_EPOCH_IN_MICROSECS
  12. #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
  13. #endif
  14. int gettimeofday(struct timeval *tv, void *tz){
  15. FILETIME ft;
  16. unsigned __int64 tmpres = 0;
  17. static int tzflag;
  18. if (NULL != tv)
  19. {
  20. GetSystemTimeAsFileTime(&ft);
  21. tmpres |= ft.dwHighDateTime;
  22. tmpres <<= 32;
  23. tmpres |= ft.dwLowDateTime;
  24. /*converting file time to unix epoch*/
  25. tmpres /= 10; /*convert into microseconds*/
  26. tmpres -= DELTA_EPOCH_IN_MICROSECS;
  27. tv->tv_sec = (long)(tmpres / 1000000UL);
  28. tv->tv_usec = (long)(tmpres % 1000000UL);
  29. }
  30. return 0;
  31. }
  32. #endif
  33. #if !defined(__WIN32__) && !defined(__WIN64__) && !defined(__CYGWIN32__) && 0
  34. static void *huge_malloc(BLASLONG size){
  35. int shmid;
  36. void *address;
  37. #ifndef SHM_HUGETLB
  38. #define SHM_HUGETLB 04000
  39. #endif
  40. if ((shmid =shmget(IPC_PRIVATE,
  41. (size + HUGE_PAGESIZE) & ~(HUGE_PAGESIZE - 1),
  42. SHM_HUGETLB | IPC_CREAT |0600)) < 0) {
  43. printf( "Memory allocation failed(shmget).\n");
  44. exit(1);
  45. }
  46. address = shmat(shmid, NULL, SHM_RND);
  47. if ((BLASLONG)address == -1){
  48. printf( "Memory allocation failed(shmat).\n");
  49. exit(1);
  50. }
  51. shmctl(shmid, IPC_RMID, 0);
  52. return address;
  53. }
  54. #define malloc huge_malloc
  55. #endif
  56. /* Benchmarks should allocate with cacheline (often 64 bytes) alignment
  57. to avoid unreliable results. This technique, storing the allocated
  58. pointer value just before the aligned memory, doesn't require
  59. C11's aligned_alloc for compatibility with older compilers. */
  60. static void *aligned_alloc_cacheline(size_t n)
  61. {
  62. void *p = malloc((size_t)(void *) + n + L1_DATA_LINESIZE - 1);
  63. if (p) {
  64. void **newp = (void **)
  65. (((uintptr_t)p + L1_DATA_LINESIZE) & (uintptr_t)-L1_DATA_LINESIZE);
  66. newp[-1] = p;
  67. p = newp;
  68. }
  69. return p;
  70. }
  71. #define malloc aligned_alloc_cacheline
  72. #define free(p) free((p) ? ((void **)(p))[-1] : (p))
  73. #if defined(__WIN32__) || defined(__WIN64__) || !defined(_POSIX_TIMERS)
  74. struct timeval start, stop;
  75. #elif defined(__APPLE__)
  76. mach_timebase_info_data_t info;
  77. uint64_t start = 0, stop = 0;
  78. #else
  79. struct timespec start = { 0, 0 }, stop = { 0, 0 };
  80. #endif
  81. double getsec()
  82. {
  83. #if defined(__WIN32__) || defined(__WIN64__) || !defined(_POSIX_TIMERS)
  84. return (double)(stop.tv_sec - start.tv_sec) + (double)((stop.tv_usec - start.tv_usec)) * 1.e-6;
  85. #elif defined(__APPLE__)
  86. mach_timebase_info(&info);
  87. return (double)(((stop - start) * info.numer)/info.denom) * 1.e-9;
  88. #else
  89. return (double)(stop.tv_sec - start.tv_sec) + (double)((stop.tv_nsec - start.tv_nsec)) * 1.e-9;
  90. #endif
  91. }
  92. void begin() {
  93. #if defined(__WIN32__) || defined(__WIN64__) || !defined(_POSIX_TIMERS)
  94. gettimeofday( &start, (struct timezone *)0);
  95. #elif defined(__APPLE__)
  96. start = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
  97. #else
  98. clock_gettime(CLOCK_REALTIME, &start);
  99. #endif
  100. }
  101. void end() {
  102. #if defined(__WIN32__) || defined(__WIN64__) || !defined(_POSIX_TIMERS)
  103. gettimeofday( &stop, (struct timezone *)0);
  104. #elif defined(__APPLE__)
  105. stop = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
  106. #else
  107. clock_gettime(CLOCK_REALTIME, &stop);
  108. #endif
  109. }