Skip to content
Snippets Groups Projects
Commit d23b98d9 authored by Josef Schmeißer's avatar Josef Schmeißer
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
---
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^<ext/.*\.h>'
Priority: 2
- Regex: '^<.*\.h>'
Priority: 1
- Regex: '^<.*'
Priority: 2
- Regex: '.*'
Priority: 3
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
RawStringFormats:
- Delimiter: pb
Language: TextProto
BasedOnStyle: google
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
TabWidth: 8
UseTab: Never
...
.vscode
perf.data*
build*
stages:
- test
test:
stage: test
script:
- mkdir -p build/debug
- cd build/debug
- cmake -DCMAKE_BUILD_TYPE=Debug ../..
- make -j8
- ./test_all
cache:
key: "$CI_JOB_STAGE-$CI_COMMIT_REF_NAME"
paths:
- build/debug
policy: pull-push
tags:
- "fdedi"
clear_cache_test:
stage: test
script:
- rm -rf ./build/*
cache:
key: "$CI_JOB_STAGE-$CI_COMMIT_REF_NAME"
paths:
- build/
policy: pull-push
when: manual
allow_failure: true
cmake_minimum_required(VERSION 3.5)
project(tpchjoinoptimized C CXX)
set(CMAKE_C_COMPILER gcc)
set(CMAKE_CXX_COMPILER g++)
set(CMAKE_CXX_STANDARD 17)
# Compiler flags for the different targets
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fPIC -Wall -Wextra -fno-omit-frame-pointer -march=native -Wno-unknown-pragmas")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fPIC -Wall -Wextra -fno-omit-frame-pointer -march=native")
find_package(Threads)
include(lib/gtest.cmake)
### Library for join query
add_library(sum src/sum.cpp)
target_include_directories(sum PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE src)
### Tests
add_executable(test_all test/test_sum.cpp)
target_link_libraries(test_all sum gtest gtest_main pthread)
### main executable
add_executable(main src/main.cpp)
target_link_libraries(main sum)
# FDE Exercise 3.4
## Task
Implement the missing code fragments in src/main.cpp and src/sum.cpp.
See the corresponding header include/sum.hpp for documentation.
You can use the test provided in test/test_sum.cpp to check if your implementation works
correctly.
## Build
A configuration file is provided to build this project with CMake.
This allows you to build the project in the terminal but also
provides the option to use Jetbrains CLion or Microsoft Visual Studio
and other IDEs.
Building from Terminal:
Start in the project directory.
```
mkdir -p build/debug
cd build/debug
cmake -DCMAKE_BUILD_TYPE=Debug ../..
make
```
This creates the binaries test_all and main.
Make sure your builds are not failing! <br/>
*Left Sidebar > CI /CD > Pipelines*
#pragma once
#include <cassert>
#include <string>
#include <string_view>
// pattern : the character to search broadcasted into a 64bit integer
// begin : points somewhere into the partition
// len : remaining length of the partition
// return : the position of the first matching character or -1 otherwise
ssize_t find_first(uint64_t pattern, const char* begin, size_t len);
// begin : points somewhere into the partition
// len : remaining length of the partition
// return : 64-bit integer representation of the provided numeric
int64_t read_numeric(const char* begin, size_t len);
// data_start : pointer to the first byte of the file
// partition_start: start of partition
// partition_size_hint : size of partition in bytes (must be corrected to end on
// a newline character).
// result : pointer to the variable where the result should be stored in
template <bool first_partition = false>
void sum_extendedprice(const char* data_start, const char* partition_start,
size_t partition_size_hint, int64_t* result) {
int64_t sum = 0;
// correct partition size
if (!first_partition) {
size_t offset = 0;
const size_t max_offset = partition_start - data_start;
while (offset < partition_size_hint) {
if (offset > max_offset) {
return;
} else if (*(partition_start - offset) == '\n') {
break;
} else {
offset++;
}
}
partition_start -= offset;
partition_size_hint += offset;
}
constexpr uint64_t bar_pattern = 0x7C7C7C7C7C7C7C7Cull;
constexpr uint64_t newline_pattern = 0x0A0A0A0A0A0A0A0Aull;
size_t i = 0;
int64_t bar_cnt = 0;
// for each line
while (i < partition_size_hint) {
auto pos = find_first(bar_pattern, partition_start + i,
partition_size_hint - i);
bar_cnt += (pos >= 0);
if (bar_cnt == 5) {
auto bar_pos = i + pos + 1;
auto len = find_first(bar_pattern, partition_start + bar_pos,
partition_size_hint - bar_pos);
assert(len >= 1);
int64_t extendedprice =
read_numeric(partition_start + bar_pos, len);
sum += extendedprice;
bar_cnt = 0;
// skip to end of line
i = bar_pos + len + 1;
auto newline_pos = find_first(newline_pattern, partition_start + i,
partition_size_hint - i);
if (newline_pos < 0 || (newline_pos + i) > partition_size_hint) {
// undo
sum -= extendedprice;
break;
}
i += newline_pos + 1;
} else {
i += pos + 1;
}
}
*result = sum;
}
#include <string>
//---------------------------------------------------------------------------
inline std::string getDir(const std::string &file)
/// Returns path to file in 'file'
{
size_t found = file.find_last_of("/\\");
return (file.substr(0, found));
}
//---------------------------------------------------------------------------
include(ExternalProject)
find_package(Git REQUIRED)
# Get benchmark
ExternalProject_Add(
benchmark_src
PREFIX "vendor/benchmark"
GIT_REPOSITORY "https://github.com/google/benchmark.git"
GIT_TAG v1.2.0
TIMEOUT 10
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/vendor/benchmark
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
UPDATE_COMMAND ""
)
# Prepare benchmark
ExternalProject_Get_Property(benchmark_src install_dir)
set(BENCHMARK_INCLUDE_DIR ${install_dir}/include)
set(BENCHMARK_LIBRARY_PATH ${install_dir}/lib/libbenchmark.a)
file(MAKE_DIRECTORY ${BENCHMARK_INCLUDE_DIR})
add_library(benchmark STATIC IMPORTED)
set_property(TARGET benchmark PROPERTY IMPORTED_LOCATION ${BENCHMARK_LIBRARY_PATH})
set_property(TARGET benchmark APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${BENCHMARK_INCLUDE_DIR})
# Dependencies
add_dependencies(benchmark benchmark_src)
include(ExternalProject)
find_package(Git REQUIRED)
find_package(Threads REQUIRED)
# Get googletest
ExternalProject_Add(
googletest
PREFIX "vendor/gtm"
GIT_REPOSITORY "https://github.com/google/googletest.git"
GIT_TAG release-1.8.0
TIMEOUT 10
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
UPDATE_COMMAND ""
)
# Build gtest
ExternalProject_Add(
gtest_src
PREFIX "vendor/gtm"
SOURCE_DIR "vendor/gtm/src/googletest/googletest"
INSTALL_DIR "vendor/gtm/gtest"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/vendor/gtm/gtest
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
DOWNLOAD_COMMAND ""
UPDATE_COMMAND ""
)
# Build gmock
ExternalProject_Add(
gmock_src
PREFIX "vendor/gtm"
SOURCE_DIR "vendor/gtm/src/googletest/googlemock"
INSTALL_DIR "vendor/gtm/gmock"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/vendor/gtm/gmock
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
DOWNLOAD_COMMAND ""
UPDATE_COMMAND ""
)
# Prepare gtest
ExternalProject_Get_Property(gtest_src install_dir)
set(GTEST_INCLUDE_DIR ${install_dir}/include )
set(GTEST_LIBRARY_PATH ${install_dir}/lib/libgtest.a)
file(MAKE_DIRECTORY ${GTEST_INCLUDE_DIR})
add_library(gtest STATIC IMPORTED)
set_property(TARGET gtest PROPERTY IMPORTED_LOCATION ${GTEST_LIBRARY_PATH})
set_property(TARGET gtest APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIR})
add_library(gtest_main STATIC IMPORTED)
set(GTEST_MAIN_LIBRARY_PATH ${install_dir}/lib/libgtest_main.a)
set_property(TARGET gtest_main PROPERTY IMPORTED_LOCATION ${GTEST_MAIN_LIBRARY_PATH})
set_property(TARGET gtest_main APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIR})
# Prepare gmock
ExternalProject_Get_Property(gmock_src install_dir)
set(GMOCK_INCLUDE_DIR ${install_dir}/include)
set(GMOCK_LIBRARY_PATH ${install_dir}/lib/libgmock.a)
file(MAKE_DIRECTORY ${GMOCK_INCLUDE_DIR})
add_library(gmock STATIC IMPORTED)
set_property(TARGET gmock PROPERTY IMPORTED_LOCATION ${GMOCK_LIBRARY_PATH})
set_property(TARGET gmock APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${GMOCK_INCLUDE_DIR})
# Dependencies
add_dependencies(gtest_src googletest)
add_dependencies(gmock_src googletest)
add_dependencies(gtest gtest_src)
add_dependencies(gtest_main gtest_src)
add_dependencies(gmock gmock_src)
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <cassert>
#include <chrono>
#include <iostream>
#include <memory>
#include <numeric>
#include <string>
#include <vector>
#include "sum.hpp"
using namespace std::chrono;
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cout << "Usage: " << argv[0] << " <lineitem.tbl>" << std::endl;
return 1;
}
const auto start = high_resolution_clock::now();
//-- TODO exercise 3.4
// your code goes here
//--
char* data_start = static_cast<char*>(data);
int64_t price_sum;
sum_extendedprice<true>(data_start, data_start, size, &price_sum);
std::cout << price_sum << std::endl;
const auto duration =
duration_cast<milliseconds>(high_resolution_clock::now() - start)
.count();
std::cout << "duration: " << duration << " ms" << std::endl;
//-- TODO exercise 3.4
// cleanup
//--
return 0;
}
#include <cassert>
#include <memory>
#include <numeric>
#include <string>
#include <string_view>
int64_t ToInt(std::string_view s) {
int64_t result = 0;
for (auto c : s) result = result * 10 + (c - '0');
return result;
}
// pattern : the character to search broadcasted into a 64-bit integer
// block : memory block in which to search
// return : 64-bit integer with all the matches as seen in the lecture
inline uint64_t get_matches(uint64_t pattern, uint64_t block) {
//-- TODO exercise 3.4 part 2
// your code goes here
//--
}
// pattern : the character to search broadcasted into a 64bit integer
// begin : points somewhere into the partition
// len : remaining length of the partition
// return : the position of the first matching character or -1 otherwise
ssize_t find_first(uint64_t pattern, const char* begin, size_t len) {
// locate the position of the following character (you'll have to use the
// pattern argument directly in the second part of the exercise)
const char to_search = pattern & 0xff;
// Hint: You may assume that reads from 'begin' within [len, len + 8) yield
// zero
//-- TODO exercise 3.4
// your code goes here
//--
return -1;
}
// begin : points somewhere into the partition
// len : remaining length of the partition
// return : 64-bit integer representation of the provided numeric
int64_t read_numeric(const char* begin, size_t len) {
constexpr uint64_t period_pattern = 0x2E2E2E2E2E2E2E2Eull;
std::string_view numeric_view(begin, len);
ssize_t dot_position = find_first(period_pattern, begin, len);
assert(dot_position > 0);
auto part1 = numeric_view.substr(0, dot_position);
auto part2 = numeric_view.substr(dot_position + 1);
int64_t numeric = ToInt(part1) * 100 + ToInt(part2);
return numeric;
}
This diff is collapsed.
#include <gtest/gtest.h>
#include <cstring>
#include <fstream>
#include "sum.hpp"
#include "util.hpp"
//---------------------------------------------------------------------------
TEST(SumTest, FindFirst)
/// test find_first()
{
constexpr uint64_t bar_pattern = 0x7C7C7C7C7C7C7C7Cull;
auto pos = find_first(bar_pattern, "| ", 1);
ASSERT_EQ(pos, 0);
pos = find_first(bar_pattern, " | ", 4);
ASSERT_EQ(pos, 3);
pos = find_first(bar_pattern, " | ", 8);
ASSERT_EQ(pos, 7);
pos = find_first(bar_pattern, " | ", 12);
ASSERT_EQ(pos, 11);
pos = find_first(bar_pattern, " | ", 16);
ASSERT_EQ(pos, 15);
}
//---------------------------------------------------------------------------
TEST(SumTest, ReadNumeric)
/// test find_first()
{
auto value = read_numeric("29312.32 ", 8);
ASSERT_EQ(value, 2931232);
}
//---------------------------------------------------------------------------
TEST(SumTest, SumExtendedPrice)
/// test sum_extendedprice()
{
auto lineitem_file = getDir(__FILE__) + "/data/tpch/sf0_001/lineitem.tbl";
std::ifstream in(lineitem_file);
std::string contents((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
size_t full_size = contents.size() + 8;
char* buffer = new char[full_size]();
std::unique_ptr<char[]> guard(buffer);
std::memcpy(buffer, contents.c_str(), contents.size());
int64_t result;
sum_extendedprice<true>(buffer, buffer, contents.size(), &result);
ASSERT_EQ(result, 15277439838ul);
}
//---------------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment