<commit_before>#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Persons.include"

struct Person {
  char first_name[32];
  char surname[32];
  int age;
  char ssn[32];
};

void get_string(const char* msg, char* buf, int num) {
  puts(msg);
  getline(buf, num);
}
void get_int(const char* msg, int* result) {
  int success;
  while (true) {
    puts(msg);
    success = scanf("%i", result); while(getchar() != '\n');
    if (success) return;
    puts("Please enter a number\n----------------");
  }
}

int main(int, char const **) {
  enum MenuType {NORMAL_MENU, PRETTY_MENU, SIMPLE_MENU, SELECT_MENU };
  MenuType  menu_type = NORMAL_MENU;
  Person* people = (Person*) malloc(64*sizeof(Person));
  int num_people = 0, capacity = 64;
  while (true) {
    switch (menu_type) {
      case NORMAL_MENU:
        puts(
          "Hello welcome to the personprogram.\n"
          "Enter q to quit\n"
          "Enter p to print persons\n"
          "Enter a to add person\n"
          "Enter d to delete a person by ssn\n"
          "Enter m to switch menu\n"
        );
        break;
      case PRETTY_MENU:
        puts(
          "<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3\n"
          "<3 Hello welcome to the personprogram.<3\n"
          "<3          Enter q to quit           <3\n"
          "<3      Enter p to print persons      <3\n"
          "<3        Enter a to add person       <3\n"
          "<3  Enter d to delete a person by ssn <3\n"
          "<3     Enter m to switch menu         <3\n"
          "<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3\n"
        );
        break;
      case SIMPLE_MENU:
        puts("Enter m to switch menu\n");
        break;
      case SELECT_MENU:
        puts(
          "Enter n for NORMAL\n"
          "Enter p for PRETTY\n"
          "Enter s for SIMPLE\n"
        );
        break;
    }
    char command;
    while((command = getchar()) == '\n');
    while(getchar() != '\n');
    switch (menu_type) {
      case NORMAL_MENU:
      case PRETTY_MENU:
        switch (command) {
          case 'q':
            return 0;
          case 'p':
            puts("Your people are:");
            for (int i = 0; i < num_people; ++i)
              printf("- %s %s %i %s \n",people[i].first_name,people[i].surname, people[i].age, people[i].ssn);
            break;
          case 'a': {
            Person p = {};
            get_string("Enter first name", p.first_name, sizeof(p.first_name));
            get_string("Enter surname", p.surname, sizeof(p.surname));
            get_int("Enter age", &p.age);
            get_string("Enter social security number", p.ssn, sizeof(p.ssn));
            if (num_people == capacity) {
              capacity *= 2;
              people = (Person*) realloc(people, capacity*sizeof(Person));
            }
            people[num_people++] = p;
          } break;
          case 'd':
            char ssn[32];
            get_string("Enter social security number", ssn, sizeof(ssn));
            for (int i = 0; i < num_people; ++i)
              if (strcmp(people[i].ssn, ssn) == 0)
                people[i] = people[--num_people];
            break;
          case 'm':
            menu_type = SELECT_MENU;
            break;
          default:
            puts("Not valid option");
            break;
        }
        break;
      case SIMPLE_MENU:
        if (command == 'm')
          menu_type = SELECT_MENU;
        else
          puts("Not valid option");
        break;
      case SELECT_MENU:
        switch (command) {
          case 'n': menu_type = NORMAL_MENU; break;
          case 'p': menu_type = PRETTY_MENU; break;
          case 's': menu_type = SIMPLE_MENU; break;
          default: puts("Not valid option");
        }
        break;

    }
  }
  return 0;
}
<commit_msg>Added comments<commit_after>#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Persons.include"

struct Person {
  char first_name[32];
  char surname[32];
  int age;
  char ssn[32];
};

void get_string(const char* msg, char* buf, int num) {
  puts(msg);
  getline(buf, num);
}
void get_int(const char* msg, int* result) {
  int success;
  while (true) {
    puts(msg);
    success = scanf("%i", result); while(getchar() != '\n');
    if (success) return;
    puts("Please enter a number\n----------------");
  }
}

int main(int, char const **) {
  enum MenuType {NORMAL_MENU, PRETTY_MENU, SIMPLE_MENU, SELECT_MENU };
  MenuType  menu_type = NORMAL_MENU;
  Person* people = (Person*) malloc(64*sizeof(Person));
  int num_people = 0, capacity = 64;
  while (true) {
    /* Print menu */
    switch (menu_type) {
      case NORMAL_MENU:
        puts(
          "Hello welcome to the personprogram.\n"
          "Enter q to quit\n"
          "Enter p to print persons\n"
          "Enter a to add person\n"
          "Enter d to delete a person by ssn\n"
          "Enter m to switch menu\n"
        );
        break;
      case PRETTY_MENU:
        puts(
          "<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3\n"
          "<3 Hello welcome to the personprogram.<3\n"
          "<3          Enter q to quit           <3\n"
          "<3      Enter p to print persons      <3\n"
          "<3        Enter a to add person       <3\n"
          "<3  Enter d to delete a person by ssn <3\n"
          "<3     Enter m to switch menu         <3\n"
          "<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3<3\n"
        );
        break;
      case SIMPLE_MENU:
        puts("Enter m to switch menu\n");
        break;
      case SELECT_MENU:
        puts(
          "Enter n for NORMAL\n"
          "Enter p for PRETTY\n"
          "Enter s for SIMPLE\n"
        );
        break;
    }

    /* Get input */
    char command;
    while((command = getchar()) == '\n');
    while(getchar() != '\n');

    /* Process input */
    switch (menu_type) {
      case NORMAL_MENU:
      case PRETTY_MENU:
        switch (command) {
          case 'q':
            return 0;
          case 'p':
            puts("Your people are:");
            for (int i = 0; i < num_people; ++i)
              printf("- %s %s %i %s \n",people[i].first_name,people[i].surname, people[i].age, people[i].ssn);
            break;
          case 'a': {
            Person p = {};
            get_string("Enter first name", p.first_name, sizeof(p.first_name));
            get_string("Enter surname", p.surname, sizeof(p.surname));
            get_int("Enter age", &p.age);
            get_string("Enter social security number", p.ssn, sizeof(p.ssn));
            if (num_people == capacity) {
              capacity *= 2;
              people = (Person*) realloc(people, capacity*sizeof(Person));
            }
            people[num_people++] = p;
          } break;
          case 'd':
            char ssn[32];
            get_string("Enter social security number", ssn, sizeof(ssn));
            for (int i = 0; i < num_people; ++i)
              if (strcmp(people[i].ssn, ssn) == 0)
                people[i] = people[--num_people];
            break;
          case 'm':
            menu_type = SELECT_MENU;
            break;
          default:
            puts("Not valid option");
            break;
        }
        break;
      case SIMPLE_MENU:
        if (command == 'm')
          menu_type = SELECT_MENU;
        else
          puts("Not valid option");
        break;
      case SELECT_MENU:
        switch (command) {
          case 'n': menu_type = NORMAL_MENU; break;
          case 'p': menu_type = PRETTY_MENU; break;
          case 's': menu_type = SIMPLE_MENU; break;
          default: puts("Not valid option");
        }
        break;
    }
  }
  return 0;
}
<|endoftext|>


<commit_before>// 0 = all
// 1 = not pion
// 2 = not kaon
// 3 = not proton
// 4 = not muon
// 5 = not electron
// 6 = not neutron

Int_t particle_type=0;

#include "iostream.h"

void RICHdigit (Int_t evNumber1=0,Int_t evNumber2=0)
{
/////////////////////////////////////////////////////////////////////////
//   This macro is a small example of a ROOT macro
//   illustrating how to read the output of GALICE
//   and do some analysis.
//
/////////////////////////////////////////////////////////////////////////

// Dynamically link some shared libs

  if (gClassTable->GetID("AliRun") < 0) {
      gROOT->LoadMacro("loadlibs.C");
      loadlibs();
   }

// Connect the Root Galice file containing Geometry, Kine and Hits

   TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject("galice.root");
   if (file) file->Close();
   file = new TFile("galice.root","UPDATE");
//   file->ls();
// Get AliRun object from file or create it if not on file

   if (gClassTable->GetID("AliRun") < 0) {
	gROOT->LoadMacro("loadlibs.C");
	loadlibs();
    }
    else {
      //delete gAlice;
      gAlice = 0;
    }

   if (!gAlice) {
       gAlice = (AliRun*)file->Get("gAlice");
       if (gAlice) printf("AliRun object found on file\n");
       if (!gAlice) gAlice = new AliRun("gAlice","Alice test program");
   } else {
      delete gAlice;
      gAlice = (AliRun*)file->Get("gAlice");
      	if (gAlice) printf("AliRun object found on file\n");
	if (!gAlice) gAlice = new AliRun("gAlice","Alice test program");
    }

   AliRICH *RICH  = (AliRICH*) gAlice->GetDetector("RICH");

   AliRICHChamber*       iChamber;

   printf("Generating tresholds...\n");

   for(Int_t i=0;i<7;i++)
     {
       iChamber = &(RICH->Chamber(i));
       iChamber->GenerateTresholds();
     }

//
// Event Loop
//
   for (int nev=0; nev<= evNumber2; nev++) {
       Int_t nparticles = gAlice->GetEvent(nev);
       cout <<endl<< "Processing event:" <<nev<<endl;
       cout << "Particles       :" <<nparticles<<endl;
       if (nev < evNumber1) continue;
       if (nparticles <= 0) return;
       if (RICH) RICH->Digitise(nev, particle_type);
       //char hname[30];
       //sprintf(hname,"TreeD%d",nev);
       //gAlice->TreeD()->Write(hname);
       //gAlice->TreeD()->Reset();
   } // event loop
   file->Close();

   //delete gAlice;
   printf("\nEnd of Macro  *************************************\n");
}

<commit_msg>Have to create Digits trees and branches by hand in the case of multiple event files.<commit_after>// 0 = all
// 1 = not pion
// 2 = not kaon
// 3 = not proton
// 4 = not muon
// 5 = not electron
// 6 = not neutron

Int_t particle_type=0;

#include "iostream.h"

void RICHdigit (Int_t evNumber1=0,Int_t evNumber2=0)
{
/////////////////////////////////////////////////////////////////////////
//   This macro is a small example of a ROOT macro
//   illustrating how to read the output of GALICE
//   and do some analysis.
//
/////////////////////////////////////////////////////////////////////////

// Dynamically link some shared libs

  if (gClassTable->GetID("AliRun") < 0) {
      gROOT->LoadMacro("loadlibs.C");
      loadlibs();
  }else {
    delete gAlice;
    gAlice = 0;
  }

  galice=0;

// Connect the Root Galice file containing Geometry, Kine and Hits

   TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject("galice.root");
   if (file) file->Close();
   file = new TFile("galice.root","UPDATE");
//   file->ls();
// Get AliRun object from file or create it if not on file

   if (gClassTable->GetID("AliRun") < 0) {
	gROOT->LoadMacro("loadlibs.C");
	loadlibs();
    }

   if (!gAlice) {
       gAlice = (AliRun*)file->Get("gAlice");
       if (gAlice) printf("AliRun object found on file\n");
       if (!gAlice) gAlice = new AliRun("gAlice","Alice test program");
   } else {
      delete gAlice;
      gAlice = (AliRun*)file->Get("gAlice");
      	if (gAlice) printf("AliRun object found on file\n");
	if (!gAlice) gAlice = new AliRun("gAlice","Alice test program");
    }

   AliRICH *RICH  = (AliRICH*) gAlice->GetDetector("RICH");

   AliRICHChamber*       iChamber;

   printf("Generating tresholds...\n");

   for(Int_t i=0;i<7;i++)
     {
       iChamber = &(RICH->Chamber(i));
       iChamber->GenerateTresholds();
     }

//
// Event Loop
//
   for (int nev=0; nev<= evNumber2; nev++) {
       Int_t nparticles = gAlice->GetEvent(nev);
       cout <<endl<< "Processing event:" <<nev<<endl;
       cout << "Particles       :" <<nparticles<<endl;
       if (nev < evNumber1) continue;
       if (nparticles <= 0) return;
       if (RICH)
	 {
	   gAlice->MakeTree("D");
	   RICH->MakeBranch("D");
	   RICH->Digitise(nev, particle_type);
	 }
       //if (RICH) gAlice->SDigits2Digits("RICH");
       //char hname[30];
       //sprintf(hname,"TreeD%d",nev);
       //gAlice->TreeD()->Write(hname);
       //gAlice->TreeD()->Reset();
   } // event loop
   file->Close();

   //delete gAlice;
   printf("\nEnd of Macro  *************************************\n");
}

<|endoftext|>


<commit_before>#include "cnn/edges.h"
#include "cnn/cnn.h"
#include "cnn/training.h"
#include "cnn/timing.h"
#include "cnn/rnn.h"
#include "cnn/lstm-fast.h"
#include "cnn/dict.h"

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;
using namespace cnn;

unsigned LAYERS = 2;
unsigned INPUT_DIM = 8;
unsigned HIDDEN_DIM = 24;
unsigned VOCAB_SIZE = 0;

cnn::Dict d;
int kSOS;
int kEOS;

template <class Builder>
struct RNNLanguageModel {
  LookupParameters* p_c;
  Parameters* p_R;
  Parameters* p_bias;
  Builder builder;
  explicit RNNLanguageModel(Model& model) : builder(LAYERS, INPUT_DIM, HIDDEN_DIM, &model) {
    p_c = model.add_lookup_parameters(VOCAB_SIZE, Dim({INPUT_DIM}));
    p_R = model.add_parameters(Dim({VOCAB_SIZE, HIDDEN_DIM}));
    p_bias = model.add_parameters(Dim({VOCAB_SIZE}));
  }

  // return VariableIndex of total loss
  VariableIndex BuildLMGraph(const vector<int>& sent, Hypergraph& hg) {
    const unsigned slen = sent.size() - 1;
    builder.new_graph();  // reset RNN builder for new graph
    builder.add_parameter_edges(&hg);  // add variables for its parameters
    builder.start_new_sequence(&hg);
    VariableIndex i_R = hg.add_parameter(p_R); // hidden -> word rep parameter
    VariableIndex i_bias = hg.add_parameter(p_bias);  // word bias
    vector<VariableIndex> errs;
    for (unsigned t = 0; t < slen; ++t) {
      // x_t = lookup sent[t] in parameters p_c
      VariableIndex i_x_t = hg.add_lookup(p_c, sent[t]);
      // y_t = RNN(x_t)
      VariableIndex i_y_t = builder.add_input(i_x_t, &hg);
      // r_t = bias + R * y_t
      VariableIndex i_r_t = hg.add_function<Multilinear>({i_bias, i_R, i_y_t});
      // ydist = softmax(r_t)
      VariableIndex i_ydist = hg.add_function<LogSoftmax>({i_r_t});
      errs.push_back(hg.add_function<PickElement>({i_ydist}, sent[t+1]));
    }
    VariableIndex i_nerr = hg.add_function<Sum>(errs);
    return hg.add_function<Negate>({i_nerr});
  }

  // return VariableIndex of total loss
  void RandomSample(int max_len = 150) {
    cerr << endl;
    Hypergraph hg;
    builder.new_graph();  // reset RNN builder for new graph
    builder.add_parameter_edges(&hg);  // add variables for its parameters
    builder.start_new_sequence(&hg);
    VariableIndex i_R = hg.add_parameter(p_R); // hidden -> word rep parameter
    VariableIndex i_bias = hg.add_parameter(p_bias);  // word bias
    vector<VariableIndex> errs;
    int len = 0;
    int cur = kSOS;
    while(len < max_len && cur != kEOS) {
      ++len;
      // x_t = lookup sent[t] in parameters p_c
      VariableIndex i_x_t = hg.add_lookup(p_c, cur);
      // y_t = RNN(x_t)
      VariableIndex i_y_t = builder.add_input(i_x_t, &hg);
      // r_t = bias + R * y_t
      VariableIndex i_r_t = hg.add_function<Multilinear>({i_bias, i_R, i_y_t});
      // ydist = softmax(r_t)
      hg.add_function<Softmax>({i_r_t});
      unsigned w = 0;
      while (w == 0 || (int)w == kSOS) {
        auto dist = as_vector(hg.incremental_forward());
        double p = (double)rand() / (RAND_MAX + 1.0);
        for (; w < dist.size(); ++w) {
          p -= dist[w];
          if (p < 0.0) { break; }
        }
        if (w == dist.size()) w = kEOS;
      }
      cerr << (len == 1 ? "" : " ") << d.Convert(w);
      cur = w;
    }
    cerr << endl;
  }
};

int main(int argc, char** argv) {
  cnn::Initialize(argc, argv);
  if (argc != 3 && argc != 4) {
    cerr << "Usage: " << argv[0] << " corpus.txt dev.txt [model.params]\n";
    return 1;
  }
  kSOS = d.Convert("<s>");
  kEOS = d.Convert("</s>");
  vector<vector<int>> training, dev;
  string line;
  int tlc = 0;
  int ttoks = 0;
  cerr << "Reading training data from " << argv[1] << "...\n";
  {
    ifstream in(argv[1]);
    assert(in);
    while(getline(in, line)) {
      ++tlc;
      training.push_back(ReadSentence(line, &d));
      ttoks += training.back().size();
      if (training.back().front() != kSOS && training.back().back() != kEOS) {
        cerr << "Training sentence in " << argv[1] << ":" << tlc << " didn't start or end with <s>, </s>\n";
        abort();
      }
    }
    cerr << tlc << " lines, " << ttoks << " tokens, " << d.size() << " types\n";
  }
  d.Freeze(); // no new word types allowed
  VOCAB_SIZE = d.size();

  int dlc = 0;
  int dtoks = 0;
  cerr << "Reading dev data from " << argv[2] << "...\n";
  {
    ifstream in(argv[2]);
    assert(in);
    while(getline(in, line)) {
      ++dlc;
      dev.push_back(ReadSentence(line, &d));
      dtoks += dev.back().size();
      if (dev.back().front() != kSOS && dev.back().back() != kEOS) {
        cerr << "Dev sentence in " << argv[2] << ":" << tlc << " didn't start or end with <s>, </s>\n";
        abort();
      }
    }
    cerr << dlc << " lines, " << dtoks << " tokens\n";
  }
  ostringstream os;
  os << "lm"
     << '_' << LAYERS
     << '_' << INPUT_DIM
     << '_' << HIDDEN_DIM
     << "-pid" << getpid() << ".params";
  const string fname = os.str();
  cerr << "Parameters will be written to: " << fname << endl;
  double best = 9e+99;

  Model model;
  bool use_momentum = false;
  Trainer* sgd = nullptr;
  if (use_momentum)
    sgd = new MomentumSGDTrainer(&model);
  else
    sgd = new SimpleSGDTrainer(&model);

  //RNNBuilder rnn(LAYERS, INPUT_DIM, HIDDEN_DIM, &model);
  RNNLanguageModel<LSTMBuilder_CIFG> lm(model);
  if (argc == 4) {
    string fname = argv[3];
    ifstream in(fname);
    boost::archive::text_iarchive ia(in);
    ia >> model;
  }

  unsigned report_every_i = 50;
  unsigned dev_every_i_reports = 500;
  unsigned si = training.size();
  vector<unsigned> order(training.size());
  for (unsigned i = 0; i < order.size(); ++i) order[i] = i;
  bool first = true;
  int report = 0;
  unsigned lines = 0;
  while(1) {
    Timer iteration("completed in");
    double loss = 0;
    unsigned chars = 0;
    for (unsigned i = 0; i < report_every_i; ++i) {
      if (si == training.size()) {
        si = 0;
        if (first) { first = false; } else { sgd->update_epoch(); }
        cerr << "**SHUFFLE\n";
        random_shuffle(order.begin(), order.end());
      }

      // build graph for this instance
      Hypergraph hg;
      auto& sent = training[order[si]];
      chars += sent.size() - 1;
      ++si;
      lm.BuildLMGraph(sent, hg);
      loss += as_scalar(hg.forward());
      hg.backward();
      sgd->update();
      ++lines;
    }
    sgd->status();
    cerr << " E = " << (loss / chars) << " ppl=" << exp(loss / chars) << ' ';
    lm.RandomSample();

    // show score on dev data?
    report++;
    if (report % dev_every_i_reports == 0) {
      double dloss = 0;
      int dchars = 0;
      for (auto& sent : dev) {
        Hypergraph hg;
        lm.BuildLMGraph(sent, hg);
        dloss += as_scalar(hg.forward());
        dchars += sent.size() - 1;
      }
      if (dloss < best) {
        best = dloss;
        ofstream out(fname);
        boost::archive::text_oarchive oa(out);
        oa << model;
      }
      cerr << "\n***DEV [epoch=" << (lines / (double)training.size()) << "] E = " << (dloss / dchars) << " ppl=" << exp(dloss / dchars) << ' ';
    }
  }
  delete sgd;
}

<commit_msg>use cleaner interface<commit_after>#include "cnn/edges.h"
#include "cnn/cnn.h"
#include "cnn/training.h"
#include "cnn/timing.h"
#include "cnn/rnn.h"
#include "cnn/lstm-fast.h"
#include "cnn/dict.h"

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;
using namespace cnn;

unsigned LAYERS = 2;
unsigned INPUT_DIM = 8;
unsigned HIDDEN_DIM = 24;
unsigned VOCAB_SIZE = 0;

cnn::Dict d;
int kSOS;
int kEOS;

template <class Builder>
struct RNNLanguageModel {
  LookupParameters* p_c;
  Parameters* p_R;
  Parameters* p_bias;
  Builder builder;
  explicit RNNLanguageModel(Model& model) : builder(LAYERS, INPUT_DIM, HIDDEN_DIM, &model) {
    p_c = model.add_lookup_parameters(VOCAB_SIZE, {INPUT_DIM});
    p_R = model.add_parameters({VOCAB_SIZE, HIDDEN_DIM});
    p_bias = model.add_parameters({VOCAB_SIZE});
  }

  // return VariableIndex of total loss
  VariableIndex BuildLMGraph(const vector<int>& sent, Hypergraph& hg) {
    const unsigned slen = sent.size() - 1;
    builder.new_graph();  // reset RNN builder for new graph
    builder.add_parameter_edges(&hg);  // add variables for its parameters
    builder.start_new_sequence(&hg);
    VariableIndex i_R = hg.add_parameter(p_R); // hidden -> word rep parameter
    VariableIndex i_bias = hg.add_parameter(p_bias);  // word bias
    vector<VariableIndex> errs;
    for (unsigned t = 0; t < slen; ++t) {
      // x_t = lookup sent[t] in parameters p_c
      VariableIndex i_x_t = hg.add_lookup(p_c, sent[t]);
      // y_t = RNN(x_t)
      VariableIndex i_y_t = builder.add_input(i_x_t, &hg);
      // r_t = bias + R * y_t
      VariableIndex i_r_t = hg.add_function<Multilinear>({i_bias, i_R, i_y_t});
      // ydist = softmax(r_t)
      VariableIndex i_ydist = hg.add_function<LogSoftmax>({i_r_t});
      errs.push_back(hg.add_function<PickElement>({i_ydist}, sent[t+1]));
    }
    VariableIndex i_nerr = hg.add_function<Sum>(errs);
    return hg.add_function<Negate>({i_nerr});
  }

  // return VariableIndex of total loss
  void RandomSample(int max_len = 150) {
    cerr << endl;
    Hypergraph hg;
    builder.new_graph();  // reset RNN builder for new graph
    builder.add_parameter_edges(&hg);  // add variables for its parameters
    builder.start_new_sequence(&hg);
    VariableIndex i_R = hg.add_parameter(p_R); // hidden -> word rep parameter
    VariableIndex i_bias = hg.add_parameter(p_bias);  // word bias
    vector<VariableIndex> errs;
    int len = 0;
    int cur = kSOS;
    while(len < max_len && cur != kEOS) {
      ++len;
      // x_t = lookup sent[t] in parameters p_c
      VariableIndex i_x_t = hg.add_lookup(p_c, cur);
      // y_t = RNN(x_t)
      VariableIndex i_y_t = builder.add_input(i_x_t, &hg);
      // r_t = bias + R * y_t
      VariableIndex i_r_t = hg.add_function<Multilinear>({i_bias, i_R, i_y_t});
      // ydist = softmax(r_t)
      hg.add_function<Softmax>({i_r_t});
      unsigned w = 0;
      while (w == 0 || (int)w == kSOS) {
        auto dist = as_vector(hg.incremental_forward());
        double p = (double)rand() / (RAND_MAX + 1.0);
        for (; w < dist.size(); ++w) {
          p -= dist[w];
          if (p < 0.0) { break; }
        }
        if (w == dist.size()) w = kEOS;
      }
      cerr << (len == 1 ? "" : " ") << d.Convert(w);
      cur = w;
    }
    cerr << endl;
  }
};

int main(int argc, char** argv) {
  cnn::Initialize(argc, argv);
  if (argc != 3 && argc != 4) {
    cerr << "Usage: " << argv[0] << " corpus.txt dev.txt [model.params]\n";
    return 1;
  }
  kSOS = d.Convert("<s>");
  kEOS = d.Convert("</s>");
  vector<vector<int>> training, dev;
  string line;
  int tlc = 0;
  int ttoks = 0;
  cerr << "Reading training data from " << argv[1] << "...\n";
  {
    ifstream in(argv[1]);
    assert(in);
    while(getline(in, line)) {
      ++tlc;
      training.push_back(ReadSentence(line, &d));
      ttoks += training.back().size();
      if (training.back().front() != kSOS && training.back().back() != kEOS) {
        cerr << "Training sentence in " << argv[1] << ":" << tlc << " didn't start or end with <s>, </s>\n";
        abort();
      }
    }
    cerr << tlc << " lines, " << ttoks << " tokens, " << d.size() << " types\n";
  }
  d.Freeze(); // no new word types allowed
  VOCAB_SIZE = d.size();

  int dlc = 0;
  int dtoks = 0;
  cerr << "Reading dev data from " << argv[2] << "...\n";
  {
    ifstream in(argv[2]);
    assert(in);
    while(getline(in, line)) {
      ++dlc;
      dev.push_back(ReadSentence(line, &d));
      dtoks += dev.back().size();
      if (dev.back().front() != kSOS && dev.back().back() != kEOS) {
        cerr << "Dev sentence in " << argv[2] << ":" << tlc << " didn't start or end with <s>, </s>\n";
        abort();
      }
    }
    cerr << dlc << " lines, " << dtoks << " tokens\n";
  }
  ostringstream os;
  os << "lm"
     << '_' << LAYERS
     << '_' << INPUT_DIM
     << '_' << HIDDEN_DIM
     << "-pid" << getpid() << ".params";
  const string fname = os.str();
  cerr << "Parameters will be written to: " << fname << endl;
  double best = 9e+99;

  Model model;
  bool use_momentum = false;
  Trainer* sgd = nullptr;
  if (use_momentum)
    sgd = new MomentumSGDTrainer(&model);
  else
    sgd = new SimpleSGDTrainer(&model);

  //RNNBuilder rnn(LAYERS, INPUT_DIM, HIDDEN_DIM, &model);
  RNNLanguageModel<LSTMBuilder_CIFG> lm(model);
  if (argc == 4) {
    string fname = argv[3];
    ifstream in(fname);
    boost::archive::text_iarchive ia(in);
    ia >> model;
  }

  unsigned report_every_i = 50;
  unsigned dev_every_i_reports = 500;
  unsigned si = training.size();
  vector<unsigned> order(training.size());
  for (unsigned i = 0; i < order.size(); ++i) order[i] = i;
  bool first = true;
  int report = 0;
  unsigned lines = 0;
  while(1) {
    Timer iteration("completed in");
    double loss = 0;
    unsigned chars = 0;
    for (unsigned i = 0; i < report_every_i; ++i) {
      if (si == training.size()) {
        si = 0;
        if (first) { first = false; } else { sgd->update_epoch(); }
        cerr << "**SHUFFLE\n";
        random_shuffle(order.begin(), order.end());
      }

      // build graph for this instance
      Hypergraph hg;
      auto& sent = training[order[si]];
      chars += sent.size() - 1;
      ++si;
      lm.BuildLMGraph(sent, hg);
      loss += as_scalar(hg.forward());
      hg.backward();
      sgd->update();
      ++lines;
    }
    sgd->status();
    cerr << " E = " << (loss / chars) << " ppl=" << exp(loss / chars) << ' ';
    lm.RandomSample();

    // show score on dev data?
    report++;
    if (report % dev_every_i_reports == 0) {
      double dloss = 0;
      int dchars = 0;
      for (auto& sent : dev) {
        Hypergraph hg;
        lm.BuildLMGraph(sent, hg);
        dloss += as_scalar(hg.forward());
        dchars += sent.size() - 1;
      }
      if (dloss < best) {
        best = dloss;
        ofstream out(fname);
        boost::archive::text_oarchive oa(out);
        oa << model;
      }
      cerr << "\n***DEV [epoch=" << (lines / (double)training.size()) << "] E = " << (dloss / dchars) << " ppl=" << exp(dloss / dchars) << ' ';
    }
  }
  delete sgd;
}

<|endoftext|>


<commit_before>#include <iostream>
#include <map>
#include <unordered_map>
#include <vector>
#include <string>
#include "dict.h"

typedef std::unordered_map<std::string, std::string> Dict;
typedef unsigned long IdentificatorType;

// consider map<unsigned long, unordered_map>
// somehow initialize dictglobal dictionary
std::map<IdentificatorType, Dict> dictionaries;
IdentificatorType dictCounter = 0;

IdentificatorType dict_new() {
    dictionaries.insert(std::pair<IdentificatorType, Dict>(++dictCounter, Dict()));

    return dictCounter;
}

void dict_remove(IdentificatorType id, const char* key) {
    auto dictionaryIt = dictionaries.find(id);
    if (dictionaryIt != dictionaries.end()) {
        dictionaryIt->second.erase(key);
    }
}

const char* dict_find(IdentificatorType id, const char* key) {
    auto dictionaryIt = dictionaries.find(id);
    if (dictionaryIt != dictionaries.end()) {
        auto stringIt = dictionaryIt->second.find(key);
        if (stringIt != dictionaryIt->second.end()) {
            return stringIt->second.c_str();
        }
    }

    auto stringIt = dictionaries.at(0).find(key);
    if (stringIt != dictionaries.at(0).end()) {
        return stringIt->second.c_str();
    }

    return NULL;
}

void dict_clear(IdentificatorType id) {
    auto dictionaryIt = dictionaries.find(id);
    if (dictionaryIt != dictionaries.end()) {
        dictionaryIt->second.clear();
    }
}

void dict_copy(IdentificatorType src_id, IdentificatorType dst_id) {
    auto srcDictionaryIt = dictionaries.find(src_id);
    auto dstDictionaryIt = dictionaries.find(dst_id);
    if (srcDictionaryIt != dictionaries.end() &&
        dstDictionaryIt != dictionaries.end()) {
        // do not copy if destination dictionary is dictglobal and amount of keys exceeds size

        // copy contents
    }
}<commit_msg>Include dictglobal header<commit_after>#include <iostream>
#include <map>
#include <unordered_map>
#include <vector>
#include <string>
#include "dict.h"
#include "dictglobal.h"

typedef std::unordered_map<std::string, std::string> Dict;
typedef unsigned long IdentificatorType;

// consider map<unsigned long, unordered_map>
// somehow initialize dictglobal dictionary
std::map<IdentificatorType, Dict> dictionaries;
IdentificatorType dictCounter = 0;

IdentificatorType dict_new() {
    dictionaries.insert(std::pair<IdentificatorType, Dict>(++dictCounter, Dict()));

    return dictCounter;
}

void dict_remove(IdentificatorType id, const char* key) {
    auto dictionaryIt = dictionaries.find(id);
    if (dictionaryIt != dictionaries.end()) {
        dictionaryIt->second.erase(key);
    }
}

const char* dict_find(IdentificatorType id, const char* key) {
    auto dictionaryIt = dictionaries.find(id);
    if (dictionaryIt != dictionaries.end()) {
        auto stringIt = dictionaryIt->second.find(key);
        if (stringIt != dictionaryIt->second.end()) {
            return stringIt->second.c_str();
        }
    }

    auto stringIt = dictionaries.at(0).find(key);
    if (stringIt != dictionaries.at(0).end()) {
        return stringIt->second.c_str();
    }

    return NULL;
}

void dict_clear(IdentificatorType id) {
    auto dictionaryIt = dictionaries.find(id);
    if (dictionaryIt != dictionaries.end()) {
        dictionaryIt->second.clear();
    }
}

void dict_copy(IdentificatorType src_id, IdentificatorType dst_id) {
    auto srcDictionaryIt = dictionaries.find(src_id);
    auto dstDictionaryIt = dictionaries.find(dst_id);
    if (srcDictionaryIt != dictionaries.end() &&
        dstDictionaryIt != dictionaries.end()) {
        // do not copy if destination dictionary is dictglobal and amount of keys exceeds size

        // copy contents
    }
}<|endoftext|>


<commit_before>#pragma once

#include "Defs.hpp"
#include <string>

struct EscapeCodeGenerator {

    using EscapeCode = std::string;

    virtual EscapeCode cursorForward(int) const { return EscapeCode{}; }
    virtual EscapeCode cursorBackward(int) const { return EscapeCode{}; }
    virtual EscapeCode clearTerminalLine() const { return EscapeCode{}; }
    virtual EscapeCode setColor(Color) const { return EscapeCode{}; }

};

struct ANSIEscapeCodeGenerator final : EscapeCodeGenerator {

    EscapeCode cursorForward(int n) const override {
        if (!n) return EscapeCode{};
        EscapeCode a{"\033["};
        a += std::to_string(n);
        a += 'C';
        return a;
    }

    EscapeCode cursorBackward(int n) const override {
        if (!n) return EscapeCode{};
        EscapeCode a{"\033["};
        a += std::to_string(n);
        a += 'D';
        return a;
    }

    EscapeCode clearTerminalLine() const override {
        return EscapeCode{"\033[K"};
    }

    EscapeCode setColor(Color color) const override {
        EscapeCode a{"\033["};
        a += std::to_string(static_cast<int>(color));
        a += 'm';
        return a;
    }

};

<commit_msg>Change type of parameter in cursor moving methods<commit_after>#pragma once

#include "Defs.hpp"
#include <string>

struct EscapeCodeGenerator {

    using EscapeCode = std::string;

    virtual EscapeCode cursorForward(unsigned int) const { return EscapeCode{}; }
    virtual EscapeCode cursorBackward(unsigned int) const { return EscapeCode{}; }
    virtual EscapeCode clearTerminalLine() const { return EscapeCode{}; }
    virtual EscapeCode setColor(Color) const { return EscapeCode{}; }

};

struct ANSIEscapeCodeGenerator final : EscapeCodeGenerator {

    EscapeCode cursorForward(unsigned int n) const override {
        if (!n) return EscapeCode{};
        EscapeCode a{"\033["};
        a += std::to_string(n);
        a += 'C';
        return a;
    }

    EscapeCode cursorBackward(unsigned int n) const override {
        if (!n) return EscapeCode{};
        EscapeCode a{"\033["};
        a += std::to_string(n);
        a += 'D';
        return a;
    }

    EscapeCode clearTerminalLine() const override {
        return EscapeCode{"\033[K"};
    }

    EscapeCode setColor(Color color) const override {
        EscapeCode a{"\033["};
        a += std::to_string(static_cast<int>(color));
        a += 'm';
        return a;
    }

};

<|endoftext|>


<commit_before>#pragma once

#include "Defs.hpp"
#include <string>

struct EscapeCodeGenerator {

    using EscapeCode = std::string;

    virtual EscapeCode cursorForward(unsigned int) const { return EscapeCode{}; }
    virtual EscapeCode cursorBackward(unsigned int) const { return EscapeCode{}; }
    virtual EscapeCode clearTerminalLine() const { return EscapeCode{}; }
    virtual EscapeCode setColor(Color) const { return EscapeCode{}; }

};

struct ANSIEscapeCodeGenerator final : EscapeCodeGenerator {

    const EscapeCode EscapeCodeStart = "\033[";

    EscapeCode cursorForward(unsigned int n) const override {
        if (!n) return EscapeCode{};
        return EscapeCodeStart + std::to_string(n) + 'C';
    }

    EscapeCode cursorBackward(unsigned int n) const override {
        if (!n) return EscapeCode{};
        return EscapeCodeStart + std::to_string(n) + 'D';
    }

    EscapeCode clearTerminalLine() const override {
        return EscapeCodeStart + 'K';
    }

    EscapeCode setColor(Color color) const override {
        return EscapeCodeStart + std::to_string(static_cast<int>(color)) + 'm';
    }

};

<commit_msg>Make EscapeCodeStart private<commit_after>#pragma once

#include "Defs.hpp"
#include <string>

struct EscapeCodeGenerator {

    using EscapeCode = std::string;

    virtual EscapeCode cursorForward(unsigned int) const { return EscapeCode{}; }
    virtual EscapeCode cursorBackward(unsigned int) const { return EscapeCode{}; }
    virtual EscapeCode clearTerminalLine() const { return EscapeCode{}; }
    virtual EscapeCode setColor(Color) const { return EscapeCode{}; }

};

struct ANSIEscapeCodeGenerator final : EscapeCodeGenerator {

    EscapeCode cursorForward(unsigned int n) const override {
        if (!n) return EscapeCode{};
        return EscapeCodeStart + std::to_string(n) + 'C';
    }

    EscapeCode cursorBackward(unsigned int n) const override {
        if (!n) return EscapeCode{};
        return EscapeCodeStart + std::to_string(n) + 'D';
    }

    EscapeCode clearTerminalLine() const override {
        return EscapeCodeStart + 'K';
    }

    EscapeCode setColor(Color color) const override {
        return EscapeCodeStart + std::to_string(static_cast<int>(color)) + 'm';
    }

private:
    const EscapeCode EscapeCodeStart = "\033[";

};

<|endoftext|>


<commit_before>/**
 * ofxAppUpdater is developed by Paul Vollmer
 * http://www.wng.cc
 *
 *
 * Copyright (c) 2012 Paul Vollmer
 *
 * ofxAppUpdater is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * ofxAppUpdater is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with ofxAppUpdater; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 *
 * @author      Paul Vollmer
 * @modified    2012.04.23
 * @version     1.0.1d
 */

#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){

	vera.loadFont("vera.ttf", 9, true, false);

	ofRegisterURLNotification(this);
	ofLoadURLAsync("http://www.wrong-entertainment.com/code/ofxAppUpdater/appcastSample.xml","load");

}

//--------------------------------------------------------------
void testApp::update(){

}

//--------------------------------------------------------------
void testApp::draw(){
	ofBackground(ofColor::white);

	ofSetColor(ofColor::black);
	vera.drawString("ofxAppcast", 100, 100);
	vera.drawString(channelTitle, 100, 120);
	vera.drawString(channelLink, 100, 140);
	vera.drawString(channelDate, 100, 160);

	vera.drawString(itemTitle, 100, 200);
	vera.drawString(itemDescription, 100, 220);
	vera.drawString(itemDate, 100, 240);
	vera.drawString(itemUrl, 100, 260);

	vera.drawString("appcastVersion: "+appcastVersion, 100, 300);
	vera.drawString("appcastAuthor: "+appcastAuthor, 100, 320);
	vera.drawString("appcastLicense: "+appcastLicense, 100, 340);
	vera.drawString("appcastDownloads: "+appcastDownloads, 100, 360);

}

//--------------------------------------------------------------
void testApp::keyPressed(int key){
	ofLoadURLAsync("http://www.wrong-entertainment.com/code/ofxAppUpdater/appcastSample.xml","load");
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){

}

//--------------------------------------------------------------
void testApp::exit(){
	ofUnregisterURLNotification(this);
}

//--------------------------------------------------------------
void testApp::urlResponse(ofHttpResponse & response){

	// based on http://forum.openframeworks.cc/index.php/topic,8398.0.html
    if(response.status==200 ){
        cout << response.request.name << endl;
        cout << response.data.getText() << endl;

		xml.loadFromBuffer(response.data);

		// set string variables with appcast tags.
		// If you want to change the cantent later, you can
		// !We have no variables to save at ofxAppcast class!
		channelTitle = appcast.getChannelTitle(xml);
		channelLink = appcast.getChannelLink(xml);
		channelDate = appcast.getChannelPubDate(xml);
		itemTitle = appcast.getTitle(xml, 0);
		itemDescription = appcast.getDescription(xml, 0);
		itemDate = appcast.getPubDate(xml, 0);
		itemUrl = appcast.getEnclosureUrl(xml, 0);
		appcastVersion = appcast.getAppcastVersion(xml, 0);
		appcastAuthor = appcast.getAppcastAuthor(xml, 0);
		appcastLicense = appcast.getAppcastLicense(xml, 0);
		appcastDownloads = appcast.getAppcastDownloadCount(xml, 0);

		// This is a list example of all get methods.
		/*
		cout << "\nGet a tag from our channel: \n";
		cout << "getChannelTitle            = " << appcast.getChannelTitle() << endl;
		cout << "getChannelLink             = " << appcast.getChannelLink() << endl;
		cout << "getChannelDescription      = " << appcast.getChannelDescription() << endl;
		cout << "getChannelLanguage         = " << appcast.getChannelLanguage() << endl;
		cout << "getChannelPubDate          = " << appcast.getChannelPubDate() << endl;
		cout << "\nGet the tags from our first item: \n";
		cout << "getTitle                   = " << appcast.getTitle(0) << endl;
		cout << "getDescription             = " << appcast.getDescription(0) << endl;
		cout << "getPubDate                 = " << appcast.getPubDate(0) << endl;
		cout << "getEnclosureUrl            = " << appcast.getEnclosureUrl(0) << endl;
		cout << "getEnclosureType           = " << appcast.getEnclosureType(0) << endl;
		cout << "\nGet the appcast tags from our first item: \n";
		cout << "getAppcastVersion          = " << appcast.getAppcastVersion(0) << endl;
		cout << "getAppcastAuthor           = " << appcast.getAppcastAuthor(0) << endl;
		cout << "getAppcastAuthorUrl        = " << appcast.getAppcastAuthorUrl(0) << endl;
		cout << "getAppcastAuthorEmail      = " << appcast.getAppcastAuthorEmail(0) << endl;
		cout << "getAppcastShortDescription = " << appcast.getAppcastShortDescription(0) << endl;
		cout << "getAppcastLicense          = " << appcast.getAppcastLicense(0) << endl;
		cout << "getAppcastLicenseUrl       = " << appcast.getAppcastLicenseUrl(0) << endl;
		cout << "getAppcastHash             = " << appcast.getAppcastHash(0) << endl;
		cout << "getAppcastHashAlgo         = " << appcast.getAppcastHashAlgo(0) << endl;
		cout << "getAppcastRating           = " << appcast.getAppcastRating(0) << endl;
		cout << "getAppcastRatingVotes      = " << appcast.getAppcastRatingVotes(0) << endl;
		cout << "getAppcastDownloadCount    = " << appcast.getAppcastDownloadCount(0) << endl;
		cout << "getAppcastKeywords         = " << appcast.getAppcastKeywords(0) << endl;
		cout << "getAppcastDocLink          = " << appcast.getAppcastDocLink(0) << endl;
		cout << "getAppcastSourceLink       = " << appcast.getAppcastSourceLink(0) << endl;
		cout << "getAppcastPreviewLink      = " << appcast.getAppcastPreviewLink(0) << endl;
		*/

    }else{
        cout << response.status << " " << response.error << endl;
    }

}
<commit_msg>fixed list example get... methods<commit_after>/**
 * ofxAppUpdater is developed by Paul Vollmer
 * http://www.wng.cc
 *
 *
 * Copyright (c) 2012 Paul Vollmer
 *
 * ofxAppUpdater is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * ofxAppUpdater is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with ofxAppUpdater; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 *
 * @author      Paul Vollmer
 * @modified    2012.04.23
 * @version     1.0.1d
 */

#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){

	vera.loadFont("vera.ttf", 9, true, false);

	ofRegisterURLNotification(this);
	ofLoadURLAsync("http://www.wrong-entertainment.com/code/ofxAppUpdater/appcastSample.xml","load");

}

//--------------------------------------------------------------
void testApp::update(){

}

//--------------------------------------------------------------
void testApp::draw(){
	ofBackground(ofColor::white);

	ofSetColor(ofColor::black);
	vera.drawString("ofxAppcast", 100, 100);
	vera.drawString(channelTitle, 100, 120);
	vera.drawString(channelLink, 100, 140);
	vera.drawString(channelDate, 100, 160);

	vera.drawString(itemTitle, 100, 200);
	vera.drawString(itemDescription, 100, 220);
	vera.drawString(itemDate, 100, 240);
	vera.drawString(itemUrl, 100, 260);

	vera.drawString("appcastVersion: "+appcastVersion, 100, 300);
	vera.drawString("appcastAuthor: "+appcastAuthor, 100, 320);
	vera.drawString("appcastLicense: "+appcastLicense, 100, 340);
	vera.drawString("appcastDownloads: "+appcastDownloads, 100, 360);

}

//--------------------------------------------------------------
void testApp::keyPressed(int key){

	// You can reload your appcast.xml like this
	//ofLoadURLAsync("http://www.wrong-entertainment.com/code/ofxAppUpdater/appcastSample.xml","load");

}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){

}

//--------------------------------------------------------------
void testApp::exit(){

}

//--------------------------------------------------------------
void testApp::urlResponse(ofHttpResponse & response){

	// based on http://forum.openframeworks.cc/index.php/topic,8398.0.html
    if(response.status==200 ){
        cout << response.request.name << endl;
        cout << response.data.getText() << endl;

		xml.loadFromBuffer(response.data);

		// set string variables with appcast tags.
		// If you want to change the cantent later, you can
		// !We have no variables to save at ofxAppcast class!
		channelTitle = appcast.getChannelTitle(xml);
		channelLink = appcast.getChannelLink(xml);
		channelDate = appcast.getChannelPubDate(xml);
		itemTitle = appcast.getTitle(xml, 0);
		itemDescription = appcast.getDescription(xml, 0);
		itemDate = appcast.getPubDate(xml, 0);
		itemUrl = appcast.getEnclosureUrl(xml, 0);
		appcastVersion = appcast.getAppcastVersion(xml, 0);
		appcastAuthor = appcast.getAppcastAuthor(xml, 0);
		appcastLicense = appcast.getAppcastLicense(xml, 0);
		appcastDownloads = appcast.getAppcastDownloadCount(xml, 0);

		// This is a list example of all get methods.
		cout << "\nGet a tag from our channel: \n";
		cout << "getChannelTitle            = " << appcast.getChannelTitle(xml) << endl;
		cout << "getChannelLink             = " << appcast.getChannelLink(xml) << endl;
		cout << "getChannelDescription      = " << appcast.getChannelDescription(xml) << endl;
		cout << "getChannelLanguage         = " << appcast.getChannelLanguage(xml) << endl;
		cout << "getChannelPubDate          = " << appcast.getChannelPubDate(xml) << endl;
		cout << "\nGet the tags from our first item: \n";
		cout << "getTitle                   = " << appcast.getTitle(xml, 0) << endl;
		cout << "getDescription             = " << appcast.getDescription(xml, 0) << endl;
		cout << "getPubDate                 = " << appcast.getPubDate(xml, 0) << endl;
		cout << "getEnclosureUrl            = " << appcast.getEnclosureUrl(xml, 0) << endl;
		cout << "getEnclosureType           = " << appcast.getEnclosureType(xml, 0) << endl;
		cout << "\nGet the appcast tags from our first item: \n";
		cout << "getAppcastVersion          = " << appcast.getAppcastVersion(xml, 0) << endl;
		cout << "getAppcastAuthor           = " << appcast.getAppcastAuthor(xml, 0) << endl;
		cout << "getAppcastAuthorUrl        = " << appcast.getAppcastAuthorUrl(xml, 0) << endl;
		cout << "getAppcastAuthorEmail      = " << appcast.getAppcastAuthorEmail(xml, 0) << endl;
		cout << "getAppcastShortDescription = " << appcast.getAppcastShortDescription(xml, 0) << endl;
		cout << "getAppcastLicense          = " << appcast.getAppcastLicense(xml, 0) << endl;
		cout << "getAppcastLicenseUrl       = " << appcast.getAppcastLicenseUrl(xml, 0) << endl;
		cout << "getAppcastHash             = " << appcast.getAppcastHash(xml, 0) << endl;
		cout << "getAppcastHashAlgo         = " << appcast.getAppcastHashAlgo(xml, 0) << endl;
		cout << "getAppcastRating           = " << appcast.getAppcastRating(xml, 0) << endl;
		cout << "getAppcastRatingVotes      = " << appcast.getAppcastRatingVotes(xml, 0) << endl;
		cout << "getAppcastDownloadCount    = " << appcast.getAppcastDownloadCount(xml, 0) << endl;
		cout << "getAppcastKeywords         = " << appcast.getAppcastKeywords(xml, 0) << endl;
		cout << "getAppcastDocLink          = " << appcast.getAppcastDocLink(xml, 0) << endl;
		cout << "getAppcastSourceLink       = " << appcast.getAppcastSourceLink(xml, 0) << endl;
		cout << "getAppcastPreviewLink      = " << appcast.getAppcastPreviewLink(xml, 0) << endl;

    }else{
        cout << response.status << " " << response.error << endl;
    }

	ofSleepMillis(200);
	ofUnregisterURLNotification(this);

}
<|endoftext|>


<commit_before>/*

Copyright (c) 2010, Arvid Norberg
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/

#include "libtorrent/storage.hpp"
#include "libtorrent/file_pool.hpp"

#include <boost/utility.hpp>

#include <stdlib.h>

using namespace libtorrent;

int main(int argc, char* argv[])
{
	if (argc != 3 && argc != 2)
	{
		fprintf(stderr, "Usage: fragmentation_test torrent-file file-storage-path\n"
			"       fragmentation_test file\n\n");
		return 1;
	}

	if (argc == 2)
	{
		error_code ec;
		file f(argv[1], file::read_only, ec);
		if (ec)
		{
			fprintf(stderr, "Error opening file %s: %s\n", argv[1], ec.message().c_str());
			return 1;
		}
		size_type off = f.phys_offset(0);
		printf("physical offset of file %s: %" PRId64 "\n", argv[1], off);
		return 0;
	}

	error_code ec;
	boost::intrusive_ptr<torrent_info> ti(new torrent_info(argv[1], ec));

	if (ec)
	{
		fprintf(stderr, "Error while loading torrent file: %s\n", ec.message().c_str());
		return 1;
	}

	file_pool fp;
	boost::shared_ptr<storage_interface> st(default_storage_constructor(ti->files(), 0, argv[2], fp, std::vector<boost::uint8_t>()));

	// the first field is the piece index, the second
	// one is the physical location of the piece on disk
	std::vector<std::pair<int, size_type> > pieces;

	// make sure all the files are there
/*	std::vector<std::pair<size_type, std::time_t> > files = get_filesizes(ti->files(), argv[2]);
	for (int i = 0; i < ti->num_files(); ++i)
	{
		if (ti->file_at(i).size == files[i].first) continue;
		fprintf(stderr, "Files for this torrent are missing or incomplete: %s was %"PRId64" bytes, expected %"PRId64" bytes\n"
			, ti->files().file_path(ti->file_at(i)).c_str(), files[i].first, ti->file_at(i).size);
		return 1;
	}
*/
	bool warned = false;
	for (int i = 0; i < ti->num_pieces(); ++i)
	{
		pieces.push_back(std::make_pair(i, st->physical_offset(i, 0)));
		if (pieces.back().second == size_type(i) * ti->piece_length())
		{
			// this suggests that the OS doesn't support physical offset
			// or that the file doesn't exist or is incomplete
			if (!warned)
			{
				fprintf(stderr, "The files are incomplete\n");
				warned = true;
			}
			pieces.pop_back();
		}
	}

	if (pieces.empty())
	{
		fprintf(stderr, "Your operating system or filesystem "
			"does not appear to support querying physical disk offset\n");
	}

	FILE* f = fopen("fragmentation.log", "w+");
	if (f == 0)
	{
		fprintf(stderr, "error while opening log file: %s\n", strerror(errno));
		return 1;
	}

	for (int i = 0; i < pieces.size(); ++i)
	{
		fprintf(f, "%d %"PRId64"\n", pieces[i].first, pieces[i].second);
	}

	fclose(f);

	f = fopen("fragmentation.gnuplot", "w+");
	if (f == 0)
	{
		fprintf(stderr, "error while opening gnuplot file: %s\n", strerror(errno));
		return 1;
	}

	fprintf(f,
		"set term png size 1200,800\n"
		"set output \"fragmentation.png\"\n"
		"set xrange [*:*]\n"
		"set xlabel \"piece\"\n"
		"set ylabel \"drive offset\"\n"
		"set key box\n"
		"set title \"fragmentation for '%s'\"\n"
		"set tics nomirror\n"
		"plot \"fragmentation.log\" using 1:2 with points lt rgb \"#e07070\" notitle axis x1y1\n"
		, ti->name().c_str());

	fclose(f);

	system("gnuplot fragmentation.gnuplot");
}

<commit_msg>fix comment<commit_after>/*

Copyright (c) 2010, Arvid Norberg
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/

#include "libtorrent/storage.hpp"
#include "libtorrent/file_pool.hpp"

#include <boost/utility.hpp>

#include <stdlib.h>

using namespace libtorrent;

int main(int argc, char* argv[])
{
	if (argc != 3 && argc != 2)
	{
		fprintf(stderr, "Usage: fragmentation_test torrent-file file-storage-path\n"
			"       fragmentation_test file\n\n");
		return 1;
	}

	if (argc == 2)
	{
		error_code ec;
		file f(argv[1], file::read_only, ec);
		if (ec)
		{
			fprintf(stderr, "Error opening file %s: %s\n", argv[1], ec.message().c_str());
			return 1;
		}
		size_type off = f.phys_offset(0);
		printf("physical offset of file %s: %" PRId64 "\n", argv[1], off);
		return 0;
	}

	error_code ec;
	boost::intrusive_ptr<torrent_info> ti(new torrent_info(argv[1], ec));

	if (ec)
	{
		fprintf(stderr, "Error while loading torrent file: %s\n", ec.message().c_str());
		return 1;
	}

	file_pool fp;
	boost::shared_ptr<storage_interface> st(default_storage_constructor(ti->files(), 0, argv[2], fp, std::vector<boost::uint8_t>()));

	// the first field is the piece index, the second
	// one is the physical location of the piece on disk
	std::vector<std::pair<int, size_type> > pieces;

	// make sure all the files are there
/*	std::vector<std::pair<size_type, std::time_t> > files = get_filesizes(ti->files(), argv[2]);
	for (int i = 0; i < ti->num_files(); ++i)
	{
		if (ti->file_at(i).size == files[i].first) continue;
		fprintf(stderr, "Files for this torrent are missing or incomplete: %s was %"PRId64" bytes, expected %"PRId64" bytes\n"
			, ti->files().file_path(ti->file_at(i)).c_str(), files[i].first, ti->file_at(i).size);
		return 1;
	}
*/
	bool warned = false;
	for (int i = 0; i < ti->num_pieces(); ++i)
	{
		pieces.push_back(std::make_pair(i, st->physical_offset(i, 0)));
		if (pieces.back().second == size_type(i) * ti->piece_length())
		{
			if (!warned)
			{
				fprintf(stderr, "The files are incomplete\n");
				warned = true;
			}
			pieces.pop_back();
		}
	}

	// this suggests that the OS doesn't support physical offset
	// or that the file doesn't exist or is incomplete
	if (pieces.empty())
	{
		fprintf(stderr, "Your operating system or filesystem "
			"does not appear to support querying physical disk offset\n");
	}

	FILE* f = fopen("fragmentation.log", "w+");
	if (f == 0)
	{
		fprintf(stderr, "error while opening log file: %s\n", strerror(errno));
		return 1;
	}

	for (int i = 0; i < pieces.size(); ++i)
	{
		fprintf(f, "%d %"PRId64"\n", pieces[i].first, pieces[i].second);
	}

	fclose(f);

	f = fopen("fragmentation.gnuplot", "w+");
	if (f == 0)
	{
		fprintf(stderr, "error while opening gnuplot file: %s\n", strerror(errno));
		return 1;
	}

	fprintf(f,
		"set term png size 1200,800\n"
		"set output \"fragmentation.png\"\n"
		"set xrange [*:*]\n"
		"set xlabel \"piece\"\n"
		"set ylabel \"drive offset\"\n"
		"set key box\n"
		"set title \"fragmentation for '%s'\"\n"
		"set tics nomirror\n"
		"plot \"fragmentation.log\" using 1:2 with points lt rgb \"#e07070\" notitle axis x1y1\n"
		, ti->name().c_str());

	fclose(f);

	system("gnuplot fragmentation.gnuplot");
}

<|endoftext|>


<commit_before>// Copyright (c) 2013 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and / or modify it
// under the terms of the GNU Lesser General Public version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.

#include <Graphic3d.ixx>

#include <Aspect_DisplayConnection.hxx>
#include <Aspect_DriverDefinitionError.hxx>
#include <OSD_Environment.hxx>

#ifdef HAVE_OCE_PATHS_H
# include <oce-paths.h>
#endif

//=======================================================================
//function : InitGraphicDriver
//purpose  :
//=======================================================================
Handle(Graphic3d_GraphicDriver) Graphic3d::InitGraphicDriver (const Handle(Aspect_DisplayConnection)& theDisplayConnection)
{
#if !defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
  if (theDisplayConnection.IsNull())
  {
    Aspect_DriverDefinitionError::Raise ("Null display connection.");
  }
#endif

  TCollection_AsciiString aGraphicLibName;

  const char *shr = getenv("CSF_GraphicShr");
  if (shr != NULL) {
    aGraphicLibName = shr;
  } else {
#ifdef OCE_DEFAULT_CSF_GraphicShr
  aGraphicLibName = OCE_DEFAULT_CSF_GraphicShr;
#else
  // Assemble library name according to the variables defined by CMAKE
  aGraphicLibName += OCE_LIBRARY_PREFIX;
  aGraphicLibName += "TKOpenGl";
  aGraphicLibName += OCE_LIBRARY_DEBUG_POSTFIX;
  aGraphicLibName += OCE_LIBRARY_EXTENSION;
#endif
  }

  // Loading the library.
  OSD_SharedLibrary aSharedLibrary (aGraphicLibName.ToCString());
  if (!aSharedLibrary.DlOpen (OSD_RTLD_LAZY))
  {
    Aspect_DriverDefinitionError::Raise (aSharedLibrary.DlError());
  }

  // Retrieving factory function pointer.
  typedef Handle(Graphic3d_GraphicDriver) (*GraphicDriverFactoryPointer) (Standard_CString);
  GraphicDriverFactoryPointer aGraphicDriverConstructor = (GraphicDriverFactoryPointer )aSharedLibrary.DlSymb ("MetaGraphicDriverFactory");
  if (aGraphicDriverConstructor == NULL)
  {
    Aspect_DriverDefinitionError::Raise (aSharedLibrary.DlError());
  }

  // Creating driver instance.
  Handle(Graphic3d_GraphicDriver) aGraphicDriver = aGraphicDriverConstructor (aSharedLibrary.Name());

  // Management of traces.
  OSD_Environment aTraceEnv ("CSF_GraphicTrace");
  TCollection_AsciiString aTrace = aTraceEnv.Value();
  if (aTrace.IsIntegerValue())
  {
    aGraphicDriver->SetTrace (aTrace.IntegerValue());
  }

  // Starting graphic driver.
  if (!aGraphicDriver->Begin (theDisplayConnection))
  {
    Aspect_DriverDefinitionError::Raise ("Cannot connect to graphic library.");
  }

  return aGraphicDriver;
}
<commit_msg>Fix Graphic3d::InitGraphicDriver with static linking<commit_after>// Copyright (c) 2013 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and / or modify it
// under the terms of the GNU Lesser General Public version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.

#include <Graphic3d.ixx>

#include <Aspect_DisplayConnection.hxx>
#include <Aspect_DriverDefinitionError.hxx>
#include <OSD_Environment.hxx>

#ifdef HAVE_OCE_PATHS_H
# include <oce-paths.h>
#endif

//=======================================================================
//function : InitGraphicDriver
//purpose  :
//=======================================================================
#if !defined(OCE_BUILD_STATIC_LIB) && !defined(HAVE_NO_DLL)
Handle(Graphic3d_GraphicDriver) Graphic3d::InitGraphicDriver (const Handle(Aspect_DisplayConnection)& theDisplayConnection)
{
#if !defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
  if (theDisplayConnection.IsNull())
  {
    Aspect_DriverDefinitionError::Raise ("Null display connection.");
  }
#endif

  TCollection_AsciiString aGraphicLibName;

  const char *shr = getenv("CSF_GraphicShr");
  if (shr != NULL) {
    aGraphicLibName = shr;
  } else {
#ifdef OCE_DEFAULT_CSF_GraphicShr
  aGraphicLibName = OCE_DEFAULT_CSF_GraphicShr;
#else
  // Assemble library name according to the variables defined by CMAKE
  aGraphicLibName += OCE_LIBRARY_PREFIX;
  aGraphicLibName += "TKOpenGl";
  aGraphicLibName += OCE_LIBRARY_DEBUG_POSTFIX;
  aGraphicLibName += OCE_LIBRARY_EXTENSION;
#endif
  }

  // Loading the library.
  OSD_SharedLibrary aSharedLibrary (aGraphicLibName.ToCString());
  if (!aSharedLibrary.DlOpen (OSD_RTLD_LAZY))
  {
    Aspect_DriverDefinitionError::Raise (aSharedLibrary.DlError());
  }

  // Retrieving factory function pointer.
  typedef Handle(Graphic3d_GraphicDriver) (*GraphicDriverFactoryPointer) (Standard_CString);
  GraphicDriverFactoryPointer aGraphicDriverConstructor = (GraphicDriverFactoryPointer )aSharedLibrary.DlSymb ("MetaGraphicDriverFactory");
  if (aGraphicDriverConstructor == NULL)
  {
    Aspect_DriverDefinitionError::Raise (aSharedLibrary.DlError());
  }

  // Creating driver instance.
  Handle(Graphic3d_GraphicDriver) aGraphicDriver = aGraphicDriverConstructor (aSharedLibrary.Name());

  // Management of traces.
  OSD_Environment aTraceEnv ("CSF_GraphicTrace");
  TCollection_AsciiString aTrace = aTraceEnv.Value();
  if (aTrace.IsIntegerValue())
  {
    aGraphicDriver->SetTrace (aTrace.IntegerValue());
  }

  // Starting graphic driver.
  if (!aGraphicDriver->Begin (theDisplayConnection))
  {
    Aspect_DriverDefinitionError::Raise ("Cannot connect to graphic library.");
  }

  return aGraphicDriver;
}
#else

extern "C"
{
     Handle(Graphic3d_GraphicDriver) MetaGraphicDriverFactory(const Standard_CString AShrName);
}

Handle(Graphic3d_GraphicDriver) Graphic3d::InitGraphicDriver (const Handle(Aspect_DisplayConnection)& theDisplayConnection)
{
     return MetaGraphicDriverFactory("");
}
#endif
<|endoftext|>


<commit_before>#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <time.h>
#include <vector>

#include "block_size.h"
#include "derecho/derecho.h"
#include "initialize.h"
#include <mutils-serialization/SerializationSupport.hpp>
#include <mutils-serialization/context_ptr.hpp>
#include <persistent/Persistent.hpp>

using mutils::context_ptr;

//This class is modified from Matt's implementation
struct Bytes : public mutils::ByteRepresentable{

        char *bytes;
        const std::size_t size;

        Bytes(const char * b, decltype(size) s)
                :size(s){
            bytes = nullptr;
            if(s>0) {
                bytes = new char[s];
                memcpy(bytes,b,s);
            }
        }
        virtual ~Bytes(){
            if(bytes!=nullptr) {
                delete bytes;
            }
        }

        std::size_t to_bytes(char* v) const{
                ((std::size_t*)(v))[0] = size;
                memcpy(v + sizeof(size),bytes,size);
                return size + sizeof(size);
        }

        std::size_t bytes_size() const {
                return size + sizeof(size);
        }

        void post_object(const std::function<void (char const * const,std::size_t)>& f) const{
                f((char*)&size,sizeof(size));
                f(bytes,size);
        }

        void ensure_registered(mutils::DeserializationManager&){}

        static std::unique_ptr<Bytes> from_bytes(mutils::DeserializationManager *, const  char * const v){
            return std::make_unique<Bytes>(v + sizeof(std::size_t),((std::size_t*)(v))[0]);
        }

        static context_ptr<Bytes> from_bytes_noalloc(mutils::DeserializationManager *, const char * const v)  {
                return context_ptr<Bytes>{new Bytes(v + sizeof(std::size_t),((std::size_t*)(v))[0])};
        }

};

/**
 * RPC Object with a single function that accepts a string
 */
class TestObject {
public:
    void fun(const std::string& words) {
    }

    void bytes_fun(const Bytes& bytes) {
    }

    /** Named integers that will be used to tag the RPC methods */
    enum Functions { FUN, BYTES_FUN };

    static auto register_functions() {
        return std::make_tuple(derecho::rpc::tag<FUN>(&TestObject::fun),
            derecho::rpc::tag<BYTES_FUN>(&TestObject::bytes_fun));
    }
};

int main(int argc, char* argv[]) {
    if(argc != 4) {
        std::cout << "usage:" << argv[0] << " <num_of_nodes> <max_msg_size> <count>" << std::endl;
        return -1;
    }
    int num_of_nodes = atoi(argv[1]);
    long long unsigned int max_msg_size = atoi(argv[2]);
    int count = atoi(argv[3]);

    derecho::node_id_t node_id;
    derecho::ip_addr my_ip;
    derecho::ip_addr leader_ip;
    query_node_info(node_id, my_ip, leader_ip);
    long long unsigned int block_size = get_block_size(max_msg_size);
    derecho::DerechoParams derecho_params{max_msg_size, block_size};

    derecho::CallbackSet callback_set{
            nullptr,  //we don't need the stability_callback here
            nullptr   //we don't need the persistence_callback either
    };

    derecho::SubgroupInfo subgroup_info{
            {{std::type_index(typeid(TestObject)), [num_of_nodes](const derecho::View& curr_view, int& next_unassigned_rank, bool previous_was_successful) {
                  if(curr_view.num_members < num_of_nodes) {
                      std::cout << "not enough members yet:" << curr_view.num_members << " < " << num_of_nodes << std::endl;
                      throw derecho::subgroup_provisioning_exception();
                  }
                  derecho::subgroup_shard_layout_t subgroup_vector(1);

                  std::vector<uint32_t> members(num_of_nodes);
                  for(int i = 0; i < num_of_nodes; i++) {
                      members[i] = i;
                  }

                  subgroup_vector[0].emplace_back(curr_view.make_subview(members));
                  next_unassigned_rank = std::max(next_unassigned_rank, num_of_nodes);
                  return subgroup_vector;
              }}},
            {std::type_index(typeid(TestObject))}};

    auto ba_factory = [](PersistentRegistry*) { return std::make_unique<TestObject>(); };

    std::unique_ptr<derecho::Group<TestObject>> group;
    if(my_ip == leader_ip) {
        group = std::make_unique<derecho::Group<TestObject>>(
                node_id, my_ip, callback_set, subgroup_info, derecho_params,
                std::vector<derecho::view_upcall_t>{}, derecho::derecho_gms_port,
                ba_factory);
    } else {
        group = std::make_unique<derecho::Group<TestObject>>(
                node_id, my_ip, leader_ip, callback_set, subgroup_info,
                std::vector<derecho::view_upcall_t>{}, derecho::derecho_gms_port,
                ba_factory);
    }

    std::cout << "Finished constructing/joining Group" << std::endl;

    bool inadequately_provisioned = true;
    while(inadequately_provisioned) {
        try {
            group->get_subgroup<TestObject>();
            inadequately_provisioned = false;
        } catch(derecho::subgroup_provisioning_exception& e) {
            inadequately_provisioned = true;
        }
    }

    std::cout << "All members have joined, subgroups are provisioned." << std::endl;
    derecho::Replicated<TestObject>& handle = group->get_subgroup<TestObject>();
    //std::string str_1k(max_msg_size, 'x');
    char * bbuf = (char*)malloc(max_msg_size);
    bzero(bbuf,max_msg_size);
    Bytes bytes(bbuf,max_msg_size);

    struct timespec t1, t2;
    clock_gettime(CLOCK_REALTIME, &t1);

    for(int i = 0; i < count; i++) {
        //handle.ordered_send<TestObject::FUN>(str_1k);
        handle.ordered_send<TestObject::BYTES_FUN>(bytes);
    }
    clock_gettime(CLOCK_REALTIME, &t2);
    free(bbuf);

    int64_t nsec = ((int64_t)t2.tv_sec - t1.tv_sec) * 1000000000 + t2.tv_nsec - t1.tv_nsec;
    double msec = (double)nsec / 1000000;
    double thp_gbps = ((double)count * max_msg_size * 8) / nsec;
    double thp_ops = ((double)count * 1000000000) / nsec;
    std::cout << "timespan:" << msec << " millisecond." << std::endl;
    std::cout << "throughput:" << thp_gbps << "Gbit/s." << std::endl;
    std::cout << "throughput:" << thp_ops << "ops." << std::endl;

    std::cout << "Reached end of main(), entering infinite loop so program doesn't exit" << std::endl;
    while(true) {
    }
}
<commit_msg>fix for typed_subgroup_bw_test.cpp<commit_after>#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <time.h>
#include <vector>

#include "block_size.h"
#include "derecho/derecho.h"
#include "initialize.h"
#include <mutils-serialization/SerializationSupport.hpp>
#include <mutils-serialization/context_ptr.hpp>
#include <persistent/Persistent.hpp>

using mutils::context_ptr;

//This class is modified from Matt's implementation
struct Bytes : public mutils::ByteRepresentable{

        char *bytes;
        const std::size_t size;

        Bytes(const char * b, decltype(size) s)
                :size(s){
            bytes = nullptr;
            if(s>0) {
                bytes = new char[s];
                memcpy(bytes,b,s);
            }
        }
        virtual ~Bytes(){
            if(bytes!=nullptr) {
                delete bytes;
            }
        }

        std::size_t to_bytes(char* v) const{
                ((std::size_t*)(v))[0] = size;
                memcpy(v + sizeof(size),bytes,size);
                return size + sizeof(size);
        }

        std::size_t bytes_size() const {
                return size + sizeof(size);
        }

        void post_object(const std::function<void (char const * const,std::size_t)>& f) const{
                f((char*)&size,sizeof(size));
                f(bytes,size);
        }

        void ensure_registered(mutils::DeserializationManager&){}

        static std::unique_ptr<Bytes> from_bytes(mutils::DeserializationManager *, const  char * const v){
            return std::make_unique<Bytes>(v + sizeof(std::size_t),((std::size_t*)(v))[0]);
        }

        static context_ptr<Bytes> from_bytes_noalloc(mutils::DeserializationManager *, const char * const v)  {
                return context_ptr<Bytes>{new Bytes(v + sizeof(std::size_t),((std::size_t*)(v))[0])};
        }

};

/**
 * RPC Object with a single function that accepts a string
 */
class TestObject {
public:
    void fun(const std::string& words) {
    }

    void bytes_fun(const Bytes& bytes) {
    }

    bool finishing_call(int x) {
        return true;
    }

    /** Named integers that will be used to tag the RPC methods */
    enum Functions { FUN, BYTES_FUN, FINISHING_CALL };

    static auto register_functions() {
        return std::make_tuple(derecho::rpc::tag<FUN>(&TestObject::fun),
            derecho::rpc::tag<BYTES_FUN>(&TestObject::bytes_fun),
            derecho::rpc::tag<FINISHING_CALL>(&TestObject::finishing_call));
    }
};

int main(int argc, char* argv[]) {
    if(argc != 4) {
        std::cout << "usage:" << argv[0] << " <num_of_nodes> <max_msg_size> <count>" << std::endl;
        return -1;
    }
    int num_of_nodes = atoi(argv[1]);
    long long unsigned int max_msg_size = atoi(argv[2]);
    int count = atoi(argv[3]);

    derecho::node_id_t node_id;
    derecho::ip_addr my_ip;
    derecho::ip_addr leader_ip;
    query_node_info(node_id, my_ip, leader_ip);
    long long unsigned int block_size = get_block_size(max_msg_size);
    derecho::DerechoParams derecho_params{max_msg_size, block_size};

    derecho::CallbackSet callback_set{
            nullptr,  //we don't need the stability_callback here
            nullptr   //we don't need the persistence_callback either
    };

    derecho::SubgroupInfo subgroup_info{
            {{std::type_index(typeid(TestObject)), [num_of_nodes](const derecho::View& curr_view, int& next_unassigned_rank, bool previous_was_successful) {
                  if(curr_view.num_members < num_of_nodes) {
                      std::cout << "not enough members yet:" << curr_view.num_members << " < " << num_of_nodes << std::endl;
                      throw derecho::subgroup_provisioning_exception();
                  }
                  derecho::subgroup_shard_layout_t subgroup_vector(1);

                  std::vector<uint32_t> members(num_of_nodes);
                  for(int i = 0; i < num_of_nodes; i++) {
                      members[i] = i;
                  }

                  subgroup_vector[0].emplace_back(curr_view.make_subview(members));
                  next_unassigned_rank = std::max(next_unassigned_rank, num_of_nodes);
                  return subgroup_vector;
              }}},
            {std::type_index(typeid(TestObject))}};

    auto ba_factory = [](PersistentRegistry*) { return std::make_unique<TestObject>(); };

    std::unique_ptr<derecho::Group<TestObject>> group;
    if(my_ip == leader_ip) {
        group = std::make_unique<derecho::Group<TestObject>>(
                node_id, my_ip, callback_set, subgroup_info, derecho_params,
                std::vector<derecho::view_upcall_t>{}, derecho::derecho_gms_port,
                ba_factory);
    } else {
        group = std::make_unique<derecho::Group<TestObject>>(
                node_id, my_ip, leader_ip, callback_set, subgroup_info,
                std::vector<derecho::view_upcall_t>{}, derecho::derecho_gms_port,
                ba_factory);
    }

    std::cout << "Finished constructing/joining Group" << std::endl;

    bool inadequately_provisioned = true;
    while(inadequately_provisioned) {
        try {
            group->get_subgroup<TestObject>();
            inadequately_provisioned = false;
        } catch(derecho::subgroup_provisioning_exception& e) {
            inadequately_provisioned = true;
        }
    }

    std::cout << "All members have joined, subgroups are provisioned." << std::endl;
    derecho::Replicated<TestObject>& handle = group->get_subgroup<TestObject>();
    //std::string str_1k(max_msg_size, 'x');
    char * bbuf = (char*)malloc(max_msg_size);
    bzero(bbuf,max_msg_size);
    Bytes bytes(bbuf,max_msg_size);

    struct timespec t1, t2;
    clock_gettime(CLOCK_REALTIME, &t1);

    for(int i = 0; i < count; i++) {
        //handle.ordered_send<TestObject::FUN>(str_1k);
        handle.ordered_send<TestObject::BYTES_FUN>(bytes);
    }
    derecho::rpc::QueryResults<bool> results = handle.ordered_query<TestObject::FINISHING_CALL>(0);
    decltype(results)::ReplyMap& replies = results.get();
    clock_gettime(CLOCK_REALTIME, &t2);
    free(bbuf);

    int64_t nsec = ((int64_t)t2.tv_sec - t1.tv_sec) * 1000000000 + t2.tv_nsec - t1.tv_nsec;
    double msec = (double)nsec / 1000000;
    double thp_gbps = ((double)count * max_msg_size * 8) / nsec;
    double thp_ops = ((double)count * 1000000000) / nsec;
    std::cout << "timespan:" << msec << " millisecond." << std::endl;
    std::cout << "throughput:" << thp_gbps << "Gbit/s." << std::endl;
    std::cout << "throughput:" << thp_ops << "ops." << std::endl;

    std::cout << "Reached end of main(), entering infinite loop so program doesn't exit" << std::endl;
    while(true) {
    }
}
<|endoftext|>