Skip to content

Commit

Permalink
Initialize monitors only once in network
Browse files Browse the repository at this point in the history
  • Loading branch information
lukamac committed Jan 31, 2024
1 parent 2d3d0a7 commit 47dd0fd
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 72 deletions.
6 changes: 6 additions & 0 deletions dory/Hardware_targets/PULP/GAP9/Utils_files/net_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __PERF_UTILS_H__
#include <stddef.h>
#include <stdint.h>
#include "monitor.h"

// Padding flags

Expand All @@ -11,6 +12,10 @@
#define NET_UTILS_PAD_LEFT (1 << 0)
#define NET_UTILS_NO_PAD (0)

typedef struct {
Monitor input, output, store_conf;
} TaskMonitors;

typedef struct {
unsigned int L3_input;
unsigned int L3_output;
Expand All @@ -23,6 +28,7 @@ typedef struct {
unsigned int ram;
unsigned int padding;
unsigned int layer_id;
TaskMonitors *monitor;
} layer_args_t;

void print_perf(const char *name, const int cycles, const int macs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,6 @@ static int inc(int index, int end) {

#define BUFFER_SIZE (2)

typedef struct {
Monitor input, output, store_conf;
} TaskMonitors;

struct layer_task_fork_args_t {
uint32_t L2_input;
uint32_t L2_weights;
Expand Down Expand Up @@ -360,29 +356,6 @@ void ${func_name}(void *args) {

layer_args_t *layer_args = (layer_args_t *)args;

// Initialization
TaskMonitors monitor;

int err = 0;

if (err = monitor_init(&monitor.input, BUFFER_SIZE)) {
printf("Input monitor initialization failed with status %d.\n", err);
return;
}

if (err = monitor_init(&monitor.output, BUFFER_SIZE)) {
printf("Output monitor initialization failed with status %d.\n", err);
monitor_term(monitor.input);
return;
}

if (err = monitor_init(&monitor.store_conf, BUFFER_SIZE)) {
printf("Store conf monitor initialization failed with status %d.\n", err);
monitor_term(monitor.input);
monitor_term(monitor.output);
return;
}

// Init nnx tasks
ne16_task_t ne16_tasks[BUFFER_SIZE];
for (int i = 0; i < BUFFER_SIZE; i++) {
Expand Down Expand Up @@ -417,14 +390,10 @@ void ${func_name}(void *args) {
.ne16_tasks = ne16_tasks,
.tiles = tiles,
.store_conf = store_conf,
.monitor = &monitor,
.monitor = layer_args->monitor,
};
pi_cl_team_fork(CORES, layer_task_fork, (void *)&layer_task_fork_args);


// Terminate

monitor_term(monitor.input);
monitor_term(monitor.output);
monitor_term(monitor.store_conf);
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,27 @@ void ${prefix}network_run_cluster(void *args) {
const ne16_pulp_conf_t ne16_pulp_conf = {.max_stall = 8};
ne16_nnx_init(ne16_pulp_get_dev(), &ne16_pulp_conf);

TaskMonitors monitor;

int err = 0;

if (err = monitor_init(&monitor.input, 2)) {
printf("Input monitor initialization failed with status %d.\n", err);
return;
}

if (err = monitor_init(&monitor.output, 2)) {
printf("Output monitor initialization failed with status %d.\n", err);
monitor_term(monitor.input);
return;
}

if (err = monitor_init(&monitor.store_conf, 2)) {
printf("Store conf monitor initialization failed with status %d.\n", err);
monitor_term(monitor.input);
monitor_term(monitor.output);
return;
}
/* ---------------------------------- */
/* --------- SECTION 0 END ---------- */
/* ---------------------------------- */
Expand Down Expand Up @@ -317,7 +338,8 @@ void ${prefix}network_run_cluster(void *args) {
.L1_buffer = 0,
.ram = (unsigned int) get_ram_ptr(),
.padding = NET_UTILS_PAD_TOP | NET_UTILS_PAD_BOTTOM,
.layer_id = i
.layer_id = i,
.monitor = &monitor,
};

% if 'Yes' in performance or 'Perf_final' in verbose_level:
Expand Down Expand Up @@ -514,4 +536,7 @@ void ${prefix}network_run_cluster(void *args) {
/* ---------------------------------- */

ne16_nnx_term(ne16_pulp_get_dev());
monitor_term(monitor.input);
monitor_term(monitor.output);
monitor_term(monitor.store_conf);
}
42 changes: 42 additions & 0 deletions dory/Hardware_targets/PULP/GAP9_NE16/Utils_files/monitor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "monitor.h"
#include "pmsis.h"


int monitor_init(Monitor * const monitor, int buffer_size) {
monitor->empty = pi_cl_sem_alloc();
if (monitor->empty == 0) {
return -1;
}

monitor->full = pi_cl_sem_alloc();
if (monitor->full == 0) {
pi_cl_sem_free(monitor->empty);
return -2;
}

pi_cl_sem_set(monitor->full, 0);
pi_cl_sem_set(monitor->empty, buffer_size);

return 0;
}

void monitor_term(Monitor monitor) {
pi_cl_sem_free(monitor.empty);
pi_cl_sem_free(monitor.full);
}

void monitor_produce_begin(Monitor monitor) {
pi_cl_sem_dec(monitor.empty);
}

void monitor_produce_end(Monitor monitor) {
pi_cl_sem_inc(monitor.full, 1);
}

void monitor_consume_begin(Monitor monitor) {
pi_cl_sem_dec(monitor.full);
}

void monitor_consume_end(Monitor monitor) {
pi_cl_sem_inc(monitor.empty, 1);
}
51 changes: 12 additions & 39 deletions dory/Hardware_targets/PULP/GAP9_NE16/Utils_files/monitor.h
Original file line number Diff line number Diff line change
@@ -1,47 +1,20 @@
#include "pmsis.h"
#ifndef __MONITOR_H__
#define __MONITOR_H__

#include <stdint.h>


typedef struct Monitor {
uint32_t empty;
uint32_t full;
} Monitor;


static int monitor_init(Monitor * const monitor, int buffer_size) {
monitor->empty = pi_cl_sem_alloc();
if (monitor->empty == 0) {
return -1;
}

monitor->full = pi_cl_sem_alloc();
if (monitor->full == 0) {
pi_cl_sem_free(monitor->empty);
return -2;
}

pi_cl_sem_set(monitor->full, 0);
pi_cl_sem_set(monitor->empty, buffer_size);

return 0;
}

static void monitor_term(Monitor monitor) {
pi_cl_sem_free(monitor.empty);
pi_cl_sem_free(monitor.full);
}

static void monitor_produce_begin(Monitor monitor) {
pi_cl_sem_dec(monitor.empty);
}

static void monitor_produce_end(Monitor monitor) {
pi_cl_sem_inc(monitor.full, 1);
}

static void monitor_consume_begin(Monitor monitor) {
pi_cl_sem_dec(monitor.full);
}

static void monitor_consume_end(Monitor monitor) {
pi_cl_sem_inc(monitor.empty, 1);
}
int monitor_init(Monitor * const monitor, int buffer_size);
void monitor_term(Monitor monitor);
void monitor_produce_begin(Monitor monitor);
void monitor_produce_end(Monitor monitor);
void monitor_consume_begin(Monitor monitor);
void monitor_consume_end(Monitor monitor);

#endif // __MONITOR_H__

0 comments on commit 47dd0fd

Please sign in to comment.