From 3de4b495fb176eba9a0eb517a4ce05903cb67acb Mon Sep 17 00:00:00 2001 From: Juan Gomez Luna Date: Wed, 16 Jun 2021 19:46:05 +0200 Subject: PrIM -- first commit --- HST-S/Makefile | 45 ++++ HST-S/baselines/cpu/Makefile | 6 + HST-S/baselines/cpu/README | 13 + HST-S/baselines/cpu/app_baseline.c | 188 +++++++++++++++ HST-S/baselines/gpu/Makefile | 51 ++++ HST-S/baselines/gpu/README | 24 ++ HST-S/baselines/gpu/input/image_VanHateren.iml | Bin 0 -> 3145728 bytes HST-S/baselines/gpu/kernel.cpp | 83 +++++++ HST-S/baselines/gpu/kernel.cu | 113 +++++++++ HST-S/baselines/gpu/kernel.h | 51 ++++ HST-S/baselines/gpu/main.cpp | 322 +++++++++++++++++++++++++ HST-S/baselines/gpu/support/common.h | 45 ++++ HST-S/baselines/gpu/support/cuda-setup.h | 78 ++++++ HST-S/baselines/gpu/support/partitioner.h | 213 ++++++++++++++++ HST-S/baselines/gpu/support/timer.h | 73 ++++++ HST-S/baselines/gpu/support/verify.h | 66 +++++ HST-S/dpu/task.c | 116 +++++++++ HST-S/host/app.c | 285 ++++++++++++++++++++++ HST-S/input/image_VanHateren.iml | Bin 0 -> 3145728 bytes HST-S/run.sh | 17 ++ HST-S/support/common.h | 45 ++++ HST-S/support/params.h | 67 +++++ HST-S/support/timer.h | 59 +++++ 23 files changed, 1960 insertions(+) create mode 100644 HST-S/Makefile create mode 100644 HST-S/baselines/cpu/Makefile create mode 100644 HST-S/baselines/cpu/README create mode 100644 HST-S/baselines/cpu/app_baseline.c create mode 100644 HST-S/baselines/gpu/Makefile create mode 100644 HST-S/baselines/gpu/README create mode 100644 HST-S/baselines/gpu/input/image_VanHateren.iml create mode 100644 HST-S/baselines/gpu/kernel.cpp create mode 100644 HST-S/baselines/gpu/kernel.cu create mode 100644 HST-S/baselines/gpu/kernel.h create mode 100644 HST-S/baselines/gpu/main.cpp create mode 100644 HST-S/baselines/gpu/support/common.h create mode 100644 HST-S/baselines/gpu/support/cuda-setup.h create mode 100644 HST-S/baselines/gpu/support/partitioner.h create mode 100644 HST-S/baselines/gpu/support/timer.h create mode 100644 HST-S/baselines/gpu/support/verify.h create mode 100644 HST-S/dpu/task.c create mode 100644 HST-S/host/app.c create mode 100644 HST-S/input/image_VanHateren.iml create mode 100755 HST-S/run.sh create mode 100755 HST-S/support/common.h create mode 100644 HST-S/support/params.h create mode 100755 HST-S/support/timer.h (limited to 'HST-S') diff --git a/HST-S/Makefile b/HST-S/Makefile new file mode 100644 index 0000000..d71e793 --- /dev/null +++ b/HST-S/Makefile @@ -0,0 +1,45 @@ +DPU_DIR := dpu +HOST_DIR := host +BUILDDIR ?= bin +NR_TASKLETS ?= 16 +BL ?= 10 +NR_DPUS ?= 1 +ENERGY ?= 0 + +define conf_filename + ${BUILDDIR}/.NR_DPUS_$(1)_NR_TASKLETS_$(2)_BL_$(3).conf +endef +CONF := $(call conf_filename,${NR_DPUS},${NR_TASKLETS},${BL}) + +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} -DENERGY=${ENERGY} +DPU_FLAGS := ${COMMON_FLAGS} -O2 -DNR_TASKLETS=${NR_TASKLETS} -DBL=${BL} + +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} diff --git a/HST-S/baselines/cpu/Makefile b/HST-S/baselines/cpu/Makefile new file mode 100644 index 0000000..708ae72 --- /dev/null +++ b/HST-S/baselines/cpu/Makefile @@ -0,0 +1,6 @@ +all: + gcc -o hist -fopenmp app_baseline.c + +clean: + rm hist + diff --git a/HST-S/baselines/cpu/README b/HST-S/baselines/cpu/README new file mode 100644 index 0000000..7c8d21a --- /dev/null +++ b/HST-S/baselines/cpu/README @@ -0,0 +1,13 @@ +Histogram - input partition (HST) + +Compilation instructions: + + make + +Execution instructions + + ./hist -y 1006632960 -t 4 + +For more options: + + ./hsti -h diff --git a/HST-S/baselines/cpu/app_baseline.c b/HST-S/baselines/cpu/app_baseline.c new file mode 100644 index 0000000..8ae2c12 --- /dev/null +++ b/HST-S/baselines/cpu/app_baseline.c @@ -0,0 +1,188 @@ +/* +* JGL@SAFARI +*/ + +/** +* @file app.c +* @brief Template for a Host Application Source File. +* +* The macros DPU_BINARY and NR_TASKLETS are directly +* used in the static functions, and are not passed as arguments of these functions. +*/ +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "../../support/common.h" +#include "../../support/timer.h" + +// Pointer declaration +static T* A; +static unsigned int* histo_host; + +typedef struct Params { + unsigned int input_size; + unsigned int bins; + int n_warmup; + int n_reps; + const char *file_name; + int exp; + int n_threads; +}Params; + +/** +* @brief creates input arrays +* @param nr_elements how many elements in input arrays +*/ +static void read_input(T* A, const Params p) { + + char dctFileName[100]; + FILE *File = NULL; + + // Open input file + unsigned short temp; + sprintf(dctFileName, p.file_name); + if((File = fopen(dctFileName, "rb")) != NULL) { + for(unsigned int y = 0; y < p.input_size; y++) { + fread(&temp, sizeof(unsigned short), 1, File); + A[y] = (unsigned int)ByteSwap16(temp); + if(A[y] >= 4096) + A[y] = 4095; + } + fclose(File); + } else { + printf("%s does not exist\n", dctFileName); + exit(1); + } +} + +/** +* @brief compute output in the host +*/ +static void histogram_host(unsigned int* histo, T* A, unsigned int bins, unsigned int nr_elements, int exp, unsigned int nr_of_dpus, int t) { + + omp_set_num_threads(t); + + if(!exp){ + #pragma omp parallel for + for (unsigned int i = 0; i < nr_of_dpus; i++) { + for (unsigned int j = 0; j < nr_elements; j++) { + T d = A[j]; + histo[i * bins + ((d * bins) >> DEPTH)] += 1; + } + } + } + else{ + #pragma omp parallel for + for (unsigned int j = 0; j < nr_elements; j++) { + T d = A[j]; + #pragma omp atomic update + histo[(d * bins) >> DEPTH] += 1; + } + } +} + +// Params --------------------------------------------------------------------- +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 -t # of threads (default=8)" + "\n -x Weak (0) or strong (1) scaling (default=0)" + "\n" + "\nBenchmark-specific options:" + "\n -i input size (default=1536*1024 elements)" + "\n -b histogram size (default=256 bins)" + "\n -f input image file (default=../input/image_VanHateren.iml)" + "\n"); +} + +struct Params input_params(int argc, char **argv) { + struct Params p; + p.input_size = 1536 * 1024; + p.bins = 256; + p.n_warmup = 1; + p.n_reps = 3; + p.n_threads = 8; + p.exp = 1; + p.file_name = "../../input/image_VanHateren.iml"; + + int opt; + while((opt = getopt(argc, argv, "hi:b:w:e:f:x:t:")) >= 0) { + switch(opt) { + case 'h': + usage(); + exit(0); + break; + case 'i': p.input_size = atoi(optarg); break; + case 'b': p.bins = atoi(optarg); break; + case 'w': p.n_warmup = atoi(optarg); break; + case 'e': p.n_reps = atoi(optarg); break; + case 'f': p.file_name = 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 ranks!"); + + return p; +} + +/** +* @brief Main of the Host Application. +*/ +int main(int argc, char **argv) { + + struct Params p = input_params(argc, argv); + + uint32_t nr_of_dpus; + + const unsigned int input_size = p.input_size; // Size of input image + if(!p.exp) + assert(input_size % p.n_threads == 0 && "Input size!"); + else + assert(input_size % p.n_threads == 0 && "Input size!"); + + // Input/output allocation + A = malloc(input_size * sizeof(T)); + T *bufferA = A; + if(!p.exp) + histo_host = malloc(nr_of_dpus * p.bins * sizeof(unsigned int)); + else + histo_host = malloc(p.bins * sizeof(unsigned int)); + + // Create an input file with arbitrary data. + read_input(A, p); + + Timer timer; + start(&timer, 0, 0); + + if(!p.exp) + memset(histo_host, 0, nr_of_dpus * p.bins * sizeof(unsigned int)); + else + memset(histo_host, 0, p.bins * sizeof(unsigned int)); + + histogram_host(histo_host, A, p.bins, input_size, p.exp, nr_of_dpus, p.n_threads); + + stop(&timer, 0); + printf("Kernel "); + print(&timer, 0, 1); + printf("\n"); + + return 0; +} diff --git a/HST-S/baselines/gpu/Makefile b/HST-S/baselines/gpu/Makefile new file mode 100644 index 0000000..620be83 --- /dev/null +++ b/HST-S/baselines/gpu/Makefile @@ -0,0 +1,51 @@ +# +# 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. +# + +CXX=/usr/local/cuda/bin/nvcc +CXX_FLAGS=-std=c++11 + +LIB=-L/usr/lib/ -L/usr/local/cuda/lib64 -lm + +INC=-I/usr/local/cuda/include + +DEP=kernel.cpp kernel.h main.cpp kernel.cu support/common.h support/cuda-setup.h support/partitioner.h support/timer.h support/verify.h +SRC=main.cpp kernel.cpp kernel.cu +EXE=hsti + +all: + $(CXX) $(CXX_FLAGS) $(SRC) $(LIB) $(INC) -o $(EXE) + +clean: + rm -f $(EXE) + diff --git a/HST-S/baselines/gpu/README b/HST-S/baselines/gpu/README new file mode 100644 index 0000000..4d6fdcc --- /dev/null +++ b/HST-S/baselines/gpu/README @@ -0,0 +1,24 @@ +Histogram - input partition (HST) + +Compilation instructions: + + make + +Execution instructions + + ./hsti -n 1006632960 -g 512 + +For more options: + + ./hsti -h + + +Note: +The input folder contains one image from Van Hateren's natural image database +(http://www.kyb.tuebingen.mpg.de/?id=227). Image pixels are 12-bit depth. Thus, +for calculation of the B-bin histogram of an image, the corresponding histogram +bin is computed as ((pixel * B) >> 12). +Monochrome images from other databases or synthetic images can also be used. The +read input function (in main.cpp) might need to be changed accordingly. If image +pixels are b-bit depth and the histogram contains B bins, the histogram bin will +be computed as ((pixel * B) >> b). diff --git a/HST-S/baselines/gpu/input/image_VanHateren.iml b/HST-S/baselines/gpu/input/image_VanHateren.iml new file mode 100644 index 0000000..1ae6047 Binary files /dev/null and b/HST-S/baselines/gpu/input/image_VanHateren.iml differ diff --git a/HST-S/baselines/gpu/kernel.cpp b/HST-S/baselines/gpu/kernel.cpp new file mode 100644 index 0000000..eab0dfe --- /dev/null +++ b/HST-S/baselines/gpu/kernel.cpp @@ -0,0 +1,83 @@ +/* + * 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 "kernel.h" +#include "support/partitioner.h" +#include +#include +#include +#include + +// CPU threads-------------------------------------------------------------------------------------- +void run_cpu_threads(std::atomic_uint *histo, unsigned int *data, int size, int bins, int n_threads, int chunk, int n_tasks, float alpha +#ifdef CUDA_8_0 + , std::atomic_int *worklist +#endif + ) { + std::vector cpu_threads; + for(int k = 0; k < n_threads; k++) { + cpu_threads.push_back(std::thread([=]() { + +#ifdef CUDA_8_0 + Partitioner p = partitioner_create(n_tasks, alpha, k, n_threads, worklist); +#else + Partitioner p = partitioner_create(n_tasks, alpha, k, n_threads); +#endif + + unsigned int Hs[bins]; + // Local histogram initialization + for(int i = 0; i < bins; i++) { + Hs[i] = 0; + } + + for(int i = cpu_first(&p); cpu_more(&p); i = cpu_next(&p)) { + for(int j = 0; j < chunk; j++) { + // Read pixel + unsigned int d = ((data[i * chunk + j] * bins) >> 12); + + // Vote in histogram + Hs[d]++; + } + } + + // Merge to global histogram + for(int i = 0; i < bins; i++) { + (&histo[i])->fetch_add(Hs[i]); + } + + })); + } + std::for_each(cpu_threads.begin(), cpu_threads.end(), [](std::thread &t) { t.join(); }); +} diff --git a/HST-S/baselines/gpu/kernel.cu b/HST-S/baselines/gpu/kernel.cu new file mode 100644 index 0000000..fdbb2c6 --- /dev/null +++ b/HST-S/baselines/gpu/kernel.cu @@ -0,0 +1,113 @@ +/* + * 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. + * + */ + +#define _CUDA_COMPILER_ + +#include "support/common.h" +#include "support/partitioner.h" + +// CUDA kernel ------------------------------------------------------------------------------------------ +__global__ void Histogram_kernel(int size, int bins, int n_tasks, float alpha, unsigned int *data, + unsigned int *histo +#ifdef CUDA_8_0 + , int *worklist +#endif + ) { + + extern __shared__ unsigned int l_mem[]; + unsigned int* l_histo = l_mem; +#ifdef CUDA_8_0 + int* l_tmp = (int*)&l_histo[bins]; +#endif + +#ifdef CUDA_8_0 + Partitioner p = partitioner_create(n_tasks, alpha, worklist, l_tmp); +#else + Partitioner p = partitioner_create(n_tasks, alpha); +#endif + + // Block and thread index + const int bx = blockIdx.x; + const int tx = threadIdx.x; + const int bD = blockDim.x; + const int gD = gridDim.x; + + // Sub-histograms initialization + for(int pos = tx; pos < bins; pos += bD) { + l_histo[pos] = 0; + } + + __syncthreads(); // Intra-block synchronization + + // Main loop + for(int i = gpu_first(&p); gpu_more(&p); i = gpu_next(&p)) { + + // Global memory read + unsigned int d = data[i * bD + tx]; + + // Atomic vote in shared memory + atomicAdd(&l_histo[((d * bins) >> 12)], 1); + } + + __syncthreads(); // Intra-block synchronization + + // Merge per-block histograms and write to global memory + for(int pos = tx; pos < bins; pos += bD) { +// Atomic addition in global memory +#ifdef CUDA_8_0 + atomicAdd_system(histo + pos, l_histo[pos]); +#else + atomicAdd(histo + pos, l_histo[pos]); +#endif + } +} + +cudaError_t call_Histogram_kernel(int blocks, int threads, int size, int bins, int n_tasks, float alpha, + unsigned int *data, unsigned int *histo, int l_mem_size +#ifdef CUDA_8_0 + , int* worklist +#endif + ){ + dim3 dimGrid(blocks); + dim3 dimBlock(threads); + Histogram_kernel<<>>(size, bins, n_tasks, alpha, + data, histo +#ifdef CUDA_8_0 + , worklist +#endif + ); + cudaError_t err = cudaGetLastError(); + return err; +} diff --git a/HST-S/baselines/gpu/kernel.h b/HST-S/baselines/gpu/kernel.h new file mode 100644 index 0000000..525d372 --- /dev/null +++ b/HST-S/baselines/gpu/kernel.h @@ -0,0 +1,51 @@ +/* + * 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 +#include +#include "support/common.h" + +void run_cpu_threads(std::atomic_uint *histo, unsigned int *data, int size, int bins, int num_threads, int chunk, int n_tasks, float alpha +#ifdef CUDA_8_0 + , std::atomic_int *wl +#endif + ); + +cudaError_t call_Histogram_kernel(int blocks, int threads, int size, int bins, int n_tasks, float alpha, + unsigned int *data, unsigned int *histo, int l_mem_size +#ifdef CUDA_8_0 + , int* worklist +#endif + ); diff --git a/HST-S/baselines/gpu/main.cpp b/HST-S/baselines/gpu/main.cpp new file mode 100644 index 0000000..e0b5dfa --- /dev/null +++ b/HST-S/baselines/gpu/main.cpp @@ -0,0 +1,322 @@ +/* + * 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 "support/cuda-setup.h" +#include "kernel.h" +#include "support/common.h" +#include "support/timer.h" +#include "support/verify.h" + +#include +#include +#include + +// Params --------------------------------------------------------------------- +struct Params { + + int device; + int n_gpu_threads; + int n_gpu_blocks; + int n_threads; + int n_warmup; + int n_reps; + float alpha; + int in_size; + int n_bins; + + Params(int argc, char **argv) { + device = 0; + n_gpu_threads = 256; + n_gpu_blocks = 16; + n_threads = 4; + n_warmup = 5; + n_reps = 50; + alpha = 0.2; + in_size = 1536 * 1024 * 640; + n_bins = 256; + int opt; + while((opt = getopt(argc, argv, "hd:i:g:t:w:r:a:n:b:")) >= 0) { + switch(opt) { + case 'h': + usage(); + exit(0); + break; + case 'd': device = atoi(optarg); break; + case 'i': n_gpu_threads = atoi(optarg); break; + case 'g': n_gpu_blocks = atoi(optarg); break; + case 't': n_threads = atoi(optarg); break; + case 'w': n_warmup = atoi(optarg); break; + case 'r': n_reps = atoi(optarg); break; + case 'a': alpha = atof(optarg); break; + case 'n': in_size = atoi(optarg); break; + case 'b': n_bins = atoi(optarg); break; + default: + fprintf(stderr, "\nUnrecognized option!\n"); + usage(); + exit(0); + } + } + if(alpha == 0.0) { + assert(n_gpu_threads > 0 && "Invalid # of device threads!"); + assert(n_gpu_blocks > 0 && "Invalid # of device blocks!"); + } else if(alpha == 1.0) { + assert(n_threads > 0 && "Invalid # of host threads!"); + } else if(alpha > 0.0 && alpha < 1.0) { + assert(n_gpu_threads > 0 && "Invalid # of device threads!"); + assert(n_gpu_blocks > 0 && "Invalid # of device blocks!"); + assert(n_threads > 0 && "Invalid # of host threads!"); + } else { +#ifdef CUDA_8_0 + assert((n_gpu_threads > 0 && n_gpu_blocks > 0 || n_threads > 0) && "Invalid # of host + device workers!"); +#else + assert(0 && "Illegal value for -a"); +#endif + } + } + + void usage() { + fprintf(stderr, + "\nUsage: ./hsti [options]" + "\n" + "\nGeneral options:" + "\n -h help" + "\n -d CUDA device ID (default=0)" + "\n -i # of device threads per block (default=256)" + "\n -g # of device blocks (default=16)" + "\n -t # of host threads (default=4)" + "\n -w # of untimed warmup iterations (default=5)" + "\n -r # of timed repetition iterations (default=50)" + "\n" + "\nData-partitioning-specific options:" + "\n -a fraction of input elements to process on host (default=0.2)" +#ifdef CUDA_8_0 + "\n NOTE: Dynamic partitioning used when is not between 0.0 and 1.0" +#else + "\n NOTE: must be between 0.0 and 1.0" +#endif + "\n" + "\nBenchmark-specific options:" + "\n -n input size (default=1572864, i.e., 1536x1024)" + "\n -b # of bins in histogram (default=256)" + "\n"); + } +}; + +// Input Data ----------------------------------------------------------------- +void read_input(unsigned int *input, const Params &p) { + + char dctFileName[100]; + FILE *File = NULL; + + // Open input file + unsigned short temp; + sprintf(dctFileName, "./input/image_VanHateren.iml"); + if((File = fopen(dctFileName, "rb")) != NULL) { + for(int y = 0; y < p.in_size; y++) { + int fr = fread(&temp, sizeof(unsigned short), 1, File); + input[y] = (unsigned int)ByteSwap16(temp); + if(input[y] >= 4096) + input[y] = 4095; + } + fclose(File); + } else { + printf("%s does not exist\n", dctFileName); + exit(1); + } +} + +// Main ------------------------------------------------------------------------------------------ +int main(int argc, char **argv) { + + Params p(argc, argv); + CUDASetup setcuda(p.device); + Timer timer; + cudaError_t cudaStatus; + + // Allocate buffers + timer.start("Allocation"); + int n_tasks = divceil(p.in_size, p.n_gpu_threads); +#ifdef CUDA_8_0 + unsigned int *h_in; + cudaStatus = cudaMallocManaged(&h_in, p.in_size * sizeof(unsigned int)); + std::atomic_uint *h_histo; + cudaStatus = cudaMallocManaged(&h_histo, p.n_bins * sizeof(std::atomic_uint)); + unsigned int * d_in = h_in; + std::atomic_uint *d_histo = h_histo; + std::atomic_int * worklist; + cudaStatus = cudaMallocManaged(&worklist, sizeof(std::atomic_int)); +#else + unsigned int * h_in = (unsigned int *)malloc(p.in_size * sizeof(unsigned int)); + std::atomic_uint *h_histo = (std::atomic_uint *)malloc(p.n_bins * sizeof(std::atomic_uint)); + unsigned int * h_histo_merge = (unsigned int *)malloc(p.n_bins * sizeof(unsigned int)); + unsigned int * d_in; + cudaStatus = cudaMalloc((void**)&d_in, p.in_size * sizeof(unsigned int)); + unsigned int * d_histo; + cudaStatus = cudaMalloc((void**)&d_histo, p.n_bins * sizeof(unsigned int)); + ALLOC_ERR(h_in, h_histo, h_histo_merge); +#endif + CUDA_ERR(); + cudaDeviceSynchronize(); + timer.stop("Allocation"); + timer.print("Allocation", 1); + + // Initialize + timer.start("Initialization"); + const int max_gpu_threads = setcuda.max_gpu_threads(); + read_input(h_in, p); +#ifdef CUDA_8_0 + for(int i = 0; i < p.n_bins; i++) { + h_histo[i].store(0); + } +#else + memset(h_histo, 0, p.n_bins * sizeof(unsigned int)); +#endif + cudaDeviceSynchronize(); + timer.stop("Initialization"); + timer.print("Initialization", 1); + +#ifndef CUDA_8_0 + // Copy to device + timer.start("Copy To Device"); + cudaStatus = cudaMemcpy(d_in, h_in, p.in_size * sizeof(unsigned int), cudaMemcpyHostToDevice); + cudaStatus = cudaMemcpy(d_histo, h_histo, p.n_bins * sizeof(unsigned int), cudaMemcpyHostToDevice); + cudaDeviceSynchronize(); + CUDA_ERR(); + timer.stop("Copy To Device"); + timer.print("Copy To Device", 1); +#endif + + // Loop over main kernel + for(int rep = 0; rep < p.n_warmup + p.n_reps; rep++) { + + // Reset +#ifdef CUDA_8_0 + if(p.alpha < 0.0 || p.alpha > 1.0) { // Dynamic partitioning + worklist[0].store(0); + } + for(int i = 0; i < p.n_bins; i++) { + h_histo[i].store(0); + } +#else + memset(h_histo, 0, p.n_bins * sizeof(unsigned int)); + cudaStatus = cudaMemcpy(d_histo, h_histo, p.n_bins * sizeof(unsigned int), cudaMemcpyHostToDevice); + cudaDeviceSynchronize(); + CUDA_ERR(); +#endif + + if(rep >= p.n_warmup) + timer.start("Kernel"); + + p.n_gpu_blocks = p.in_size / p.n_gpu_threads; + + // Launch GPU threads + // Kernel launch + if(p.n_gpu_blocks > 0) { + assert(p.n_gpu_threads <= max_gpu_threads && + "The thread block size is greater than the maximum thread block size that can be used on this device"); + cudaStatus = call_Histogram_kernel(p.n_gpu_blocks, p.n_gpu_threads, p.in_size, p.n_bins, n_tasks, + p.alpha, d_in, (unsigned int*)d_histo, p.n_bins * sizeof(unsigned int) +#ifdef CUDA_8_0 + + sizeof(int), (int*)worklist +#endif + ); + CUDA_ERR(); + } + + // Launch CPU threads + std::thread main_thread(run_cpu_threads, h_histo, h_in, p.in_size, p.n_bins, p.n_threads, p.n_gpu_threads, + n_tasks, p.alpha +#ifdef CUDA_8_0 + , worklist +#endif + ); + + cudaDeviceSynchronize(); + main_thread.join(); + + if(rep >= p.n_warmup) + timer.stop("Kernel"); + } + timer.print("Kernel", p.n_reps); + +#ifndef CUDA_8_0 + // Copy back + timer.start("Copy Back and Merge"); + cudaStatus = cudaMemcpy(h_histo_merge, d_histo, p.n_bins * sizeof(unsigned int), cudaMemcpyDeviceToHost); + CUDA_ERR(); + cudaDeviceSynchronize(); + for(unsigned int i = 0; i < p.n_bins; ++i) { + h_histo_merge[i] += (unsigned int)h_histo[i]; + } + timer.stop("Copy Back and Merge"); + timer.print("Copy Back and Merge", 1); +#endif + + // Verify answer +#ifdef CUDA_8_0 + verify((unsigned int *)h_histo, h_in, p.in_size, p.n_bins); +#else + verify((unsigned int *)h_histo_merge, h_in, p.in_size, p.n_bins); +#endif + + // Free memory + timer.start("Deallocation"); +#ifdef CUDA_8_0 + cudaStatus = cudaFree(h_in); + cudaStatus = cudaFree(h_histo); + cudaStatus = cudaFree(worklist); +#else + free(h_in); + free(h_histo); + free(h_histo_merge); + cudaStatus = cudaFree(d_in); + cudaStatus = cudaFree(d_histo); +#endif + CUDA_ERR(); + cudaDeviceSynchronize(); + timer.stop("Deallocation"); + timer.print("Deallocation", 1); + + // Release timers + timer.release("Allocation"); + timer.release("Initialization"); + timer.release("Copy To Device"); + timer.release("Kernel"); + timer.release("Copy Back and Merge"); + timer.release("Deallocation"); + + printf("Test Passed\n"); + return 0; +} diff --git a/HST-S/baselines/gpu/support/common.h b/HST-S/baselines/gpu/support/common.h new file mode 100644 index 0000000..2383eff --- /dev/null +++ b/HST-S/baselines/gpu/support/common.h @@ -0,0 +1,45 @@ +/* + * 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. + * + */ + +#ifndef _COMMON_H_ +#define _COMMON_H_ + +#define ByteSwap16(n) (((((unsigned int)n) << 8) & 0xFF00) | ((((unsigned int)n) >> 8) & 0x00FF)) + +#define PRINT 0 + +#define divceil(n, m) (((n)-1) / (m) + 1) + +#endif diff --git a/HST-S/baselines/gpu/support/cuda-setup.h b/HST-S/baselines/gpu/support/cuda-setup.h new file mode 100644 index 0000000..7b7eefe --- /dev/null +++ b/HST-S/baselines/gpu/support/cuda-setup.h @@ -0,0 +1,78 @@ +/* + * 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 +#include +#include + +// Allocation error checking +#define ERR_1(v1) \ + if(v1 == NULL) { \ + fprintf(stderr, "Allocation error at %s, %d\n", __FILE__, __LINE__); \ + exit(-1); \ + } +#define ERR_2(v1,v2) ERR_1(v1) ERR_1(v2) +#define ERR_3(v1,v2,v3) ERR_2(v1,v2) ERR_1(v3) +#define ERR_4(v1,v2,v3,v4) ERR_3(v1,v2,v3) ERR_1(v4) +#define ERR_5(v1,v2,v3,v4,v5) ERR_4(v1,v2,v3,v4) ERR_1(v5) +#define ERR_6(v1,v2,v3,v4,v5,v6) ERR_5(v1,v2,v3,v4,v5) ERR_1(v6) +#define GET_ERR_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME +#define ALLOC_ERR(...) GET_ERR_MACRO(__VA_ARGS__,ERR_6,ERR_5,ERR_4,ERR_3,ERR_2,ERR_1)(__VA_ARGS__) + +#define CUDA_ERR() \ + if(cudaStatus != cudaSuccess) { \ + fprintf(stderr, "CUDA error: %s\n at %s, %d\n", cudaGetErrorString(cudaStatus), __FILE__, __LINE__); \ + exit(-1); \ + } + +struct CUDASetup { + + cudaDeviceProp device_prop; + + CUDASetup(int device) { + cudaError_t cudaStatus; + cudaStatus = cudaSetDevice(device); + CUDA_ERR(); + + cudaStatus = cudaGetDeviceProperties(&device_prop, device); + CUDA_ERR(); + fprintf(stderr, "%s\t", device_prop.name); + + } + + int max_gpu_threads() { + return device_prop.maxThreadsPerBlock; + } +}; diff --git a/HST-S/baselines/gpu/support/partitioner.h b/HST-S/baselines/gpu/support/partitioner.h new file mode 100644 index 0000000..61dbe87 --- /dev/null +++ b/HST-S/baselines/gpu/support/partitioner.h @@ -0,0 +1,213 @@ +/* + * 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. + * + */ + +#ifndef _PARTITIONER_H_ +#define _PARTITIONER_H_ + +#ifndef _CUDA_COMPILER_ +#include +#endif + +#if !defined(_CUDA_COMPILER_) && defined(CUDA_8_0) +#include +#endif + +// Partitioner definition ----------------------------------------------------- + +typedef struct Partitioner { + + int n_tasks; + int cut; + int current; +#ifndef _CUDA_COMPILER_ + int thread_id; + int n_threads; +#endif + + +#ifdef CUDA_8_0 + // CUDA 8.0 support for dynamic partitioning + int strategy; +#ifdef _CUDA_COMPILER_ + int *worklist; + int *tmp; +#else + std::atomic_int *worklist; +#endif +#endif + +} Partitioner; + +// Partitioning strategies +#define STATIC_PARTITIONING 0 +#define DYNAMIC_PARTITIONING 1 + +// Create a partitioner ------------------------------------------------------- + +#ifdef _CUDA_COMPILER_ +__device__ +#endif +inline Partitioner partitioner_create(int n_tasks, float alpha +#ifndef _CUDA_COMPILER_ + , int thread_id, int n_threads +#endif +#ifdef CUDA_8_0 +#ifdef _CUDA_COMPILER_ + , int *worklist + , int *tmp +#else + , std::atomic_int *worklist +#endif +#endif + ) { + Partitioner p; + p.n_tasks = n_tasks; +#ifndef _CUDA_COMPILER_ + p.thread_id = thread_id; + p.n_threads = n_threads; +#endif + if(alpha >= 0.0 && alpha <= 1.0) { + p.cut = p.n_tasks * alpha; +#ifdef CUDA_8_0 + p.strategy = STATIC_PARTITIONING; +#endif + } else { +#ifdef CUDA_8_0 + p.strategy = DYNAMIC_PARTITIONING; + p.worklist = worklist; +#ifdef _CUDA_COMPILER_ + p.tmp = tmp; +#endif +#endif + } + return p; +} + +// Partitioner iterators: first() --------------------------------------------- + +#ifndef _CUDA_COMPILER_ + +inline int cpu_first(Partitioner *p) { +#ifdef CUDA_8_0 + if(p->strategy == DYNAMIC_PARTITIONING) { + p->current = p->worklist->fetch_add(1); + } else +#endif + { + p->current = p->thread_id; + } + return p->current; +} + +#else + +__device__ inline int gpu_first(Partitioner *p) { +#ifdef CUDA_8_0 + if(p->strategy == DYNAMIC_PARTITIONING) { + if(threadIdx.y == 0 && threadIdx.x == 0) { + p->tmp[0] = atomicAdd_system(p->worklist, 1); + } + __syncthreads(); + p->current = p->tmp[0]; + } else +#endif + { + p->current = p->cut + blockIdx.x; + } + return p->current; +} + +#endif + +// Partitioner iterators: more() ---------------------------------------------- + +#ifndef _CUDA_COMPILER_ + +inline bool cpu_more(const Partitioner *p) { +#ifdef CUDA_8_0 + if(p->strategy == DYNAMIC_PARTITIONING) { + return (p->current < p->n_tasks); + } else +#endif + { + return (p->current < p->cut); + } +} + +#else + +__device__ inline bool gpu_more(const Partitioner *p) { + return (p->current < p->n_tasks); +} + +#endif + +// Partitioner iterators: next() ---------------------------------------------- + +#ifndef _CUDA_COMPILER_ + +inline int cpu_next(Partitioner *p) { +#ifdef CUDA_8_0 + if(p->strategy == DYNAMIC_PARTITIONING) { + p->current = p->worklist->fetch_add(1); + } else +#endif + { + p->current = p->current + p->n_threads; + } + return p->current; +} + +#else + +__device__ inline int gpu_next(Partitioner *p) { +#ifdef CUDA_8_0 + if(p->strategy == DYNAMIC_PARTITIONING) { + if(threadIdx.y == 0 && threadIdx.x == 0) { + p->tmp[0] = atomicAdd_system(p->worklist, 1); + } + __syncthreads(); + p->current = p->tmp[0]; + } else +#endif + { + p->current = p->current + gridDim.x; + } + return p->current; +} + +#endif + +#endif diff --git a/HST-S/baselines/gpu/support/timer.h b/HST-S/baselines/gpu/support/timer.h new file mode 100644 index 0000000..fceab04 --- /dev/null +++ b/HST-S/baselines/gpu/support/timer.h @@ -0,0 +1,73 @@ +/* + * 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 +#include +#include +#include +#include + +using namespace std; + +struct Timer { + + map startTime; + map stopTime; + map time; + + void start(string name) { + if(!time.count(name)) { + cudaEventCreate(&startTime[name]); + cudaEventCreate(&stopTime[name]); + time[name] = 0.0; + } + cudaEventRecord(startTime[name], 0); + } + + void stop(string name) { + cudaEventRecord(stopTime[name],0); + cudaEventSynchronize(stopTime[name]); + float part_time = 0.0; + cudaEventElapsedTime(&part_time, startTime[name], stopTime[name]); + time[name] += part_time; + } + + void print(string name, unsigned int REP) { printf("%s Time (ms): %f\n", name.c_str(), time[name] / REP); } + + void release(string name){ + cudaEventDestroy(startTime[name]); + cudaEventDestroy(stopTime[name]); + } +}; diff --git a/HST-S/baselines/gpu/support/verify.h b/HST-S/baselines/gpu/support/verify.h new file mode 100644 index 0000000..9cb9e53 --- /dev/null +++ b/HST-S/baselines/gpu/support/verify.h @@ -0,0 +1,66 @@ +/* + * 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 "common.h" +#include +#include + +inline int compare_output(unsigned int *outp, unsigned int *outpCPU, int bins) { + for(int i = 0; i < bins; i++) { + if(outp[i] != outpCPU[i]) { + printf("Test failed\n"); + exit(EXIT_FAILURE); + } + } + return 0; +} + +// Sequential implementation for comparison purposes +inline void HistogramCPU(unsigned int *histo, unsigned int *data, int size, int bins) { + for(int i = 0; i < size; i++) { + // Read pixel + unsigned int d = ((data[i] * bins) >> 12); + // Vote in histogram + histo[d]++; + } +} + +inline void verify(unsigned int *histo, unsigned int *input, int size, int bins) { + unsigned int *gold = (unsigned int *)malloc(bins * sizeof(unsigned int)); + memset(gold, 0, bins * sizeof(unsigned int)); + HistogramCPU(gold, input, size, bins); + compare_output(histo, gold, bins); + free(gold); +} diff --git a/HST-S/dpu/task.c b/HST-S/dpu/task.c new file mode 100644 index 0000000..135f0d1 --- /dev/null +++ b/HST-S/dpu/task.c @@ -0,0 +1,116 @@ +/* +* Histogram (HST-S) with multiple tasklets +* +*/ +#include +#include +#include +#include +#include +#include +#include + +#include "../support/common.h" + +__host dpu_arguments_t DPU_INPUT_ARGUMENTS; + +// Array for communication between adjacent tasklets +uint32_t* message[NR_TASKLETS]; +// DPU histogram +uint32_t* histo_dpu; + +// Barrier +BARRIER_INIT(my_barrier, NR_TASKLETS); + +// Histogram in each tasklet +static void histogram(uint32_t* histo, uint32_t bins, T *input, unsigned int l_size){ + for(unsigned int j = 0; j < l_size; j++) { + T d = input[j]; + histo[(d * bins) >> DEPTH] += 1; + } +} + +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 + } + // Barrier + barrier_wait(&my_barrier); + + uint32_t input_size_dpu_bytes = DPU_INPUT_ARGUMENTS.size; + uint32_t input_size_dpu_bytes_transfer = DPU_INPUT_ARGUMENTS.transfer_size; // Transfer input size per DPU in bytes + uint32_t bins = DPU_INPUT_ARGUMENTS.bins; + + // 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; + uint32_t mram_base_addr_histo = (uint32_t)(DPU_MRAM_HEAP_POINTER + input_size_dpu_bytes_transfer); + + // Initialize a local cache to store the MRAM block + T *cache_A = (T *) mem_alloc(BLOCK_SIZE); + + // Local histogram + uint32_t *histo = (uint32_t *) mem_alloc(bins * sizeof(uint32_t)); + + // Initialize local histogram + for(unsigned int i = 0; i < bins; i++){ + histo[i] = 0; + } + + // Compute histogram + 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((const __mram_ptr void*)(mram_base_addr_A + byte_index), cache_A, l_size_bytes); + + // Histogram in each tasklet + histogram(histo, bins, cache_A, l_size_bytes >> DIV); + + } + message[tasklet_id] = histo; + + // Barrier + barrier_wait(&my_barrier); + + uint32_t *histo_dpu = message[0]; + + for (unsigned int i = tasklet_id; i < bins; i += NR_TASKLETS){ + uint32_t b = 0; + for (unsigned int j = 0; j < NR_TASKLETS; j++){ + b += *(message[j] + i); + } + histo_dpu[i] = b; + } + + // Barrier + barrier_wait(&my_barrier); + + // Write dpu histogram to current MRAM block + if(tasklet_id == 0){ + if(bins * sizeof(uint32_t) <= 2048) + mram_write(histo_dpu, (__mram_ptr void*)(mram_base_addr_histo), bins * sizeof(uint32_t)); + else + for(unsigned int offset = 0; offset < ((bins * sizeof(uint32_t)) >> 11); offset++){ + mram_write(histo_dpu + (offset << 9), (__mram_ptr void*)(mram_base_addr_histo + (offset << 11)), 2048); + } + } + + return 0; +} diff --git a/HST-S/host/app.c b/HST-S/host/app.c new file mode 100644 index 0000000..e50fd62 --- /dev/null +++ b/HST-S/host/app.c @@ -0,0 +1,285 @@ +/** +* app.c +* HST-S Host Application Source File +* +*/ +#include +#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 unsigned int* histo_host; +static unsigned int* histo; + +// Create input arrays +static void read_input(T* A, const Params p) { + + char dctFileName[100]; + FILE *File = NULL; + + // Open input file + unsigned short temp; + sprintf(dctFileName, p.file_name); + if((File = fopen(dctFileName, "rb")) != NULL) { + for(unsigned int y = 0; y < p.input_size; y++) { + fread(&temp, sizeof(unsigned short), 1, File); + A[y] = (unsigned int)ByteSwap16(temp); + if(A[y] >= 4096) + A[y] = 4095; + } + fclose(File); + } else { + printf("%s does not exist\n", dctFileName); + exit(1); + } +} + +// Compute output in the host +static void histogram_host(unsigned int* histo, T* A, unsigned int bins, unsigned int nr_elements, int exp, unsigned int nr_of_dpus) { + if(!exp){ + for (unsigned int i = 0; i < nr_of_dpus; i++) { + for (unsigned int j = 0; j < nr_elements; j++) { + T d = A[j]; + histo[i * bins + ((d * bins) >> DEPTH)] += 1; + } + } + } + else{ + for (unsigned int j = 0; j < nr_elements; j++) { + T d = A[j]; + histo[(d * bins) >> DEPTH] += 1; + } + } +} + +// 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; + unsigned int input_size; // Size of input image + unsigned int dpu_s = p.dpu_s; + if(p.exp == 0) + input_size = p.input_size * nr_of_dpus; // Size of input image + else if(p.exp == 1) + input_size = p.input_size; // Size of input image + else + input_size = p.input_size * dpu_s; // Size of input image + + 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; + histo_host = malloc(p.bins * sizeof(unsigned int)); + histo = malloc(nr_of_dpus * p.bins * sizeof(unsigned int)); + + // Create an input file with arbitrary data + read_input(A, p); + if(p.exp == 0){ + for(unsigned int j = 1; j < nr_of_dpus; j++){ + memcpy(&A[j * input_size_dpu_8bytes], &A[0], input_size_dpu_8bytes * sizeof(T)); + } + } + else if(p.exp == 2){ + for(unsigned int j = 1; j < dpu_s; j++) + memcpy(&A[j * p.input_size], &A[0], p.input_size * sizeof(T)); + } + + // Timer declaration + Timer timer; + + printf("NR_TASKLETS\t%d\tBL\t%d\tinput_size\t%u\n", NR_TASKLETS, BL, input_size); + + // Loop over main kernel + for(int rep = 0; rep < p.n_warmup + p.n_reps; rep++) { + memset(histo_host, 0, p.bins * sizeof(unsigned int)); + memset(histo, 0, nr_of_dpus * p.bins * sizeof(unsigned int)); + + // Compute output on CPU (performance comparison and verification purposes) + if(rep >= p.n_warmup) + start(&timer, 0, rep - p.n_warmup); + histogram_host(histo_host, A, p.bins, p.input_size, 1, nr_of_dpus); + 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; + i = 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"); + i = 0; + if(rep >= p.n_warmup) + start(&timer, 3, rep - p.n_warmup); + // PARALLEL RETRIEVE TRANSFER + DPU_FOREACH(dpu_set, dpu, i) { + DPU_ASSERT(dpu_prepare_xfer(dpu, histo + p.bins * i)); + } + DPU_ASSERT(dpu_push_xfer(dpu_set, DPU_XFER_FROM_DPU, DPU_MRAM_HEAP_POINTER_NAME, input_size_dpu_8bytes * sizeof(T), p.bins * sizeof(unsigned int), DPU_XFER_DEFAULT)); + + // Final histogram merging + for(i = 1; i < nr_of_dpus; i++){ + for(unsigned int j = 0; j < p.bins; j++){ + histo[j] += histo[j + i * p.bins]; + } + } + 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; + if(p.exp == 1) + for (unsigned int j = 0; j < p.bins; j++) { + if(histo_host[j] != histo[j]){ + status = false; +#if PRINT + printf("%u - %u: %u -- %u\n", j, j, histo_host[j], histo[j]); +#endif + } + } + else if(p.exp == 2) + for (unsigned int j = 0; j < p.bins; j++) { + if(dpu_s * histo_host[j] != histo[j]){ + status = false; +#if PRINT + printf("%u - %u: %u -- %u\n", j, j, dpu_s * histo_host[j], histo[j]); +#endif + } + } + else + for (unsigned int j = 0; j < p.bins; j++) { + if(nr_of_dpus * histo_host[j] != histo[j]){ + status = false; +#if PRINT + printf("%u - %u: %u -- %u\n", j, j, nr_of_dpus * histo_host[j], histo[j]); +#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(histo_host); + free(histo); + DPU_ASSERT(dpu_free(dpu_set)); + + return status ? 0 : -1; +} diff --git a/HST-S/input/image_VanHateren.iml b/HST-S/input/image_VanHateren.iml new file mode 100644 index 0000000..1ae6047 Binary files /dev/null and b/HST-S/input/image_VanHateren.iml differ diff --git a/HST-S/run.sh b/HST-S/run.sh new file mode 100755 index 0000000..0eba853 --- /dev/null +++ b/HST-S/run.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +for i in 1 +do + for b in 64 128 256 512 1024 2048 4096 + do + for k in 1 2 4 8 16 + do + NR_DPUS=$i NR_TASKLETS=$k BL=10 make all + wait + ./bin/host_code -w 2 -e 5 -b ${b} -x 1 > profile/HSTS_${b}_tl${k}_dpu${i}.txt + wait + make clean + wait + done + done +done diff --git a/HST-S/support/common.h b/HST-S/support/common.h new file mode 100755 index 0000000..30df40d --- /dev/null +++ b/HST-S/support/common.h @@ -0,0 +1,45 @@ +#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 +#define T uint32_t +#define DIV 2 // Shift right to divide by sizeof(T) +#define REGS (BLOCK_SIZE >> 2) // 32 bits + +// Pixel depth +#define DEPTH 12 +#define ByteSwap16(n) (((((unsigned int)n) << 8) & 0xFF00) | ((((unsigned int)n) >> 8) & 0x00FF)) + +// Structures used by both the host and the dpu to communicate information +typedef struct { + uint32_t size; + uint32_t transfer_size; + uint32_t bins; + enum kernels { + kernel1 = 0, + nr_kernels = 1, + } kernel; +} dpu_arguments_t; + +#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/HST-S/support/params.h b/HST-S/support/params.h new file mode 100644 index 0000000..e29449b --- /dev/null +++ b/HST-S/support/params.h @@ -0,0 +1,67 @@ +#ifndef _PARAMS_H_ +#define _PARAMS_H_ + +#include "common.h" + +typedef struct Params { + unsigned int input_size; + unsigned int bins; + int n_warmup; + int n_reps; + const char *file_name; + int exp; + int dpu_s; +}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, 2) scaling (default=0)" + "\n" + "\nBenchmark-specific options:" + "\n -i input size (default=1536*1024 elements)" + "\n -b histogram size (default=256 bins)" + "\n -f input image file (default=../input/image_VanHateren.iml)" + "\n"); +} + +struct Params input_params(int argc, char **argv) { + struct Params p; + p.input_size = 1536 * 1024; + p.bins = 256; + p.n_warmup = 1; + p.n_reps = 3; + p.exp = 0; + p.file_name = "./input/image_VanHateren.iml"; + p.dpu_s = 64; + + int opt; + while((opt = getopt(argc, argv, "hi:b:w:e:f:x:z:")) >= 0) { + switch(opt) { + case 'h': + usage(); + exit(0); + break; + case 'i': p.input_size = atoi(optarg); break; + case 'b': p.bins = atoi(optarg); break; + case 'w': p.n_warmup = atoi(optarg); break; + case 'e': p.n_reps = atoi(optarg); break; + case 'f': p.file_name = optarg; break; + case 'x': p.exp = atoi(optarg); break; + case 'z': p.dpu_s = 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/HST-S/support/timer.h b/HST-S/support/timer.h new file mode 100755 index 0000000..eedc385 --- /dev/null +++ b/HST-S/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)); } -- cgit v1.2.3