text:
//
//  data.hpp
//
//  Created by Anton Leuski on 7/31/15.
//  Copyright (c) 2015 Anton Leuski & ICT/USC. All rights reserved.
//
//  This file is part of Jerome.
//
//  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.
//

#ifndef __jerome_ir_training_Data_hpp__
#define __jerome_ir_training_Data_hpp__

#include <random>

#include <boost/range/algorithm_ext/push_back.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/algorithm/find_if.hpp>

#include <jerome/types.hpp>
#include <jerome/type/algorithm.hpp>

namespace jerome {
  namespace ir {
    namespace training {

      // random generator function:
      inline ptrdiff_t myrandom(ptrdiff_t i)
      {
        return rand() % i;
      }

      template <class Q, class A, class L>
      struct DataStorage
      {
        typedef Q question_type;
        typedef A answer_type;
        typedef L link_type;

        typedef List<question_type> questions_type;
        typedef List<answer_type> answers_type;
        typedef List<link_type>   links_type;

        questions_type  mQuestions;
        answers_type  mAnswers;
        links_type  mLinks;

        DataStorage() = default;

        template <class QR, class AR, class LR>
        DataStorage(const QR& qr, const AR& ar, const LR& lr)
          : mQuestions(std::begin(qr), std::end(qr))
          , mAnswers(std::begin(ar), std::end(ar))
          , mLinks(std::begin(lr), std::end(lr))
        {
        }

      };

      template <class Q, class A, class L>
      struct Data
        : public ReferenceClassInterface<DataStorage<Q, A, L>>
      {
        typedef DataStorage<Q, A, L> storage_type;

        typedef typename storage_type::question_type question_type;
        typedef typename storage_type::answer_type answer_type;
        typedef typename storage_type::link_type link_type;

        typedef ReferenceClassInterface<storage_type> parent_type;
        typedef Data<question_type, answer_type, link_type>   this_type;

        typedef typename storage_type::questions_type questions_type;
        typedef typename storage_type::answers_type answers_type;
        typedef typename storage_type::links_type links_type;

        template <class QR, class AR, class LR>
        Data(const QR& qr, const AR& ar, const LR& lr)
          : parent_type(std::make_shared<storage_type>(qr, ar, lr))
        {
        }

        Data()
          : parent_type(std::make_shared<storage_type>())
        {
        }

        using parent_type::parent_type;

        questions_type&     questions()
        {
          return this->implementation().mQuestions;
        }

        answers_type&     answers()
        {
          return this->implementation().mAnswers;
        }

        links_type&       links()
        {
          return this->implementation().mLinks;
        }

        const questions_type& questions() const
        {
          return this->implementation().mQuestions;
        }

        const answers_type&   answers() const
        {
          return this->implementation().mAnswers;
        }

        const links_type&   links()   const
        {
          return this->implementation().mLinks;
        }

      private:
        typedef Set<question_type>  question_set_type;

      public:
        typedef double split_size_type;

        template <class Range>
        this_type
        subdata(const Range& inQuestionRange) const
        {

          question_set_type questions_set(std::begin(inQuestionRange), std::end(
              inQuestionRange));
          auto check_question =
            make_object_contained_in_set<question_set_type, question_type>(
              questions_set);

          this_type newData;

          boost::range::push_back(newData.answers(), answers());
          boost::range::push_back(newData.questions(), inQuestionRange);
          boost::range::push_back(newData.links(), links()
            | boost::adaptors::filtered((std::function<bool(const link_type&)> )(
                [&check_question] (const link_type& l)
          {
            return l.utterances().end() !=
            boost::find_if(l.utterances(), check_question);
          })));
          return newData;
        }

        this_type
        dataByRemovingUnlinkedQuestions() const
        {
          question_set_type linked_questions_set; // all questions that have
                                                  // links
          question_set_type all_questions_set(std::begin(questions()), std::end(
              questions()));
          for (const link_type& l : links()) {
            boost::range::push_back(linked_questions_set, l.utterances()
              | boost::adaptors::filtered(make_object_contained_in_set(
                  all_questions_set)));
          }

          this_type newData;
          boost::range::push_back(newData.answers(), answers());
          boost::range::push_back(newData.questions(), linked_questions_set);
          boost::range::push_back(newData.links(), links());
          return newData;
        }

        // put a proportion of the questions into the first data set and the
        // rest into the second set
        // if the proportion is weird (< 0 or >= |Q|) then duplicate the set
        //

        template <class RNG = std::default_random_engine>
        std::pair<this_type, this_type>
        split(split_size_type inProportion, RNG&& inRandomNumberGenerator = RNG())
        const
        {
          auto p = jerome::split<std::vector<question_type>>(questions(),
                                        inProportion, inRandomNumberGenerator);
          return std::pair<this_type, this_type>(subdata(p.first),
                                                 subdata(p.second));
        }

      };

    }
  }
}
#endif // defined __jerome_ir_training_Data_hpp__

max_stars_repo_name: leuski-ict/jerome

max_issues_repo_name: leuski-ict/jerome

max_forks_repo_name: leuski-ict/jerome


text:
//  Copyright (c) 2007-2016 Hartmut Kaiser
//
//  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)

#ifndef HPX_RUNTIME_THREADS_EXECUTORS_THREAD_POOL_OS_EXECUTORS_HPP
#define HPX_RUNTIME_THREADS_EXECUTORS_THREAD_POOL_OS_EXECUTORS_HPP

#include <hpx/config.hpp>
#include <hpx/runtime/threads/detail/thread_pool.hpp>
#include <hpx/runtime/threads/policies/callback_notifier.hpp>
#include <hpx/runtime/threads/thread_enums.hpp>
#include <hpx/runtime/threads/thread_executor.hpp>
#include <hpx/util/steady_clock.hpp>
#include <hpx/util/thread_description.hpp>
#include <hpx/util/unique_function.hpp>

#include <boost/atomic.hpp>
#include <boost/thread/mutex.hpp>

#include <chrono>
#include <cstddef>
#include <cstdint>
#include <string>

#include <hpx/config/warnings_prefix.hpp>

namespace hpx { namespace threads { namespace executors
{
    namespace detail
    {
        //////////////////////////////////////////////////////////////////////
        template <typename Scheduler>
        class HPX_EXPORT thread_pool_os_executor
          : public threads::detail::scheduled_executor_base
        {
        public:
            thread_pool_os_executor(std::size_t num_threads,
                std::string const& affinity_desc = "");
            ~thread_pool_os_executor();

            // Schedule the specified function for execution in this executor.
            // Depending on the subclass implementation, this may block in some
            // situations.
            void add(closure_type && f,
                util::thread_description const& description,
                threads::thread_state_enum initial_state, bool run_now,
                threads::thread_stacksize stacksize, error_code& ec);

            // Schedule given function for execution in this executor no sooner
            // than time abs_time. This call never blocks, and may violate
            // bounds on the executor's queue size.
            void add_at(
                util::steady_clock::time_point const& abs_time,
                closure_type && f, util::thread_description const& description,
                threads::thread_stacksize stacksize, error_code& ec);

            // Schedule given function for execution in this executor no sooner
            // than time rel_time from now. This call never blocks, and may
            // violate bounds on the executor's queue size.
            void add_after(
                util::steady_clock::duration const& rel_time,
                closure_type && f, util::thread_description const& description,
                threads::thread_stacksize stacksize, error_code& ec);

            // Return an estimate of the number of waiting tasks.
            std::uint64_t num_pending_closures(error_code& ec) const;

            // Reset internal (round robin) thread distribution scheme
            void reset_thread_distribution();

            /// Return the mask for processing units the given thread is allowed
            /// to run on.
            mask_cref_type get_pu_mask(topology const& topology,
                std::size_t num_thread) const
            {
                return pool_.get_pu_mask(topology, num_thread);
            }

            /// Set the new scheduler mode
            void set_scheduler_mode(threads::policies::scheduler_mode mode)
            {
                pool_.set_scheduler_mode(mode);
            }

        protected:
            // Return the requested policy element
            std::size_t get_policy_element(
                threads::detail::executor_parameter p, error_code& ec) const;

            static threads::thread_result_type thread_function_nullary(
                closure_type func);

        private:
            // the scheduler used by this executor
            Scheduler scheduler_;
            std::string executor_name_;
            threads::policies::callback_notifier notifier_;
            threads::detail::thread_pool<Scheduler> pool_;

            std::size_t num_threads_;

            static boost::atomic<std::size_t> os_executor_count_;
            static std::string get_unique_name();

            // protect scheduler initialization
            typedef boost::mutex mutex_type;
            mutable mutex_type mtx_;
        };
    }

    ///////////////////////////////////////////////////////////////////////////
#if defined(HPX_HAVE_LOCAL_SCHEDULER)
    struct HPX_EXPORT local_queue_os_executor
      : public scheduled_executor
    {
        local_queue_os_executor();

        explicit local_queue_os_executor(std::size_t num_threads,
            std::string const& affinity_desc = "");
    };
#endif

#if defined(HPX_HAVE_STATIC_SCHEDULER)
    struct HPX_EXPORT static_queue_os_executor
      : public scheduled_executor
    {
        static_queue_os_executor();

        explicit static_queue_os_executor(std::size_t num_threads,
            std::string const& affinity_desc = "");
    };
#endif

    struct HPX_EXPORT local_priority_queue_os_executor
      : public scheduled_executor
    {
        local_priority_queue_os_executor();

        explicit local_priority_queue_os_executor(std::size_t num_threads,
            std::string const& affinity_desc = "");
    };

#if defined(HPX_HAVE_STATIC_PRIORITY_SCHEDULER)
    struct HPX_EXPORT static_priority_queue_os_executor
      : public scheduled_executor
    {
        static_priority_queue_os_executor();

        explicit static_priority_queue_os_executor(std::size_t num_threads,
            std::string const& affinity_desc = "");
    };
#endif
}}}

#include <hpx/config/warnings_suffix.hpp>

#endif /*HPX_RUNTIME_THREADS_EXECUTORS_THREAD_POOL_OS_EXECUTORS_HPP*/

ext: hpp

max_stars_repo_name: brycelelbach/hpx

max_issues_repo_name: brycelelbach/hpx

max_forks_repo_name: brycelelbach/hpx


text:
#ifndef TESTT2SWAPANCESTORISAPOPTOTICWRITER_HPP
#define TESTT2SWAPANCESTORISAPOPTOTICWRITER_HPP

#include <cxxtest/TestSuite.h>

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

#include "ArchiveOpener.hpp"
#include "AbstractCellBasedTestSuite.hpp"
#include "FileComparison.hpp"
#include "DifferentiatedCellProliferativeType.hpp"

// Cell population writers
#include "T2SwapAncestorIsApoptoticWriter.hpp"

// Files to create populations
#include "HoneycombVertexMeshGenerator.hpp"
#include "HoneycombMeshGenerator.hpp"
#include "PottsMeshGenerator.hpp"
#include "CellsGenerator.hpp"
#include "FixedG1GenerationalCellCycleModel.hpp"

#include "MeshBasedCellPopulation.hpp"
#include "CaBasedCellPopulation.hpp"
#include "NodeBasedCellPopulation.hpp"
#include "PottsBasedCellPopulation.hpp"
#include "VertexBasedCellPopulation.hpp"

#include "CustomVertexBasedCellPopulation.hpp"
#include "CustomT2SwapCellKiller.hpp"

#include "PetscSetupAndFinalize.hpp"

class TestT2SwapAncestorIsApoptoticWriter : public AbstractCellBasedTestSuite
{
public:

    void TestT2SwapAncestorIsApoptoticWriter1()
    {
        EXIT_IF_PARALLEL;

        // Create a simple 2D VertexBasedCellPopulation
        HoneycombVertexMeshGenerator generator(4, 6);
        MutableVertexMesh<2,2>* p_mesh = generator.GetMesh();
        std::vector<CellPtr> cells;
        boost::shared_ptr<AbstractCellProperty> p_diff_type(CellPropertyRegistry::Instance()->Get<DifferentiatedCellProliferativeType>());
        CellsGenerator<FixedG1GenerationalCellCycleModel, 2> cells_generator;
        cells_generator.GenerateBasic(cells, p_mesh->GetNumElements(), std::vector<unsigned>(), p_diff_type);
        CustomVertexBasedCellPopulation<2> cell_population(*p_mesh, cells);

        // Create an output directory for the writer
        std::string output_directory = "TestT2SwapAncestorIsApoptoticWriter";
        OutputFileHandler output_file_handler(output_directory, false);
        std::string results_dir = output_file_handler.GetOutputDirectoryFullPath();

        // Create a T2SwapAncestorIsApoptoticWriter and test that the correct output is generated
        T2SwapAncestorIsApoptoticWriter<2,2> t2_swaps_writer;
        t2_swaps_writer.OpenOutputFile(output_file_handler);
        t2_swaps_writer.WriteTimeStamp();
        t2_swaps_writer.Visit(&cell_population);
        t2_swaps_writer.WriteNewline();
        t2_swaps_writer.CloseFile();

        FileComparison(results_dir + "T2AncestorsIsApoptotics.dat", "cell_based/test/data/TestCellPopulationWriters/T2SwapLocations.dat").CompareFiles();

        // Test that we can append to files
        t2_swaps_writer.OpenOutputFileForAppend(output_file_handler);
        t2_swaps_writer.WriteTimeStamp();
        t2_swaps_writer.Visit(&cell_population);
        t2_swaps_writer.WriteNewline();
        t2_swaps_writer.CloseFile();

        FileComparison(results_dir + "T2AncestorsIsApoptotics.dat", "cell_based/test/data/TestCellPopulationWriters/T2SwapLocations_twice.dat").CompareFiles();

        {
            // Coverage of the Visit() method when called on a MeshBasedCellPopulation
            HoneycombMeshGenerator tet_generator(5, 5, 0);
            MutableMesh<2,2>* p_tet_mesh = tet_generator.GetMesh();
            std::vector<CellPtr> mesh_based_cells;
            CellsGenerator<FixedG1GenerationalCellCycleModel, 2> mesh_based_cells_generator;
            mesh_based_cells_generator.GenerateBasic(mesh_based_cells, p_tet_mesh->GetNumNodes());
            MeshBasedCellPopulation<2> mesh_based_cell_population(*p_tet_mesh, mesh_based_cells);

            TS_ASSERT_THROWS_NOTHING(t2_swaps_writer.Visit(&mesh_based_cell_population));
        }

        {
            // Coverage of the Visit() method when called on a CaBasedCellPopulation
            PottsMeshGenerator<2> ca_based_generator(5, 0, 0, 5, 0, 0);
            PottsMesh<2>* p_ca_based_mesh = ca_based_generator.GetMesh();
            std::vector<CellPtr> ca_based_cells;
            CellsGenerator<FixedG1GenerationalCellCycleModel, 2> ca_based_cells_generator;
            ca_based_cells_generator.GenerateBasic(ca_based_cells, 5);
            std::vector<unsigned> location_indices;
            location_indices.push_back(7);
            location_indices.push_back(11);
            location_indices.push_back(12);
            location_indices.push_back(13);
            location_indices.push_back(17);
            CaBasedCellPopulation<2> ca_based_cell_population(*p_ca_based_mesh, ca_based_cells, location_indices);

            TS_ASSERT_THROWS_NOTHING(t2_swaps_writer.Visit(&ca_based_cell_population));
        }

        {
            // Coverage of the Visit() method when called on a NodeBasedCellPopulation
            std::vector<Node<2>* > node_based_nodes;
            node_based_nodes.push_back(new Node<2>(0, false, 0.0, 0.0));
            node_based_nodes.push_back(new Node<2>(1, false, 1.0, 1.0));
            NodesOnlyMesh<2> node_based_mesh;
            node_based_mesh.ConstructNodesWithoutMesh(node_based_nodes, 1.5);
            std::vector<CellPtr> node_based_cells;
            CellsGenerator<FixedG1GenerationalCellCycleModel, 2> node_based_generator;
            node_based_generator.GenerateBasic(node_based_cells, node_based_mesh.GetNumNodes());
            NodeBasedCellPopulation<2> node_based_cell_population(node_based_mesh, node_based_cells);

            TS_ASSERT_THROWS_NOTHING(t2_swaps_writer.Visit(&node_based_cell_population));

            // Tidy up
            delete node_based_nodes[0];
            delete node_based_nodes[1];
        }

        {
            // Coverage of the Visit() method when called on a PottsBasedCellPopulation
            PottsMeshGenerator<2> potts_based_generator(4, 1, 2, 4, 1, 2);
            PottsMesh<2>* p_potts_based_mesh = potts_based_generator.GetMesh();
            std::vector<CellPtr> potts_based_cells;
            CellsGenerator<FixedG1GenerationalCellCycleModel, 2> potts_based_cells_generator;
            potts_based_cells_generator.GenerateBasic(potts_based_cells, p_potts_based_mesh->GetNumElements());
            PottsBasedCellPopulation<2> potts_based_cell_population(*p_potts_based_mesh, potts_based_cells);

            TS_ASSERT_THROWS_NOTHING(t2_swaps_writer.Visit(&potts_based_cell_population));
        }

        {
            // Coverage of the Visit() method when called on a VertexBasedCellPopulation
            HoneycombVertexMeshGenerator vertex_generator(4, 6);
            MutableVertexMesh<2,2>* p_vertex_mesh = vertex_generator.GetMesh();
            std::vector<CellPtr> vertex_based_cells;
            CellsGenerator<FixedG1GenerationalCellCycleModel, 2> vertex_based_cells_generator;
            vertex_based_cells_generator.GenerateBasic(vertex_based_cells,
                    p_vertex_mesh->GetNumElements());
            VertexBasedCellPopulation<2>
                vertex_based_cell_population(*p_vertex_mesh, vertex_based_cells);

            TS_ASSERT_THROWS_NOTHING(t2_swaps_writer.Visit(&vertex_based_cell_population));
        }
    }

    void TestT2SwapAncestorIsApoptoticWriter2()
    {
        EXIT_IF_PARALLEL;

        // Make 6 nodes to assign to four elements
        std::vector<Node<2>*> nodes;
        nodes.push_back(new Node<2>(0, false, 0.0, 0.0));
        nodes.push_back(new Node<2>(1, false, 1.0, 0.0));
        nodes.push_back(new Node<2>(2, false, 0.5, 0.5));
        nodes.push_back(new Node<2>(3, false, 0.1, 0.05));
        nodes.push_back(new Node<2>(4, false, 0.9, 0.05));
        nodes.push_back(new Node<2>(5, false, 0.5, 0.475));

        // Make one triangular and three trapezium elements out of these nodes
        std::vector<Node<2>*> nodes_elem_0, nodes_elem_1, nodes_elem_2, nodes_elem_3;
        unsigned node_indices_elem_0[3] = {3, 4, 5};
        unsigned node_indices_elem_1[4] = {1, 2, 5, 4};
        unsigned node_indices_elem_2[4] = {2, 0, 3, 5};
        unsigned node_indices_elem_3[4] = {0, 1, 4, 3};
        for (unsigned i=0; i<4; i++)
        {
            if (i < 3)
            {
                nodes_elem_0.push_back(nodes[node_indices_elem_0[i]]);
            }
            nodes_elem_1.push_back(nodes[node_indices_elem_1[i]]);
            nodes_elem_2.push_back(nodes[node_indices_elem_2[i]]);
            nodes_elem_3.push_back(nodes[node_indices_elem_3[i]]);
        }

        std::vector<VertexElement<2,2>*> vertex_elements;
        vertex_elements.push_back(new VertexElement<2,2>(0, nodes_elem_0));
        vertex_elements.push_back(new VertexElement<2,2>(1, nodes_elem_1));
        vertex_elements.push_back(new VertexElement<2,2>(2, nodes_elem_2));
        vertex_elements.push_back(new VertexElement<2,2>(3, nodes_elem_3));

        // Make a vertex mesh
        MutableVertexMesh<2,2> vertex_mesh(nodes, vertex_elements);

        vertex_mesh.SetT2Threshold(0.01);

        TS_ASSERT_EQUALS(vertex_mesh.GetNumElements(), 4u);
        TS_ASSERT_EQUALS(vertex_mesh.GetNumNodes(), 6u);

        // Get a cell population
        CellsGenerator<FixedG1GenerationalCellCycleModel, 2> cells_generator;
        std::vector<CellPtr> cells;
        cells_generator.GenerateBasic(cells, vertex_mesh.GetNumElements(), std::vector<unsigned>());
        CustomVertexBasedCellPopulation<2> cell_population(vertex_mesh, cells);

        // Set cell ancestor to location indices
        cell_population.SetCellAncestorsToLocationIndices();

        // Call apoptosis on T2 cell
        auto cell_iter = cell_population.Begin();
        cell_iter->StartApoptosis(false);

        // The population should have 4 cells
        TS_ASSERT_EQUALS(cell_population.GetNumRealCells(), 4u)

        // Give the Population to the cell killer
        CustomT2SwapCellKiller<2> cell_killer(&cell_population);

        // We move the inner vertices a bit inwards
        c_vector<double, 2>& new_location_0 = vertex_elements[0]->GetNode(0)->rGetModifiableLocation();
        new_location_0(0) = 0.499;
        new_location_0(1) = 0.249;

        c_vector<double, 2>& new_location_1 = vertex_elements[0]->GetNode(1)->rGetModifiableLocation();
        new_location_1(0) = 0.501;
        new_location_1(1) = 0.249;

        c_vector<double, 2>& new_location_2 = vertex_elements[0]->GetNode(2)->rGetModifiableLocation();
        new_location_2(0) = 0.5;
        new_location_2(1) = 0.251;

        // Perform swaps
        cell_killer.CheckAndLabelCellsForApoptosisOrDeath();

        // Create an output directory for the writer
        std::string output_directory = "TestT2SwapAncestorIsApoptoticWriter";
        OutputFileHandler output_file_handler(output_directory, false);
        std::string results_dir = output_file_handler.GetOutputDirectoryFullPath();

        // Create test files
        out_stream test_file_1 =
            output_file_handler.OpenOutputFile("test_ancestors_1.dat");
        *test_file_1 << "0\t1\t0\tapoptosis\t\n";
        test_file_1->close();

        out_stream test_file_2 =
            output_file_handler.OpenOutputFile("test_ancestors_2.dat");
        *test_file_2 << "0\t1\t0\tapoptosis\t\n0\t0\t\n";
        test_file_2->close();

        // Create a T2SwapAncestorIsApoptoticWriter and test that the correct output is generated
        T2SwapAncestorIsApoptoticWriter<2,2> t2_swaps_writer;
        t2_swaps_writer.OpenOutputFile(output_file_handler);
        t2_swaps_writer.WriteTimeStamp();
        t2_swaps_writer.Visit(&cell_population);
        t2_swaps_writer.WriteNewline();
        t2_swaps_writer.CloseFile();

        FileComparison(results_dir + "T2AncestorsIsApoptotics.dat", results_dir
                + "test_ancestors_1.dat").CompareFiles();

        // Test that we can append to files
        t2_swaps_writer.OpenOutputFileForAppend(output_file_handler);
        t2_swaps_writer.WriteTimeStamp();
        t2_swaps_writer.Visit(&cell_population);
        t2_swaps_writer.WriteNewline();
        t2_swaps_writer.CloseFile();

        FileComparison(results_dir + "T2AncestorsIsApoptotics.dat", results_dir
                + "test_ancestors_2.dat").CompareFiles();
    }

    void TestT2SwapAncestorIsApoptoticWriter3()
    {
        EXIT_IF_PARALLEL;

        /**
         * Create a mesh comprising ten nodes contained in six elements, two of which are small triangles,
         * as shown below. We will test that the CheckAndLabelCellsForApoptosisOrDeath() method works correctly in the case where multiple
         * T2 swaps are required. After remeshing, the two triangular elements should be removed from the mesh.
         *
         *   ________          _________
         *  |\      /|        |\       /|
         *  | |\__/| | -----> | \_____/ |
         *  | |/  \| |        | /     \ |
         *  |/______\|        |/_______\|
         *
         */
        std::vector<Node<2>*> nodes;
        // Create the nodes
        // The boolean is true for boundary nodes
        nodes.push_back(new Node<2>(0, true,  0.0, 0.0));
        nodes.push_back(new Node<2>(1, true,  1.0, 0.0));
        nodes.push_back(new Node<2>(2, true,  1.0, 1.0));
        nodes.push_back(new Node<2>(3, true,  0.0, 1.0));
        nodes.push_back(new Node<2>(4, false, 0.3, 0.45));
        nodes.push_back(new Node<2>(5, false, 0.4, 0.5));
        nodes.push_back(new Node<2>(6, false, 0.3, 0.55));
        nodes.push_back(new Node<2>(7, false, 0.6, 0.5));
        nodes.push_back(new Node<2>(8, false, 0.7, 0.45));
        nodes.push_back(new Node<2>(9, false, 0.7, 0.55));

        // Construct elements from the nodes
        std::vector<Node<2>*> nodes_elem_0, nodes_elem_1, nodes_elem_2, nodes_elem_3, nodes_elem_4, nodes_elem_5;
        unsigned node_indices_elem_0[6] = {0, 1, 8, 7, 5, 4};
        unsigned node_indices_elem_1[6] = {2, 3, 6, 5, 7, 9};
        unsigned node_indices_elem_2[4] = {1, 2, 9, 8};
        unsigned node_indices_elem_3[4] = {0, 4, 6, 3};
        unsigned node_indices_elem_4[3] = {4, 5, 6};
        unsigned node_indices_elem_5[3] = {7, 8, 9};

        for (unsigned i=0; i<6; i++)
        {
            nodes_elem_0.push_back(nodes[node_indices_elem_0[i]]);
            nodes_elem_1.push_back(nodes[node_indices_elem_1[i]]);
            if (i < 4)
            {
                nodes_elem_2.push_back(nodes[node_indices_elem_2[i]]);
                nodes_elem_3.push_back(nodes[node_indices_elem_3[i]]);
            }
            if (i < 3)
            {
                nodes_elem_4.push_back(nodes[node_indices_elem_4[i]]);
                nodes_elem_5.push_back(nodes[node_indices_elem_5[i]]);
            }
        }

        // create a mesh
        std::vector<VertexElement<2,2>*> vertex_elements;
        vertex_elements.push_back(new VertexElement<2,2>(0, nodes_elem_0));
        vertex_elements.push_back(new VertexElement<2,2>(1, nodes_elem_1));
        vertex_elements.push_back(new VertexElement<2,2>(2, nodes_elem_2));
        vertex_elements.push_back(new VertexElement<2,2>(3, nodes_elem_3));
        vertex_elements.push_back(new VertexElement<2,2>(4, nodes_elem_4));
        vertex_elements.push_back(new VertexElement<2,2>(5, nodes_elem_5));

        MutableVertexMesh<2,2> vertex_mesh(nodes, vertex_elements);

        // Set the remeshing threshold parameter values so that T2 swaps do occur, but T1 swaps do not
        vertex_mesh.SetT2Threshold(0.01);
        vertex_mesh.SetCellRearrangementThreshold(0.00001);

        // Test that the numbers of nodes and elements are correct
        TS_ASSERT_EQUALS(vertex_mesh.GetNumElements(), 6u);
        TS_ASSERT_EQUALS(vertex_mesh.GetNumNodes(), 10u);

        // Get a cell population
        CellsGenerator<FixedG1GenerationalCellCycleModel, 2> cells_generator;
        std::vector<CellPtr> cells;
        cells_generator.GenerateBasic(cells, vertex_mesh.GetNumElements(), std::vector<unsigned>());
        CustomVertexBasedCellPopulation<2> cell_population(vertex_mesh, cells);

        // Set cell ancestor to location indices
        cell_population.SetCellAncestorsToLocationIndices();

        // Call apoptosis on first cell to do T2 swap (index 4)
        auto cell_iter = cell_population.Begin();
        for (int i = 0; i < 4; i++)
        {
            ++cell_iter;
        }
        cell_iter->StartApoptosis();

        // the population should have 6 cells
        TS_ASSERT_EQUALS(cell_population.GetNumRealCells(), 6u)

        // Give the Population to the cell killer
        CustomT2SwapCellKiller<2> cell_killer(&cell_population);
        // Perform swaps
        cell_killer.CheckAndLabelCellsForApoptosisOrDeath();

        // Create an output directory for the writer
        std::string output_directory = "TestT2SwapAncestorIsApoptoticWriter";
        OutputFileHandler output_file_handler(output_directory, false);
        std::string results_dir = output_file_handler.GetOutputDirectoryFullPath();

        // Create test files
        out_stream test_file_3 =
            output_file_handler.OpenOutputFile("test_ancestors_3.dat");
        *test_file_3 << "0\t2\t4\tapoptosis\t5\textrusion\t\n";
        test_file_3->close();

        out_stream test_file_4 =
            output_file_handler.OpenOutputFile("test_ancestors_4.dat");
        *test_file_4 << "0\t2\t4\tapoptosis\t5\textrusion\t\n0\t0\t\n";
        test_file_4->close();

        // Create a T2SwapAncestorIsApoptoticWriter and test that the correct output is generated
        T2SwapAncestorIsApoptoticWriter<2,2> t2_swaps_writer;
        t2_swaps_writer.OpenOutputFile(output_file_handler);
        t2_swaps_writer.WriteTimeStamp();
        t2_swaps_writer.Visit(&cell_population);
        t2_swaps_writer.WriteNewline();
        t2_swaps_writer.CloseFile();

        FileComparison(results_dir + "T2AncestorsIsApoptotics.dat", results_dir
                + "test_ancestors_3.dat").CompareFiles();

        // Test that we can append to files
        t2_swaps_writer.OpenOutputFileForAppend(output_file_handler);
        t2_swaps_writer.WriteTimeStamp();
        t2_swaps_writer.Visit(&cell_population);
        t2_swaps_writer.WriteNewline();
        t2_swaps_writer.CloseFile();

        FileComparison(results_dir + "T2AncestorsIsApoptotics.dat", results_dir
                + "test_ancestors_4.dat").CompareFiles();
    }

    void TestT2SwapAncestorIsApoptoticWriterArchiving()
    {
        // The purpose of this test is to check that archiving can be done for this class
        OutputFileHandler handler("archive", false);
        std::string archive_filename = handler.GetOutputDirectoryFullPath() + "T2SwapAncestorIsApoptoticWriter.arch";

        {
            AbstractCellBasedWriter<2,2>* const p_cell_writer = new T2SwapAncestorIsApoptoticWriter<2,2>();

            std::ofstream ofs(archive_filename.c_str());
            boost::archive::text_oarchive output_arch(ofs);
            output_arch << p_cell_writer;
            delete p_cell_writer;
        }
        PetscTools::Barrier(); //Processes read after last process has (over-)written archive
        {
            AbstractCellBasedWriter<2,2>* p_cell_writer_2;
            std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive input_arch(ifs);
            input_arch >> p_cell_writer_2;
            delete p_cell_writer_2;
       }
    }
};

#endif // TESTT2SWAPANCESTORISAPOPTOTICWRITER_HPP

max_stars_repo_name: ThomasPak/cell-competition

max_issues_repo_name: ThomasPak/cell-competition

max_forks_repo_name: ThomasPak/cell-competition


text:
//  Copyright (c) 2015 Thomas Heller
//  Copyright (c) 2015 Hartmut Kaiser
//  Copyright (c) 2017 Zahra Khatami
//
//  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)

#include <hpx/hpx_init.hpp>
#include <hpx/hpx.hpp>
#include <hpx/version.hpp>
#include <hpx/include/parallel_algorithm.hpp>
#include <hpx/include/parallel_executors.hpp>
#include <hpx/include/parallel_transform.hpp>
#include <hpx/include/parallel_executor_parameters.hpp>
#include <hpx/include/iostreams.hpp>
#include <hpx/include/threads.hpp>

#include <hpx/parallel/util/numa_allocator.hpp>

//new classes for implementing machine learning techniques
#include <hpx/parallel/seq_or_par.hpp>
#include <hpx/parallel/chunk_size_determination.hpp>
#include <hpx/parallel/prefetching_distance_determination.hpp>
#include <hpx/parallel/executors/dynamic_chunk_size.hpp>
#include <hpx/parallel/executors/adaptive_chunk_size.hpp>

#include <boost/format.hpp>
#include <boost/range/functions.hpp>

#include <string>
#include <vector>

#ifndef STREAM_TYPE
#define STREAM_TYPE double
#endif

///////////////////////////////////////////////////////////////////////////////
hpx::threads::topology& retrieve_topology()
{
    static hpx::threads::topology& topo = hpx::threads::create_topology();
    return topo;
}

///////////////////////////////////////////////////////////////////////////////
double mysecond()
{
    return hpx::util::high_resolution_clock::now() * 1e-9;
}

int checktick()
{
    static const std::size_t M = 20;
    int minDelta, Delta;
    double t1, t2, timesfound[M];

    // Collect a sequence of M unique time values from the system.
    for (std::size_t i = 0; i < M; i++) {
        t1 = mysecond();
        while( ((t2=mysecond()) - t1) < 1.0E-6 )
            ;
        timesfound[i] = t1 = t2;
    }

    // Determine the minimum difference between these M values.
    // This result will be our estimate (in microseconds) for the
    // clock granularity.
    minDelta = 1000000;
    for (std::size_t i = 1; i < M; i++) {
        Delta = (int)( 1.0E6 * (timesfound[i]-timesfound[i-1]));
        minDelta = (std::min)(minDelta, (std::max)(Delta,0));
    }

    return(minDelta);
}

template <typename Vector>
void check_results(std::size_t iterations,
    Vector const & a, Vector const & b, Vector const & c)
{
    STREAM_TYPE aj,bj,cj,scalar;
    STREAM_TYPE aSumErr,bSumErr,cSumErr;
    STREAM_TYPE aAvgErr,bAvgErr,cAvgErr;
    double epsilon;
    int ierr,err;

    // reproduce initialization
    aj = 1.0;
    bj = 2.0;
    cj = 0.0;
    // a[] is modified during timing check
    aj = 2.0E0 * aj;
    // now execute timing loop
    scalar = 3.0;
    for (std::size_t k=0; k<iterations; k++)
        {
            cj = aj;
            bj = scalar*cj;
            cj = aj+bj;
            aj = bj+scalar*cj;
        }

    // accumulate deltas between observed and expected results
    aSumErr = 0.0;
    bSumErr = 0.0;
    cSumErr = 0.0;
    for (std::size_t j=0; j<a.size(); j++) {
        aSumErr += std::abs(a[j] - aj);
        bSumErr += std::abs(b[j] - bj);
        cSumErr += std::abs(c[j] - cj);
        // if (j == 417) printf("Index 417: c[j]: %f, cj: %f\n",c[j],cj);   // MCCALPIN
    }
    aAvgErr = aSumErr / (STREAM_TYPE) a.size();
    bAvgErr = bSumErr / (STREAM_TYPE) a.size();
    cAvgErr = cSumErr / (STREAM_TYPE) a.size();

    if (sizeof(STREAM_TYPE) == 4) {
        epsilon = 1.e-6;
    }
    else if (sizeof(STREAM_TYPE) == 8) {
        epsilon = 1.e-13;
    }
    else {
        printf("WEIRD: sizeof(STREAM_TYPE) = %zu\n", sizeof(STREAM_TYPE));
        epsilon = 1.e-6;
    }

    err = 0;

    if (std::abs(aAvgErr/aj) > epsilon) {
        err++;
        printf ("Failed Validation on array a[], AvgRelAbsErr > epsilon (%e)\n",
            epsilon);
        printf ("     Expected Value: %e, AvgAbsErr: %e, AvgRelAbsErr: %e\n",
            aj,aAvgErr,std::abs(aAvgErr)/aj);
        ierr = 0;
        for (std::size_t j=0; j<a.size(); j++) {
            if (std::abs(a[j]/aj-1.0) > epsilon) {
                ierr++;
#ifdef VERBOSE
                if (ierr < 10) {
                    printf("         array a: index: %ld, expected: %e, "
                        "observed: %e, relative error: %e\n",
                        j,aj,a[j],std::abs((aj-a[j])/aAvgErr));
                }
#endif
            }
        }
        printf("     For array a[], %d errors were found.\n",ierr);
    }

    if (std::abs(bAvgErr/bj) > epsilon) {
        err++;
        printf ("Failed Validation on array b[], AvgRelAbsErr > epsilon (%e)\n",
            epsilon);
        printf ("     Expected Value: %e, AvgAbsErr: %e, AvgRelAbsErr: %e\n",
            bj,bAvgErr,std::abs(bAvgErr)/bj);
        printf ("     AvgRelAbsErr > Epsilon (%e)\n",epsilon);
        ierr = 0;
        for (std::size_t j=0; j<a.size(); j++) {
            if (std::abs(b[j]/bj-1.0) > epsilon) {
                ierr++;
#ifdef VERBOSE
                if (ierr < 10) {
                    printf("         array b: index: %ld, expected: %e, "
                        "observed: %e, relative error: %e\n",
                        j,bj,b[j],std::abs((bj-b[j])/bAvgErr));
                }
#endif
            }
        }
        printf("     For array b[], %d errors were found.\n",ierr);
    }
    if (std::abs(cAvgErr/cj) > epsilon) {
        err++;
        printf ("Failed Validation on array c[], AvgRelAbsErr > epsilon (%e)\n",
            epsilon);
        printf ("     Expected Value: %e, AvgAbsErr: %e, AvgRelAbsErr: %e\n",
            cj,cAvgErr,std::abs(cAvgErr)/cj);
        printf ("     AvgRelAbsErr > Epsilon (%e)\n",epsilon);
        ierr = 0;
        for (std::size_t j=0; j<a.size(); j++) {
            if (std::abs(c[j]/cj-1.0) > epsilon) {
                ierr++;
#ifdef VERBOSE
                if (ierr < 10) {
                    printf("         array c: index: %ld, expected: %e, "
                        "observed: %e, relative error: %e\n",
                        j,cj,c[j],std::abs((cj-c[j])/cAvgErr));
                }
#endif
            }
        }
        printf("     For array c[], %d errors were found.\n",ierr);
    }

    if (err == 0) {
        printf ("Solution Validates: avg error less than %e on all three arrays\n",
            epsilon);
    }
#ifdef VERBOSE
    printf ("Results Validation Verbose Results: \n");
    printf ("    Expected a(1), b(1), c(1): %f %f %f \n",aj,bj,cj);
    printf ("    Observed a(1), b(1), c(1): %f %f %f \n",a[1],b[1],c[1]);
    printf ("    Rel Errors on a, b, c:     %e %e %e \n",std::abs(aAvgErr/aj),
        std::abs(bAvgErr/bj),std::abs(cAvgErr/cj));
#endif
}

template <typename Vector, typename Policy>
std::vector<std::vector<double> >
numa_domain_worker(std::size_t domain,
    Policy policy,
    hpx::lcos::local::latch& l, int vector_size,
    std::size_t part_size, std::size_t offset, std::size_t iterations, std::size_t& prefetch_distance_factor,
    Vector& a, Vector& b, Vector& c)
{
    typedef typename Vector::iterator iterator;
    iterator a_begin = a.begin() + offset;
    iterator b_begin = b.begin() + offset;
    iterator c_begin = c.begin() + offset;

    iterator a_end = a_begin + part_size;
    iterator b_end = b_begin + part_size;
    iterator c_end = c_begin + part_size;

    // Initialize arrays
    hpx::parallel::fill(policy, a_begin, a_end, 1.0);
    hpx::parallel::fill(policy, b_begin, b_end, 2.0);
    hpx::parallel::fill(policy, c_begin, c_end, 0.0);

    double t = mysecond();
    hpx::parallel::for_each(policy, a_begin, a_end,
        [&policy](STREAM_TYPE & v)
        {
            v = 2.0 * v;
        }
    );

    t = 1.0E6 * (mysecond() - t);

    if (domain == 0)
    {
        // Get initial value for system clock.
        int quantum = checktick();
        if(quantum >= 1)
        {
            std::cout
                << "Your clock granularity/precision appears to be " << quantum
                << " microseconds.\n"
                ;
        }
        else
        {
            std::cout
                << "Your clock granularity appears to be less than one microsecond.\n"
                ;
            quantum = 1;
        }

        std::cout
            << "Each test below will take on the order"
            << " of " << (int) t << " microseconds.\n"
            << "   (= " << (int) (t/quantum) << " clock ticks)\n"
            << "Increase the size of the arrays if this shows that\n"
            << "you are not getting at least 20 clock ticks per test.\n"
            << "-------------------------------------------------------------\n"
            ;

        std::cout
            << "WARNING -- The above is only a rough guideline.\n"
            << "For best results, please be sure you know the\n"
            << "precision of your system timer.\n"
            << "-------------------------------------------------------------\n"
            ;
    }

    // synchronize across NUMA domains
    l.count_down_and_wait();

    ///////////////////////////////////////////////////////////////////////////

    // Main Loop
    std::vector<std::vector<double> > timing(3, std::vector<double>(iterations));

    /// parameters needed for comparing different for_each styles
    //minimum chunk_size is chosen with : cashe_size_line / sizeof(type)
    std::size_t minimum_chunk_size = 64 / sizeof(STREAM_TYPE);
    //There is prefetch_distance_factor number of minimum_chunk_sizes within each chunk
    std::size_t chunk_size = prefetch_distance_factor * minimum_chunk_size;
    //number of chunk_size in data
    int chunk_count = vector_size / chunk_size;
    //This range is used for Triad_for_each
    auto original_range=boost::irange(0,vector_size);

    double scalar = 3.0;

    auto f = [&](std::size_t i) {
        c[i] = a[i];
        b[i] = scalar * c[i];
        c[i] = a[i] + b[i];
        a[i] = b[i] + c[i] * 2.5;
    };

    for(std::size_t iteration = 0; iteration != iterations; ++iteration)
    {
        //All together
        //original
        timing[0][iteration] = mysecond();
        hpx::parallel::for_each(hpx::parallel::par, original_range.begin(), original_range.end(), f);
        timing[0][iteration] = mysecond() - timing[0][iteration];

        //chunk
        timing[1][iteration] = mysecond();
	    hpx::parallel::for_each(hpx::parallel::par.with(hpx::parallel::adaptive_chunk_size()), original_range.begin(), original_range.end(), f);
        timing[1][iteration] = mysecond() - timing[1][iteration];

        //prefetch
        timing[2][iteration] = mysecond();
	    hpx::parallel::for_each(hpx::parallel::execution::make_prefetcher_policy(policy, prefetch_distance_factor, a, b, c),
         original_range.begin(), original_range.end(), f);
        timing[2][iteration] = mysecond() - timing[2][iteration];
    }

    return timing;
}

///////////////////////////////////////////////////////////////////////////////
std::size_t get_num_numa_nodes(hpx::threads::topology const& topo,
    boost::program_options::variables_map& vm)
{
    std::size_t numa_nodes = topo.get_number_of_numa_nodes();
    if (numa_nodes == 0)
        numa_nodes = topo.get_number_of_sockets();

    std::string num_numa_domains_str = vm["stream-numa-domains"].as<std::string>();
    if (num_numa_domains_str != "all")
    {
        numa_nodes = hpx::util::safe_lexical_cast<std::size_t>(num_numa_domains_str);
    }
    return numa_nodes;
}

std::pair<std::size_t, std::size_t> get_num_numa_pus(
    hpx::threads::topology const& topo, std::size_t numa_nodes,
    boost::program_options::variables_map& vm)
{
    std::size_t numa_pus = hpx::threads::hardware_concurrency() / numa_nodes;

    std::string num_threads_str = vm["stream-threads"].as<std::string>();
    std::size_t pus = numa_pus;

    if(num_threads_str != "all")
    {
        pus = hpx::util::safe_lexical_cast<std::size_t>(num_threads_str);
    }

    return std::make_pair(numa_pus, pus);
}

///////////////////////////////////////////////////////////////////////////////
int hpx_main(boost::program_options::variables_map& vm)
{
    // extract hardware topology
    hpx::threads::topology const& topo = retrieve_topology();

    std::size_t numa_nodes = get_num_numa_nodes(topo, vm);
    std::pair<std::size_t, std::size_t> pus =
        get_num_numa_pus(topo, numa_nodes, vm);

    std::size_t vector_size = vm["vector_size"].as<std::size_t>();
    std::size_t offset = vm["offset"].as<std::size_t>();
    std::size_t iterations = vm["iterations"].as<std::size_t>();
    std::size_t prefetch_distance_factor = vm["prefetch_distance_factor"].as<std::size_t>();

    std::string num_numa_domains_str = vm["stream-numa-domains"].as<std::string>();

    std::string chunker = vm["chunker"].as<std::string>();

    std::cout
        << "-------------------------------------------------------------\n"
        << "Modified STREAM bechmark based on\nHPX version: "
            << hpx::build_string() << "\n"
        << "-------------------------------------------------------------\n"
        << "This system uses " << sizeof(STREAM_TYPE)
            << " bytes per array element.\n"
        << "-------------------------------------------------------------\n"
        << "Array size = " << vector_size << " (elements), "
           "Offset = " << offset << " (elements)\n"
        << "Memory per array = "
            << sizeof(STREAM_TYPE) * (vector_size / 1024. / 1024.) << " MiB "
        << "(= "
            <<  sizeof(STREAM_TYPE) * (vector_size / 1024. / 1024. / 1024.)
            << " GiB).\n"
        << "Each kernel will be executed " << iterations << " times.\n"
        << " The *best* time for each kernel (excluding the first iteration)\n"
        << " will be used to compute the reported bandwidth.\n"
        << "-------------------------------------------------------------\n"
        << "Number of Threads requested = "
            << numa_nodes * pus.second << "\n"
        << "Chunking policy requested: " << chunker << "\n"
        << "-------------------------------------------------------------\n"
        ;

    using namespace hpx::parallel;

    typedef hpx::threads::executors::local_priority_queue_attached_executor
        executor_type;
    typedef std::vector<executor_type> executors_vector;

    executors_vector execs;
    execs.reserve(numa_nodes);

    // creating our executors ....
    for (std::size_t i = 0; i != numa_nodes; ++i)
    {
        // create executor for this NUMA domain
        execs.emplace_back(i * pus.first, pus.second);
    }

    // allocate data
    typedef hpx::parallel::util::numa_allocator<
            STREAM_TYPE, executors_vector
        > allocator_type;
    allocator_type alloc(execs, retrieve_topology());

    typedef std::vector<STREAM_TYPE, allocator_type> vector_type;
    vector_type a(vector_size, STREAM_TYPE(), alloc);
    vector_type b(vector_size, STREAM_TYPE(), alloc);
    vector_type c(vector_size, STREAM_TYPE(), alloc);

    // perform benchmark
    hpx::lcos::local::latch l(numa_nodes);

    double time_total = mysecond();
    std::vector<hpx::future<std::vector<std::vector<double> > > > workers;
    workers.reserve(numa_nodes);

    std::size_t part_size = vector_size / numa_nodes;

    for (std::size_t i = 0; i != numa_nodes; ++i)
    {
        if(chunker == "dynamic")
        {
            auto policy = par.on(execs[i]).with(dynamic_chunk_size());
            workers.push_back(
                hpx::async(execs[i], &numa_domain_worker<vector_type, decltype(policy)>,
                    i, policy, boost::ref(l),
                    vector_size, part_size, part_size*i, iterations, boost::ref(prefetch_distance_factor),
                    boost::ref(a), boost::ref(b), boost::ref(c))
            );
        }
        else if(chunker == "auto")
        {
            auto policy = par.on(execs[i]).with(auto_chunk_size());
            workers.push_back(
                hpx::async(execs[i], &numa_domain_worker<vector_type, decltype(policy)>,
                    i, policy, boost::ref(l),
                    vector_size, part_size, part_size*i, iterations, boost::ref(prefetch_distance_factor),
                    boost::ref(a), boost::ref(b), boost::ref(c))
            );
        }
        else if(chunker == "guided")
        {
            auto policy = par.on(execs[i]).with(guided_chunk_size());
            workers.push_back(
                hpx::async(execs[i], &numa_domain_worker<vector_type, decltype(policy)>,
                    i, policy, boost::ref(l),
                    vector_size, part_size, part_size*i, iterations, boost::ref(prefetch_distance_factor),
                    boost::ref(a), boost::ref(b), boost::ref(c))
            );
        }
        else
        {
            // default
            auto policy = par.on(execs[i]);
            workers.push_back(
                hpx::async(execs[i], &numa_domain_worker<vector_type, decltype(policy)>,
                    i, policy, boost::ref(l),
                    vector_size, part_size, part_size*i, iterations, boost::ref(prefetch_distance_factor),
                    boost::ref(a), boost::ref(b), boost::ref(c))
            );
        }
    }

    std::vector<std::vector<std::vector<double> > >
        timings_all = hpx::util::unwrapped(workers);
    time_total = mysecond() - time_total;

    // --- SUMMARY ---
    const char *label[3] = {
        "original_for_each:                       ", //with normal iterator
        "chunk_size_for_each:                     ",  //with adaptive_chunk_size
        "prefetch_for_each:                       " //with prfetching itr

    };

    const double bytes[3] = {
        7 * sizeof(STREAM_TYPE) * static_cast<double>(vector_size),
        7 * sizeof(STREAM_TYPE) * static_cast<double>(vector_size),
        7 * sizeof(STREAM_TYPE) * static_cast<double>(vector_size)
        // FIXME
        // +30 instructions in calling prefetching NOT SURE: (7 * sizeof(STREAM_TYPE) * static_cast<double>(vector_size) + 12 * sizeof(int) * static_cast<double>(vector_size) + 18 * sizeof(int))
    };
    std::vector<std::vector<double> > timing(3, std::vector<double>(iterations, 0.0));

    for(auto const & times : timings_all)
    {
        for(std::size_t iteration = 0; iteration != iterations; ++iteration)
        {
            timing[0][iteration] += times[0][iteration];
            timing[1][iteration] += times[1][iteration];
            timing[2][iteration] += times[2][iteration];
        }
    }
    for(std::size_t iteration = 0; iteration != iterations; ++iteration)
    {
        timing[0][iteration] /= numa_nodes;
        timing[1][iteration] /= numa_nodes;
        timing[2][iteration] /= numa_nodes;
    }
    // Note: skip first iteration
    std::vector<double> avgtime(3, 0.0);
    std::vector<double> mintime(3, (std::numeric_limits<double>::max)());
    std::vector<double> maxtime(3, 0.0);
    for(std::size_t iteration = 1; iteration != iterations; ++iteration)
    {
        for (std::size_t j=0; j<3; j++)
        {
            avgtime[j] = avgtime[j] + timing[j][iteration];
            mintime[j] = (std::min)(mintime[j], timing[j][iteration]);
            maxtime[j] = (std::max)(maxtime[j], timing[j][iteration]);
        }
    }

    printf("Function                           BestRate MB/s  Avg time     Min time     Max time\n");
    // Printing all

    for (std::size_t j = 0; j < 3; j++) {
        avgtime[j] = avgtime[j]/(double)(iterations-1);

        printf("%s%12.1f  %11.6f  %11.6f  %11.6f\n", label[j],
           1.0E-06 * bytes[j]/mintime[j],
           avgtime[j],
           mintime[j],
           maxtime[j]);
    }

    std::cout
        << "\nTotal time: " << time_total
        << " (per iteration: " << time_total/iterations << ")\n";

    std::cout
        << "-------------------------------------------------------------\n"
        ;

    return hpx::finalize();
}

// --stream-numa-domains=2 --stream-threads=6
//
// Don't use --hpx:threads or --hpx:bind, those are computed internally.
//
int main(int argc, char* argv[])
{
    using namespace boost::program_options;

    options_description cmdline("usage: " HPX_APPLICATION_STRING " [options]");

    cmdline.add_options()
        (   "vector_size",
            boost::program_options::value<std::size_t>()->default_value(50000000),
            "size of vector (default: 1024)")
        (   "offset",
            boost::program_options::value<std::size_t>()->default_value(0),
            "offset (default: 0)")
        (   "iterations",
            boost::program_options::value<std::size_t>()->default_value(10),
            "number of iterations to repeat each test. (default: 10)")
        (   "prefetch_distance_factor",
            boost::program_options::value<std::size_t>()->default_value(1),
            "Distance (in chunk_size) between each preteching data. (default: 1)")
        (   "stream-threads",
            boost::program_options::value<std::string>()->default_value("all"),
            "number of threads per NUMA domain to use. (default: all)")
        (   "stream-numa-domains",
            boost::program_options::value<std::string>()->default_value("all"),
            "number of NUMA domains to use. (default: all)")
        (   "chunker",
            boost::program_options::value<std::string>()->default_value("default"),
            "Which chunker to use for the parallel algorithms. "
            "possible values: dynamic, auto, guided. (default: default)")
        ;

    // parse command line here to extract the necessary settings for HPX
    parsed_options opts =
        command_line_parser(argc, argv)
            .allow_unregistered()
            .options(cmdline)
            .style(command_line_style::unix_style)
            .run();

    variables_map vm;
    store(opts, vm);

    hpx::threads::topology const& topo = retrieve_topology();
    std::size_t numa_nodes = get_num_numa_nodes(topo, vm);
    std::pair<std::size_t, std::size_t> pus =
        get_num_numa_pus(topo, numa_nodes, vm);
    std::size_t num_cores = topo.get_number_of_numa_node_cores(0);

    std::vector<std::string> cfg;
    cfg.push_back("hpx.numa_sensitive=2");  // no-cross NUMA stealing

    // block all cores of requested number of NUMA-domains
    cfg.push_back(boost::str(
        boost::format("hpx.cores=%d") % (numa_nodes * num_cores)
    ));
    cfg.push_back(boost::str(
        boost::format("hpx.os_threads=%d") % (numa_nodes * pus.second)
    ));

    std::string node_name("numanode");
    if (topo.get_number_of_numa_nodes() == 0)
        node_name = "socket";

    std::string bind_desc("hpx.bind!=");
    for (std::size_t i = 0; i != numa_nodes; ++i)
    {
        if (i != 0)
            bind_desc += ";";

        std::size_t base_thread = i * pus.second;
        bind_desc += boost::str(
            boost::format("thread:%d-%d=%s:%d.core:0-%d.pu:0")
              % base_thread % (base_thread+pus.second-1)  // thread:%d-%d
              % node_name % i                             // %s:%d
              % (pus.second-1)                            // core:0-%d
        );
    }
    cfg.push_back(bind_desc);

    return hpx::init(cmdline, argc, argv, cfg);
}

ext: cpp

max_stars_repo_name: STEllAR-GROUP/hpxML

max_issues_repo_name: STEllAR-GROUP/hpxML

max_forks_repo_name: STEllAR-GROUP/hpxML


text:
// Copyright (C) 2020 T. Zachary Laine
//
// 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)
//[ extended_json_example
// This header includes a type called json::value that acts as a
// Javascript-like polymorphic value type.
#include "json.hpp"

#include <boost/parser/parser.hpp>

#include <boost/container/small_vector.hpp>

#include <fstream>
#include <climits>

namespace json {

    namespace bp = ::boost::parser;
    using namespace bp::literals;

    // The JSON spec imposes a limit on how deeply JSON data structures are
    // allowed to nest.  This exception is thrown when that limit is exceeded
    // during the parse.
    template<typename Iter>
    struct excessive_nesting : std::runtime_error
    {
        excessive_nesting(Iter it) :
            runtime_error("excessive_nesting"),
            iter(it)
        {}
        Iter iter;
    };

    // The only globals we need to parse JSON are: "How many data structures
    // deep are we?", and "What is the limit of open data structures
    // allowed?".
    struct global_state
    {
        int recursive_open_count = 0;
        int max_recursive_open_count = 0;
    };

    // When matching paired UTF-16 surrogates, we need to track a bit of state
    // between matching the first and second UTF-16 code units: namely, the
    // value of the first code unit.
    struct double_escape_locals
    {
        int first_surrogate = 0;
    };

    // Here are all the rules declared.  I've given them names that are
    // end-user friendly, so that if there is a parse error, you get a message
    // like "expected four hexidecimal digits here:", instead of "expected
    // hex_4 here:".

    bp::rule<class ws> const ws = "whitespace";

    bp::rule<class string_char, uint32_t> const string_char =
        "code point (code points <= U+001F must be escaped)";
    bp::rule<class four_hex_digits, uint32_t> const hex_4 =
        "four hexidecimal digits";
    bp::rule<class escape_seq, uint32_t> const escape_seq =
        "\\uXXXX hexidecimal escape sequence";
    bp::rule<class escape_double_seq, uint32_t, double_escape_locals> const
        escape_double_seq = "\\uXXXX hexidecimal escape sequence";
    bp::rule<class single_escaped_char, uint32_t> const single_escaped_char =
        "'\"', '\\', '/', 'b', 'f', 'n', 'r', or 't'";

    bp::rule<class null, value> const null = "null";
    bp::rule<class string, std::string> const string = "string";
    bp::rule<class number, double> const number = "number";
    bp::rule<class object_element, boost::parser::tuple<std::string, value>> const
        object_element = "object-element";
    bp::rule<class object_tag, value> const object_p = "object";
    bp::rule<class array_tag, value> const array_p = "array";

    bp::rule<class value_tag, value> const value_p = "value";

    // JSON limits whitespace to just these four characters.
    auto const ws_def = '\x09'_l | '\x0a' | '\x0d' | '\x20';

    // Since our json object representation, json::value, is polymorphic, and
    // since its default-constructed state represents the JSON value "null",
    // we need to tell a json::value that it is an object (similar to a map)
    // before we start inserting values into it.  That's why we need
    // object_init.
    auto object_init = [](auto & ctx) {
        auto & globals = _globals(ctx);
        if (globals.max_recursive_open_count < ++globals.recursive_open_count)
            throw excessive_nesting(_where(ctx).begin());
        _val(ctx) = object();
    };

    // We need object_insert because we can't just insert into the json::value
    // itself.  The json::value does not have an insert() member, because if
    // it is currently holding a number, that makes no sense.  So, for a
    // json::value x, we need to call get<object>(x) to get the object
    // interface.
    auto object_insert = [](auto & ctx) {
        value & v = _val(ctx);
        get<object>(v).insert(std::make_pair(
            std::move(_attr(ctx))[0_c], std::move(_attr(ctx)[1_c])));
    };

    // These are the array analogues of the object semantic actions above.
    auto array_init = [](auto & ctx) {
        auto & globals = _globals(ctx);
        if (globals.max_recursive_open_count < ++globals.recursive_open_count)
            throw excessive_nesting(_where(ctx).begin());
        _val(ctx) = array();
    };
    auto array_append = [](auto & ctx) {
        value & v = _val(ctx);
        get<array>(v).push_back(std::move(_attr(ctx)));
    };

    // escape_double_seq is used to match pairs of UTF-16 surrogates that form
    // a single code point.  So, after matching one UTF-16 code unit c, we
    // only want to keep going if c is a lead/high surrogate.
    auto first_hex_escape = [](auto & ctx) {
        auto & locals = _locals(ctx);
        uint32_t const cu = _attr(ctx);
        if (!boost::parser::detail::text::high_surrogate(cu))
            _pass(ctx) = false; // Not a high surrogate; explicitly fail the parse.
        else
            locals.first_surrogate = cu; // Save this initial code unit for later.
    };
    // This is also used in escape_double_seq.  When we get to this action, we
    // know we've already matched a high surrogate, and so this one had better
    // be a low surrogate, or we have a (local) parse failure.
    auto second_hex_escape = [](auto & ctx) {
        auto & locals = _locals(ctx);
        uint32_t const cu = _attr(ctx);
        if (!boost::parser::detail::text::low_surrogate(cu)) {
            _pass(ctx) = false; // Not a low surrogate; explicitly fail the parse.
        } else {
            // Success!  Write to the rule's attribute the code point that the
            // first and second code points form.
            uint32_t const high_surrogate_min = 0xd800;
            uint32_t const low_surrogate_min = 0xdc00;
            uint32_t const surrogate_offset =
                0x10000 - (high_surrogate_min << 10) - low_surrogate_min;
            uint32_t const first_cu = locals.first_surrogate;
            _val(ctx) = (first_cu << 10) + cu + surrogate_offset;
        }
    };

    // This is the verbose form of declaration for the integer and unsigned
    // integer parsers int_parser and uint_parser.  In this case, we don't
    // want to use boost::parser::hex directly, since it has a variable number
    // of digits.  We want to match exactly 4 digits, and this is how we
    // declare a hexidecimal parser that matches exactly 4.
    bp::parser_interface<bp::uint_parser<uint32_t, 16, 4, 4>> const hex_4_def;

    // We use > here instead of >>, because once we see \u, we know that
    // exactly four hex digits must follow -- no other production rule starts
    // with \u.
    auto const escape_seq_def = "\\u" > hex_4;

    // This uses the actions above and the simpler rule escape_seq to find
    // matched UTF-16 surrogate pairs.
    auto const escape_double_seq_def =
        escape_seq[first_hex_escape] >> escape_seq[second_hex_escape];

    // This symbol table recognizes each character that can appear right after
    // an escaping backslash, and, if it finds one, produces the associated
    // code point as its attribute.
    bp::symbols<uint32_t> const single_escaped_char_def = {
        {"\"", 0x0022u},
        {"\\", 0x005cu},
        {"/", 0x002fu},
        {"b", 0x0008u},
        {"f", 0x000cu},
        {"n", 0x000au},
        {"r", 0x000du},
        {"t", 0x0009u}};

    // A string may be a matched UTF-16 escaped surrogate pair, a single
    // escaped UTF-16 code uint treated as a whole code point, a single
    // escaped character like \f, or any other code point outside the range
    // [0x0000u, 0x001fu].  Note that we had to put escape_double_seq before
    // escape_seq.  Otherwise, escape_seq would eat all the escape sequences
    // before escape_double_seq could try to match them.
    auto const string_char_def = escape_double_seq | escape_seq |
                                 ('\\'_l > single_escaped_char) |
                                 (bp::cp - bp::char_(0x0000u, 0x001fu));

    // If we see the special token null, treat that as a default-constructed
    // json::value.  Note that we could have done this with a semantic action,
    // but it is best to do everything you can without semantic actions;
    // they're a lot of code.
    auto const null_def = "null" >> bp::attr(value());

    auto const string_def = bp::lexeme['"' >> *(string_char - '"') > '"'];

    // Since the JSON format for numbers is not exactly what
    // boost::parser::double_ accepts (double_ accepts too much), we need to
    // parse a JSON number as a sequence of characters, and then pass the
    // result to double_ to actually get the numeric value.  This action does
    // that.  The parser uses boost::parser::raw to produce the subrange of
    // the input that covers the number as an attribute, which is used here.
    auto parse_double = [](auto & ctx) {
        auto const cp_range = _attr(ctx);
        auto cp_first = cp_range.begin();
        auto const cp_last = cp_range.end();

        auto const result = bp::parse(cp_first, cp_last, bp::double_);
        if (result) {
            _val(ctx) = *result;
        } else {
            boost::container::small_vector<char, 128> chars(cp_first, cp_last);
            auto const chars_first = &*chars.begin();
            auto chars_last = chars_first + chars.size();
            _val(ctx) = std::strtod(chars_first, &chars_last);
        }
    };

    // As indicated above, we want to match the specific formats JSON allows,
    // and then re-parse the resulting matched range within the semantic
    // action.
    auto const number_def = bp::raw
        [bp::lexeme
             [-bp::char_('-') >>
              (bp::char_('1', '9') >> *bp::ascii::digit | bp::char_('0')) >>
              -(bp::char_('.') >> +bp::ascii::digit) >>
              -(bp::char_("eE") >> -bp::char_("+-") >> +bp::ascii::digit)]]
        [parse_double];

    // Note how, in the next three parsers, we turn off backtracking by using
    // > instead of >>, once we know that there is no backtracking alternative
    // that might match if we fail to match the next element.  This produces
    // much better error messages than if you always use >>.

    auto const object_element_def = string > ':' > value_p;

    auto const object_p_def = '{'_l[object_init] >>
                              -(object_element[object_insert] % ',') > '}';

    auto const array_p_def = '['_l[array_init] >>
                             -(value_p[array_append] % ',') > ']';

    // This is the top-level parser.
    auto const value_p_def =
        number | bp::bool_ | null | string | array_p | object_p;

    // Here, we define all the rules we've declared about, which also connects
    // each rule to its _def-suffixed parser.
    BOOST_PARSER_DEFINE_RULES(
        ws,
        hex_4,
        escape_seq,
        escape_double_seq,
        single_escaped_char,
        string_char,
        null,
        string,
        number,
        object_element,
        object_p,
        array_p,
        value_p);

    // json::parse() takes a string_view as input.  It takes an optional
    // callback to use for error reporting, which defaults to a no-op that
    // ignores all errors.  It also takes an optional max recursion depth
    // limit, which defaults to the one from the JSON spec, 512.
    std::optional<value> parse(
        std::string_view str,
        diagnostic_function errors_callback = diagnostic_function(),
        int max_recursion = 512)
    {
        // Turn the input range into a UTF-32 range, so that we can be sure
        // that we fall into the Unicode-aware parsing path inside parse()
        // below.
        auto const range = boost::parser::detail::text::as_utf32(str);
        using iter_t = decltype(range.begin());

        if (max_recursion <= 0)
            max_recursion = INT_MAX;

        // Initialize our globals to the current depth (0), and the max depth
        // (max_recursion).
        global_state globals{0, max_recursion};
        bp::callback_error_handler error_handler(errors_callback);
        // Make a new parser that includes the globals and error handler.
        auto const parser = bp::with_error_handler(
            bp::with_globals(value_p, globals), error_handler);

        try {
            // Parse.  If no exception is thrown, due to: a failed expectation
            // (such as foo > bar, where foo matches the input, but then bar
            // cannot); or because the nesting depth is exceeded; we simply
            // return the result of the parse.  The result will contextually
            // convert to false if the parse failed.  Note that the
            // failed-expectation exception is caught internally, and used to
            // generate an error message.
            return bp::parse(range, parser, ws);
        } catch (excessive_nesting<iter_t> const & e) {
            // If we catch an excessive_nesting exception, just reported it
            // and return an empty/failure result.
            if (errors_callback) {
                std::string const message = "error: Exceeded maximum number (" +
                                            std::to_string(max_recursion) +
                                            ") of open arrays and/or objects";
                std::stringstream ss;
                bp::write_formatted_message(
                    ss, "", range.begin(), e.iter, range.end(), message);
                errors_callback(ss.str());
            }
        }

        return {};
    }

}

std::string file_slurp(std::ifstream & ifs)
{
    std::string retval;
    while (ifs) {
        char const c = ifs.get();
        retval += c;
    }
    if (!retval.empty() && retval[retval.back() == -1])
        retval.pop_back();
    return retval;
}

int main(int argc, char * argv[])
{
    if (argc < 2) {
        std::cerr << "A filename to parse is required.\n";
        exit(1);
    }

    std::ifstream ifs(argv[1]);
    if (!ifs) {
        std::cerr << "Unable to read file '" << argv[1] << "'.\n";
        exit(1);
    }

    // Read in the entire file.
    std::string const file_contents = file_slurp(ifs);
    // Parse the contents.  If there is an error, just stream it to cerr.
    auto json = json::parse(
        file_contents, [](std::string const & msg) { std::cerr << msg; });
    if (!json) {
        std::cerr << "Parse failure.\n";
        exit(1);
    }

    std::cout << "Parse successful; contents:\n" << *json << "\n";

    return 0;
}
//]

max_stars_repo_name: tzlaine/parser

max_issues_repo_name: tzlaine/parser

max_forks_repo_name: tzlaine/parser


text:
/*
 *
 * Copyright (c) 1998-2002
 * Dr John Maddock
 *
 * Use, modification and distribution are subject to 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)
 *
 */

 /*
  *   LOCATION:    see http://www.boost.org for most recent version.
  *   FILE         src.cpp
  *   VERSION      see <boost/version.hpp>
  *   DESCRIPTION: Includes all the regex source files, include this
  *                file only if you need to build the regex library
  *                as a single file.  You must include this file
  *                before any other regex header.
  *
  *                CAUTION: THIS FILE IS DEPRICATED AND WILL CAUSE
  *                UNNECESSARY CODE BLOAT.
  */

#if (!defined(BOOST_REGEX_NO_LIB) || !defined(BOOST_REGEX_NO_EXTERNAL_TEMPLATES)) && defined(BOOST_REGEX_CONFIG_HPP)
#error too late you have already included a regex header - make sure that you include this header before any other boost header
#endif

#define BOOST_REGEX_NO_LIB
#define BOOST_REGEX_STATIC_LINK
#define BOOST_REGEX_NO_EXTERNAL_TEMPLATES

#include <boost/regex.hpp>

//
// include library source files:
//
#ifdef BOOST_REGEX_USE_WIN32_LOCALE
#include "libs/regex/src/w32_regex_traits.cpp"
#elif defined(BOOST_REGEX_USE_C_LOCALE)
#include "libs/regex/src/c_regex_traits.cpp"
#else
#include "libs/regex/src/cpp_regex_traits.cpp"
#endif
#include "libs/regex/src/c_regex_traits_common.cpp"
#include "libs/regex/src/cregex.cpp"
#include "libs/regex/src/fileiter.cpp"
#include "libs/regex/src/posix_api.cpp"
#include "libs/regex/src/wide_posix_api.cpp"
#include "libs/regex/src/regex.cpp"
#include "libs/regex/src/regex_debug.cpp"
#include "libs/regex/src/regex_synch.cpp"

max_stars_repo_name: macaurther/DOCUSA

max_issues_repo_name: macaurther/DOCUSA

max_forks_repo_name: dguenms/Dawn-of-Civilization


text:
//  helper class for future

//  Copyright (c) 2005 Thorsten Schuett. 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)

#ifndef BOOST_DETAIL_CALLBACK_REGISTRY_HPP
#define BOOST_DETAIL_CALLBACK_REGISTRY_HPP 1

#include <map>

#include <boost/assert.hpp>

///////////////////////////////////////////////////////////////////////////////
namespace boost {  namespace futures {  namespace detail {

  /////////////////////////////////////////////////////////////////////////////
  //
  //  The visitor_registry class wraps the registration and notfication of
  //  nullary functors. Note: this class is not thread safe by itself. Threading
  //  issues have to be taken into account at the call point.
  //
  /////////////////////////////////////////////////////////////////////////////
  template <typename T>
  class visitor_registry
  {
      typedef std::map<int, boost::function<void (T)> > callbacks_type;

  public:
      visitor_registry()
      : count(0)
      {
      }

      ~visitor_registry()
      {
          BOOST_ASSERT(0 == callbacks.size());
      }

      template <typename Func>
      int register_callback(Func const& cb)
      {
          boost::mutex::scoped_lock scoped_lock(lock);

          callbacks[count] = boost::function<void (T)>(cb);
          return count++;
      }

      void unregister_callback(int cookie)
      {
          boost::mutex::scoped_lock scoped_lock(lock);

          BOOST_ASSERT(callbacks.find(cookie) != callbacks.end());
          callbacks.erase(cookie);
      }

      void notify_callbacks(T const& data)
      {
          boost::mutex::scoped_lock scoped_lock(lock);

          typedef typename callbacks_type::iterator iterator;
          iterator end = callbacks.end();
          for (iterator it = callbacks.begin();
	       it != end;
	       ++it)
          {
              (it->second)(data);
          }
      }

  private:
      boost::mutex lock;
      callbacks_type callbacks;
      int count;
  };

///////////////////////////////////////////////////////////////////////////////
}}}  // namespace boost::futures::detail

#endif /* BOOST_DETAIL_CALLBACK_REGISTRY_HPP */

max_stars_repo_name: saga-project/saga-cpp

max_issues_repo_name: saga-project/saga-cpp

max_forks_repo_name: saga-project/saga-cpp


text:
#include "Peer.h"
#include <boost/bind.hpp>

#include <iostream>

using namespace std;

Peer::Peer(IOServiceType &service) : m_spSock(new SocketType(service))
{
    m_pBuf = NULL;
    m_bufLen = 0;
    m_bodyLen = 0;
}

Peer::Peer(SocketPtr spSock) : m_spSock(spSock)
{
    m_pBuf = NULL;
    m_bufLen = 0;
    m_bodyLen = 0;
}

Peer::~Peer()
{
    delete[] m_pBuf;
    m_bufLen = 0;
    m_bodyLen = 0;

    cout << "peer desconsturct" << endl;
}

bool Peer::Vaild()
{
    return false;
}

void Peer::Close()
{
    return m_spSock->close();
}

bool Peer::Send(uint8_t *pData, uint32_t len)
{
    cout << "peer send" << endl;
    cout << "send body len:" << *((uint32_t *)pData) << endl;
    async_write(*m_spSock.Get(), AsioBuffer(pData, len),
                            boost::bind(&Peer::SendHandler, this, pData, len, ErrorPlaceholder));

    return true;
}

void Peer::SendHandler(uint8_t *pData, uint32_t len, const ErrorCode &ec)
{
    delete[] pData;
    pData = NULL;
    cout << "send sum len:" << len << endl;
    if (ec)
    {
        OnError(ec);
    }
    cout << "send ok" << endl;
    return ;
}

void Peer::AsyncRecv()
{
    cout << "peer recv" << endl;

    async_read(*m_spSock.Get(), AsioBuffer(&m_bodyLen, NET_HEAD_LEN),
                    boost::bind(&Peer::HeadRecvHandler, this, ErrorPlaceholder));
}

void Peer::HeadRecvHandler(const ErrorCode &ec)
{
    cout << "recv head" << endl;
    if (ec)
    {
        OnError(ec);
        return ;
    }
    cout << "recv body len:" << m_bodyLen << endl;

    int32_t len = m_bodyLen + NET_HEAD_LEN;
    cout << "recv sum len:" << len << endl;
    if (m_bufLen < len)
    {
        delete[] m_pBuf;
        m_pBuf = new uint8_t[len];
        m_bufLen = len;
    }

    memcpy(m_pBuf, &m_bodyLen, NET_HEAD_LEN);

    async_read(*m_spSock.Get(), AsioBuffer(m_pBuf + NET_HEAD_LEN, m_bodyLen),
                    boost::bind(&Peer::BodyRecvHandler, this, ErrorPlaceholder));
}

void Peer::BodyRecvHandler(const ErrorCode &ec)
{
    cout << "recv body" << endl;
    if (ec)
    {
        OnError(ec);
        return ;
    }

    OnData(m_pBuf, m_bodyLen + NET_HEAD_LEN);

    async_read(*m_spSock.Get(), AsioBuffer(&m_bodyLen, NET_HEAD_LEN),
                    boost::bind(&Peer::HeadRecvHandler, this, ErrorPlaceholder));
}

void Peer::OnData(uint8_t *pData, uint32_t len)
{
}

int Peer::OnError(const ErrorCode &ec)
{
    cout << ec.value() << endl;
    cout << ec.category().name() << endl;
    return -ec.value();
}

max_stars_repo_name: SeeForward/SysMonitor

max_issues_repo_name: SeeForward/SysMonitor

max_forks_repo_name: SeeForward/SysMonitor


text:
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <time.h>
#include <algorithm>
#include <vector>
#include <map>

// OpenCV headers
#include <cv.h>
#include <highgui.h>

#include <opencv2/contrib/contrib.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <opencv_workbench/utils/Stream.h>
#include <opencv_workbench/syllo/syllo.h>

#include <opencv_workbench/utils/AnnotationParser.h>
#include <opencv_workbench/plot/Plot.h>

#include <opencv_workbench/detector/Detector.h>
#include <opencv_workbench/plugin_manager/PluginManager.h>

#include <opencv_workbench/syllo/syllo.h>
#include <opencv_workbench/wb/ROC.h>
#include <yaml-cpp/yaml.h>

#include <boost/regex.hpp>
#include <boost/filesystem.hpp>

using std::cout;
using std::endl;

PluginManager<Detector, Detector_maker_t> plugin_manager_;

struct ROCData{
     double TPR;
     double FPR;
     double threshold;
     double TPR_error_low;
     double TPR_error_high;
     double FPR_error_low;
     double FPR_error_high;
     int FP;
     int FN;
     int TP;
     int TN;
};

bool myfunction (struct ROCData i, struct ROCData j)
{
     if (i.FPR == j.FPR) {
          return (i.TPR < j.TPR);
     } else {
          return (i.FPR < j.FPR);
     }
}

int main(int argc, char *argv[])
{
     std::string work_dir = "";

     int c;
     std::string param_sweep = "";
     std::string output_file = "";
     std::string output_dir = "./";
     std::string ranges_dir = "NONE";
     while ((c = getopt (argc, argv, "r:s:f:o:d:")) != -1) {
          switch (c) {
          case 'r':
               ranges_dir = std::string(optarg);
               break;
          case 's':
               param_sweep = std::string(optarg);
               break;
          case 'o':
               output_dir = std::string(optarg);
               break;
          case 'f':
               output_file = std::string(optarg);
               break;
          case 'd':
               work_dir = std::string(optarg);
               break;
          case '?':
               if (optopt == 'd') {
                    fprintf (stderr, "Option -%c requires an argument.\n", optopt);
               } else if (isprint (optopt)) {
                    fprintf (stderr, "Unknown option `-%c'.\n", optopt);
               } else {
                    fprintf (stderr,
                             "Unknown option character `\\x%x'.\n",
                             optopt);
               }
               return 1;
          default:
               abort ();
          }
     }

     if (param_sweep == "") {
          cout << "Error: Specify a parameter to sweep with -s" << endl;
          return -1;
     }

     // Check existence of xml directory
     if ( !boost::filesystem::exists( fs::path(work_dir) ) ) {
          cout << "Error: XML Directory doesn't exist." << endl;
          return -1;
     }

     // Check existence of parameter ranges directory
     if ( !boost::filesystem::exists( fs::path(ranges_dir) ) ) {
          cout << "Error: Range Directory doesn't exist:" << ranges_dir << endl;
          return -1;
     }

     std::string threshold_type = "NOTSET";

     // Extract the TPR, FPR, TN counts from the /tracks/roc.csv files
     // Key = threshold (as string)
     std::map<std::string, std::vector<struct ROCData> > all_data;
     boost::filesystem::directory_iterator end_itr; // Default ctor yields past-the-end
     for( boost::filesystem::directory_iterator i( work_dir ); i != end_itr; ++i )
     {
          std::string str = i->path().stem().string();

          // Skip if no match
          if (str.compare(0,4,"fold") != 0 ) continue;

          std::string roc_file = i->path().string() + "/tracks/roc.csv";

          // Check existence of roc file
          if ( !boost::filesystem::exists( fs::path(roc_file) ) ) {
               cout << "Error: ROC File Directory doesn't exist: " << roc_file << endl;
               continue;
          }

          // Open the file and read the data
          std::ifstream file(roc_file.c_str());

          std::string TPR, FPR, threshold,FP,FN,TP,TN;
          while (std::getline(file, FPR, ',')) {

               if (FPR.compare(0,1,"#") == 0) {
                    // Throw away comment lines
                    std::getline(file, FPR) ;
                    continue;
               }

               std::getline(file, TPR, ',') ;
               std::getline(file, threshold, ',') ;

               std::getline(file, FP, ',');
               std::getline(file, FN, ',');
               std::getline(file, TP, ',');
               std::getline(file, TN, ',');
               std::getline(file, threshold_type);

               struct ROCData d;
               d.TPR = syllo::str2double(TPR);
               d.FPR = syllo::str2double(FPR);
               d.threshold = syllo::str2double(threshold);
               d.FP = syllo::str2int(FP);
               d.FN = syllo::str2int(FN);
               d.TP = syllo::str2int(TP);
               d.TN = syllo::str2int(TN);

               all_data[syllo::double2str(d.threshold)].push_back(d);
          }
     }

     // Compute mean and std for each threshold point
     std::vector< struct ROCData > final;
     for(std::map<std::string, std::vector<struct ROCData> >::iterator it1 = all_data.begin();
         it1 != all_data.end(); it1++) {
          double TPR_sum = 0;
          double FPR_sum = 0;
          int FP_sum = 0;
          int FN_sum = 0;
          int TP_sum = 0;
          int TN_sum = 0;
          for (std::vector<struct ROCData>::iterator it2 = it1->second.begin();
               it2 != it1->second.end(); it2++) {
               if (it2->threshold != syllo::str2double(it1->first)) {
                    cout << "ERROR: Mismatched thresholds" << endl;
                    return -1;
               }

               TPR_sum += it2->TPR;
               FPR_sum += it2->FPR;

               FP_sum += it2->FP;
               FN_sum += it2->FN;
               TP_sum += it2->TP;
               TN_sum += it2->TN;
          }
          double TPR_avg = TPR_sum / ((double)it1->second.size());
          double FPR_avg = FPR_sum / ((double)it1->second.size());

          int FP_avg = cvRound(FP_sum / ((double)it1->second.size()));
          int FN_avg = cvRound(FN_sum / ((double)it1->second.size()));
          int TP_avg = cvRound(TP_sum / ((double)it1->second.size()));
          int TN_avg = cvRound(TN_sum / ((double)it1->second.size()));

          double TPR_sum_var = 0;
          double FPR_sum_var = 0;
          for (std::vector<struct ROCData>::iterator it2 = it1->second.begin();
               it2 != it1->second.end(); it2++) {
               if (it2->threshold != syllo::str2double(it1->first)) {
                    cout << "ERROR: Mismatched thresholds" << endl;
                    return -1;
               }

               TPR_sum_var += pow(it2->TPR - TPR_avg,2);
               FPR_sum_var += pow(it2->FPR - FPR_avg,2);
          }
          int count = it1->second.size()-1;
          double TPR_std = sqrt(TPR_sum_var / (double)count);
          double FPR_std = sqrt(FPR_sum_var / (double)count);

          struct ROCData d;
          d.TPR = TPR_avg;
          d.FPR = FPR_avg;
          d.threshold = syllo::str2double(it1->first);

          double z = 1.96; // 95% confidence interval
          //d.TPR_error_low = TPR_avg - z*sqrt(TPR_avg*(1-TPR_avg)/count);
          //d.TPR_error_high = TPR_avg + z*sqrt(TPR_avg*(1-TPR_avg)/count);
          //d.FPR_error_low = FPR_avg - z*sqrt(FPR_avg*(1-FPR_avg)/count);
          //d.FPR_error_high = FPR_avg + z*sqrt(FPR_avg*(1-FPR_avg)/count);
          d.TPR_error_low = TPR_avg - z * TPR_std / sqrt(count);
          d.TPR_error_high = TPR_avg + z * TPR_std / sqrt(count);
          d.FPR_error_low = FPR_avg - z * FPR_std / sqrt(count);
          d.FPR_error_high = FPR_avg + z * FPR_std / sqrt(count);

          d.FP = FP_avg;
          d.FN = FN_avg;
          d.TP = TP_avg;
          d.TN = TN_avg;

          final.push_back(d);
     }

     // sort final vector by TPR/FPR
     std::sort(final.begin(), final.end(), myfunction);

     // Print out the results
     std::string roc_fn = output_dir + "/avg-roc.csv";
     std::ofstream roc_stream;
     cout << "ROC Output file: " << roc_fn << endl;
     roc_stream.open (roc_fn.c_str(), std::ofstream::out);
     roc_stream << "# FPR,TPR,FPR_error_low,FPR_error_high,TPR_error_low,TPR_error_high,thresh" << endl;

     // Save the average ROC plot to a text file for gnuplot. Also, create a
     // metrics_vector to be used by ROC.h to find the operating point.
     std::vector< std::map<std::string,double> > metrics_vector;
     for(std::vector< struct ROCData >::iterator it = final.begin();
         it != final.end(); it++) {
          roc_stream << it->FPR << "," << it->TPR << ","
                     << it->FPR_error_low << "," << it->FPR_error_high << ","
                     << it->TPR_error_low << "," << it->TPR_error_high << ","
                     << it->threshold << endl;

          std::map<std::string,double> metrics;
          metrics["FPR"] = it->FPR;
          metrics["TPR"] = it->TPR;
          metrics["thresh_value"] = it->threshold;
          metrics["FP"] = it->FP;
          metrics["FN"] = it->FN;
          metrics["TP"] = it->TP;
          metrics["TN"] = it->TN;

          metrics_vector.push_back(metrics);
     }
     roc_stream.close();

     std::vector< std::map<std::string,double> >::iterator it_oppt;
     ROC::OperatingPoint(metrics_vector, it_oppt);

     // Find the yaml file that produced the operating point and copy it to the
     // test.yaml
     std::string output_yaml = output_dir + "/test.yaml";
     if (!syllo::copy_file_with_value(ranges_dir, output_yaml, param_sweep, (*it_oppt)["thresh_value"])) {
          return -1;
     }

     //// Write out the threshold to a yaml param file for the final test set
     //YAML::Emitter out;
     //out << YAML::BeginMap;
     //out << YAML::Key << param_sweep;
     //out << YAML::Value << syllo::double2str((*it_oppt)["thresh_value"]);
     //out << YAML::Key << "threshold_type";
     //out << YAML::Value << threshold_type;
     //out << YAML::EndMap;
     //
     //std::string out_filename = output_dir + "/test.yaml";
     //std::ofstream output;
     //output.open(out_filename.c_str(), std::ofstream::out);
     //output << out.c_str();
     //output.close();

     return 0;
}

max_stars_repo_name: SyllogismRXS/opencv-workbench

max_issues_repo_name: SyllogismRXS/opencv-workbench

max_forks_repo_name: SyllogismRXS/opencv-workbench


text:
#ifndef STAN_MATH_PRIM_MAT_FUN_GET_LP_HPP
#define STAN_MATH_PRIM_MAT_FUN_GET_LP_HPP

#include <boost/math/tools/promotion.hpp>
#include <stan/math/prim/mat/fun/accumulator.hpp>

namespace stan {
namespace math {

template <typename T_lp, typename T_lp_accum>
inline typename boost::math::tools::promote_args<T_lp, T_lp_accum>::type get_lp(
    const T_lp& lp, const accumulator<T_lp_accum>& lp_accum) {
  return lp + lp_accum.sum();
}

}  // namespace math
}  // namespace stan
#endif

max_stars_repo_name: jrmie/math

max_issues_repo_name: jrmie/math

max_forks_repo_name: jrmie/math


text:
/**
 * Copyright Soramitsu Co., Ltd. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

#ifndef KAGOME_TEST_CORE_RUNTIME_RUNTIME_TEST_BASE_HPP
#define KAGOME_TEST_CORE_RUNTIME_RUNTIME_TEST_BASE_HPP

#include <gtest/gtest.h>

#include <boost/filesystem/path.hpp>
#include <fstream>
#include <memory>

#include "crypto/bip39/impl/bip39_provider_impl.hpp"
#include "crypto/crypto_store/crypto_store_impl.hpp"
#include "crypto/ed25519/ed25519_provider_impl.hpp"
#include "crypto/hasher/hasher_impl.hpp"
#include "crypto/pbkdf2/impl/pbkdf2_provider_impl.hpp"
#include "crypto/random_generator/boost_generator.hpp"
#include "crypto/secp256k1/secp256k1_provider_impl.hpp"
#include "crypto/sr25519/sr25519_provider_impl.hpp"
#include "host_api/impl/host_api_factory_impl.hpp"
#include "mock/core/application/app_configuration_mock.hpp"
#include "mock/core/blockchain/block_header_repository_mock.hpp"
#include "mock/core/offchain/offchain_persistent_storage_mock.hpp"
#include "mock/core/runtime/trie_storage_provider_mock.hpp"
#include "mock/core/storage/changes_trie/changes_tracker_mock.hpp"
#include "mock/core/storage/trie/polkadot_trie_cursor_mock.h"
#include "mock/core/storage/trie/serialization/trie_serializer_mock.hpp"
#include "mock/core/storage/trie/trie_batches_mock.hpp"
#include "mock/core/storage/trie/trie_storage_mock.hpp"
#include "primitives/block.hpp"
#include "primitives/block_header.hpp"
#include "primitives/block_id.hpp"
#include "runtime/common/module_repository_impl.hpp"
#include "runtime/common/runtime_transaction_error.hpp"
#include "runtime/common/runtime_upgrade_tracker_impl.hpp"
#include "runtime/core_api_factory.hpp"
#include "runtime/executor.hpp"
#include "runtime/runtime_environment_factory.hpp"
#include "storage/in_memory/in_memory_storage.hpp"
#include "testutil/literals.hpp"
#include "testutil/outcome.hpp"
#include "testutil/runtime/common/basic_code_provider.hpp"

using testing::_;
using testing::Return;

using namespace kagome;

class RuntimeTestBase : public ::testing::Test {
 public:
  using Buffer = common::Buffer;
  using Block = primitives::Block;
  using BlockId = primitives::BlockId;
  using BlockHeader = primitives::BlockHeader;
  using Extrinsic = primitives::Extrinsic;
  using Digest = primitives::Digest;
  using PersistentTrieBatchMock = storage::trie::PersistentTrieBatchMock;
  using EphemeralTrieBatchMock = storage::trie::EphemeralTrieBatchMock;
  using TopperTrieBatchMock = storage::trie::TopperTrieBatchMock;

  void initStorage() {
    using storage::trie::PersistentTrieBatch;
    using storage::trie::TopperTrieBatchMock;

    auto random_generator = std::make_shared<crypto::BoostRandomGenerator>();
    auto sr25519_provider =
        std::make_shared<crypto::Sr25519ProviderImpl>(random_generator);
    auto ed25519_provider =
        std::make_shared<crypto::Ed25519ProviderImpl>(random_generator);
    auto secp256k1_provider = std::make_shared<crypto::Secp256k1ProviderImpl>();
    hasher_ = std::make_shared<crypto::HasherImpl>();
    auto pbkdf2_provider = std::make_shared<crypto::Pbkdf2ProviderImpl>();
    auto bip39_provider =
        std::make_shared<crypto::Bip39ProviderImpl>(pbkdf2_provider);
    auto keystore_path =
        boost::filesystem::temp_directory_path()
        / boost::filesystem::unique_path("kagome_keystore_test_dir");
    auto crypto_store = std::make_shared<crypto::CryptoStoreImpl>(
        std::make_shared<crypto::Ed25519Suite>(ed25519_provider),
        std::make_shared<crypto::Sr25519Suite>(sr25519_provider),
        bip39_provider,
        crypto::KeyFileStorage::createAt(keystore_path).value());
    changes_tracker_ =
        std::make_shared<storage::changes_trie::ChangesTrackerMock>();
    offchain_storage_ =
        std::make_shared<offchain::OffchainPersistentStorageMock>();

    host_api_factory_ = std::make_shared<host_api::HostApiFactoryImpl>(
        kagome::host_api::OffchainExtensionConfig{},
        changes_tracker_,
        sr25519_provider,
        ed25519_provider,
        secp256k1_provider,
        hasher_,
        crypto_store,
        bip39_provider,
        offchain_storage_);

    header_repo_ = std::make_shared<
        testing::NiceMock<blockchain::BlockHeaderRepositoryMock>>();

    ON_CALL(*header_repo_, getHashByNumber(0))
        .WillByDefault(testing::Return("genesis_hash"_hash256));
    EXPECT_CALL(*header_repo_,
                getBlockHeader(testing::AnyOf(
                    primitives::BlockId{0},
                    primitives::BlockId{"genesis_hash"_hash256})))
        .WillRepeatedly(testing::Return(primitives::BlockHeader{
            .parent_hash = {},
            .number = 0,
            .state_root = {"genesis state root"_hash256},
            .extrinsics_root = {"genesis ext root"_hash256},
            .digest = {}}));
  }

  virtual std::shared_ptr<runtime::ModuleFactory> createModuleFactory() = 0;

  void SetUp() override {
    initStorage();
    trie_storage_ = std::make_shared<storage::trie::TrieStorageMock>();
    serializer_ = std::make_shared<storage::trie::TrieSerializerMock>();

    auto module_factory = createModuleFactory();

    auto wasm_path = boost::filesystem::path(__FILE__).parent_path().string()
                     + "/wasm/sub2dev.wasm";
    wasm_provider_ = std::make_shared<runtime::BasicCodeProvider>(wasm_path);

    std::shared_ptr<runtime::RuntimeUpgradeTrackerImpl> upgrade_tracker =
        runtime::RuntimeUpgradeTrackerImpl::create(
            header_repo_,
            std::make_shared<storage::InMemoryStorage>(),
            std::make_shared<primitives::CodeSubstituteHashes>())
            .value();

    auto module_repo = std::make_shared<runtime::ModuleRepositoryImpl>(
        upgrade_tracker, module_factory);

    runtime_env_factory_ = std::make_shared<runtime::RuntimeEnvironmentFactory>(
        std::move(wasm_provider_), std::move(module_repo), header_repo_);

    executor_ =
        std::make_shared<runtime::Executor>(header_repo_, runtime_env_factory_);
  }

  void preparePersistentStorageExpects() {
    EXPECT_CALL(*trie_storage_, getPersistentBatchAt(_))
        .WillOnce(testing::Invoke([this](auto &root) {
          auto batch = std::make_unique<PersistentTrieBatchMock>();
          prepareStorageBatchExpectations(*batch);
          return batch;
        }));
  }

  void prepareEphemeralStorageExpects() {
    EXPECT_CALL(*trie_storage_, getEphemeralBatchAt(_))
        .WillOnce(testing::Invoke([this](auto &root) {
          auto batch = std::make_unique<EphemeralTrieBatchMock>();
          prepareStorageBatchExpectations(*batch);
          return batch;
        }));
  }

  template <typename BatchMock>
  void prepareStorageBatchExpectations(BatchMock &batch) {
    ON_CALL(batch, get(_)).WillByDefault(testing::Invoke([](auto &key) {
      return common::Buffer();
    }));
    ON_CALL(batch, put(_, _))
        .WillByDefault(testing::Return(outcome::success()));
    ON_CALL(batch, remove(_))
        .WillByDefault(testing::Return(outcome::success()));
    ON_CALL(batch, clearPrefix(_, _))
        .WillByDefault(testing::Return(outcome::success()));
    ON_CALL(batch, trieCursor()).WillByDefault(testing::Invoke([] {
      auto cursor = std::make_unique<storage::trie::PolkadotTrieCursorMock>();
      ON_CALL(*cursor, seekUpperBound(_))
          .WillByDefault(Return(outcome::success()));
      return cursor;
    }));
    auto heappages_key = ":heappages"_buf;
    EXPECT_CALL(batch, get(heappages_key));
  }

  primitives::BlockHeader createBlockHeader(primitives::BlockHash const &hash,
                                            primitives::BlockNumber number) {
    common::Hash256 parent_hash = "genesis_hash"_hash256;

    common::Hash256 state_root = "state_root"_hash256;

    common::Hash256 extrinsics_root = "extrinsics_root"_hash256;

    Digest digest{};

    ON_CALL(*header_repo_, getHashByNumber(number))
        .WillByDefault(testing::Return(hash));
    ON_CALL(*header_repo_, getNumberByHash(hash))
        .WillByDefault(testing::Return(number));

    BlockHeader header{
        parent_hash, number, state_root, extrinsics_root, digest};
    EXPECT_CALL(*header_repo_, getBlockHeader(testing::AnyOf(hash, number)))
        .WillRepeatedly(testing::Return(header));
    return header;
  }

  primitives::Block createBlock(primitives::BlockHash const &hash,
                                primitives::BlockNumber number) {
    auto header = createBlockHeader(hash, number);

    std::vector<Extrinsic> xts;

    return Block{header, xts};
  }

 protected:
  std::shared_ptr<testing::NiceMock<blockchain::BlockHeaderRepositoryMock>>
      header_repo_;
  std::shared_ptr<runtime::RuntimeCodeProvider> wasm_provider_;
  std::shared_ptr<storage::trie::TrieStorageMock> trie_storage_;
  std::shared_ptr<storage::trie::TrieSerializerMock> serializer_;
  std::shared_ptr<runtime::RuntimeEnvironmentFactory> runtime_env_factory_;
  std::shared_ptr<runtime::Executor> executor_;
  std::shared_ptr<storage::changes_trie::ChangesTrackerMock> changes_tracker_;
  std::shared_ptr<offchain::OffchainPersistentStorageMock> offchain_storage_;
  std::shared_ptr<crypto::Hasher> hasher_;
  std::shared_ptr<host_api::HostApiFactory> host_api_factory_;
};

#endif  // KAGOME_TEST_CORE_RUNTIME_RUNTIME_TEST_BASE_HPP

max_stars_repo_name: soramitsu/kagome

max_issues_repo_name: soramitsu/kagome

max_forks_repo_name: soramitsu/kagome