From 5bf90aa4e7e5b6127352a494782346caf69386b7 Mon Sep 17 00:00:00 2001 From: KunoiSayami Date: Thu, 21 Oct 2021 01:57:05 +0800 Subject: init Signed-off-by: KunoiSayami --- .gitignore | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 9 ++++ arena.cu | 68 +++++++++++++++++++++++++++ arena.cuh | 68 +++++++++++++++++++++++++++ main.cu | 6 +++ 5 files changed, 293 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 arena.cu create mode 100644 arena.cuh create mode 100644 main.cu diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f2623c7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,142 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +.idea/ + +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app +out + +cmake-build-*/ + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..f1c2883 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.20) +project(cleveldb CUDA) + +set(CMAKE_CUDA_STANDARD 14) + +add_executable(cleveldb main.cu arena.cu arena.cuh) + +set_target_properties(cleveldb PROPERTIES + CUDA_SEPARABLE_COMPILATION ON) diff --git a/arena.cu b/arena.cu new file mode 100644 index 0000000..1bb0f91 --- /dev/null +++ b/arena.cu @@ -0,0 +1,68 @@ +// +// Created by user on 20/10/2021. +// + +#include "arena.cuh" + +namespace cleveldb { + +static const int kBlockSize = 4096; +Arena::Arena() + : alloc_ptr_(nullptr), alloc_bytes_remaining_(0), memory_usage_(0) {} + +Arena::~Arena() { + for (size_t i = 0; i < blocks_.size(); i++) { + //delete[] blocks_[i]; + cudaFree(blocks_[i]); + } +} + +char* Arena::AllocateFallback(size_t bytes) { + if (bytes > kBlockSize / 4) { + // Object is more than a quarter of our block size. Allocate it separately + // to avoid wasting too much space in leftover bytes. + char* result = AllocateNewBlock(bytes); + return result; + } + + // We waste the remaining space in the current block. + alloc_ptr_ = AllocateNewBlock(kBlockSize); + alloc_bytes_remaining_ = kBlockSize; + + char* result = alloc_ptr_; + alloc_ptr_ += bytes; + alloc_bytes_remaining_ -= bytes; + return result; +} + +char* Arena::AllocateAligned(size_t bytes) { + const int align = (sizeof(void*) > 8) ? sizeof(void*) : 8; + static_assert((align & (align - 1)) == 0, + "Pointer size should be a power of 2"); + size_t current_mod = reinterpret_cast(alloc_ptr_) & (align - 1); + size_t slop = (current_mod == 0 ? 0 : align - current_mod); + size_t needed = bytes + slop; + char* result; + if (needed <= alloc_bytes_remaining_) { + result = alloc_ptr_ + slop; + alloc_ptr_ += needed; + alloc_bytes_remaining_ -= needed; + } else { + // AllocateFallback always returned aligned memory + result = AllocateFallback(bytes); + } + assert((reinterpret_cast(result) & (align - 1)) == 0); + return result; +} + +char* Arena::AllocateNewBlock(size_t block_bytes) { + //char* result = new char[block_bytes]; + char* result = nullptr; + cudaMallocManaged((void **)&result, sizeof(char) * block_bytes); + blocks_.push_back(result); + memory_usage_.fetch_add(block_bytes + sizeof(char*), + std::memory_order_relaxed); + return result; +} + +} // namespace cleveldb \ No newline at end of file diff --git a/arena.cuh b/arena.cuh new file mode 100644 index 0000000..20da470 --- /dev/null +++ b/arena.cuh @@ -0,0 +1,68 @@ +// +// Created by user on 20/10/2021. +// + +#ifndef CLEVELDB_ARENA_CUH +#define CLEVELDB_ARENA_CUH + + +#include +#include +#include +#include +#include + +namespace cleveldb { + + +class Arena { +public: + Arena(); + Arena(const Arena &) = delete; + + Arena& operator=(const Arena&) = delete; + + ~Arena(); + + // Return a pointer to a newly allocated memory block of "bytes" bytes. + char* Allocate(size_t bytes); + + // Allocate memory with the normal alignment guarantees provided by malloc. + char* AllocateAligned(size_t bytes); + + // Returns an estimate of the total memory usage of data allocated + // by the arena. + size_t MemoryUsage() const { + return memory_usage_.load(std::memory_order_relaxed); + } + +private: + char* AllocateFallback(size_t bytes); + char* AllocateNewBlock(size_t block_bytes); + + char* alloc_ptr_; + size_t alloc_bytes_remaining_; + + std::vector blocks_; + + std::atomic memory_usage_; +}; + +inline char* Arena::Allocate(size_t bytes) { + // The semantics of what to return are a bit messy if we allow + // 0-byte allocations, so we disallow them here (we don't need + // them for our internal use). + assert(bytes > 0); + if (bytes <= alloc_bytes_remaining_) { + char* result = alloc_ptr_; + alloc_ptr_ += bytes; + alloc_bytes_remaining_ -= bytes; + return result; + } + return AllocateFallback(bytes); +} + + +} // namespace cleveldb + +#endif //CLEVELDB_ARENA_CUH diff --git a/main.cu b/main.cu new file mode 100644 index 0000000..8fbfc65 --- /dev/null +++ b/main.cu @@ -0,0 +1,6 @@ +#include + +int main() { + std::cout << "Hello, World!" << std::endl; + return 0; +} -- cgit v1.3.1