From 4aeb31bfbd1322e7bb1ce85f2eb9cc25328dbcdb Mon Sep 17 00:00:00 2001 From: Sergey Egorov <sergeyegorov@c02fl93jmd6m.surrey.ac.uk> Date: Mon, 11 Oct 2021 16:07:42 +0100 Subject: [PATCH] Sequential buffer reads and writes. --- CMakeLists.txt | 6 ++ Makefile | 181 +++++++++++++++++++++++++++++++++++++++++++++++++ main.c | 65 ++++++++++++++++++ 3 files changed, 252 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 Makefile create mode 100644 main.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..3341823 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(Pmem_tests C) + +set(CMAKE_C_STANDARD 99) + +add_executable(Pmem_tests main.c) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..84393c6 --- /dev/null +++ b/Makefile @@ -0,0 +1,181 @@ +# CMAKE generated file: DO NOT EDIT! +# Generated by "Unix Makefiles" Generator, CMake Version 3.21 + +# Default target executed when no arguments are given to make. +default_target: all +.PHONY : default_target + +# Allow only one "make -f Makefile2" at a time, but pass parallelism. +.NOTPARALLEL: + +#============================================================================= +# Special targets provided by cmake. + +# Disable implicit rules so canonical targets will work. +.SUFFIXES: + +# Disable VCS-based implicit rules. +% : %,v + +# Disable VCS-based implicit rules. +% : RCS/% + +# Disable VCS-based implicit rules. +% : RCS/%,v + +# Disable VCS-based implicit rules. +% : SCCS/s.% + +# Disable VCS-based implicit rules. +% : s.% + +.SUFFIXES: .hpux_make_needs_suffix_list + +# Command-line flag to silence nested $(MAKE). +$(VERBOSE)MAKESILENT = -s + +#Suppress display of executed commands. +$(VERBOSE).SILENT: + +# A target that is always out of date. +cmake_force: +.PHONY : cmake_force + +#============================================================================= +# Set environment variables for the build. + +# The shell in which to execute make rules. +SHELL = /bin/sh + +# The CMake executable. +CMAKE_COMMAND = /usr/local/Cellar/cmake/3.21.3_1/bin/cmake + +# The command to remove a file. +RM = /usr/local/Cellar/cmake/3.21.3_1/bin/cmake -E rm -f + +# Escaping for special characters. +EQUALS = = + +# The top-level source directory on which CMake was run. +CMAKE_SOURCE_DIR = /Users/sergeyegorov/Desktop/university/Pmem_tests + +# The top-level build directory on which CMake was run. +CMAKE_BINARY_DIR = /Users/sergeyegorov/Desktop/university/Pmem_tests + +#============================================================================= +# Targets provided globally by CMake. + +# Special rule for the target rebuild_cache +rebuild_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..." + /usr/local/Cellar/cmake/3.21.3_1/bin/cmake --regenerate-during-build -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) +.PHONY : rebuild_cache + +# Special rule for the target rebuild_cache +rebuild_cache/fast: rebuild_cache +.PHONY : rebuild_cache/fast + +# Special rule for the target edit_cache +edit_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..." + /usr/local/Cellar/cmake/3.21.3_1/bin/ccmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) +.PHONY : edit_cache + +# Special rule for the target edit_cache +edit_cache/fast: edit_cache +.PHONY : edit_cache/fast + +# The main all target +all: cmake_check_build_system + $(CMAKE_COMMAND) -E cmake_progress_start /Users/sergeyegorov/Desktop/university/Pmem_tests/CMakeFiles /Users/sergeyegorov/Desktop/university/Pmem_tests//CMakeFiles/progress.marks + $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 all + $(CMAKE_COMMAND) -E cmake_progress_start /Users/sergeyegorov/Desktop/university/Pmem_tests/CMakeFiles 0 +.PHONY : all + +# The main clean target +clean: + $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 clean +.PHONY : clean + +# The main clean target +clean/fast: clean +.PHONY : clean/fast + +# Prepare targets for installation. +preinstall: all + $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall +.PHONY : preinstall + +# Prepare targets for installation. +preinstall/fast: + $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall +.PHONY : preinstall/fast + +# clear depends +depend: + $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1 +.PHONY : depend + +#============================================================================= +# Target rules for targets named Pmem_tests + +# Build rule for target. +Pmem_tests: cmake_check_build_system + $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 Pmem_tests +.PHONY : Pmem_tests + +# fast build rule for target. +Pmem_tests/fast: + $(MAKE) $(MAKESILENT) -f CMakeFiles/Pmem_tests.dir/build.make CMakeFiles/Pmem_tests.dir/build +.PHONY : Pmem_tests/fast + +main.o: main.c.o +.PHONY : main.o + +# target to build an object file +main.c.o: + $(MAKE) $(MAKESILENT) -f CMakeFiles/Pmem_tests.dir/build.make CMakeFiles/Pmem_tests.dir/main.c.o +.PHONY : main.c.o + +main.i: main.c.i +.PHONY : main.i + +# target to preprocess a source file +main.c.i: + $(MAKE) $(MAKESILENT) -f CMakeFiles/Pmem_tests.dir/build.make CMakeFiles/Pmem_tests.dir/main.c.i +.PHONY : main.c.i + +main.s: main.c.s +.PHONY : main.s + +# target to generate assembly for a file +main.c.s: + $(MAKE) $(MAKESILENT) -f CMakeFiles/Pmem_tests.dir/build.make CMakeFiles/Pmem_tests.dir/main.c.s +.PHONY : main.c.s + +# Help Target +help: + @echo "The following are some of the valid targets for this Makefile:" + @echo "... all (the default if no target is provided)" + @echo "... clean" + @echo "... depend" + @echo "... edit_cache" + @echo "... rebuild_cache" + @echo "... Pmem_tests" + @echo "... main.o" + @echo "... main.i" + @echo "... main.s" +.PHONY : help + + + +#============================================================================= +# Special targets to cleanup operation of make. + +# Special rule to run CMake to check the build system integrity. +# No rule that depends on this can have commands that come from listfiles +# because they might be regenerated. +cmake_check_build_system: + $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0 +.PHONY : cmake_check_build_system + diff --git a/main.c b/main.c new file mode 100644 index 0000000..63bf7fb --- /dev/null +++ b/main.c @@ -0,0 +1,65 @@ +#include <stdio.h> +#include <time.h> +#include <stdlib.h> + +static const long buffer_size = 100000000; + +struct array{ + char *buffer; +}; +//Sequential read +void iterate_read(const char *buffer){ + for(int i = 0; i < buffer_size; i++ ){ + buffer[i]; + } +} + +//Sequential read +void iterate_read_rand(const char *buffer){ + for(int i = 0; i < buffer_size; i++ ){ + buffer[rand() % buffer_size]; + } +} + +//Sequential write VS Random +void iterate_write(char *buffer){ + for(int i = 0; i < buffer_size; i++ ){ + buffer[i] = 'a'; + } +} +//Random writes +void iterate_write_rand(char *buffer){ + for(int i = 0; i < buffer_size; i++ ){ + buffer[rand() % buffer_size] = 'a'; + } +} + +void execute(struct array *ap, void *r(), void *w()){ + clock_t begin = clock(); + srand(time(NULL)); + w(ap->buffer); + clock_t write_finished = clock(); + r(ap->buffer); + clock_t read_finished = clock(); + + double time_spent_writing = (double)(write_finished - begin) / CLOCKS_PER_SEC; + double time_spent_reading = (double)(read_finished - write_finished) / CLOCKS_PER_SEC; + + printf("Writing: %f, Reading: %f \n", time_spent_writing, time_spent_reading); +} + +int main() { + /** + * Case 1: DRAM only. + */ + //Initialise array on the heap: + struct array *ap = malloc(sizeof (struct array)); + ap->buffer = malloc(sizeof (char)*buffer_size); + printf("Sequential time: "); + execute(ap, iterate_write, iterate_read); + printf("Random time: "); + execute(ap, iterate_write_rand, iterate_read_rand); + + + return 0; +} -- GitLab