text:
/**************************************************************
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 *
 *************************************************************/

#ifndef INCLUDED_SERFSESSION_HXX
#define INCLUDED_SERFSESSION_HXX

#include <vector>
#include <boost/shared_ptr.hpp>
#include <osl/mutex.hxx>
#include "DAVResource.hxx"
#include "DAVSession.hxx"
#include "SerfTypes.hxx"
#include "SerfLockStore.hxx"
#include "SerfUri.hxx"
#include <com/sun/star/lang/XMultiServiceFactory.hpp>

namespace ucbhelper { class ProxyDecider; }

namespace http_dav_ucp
{

class SerfRequestProcessor;

// -------------------------------------------------------------------
// SerfSession
// A DAVSession implementation using the neon/expat library
// -------------------------------------------------------------------

class SerfSession : public DAVSession
{
private:
    osl::Mutex              m_aMutex;

    SerfUri                 m_aUri;

    rtl::OUString           m_aProxyName;
    sal_Int32               m_nProxyPort;
    // The server, according RFC7231
    // http://tools.ietf.org/html/rfc7231#section-7.4.2
    rtl::OUString           m_aServerHeaderField;

    SerfConnection*         m_pSerfConnection;
    serf_context_t*         m_pSerfContext;
    serf_bucket_alloc_t*    m_pSerfBucket_Alloc;
    bool                    m_bIsHeadRequestInProgress;
    bool                    m_bUseChunkedEncoding;
    sal_Int16               m_bNoOfTransferEncodingSwitches;

    const ucbhelper::InternetProxyDecider & m_rProxyDecider;

    DAVRequestEnvironment m_aEnv;

    static SerfLockStore m_aSerfLockStore;

    char* getHostinfo();
    bool isSSLNeeded();

    SerfRequestProcessor*   createReqProc( const rtl::OUString & inPath );

    rtl::OUString           composeCurrentUri( const rtl::OUString & inPath );

protected:
    virtual ~SerfSession();

public:
    SerfSession( const rtl::Reference< DAVSessionFactory > & rSessionFactory,
                 const rtl::OUString& inUri,
                 const ucbhelper::InternetProxyDecider & rProxyDecider )
        throw ( DAVException );

    // Serf library callbacks
    apr_status_t setupSerfConnection( apr_socket_t * inAprSocket,
                                      serf_bucket_t **outSerfInputBucket,
                                      serf_bucket_t **outSerfOutputBucket,
                                      apr_pool_t* inAprPool );

    apr_status_t provideSerfCredentials( bool bGiveProvidedCredentialsASecondTry,
                                         char ** outUsername,
                                         char ** outPassword,
                                         serf_request_t * inRequest,
                                         int inCode,
                                         const char *inAuthProtocol,
                                         const char *inRealm,
                                         apr_pool_t *inAprPool );

    apr_status_t verifySerfCertificateChain (
        int nFailures,
        const serf_ssl_certificate_t * const * pCertificateChainBase64Encoded,
        int nCertificateChainLength);

    serf_bucket_t* acceptSerfResponse( serf_request_t * inSerfRequest,
                                       serf_bucket_t * inSerfStreamBucket,
                                       apr_pool_t* inAprPool );

    // Serf-related data structures
    apr_pool_t* getAprPool();
    serf_bucket_alloc_t* getSerfBktAlloc();
    serf_context_t* getSerfContext();
    SerfConnection* getSerfConnection();

    // DAVSession methods
    virtual sal_Bool CanUse( const ::rtl::OUString & inUri );

    virtual sal_Bool UsesProxy();

    const DAVRequestEnvironment & getRequestEnvironment() const
    { return m_aEnv; }

    // allprop & named
    virtual void
    PROPFIND( const ::rtl::OUString & inPath,
              const Depth inDepth,
              const std::vector< ::rtl::OUString > & inPropNames,
              std::vector< DAVResource > & ioResources,
              const DAVRequestEnvironment & rEnv )
        throw ( DAVException );

    // propnames
    virtual void
    PROPFIND( const ::rtl::OUString & inPath,
              const Depth inDepth,
              std::vector< DAVResourceInfo >& ioResInfo,
              const DAVRequestEnvironment & rEnv )
        throw ( DAVException );

    virtual void
    PROPPATCH( const ::rtl::OUString & inPath,
               const std::vector< ProppatchValue > & inValues,
               const DAVRequestEnvironment & rEnv )
        throw ( DAVException );

    virtual void
    HEAD( const ::rtl::OUString &  inPath,
          const std::vector< ::rtl::OUString > & inHeaderNames,
          DAVResource & ioResource,
          const DAVRequestEnvironment & rEnv )
        throw ( DAVException );

    bool isHeadRequestInProgress();

    virtual com::sun::star::uno::Reference< com::sun::star::io::XInputStream >
    GET( const ::rtl::OUString & inPath,
         const DAVRequestEnvironment & rEnv )
        throw ( DAVException );

    virtual void
    GET( const ::rtl::OUString & inPath,
         com::sun::star::uno::Reference<
             com::sun::star::io::XOutputStream > &  ioOutputStream,
         const DAVRequestEnvironment & rEnv )
        throw ( DAVException );

    virtual com::sun::star::uno::Reference< com::sun::star::io::XInputStream >
    GET( const ::rtl::OUString & inPath,
         const std::vector< ::rtl::OUString > & inHeaderNames,
         DAVResource & ioResource,
         const DAVRequestEnvironment & rEnv )
        throw ( DAVException );

    virtual void
    GET( const ::rtl::OUString & inPath,
         com::sun::star::uno::Reference<
             com::sun::star::io::XOutputStream > & ioOutputStream,
         const std::vector< ::rtl::OUString > & inHeaderNames,
         DAVResource & ioResource,
         const DAVRequestEnvironment & rEnv )
        throw ( DAVException );

    virtual void
    PUT( const ::rtl::OUString & inPath,
         const com::sun::star::uno::Reference<
             com::sun::star::io::XInputStream > & inInputStream,
         const DAVRequestEnvironment & rEnv )
        throw ( DAVException );

    virtual com::sun::star::uno::Reference< com::sun::star::io::XInputStream >
    POST( const rtl::OUString & inPath,
          const rtl::OUString & rContentType,
          const rtl::OUString & rReferer,
          const com::sun::star::uno::Reference<
              com::sun::star::io::XInputStream > & inInputStream,
          const DAVRequestEnvironment & rEnv )
        throw ( DAVException );

    virtual void
    POST( const rtl::OUString & inPath,
          const rtl::OUString & rContentType,
          const rtl::OUString & rReferer,
          const com::sun::star::uno::Reference<
              com::sun::star::io::XInputStream > & inInputStream,
          com::sun::star::uno::Reference<
              com::sun::star::io::XOutputStream > & oOutputStream,
          const DAVRequestEnvironment & rEnv )
        throw ( DAVException );

    virtual void
    MKCOL( const ::rtl::OUString & inPath,
           const DAVRequestEnvironment & rEnv )
        throw ( DAVException );

    virtual void
    COPY( const ::rtl::OUString & inSourceURL,
          const ::rtl::OUString & inDestinationURL,
          const DAVRequestEnvironment & rEnv,
          sal_Bool inOverWrite )
        throw ( DAVException );

    virtual void
    MOVE( const ::rtl::OUString & inSourceURL,
          const ::rtl::OUString & inDestinationURL,
          const DAVRequestEnvironment & rEnv,
          sal_Bool inOverWrite )
        throw ( DAVException );

    virtual void DESTROY( const ::rtl::OUString & inPath,
                          const DAVRequestEnvironment & rEnv )
        throw ( DAVException );

    // set new lock.
    virtual void LOCK( const ::rtl::OUString & inURL,
                       com::sun::star::ucb::Lock & inLock,
                       const DAVRequestEnvironment & rEnv )
        throw ( DAVException );

    // refresh existing lock.
    virtual sal_Int64 LOCK( const ::rtl::OUString & inURL,
                            sal_Int64 nTimeout,
                            const DAVRequestEnvironment & rEnv )
        throw ( DAVException );

    virtual void UNLOCK( const ::rtl::OUString & inURL,
                         const DAVRequestEnvironment & rEnv )
        throw ( DAVException );

    // helpers
    virtual void abort()
        throw ( DAVException );

    const rtl::OUString & getHostName() const { return m_aUri.GetHost(); }
    int getPort() const { return m_aUri.GetPort(); }

    const ::uno::Reference< ::lang::XMultiServiceFactory > getMSF()
    { return m_xFactory->getServiceFactory(); }

    sal_Bool isDomainMatch( rtl::OUString certHostName );

    const rtl::OUString & getServerHeaderField() { return m_aServerHeaderField; };

    void setServerHeaderField( rtl::OUString aServerHeaderField ) { m_aServerHeaderField = aServerHeaderField; };

private:
    friend class SerfLockStore;

    void Init( void )
        throw ( DAVException );

    void Init( const DAVRequestEnvironment & rEnv )
        throw ( DAVException );

    void HandleError( boost::shared_ptr<SerfRequestProcessor> rReqProc )
        throw ( DAVException );

    const ucbhelper::InternetProxyServer & getProxySettings() const;

    bool removeExpiredLocktoken( const rtl::OUString & inURL,
                                 const DAVRequestEnvironment & rEnv );

    // refresh lock, called by SerfLockStore::refreshLocks
    bool LOCK( SerfLock * pLock,
               sal_Int32 & rlastChanceToSendRefreshRequest );

    // unlock, called by SerfLockStore::~SerfLockStore
    bool UNLOCK( SerfLock * pLock );

    /*
    // low level GET implementation, used by public GET implementations
    static int GET( SerfConnection * sess,
                    const char * uri,
                    //ne_block_reader reader,
                    bool getheaders,
                    void * userdata );

    // Buffer-based PUT implementation. Serf only has file descriptor-
    // based API.
    static int PUT( SerfConnection * sess,
                    const char * uri,
                    const char * buffer,
                    size_t size );

    // Buffer-based POST implementation. Serf only has file descriptor-
    // based API.
    int POST( SerfConnection * sess,
              const char * uri,
              const char * buffer,
              //ne_block_reader reader,
              void * userdata,
              const rtl::OUString & rContentType,
              const rtl::OUString & rReferer );
    */

    // Helper: XInputStream -> Sequence< sal_Int8 >
    static bool getDataFromInputStream(
        const com::sun::star::uno::Reference<
            com::sun::star::io::XInputStream > & xStream,
        com::sun::star::uno::Sequence< sal_Int8 > & rData,
        bool bAppendTrailingZeroByte );

    /*
    rtl::OUString makeAbsoluteURL( rtl::OUString const & rURL ) const;
    */
};

} // namespace http_dav_ucp

#endif // INCLUDED_SERFSESSION_HXX

max_stars_repo_name: Grosskopf/openoffice

max_issues_repo_name: Grosskopf/openoffice

max_forks_repo_name: Grosskopf/openoffice


text:
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE "udho Unit Test (udho::activity)"
#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string/trim_all.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/regex.hpp>
#include <boost/algorithm/string_regex.hpp>
#include <udho/router.h>
#include <boost/lexical_cast.hpp>
#include <boost/bind.hpp>
#include <udho/server.h>
#include <iostream>
#include <udho/configuration.h>
#include <udho/activities.h>

#define SMALL_TIMEOUT 2
#define LARGE_TIMEOUT 4

struct A1SData{
    int value;

    A1SData(): value(0){}
};

struct A1FData{
    int reason;
};

struct A1: udho::activity<A1, A1SData, A1FData>{
    typedef udho::activity<A1, A1SData, A1FData> base;

    boost::asio::deadline_timer _timer;
    bool _succeed;

    template <typename CollectorT>
    A1(CollectorT c, boost::asio::io_context& io, bool succeed = true): base(c), _timer(io), _succeed(succeed){}

    void operator()(){
        _timer.expires_from_now(boost::posix_time::seconds(SMALL_TIMEOUT));
        _timer.async_wait(boost::bind(&A1::finished, self(), boost::asio::placeholders::error));
    }

    void finished(const boost::system::error_code& e){
        if(_succeed){
            A1SData data;
            data.value = 42;
            success(data);
        }else{
            A1FData data;
            data.reason = 100;
            failure(data);
        }
    }
};

struct A2SData{
    int value;
};

struct A2FData{
    int reason;
};

struct A2: udho::activity<A2, A2SData, A2FData>{
    typedef udho::activity<A2, A2SData, A2FData> base;

    boost::asio::deadline_timer _timer;
    udho::accessor<A1> _accessor;

    template <typename CollectorT>
    A2(CollectorT c, boost::asio::io_context& io): base(c), _timer(io), _accessor(c){}

    void operator()(){
        _timer.expires_from_now(boost::posix_time::seconds(LARGE_TIMEOUT));
        _timer.async_wait(boost::bind(&A2::finished, self(), boost::asio::placeholders::error));
    }

    void finished(const boost::system::error_code& err){
        if(!err && !_accessor.failed<A1>()){
            A1SData pre = _accessor.success<A1>();
            A2SData data;
            data.value = pre.value + 2;
            success(data);
        }
    }
};

struct A2i: udho::activity<A2i, A2SData, A2FData>{
    typedef udho::activity<A2i, A2SData, A2FData> base;

    int prevalue;
    boost::asio::deadline_timer _timer;

    template <typename CollectorT>
    A2i(CollectorT c, boost::asio::io_context& io, int p): base(c), prevalue(p), _timer(io){}

    template <typename CollectorT>
    A2i(CollectorT c, boost::asio::io_context& io): base(c), _timer(io){}

    void operator()(){
        _timer.expires_from_now(boost::posix_time::seconds(LARGE_TIMEOUT));
        _timer.async_wait(boost::bind(&A2i::finished, self(), boost::asio::placeholders::error));
    }

    void finished(const boost::system::error_code& err){
        if(!err){
            A2SData data;
            data.value = prevalue + 2;
            success(data);
        }
    }
};

struct A3SData{
    int value;
};

struct A3FData{
    int reason;
};

struct A3: udho::activity<A3, A3SData, A3FData>{
    typedef udho::activity<A3, A3SData, A3FData> base;

    boost::asio::deadline_timer _timer;
    udho::accessor<A1> _accessor;

    template <typename CollectorT>
    A3(CollectorT c, boost::asio::io_context& io): base(c), _timer(io), _accessor(c){}

    void operator()(){
        _timer.expires_from_now(boost::posix_time::seconds(SMALL_TIMEOUT));
        _timer.async_wait(boost::bind(&A3::finished, self(), boost::asio::placeholders::error));
    }

    void finished(const boost::system::error_code& err){
        if(!err && !_accessor.failed<A1>()){
            A1SData pre = _accessor.success<A1>();
            A3SData data;
            data.value = pre.value * 2;
            success(data);
        }
    }
};

struct A3i: udho::activity<A3i, A3SData, A3FData>{
    typedef udho::activity<A3i, A3SData, A3FData> base;

    boost::asio::deadline_timer _timer;
    int prevalue;

    template <typename CollectorT>
    A3i(CollectorT c, boost::asio::io_context& io): base(c), _timer(io){}

    void operator()(){
        _timer.expires_from_now(boost::posix_time::seconds(SMALL_TIMEOUT));
        _timer.async_wait(boost::bind(&A3i::finished, self(), boost::asio::placeholders::error));
    }

    void finished(const boost::system::error_code& err){
        if(!err){
            A3SData data;
            data.value = prevalue * 2;
            success(data);
        }
    }
};

void unprepared(udho::contexts::stateless ctx){
    auto& io = ctx.aux()._io;

    auto data = udho::collect<A1, A2, A3>(ctx, "A");

    auto t1 = udho::perform<A1>::with(data, io);
    auto t2 = udho::perform<A2>::require<A1>::with(data, io).after(t1);
    auto t3 = udho::perform<A3>::require<A1>::with(data, io).after(t1);

    udho::require<A2, A3>::with(data).exec([ctx](const udho::accessor<A1, A2, A3>& d) mutable{
        BOOST_CHECK(d.completed<A1>());
        BOOST_CHECK(d.completed<A2>());
        BOOST_CHECK(d.completed<A3>());
        BOOST_CHECK(!d.failed<A1>());
        BOOST_CHECK(!d.failed<A2>());
        BOOST_CHECK(!d.failed<A3>());

        int sum = 0;

        if(!d.failed<A2>()){
            A2SData pre = d.success<A2>();
            sum += pre.value;
        }

        if(!d.failed<A3>()){
            A3SData pre = d.success<A3>();
            sum += pre.value;
        }

        ctx.respond(boost::lexical_cast<std::string>(sum), "text/plain");
    }).after(t2).after(t3);

    t1();
}

void prepared(udho::contexts::stateless ctx){
    auto& io = ctx.aux()._io;

    auto data = udho::collect<A1, A2i, A3i>(ctx, "Ai");

    auto t1 = udho::perform<A1>::with(data, io);
    auto t2 = udho::perform<A2i>::require<A1>::with(data, io).after(t1).prepare([data](A2i& a2i){
        udho::accessor<A1> a1_access(data);
        A1SData pre = a1_access.success<A1>();
        a2i.prevalue = pre.value;
    });
    auto t3 = udho::perform<A3i>::require<A1>::with(data, io).after(t1).prepare([data](A3i& a3i){
        udho::accessor<A1> a1_access(data);
        A1SData pre = a1_access.success<A1>();
        a3i.prevalue = pre.value;
    });

    udho::require<A2i, A3i>::with(data).exec([ctx](const udho::accessor<A1, A2i, A3i>& d) mutable{
        BOOST_CHECK(d.completed<A1>());
        BOOST_CHECK(d.completed<A2i>());
        BOOST_CHECK(d.completed<A3i>());
        BOOST_CHECK(!d.failed<A1>());
        BOOST_CHECK(!d.failed<A2i>());
        BOOST_CHECK(!d.failed<A3i>());

        int sum = 0;

        if(!d.failed<A2i>()){
            A2SData pre = d.success<A2i>();
            sum += pre.value;
        }

        if(!d.failed<A3i>()){
            A3SData pre = d.success<A3i>();
            sum += pre.value;
        }

        ctx.respond(boost::lexical_cast<std::string>(sum), "text/plain");
    }).after(t2).after(t3);

    t1();
}

void unprepared_a1_fail(udho::contexts::stateless ctx){
    auto& io = ctx.aux()._io;

    auto data = udho::collect<A1, A2, A3>(ctx, "A");

    auto t1 = udho::perform<A1>::with(data, io, false);
    auto t2 = udho::perform<A2>::require<A1>::with(data, io).after(t1);
    auto t3 = udho::perform<A3>::require<A1>::with(data, io).after(t1);

    udho::require<A2, A3>::with(data).exec([ctx](const udho::accessor<A1, A2, A3>& d) mutable{
        BOOST_CHECK(d.completed<A1>());
        BOOST_CHECK(d.failed<A1>());
        BOOST_CHECK(!d.completed<A2>());
        BOOST_CHECK(!d.completed<A3>());

        A1FData pre = d.failure<A1>();

        ctx.respond(boost::lexical_cast<std::string>(pre.reason), "text/plain");
    }).after(t2).after(t3);

    t1();
}

BOOST_AUTO_TEST_SUITE(activity)

BOOST_AUTO_TEST_CASE(unprepared_subtasks){
    boost::asio::io_service io;
    udho::servers::quiet::stateless server(io);
    auto urls = udho::router()
        | (udho::get(&unprepared).deferred() = "^/unprepared");
    server.serve(urls, 9198);

    udho::servers::quiet::stateless::request_type req;
    udho::servers::quiet::stateless::attachment_type attachment(io);
    udho::contexts::stateless ctx(attachment.aux(), req, attachment);

    ctx.client().get("http://localhost:9198/unprepared")
        .done([ctx, &io](boost::beast::http::status status, const std::string& body) mutable {
            BOOST_CHECK(status == boost::beast::http::status::ok);
            BOOST_CHECK(body == "128");
            io.stop();
        });

    io.run();
}

BOOST_AUTO_TEST_CASE(prepared_subtasks){
    boost::asio::io_service io;
    udho::servers::quiet::stateless server(io);
    auto urls = udho::router()
        | (udho::get(&prepared).deferred() = "^/prepared");
    server.serve(urls, 9198);

    udho::servers::quiet::stateless::request_type req;
    udho::servers::quiet::stateless::attachment_type attachment(io);
    udho::contexts::stateless ctx(attachment.aux(), req, attachment);

    ctx.client().get("http://localhost:9198/prepared")
        .done([ctx, &io](boost::beast::http::status status, const std::string& body) mutable {
            BOOST_CHECK(status == boost::beast::http::status::ok);
            BOOST_CHECK(body == "128");
            io.stop();
        });

    io.run();
}

BOOST_AUTO_TEST_CASE(unprepared_subtasks_a1_fail){
    boost::asio::io_service io;
    udho::servers::quiet::stateless server(io);
    auto urls = udho::router()
        | (udho::get(&unprepared_a1_fail).deferred() = "^/unprepared_a1_fail");
    server.serve(urls, 9198);

    udho::servers::quiet::stateless::request_type req;
    udho::servers::quiet::stateless::attachment_type attachment(io);
    udho::contexts::stateless ctx(attachment.aux(), req, attachment);

    ctx.client().get("http://localhost:9198/unprepared_a1_fail")
        .done([ctx, &io](boost::beast::http::status status, const std::string& body) mutable {
            BOOST_CHECK(status == boost::beast::http::status::ok);
            BOOST_CHECK(body == "100");
            io.stop();
        });

    io.run();
}

BOOST_AUTO_TEST_SUITE_END()

ext: cpp

max_stars_repo_name: neel/udho

max_issues_repo_name: neel/udho

max_forks_repo_name: neel/udho


text:
#include <fmt/core.h>
#include <yaml-cpp/yaml.h>

#include <Eigen/Dense>
#include <Eigen/Eigenvalues>
#include <boost/math/quadrature/gauss.hpp>
#include <iostream>
#include <numeric>
#include <optional>
#include <vector>

#include "NumpySaver.hpp"
#include "bsplines.hpp"

using namespace Eigen;
using std::cout, std::endl, std::cerr;

const char* devider = "##########################################";

template <int num_points>
double matrix_B_element(int i, int j, std::vector<double>& knots, Index order) {
  using boost::math::quadrature::gauss;

  if (std::abs((int)i - (int)j) > order) return 0;

  int imax = std::max(i, j);
  int imin = std::min(i, j);

  Index n = knots.size();

  double sum = 0;

  for (int k = std::max(0, imax - 1); k < std::min(imin + order + 1, n - 1);
       k++) {
    auto f = [&](const double& t) {
      auto [values, idx] = bsplines::bsplines(t, knots, order);
      return values[i + order - idx] * values[j + order - idx];
    };
    sum += gauss<double, num_points>::integrate(f, knots[k], knots[k + 1]);
  }

  return sum;
}

template <int num_points>
double matrix_H_element(int i, int j, std::vector<double>& knots, Index order,
                        int l, int z) {
  using boost::math::quadrature::gauss;
  if (std::abs((int)i - (int)j) > order) return 0;

  Index n = knots.size();
  int imax = std::max(i, j);
  int imin = std::min(i, j);

  double sum = 0;

  for (int k = std::max(0, imax - 1); k < std::min(imin + order + 1, n - 1);
       k++) {
    auto f = [&](const double& t) {
      auto [values, idx] = bsplines::bsplines(t, knots, order);
      auto [values_dd, idx_dd] = bsplines::ndx_bsplines(t, knots, order, 2);
      double h_val =
          -values_dd[j - (idx - order)] / 2. +
          (l * (l + 1.) / (t * t * 2.) - z / t) * values[j - (idx - order)];
      return values[i - (idx - order)] * h_val;
      ;
    };

    sum += gauss<double, num_points>::integrate(f, knots[k], knots[k + 1]);
  }
  return sum;
}

template <int num_points>
MatrixXd matrix_B(std::vector<double>& knots, Index order) {
  Index n = knots.size();
  MatrixXd mat(n - 3 + order, n - 3 + order);

  for (int row = 0; row < mat.rows(); row++)
    for (int col = 0; col < mat.cols(); col++)
      mat(row, col) = matrix_B_element<num_points>(
          row - order + 1, col - order + 1, knots, order);

  return mat;
}

template <int num_points>
MatrixXd matrix_H(std::vector<double>& knots, Index order, int l, int z) {
  Index n = knots.size();
  MatrixXd mat(n - 3 + order, n - 3 + order);

  for (int row = 0; row < mat.rows(); row++)
    for (int col = 0; col < mat.cols(); col++)
      mat(row, col) = matrix_H_element<num_points>(
          row - order + 1, col - order + 1, knots, order, l, z);

  return mat;
}

double evaluate_spline(const double& x, const std::vector<double>& knots,
                       const VectorXd& weights, std::size_t order) {
  auto n = knots.size();
  assert(int(n + order) - 3 == weights.size());

  auto [values, index] = bsplines::bsplines(x, knots, order);

  double sum = 0;
  for (std::size_t i = std::max(int(index) - 1, 0);
       i <= std::min(index - 1 + order, n + order - 4); i++)
    sum += values[i + 1 - index] * weights[i];

  return sum;
}

template <int order>
void solve(std::vector<double>& knots, std::string& basefilename,
           std::optional<double> plot_max, int l, int z) {
  constexpr int gauss_points = order * 1.5;
  int n_knots = knots.size();

  cout << "knots = ";
  for (auto k : knots) cout << k << ", ";
  cout << endl;

  auto B = matrix_B<gauss_points>(knots, order);
  auto H = matrix_H<gauss_points>(knots, order, l, z);

  if (knots.size() < 20) {
    cout << "B=\n" << B << "\n\n";
    cout << "H=\n" << H << endl;
  }

  GeneralizedEigenSolver<MatrixXd> ges;
  ges.compute(H, B);
  VectorXcd eigenvalues = ges.eigenvalues();
  VectorXd real_eigen = eigenvalues.real();

  // sort the eigenvalues (and create an index list)
  std::vector<int> indices(real_eigen.size());
  std::iota(indices.begin(), indices.end(), 0);

  std::sort(indices.begin(), indices.end(), [&](int A, int B) -> bool {
    return real_eigen[A] < real_eigen[B];
  });
  std::sort(real_eigen.data(), real_eigen.data() + real_eigen.size());

  if (eigenvalues.imag().cwiseAbs().sum() > 10e-10) {
    cerr << "Error: Complex eigenvalues!" << endl;
  }

  cout << "Eigenvalues: " << real_eigen.transpose() << endl;

  MatrixXd eigenvectors = ges.eigenvectors().real();

  NumpySaver(fmt::format("build/output/{}_eigenvalues.npy", basefilename))
      << real_eigen;
  NumpySaver(fmt::format("build/output/{}_knots.npy", basefilename)) << knots;

  NumpySaver eigvec_saver(
      fmt::format("build/output/{}_eigenvectors.npy", basefilename));
  for (int col = 0; col < eigenvectors.cols(); col++)
    eigvec_saver << eigenvectors.col(indices[col]);

  if (auto xmax = plot_max) {
    ArrayXd x = ArrayXd::LinSpaced(1000, 0, *xmax);
    MatrixXd points(x.size(), eigenvectors.cols());
    for (int row = 0; row < x.size(); row++)
      for (int col = 0; col < points.cols(); col++)
        points(row, col) = evaluate_spline(
            x[row], knots, eigenvectors.col(indices[col]), order);

    NumpySaver plot_saver(
        fmt::format("build/output/{}_plot_points.npy", basefilename));
    plot_saver << x;
    for (int col = 0; col < points.cols(); col++) plot_saver << points.col(col);
  }
}

template <int order>
void solve_exp(int n_knots, double rmin, double rmax, double exp_fak,
               std::string& basefilename, std::optional<double> plot, int l,
               int z) {
  std::vector<double> knots(n_knots);
  for (int i = 0; i < n_knots; i++)
    knots[i] = (rmax - rmin) *
                   (std::exp(double(i) / double(n_knots - 1) * exp_fak) - 1.) /
                   (std::exp(exp_fak) - 1) +
               rmin;

  solve<order>(knots, basefilename, plot, l, z);
}

template <int order>
void solve_lin(int n_knots, double rmin, double rmax, std::string& basefilename,
               std::optional<double> plot, int l, int z) {
  std::vector<double> knots(n_knots);
  for (int i = 0; i < n_knots; i++)
    knots[i] = double(i) / double(n_knots - 1) * (rmax - rmin) + rmin;

  solve<order>(knots, basefilename, plot, l, z);
}

int main() {
  YAML::Node configs = YAML::LoadFile("config.yaml");

  constexpr int order = 3;

  for (std::size_t i = 0; i < configs.size(); i++) {
    YAML::Node config = configs[i];

    std::optional<double> plot;

    if (auto p_node = config["plot_xmax"]) {
      try {
        auto val = p_node.as<double>();
        if (val > 0) plot.emplace(val);
      } catch (const YAML::TypedBadConversion<double>&) {
        auto boolval = p_node.as<bool>();
        if (boolval) plot.emplace(10);
      }
    }

    auto l = config["l"].as<int>();
    auto z = config["Z"].as<int>();
    auto basefilename = config["name"].as<std::string>();

    fmt::print("\n\n{0}\n# Simulation '{1}' {2}/{3}\n{0}\n", devider,
               basefilename, i + 1, configs.size());

    YAML::Node knot_node = config["knots"];
    auto type = knot_node.Type();
    // single value
    if (type == YAML::NodeType::Sequence) {
      auto knots = knot_node.as<std::vector<double>>();
      solve<order>(knots, basefilename, plot, l, z);
    } else if (type == YAML::NodeType::Map) {
      auto rmin = knot_node["rmin"].as<double>();
      auto rmax = knot_node["rmax"].as<double>();
      auto N = knot_node["N"].as<size_t>();
      auto type = knot_node["type"].as<std::string>();
      if (type == "linear") {
        solve_lin<order>(N, rmin, rmax, basefilename, plot, l, z);
      } else if (type == "exponential") {
        auto expfac = knot_node["expfac"].as<double>();
        solve_exp<order>(N, rmin, rmax, expfac, basefilename, plot, l, z);
      } else {
        throw std::runtime_error(
            "'type' of knots must either be 'linear' or 'exponential'.");
      }
    } else {
      throw std::runtime_error(
          "Range node must either be list, scalar or a map defining "
          "'type', 'N', 'rmin' and 'rmax'.");
    }
  }
}


text:
#include <boost/thread/condition_variable.hpp>
#include <pcl/point_types.h>
#include <pcl/conversions.h>
#include "../openni2.h"
#include "OpenNI2GrabberStream.h"

PCLIMP(void*, OpenNI2Stream, new)(const char* device_id, int max_backlog, bool grab_RGB = false, bool grab_Depth = false, bool grab_IR = false)
{
  try
  {
    return new OpenNI2GrabberStream<_PointT>(device_id, max_backlog, grab_RGB, grab_Depth, grab_IR);
  }
  catch (const std::exception& ex)
  {
    // ## temporary debug output, remove as soon as C++ exception translation method is implemented
    std::cout << ex.what() << std::endl;
    throw;
  }
}

PCLIMP(void, OpenNI2Stream, delete)(OpenNI2GrabberStream<_PointT> *self)
{
  delete self;
}

PCLIMP(void, OpenNI2Stream, start)(OpenNI2GrabberStream<_PointT> *self)
{
  try
  {
    self->start();
  }
  catch (const std::exception& ex)
  {
    // ## temporary debug output, remove as soon as C++ exception translation method is implemented
    std::cout << ex.what() << std::endl;
    throw;
  }
}

PCLIMP(void, OpenNI2Stream, stop)(OpenNI2GrabberStream<_PointT> *self)
{
  self->stop();
}

PCLIMP(void*, OpenNI2Stream, read)(OpenNI2GrabberStream<_PointT> *self, int timeout_milliseconds)
{
  pcl::PointCloud<_PointT>::Ptr cloud = self->read(timeout_milliseconds);
  if (!cloud)
    return 0;
  return new pcl::PointCloud<_PointT>::Ptr(cloud);
}

PCLIMP(void, OpenNI2Stream, readRGBImage)(OpenNI2GrabberStream<_PointT> *self, int timeout_milliseconds, THByteTensor* output)
{
  pcl::io::Image::ConstPtr ptr = self->readRGBImage(timeout_milliseconds);
  if (ptr)
  {
    unsigned int height = ptr->getHeight(), width = ptr->getWidth();
    THByteTensor_resize3d(output, height, width, 3);
    THByteTensor* output_ = THByteTensor_newContiguous(output);
    ptr->fillRGB(width, height, THByteTensor_data(output_));
    THByteTensor_freeCopyTo(output_, output);
  }
}

PCLIMP(void, OpenNI2Stream, readDepthImage)(OpenNI2GrabberStream<_PointT> *self, int timeout_milliseconds, THShortTensor* output)
{
  pcl::io::DepthImage::ConstPtr ptr = self->readDepthImage(timeout_milliseconds);
  if (ptr)
  {
    unsigned int height = ptr->getHeight(), width = ptr->getWidth();
    THShortTensor_resize3d(output, height, width, 1);
    THShortTensor* output_ = THShortTensor_newContiguous(output);
    ptr->fillDepthImageRaw(width, height, reinterpret_cast<unsigned short*>(THShortTensor_data(output_)));
    THShortTensor_freeCopyTo(output_, output);
  }
}

PCLIMP(void, OpenNI2Stream, readIRImage)(OpenNI2GrabberStream<_PointT> *self, int timeout_milliseconds, THShortTensor* output)
{
  pcl::io::IRImage::ConstPtr ptr = self->readIRImage(timeout_milliseconds);
  if (ptr)
  {
    unsigned int height = ptr->getHeight(), width = ptr->getWidth();
    THShortTensor_resize3d(output, height, width, 1);
    THShortTensor* output_ = THShortTensor_newContiguous(output);
    ptr->fillRaw(width, height, reinterpret_cast<unsigned short*>(THShortTensor_data(output_)));
    THShortTensor_freeCopyTo(output_, output);
  }
}

PCLIMP(void, OpenNI2Stream, getRGBCameraIntrinsics)(OpenNI2GrabberStream<_PointT> *self, OpenNI2CameraParameters& p)
{
  self->getGrabber().getRGBCameraIntrinsics(p.focal_length_x, p.focal_length_y, p.principal_point_x, p.principal_point_y);
}

PCLIMP(void, OpenNI2Stream, setRGBCameraIntrinsics)(OpenNI2GrabberStream<_PointT> *self, const OpenNI2CameraParameters& p)
{
  self->getGrabber().setRGBCameraIntrinsics(p.focal_length_x, p.focal_length_y, p.principal_point_x, p.principal_point_y);
}

PCLIMP(void, OpenNI2Stream, getDepthCameraIntrinsics)(OpenNI2GrabberStream<_PointT> *self, OpenNI2CameraParameters& p)
{
  self->getGrabber().getDepthCameraIntrinsics(p.focal_length_x, p.focal_length_y, p.principal_point_x, p.principal_point_y);
}

PCLIMP(void, OpenNI2Stream, setDepthCameraIntrinsics)(OpenNI2GrabberStream<_PointT> *self, const OpenNI2CameraParameters& p)
{
  self->getGrabber().setDepthCameraIntrinsics(p.focal_length_x, p.focal_length_y, p.principal_point_x, p.principal_point_y);
}

PCLIMP(const char*, OpenNI2Stream, getName)(OpenNI2GrabberStream<_PointT> *self)
{
  return self->getGrabber().getName().c_str();
}

PCLIMP(float, OpenNI2Stream, getFramesPerSecond)(OpenNI2GrabberStream<_PointT> *self)
{
  return self->getGrabber().getFramesPerSecond();
}

max_stars_repo_name: andreaskoepf/torch-pcl

max_issues_repo_name: andreaskoepf/torch-pcl

max_forks_repo_name: andreaskoepf/torch-pcl


text:
//
// Copyright (c) 2015-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

// Test that header file is self-contained
#include <nudb/verify.hpp>

#include "suite.hpp"

#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <nudb/_experimental/test/test_store.hpp>
#include <nudb/progress.hpp>
#include <nudb/verify.hpp>

namespace nudb {
namespace test {

class verify_test : public boost::beast::unit_test::suite
{
public:
    // File doesn't exist
    void
    test_missing()
    {
        error_code ec;
        test_store ts{4, 4096, 0.5f};
        verify_info info;
        verify<xxhasher>(info,
            ts.dp, ts.kp, 0, no_progress{}, ec);
        BEAST_EXPECTS(ec ==
            errc::no_such_file_or_directory, ec.message());
    }

    void
    test_verify(
        std::size_t N,
        std::size_t keySize,
        std::size_t blockSize,
        float loadFactor)
    {
        testcase <<
            "N=" << N << ", "
            "keySize=" << keySize << ", "
            "blockSize=" << blockSize;
        error_code ec;
        test_store ts{keySize, blockSize, loadFactor};
        ts.create(ec);
        if(! BEAST_EXPECTS(! ec, ec.message()))
            return;
        ts.open(ec);
        if(! BEAST_EXPECTS(! ec, ec.message()))
            return;
        // Insert
        for(std::size_t n = 0; n < N; ++n)
        {
            auto const item = ts[n];
            ts.db.insert(item.key, item.data, item.size, ec);
            if(! BEAST_EXPECTS(! ec, ec.message()))
                return;
        }
        ts.close(ec);
        if(! BEAST_EXPECTS(! ec, ec.message()))
            return;

        // Verify
        verify_info info;
        verify<xxhasher>(info, ts.dp, ts.kp,
            0, no_progress{}, ec);
        if(! BEAST_EXPECTS(! ec, ec.message()))
            return;
        BEAST_EXPECT(info.hist[1] > 0);

        // Verify fast
        verify<xxhasher>(info, ts.dp, ts.kp,
            10 * 1024 * 1024, no_progress{}, ec);
        if(! BEAST_EXPECTS(! ec, ec.message()))
            return;
        BEAST_EXPECT(info.hist[1] > 0);
    }

    void
    run() override
    {
        float const loadFactor = 0.95f;
        test_missing();
        test_verify(5000, 4, 256, loadFactor);
    }
};

DEFINE_TESTSUITE(nudb,test,verify);

} // test
} // nudb

max_stars_repo_name: movitto/NuDB

max_issues_repo_name: movitto/NuDB

max_forks_repo_name: movitto/NuDB


text:
#include <boost/test/unit_test.hpp>

#include <scorum/chain/dba/db_accessor.hpp>
#include <scorum/chain/schema/betting_property_object.hpp>
#include <scorum/chain/schema/game_object.hpp>

#include <scorum/protocol/betting/market_kind.hpp>

#include "defines.hpp"
#include "detail.hpp"
#include "database_betting_integration.hpp"
#include "actor.hpp"

namespace bet_operations_tests {

using namespace scorum::protocol;
using namespace scorum::chain;

struct bet_operations_fixture : public database_fixture::database_betting_integration_fixture
{
    bet_operations_fixture()
    {
        open_database();

        alice.scorum(ASSET_SCR(1e+9));
        actor(initdelegate).create_account(alice);
        actor(initdelegate).give_sp(alice, 1e+9);
        actor(initdelegate).give_scr(alice, alice.scr_amount.amount.value);

        bob.scorum(ASSET_SCR(1e+9));
        actor(initdelegate).create_account(bob);
        actor(initdelegate).give_sp(bob, 1e+9);
        actor(initdelegate).give_scr(bob, bob.scr_amount.amount.value);

        actor(initdelegate).create_account(moderator);
        actor(initdelegate).give_sp(moderator, 1e+9);
        actor(initdelegate).give_scr(moderator, 1e+9);

        empower_moderator(moderator);

        BOOST_REQUIRE_EQUAL(this->betting_moderator(), moderator.name);

        create_game(moderator, { result_home{}, total{ 2000 } }, SCORUM_BLOCK_INTERVAL * 60 * 20);

        generate_block();

        BOOST_REQUIRE_EQUAL(1u, db.get_index<game_index>().indices().size());

        const auto& game = db.get(game_object::id_type(0));

        BOOST_REQUIRE_EQUAL(2u, game.markets.size());
    }

    Actor alice = "alice";
    Actor bob = "bob";
    Actor moderator = "smit";
};

BOOST_FIXTURE_TEST_SUITE(create_and_cancel_bet_tests, bet_operations_fixture)

SCORUM_TEST_CASE(post_bet_operation_check)
{
    generate_block();

    create_bet(gen_uuid("b1"), alice, result_home::yes{}, { 10, 2 }, alice.scr_amount / 2);

    generate_block();

    create_bet(gen_uuid("b2"), bob, result_home::yes{}, { 10, 8 }, bob.scr_amount / 2);

    generate_block();
}

SCORUM_TEST_CASE(cancel_single_pending_bet_by_better_operation_check)
{
    generate_block();

    create_bet(gen_uuid("b1"), alice, result_home::yes{}, { 10, 2 }, alice.scr_amount / 2);

    generate_block();

    cancel_pending_bet(alice, { gen_uuid("b1") });

    generate_block();
}

SCORUM_TEST_CASE(cancel_some_pending_bets_by_better_operation_check)
{
    generate_block();

    create_bet(gen_uuid("b1"), alice, total::under{ 2000 }, { 10, 2 }, alice.scr_amount / 2);

    generate_block();

    create_bet(gen_uuid("b2"), alice, result_home::yes(), { 10, 5 }, alice.scr_amount / 2);

    generate_block();

    create_bet(gen_uuid("b3"), bob, total::over{ 2000 }, { 10, 8 }, bob.scr_amount / 2);

    generate_block();

    cancel_pending_bet(alice, { gen_uuid("b1"), gen_uuid("b2") });

    generate_block();
}

SCORUM_TEST_CASE(throw_on_bet_with_existing_uuid)
{
    generate_block();

    create_bet(gen_uuid("b1"), alice, result_home::yes{}, { 10, 2 }, alice.scr_amount / 2);

    generate_block();

    BOOST_REQUIRE_THROW(create_bet(gen_uuid("b1"), bob, result_home::yes{}, { 10, 8 }, bob.scr_amount / 2),
                        fc::assert_exception);
}

BOOST_AUTO_TEST_SUITE_END()
}

max_stars_repo_name: scorum/scorum

max_issues_repo_name: Scorum/Scorum

max_forks_repo_name: Scorum/Scorum


text:
/*
 * Copyright (C) 2011-2014 Daniel Scharrer
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the author(s) be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */

/*!
 * \file
 *
 * LZMA 1 and 2 (aka xz) descompression filters to be used with boost::iostreams.
 */
#ifndef INNOEXTRACT_STREAM_LZMA_HPP
#define INNOEXTRACT_STREAM_LZMA_HPP

#include "configure.hpp"

#if INNOEXTRACT_HAVE_LZMA

#include <stddef.h>
#include <iosfwd>

#include <boost/iostreams/filter/symmetric.hpp>
#include <boost/noncopyable.hpp>

namespace stream {

//! Error thrown if there was en error in an LZMA stream
struct lzma_error : public std::ios_base::failure {

	lzma_error(std::string msg, int code)
		: std::ios_base::failure(msg), error_code(code) { }

	//! \return the liblzma code for the error.
	int error() const { return error_code; }

private:

	int error_code;
};

class lzma_decompressor_impl_base : public boost::noncopyable {

public:

	typedef char char_type;

	~lzma_decompressor_impl_base() { close(); }

	bool filter(const char * & begin_in, const char * end_in,
	            char * & begin_out, char * end_out, bool flush);

	void close();

protected:

	//! Abstract base class, subclasses need to intialize stream.
	lzma_decompressor_impl_base() : stream(NULL) { }

	void * stream;

};

class inno_lzma1_decompressor_impl : public lzma_decompressor_impl_base {

public:

	inno_lzma1_decompressor_impl() : nread(0) { }

	bool filter(const char * & begin_in, const char * end_in,
	            char * & begin_out, char * end_out, bool flush);

	void close() { lzma_decompressor_impl_base::close(), nread = 0; }

private:

	size_t nread; //! Number of bytes read into header.
	char header[5];

};

class inno_lzma2_decompressor_impl : public lzma_decompressor_impl_base {

public:

	bool filter(const char * & begin_in, const char * end_in,
	            char * & begin_out, char * end_out, bool flush);

};

template <class Impl, class Allocator = std::allocator<typename Impl::char_type> >
class lzma_decompressor : public boost::iostreams::symmetric_filter<Impl, Allocator> {

public:

	explicit lzma_decompressor(int buffer_size = boost::iostreams::default_device_buffer_size)
		: boost::iostreams::symmetric_filter<Impl, Allocator>(buffer_size) { }

};

/*!
 * A filter that decompressess LZMA1 streams found in Inno Setup installers,
 * to be used with boost::iostreams.
 *
 * The LZMA1 streams used by Inno Setup differ slightly from the LZMA Alone file format:
 * The stream header only stores the properties (lc, lp, pb) and the dictionary size and
 * is missing the uncompressed size field. The fiels that are present are encoded
 * identically.
 */
typedef lzma_decompressor<inno_lzma1_decompressor_impl> inno_lzma1_decompressor;

/*!
 * A filter that decompressess LZMA2 streams found in Inno Setup installers,
 * to be used with boost::iostreams.
 *
 * Inno Setup uses raw LZMA2 streams.
 * (preceded only by the dictionary size encoded as one byte)
 */
typedef lzma_decompressor<inno_lzma2_decompressor_impl> inno_lzma2_decompressor;

} // namespace stream

#endif // INNOEXTRACT_HAVE_LZMA

#endif // INNOEXTRACT_STREAM_LZMA_HPP

ext: hpp

max_stars_repo_name: kennethmyhra/innoextract


text:
/** @file vec2dic.cpp
 *
 *  @brief Generate sentiment lexicons from neural word embeddings.
 *
 *  This file provides main method for generating sentiment lexicons
 *  on the basis of previously computed neural word embeddings.
 */

//////////////
// Includes //
//////////////
#include "src/vec2dic/expansion.h"
#include "src/vec2dic/optparse.h"

#include <cctype>         // std::isspace()
#include <clocale>        // setlocale()
#include <cmath>          // sqrt(), fabs()
#include <cstdio>         // sscanf()
#include <cstdlib>        // std::exit(), std::strtoul()
#include <cstring>        // strcmp(), strlen()

#include <algorithm>
#include <armadillo>      // arma::mat
#include <fstream>        // std::ifstream
#include <functional>
#include <iostream>       // std::cerr, std::cout
#include <locale>
#include <stdexcept>      // std::domain_error()
#include <string>         // std::string
#include <unordered_map>  // std::unordered_map
#include <utility>        // std::make_pair

/////////////
// Classes //
/////////////

/**
 * Type of algorithm to use for lexicon expansion.
 */
enum class ExpansionType: int {
  NC_CLUSTERING = 0,          // Nearest centroids algorithm
    KNN_CLUSTERING,           // K-nearest neighbors
    PCA_CLUSTERING,           // Proincipal component analysis
    PRJ_CLUSTERING,           // Projection-based clustering
    MAX_SENTINEL              // Unused type that serves as a sentinel
    };

// forward declaration of `usage()` method
static void usage(int a_ret = EXIT_SUCCESS);

/**
 * Custom option handler
 */
class Option: public optparse {
public:
  // Members
  /// input file containing seed polarity terms
  std::ifstream m_seedfile {};
  /// default number of nearest neighbors to consider by the KNN algorithm
  int knn = 5;
  /// maximum number of new terms to extract (-1 means all new terms)
  int n_terms = -1;
  /// learning rate for gradient methods
  double alpha = DFLT_ALPHA;
  /// minimum required improvement for gradient methods
  double delta = DFLT_DELTA;
  /// maximum number of gradient updates
  unsigned long max_iters = MAX_ITERS;
  /// elongation coefficient for vector lengths (useful for linear transformation)
  double coefficient = 1.;
  /// do not normalize length of the vectors
  bool no_length_normalize = false;
  /// do not center means of the vectors
  bool no_mean_normalize = false;
  /// algorithm to use for expansion
  ExpansionType etype = ExpansionType::NC_CLUSTERING;

  Option() {}

  BEGIN_OPTION_MAP_INLINE()
  ON_OPTION(SHORTOPT('L') || LONGOPT("no-length-normalizion"))
  no_length_normalize = true;

  ON_OPTION(SHORTOPT('M') || LONGOPT("no-mean-normalizion"))
  no_mean_normalize = true;

  ON_OPTION_WITH_ARG(SHORTOPT('a') || LONGOPT("alpha"))
  alpha = std::atof(arg);

  ON_OPTION_WITH_ARG(SHORTOPT('c') || LONGOPT("coefficient"))
  coefficient = std::atof(arg);

  ON_OPTION_WITH_ARG(SHORTOPT('d') || LONGOPT("delta"))
  delta = std::atof(arg);

  ON_OPTION(SHORTOPT('h') || LONGOPT("help"))
  usage();

  ON_OPTION_WITH_ARG(SHORTOPT('i') || LONGOPT("max-iterations"))
  max_iters = std::strtoul(arg, nullptr, 10);

  ON_OPTION_WITH_ARG(SHORTOPT('k') || LONGOPT("k-nearest-neighbors"))
  knn = std::atoi(arg);
  if (knn < 1)
    throw optparse::invalid_value("k-nearest-neighbors should be >= 1");

  ON_OPTION_WITH_ARG(SHORTOPT('n') || LONGOPT("n-terms"))
  n_terms = std::atoi(arg);

  ON_OPTION_WITH_ARG(SHORTOPT('t') || LONGOPT("type"))
  int itype = std::atoi(arg);
  if (itype < 0 || itype >= static_cast<int>(ExpansionType::MAX_SENTINEL))
    throw invalid_value("Invalid type of expansion algorithm.");

  etype = static_cast<ExpansionType>(itype);

  END_OPTION_MAP()
};

/// Pair of c_string and polarity
typedef struct WP {
  /// polar word
  const char *m_word = nullptr;
  /// polarity class of the word
  Polarity m_polarity = Polarity::NEUTRAL;
  /// polarity score of the word
  dist_t m_score = 0.;

  /// copy constructor
  WP(const char *a_word, const Polarity a_polarity,
     dist_t a_score):
    m_word(a_word), m_polarity(a_polarity), m_score(fabs(a_score))
  {}

  /// copy constructor
  WP(const char *a_word, const ps_t *a_ps):
    m_word(a_word), m_polarity(a_ps->first), m_score(fabs(a_ps->second))
  {}
} wp_t;

/// Vector of pairs of c_string and their polarities
typedef std::vector<wp_t> wpv_t;

/////////////////////////////
// Variables and Constants //
/////////////////////////////

/// string representing positive polarity class
const char *positive  = "positive";
/// string representing negative polarity class
const char *negative = "negative";
/// string representing neutral polarity class
const char *neutral = "neutral";

/// Mapping from words to the index of their NWE vector
static w2v_t word2vecid;
/// Mapping from the index of NWE vectors to their respective words
static v2w_t vecid2word;
/// Mapping from word to its polarity
static w2ps_t word2polscore;
/// Matrix of neural word embeddings
static arma::mat NWE;
/// Output debug information
const bool debug = false;

/////////////
// Methods //
/////////////

/**
 * Print usage message and exit
 *
 * @param a_ret - exit code for the program
 *
 * @return \c void
 */
static void usage(int a_ret) {
  std::cerr << "Expand initial seed set of subjective terms by"
      " applying clustering" << std::endl;
  std::cerr << "to neural word embeddings." << std::endl << std::endl;
  std::cerr << "Usage:" << std::endl;
  std::cerr << "vec2dic [OPTIONS] VECTOR_FILE SEED_FILE"
            << std::endl << std::endl;
  std::cerr << "Options:" << std::endl;
  std::cerr << "-L|--no-length-normalizion  do not normalize length"
      " of word vectors" << std::endl;
  std::cerr << "-M|--no-mean-normalizion  do not normalize means"
      " of word vectors" << std::endl;
  std::cerr << "-a|--alpha  learning rate for gradient methods"
      " (default " << DFLT_ALPHA << ")" << std::endl;
  std::cerr << "-c|--coefficient  elongate vectors by the"
      " coefficient (implies -L)" << std::endl;
  std::cerr << "-d|--delta  learning rate for gradient methods"
      " (default " << DFLT_DELTA << ")" << std::endl;
  std::cerr << "-h|--help  show this screen and exit" << std::endl;
  std::cerr << "-i|--max-iterations  maximum number of gradient"
      " updates (default " << MAX_ITERS << ")" << std::endl;
  std::cerr << "-k|--k-nearest-neighbors  set the number of neighbors"
      " for KNN algorithm" << std::endl;
  std::cerr << "-n|--n-terms  number of terms to extract (default:"
      " -1 (unlimited))" << std::endl;
  std::cerr << "-t|--type  type of expansion algorithm to use:" << std::endl;
  std::cerr << "           (0 - nearest centroids (default), "
      "1 - KNN, 2 - PCA dimension)" << std::endl << std::endl;
  std::cerr << "Exit status:" << std::endl;
  std::cerr << EXIT_SUCCESS << " on sucess, non-" << EXIT_SUCCESS
            << " otherwise" << std::endl;
  std::exit(a_ret);
}

/**
 * Output polar terms in sorted alphabetic order
 *
 * @param a_stream - output stream to use
 * @param a_vecid2polscore - mapping from vector id's to their respective polarities
 *
 * @return \c void
 */
static void output_terms(std::ostream &a_stream,
                         const v2ps_t *a_vecid2polscore) {
  // add new words to `word2polscore` map
  v2w_t::iterator v2w_it;

  for (auto &v2ps : *a_vecid2polscore) {
    // we assume that the word is always found
    v2w_it = vecid2word.find(v2ps.first);
    word2polscore.emplace(v2w_it->second, v2ps.second);
  }

  // populate word/polarity vector
  wpv_t wpv;
  wpv.reserve(word2polscore.size());
  for (auto &w2p : word2polscore) {
    wpv.push_back(WP {w2p.first.c_str(), &w2p.second});
  }
  // sort words
  std::sort(wpv.begin(), wpv.end(), [](const wp_t& wp1, const wp_t& wp2) \
            {return wp1.m_score < wp2.m_score;});

  // output sorted dict to the requested stream
  for (auto &wp : wpv) {
    switch (wp.m_polarity) {
      case Polarity::POSITIVE:
        a_stream << wp.m_word << '\t' << positive << '\t' << wp.m_score;
        break;
      case Polarity::NEGATIVE:
        a_stream << wp.m_word << '\t' << negative << '\t' << wp.m_score;
        break;
      case Polarity::NEUTRAL:
        continue;
    default:
      throw std::domain_error("Unknown polarity type");
    }
    a_stream << std::endl;
  }
}

/**
 * Auxiliary function for removing blanks from the left end of a string
 *
 * @param s - string to be trimmed
 *
 * @return reference to the original string with leading blanks truncated
 */
static inline std::string *ltrim(std::string *s) {
  s->erase(s->begin(),
           std::find_if(s->begin(), s->end(),
                        std::not1(std::ptr_fun<int, int>(std::isspace))));
  return s;
}

/**
 * Auxiliary function for removing blanks from the right end of a string
 *
 * @param s - string to be trimmed
 *
 * @return reference to the original string with trailing blanks truncated
 */
static inline std::string *rtrim(std::string *s) {
  s->erase(std::find_if(s->rbegin(), s->rend(),
                        std::not1(std::ptr_fun<int, int>(std::isspace))).base(),
           s->end());
  return s;
}

/**
 * Auxiliary function for removing blanks from both ends of a string
 *
 * @param s - string to be trimmed
 *
 * @return original string with leading and trailing blanks removed
 */
static inline std::string *normalize(std::string *s) {
  // strip leading and trailing whitespaces
  ltrim(rtrim(s));
  // downcase the string
  std::transform(s->begin(), s->end(), s->begin(), ::tolower);;
  return s;
}

/**
 * Perform length-normalization of word vectors
 *
 * @param a_nwe - Armadillo matrix of word vectors (each word vector
 *                is a column in this matrix)
 *
 * @return \c void
 */
static void _length_normalize(arma::mat *a_nwe) {
  dist_t ilength = 0., tmp_j;
  size_t j, n_rows = a_nwe->n_rows;
  for (size_t i = 0; i < a_nwe->n_cols; ++i) {
    ilength = 0.;
    // compute the unnormalized length of the vector
    for (j = 0; j < n_rows; ++j) {
      tmp_j = (*a_nwe)(j, i);
      ilength += tmp_j * tmp_j;
    }
    // normalize the length
    ilength = sqrt(ilength);
    // normalize the vector by dividing it by normalized length
    if (ilength)
      a_nwe->col(i) /= float(ilength);
  }
}

/**
 * Perform mean-normalization of word vectors
 *
 * @param a_nwe - Armadillo matrix of word vectors (each word vector
 *                is a column in this matrix)
 *
 * @return \c void
 */
static void _mean_normalize(arma::mat *a_nwe) {
  arma::vec vmean = arma::mean(*a_nwe, 1);
  arma::vec vstddev = arma::stddev(*a_nwe, 0, 1);

  for (vid_t i = 0; i < vmean.n_rows; ++i) {
    a_nwe->row(i) -= vmean[i];

    if (vstddev[i])
      a_nwe->row(i) /= vstddev[i];
  }
}

/**
 * Read NWE vectors for words.
 *
 * @param a_ret - exit code for the program
 * @param a_option - pointer to user's options
 *
 * @return \c 0 on success, non-\c 0 otherwise
 */
static int read_vectors(const char *a_fname, const Option *a_option) {
  float iwght;
  const char *cline;
  std::string iline;
  size_t space_pos, tab_pos;
  int nchars;
  vid_t mrows = 0, ncolumns = 0, icol = 0, irow = 0;
  const int coefficient = a_option->coefficient;
  const bool no_length_normalize = a_option->no_length_normalize;
  const bool no_mean_normalize = a_option->no_mean_normalize;
  std::cerr << "Reading word vectors ... ";

  std::ifstream is(a_fname);
  if (!is) {
    std::cerr << "Cannot open file " << a_fname << std::endl;
    goto error_exit;
  }
  // skip empty lines at the beginning of file
  while (std::getline(is, iline) && iline.empty()) {}
  // initialize matrix (columns represent words, rows are coordinates)
  if (sscanf(iline.c_str(), "%llu %llu", &ncolumns, &mrows) != 2) {
    std::cerr << "Incorrect declaration line format: '"
              << iline.c_str() << std::endl;
    goto error_exit;
  }

  // allocate space for map and matrix
  word2vecid.reserve(ncolumns); vecid2word.reserve(ncolumns);
  NWE.set_size(mrows, ncolumns);

  while (icol < ncolumns && std::getline(is, iline)) {
    tab_pos = iline.find_first_of('\t');
    space_pos = iline.find_first_of(' ');
    if (tab_pos < space_pos)
      space_pos = tab_pos;

    while (space_pos > 0 && std::isspace(iline[space_pos])) {--space_pos;}
    if (space_pos == 0 && std::isspace(iline[space_pos])) {
      std::cerr << "Incorrect line format (missing word): "
                << iline << std::endl;
      goto error_exit;
    }
    ++space_pos;
    word2vecid.emplace(std::move(iline.substr(0, space_pos)), std::move(icol));
    vecid2word.emplace(std::move(icol), std::move(iline.substr(0, space_pos)));

    cline = &(iline.c_str()[space_pos]);
    for (irow = 0; irow < mrows
           && sscanf(cline, " %f%n", &iwght, &nchars) == 1; ++irow) {
      NWE(irow, icol) = iwght;
      cline += nchars;
    }
    if (irow != mrows) {
      std::cerr << "Incorrect line format (declared vector size " << mrows
                << " differs from the actual size " << irow << "):\n"
		<< iline << std::endl;
      goto error_exit;
    }
    ++icol;
  }

  if (!is.eof() && is.fail()) {
    std::cerr << "Failed to read vector file " << a_fname << std::endl;
    goto error_exit;
  }
  if (icol != ncolumns) {
    std::cerr << "Incorrect file format: declared number of vectors "
              << ncolumns << " differs from the actual number "
              << icol << std::endl;
    goto error_exit;
  }
  is.close();
  // normalize lengths of word vectors
  if (coefficient != 1)
    NWE *= coefficient;

  // normalize lengths of word vectors
  if (!no_length_normalize)
    _length_normalize(&NWE);

  // normalize means of word vectors
  if (!no_mean_normalize)
    _mean_normalize(&NWE);

  std::cerr << "done (read " << mrows << " rows with "
            << ncolumns << " columns)" << std::endl;
  return 0;

 error_exit:
  is.close();            // basic guarantee
  word2vecid.clear();
  vecid2word.clear();
  NWE.reset();
  return 1;
}

/**
 * Read seed set of polarity terms
 *
 * @param a_ret - exit code for the program
 *
 * @return \c 0 on success, non-\c 0 otherwise
 */
static int read_seed_set(const char *a_fname) {
  Polarity ipol;
  std::string iline;
  size_t tab_pos, tab_pos_orig;

  std::cerr << "Reading seed set file ...";

  std::ifstream is(a_fname);
  if (!is) {
    std::cerr << "Cannot open file " << a_fname << std::endl;
    goto error_exit;
  }

  // read input file
  while (std::getline(is, iline)) {
    if (iline.empty() || iline.compare(0, 3, "###") == 0)
      continue;

    // remove leading and trailing whitespaces
    normalize(&iline);
    // find first tab character
    tab_pos = iline.find_first_of('\t');
    tab_pos_orig = tab_pos;
    // skip leading whitespaces
    while (iline[++tab_pos] && std::isspace(iline[tab_pos])) {}
    if (tab_pos == std::string::npos || !iline[tab_pos]) {
      std::cerr << "Incorrect line format (missing polarity): "
                << iline << std::endl;
      goto error_exit;
    }
    // determine polarity class
    if (iline.compare(tab_pos, strlen(positive), positive) == 0) {
      ipol = Polarity::POSITIVE;
    } else if (iline.compare(tab_pos, strlen(negative), negative) == 0) {
      ipol = Polarity::NEGATIVE;
    } else if (iline.compare(tab_pos, strlen(neutral), neutral) == 0) {
      ipol = Polarity::NEUTRAL;
    } else {
      std::cerr << "Unrecognized polarity class at line '"
                << iline.substr(tab_pos) << "'" << std::endl;
      goto error_exit;
    }

    while (tab_pos_orig > 0
           && std::isspace(iline[tab_pos_orig])) {--tab_pos_orig;}
    if (tab_pos_orig == 0 && std::isspace(iline[tab_pos_orig])) {
      std::cerr << "Incorrect line format (missing word): "
                << iline << std::endl;
      goto error_exit;
    }
    ++tab_pos_orig;
    word2polscore.emplace(
        std::move(iline.substr(0, tab_pos_orig)),
        std::make_pair(ipol, 0.));
  }

  if (!is.eof() && is.fail()) {
    std::cerr << "Failed to read seed set file " << a_fname << std::endl;
    goto error_exit;
  }
  is.close();
  std::cerr << "done (read " << word2polscore.size() << " entries)"
            << std::endl;
  return 0;

 error_exit:
  is.close();        // basic guarantee
  word2polscore.clear();
  return 1;
}

//////////
// Main //
//////////

/**
 * Main method for expanding sentiment lexicons
 *
 * @param argc - number of command line arguments
 * @param argv - array of command line arguments
 *
 * @return 0 on success, non-0 otherwise
 */
int main(int argc, char *argv[]) {
  int nargs = 0, ret = EXIT_SUCCESS;

  // set appropriate locale
  setlocale(LC_ALL, NULL);

  Option opt {};
  int argused = 1 + opt.parse(&argv[1], argc-1);  // Skip argv[0].

  if ((nargs = argc - argused) != 2) {
    std::cerr << "Incorrect number of arguments "
              << nargs << " (2 arguments expected).  "  \
      "Type --help to see usage." << std::endl;
    std::exit(EXIT_FAILURE);
  }
  // clean up options
  if (opt.coefficient != 1)
    opt.no_length_normalize = true;

  // read word vectors
  if ((ret = read_vectors(argv[argused++], &opt)))
    return ret;

  // read seed sets
  if ((ret = read_seed_set(argv[argused++])))
    return ret;

  // generate mapping from vector ids to the polarities of respective
  // words
  v2ps_t vecid2polscore;
  int seed_cnt = 0;
  w2v_t::const_iterator vecid, vecend = word2vecid.end();
  for (auto &w2p : word2polscore) {
    if (w2p.second.first != Polarity::NEUTRAL)
      ++seed_cnt;

    vecid = word2vecid.find(w2p.first);
    if (vecid == vecend)
      continue;

    vecid2polscore.emplace(vecid->second, w2p.second);
    if (debug) {
      std::cerr << "word: " << w2p.first
		<< ", vecid = " << vecid->second
		<< ", polarity = " << (int) w2p.second.first << std::endl;
    }
  }

  if (opt.n_terms > 0) {
    opt.n_terms -= seed_cnt;
    if (opt.n_terms < 1)
      goto print_steps;
  }

  // apply the requested expansion algorithm
  switch (opt.etype) {
  case ExpansionType::NC_CLUSTERING:
    expand_nearest_centroids(&vecid2polscore, &NWE, opt.n_terms);
    break;
  case ExpansionType::KNN_CLUSTERING:
    expand_knn(&vecid2polscore, &NWE, opt.n_terms, opt.knn);
    break;
  case ExpansionType::PCA_CLUSTERING:
    expand_pca(&vecid2polscore, &NWE, opt.n_terms);
    break;
  default:
    throw std::invalid_argument("Invalid type of seed set"
                                " expansion algorithm.");
  }
  // output new terms in sorted alphabetic order
 print_steps:
  output_terms(std::cout, &vecid2polscore);
  return ret;
}

max_stars_repo_name: WladimirSidorenko/SentimentLexicon

max_issues_repo_name: WladimirSidorenko/SentimentLexicon

max_forks_repo_name: WladimirSidorenko/SentimentLexicon


text:
/**
 * Copyright (c) 2013 Jonan Cruz-Martin
 *
 * Distributed under the terms of the MIT license, see the accompanying
 * file COPYING or http://opensource.org/licenses/MIT.
 *
 * @file
 * @brief Class definitions for segments of tetrahedral domains.
 */

#ifndef SEGMENTS_TETRAHEDRAL_HPP
#define SEGMENTS_TETRAHEDRAL_HPP

#include "types.hpp"

#include "../domains/forward.hpp"

#include "../points/cartesian.hpp"
#include "../points/cylindrical.hpp"
#include "../points/polar.hpp"
#include "../points/spherical.hpp"

#include "../cells/tetrahedral.hpp"
#include "../segmentations/forward.hpp"
#include "../vertices/tetrahedral.hpp"

#include <boost/python.hpp>
using namespace boost::python;

/**
 * Wrapped segment of a tetrahedral, cartesian 3D domain.
 *
 * @since 0.1.0
 */
class TetrahedralCartesian3D_Segment
{
	/**
	 * ViennaGrid segment object.
	 * @since 0.1.0
	 */
	TetrahedralCartesian3D_Segment_t        segment;

	/**
	 * Pointer to the wrapped segmentation to which the segment belongs.
	 * @since 0.1.0
	 */
	TetrahedralCartesian3D_Segmentation    *segmentation;
public:
	TetrahedralCartesian3D_Segment(TetrahedralCartesian3D_Segment_t &initial_segment, TetrahedralCartesian3D_Segmentation &initial_segmentation);

	/**
	 * Return the number of cells contained in the segment.
	 *
	 * @return unsigned integer
	 *
	 * @since 0.1.0
	 */
	unsigned int num_cells();

	/**
	 * Create a new cell in the segment.
	 *
	 * @param vertex1
	 * @param vertex2
	 * @param vertex3
	 * @param vertex4
	 *
	 * @return The newly created cell.
	 *
	 * @since 0.1.0
	 */
	TetrahedralCartesian3D_Cell make_cell(TetrahedralCartesian3D_Vertex vertex1, TetrahedralCartesian3D_Vertex vertex2, TetrahedralCartesian3D_Vertex vertex3, TetrahedralCartesian3D_Vertex vertex4);

	/**
	 * Return a Python list containing all the cells contained in the segment.
	 *
	 * @return Python list
	 *
	 * @since 0.1.0
	 */
	list get_cells();

	/**
	 * Get a reference to the ViennaGrind segment object.
	 *
	 * @return Reference to the ViennaGrid segment object.
	 * @since 0.1.0
	 */
	TetrahedralCartesian3D_Segment_t & get_segment();
};

/**
 * Wrapped segment of a tetrahedral, cylindrical domain.
 *
 * @since 0.1.0
 */
class TetrahedralCylindrical3D_Segment
{
	/**
	 * ViennaGrid segment object.
	 * @since 0.1.0
	 */
	TetrahedralCylindrical3D_Segment_t        segment;

	/**
	 * Pointer to the wrapped segmentation to which the segment belongs.
	 * @since 0.1.0
	 */
	TetrahedralCylindrical3D_Segmentation    *segmentation;
public:
	TetrahedralCylindrical3D_Segment(TetrahedralCylindrical3D_Segment_t &initial_segment, TetrahedralCylindrical3D_Segmentation &initial_segmentation);

	/**
	 * Return the number of cells contained in the segment.
	 *
	 * @return unsigned integer
	 *
	 * @since 0.1.0
	 */
	unsigned int num_cells();

	/**
	 * Create a new cell in the segment.
	 *
	 * @param vertex1
	 * @param vertex2
	 * @param vertex3
	 * @param vertex4
	 *
	 * @return The newly created cell.
	 *
	 * @since 0.1.0
	 */
	TetrahedralCylindrical3D_Cell make_cell(TetrahedralCylindrical3D_Vertex vertex1, TetrahedralCylindrical3D_Vertex vertex2, TetrahedralCylindrical3D_Vertex vertex3, TetrahedralCylindrical3D_Vertex vertex4);

	/**
	 * Return a Python list containing all the cells contained in the segment.
	 *
	 * @return Python list
	 *
	 * @since 0.1.0
	 */
	list get_cells();

	/**
	 * Get a reference to the ViennaGrind segment object.
	 *
	 * @return Reference to the ViennaGrid segment object.
	 * @since 0.1.0
	 */
	TetrahedralCylindrical3D_Segment_t & get_segment();
};

/**
 * Wrapped segment of a tetrahedral, spherical domain.
 *
 * @since 0.1.0
 */
class TetrahedralSpherical3D_Segment
{
	/**
	 * ViennaGrid segment object.
	 * @since 0.1.0
	 */
	TetrahedralSpherical3D_Segment_t        segment;

	/**
	 * Pointer to the wrapped segmentation to which the segment belongs.
	 * @since 0.1.0
	 */
	TetrahedralSpherical3D_Segmentation    *segmentation;
public:
	TetrahedralSpherical3D_Segment(TetrahedralSpherical3D_Segment_t &initial_segment, TetrahedralSpherical3D_Segmentation &initial_segmentation);

	/**
	 * Return the number of cells contained in the segment.
	 *
	 * @return unsigned integer
	 *
	 * @since 0.1.0
	 */
	unsigned int num_cells();

	/**
	 * Create a new cell in the segment.
	 *
	 * @param vertex1
	 * @param vertex2
	 * @param vertex3
	 * @param vertex4
	 *
	 * @return The newly created cell.
	 *
	 * @since 0.1.0
	 */
	TetrahedralSpherical3D_Cell make_cell(TetrahedralSpherical3D_Vertex vertex1, TetrahedralSpherical3D_Vertex vertex2, TetrahedralSpherical3D_Vertex vertex3, TetrahedralSpherical3D_Vertex vertex4);

	/**
	 * Return a Python list containing all the cells contained in the segment.
	 *
	 * @return Python list
	 *
	 * @since 0.1.0
	 */
	list get_cells();

	/**
	 * Get a reference to the ViennaGrind segment object.
	 *
	 * @return Reference to the ViennaGrid segment object.
	 * @since 0.1.0
	 */
	TetrahedralSpherical3D_Segment_t & get_segment();
};

#endif /* end of include guard: SEGMENTS_TETRAHEDRAL_HPP */

max_stars_repo_name: jonancm/viennagrid-python

max_issues_repo_name: jonancm/viennagrid-python

max_forks_repo_name: jonancm/viennagrid-python


text:
/**
 * AssemblyState.h
 *
 *  @date Feb 2, 2011
 *      @author Nathan Merritt
 *
 *  @see Assembly.hpp
 */

#ifndef ASSEMBLYSTATE_H_
#define ASSEMBLYSTATE_H_

#include <boost/shared_ptr.hpp>

const float INITIAL_INTRA_LTCS = 0.5;

/**
 * @brief Contains the state variables for an Assembly.
 *
 *  activity -> current activation of the Assembly
 *  output -> output of the Assembly (A(t-1))
 *  fatige -> grows with increasing activation
 *
 *  Measures of the Assembly's ability to self-activate:
 *  ltcs -> long term connection strength (locked at 0)
 *  stcs -> short term connection strength
 */
class AssemblyState {
public:
     typedef boost::shared_ptr<AssemblyState> ptr;

public:
     AssemblyState() :
          activity(0), output(0), ltcs(INITIAL_INTRA_LTCS), stcs(0), fatigue(0), manual_input(0),
          regional_activation(0), lateral_inhibition(0)
          {
          }
     virtual ~AssemblyState() {
     }

     //Copy constructor
     AssemblyState(const AssemblyState &a) :
          activity(a.activity), output(a.output), ltcs(a.ltcs), stcs(a.stcs), fatigue(a.fatigue),
          regional_activation(a.regional_activation)
          {
          }
     /** multiTRACE variables */
     float activity;
     float output;
     const float ltcs; /** const because we are not modeling intra-Assembly learning */
     float stcs;
     float fatigue;

     float manual_input; // replaces input calculations for 1 tick

     /** set by Layer when it calls tick() on each assembly */
     float regional_activation;
     float lateral_inhibition;
};

#endif /* ASSEMBLYSTATE_H_ */

max_stars_repo_name: gnmerritt/multiTrace-features

max_issues_repo_name: gnmerritt/multiTrace-features

max_forks_repo_name: gnmerritt/multiTrace-features


text:
/*
 * Copyright (c) 2016 Shanghai Jiao Tong University.
 *     All rights reserved.
 *
 *  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.
 *
 * For more about this software visit:
 *
 *      http://ipads.se.sjtu.edu.cn/projects/wukong
 *
 */

#include <map>
#include <iostream>

#include <boost/mpi.hpp>

#include "client/proxy.hpp"
#include "client/console.hpp"

#include "core/common/conflict.hpp"
#include "core/common/config.hpp"
#include "core/common/string_server.hpp"
#include "core/common/bind.hpp"
#include "core/common/rdma.hpp"
#include "core/common/mem.hpp"

#include "core/engine/engine.hpp"

#ifdef DYNAMIC_GSTORE
#include "core/store/dynamic_dgraph.hpp"
#else
#include "core/store/static_dgraph.hpp"
#include "core/store/segment_dgraph.hpp"
#endif

#include "core/network/adaptor.hpp"

#include "optimizer/stats.hpp"

// utils
#include "utils/unit.hpp"
#include "utils/logger2.hpp"

#ifdef USE_GPU

#include "gpu/gpu_mem.hpp"
#include "gpu/gpu_agent.hpp"
#include "gpu/gpu_engine.hpp"
#include "gpu/gpu_cache.hpp"
#include "gpu/gpu_stream.hpp"

void *agent_thread(void *arg)
{
    wukong::GPUAgent *agent = (wukong::GPUAgent *)arg;
    if (wukong::enable_binding && wukong::core_bindings.count(agent->tid) != 0)
        wukong::bind_to_core(wukong::core_bindings[agent->tid]);
    else
        wukong::bind_to_core(wukong::default_bindings[agent->tid % wukong::num_cores]);

    agent->run();
}
#endif  // end of USE_GPU

void *engine_thread(void *arg)
{
    wukong::Engine *engine = (wukong::Engine *)arg;
    if (wukong::enable_binding && wukong::core_bindings.count(engine->tid) != 0)
        wukong::bind_to_core(wukong::core_bindings[engine->tid]);
    else
        wukong::bind_to_core(wukong::default_bindings[engine->tid % wukong::num_cores]);

    engine->run();
}

void *proxy_thread(void *arg)
{
    wukong::Proxy *proxy = (wukong::Proxy *)arg;
    if (wukong::enable_binding && wukong::core_bindings.count(proxy->tid) != 0)
        wukong::bind_to_core(wukong::core_bindings[proxy->tid]);
    else
        wukong::bind_to_core(wukong::default_bindings[proxy->tid % wukong::num_cores]);

    // run the builtin console
    wukong::run_console(proxy);
}

static void
usage(char *fn)
{
    std::cout << "usage: " << fn << " <config_fname> <host_fname> [options]" << std::endl;
    std::cout << "options:" << std::endl;
    std::cout << "  -b binding : the file of core binding" << std::endl;
    std::cout << "  -c command : the one-shot command" << std::endl;
}

void print_badge(){
    std::cout << "====================================" << std::endl;
    std::cout << " _      __     __" << std::endl;
    std::cout << "| | /| / /_ __/ /_____  ___  ___ _" << std::endl;
    std::cout << "| |/ |/ / // /  '_/ _ \\/ _ \\/ _ `/" << std::endl;
    std::cout << "|__/|__/\\_,_/_/\\_\\\\___/_//_/\\_, / " << std::endl;
    std::cout << "                           /___/" << std::endl;
    std::cout << "====================================" << std::endl;
}

int
main(int argc, char *argv[])
{
    wukong::conflict_detector();

    boost::mpi::environment env(argc, argv);
    boost::mpi::communicator world;
    int sid = world.rank(); // server ID

    if (argc < 3) {
        usage(argv[0]);
        exit(EXIT_FAILURE);
    }

    if (sid == 0)
        print_badge();

    // load global configs
    wukong::load_config(std::string(argv[1]), world.size());

    // set the address file of host/cluster
    std::string host_fname = std::string(argv[2]);

    // load CPU topology by hwloc
    wukong::load_node_topo();
    logstream(LOG_INFO) << "#" << sid << ": has " << wukong::num_cores << " cores." << LOG_endl;

    int c;
    while ((c = getopt(argc - 2, argv + 2, "b:c:")) != -1) {
        switch (c) {
        case 'b':
            wukong::enable_binding = wukong::load_core_binding(optarg);
            break;
        case 'c':
            wukong::enable_oneshot = true;
            wukong::oneshot_cmd = optarg;
            break;
        default:
            usage(argv[0]);
            exit(EXIT_FAILURE);
        }
    }

    // allocate memory regions
    std::vector<wukong::RDMA::MemoryRegion> mrs;

    // rdma broadcast memory
    std::vector<wukong::Broadcast_Mem *> bcast_mems;
    wukong::Broadcast_Mem *ss_bcast_mem = new wukong::Broadcast_Mem(wukong::Global::num_servers, wukong::Global::num_threads);
    bcast_mems.push_back(ss_bcast_mem);
    // CPU (host) memory
    wukong::Mem *mem = new wukong::Mem(wukong::Global::num_servers, wukong::Global::num_threads, bcast_mems);
    logstream(LOG_INFO) << "[Mem] #" << sid << ": allocate " << B2GiB(mem->size())
                        << "GB memory" << LOG_endl;
    wukong::RDMA::MemoryRegion mr_cpu = { wukong::RDMA::MemType::CPU, mem->address(), mem->size(), mem };
    mrs.push_back(mr_cpu);

#ifdef USE_GPU
    // GPU (device) memory
    int devid = 0; // FIXME: it means one GPU device?
    wukong::GPUMem *gpu_mem = new wukong::GPUMem(devid, wukong::Global::num_servers, wukong::Global::num_gpus);
    logstream(LOG_INFO) << "[GPUMem] #" << sid << ": allocate " << B2GiB(gpu_mem->size())
                        << "GB GPU memory" << LOG_endl;
    wukong::RDMA::MemoryRegion mr_gpu = { wukong::RDMA::MemType::GPU, gpu_mem->address(), gpu_mem->size(), gpu_mem };
    mrs.push_back(mr_gpu);
#endif

    // RDMA full-link communication
    int flink_nthreads = wukong::Global::num_proxies + wukong::Global::num_engines;
    // RDMA broadcast communication
    int bcast_nthreads = 2;
    int rdma_init_nthreads = flink_nthreads + bcast_nthreads;
    // init RDMA devices and connections
    wukong::RDMA_init(wukong::Global::num_servers, rdma_init_nthreads, sid, mrs, host_fname);

    // init communication
    wukong::RDMA_Adaptor *rdma_adaptor = new wukong::RDMA_Adaptor(sid, mrs,
            wukong::Global::num_servers, wukong::Global::num_threads);
    wukong::TCP_Adaptor *tcp_adaptor = new wukong::TCP_Adaptor(sid, host_fname, wukong::Global::data_port_base,
            wukong::Global::num_servers, wukong::Global::num_threads);

    // init control communicaiton
    wukong::con_adaptor = new wukong::TCP_Adaptor(sid, host_fname, wukong::Global::ctrl_port_base,
                                  wukong::Global::num_servers, wukong::Global::num_proxies);

    // load string server (read-only, shared by all proxies and all engines)
    wukong::StringServer str_server(wukong::Global::input_folder);

    // load RDF graph (shared by all engines and proxies)
#ifdef DYNAMIC_GSTORE
    wukong::DGraph * dgraph = new wukong::DynamicRDFGraph(sid, mem, &str_server);
#else
    wukong::DGraph * dgraph = new wukong::SegmentRDFGraph(sid, mem, &str_server);
#endif
    dgraph->load(wukong::Global::input_folder);

    // prepare statistics for SPARQL optimizer
    wukong::Stats stats(sid);
    uint64_t t0, t1;
    if (wukong::Global::generate_statistics) {
        t0 = wukong::timer::get_usec();
        stats.generate_statistics(dgraph);
        t1 = wukong::timer::get_usec();
        logstream(LOG_EMPH)  << "[Stats] generate statistics using time: " << t1 - t0 << "usec" << LOG_endl;
        stats.gather_stat(wukong::con_adaptor);
    } else {
        t0 = wukong::timer::get_usec();
        std::string fname = wukong::Global::input_folder + "/statfile";  // using default name
        stats.load_stat_from_file(fname, wukong::con_adaptor);
        t1 = wukong::timer::get_usec();
        logstream(LOG_EMPH)  << "[Stats] load statistics using time: " << t1 - t0 << "usec" << LOG_endl;
    }
    // create proxies and engines
    for (int tid = 0; tid < wukong::Global::num_proxies + wukong::Global::num_engines; tid++) {
        wukong::Adaptor *adaptor = new wukong::Adaptor(tid, tcp_adaptor, rdma_adaptor);

        // TID: proxy = [0, #proxies), engine = [#proxies, #proxies + #engines)
        if (tid < wukong::Global::num_proxies) {
            wukong::Proxy *proxy = new wukong::Proxy(sid, tid, &str_server, dgraph, adaptor, &stats);
            wukong::proxies.push_back(proxy);
        } else {
            wukong::Engine *engine = new wukong::Engine(sid, tid, &str_server, dgraph, adaptor);
            wukong::engines.push_back(engine);
        }
    }

    // launch all proxies and engines
    pthread_t *threads  = new pthread_t[wukong::Global::num_threads];
    for (int tid = 0; tid < wukong::Global::num_proxies + wukong::Global::num_engines; tid++) {
        // TID: proxy = [0, #proxies), engine = [#proxies, #proxies + #engines)
        if (tid < wukong::Global::num_proxies)
            pthread_create(&(threads[tid]), NULL, proxy_thread,
                           (void *)wukong::proxies[tid]);
        else
            pthread_create(&(threads[tid]), NULL, engine_thread,
                           (void *)wukong::engines[tid - wukong::Global::num_proxies]);
    }

#ifdef USE_GPU
    logstream(LOG_INFO) << "#" << sid
                        << " #threads:" << wukong::Global::num_threads
                        << ", #proxies:" << wukong::Global::num_proxies
                        << ", #engines:" << wukong::Global::num_engines
                        << ", #agent:" << wukong::Global::num_gpus << LOG_endl;

    // create GPU agent
    wukong::GPUStreamPool stream_pool(32);
    wukong::GPUCache gpu_cache(gpu_mem, (wukong::vertex_t *)dgraph->gstore->get_slot_addr(), (wukong::edge_t *)dgraph->gstore->get_value_addr(),
                       dynamic_cast<wukong::SegmentRDFGraph *>(dgraph)->get_rdf_seg_metas());
    wukong::GPUEngine gpu_engine(sid, WUKONG_GPU_AGENT_TID, gpu_mem, &gpu_cache, &stream_pool, dgraph);
    wukong::GPUAgent agent(sid, WUKONG_GPU_AGENT_TID, new wukong::Adaptor(WUKONG_GPU_AGENT_TID,
                   tcp_adaptor, rdma_adaptor), &gpu_engine);
    pthread_create(&(threads[WUKONG_GPU_AGENT_TID]), NULL, agent_thread, (void *)&agent);
#endif

    // wait to all threads termination
    for (size_t t = 0; t < wukong::Global::num_threads; t++) {
        if (int rc = pthread_join(threads[t], NULL)) {
            logger(LOG_ERROR, "return code from pthread_join() is %d\n", rc);
            exit(-1);
        }
    }

    /// TODO: exit gracefully (properly call MPI_Init() and MPI_Finalize(), delete all objects)
    return 0;
}

max_stars_repo_name: SJTU-IPADS/wukong-cube

max_issues_repo_name: SJTU-IPADS/wukong-cube

max_forks_repo_name: SJTU-IPADS/wukong-cube