Skip to content

Commit

Permalink
Basic infrastructure for Multicore Computing: Missing some barriers, …
Browse files Browse the repository at this point in the history
…missing G(lobal) and P(artitioned) schedulers
  • Loading branch information
Leonardo Horstmann authored and emilysk49 committed Oct 22, 2024
1 parent 3d3d7f2 commit 9c94639
Show file tree
Hide file tree
Showing 96 changed files with 2,076 additions and 668 deletions.
135 changes: 135 additions & 0 deletions app/concurrent_philosophers_dinner/concurrent_philosophers_dinner.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// EPOS Scheduler Test Program

#include <machine.h>
#include <time.h>
#include <synchronizer.h>
#include <process.h>

using namespace EPOS;

const int iterations = 10;

Semaphore table;

Thread * phil[5];
Semaphore * chopstick[5];

OStream cout;

int philosopher(int n, int l, int c);
void think(unsigned long long n);
void eat(unsigned long long n);
unsigned long long busy_wait(unsigned long long n);

int main()
{
table.p();
Display::clear();
Display::position(0, 0);
cout << "The Philosopher's Dinner:" << endl;

for(int i = 0; i < 5; i++)
chopstick[i] = new Semaphore;

phil[0] = new Thread(&philosopher, 0, 5, 30);
phil[1] = new Thread(&philosopher, 1, 10, 44);
phil[2] = new Thread(&philosopher, 2, 16, 39);
phil[3] = new Thread(&philosopher, 3, 16, 21);
phil[4] = new Thread(&philosopher, 4, 10, 17);

cout << "Philosophers are alive and hungry!" << endl;

Display::position(7, 44);
cout << '/';
Display::position(13, 44);
cout << '\\';
Display::position(16, 35);
cout << '|';
Display::position(13, 27);
cout << '/';
Display::position(7, 27);
cout << '\\';
Display::position(19, 0);

cout << "The dinner is served ..." << endl;
table.v();

for(int i = 0; i < 5; i++) {
int ret = phil[i]->join();
table.p();
Display::position(20 + i, 0);
cout << "Philosopher " << i << " ate " << ret << " times " << endl;
table.v();
}

for(int i = 0; i < 5; i++)
delete chopstick[i];
for(int i = 0; i < 5; i++)
delete phil[i];

cout << "The end!" << endl;

return 0;
}

int philosopher(int n, int l, int c)
{
int first = (n < 4)? n : 0;
int second = (n < 4)? n + 1 : 4;

for(int i = iterations; i > 0; i--) {

table.p();
Display::position(l, c);
cout << "thinking[" << CPU::id() << "]";
table.v();

think(1000000);

table.p();
Display::position(l, c);
cout << " hungry[" << CPU::id() << "]";
table.v();

chopstick[first]->p(); // get first chopstick
chopstick[second]->p(); // get second chopstick

table.p();
Display::position(l, c);
cout << " eating[" << CPU::id() << "] ";
table.v();

eat(500000);

table.p();
Display::position(l, c);
cout << " sate[" << CPU::id() << "]";
table.v();

chopstick[first]->v(); // release first chopstick
chopstick[second]->v(); // release second chopstick
}

table.p();
Display::position(l, c);
cout << " done[" << CPU::id() << "] ";
table.v();

return iterations;
}

void eat(unsigned long long n) {
busy_wait(n);
}

void think(unsigned long long n) {
busy_wait(n);
}

unsigned long long busy_wait(unsigned long long n)
{
volatile unsigned long long v;
for(unsigned long long int j = 0; j < 20 * n; j++)
v &= 2 ^ j;
return v;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#ifndef __traits_h
#define __traits_h

#include <system/config.h>

__BEGIN_SYS

// Build
template<> struct Traits<Build>: public Traits_Tokens
{
// Basic configuration
static const unsigned int SMOD = LIBRARY;
static const unsigned int ARCHITECTURE = RV64;
static const unsigned int MACHINE = RISCV;
static const unsigned int MODEL = SiFive_U;
static const unsigned int CPUS = 1;
static const unsigned int NETWORKING = STANDALONE;
static const unsigned int EXPECTED_SIMULATION_TIME = 60; // s (0 => not simulated)

// Default flags
static const bool enabled = true;
static const bool debugged = true;
static const bool hysterically_debugged = false;
};


// Utilities
template<> struct Traits<Debug>: public Traits<Build>
{
static const bool error = true;
static const bool warning = true;
static const bool info = false;
static const bool trace = false;
};

template<> struct Traits<Lists>: public Traits<Build>
{
static const bool debugged = hysterically_debugged;
};

template<> struct Traits<Spin>: public Traits<Build>
{
static const bool debugged = hysterically_debugged;
};

template<> struct Traits<Heaps>: public Traits<Build>
{
static const bool debugged = hysterically_debugged;
};

template<> struct Traits<Observers>: public Traits<Build>
{
// Some observed objects are created before initializing the Display
// Enabling debug may cause trouble in some Machines
static const bool debugged = false;
};


// System Parts (mostly to fine control debugging)
template<> struct Traits<Boot>: public Traits<Build>
{
};

template<> struct Traits<Setup>: public Traits<Build>
{
};

template<> struct Traits<Init>: public Traits<Build>
{
};

template<> struct Traits<Framework>: public Traits<Build>
{
};

template<> struct Traits<Aspect>: public Traits<Build>
{
static const bool debugged = hysterically_debugged;
};


__END_SYS

// Mediators
#include __ARCHITECTURE_TRAITS_H
#include __MACHINE_TRAITS_H

__BEGIN_SYS


// API Components
template<> struct Traits<Application>: public Traits<Build>
{
static const unsigned int STACK_SIZE = Traits<Machine>::STACK_SIZE;
static const unsigned int HEAP_SIZE = Traits<Machine>::HEAP_SIZE;
static const unsigned int MAX_THREADS = Traits<Machine>::MAX_THREADS;
};

template<> struct Traits<System>: public Traits<Build>
{
static const bool multithread = (Traits<Application>::MAX_THREADS > 1) || (CPUS > 1);
static const bool multicore = multithread && (CPUS > 1);
static const bool multiheap = Traits<Scratchpad>::enabled;

static const unsigned long LIFE_SPAN = 1 * YEAR; // s
static const unsigned int DUTY_CYCLE = 1000000; // ppm

static const bool reboot = true;

static const unsigned int STACK_SIZE = Traits<Machine>::STACK_SIZE;
static const unsigned int HEAP_SIZE = (Traits<Application>::MAX_THREADS + Traits<Build>::CPUS) * Traits<Application>::STACK_SIZE;
};

template<> struct Traits<Thread>: public Traits<Build>
{
static const bool enabled = Traits<System>::multithread;
static const bool smp = Traits<System>::multicore;
static const bool trace_idle = hysterically_debugged;
static const bool simulate_capacity = false;

typedef IF<(CPUS > 1), GRR, RR>::Result Criterion;
static const unsigned int QUANTUM = 10000; // us
};

template<> struct Traits<Scheduler<Thread>>: public Traits<Build>
{
static const bool debugged = Traits<Thread>::trace_idle || hysterically_debugged;
};

template<> struct Traits<Synchronizer>: public Traits<Build>
{
static const bool enabled = Traits<System>::multithread;
};

template<> struct Traits<Alarm>: public Traits<Build>
{
static const bool visible = hysterically_debugged;
};

template<> struct Traits<Address_Space>: public Traits<Build> {};

template<> struct Traits<Segment>: public Traits<Build> {};

__END_SYS

#endif
17 changes: 17 additions & 0 deletions app/concurrent_philosophers_dinner/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# EPOS Application Makefile

include ../../makedefs

all: install

$(APPLICATION): $(APPLICATION).o $(LIB)/*
$(ALD) $(ALDFLAGS) -o $@ $(APPLICATION).o

$(APPLICATION).o: $(APPLICATION).cc $(SRC)
$(ACC) $(ACCFLAGS) -o $@ $<

install: $(APPLICATION)
$(INSTALL) $(APPLICATION) $(IMG)

clean:
$(CLEAN) *.o $(APPLICATION)
13 changes: 8 additions & 5 deletions app/hello/hello_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ struct Traits<Build> : public Traits_Tokens
static const unsigned int ARCHITECTURE = IA32;
static const unsigned int MACHINE = PC;
static const unsigned int MODEL = Legacy_PC;
static const unsigned int CPUS = 1;
static const unsigned int CPUS = ((MODEL == Legacy_PC) || (MODEL == Raspberry_Pi3) || (MODEL == Realview_PBX) || (MODEL == Zynq) || (MODEL == SiFive_U)) ? 2 : 1;
static const unsigned int NETWORKING = STANDALONE;
static const unsigned int EXPECTED_SIMULATION_TIME = 60; // s (0 => not simulated)

// Default flags
static const bool enabled = true;
static const bool monitored = true;
static const bool debugged = false;
static const bool debugged = true;
static const bool hysterically_debugged = false;
};

Expand Down Expand Up @@ -118,7 +118,8 @@ struct Traits<Application> : public Traits<Build>
template <>
struct Traits<System> : public Traits<Build>
{
static const bool multithread = (Traits<Application>::MAX_THREADS > 1);
static const bool multithread = (Traits<Application>::MAX_THREADS > 1) || (CPUS > 1);
static const bool multicore = multithread && (CPUS > 1);
static const bool multiheap = Traits<Scratchpad>::enabled;

static const unsigned long LIFE_SPAN = 1 * YEAR; // s
Expand All @@ -127,18 +128,20 @@ struct Traits<System> : public Traits<Build>
static const bool reboot = true;

static const unsigned int STACK_SIZE = Traits<Machine>::STACK_SIZE;
static const unsigned int HEAP_SIZE = (Traits<Application>::MAX_THREADS + 1) * Traits<Application>::STACK_SIZE;
static const unsigned int HEAP_SIZE = (Traits<Application>::MAX_THREADS + Traits<Build>::CPUS) * Traits<Application>::STACK_SIZE;
};

template <>
struct Traits<Thread> : public Traits<Build>
{
static const bool enabled = Traits<System>::multithread;
static const bool smp = Traits<System>::multicore;
static const bool trace_idle = hysterically_debugged;
static const bool simulate_capacity = false;
static const int priority_inversion_protocol = NONE;

typedef EAMQ Criterion;

typedef IF<(CPUS > 1), EAMQ, Priority>::Result Criterion;
static const unsigned int QUANTUM = 10000; // us

static const bool debugged = false;
Expand Down
Loading

0 comments on commit 9c94639

Please sign in to comment.