Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into simplify-multithread
Browse files Browse the repository at this point in the history
  • Loading branch information
wsong83 committed Jun 25, 2024
2 parents e1c228e + 2310ac7 commit 3545638
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ $(PARALLEL_REGRESSION_TESTS_EXE): %:%.cpp $(CACHE_OBJS) $(UTIL_OBJS) $(CRYPTO_LI
$(CXX) $(CXXFLAGS) $< $(CACHE_OBJS) $(UTIL_OBJS) $(CRYPTO_LIB) -o $@

$(PARALLEL_REGRESSION_TESTS_RST): %.out: %
$< 2>$@
timeout 1m $< 2>$@ 1>temp.log

regression: $(REGRESSION_TESTS_RST) $(PARALLEL_REGRESSION_TESTS_RST)

Expand All @@ -87,7 +87,7 @@ libflexicas.a: $(CACHE_OBJS) $(UTIL_OBJS) $(CRYPTO_LIB)

clean:
-$(MAKE) clean-regression
-$(MAKE) clean-parallel-regression
-$(MAKE) clean-parallel-regression temp.log
-rm $(UTIL_OBJS) $(CACHE_OBJS)

.PHONY: clean
4 changes: 2 additions & 2 deletions cache/exclusive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ class ExclusiveOuterCohPortBroadcastT : public OPUC
};

template<bool EnMT = false>
using ExclusiveOuterCohPortBroadcast = ExclusiveOuterCohPortBroadcastT<OuterCohPortUncached<EnMT>, EnMT>;
using ExclusiveOuterCohPortBroadcast = ExclusiveOuterCohPortBroadcastT<OuterCohPort<EnMT>, EnMT>;

template<class OPUC, bool EnMT> requires C_DERIVE<OPUC, OuterCohPortUncached<EnMT> >
class ExclusiveOuterCohPortDirectoryT : public OPUC
Expand Down Expand Up @@ -639,6 +639,6 @@ class ExclusiveOuterCohPortDirectoryT : public OPUC
};

template<bool EnMT = false>
using ExclusiveOuterCohPortDirectory = ExclusiveOuterCohPortDirectoryT<OuterCohPortUncached<EnMT>, EnMT>;
using ExclusiveOuterCohPortDirectory = ExclusiveOuterCohPortDirectoryT<OuterCohPort<EnMT>, EnMT>;

#endif
15 changes: 11 additions & 4 deletions regression/multi-l2-msi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,31 @@
//#define TestN 512

int main(){
auto l1d = cache_gen_multi_thread_l1<L1IW, L1WN, Data64B, MetadataBroadcastBase, ReplaceLRU_MT, MSIMultiThreadPolicy, false, false, void, false>(NCore, "l1d");
auto l1d = cache_gen_multi_thread_l1<L1IW, L1WN, Data64B, MetadataBroadcastBase, ReplaceLRU_MT, MSIMultiThreadPolicy, false, false, void, true>(NCore, "l1d");
auto core_data = get_l1_core_interface(l1d);
auto l1i = cache_gen_multi_thread_l1<L1IW, L1WN, Data64B, MetadataBroadcastBase, ReplaceLRU_MT, MSIMultiThreadPolicy, false, true, void, false>(NCore, "l1i");
auto l1i = cache_gen_multi_thread_l1<L1IW, L1WN, Data64B, MetadataBroadcastBase, ReplaceLRU_MT, MSIMultiThreadPolicy, false, true, void, true>(NCore, "l1i");
auto core_inst = get_l1_core_interface(l1i);

auto l2 = cache_gen_multi_thread_l2<L2IW, L2WN, Data64B, MetadataBroadcastBase, ReplaceLRU_MT, MSIMultiThreadPolicy, true, void, false>(1, "l2")[0];
auto mem = new SimpleMemoryModel<Data64B, void, false, true>("mem");
auto l2 = cache_gen_multi_thread_l2<L2IW, L2WN, Data64B, MetadataBroadcastBase, ReplaceLRU_MT, MSIMultiThreadPolicy, true, void, true>(1, "l2")[0];
auto mem = new SimpleMemoryModel<Data64B, void, true, true>("mem");
SimpleTracerMT tracer(true);

for(int i=0; i<NCore; i++) {
l1i[i]->outer->connect(l2->inner, l2->inner->connect(l1i[i]->outer, true));
l1d[i]->outer->connect(l2->inner, l2->inner->connect(l1d[i]->outer));
l1i[i]->attach_monitor(&tracer);
l1d[i]->attach_monitor(&tracer);
}
l2->outer->connect(mem, mem->connect(l2->outer));

l2->attach_monitor(&tracer);
mem->attach_monitor(&tracer);

ParallelRegressionGen<NCore, true, false, PAddrN, SAddrN, Data64B> tgen;

tgen.run(TestN, &core_inst, &core_data);

tracer.stop();
delete_caches(l1d);
delete_caches(l1i);
delete l2;
Expand Down
64 changes: 30 additions & 34 deletions util/monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,51 @@
#include <boost/format.hpp>
#include "cache/metadata.hpp"

static boost::format read_fmt("%-10s read %016x %02d %04d %02d %1x");
static boost::format write_fmt("%-10s write %016x %02d %04d %02d %1x");
static boost::format invalid_fmt("%-10s evict %016x %02d %04d %02d ");
static boost::format data_fmt("%016x");
void SimpleTracer::print(std::string& msg) {
std::cout << msg << std::endl;
}

void SimpleTracer::read(uint64_t cache_id, uint64_t addr, int32_t ai, int32_t s, int32_t w, bool hit, const CMMetadataBase *meta, const CMDataBase *data) {
std::cout << (read_fmt % UniqueID::name(cache_id) % addr % ai % s % w % hit);
std::string msg; msg.reserve(100);
msg += (boost::format("%-10s read %016x %02d %04d %02d %1x") % UniqueID::name(cache_id) % addr % ai % s % w % hit).str();

if(meta)
std::cout << " [" << meta->to_string() << "]";
msg.append(" [").append(meta->to_string()).append("]");
else if(data)
std::cout << " ";

if(data) {
std::cout << " ";
if(compact_data) std::cout << (data_fmt % (data->read(0)));
else std::cout << data->to_string();
}
std::cout << std::endl;
msg.append(" ");

if(data)
msg.append(" ").append(compact_data ? (boost::format("%016x") % (data->read(0))).str() : data->to_string());

print(msg);
}

void SimpleTracer::write(uint64_t cache_id, uint64_t addr, int32_t ai, int32_t s, int32_t w, bool hit, const CMMetadataBase *meta, const CMDataBase *data) {
std::cout << (write_fmt % UniqueID::name(cache_id) % addr % ai % s % w % hit);
std::string msg; msg.reserve(100);
msg += (boost::format("%-10s write %016x %02d %04d %02d %1x") % UniqueID::name(cache_id) % addr % ai % s % w % hit).str();

if(meta)
std::cout << " [" << meta->to_string() << "]";
msg.append(" [").append(meta->to_string()).append("]");
else if(data)
std::cout << " ";

if(data) {
std::cout << " ";
if(compact_data) std::cout << (data_fmt % (data->read(0)));
else std::cout << data->to_string();
}
std::cout << std::endl;
msg.append(" ");

if(data)
msg.append(" ").append(compact_data ? (boost::format("%016x") % (data->read(0))).str() : data->to_string());

print(msg);
}

void SimpleTracer::invalid(uint64_t cache_id, uint64_t addr, int32_t ai, int32_t s, int32_t w, const CMMetadataBase *meta, const CMDataBase *data) {
std::cout << (invalid_fmt % UniqueID::name(cache_id) % addr % ai % s % w) ;
std::string msg; msg.reserve(100);
msg += (boost::format("%-10s evict %016x %02d %04d %02d ") % UniqueID::name(cache_id) % addr % ai % s % w).str() ;

if(meta)
std::cout << " [" << meta->to_string() << "]";
msg.append(" [").append(meta->to_string()).append("]");
else if(data)
std::cout << " ";

if(data) {
std::cout << " ";
if(compact_data) std::cout << (data_fmt % (data->read(0)));
else std::cout << data->to_string();
}
std::cout << std::endl;
msg.append(" ");

if(data)
msg.append(" ").append(compact_data ? (boost::format("%016x") % (data->read(0))).str() : data->to_string());

print(msg);
}
20 changes: 20 additions & 0 deletions util/monitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

#include <cstdint>
#include <set>
#include <string>
#include <thread>
#include "util/delay.hpp"
#include "util/concept_macro.hpp"
#include "util/print.hpp"

class CMDataBase;
class CMMetadataBase;
Expand Down Expand Up @@ -170,6 +173,9 @@ class SimpleTracer : public MonitorBase
{
bool active;
bool compact_data;

virtual void print(std::string& msg);

public:
SimpleTracer(bool cd = false): active(true), compact_data(cd) {}
virtual ~SimpleTracer() {}
Expand All @@ -187,4 +193,18 @@ class SimpleTracer : public MonitorBase
virtual void reset() { active = false; }
};

// multithread version of simple tracer
class SimpleTracerMT : public SimpleTracer
{
PrintPool pool;
std::thread print_thread;
virtual void print(std::string& msg) { pool.add(msg); }

public:
SimpleTracerMT(bool cd = false): SimpleTracer(cd), pool(256) {
print_thread = std::thread(&PrintPool::print, &pool);
}
virtual void stop() { pool.stop(); print_thread.join(); }
};

#endif
10 changes: 9 additions & 1 deletion util/multithread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <mutex>
#include <condition_variable>
#include <memory>
#include <chrono>

template<typename T>
class AtomicVar {
Expand All @@ -23,8 +24,9 @@ class AtomicVar {
return var->load();
}

__always_inline void write(T v) {
__always_inline void write(T v, bool notify = false) {
var->store(v);
if(notify) cv.notify_all();
}

__always_inline bool swap(T& expect, T v, bool notify = false) {
Expand All @@ -40,6 +42,12 @@ class AtomicVar {
std::unique_lock lk(mtx);
cv.wait(lk);
}

__always_inline void wait_timeout() {
using namespace std::chrono_literals;
std::unique_lock lk(mtx);
cv.wait_for(lk, 10us);
}
};

#endif
54 changes: 54 additions & 0 deletions util/print.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef CM_UTIL_PRINT_HPP_
#define CM_UTIL_PRINT_HPP_

#include <string>
#include <vector>
#include <utility>
#include <iostream>
#include "util/multithread.hpp"

class PrintPool {
const int pool_size;
AtomicVar<int> pw, pr;
std::vector<std::string> pool;
std::vector<AtomicVar<bool> > valid;
AtomicVar<bool> finish;
public:
PrintPool(int pool_size) : pool_size(pool_size), pw(0), pr(0), pool(pool_size), valid(pool_size, false), finish(false) {}

std::pair<std::string *, AtomicVar<bool> *> allocate() {
while(true) {
auto index = pw.read();
if(valid[index].read()) pr.wait(); // full, wait
if(pw.swap(index, (index + 1)%pool_size)) return std::pair(&(pool[index]), &(valid[index]));
}
}

// a safer but might slower way of print a message
void add(std::string& msg) {
auto [buf, flag] = allocate();
*buf = msg;
flag->write(true, true);
}

void stop() { finish.write(true); }

void sync(){ // wait until all existing messages are printed
auto index = (pw.read() + pool_size - 1) % pool_size;
while(index != pr.read()) pr.wait();
while(valid[index].read() != false) valid[index].wait();
}

void print() { // start print the pool
auto index = pr.read();
while(!finish.read()) {
if(!valid[index].read()) { valid[index].wait_timeout(); continue; }
std::cout << pool[index] << std::endl;
valid[index].write(false, true);
index = (index + 1) % pool_size;
pr.write(index, true);
}
}
};

#endif

0 comments on commit 3545638

Please sign in to comment.