diff --git a/golang/uPIMulator/benchmark/CMakeLists.txt b/golang/uPIMulator/benchmark/CMakeLists.txt index 30998a1..528bd99 100644 --- a/golang/uPIMulator/benchmark/CMakeLists.txt +++ b/golang/uPIMulator/benchmark/CMakeLists.txt @@ -11,6 +11,7 @@ add_subdirectory(RED) add_subdirectory(SCAN-RSS) add_subdirectory(SCAN-SSA) add_subdirectory(SEL) +add_subdirectory(SIM) add_subdirectory(TRNS) add_subdirectory(TS) add_subdirectory(UNI) diff --git a/golang/uPIMulator/benchmark/SIM/CMakeLists.txt b/golang/uPIMulator/benchmark/SIM/CMakeLists.txt new file mode 100644 index 0000000..18f9379 --- /dev/null +++ b/golang/uPIMulator/benchmark/SIM/CMakeLists.txt @@ -0,0 +1,2 @@ +#add_subdirectory(host) +add_subdirectory(dpu) \ No newline at end of file diff --git a/golang/uPIMulator/benchmark/SIM/Makefile b/golang/uPIMulator/benchmark/SIM/Makefile new file mode 100644 index 0000000..0c06dc4 --- /dev/null +++ b/golang/uPIMulator/benchmark/SIM/Makefile @@ -0,0 +1,48 @@ +DPU_DIR := dpu +HOST_DIR := host +BUILDDIR ?= bin +NR_TASKLETS ?= 16 +BL ?= 10 +NR_DPUS ?= 1 +TYPE ?= INT32 +ENERGY ?= 0 + +define conf_filename + ${BUILDDIR}/.NR_DPUS_$(1)_NR_TASKLETS_$(2)_BL_$(3)_TYPE_$(4).conf +endef +CONF := $(call conf_filename,${NR_DPUS},${NR_TASKLETS},${BL},${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 := -w -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${TYPE} -DENERGY=${ENERGY} +DPU_FLAGS := ${COMMON_FLAGS} -O2 -DNR_TASKLETS=${NR_TASKLETS} -DBL=${BL} -D${TYPE} + +all: ${HOST_TARGET} ${DPU_TARGET} + +${CONF}: + $(RM) $(call conf_filename,*,*) + touch ${CONF} + +${HOST_TARGET}: ${HOST_SOURCES} ${COMMON_INCLUDES} ${CONF} + $(CC) -o ${HOST_TARGET}.bin ${HOST_SOURCES} ${HOST_FLAGS} + $(CC) -S -o ${HOST_TARGET}.S ${HOST_SOURCES} ${HOST_FLAGS} + +${DPU_TARGET}: ${DPU_SOURCES} ${COMMON_INCLUDES} ${CONF} + dpu-upmem-dpurte-clang ${DPU_FLAGS} -o $@ ${DPU_SOURCES} + dpu-upmem-dpurte-clang -S ${DPU_FLAGS} -o ${DPU_TARGET}.S ${DPU_SOURCES} + +clean: + $(RM) -r $(BUILDDIR) + +test: all + ./${HOST_TARGET} diff --git a/golang/uPIMulator/benchmark/SIM/baselines/cpu/Makefile b/golang/uPIMulator/benchmark/SIM/baselines/cpu/Makefile new file mode 100644 index 0000000..f320d87 --- /dev/null +++ b/golang/uPIMulator/benchmark/SIM/baselines/cpu/Makefile @@ -0,0 +1,5 @@ +all: + gcc -o va -fopenmp app_baseline.c + +clean: + rm va diff --git a/golang/uPIMulator/benchmark/SIM/baselines/cpu/README b/golang/uPIMulator/benchmark/SIM/baselines/cpu/README new file mode 100644 index 0000000..1b979ac --- /dev/null +++ b/golang/uPIMulator/benchmark/SIM/baselines/cpu/README @@ -0,0 +1,9 @@ +Vector addition (VA) + +Compilation instructions + + make + +Execution instructions + + ./va -t 4 diff --git a/golang/uPIMulator/benchmark/SIM/baselines/cpu/app_baseline.c b/golang/uPIMulator/benchmark/SIM/baselines/cpu/app_baseline.c new file mode 100644 index 0000000..ecd8efa --- /dev/null +++ b/golang/uPIMulator/benchmark/SIM/baselines/cpu/app_baseline.c @@ -0,0 +1,132 @@ +/** +* @file app.c +* @brief Template for a Host Application Source File. +* +*/ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "../../support/timer.h" + +static int32_t *A; +static int32_t *B; +static int32_t *C; +static int32_t *C2; + +/** +* @brief creates a "test file" by filling a buffer of 64MB with pseudo-random values +* @param nr_elements how many 32-bit elements we want the file to be +* @return the buffer address +*/ +void *create_test_file(unsigned int nr_elements) { + srand(0); + printf("nr_elements\t%u\t", nr_elements); + A = (uint32_t*) malloc(nr_elements * sizeof(uint32_t)); + B = (uint32_t*) malloc(nr_elements * sizeof(uint32_t)); + C = (uint32_t*) malloc(nr_elements * sizeof(uint32_t)); + + for (int i = 0; i < nr_elements; i++) { + A[i] = (int) (rand()); + B[i] = (int) (rand()); + } + +} + +/** +* @brief compute output in the host +*/ +static void vector_addition_host(unsigned int nr_elements, int t) { + omp_set_num_threads(t); + #pragma omp parallel for + for (int i = 0; i < nr_elements; i++) { + C[i] = A[i] + B[i]; + } +} + +// Params --------------------------------------------------------------------- +typedef struct Params { + int input_size; + int n_warmup; + int n_reps; + int n_threads; +}Params; + +void usage() { + fprintf(stderr, + "\nUsage: ./program [options]" + "\n" + "\nGeneral options:" + "\n -h help" + "\n -t # of threads (default=8)" + "\n -w # of untimed warmup iterations (default=1)" + "\n -e # of timed repetition iterations (default=3)" + "\n" + "\nBenchmark-specific options:" + "\n -i input size (default=8M elements)" + "\n"); +} + +struct Params input_params(int argc, char **argv) { + struct Params p; + p.input_size = 16777216; + p.n_warmup = 1; + p.n_reps = 3; + p.n_threads = 5; + + int opt; + while((opt = getopt(argc, argv, "hi:w:e: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 't': p.n_threads = atoi(optarg); break; + default: + fprintf(stderr, "\nUnrecognized option!\n"); + usage(); + exit(0); + } + } + assert(p.n_threads > 0 && "Invalid # of ranks!"); + + return p; +} + +/** +* @brief Main of the Host Application. +*/ +int main(int argc, char **argv) { + + struct Params p = input_params(argc, argv); + + const unsigned int file_size = p.input_size; + + // Create an input file with arbitrary data. + create_test_file(file_size); + + Timer timer; + start(&timer, 0, 0); + + vector_addition_host(file_size, p.n_threads); + + stop(&timer, 0); + printf("Kernel "); + print(&timer, 0, 1); + printf("\n"); + + free(A); + free(B); + free(C); + + return 0; + } diff --git a/golang/uPIMulator/benchmark/SIM/baselines/gpu/Makefile b/golang/uPIMulator/benchmark/SIM/baselines/gpu/Makefile new file mode 100644 index 0000000..0b822b6 --- /dev/null +++ b/golang/uPIMulator/benchmark/SIM/baselines/gpu/Makefile @@ -0,0 +1,5 @@ +all: + /usr/local/cuda/bin/nvcc vec_add.cu -I/usr/local/cuda/include -lm -o va + +clean: + rm va diff --git a/golang/uPIMulator/benchmark/SIM/baselines/gpu/README b/golang/uPIMulator/benchmark/SIM/baselines/gpu/README new file mode 100644 index 0000000..3cfa0c6 --- /dev/null +++ b/golang/uPIMulator/benchmark/SIM/baselines/gpu/README @@ -0,0 +1,9 @@ +Vector addition (VA) + +Compilation instructions + + make + +Execution instructions + + ./va diff --git a/golang/uPIMulator/benchmark/SIM/baselines/gpu/vec_add.cu b/golang/uPIMulator/benchmark/SIM/baselines/gpu/vec_add.cu new file mode 100644 index 0000000..c0c259b --- /dev/null +++ b/golang/uPIMulator/benchmark/SIM/baselines/gpu/vec_add.cu @@ -0,0 +1,101 @@ +/* File: vec_add.cu + * Purpose: Implement vector addition on a gpu using cuda + * + * Compile: nvcc [-g] [-G] -o vec_add vec_add.cu + * Run: ./vec_add + */ + +#include +#include +#include +#include + +__global__ void Vec_add(unsigned int x[], unsigned int y[], unsigned int z[], int n) { + int thread_id = blockIdx.x * blockDim.x + threadIdx.x; + if (thread_id < n){ + z[thread_id] = x[thread_id] + y[thread_id]; + } +} + + +int main(int argc, char* argv[]) { + int n, m; + unsigned int *h_x, *h_y, *h_z; + unsigned int *d_x, *d_y, *d_z; + size_t size; + + /* Define vector length */ + n = 2621440; + m = 320; + size = m * n * sizeof(unsigned int); + + // Allocate memory for the vectors on host memory. + h_x = (unsigned int*) malloc(size); + h_y = (unsigned int*) malloc(size); + h_z = (unsigned int*) malloc(size); + + for (int i = 0; i < n * m; i++) { + h_x[i] = i+1; + h_y[i] = n-i; + } + + printf("Input size = %d\n", n * m); + + // Print original vectors. + /*printf("h_x = "); + for (int i = 0; i < m; i++){ + printf("%u ", h_x[i]); + } + printf("\n\n"); + printf("h_y = "); + for (int i = 0; i < m; i++){ + printf("%u ", h_y[i]); + } + printf("\n\n");*/ + + // Event creation + cudaEvent_t start, stop; + cudaEventCreate(&start); + cudaEventCreate(&stop); + float time1 = 0; + + /* Allocate vectors in device memory */ + cudaMalloc(&d_x, size); + cudaMalloc(&d_y, size); + cudaMalloc(&d_z, size); + + /* Copy vectors from host memory to device memory */ + cudaMemcpy(d_x, h_x, size, cudaMemcpyHostToDevice); + cudaMemcpy(d_y, h_y, size, cudaMemcpyHostToDevice); + + // Start timer + cudaEventRecord( start, 0 ); + + /* Kernel Call */ + Vec_add<<<(n * m) / 256, 256>>>(d_x, d_y, d_z, n * m); + + // End timer + cudaEventRecord( stop, 0 ); + cudaEventSynchronize( stop ); + cudaEventElapsedTime( &time1, start, stop ); + + cudaMemcpy(h_z, d_z, size, cudaMemcpyDeviceToHost); + /*printf("The sum is: \n"); + for (int i = 0; i < m; i++){ + printf("%u ", h_z[i]); + } + printf("\n");*/ + + printf("Execution time = %f ms\n", time1); + + /* Free device memory */ + cudaFree(d_x); + cudaFree(d_y); + cudaFree(d_z); + /* Free host memory */ + free(h_x); + free(h_y); + free(h_z); + + return 0; +} /* main */ diff --git a/golang/uPIMulator/benchmark/SIM/dpu/CMakeLists.txt b/golang/uPIMulator/benchmark/SIM/dpu/CMakeLists.txt new file mode 100644 index 0000000..3149359 --- /dev/null +++ b/golang/uPIMulator/benchmark/SIM/dpu/CMakeLists.txt @@ -0,0 +1,10 @@ +SET(BL 10) +SET(TYPE INT32) + +set(CMAKE_C_COMPILER "/root/upmem-2023.2.0-Linux-x86_64/bin/dpu-upmem-dpurte-clang") +set(CMAKE_C_FLAGS "-w -I/root/uPIMulator/benchmark/SIM/support -O2 -S -DNR_TASKLETS=${NR_TASKLETS} -DBL=${BL} -D${TYPE}") + +file(GLOB_RECURSE SRCS *.c) + +add_executable(SIM_device ${SRCS}) + diff --git a/golang/uPIMulator/benchmark/SIM/dpu/task.c b/golang/uPIMulator/benchmark/SIM/dpu/task.c new file mode 100644 index 0000000..44d7974 --- /dev/null +++ b/golang/uPIMulator/benchmark/SIM/dpu/task.c @@ -0,0 +1,34 @@ +/* +* Vector addition with multiple tasklets +* +*/ +#include +#include +#include +#include +#include +#include +#include + +#include "../support/common.h" + +// Barrier +BARRIER_INIT(my_barrier, NR_TASKLETS); +// main_kernel +int main(void) { + unsigned int tasklet_id = me(); + if (tasklet_id == 0){ // Initialize once the cycle counter + mem_reset(); // Reset the heap + } + // Barrier + barrier_wait(&my_barrier); + uint64_t result = 0; + uint64_t task_num = -1; + mram_read(DPU_MRAM_HEAP_POINTER, &task_num, 8); + for (uint64_t i = 0; i < task_num; i++) { + result += task_num; + } + mram_write(&result, DPU_MRAM_HEAP_POINTER + 64, 8); + + return 0; +} diff --git a/golang/uPIMulator/benchmark/SIM/host/app.c b/golang/uPIMulator/benchmark/SIM/host/app.c new file mode 100644 index 0000000..c7c02f3 --- /dev/null +++ b/golang/uPIMulator/benchmark/SIM/host/app.c @@ -0,0 +1,222 @@ +/** +* app.c +* VA Host Application Source File +* +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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 +#endif + +// Pointer declaration +static T* A; +static T* B; +static T* C; +static T* C2; + +// Create input arrays +static void read_input(T* A, T* B, 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()); + B[i] = (T) (rand()); + } +} + +// Compute output in the host +static void vector_addition_host(T* C, T* A, T* B, unsigned int nr_elements) { + for (unsigned int i = 0; i < nr_elements; i++) { + C[i] = A[i] + B[i]; + } +} + +// 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; + + 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)); + B = malloc(input_size_dpu_8bytes * nr_of_dpus * sizeof(T)); + C = malloc(input_size_dpu_8bytes * nr_of_dpus * sizeof(T)); + C2 = malloc(input_size_dpu_8bytes * nr_of_dpus * sizeof(T)); + T *bufferA = A; + T *bufferB = B; + T *bufferC = C2; + + // Create an input file with arbitrary data + read_input(A, B, 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); + vector_addition_host(C, A, B, 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); + // Input arguments + unsigned int kernel = 0; + dpu_arguments_t input_arguments[NR_DPUS]; + for(i=0; i= 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"); + if(rep >= p.n_warmup) + start(&timer, 3, rep - p.n_warmup); + i = 0; + // PARALLEL RETRIEVE TRANSFER + DPU_FOREACH(dpu_set, dpu, i) { + DPU_ASSERT(dpu_prepare_xfer(dpu, bufferC + input_size_dpu_8bytes * i)); + } + DPU_ASSERT(dpu_push_xfer(dpu_set, DPU_XFER_FROM_DPU, DPU_MRAM_HEAP_POINTER_NAME, input_size_dpu_8bytes * sizeof(T), input_size_dpu_8bytes * sizeof(T), DPU_XFER_DEFAULT)); + if(rep >= p.n_warmup) + stop(&timer, 3); + + } + + // 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("DPU-CPU "); + 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; + for (i = 0; i < input_size; i++) { + if(C[i] != bufferC[i]){ + status = false; +#if PRINT + printf("%d: %u -- %u\n", i, C[i], bufferC[i]); +#endif + } + } + 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(B); + free(C); + free(C2); + DPU_ASSERT(dpu_free(dpu_set)); + + return status ? 0 : -1; +} diff --git a/golang/uPIMulator/benchmark/SIM/support/common.h b/golang/uPIMulator/benchmark/SIM/support/common.h new file mode 100644 index 0000000..c1043fd --- /dev/null +++ b/golang/uPIMulator/benchmark/SIM/support/common.h @@ -0,0 +1,62 @@ +#ifndef _COMMON_H_ +#define _COMMON_H_ + +// Structures used by both the host and the dpu to communicate information +typedef struct { + uint32_t size; + uint32_t transfer_size; + enum kernels { + kernel1 = 0, + nr_kernels = 1, + } kernel; +} dpu_arguments_t; + +// 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 + +#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/golang/uPIMulator/benchmark/SIM/support/params.h b/golang/uPIMulator/benchmark/SIM/support/params.h new file mode 100644 index 0000000..efb34f3 --- /dev/null +++ b/golang/uPIMulator/benchmark/SIM/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 # of untimed warmup iterations (default=1)" + "\n -e # of timed repetition iterations (default=3)" + "\n -x Weak (0) or strong (1) scaling (default=0)" + "\n" + "\nBenchmark-specific options:" + "\n -i input size (default=2621440 elements)" + "\n"); +} + +struct Params input_params(int argc, char **argv) { + struct Params p; + p.input_size = 2621440; + 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/golang/uPIMulator/benchmark/SIM/support/timer.h b/golang/uPIMulator/benchmark/SIM/support/timer.h new file mode 100644 index 0000000..eedc385 --- /dev/null +++ b/golang/uPIMulator/benchmark/SIM/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 + +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)); } diff --git a/golang/uPIMulator/build_run.sh b/golang/uPIMulator/build_run.sh new file mode 100644 index 0000000..b394647 --- /dev/null +++ b/golang/uPIMulator/build_run.sh @@ -0,0 +1,7 @@ +#!/bin/bash +rm -rf bin +mkdir bin +pushd script || exit 1 +python3 build.py || exit 1 +popd || exit 1 +bash run.sh diff --git a/golang/uPIMulator/run.sh b/golang/uPIMulator/run.sh new file mode 100644 index 0000000..ece675d --- /dev/null +++ b/golang/uPIMulator/run.sh @@ -0,0 +1,8 @@ +#!/bin/bash +rm -rf bin +mkdir bin +# pushd script || exit 1 +# python3 build.py || exit 1 +# popd || exit 1 +./build/uPIMulator --root_dirpath $PWD --bin_dirpath $PWD/bin --benchmark SIM --num_channels 1 --num_ranks_per_channel 1 --num_dpus_per_rank $NUM_DPUS --num_tasklets $NUM_TASKLETS --data_prep_params 1024 --verbose 3 + diff --git a/golang/uPIMulator/src/assembler/assembler.go b/golang/uPIMulator/src/assembler/assembler.go index 0b26695..5d20298 100644 --- a/golang/uPIMulator/src/assembler/assembler.go +++ b/golang/uPIMulator/src/assembler/assembler.go @@ -50,6 +50,7 @@ func (this *Assembler) Init(command_line_parser *misc.CommandLineParser) { this.assemblables["TS"] = new(prim.Ts) this.assemblables["UNI"] = new(prim.Uni) this.assemblables["VA"] = new(prim.Va) + this.assemblables["SIM"] = new(prim.Sim) if assemblable, found := this.assemblables[this.benchmark]; found { assemblable.Init(command_line_parser) diff --git a/golang/uPIMulator/src/assembler/prim/sim.go b/golang/uPIMulator/src/assembler/prim/sim.go new file mode 100644 index 0000000..a8c09ef --- /dev/null +++ b/golang/uPIMulator/src/assembler/prim/sim.go @@ -0,0 +1,103 @@ +package prim + +import ( + "errors" + "uPIMulator/src/abi/encoding" + "uPIMulator/src/abi/word" + "uPIMulator/src/misc" +) + +type Sim struct { + num_dpus int + num_tasklets int + num_executions int +} + +func (this *Sim) Init(command_line_parser *misc.CommandLineParser) { + num_channels := int(command_line_parser.IntParameter("num_channels")) + num_ranks_per_channel := int(command_line_parser.IntParameter("num_ranks_per_channel")) + num_dpus_per_rank := int(command_line_parser.IntParameter("num_dpus_per_rank")) + + this.num_dpus = num_channels * num_ranks_per_channel * num_dpus_per_rank + this.num_tasklets = int(command_line_parser.IntParameter("num_tasklets")) + + this.num_executions = 1 +} + +func (this *Sim) InputDpuHost(execution int, dpu_id int) map[string]*encoding.ByteStream { + if execution >= this.num_executions { + err := errors.New("execution >= num executions") + panic(err) + } else if dpu_id >= this.num_dpus { + err := errors.New("DPU ID >= num DPUs") + panic(err) + } + + dpu_host := make(map[string]*encoding.ByteStream, 0) + return dpu_host +} + +func (this *Sim) OutputDpuHost(execution int, dpu_id int) map[string]*encoding.ByteStream { + if execution >= this.num_executions { + err := errors.New("execution >= num executions") + panic(err) + } else if dpu_id >= this.num_dpus { + err := errors.New("DPU ID >= num DPUs") + panic(err) + } + + return make(map[string]*encoding.ByteStream, 0) +} + +func (this *Sim) InputDpuMramHeapPointerName( + execution int, + dpu_id int, +) (int64, *encoding.ByteStream) { + if execution >= this.num_executions { + err := errors.New("execution >= num executions") + panic(err) + } else if dpu_id >= this.num_dpus { + err := errors.New("DPU ID >= num DPUs") + panic(err) + } + + byte_stream := new(encoding.ByteStream) + byte_stream.Init() + + dpu_id_word := new(word.Word) + dpu_id_word.Init(64) + dpu_id_word.SetValue(int64(dpu_id)) + byte_stream.Merge(dpu_id_word.ToByteStream()) + + return 0, byte_stream +} + +func (this *Sim) OutputDpuMramHeapPointerName( + execution int, + dpu_id int, +) (int64, *encoding.ByteStream) { + if execution >= this.num_executions { + err := errors.New("execution >= num executions") + panic(err) + } else if dpu_id >= this.num_dpus { + err := errors.New("DPU ID >= num DPUs") + panic(err) + } + + byte_stream := new(encoding.ByteStream) + byte_stream.Init() + // result := int64(dpu_id * (dpu_id - 1) / 2) + result := int64(dpu_id * dpu_id) + // result := int64(dpu_id) + result_word := new(word.Word) + result_word.Init(64) + result_word.SetValue(result) + byte_stream.Merge(result_word.ToByteStream()) + + return 64, byte_stream +} + +func (this *Sim) NumExecutions() int { + return this.num_executions +} + diff --git a/golang/uPIMulator/src/compiler/compiler.go b/golang/uPIMulator/src/compiler/compiler.go index f14b65a..e1aa541 100644 --- a/golang/uPIMulator/src/compiler/compiler.go +++ b/golang/uPIMulator/src/compiler/compiler.go @@ -1,8 +1,8 @@ package compiler import ( + "fmt" "os/exec" - "path/filepath" "strconv" "uPIMulator/src/misc" ) @@ -34,9 +34,9 @@ func (this *Compiler) Init(command_line_parser *misc.CommandLineParser) { } func (this *Compiler) Build() { - docker_dirpath := filepath.Join(this.root_dirpath, "docker") + // docker_dirpath := filepath.Join(this.root_dirpath, "docker") - command := exec.Command("docker", "build", "-t", "bongjoonhyun/upimulator", docker_dirpath) + command := exec.Command("docker", "pull", "patricklidockerhub/upimulator") err := command.Run() @@ -58,7 +58,7 @@ func (this *Compiler) CompileBenchmark() { "--rm", "-v", this.root_dirpath+":/root/uPIMulator", - "bongjoonhyun/upimulator", + "patricklidockerhub/upimulator", "python3", "/root/uPIMulator/benchmark/build.py", "--num_dpus", @@ -67,11 +67,12 @@ func (this *Compiler) CompileBenchmark() { strconv.Itoa(this.num_tasklets), ) - err := command.Run() + out, err := command.Output() if err != nil { panic(err) } + fmt.Printf("Command output:\n%s", out) } func (this *Compiler) CompileSdk() { @@ -82,7 +83,7 @@ func (this *Compiler) CompileSdk() { "--rm", "-v", this.root_dirpath+":/root/uPIMulator", - "bongjoonhyun/upimulator", + "patricklidockerhub/upimulator", "python3", "/root/uPIMulator/sdk/build.py", "--num_tasklets", diff --git a/golang/uPIMulator/src/simulator/dpu/logic/alu.go b/golang/uPIMulator/src/simulator/dpu/logic/alu.go index a14682f..3af1b79 100644 --- a/golang/uPIMulator/src/simulator/dpu/logic/alu.go +++ b/golang/uPIMulator/src/simulator/dpu/logic/alu.go @@ -133,7 +133,7 @@ func (this *Alu) Subc(operand1 int64, operand2 int64, carry_flag bool) (int64, b var result int64 var carry bool if carry_flag { - if word1.Value(word.UNSIGNED)+1 >= word2.Value(word.UNSIGNED) { + if word1.Value(word.UNSIGNED) > word2.Value(word.UNSIGNED) { result = word1.Value(word.UNSIGNED) - word2.Value(word.UNSIGNED) - 1 carry = false } else { diff --git a/golang/uPIMulator/src/simulator/dpu/logic/logic.go b/golang/uPIMulator/src/simulator/dpu/logic/logic.go index 32de3e3..3befa39 100644 --- a/golang/uPIMulator/src/simulator/dpu/logic/logic.go +++ b/golang/uPIMulator/src/simulator/dpu/logic/logic.go @@ -5613,19 +5613,19 @@ func (this *Logic) SetSubNzCc( word2.Init(mram_data_width) word2.SetValue(operand2) - if word1.Value(word.UNSIGNED) < word2.Value(word.UNSIGNED) { + if carry { thread.RegFile().SetCondition(cc.LTU) } - if word1.Value(word.UNSIGNED) <= word2.Value(word.UNSIGNED) { + if carry || result == 0 { thread.RegFile().SetCondition(cc.LEU) } - if word1.Value(word.UNSIGNED) > word2.Value(word.UNSIGNED) { + if !carry { thread.RegFile().SetCondition(cc.GTU) } - if word1.Value(word.UNSIGNED) >= word2.Value(word.UNSIGNED) { + if !carry && result != 0 { thread.RegFile().SetCondition(cc.GEU) } diff --git a/golang/uPIMulator/src/simulator/host/channel_transfer_read_job.go b/golang/uPIMulator/src/simulator/host/channel_transfer_read_job.go index 9b9c214..4387c08 100644 --- a/golang/uPIMulator/src/simulator/host/channel_transfer_read_job.go +++ b/golang/uPIMulator/src/simulator/host/channel_transfer_read_job.go @@ -2,6 +2,7 @@ package host import ( "errors" + "fmt" "uPIMulator/src/abi/encoding" "uPIMulator/src/simulator/channel" ) @@ -65,7 +66,7 @@ func (this *ChannelTransferReadJob) CompareByteStreams( for j := int64(0); j < byte_stream_1.Size(); j++ { if byte_stream_1.Get(int(j)) != byte_stream_2.Get(int(j)) { - err := errors.New("bytes are different") + err := errors.New(fmt.Sprintf("bytes are different (%d != %d)", byte_stream_1.Get(int(j)), byte_stream_2.Get(int(j)))) panic(err) } }