text:
#include "Common/Common.h"
#include "Demos/Visualization/MiniGL.h"
#include "Demos/Visualization/Selection.h"
#include "GL/glut.h"
#include "Simulation/TimeManager.h"
#include <Eigen/Dense>
#include "Simulation/SimulationModel.h"
#include "Simulation/TimeStepController.h"
#include <iostream>
#include "Simulation/Constraints.h"
#include "Demos/Visualization/Visualization.h"
#include "Utils/OBJLoader.h"
#include "Utils/Logger.h"
#include "Utils/Timing.h"
#include "Utils/FileSystem.h"
#include "Demos/Common/DemoBase.h"
#include "Demos/Common/TweakBarParameters.h"
#include "Simulation/Simulation.h"

#define _USE_MATH_DEFINES

#include "math.h"

// Enable memory leak detection
#if defined(_DEBUG) && !defined(EIGEN_ALIGN)
#define new DEBUG_NEW
#endif

using namespace PBD;
using namespace Eigen;
using namespace std;
using namespace Utilities;

void initParameters();
void timeStep();
void buildModel();
void createRigidBodyModel();
void createClothMesh();
void render();
void reset();
void TW_CALL setBendingMethod(const void *value, void *clientData);
void TW_CALL getBendingMethod(void *value, void *clientData);
void TW_CALL setSimulationMethod(const void *value, void *clientData);
void TW_CALL getSimulationMethod(void *value, void *clientData);

DemoBase *base;
const int nRows = 20;
const int nCols = 20;
const Real clothWidth = 10.0;
const Real clothHeight = 10.0;
const Real width = static_cast<Real>(0.2);
const Real height = static_cast<Real>(2.0);
const Real depth = static_cast<Real>(0.2);
short simulationMethod = 2;
short bendingMethod = 2;

// main
int main(int argc, char **argv)
{
    REPORT_MEMORY_LEAKS

    base = new DemoBase();
    base->init(argc, argv, "Rigid-cloth coupling demo");

    SimulationModel *model = new SimulationModel();
    model->init();
    Simulation::getCurrent()->setModel(model);

    buildModel();

    initParameters();

    Simulation::getCurrent()->setSimulationMethodChangedCallback([&]()
                                                                 {
                                                                     reset();
                                                                     initParameters();
                                                                     base->getSceneLoader()->readParameterObject(Simulation::getCurrent()->getTimeStep());
                                                                 });

    // OpenGL
    MiniGL::setClientIdleFunc(50, timeStep);
    MiniGL::setKeyFunc(0, 'r', reset);
    MiniGL::setClientSceneFunc(render);
    MiniGL::setViewport(40.0, 0.1f, 500.0, Vector3r(0.0, 10.0, 30.0), Vector3r(0.0, 0.0, 0.0));

    TwType enumType2 = TwDefineEnum("SimulationMethodType", NULL, 0);
    TwAddVarCB(MiniGL::getTweakBar(), "SimulationMethod", enumType2, setSimulationMethod, getSimulationMethod, &simulationMethod,
               " label='Simulation method' enum='0 {None}, 1 {Distance constraints}, 2 {FEM based PBD}, 3 {Strain based dynamics}' group=Simulation");
    TwType enumType3 = TwDefineEnum("BendingMethodType", NULL, 0);
    TwAddVarCB(MiniGL::getTweakBar(), "BendingMethod", enumType3, setBendingMethod, getBendingMethod, &bendingMethod,
               " label='Bending method' enum='0 {None}, 1 {Dihedral angle}, 2 {Isometric bending}' group=Bending");

    glutMainLoop();

    base->cleanup();

    Utilities::Timing::printAverageTimes();
    Utilities::Timing::printTimeSums();

    delete Simulation::getCurrent();
    delete base;
    delete model;

    return 0;
}

void initParameters()
{
    TwRemoveAllVars(MiniGL::getTweakBar());
    TweakBarParameters::cleanup();

    MiniGL::initTweakBarParameters();

    TweakBarParameters::createParameterGUI();
    TweakBarParameters::createParameterObjectGUI(base);
    TweakBarParameters::createParameterObjectGUI(Simulation::getCurrent());
    TweakBarParameters::createParameterObjectGUI(Simulation::getCurrent()->getModel());
    TweakBarParameters::createParameterObjectGUI(Simulation::getCurrent()->getTimeStep());
}

void reset()
{
    Utilities::Timing::printAverageTimes();
    Utilities::Timing::reset();

    Simulation::getCurrent()->reset();
    base->getSelectedParticles().clear();

    Simulation::getCurrent()->getModel()->cleanup();

    buildModel();
}

void timeStep()
{
    const Real pauseAt = base->getValue<Real>(DemoBase::PAUSE_AT);
    if ((pauseAt > 0.0) && (pauseAt < TimeManager::getCurrent()->getTime()))
        base->setValue(DemoBase::PAUSE, true);

    if (base->getValue<bool>(DemoBase::PAUSE))
        return;

    // Simulation code
    SimulationModel *model = Simulation::getCurrent()->getModel();
    const unsigned int numSteps = base->getValue<unsigned int>(DemoBase::NUM_STEPS_PER_RENDER);
    for (unsigned int i = 0; i < numSteps; i++)
    {
        START_TIMING("SimStep");
        Simulation::getCurrent()->getTimeStep()->step(*model);
        STOP_TIMING_AVG;
    }

    for (unsigned int i = 0; i < model->getTriangleModels().size(); i++)
        model->getTriangleModels()[i]->updateMeshNormals(model->getParticles());
}

void buildModel()
{
    TimeManager::getCurrent()->setTimeStepSize(static_cast<Real>(0.005));

    createClothMesh();
    createRigidBodyModel();
}

void render()
{
    base->render();
}

// Compute diagonal inertia tensor
Vector3r computeInertiaTensorBox(const Real mass, const Real width, const Real height, const Real depth)
{
    const Real Ix = (mass / static_cast<Real>(12.0)) * (height * height + depth * depth);
    const Real Iy = (mass / static_cast<Real>(12.0)) * (width * width + depth * depth);
    const Real Iz = (mass / static_cast<Real>(12.0)) * (width * width + height * height);
    return Vector3r(Ix, Iy, Iz);
}

void loadObj(const std::string &filename, VertexData &vd, IndexedFaceMesh &mesh, const Vector3r &scale)
{
    std::vector<OBJLoader::Vec3f> x;
    std::vector<OBJLoader::Vec3f> normals;
    std::vector<OBJLoader::Vec2f> texCoords;
    std::vector<MeshFaceIndices> faces;
    OBJLoader::Vec3f s = {(float) scale[0], (float) scale[1], (float) scale[2]};
    OBJLoader::loadObj(filename, &x, &faces, &normals, &texCoords, s);

    mesh.release();
    const unsigned int nPoints = (unsigned int) x.size();
    const unsigned int nFaces = (unsigned int) faces.size();
    const unsigned int nTexCoords = (unsigned int) texCoords.size();
    mesh.initMesh(nPoints, nFaces * 2, nFaces);
    vd.reserve(nPoints);
    for (unsigned int i = 0; i < nPoints; i++)
    {
        vd.addVertex(Vector3r(x[i][0], x[i][1], x[i][2]));
    }
    for (unsigned int i = 0; i < nTexCoords; i++)
    {
        mesh.addUV(texCoords[i][0], texCoords[i][1]);
    }
    for (unsigned int i = 0; i < nFaces; i++)
    {
        // Reduce the indices by one
        int posIndices[3];
        int texIndices[3];
        for (int j = 0; j < 3; j++)
        {
            posIndices[j] = faces[i].posIndices[j] - 1;
            if (nTexCoords > 0)
            {
                texIndices[j] = faces[i].texIndices[j] - 1;
                mesh.addUVIndex(texIndices[j]);
            }
        }

        mesh.addFace(&posIndices[0]);
    }
    mesh.buildNeighbors();

    mesh.updateNormals(vd, 0);
    mesh.updateVertexNormals(vd);

    LOG_INFO << "Number of triangles: " << nFaces;
    LOG_INFO << "Number of vertices: " << nPoints;
}

/** Create the model
*/
void createRigidBodyModel()
{
    SimulationModel *model = Simulation::getCurrent()->getModel();
    SimulationModel::RigidBodyVector &rb = model->getRigidBodies();
    SimulationModel::ConstraintVector &constraints = model->getConstraints();

    string fileName = FileSystem::normalizePath(base->getDataPath() + "/models/cube.obj");
    IndexedFaceMesh mesh;
    VertexData vd;
    loadObj(fileName, vd, mesh, Vector3r(width, height, depth));

    IndexedFaceMesh mesh_static;
    VertexData vd_static;
    loadObj(fileName, vd_static, mesh_static, Vector3r(0.5, 0.5, 0.5));

    rb.resize(12);

    //////////////////////////////////////////////////////////////////////////
    // -5, -5
    //////////////////////////////////////////////////////////////////////////
    rb[0] = new RigidBody();
    rb[0]->initBody(0.0, Vector3r(-5.0, 0.0, -5.0), computeInertiaTensorBox(1.0, 0.5, 0.5, 0.5), Quaternionr(1.0, 0.0, 0.0, 0.0), vd_static, mesh_static);

    // dynamic body
    rb[1] = new RigidBody();
    rb[1]->initBody(1.0, Vector3r(-5.0, 1.0, -5.0), computeInertiaTensorBox(1.0, width, height, depth), Quaternionr(1.0, 0.0, 0.0, 0.0), vd, mesh);

    // dynamic body
    rb[2] = new RigidBody();
    rb[2]->initBody(1.0, Vector3r(-5.0, 3.0, -5.0), computeInertiaTensorBox(1.0, width, height, depth), Quaternionr(1.0, 0.0, 0.0, 0.0), vd, mesh);

    model->addBallJoint(0, 1, Vector3r(-5.0, 0.0, -5.0));
    model->addBallJoint(1, 2, Vector3r(-5.0, 2.0, -5.0));

    //////////////////////////////////////////////////////////////////////////
    // 5, -5
    //////////////////////////////////////////////////////////////////////////
    rb[3] = new RigidBody();
    rb[3]->initBody(0.0, Vector3r(5.0, 0.0, -5.0), computeInertiaTensorBox(1.0, 0.5, 0.5, 0.5), Quaternionr(1.0, 0.0, 0.0, 0.0), vd_static, mesh_static);

    // dynamic body
    rb[4] = new RigidBody();
    rb[4]->initBody(1.0, Vector3r(5.0, 1.0, -5.0), computeInertiaTensorBox(1.0, width, height, depth), Quaternionr(1.0, 0.0, 0.0, 0.0), vd, mesh);

    // dynamic body
    rb[5] = new RigidBody();
    rb[5]->initBody(1.0, Vector3r(5.0, 3.0, -5.0), computeInertiaTensorBox(1.0, width, height, depth), Quaternionr(1.0, 0.0, 0.0, 0.0), vd, mesh);

    model->addBallJoint(3, 4, Vector3r(5.0, 0.0, -5.0));
    model->addBallJoint(4, 5, Vector3r(5.0, 2.0, -5.0));

    //////////////////////////////////////////////////////////////////////////
    // 5, 5
    //////////////////////////////////////////////////////////////////////////
    rb[6] = new RigidBody();
    rb[6]->initBody(0.0, Vector3r(5.0, 0.0, 5.0), computeInertiaTensorBox(1.0, 0.5, 0.5, 0.5), Quaternionr(1.0, 0.0, 0.0, 0.0), vd_static, mesh_static);

    // dynamic body
    rb[7] = new RigidBody();
    rb[7]->initBody(1.0, Vector3r(5.0, 1.0, 5.0), computeInertiaTensorBox(1.0, width, height, depth), Quaternionr(1.0, 0.0, 0.0, 0.0), vd, mesh);

    // dynamic body
    rb[8] = new RigidBody();
    rb[8]->initBody(1.0, Vector3r(5.0, 3.0, 5.0), computeInertiaTensorBox(1.0, width, height, depth), Quaternionr(1.0, 0.0, 0.0, 0.0), vd, mesh);

    model->addBallJoint(6, 7, Vector3r(5.0, 0.0, 5.0));
    model->addBallJoint(7, 8, Vector3r(5.0, 2.0, 5.0));

    //////////////////////////////////////////////////////////////////////////
    // -5, 5
    //////////////////////////////////////////////////////////////////////////
    rb[9] = new RigidBody();
    rb[9]->initBody(0.0, Vector3r(-5.0, 0.0, 5.0), computeInertiaTensorBox(1.0, 0.5, 0.5, 0.5), Quaternionr(1.0, 0.0, 0.0, 0.0), vd_static, mesh_static);

    // dynamic body
    rb[10] = new RigidBody();
    rb[10]->initBody(1.0, Vector3r(-5.0, 1.0, 5.0), computeInertiaTensorBox(1.0, width, height, depth), Quaternionr(1.0, 0.0, 0.0, 0.0), vd, mesh);

    // dynamic body
    rb[11] = new RigidBody();
    rb[11]->initBody(1.0, Vector3r(-5.0, 3.0, 5.0), computeInertiaTensorBox(1.0, width, height, depth), Quaternionr(1.0, 0.0, 0.0, 0.0), vd, mesh);

    model->addBallJoint(9, 10, Vector3r(-5.0, 0.0, 5.0));
    model->addBallJoint(10, 11, Vector3r(-5.0, 2.0, 5.0));

    model->addRigidBodyParticleBallJoint(2, 0);
    model->addRigidBodyParticleBallJoint(5, (nRows - 1) * nCols);
    model->addRigidBodyParticleBallJoint(8, nRows * nCols - 1);
    model->addRigidBodyParticleBallJoint(11, nCols - 1);

}

/** Create a particle model mesh
*/
void createClothMesh()
{
    TriangleModel::ParticleMesh::UVs uvs;
    uvs.resize(nRows * nCols);

    const Real dy = clothWidth / (Real) (nCols - 1);
    const Real dx = clothHeight / (Real) (nRows - 1);

    Vector3r points[nRows * nCols];
    for (int i = 0; i < nRows; i++)
    {
        for (int j = 0; j < nCols; j++)
        {
            const Real y = (Real) dy * j;
            const Real x = (Real) dx * i;
            points[i * nCols + j] = Vector3r(x - static_cast<Real>(5.0), static_cast<Real>(4.0), y - static_cast<Real>(5.0));

            uvs[i * nCols + j][0] = x / clothWidth;
            uvs[i * nCols + j][1] = y / clothHeight;
        }
    }
    const int nIndices = 6 * (nRows - 1) * (nCols - 1);

    TriangleModel::ParticleMesh::UVIndices uvIndices;
    uvIndices.resize(nIndices);

    unsigned int indices[nIndices];
    int index = 0;
    for (int i = 0; i < nRows - 1; i++)
    {
        for (int j = 0; j < nCols - 1; j++)
        {
            int helper = 0;
            if (i % 2 == j % 2)
                helper = 1;

            indices[index] = i * nCols + j;
            indices[index + 1] = i * nCols + j + 1;
            indices[index + 2] = (i + 1) * nCols + j + helper;

            uvIndices[index] = i * nCols + j;
            uvIndices[index + 1] = i * nCols + j + 1;
            uvIndices[index + 2] = (i + 1) * nCols + j + helper;
            index += 3;

            indices[index] = (i + 1) * nCols + j + 1;
            indices[index + 1] = (i + 1) * nCols + j;
            indices[index + 2] = i * nCols + j + 1 - helper;

            uvIndices[index] = (i + 1) * nCols + j + 1;
            uvIndices[index + 1] = (i + 1) * nCols + j;
            uvIndices[index + 2] = i * nCols + j + 1 - helper;
            index += 3;
        }
    }

    SimulationModel *model = Simulation::getCurrent()->getModel();
    model->addTriangleModel(nRows * nCols, nIndices / 3, &points[0], &indices[0], uvIndices, uvs);

    ParticleData &pd = model->getParticles();
    for (unsigned int i = 0; i < pd.getNumberOfParticles(); i++)
    {
        pd.setMass(i, 1.0);
    }

    // init constraints
    for (unsigned int cm = 0; cm < model->getTriangleModels().size(); cm++)
    {
        if (simulationMethod == 1)
        {
            const unsigned int offset = model->getTriangleModels()[cm]->getIndexOffset();
            const unsigned int nEdges = model->getTriangleModels()[cm]->getParticleMesh().numEdges();
            const IndexedFaceMesh::Edge *edges = model->getTriangleModels()[cm]->getParticleMesh().getEdges().data();
            for (unsigned int i = 0; i < nEdges; i++)
            {
                const unsigned int v1 = edges[i].m_vert[0] + offset;
                const unsigned int v2 = edges[i].m_vert[1] + offset;

                model->addDistanceConstraint(v1, v2);
            }
        } else if (simulationMethod == 2)
        {
            const unsigned int offset = model->getTriangleModels()[cm]->getIndexOffset();
            TriangleModel::ParticleMesh &mesh = model->getTriangleModels()[cm]->getParticleMesh();
            const unsigned int *tris = mesh.getFaces().data();
            const unsigned int nFaces = mesh.numFaces();
            for (unsigned int i = 0; i < nFaces; i++)
            {
                const unsigned int v1 = tris[3 * i] + offset;
                const unsigned int v2 = tris[3 * i + 1] + offset;
                const unsigned int v3 = tris[3 * i + 2] + offset;
                model->addFEMTriangleConstraint(v1, v2, v3);
            }
        } else if (simulationMethod == 3)
        {
            const unsigned int offset = model->getTriangleModels()[cm]->getIndexOffset();
            TriangleModel::ParticleMesh &mesh = model->getTriangleModels()[cm]->getParticleMesh();
            const unsigned int *tris = mesh.getFaces().data();
            const unsigned int nFaces = mesh.numFaces();
            for (unsigned int i = 0; i < nFaces; i++)
            {
                const unsigned int v1 = tris[3 * i] + offset;
                const unsigned int v2 = tris[3 * i + 1] + offset;
                const unsigned int v3 = tris[3 * i + 2] + offset;
                model->addStrainTriangleConstraint(v1, v2, v3);
            }
        }
        if (bendingMethod != 0)
        {
            const unsigned int offset = model->getTriangleModels()[cm]->getIndexOffset();
            TriangleModel::ParticleMesh &mesh = model->getTriangleModels()[cm]->getParticleMesh();
            unsigned int nEdges = mesh.numEdges();
            const TriangleModel::ParticleMesh::Edge *edges = mesh.getEdges().data();
            const unsigned int *tris = mesh.getFaces().data();
            for (unsigned int i = 0; i < nEdges; i++)
            {
                const int tri1 = edges[i].m_face[0];
                const int tri2 = edges[i].m_face[1];
                if ((tri1 != 0xffffffff) && (tri2 != 0xffffffff))
                {
                    // Find the triangle points which do not lie on the axis
                    const int axisPoint1 = edges[i].m_vert[0];
                    const int axisPoint2 = edges[i].m_vert[1];
                    int point1 = -1;
                    int point2 = -1;
                    for (int j = 0; j < 3; j++)
                    {
                        if ((tris[3 * tri1 + j] != axisPoint1) && (tris[3 * tri1 + j] != axisPoint2))
                        {
                            point1 = tris[3 * tri1 + j];
                            break;
                        }
                    }
                    for (int j = 0; j < 3; j++)
                    {
                        if ((tris[3 * tri2 + j] != axisPoint1) && (tris[3 * tri2 + j] != axisPoint2))
                        {
                            point2 = tris[3 * tri2 + j];
                            break;
                        }
                    }
                    if ((point1 != -1) && (point2 != -1))
                    {
                        const unsigned int vertex1 = point1 + offset;
                        const unsigned int vertex2 = point2 + offset;
                        const unsigned int vertex3 = edges[i].m_vert[0] + offset;
                        const unsigned int vertex4 = edges[i].m_vert[1] + offset;
                        if (bendingMethod == 1)
                            model->addDihedralConstraint(vertex1, vertex2, vertex3, vertex4);
                        else if (bendingMethod == 2)
                            model->addIsometricBendingConstraint(vertex1, vertex2, vertex3, vertex4);
                    }
                }
            }
        }
    }

    LOG_INFO << "Number of triangles: " << nIndices / 3;
    LOG_INFO << "Number of vertices: " << nRows * nCols;

}

void TW_CALL setBendingMethod(const void *value, void *clientData)
{
    const short val = *(const short *) (value);
    *((short *) clientData) = val;
    reset();
}

void TW_CALL getBendingMethod(void *value, void *clientData)
{
    *(short *) (value) = *((short *) clientData);
}

void TW_CALL setSimulationMethod(const void *value, void *clientData)
{
    const short val = *(const short *) (value);
    *((short *) clientData) = val;
    reset();
}

void TW_CALL getSimulationMethod(void *value, void *clientData)
{
    *(short *) (value) = *((short *) clientData);
}

ext: cpp

max_stars_repo_name: Xayahp/branchfrac

max_issues_repo_name: Xayahp/branchfrac

max_forks_repo_name: Xayahp/branchfrac


text:
//  run_timer_example.cpp  ---------------------------------------------------//

//  Copyright Beman Dawes 2006, 2008

//  Distributed under the Boost Software License, Version 1.0.
//  See http://www.boost.org/LICENSE_1_0.txt

//  See http://www.boost.org/libs/chrono for documentation.

#include <boost/chrono/process_times.hpp>
#include <cmath>

int main( int argc, char * argv[] )
{
  const char * format = argc > 1 ? argv[1] : "%t cpu seconds\n";
  int          places = argc > 2 ? std::atoi( argv[2] ) : 2;

  boost::chrono::run_timer t( format, places );

  for ( long i = 0; i < 10000; ++i )
    std::sqrt( 123.456L ); // burn some time

  return 0;
}


text:
/// @file
/// @brief Service based implementation of the calibration solution source
/// @details This implementation is to be used with the Calibration Data Service
/// Main functionality is implemented in the corresponding ServiceCalSolutionAccessor class.
/// This class just creates an instance of the accessor and manages it.
///
/// @copyright (c) 2017 CSIRO
/// Australia Telescope National Facility (ATNF)
/// Commonwealth Scientific and Industrial Research Organisation (CSIRO)
/// PO Box 76, Epping NSW 1710, Australia
/// atnf-enquiries@csiro.au
///
/// This file is part of the ASKAP software distribution.
///
/// The ASKAP software distribution is free software: you can redistribute it
/// and/or modify it under the terms of the GNU General Public License as
/// published by the Free Software Foundation; either version 2 of the License,
/// or (at your option) any later version.
///
/// This program is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with this program; if not, write to the Free Software
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
///
/// @author Max Voronkov <Maxim.Voronkov@csiro.au>
/// @author Stephen Ord <Stephen.Ord@csiro.au>

#include <calibaccess/ServiceCalSolutionSourceStub.h>
#include "ServiceCalSolutionSource.h"
#include "ServiceCalSolutionAccessor.h"

#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

// logging stuff
#include <askap_accessors.h>
#include <askap/AskapLogging.h>
ASKAP_LOGGER(logger, ".calibaccess");

namespace askap {

namespace accessors {

/// @brief constructor
/// @details Creates solution source object for a given parset file
/// (whether it is for writing or reading depends on the actual methods
/// used). Also need to decide whether it is the source or the accessor that
/// creates the client. I think it should be the source then an accessor of any
/// particular type can be instaniated when required.
/// But the accessor does need a communicator....
/// how about the source instantiates the client and the accessor is instantiaed using the client
///
/// @param[in] parset parset file name
ServiceCalSolutionSource::ServiceCalSolutionSource(const LOFAR::ParameterSet &parset) : ServiceCalSolutionSourceStub(parset)
  {
  ASKAPLOG_WARN_STR(logger, "ServiceCalSolutionSource constructor - override the stub");

  // Need to generate the calibrationclient and set up all the solutions

  const string locatorHost = parset.getString("ice.locator.host");
  const string locatorPort = parset.getString("ice.locator.port");
  const string serviceName = parset.getString("calibrationdataservice.name");
  long solution = parset.getInt("solution.id",-1);
  const double solutionTime = parset.getDouble("solution.time",-1.0);
  bool newSol = parset.getBool("solution.new",false);

  itsClient = boost::make_shared<askap::cp::caldataservice::CalibrationDataServiceClient> (locatorHost, locatorPort, serviceName);

  if (solutionTime > 0) {
    if (solution > 0) {
      ASKAPTHROW(AskapError, "Ambiguous parameters: Specified a solution ID and a time");
    }
    if (!newSol) {
      solution = this->solutionID(solutionTime);
    }
    else {
      // new solution requested
      // need to know the solution size for this to work
      solution = this->newSolutionID(solutionTime);

    }
  }

  // should have a valid solution ID now.

  if (solution > 0) {
    /// Requesting a specific solutionID
    if (!newSol) {
        ASKAPLOG_WARN_STR(logger, "ServiceCalSolutionAccessor Read Only with a known solution ID");
        itsAccessor.reset(new ServiceCalSolutionAccessor(itsClient, solution, true));
    }
    else {
        ASKAPLOG_WARN_STR(logger, "ServiceCalSolutionAccessor RW with a new solution ID");

        itsAccessor.reset(new ServiceCalSolutionAccessor(itsClient, solution,false));

        int nAnt = parset.getInt("solution.nant",0);
        if (nAnt == 0) {
          nAnt = parset.getInt("nAnt",0);
        }
        int nBeam = parset.getInt("solution.nbeam",0);
        if (nBeam == 0) {
          nBeam = parset.getInt("nBeam",0);
        }

        if (nAnt == 0 || nBeam == 0) {
          ASKAPTHROW(AskapError, "Ambiguous parameters:Specified new solution but did not provide nAnt or NBeam");
        }
        else {

          this->addDefaultGainSolution(solution, solutionTime, nAnt, nBeam);
          this->addDefaultLeakageSolution(solution, solutionTime, nAnt, nBeam);
        }
        int nChan = parset.getInt("solution.nchan",0);
        if (nChan == 0) {
          nChan = parset.getInt("nChan",0);
        }
        if (nChan == 0) {
          ASKAPLOG_WARN_STR(logger, "Cannot add a bandpass solution for this ID - no chan in parset");

        }
        else {
          this->addDefaultBandpassSolution(solution, solutionTime, nAnt, nBeam,nChan);
        }
        boost::shared_ptr<ServiceCalSolutionAccessor> acc = boost::dynamic_pointer_cast<ServiceCalSolutionAccessor>(accessor());
        acc->solutionsValid = true; // valid but default ....

    }
  }
  else if (solution == 0){
    ASKAPLOG_WARN_STR(logger, "ServiceCalSolutionAccessor Read Only with the most recent ID");
    itsAccessor.reset(new ServiceCalSolutionAccessor(itsClient,this->mostRecentSolution(),true));
  }
  else if (solution < 0) {
    /// Going to make a completely new solution
    /// specify a blank one?
    ASKAPLOG_WARN_STR(logger, "ServiceCalSolutionAccessor NO ACCESSOR");
    if (solutionTime < 0) {
      ASKAPTHROW(AskapError, "Ambiguous parameters: Specified a new solution but did not give a timestamp");
    }

  }
}

/// @brief obtain ID for the most recent solution
/// @return ID for the most recent solution
/// @note This particular implementation doesn't support multiple
/// solutions and, therefore, always returns the same ID.
long ServiceCalSolutionSource::mostRecentSolution() const
{

  return itsClient->getLatestSolutionID();

}

/**
 * Obtain smallest solution ID corresponding to the time >= the given timestamp
 * @param timestamp absolute time given as MJD in the UTC frame (same as timestamp
 *                  in solutions - can be compared directly)
 * @return solution ID
 * @note gain, bandpass and leakage solutions corresponding to one solution ID
 *       can have different timestamps. Use the greatest for comparison.
 * if all the timestamps in the stored solutions are less than the given timestamp,
 * this method is equivalent to getLatestSolutionID().
 */

long ServiceCalSolutionSource::solutionID(const double timetag) const
{

  itsClient->getUpperBoundID(timetag);

  return 0;
}

/// @brief obtain read-only accessor for a given solution ID
/// @details This method returns a shared pointer to the solution accessor, which
/// can be used to read the parameters. If a solution with the given ID doesn't
/// exist, an exception is thrown. Existing solutions with undefined parameters
/// are managed via validity flags of gains, leakages and bandpasses
/// @return shared pointer to an accessor object
/// @note This particular implementation doesn't support multiple solutions and
/// always returns the same accessor (for both reading and writing)
boost::shared_ptr<ICalSolutionConstAccessor> ServiceCalSolutionSource::roSolution(const long ID) const
{

  ASKAPDEBUGASSERT(accessor());
  boost::shared_ptr<ICalSolutionAccessor> acc = boost::dynamic_pointer_cast<ICalSolutionAccessor>(accessor());
  ASKAPCHECK(acc,
     "Unable to cast solution accessor to read-write type, CalSolutionSourceStub has been initialised with an incompatible object");
  return acc;
}

/// @brief obtain a solution ID to store new solution
/// @details This method provides a solution ID for a new solution. It must
/// be called before any write operation (one needs a writable accessor to
/// write the actual solution and to get this accessor one needs an ID).
/// @param[in] time time stamp of the new solution in seconds since MJD of 0.
/// @return solution ID

long ServiceCalSolutionSource::newSolutionID(const double timetag)
{
  // the time tag is added to the solution when it is created.
  return itsClient->newSolutionID();

}
/// @brief obtain a writeable accessor for a given solution ID
/// @details This method returns a shared pointer to the solution accessor, which
/// can be used to both read the parameters and write them back. If a solution with
/// the given ID doesn't exist, an exception is thrown. Existing solutions with undefined
/// parameters are managed via validity flags of gains, leakages and bandpasses
/// @return shared pointer to an accessor object
/// @note This particular implementation returns the same accessor regardless of the
/// chosen ID (for both reading and writing)
boost::shared_ptr<ICalSolutionAccessor> ServiceCalSolutionSource::rwSolution(const long ID) const
{

  ASKAPDEBUGASSERT(accessor());
  boost::shared_ptr<ICalSolutionAccessor> acc = boost::dynamic_pointer_cast<ICalSolutionAccessor>(accessor());
  ASKAPCHECK(acc,
     "Unable to cast solution accessor to read-write type, CalSolutionSourceStub has been initialised with an incompatible object");
  return acc;
}
void ServiceCalSolutionSource::addDefaultGainSolution(const long id,
        const double timestamp,
        const short nAntenna, const short nBeam)
{
    ASKAPASSERT(accessor());
    boost::shared_ptr<ServiceCalSolutionAccessor> acc = boost::dynamic_pointer_cast<ServiceCalSolutionAccessor>(accessor());

    ASKAPLOG_INFO_STR(logger, "addDefaultGainSolution");
    askap::cp::caldataservice::GainSolution sol(timestamp);
    // Create a map entry for each antenna/beam combination
    for (short  antenna = 0; antenna < nAntenna; ++antenna) {
        for (short beam = 0; beam < nBeam; ++beam) {
            JonesJTerm jterm(casa::Complex(1.0, 1.0), true,
                    casa::Complex(1.0, 1.0), true);
            sol.map()[askap::accessors::JonesIndex(antenna, beam)] = jterm;
        }
    }

    acc->addGainSolution(sol);
}

void ServiceCalSolutionSource::addDefaultLeakageSolution( const long id,
        const double timestamp,
        const short nAntenna, const short nBeam)
{
    ASKAPASSERT(accessor());
    boost::shared_ptr<ServiceCalSolutionAccessor> acc = boost::dynamic_pointer_cast<ServiceCalSolutionAccessor>(accessor());
    ASKAPLOG_INFO_STR(logger, "addDefaultLeakageSolution");
    askap::cp::caldataservice::LeakageSolution sol(timestamp);
    // Create a map entry for each antenna/beam combination
    for (short antenna = 0; antenna < nAntenna; ++antenna) {
        for (short beam = 0; beam < nBeam; ++beam) {
            sol.map()[askap::accessors::JonesIndex(antenna, beam)] = askap::accessors::JonesDTerm(
                    casa::Complex(1.0, 1.0), casa::Complex(1.0, 1.0));
        }
    }

    acc->addLeakageSolution(sol);
}

void ServiceCalSolutionSource::addDefaultBandpassSolution(const long id,
        const double timestamp,
        const short nAntenna, const short nBeam, const int nChan)
{
    ASKAPASSERT(accessor());
    boost::shared_ptr<ServiceCalSolutionAccessor> acc = boost::dynamic_pointer_cast<ServiceCalSolutionAccessor>(accessor());
    ASKAPLOG_INFO_STR(logger, "addDefaultBandpassSolution");
    askap::cp::caldataservice::BandpassSolution sol(timestamp);
    // Create a map entry for each antenna/beam combination
    for (short antenna = 0; antenna < nAntenna; ++antenna) {
        for (short beam = 0; beam < nBeam; ++beam) {
            JonesJTerm jterm(casa::Complex(1.0, 1.0), true,
                    casa::Complex(1.0, 1.0), true);
            std::vector<askap::accessors::JonesJTerm> jterms(nChan, jterm);
            sol.map()[askap::accessors::JonesIndex(antenna, beam)] = jterms;
        }
    }

    acc->addBandpassSolution(sol);
}

/// @brief this source will be used to solve for the following:
/// @details this works by setting flags in the accessor to allow it to
/// update the solution in the database. Without these the solution will still
/// be found but <not> pushed to the service

void ServiceCalSolutionSource::solveGains() {
  ASKAPASSERT(accessor());
  boost::shared_ptr<ServiceCalSolutionAccessor> acc = boost::dynamic_pointer_cast<ServiceCalSolutionAccessor>(accessor());
  acc->willPushGains();

}
void ServiceCalSolutionSource::solveLeakages() {
  ASKAPASSERT(accessor());
  boost::shared_ptr<ServiceCalSolutionAccessor> acc = boost::dynamic_pointer_cast<ServiceCalSolutionAccessor>(accessor());
  acc->willPushLeakages();

}
void ServiceCalSolutionSource::solveBandpass() {
  ASKAPASSERT(accessor());
  boost::shared_ptr<ServiceCalSolutionAccessor> acc = boost::dynamic_pointer_cast<ServiceCalSolutionAccessor>(accessor());
  acc->willPushBandpass();

}

} // accessors

} // namespace askap

ext: cc

max_stars_repo_name: steve-ord/askapsoft

max_issues_repo_name: ATNF/askapsoft

max_forks_repo_name: ATNF/askapsoft


text:
// Copyright (c) 2012 Christopher Lux <christopherlux@gmail.com>
// Distributed under the Modified BSD License, see license.txt.

#include "wgl_extensions.h"

#if SCM_PLATFORM == SCM_PLATFORM_WINDOWS

#include <ostream>

#include <boost/tokenizer.hpp>

#include <scm/core/memory.h>

#include <scm/gl_core/window_management/wm_win32/util/classic_context_win32.h>

namespace scm {
namespace gl {
namespace wm {
namespace util {

wgl_extensions::wgl_extensions()
{
    _initialized = false;

    // WGL_ARB_create_context
    wglCreateContextAttribsARB      = 0;

    // WGL_ARB_pixel_format
    wglGetPixelFormatAttribivARB    = 0;
    wglGetPixelFormatAttribfvARB    = 0;
    wglChoosePixelFormatARB         = 0;

    // WGL_ARB_extensions_string
    wglGetExtensionsStringARB       = 0;

    // WGL_EXT_swap_control
    wglSwapIntervalEXT              = 0;
    wglGetSwapIntervalEXT           = 0;

    // WGL_ARB_pbuffer
    wglCreatePbufferARB             = 0;
    wglGetPbufferDCARB              = 0;
    wglReleasePbufferDCARB          = 0;
    wglDestroyPbufferARB            = 0;
    wglQueryPbufferARB              = 0;

    _swap_control_supported         = false;
}

bool
wgl_extensions::initialize(std::ostream& os)
{
    if (is_initialized()) {
        return (true);
    }

    if (0 == wglGetCurrentContext()) {
        os << "wgl_extensions::initialize(): no OpenGL context present, unable to initialize WGL extensions.";
        return (false);
    }

    // WGL_ARB_extensions_string
    wglGetExtensionsStringARB       = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");

    if (wglGetExtensionsStringARB == 0) {
        os << "wgl_extensions::initialize(): WGL_ARB_extensions_string not supported";
        return (false);
    }

    // get and tokenize the extension strings
    HDC cur_hdc = wglGetCurrentDC();
    if (cur_hdc == 0) {
        os << "wgl_extensions::initialize(): unable to retrieve current HDC";
        return (false);
    }
    std::string wgl_ext_string = reinterpret_cast<const char*>(wglGetExtensionsStringARB(cur_hdc));

    typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
    boost::char_separator<char> space_separator(" ");
    tokenizer                   extension_strings(wgl_ext_string, space_separator);

    for (tokenizer::const_iterator i = extension_strings.begin(); i != extension_strings.end(); ++i) {
        _wgl_extensions.insert(std::string(*i));
    }

    // WGL_ARB_create_context
    wglCreateContextAttribsARB      = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");

    if (wglCreateContextAttribsARB == 0) {
        os << "wgl_extensions::initialize(): WGL_ARB_create_context not supported";
        return (false);
    }

    // WGL_ARB_pixel_format
    wglGetPixelFormatAttribivARB    = (PFNWGLGETPIXELFORMATATTRIBIVARBPROC)wglGetProcAddress("wglGetPixelFormatAttribivARB");
    wglGetPixelFormatAttribfvARB    = (PFNWGLGETPIXELFORMATATTRIBFVARBPROC)wglGetProcAddress("wglGetPixelFormatAttribfvARB");
    wglChoosePixelFormatARB         = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");

    if (   wglGetPixelFormatAttribivARB == 0
        || wglGetPixelFormatAttribfvARB == 0
        || wglChoosePixelFormatARB == 0) {
        os << "wgl_extensions::initialize(): WGL_ARB_pixel_format not supported";
        return (false);
    }

    // WGL_EXT_swap_control
    wglSwapIntervalEXT              = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
    wglGetSwapIntervalEXT           = (PFNWGLGETSWAPINTERVALEXTPROC)wglGetProcAddress("wglGetSwapIntervalEXT");

    if (   wglSwapIntervalEXT == 0
        || wglGetSwapIntervalEXT == 0) {
        os << "wgl_extensions::initialize(): WGL_EXT_swap_control not supported";
        return (false);
    }

    if (  !is_supported("WGL_ARB_framebuffer_sRGB")
        ||!is_supported("WGL_EXT_framebuffer_sRGB")) {
        os << "wgl_extensions::initialize(): WGL_ARB_framebuffer_sRGB not supported";
    }
    if (!is_supported("WGL_ARB_multisample")) {
        os << "wgl_extensions::initialize(): WGL_ARB_multisample not supported";
    }

    // WGL_ARB_pbuffer
    wglCreatePbufferARB             = (PFNWGLCREATEPBUFFERARBPROC)wglGetProcAddress("wglCreatePbufferARB");
    wglGetPbufferDCARB              = (PFNWGLGETPBUFFERDCARBPROC)wglGetProcAddress("wglGetPbufferDCARB");
    wglReleasePbufferDCARB          = (PFNWGLRELEASEPBUFFERDCARBPROC)wglGetProcAddress("wglReleasePbufferDCARB");
    wglDestroyPbufferARB            = (PFNWGLDESTROYPBUFFERARBPROC)wglGetProcAddress("wglDestroyPbufferARB");
    wglQueryPbufferARB              = (PFNWGLQUERYPBUFFERARBPROC)wglGetProcAddress("wglQueryPbufferARB");

    if (   wglCreatePbufferARB == 0
        || wglGetPbufferDCARB == 0
        || wglReleasePbufferDCARB == 0
        || wglDestroyPbufferARB == 0
        || wglQueryPbufferARB == 0) {
        os << "wgl_extensions::initialize(): WGL_ARB_pbuffer not supported";
        return (false);
    }

    if (is_supported("WGL_EXT_swap_control")) {
        _swap_control_supported = true;
    }

    _initialized = true;

    return (true);
}

bool
wgl_extensions::is_initialized() const
{
    return (_initialized);
}

bool
wgl_extensions::is_supported(const std::string& ext) const
{
    if (_wgl_extensions.find(ext) != _wgl_extensions.end()) {
        return (true);
    }
    else {
        return (false);
    }
}

std::ostream& operator<<(std::ostream& out_stream, const wgl_extensions& w)
{
    std::ostream::sentry const  out_sentry(out_stream);

    out_stream << "extensions :      " << "(found " << w._wgl_extensions.size() << ")" << std::endl;

    for (wgl_extensions::string_set::const_iterator i = w._wgl_extensions.begin(); i != w._wgl_extensions.end(); ++i) {
        out_stream << "                  " << *i << std::endl;
    }

    return out_stream;
}

} // namespace util
} // namespace wm
} // namepspace gl
} // namepspace scm

#endif // SCM_PLATFORM == SCM_PLATFORM_WINDOWS

max_stars_repo_name: Nyran/schism

max_issues_repo_name: Nyran/schism

max_forks_repo_name: Nyran/schism


text:
static bool eigen_did_assert = false;
#define eigen_assert(X) if(!eigen_did_assert && !(X)){ std::cout << "### Assertion raised in " << __FILE__ << ":" << __LINE__ << ":\n" #X << "\n### The following would happen without assertions:\n"; eigen_did_assert = true;}

#include <iostream>
#include <Eigen/Eigen>

#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif

using namespace Eigen;
using namespace std;

int main(int, char**)
{
  cout.precision(3);
  std::cout << "A fixed-size array:\n";
Array33f a1 = Array33f::Zero();
std::cout << a1 << "\n\n";

std::cout << "A one-dimensional dynamic-size array:\n";
ArrayXf a2 = ArrayXf::Zero(3);
std::cout << a2 << "\n\n";

std::cout << "A two-dimensional dynamic-size array:\n";
ArrayXXf a3 = ArrayXXf::Zero(3, 4);
std::cout << a3 << "\n";

  return 0;
}

max_stars_repo_name: mousepawmedia/libdeps

max_issues_repo_name: mousepawmedia/libdeps

max_forks_repo_name: mousepawmedia/libdeps


text:
/*
 * Copyright (c) 2014 CodiLime, Inc. All rights reserved.
 */

#include "bfd/bfd_session.h"
#include "bfd/bfd_control_packet.h"
#include "bfd/bfd_common.h"
#include "bfd/bfd_connection.h"

#include <boost/asio.hpp>
#include <boost/random.hpp>
#include <string>
#include <algorithm>

#include "base/logging.h"

namespace BFD {

Session::Session(Discriminator localDiscriminator,
        const SessionKey &key,
        EventManager *evm,
        const SessionConfig &config, Connection *communicator) :
        localDiscriminator_(localDiscriminator),
        key_(key),
        sendTimer_(TimerManager::CreateTimer(*evm->io_service(),
            "BFD TX", TaskScheduler::GetInstance()->GetTaskId("BFD"), 0)),
        recvTimer_(TimerManager::CreateTimer(*evm->io_service(),
            "BFD RX", TaskScheduler::GetInstance()->GetTaskId("BFD"), 0)),
        currentConfig_(config),
        nextConfig_(config),
        sm_(CreateStateMachine(evm, this)),
        pollSequence_(false),
        communicator_(communicator),
        local_endpoint_(key.local_address, GetRandomLocalPort()),
        remote_endpoint_(key.remote_address, key.remote_port),
        stopped_(false) {
    ScheduleSendTimer();
    ScheduleRecvDeadlineTimer();
    sm_->SetCallback(boost::optional<ChangeCb>(
        boost::bind(&Session::CallStateChangeCallbacks, this, _1, _2)));
}

uint16_t Session::GetRandomLocalPort() const {
    boost::random::uniform_int_distribution<> dist(kSendPortMin, kSendPortMax);
    return dist(randomGen);
}

Session::~Session() {
    Stop();
}

bool Session::SendTimerExpired() {
    LOG(DEBUG, __func__);

    ControlPacket packet;
    PreparePacket(nextConfig_, &packet);
    SendPacket(&packet);

    // Workaround: Timer code isn't re-entrant
    this->sendTimer_->Reschedule(tx_interval().total_milliseconds());
    return true;
}

bool Session::RecvTimerExpired() {
    LOG(DEBUG, __func__);
    sm_->ProcessTimeout();

    return false;
}

std::string Session::toString() const {
    std::ostringstream out;
    out << "SessionKey: " << key_.to_string() << "\n";
    out << "LocalDiscriminator: 0x" << std::hex << localDiscriminator_ << "\n";
    out << "RemoteDiscriminator: 0x" << std::hex << remoteSession_.discriminator
        << "\n";
    out << "DesiredMinTxInterval: " << currentConfig_.desiredMinTxInterval
        << "\n";
    out << "RequiredMinRxInterval: " << currentConfig_.requiredMinRxInterval
        << "\n";
    out << "RemoteMinRxInterval: " << remoteSession_.minRxInterval << "\n";
    out << "RemoteMinTxInterval: " << remoteSession_.minTxInterval << "\n";
    out << "Local State:" << local_state() << "\n";
    out << "Remote State:" << remote_state().state << "\n";

    return out.str();
}

void Session::ScheduleSendTimer() {
    TimeInterval ti = tx_interval();
    LOG(DEBUG, __func__ << " " << ti);

    sendTimer_->Cancel();
    sendTimer_->Start(ti.total_milliseconds(),
                      boost::bind(&Session::SendTimerExpired, this));
}

void Session::ScheduleRecvDeadlineTimer() {
    TimeInterval ti = detection_time();
    LOG(DEBUG, __func__ << ti);

    recvTimer_->Cancel();
    recvTimer_->Start(ti.total_milliseconds(),
                      boost::bind(&Session::RecvTimerExpired, this));
}

BFDState Session::local_state_non_locking() const {
    return sm_->GetState();
}

BFDState Session::local_state() const {
    return local_state_non_locking();
}

//  If periodic BFD Control packets are already being sent (the remote
//  system is not in Demand mode), the Poll Sequence MUST be performed by
//  setting the Poll (P) bit on those scheduled periodic transmissions;
//  additional packets MUST NOT be sent.
void Session::InitPollSequence() {
    pollSequence_ = true;
    if (local_state_non_locking() != kUp &&
        local_state_non_locking() != kAdminDown) {
        ControlPacket packet;
        PreparePacket(nextConfig_, &packet);
        SendPacket(&packet);
    }
}

void Session::PreparePacket(const SessionConfig &config,
                            ControlPacket *packet) {

    packet->state = local_state_non_locking();
    packet->poll = pollSequence_;
    packet->sender_discriminator = localDiscriminator_;
    packet->receiver_discriminator = remoteSession_.discriminator;
    packet->detection_time_multiplier = config.detectionTimeMultiplier;
    packet->desired_min_tx_interval = config.desiredMinTxInterval;
    packet->required_min_rx_interval = config.requiredMinRxInterval;
}

ResultCode Session::ProcessControlPacket(const ControlPacket *packet) {
    remoteSession_.discriminator = packet->sender_discriminator;
    remoteSession_.detectionTimeMultiplier = packet->detection_time_multiplier;
    remoteSession_.state = packet->state;
    if ((local_state_non_locking() == kUp) && packet->poll) {
        if (packet->required_min_rx_interval < remoteSession_.minRxInterval) {
            remoteSession_.minRxInterval = packet->required_min_rx_interval;
            ScheduleSendTimer();
        }
        if (packet->desired_min_tx_interval > remoteSession_.minTxInterval) {
            remoteSession_.minTxInterval = packet->desired_min_tx_interval;
        }
    } else if (local_state_non_locking() == kInit) {
        remoteSession_.minRxInterval = packet->required_min_rx_interval;
        remoteSession_.minTxInterval = packet->desired_min_tx_interval;
        remoteSession_.detectionTimeMultiplier = packet->detection_time_multiplier;
    }

    sm_->ProcessRemoteState(packet->state);

    // poll sequence
    if (packet->poll) {
        ControlPacket newPacket;
        PreparePacket(nextConfig_, &newPacket);
        newPacket.poll = false;  // poll & final are forbidden in single packet
        newPacket.final = true;
        SendPacket(&newPacket);
    }
    if (packet->final) {
        pollSequence_ = false;
        currentConfig_ = nextConfig_;
    }

    if (local_state_non_locking() == kUp ||
        local_state_non_locking() == kInit) {
        ScheduleRecvDeadlineTimer();
    }

    return kResultCode_Ok;
}

void Session::SendPacket(const ControlPacket *packet) {
    LOG(DEBUG, __func__ << " session:" << toString());
    boost::asio::mutable_buffer buffer =
        boost::asio::mutable_buffer(new u_int8_t[kMinimalPacketLength],
                                    kMinimalPacketLength);
    int pktSize = EncodeControlPacket(packet,
        boost::asio::buffer_cast<uint8_t *>(buffer), kMinimalPacketLength);
    if (pktSize != kMinimalPacketLength) {
        LOG(ERROR, "Unable to encode packet");
    } else {
        communicator_->SendPacket(local_endpoint_, remote_endpoint_,
                                  key_.index, buffer, pktSize);
        stats_.tx_count++;
    }
}

TimeInterval Session::detection_time() {
    return std::max(currentConfig_.requiredMinRxInterval,
                    remoteSession_.minTxInterval) *
            remoteSession_.detectionTimeMultiplier;
}

TimeInterval Session::tx_interval() {
    TimeInterval minInterval, maxInterval;

    if (local_state_non_locking() == kUp) {
        TimeInterval negotiatedInterval =
                std::max(currentConfig_.desiredMinTxInterval,
                         remoteSession_.minRxInterval);

        minInterval = negotiatedInterval * 3/4;
        if (currentConfig_.detectionTimeMultiplier == 1)
            maxInterval = negotiatedInterval * 9/10;
        else
            maxInterval = negotiatedInterval;
    } else {
        minInterval = kIdleTxInterval * 3/4;
        maxInterval = kIdleTxInterval;
    }
    boost::random::uniform_int_distribution<>
            dist(minInterval.total_microseconds(),
                 maxInterval.total_microseconds());
    return boost::posix_time::microseconds(dist(randomGen));
}

const SessionKey &Session::key() const {
    return key_;
}

void Session::Stop() {
    if (stopped_ == false) {
        TimerManager::DeleteTimer(sendTimer_);
        TimerManager::DeleteTimer(recvTimer_);
        stopped_ = true;
        sm_->SetCallback(boost::optional<ChangeCb>());
    }
}

SessionConfig Session::config() const {
    return nextConfig_;
}

BFDRemoteSessionState Session::remote_state() const {
    return remoteSession_;
}

Discriminator Session::local_discriminator() const {
    return localDiscriminator_;
}

void Session::CallStateChangeCallbacks(
    const SessionKey &key, const BFD::BFDState &new_state) {
    for (Callbacks::const_iterator it = callbacks_.begin();
         it != callbacks_.end(); ++it) {
        it->second(key, new_state);
    }
}

void Session::RegisterChangeCallback(ClientId client_id, ChangeCb cb) {
    callbacks_[client_id] = cb;
}

void Session::UnregisterChangeCallback(ClientId client_id) {
    callbacks_.erase(client_id);
}

void Session::UpdateConfig(const SessionConfig& config) {
    nextConfig_.desiredMinTxInterval = config.desiredMinTxInterval;
    nextConfig_.requiredMinRxInterval = config.requiredMinRxInterval;
    nextConfig_.detectionTimeMultiplier = config.detectionTimeMultiplier;
    pollSequence_ = true;
}

int Session::reference_count() {
    return callbacks_.size();
}

bool Session::Up() const {
    return local_state() == kUp;
}

}  // namespace BFD

max_stars_repo_name: zhongyangni/controller

max_issues_repo_name: zhongyangni/controller

max_forks_repo_name: zhongyangni/controller


text:
// Copyright 2013 by Raytheon BBN Technologies
// All Rights Reserved.

#include <math.h>
#include <vector>
#include <wchar.h>
#include <boost/algorithm/string.hpp>
#include <boost/foreach.hpp>
#include <boost/regex.hpp>

#include "Generic/common/leak_detection.h"

#include "Urdu/sentences/ur_SentenceBreaker.h"
#include "Generic/common/limits.h"
#include "Generic/common/UnexpectedInputException.h"
#include "Generic/common/LocatedString.h"
#include "Generic/common/ParamReader.h"
#include "Generic/common/SessionLogger.h"
#include "Generic/common/InputUtil.h"
#include "Generic/tokens/Tokenizer.h"
#include "Generic/theories/Document.h"
#include "Generic/theories/Region.h"
#include "Generic/theories/Span.h"

class Metadata;
static wchar_t URDU_PERIOD = 0x06d4;
static wchar_t SPLIT_PUNCTUATION[] = {0x002e, 0x003f, 0x0021, 0x0025, 0x06d4, 0x061f}; //.?!% URDU PERIOD, BACKWARDS QUESTION MARK
static wchar_t CLOSING_PUNCTUATION[] = {0x0029, 0x005d, 0x007d, 0x0027, 0x0022, 0x06d4}; //)]}'" URDU PERIOD
static wchar_t SECONDARY_EOS_SYMBOL[] = {0x002d, 0x2026, 0x005f}; //- ELLIPSIS _
static wchar_t CARRIAGE_RETURN[] = {L"\n"};
UrduSentenceBreaker::UrduSentenceBreaker() {

	_breakLongSentences = true;
	_max_whitespace = ParamReader::getOptionalIntParamWithDefaultValue("en_sentence_breaker_max_whitespace", 100);
	std::string input_file = ParamReader::getParam("sentence_breaker_debug_file");
	DEBUG = (!input_file.empty());
	if (DEBUG) _debugStream.open(input_file.c_str());
}

UrduSentenceBreaker::~UrduSentenceBreaker() {
	if (DEBUG) _debugStream.close();
}

/**
 * Sets the current document pointer to point to the
 * document passed in and resets the current sentence
 * number to 0.
 */
void UrduSentenceBreaker::resetForNewDocument(const Document *doc) {
	_curDoc = doc;
	_curRegion = 0;
	_cur_sent_no = 0;
}

/**
 * Takes an array of pointers to LocatedStrings corresponding to the regions of
 * the document text and the number of regions. It puts an array of pointers to
 * Sentences where specified by results arg, and returns its size.
 *
 * Note that it is assumed that every region boundary is also a sentence
 * boundary, that is, no single sentence can span two regions.
 *
 * The caller is responsible for deleting the array of Sentences and all the
 * Sentence objects stored in it.
 *
 * @param results an output parameter containing an array of
 *                pointers to Sentences that will be filled in
 *                with the sentences found by the sentence breaker.
 * @param max_sentences the size of the results arrays, i.e., the maximum number
 *                      of sentences that can broken up and placed in the array.
 * @param text an array of pointers to text regions from the input file.
 * @param num_regions the size of the text array, i.e., the number of text regions.
 * @return the number of sentences found and placed in the results array.
 * @throws UnexpectedInputException if more than max_sentences sentences are found.
 */
int UrduSentenceBreaker::getSentencesRaw(Sentence **results,
									 int max_sentences,
									 const Region* const* regions,
									 int num_regions)
{
	if (num_regions < 1) {
		SessionLogger::warn("no_reg_0") << "No regions in document. Thus, no sentences.";
		return 0;
	}

	int text_index = 0;

	// Read the rest of the document.
	int last_headline_sent_no = max_sentences;  // Used to do intelligent dateline processing
	for (int i = 0; i < num_regions; i++) {
		_curRegion = regions[i];
		LocatedString *text = _new LocatedString(*_curRegion->getString());
		const int len = text->length();

		while (text_index < len) {
			if (_cur_sent_no == max_sentences) {
				throw UnexpectedInputException("UrduSentenceBreaker::getSentences()",
											   "Document contains more than max_sentences sentences");
			}
			if ((results[_cur_sent_no] = getNextSentence(&text_index, text)) != NULL) {
				if (_breakLongSentences) {
					attemptLongSentenceBreak(results, max_sentences, _curDoc->getMetadata());
					}
				if (_curRegion->getRegionTag() == Symbol(L"HEADLINE")) {
						last_headline_sent_no = _cur_sent_no;
					}
				_cur_sent_no++;
			}

		}
		text_index = 0;
		delete text;
	}

	return _cur_sent_no;
}

/**
 * The sentence is read starting from *offset.
 *
 * @param offset a value-result parameter; on entry to the function this parameter
 *               should refer to an integer representing the first offset of the
 *               next sentence, and on exit it will refer to an integer representing
 *               the next index into the located string after the end of the found
 *               sentence.
 * @param input the input string to read from.
 * @return a new Sentence object representing the next sentence found, or
 *         NULL if there were no more sentences.
 */
Sentence* UrduSentenceBreaker::getNextSentence(int *offset, const LocatedString *input) {
	const int len = input->length();

	// Start after any initial whitespace.
	int start = *offset;
	while ((start < len) && (iswspace(input->charAt(start)))) {
		start++;
	}

	// If we've reached the end, there's no sentence.
	if (start >= len) {
		*offset = start;
		return NULL;
	}

	Metadata *metadata = _curDoc->getMetadata();

	// Search for the next sentence break.
	int end = start;
	bool found_break = false;
	while ((end < len) && (!found_break)) {
		if (isSplitChar(input->charAt(end))) {
			//consume any repeated SplitChars
			while(isSplitChar(input->charAt(end))) {
				end++;
			}
			int next_word = end + 1;
			//find the next real word, or the end of the document
			while ((next_word < len) && !iswalpha(input->charAt(next_word))) {
				next_word++;
			}
			if (next_word >= len) {
				//catch quotation marks or other closing punct before the next word
				end = getIndexAfterClosingPunctuation(input, end);
				found_break = true;
			}
			else if (!Tokenizer::matchURL(input, end) && !matchEmail(input, end)) {
					found_break = true;
					end++;
			}

		} else if (matchFinalPeriod(input, end, start)) {
			// If the sentence ended with an ellipsis, skip to the end of it.
			int ellipsis_len = matchEllipsis(input, end);
			if (ellipsis_len > 0) {
				end += ellipsis_len - 1;
			}
			// If followed by any closing punctuation, include that too.
			end = getIndexAfterClosingPunctuation(input, end);
			found_break = true;
		} else if (isCarriageReturn(input->charAt(end))) {
			found_break = true;
			end++;
		} else {
			end += max(1, matchEllipsis(input, end));
			// Note that this moves  end forward, with or without an ellipsis
		}
	}
	// Trim off any whitespace at the end of the sentence
	*offset = end;
	while ((end > start) && (end > input->length() || iswspace(input->charAt(end - 1)))) {
		end--;
	}

	// Create the value of the sentence output parameter with the substring.
	LocatedString *sentenceString;
	if (end == start) {
		// Use the pre-trim sentence end
		sentenceString = input->substring(start, *offset);
	} else {
		// Use the trimmed sentence end
		sentenceString = input->substring(start, end);
	}

	Sentence *result = _new Sentence(_curDoc, _curRegion, _cur_sent_no, sentenceString);
	delete sentenceString;

	return result;
}

std::wstring UrduSentenceBreaker::_getNextToken(const LocatedString *input, int start, bool letters_only) {

	// Get the token immediately preceding this position. Tokens are defined by whitespace.
	// If letters_only is set to true, we also break on non-letters.
	std::wstring word = L"";
	while (start < input->length() && iswspace(input->charAt(start)))
		start++;
	while (start < input->length()) {
		wchar_t next_char = input->charAt(start);
		// strip basic punctuation
		if (iswspace(next_char))
			break;
		if (letters_only && !iswlower(next_char) && !iswupper(next_char))
			break;
		word += next_char;
		start++;
	}
	return word;

}

std::wstring UrduSentenceBreaker::_getPrevToken(const LocatedString *input, int start, bool letters_only) {

	// Get the token immediately preceding this position. Tokens are defined by whitespace.
	// If letters_only is set to true, we also break on non-letters.
	std::wstring word = L"";
	start--;
	while (start > 0 && iswspace(input->charAt(start)))
		start--;
	while (start > 0) {
		wchar_t prev_char = input->charAt(start);
		if (iswspace(prev_char))
			break;
		// strip basic punctuation
		if (letters_only && !iswlower(prev_char) && !iswupper(prev_char))
			break;
		word = prev_char + word;
		start--;
	}
	return word;
}

bool UrduSentenceBreaker::isSplitChar(const wchar_t c) const {
	return wcschr(SPLIT_PUNCTUATION, c) != NULL;
}

int UrduSentenceBreaker::getIndexAfterClosingPunctuation(const LocatedString *input, int index) {
	const int len = input->length();
	do {
		index++;
	} while ((index < len) && isClosingPunctuation(input->charAt(index)));
	return index;
}

bool UrduSentenceBreaker::isClosingPunctuation(const wchar_t c) const {
	return wcschr(CLOSING_PUNCTUATION, c) != NULL;
}

bool UrduSentenceBreaker::matchFinalPeriod(const LocatedString *input, int index, int origin) {
	// A sentence can't begin with a final period.
	if (index <= origin) {
		return false;
	}

	const int len = input->length();

	// If the period is the last character, it must be a final period.
	if ((index + 1) >= len) {
		return true;
	}

	// The char at the index must be a period.
	if (input->charAt(index) != URDU_PERIOD) {
		return false;
	}

	// The preceding char must be alphanumeric, closing punctuation, or
	// whitespace (in case the input text has already separated periods).
	wchar_t prev = input->charAt(index - 1);
	if (!iswalnum(prev) && !isClosingPunctuation(prev) && !iswspace(prev)) {
		return false;
	}

	// If it's an ellipsis, it could be final.
	if (matchEllipsis(input, index) > 0) {
		return true;
	}

	// If it's followed by anything else but whitespace or closing punctuation
	// followed in turn by whitespace, it can't be a final period.
	int pos = index + 1;
	while ((pos < input->length()) && isClosingPunctuation(input->charAt(pos))) {
		pos++;
	}
	// TODO: what if closing punctuation followed by end of line?
	if ((pos < len) && !iswspace(input->charAt(pos))) {
		return false;
	}

	// Otherwise, it's possible.
	return true;
}

int UrduSentenceBreaker::matchEllipsis(const LocatedString *input, int index) {
	const int len = input->length();

	// We have to be at a period to match.
	if ((index >= len) || (input->charAt(index) != URDU_PERIOD)) {
		return 0;
	}

	int matched_periods = 0;
	int end = index;
	int last_period = index;
	while (true) {
		// Skip any whitespace.
		while ((end < len) && iswspace(input->charAt(end))) {
			end++;
		}
		if ((end >= len) || (input->charAt(end) != URDU_PERIOD)) {
			break;
		}
		last_period = end;
		end++;
		matched_periods++;
	}

	// as in "larouse...@yahoo.com" -- we don't want to break here
	if (last_period + 1 < len && input->charAt(last_period + 1) == L'@')
		return false;

	return (matched_periods < 3) ? 0 : ((last_period + 1) - index);
}

void UrduSentenceBreaker::attemptLongSentenceBreak(Sentence **results, int max_sentences, Metadata *metadata) {
	Sentence *thisSentence = results[_cur_sent_no];
	int whitespace_count = countWhiteSpace(thisSentence->getString());
	if (whitespace_count <= _max_whitespace)
		return;

	std::vector<Sentence*> subSentences;
	int next_sent_no = thisSentence->getSentNumber() + static_cast<int>(subSentences.size());
	int sent_start = 0;
	for (int breakpoint = sent_start; breakpoint < thisSentence->getString()->length(); breakpoint++) {
		if (isSecondaryEOSSymbol(thisSentence->getString()->charAt(breakpoint))) {
			if (!DefaultSentenceBreaker::inRestrictedBreakSpan(metadata, thisSentence->getString(), breakpoint)) {
				subSentences.push_back(DefaultSentenceBreaker::createSubSentence(thisSentence, next_sent_no, sent_start, breakpoint-1, "hyphen"));
				sent_start = breakpoint;
				next_sent_no++;
			}
		}
	}
	subSentences.push_back(DefaultSentenceBreaker::createFinalSubSentence(thisSentence, next_sent_no, sent_start));

	// if no sub-sentences returned, keep this sentence as-is
	if (subSentences.empty())  {
		SessionLogger::warn("long_sentence") << "Could not break up long sentence, sentence number " << _cur_sent_no;
		return;
	}
	if (_cur_sent_no + static_cast<int>(subSentences.size()) > max_sentences) {
		throw UnexpectedInputException("UrduSentenceBreaker::attemptLongSentenceBreak()",
									   "Document contains more than max_sentences sentences");
	}

	BOOST_FOREACH(Sentence *sent, subSentences) {
		results[_cur_sent_no++] = sent;
	}

	// decrement sent_no to point to last complete sentence
	_cur_sent_no--;

	delete thisSentence;
}

bool UrduSentenceBreaker::isSecondaryEOSSymbol(const wchar_t c) const {
	return wcschr(SECONDARY_EOS_SYMBOL, c) != NULL;
}

bool UrduSentenceBreaker::isCarriageReturn(const wchar_t c) const {
  return wcschr(CARRIAGE_RETURN, c) != NULL;
}

bool UrduSentenceBreaker::matchEmail(const LocatedString *string, int index) {
  int prev_whitespace = 0;
  int i= 0;
  for (i = index; i>=0; i--) {
    if (iswspace(string->charAt(i))) {
      prev_whitespace = i;
      break;
    }
  }
  int at = string->indexOf(L"@", prev_whitespace);
  if (at > 0 && at < index)
    return true;
  return false;
}

max_stars_repo_name: BBN-E/serif

max_issues_repo_name: BBN-E/serif

max_forks_repo_name: BBN-E/serif


text:
/** \file usi_compression.cpp
 *
 *  Compression of NetCDF data based on an algorithm developped by Prof. Illia
 *  Horenko at Universita della Svizzera italiana (USI).
 *
 *  Reference:
 *  Horenko, Klein, Dolaptchiev, Schuette
 *  Automated Generation of Reduced Stochastic Weather Models I:
 *  simultaneous dimension and model reduction for time series analysis
 *
 *  \copyright Copyright (c) 2014,
 *             Universita della Svizzera italiana (USI) &
 *             Centro Svizzero di Calcolo Scientifico (CSCS).
 *             All rights reserved.
 *             This software may be modified and distributed under the terms of
 *             the BSD license. See the [LICENSE file](LICENSE.md) for details.
 *
 *  \author Will Sawyer (CSCS)
 *  \author Ben Cumming (CSCS)
 *  \author Manuel Schmid (CSCS)
 */

/** \mainpage USI Compression Algorithm
 *
 * \section readme Compiling and Running the Program
 * Information on how to compile the code (including requirements) and its
 * usage can be found in the [README](README.md) file.
 *
 * \section overview Code Overview
 * The code has two main parts: The class NetCDFInterface, which provides the
 * mapping between the NetCDF file and the matrix used for compression, and
 * the class CompressedMatrix, which is responsible for compressing and
 * reconstructing the data.
 *
 * The class NetCDFInterface provides two main functions:
 * NetCDFInterface::construct_matrix() and NetCDFInterface::write_matrix().
 * In the constructor, several internal data structures are set up to describe
 * how the multi-variable, multi-dimensional data from the NetCDF file will be
 * converted to a single 2D matrix. This information is then used by
 * construct_matrix() to create the matrix that can be used for compression.
 * The reconstructed matrix can the be written back to a NetCDF file with
 * write_matrix(), which ensures that each part of the matrix is written to
 * the correct variable using the same mapping as for reading the data. Some
 * other member functions provide information about this mapping that is used
 * e.g. for calculating statistics in print_statistics().
 *
 * The class CompressedMatrix runs the compression algorithm on the data. It
 * doesn't need any information on how the data is structured within the
 * matrix. The bulk of the work is done in the constructor, which sets up the
 * compressed representation of the matrix. It then provides a function
 * CompressedMatrix::reconstruct() that reconstructs the full matrix
 * from the compressed version, apart from compression losses. The Lanczos
 * algorithm used for finding the eigenvectors is implemented in two separate
 * files, lanczos_correlation_eigen.h and lanczos_correlation_minlin.h, using
 * Eigen or minlin as linear algebra backend.
 *
 * The function print_statistics() can compare the reconstructed data to the
 * original data and prints out statistics comparing the two. As these
 * statistics are calculated for each variable, it needs information on where
 * the data for each variable is located in the matrix.
 *
 * The main() function in usi_compression.cpp is responsible for tying all the
 * parts together. It parses all the command line options, constructs the
 * matrix, compresses and decompresses the data, optionally prints out
 * statistics comparing the two matrices and writes the reconstructed data
 * back to a NetCDF file.
 *
 * The other files are small helpers where matrices.h contains type
 * definitions for matrices and vectors, mpi_type_helper.h contains a little
 * helper for using MPI types in templated code, and usi_compression.cu just
 * includes usi_compression.cpp as NVCC doesn't want to compile .cpp files.
 *
 * \see main(), NetCDFInterface, CompressedMatrix, print_statistics(),
 *      matrices.h
 *
 * \section todo Issues and Future Development
 * Issues and plans for future development are collected on the
 * [to-do list](TODO.md).
 */

#include <iostream> // std::cout, std::endl
#include <string>   // std::string
#include <vector>   // std::vector
#include <boost/program_options.hpp>
#include <mpi.h>    // MPI_Init, MPI_Comm_size, MPI_Comm_rank, MPI_Allreduce, MPI_Finalize
#include "mpi_type_helper.h"
#include "matrices.h"
#include "NetCDFInterface.h"
#include "CompressedMatrix.h"
#include "matrix_statistics.h"

#define VERSION 0.1 ///< Version number of the program.
#define KSIZE 10 ///< Default number of clusters.
#define MSIZE 5  ///< Default number of eigenvectors used for final compression.

/**
 * \brief The floating point number type used for all computations. Use
 * 'float' or 'double' depending on hardware support.
 */
typedef double Scalar;

/**
 * The main function of the compression algorithm. It reads the command line
 * arguments, sets up MPI, reads the NetCDF file, compresses the data,
 * reconstructs the data, prints out statistics about the compression, writes
 * the data back to a NetCDF file and closes NetCDF and MPI connections.
 *
 * \see NetCDFInterface, CompressedMatrix, print_statistics
 *
 * \param[in] argc  Number of command line arguments.
 * \param[in] argv  Command line arguments.
 */
int main(int argc, char *argv[]) {

  //
  //  Initialize MPI
  //

  MPI_Init ( &argc, &argv );
  double time_at_start = MPI_Wtime();   // used for calculating running time of each part of the algorithm

  int my_rank, mpi_processes;
  MPI_Comm_rank( MPI_COMM_WORLD, &my_rank );
  MPI_Comm_size( MPI_COMM_WORLD, &mpi_processes );

  //
  // Input Argument Handling
  //

  int M_size, K_size;
  std::string filename;
  std::string filename_out;
  std::vector<std::string> variables;
  std::vector<std::string> compressed_dims;
  std::vector<std::string> distributed_dims;
  std::vector<std::string> indexed_dims;

  std::vector<std::string> default_compressed_dims;
  default_compressed_dims.push_back("lon");
  default_compressed_dims.push_back("lat");

  namespace po = boost::program_options;
  po::options_description po_description("USI Compression: Options");
  po_description.add_options()
    ("help", "display this help message")
    ("version", "display the version number")
    ("statistics,s", "print out statistics about the compression at the end")
    ("compressed,c", po::value< std::vector<std::string> >(&compressed_dims)
        ->default_value(default_compressed_dims, "lon,lat")->multitoken(),
        "list of compressed dimensions")
    ("indexed,i", po::value< std::vector<std::string> >(&indexed_dims)
        ->default_value(std::vector<std::string>(0), "none")->multitoken(),
        "list of indexed dimensions")
    ("clusters,K", po::value<int>(&K_size)->default_value(KSIZE),
        "the number of clusters used for PCA (K)")
    ("eigenvectors,M", po::value<int>(&M_size)->default_value(MSIZE),
        "the number of eigenvectors used for final compression (M)")
    ("horizontal-stacking,h", "stack variables in distributed (horizontal) instead of compressed (vertical) direction")
    ("file", po::value<std::string>(&filename)->required(),
        "the path to the NetCDF4 file")
    ("output-file,o", po::value<std::string>(&filename_out)
        ->default_value(std::string(), "FILE_reconstructed.nc4"),
        "the path to the NetCDF4 output file")
    ("append,a", "append reconstructed variables to output file instead of overwriting it")
    ("variables,v", po::value< std::vector<std::string> >(&variables)->required()->multitoken(),
        "the variable that is to be compressed")
    ;

  po::positional_options_description po_positional;
  po_positional.add("file",1);
  po::variables_map po_vm;
  po::store(po::command_line_parser(argc, argv).options(po_description)
      .positional(po_positional).run(), po_vm);

  bool append = false;
  if (po_vm.count("append")) {
    append = true;
  }

  if (po_vm.count("help")) {
    if (!my_rank) std::cout << po_description << std::endl;
    return 0;
  }

  if (po_vm.count("version")) {
    if (!my_rank) std::cout << "USI Compression, Version " << VERSION << std::endl;
    return 0;
  }

  NetCDFInterface<Scalar>::Stacking stacking;
  if (po_vm.count("horizontal-stacking")) {
    stacking = NetCDFInterface<Scalar>::HORIZONTAL;
  } else {
    stacking = NetCDFInterface<Scalar>::VERTICAL;
  }

  // this has to be after the help/version commands as this
  // exits with an error if the required arguments aren't
  // specified
  po::notify(po_vm);

  //
  // Read NetCDF Data
  //

  NetCDFInterface<Scalar> netcdf_interface(filename, variables,
      compressed_dims, indexed_dims, stacking);
  DeviceMatrix<Scalar> X = netcdf_interface.construct_matrix();

  double time_after_reading_data = MPI_Wtime();

  //
  // Compress Matrix
  //

  std::vector<int> col_ids = netcdf_interface.get_column_ids();
  CompressedMatrix<Scalar> X_compressed(X, K_size, M_size, col_ids);

  double time_after_compression = MPI_Wtime();

  //
  // Reconstruct Matrix
  //

  DeviceMatrix<Scalar> X_reconstructed = X_compressed.reconstruct();

  //
  // Statistics Output
  //

  if (po_vm.count("statistics")) {

    // Title & upper border
    if (!my_rank) std::cout << std::endl << std::setfill('-') << std::setw(80) << '-' << std::endl;
    if (!my_rank) std::cout << " Statistics" << std::endl;
    if (!my_rank) std::cout << std::setw(80) << '-' << std::setfill(' ') << std::endl << std::endl;

    // Timing information
    double time_for_solve = time_after_compression -  time_after_reading_data;
    double time_for_input = time_after_reading_data - time_at_start;
    double max_time_for_solve, max_time_for_input;
    MPI_Allreduce( &time_for_solve, &max_time_for_solve, 1, mpi_type_helper<Scalar>::value, MPI_MAX, MPI_COMM_WORLD );
    MPI_Allreduce( &time_for_input, &max_time_for_input, 1, mpi_type_helper<Scalar>::value, MPI_MAX, MPI_COMM_WORLD );
    if (!my_rank) std::cout << " Maximum time for input: " << max_time_for_input << std::endl;
    if (!my_rank) std::cout << " Maximum time for solve: " << max_time_for_solve << std::endl;
    if (!my_rank) std::cout << std::endl;

    // Compression ratio
    if (!my_rank) std::cout << " Compression ratio: " <<
        X_compressed.compressed_size / (double) X_compressed.original_size
        << std::endl << std::endl;

    // Matrix statistics
    std::vector<int> row_start, row_count, col_start, col_count;
    std::vector<Scalar> variable_mean, variable_max;
    netcdf_interface.get_variable_ranges(row_start, row_count, col_start, col_count);
    netcdf_interface.get_variable_transformation(variable_mean, variable_max);
    print_statistics(X, X_reconstructed, variables, variable_mean,
        variable_max, row_start, row_count, col_start, col_count);

    // Lower border
    if (!my_rank) std::cout << std::setfill('-') << std::setw(80) << '-' <<
        std::setfill(' ') << std::endl << std::endl;
  }

  //
  // Write Reconstructed Data to File
  //

  netcdf_interface.write_matrix(X_reconstructed, filename_out, append);

  //
  //  Terminate MPI and Quit Program.
  //

  netcdf_interface.close();
  MPI_Finalize();
  return 0;
}

max_stars_repo_name: eth-cscs/compression

max_issues_repo_name: eth-cscs/compression

max_forks_repo_name: eth-cscs/compression


text:
#pragma once

#include <vban/lib/numbers.hpp>
#include <vban/node/election.hpp>
#include <vban/node/voting.hpp>
#include <vban/secure/common.hpp>

#include <boost/circular_buffer.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/optional.hpp>
#include <boost/thread/thread.hpp>

#include <atomic>
#include <condition_variable>
#include <deque>
#include <memory>
#include <unordered_map>
#include <unordered_set>

namespace mi = boost::multi_index;

namespace vban
{
class node;
class block;
class block_sideband;
class election;
class election_scheduler;
class vote;
class transaction;
class confirmation_height_processor;
class stat;

class cementable_account final
{
public:
	cementable_account (vban::account const & account_a, size_t blocks_uncemented_a);
	vban::account account;
	uint64_t blocks_uncemented{ 0 };
};

class election_timepoint final
{
public:
	std::chrono::steady_clock::time_point time;
	vban::qualified_root root;
};

class inactive_cache_status final
{
public:
	bool bootstrap_started{ false };
	bool election_started{ false }; // Did item reach config threshold to start an impromptu election?
	bool confirmed{ false }; // Did item reach votes quorum? (minimum config value)
	vban::uint256_t tally{ 0 }; // Last votes tally for block

	bool operator!= (inactive_cache_status const other) const
	{
		return bootstrap_started != other.bootstrap_started || election_started != other.election_started || confirmed != other.confirmed || tally != other.tally;
	}
};

class inactive_cache_information final
{
public:
	inactive_cache_information () = default;
	inactive_cache_information (std::chrono::steady_clock::time_point arrival, vban::block_hash hash, vban::account initial_rep_a, uint64_t initial_timestamp_a, vban::inactive_cache_status status) :
		arrival (arrival),
		hash (hash),
		status (status)
	{
		voters.reserve (8);
		voters.emplace_back (initial_rep_a, initial_timestamp_a);
	}

	std::chrono::steady_clock::time_point arrival;
	vban::block_hash hash;
	vban::inactive_cache_status status;
	std::vector<std::pair<vban::account, uint64_t>> voters;
	bool needs_eval () const
	{
		return !status.bootstrap_started || !status.election_started || !status.confirmed;
	}
};

class expired_optimistic_election_info final
{
public:
	expired_optimistic_election_info (std::chrono::steady_clock::time_point, vban::account);

	std::chrono::steady_clock::time_point expired_time;
	vban::account account;
	bool election_started{ false };
};

class frontiers_confirmation_info
{
public:
	bool can_start_elections () const;

	size_t max_elections{ 0 };
	bool aggressive_mode{ false };
};

class election_insertion_result final
{
public:
	std::shared_ptr<vban::election> election;
	bool inserted{ false };
};

// Core class for determining consensus
// Holds all active blocks i.e. recently added blocks that need confirmation
class active_transactions final
{
	class conflict_info final
	{
	public:
		vban::qualified_root root;
		std::shared_ptr<vban::election> election;
		vban::epoch epoch;
		vban::uint256_t previous_balance;
	};

	friend class vban::election;

	// clang-format off
	class tag_account {};
	class tag_random_access {};
	class tag_root {};
	class tag_sequence {};
	class tag_uncemented {};
	class tag_arrival {};
	class tag_hash {};
	class tag_expired_time {};
	class tag_election_started {};
	// clang-format on

public:
	// clang-format off
	using ordered_roots = boost::multi_index_container<conflict_info,
	mi::indexed_by<
		mi::random_access<mi::tag<tag_random_access>>,
		mi::hashed_unique<mi::tag<tag_root>,
			mi::member<conflict_info, vban::qualified_root, &conflict_info::root>>>>;
	// clang-format on
	ordered_roots roots;
	using roots_iterator = active_transactions::ordered_roots::index_iterator<tag_root>::type;

	explicit active_transactions (vban::node &, vban::confirmation_height_processor &);
	~active_transactions ();
	// Distinguishes replay votes, cannot be determined if the block is not in any election
	vban::vote_code vote (std::shared_ptr<vban::vote> const &);
	// Is the root of this block in the roots container
	bool active (vban::block const &);
	bool active (vban::qualified_root const &);
	std::shared_ptr<vban::election> election (vban::qualified_root const &) const;
	std::shared_ptr<vban::block> winner (vban::block_hash const &) const;
	// Returns false if the election was restarted
	void restart (vban::transaction const &, std::shared_ptr<vban::block> const &);
	// Returns a list of elections sorted by difficulty
	std::vector<std::shared_ptr<vban::election>> list_active (size_t = std::numeric_limits<size_t>::max ());
	void erase (vban::block const &);
	void erase_hash (vban::block_hash const &);
	void erase_oldest ();
	bool empty ();
	size_t size ();
	void stop ();
	bool publish (std::shared_ptr<vban::block> const &);
	boost::optional<vban::election_status_type> confirm_block (vban::transaction const &, std::shared_ptr<vban::block> const &);
	void block_cemented_callback (std::shared_ptr<vban::block> const &);
	void block_already_cemented_callback (vban::block_hash const &);

	int64_t vacancy () const;
	std::function<void ()> vacancy_update{ [] () {} };

	std::unordered_map<vban::block_hash, std::shared_ptr<vban::election>> blocks;
	std::deque<vban::election_status> list_recently_cemented ();
	std::deque<vban::election_status> recently_cemented;

	void add_recently_cemented (vban::election_status const &);
	void add_recently_confirmed (vban::qualified_root const &, vban::block_hash const &);
	void erase_recently_confirmed (vban::block_hash const &);
	void add_inactive_votes_cache (vban::unique_lock<vban::mutex> &, vban::block_hash const &, vban::account const &, uint64_t const);
	// Inserts an election if conditions are met
	void trigger_inactive_votes_cache_election (std::shared_ptr<vban::block> const &);
	vban::inactive_cache_information find_inactive_votes_cache (vban::block_hash const &);
	void erase_inactive_votes_cache (vban::block_hash const &);
	vban::election_scheduler & scheduler;
	vban::confirmation_height_processor & confirmation_height_processor;
	vban::node & node;
	mutable vban::mutex mutex{ mutex_identifier (mutexes::active) };
	size_t priority_cementable_frontiers_size ();
	size_t priority_wallet_cementable_frontiers_size ();
	size_t inactive_votes_cache_size ();
	size_t election_winner_details_size ();
	void add_election_winner_details (vban::block_hash const &, std::shared_ptr<vban::election> const &);
	void remove_election_winner_details (vban::block_hash const &);

	vban::vote_generator generator;
	vban::vote_generator final_generator;

#ifdef MEMORY_POOL_DISABLED
	using allocator = std::allocator<vban::inactive_cache_information>;
#else
	using allocator = boost::fast_pool_allocator<vban::inactive_cache_information>;
#endif

	// clang-format off
	using ordered_cache = boost::multi_index_container<vban::inactive_cache_information,
	mi::indexed_by<
		mi::ordered_non_unique<mi::tag<tag_arrival>,
			mi::member<vban::inactive_cache_information, std::chrono::steady_clock::time_point, &vban::inactive_cache_information::arrival>>,
		mi::hashed_unique<mi::tag<tag_hash>,
			mi::member<vban::inactive_cache_information, vban::block_hash, &vban::inactive_cache_information::hash>>>, allocator>;
	// clang-format on

private:
	vban::mutex election_winner_details_mutex{ mutex_identifier (mutexes::election_winner_details) };

	std::unordered_map<vban::block_hash, std::shared_ptr<vban::election>> election_winner_details;

	// Call action with confirmed block, may be different than what we started with
	// clang-format off
	vban::election_insertion_result insert_impl (vban::unique_lock<vban::mutex> &, std::shared_ptr<vban::block> const&, boost::optional<vban::uint256_t> const & = boost::none, vban::election_behavior = vban::election_behavior::normal, std::function<void(std::shared_ptr<vban::block>const&)> const & = nullptr);
	// clang-format on
	void request_loop ();
	void request_confirm (vban::unique_lock<vban::mutex> &);
	void erase (vban::qualified_root const &);
	// Erase all blocks from active and, if not confirmed, clear digests from network filters
	void cleanup_election (vban::unique_lock<vban::mutex> & lock_a, vban::election const &);
	// Returns a list of elections sorted by difficulty, mutex must be locked
	std::vector<std::shared_ptr<vban::election>> list_active_impl (size_t) const;

	vban::condition_variable condition;
	bool started{ false };
	std::atomic<bool> stopped{ false };

	// Maximum time an election can be kept active if it is extending the container
	std::chrono::seconds const election_time_to_live;

	static size_t constexpr recently_confirmed_size{ 65536 };
	using recent_confirmation = std::pair<vban::qualified_root, vban::block_hash>;
	// clang-format off
	boost::multi_index_container<recent_confirmation,
	mi::indexed_by<
		mi::sequenced<mi::tag<tag_sequence>>,
		mi::hashed_unique<mi::tag<tag_root>,
			mi::member<recent_confirmation, vban::qualified_root, &recent_confirmation::first>>,
		mi::hashed_unique<mi::tag<tag_hash>,
			mi::member<recent_confirmation, vban::block_hash, &recent_confirmation::second>>>>
	recently_confirmed;
	using prioritize_num_uncemented = boost::multi_index_container<vban::cementable_account,
	mi::indexed_by<
		mi::hashed_unique<mi::tag<tag_account>,
			mi::member<vban::cementable_account, vban::account, &vban::cementable_account::account>>,
		mi::ordered_non_unique<mi::tag<tag_uncemented>,
			mi::member<vban::cementable_account, uint64_t, &vban::cementable_account::blocks_uncemented>,
			std::greater<uint64_t>>>>;

	boost::multi_index_container<vban::expired_optimistic_election_info,
	mi::indexed_by<
		mi::ordered_non_unique<mi::tag<tag_expired_time>,
			mi::member<expired_optimistic_election_info, std::chrono::steady_clock::time_point, &expired_optimistic_election_info::expired_time>>,
		mi::hashed_unique<mi::tag<tag_account>,
			mi::member<expired_optimistic_election_info, vban::account, &expired_optimistic_election_info::account>>,
		mi::ordered_non_unique<mi::tag<tag_election_started>,
			mi::member<expired_optimistic_election_info, bool, &expired_optimistic_election_info::election_started>, std::greater<bool>>>>
	expired_optimistic_election_infos;
	// clang-format on
	std::atomic<uint64_t> expired_optimistic_election_infos_size{ 0 };

	// Frontiers confirmation
	vban::frontiers_confirmation_info get_frontiers_confirmation_info ();
	void confirm_prioritized_frontiers (vban::transaction const &, uint64_t, uint64_t &);
	void confirm_expired_frontiers_pessimistically (vban::transaction const &, uint64_t, uint64_t &);
	void frontiers_confirmation (vban::unique_lock<vban::mutex> &);
	bool insert_election_from_frontiers_confirmation (std::shared_ptr<vban::block> const &, vban::account const &, vban::uint256_t, vban::election_behavior);
	vban::account next_frontier_account{ 0 };
	std::chrono::steady_clock::time_point next_frontier_check{ std::chrono::steady_clock::now () };
	constexpr static size_t max_active_elections_frontier_insertion{ 1000 };
	prioritize_num_uncemented priority_wallet_cementable_frontiers;
	prioritize_num_uncemented priority_cementable_frontiers;
	std::unordered_set<vban::wallet_id> wallet_ids_already_iterated;
	std::unordered_map<vban::wallet_id, vban::account> next_wallet_id_accounts;
	bool skip_wallets{ false };
	std::atomic<unsigned> optimistic_elections_count{ 0 };
	void prioritize_frontiers_for_confirmation (vban::transaction const &, std::chrono::milliseconds, std::chrono::milliseconds);
	bool prioritize_account_for_confirmation (prioritize_num_uncemented &, size_t &, vban::account const &, vban::account_info const &, uint64_t);
	unsigned max_optimistic ();
	void set_next_frontier_check (bool);
	void add_expired_optimistic_election (vban::election const &);
	bool should_do_frontiers_confirmation () const;
	static size_t constexpr max_priority_cementable_frontiers{ 100000 };
	static size_t constexpr confirmed_frontiers_max_pending_size{ 10000 };
	static std::chrono::minutes constexpr expired_optimistic_election_info_cutoff{ 30 };
	ordered_cache inactive_votes_cache;
	vban::inactive_cache_status inactive_votes_bootstrap_check (vban::unique_lock<vban::mutex> &, std::vector<std::pair<vban::account, uint64_t>> const &, vban::block_hash const &, vban::inactive_cache_status const &);
	vban::inactive_cache_status inactive_votes_bootstrap_check (vban::unique_lock<vban::mutex> &, vban::account const &, vban::block_hash const &, vban::inactive_cache_status const &);
	vban::inactive_cache_status inactive_votes_bootstrap_check_impl (vban::unique_lock<vban::mutex> &, vban::uint256_t const &, size_t, vban::block_hash const &, vban::inactive_cache_status const &);
	vban::inactive_cache_information find_inactive_votes_cache_impl (vban::block_hash const &);
	boost::thread thread;

	friend class election;
	friend class election_scheduler;
	friend std::unique_ptr<container_info_component> collect_container_info (active_transactions &, const std::string &);

	friend class active_transactions_vote_replays_Test;
	friend class frontiers_confirmation_prioritize_frontiers_Test;
	friend class frontiers_confirmation_prioritize_frontiers_max_optimistic_elections_Test;
	friend class confirmation_height_prioritize_frontiers_overwrite_Test;
	friend class active_transactions_confirmation_consistency_Test;
	friend class node_deferred_dependent_elections_Test;
	friend class active_transactions_pessimistic_elections_Test;
	friend class frontiers_confirmation_expired_optimistic_elections_removal_Test;
};

bool purge_singleton_inactive_votes_cache_pool_memory ();
std::unique_ptr<container_info_component> collect_container_info (active_transactions & active_transactions, std::string const & name);
}

ext: hpp

max_stars_repo_name: EduardAl/vban-node

max_issues_repo_name: EduardAl/vban-node

max_forks_repo_name: EduardAl/vban-node


text:
/*
 * @file  utilities.cc
 * @brief define some misc utilities used by cache layer
 *
 * @date   Nov 22, 2014
 * @author elenav
 */

#include <sstream>
#include <boost/filesystem.hpp>
#include "dfs_cache/utilities.hpp"

namespace impala
{
namespace utilities{

std::vector<std::string> &split(const std::string &original, char delimiter, std::vector<std::string> &elements){
    std::stringstream ss(original);
    std::string item;
    while (std::getline(ss, item, delimiter)) {
    	elements.push_back(item);
    }
    return elements;
}

std::vector<std::string> split(const std::string &original, char delimiter) {
    std::vector<std::string> elems;
    split(original, delimiter, elems);
    return elems;
}

bool endsWith (const std::string& original, const std::string& ending)
{
    if (original.length() >= ending.length()) {
        return (0 == original.compare (original.length() - ending.length(), ending.length(), ending));
    } else {
        return false;
    }
}

time_t posix_time_to_time_t(boost::posix_time::ptime time)
{
    using namespace boost::posix_time;
    ptime epoch(boost::gregorian::date(1970, 1, 1));
    time_duration::sec_type time_duartion_in_sec = (time - epoch).total_seconds();
    return time_t(time_duartion_in_sec);
}

boost::uintmax_t get_free_space_on_disk(const std::string& path){
	boost::system::error_code ec;
	boost::filesystem::space_info  space_info = boost::filesystem::space(path,  ec);
	if(!ec)
		return space_info.available;
	return 0;
}

boost::uintmax_t get_dir_busy_space(const std::string& path){
	boost::uintmax_t size = 0;
	for(boost::filesystem::recursive_directory_iterator it(path); it != boost::filesystem::recursive_directory_iterator(); ++it){
		if(!boost::filesystem::is_directory(*it))
			size += boost::filesystem::file_size(*it);
	}
	return size;
}
}
}

max_stars_repo_name: ImpalaToGo/ImpalaToGo

max_issues_repo_name: ImpalaToGo/ImpalaToGo

max_forks_repo_name: ImpalaToGo/ImpalaToGo


text:
// Copyright 2020 Evgengrmit evgengrmit@icloud.com

#ifndef INCLUDE_DECLARATIONS_HPP_
#define INCLUDE_DECLARATIONS_HPP_

#include <boost/log/expressions.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup.hpp>

const char kEndChar[] = "0000";
const size_t kEndCharSize = 4;
void init();
#endif  // INCLUDE_DECLARATIONS_HPP_