text:
#if defined IS_TEST_NET
#include <boost/test/unit_test.hpp>
#include <CreateCoin/chain/account_object.hpp>
#include <CreateCoin/protocol/CreateCoin_operations.hpp>
#include <CreateCoin/protocol/config.hpp>
#include <CreateCoin/plugins/transaction_status/transaction_status_plugin.hpp>
#include <CreateCoin/plugins/transaction_status/transaction_status_objects.hpp>
#include <CreateCoin/plugins/transaction_status_api/transaction_status_api_plugin.hpp>
#include <CreateCoin/plugins/transaction_status_api/transaction_status_api.hpp>

#include "../db_fixture/database_fixture.hpp"

using namespace CreateCoin::chain;
using namespace CreateCoin::protocol;

#define TRANSCATION_STATUS_TRACK_AFTER_BLOCK 1300
#define TRANSCATION_STATUS_TRACK_AFTER_BLOCK_STR BOOST_PP_STRINGIZE( TRANSCATION_STATUS_TRACK_AFTER_BLOCK )
#define TRANSACTION_STATUS_TEST_BLOCK_DEPTH 30
#define TRANSACTION_STATUS_TEST_BLOCK_DEPTH_STR BOOST_PP_STRINGIZE( TRANSACTION_STATUS_TEST_BLOCK_DEPTH )

BOOST_FIXTURE_TEST_SUITE( transaction_status, database_fixture );

BOOST_AUTO_TEST_CASE( transaction_status_test )
{
   using namespace CreateCoin::plugins::transaction_status;

   try
   {
      int argc = boost::unit_test::framework::master_test_suite().argc;
      char** argv = boost::unit_test::framework::master_test_suite().argv;

      for ( int i = 1; i < argc; i++ )
      {
         const std::string arg = argv[ i ];
         if ( arg == "--record-assert-trip" )
            fc::enable_record_assert_trip = true;
         if ( arg == "--show-test-names" )
            std::cout << "running test " << boost::unit_test::framework::current_test_case().p_name << std::endl;
      }

      appbase::app().register_plugin< transaction_status_plugin >();
      appbase::app().register_plugin< CreateCoin::plugins::transaction_status_api::transaction_status_api_plugin >();
      db_plugin = &appbase::app().register_plugin< CreateCoin::plugins::debug_node::debug_node_plugin >();
      init_account_pub_key = init_account_priv_key.get_public_key();

      // We create an argc/argv so that the transaction_status plugin can be initialized with a reasonable block depth
      int test_argc = 5;
      const char* test_argv[] = { boost::unit_test::framework::master_test_suite().argv[0],
                                  "--transaction-status-block-depth",
                                  TRANSACTION_STATUS_TEST_BLOCK_DEPTH_STR,
                                  "--transaction-status-track-after-block",
                                  TRANSCATION_STATUS_TRACK_AFTER_BLOCK_STR };

      db_plugin->logging = false;
      appbase::app().initialize<
         CreateCoin::plugins::transaction_status_api::transaction_status_api_plugin,
         CreateCoin::plugins::debug_node::debug_node_plugin >( test_argc, (char**)test_argv );

      db = &appbase::app().get_plugin< CreateCoin::plugins::chain::chain_plugin >().db();
      BOOST_REQUIRE( db );

      auto tx_status_api = &appbase::app().get_plugin< CreateCoin::plugins::transaction_status_api::transaction_status_api_plugin >();
      BOOST_REQUIRE( tx_status_api );

      auto tx_status = &appbase::app().get_plugin< CreateCoin::plugins::transaction_status::transaction_status_plugin >();
      BOOST_REQUIRE( tx_status );

      open_database();

      BOOST_REQUIRE( db->get_index< transaction_status_index >().indices().get< by_id >().empty() );
      BOOST_REQUIRE( db->get_index< transaction_status_index >().indices().get< by_trx_id >().empty() );
      BOOST_REQUIRE( db->get_index< transaction_status_index >().indices().get< by_block_num >().empty() );

      BOOST_REQUIRE( tx_status->state_is_valid() );

      generate_block();
      db->set_hardfork( CreateCoin_NUM_HARDFORKS );
      generate_block();

      vest( "initminer", 10000 );

      // Fill up the rest of the required miners
      for( int i = CreateCoin_NUM_INIT_MINERS; i < CreateCoin_MAX_WITNESSES; i++ )
      {
         account_create( CreateCoin_INIT_MINER_NAME + fc::to_string( i ), init_account_pub_key );
         fund( CreateCoin_INIT_MINER_NAME + fc::to_string( i ), CreateCoin_MIN_PRODUCER_REWARD.amount.value );
         witness_create( CreateCoin_INIT_MINER_NAME + fc::to_string( i ), init_account_priv_key, "foo.bar", init_account_pub_key, CreateCoin_MIN_PRODUCER_REWARD.amount );
      }

      validate_database();

      ACTORS( (alice)(bob) );
      generate_block();

      fund( "alice", ASSET( "1000.000 TESTS" ) );
      fund( "alice", ASSET( "1000.000 TBD" ) );
      fund( "bob", ASSET( "1000.000 TESTS" ) );

      generate_block();

      validate_database();

      BOOST_TEST_MESSAGE(" -- transaction status tracking test" );

      signed_transaction tx0;
      transfer_operation op0;
      auto tx0_expiration = db->head_block_time() + CreateCoin_MAX_TIME_UNTIL_EXPIRATION;

      op0.from = "alice";
      op0.to = "bob";
      op0.amount = ASSET( "5.000 TESTS" );

      // Create transaction 0
      tx0.operations.push_back( op0 );
      tx0.set_expiration( tx0_expiration );
      sign( tx0, alice_private_key );
      db->push_transaction( tx0, 0 );

      // Tracking should not be enabled until we have reached TRANSCATION_STATUS_TRACK_AFTER_BLOCK - ( CreateCoin_MAX_TIME_UNTIL_EXPIRATION / CreateCoin_BLOCK_INTERVAL ) blocks
      BOOST_REQUIRE( db->get_index< transaction_status_index >().indices().get< by_id >().empty() );
      BOOST_REQUIRE( db->get_index< transaction_status_index >().indices().get< by_trx_id >().empty() );
      BOOST_REQUIRE( db->get_index< transaction_status_index >().indices().get< by_block_num >().empty() );

      // Transaction 0 should not be tracked
      auto tso = db->find< transaction_status_object, by_trx_id >( tx0.id() );
      BOOST_REQUIRE( tso == nullptr );

      auto api_return = tx_status_api->api->find_transaction( { .transaction_id = tx0.id() } );
      BOOST_REQUIRE( api_return.status == unknown );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx0.id(), tx0_expiration  } );
      BOOST_REQUIRE( api_return.status == unknown );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      generate_blocks( TRANSCATION_STATUS_TRACK_AFTER_BLOCK - db->head_block_num() );

      signed_transaction tx1;
      transfer_operation op1;
      auto tx1_expiration = db->head_block_time() + CreateCoin_MAX_TIME_UNTIL_EXPIRATION;

      op1.from = "alice";
      op1.to = "bob";
      op1.amount = ASSET( "5.000 TESTS" );

      // Create transaction 1
      tx1.operations.push_back( op1 );
      tx1.set_expiration( tx1_expiration );
      sign( tx1, alice_private_key );
      db->push_transaction( tx1, 0 );

      // Transaction 1 exists in the mem pool
      tso = db->find< transaction_status_object, by_trx_id >( tx1.id() );
      BOOST_REQUIRE( tso != nullptr );
      BOOST_REQUIRE( tso->block_num == 0 );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx1.id() } );
      BOOST_REQUIRE( api_return.status == within_mempool );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx1.id(), tx1_expiration } );
      BOOST_REQUIRE( api_return.status == within_mempool );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      generate_block();

      /*
       * Test for two transactions in the same block
       */

      // Create transaction 2
      signed_transaction tx2;
      transfer_operation op2;
      auto tx2_expiration = db->head_block_time() + CreateCoin_MAX_TIME_UNTIL_EXPIRATION;

      op2.from = "alice";
      op2.to = "bob";
      op2.amount = ASSET( "5.000 TESTS" );

      tx2.operations.push_back( op2 );
      tx2.set_expiration( tx2_expiration );
      sign( tx2, alice_private_key );
      db->push_transaction( tx2, 0 );

      // Create transaction 3
      signed_transaction tx3;
      transfer_operation op3;
      auto tx3_expiration = db->head_block_time() + CreateCoin_MAX_TIME_UNTIL_EXPIRATION;

      op3.from = "bob";
      op3.to = "alice";
      op3.amount = ASSET( "5.000 TESTS" );

      tx3.operations.push_back( op3 );
      tx3.set_expiration( tx3_expiration );
      sign( tx3, bob_private_key );
      db->push_transaction( tx3, 0 );

      // Transaction 1 exists in a block
      tso = db->find< transaction_status_object, by_trx_id >( tx1.id() );
      BOOST_REQUIRE( tso != nullptr );
      BOOST_REQUIRE( tso->block_num == db->head_block_num() );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx1.id() } );
      BOOST_REQUIRE( api_return.status == within_reversible_block );
      BOOST_REQUIRE( api_return.block_num.valid() );
      BOOST_REQUIRE( api_return.block_num == db->head_block_num() );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx1.id(), tx1_expiration } );
      BOOST_REQUIRE( api_return.status == within_reversible_block );
      BOOST_REQUIRE( api_return.block_num.valid() );
      BOOST_REQUIRE( api_return.block_num == db->head_block_num() );

      // Transaction 2 exists in a mem pool
      tso = db->find< transaction_status_object, by_trx_id >( tx2.id() );
      BOOST_REQUIRE( tso != nullptr );
      BOOST_REQUIRE( tso->block_num == 0 );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx2.id() } );
      BOOST_REQUIRE( api_return.status == within_mempool );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx2.id(), tx2_expiration } );
      BOOST_REQUIRE( api_return.status == within_mempool );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      // Transaction 3 exists in a mem pool
      tso = db->find< transaction_status_object, by_trx_id >( tx3.id() );
      BOOST_REQUIRE( tso != nullptr );
      BOOST_REQUIRE( tso->block_num == 0 );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx3.id() } );
      BOOST_REQUIRE( api_return.status == within_mempool );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx3.id(), tx3_expiration } );
      BOOST_REQUIRE( api_return.status == within_mempool );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      BOOST_REQUIRE( tx_status->state_is_valid() );

      generate_blocks( TRANSACTION_STATUS_TEST_BLOCK_DEPTH );

      // Transaction 1 exists in a block
      tso = db->find< transaction_status_object, by_trx_id >( tx1.id() );
      BOOST_REQUIRE( tso != nullptr );
      BOOST_REQUIRE( tso->block_num > 0 );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx1.id() } );
      BOOST_REQUIRE( api_return.status == within_irreversible_block );
      BOOST_REQUIRE( *api_return.block_num > 0 );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx1.id(), tx1_expiration } );
      BOOST_REQUIRE( api_return.status == within_irreversible_block );
      BOOST_REQUIRE( *api_return.block_num > 0 );

      // Transaction 2 exists in a block
      tso = db->find< transaction_status_object, by_trx_id >( tx2.id() );
      BOOST_REQUIRE( tso != nullptr );
      BOOST_REQUIRE( tso->block_num > 0 );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx2.id() } );
      BOOST_REQUIRE( api_return.status == within_irreversible_block );
      BOOST_REQUIRE( api_return.block_num.valid() );
      BOOST_REQUIRE( *api_return.block_num > 0 );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx2.id(), tx2_expiration } );
      BOOST_REQUIRE( api_return.status == within_irreversible_block );
      BOOST_REQUIRE( api_return.block_num.valid() );
      BOOST_REQUIRE( *api_return.block_num > 0 );

      // Transaction 3 exists in a block
      tso = db->find< transaction_status_object, by_trx_id >( tx3.id() );
      BOOST_REQUIRE( tso != nullptr );
      BOOST_REQUIRE( tso->block_num > 0 );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx3.id() } );
      BOOST_REQUIRE( api_return.status == within_irreversible_block );
      BOOST_REQUIRE( api_return.block_num.valid() );
      BOOST_REQUIRE( *api_return.block_num > 0 );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx3.id(), tx3_expiration } );
      BOOST_REQUIRE( api_return.status == within_irreversible_block );
      BOOST_REQUIRE( api_return.block_num.valid() );
      BOOST_REQUIRE( *api_return.block_num > 0 );

      BOOST_REQUIRE( tx_status->state_is_valid() );

      generate_blocks( CreateCoin_MAX_TIME_UNTIL_EXPIRATION / CreateCoin_BLOCK_INTERVAL );

      // Transaction 1 is no longer tracked
      tso = db->find< transaction_status_object, by_trx_id >( tx1.id() );
      BOOST_REQUIRE( tso == nullptr );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx1.id() } );
      BOOST_REQUIRE( api_return.status == unknown );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx1.id(), tx1_expiration } );
      BOOST_REQUIRE( api_return.status == too_old );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      // Transaction 2 exists in a block
      tso = db->find< transaction_status_object, by_trx_id >( tx2.id() );
      BOOST_REQUIRE( tso != nullptr );
      BOOST_REQUIRE( tso->block_num > 0 );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx2.id() } );
      BOOST_REQUIRE( api_return.status == within_irreversible_block );
      BOOST_REQUIRE( api_return.block_num.valid() );
      BOOST_REQUIRE( *api_return.block_num > 0 );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx2.id(), tx2_expiration } );
      BOOST_REQUIRE( api_return.status == within_irreversible_block );
      BOOST_REQUIRE( api_return.block_num.valid() );
      BOOST_REQUIRE( *api_return.block_num > 0 );

      // Transaction 3 exists in a block
      tso = db->find< transaction_status_object, by_trx_id >( tx3.id() );
      BOOST_REQUIRE( tso != nullptr );
      BOOST_REQUIRE( tso->block_num > 0 );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx3.id() } );
      BOOST_REQUIRE( api_return.status == within_irreversible_block );
      BOOST_REQUIRE( api_return.block_num.valid() );
      BOOST_REQUIRE( *api_return.block_num > 0 );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx3.id(), tx3_expiration } );
      BOOST_REQUIRE( api_return.status == within_irreversible_block );
      BOOST_REQUIRE( api_return.block_num.valid() );
      BOOST_REQUIRE( *api_return.block_num > 0 );

      BOOST_REQUIRE( tx_status->state_is_valid() );

      generate_block();

      // Transaction 2 is no longer tracked
      tso = db->find< transaction_status_object, by_trx_id >( tx2.id() );
      BOOST_REQUIRE( tso == nullptr );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx2.id() } );
      BOOST_REQUIRE( api_return.status == unknown );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx2.id(), tx2_expiration } );
      BOOST_REQUIRE( api_return.status == too_old );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      // Transaction 3 is no longer tracked
      tso = db->find< transaction_status_object, by_trx_id >( tx3.id() );
      BOOST_REQUIRE( tso == nullptr );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx3.id() } );
      BOOST_REQUIRE( api_return.status == unknown );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx3.id(), tx3_expiration } );
      BOOST_REQUIRE( api_return.status == too_old );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      // At this point our index should be empty
      BOOST_REQUIRE( db->get_index< transaction_status_index >().indices().get< by_id >().empty() );
      BOOST_REQUIRE( db->get_index< transaction_status_index >().indices().get< by_trx_id >().empty() );
      BOOST_REQUIRE( db->get_index< transaction_status_index >().indices().get< by_block_num >().empty() );

      BOOST_REQUIRE( tx_status->state_is_valid() );

      generate_block();

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx1.id() } );
      BOOST_REQUIRE( api_return.status == unknown );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx1.id(), tx1_expiration } );
      BOOST_REQUIRE( api_return.status == too_old );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx2.id() } );
      BOOST_REQUIRE( api_return.status == unknown );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      api_return = tx_status_api->api->find_transaction( { .transaction_id = tx2.id(), tx2_expiration } );
      BOOST_REQUIRE( api_return.status == too_old );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      /**
       * Testing transactions that do not exist, but expirations are provided
       */
      BOOST_TEST_MESSAGE( " -- transaction status expiration test" );

      // The time of our last irreversible block
      auto lib_time = db->fetch_block_by_number( db->get_dynamic_global_properties().last_irreversible_block_num )->timestamp;
      api_return = tx_status_api->api->find_transaction( { .transaction_id = transaction_id_type(), .expiration = lib_time } );
      BOOST_REQUIRE( api_return.status == expired_irreversible );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      // One second after our last irreversible block
      auto after_lib_time = lib_time + fc::seconds(1);
      api_return = tx_status_api->api->find_transaction( { .transaction_id = transaction_id_type(), after_lib_time } );
      BOOST_REQUIRE( api_return.status == expired_reversible );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      // One second before our block depth
      auto old_time = db->fetch_block_by_number( db->head_block_num() - TRANSACTION_STATUS_TEST_BLOCK_DEPTH + 1 )->timestamp - fc::seconds(1);
      api_return = tx_status_api->api->find_transaction( { .transaction_id = transaction_id_type(), old_time } );
      BOOST_REQUIRE( api_return.status == too_old );
      BOOST_REQUIRE( api_return.block_num.valid() == false );

      BOOST_REQUIRE( tx_status->state_is_valid() );

      /**
       * Testing transaction status plugin state
       */
      BOOST_TEST_MESSAGE( " -- transaction status state test" );

      // Create transaction 4
      signed_transaction tx4;
      transfer_operation op4;
      auto tx4_expiration = db->head_block_time() + CreateCoin_MAX_TIME_UNTIL_EXPIRATION;

      op4.from = "alice";
      op4.to = "bob";
      op4.amount = ASSET( "5.000 TESTS" );

      tx4.operations.push_back( op4 );
      tx4.set_expiration( tx4_expiration );
      sign( tx4, alice_private_key );
      db->push_transaction( tx4, 0 );

      generate_block();

      BOOST_REQUIRE( tx_status->state_is_valid() );

      const auto& tx_status_obj = db->get< transaction_status_object, by_trx_id >( tx4.id() );
      db->remove( tx_status_obj );

      // Upper bound of transaction status state should cause state to be invalid
      BOOST_REQUIRE( tx_status->state_is_valid() == false );

      tx_status->rebuild_state();

      BOOST_REQUIRE( tx_status->state_is_valid() );

      // Create transaction 5
      signed_transaction tx5;
      transfer_operation op5;
      auto tx5_expiration = db->head_block_time() + CreateCoin_MAX_TIME_UNTIL_EXPIRATION;

      op5.from = "alice";
      op5.to = "bob";
      op5.amount = ASSET( "5.000 TESTS" );

      tx5.operations.push_back( op5 );
      tx5.set_expiration( tx5_expiration );
      sign( tx5, alice_private_key );
      db->push_transaction( tx5, 0 );

      generate_blocks( TRANSACTION_STATUS_TEST_BLOCK_DEPTH + ( CreateCoin_MAX_TIME_UNTIL_EXPIRATION / CreateCoin_BLOCK_INTERVAL ) - 1 );

      const auto& tx_status_obj2 = db->get< transaction_status_object, by_trx_id >( tx5.id() );
      db->remove( tx_status_obj2 );

      // Lower bound of transaction status state should cause state to be invalid
      BOOST_REQUIRE( tx_status->state_is_valid() == false );

      tx_status->rebuild_state();

      BOOST_REQUIRE( tx_status->state_is_valid() );
   }
   FC_LOG_AND_RETHROW()
}

BOOST_AUTO_TEST_SUITE_END()
#endif

max_stars_repo_name: DunnCreativeSS/ccd

max_issues_repo_name: DunnCreativeSS/ccd

max_forks_repo_name: DunnCreativeSS/ccd


text:
#include <mpi.h>
#include <stdio.h>
#include <Eigen/Dense>
#include <Eigen/Sparse>

#include <iostream>
#include <fstream>

#include <fstream>
#include <string>
#include <algorithm>
#include <random>
#include <chrono>
#include <memory>
#include <cmath>
#include <stdlib.h>

#include <getopt.h>

#include <unsupported/Eigen/SparseExtra>

#include "linop.h"
#include "macau_mpi.h"
#include "macau.h"

extern "C" {
  #include "dsparse.h"
}

using namespace Eigen;
using namespace std;

void usage() {
   printf("Usage:\n");
   printf("  macau_mpi --train <train_file> --row-features <feature-file> [options]\n");
   printf("Optional:\n");
   printf("  --test    test_file  test data (for computing RMSE)\n");
   printf("  --burnin        200  number of samples to discard\n");
   printf("  --nsamples      800  number of samples to collect\n");
   printf("  --num-latent     96  number of latent dimensions\n");
   printf("  --precision     5.0  precision of observations\n");
   printf("  --lambda-beta  10.0  initial value of lambda beta\n");
   printf("  --tol          1e-6  tolerance for CG\n");
   printf("  --output    results  prefix for result files\n");
}

bool file_exists(const char *fileName)
{
   std::ifstream infile(fileName);
   return infile.good();
}

int get_num_omp_threads() {
   int nt = 0;
#pragma omp parallel
   {
#pragma omp single
      {
         nt = omp_get_num_threads();
      }
   }
   return nt;
}

void die(std::string message, int world_rank) {
   if (world_rank == 0) {
      std::cout << message;
   }
   MPI_Finalize();
   exit(1);
}

std::unique_ptr<SparseFeat> load_bcsr(const char* filename) {
   SparseBinaryMatrix* A = read_sbm(filename);
   SparseFeat* sf = new SparseFeat(A->nrow, A->ncol, A->nnz, A->rows, A->cols);
   free_sbm(A);
   std::unique_ptr<SparseFeat> sf_ptr(sf);
   return sf_ptr;
}

// var for MPI
int* rhs_for_rank = NULL;
double* rec     = NULL;
int* sendcounts = NULL;
int* displs     = NULL;

int main(int argc, char** argv) {
   // Initialize the MPI environment
   MPI_Init(NULL, NULL);
   // Get the number of processes
   int world_size, world_rank;
   MPI_Comm_size(MPI_COMM_WORLD, &world_size);
   MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

   // Get the name of the processor
   char processor_name[MPI_MAX_PROCESSOR_NAME];
   int name_len;
   MPI_Get_processor_name(processor_name, &name_len);

   char* fname_train         = NULL;
   char* fname_test          = NULL;
   char* fname_row_features  = NULL;
   std::string output_prefix = std::string("result");
   double precision   = 5.0;
   double lambda_beta = 10.0;
   double tol         = 1e-6;
   int burnin     = 200;
   int nsamples   = 800;
   int num_latent = 96;

   // reading command line arguments
   while (1) {
      static struct option long_options[] =
      {
         {"train",      required_argument, 0, 't'},
         {"test",       required_argument, 0, 'e'},
         {"row-features", required_argument, 0, 'r'},
         {"precision",  required_argument, 0, 'p'},
         {"burnin",     required_argument, 0, 'b'},
         {"nsamples",   required_argument, 0, 'n'},
         {"output",     required_argument, 0, 'o'},
         {"num-latent", required_argument, 0, 'l'},
         {"lambda-beta",required_argument, 0, 'a'},
         {"tol",        required_argument, 0, 'c'},
         {0, 0, 0, 0}
      };
      int option_index = 0;
      int c = getopt_long(argc, argv, "t:e:r:p:b:n:o:a:c:", long_options, &option_index);
      if (c == -1)
         break;

      switch (c) {
         case 'a': lambda_beta   = strtod(optarg, NULL); break;
         case 'b': burnin        = strtol(optarg, NULL, 10); break;
         case 'c': tol           = atof(optarg); break;
         case 'e': fname_test    = optarg; break;
         case 'l': num_latent    = strtol(optarg, NULL, 10); break;
         case 'n': nsamples      = strtol(optarg, NULL, 10); break;
         case 'o': output_prefix = std::string(optarg); break;
         case 'p': precision     = strtod(optarg, NULL); break;
         case 'r': fname_row_features = optarg; break;
         case 't': fname_train = optarg; break;
         case '?':
         default:
           if (world_rank == 0)
              usage();
           MPI_Finalize();
           exit(1);
      }
   }
   if (fname_train == NULL || fname_row_features == NULL) {
      if (world_rank == 0) {
         printf("[ERROR]\nMissing parameters '--matrix' or '--row-features'.\n");
         usage();
      }
      MPI_Finalize();
      exit(1);
   }
   if (world_rank == 0) {
      printf("Train data:    '%s'\n", fname_train);
      printf("Test data:     '%s'\n", fname_test==NULL ?"" :fname_test);
      printf("Row features:  '%s'\n", fname_row_features);
      printf("Output prefix: '%s'\n", output_prefix.c_str());
      printf("Burn-in:       %d\n", burnin);
      printf("Samples:       %d\n", nsamples);
      printf("Num-latents:   %d\n", num_latent);
      printf("Precision:     %.1f\n", precision);
      printf("Lambda-beta:   %.1f\n", lambda_beta);
      printf("tol:           %.1e\n", tol);
   }
   if ( ! file_exists(fname_train) ) {
      die(std::string("[ERROR]\nTrain data file '") + fname_train + "' not found.\n", world_rank);
   }
   if ( ! file_exists(fname_row_features) ) {
      die(std::string("[ERROR]\nRow feature file '") + fname_row_features + "' not found.\n", world_rank);
   }
   if ( (fname_test != NULL) && ! file_exists(fname_test) ) {
      die(std::string("[ERROR]\nTest data file '") + fname_test + "' not found.\n", world_rank);
   }

   int n_omp_threads = get_num_omp_threads();
   rhs_for_rank = new int[world_size];
   split_work_mpi(num_latent, world_size, rhs_for_rank);

   // Print off a hello world message
   printf("Processor %s, rank %d"
          " out of %d processors using %d OpenMP threads for %d RHS.\n",
          processor_name, world_rank, world_size, n_omp_threads, rhs_for_rank[world_rank]);

   // Step 1. Loading data
   //std::unique_ptr<SparseFeat> row_features = load_bcsr(fname_row_features);
   auto row_features = load_bcsr(fname_row_features);
   if (world_rank == 0) {
      printf("Row features:   [%d x %d].\n", row_features->rows(), row_features->cols());
   }
   sendcounts = new int[world_size];
   displs     = new int[world_size];
   int sum = 0;
   for (int n = 0; n < world_size; n++) {
      sendcounts[n] = rhs_for_rank[n] * row_features->cols();
      displs[n]     = sum;
      sum          += sendcounts[n];
   }
   rec = new double[sendcounts[world_rank]];

   SparseDoubleMatrix* Y     = NULL;
   SparseDoubleMatrix* Ytest = NULL;

   Macau* macau = new Macau(num_latent);
   macau->setPrecision(precision);
   macau->setSamples(burnin, nsamples);
   macau->setVerbose(true);

   // 1) row prior with side information
   int nfeat    = row_features->rows();
   auto prior_u = new MacauPrior<SparseFeat>(num_latent, row_features, false);
   prior_u->setLambdaBeta(lambda_beta);
   prior_u->setTol(tol);
   auto prior_v = new BPMFPrior(num_latent);

   // 2) activity data (read_sdm)
   Y = read_sdm(fname_train);

   if (nfeat != Y->nrow) {
      die(std::string("[ERROR]\nNumber of rows (" +
                      std::to_string(Y->nrow) +
                      ") in train must be equal to number of rows in row-features (" +
                      std::to_string(row_features->rows()) +
                      ")."),
          world_rank);
   }

   // 3) create Macau object
   macau->setRelationData(Y->rows, Y->cols, Y->vals, Y->nnz, Y->nrow, Y->ncol);

   // test data
   if (fname_test != NULL) {
      Ytest = read_sdm(fname_test);
      if (Ytest->nrow != Y->nrow || Ytest->ncol != Y->ncol) {
         die(std::string("[ERROR]\nSize of train (") +
                         std::to_string(Y->nrow) + " x " +
                         std::to_string(Y->ncol) + ") must be equal to size of test (" +
                         std::to_string(Ytest->nrow) + " x " +
                         std::to_string(Ytest->ncol) + ").",
             world_rank);
      }
      macau->setRelationDataTest(Ytest->rows, Ytest->cols, Ytest->vals, Ytest->nnz, Ytest->nrow, Ytest->ncol);
   }
   std::unique_ptr<ILatentPrior> u_ptr(prior_u);
   std::unique_ptr<ILatentPrior> v_ptr(prior_v);
   macau->addPrior( u_ptr );
   macau->addPrior( v_ptr );

   if (world_rank == 0) {
      printf("Training data:  %ld [%d x %d]\n", Y->nnz, Y->nrow, Y->ncol);
      if (Ytest != NULL) {
         printf("Test data:      %ld [%d x %d]\n", Ytest->nnz, Ytest->nrow, Ytest->ncol);
      } else {
         printf("Test data:      --\n");
      }
   }

   run_macau_mpi(macau, world_rank);

   // save results
   if (world_rank == 0) {
      VectorXd yhat_raw     = macau->getPredictions();
      VectorXd yhat_sd_raw  = macau->getStds();
      MatrixXd testdata_raw = macau->getTestData();

      std::string fname_pred = output_prefix + "-predictions.csv";
      std::ofstream predfile;
      predfile.open(fname_pred);
      predfile << "row,col,y,y_pred,y_pred_std\n";
      for (int i = 0; i < yhat_raw.size(); i++) {
         predfile << to_string( (int)testdata_raw(i,0) );
         predfile << "," << to_string( (int)testdata_raw(i,1) );
         predfile << "," << to_string( testdata_raw(i,2) );
         predfile << "," << to_string( yhat_raw(i) );
         predfile << "," << to_string( yhat_sd_raw(i) );
         predfile << "\n";
      }
      predfile.close();
      printf("Saved predictions into '%s'.\n", fname_pred.c_str());
   }

   // Finalize the MPI environment.
   delete macau;
   MPI_Finalize();
   return 0;
}

void run_macau_mpi(
      Macau* macau,
      int world_rank)
{
   /* adapted from Macau.run() */
   macau->init();
   if (world_rank == 0 && macau->verbose) {
      std::cout << "Sampling" << std::endl;
   }

   const int num_rows = macau->Y.rows();
   const int num_cols = macau->Y.cols();
   macau->predictions     = VectorXd::Zero( macau->Ytest.nonZeros() );
   macau->predictions_var = VectorXd::Zero( macau->Ytest.nonZeros() );

   auto start = tick();
   for (int i = 0; i < macau->burnin + macau->nsamples; i++) {
      if (world_rank == 0 && macau->verbose && i == macau->burnin) {
         printf(" ====== Burn-in complete, averaging samples ====== \n");
      }
      auto starti = tick();

      if (world_rank == 0) {
         // sample latent vectors
         macau->noise->sample_latents(macau->priors[0], *macau->samples[0], macau->Yt, macau->mean_rating, *macau->samples[1], macau->num_latent);
         macau->noise->sample_latents(macau->priors[1], *macau->samples[1], macau->Y,  macau->mean_rating, *macau->samples[0], macau->num_latent);
         //macau->priors[0]->sample_latents(*macau->samples[0], macau->Yt, macau->mean_rating, *macau->samples[1], macau->alpha, macau->num_latent);
         //macau->priors[1]->sample_latents(*macau->samples[1], macau->Y,  macau->mean_rating, *macau->samples[0], macau->alpha, macau->num_latent);
      }

      // Sample hyperparams
      update_prior_mpi( *(MacauPrior<SparseFeat>*) macau->priors[0].get(), *macau->samples[0], world_rank);
      if (world_rank == 0) {
         macau->priors[1]->update_prior(*macau->samples[1]);

         auto eval = eval_rmse(macau->Ytest, (i < macau->burnin) ? 0 : (i - macau->burnin), macau->predictions, macau->predictions_var,
                               *macau->samples[1], *macau->samples[0], macau->mean_rating);

         auto endi = tick();
         auto elapsed = endi - start;
         double samples_per_sec = (i + 1) * (num_rows + num_cols) / elapsed;
         double elapsedi = endi - starti;

         if (macau->verbose) {
           macau->printStatus(i, eval.first, eval.second, elapsedi, samples_per_sec);
         }
         macau->rmse_test = eval.second;
      }
   }
}

void update_prior_mpi(MacauPrior<SparseFeat> &prior, const Eigen::MatrixXd &U, int world_rank) {
   if (world_rank == 0) {
      // residual (Uhat is later overwritten):
      prior.Uhat.noalias() = U - prior.Uhat;
      MatrixXd BBt = A_mul_At_combo(prior.beta);
      // sampling Gaussian
      std::tie(prior.mu, prior.Lambda) = CondNormalWishart(prior.Uhat, prior.mu0, prior.b0, prior.WI + prior.lambda_beta * BBt, prior.df + prior.beta.cols());
   }
   sample_beta_mpi(prior, U, world_rank);
   if (world_rank == 0) {
      compute_uhat(prior.Uhat, *prior.F, prior.beta);
      prior.lambda_beta = sample_lambda_beta(prior.beta, prior.Lambda, prior.lambda_beta_nu0, prior.lambda_beta_mu0);
   }
}

void sample_beta_mpi(MacauPrior<SparseFeat> &prior, const Eigen::MatrixXd &U, int world_rank) {
   const int num_latent = prior.beta.rows();
   const int num_feat = prior.beta.cols();
   MatrixXd Ft_y(0,0);

   if (world_rank == 0) {
      // Ft_y = (U .- mu + Normal(0, Lambda^-1)) * F + sqrt(lambda_beta) * Normal(0, Lambda^-1)
      // Ft_y is [ D x F ] matrix
      MatrixXd tmp = (U + MvNormal_prec_omp(prior.Lambda, U.cols())).colwise() - prior.mu;
      Ft_y = A_mul_B(tmp, *prior.F);
      MatrixXd tmp2 = MvNormal_prec_omp(prior.Lambda, num_feat);

      #pragma omp parallel for schedule(static)
      for (int f = 0; f < num_feat; f++) {
         for (int d = 0; d < num_latent; d++) {
            Ft_y(d, f) += sqrt(prior.lambda_beta) * tmp2(d, f);
         }
      }
      Ft_y.transposeInPlace();
   }

   MPI_Bcast(& prior.lambda_beta, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
   MPI_Bcast(& prior.tol,         1, MPI_DOUBLE, 0, MPI_COMM_WORLD);

   // sending Ft_y
   MPI_Scatterv(Ft_y.data(), sendcounts, displs, MPI_DOUBLE, rec, sendcounts[world_rank], MPI_DOUBLE, 0, MPI_COMM_WORLD);
   int nrhs = rhs_for_rank[world_rank];
   MatrixXd RHS(nrhs, num_feat), result(nrhs, num_feat);

#pragma omp parallel for schedule(static)
   for (int f = 0; f < num_feat; f++) {
      for (int d = 0; d < nrhs; d++) {
         RHS(d, f) = rec[f + d * num_feat];
      }
   }
   // solving
   solve_blockcg(result, *prior.F, prior.lambda_beta, RHS, prior.tol, 32, 8);
   result.transposeInPlace();
   MPI_Gatherv(result.data(), nrhs*num_feat, MPI_DOUBLE, Ft_y.data(), sendcounts, displs, MPI_DOUBLE, 0, MPI_COMM_WORLD);
   if (world_rank == 0) {
      //prior.beta = Ft_y.transpose();
#pragma omp parallel for schedule(static)
      for (int f = 0; f < num_feat; f++) {
         for (int d = 0; d < num_latent; d++) {
            prior.beta(d, f) = Ft_y(f, d);
         }
      }
   }
}

ext: cpp

max_stars_repo_name: edebrouwer/macau

max_issues_repo_name: edebrouwer/macau

max_forks_repo_name: edebrouwer/macau


text:
#include <SystemContext.h>
#include <boost/program_options.hpp>

namespace po = boost::program_options;

int main(int argc, char** argv) {
    SystemContext& systemContext = SystemContext::GenerateContext(argc, argv);
    systemContext.StartAPI();
    return 0;
}

max_stars_repo_name: squarebat/Message-Broker-System

max_issues_repo_name: squarebat/Message-Broker-System

max_forks_repo_name: squarebat/Message-Broker-System


text:
#include <fiberchain/protocol/asset.hpp>
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>

/*

The bounds on asset serialization are as follows:

index : field
0     : decimals
1..6  : symbol
   7  : \0
*/

namespace fiberchain { namespace protocol {
      typedef boost::multiprecision::int128_t  int128_t;

      uint8_t asset::decimals()const
      {
         auto a = (const char*)&symbol;
         uint8_t result = uint8_t( a[0] );
         FC_ASSERT( result < 15 );
         return result;
      }

      void asset::set_decimals(uint8_t d)
      {
         FC_ASSERT( d < 15 );
         auto a = (char*)&symbol;
         a[0] = d;
      }

      void asset::change_fraction_size(uint8_t size)
      {
         share_type old_precision = precision();
         set_decimals(size);
         share_type new_precision = precision();

         if(new_precision > old_precision) {
            share_type multiply_rate = new_precision / old_precision;
            amount *= multiply_rate;
         } else {
            share_type divide_rate = old_precision / new_precision;
            amount /= divide_rate;
         }
      }

      std::string asset::symbol_name()const
      {
         auto a = (const char*)&symbol;
         FC_ASSERT( a[7] == 0 );
         return &a[1];
      }

      int64_t asset::precision()const
      {
         static int64_t table[] = {
                           1, 10, 100, 1000, 10000,
                           100000, 1000000, 10000000, 100000000ll,
                           1000000000ll, 10000000000ll,
                           100000000000ll, 1000000000000ll,
                           10000000000000ll, 100000000000000ll
                         };
         uint8_t d = decimals();
         return table[ d ];
      }

      string asset::to_string()const
      {
         int64_t prec = precision();
         string result = fc::to_string(amount.value / prec);
         if( prec > 1 )
         {
            auto fract = amount.value % prec;
            // prec is a power of ten, so for example when working with
            // 7.005 we have fract = 5, prec = 1000.  So prec+fract=1005
            // has the correct number of zeros and we can simply trim the
            // leading 1.
            result += "." + fc::to_string(prec + fract).erase(0,1);
         }
         return result + " " + symbol_name();
      }

      asset asset::from_string( const string& from )
      {
         try
         {
            string s = fc::trim( from );
            auto space_pos = s.find( " " );
            auto dot_pos = s.find( "." );

            FC_ASSERT( space_pos != std::string::npos );

            asset result;
            result.symbol = uint64_t(0);
            auto sy = (char*)&result.symbol;

            if( dot_pos != std::string::npos )
            {
               FC_ASSERT( space_pos > dot_pos );

               auto intpart = s.substr( 0, dot_pos );
               auto fractpart = "1" + s.substr( dot_pos + 1, space_pos - dot_pos - 1 );
               result.set_decimals( fractpart.size() - 1 );

               result.amount = fc::to_int64( intpart );
               result.amount.value *= result.precision();
               result.amount.value += fc::to_int64( fractpart );
               result.amount.value -= result.precision();
            }
            else
            {
               auto intpart = s.substr( 0, space_pos );
               result.amount = fc::to_int64( intpart );
               result.set_decimals( 0 );
            }
            auto symbol = s.substr( space_pos + 1 );
            size_t symbol_size = symbol.size();

            if( symbol_size > 0 )
            {
               FC_ASSERT( symbol_size <= 6 );
               memcpy( sy+1, symbol.c_str(), symbol_size );
            }

            return result;
         }
         FC_CAPTURE_AND_RETHROW( (from) )
      }

      bool operator == ( const price& a, const price& b )
      {
         if( std::tie( a.base.symbol, a.quote.symbol ) != std::tie( b.base.symbol, b.quote.symbol ) )
             return false;

         const auto amult = uint128_t( b.quote.amount.value ) * a.base.amount.value;
         const auto bmult = uint128_t( a.quote.amount.value ) * b.base.amount.value;

         return amult == bmult;
      }

      bool operator < ( const price& a, const price& b )
      {
         if( a.base.symbol < b.base.symbol ) return true;
         if( a.base.symbol > b.base.symbol ) return false;
         if( a.quote.symbol < b.quote.symbol ) return true;
         if( a.quote.symbol > b.quote.symbol ) return false;

         const auto amult = uint128_t( b.quote.amount.value ) * a.base.amount.value;
         const auto bmult = uint128_t( a.quote.amount.value ) * b.base.amount.value;

         return amult < bmult;
      }

      bool operator <= ( const price& a, const price& b )
      {
         return (a == b) || (a < b);
      }

      bool operator != ( const price& a, const price& b )
      {
         return !(a == b);
      }

      bool operator > ( const price& a, const price& b )
      {
         return !(a <= b);
      }

      bool operator >= ( const price& a, const price& b )
      {
         return !(a < b);
      }

      asset operator * ( const asset& a, const price& b )
      {
         if( a.symbol_name() == b.base.symbol_name() )
         {
            FC_ASSERT( b.base.amount.value > 0 );
            uint128_t result = (uint128_t(a.amount.value) * b.quote.amount.value)/b.base.amount.value;
            FC_ASSERT( result.hi == 0 );
            return asset( result.to_uint64(), b.quote.symbol );
         }
         else if( a.symbol_name() == b.quote.symbol_name() )
         {
            FC_ASSERT( b.quote.amount.value > 0 );
            uint128_t result = (uint128_t(a.amount.value) * b.base.amount.value)/b.quote.amount.value;
            FC_ASSERT( result.hi == 0 );
            return asset( result.to_uint64(), b.base.symbol );
         }
         FC_THROW_EXCEPTION( fc::assert_exception, "invalid asset * price", ("asset",a)("price",b) );
      }

      price operator / ( const asset& base, const asset& quote )
      { try {
         FC_ASSERT( base.symbol_name() != quote.symbol_name() );
         return price{ base, quote };
      } FC_CAPTURE_AND_RETHROW( (base)(quote) ) }

      price price::max( asset_symbol_type base, asset_symbol_type quote ) { return asset( share_type(FIBERCHAIN_MAX_SHARE_SUPPLY), base ) / asset( share_type(1), quote); }
      price price::min( asset_symbol_type base, asset_symbol_type quote ) { return asset( 1, base ) / asset( FIBERCHAIN_MAX_SHARE_SUPPLY, quote); }

      bool price::is_null() const { return *this == price(); }

      void price::validate() const
      { try {
         FC_ASSERT( base.amount > share_type(0) );
         FC_ASSERT( quote.amount > share_type(0) );
         FC_ASSERT( base.symbol_name() != quote.symbol_name() );
      } FC_CAPTURE_AND_RETHROW( (base)(quote) ) }

} } // fiberchain::protocol

max_stars_repo_name: FiberChain/FiberChain

max_issues_repo_name: FiberChain/FiberChain

max_forks_repo_name: FiberChain/FiberChain


text:
//////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 KDE Laboratory, University of Tsukuba, Tsukuba, Japan.            //
//                                                                                //
// The JsSpinnerSPE/StreamingCube and the accompanying materials are made available     //
// under the terms of Eclipse Public License v1.0 which accompanies this distribution   //
// and is available at <https://eclipse.org/org/documents/epl-v10.php>                  //
//////////////////////////////////////////////////////////////////////////////////////////

#include "../Common/stdafx.h"
#include "../IO/SpecifiedInputRateStreamInput.h"
#include "../Schema/SchemaInterpreter.h"
#include "../Utility/DocumentIdentifierGenerator.h"
#include "../Utility/TimestampGenerator.h"
#include "../Utility/SleepUtility.h"
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <time.h>
#include "../Configure/ConfigureManager.h"
//int SpecifiedInputRateStreamInput::ignoredInputNumber = 0;

SpecifiedInputRateStreamInput::SpecifiedInputRateStreamInput(boost::shared_ptr<JsonSchema> schema) : IStreamInput(schema)
{
	this->schema = schema;
	SchemaInterpreter::generateRandomDocumentBySchema(this->schema,	this->bufferedDocument);
	this->bufferedElementNumber = 0;
	this->totalElementNumber = 0;
}

SpecifiedInputRateStreamInput::~SpecifiedInputRateStreamInput(void) {
	//	std::cout<<this->getId()<<" total element number : "<< totalElementNumber<<std::endl;
}

void SpecifiedInputRateStreamInput::getNextElement(Element& element) {
	//	boost::recursive_mutex::scoped_lock lock(m_oMutex);
	//	this->bufferedElementNumber--;
	//	assert(this->bufferedElementNumber>=0);
	//	lock.unlock();
	//	assert(bufferedElementNumber>0);

	bufferedElementNumber--;
	element.id = DocumentIdentifierGenerator::generateNewDocumentIdentifier();
	element.mark = PLUS_MARK;
	element.timestamp = TimestampGenerator::getCurrentTime();
	//element.document = this->bufferedDocument;

	if (this->getId() == "performanceTestStream1") {
		DocumentBuilder builder;
		builder.append("A", "hello");
		builder.append("B", 1);
		Document obj = builder.obj();
		element.document = obj;
	}
	if (this->getId() == "performanceTestStream2") {
		DocumentBuilder builder;
		//builder.append("A",rand()%10);
		builder.append("A", "hello");
		builder.append("C", 1);
		Document obj = builder.obj();
		element.document = obj;
	}
	if (this->getId() == "performanceTestStream3") {
		DocumentBuilder builder;
		builder.append("A", "hello");
		builder.append("D", 1);
		Document obj = builder.obj();
		element.document = obj;
	}
	if (this->getId() == "performanceTestStream4") {
		DocumentBuilder builder;
		builder.append("A", "hello");
		builder.append("E", 1);
		Document obj = builder.obj();
		element.document = obj;
	}
	if (this->getId() == "salesFact") {
		//SchemaInterpreter::generateRandomDocumentBySchema(this->schema,	this->bufferedDocument);
		//element.document = this->bufferedDocument;
		DocumentBuilder builder;
		builder.append("prodID", 125);
		builder.append("suppID", 130);
		builder.append("salesAmountFACT", 100);
		Document obj = builder.obj();
		element.document = obj;
	}
	if (this->getId() == "salesFact5Dim") {
		//SchemaInterpreter::generateRandomDocumentBySchema(this->schema,	this->bufferedDocument);
		//element.document = this->bufferedDocument;
		DocumentBuilder builder;

		/* initialize random seed: */

		//Formula: min + (rand() % (int)(max - min + 1))
		int randInt = 125 + (rand() % (int)(10));
		builder.append("prodID", randInt);

		randInt = 125 + (rand() % (int)(10));
		builder.append("suppID", randInt);

		randInt = 140 + (rand() % (int)(10));
		builder.append("promoID", randInt);

		builder.append("custID", 150);
		builder.append("storeID", 160);

		randInt = 100 + (rand() % (int)(900));
		builder.append("salesAmountFACT", randInt);

		Document obj = builder.obj();
		element.document = obj;
	}
	if (this->getId() == "salesFact5DimWNamesWrapper")
	{
		//sleep(1);
		DocumentBuilder builder;

		//Formula: min + (rand() % (int)(max - min + 1))
		int randInt = 1 + (rand() % (int)(250));
		builder.append("prodID", randInt);

		randInt = 1 + (rand() % (int)(4));
		builder.append("suppID", randInt);

		randInt = 1 + (rand() % (int)(5));
		builder.append("promoID", randInt);

		std::stringstream randDim;

        randInt = 1 + (rand() % (int)(10));
		builder.append("custID", randInt);
		randDim << "Customer" << randInt;
		builder.append("custName", randDim.str());

		randDim.clear();
        randDim.str(std::string());

		randInt = 1 + (rand() % (int)(2));
		builder.append("storeID", randInt);
		randDim << "Store" << randInt;
		builder.append("storeArea", randDim.str());

		/*
		builder.append("custID", 150);
		builder.append("custName", "Suzuki");

		builder.append("storeID", 160);
		builder.append("storeArea", "Tsukuba");
		*/

		randInt = 100 + (rand() % (int)(900));
		builder.append("salesAmountFACT", randInt);

		Document obj = builder.obj();
		element.document = obj;
	}
	if (this->getId() == "salesFact6DimWNamesWrapper")
	{
		//sleep(1);
		DocumentBuilder builder;

		//Formula: min + (rand() % (int)(max - min + 1))
		int randInt = 1 + (rand() % (int)(250));
		builder.append("prodID", randInt);

		randInt = 1 + (rand() % (int)(4));
		builder.append("suppID", randInt);

		randInt = 1 + (rand() % (int)(5));
		builder.append("promoID", randInt);

		std::stringstream randDim;

        randInt = 1 + (rand() % (int)(7));
		builder.append("custID", randInt);
		randDim << "Customer" << randInt;
		builder.append("custName", randDim.str());

		randDim.clear();
        randDim.str(std::string());

		randInt = 1 + (rand() % (int)(2));
		builder.append("storeID", randInt);
		randDim << "Store" << randInt;
		builder.append("storeArea", randDim.str());

		randDim.clear();
        randDim.str(std::string());

		randInt = 1 + (rand() % (int)(4));
		builder.append("salesPersonID", randInt); // SalesPerson
		randDim << "SP" << randInt;
		builder.append("salesPersonName", randDim.str());

		/*
		builder.append("custID", 150);
		builder.append("custName", "Suzuki");

		builder.append("storeID", 160);
		builder.append("storeArea", "Tsukuba");
		*/

		randInt = 100 + (rand() % (int)(900));
		builder.append("salesAmountFACT", randInt);

		Document obj = builder.obj();
		element.document = obj;
	}
	if (this->getId() == "salesFactPreJoined6DWrapper")
	{
		//sleep(1);
		DocumentBuilder builder;
		std::stringstream randDim;

		//Formula: min + (rand() % (int)(max - min + 1))
		int randInt = 1 + (rand() % (int)(250));
		builder.append("prodID", randInt);
		randDim << "Product" << randInt;
		builder.append("prodName", randDim.str());

		randDim.clear();
        randDim.str(std::string());

		randInt = 1 + (rand() % (int)(4));
		builder.append("suppID", randInt);
		randDim << "Supplier" << randInt;
		builder.append("suppName", randDim.str());

		randDim.clear();
        randDim.str(std::string());

		randInt = 1 + (rand() % (int)(5));
		builder.append("promoID", randInt);
		randDim << "Promotion" << randInt;
		builder.append("promoName", randDim.str());

		randDim.clear();
        randDim.str(std::string());

        randInt = 1 + (rand() % (int)(7));
		builder.append("custID", randInt);
		randDim << "Customer" << randInt;
		builder.append("custName", randDim.str());

		randDim.clear();
        randDim.str(std::string());

		randInt = 1 + (rand() % (int)(2));
		builder.append("storeID", randInt);
		randDim << "Store" << randInt;
		builder.append("storeArea", randDim.str());

		randDim.clear();
        randDim.str(std::string());

		randInt = 1 + (rand() % (int)(4));
		builder.append("salesPersonID", randInt); // SalesPerson
		randDim << "SP" << randInt;
		builder.append("salesPersonName", randDim.str());

		/*
		builder.append("custID", 150);
		builder.append("custName", "Suzuki");

		builder.append("storeID", 160);
		builder.append("storeArea", "Tsukuba");
		*/

		randInt = 100 + (rand() % (int)(900));
		builder.append("salesAmountFACT", randInt);

		Document obj = builder.obj();
		element.document = obj;
	}
	if (this->getId() == "salesFact4DimTPCH")
	{
		//sleep(1);
		DocumentBuilder builder;

		//Formula: min + (rand() % (int)(max - min + 1))
		int randInt = 1 + (rand() % (int)(2000));
		builder.append("partKey", randInt);

		randInt = 1 + (rand() % (int)(100));
		builder.append("suppKey", randInt);

		randInt = 1 + (rand() % (int)(1500));
		builder.append("custKey", randInt);

		builder.append("storeKey", 150);
		builder.append("storeArea", "Japan");

		randInt = 100 + (rand() % (int)(900));
		builder.append("salesQtyFACT", randInt);

		Document obj = builder.obj();
		element.document = obj;
	}
	if (this->getId() == "eventStream")
    {}
	if (this->getId() == "performanceTestStreamGroup")
	{
		SchemaInterpreter::generateRandomDocumentBySchema(this->schema,	this->bufferedDocument);
		element.document = this->bufferedDocument;
	}

	element.document.getOwned();
	//std::cout << "element from specifiedInputRate" << element << std::endl;
}

bool SpecifiedInputRateStreamInput::isEmpty() {
	//	boost::recursive_mutex::scoped_lock lock(m_oMutex);
	//	bool bl = false;
	//	if(bufferedElementNumber==0)
	//	{
	//	bl = true;
	//	}
	//	lock.unlock();
	//	return bl;
	return bufferedElementNumber == 0;
}

void SpecifiedInputRateStreamInput::initial() {
	// A new thread is generated for each data stream source
	//std::cout << "SpecifiedInputRateStreamInput thread initialization" << std::endl;

	int sleepMiliSeconds = 20;
	//std::cout<<"creating one thread+++++++++++++++"<<std::endl;
	boost::function<void(int)> memberFunctionWrapper(boost::bind(
			&SpecifiedInputRateStreamInput::generateInput, this, _1));
	boost::thread downloadThread((boost::bind(memberFunctionWrapper,
			sleepMiliSeconds)));

}

void SpecifiedInputRateStreamInput::generateInput(int sleepMicroSeconds) {
	//std::string inputRateStr = ConfigureManager::getInstance()->getConfigureValue("stream1_input_rate");
	//double inputRate = 0.0;
	//inputRate = atof(inputRateStr.c_str());

	while (1) {

		if (this->getId() == "performanceTestStream1") {
			//boost::recursive_mutex::scoped_lock lock(m_oMutex);
			this->bufferedElementNumber++;
			//lock.unlock();
			this->totalElementNumber++;
			std::stringstream ss;
			//
			std::string inputRateStr = ConfigureManager::getInstance()->getConfigureValue("stream1_input_rate");
			double inputRate = 0.0;
			inputRate = atof(inputRateStr.c_str());
			//std::cout << this->getId() << " inputRate " << inputRate << std::endl;

			//ss << inputRateStr;
			//std::cout << this->getId() << " inputRate " << inputRateStr.c_str() << std::endl;
			//inputRate = boost::lexical_cast<int>(ss);
			//inputRate = atof(inputRateStr.c_str());
			//ss >> inputRate;

			// For the sake of distributed experiments only
			static int counter = 0;
            if(inputRate < 10000 & counter < 50)
            {
                    counter++;
                    inputRate = 10000;
                    //std::cout << "Modified for the sake of distributed proc. only " << counter << std::endl;
            }

			if (inputRate == 0.11) {
				SleepUtility::sleepMicroSecond(9000000);//1  tuple/9s
			} else if (inputRate == 0.14) {
				SleepUtility::sleepMicroSecond(7000000);//1  tuple/7s
			} else if (inputRate == 0.2) {
				SleepUtility::sleepMicroSecond(5000000);//0.2  tuple/s; 1 tuple/5s
			} else if (inputRate == 0.25) {
				SleepUtility::sleepMicroSecond(4000000);// 1 tuple/4s
			} else if (inputRate == 0.33) {
				SleepUtility::sleepMicroSecond(3000000);// 1 tuple/3s
			} else if (inputRate == 0.4) {
				SleepUtility::sleepMicroSecond(2600000);//0.4  tuple/s
			} else if (inputRate == 0.6) {
				SleepUtility::sleepMicroSecond(1676667);//0.6  tuple/s
			} else if (inputRate == 0.5) {
				SleepUtility::sleepMicroSecond(2000000);// 1 tuple/2s
			} else if (inputRate == 0.8) {
				SleepUtility::sleepMicroSecond(1260000);//0.8  tuple/s
			} else if (inputRate == 1) {
				SleepUtility::sleepMicroSecond(1000000);//1  tuple/s
			} else if (inputRate == 1.2) {
				SleepUtility::sleepMicroSecond(843333);//1.2  tuple/s
			} else if (inputRate == 10) {
				SleepUtility::sleepMicroSecond(100000);//10  tuple/s
			} else if (inputRate == 100) {
				SleepUtility::sleepMicroSecond(10000);//100  tuple/s
			} else if (inputRate == 200) {
				SleepUtility::sleepMicroSecond(5000);//200  tuple/s
			} else if (inputRate == 300) {
				SleepUtility::sleepMicroSecond(3333);//300  tuple/s
			} else if (inputRate == 400) {
				SleepUtility::sleepMicroSecond(2500);//400  tuple/s
			} else if (inputRate == 500) {
				SleepUtility::sleepMicroSecond(2000);//500  tuple/s
			} else if (inputRate == 600) {
				SleepUtility::sleepMicroSecond(1666);//600  tuple/s
			} else if (inputRate == 700) {
				SleepUtility::sleepMicroSecond(1428);//700  tuple/s
			} else if (inputRate == 800) {
				SleepUtility::sleepMicroSecond(1250);//800  tuple/s
			} else if (inputRate == 900) {
				SleepUtility::sleepMicroSecond(1111);//900  tuple/s
			} else if (inputRate == 1000) {
				SleepUtility::sleepMicroSecond(910);//1000  tuple/s
			} else if (inputRate == 10000) {
				SleepUtility::sleepMicroSecond(30);//10000  tuple/s
			} else {
				std::cout << "error input specification1" << std::endl;
				assert(false);
			}
			//SleepUtility::sleepMicroSecond(900000);//1  tuple/s
			//SleepUtility::sleepMicroSecond(20000);//50  tuple/s
			//SleepUtility::sleepMicroSecond(10000);//100 tuple/s
			//SleepUtility::sleepMicroSecond(6666);//150 tuple/s
			//SleepUtility::sleepMicroSecond(5000);//200 tuple/s
		} else if (this->getId() == "performanceTestStream2") {
			//boost::recursive_mutex::scoped_lock lock(m_oMutex);
			this->bufferedElementNumber++;
			//lock.unlock();
			this->totalElementNumber++;
			std::stringstream ss;
			// Q
			std::string inputRateStr =
					ConfigureManager::getInstance()->getConfigureValue(
							"stream2_input_rate");
			//int inputRate ;
			//ss << inputRateStr;
			//ss >> inputRate;

			double inputRate = 0.0;
			inputRate = atof(inputRateStr.c_str());
			//std::cout << this->getId() << " inputRate " << inputRate << std::endl;

			if (inputRate == 0.2) {
				SleepUtility::sleepMicroSecond(5100000);//0.2  tuple/s
			} else if (inputRate == 0.4) {
				SleepUtility::sleepMicroSecond(2600000);//0.4  tuple/s
			} else if (inputRate == 0.6) {
				SleepUtility::sleepMicroSecond(1676667);//0.6  tuple/s
			} else if (inputRate == 0.8) {
				SleepUtility::sleepMicroSecond(1260000);//0.8  tuple/s
			} else if (inputRate == 1) {
				SleepUtility::sleepMicroSecond(1000000);//1  tuple/s
			} else if (inputRate == 1.2) {
				SleepUtility::sleepMicroSecond(843333);//1.2  tuple/s
			} else if (inputRate == 10) {
				SleepUtility::sleepMicroSecond(100000);//10  tuple/s
			} else if (inputRate == 100) {
				SleepUtility::sleepMicroSecond(10000);//100  tuple/s
			} else if (inputRate == 1000) {
				SleepUtility::sleepMicroSecond(910);//1000  tuple/s
			} else if (inputRate == 10000) {
				SleepUtility::sleepMicroSecond(30);//10000  tuple/s
			} else if (inputRate == 20000) {
				static int k1 = 0; // will be initialized only once
				if (totalElementNumber > 100 * k1) { // sleep only after generation of multiples of 100
					k1++;
					SleepUtility::sleepMicroSecond(4600); //
				}
			} else if (inputRate == 30000) {
				static int k2 = 0; // will be initialized only once
				if (totalElementNumber > 100 * k2) { // sleep only after generation of multiples of 100
					k2++;
					SleepUtility::sleepMicroSecond(3000); //
				}
			} else if (inputRate == 40000) {
				static int k3 = 0; // will be initialized only once
				if (totalElementNumber > 100 * k3) { // sleep only after generation of multiples of 100
					k3++;
					SleepUtility::sleepMicroSecond(2180); //
				}
			} else if (inputRate == 50000) {
				static int k4 = 0; // will be initialized only once
				if (totalElementNumber > 100 * k4) { // sleep only after generation of multiples of 100
					k4++;
					SleepUtility::sleepMicroSecond(1675); //
				}
			} else if (inputRate == 60000) {
				static int k5 = 0; // will be initialized only once
				if (totalElementNumber > 100 * k5) { // sleep only after generation of multiples of 100
					k5++;
					SleepUtility::sleepMicroSecond(1340); //
				}
			} else if (inputRate == 70000) {
				static int k6 = 0; // will be initialized only once
				if (totalElementNumber > 100 * k6) { // sleep only after generation of multiples of 100
					k6++;
					SleepUtility::sleepMicroSecond(1115); //
				}
			} else if (inputRate == 80000) {
				static int k7 = 0; // will be initialized only once
				if (totalElementNumber > 100 * k7) { // sleep only after generation of multiples of 100
					k7++;
					SleepUtility::sleepMicroSecond(935); //
				}
			} else if (inputRate == 90000) {
				static int k8 = 0; // will be initialized only once
				if (totalElementNumber > 100 * k8) { // sleep only after generation of multiples of 100
					k8++;
					SleepUtility::sleepMicroSecond(805); //
				}
			} else if (inputRate == 100000) {
				static int k11 = 0; // will be initialized only once
				if (totalElementNumber > 100 * k11) { // sleep only after generation of multiples of 100
					k11++;
					SleepUtility::sleepMicroSecond(700); // 100,000 tuples/s
				}
			} else if (inputRate == 200000) {
				static int k12 = 0;
				if (totalElementNumber > 100 * k12) {
					k12++;
					SleepUtility::sleepMicroSecond(240); // 200,000 tuples/s
				}
			} else if (inputRate == 300000) {
				static int k13 = 0;
				if (totalElementNumber > 100 * k13) {
					k13++;
					SleepUtility::sleepMicroSecond(200); // 300,000 tuples/s
				}
			} else if (inputRate == 400000) {
				static int k14 = 0;
				if (totalElementNumber > 100 * k14) {
					k14++;
					SleepUtility::sleepMicroSecond(155); // 400,000 tuples/s
				}
			} else if (inputRate == 500000) {
				static int k15 = 0;
				if (totalElementNumber > 100 * k15) {
					k15++;
					SleepUtility::sleepMicroSecond(110); // 500,000 tuples/s
				}
			} else if (inputRate == 600000) {
				static int k16 = 0;
				if (totalElementNumber > 100 * k16) {
					k16++;
					SleepUtility::sleepMicroSecond(75); // 600,000 tuples/s
				}
			} else if (inputRate == 700000) {
				static int k17 = 0;
				if (totalElementNumber > 100 * k17) {
					k17++;
					SleepUtility::sleepMicroSecond(50); // 700,000 tuples/s
				}
			} else if (inputRate == 800000) {
				static int k18 = 0;
				if (totalElementNumber > 100 * k18) {
					k18++;
					SleepUtility::sleepMicroSecond(35); // 800,000 tuples/s
				}
			} else if (inputRate == 900000) {
				static int k19 = 0;
				if (totalElementNumber > 100 * k19) {
					k19++;
					SleepUtility::sleepMicroSecond(32); // 900,000 tuples/s
				}
			} else if (inputRate == 1000000) {
				static int k20 = 0;
				if (totalElementNumber > 100 * k20) {
					k20++;
					SleepUtility::sleepMicroSecond(30); // 1,000,000 tuples/s
				}
			}
		} else if (this->getId() == "performanceTestStream3") {
			this->bufferedElementNumber++;
			this->totalElementNumber++;
			std::stringstream ss;
			std::string inputRateStr =
					ConfigureManager::getInstance()->getConfigureValue(
							"stream3_input_rate");
			//int inputRate ;
			//ss << inputRateStr;
			//ss >> inputRate;
			double inputRate = 0.0;
			inputRate = atof(inputRateStr.c_str());
			//std::cout << this->getId() << " inputRate " << inputRate << std::endl;

			if (inputRate == 0.11) {
				SleepUtility::sleepMicroSecond(9000000);//1  tuple/9s
			} else if (inputRate == 0.14) {
				SleepUtility::sleepMicroSecond(7000000);//1  tuple/7s
			} else if (inputRate == 0.2) {
				SleepUtility::sleepMicroSecond(5000000);//0.2  tuple/s
			} else if (inputRate == 0.25) {
				SleepUtility::sleepMicroSecond(4000000);// 1 tuple/4s
			} else if (inputRate == 0.33) {
				SleepUtility::sleepMicroSecond(3000000);// 1 tuple/3s
			} else if (inputRate == 0.4) {
				SleepUtility::sleepMicroSecond(2600000);//0.4  tuple/s
			} else if (inputRate == 0.6) {
				SleepUtility::sleepMicroSecond(1676667);//0.6  tuple/s
			} else if (inputRate == 0.5) {
				SleepUtility::sleepMicroSecond(2000000);// 1 tuple/2s
			} else if (inputRate == 0.8) {
				SleepUtility::sleepMicroSecond(1260000);//0.8  tuple/s
			} else if (inputRate == 1) {
				SleepUtility::sleepMicroSecond(1000000);//1  tuple/s
			} else if (inputRate == 1.2) {
				SleepUtility::sleepMicroSecond(843333);//1.2  tuple/s
			} else if (inputRate == 10) {
				SleepUtility::sleepMicroSecond(100000);//10  tuple/s
			} else if (inputRate == 100) {
				SleepUtility::sleepMicroSecond(10000);//100  tuple/s
			} else if (inputRate == 1000) {
				SleepUtility::sleepMicroSecond(910);//1000  tuple/s
			} else if (inputRate == 10000) {
				SleepUtility::sleepMicroSecond(30);//10000  tuple/s
			} else if (inputRate == 100000) {
				static int k11 = 0;
				if (totalElementNumber > 100 * k11) {
					k11++;
					SleepUtility::sleepMicroSecond(700); // 100,000 tuples/s
				}
			} else if (inputRate == 200000) {
				static int k12 = 0;
				if (totalElementNumber > 100 * k12) {
					k12++;
					SleepUtility::sleepMicroSecond(240); // 200,000 tuples/s
				}
			} else if (inputRate == 300000) {
				static int k13 = 0;
				if (totalElementNumber > 100 * k13) {
					k13++;
					SleepUtility::sleepMicroSecond(200); // 300,000 tuples/s
				}
			} else if (inputRate == 400000) {
				static int k14 = 0;
				if (totalElementNumber > 100 * k14) {
					k14++;
					SleepUtility::sleepMicroSecond(155); // 400,000 tuples/s
				}
			} else if (inputRate == 500000) {
				static int k15 = 0;
				if (totalElementNumber > 100 * k15) {
					k15++;
					SleepUtility::sleepMicroSecond(110); // 500,000 tuples/s
				}
			} else if (inputRate == 600000) {
				static int k16 = 0;
				if (totalElementNumber > 100 * k16) {
					k16++;
					SleepUtility::sleepMicroSecond(75); // 600,000 tuples/s
				}
			} else if (inputRate == 700000) {
				static int k17 = 0;
				if (totalElementNumber > 100 * k17) {
					k17++;
					SleepUtility::sleepMicroSecond(50); // 700,000 tuples/s
				}
			} else if (inputRate == 800000) {
				static int k18 = 0;
				if (totalElementNumber > 100 * k18) {
					k18++;
					SleepUtility::sleepMicroSecond(35); // 800,000 tuples/s
				}
			} else if (inputRate == 900000) {
				static int k19 = 0;
				if (totalElementNumber > 100 * k19) {
					k19++;
					SleepUtility::sleepMicroSecond(32); // 900,000 tuples/s
				}
			} else if (inputRate == 1000000) {
				static int k10 = 0;
				if (totalElementNumber > 100 * k10) {
					k10++;
					SleepUtility::sleepMicroSecond(30); // 1,000,000 tuples/s
				}
			}else if (inputRate == 100000) {
				static int k11 = 0;
				if (totalElementNumber > 100 * k11) {
					k11++;
					SleepUtility::sleepMicroSecond(700); // 100,000 tuples/s
				}
			} else if (inputRate == 200000) {
				static int k12 = 0;
				if (totalElementNumber > 100 * k12) {
					k12++;
					SleepUtility::sleepMicroSecond(240); // 200,000 tuples/s
				}
			} else if (inputRate == 300000) {
				static int k13 = 0;
				if (totalElementNumber > 100 * k13) {
					k13++;
					SleepUtility::sleepMicroSecond(200); // 300,000 tuples/s
				}
			} else if (inputRate == 400000) {
				static int k14 = 0;
				if (totalElementNumber > 100 * k14) {
					k14++;
					SleepUtility::sleepMicroSecond(155); // 400,000 tuples/s
				}
			} else if (inputRate == 500000) {
				static int k15 = 0;
				if (totalElementNumber > 100 * k15) {
					k15++;
					SleepUtility::sleepMicroSecond(110); // 500,000 tuples/s
				}
			} else if (inputRate == 600000) {
				static int k16 = 0;
				if (totalElementNumber > 100 * k16) {
					k16++;
					SleepUtility::sleepMicroSecond(75); // 600,000 tuples/s
				}
			} else if (inputRate == 700000) {
				static int k17 = 0;
				if (totalElementNumber > 100 * k17) {
					k17++;
					SleepUtility::sleepMicroSecond(50); // 700,000 tuples/s
				}
			} else if (inputRate == 800000) {
				static int k18 = 0;
				if (totalElementNumber > 100 * k18) {
					k18++;
					SleepUtility::sleepMicroSecond(35); // 800,000 tuples/s
				}
			} else if (inputRate == 900000) {
				static int k19 = 0;
				if (totalElementNumber > 100 * k19) {
					k19++;
					SleepUtility::sleepMicroSecond(32); // 900,000 tuples/s
				}
			} else if (inputRate == 1000000) {
				static int k10 = 0;
				if (totalElementNumber > 100 * k10) {
					k10++;
					SleepUtility::sleepMicroSecond(30); // 1,000,000 tuples/s
				}
			}
			else {
				std::cout << "error input specification3" << std::endl;
				//sleep(1);
				assert(false);
			}
		} else if (this->getId() == "performanceTestStream4") {
			this->bufferedElementNumber++;
			this->totalElementNumber++;
			std::stringstream ss;
			std::string inputRateStr =
					ConfigureManager::getInstance()->getConfigureValue(
							"stream4_input_rate");
			//int inputRate ;
			//ss << inputRateStr;
			//ss >> inputRate;
			double inputRate = 0.0;
			inputRate = atof(inputRateStr.c_str());
			//std::cout << this->getId() << " inputRate " << inputRate << std::endl;

			if (inputRate == 0.2) {
				SleepUtility::sleepMicroSecond(5100000);//0.2  tuple/s
			} else if (inputRate == 0.4) {
				SleepUtility::sleepMicroSecond(2600000);//0.4  tuple/s
			} else if (inputRate == 0.6) {
				SleepUtility::sleepMicroSecond(1676667);//0.6  tuple/s
			} else if (inputRate == 0.8) {
				SleepUtility::sleepMicroSecond(1260000);//0.8  tuple/s
			} else if (inputRate == 1) {
				SleepUtility::sleepMicroSecond(1000000);//1  tuple/s
			} else if (inputRate == 1.2) {
				SleepUtility::sleepMicroSecond(843333);//1.2  tuple/s
			} else if (inputRate == 10) {
				SleepUtility::sleepMicroSecond(100000);//10  tuple/s
			} else if (inputRate == 100) {
				SleepUtility::sleepMicroSecond(10000);//100  tuple/s
			} else if (inputRate == 1000) {
				SleepUtility::sleepMicroSecond(910);//1000  tuple/s
			} else if (inputRate == 10000) {
				SleepUtility::sleepMicroSecond(30);//10000  tuple/s
			} else if (inputRate = 100000) {

				static int k2 = 0;
				if (totalElementNumber > 100 * k2) {
					k2++;
					SleepUtility::sleepMicroSecond(600); // 100,000 tuples/s
				}
			}
		} else if (this->getId() == "eventStream") {
			//boost::recursive_mutex::scoped_lock lock(m_oMutex);
			this->bufferedElementNumber++;
			//lock.unlock();
			this->totalElementNumber++;
			// P
			//SleepUtility::sleepMicroSecond(900000);//1  tuple/s
			//SleepUtility::sleepMicroSecond(20000);//50  tuple/s
			SleepUtility::sleepMicroSecond(10000);//100 tuple/s
			//SleepUtility::sleepMicroSecond(6666);//150 tuple/s
			//SleepUtility::sleepMicroSecond(5000);//200 tuple/s
		} else if (this->getId() == "performanceTestStreamGroup") {

			boost::recursive_mutex::scoped_lock lock(m_oMutex);
			this->bufferedElementNumber++;

			lock.unlock();
			this->totalElementNumber++;
			SleepUtility::sleepMicroSecond(1000); //1000tuple/s

		} else if (this->getId() == "salesFact" || this->getId() == "salesFact5Dim" || this->getId() == "salesFact5DimWNamesWrapper" || this->getId() == "salesFact6DimWNamesWrapper" || this->getId() == "salesFact4DimTPCH" || this->getId() == "salesFactPreJoined6DWrapper") {

			boost::recursive_mutex::scoped_lock lock(m_oMutex);
			this->bufferedElementNumber++;

			lock.unlock();
			this->totalElementNumber++;
			SleepUtility::sleepMicroSecond(1000); //1000tuple/s

		}
	}
}
/*
 //original function by wang
 //void SpecifiedInputRateStreamInput::generateInput(int sleepMicroSeconds)
 //{
 //	while(1)
 //	{
 //
 //		if(this->getId() == "performanceTestStream1")
 //		{
 //	 		//boost::recursive_mutex::scoped_lock lock(m_oMutex);
 //			this->bufferedElementNumber++;
 //			//lock.unlock();
 //			this->totalElementNumber++;
 //		//	SleepUtility::sleepMicroSecond(10000);//tuple/s
 //		//	SleepUtility::sleepMicroSecond(5000);//tuple/s
 //		//	SleepUtility::sleepMicroSecond(3333);//tuple/s
 //		//	SleepUtility::sleepMicroSecond(2500);//tuple/s
 //		//	SleepUtility::sleepMicroSecond(2000);//tuple/s
 //		//	SleepUtility::sleepMicroSecond(1666);//tuple/s
 //		//	SleepUtility::sleepMicroSecond(1428);//tuple/s
 //		//	SleepUtility::sleepMicroSecond(1250);//tuple/s
 //		//	SleepUtility::sleepMicroSecond(1111);//tuple/s
 //			SleepUtility::sleepMicroSecond(1000);//tuple/s
 //		}
 //		else if(this->getId() == "performanceTestStream2")
 //		{
 //	 		//boost::recursive_mutex::scoped_lock lock(m_oMutex);
 //			this->bufferedElementNumber++;
 //			//lock.unlock();
 //			this->totalElementNumber++;
 //			static int k = 0;
 //			if(totalElementNumber>100*k)
 //			{
 //				k++;
 //		//		SleepUtility::sleepMicroSecond(900);   100,000 tuples/s
 //		//		SleepUtility::sleepMicroSecond(400);   200,000 tuples/s
 //		//		SleepUtility::sleepMicroSecond(240);   300,000 tuples/s
 //		//		SleepUtility::sleepMicroSecond(155);   400,000 tuples/s
 //		//		SleepUtility::sleepMicroSecond(110);   500,000 tuples/s
 //		//		SleepUtility::sleepMicroSecond(75);   600,000 tuples/s
 //		//		SleepUtility::sleepMicroSecond(50);   700,000 tuples/s
 //		//		SleepUtility::sleepMicroSecond(35);   800,000 tuples/s
 //		//		SleepUtility::sleepMicroSecond(32);   900,000 tuples/s
 //				SleepUtility::sleepMicroSecond(30);   1,000,000 tuples/s
 //			}
 //		}
 //	}
 //}
 */

/*
 //Used by salman for experiments
 #include "../Common/stdafx.h"
 #include "../IO/SpecifiedInputRateStreamInput.h"
 #include "../Schema/SchemaInterpreter.h"
 #include "../Utility/DocumentIdentifierGenerator.h"
 #include "../Utility/TimestampGenerator.h"
 #include "../Utility/SleepUtility.h"
 #include <boost/thread.hpp>
 #include <boost/bind.hpp>
 #include <boost/function.hpp>
 int SpecifiedInputRateStreamInput::ignoredInputNumber = 0;
 SpecifiedInputRateStreamInput::SpecifiedInputRateStreamInput(boost::shared_ptr<JsonSchema> schema):IStreamInput(schema)
 {
 this->schema = schema;
 SchemaInterpreter::generateRandomDocumentBySchema(this->schema,this->bufferedDocument);
 this->bufferedElementNumber = 0;
 this->totalElementNumber = 0;
 }

 SpecifiedInputRateStreamInput::~SpecifiedInputRateStreamInput(void)
 {
 std::cout<<this->getId()<<" total element number : "<< totalElementNumber<<std::endl;
 }

 void SpecifiedInputRateStreamInput::getNextElement(Element& element)
 {
 boost::recursive_mutex::scoped_lock lock(m_oMutex);
 this->bufferedElementNumber--;
 assert(this->bufferedElementNumber>=0);
 lock.unlock();
 element.id = DocumentIdentifierGenerator::generateNewDocumentIdentifier();
 element.mark = PLUS_MARK;
 element.timestamp = TimestampGenerator::getCurrentTime();
 element.document = this->bufferedDocument;
 element.document.getOwned();
 }

 bool SpecifiedInputRateStreamInput::isEmpty()
 {
 boost::recursive_mutex::scoped_lock lock(m_oMutex);
 bool bl = false;
 if(bufferedElementNumber==0)
 {
 bl = true;
 }
 lock.unlock();
 return bl;
 }

 void SpecifiedInputRateStreamInput::initial()
 {
 int sleepMiliSeconds = 20;
 //std::cout<<"creating one thread+++++++++++++++"<<std::endl;
 boost::function<void (int)> memberFunctionWrapper(boost::bind(&SpecifiedInputRateStreamInput::generateInput, this, _1));
 boost::thread downloadThread ((boost::bind(memberFunctionWrapper, sleepMiliSeconds)));
 }

 void SpecifiedInputRateStreamInput::generateInput(int sleepMiliSeconds)
 {
 while(1)
 {
 if(this->getId() == "performanceTestStream1")
 {
 boost::recursive_mutex::scoped_lock lock(m_oMutex);
 this->bufferedElementNumber++;
 lock.unlock();
 this->totalElementNumber++;
 //SleepUtility::sleepMicroSecond(1000);//tuple/s
 // If sleep 1 sec (usleep(1000000)), 1 tuple is generated per second
 // If sleep 0.1 sec (usleep(100000)), 10 tuples are generated per second
 // If sleep 0.01 sec (usleep(10000)), 100 tuples are generated per second
 //usleep(11000000); //0.1
 //usleep(5100000);  //0.2
 //usleep(2500000); //0.5
 //usleep(1550000); //0.7
 //usleep(1250000); //0.9
 //usleep(1100000); // 1
 //usleep(1000000); //1.1
 //usleep(850000); //1.3
 //usleep(100000); //10
 usleep(10000); //100

 }
 else if(this->getId() == "performanceTestStream2")
 {
 boost::recursive_mutex::scoped_lock lock(m_oMutex);
 this->bufferedElementNumber++;
 lock.unlock();
 totalElementNumber++;

 static int k = 0;
 // sleep after generating every 100 tuples
 if(totalElementNumber>100*k)
 {
 k++;
 //SleepUtility::sleepMicroSecond(100);
 usleep(100);
 }

 }
 }
 }
 */

max_stars_repo_name: salmanahmedshaikh/StreamingCube

max_issues_repo_name: salmanahmedshaikh/StreamingCube

max_forks_repo_name: salmanahmedshaikh/StreamingCube


text:
/**************************************************************************************************/
// PNGpp copyright 2017 Foster Brereton. See LICENSE.txt for license details.
/**************************************************************************************************/

#ifndef PNGPP_PNG_HPP__
#define PNGPP_PNG_HPP__

// stdc++
#include <fstream>
#include <boost/thread/future.hpp>

// libpng
#include <png.h>

// zlib
#include <zlib.h>

// application
#include <pngpp/async.hpp>
#include <pngpp/files.hpp>
#include <pngpp/image.hpp>

/**************************************************************************************************/

namespace pngpp {

/**************************************************************************************************/

image_t read_png(const path_t& path);

/**************************************************************************************************/
// "save" connotes "to disk" more than "write" does (which could also be going to memory).
enum class save_mode { one, mid, max };

struct save_options_t {
    save_mode _mode{save_mode::one};
    int       _one_z_compression{Z_BEST_COMPRESSION};
    int       _one_z_strategy{Z_FILTERED};
    int       _one_png_filter{PNG_ALL_FILTERS};
};

// returns the size of the saved file in bytes.
future<std::size_t> save_png(const image_t&        image,
                             const path_t&         path,
                             const save_options_t& options);

/**************************************************************************************************/

} // namespace pngpp

/**************************************************************************************************/

#endif // PNGPP_PNG_HPP__

/**************************************************************************************************/

ext: hpp

max_stars_repo_name: fosterbrereton/pngpp

max_issues_repo_name: fosterbrereton/pngpp

max_forks_repo_name: fosterbrereton/pngpp


text:
//
// Copyright (c) 2011-2012 Krzysztof Jusiak (krzysztof at jusiak dot net)
//
// 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 QFSM_FRONT_FUSION_TRANSITIONTABLE_HPP
#define QFSM_FRONT_FUSION_TRANSITIONTABLE_HPP

#include <boost/typeof/typeof.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/push_back.hpp>
#include "QFsm/Aux/Pool/Pool.hpp"
#include "QFsm/Aux/Pool/PushBack.hpp"
#include "QFsm/Front/Operations/None.hpp"
#include "QFsm/Front/Operations/Transition.hpp"
#include "QFsm/Front/Fusion/Event.hpp"
#include "QFsm/Front/Fusion/Detail/GuardProxy.hpp"
#include "QFsm/Front/Fusion/Detail/ActionProxy.hpp"
#include "QFsm/Front/Fusion/Detail/CompositeProxy.hpp"

namespace QFsm
{
namespace Front
{
namespace Fusion
{

template
<
    typename TInitialState = boost::mpl::_1,
    typename THistory = boost::mpl::vector0<>,
    typename TTransitionTable = boost::mpl::vector0<>,
    typename TPool = QFsm::Aux::Pool::Pool< boost::mpl::vector0<> >
>
class TransitionTable
{
public:
    typedef TTransitionTable type;
    typedef TInitialState InitialState;
    typedef TPool Pool;

   explicit TransitionTable(const TPool& p_pool = TPool())
        : m_pool(p_pool)
    { }

    template<typename TStateInit, typename TEvent, typename TStateTarget> TransitionTable
    <
        InitialState,
        typename boost::mpl::push_back
        <
            TTransitionTable,
            Operations::Transition
            <
                TStateInit, TEvent, TStateTarget,
                Detail::ActionProxy<typename boost::mpl::size<TTransitionTable>::type, TEvent>,
                Detail::GuardProxy<typename boost::mpl::size<TTransitionTable>::type, TEvent>
            >
        >::type,
        QFsm::Aux::Pool::Pool
        <
            typename boost::mpl::push_back
            <
                typename boost::mpl::push_back
                <
                    typename TPool::type,
                    Detail::ActionProxy<typename boost::mpl::size<TTransitionTable>::type, TEvent>
                >::type,
                Detail::GuardProxy<typename boost::mpl::size<TTransitionTable>::type, TEvent>
            >::type
        >
    >
    transition(const TStateInit&, Event<TEvent> p_event, const TStateTarget&)
    {
        typedef boost::shared_ptr< Detail::ActionProxy<typename boost::mpl::size<TTransitionTable>::type, TEvent> > Action;
        typedef boost::shared_ptr< Detail::GuardProxy<typename boost::mpl::size<TTransitionTable>::type, TEvent> > Guard;
        typedef typename QFsm::Aux::Pool::Pool<typename boost::mpl::push_back<typename TPool::type, typename Action::value_type>::type> Pool1;
        typedef typename QFsm::Aux::Pool::Pool<typename boost::mpl::push_back<typename boost::mpl::push_back<typename TPool::type, typename Action::value_type>::type, typename Guard::value_type>::type> Pool2;

        Action l_action(new typename Action::value_type(p_event.action()));
        Guard l_guard(new typename Guard::value_type(p_event.guard()));

        Pool1 l_pool1 = QFsm::Aux::Pool::pushBack(m_pool, l_action);
        Pool2 l_pool = QFsm::Aux::Pool::pushBack(l_pool1, l_guard);

        TransitionTable
        <
            InitialState,
            typename boost::mpl::push_back
            <
                TTransitionTable,
                Operations::Transition<TStateInit, TEvent, TStateTarget, typename Action::value_type, typename Guard::value_type>
            >::type,
            Pool2
        >
        l_table(l_pool);

        return l_table;
    }

#if 0
    template<typename TStateInit, typename TEvent> TransitionTable
    <
        InitialState,
        typename boost::mpl::push_back
        <
            TTransitionTable,
            Operations::Transition
            <
                TStateInit, TEvent, Operations::None,
                Detail::ActionProxy<typename boost::mpl::size<TTransitionTable>::type, TEvent>,
                Detail::GuardProxy<typename boost::mpl::size<TTransitionTable>::type, TEvent>
            >
        >::type,
        typename boost::fusion::result_of::push_back
        <
            const typename boost::fusion::result_of::push_back
            <
                const TPool,
                boost::shared_ptr< Detail::ActionProxy<typename boost::mpl::size<TTransitionTable>::type, TEvent> >
            >::type,
            boost::shared_ptr< Detail::GuardProxy<typename boost::mpl::size<TTransitionTable>::type, TEvent> >
        >::type
    >
    transition(const TStateInit&, Event<TEvent> p_event)
    {
        return transition<TStateInit, TEvent, Operations::None>(TStateInit(), p_event, Operations::None());
    }
#endif

    TPool& pool() { return m_pool; }

private:
    TPool m_pool;
};

} // namespace Front
} // namespace Fusion
} // namespace QFsm

#endif

max_stars_repo_name: krzysztof-jusiak/qfsm

max_issues_repo_name: krzysztof-jusiak/qfsm

max_forks_repo_name: krzysztof-jusiak/qfsm


text:
/*
   Copyright 2018 Simon Vogl <svogl@voxel.at>
                  Angel Merino-Sastre <amerino@voxel.at>

   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.
*/
#pragma once

#include <toffy/toffy_export.h>
#include <toffy/toffy_config.h>

#include <vector>

#include <boost/property_tree/ptree.hpp>
#include <boost/log/trivial.hpp>

#include <toffy/frame.hpp>

#ifdef MSVC
#define DLLExport __declspec( dllexport )
#else
#define DLLExport /**/
#endif

/** @defgroup Core Core
 *
 * Core module
 *
 */

/** @defgroup Filters Filters
 *
 * Filters module
 *
 */

/** @defgroup Viewers Viewers
 *
 * Viewers module
 *
 */

namespace toffy {

class Event;
class Filter;

/**
 * @brief Filter running state definitions
 */
enum filterState {
    filterLoaded,
    filterIdle,
    filterRunning,
    filterPaused,
    filterError};

/**
 * @brief The FilterListener class
 */
class FilterListener {
public:
    /**
     * @brief stateChanged
     * @param source
     * @param state
     */
    virtual void stateChanged(Filter& source, filterState state) = 0;
};

/**
 * @brief The Port class
 */
class Port { // meta data to be done...
public:
    /**
     * @brief The portDir enum
     */
    enum portDir { portIn, portOut };

    portDir dir; ///< dir
    std::string name; ///< name
};

/**
 * @brief Base clase filter
 * @ingroup Core
 *
 * This class define how is a filter and is capabilities.
 *
 */
class TOFFY_EXPORT Filter {

    std::string _id, ///< Internal id of the single instance of the filter
    _name, ///< id alias @deprecated Is the same as id
    _type; ///< Type id of the filter

    Filter *_bank; ///< reference to the filter bank whre the filter resides
    //static std::size_t _filter_counter;

    /*
     * From boost trivial log
     * //! Trivial severity levels
    enum severity_level
    {
        trace,
        debug,
        info,
        warning,
        error,
        fatal
    };
     * @todo Find a better way to individually set log level.
     * changing severity filter affects everithing.
     */
    boost::log::trivial::severity_level _log_lvl;

public:

    bool dbg, ///< flag for activating debug options (images view, log, etc)
    update; ///< Flag for indicating config updated

    /**
     * @brief Filter
     */
    Filter();

    /**
     * @brief ~Filter
     */
    virtual ~Filter();

    boost::log::trivial::severity_level logLvl() const {return _log_lvl;}

    /**
     * @brief getCounter
     * @return unsigned int Actuall index of filters
     */
    unsigned int getCounter() const;

    /**
     * @brief Getter type
     * @return std::string The type of the filter
     */
    const std::string& type() const { return _type;}

    /**
     * @brief Getter name
     * @return std::string The name of the filter
     *
     * Name of the filter is by default a combination of type+counter
     * at least the users indicates a name explicitly in the config
     */
    const std::string& name() const { return _name;}

    /**
     * @brief Setter name
     * @param n
     *
     * Name of the filter is by default a combination of type+counter
     * at least the users indicates a name explicitly in the config
     */
    void name(std::string n) {_name = n;}

    /** perform a filter operation on in, sending result to out
     * both Mat's may refer to the same memory region.
     *
     * @return true if processing worked, false on error
     */
    virtual bool filter(const Frame& in, Frame& out) const { return false; }

    /**
     * @brief perform a filter operation on in, sending result to out
     * both Mat's may refer to the same memory region.
     *
     * @param in
     * @param out
     * @return true if processing worked, false on error
     *
     * The default behavior of this non-const variant is to call
     * the const filter method defined above.
     *
     */
    virtual bool filter(const Frame& in, Frame& out) {
        const Filter& constF = *this;
        return constF.filter(in, out) ;
    }

    /**
     * @brief id
     * @return
     */
    std::string id() const {return _id;}

    /**
     * @brief loadConfig
     * @param pt ptree
     * @return Positive on success, negative or 0 if failled
     *
     * Loads the configuration for the first time. It checks if the \<globals>
     * node is present and update the configuration from them. Then calls
     * updateConfig to load the rest of the configuration. The local
     * configuration overrides the global.
     *
     * The derived filter can overwrite this methos to implement some extra
     * action at filter creation time (like autoconnect to a camera).
     * If so, the parent loadConfig should be called at least we are handling
     * a special filter like the FilterBank.
     *
     */
    virtual int loadConfig(const boost::property_tree::ptree& pt);

    /**
     * @brief loadFileConfig
     * @param configFile
     * @return Positive on success, negative or 0 if failled
     *
     * Load the XML file into the property tree. If reading fails
     * (cannot open file, parse error), an exception is thrown.
     *
     * @deprecated We dont want a filter reading files, only the filterbank
     *
     */
    virtual int loadFileConfig(const std::string& configFile);

    /**
     * @brief Put all public config values in a ptree
     * @return ptree
     *
     * Its use to get runtime the filter configuration.
     */
    virtual boost::property_tree::ptree getConfig() const;

    /**
     * @brief Udate the internal configuration of the filter
     * @param pt ptree
     *
     * Read a ptree and set the filter values.
     * The base filter initializes common variables like the log level.
     *
     * Each filter should overwrite this method and add the filter specific
     * variables to be read.
     *
     * This method does not care from where the configuration is comming:
     *  first loadConfig, loadGlobals or external like UI.
     * It just update the config values.
     *
     */
    virtual void updateConfig(const boost::property_tree::ptree &pt);

    /**
     * @brief Look for global nodes that contain information for the filter
     * @param pt ptree to a configuration
     *
     * Determines if the filter wants to load values from globals checking the
     * \<globals> node inside the filter configuration.
     *
     * Looks if in the globals node (if exists) threre are values which are
     * valuable for the filter. Normally this values are use also in other
     * filters and to avoid write them several time in the config, we can set
     * them to the globals.
     *
     */
    void loadGlobals(const boost::property_tree::ptree &pt);

    /**
     * @brief Getter filterBank
     * @return FilterBank
     */
    Filter * bank() const {return _bank;}

    /**
     * @brief Setter FilterBank
     * @param bank FilterBank where the filter resides
     */
    void bank(Filter * bank) {_bank=bank;}

    /**
     * @brief processEvent
     * @param e Data if the event to be runned
     *
     * Every filter could define in this method a set of actions that could
     * be executed by call from other filters.
     *
     * The events are processed outside of the FilterBank sequential mode.
     * Every filter must discribe his availabled events.
     *
     */
    virtual void processEvent(Event &e);

protected:

    /**
     * @brief Base constructor for any filter
     * @param type [in] Id of the filter
     * @param counter Int value to be append to the name of the filter
     */
    Filter(std::string type, std::size_t counter = 0);

    /*!
     *** state handling ************************
     *  \addtogroup State_handling
     *  @{
     */
private:
    filterState state; ///< Filter state
    std::vector<FilterListener*> listeners; ///< listeners
    std::string errMsg; ///< error

public:
    /**
     * @brief getState
     * @return
     *
     */
    virtual filterState getState() { return state; }

    /**
     * @brief addListener
     * @param l
     */
    void addListener(FilterListener* l) { listeners.push_back(l); }

    /**
     * @brief removeListener
     * @param l
     */
    inline void removeListener(const FilterListener* l);

    /**
     * @brief Optional step to pre-allocate resources, if not done in the
     * config updates, reset the state to idle
     *
     */
    inline virtual void init();

    /**
     * @brief start the filter -- active components will connect to
     * cameras etc.
     */
    inline virtual void start();

    /**
     * @brief stop processing
     */
    inline virtual void stop();

    /**
     * @brief enter error state, pass an error string
     */
    inline virtual void error(const std::string& errMsg);

    /**
     * @brief getErrorMsg
     * @return std::string&
     */
    const std::string& getErrorMsg() { return errMsg; }

    /**
     * @brief Sets the boost log filter severity
     */
    void setLoggingLvl();

protected:
    /**
     * @brief set the filter state and notify listeners on a change.
     */
    virtual void setState(filterState state);

    /*! @} End Group State_handling*/
};

void Filter::init() {
    errMsg = "";
    setState(filterIdle);
}

void Filter::start() {
    setState(filterRunning);
}

void Filter::stop() {
    setState(filterIdle);
}

void Filter::error(const std::string& e) {
    errMsg = e;
    setState(filterError);
}
}

/* enforce nice(r) logging by providing a short-hand version
 */
#define LOG(lvl)    BOOST_LOG_TRIVIAL(lvl) << id() << "::" << __FUNCTION__ << "() : "

max_stars_repo_name: voxel-dot-at/toffy

max_issues_repo_name: voxel-dot-at/toffy

max_forks_repo_name: voxel-dot-at/toffy


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

max_stars_repo_name: hexrain/utymap

max_issues_repo_name: daeilkim/utymap

max_forks_repo_name: daeilkim/utymap


text:
#include "http.h"
#include "util.h"

#include <iostream>
#include <istream>
#include <ostream>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

/**
 * @brief simple http client.
 * @param host Host to submit http request to. ex: "www.myhost.com"
 * @param uri path where to submit request to. ex: "/some/random/uri/blah.ext"
 * @return boolean true on success, false otherwise.
 */
bool http_post(const std::string &host, const std::string &uri, const std::string &raw_post_data) {
    printf("Initiating http_post to host=%s at uri=%s\n", host.c_str(), uri.c_str());
    try {
        boost::asio::io_service io_service;

        // Get a list of endpoints corresponding to the server name.
        tcp::resolver resolver(io_service);
        tcp::resolver::query query(host, "http");
        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        tcp::resolver::iterator end;

        // Try each endpoint until we successfully establish a connection.
        tcp::socket socket(io_service);
        boost::system::error_code error = boost::asio::error::host_not_found;
        while (error && endpoint_iterator != end) {
            socket.close();
            socket.connect(*endpoint_iterator++, error);
        }
        if (error)
            throw boost::system::system_error(error);

        // Form the request. We specify the "Connection: close" header so that the
        // server will close the socket after transmitting the response. This will
        // allow us to treat all data up until the EOF as the content.
        boost::asio::streambuf request;
        std::ostream request_stream(&request);
        request_stream << "POST " << uri << " HTTP/1.0\r\n";
        request_stream << "Host: " << host << "\r\n";
        request_stream << "Accept: */*\r\n";
        request_stream << "Content-Length: " << raw_post_data.length() << "\r\n\r\n";
        request_stream << raw_post_data << "\r\n\r\n";
        //request_stream << "Connection: close\r\n\r\n";

        // Send the request.
        boost::asio::write(socket, request);

        // Read the response status line.
        boost::asio::streambuf response;
        boost::asio::read_until(socket, response, "\r\n");

        // Check that response is OK.
        std::istream response_stream(&response);
        std::string http_version;
        response_stream >> http_version;
        unsigned int status_code;
        response_stream >> status_code;
        std::string status_message;
        std::getline(response_stream, status_message);
        if (!response_stream || http_version.substr(0, 5) != "HTTP/") {
            printf("Invalid response from %s\n", host.c_str());;
            return (false);
        }

        if (status_code != 200) {
            printf("Response returned with status code %d\n", status_code);
            return (false);
        } else {
            printf("200 OK from host/uri=%s%s\n", host.c_str(), uri.c_str());
        }

        // Read the response headers, which are terminated by a blank line.
        boost::asio::read_until(socket, response, "\r\n\r\n");

        // Process the response headers.
        std::string header;
        while (std::getline(response_stream, header) && header != "\r")
            ; //std::cout << header << "\n";
        //std::cout << "\n";

        // Write whatever content we already have to output.
        //if (response.size() > 0)
        //    std::cout << &response;

        // Read until EOF, writing data to output as we go.
        while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error))
            ; //std::cout << &response;
        if (error != boost::asio::error::eof)
            throw boost::system::system_error(error);
    } catch (std::exception& e) {
        printf("Http exception: %s\n", e.what());
        return (false);
    }

    return (true);
}


text:
// Copyright (C) 2016 Gideon May (gideon@borges.xyz)
//
// Permission to copy, use, sell and distribute this software is granted
// provided this copyright notice appears in all copies.
// Permission to modify the code and to distribute modified code is granted
// provided this copyright notice appears in all copies, and a notice
// that the code was modified is included with the copyright notice.
//
// This software is provided "as is" without express or implied warranty,
// and with no claim as to its suitability for any purpose.

#ifndef PYOSG_ARGUMENT_PARSER
#define PYOSG_ARGUMENT_PARSER 1

#include <boost/python/list.hpp>

#include <osg/ArgumentParser>
#include <osg/ApplicationUsage>

#include <string>

namespace PyOSG {

using namespace boost::python;

class ArgumentParser : public  osg::ArgumentParser {
  public:
    ArgumentParser(list& );
    ArgumentParser(int * argc, char ** argv) : osg::ArgumentParser(argc, argv) {}
    ~ArgumentParser();

    // void setApplicationUsage(osg::ApplicationUsage* usage) { _ap->setApplicationUsage(usage); }

    osg::ApplicationUsage* getApplicationUsage() {
        return osg::ArgumentParser::getApplicationUsage();
    }

    int argc() {
        return osg::ArgumentParser::argc();
    }

    // std::string getApplicationName() { return _ap->getApplicationName(); }

    bool read(const std::string& str) {
        return osg::ArgumentParser::read(str);
    }

    bool errors_0() { return errors(); }
    bool errors_1(osg::ArgumentParser::ErrorSeverity severity) { return errors(severity); }

    void reportRemainingOptionsAsUnrecognized_0() {
        reportRemainingOptionsAsUnrecognized();
    }

    void reportRemainingOptionsAsUnrecognized_1(osg::ArgumentParser::ErrorSeverity severity) {
        reportRemainingOptionsAsUnrecognized(severity);
    }

    void writeErrorMessages_0() {
        return writeErrorMessages(std::cout);
    }

    void writeErrorMessages_1(osg::ArgumentParser::ErrorSeverity severity) {
        return writeErrorMessages(std::cout, severity);
    }

    osg::ArgumentParser& argumentParser() { return *this; }

    std::string getitem(int pos)
    {
        if (_startArgc <= pos) {
            PyErr_SetString(PyExc_IndexError, "assignment index out of range");
            throw_error_already_set();
        }
        return std::string((*this)[pos]);
    }

  protected :
    int                     _totArgc;
    int                     _startArgc;
};

} // namespace PyOSG

#endif // PYOSG_ARGUMENT_PARSER

max_stars_repo_name: gideonmay/PyOSG

max_issues_repo_name: gideonmay/PyOSG

max_forks_repo_name: gideonmay/PyOSG