| 1 | #include "test.h" |
| 2 | #include <time.h> |
| 3 | |
| 4 | int bench_nthread; |
| 5 | int bench_niter; |
| 6 | int bench_mode; |
| 7 | |
| 8 | void bench(); // defined by user |
| 9 | void start_thread_group(int nth, void(*f)(int tid)); |
| 10 | |
| 11 | int main(int argc, char **argv) { |
| 12 | bench_nthread = 2; |
| 13 | if (argc > 1) |
| 14 | bench_nthread = atoi(nptr: argv[1]); |
| 15 | bench_niter = 100; |
| 16 | if (argc > 2) |
| 17 | bench_niter = atoi(nptr: argv[2]); |
| 18 | if (argc > 3) |
| 19 | bench_mode = atoi(nptr: argv[3]); |
| 20 | |
| 21 | timespec tp0; |
| 22 | clock_gettime(CLOCK_MONOTONIC, tp: &tp0); |
| 23 | bench(); |
| 24 | timespec tp1; |
| 25 | clock_gettime(CLOCK_MONOTONIC, tp: &tp1); |
| 26 | unsigned long long t = |
| 27 | (tp1.tv_sec * 1000000000ULL + tp1.tv_nsec) - |
| 28 | (tp0.tv_sec * 1000000000ULL + tp0.tv_nsec); |
| 29 | fprintf(stderr, format: "%llu ns/iter\n" , t / bench_niter); |
| 30 | fprintf(stderr, format: "DONE\n" ); |
| 31 | } |
| 32 | |
| 33 | void start_thread_group(int nth, void(*f)(int tid)) { |
| 34 | pthread_t *th = (pthread_t*)malloc(size: nth * sizeof(pthread_t)); |
| 35 | for (int i = 0; i < nth; i++) |
| 36 | pthread_create(newthread: &th[i], attr: 0, start_routine: (void*(*)(void*))f, arg: (void*)(long)i); |
| 37 | for (int i = 0; i < nth; i++) |
| 38 | pthread_join(th: th[i], thread_return: 0); |
| 39 | } |
| 40 | |