Skip to content

Commit

Permalink
Starting work on race detection
Browse files Browse the repository at this point in the history
  • Loading branch information
lightsighter committed Jan 4, 2015
1 parent 520f91e commit a4fcb19
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/instruction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1267,12 +1267,13 @@ PTXInstruction* PTXSharedAccess::emulate(Thread *thread)
if (!thread->get_value(addr, value))
return next;
int64_t address = value + offset;
WeftInstruction *instruction;
WeftAccess *instruction;
if (write)
instruction = new SharedWrite(address, this, thread);
else
instruction = new SharedRead(address, this, thread);
thread->add_instruction(instruction);
thread->update_shared_memory(instruction);
return next;
}

Expand Down
9 changes: 7 additions & 2 deletions src/program.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ void Program::convert_to_instructions(int max_num_threads,
}
}

Thread::Thread(unsigned tid, Program *p)
: thread_id(tid), program(p), max_barrier_name(-1)
Thread::Thread(unsigned tid, Program *p, SharedMemory *m)
: thread_id(tid), program(p), shared_memory(m), max_barrier_name(-1)
{
dynamic_counts.resize(PTX_LAST, 0);
}
Expand Down Expand Up @@ -340,6 +340,11 @@ int Thread::accumulate_instruction_counts(std::vector<int> &total_counts)
return total;
}

void Thread::update_shared_memory(WeftAccess *access)
{
shared_memory->update_accesses(access);
}

void Thread::initialize_happens(int total_threads,
int max_num_barriers)
{
Expand Down
18 changes: 11 additions & 7 deletions src/program.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
#ifndef __PROGRAM_H__
#define __PROGRAM_H__

class Weft;
class Thread;
class PTXInstruction;
class WeftInstruction;

#include <map>
#include <deque>
#include <vector>
#include <cassert>

class Weft;
class Thread;
class Happens;
class WeftAccess;
class SharedMemory;
class PTXInstruction;
class WeftInstruction;

class Program {
public:
Expand All @@ -54,8 +54,9 @@ class Program {

class Thread {
public:
Thread(unsigned thread_id, Program *p);
Thread(const Thread &rhs) : thread_id(0), program(NULL) { assert(false); }
Thread(unsigned thread_id, Program *p, SharedMemory *s);
Thread(const Thread &rhs) : thread_id(0),
program(NULL), shared_memory(NULL) { assert(false); }
~Thread(void);
public:
Thread& operator=(const Thread &rhs) { assert(false); return *this; }
Expand All @@ -77,6 +78,8 @@ class Thread {
public:
void profile_instruction(PTXInstruction *instruction);
int accumulate_instruction_counts(std::vector<int> &total_counts);
public:
void update_shared_memory(WeftAccess *access);
public:
inline size_t get_program_size(void) const { return instructions.size(); }
inline WeftInstruction* get_instruction(int idx)
Expand All @@ -91,6 +94,7 @@ class Thread {
public:
const unsigned thread_id;
Program *const program;
SharedMemory *const shared_memory;
protected:
std::map<std::string,int64_t/*addr*/> shared_locations;
std::map<int64_t/*register*/,int64_t/*value*/> register_store;
Expand Down
87 changes: 87 additions & 0 deletions src/race.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,90 @@ void Happens::update_happens_relationships(void)
}
}

Address::Address(const int addr)
: address(addr)
{
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)
{

}

SharedMemory::SharedMemory(Weft *w)
: weft(w)
{
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);
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)
{

}

RaceCheckTask::RaceCheckTask(Address *addr)
: address(addr)
{
}

void RaceCheckTask::execute(void)
{
address->perform_race_tests();
}

43 changes: 43 additions & 0 deletions src/race.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
#ifndef __RACE_H__
#define __RACE_H__

#include <map>
#include <deque>
#include <vector>
#include <cassert>
#include <pthread.h>

class Weft;
class WeftAccess;
class WeftBarrier;

class Happens {
Expand All @@ -42,4 +47,42 @@ class Happens {
std::vector<int> happens_after;
};

class Address {
public:
Address(const int addr);
Address(const Address &rhs) : address(0) { assert(false); }
~Address(void);
public:
Address& operator=(const Address &rhs) { assert(false); return *this; }
public:
void add_access(WeftAccess *access);
void perform_race_tests(void);
public:
const int address;
protected:
pthread_mutex_t address_lock;
std::deque<WeftAccess*> accesses;
};

class SharedMemory {
public:
SharedMemory(Weft *weft);
SharedMemory(const SharedMemory &rhs) : weft(NULL) { assert(false); }
~SharedMemory(void);
public:
SharedMemory& operator=(const SharedMemory &rhs)
{ assert(false); return *this; }
public:
void update_accesses(WeftAccess *access);
int count_addresses(void) const;
void enqueue_race_checks(void);
void check_for_races(void);
public:
Weft *const weft;
protected:
pthread_mutex_t memory_lock;
std::map<int/*address*/,Address*> addresses;
};


#endif // __RACE_H__
25 changes: 23 additions & 2 deletions src/weft.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "weft.h"
#include "race.h"
#include "graph.h"
#include "program.h"

Expand All @@ -37,7 +38,8 @@ Weft::Weft(int argc, char **argv)
: file_name(NULL), max_num_threads(-1),
thread_pool_size(1), max_num_barriers(1),
verbose(false), instrument(false),
warnings(false), program(NULL), graph(NULL),
warnings(false), warp_synchronous(false),
program(NULL), shared_memory(NULL), graph(NULL),
worker_threads(NULL), pending_count(0)
{
parse_inputs(argc, argv);
Expand All @@ -52,6 +54,11 @@ Weft::~Weft(void)
delete program;
program = NULL;
}
if (shared_memory != NULL)
{
delete shared_memory;
shared_memory = NULL;
}
if (graph != NULL)
{
delete graph;
Expand Down Expand Up @@ -105,6 +112,11 @@ void Weft::parse_inputs(int argc, char **argv)
max_num_threads = atoi(argv[++i]);
continue;
}
if (!strcmp(argv[i],"-s"))
{
warp_synchronous = true;
continue;
}
if (!strcmp(argv[i],"-t"))
{
thread_pool_size = atoi(argv[++i]);
Expand Down Expand Up @@ -140,6 +152,7 @@ void Weft::parse_inputs(int argc, char **argv)
fprintf(stdout," Verbose: %s\n", (verbose ? "yes" : "no"));
fprintf(stdout," Instrument: %s\n", (instrument ? "yes" : "no"));
fprintf(stdout," Report Warnings: %s\n", (warnings ? "yes" : "no"));
fprintf(stdout," Warp-Synchronous Execution: %s\n", (warnings ? "yes" : "no"));
}
}

Expand All @@ -151,6 +164,7 @@ void Weft::report_usage(int error, const char *error_str)
fprintf(stderr," -f: specify the input file\n");
fprintf(stderr," -i: instrument execution\n");
fprintf(stderr," -n: maximum number of threads per CTA\n");
fprintf(stderr," -s: assume warp-synchronous execution\n");
fprintf(stderr," -t: thread pool size\n");
fprintf(stderr," -v: print verbose output\n");
fprintf(stderr," -w: report emulation warnings\n");
Expand Down Expand Up @@ -189,12 +203,14 @@ void Weft::emulate_threads(void)
max_num_threads, thread_pool_size);
if (instrument)
start_instrumentation(1/*stage*/);
assert(shared_memory == NULL);
shared_memory = new SharedMemory(this);
assert(max_num_threads > 0);
threads.resize(max_num_threads, NULL);
initialize_count(max_num_threads);
for (int i = 0; i < max_num_threads; i++)
{
threads[i] = new Thread(i, program);
threads[i] = new Thread(i, program, shared_memory);
EmulateTask *task = new EmulateTask(threads[i]);
enqueue_task(task);
}
Expand Down Expand Up @@ -290,6 +306,11 @@ void Weft::check_for_race_conditions(void)
if (instrument)
start_instrumentation(4/*stage*/);

initialize_count(shared_memory->count_addresses());
shared_memory->enqueue_race_checks();
wait_until_done();
shared_memory->check_for_races();

if (instrument)
stop_instrumentation(4/*stage*/);
}
Expand Down
18 changes: 18 additions & 0 deletions src/weft.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ enum {
class Weft;
class Thread;
class Program;
class Address;
class SharedMemory;
class BarrierInstance;
class BarrierDependenceGraph;

Expand Down Expand Up @@ -151,6 +153,20 @@ class UpdateThreadTask : public WeftTask {
Thread *const thread;
};

class RaceCheckTask : public WeftTask {
public:
RaceCheckTask(Address *address);
RaceCheckTask(const RaceCheckTask &rhs) : address(NULL) { assert(false); }
virtual ~RaceCheckTask(void) { }
public:
RaceCheckTask& operator=(const RaceCheckTask &rhs)
{ assert(false); return *this; }
public:
virtual void execute(void);
public:
Address *address;
};

class Weft {
public:
Weft(int argc, char **argv);
Expand Down Expand Up @@ -195,9 +211,11 @@ class Weft {
bool verbose;
bool instrument;
bool warnings;
bool warp_synchronous;
protected:
Program *program;
std::vector<Thread*> threads;
SharedMemory *shared_memory;
protected:
BarrierDependenceGraph *graph;
protected:
Expand Down

0 comments on commit a4fcb19

Please sign in to comment.