]>
git.saurik.com Git - apple/libdispatch.git/blob - testing/yet-another-apply-test.c
1 #include <dispatch/dispatch.h>
5 #include <mach/mach_time.h>
11 static uint64_t total
;
13 #define LAPS (256 * 1024 * 1024)
14 #define SIZE (LAPS * sizeof(int))
17 main(int argc
, char *argv
[])
19 dispatch_queue_t cq
= dispatch_get_concurrent_queue(0);
29 fprintf(stderr
, "usage: %s <file>\n", argv
[0]);
33 // make sure to have 2GB + of RAM installed and run this before hand:
34 // dd if=/dev/random bs=1024k count=1024 of=/tmp/testfile
35 fd
= open(argv
[1], O_RDONLY
);
40 assert(sb
.st_size
>= (off_t
)SIZE
);
42 nums
= mmap(NULL
, SIZE
, PROT_READ
, MAP_FILE
|MAP_PRIVATE
, fd
, 0);
43 assert(nums
!= MAP_FAILED
);
45 // force pages to be faulted in
46 for (i
= 0; i
< LAPS
; i
++) {
50 for (stride
= 1; stride
< (LAPS
+ 1); stride
<<= 1) {
52 s
= mach_absolute_time();
53 dispatch_apply(LAPS
/ stride
, cq
, ^(size_t idx
) {
54 const int *nums2
= nums
+ (idx
* stride
);
58 // assert(stride > 0);
60 ptotal
+= nums2
[idx2
++];
61 } while (idx2
< stride
);
63 __sync_fetch_and_add(&total
, ptotal
);
65 e
= mach_absolute_time();
69 printf("da%lu:\t%Lf ns\n", stride
, cycles
);
74 s
= mach_absolute_time();
75 for (i
= 0; i
< LAPS
; i
++) {
79 e
= mach_absolute_time();
83 printf("naïve:\t%Lf ns\n", cycles
);
88 s
= mach_absolute_time();
89 #pragma omp parallel for reduction(+:tmp_total)
90 for (i
= 0; i
< LAPS
; i
++) {
94 e
= mach_absolute_time();
98 printf("OpenMP:\t%Lf ns\n", cycles
);