text:
#include <boost/test/unit_test.hpp>
#include <consensus/validation.h>
#include <script/interpreter.h>
#include <string>
#include <test/util/setup_common.h>
#include <validation.h>
#include <vbk/config.hpp>
#include <vbk/init.hpp>
#include <vbk/pop_service.hpp>
#include <vbk/service_locator.hpp>
#include <vbk/test/util/mock.hpp>
#include <vbk/test/util/tx.hpp>
#include <vbk/util.hpp>
#include <vbk/util_service/util_service_impl.hpp>

#include <gmock/gmock.h>

using ::testing::Return;

BOOST_AUTO_TEST_SUITE(pop_interpreter_tests)

template <typename Stream>
Stream& operator<<(Stream&& s, std::string data)
{
    return s << std::vector<uint8_t>{data.begin(), data.end()};
}

inline std::vector<uint8_t> operator""_v(const char* c, size_t s)
{
    return std::vector<uint8_t>{c, c + s};
}

struct EvalScriptFixture {
    static std::string str(const std::vector<uint8_t>& v)
    {
        return std::string{v.begin(), v.end()};
    }

    static bool CastToBool(const std::vector<uint8_t>& vch)
    {
        for (unsigned int i = 0; i < vch.size(); i++) {
            if (vch[i] != 0) {
                // Can be negative zero
                if (i == vch.size() - 1 && vch[i] == 0x80)
                    return false;
                return true;
            }
        }
        return false;
    }

    EvalScriptFixture()
    {
        VeriBlockTest::setServiceMock<VeriBlock::PopService>(pop);

        config.min_atv_size = 1;
        config.max_atv_size = 10;
        config.min_vtb_size = 1;
        config.max_vtb_size = 10;
        config.max_pop_script_size = 1000;
        config.btc_header_size = 3;
        config.vbk_header_size = 4;

        ON_CALL(pop, checkATVinternally).WillByDefault(Return(true));
        ON_CALL(pop, checkVTBinternally).WillByDefault(Return(true));
    }

    VeriBlockTest::PopServiceMock pop;
    VeriBlock::UtilService& util{VeriBlock::InitUtilService()};
    VeriBlock::Config& config{VeriBlock::InitConfig()};

    std::vector<std::vector<uint8_t>> stack{};
    VeriBlock::Publications pub{};
    VeriBlock::Context ctx{};
    VeriBlock::PopTxType type{};
    ScriptError serror{};
    CScript script = CScript()
        << "ATV" << OP_CHECKATV
        << "VTB1" << OP_CHECKVTB
        << "VTB2" << OP_CHECKVTB
        << "VTB3" << OP_CHECKVTB
        << OP_CHECKPOP;
};

BOOST_FIXTURE_TEST_CASE(eval_pop_tx_scriptSig_valid, EvalScriptFixture)
{
    EXPECT_CALL(pop, checkATVinternally).Times(1);
    EXPECT_CALL(pop, checkVTBinternally).Times(3);

    BOOST_CHECK(util.EvalScript(script, stack, &serror, &pub, &ctx, &type, true));
    BOOST_REQUIRE(!stack.empty());
    BOOST_CHECK(type == VeriBlock::PopTxType::PUBLICATIONS);
    BOOST_CHECK(CastToBool(stack.back()));
    BOOST_CHECK(pub.atv == "ATV"_v);
    BOOST_CHECK(pub.vtbs.size() == 3);
    BOOST_CHECK(pub.vtbs[0] == "VTB1"_v);
    BOOST_CHECK(pub.vtbs[1] == "VTB2"_v);
    BOOST_CHECK(pub.vtbs[2] == "VTB3"_v);
}

BOOST_FIXTURE_TEST_CASE(eval_pop_tx_scriptSig_atv_size_invalid, EvalScriptFixture)
{
    config.min_atv_size = 10; // more than 3
    BOOST_CHECK(!util.EvalScript(script, stack, &serror, nullptr, nullptr, nullptr, true));
    BOOST_CHECK(serror == SCRIPT_ERR_PUSH_SIZE);
    config.min_atv_size = 0;
    config.max_atv_size = 1; // less than 3
    BOOST_CHECK(!util.EvalScript(script, stack, &serror, nullptr, nullptr, nullptr, true));
    BOOST_CHECK(serror == SCRIPT_ERR_PUSH_SIZE);
}

BOOST_FIXTURE_TEST_CASE(eval_pop_tx_scriptSig_vtb_size_invalid, EvalScriptFixture)
{
    config.min_vtb_size = 10; // more than 4
    BOOST_CHECK(!util.EvalScript(script, stack, &serror, nullptr, nullptr, nullptr, true));
    BOOST_CHECK(serror == SCRIPT_ERR_PUSH_SIZE);
    config.min_vtb_size = 0;
    config.max_vtb_size = 2; // less than 4
    BOOST_CHECK(!util.EvalScript(script, stack, &serror, nullptr, nullptr, nullptr, true));
    BOOST_CHECK(serror == SCRIPT_ERR_PUSH_SIZE);
}

BOOST_FIXTURE_TEST_CASE(eval_pop_tx_scriptSig_only_atv, EvalScriptFixture)
{
    script = CScript() << "ATV" << OP_CHECKATV << OP_CHECKPOP;
    BOOST_CHECK(util.EvalScript(script, stack, &serror, &pub, &ctx, &type, true));
    BOOST_REQUIRE(!stack.empty());
    BOOST_CHECK(type == VeriBlock::PopTxType::PUBLICATIONS);
    BOOST_CHECK(CastToBool(stack.back()));
    BOOST_CHECK(pub.vtbs.empty());
    BOOST_CHECK(pub.atv == "ATV"_v);
}

BOOST_FIXTURE_TEST_CASE(eval_pop_tx_scriptSig_only_vtbs, EvalScriptFixture)
{
    script = CScript() << "VTB1" << OP_CHECKVTB << OP_CHECKPOP;
    BOOST_CHECK(!util.EvalScript(script, stack, &serror, nullptr, nullptr, nullptr, true));
    BOOST_CHECK(serror == SCRIPT_ERR_VBK_ATVFAIL);
}

BOOST_FIXTURE_TEST_CASE(eval_pop_tx_scriptSig_out_of_order, EvalScriptFixture)
{
    script = CScript() << "VTB1" << OP_CHECKVTB << "ATV" << OP_CHECKATV << "VTB2" << OP_CHECKVTB << OP_CHECKPOP;
    BOOST_CHECK(util.EvalScript(script, stack, &serror, &pub, &ctx, &type, true));
    BOOST_REQUIRE(!stack.empty());
    BOOST_CHECK(CastToBool(stack.back()));
    BOOST_CHECK(pub.atv == "ATV"_v);
    BOOST_CHECK(pub.vtbs.size() == 2);
    BOOST_CHECK(pub.vtbs[0] == "VTB1"_v);
    BOOST_CHECK(pub.vtbs[1] == "VTB2"_v);
    BOOST_CHECK(type == VeriBlock::PopTxType::PUBLICATIONS);
}

BOOST_FIXTURE_TEST_CASE(eval_pop_tx_scriptSig_too_big, EvalScriptFixture)
{
    config.max_pop_script_size = 10;
    script = CScript() << "this is too long, I can not handle script this long" << OP_CHECKVTB << OP_CHECKPOP;
    BOOST_CHECK(!util.EvalScript(script, stack, &serror, nullptr, nullptr, nullptr, true));
    BOOST_CHECK(serror == SCRIPT_ERR_SCRIPT_SIZE);
}

BOOST_FIXTURE_TEST_CASE(eval_pop_tx_scriptSig_checkpop_is_not_last, EvalScriptFixture)
{
    script = CScript() << "ATV" << OP_CHECKATV << "VTB" << OP_CHECKVTB << OP_CHECKPOP << "VTB2";
    BOOST_CHECK(!util.EvalScript(script, stack, &serror, nullptr, nullptr, nullptr, true));
    BOOST_CHECK(serror == SCRIPT_ERR_VBK_EXTRA_OPCODE);
}

BOOST_FIXTURE_TEST_CASE(eval_pop_tx_scriptSig_checkpop_stack_is_not_empty, EvalScriptFixture)
{
    // missing OP_CHECKATV
    script = CScript() << "ATV"
                       << "VTB" << OP_CHECKVTB << OP_CHECKPOP;
    BOOST_CHECK(!util.EvalScript(script, stack, &serror, nullptr, nullptr, nullptr, true));
    BOOST_CHECK(serror == SCRIPT_ERR_EVAL_FALSE);
}

BOOST_FIXTURE_TEST_CASE(eval_pop_tx_scriptSig_only_context_valid, EvalScriptFixture)
{
    script = CScript() << "btc" << "vbk1" << OP_POPVBKHEADER << "vbk2" << OP_POPVBKHEADER << OP_POPBTCHEADER << OP_CHECKPOP;
    BOOST_CHECK(util.EvalScript(script, stack, &serror, &pub, &ctx, &type, false));
    BOOST_CHECK(type == VeriBlock::PopTxType::CONTEXT);
    BOOST_CHECK(ctx.btc.size() == 1);
    BOOST_CHECK(ctx.btc[0] == "btc"_v);
    BOOST_CHECK(ctx.vbk.size() == 2);
    BOOST_CHECK(ctx.vbk[0] == "vbk1"_v);
    BOOST_CHECK(ctx.vbk[1] == "vbk2"_v);
}

BOOST_FIXTURE_TEST_CASE(eval_pop_tx_scriptSig_mixed_pub_and_context, EvalScriptFixture)
{
    script = CScript() << "ATV" << OP_CHECKATV << "VTB" << OP_CHECKVTB << "btc" << "vbk1" << OP_POPVBKHEADER << "vbk2" << OP_POPVBKHEADER << OP_POPBTCHEADER << OP_CHECKPOP;
    BOOST_CHECK(!util.EvalScript(script, stack, &serror, nullptr, nullptr, nullptr, false));
    BOOST_CHECK(serror == SCRIPT_ERR_BAD_OPCODE);
}

BOOST_FIXTURE_TEST_CASE(eval_pop_tx_scriptSig_ctx_invalid_size_vbk, EvalScriptFixture)
{
    script = CScript() << "btc" << "veriblock header that is not equalto expected size" << OP_POPVBKHEADER << "vbk2" << OP_POPVBKHEADER << OP_POPBTCHEADER << OP_CHECKPOP;
    BOOST_CHECK(!util.EvalScript(script, stack, &serror, nullptr, nullptr, nullptr, false));
    BOOST_CHECK(serror == SCRIPT_ERR_PUSH_SIZE);
}

BOOST_FIXTURE_TEST_CASE(eval_pop_tx_scriptSig_ctx_invalid_size_btc, EvalScriptFixture)
{
    script = CScript() << "bitcoin header that is not equal to expected size" << OP_POPBTCHEADER << "vbk2" << OP_POPVBKHEADER << OP_CHECKPOP;
    BOOST_CHECK(!util.EvalScript(script, stack, &serror, nullptr, nullptr, nullptr, false));
    BOOST_CHECK(serror == SCRIPT_ERR_PUSH_SIZE);
}

BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: xagau/vbk-ri-btc

max_issues_repo_name: xagau/vbk-ri-btc

max_forks_repo_name: xagau/vbk-ri-btc


text:
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <boost/bind/bind.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace boost;
using boost::asio::ip::tcp;

const int max_length = 1024;

typedef boost::shared_ptr<tcp::socket> socket_ptr;

void session(socket_ptr sock, int max_filesize)
{
  try
  {
    int counter = 0;
    for (;;)
    {
      char data[max_length], tmp[max_filesize];
      boost::system::error_code error;
      size_t length = sock->read_some(boost::asio::buffer(data), error);

      while (counter <= length)
      {
        /* Create files with received data */
        char filename[255];
        struct tm* tm;
        time_t now;
        now = time(0);
        tm = localtime(&now);
        sprintf(filename, "output_%04d%02d%02d%02d%02d%02d.txt",
            tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
        ofstream OutputFile(filename);

        for (int i = 0; i < max_filesize; i++) tmp[i] = data[i+counter];
        OutputFile << tmp;
        OutputFile.close();
        counter += max_filesize;
        sleep(1);
      }

      if (error == boost::asio::error::eof)
        break; // Connection closed cleanly by peer.
      else if (error)
        throw boost::system::system_error(error); // Some other error.

      boost::asio::write(*sock, boost::asio::buffer(data, length));
    }
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception in thread: " << e.what() << "\n";
  }
}

void server(boost::asio::io_context& io_context, unsigned short port, int max_filesize)
{
  tcp::acceptor a(io_context, tcp::endpoint(tcp::v4(), port));
  for (;;)
  {
    socket_ptr sock(new tcp::socket(io_context));
    a.accept(*sock);
    boost::thread t(boost::bind(session, sock, max_filesize));
  }
}

int main(int argc, char* argv[])
{
  try
  {
    int port, max_filesize;
    string line;
    vector<string> strs;

    /* Read config file */
    ifstream ConfigFile("config.cfg");
    getline (ConfigFile, line);
    boost::split(strs,line,boost::is_any_of(" "));
    port = stoi(strs[1]);
    getline (ConfigFile, line);
    boost::split(strs,line,boost::is_any_of(" "));
    max_filesize = stoi(strs[1]);

    boost::asio::io_context io_context;

    using namespace std;
    server(io_context, port, max_filesize);
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}

ext: cpp

max_stars_repo_name: brunomaximom/DesafioCognyte

max_issues_repo_name: brunomaximom/DesafioCognyte

max_forks_repo_name: brunomaximom/DesafioCognyte


text:
/*********************************************************************
 *
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2015, Daichi Yoshikawa
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of the Daichi Yoshikawa nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 *
 * Author: Daichi Yoshikawa
 *
 *********************************************************************/

#ifndef __AHL_UTILS_SHARED_MEMORY_HPP
#define __AHL_UTILS_SHARED_MEMORY_HPP

#include <string>
#include <memory>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/named_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>

using namespace boost::interprocess;

namespace ahl_utils
{

  template<typename T>
  class SharedMemory
  {
  public:
    explicit SharedMemory(const std::string& name, bool remove = false)
      : name_(name)
    {
      size_ = sizeof(T);

      if(remove)
      {
        shared_memory_object::remove(name.c_str());
      }

      try
      {
        shm_ = std::make_shared<shared_memory_object>(open_or_create, name_.c_str(), read_write);
        shm_->truncate(size_);
      }
      catch(interprocess_exception&)
      {
        shared_memory_object::remove(name.c_str());
        shm_ = std::make_shared<shared_memory_object>(open_or_create, name_.c_str(), read_write);
        shm_->truncate(size_);
      }

      region_ = std::make_shared<mapped_region>(*shm_, read_write);
      std::string mutex_name = name + "_mutex";
      mutex_ = std::make_shared<named_mutex>(open_or_create, mutex_name.c_str());
      data_ = static_cast<T*>(region_->get_address());
    }

    void write(const T& val)
    {
      scoped_lock<named_mutex> lock(*mutex_);
      *data_ = val;
    }

    void read(T& val)
    {
      scoped_lock<named_mutex> lock(*mutex_);
      val = *data_;
    }

    const std::string& getName() const
    {
      return name_;
    }

    uint32_t getSize()
    {
      return size_;
    }

  private:
    using SharedMemoryObjectPtr = std::shared_ptr<shared_memory_object>;
    using MappedRegionPtr = std::shared_ptr<mapped_region>;
    using NamedMutexPtr = std::shared_ptr<named_mutex>;

    std::string name_;
    uint32_t size_;
    SharedMemoryObjectPtr shm_;
    MappedRegionPtr region_;
    NamedMutexPtr mutex_;
    T* data_;
  };

  template<typename T>
  using SharedMemoryPtr = std::shared_ptr<SharedMemory<T>>;

} // namespace ahl_utils

#endif // __AHL_UTILS_SHARED_MEMORY_HPP

ext: hpp


text:
#include "constant_mappings.hpp"

#include <boost/bimap.hpp>
#include <boost/hana/fold.hpp>

#include <string>
#include <unordered_map>

#include "sql/Expr.h"
#include "sql/SelectStatement.h"

#include "expression/abstract_expression.hpp"
#include "expression/aggregate_expression.hpp"
#include "storage/encoding_type.hpp"
#include "storage/table.hpp"
#include "storage/vector_compression/vector_compression.hpp"
#include "utils/make_bimap.hpp"

namespace opossum {

const boost::bimap<AggregateFunction, std::string> aggregate_function_to_string =
    make_bimap<AggregateFunction, std::string>({
        {AggregateFunction::Min, "MIN"},
        {AggregateFunction::Max, "MAX"},
        {AggregateFunction::Sum, "SUM"},
        {AggregateFunction::Avg, "AVG"},
        {AggregateFunction::Count, "COUNT"},
        {AggregateFunction::CountDistinct, "COUNT DISTINCT"},
    });

const boost::bimap<FunctionType, std::string> function_type_to_string =
    make_bimap<FunctionType, std::string>({{FunctionType::Substring, "SUBSTR"}, {FunctionType::Concatenate, "CONCAT"}});

const boost::bimap<DataType, std::string> data_type_to_string =
    hana::fold(data_type_enum_string_pairs, boost::bimap<DataType, std::string>{}, [](auto map, auto pair) {
      map.insert({hana::first(pair), std::string{hana::second(pair)}});
      return map;
    });

const boost::bimap<EncodingType, std::string> encoding_type_to_string = make_bimap<EncodingType, std::string>({
    {EncodingType::Dictionary, "Dictionary"},
    {EncodingType::RunLength, "RunLength"},
    {EncodingType::FixedStringDictionary, "FixedStringDictionary"},
    {EncodingType::FrameOfReference, "FrameOfReference"},
    {EncodingType::LZ4, "LZ4"},
    {EncodingType::Unencoded, "Unencoded"},
});

const boost::bimap<VectorCompressionType, std::string> vector_compression_type_to_string =
    make_bimap<VectorCompressionType, std::string>({
        {VectorCompressionType::FixedSizeByteAligned, "Fixed-size byte-aligned"},
        {VectorCompressionType::SimdBp128, "SIMD-BP128"},
    });

std::ostream& operator<<(std::ostream& stream, AggregateFunction aggregate_function) {
  return stream << aggregate_function_to_string.left.at(aggregate_function);
}

std::ostream& operator<<(std::ostream& stream, FunctionType function_type) {
  return stream << function_type_to_string.left.at(function_type);
}

std::ostream& operator<<(std::ostream& stream, DataType data_type) {
  return stream << data_type_to_string.left.at(data_type);
}

std::ostream& operator<<(std::ostream& stream, EncodingType encoding_type) {
  return stream << encoding_type_to_string.left.at(encoding_type);
}

std::ostream& operator<<(std::ostream& stream, VectorCompressionType vector_compression_type) {
  return stream << vector_compression_type_to_string.left.at(vector_compression_type);
}

}  // namespace opossum

max_stars_repo_name: hyrise-mp/hyrise

max_issues_repo_name: hyrise-mp/hyrise

max_forks_repo_name: hyrise-mp/hyrise


text:
//
//  parser.hpp
//  pastoc
//
//  Created by Alexandr Borzykh on 10/11/2017.
//  Copyright © 2017 HATE|RED. All rights reserved.
//

#ifndef parser_hpp
#define parser_hpp

#include "AST.hpp"

#include <boost/optional.hpp>

#include <string>

namespace pastoc
{

	class Parser
	{
	public:
		static boost::optional<ast::pascal_program> Process(const std::string& inputPath);
	};

}

#endif /* parser_hpp */

max_stars_repo_name: sandyre/pastoc

max_issues_repo_name: sandyre/pastoc

max_forks_repo_name: sandyre/pastoc


text:
/*
 * Copyright (C) 2017 NEC Laboratories America, Inc. ("NECLA"). All rights reserved.
 *
 * This source code is licensed under the license found in the LICENSE file in
 * the root directory of this source tree. An additional grant of patent rights
 * can be found in the PATENTS file in the same directory.
 */
#include "shm/shormOps.hpp"
#include "shm/shormMgr.hpp"
#include <stdlib.h>             // malloc, free for 'C' stuff ?

#include <iostream>
#include <sstream>

using namespace std;

#if SHM_THREAD == SHM_IPC
#include <boost/interprocess/sync/scoped_lock.hpp>
namespace bip = boost::interprocess;
#define LOCK_GAURD( shCtrl ) bip::scoped_lock< bip::interprocess_mutex >( shCtrl->mtx )

#elif SHM_THREAD == SHM_STD_THREAD
#define LOCK_GUARD( shCtrl ) std::lock_guard<std::mutex>( shCtrl->mtx )

#endif

extern "C" {

    using shorm::ShormMgr;

    thread_local orm_rank_t irank;

    static inline ShormMgr* mgr( struct Shorm const* const shOrm ){
        return reinterpret_cast<ShormMgr*>( shOrm->opaque );
    }
    static inline ShormMgr const* cmgr( struct Shorm const* const shOrm ){
        return reinterpret_cast<ShormMgr const*>( shOrm->opaque );
    }

    /** This is a 'C'-style constructor for struct \c Shorm.
     * not all dispatch functions are set up yet! Add them as
     * the lua interface develops (if ever). */
    struct Orm const* shorm_open( char const* const path
                                  , int const reattach
                                  , struct Orm const* const orm /*=nullptr*/
                                )
    {
        std::ostringstream oss;
        // This is what we are initializing in this function:
        // There is one of these PER THREAD if shm_proc_init is called from each thread !
        struct Shorm* shOrm = (struct Shorm*)malloc(sizeof(Shorm));
        oss<<" shorm_open t"<<(void*)(uintptr_t)pthread_self()<<" malloced Shorm* @ "<<(void*)shOrm<<"\n";
        memset( shOrm, 0, sizeof(Shorm) );

        assert( orm != nullptr ); // until we double-check this use-case
        ShormConf const* config;             // remember configuration pointer (if any)
        // link Shorm to Orm dispatch table
        if( orm == nullptr ){
            const_cast<Orm*&>(shOrm->orm) = (struct Orm*)malloc(sizeof(struct Orm));
            oss<<" Orm malloc @ "<<(void*)shOrm->orm;
            const_cast<bool&>(shOrm->ownOrm) = true;
            memcpy( const_cast<Orm*>(shOrm->orm), &orm_shm, sizeof(struct Orm) );
            config = nullptr;
        }else{
            const_cast<Orm const*&>(shOrm->orm) = orm;
            const_cast<bool&>(shOrm->ownOrm) = false;
            config = reinterpret_cast<ShormConf const *>(const_cast<void const*>(orm->obj));
        }
        if( config != nullptr ) oss<<" config: nThreads = "<<config->nThreads;
        else /*              */ oss<<" config: none "<<(reattach?" reattach":"")<<endl;
        oss<<(reattach?" reattach":"");
        // link Orm ---::obj--> Shorm
        const_cast<void*&>(shOrm->orm->obj) = shOrm;

        // create & install the opaque manager object
        ShormMgr* shormMgr = new ShormMgr( *shOrm, path, reattach );
        oss<<" ShormMgr @ @ "<<(void*)shormMgr;
        const_cast<void*&>(shOrm->opaque) = (void*)shormMgr;
        //cout<<" shormMgr opaque --> "<<(void*)shOrm->opaque<<endl;
        struct ShormCtrl* shCtrl = shormMgr->getShCtrl();
        oss<<" shCtrl @ "<<(void*)shCtrl;
        //cout<<" ShormMgr shCtrl --> "<<(void*)shormMgr->getShCtrl()<<" nThreads="<<shCtrl->conf.nThreads<<endl;

        // ShormMgr constructor has created shared region with name 'path'
        // ... finally we can stick our config directly into shared mem ...
        if( config  && config->nThreads > 0U ){
            // configure shared memory ShormCtrl things JUST ONCE
            if( reattach ){
                std::runtime_error e(" shorm_open: nThreads>0 ~ MASTER, reattach illegal\n");
                oss<<" THREAD t"<<(void*)(uintptr_t)pthread_self()<<" ERROR !\n\t"<<e.what();
                shOrm->orm->printf(shOrm->orm,"%s",oss.str().c_str());
                throw(e);
            }
            memcpy( (void*)&shCtrl->conf, config, sizeof(struct ShormConf) );

            // SHM master pretends all nodes "alive" even if threads have not yet been spawned.
            oss<<"\n\tMASTER: set ri.status=1 but ri.snRank=-1 for "<<shCtrl->conf.nThreads<<" threads, awaiting assignRank";
            const_cast<uint_least16_t&>(shCtrl->conf.nThreads) = config->nThreads;
            for( uint32_t i=0U; i< config->nThreads; ++i ){
                ShormCtrl::RankInfo &ri = shCtrl->ranks[i];
                ri.status = 1U;                 // alive
                ri.snRank = orm_rank_t(-1U);  // awaiting assignRank call (even master thread)
            }
            shCtrl->nRanks = shCtrl->conf.nThreads; // awaiting shm_assignRank ...

            // we now know how many threads the barrier operation intends to coordinate
            new (&shCtrl->barrier) ShormCtrl::Barrier( (unsigned int)shCtrl->conf.nThreads );
            // XXX shCtrl->barrier is not freed, but this is in /dev/shm/ShormCtrl, so NOT a true leak
            //     ... unless we are removing the shared memory entirely
            oss<<"\n\tshorm_open NEW BARRIER( "<<shCtrl->conf.nThreads<<")";
        }else{
            oss<<"\n\tshorm_open slave, nThreads = "<<shCtrl->conf.nThreads;
        }

        shOrm->orm->printf(shOrm->orm,"%s\n",oss.str().c_str());

        assert( (void*)(shOrm->orm->obj) == (void*)(shOrm) );
        return shOrm->orm;
    }

    void shorm_close( struct Shorm** shorm )
    {
        assert( shorm != nullptr );
        struct Shorm* sh = *shorm;
        sh->orm->printf(sh->orm,"shorm_close!!!!!!\n");
        {
            assert( sh != nullptr );
            // free subobjects, make them unusable
            const_cast<void*&>(sh->orm->obj) = nullptr;
            //
            ShormMgr* m = mgr(sh);      // from sh->opaque
            sh->orm->printf(sh->orm, " deleting ShormMgr @ %p", (void*)m );
            delete m;
            const_cast<void*&>(sh->opaque) /*ShormMgr*/ = nullptr;
            //
            cout<<" shorm_close, ownOrm="<<sh->ownOrm<<endl;
            if( sh->ownOrm ){
                sh->orm->printf(sh->orm, " freeing sh->orm @ %p", (void*)sh->orm);
                free( const_cast<Orm*&>(sh->orm) );
                const_cast<Orm*&>(sh->orm) = nullptr;
            }
        }
        cout<<" shorm_close freeing Shorm @ "<<(void*)sh;
        free(sh);
        *shorm = nullptr;
    }

    orm_rank_t shorm_countlive( struct Shorm const* const shorm )
    {
        ShormCtrl * const shCtrl = cmgr(shorm)->getShCtrl();
        orm_rank_t live = 0U;
        {
            LOCK_GUARD( shCtrl );
            for( orm_rank_t i=0U; i<SHCNF_MAXRANKS; ++i ){
                ShormCtrl::RankInfo& ri = shCtrl->ranks[i];
                if( ri.status != 0 ){ // ri.status != 0 means "assigned"
                    if( ri.snRank != i ){
                        throw std::runtime_error(" ri.snRank != i");
                    }
                    if( i >= shCtrl->nRanks ){
                        throw std::runtime_error(" i >= shCtrl->nRanks");
                    }
                    ++live;
                }
            }
            return live;
        }
    }
    /** side-effect: thread_local irank is set as index into
     * ShormCtrl::ranks[irank] entry.
     *
     * \note Unsafe to call in multi-process because of issues with
     *       barrier re-initialization.
     *
     * - Current workaround is to ONLY call shorm_assignRank from
     *   shm_proc_init, which must:
     *   1. master sets nThreads/nRanks to desired value and pretends
     *      all theads already alive.
     *   1. barrier initialized, all RankInfo status != 0
     *   1. slaves assignRank and grab entries with:
     *      - status != 0 and smRank == -1U
     */
    void shorm_assignRank( struct Shorm* shorm, orm_rank_t rank )
    {
        irank = orm_rank_t(-1U);
        ShormCtrl * shCtrl = cmgr(shorm)->getShCtrl();
        {
            LOCK_GUARD( shCtrl );
            orm_rank_t firstfree = orm_rank_t(-1U);
            orm_rank_t lastused = orm_rank_t(-1U);
            //
            for( orm_rank_t i=0U; i<SHCNF_MAXRANKS; ++i ){
                ShormCtrl::RankInfo& ri = shCtrl->ranks[i];
                if( ri.status == 1 && ri.snRank == orm_rank_t(-1U) ){
                    if( firstfree == orm_rank_t(-1U) ){
                        firstfree = i;
                        //cout<<" firstfree (init) = "<<firstfree<<endl;
                    }
                }else if( ri.status == 0 ){
                    if( firstfree == orm_rank_t(-1U) ){
                        firstfree = i;
                        //cout<<" firstfree (init) = "<<firstfree<<endl;
                    }
                }else{ // ri.status != 0 means "assigned"
                    assert( ri.status == 1 );
                    assert( ri.snRank != orm_rank_t(-1U) );
                    //cout<<" i = "<<i<<" live: snRank="<<ri.snRank<<" irank="<<irank<<endl;
                    if( ri.snRank != i ){
                        throw std::runtime_error("shorm_assignRank: rank improperly assigned?");
                    }
                    lastused = i;
                }
            }
            shorm->orm->printf(shorm->orm,"assignRank: firstfree=%d lastused=%d SHCNF_MAXRANKS=%u\n",
                               (int)firstfree, (int)lastused, (unsigned)SHCNF_MAXRANKS );
            if( firstfree == orm_rank_t(-1U) )
                throw std::runtime_error(" shorm_assignRank: no ranks available" );
            irank = firstfree;
            if( irank < 0  || irank >= SHCNF_MAXRANKS )
                throw std::runtime_error(" irank out of range");
            ShormCtrl::RankInfo& ri = shCtrl->ranks[irank];
            ri.rank = rank;
            ri.snRank = irank;
            ri.status = 1U;
            if( lastused == (orm_rank_t)(-1U) || irank > lastused ) lastused = irank;
            if( lastused >= shCtrl->nRanks ){
                uint32_t old = shCtrl->nRanks;
                shCtrl->nRanks = lastused + 1U;
                shorm->orm->printf(shorm->orm," nRanks was %u,  max increased to %u\n", (unsigned)old, (unsigned)(shCtrl->nRanks) );
                // XXX deabtable ...
            }
            assert( shCtrl->nRanks > 0 && shCtrl->nRanks <= SHCNF_MAXRANKS );
        }
    }
    void shorm_remove( struct Shorm* sh )
    {
        assert( sh != nullptr );
        using namespace shorm;
        ShormMgr* mgr = reinterpret_cast<ShormMgr*>( sh->opaque );
        mgr->delete_shared_memory();
    }
}//extern "C"


text:
// Copyright 2020 Josh Pieper, jjp@pobox.com.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <boost/date_time/posix_time/posix_time.hpp>

#include "mjlib/base/clipp.h"

#include "mjlib/base/buffer_stream.h"

#include "mjlib/telemetry/emit_json.h"
#include "mjlib/telemetry/file_reader.h"

using FileReader = mjlib::telemetry::FileReader;

int main(int argc, char**argv) {
  std::vector<std::string> names;
  std::string log_filename;

  auto group = clipp::group(
      clipp::repeatable(
          (clipp::option("n", "name") & clipp::value("", names))
          % "names to include"),
      clipp::value("LOG", log_filename)
  );

  mjlib::base::ClippParse(argc, argv, group);

  FileReader file_reader(log_filename);

  FileReader::ItemsOptions options;
  options.records = names;

  for (const auto item : file_reader.items(options)) {
    mjlib::base::BufferReadStream stream(item.data);
    std::cout << "\"" << item.timestamp << "\" ";
    EmitJson(std::cout, item.record->schema->root(), stream);
    std::cout << "\n";
  }

  return 0;
}

ext: cc


text:
// Copyright (c) 2018 Evan S Weinberg
// Thick restarted and deflated Lanczos.
// Points out where some type of polynomial
// acceleration could go in.
// Based on arXiv:1512:08135

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <sstream>
#include <complex>
#include <random>
#include <vector>

// Borrow dense matrix eigenvalue routines.
#include <Eigen/Dense>

#include "blas/generic_vector.h"

#include "square_laplace.h"

using namespace std;
using namespace Eigen;

typedef Matrix<double, Dynamic, Dynamic, ColMajor> dMatrix;
typedef Matrix<std::complex<double>, Dynamic, Dynamic, ColMajor> cMatrix;

int main(int argc, char** argv)
{
  complex<double> *rhs_cplx;

  // Set output precision to be long.
  cout << setprecision(10);

  // RNG related things.
  std::mt19937 generator (1337u); // RNG, 1337u is the seed.

  ///////////////////////////////////
  // Properties of Linear operator //
  ///////////////////////////////////
  double inv_variance = 6.0; // inverse of variance for gaussian non-compact U(1) links.

  // Basic information about the lattice.
  int length = 8;
  double m_sq = 0.001;

  // Some start-up.
  int volume = length*length;

  // Create a random compact U(1) link.
  complex<double>* gauge_links = allocate_vector<complex<double>>(2*length*length);
  gaussian_real(gauge_links, 2*length*length, generator, 1.0/inv_variance);
  polar(gauge_links, 2*length*length);

  // Vectors.
  rhs_cplx = allocate_vector<complex<double>>(volume);

  // Zero out the vector.
  zero_vector(rhs_cplx, length*length);

  // Structure which gets passed to the function.
  laplace_gauged_struct lapstr_gauged;
  lapstr_gauged.length = length;
  lapstr_gauged.m_sq = m_sq;
  lapstr_gauged.gauge_links = gauge_links;

  // Uncomment this to get the free field.
  //constant_vector(gauge_links, 1.0, 2*volume);

  //////////////////////////////////////
  // Properties for Lanczos algorithm //
  //////////////////////////////////////

  int max_iterations = 20; // maximum number of restarts
  int max_no_TR_candidates = 2; // maximum number of times we press on when there's
                                // no TR candidate
  int max_no_TM_eig_candidates = 2; // maximum number of times we press on when there's
                                    // no candidates in TM
  int m = 20; // Max subspace size before restart
  double tol = 1e-8; // Tolerance of eigenvalue computation

  // We're looking to grab all eigenvalues in the interval below
  // If we switched to a polynomial acceleration scheme, we'd look
  // for all eigenvalues above some value.
  // This is a choice! I'll make it clear below where to modify
  // this stopping condition.
  double eig_ceil = 2.0;
  double eig_floor = 1e-6; // Avoid deflated modes

  //////////////////////////////////////////
  // Prepare memory for Lanczos algorithm //
  //////////////////////////////////////////

  // Allocate Lanczos space. Extra vector needed for restarts.
  complex<double>** Q = new complex<double>*[m+1];
  for (int i = 0; i < m+1; i++) {
    Q[i] = allocate_vector<complex<double>>(volume);
  }

  // Extra beta for restarts
  double beta_restart = 0.0;

  // Storage for locked vectors!
  std::vector<complex<double>> lambda;
  std::vector<complex<double>*> U;

  // Intermediate vectors
  complex<double>* w = allocate_vector<complex<double>>(volume);
  complex<double>* lambda_x_u = allocate_vector<complex<double>>(volume);

  // Allocate T_m matrix (tridiagonal in un-restarted case).
  // Can generally be complex because of the restart
  cMatrix Tm = cMatrix::Zero(m,m);

  // Prepare an eigensolver
  SelfAdjointEigenSolver<cMatrix> eigsolve_Tm(m);

  ////////////////////////
  // Begin the Lanczos! //
  ////////////////////////

  // Prepare a random unit vector for Q[0]!
  gaussian(Q[0], volume, generator);
  normalize(Q[0], volume);

  int iters = 0;
  int l = 0;
  int locked = 0;
  int num_no_TR_candidates = 0;
  int num_no_TM_eig_candidates = 0;

  // Let's gooooo
  while (iters < max_iterations) {
    // last bit of thick restart. put s in correct place.
    if (l > 0) {
      for (int i = 0; i < l; i++) {
        Tm(l,i) = Tm(m-1,i);
        Tm(m-1,i) = 0.0;
        Tm(i,l) = conj(Tm(l,i));
      }
    }

    // If we're not on the first iter, T_m has values filled in.
    // Fill out rest of T_m. Standard lanczos!
    for (int i = l; i < m; i++) {
      zero_vector(w, volume);

      // PUT IN POLY ACCELERATION HERE
      square_laplacian_gauged(w, Q[i], &lapstr_gauged); // w = B q_i
      // deflate
      for (int j = 0; j < locked; j++) {
        complex<double> dotprod = dot(U[j], w, volume);
        caxpy(-dotprod, U[j], w, volume);
      }

      if (i > l) {
        caxpy(-Tm(i-1,i), Q[i-1], w, volume); // w -= beta_i q_{i-1}
      }

      // Compute alpha_i
      Tm(i,i) = re_dot(Q[i], w, volume);

      // w -= alpha_i q_i
      caxpy(-Tm(i,i), Q[i], w, volume);

      // Reorthogonalize
      for (int j = 0; j < i; j++) {
        orthogonal(w, Q[j], volume);
      }

      // Compute beta_{i+1}
      double beta = sqrt(norm2sq(w,volume));

      if (i == m-1) {
        beta_restart = beta;
      } else {
        Tm(i+1,i) = Tm(i,i+1) = beta;
      }

      // Check for breakdown
      if (beta < 1e-10) {
        gaussian(Q[i+1], volume, generator);
        normalize(Q[i+1], volume);
      } else {
        caxy(1.0/beta, w, Q[i+1], volume);
      }
    }

    iters++;

    // Calculate the Ritz values
    eigsolve_Tm.compute(Tm);
    std::cout << "The Ritz values are:\n" << eigsolve_Tm.eigenvalues().transpose() << "\n";
    std::cout << std::flush;

    // Compute candidate pairs---evals between the min and max.
    // There's lots of potential for memory reuse here...
    std::vector<complex<double>> theta_candidate;
    std::vector<complex<double>> s_candidate; // used for restart
    std::vector<complex<double>*> u_candidate;

    for (int i = 0; i < m; i++) {
      double eval = eigsolve_Tm.eigenvalues()(i);
      if (eval > eig_floor && eval < eig_ceil) {
        // Add it as a candidate pair
        theta_candidate.push_back(eval);
        s_candidate.push_back(beta_restart*eigsolve_Tm.eigenvectors().col(i)(m-1));

        // Build up the Ritz vector
        u_candidate.push_back(allocate_vector<complex<double>>(volume));
        zero_vector(u_candidate[u_candidate.size()-1], volume);
        for (int j = 0; j < m; j++) {
          caxpy(eigsolve_Tm.eigenvectors().col(i)(j), Q[j], u_candidate[u_candidate.size()-1], volume);
        }
      }
    }

    // Zero out all Q except the last one!
    for (int i = 0; i < m; i++) {
      zero_vector(Q[i], volume);
    }

    int num_candidate = theta_candidate.size();

    // if no candidates were found, quit.
    // Might be a sign we've found all the eigenvalues... or not.
    // Needs a smarter heuristic. Again, should be safer
    // with poly accel.
    if (num_candidate == 0) {

      num_no_TM_eig_candidates++;
      std::cout << "No TM eigenvalue candidates: " << num_no_TM_eig_candidates << ".\n";
      if (num_no_TM_eig_candidates == max_no_TM_eig_candidates) {
        std::cout << "Max number of times with no candidate TM eigenvalues reached.\n";
        break;
      } else {
        l = 0;
        Tm = cMatrix::Zero(m,m);
        copy_vector(Q[0], Q[m], volume);
        continue;
      }
    }

    // Reset l
    l = 0;

    // Zero out Tm, start rebuilding
    Tm = cMatrix::Zero(m,m);

    for (int i = 0; i < num_candidate; i++) {
      // Compute Rayleigh value. Technically wasteful
      // because we aren't doing poly accel.
      // Reuse w.
      zero_vector(w, volume);
      square_laplacian_gauged(w, u_candidate[i], &lapstr_gauged);

      double rayleigh = re_dot(u_candidate[i], w, volume);

      // Make sure we're in the target range
      if (rayleigh > eig_ceil || rayleigh < eig_floor) { continue; }

      // check convergence
      caxy(rayleigh, u_candidate[i], lambda_x_u, volume);
      if (sqrt(diffnorm2sq(w, lambda_x_u, volume))/rayleigh < tol) {
        // We've got a lock!
        lambda.push_back(rayleigh);
        U.push_back(allocate_vector<complex<double>>(volume));
        copy_vector(U[locked], u_candidate[i], volume);
        normalize(U[locked], volume);
        locked++;
      } else {
        // Add it to the thick restart set
        copy_vector(Q[l], u_candidate[i], volume);
        Tm(l,l) = eigsolve_Tm.eigenvalues()(i);

        // Store s somewhere safe
        Tm(m-1,l) = s_candidate[i];

        l++;
      }
    }

    // Clean up candidates.
    for (int i = 0; i < num_candidate; i++) {
      deallocate_vector(&u_candidate[i]);
    }

    // If there are no vectors in the thick restart set, quit.
    if (l == 0) {
      num_no_TR_candidates++;
      std::cout << "No thick restart vectors: " << num_no_TR_candidates << ".\n";
      if (num_no_TR_candidates == max_no_TR_candidates) {
        std::cout << "Max number of times with no thick restart vectors reached.\n";
        break;
      }
    }

    // Put last Q in place.
    copy_vector(Q[l], Q[m], volume);

  }

  if (iters == max_iterations) {
    std::cout << "Hit maximum iterations.\n" << "\n";
  }

  // Uh... I guess we have eigenvalues.
  std::cout << "Computed eigenvalues.\n";
  for (unsigned int i = 0; i < lambda.size(); i++) {
    std::cout << lambda[i] << "\n";
  }
  std::cout << "\n";

  /**/

  if (volume <= 256) {

    // Let's get the eigenvalues of the full operator!

    // Allocate a sufficiently gigantic matrix.
    cMatrix mat_cplx = cMatrix::Zero(volume, volume);

    // Form matrix elements. This is where it's important that
    // dMatrix and cMatrix are column major.
    // I should probably make this safer by using a "Map".
    for (int i = 0; i < volume; i++)
    {
      // Set a point on the rhs for a matrix element.
      zero_vector(rhs_cplx, volume);
      rhs_cplx[i] = 1.0;

      // Where we put the result of the matrix element.
      complex<double>* mptr = &(mat_cplx(i*volume));

      square_laplacian_gauged(mptr, rhs_cplx, &lapstr_gauged);
    }

    // Get the eigenvalues.
    SelfAdjointEigenSolver<cMatrix> eigsolve_cplx(volume);
    eigsolve_cplx.compute(mat_cplx);

    std::cout << "The eigenvalues are:\n" << eigsolve_cplx.eigenvalues() << "\n";

    ////////////////////////////////
    // COMPARE LOWEST EIGENVECTOR //
    ////////////////////////////////

    std::cout << "Compare results:\n\n";
    std::cout << "Lanczos Exact Ratio\n";
    for (int i = 0; i < volume; i++) {
      std::cout << U[0][i] << " " << eigsolve_cplx.eigenvectors().col(0)(i)
                << " " << U[0][i]/eigsolve_cplx.eigenvectors().col(0)(i) << "\n";
    }
  }

  //////////////
  // CLEAN UP //
  //////////////

  deallocate_vector(&w);
  deallocate_vector(&lambda_x_u);

  for (int i = 0; i <= m; i++) {
    deallocate_vector(&Q[i]);
  }
  delete[] Q;

  deallocate_vector(&rhs_cplx);
  deallocate_vector(&gauge_links);
  return 0;
}