summaryrefslogtreecommitdiff
path: root/RED
diff options
context:
space:
mode:
authorJuan Gomez Luna <juan.gomez@safari.ethz.ch>2021-06-16 19:46:05 +0200
committerJuan Gomez Luna <juan.gomez@safari.ethz.ch>2021-06-16 19:46:05 +0200
commit3de4b495fb176eba9a0eb517a4ce05903cb67acb (patch)
treefc6776a94549d2d4039898f183dbbeb2ce013ba9 /RED
parentef5c3688c486b80a56d3c1cded25f2b2387f2668 (diff)
PrIM -- first commit
Diffstat (limited to 'RED')
-rw-r--r--RED/Makefile49
-rw-r--r--RED/baselines/cpu/Makefile5
-rw-r--r--RED/baselines/cpu/README9
-rw-r--r--RED/baselines/cpu/app_baseline.cpp206
-rw-r--r--RED/baselines/gpu/Makefile5
-rw-r--r--RED/baselines/gpu/README9
-rw-r--r--RED/baselines/gpu/app_baseline.cu216
-rw-r--r--RED/dpu/task.c153
-rw-r--r--RED/host/app.c261
-rwxr-xr-xRED/run.sh32
-rwxr-xr-xRED/support/common.h73
-rw-r--r--RED/support/cyclecount.h19
-rw-r--r--RED/support/params.h56
-rwxr-xr-xRED/support/timer.h59
14 files changed, 1152 insertions, 0 deletions
diff --git a/RED/Makefile b/RED/Makefile
new file mode 100644
index 0000000..ffdeecc
--- /dev/null
+++ b/RED/Makefile
@@ -0,0 +1,49 @@
+DPU_DIR := dpu
+HOST_DIR := host
+BUILDDIR ?= bin
+NR_TASKLETS ?= 16
+BL ?= 10
+NR_DPUS ?= 1
+VERSION ?= SINGLE
+SYNC ?= HAND
+TYPE ?= INT64
+ENERGY ?= 0
+PERF ?= 0
+
+define conf_filename
+ ${BUILDDIR}/.NR_DPUS_$(1)_NR_TASKLETS_$(2)_BL_$(3)_VERSION_$(4)_SYNC_$(5)_TYPE_$(6).conf
+endef
+CONF := $(call conf_filename,${NR_DPUS},${NR_TASKLETS},${BL},${VERSION},${SYNC},${TYPE})
+
+HOST_TARGET := ${BUILDDIR}/host_code
+DPU_TARGET := ${BUILDDIR}/dpu_code
+
+COMMON_INCLUDES := support
+HOST_SOURCES := $(wildcard ${HOST_DIR}/*.c)
+DPU_SOURCES := $(wildcard ${DPU_DIR}/*.c)
+
+.PHONY: all clean test
+
+__dirs := $(shell mkdir -p ${BUILDDIR})
+
+COMMON_FLAGS := -Wall -Wextra -g -I${COMMON_INCLUDES}
+HOST_FLAGS := ${COMMON_FLAGS} -std=c11 -O3 `dpu-pkg-config --cflags --libs dpu` -DNR_TASKLETS=${NR_TASKLETS} -DNR_DPUS=${NR_DPUS} -DBL=${BL} -D${VERSION} -D${SYNC} -D${TYPE} -DENERGY=${ENERGY} -DPERF=${PERF}
+DPU_FLAGS := ${COMMON_FLAGS} -O2 -DNR_TASKLETS=${NR_TASKLETS} -DBL=${BL} -D${VERSION} -D${SYNC} -D${TYPE} -DPERF=${PERF}
+
+all: ${HOST_TARGET} ${DPU_TARGET}
+
+${CONF}:
+ $(RM) $(call conf_filename,*,*)
+ touch ${CONF}
+
+${HOST_TARGET}: ${HOST_SOURCES} ${COMMON_INCLUDES} ${CONF}
+ $(CC) -o $@ ${HOST_SOURCES} ${HOST_FLAGS}
+
+${DPU_TARGET}: ${DPU_SOURCES} ${COMMON_INCLUDES} ${CONF}
+ dpu-upmem-dpurte-clang ${DPU_FLAGS} -o $@ ${DPU_SOURCES}
+
+clean:
+ $(RM) -r $(BUILDDIR)
+
+test: all
+ ./${HOST_TARGET} -w 0 -e 1 -i 6553600 -x 1
diff --git a/RED/baselines/cpu/Makefile b/RED/baselines/cpu/Makefile
new file mode 100644
index 0000000..154c620
--- /dev/null
+++ b/RED/baselines/cpu/Makefile
@@ -0,0 +1,5 @@
+all:
+ g++ -O2 app_baseline.cpp -fopenmp -DTHRUST_HOST_SYSTEM=THRUST_HOST_SYSTEM_CPP -DTHRUST_DEVICE_SYSTEM=THRUST_DEVICE_SYSTEM_OMP -lgomp -I/usr/local/cuda-8.0/include -lm -o red -D${TYPE}
+
+clean:
+ rm red
diff --git a/RED/baselines/cpu/README b/RED/baselines/cpu/README
new file mode 100644
index 0000000..3fcea07
--- /dev/null
+++ b/RED/baselines/cpu/README
@@ -0,0 +1,9 @@
+Reduction (RED)
+
+Compilation instructions
+
+ TYPE=UINT64 make
+
+Execution instructions
+
+ ./red -i 1048576000 -t 4
diff --git a/RED/baselines/cpu/app_baseline.cpp b/RED/baselines/cpu/app_baseline.cpp
new file mode 100644
index 0000000..af897f5
--- /dev/null
+++ b/RED/baselines/cpu/app_baseline.cpp
@@ -0,0 +1,206 @@
+/*
+* JGL@SAFARI
+*/
+
+/**
+* CPU code with Thrust
+*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <assert.h>
+
+#include <iostream>
+#include <fstream>
+#include <cstdlib>
+#include <ctime>
+#include <cstdio>
+#include <math.h>
+#include <sys/time.h>
+
+#include <vector>
+#include <thrust/device_vector.h>
+#include <thrust/host_vector.h>
+#include <thrust/scan.h>
+#include <thrust/copy.h>
+#include <thrust/system/omp/execution_policy.h>
+#include <thrust/system/omp/vector.h>
+
+#include <omp.h>
+
+#include "../../support/common.h"
+#include "../../support/timer.h"
+
+#define ANSI_COLOR_RED "\x1b[31m"
+#define ANSI_COLOR_GREEN "\x1b[32m"
+#define ANSI_COLOR_RESET "\x1b[0m"
+
+// Pointer declaration
+static T* A;
+static T* C;
+static T* C2;
+
+/**
+* @brief creates input arrays
+* @param nr_elements how many elements in input arrays
+*/
+static void read_input(T* A, unsigned int nr_elements) {
+ //srand(0);
+ printf("nr_elements\t%u\t", nr_elements);
+ for (unsigned int i = 0; i < nr_elements; i++) {
+ //A[i] = (T) (rand()) % 2;
+ A[i] = i;
+ }
+}
+
+/**
+* @brief compute output in the host
+*/
+static T reduction_host(T* A, unsigned int nr_elements) {
+ T count = 0;
+ for (unsigned int i = 0; i < nr_elements; i++) {
+ count += A[i];
+ }
+ return count;
+}
+
+// Params ---------------------------------------------------------------------
+typedef struct Params {
+ unsigned int input_size;
+ int n_warmup;
+ int n_reps;
+ int exp;
+ int n_threads;
+}Params;
+
+void usage() {
+ fprintf(stderr,
+ "\nUsage: ./program [options]"
+ "\n"
+ "\nGeneral options:"
+ "\n -h help"
+ "\n -w <W> # of untimed warmup iterations (default=1)"
+ "\n -e <E> # of timed repetition iterations (default=3)"
+ "\n -x <X> Weak (0) or strong (1) scaling (default=0)"
+ "\n -t <T> # of threads (default=8)"
+ "\n"
+ "\nBenchmark-specific options:"
+ "\n -i <I> input size (default=2M elements)"
+ "\n");
+}
+
+struct Params input_params(int argc, char **argv) {
+ struct Params p;
+ p.input_size = 2 << 20;
+ p.n_warmup = 1;
+ p.n_reps = 3;
+ p.exp = 0;
+ p.n_threads = 1;
+
+ int opt;
+ while((opt = getopt(argc, argv, "hi:w:e:x:t:")) >= 0) {
+ switch(opt) {
+ case 'h':
+ usage();
+ exit(0);
+ break;
+ case 'i': p.input_size = atoi(optarg); break;
+ case 'w': p.n_warmup = atoi(optarg); break;
+ case 'e': p.n_reps = atoi(optarg); break;
+ case 'x': p.exp = atoi(optarg); break;
+ case 't': p.n_threads = atoi(optarg); break;
+ default:
+ fprintf(stderr, "\nUnrecognized option!\n");
+ usage();
+ exit(0);
+ }
+ }
+ assert(p.n_threads > 0 && "Invalid # of threads!");
+
+ return p;
+}
+
+/**
+* @brief Main of the Host Application.
+*/
+int main(int argc, char **argv) {
+
+ struct Params p = input_params(argc, argv);
+
+ unsigned int nr_of_dpus = 1;
+
+ unsigned int i = 0;
+ const unsigned int input_size = p.exp == 0 ? p.input_size * nr_of_dpus : p.input_size;
+ assert(input_size % (p.n_threads) == 0 && "Input size!");
+
+ // Input/output allocation
+ A = (T*)malloc(input_size * sizeof(T));
+ C = (T*)malloc(input_size * sizeof(T));
+ C2 = (T*)malloc(input_size * sizeof(T));
+ T *bufferA = A;
+ T *bufferC = C2;
+
+ T count = 0;
+ T count_host = 0;
+
+ // Create an input file with arbitrary data.
+ read_input(A, input_size);
+
+ // Timer declaration
+ Timer timer;
+ float time_gpu = 0;
+
+ thrust::omp::vector<T> h_output(input_size);
+
+ // Loop over main kernel
+ for(int rep = 0; rep < p.n_warmup + p.n_reps; rep++) {
+
+ // Compute output on CPU (performance comparison and verification purposes)
+ if(rep >= p.n_warmup)
+ start(&timer, 0, rep - p.n_warmup);
+ count_host = reduction_host(A, input_size);
+ if(rep >= p.n_warmup)
+ stop(&timer, 0);
+
+ thrust::omp::vector<T> d_input(input_size);
+ memcpy(thrust::raw_pointer_cast(&d_input[0]), A, input_size * sizeof(T));
+
+ omp_set_num_threads(p.n_threads);
+
+ if(rep >= p.n_warmup)
+ start(&timer, 1, rep - p.n_warmup);
+ count = thrust::reduce(thrust::omp::par, d_input.begin(), d_input.end());
+ if(rep >= p.n_warmup)
+ stop(&timer, 1);
+ h_output = d_input;
+
+ }
+
+ // Print timing results
+ printf("CPU ");
+ print(&timer, 0, p.n_reps);
+ printf("Kernel ");
+ print(&timer, 1, p.n_reps);
+
+ // Check output
+ bool status = true;
+ if(count_host != count){
+ status = false;
+ printf("%lu -- %lu\n", count, count_host);
+ }
+ if (status) {
+ printf("[" ANSI_COLOR_GREEN "OK" ANSI_COLOR_RESET "] Outputs are equal\n");
+ } else {
+ printf("[" ANSI_COLOR_RED "ERROR" ANSI_COLOR_RESET "] Outputs differ!\n");
+ }
+
+ // Deallocation
+ free(A);
+ free(C);
+ free(C2);
+
+ return 0;
+}
diff --git a/RED/baselines/gpu/Makefile b/RED/baselines/gpu/Makefile
new file mode 100644
index 0000000..301fb16
--- /dev/null
+++ b/RED/baselines/gpu/Makefile
@@ -0,0 +1,5 @@
+all:
+ /usr/local/cuda/bin/nvcc app_baseline.cu -I/usr/local/cuda/include -lm -o red -D${TYPE}
+
+clean:
+ rm red
diff --git a/RED/baselines/gpu/README b/RED/baselines/gpu/README
new file mode 100644
index 0000000..c44123b
--- /dev/null
+++ b/RED/baselines/gpu/README
@@ -0,0 +1,9 @@
+Reduction (RED)
+
+Compilation instructions
+
+ TYPE=UINT64 make
+
+Execution instructions
+
+ ./red -i 1048576000
diff --git a/RED/baselines/gpu/app_baseline.cu b/RED/baselines/gpu/app_baseline.cu
new file mode 100644
index 0000000..7e2a317
--- /dev/null
+++ b/RED/baselines/gpu/app_baseline.cu
@@ -0,0 +1,216 @@
+/*
+* JGL@SAFARI
+*/
+
+/**
+* GPU code with Thrust
+*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <assert.h>
+
+#include <iostream>
+#include <fstream>
+#include <cstdlib>
+#include <ctime>
+#include <cstdio>
+#include <math.h>
+#include <sys/time.h>
+
+#include <vector>
+#include <thrust/device_vector.h>
+#include <thrust/host_vector.h>
+#include <thrust/scan.h>
+#include <thrust/copy.h>
+
+#include "../../support/common.h"
+#include "../../support/timer.h"
+
+#define ANSI_COLOR_RED "\x1b[31m"
+#define ANSI_COLOR_GREEN "\x1b[32m"
+#define ANSI_COLOR_RESET "\x1b[0m"
+
+// Pointer declaration
+static T* A;
+static T* C;
+static T* C2;
+
+/**
+* @brief creates input arrays
+* @param nr_elements how many elements in input arrays
+*/
+static void read_input(T* A, unsigned int nr_elements) {
+ //srand(0);
+ printf("nr_elements\t%u\t", nr_elements);
+ for (unsigned int i = 0; i < nr_elements; i++) {
+ //A[i] = (T) (rand()) % 2;
+ A[i] = i;
+ }
+}
+
+/**
+* @brief compute output in the host
+*/
+static T reduction_host(T* A, unsigned int nr_elements) {
+ T count = 0;
+ for (unsigned int i = 0; i < nr_elements; i++) {
+ count += A[i];
+ }
+ return count;
+}
+
+// Params ---------------------------------------------------------------------
+typedef struct Params {
+ unsigned int input_size;
+ int n_warmup;
+ int n_reps;
+ int exp;
+ int n_threads;
+}Params;
+
+void usage() {
+ fprintf(stderr,
+ "\nUsage: ./program [options]"
+ "\n"
+ "\nGeneral options:"
+ "\n -h help"
+ "\n -w <W> # of untimed warmup iterations (default=1)"
+ "\n -e <E> # of timed repetition iterations (default=3)"
+ "\n -x <X> Weak (0) or strong (1) scaling (default=0)"
+ "\n -t <T> # of threads (default=8)"
+ "\n"
+ "\nBenchmark-specific options:"
+ "\n -i <I> input size (default=8M elements)"
+ "\n");
+}
+
+struct Params input_params(int argc, char **argv) {
+ struct Params p;
+ p.input_size = 160 * 6553600;
+ p.n_warmup = 1;
+ p.n_reps = 3;
+ p.exp = 0;
+ p.n_threads = 8;
+
+ int opt;
+ while((opt = getopt(argc, argv, "hi:w:e:x:t:")) >= 0) {
+ switch(opt) {
+ case 'h':
+ usage();
+ exit(0);
+ break;
+ case 'i': p.input_size = atoi(optarg); break;
+ case 'w': p.n_warmup = atoi(optarg); break;
+ case 'e': p.n_reps = atoi(optarg); break;
+ case 'x': p.exp = atoi(optarg); break;
+ case 't': p.n_threads = atoi(optarg); break;
+ default:
+ fprintf(stderr, "\nUnrecognized option!\n");
+ usage();
+ exit(0);
+ }
+ }
+ assert(p.n_threads > 0 && "Invalid # of threads!");
+
+ return p;
+}
+
+/**
+* @brief Main of the Host Application.
+*/
+int main(int argc, char **argv) {
+
+ cudaDeviceProp device_properties;
+ cudaGetDeviceProperties(&device_properties, 0);
+ cudaSetDevice(0);
+
+ struct Params p = input_params(argc, argv);
+
+ unsigned int nr_of_dpus = 1;
+
+ unsigned int i = 0;
+ const unsigned int input_size = p.exp == 0 ? p.input_size * nr_of_dpus : p.input_size;
+
+ // Input/output allocation
+ A = (T*)malloc(input_size * sizeof(T));
+ C = (T*)malloc(input_size * sizeof(T));
+ C2 = (T*)malloc(input_size * sizeof(T));
+ T *bufferA = A;
+ T *bufferC = C2;
+
+ T count = 0;
+ T count_host = 0;
+
+ // Create an input file with arbitrary data.
+ read_input(A, input_size);
+
+ // Timer declaration
+ Timer timer;
+ float time_gpu = 0;
+
+ thrust::host_vector<T> h_output(input_size);
+
+ // Loop over main kernel
+ for(int rep = 0; rep < p.n_warmup + p.n_reps; rep++) {
+
+ // Compute output on CPU (performance comparison and verification purposes)
+ if(rep >= p.n_warmup)
+ start(&timer, 0, rep - p.n_warmup);
+ count_host = reduction_host(A, input_size);
+ if(rep >= p.n_warmup)
+ stop(&timer, 0);
+
+
+ // Event creation
+ cudaEvent_t start, stop;
+ cudaEventCreate(&start);
+ cudaEventCreate(&stop);
+ float time1 = 0;
+
+ thrust::device_vector<T> d_input(input_size);
+ cudaMemcpy(thrust::raw_pointer_cast(&d_input[0]), A, input_size * sizeof(T), cudaMemcpyHostToDevice);
+
+ // Start timer
+ cudaEventRecord( start, 0 );
+ count = thrust::reduce(d_input.begin(),d_input.end());
+ // End timer
+ cudaEventRecord( stop, 0 );
+ cudaEventSynchronize( stop );
+ cudaEventElapsedTime( &time1, start, stop );
+ time_gpu += time1;
+
+ h_output = d_input;
+
+ cudaEventDestroy(start);
+ cudaEventDestroy(stop);
+ }
+
+ // Print timing results
+ printf("CPU ");
+ print(&timer, 0, p.n_reps);
+ printf("Kernel (ms):");
+ printf("%f\n", time_gpu / p.n_reps);
+
+ // Check output
+ bool status = true;
+ if(count_host != count){
+ status = false;
+ printf("%lu -- %lu\n", count, count_host);
+ }
+ if (status) {
+ printf("[" ANSI_COLOR_GREEN "OK" ANSI_COLOR_RESET "] Outputs are equal\n");
+ } else {
+ printf("[" ANSI_COLOR_RED "ERROR" ANSI_COLOR_RESET "] Outputs differ!\n");
+ }
+
+ // Deallocation
+ free(A);
+ free(C);
+ free(C2);
+
+ return 0;
+}
diff --git a/RED/dpu/task.c b/RED/dpu/task.c
new file mode 100644
index 0000000..5536d4d
--- /dev/null
+++ b/RED/dpu/task.c
@@ -0,0 +1,153 @@
+/*
+* Reduction with multiple tasklets
+*
+*/
+#include <stdint.h>
+#include <stdio.h>
+#include <defs.h>
+#include <mram.h>
+#include <alloc.h>
+#include <perfcounter.h>
+#include <handshake.h>
+#include <barrier.h>
+
+#include "../support/common.h"
+#include "../support/cyclecount.h"
+
+__host dpu_arguments_t DPU_INPUT_ARGUMENTS;
+__host dpu_results_t DPU_RESULTS[NR_TASKLETS];
+
+// Array for communication between adjacent tasklets
+T message[NR_TASKLETS];
+
+// Reduction in each tasklet
+static T reduction(T *input, unsigned int l_size){
+ T output = 0;
+ for (unsigned int j = 0; j < l_size; j++){
+ output += input[j];
+ }
+ return output;
+}
+
+// Barrier
+BARRIER_INIT(my_barrier, NR_TASKLETS);
+
+extern int main_kernel1(void);
+
+int (*kernels[nr_kernels])(void) = {main_kernel1};
+
+int main(void) {
+ // Kernel
+ return kernels[DPU_INPUT_ARGUMENTS.kernel]();
+}
+
+// main_kernel1
+int main_kernel1() {
+ unsigned int tasklet_id = me();
+#if PRINT
+ printf("tasklet_id = %u\n", tasklet_id);
+#endif
+ if (tasklet_id == 0){ // Initialize once the cycle counter
+ mem_reset(); // Reset the heap
+#if PERF
+ perfcounter_config(COUNT_CYCLES, true);
+#endif
+ }
+ // Barrier
+ barrier_wait(&my_barrier);
+
+ dpu_results_t *result = &DPU_RESULTS[tasklet_id];
+#if PERF && !PERF_SYNC
+ result->cycles = 0;
+ perfcounter_cycles cycles;
+ timer_start(&cycles); // START TIMER
+#endif
+
+ uint32_t input_size_dpu_bytes = DPU_INPUT_ARGUMENTS.size; // Input size per DPU in bytes
+
+ // Address of the current processing block in MRAM
+ uint32_t base_tasklet = tasklet_id << BLOCK_SIZE_LOG2;
+ uint32_t mram_base_addr_A = (uint32_t)DPU_MRAM_HEAP_POINTER;
+
+ // Initialize a local cache to store the MRAM block
+ T *cache_A = (T *) mem_alloc(BLOCK_SIZE);
+
+ // Local count
+ T l_count = 0;
+
+#if !PERF_SYNC // COMMENT OUT TO COMPARE SYNC PRIMITIVES (Experiment in Appendix)
+ for(unsigned int byte_index = base_tasklet; byte_index < input_size_dpu_bytes; byte_index += BLOCK_SIZE * NR_TASKLETS){
+
+ // Bound checking
+ uint32_t l_size_bytes = (byte_index + BLOCK_SIZE >= input_size_dpu_bytes) ? (input_size_dpu_bytes - byte_index) : BLOCK_SIZE;
+
+ // Load cache with current MRAM block
+ mram_read((__mram_ptr void const*)(mram_base_addr_A + byte_index), cache_A, l_size_bytes);
+
+ // Reduction in each tasklet
+ l_count += reduction(cache_A, l_size_bytes >> DIV);
+
+ }
+#endif
+
+ // Reduce local counts
+ message[tasklet_id] = l_count;
+
+#if PERF && PERF_SYNC // TIMER FOR SYNC PRIMITIVES
+ result->cycles = 0;
+ perfcounter_cycles cycles;
+ timer_start(&cycles); // START TIMER
+#endif
+#ifdef TREE // Tree-based reduction
+#ifdef BARRIER
+ // Barrier
+ barrier_wait(&my_barrier);
+#endif
+
+ #pragma unroll
+ for (unsigned int offset = 1; offset < NR_TASKLETS; offset <<= 1){
+
+ if((tasklet_id & (2*offset - 1)) == 0){
+#ifndef BARRIER
+ // Wait
+ handshake_wait_for(tasklet_id + offset);
+#endif
+ message[tasklet_id] += message[tasklet_id + offset];
+ }
+
+#ifdef BARRIER
+ // Barrier
+ barrier_wait(&my_barrier);
+#else
+ else if ((tasklet_id & (offset - 1)) == 0){ // Ensure that wait and notify are in pair
+ // Notify
+ handshake_notify();
+ }
+#endif
+
+ }
+
+#else // Single-thread reduction
+ // Barrier
+ barrier_wait(&my_barrier);
+ if(tasklet_id == 0)
+ #pragma unroll
+ for (unsigned int each_tasklet = 1; each_tasklet < NR_TASKLETS; each_tasklet++){
+ message[0] += message[each_tasklet];
+ }
+#endif
+#if PERF && PERF_SYNC // TIMER FOR SYNC PRIMITIVES
+ result->cycles = timer_stop(&cycles); // STOP TIMER
+#endif
+
+ // Total count in this DPU
+ if(tasklet_id == 0){
+ result->t_count = message[tasklet_id];
+ }
+
+#if PERF && !PERF_SYNC
+ result->cycles = timer_stop(&cycles); // STOP TIMER
+#endif
+
+ return 0;
+}
diff --git a/RED/host/app.c b/RED/host/app.c
new file mode 100644
index 0000000..c274db0
--- /dev/null
+++ b/RED/host/app.c
@@ -0,0 +1,261 @@
+/**
+* app.c
+* RED Host Application Source File
+*
+*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <dpu.h>
+#include <dpu_log.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <assert.h>
+
+#include "../support/common.h"
+#include "../support/timer.h"
+#include "../support/params.h"
+
+// Define the DPU Binary path as DPU_BINARY here
+#ifndef DPU_BINARY
+#define DPU_BINARY "./bin/dpu_code"
+#endif
+
+#if ENERGY
+#include <dpu_probe.h>
+#endif
+
+// Pointer declaration
+static T* A;
+
+// Create input arrays
+static void read_input(T* A, unsigned int nr_elements) {
+ srand(0);
+ printf("nr_elements\t%u\t", nr_elements);
+ for (unsigned int i = 0; i < nr_elements; i++) {
+ A[i] = (T)(rand());
+ }
+}
+
+// Compute output in the host
+static T reduction_host(T* A, unsigned int nr_elements) {
+ T count = 0;
+ for (unsigned int i = 0; i < nr_elements; i++) {
+ count += A[i];
+ }
+ return count;
+}
+
+// Main of the Host Application
+int main(int argc, char **argv) {
+
+ struct Params p = input_params(argc, argv);
+
+ struct dpu_set_t dpu_set, dpu;
+ uint32_t nr_of_dpus;
+
+#if ENERGY
+ struct dpu_probe_t probe;
+ DPU_ASSERT(dpu_probe_init("energy_probe", &probe));
+#endif
+
+ // Allocate DPUs and load binary
+ DPU_ASSERT(dpu_alloc(NR_DPUS, NULL, &dpu_set));
+ DPU_ASSERT(dpu_load(dpu_set, DPU_BINARY, NULL));
+ DPU_ASSERT(dpu_get_nr_dpus(dpu_set, &nr_of_dpus));
+ printf("Allocated %d DPU(s)\n", nr_of_dpus);
+
+ unsigned int i = 0;
+#if PERF
+ double cc = 0;
+ double cc_min = 0;
+#endif
+
+ const unsigned int input_size = p.exp == 0 ? p.input_size * nr_of_dpus : p.input_size; // Total input size (weak or strong scaling)
+ const unsigned int input_size_8bytes =
+ ((input_size * sizeof(T)) % 8) != 0 ? roundup(input_size, 8) : input_size; // Input size per DPU (max.), 8-byte aligned
+ const unsigned int input_size_dpu = divceil(input_size, nr_of_dpus); // Input size per DPU (max.)
+ const unsigned int input_size_dpu_8bytes =
+ ((input_size_dpu * sizeof(T)) % 8) != 0 ? roundup(input_size_dpu, 8) : input_size_dpu; // Input size per DPU (max.), 8-byte aligned
+
+ // Input/output allocation
+ A = malloc(input_size_dpu_8bytes * nr_of_dpus * sizeof(T));
+ T *bufferA = A;
+ T count = 0;
+ T count_host = 0;
+
+ // Create an input file with arbitrary data
+ read_input(A, input_size);
+
+ // Timer declaration
+ Timer timer;
+
+ printf("NR_TASKLETS\t%d\tBL\t%d\n", NR_TASKLETS, BL);
+
+ // Loop over main kernel
+ for(int rep = 0; rep < p.n_warmup + p.n_reps; rep++) {
+
+ // Compute output on CPU (performance comparison and verification purposes)
+ if(rep >= p.n_warmup)
+ start(&timer, 0, rep - p.n_warmup);
+ count_host = reduction_host(A, input_size);
+ if(rep >= p.n_warmup)
+ stop(&timer, 0);
+
+ printf("Load input data\n");
+ if(rep >= p.n_warmup)
+ start(&timer, 1, rep - p.n_warmup);
+ count = 0;
+ // Input arguments
+ unsigned int kernel = 0;
+ dpu_arguments_t input_arguments[NR_DPUS];
+ for(i=0; i<nr_of_dpus-1; i++) {
+ input_arguments[i].size=input_size_dpu_8bytes * sizeof(T);
+ input_arguments[i].kernel=kernel;
+ }
+ input_arguments[nr_of_dpus-1].size=(input_size_8bytes - input_size_dpu_8bytes * (NR_DPUS-1)) * sizeof(T);
+ input_arguments[nr_of_dpus-1].kernel=kernel;
+ // Copy input arrays
+ i = 0;
+ DPU_FOREACH(dpu_set, dpu, i) {
+ DPU_ASSERT(dpu_prepare_xfer(dpu, &input_arguments[i]));
+ }
+ DPU_ASSERT(dpu_push_xfer(dpu_set, DPU_XFER_TO_DPU, "DPU_INPUT_ARGUMENTS", 0, sizeof(input_arguments[0]), DPU_XFER_DEFAULT));
+ DPU_FOREACH(dpu_set, dpu, i) {
+ DPU_ASSERT(dpu_prepare_xfer(dpu, bufferA + input_size_dpu_8bytes * i));
+ }
+ DPU_ASSERT(dpu_push_xfer(dpu_set, DPU_XFER_TO_DPU, DPU_MRAM_HEAP_POINTER_NAME, 0, input_size_dpu_8bytes * sizeof(T), DPU_XFER_DEFAULT));
+ if(rep >= p.n_warmup)
+ stop(&timer, 1);
+
+ printf("Run program on DPU(s) \n");
+ // Run DPU kernel
+ if(rep >= p.n_warmup) {
+ start(&timer, 2, rep - p.n_warmup);
+ #if ENERGY
+ DPU_ASSERT(dpu_probe_start(&probe));
+ #endif
+ }
+
+ DPU_ASSERT(dpu_launch(dpu_set, DPU_SYNCHRONOUS));
+ if(rep >= p.n_warmup) {
+ stop(&timer, 2);
+ #if ENERGY
+ DPU_ASSERT(dpu_probe_stop(&probe));
+ #endif
+ }
+
+#if PRINT
+ {
+ unsigned int each_dpu = 0;
+ printf("Display DPU Logs\n");
+ DPU_FOREACH (dpu_set, dpu) {
+ printf("DPU#%d:\n", each_dpu);
+ DPU_ASSERT(dpulog_read_for_dpu(dpu.dpu, stdout));
+ each_dpu++;
+ }
+ }
+#endif
+
+ printf("Retrieve results\n");
+ dpu_results_t results[nr_of_dpus];
+ T* results_count = malloc(nr_of_dpus * sizeof(T));
+ if(rep >= p.n_warmup)
+ start(&timer, 3, rep - p.n_warmup);
+ i = 0;
+ // PARALLEL RETRIEVE TRANSFER
+ dpu_results_t* results_retrieve[nr_of_dpus];
+
+ DPU_FOREACH(dpu_set, dpu, i) {
+ results_retrieve[i] = (dpu_results_t*)malloc(NR_TASKLETS * sizeof(dpu_results_t));
+ DPU_ASSERT(dpu_prepare_xfer(dpu, results_retrieve[i]));
+ }
+ DPU_ASSERT(dpu_push_xfer(dpu_set, DPU_XFER_FROM_DPU, "DPU_RESULTS", 0, NR_TASKLETS * sizeof(dpu_results_t), DPU_XFER_DEFAULT));
+
+ DPU_FOREACH(dpu_set, dpu, i) {
+ // Retrieve tasklet timings
+ for (unsigned int each_tasklet = 0; each_tasklet < NR_TASKLETS; each_tasklet++) {
+ if(each_tasklet == 0)
+ results[i].t_count = results_retrieve[i][each_tasklet].t_count;
+ }
+#if !PERF
+ free(results_retrieve[i]);
+#endif
+ // Sequential reduction
+ count += results[i].t_count;
+#if PRINT
+ printf("i=%d -- %lu\n", i, count);
+#endif
+ }
+
+#if PERF
+ DPU_FOREACH(dpu_set, dpu, i) {
+ results[i].cycles = 0;
+ // Retrieve tasklet timings
+ for (unsigned int each_tasklet = 0; each_tasklet < NR_TASKLETS; each_tasklet++) {
+ if (results_retrieve[i][each_tasklet].cycles > results[i].cycles)
+ results[i].cycles = results_retrieve[i][each_tasklet].cycles;
+ }
+ free(results_retrieve[i]);
+ }
+#endif
+ if(rep >= p.n_warmup)
+ stop(&timer, 3);
+
+#if PERF
+ uint64_t max_cycles = 0;
+ uint64_t min_cycles = 0xFFFFFFFFFFFFFFFF;
+ // Print performance results
+ if(rep >= p.n_warmup){
+ i = 0;
+ DPU_FOREACH(dpu_set, dpu) {
+ if(results[i].cycles > max_cycles)
+ max_cycles = results[i].cycles;
+ if(results[i].cycles < min_cycles)
+ min_cycles = results[i].cycles;
+ i++;
+ }
+ cc += (double)max_cycles;
+ cc_min += (double)min_cycles;
+ }
+#endif
+
+ // Free memory
+ free(results_count);
+ }
+#if PERF
+ printf("DPU cycles = %g cc\n", cc / p.n_reps);
+#endif
+
+ // Print timing results
+ printf("CPU ");
+ print(&timer, 0, p.n_reps);
+ printf("CPU-DPU ");
+ print(&timer, 1, p.n_reps);
+ printf("DPU Kernel ");
+ print(&timer, 2, p.n_reps);
+ printf("Inter-DPU ");
+ print(&timer, 3, p.n_reps);
+
+ #if ENERGY
+ double energy;
+ DPU_ASSERT(dpu_probe_get(&probe, DPU_ENERGY, DPU_AVERAGE, &energy));
+ printf("DPU Energy (J): %f\t", energy);
+ #endif
+
+ // Check output
+ bool status = true;
+ if(count != count_host) status = false;
+ if (status) {
+ printf("[" ANSI_COLOR_GREEN "OK" ANSI_COLOR_RESET "] Outputs are equal\n");
+ } else {
+ printf("[" ANSI_COLOR_RED "ERROR" ANSI_COLOR_RESET "] Outputs differ!\n");
+ }
+
+ // Deallocation
+ free(A);
+ DPU_ASSERT(dpu_free(dpu_set));
+
+ return status ? 0 : -1;
+}
diff --git a/RED/run.sh b/RED/run.sh
new file mode 100755
index 0000000..9d70a51
--- /dev/null
+++ b/RED/run.sh
@@ -0,0 +1,32 @@
+#!/bin/bash
+
+for i in 1
+do
+ for j in BARRIER HAND
+ do
+ for k in 1 2 4 8 16
+ do
+ PERF=1 NR_DPUS=$i NR_TASKLETS=$k BL=10 VERSION=TREE SYNC=$j make all
+ wait
+ ./bin/host_code -w 2 -e 10 -i 2097152 > profile/TREE_${j}_tl${k}_dpu${i}.txt
+ #./bin/host_code -w 2 -e 10 -i 2048 > profile/TREE_${j}_tl${k}_dpu${i}.txt
+ wait
+ make clean
+ wait
+ done
+ done
+done
+
+for i in 1
+do
+ for k in 1 2 4 8 16
+ do
+ PERF=1 NR_DPUS=$i NR_TASKLETS=$k BL=10 VERSION=SINGLE make all
+ wait
+ ./bin/host_code -w 2 -e 10 -i 2097152 > profile/SINGLE_SINGLE_tl${k}_dpu${i}.txt
+ #./bin/host_code -w 2 -e 10 -i 2048 > profile/SINGLE_SINGLE_tl${k}_dpu${i}.txt
+ wait
+ make clean
+ wait
+ done
+done
diff --git a/RED/support/common.h b/RED/support/common.h
new file mode 100755
index 0000000..121bf31
--- /dev/null
+++ b/RED/support/common.h
@@ -0,0 +1,73 @@
+#ifndef _COMMON_H_
+#define _COMMON_H_
+
+// Transfer size between MRAM and WRAM
+#ifdef BL
+#define BLOCK_SIZE_LOG2 BL
+#define BLOCK_SIZE (1 << BLOCK_SIZE_LOG2)
+#else
+#define BLOCK_SIZE_LOG2 8
+#define BLOCK_SIZE (1 << BLOCK_SIZE_LOG2)
+#define BL BLOCK_SIZE_LOG2
+#endif
+
+// Data type
+#ifdef UINT32
+#define T uint32_t
+#define DIV 2 // Shift right to divide by sizeof(T)
+#elif UINT64
+#define T uint64_t
+#define DIV 3 // Shift right to divide by sizeof(T)
+#elif INT32
+#define T int32_t
+#define DIV 2 // Shift right to divide by sizeof(T)
+#elif INT64
+#define T int64_t
+#define DIV 3 // Shift right to divide by sizeof(T)
+#elif FLOAT
+#define T float
+#define DIV 2 // Shift right to divide by sizeof(T)
+#elif DOUBLE
+#define T double
+#define DIV 3 // Shift right to divide by sizeof(T)
+#elif CHAR
+#define T char
+#define DIV 0 // Shift right to divide by sizeof(T)
+#elif SHORT
+#define T short
+#define DIV 1 // Shift right to divide by sizeof(T)
+#endif
+
+// Structures used by both the host and the dpu to communicate information
+typedef struct {
+ uint32_t size;
+ enum kernels {
+ kernel1 = 0,
+ nr_kernels = 1,
+ } kernel;
+ T t_count;
+} dpu_arguments_t;
+
+typedef struct {
+ uint64_t cycles;
+ T t_count;
+} dpu_results_t;
+
+#ifndef PERF
+#define PERF 0 // Use perfcounters?
+#endif
+#ifndef PERF_SYNC
+#define PERF_SYNC 0 // Use perfcounters to time sync primitives?
+#endif
+#ifndef ENERGY
+#define ENERGY 0
+#endif
+#define PRINT 0
+
+#define ANSI_COLOR_RED "\x1b[31m"
+#define ANSI_COLOR_GREEN "\x1b[32m"
+#define ANSI_COLOR_RESET "\x1b[0m"
+
+#define divceil(n, m) (((n)-1) / (m) + 1)
+#define roundup(n, m) ((n / m) * m + m)
+#endif
diff --git a/RED/support/cyclecount.h b/RED/support/cyclecount.h
new file mode 100644
index 0000000..c4247b5
--- /dev/null
+++ b/RED/support/cyclecount.h
@@ -0,0 +1,19 @@
+#include <perfcounter.h>
+
+// Timer
+typedef struct perfcounter_cycles{
+ perfcounter_t start;
+ perfcounter_t end;
+ perfcounter_t end2;
+
+}perfcounter_cycles;
+
+void timer_start(perfcounter_cycles *cycles){
+ cycles->start = perfcounter_get(); // START TIMER
+}
+
+uint64_t timer_stop(perfcounter_cycles *cycles){
+ cycles->end = perfcounter_get(); // STOP TIMER
+ cycles->end2 = perfcounter_get(); // STOP TIMER
+ return(((uint64_t)((uint32_t)(((cycles->end >> 4) - (cycles->start >> 4)) - ((cycles->end2 >> 4) - (cycles->end >> 4))))) << 4);
+}
diff --git a/RED/support/params.h b/RED/support/params.h
new file mode 100644
index 0000000..97bc50a
--- /dev/null
+++ b/RED/support/params.h
@@ -0,0 +1,56 @@
+#ifndef _PARAMS_H_
+#define _PARAMS_H_
+
+#include "common.h"
+
+typedef struct Params {
+ unsigned int input_size;
+ int n_warmup;
+ int n_reps;
+ int exp;
+}Params;
+
+static void usage() {
+ fprintf(stderr,
+ "\nUsage: ./program [options]"
+ "\n"
+ "\nGeneral options:"
+ "\n -h help"
+ "\n -w <W> # of untimed warmup iterations (default=1)"
+ "\n -e <E> # of timed repetition iterations (default=3)"
+ "\n -x <X> Weak (0) or strong (1) scaling (default=0)"
+ "\n"
+ "\nBenchmark-specific options:"
+ "\n -i <I> input size (default=6553600 elements)"
+ "\n");
+}
+
+struct Params input_params(int argc, char **argv) {
+ struct Params p;
+ p.input_size = 6553600;
+ p.n_warmup = 1;
+ p.n_reps = 3;
+ p.exp = 0;
+
+ int opt;
+ while((opt = getopt(argc, argv, "hi:w:e:x:")) >= 0) {
+ switch(opt) {
+ case 'h':
+ usage();
+ exit(0);
+ break;
+ case 'i': p.input_size = atoi(optarg); break;
+ case 'w': p.n_warmup = atoi(optarg); break;
+ case 'e': p.n_reps = atoi(optarg); break;
+ case 'x': p.exp = atoi(optarg); break;
+ default:
+ fprintf(stderr, "\nUnrecognized option!\n");
+ usage();
+ exit(0);
+ }
+ }
+ assert(NR_DPUS > 0 && "Invalid # of dpus!");
+
+ return p;
+}
+#endif
diff --git a/RED/support/timer.h b/RED/support/timer.h
new file mode 100755
index 0000000..eedc385
--- /dev/null
+++ b/RED/support/timer.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2016 University of Cordoba and University of Illinois
+ * All rights reserved.
+ *
+ * Developed by: IMPACT Research Group
+ * University of Cordoba and University of Illinois
+ * http://impact.crhc.illinois.edu/
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * with the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * > Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimers.
+ * > Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimers in the
+ * documentation and/or other materials provided with the distribution.
+ * > Neither the names of IMPACT Research Group, University of Cordoba,
+ * University of Illinois nor the names of its contributors may be used
+ * to endorse or promote products derived from this Software without
+ * specific prior written permission.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
+ * THE SOFTWARE.
+ *
+ */
+
+#include <sys/time.h>
+
+typedef struct Timer{
+
+ struct timeval startTime[4];
+ struct timeval stopTime[4];
+ double time[4];
+
+}Timer;
+
+void start(Timer *timer, int i, int rep) {
+ if(rep == 0) {
+ timer->time[i] = 0.0;
+ }
+ gettimeofday(&timer->startTime[i], NULL);
+}
+
+void stop(Timer *timer, int i) {
+ gettimeofday(&timer->stopTime[i], NULL);
+ timer->time[i] += (timer->stopTime[i].tv_sec - timer->startTime[i].tv_sec) * 1000000.0 +
+ (timer->stopTime[i].tv_usec - timer->startTime[i].tv_usec);
+}
+
+void print(Timer *timer, int i, int REP) { printf("Time (ms): %f\t", timer->time[i] / (1000 * REP)); }