-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFalse-sharing.cpp
125 lines (106 loc) · 2.59 KB
/
False-sharing.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stdio.h>
#include <pthread.h>
#include <vector>
#include <stdarg.h>
#include <string.h>
//#include <sys/mman.h>
//#include <assert.h>
#include <stdint.h>
int result[10];
static inline uint64_t rdtsc()
{
unsigned long a, d;
asm volatile("cpuid;rdtsc"
: "=a"(a), "=d"(d)
:
: "ebx", "ecx");
return a | ((uint64_t)d << 32);
}
static inline uint64_t rdtsc2() {
uint64_t a, d;
asm volatile("mfence");
#if defined(USE_RDTSCP)
asm volatile("rdtscp" : "=a"(a), "=d"(d) :: "rcx");
#else
asm volatile("rdtsc" : "=a"(a), "=d"(d));
#endif
a = (d << 32) | a;
asm volatile("mfence");
return a;
}
inline void clflush(void *p)
{
asm volatile("clflush (%0)" ::"r"(p));
}
void *worker(void *arg)
{
(void)arg;
/// for(;;){
uint64_t start, end;
start = rdtsc2();
result[2]++;
int b = result[3];
end = rdtsc2();
printf("%ld ticks\n", end - start);
//}
// usleep(100000);
return NULL;
}
void *worker33(void * arg){
uint64_t a;
}
class pool
{
int p, q;
std::vector<pthread_t> thread;
std::vector<cpu_set_t> cpuset;
public:
pool(int num_threads...)
{
thread.reserve(num_threads);
cpuset.reserve(num_threads);
q = num_threads;
va_list thread_pool_core;
va_start(thread_pool_core, num_threads);
//assert(CPU_SETSIZE >= num_threads);
for (int i = 0; i < num_threads; ++i)
{
pthread_create(&thread[i], NULL, worker, NULL);
//Set cpu affinity mark to all-zero
CPU_ZERO(&cpuset[i]);
//affine thread to cpu#q
int core = va_arg(thread_pool_core, int);
CPU_SET(core, &cpuset[i]);
}
}
~pool()
{
this->p = 0;
this->q = 0;
}
void run()
{
//Get thread's cpu#
//Apply the settings
for (int i = 0; i < q; ++i)
{
p = pthread_setaffinity_np(thread[i], sizeof(cpu_set_t), &cpuset[i]);
// p = pthread_getaffinity_np(thread[i], sizeof(cpu_set_t), &cpuset[i]);
//for (int j = 0; j < CPU_SETSIZE; j++)
// if (CPU_ISSET(j, &cpuset[i]))
// printf(" CPU %d\n", j);
// clflush((char *)worker);
// clflush((char *)worker + 64);
// clflush((char *)worker + 128);
// clflush((char *)worker + 192);
// clflush((char *)worker + 256);
pthread_join(thread[i], NULL);
}
}
};
int main()
{
memset(result, 1, sizeof(result));
pool p(2, 1, 3,4);
p.run();
}