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 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #if defined(__WIN32__) || defined(__WIN64__) || !defined(_POSIX_TIMERS)
  57. struct timeval start, stop;
  58. #elif defined(__APPLE__)
  59. mach_timebase_info_data_t info;
  60. uint64_t start = 0, stop = 0;
  61. #else
  62. struct timespec start = { 0, 0 }, stop = { 0, 0 };
  63. #endif
  64. double getsec()
  65. {
  66. #if defined(__WIN32__) || defined(__WIN64__) || !defined(_POSIX_TIMERS)
  67. return (double)(stop.tv_sec - start.tv_sec) + (double)((stop.tv_usec - start.tv_usec)) * 1.e-6;
  68. #elif defined(__APPLE__)
  69. mach_timebase_info(&info);
  70. return (double)(((stop - start) * info.numer)/info.denom) * 1.e-9;
  71. #else
  72. return (double)(stop.tv_sec - start.tv_sec) + (double)((stop.tv_nsec - start.tv_nsec)) * 1.e-9;
  73. #endif
  74. }
  75. void begin() {
  76. #if defined(__WIN32__) || defined(__WIN64__) || !defined(_POSIX_TIMERS)
  77. gettimeofday( &start, (struct timezone *)0);
  78. #elif defined(__APPLE__)
  79. start = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
  80. #else
  81. clock_gettime(CLOCK_REALTIME, &start);
  82. #endif
  83. }
  84. void end() {
  85. #if defined(__WIN32__) || defined(__WIN64__) || !defined(_POSIX_TIMERS)
  86. gettimeofday( &stop, (struct timezone *)0);
  87. #elif defined(__APPLE__)
  88. stop = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
  89. #else
  90. clock_gettime(CLOCK_REALTIME, &stop);
  91. #endif
  92. }