Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ded08aa
Add b_instrset_detect to namespace
lukas-valenta-tul Mar 30, 2025
d17d873
arena_resource
lukas-valenta-tul Mar 30, 2025
21d8a39
note
lukas-valenta-tul Mar 30, 2025
975d254
Install Eigen in github action
jbrezmorf Mar 31, 2025
e9ee1f4
ArenaAlloc is now a PatchArena wrapper
lukas-valenta-tul Apr 6, 2025
82f21d6
Eigen
lukas-valenta-tul Apr 6, 2025
11fbb54
does not work, topic for consultation
lukas-valenta-tul Apr 13, 2025
8bf9907
Found the issue, but not fixed yet
lukas-valenta-tul Apr 13, 2025
8ccd10e
Added getter
lukas-valenta-tul Apr 26, 2025
c48a10f
Expanded wrap_array wiht MultiIdx.
lukas-valenta-tul Apr 26, 2025
d248826
workflow change
lukas-valenta-tul Apr 26, 2025
783cbdc
Update eigen install in GH action
jbrezmorf May 4, 2025
6703ffc
Merge branch 'LV_eigen' of github.com:flow123d/bparser into LV_eigen
jbrezmorf May 4, 2025
740f691
Add more complex matrix multiplication tests.
jbrezmorf May 4, 2025
8b433d8
Merge branch 'master' into LV_eigen
jbrezmorf May 4, 2025
7e162d6
const keyword
lukas-valenta-tul May 5, 2025
a384ac8
reworked mat_mult, doesn't work for tensors...
lukas-valenta-tul May 5, 2025
f096b04
Matmult for tensors+
lukas-valenta-tul May 6, 2025
ada89c0
FetchContent_Declare
lukas-valenta-tul May 6, 2025
be6e36a
Eigen install attempt 2
lukas-valenta-tul May 6, 2025
9bd4c50
Eigen install attempt 3
lukas-valenta-tul May 6, 2025
18b1393
Eigen install attempt 4
lukas-valenta-tul May 6, 2025
5c8edbe
Eigen install attempt 5
lukas-valenta-tul May 6, 2025
188cc58
Eigen install attempt 6, now via apt-get
lukas-valenta-tul May 6, 2025
f8fd43b
forgot delete
lukas-valenta-tul May 6, 2025
9b8bd1b
PatchArena in test_speed and commented debug print
lukas-valenta-tul May 6, 2025
921a13e
tr - matrix trace
lukas-valenta-tul May 8, 2025
090c867
Alternative dot print which helps understand the DAG using any dot gr…
lukas-valenta-tul Oct 15, 2025
cb9414e
Better cast
lukas-valenta-tul Oct 19, 2025
95d91c3
Improved print_in_dot2. If the symbols_ map is supplied, it will also…
lukas-valenta-tul Oct 20, 2025
12b37e9
More appropriate catch blocks
lukas-valenta-tul Oct 20, 2025
b44c9d7
Matrix and vector norms
lukas-valenta-tul Nov 1, 2025
69be136
Attempt at computing spectral norm using eigenvalues. Requires compar…
lukas-valenta-tul Nov 9, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ jobs:
# Bparser dependency
sudo apt-get install -y libboost-all-dev

# install eigen
sudo apt install libeigen3-dev



Expand Down
34 changes: 31 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ option(SANITIZER_ON "Whether to use AddressSanitizer (asan) in the DEBUG configu

message(STATUS "CMakeLists.txt - BParser")

include(FetchContent)

if(POLICY CMP0135)
cmake_policy(SET CMP0135 NEW)
endif()

# CLANG

#set(CMAKE_CXX_FLAGS "-std=c++14 -finline-hint-functions -pedantic-errors -Werror=pedantic -Wall -Wextra -Werror -Wno-long-long -Wno-strict-aliasing -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION")
Expand Down Expand Up @@ -160,7 +166,7 @@ if (NOT Boost_FOUND)
if (NOT EXTERNAL_PROJECT_DIR)
unset(BOOST_ROOT)
endif()
find_package( Boost 1.58.0 REQUIRED)
find_package( Boost 1.70.0 REQUIRED) #since Boost 1.70.0 it should be find_package( Boost CONFIG REQUIRED)
endif()

message(STATUS "-------------------------------------------------------")
Expand All @@ -169,11 +175,31 @@ message(STATUS "BOOST_ROOT = ${BOOST_ROOT}")
message(STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}")
message(STATUS "Boost_LIBRARY_DIRS = ${Boost_LIBRARY_DIRS}")
message(STATUS "Boost_INCLUDE_DIR = ${Boost_INCLUDE_DIR}")
message(STATUS "=======================================================\n")

#Eigen
message(STATUS "=======================================================")
message(STATUS "====== EIGEN ==========================================")
message(STATUS "=======================================================")

#find_package(Eigen3 CONFIG REQUIRED) #Remember to install Eigen for this to work. See eigen-3.x.x/INSTALL
FetchContent_Declare(
Eigen3
URL https://gitlab.com/libeigen/eigen/-/archive/3.4.0/eigen-3.4.0.tar.gz
EXCLUDE_FROM_ALL
FIND_PACKAGE_ARGS CONFIG #same as find_package(Eigen3 CONFIG)
)
FetchContent_MakeAvailable(Eigen3)

message(STATUS "-------------------------------------------------------")
message(STATUS "EIGEN_ROOT = ${EIGEN_ROOT}")
message(STATUS "Eigen3_DIR = ${Eigen3_DIR}")
message(STATUS "EIGEN3_INCLUDE_DIR = ${EIGEN3_INCLUDE_DIR}")
message(STATUS "=======================================================\n\n")

message(STATUS "VCL2_INCLUDE_DIR = ${CMAKE_CURRENT_SOURCE_DIR}/third_party/VCL_v2")

set(BPARSER_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/include ${Boost_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/third_party/VCL_v2)
set(BPARSER_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/include ${Boost_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/third_party/VCL_v2 ${EIGEN3_INCLUDE_DIR})
if(NOT PROJECT_IS_TOP_LEVEL)
set(BPARSER_INCLUDES ${BPARSER_INCLUDES} PARENT_SCOPE)
endif()
Expand All @@ -198,6 +224,8 @@ add_library(bparser SHARED
${CMAKE_CURRENT_SOURCE_DIR}/include/processor_AVX512.cc
${CMAKE_CURRENT_SOURCE_DIR}/include/processor_double.cc
)
target_link_libraries(bparser Eigen3::Eigen) #Interface library, includes the header files
set_target_properties(bparser PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -DBPARSER_DLL")



Expand Down Expand Up @@ -259,6 +287,6 @@ endmacro()
define_test(test_parser bparser)
define_test(test_array)
define_test(test_grammar bparser)
define_test(test_processor)
define_test(test_processor bparser) #is it broken? -LV
define_test(test_speed bparser)
define_test(test_simd)
62 changes: 47 additions & 15 deletions include/arena_alloc.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <utility>
#include <malloc.h>
#include "aligned_alloc.hh"
#include "arena_resource.hh"

namespace bparser {

Expand All @@ -21,55 +22,86 @@ inline size_t align_size(size_t al, size_t size) {
}

struct ArenaAlloc {


//Creates a wrapper of PatchArena for backwards compatibility with BParser
ArenaAlloc(PatchArena& existing_arena) : arena(&existing_arena),buffer(nullptr) {
;
}
//Creates a wrapper with a new PatchArena with the specified memory alignment and size
//However AssemblyArena might be the correct class to create
ArenaAlloc(std::size_t alignment, std::size_t size)
: alignment_(alignment),
size_(0)
//: alignment_(alignment),
// size_(0)
{
size_ = align_size(alignment_, size);
size_t size_ = align_size(alignment, size);
buffer = align_alloc(alignment, size_);
arena = new PatchArena(buffer, size_, alignment);
/*size_ = align_size(alignment_, size);
base_ = (char*)align_alloc(alignment_, size_);
BP_ASSERT(base_ != nullptr);
ptr_ = base_;
//std::cout << "arena begin: " << (void *)base_ << " end: " << end() << std::endl;
*/
}

~ArenaAlloc() {
destroy();
}

void destroy() {
align_free(base_);
inline void destroy() {
//align_free(base_);
if (buffer != nullptr) {
align_free(buffer);
delete arena;
}
}

void *end() {
/*void* end() {
return base_ + size_;
}
}*/

void * allocate(std::size_t size) {
inline void* allocate(std::size_t size) {
/*
size = align_size(alignment_, size);
void * ptr = ptr_;
ptr_ += size;
BP_ASSERT(ptr_ <= end());
//std::cout << "allocated: " << ptr << " end: " << (void *)ptr_ << " aend: " << end() << "\n";
return ptr;
*/
return arena->allocate(size);

}

template <class T, typename... Args>
T * create(Args&&... args) {
T* create(Args&&... args) {

void * ptr = allocate(sizeof(T));
return new (ptr) T(std::forward<Args>(args)...);

}

template <class T>
T * create_array(uint n_items) {
T* create_array(uint n_items) {
/*
void * ptr = allocate(sizeof(T) * n_items);
return new (ptr) T[n_items];
*/
return arena->allocate_simd<T>(n_items);
}


std::size_t alignment_;
std::size_t size_;
char * base_;
char * ptr_;
inline std::size_t get_size() const {
return arena->get_size();
}

//std::size_t alignment_;
//std::size_t size_;
//char * base_;
//char * ptr_;
protected:
PatchArena* arena;
void* buffer;
};

} // namespace bparser
Expand Down
184 changes: 184 additions & 0 deletions include/arena_resource.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*!
*
 * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 3 as published by the
* Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
*
* @file arena_resource.hh
*/

#ifndef ARENA_RESOURCE_HH_
#define ARENA_RESOURCE_HH_

#include <memory_resource>
#include <vector>
#include <iostream>
#include <new>
#include <stdexcept> // !! Use Flow exception mechanism

//#include "system/asserts.hh"
#include "assert.hh"


// Final proposal of Arena
// TODO shared_ptr out of class, pass pointer to data, describe how to use
template <class Resource>
class PatchArenaResource : public std::pmr::memory_resource {
protected:
/// Returns different upstream resource in debug / release mode
static inline std::pmr::memory_resource* upstream_resource() {
#ifdef DEBUG
return std::pmr::null_memory_resource();
#else
return std::pmr::get_default_resource();
#endif
}

public:
//DECLARE_EXCEPTION( ExcArenaAllocation,
// << "Allocation of ArenaResource failed. Please check if correct type of upstream is used.");
#define EXC_ARENA_ALLOCATION "Allocation of ArenaResource failed. Please check if correct type of upstream is used."

/// Same as previous but doesn't construct buffer implicitly.
PatchArenaResource(void *buffer, size_t buffer_size, size_t simd_alignment, std::pmr::memory_resource* upstream = PatchArenaResource<Resource>::upstream_resource())
: upstream_( upstream ),
buffer_(buffer),
buffer_size_(buffer_size),
resource_(buffer_, buffer_size, upstream_),
simd_alignment_(simd_alignment),
full_data_(false)
{
//ASSERT_PERMANENT_EQ( (buffer_size%simd_alignment), 0 );
BP_ASSERT( (buffer_size % simd_alignment) == 0 );
}


~PatchArenaResource() = default; // virtual, call destructor buffer_ = default_resource, (resource_)

/// Compute and print free space and used space of arena buffer. Development method
inline void print_space() {
void *p = this->raw_allocate(1, simd_alignment_);
size_t used_size = (char *)p - (char *)buffer_;
size_t free_space = buffer_size_ - used_size;
std::cout << "Allocated space of arena is " << used_size << " B, free space is " << free_space << " B." << std::endl;
}


/// Getter for resource
Resource &resource() {
return resource_;
}

/// Allocate and return data pointer of n_item array of type T (alignment to length 8 bytes)
template <class T>
T* allocate_8(size_t n_items) {
size_t bytes = sizeof(T) * n_items;
return (T*)this->raw_allocate(bytes, 8);
}

/// Allocate and return data pointer of n_item array of type T (alignment to length given by simd_alignment constructor argument)
template <class T>
T* allocate_simd(size_t n_items) {
size_t bytes = sizeof(T) * n_items;
return (T*)this->raw_allocate(bytes, simd_alignment_);
}

// Reset allocated data
void reset() {
resource_.release();
full_data_ = false;
#ifdef DEBUG
char *c_buffer = (char *)buffer_;
for (size_t i=0; i<buffer_size_; ++i)
c_buffer[i] = 0;
#endif
}

inline size_t get_size() const{
return buffer_size_;
}
protected:
void* raw_allocate(size_t bytes, size_t alignment) {
//ASSERT(!full_data_).error("Allocation of new data is not possible because child arena was created.");
BP_ASSERT( !full_data_ );
//ASSERT_EQ(buffer_size_%alignment, 0);
BP_ASSERT( (buffer_size_ % alignment) == 0 );

try {
void* p = resource_.allocate(bytes, alignment);
return p;
} catch ( std::bad_alloc& ) {
//THROW( ExcArenaAllocation() );
using bparser::Exception;
Throw() << EXC_ARENA_ALLOCATION;

}
return nullptr;
}

/// Override do_allocate to handle allocation logic
void* do_allocate(size_t bytes, size_t alignment) override {
return raw_allocate(bytes, alignment);
}

/// Override do_deallocate (no-op for monotonic buffer)
void do_deallocate(void* p, size_t bytes, size_t alignment) override {
upstream_->deallocate(p, bytes, alignment);
}

/// Override do_is_equal for memory resource comparison
bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
return this == &other;
}

std::pmr::memory_resource* upstream_; ///< Pointer to upstream
void* buffer_; ///< Pointer to buffer
size_t buffer_size_; ///< Size of buffer
Resource resource_; ///< Resource of arena
size_t simd_alignment_; ///< Size of SIMD alignment
bool full_data_; ///< Flag signs full data (child arena is created)
};


template <class Resource>
class AssemblyArenaResource : public PatchArenaResource<Resource> {
public:
/// Constructor. Creates assembly arena
AssemblyArenaResource(size_t buffer_size, size_t simd_alignment, std::pmr::memory_resource* upstream = PatchArenaResource<Resource>::upstream_resource())
: PatchArenaResource<Resource>( std::pmr::get_default_resource()->allocate(buffer_size, simd_alignment), buffer_size, simd_alignment, upstream ) {}

virtual ~AssemblyArenaResource() {
this->do_deallocate(this->buffer_, this->buffer_size_, this->simd_alignment_);
}

/**
* Create and return child arena.
*
* Child arena is created in free space of actual arena.
* Actual arena is marked as full (flag full_data_) and cannot allocate new data.
*/
PatchArenaResource<Resource> *get_child_arena() {
void *p = this->raw_allocate(1, this->simd_alignment_);
size_t used_size = (char *)p - (char *)this->buffer_;
size_t free_space = this->buffer_size_ - used_size;
this->full_data_ = true;
return new PatchArenaResource<Resource>(p, free_space, this->simd_alignment_);
}


};



using AssemblyArena = AssemblyArenaResource<std::pmr::monotonic_buffer_resource>;
using PatchArena = PatchArenaResource<std::pmr::monotonic_buffer_resource>;


#endif /* ARENA_RESOURCE_HH_ */
Loading