forked from lightsighter/Weft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrace.cc
398 lines (375 loc) · 12.4 KB
/
race.cc
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright 2015 Stanford University and NVIDIA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "weft.h"
#include "race.h"
#include "graph.h"
#include "program.h"
#include "instruction.h"
Happens::Happens(int total_threads)
: initialized(false)
{
happens_before.resize(total_threads, -1);
happens_after.resize(total_threads, -1);
}
void Happens::update_barriers_before(const std::vector<WeftBarrier*> &before)
{
assert(latest_before.empty());
latest_before = before;
}
void Happens::update_barriers_after(const std::vector<WeftBarrier*> &after)
{
assert(earliest_after.empty());
earliest_after = after;
}
void Happens::update_happens_relationships(void)
{
for (std::vector<WeftBarrier*>::const_iterator it =
latest_before.begin(); it != latest_before.end(); it++)
{
if ((*it) == NULL)
continue;
(*it)->get_instance()->update_latest_before(happens_after);
}
for (std::vector<WeftBarrier*>::const_iterator it =
earliest_after.begin(); it != earliest_after.end(); it++)
{
if ((*it) == NULL)
continue;
(*it)->get_instance()->update_earliest_after(happens_before);
}
}
bool Happens::has_happens(int thread, int line_number)
{
if (happens_before[thread] <= line_number)
return true;
if (happens_after[thread] >= line_number)
return true;
return false;
}
Address::Address(const int addr, SharedMemory *mem)
: address(addr), memory(mem), total_races(0)
{
PTHREAD_SAFE_CALL( pthread_mutex_init(&address_lock,NULL) );
}
Address::~Address(void)
{
PTHREAD_SAFE_CALL( pthread_mutex_destroy(&address_lock) );
}
void Address::add_access(WeftAccess *access)
{
PTHREAD_SAFE_CALL( pthread_mutex_lock(&address_lock) );
accesses.push_back(access);
PTHREAD_SAFE_CALL( pthread_mutex_unlock(&address_lock) );
}
void Address::perform_race_tests(void)
{
if (memory->program->assume_warp_synchronous())
{
for (unsigned idx1 = 0; idx1 < accesses.size(); idx1++)
{
WeftAccess *first = accesses[idx1];
if (first->is_read())
{
for (unsigned idx2 = idx1+1; idx2 < accesses.size(); idx2++)
{
WeftAccess *second = accesses[idx2];
// Check for both reads
if (second->is_read())
continue;
// Check for warp-synchronous
if (first->is_warp_synchronous(second))
continue;
if (!first->has_happens_relationship(second))
record_race(first, second);
}
}
else
{
for (unsigned idx2 = idx1+1; idx2 < accesses.size(); idx2++)
{
WeftAccess *second = accesses[idx2];
// Check for warp-synchronous
if (first->is_warp_synchronous(second))
continue;
if (!first->has_happens_relationship(second))
record_race(first, second);
}
}
}
}
else
{
// For every pair of addresses, check to see if we can
// establish a happens before or a happens after relationship
for (unsigned idx1 = 0; idx1 < accesses.size(); idx1++)
{
WeftAccess *first = accesses[idx1];
if (first->is_read())
{
for (unsigned idx2 = idx1+1; idx2 < accesses.size(); idx2++)
{
WeftAccess *second = accesses[idx2];
// Check for both reads
if (second->is_read())
continue;
if (!first->has_happens_relationship(second))
record_race(first, second);
}
}
else
{
for (unsigned idx2 = idx1+1; idx2 < accesses.size(); idx2++)
{
WeftAccess *second = accesses[idx2];
if (!first->has_happens_relationship(second))
record_race(first, second);
}
}
}
}
}
void Address::record_race(WeftAccess *one, WeftAccess *two)
{
// Alternative race reporting
//printf("Race between threads %d and %d on instructions "
// "%d and %d (PTX %d and %d)\n",
// one->thread->thread_id, two->thread->thread_id,
// one->thread_line_number, two->thread_line_number,
// one->instruction->line_number, two->instruction->line_number);
total_races++;
// Save the races based on the PTX instructions
int ptx_one = one->instruction->line_number;
int ptx_two = two->instruction->line_number;
if (ptx_one <= ptx_two)
{
std::pair<PTXInstruction*,PTXInstruction*>
key(one->instruction, two->instruction);
if (one->thread->thread_id <= two->thread->thread_id)
ptx_races[key].insert(
std::pair<Thread*,Thread*>(one->thread, two->thread));
else
ptx_races[key].insert(
std::pair<Thread*,Thread*>(two->thread, one->thread));
}
else
{
std::pair<PTXInstruction*,PTXInstruction*>
key(two->instruction, one->instruction);
if (one->thread->thread_id <= two->thread->thread_id)
ptx_races[key].insert(
std::pair<Thread*,Thread*>(one->thread, two->thread));
else
ptx_races[key].insert(
std::pair<Thread*,Thread*>(two->thread, one->thread));
}
}
int Address::report_races(std::map<
std::pair<PTXInstruction*,PTXInstruction*>,size_t> &all_races)
{
if (total_races > 0)
{
if (memory->weft->print_detail())
{
fprintf(stderr,"WEFT INFO: Found %d races on address %d!\n",
total_races, address);
for (std::map<std::pair<PTXInstruction*,PTXInstruction*>,std::set<
std::pair<Thread*,Thread*> > >::const_iterator it =
ptx_races.begin(); it != ptx_races.end(); it++)
{
PTXInstruction *one = it->first.first;
PTXInstruction *two = it->first.second;
if (one->source_file != NULL)
{
assert(two->source_file != NULL);
if (one == two)
fprintf(stderr,"\tThere are %ld races between different threads "
"on line %d of %s with address %d\n", it->second.size(),
one->source_line_number, one->source_file, address);
else
fprintf(stderr,"\tThere are %ld races between line %d of %s "
" and line %d of %s with address %d\n", it->second.size(),
one->source_line_number, one->source_file,
two->source_line_number, two->source_file, address);
}
else
{
assert(two->source_file == NULL);
if (one == two)
fprintf(stderr,"\tThere are %ld races between different threads "
"on PTX line %d with address %d\n", it->second.size(),
one->line_number, address);
else
fprintf(stderr,"\tThere are %ld races between PTX line %d "
" and PTX line %d with address %d\n", it->second.size(),
one->line_number, two->line_number, address);
}
const std::set<std::pair<Thread*,Thread*> > &threads = it->second;
for (std::set<std::pair<Thread*,Thread*> >::const_iterator
thread_it = threads.begin();
thread_it != threads.end(); thread_it++)
{
Thread *first = thread_it->first;
Thread *second = thread_it->second;
fprintf(stderr,"\t\t... between thread (%d,%d,%d) and (%d,%d,%d)\n",
first->tid_x, first->tid_y, first->tid_z,
second->tid_x, second->tid_y, second->tid_z);
}
}
}
else
{
for (std::map<std::pair<PTXInstruction*,PTXInstruction*>,
std::set<std::pair<Thread*,Thread*> > >::const_iterator
it = ptx_races.begin(); it != ptx_races.end(); it++)
{
std::map<std::pair<PTXInstruction*,PTXInstruction*>,size_t>::iterator
finder = all_races.find(it->first);
if (finder == all_races.end())
all_races[it->first] = it->second.size();
else
finder->second += it->second.size();
}
}
}
return total_races;
}
size_t Address::count_race_tests(void)
{
size_t num_accesses = accesses.size();
// OLA's equality
// 1 + 2 + 3 + ... + n-1 = (n-1)*n/2
return ((num_accesses * (num_accesses-1))/2);
}
SharedMemory::SharedMemory(Weft *w, Program *p)
: weft(w), program(p)
{
PTHREAD_SAFE_CALL( pthread_mutex_init(&memory_lock,NULL) );
}
SharedMemory::~SharedMemory(void)
{
for (std::map<int,Address*>::iterator it = addresses.begin();
it != addresses.end(); it++)
{
delete it->second;
}
addresses.clear();
PTHREAD_SAFE_CALL( pthread_mutex_destroy(&memory_lock) );
}
void SharedMemory::update_accesses(WeftAccess *access)
{
Address *address;
// These lookups need to be thread safe
PTHREAD_SAFE_CALL( pthread_mutex_lock(&memory_lock) );
std::map<int,Address*>::const_iterator finder =
addresses.find(access->address);
if (finder == addresses.end())
{
address = new Address(access->address, this);
addresses[access->address] = address;
}
else
address = finder->second;
PTHREAD_SAFE_CALL( pthread_mutex_unlock(&memory_lock) );
address->add_access(access);
}
int SharedMemory::count_addresses(void) const
{
return addresses.size();
}
void SharedMemory::enqueue_race_checks(void)
{
for (std::map<int,Address*>::const_iterator it = addresses.begin();
it != addresses.end(); it++)
{
weft->enqueue_task(new RaceCheckTask(it->second));
}
}
void SharedMemory::check_for_races(void)
{
int total_races = 0;
std::map<std::pair<PTXInstruction*,PTXInstruction*>,size_t> all_races;
for (std::map<int,Address*>::const_iterator it =
addresses.begin(); it != addresses.end(); it++)
{
total_races += it->second->report_races(all_races);
}
if (total_races > 0)
{
if (!weft->print_detail())
{
for (std::map<std::pair<PTXInstruction*,PTXInstruction*>,size_t>::const_iterator
it = all_races.begin(); it != all_races.end(); it++)
{
PTXInstruction *one = it->first.first;
PTXInstruction *two = it->first.second;
if (one->source_file != NULL)
{
assert(two->source_file != NULL);
if (one == two)
fprintf(stderr,"\tFound races between %ld pairs of "
"threads on line %d of %s\n", it->second,
one->source_line_number, one->source_file);
else
fprintf(stderr,"\tFound races between %ld pairs of threads "
"on line %d of %s and line %d of %s\n", it->second,
one->source_line_number, one->source_file,
two->source_line_number, two->source_file);
}
else
{
assert(two->source_file == NULL);
if (one == two)
fprintf(stderr,"\tFound races between %ld pairs of "
"threads on PTX line number %d\n",
it->second, one->line_number);
else
fprintf(stderr,"\tFound races between %ld pairs of threads on "
"PTX line %d and PTX line %d\n", it->second,
one->line_number, two->line_number);
}
}
fprintf(stderr,"WEFT INFO: Found %d total races in kernel %s!\n"
" Run with '-d' flag to see detailed per-thread "
"and per-address races\n", total_races, program->get_name());
}
else
fprintf(stderr,"WEFT INFO: Found %d total races in kernel %s!\n",
total_races, program->get_name());
fprintf(stderr,"WEFT INFO: RACES DETECTED IN KERNEL %s!\n",
program->get_name());
}
else
fprintf(stdout,"WEFT INFO: No races detected in kernel %s!\n",
program->get_name());
}
size_t SharedMemory::count_race_tests(void)
{
size_t result = 0;
for (std::map<int,Address*>::const_iterator it = addresses.begin();
it != addresses.end(); it++)
{
result += it->second->count_race_tests();
}
return result;
}
RaceCheckTask::RaceCheckTask(Address *addr)
: address(addr)
{
}
void RaceCheckTask::execute(void)
{
address->perform_race_tests();
}