-
-
Save jnordwick/b30b1584fd7c49d68a6bb842abf7d98b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compile as: | |
// gcc -DCLSIZE=64 -O2 -c cl.c -o cl | |
// replace 64 with whatever value you want to pad to. | |
// this bash loop can be helpful: | |
// for x in 8 16 32 64 128 256; do gcc -DCLSIZE=${x} -O2 cl.c -o cl; ./cl; done | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <threads.h> | |
#include <stdatomic.h> | |
#include <time.h> | |
// number of threads to spawn | |
#define NTT 4 | |
// type to increment | |
#define atomic_type atomic_llong | |
//----------------------------------------- | |
#define PADSIZE (CLSIZE - sizeof(atomic_type)) | |
struct cell { | |
atomic_type x; | |
char pad[PADSIZE]; | |
}; | |
struct cell arr[NTT]; | |
uint64_t res[NTT]; | |
thrd_t tt[NTT]; | |
int func(void *u) { | |
uint64_t uu = (uint64_t)u; | |
for(int i = 0; i < 100000000; ++i) { | |
arr[uu].x += 1; | |
} | |
res[uu] = arr[uu].x; | |
return 0; | |
} | |
int main () { | |
struct timespec ta, tb; | |
uint64_t na, nb, nd; | |
printf("pad to %3u: ", CLSIZE); | |
clock_gettime(CLOCK_MONOTONIC, &ta); | |
for(uint64_t i = 0; i < NTT; ++i) { | |
thrd_create(tt + i, func, (void*)i); | |
} | |
for(int i = 0; i < NTT; ++i) { | |
thrd_join(tt[i], NULL); | |
} | |
clock_gettime(CLOCK_MONOTONIC, &tb); | |
na = ta.tv_sec * 1000000000 + ta.tv_nsec; | |
nb = tb.tv_sec * 1000000000 + tb.tv_nsec; | |
nd = nb - na; | |
printf("%llu ns\n", nd); | |
// for(int i = 0; i < NTT; ++i) { | |
// printf("#%d: %llu\n", i, res[i]); | |
// } | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment