Skip to content

Commit

Permalink
Merge pull request #8 from hassansalehe/code-readability
Browse files Browse the repository at this point in the history
A step at time: replace comment with readable code
  • Loading branch information
hassansalehe authored Jun 20, 2021
2 parents 901ade2 + 8d8c8ae commit 4b1f8ba
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 182 deletions.
38 changes: 19 additions & 19 deletions src/common/MemoryActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class MemoryActions {

Action action;

int taskId;
ADDRESS addr; // destination address
int accessing_task_id;
ADDRESS destination_address;

// Default constructor
MemoryActions() {
Expand All @@ -41,34 +41,34 @@ class MemoryActions {
// Stores action if (a) is first action of task, or
// (b) is last write action
inline void storeAction(Action & act) {
if ( isEmpty || act.isWrite ) {
action = act;
isEmpty = false;
taskId = action.taskId;
addr = action.addr;
if ( isEmpty || act.is_write_action ) {
action = act;
isEmpty = false;
accessing_task_id = action.accessing_task_id;
destination_address = action.destination_address;
}
}

inline void storeAction(uint & taskID, ADDRESS & adr,
INTEGER & val, INTEGER & linNo,
INTEGER & funcID, bool isWrite_) {
if ( isEmpty || isWrite_) {
action.taskId = taskID;
action.addr = adr;
action.funcId = funcID;
action.value = val;
action.lineNo = linNo;
action.isWrite = isWrite_;
INTEGER & funcID, bool is_write_action_) {
if ( isEmpty || is_write_action_) {
action.accessing_task_id = taskID;
action.destination_address = adr;
action.source_func_id = funcID;
action.value_written = val;
action.source_line_num = linNo;
action.is_write_action = is_write_action_;

isEmpty = false;
taskId = action.taskId;
addr = action.addr;
isEmpty = false;
accessing_task_id = action.accessing_task_id;
destination_address = action.destination_address;
}
}

// Returns true if current action is a write
bool hasWrite() {
if ( isEmpty || ( !action.isWrite )) {
if ( isEmpty || ( !action.is_write_action )) {
return false;
} else {
return true;
Expand Down
28 changes: 14 additions & 14 deletions src/common/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@

class Action {
public:
INTEGER taskId; // task id of writter
ADDRESS addr; // destination address
VALUE value; // value written
VALUE lineNo; // source-line number
INTEGER funcId; // the identifier of corresponding function
std::string funcName; // source-function name
bool isWrite; // true if this action is "write"
INTEGER accessing_task_id;
ADDRESS destination_address;
VALUE value_written;
VALUE source_line_num;
INTEGER source_func_id;
std::string source_func_name;
bool is_write_action;

Action(INTEGER tskId, VALUE val, VALUE ln, INTEGER fuId):
taskId(tskId), value(val), lineNo(ln), funcId(fuId) {}
accessing_task_id(tskId), value_written(val), source_line_num(ln), source_func_id(fuId) {}

Action(INTEGER tskId, ADDRESS adr, VALUE val, VALUE ln, INTEGER fuId):
taskId(tskId), addr(adr), value(val), lineNo(ln), funcId(fuId) { }
accessing_task_id(tskId), destination_address(adr), value_written(val), source_line_num(ln), source_func_id(fuId) { }

Action(INTEGER tskId, address adr, lint val, int ln, INTEGER fuId ):
taskId(tskId), addr(adr), value(val), lineNo(ln), funcId(fuId) { }
accessing_task_id(tskId), destination_address(adr), value_written(val), source_line_num(ln), source_func_id(fuId) { }

Action() { }

Expand All @@ -51,10 +51,10 @@ class Action {
// Generates std::string representation of the action and stores in "buff".
// It does not append '\n' at the end of. the std::string
void printActionNN(std::ostringstream & buff) {
std::string type = " R ";
if ( isWrite ) type = " W ";
buff << taskId << type << addr << " " << value
<< " " << lineNo << " " << funcId;
std::string action_type = " R ";
if ( is_write_action ) action_type = " W ";
buff << accessing_task_id << action_type << destination_address << " " << value_written
<< " " << source_line_num << " " << source_func_id;
}

}; // end Action
Expand Down
4 changes: 2 additions & 2 deletions src/common/instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class Instruction {
public:
INTEGER lineNo;
INTEGER source_line_num;
std::string destination;
std::string type;
OPERATION oper;
Expand Down Expand Up @@ -128,7 +128,7 @@ class Instruction {
}

void print() {
std::cout << "LineNo: " << lineNo
std::cout << "source_line_num: " << source_line_num
<< ", type: " << type
<< ", oper: " << OperRepresentation(oper)
<< ", dest: " << destination
Expand Down
16 changes: 8 additions & 8 deletions src/detector/commutativity/CommutativityChecker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ VOID CommutativityChecker::parseTasksIR(char * IRlogName) {

sttmt = Instruction::trim( sttmt ); // trim spaces
if ( isValidStatement(sttmt) ) { // check if normal statement
INTEGER lineNo = getLineNumber( sttmt );
INTEGER source_line_num = getLineNumber( sttmt );
// skip instruction with line # 0: args to task body
if (lineNo <= 0) continue;
if (source_line_num <= 0) continue;

sttmt = sttmt.substr(sttmt.find_first_not_of(' ',
sttmt.find_first_of(' ')));
Instruction instr( sttmt );
instr.lineNo = lineNo;
instr.source_line_num = source_line_num;
currentTask.push_back(instr);
continue;
} // if
Expand All @@ -59,11 +59,11 @@ VOID CommutativityChecker::parseTasksIR(char * IRlogName) {
bool CommutativityChecker::isCommutative(const Conflict & conflict) {

// skip commutativity check if read-write conflict
if (conflict.action1.isWrite != conflict.action2.isWrite) {
if (conflict.action1.is_write_action != conflict.action2.is_write_action) {
return false;
}
INTEGER line1 = conflict.action1.lineNo;
INTEGER line2 = conflict.action2.lineNo;
INTEGER line1 = conflict.action1.source_line_num;
INTEGER line2 = conflict.action2.source_line_num;
operationSet.clear(); // clear set of commuting operations

// check if line1 operations commute & line2 operations commute
Expand All @@ -87,8 +87,8 @@ BOOL CommutativityChecker::involveSimpleOperations(
INTEGER index = -1;

for (auto i = taskBody->begin(); i != taskBody->end(); i++) {
if (i->lineNo > lineNumber) break;
if (i->lineNo == lineNumber) instr = *i;
if (i->source_line_num > lineNumber) break;
if (i->source_line_num == lineNumber) instr = *i;
index++;
}

Expand Down
10 changes: 5 additions & 5 deletions src/detector/commutativity/CriticalSectionBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ class CriticalSectionBody {

CriticalSectionBody(std::vector<Instruction> _body) {
if( _body.size() > 0 ) {
setStartLineNo( _body.front().lineNo );
setEndLineNo ( _body.back().lineNo );
setStartLineNo( _body.front().source_line_num );
setEndLineNo ( _body.back().source_line_num );
body = _body;
} else {
throw "Critical section must contain at least one statement";
}
}
void setStartLineNo(int lineNo) { startLineNo = lineNo; }
void setEndLineNo(int lineNo) {
endLineNo = lineNo;
void setStartLineNo(int source_line_num) { startLineNo = source_line_num; }
void setEndLineNo(int source_line_num) {
endLineNo = source_line_num;
assert(endLineNo >= startLineNo);
}
int getStartLineNo() { return startLineNo; }
Expand Down
12 changes: 6 additions & 6 deletions src/detector/commutativity/CriticalSections.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ class CriticalSections {

size_t getSize() { return sections.size(); }

CriticalSectionBody *find(int lineNo) {
CriticalSectionBody *find(int source_line_num) {
if (0 == sections.size()) return end();

for (auto &cr : sections) {
if (cr.second.getStartLineNo() <= lineNo
&& lineNo <= cr.second.getEndLineNo()) {
if (cr.second.getStartLineNo() <= source_line_num
&& source_line_num <= cr.second.getEndLineNo()) {
return &cr.second;
}
}
auto start = sections.lower_bound(lineNo);
auto start = sections.lower_bound(source_line_num);
if (start == sections.end()) return end();
if ( lineNo >= start->second.getStartLineNo() &&
lineNo <= start->second.getEndLineNo() ) {
if ( source_line_num >= start->second.getStartLineNo() &&
source_line_num <= start->second.getEndLineNo() ) {
return &start->second;
}

Expand Down
60 changes: 30 additions & 30 deletions src/detector/determinacy/checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ void Checker::detectRaceOnMem(
std::stringstream & ssin) {

Action action;
action.taskId = taskID;
action.accessing_task_id = taskID;
constructMemoryAction(ssin, operation, action);

if (action.funcId == 0) {
if (action.source_func_id == 0) {
std::cout << "Warning function Id 0: " << std::endl;
exit(0);
}
Expand All @@ -127,7 +127,7 @@ void Checker::detectRaceOnMem(
ssin >> separator;
ssin >> taskID;
Action lastWAction;
lastWAction.taskId = taskID;
lastWAction.accessing_task_id = taskID;
ssin >> operation;
constructMemoryAction(ssin, operation, lastWAction);
memActions.storeAction( lastWAction ); // save second action
Expand All @@ -147,34 +147,34 @@ void Checker::saveTaskActions( const MemoryActions & taskActions ) {
// write in the parallel writes,update and take it forward
// 4.2.1 check conflicts with other parallel tasks

auto perAddrActions = writes.find( taskActions.addr );
auto perAddrActions = writes.find(taskActions.destination_address);
if (perAddrActions == writes.end()) {// 1. first action
writes[taskActions.addr] = std::list<MemoryActions>();
writes[taskActions.destination_address] = std::list<MemoryActions>();
}
std::list<MemoryActions> & AddrActions = writes[taskActions.addr];
std::list<MemoryActions> & AddrActions = writes[taskActions.destination_address];
for (auto lastWrt = AddrActions.begin();
lastWrt != AddrActions.end(); lastWrt++) {
// actions of same task
if (taskActions.taskId == lastWrt->taskId) continue;
if (taskActions.accessing_task_id == lastWrt->accessing_task_id) continue;

auto HBfound = serial_bags[taskActions.taskId]->HB.find(lastWrt->taskId);
auto end = serial_bags[taskActions.taskId]->HB.end();
auto HBfound = serial_bags[taskActions.accessing_task_id]->HB.find(lastWrt->accessing_task_id);
auto end = serial_bags[taskActions.accessing_task_id]->HB.end();
if (HBfound != end) continue; // 3. there's happens-before

// 4. parallel, possible race! ((check race))

// check write-write case (different values written)
// 4.1 both write to shared memory
if ( (taskActions.action.isWrite && lastWrt->action.isWrite) &&
(taskActions.action.value != lastWrt->action.value) ) {
if ( (taskActions.action.is_write_action && lastWrt->action.is_write_action) &&
(taskActions.action.value_written != lastWrt->action.value_written) ) {
// write different values, code for recording errors
saveDeterminacyRaceReport( taskActions.action, lastWrt->action );
} else if ((!taskActions.action.isWrite) && lastWrt->action.isWrite) {
} else if ((!taskActions.action.is_write_action) && lastWrt->action.is_write_action) {
// 4.2 read-after-write or write-after-read conflicts
// (a) taskActions is read-only and lastWrt is a writer:
// code for recording errors
saveDeterminacyRaceReport(taskActions.action, lastWrt->action);
} else if ((!lastWrt->action.isWrite) && taskActions.action.isWrite ) {
} else if ((!lastWrt->action.is_write_action) && taskActions.action.is_write_action ) {
// (b) lastWrt is read-only and taskActions is a writer:
// code for recording errors
saveDeterminacyRaceReport(taskActions.action, lastWrt->action);
Expand All @@ -185,7 +185,7 @@ void Checker::saveTaskActions( const MemoryActions & taskActions ) {
AddrActions.pop_front(); // remove oldest element
}

writes[taskActions.addr].push_back( taskActions ); // save
writes[taskActions.destination_address].push_back( taskActions ); // save
}

// Records the determinacy race warning to the conflicts table.
Expand All @@ -200,8 +200,8 @@ VOID Checker::saveDeterminacyRaceReport(const Action& curMemAction,
// code for recording errors
std::pair<int, int> linePair =
{
std::min(curMemAction.lineNo, prevMemAction.lineNo),
std::max(curMemAction.lineNo, prevMemAction.lineNo)
std::min(curMemAction.source_line_num, prevMemAction.source_line_num),
std::max(curMemAction.source_line_num, prevMemAction.source_line_num)
};
conflictTable[linePair].insert( aConflict );
}
Expand All @@ -227,20 +227,20 @@ void Checker::constructMemoryAction(std::stringstream & ssin,
Action & action) {
std::string tempBuff;
ssin >> tempBuff; // address
action.addr = (ADDRESS)stoul(tempBuff, 0, 16);
action.destination_address = (ADDRESS)stoul(tempBuff, 0, 16);

ssin >> tempBuff; // value
action.value = stol(tempBuff);
action.value_written = stol(tempBuff);

ssin >> tempBuff; // line number
action.lineNo = stol(tempBuff);
action.source_line_num = stol(tempBuff);

ssin >> action.funcId; // get function id
ssin >> action.source_func_id; // get function id

if (operation == "W") {
action.isWrite = true;
action.is_write_action = true;
} else {
action.isWrite = false;
action.is_write_action = false;
}
#ifdef DEBUG // check if data correctly set
std::cout << "Action constructed: ";
Expand Down Expand Up @@ -309,14 +309,14 @@ VOID Checker::reportConflicts() {

for (auto aConflict : it.second) {
std::cout << " " << aConflict.addr << " lines: " << " "
<< functions.at( aConflict.action1.funcId )
<< ": " << aConflict.action1.lineNo
<< ", " << functions.at( aConflict.action2.funcId )
<< ": " << aConflict.action2.lineNo
<< " task ids: (" << aConflict.action1.taskId
<< "[" << (aConflict.action1.isWrite? "W]" : "R]")
<< " " << aConflict.action2.taskId
<< "[" << (aConflict.action2.isWrite? "W])" : "R])")
<< functions.at( aConflict.action1.source_func_id )
<< ": " << aConflict.action1.source_line_num
<< ", " << functions.at( aConflict.action2.source_func_id )
<< ": " << aConflict.action2.source_line_num
<< " task ids: (" << aConflict.action1.accessing_task_id
<< "[" << (aConflict.action1.is_write_action? "W]" : "R]")
<< " " << aConflict.action2.accessing_task_id
<< "[" << (aConflict.action2.is_write_action? "W])" : "R])")
<< std::endl;
addressCount++;

Expand Down
6 changes: 3 additions & 3 deletions src/detector/determinacy/conflict.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class Conflict {
Conflict(const Action& curMemAction, const Action& prevMemAction) {
action1 = curMemAction;
action2 = prevMemAction;
addr = curMemAction.addr;
addr = curMemAction.destination_address;
}

inline int getTask1Id() {
return action1.taskId;
return action1.accessing_task_id;
}

inline int getTask2Id() {
return action2.taskId;
return action2.accessing_task_id;
}

bool operator<(const Conflict &RHS) const {
Expand Down
2 changes: 1 addition & 1 deletion src/functionengine/FunctionEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class FunctionEngine {
public:
void pushFunction(std::string name, unsigned taskID, int lineNo);
void pushFunction(std::string name, unsigned taskID, int source_line_num);
void popFunction(std::string name, unsigned taskID);
CallStack &getStack(unsigned taskID);
void removeStack(unsigned taskID);
Expand Down
Loading

0 comments on commit 4b1f8ba

Please sign in to comment.