Last active
March 3, 2017 15:48
-
-
Save tanakamura/875187a1d96a3f5f3ad87ed6a559dff7 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
#include <omp.h> | |
#include <stdlib.h> | |
#include <malloc.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <pthread.h> | |
#include <x86intrin.h> | |
#define LINE_SIZE 64 | |
struct thread_arg { | |
char *mem; | |
pthread_t t; | |
}; | |
int main(int argc, char **argv) | |
{ | |
int n = 2; | |
int self = 0; | |
if (argc >= 2) { | |
n = atoi(argv[1]); | |
} | |
if (argc >= 3) { | |
self = atoi(argv[2]); | |
} | |
omp_set_num_threads(n); | |
int niter = 1024; | |
char *mem = memalign(LINE_SIZE, LINE_SIZE*niter*n); | |
int *core_table = malloc(sizeof(int) * n); | |
#pragma omp parallel for | |
for (int i=0; i<n; i++) { | |
memset(mem+i*niter*LINE_SIZE, 0, niter*LINE_SIZE); | |
} | |
#pragma omp parallel for | |
for (int i=0; i<n; i++) { | |
unsigned int id; | |
__rdtscp(&id); | |
if (i==self) { | |
for (int j=0; j<n; j++) { | |
if (j == self) { | |
continue; | |
} | |
volatile char *ptr = (char*)(mem + j*LINE_SIZE*niter); | |
long long t0, t1; | |
long long min=9999, max=0; | |
for (int k=0; k<niter; k++) { | |
t0 = __rdtsc(); | |
ptr[k*2] = 1; | |
while (ptr[k*2+1] == 0) | |
; | |
t1 = __rdtsc(); | |
long long d = t1-t0; | |
if (d < min) { | |
min = d; | |
} | |
if (d > max) { | |
max = d; | |
} | |
} | |
printf("%4d(%4d-%4d): %4lld, %4lld\n", | |
j, id, core_table[j], min, max); | |
} | |
} else { | |
volatile char *ptr = (char*)(mem + i*LINE_SIZE*niter); | |
core_table[i] = id; | |
for (int k=0; k<niter; k++) { | |
while (ptr[k*2] == 0) | |
; | |
ptr[k*2+1] = 1; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment