<commit_before>#include <iostream>
#include <map>
#include <stdexcept>
#include <stdio.h>
#include <sstream>
#include <string>
#include <boost/algorithm/string.hpp>
#include <boost/assign.hpp>
#include <boost/format.hpp>
#include <pkmnsim/base_pkmn.hpp>
#include <sqlitecpp/SQLiteCPP.h>

using namespace std;
typedef vector<string>::iterator str_iter;

base_pkmn::base_pkmn(string name, int num, string spec, string t1, string t2, string a1, string a2, string a3,
                        double ht, double wt, double cm, double cf,
                        int bhp, int batk, int bdef, int bsatk, int bsdef, int bspd,
                        int ehp, int eatk, int edef, int esatk, int esdef, int espd, int exp)
{
    display_name = name;
    nat_pokedex_num = num;
    species = spec;
    type1 = t1;
    type2 = t2;
    ability1 = a1;
    ability2 = a2;
    ability3 = a3;
    height = ht;
    weight = wt;
    chance_male = cm;
    chance_female = cf;
    baseHP = bhp;
    baseATK = batk;
    baseDEF = bdef;
    baseSATK = bsatk;
    baseSDEF = bsdef;
    baseSPD = bspd;
    evHP = ehp;
    evATK = eatk;
    evDEF = edef;
    evSATK = esatk;
    evSDEF = esdef;
    evSPD = espd;
    exp_yield = exp;
}

//Used when grabbing from databases
base_pkmn::base_pkmn(map<string,string> from_database)
{
    //Already strings
    display_name = from_database["display_name"];
    species = from_database["species"];
    type1 = from_database["type1"];
    type2 = from_database["type2"];
    ability1 = from_database["ability1"];
    ability2 = from_database["ability2"];
    ability3 = from_database["ability3"];

    //Need to be ints
    stringstream sin_num(from_database["pokedex_num"]);
    if(not (sin_num >> nat_pokedex_num)) throw runtime_error("Invalid input.");
    stringstream sin_bhp(from_database["base_hp"]);
    if(not (sin_bhp >> baseHP)) throw runtime_error("Invalid input.");
    stringstream sin_batk(from_database["base_atk"]);
    if(not (sin_batk >> baseATK)) throw runtime_error("Invalid input.");
    stringstream sin_bdef(from_database["base_def"]);
    if(not (sin_bdef >> baseDEF)) throw runtime_error("Invalid input.");
    stringstream sin_bsatk(from_database["base_satk"]);
    if(not (sin_bsatk >> baseSATK)) throw runtime_error("Invalid input.");
    stringstream sin_bsdef(from_database["base_sdef"]);
    if(not (sin_bsdef >> baseSDEF)) throw runtime_error("Invalid input.");
    stringstream sin_bspd(from_database["base_spd"]);
    if(not (sin_bspd >> baseSPD)) throw runtime_error("Invalid input.");
    stringstream sin_ehp(from_database["ev_hp"]);
    if(not (sin_ehp >> evHP)) throw runtime_error("Invalid input.");
    stringstream sin_eatk(from_database["ev_atk"]);
    if(not (sin_eatk >> evATK)) throw runtime_error("Invalid input.");
    stringstream sin_edef(from_database["ev_def"]);
    if(not (sin_edef >> evDEF)) throw runtime_error("Invalid input.");
    stringstream sin_esatk(from_database["ev_satk"]);
    if(not (sin_esatk >> evSATK)) throw runtime_error("Invalid input.");
    stringstream sin_esdef(from_database["ev_sdef"]);
    if(not (sin_esdef >> evSDEF)) throw runtime_error("Invalid input.");
    stringstream sin_espd(from_database["ev_spd"]);
    if(not (sin_espd >> evSPD)) throw runtime_error("Invalid input.");
    stringstream sin_exp(from_database["exp_yield"]);
    if(not (sin_exp >> exp_yield)) throw runtime_error("Invalid input.");

    //Need to be doubles
    stringstream sin_ht(from_database["height"]);
    if(not (sin_ht >> height)) throw runtime_error("Invalid input.");
    stringstream sin_wt(from_database["weight"]);
    if(not (sin_wt >> weight)) throw runtime_error("Invalid input.");
    stringstream sin_cm(from_database["chance_male"]);
    if(not (sin_cm >> chance_male)) throw runtime_error("Invalid input.");
    stringstream sin_cf(from_database["chance_female"]);
    if(not (sin_cf >> chance_female)) throw runtime_error("Invalid input.");
}

void base_pkmn::print()
{
    cout << boost::format("%s (#%d)") % display_name % nat_pokedex_num << endl;
    if(type2 == "None") cout << boost::format("Type: %s") % type1 << endl;
    else cout << boost::format("Type: %s/%s") % type1 % type2 << endl;
    cout << boost::format("Stats: %d,%d,%d,%d,%d,%d") %
                               baseHP % baseATK % baseDEF % baseSPD % baseSATK % baseSDEF << endl;
}

void base_pkmn::print_verbose()
{
    cout << boost::format("%s (#%d)") % display_name % nat_pokedex_num << endl;
    cout << boost::format("%s Pokemon") % species << endl;

    if(type2.empty()) cout << boost::format("Type: %s") % type1 << endl;
    else cout << boost::format("Type: %s/%s") % type1 % type2 << endl;

    if((ability2.empty()) && (ability3.empty())) cout << boost::format("Ability: %s") % ability1 << endl;
    else
    {
        cout << "Potential Abilities:" << endl;
        cout << boost::format(" - %s") % ability1 << endl;
        if(!ability2.empty()) cout << boost::format(" - %s") % ability2 << endl;
        if(!ability3.empty()) cout << boost::format(" - %s") % ability3 << endl;
    }

    cout << boost::format("%d m, %d kg") % height % weight << endl;
    if((chance_male + chance_female) == 0.0) cout << "Genderless" << endl;
    else cout << boost::format("%2.1f%s Male, %2.1f%s Female") % (chance_male*100) % "%" % (chance_female*100) % "%" << endl;

    cout << "Base Stats:" << endl;
    cout << boost::format(" - HP: %d") % baseHP << endl;
    cout << boost::format(" - Attack: %d") % baseATK << endl;
    cout << boost::format(" - Defense: %d") % baseDEF << endl;
    cout << boost::format(" - Speed: %d") % baseSPD << endl;
    cout << boost::format(" - Special Attack: %d") % baseSATK << endl;
    cout << boost::format(" - Special Defense: %d") % baseSDEF << endl;

    cout << "Effort Value Yields:" << endl;
    cout << boost::format(" - HP: %d") % evHP << endl;
    cout << boost::format(" - Attack: %d") % evATK << endl;
    cout << boost::format(" - Defense: %d") % evDEF << endl;
    cout << boost::format(" - Speed: %d") % evSPD << endl;
    cout << boost::format(" - Special Attack: %d") % evSATK << endl;
    cout << boost::format(" - Special Defense: %d") % evSDEF << endl;

    cout << boost::format("Experience Yield: %d") % exp_yield << endl;
}

string base_pkmn::get_display_name() {return display_name;}
int base_pkmn::get_nat_pokedex_num() {return nat_pokedex_num;}
string base_pkmn::get_species() {return species;}

string* base_pkmn::get_types()
{
    string *types = new string[2];
    types[0] = type1;
    types[1] = type2;
    return types;
}

string* base_pkmn::get_abilities()
{
    string *abilities = new string[3];
    abilities[0] = ability1;
    abilities[1] = ability2;
    abilities[2] = ability3;
    return abilities;
}

double base_pkmn::get_height() {return height;}
double base_pkmn::get_weight() {return weight;}
double base_pkmn::get_chance_male() {return chance_male;}
double base_pkmn::get_chance_female() {return chance_female;}

int* base_pkmn::get_base_stats()
{
    int *stats = new int[6];

    stats[0] = baseHP;
    stats[1] = baseATK;
    stats[2] = baseDEF;
    stats[3] = baseSPD;
    stats[4] = baseSATK;
    stats[5] = baseSDEF;

    return stats;
}

int* base_pkmn::get_ev_yields()
{
    int *yields = new int[7];

    yields[0] = evHP;
    yields[1] = evATK;
    yields[2] = evDEF;
    yields[3] = evSPD;
    yields[4] = evSATK;
    yields[5] = evSDEF;
    yields[6] = exp_yield;

    return yields;
}
//get_move_list

base_pkmn get_pokemon(string identifier)
{
    string db_fields[] = {"display_name","pokedex_num","species","type1","type2","ability1",
                          "ability2","ability3","height","weight","chance_male","chance_female",
                          "base_hp","base_atk","base_def","base_satk","base_sdef","base_spd",
                          "ev_hp","ev_atk","ev_def","ev_satk","ev_sdef","ev_spd","exp_yield"};
    map<string,string> from_database;
    SQLite::Database db("@PKMNSIM_PKG_DATA_PATH@/pkmnsim.db");

    transform(identifier.begin(), identifier.end(), identifier.begin(), ::tolower);
    for(int i = 0; i < 25; i++)
    {
        string query_string = str(boost::format("SELECT %s FROM pokedex WHERE identifier='%s'") % db_fields[i] % identifier);
        string result = db.execAndGet(query_string.c_str(), identifier);
        from_database[db_fields[i]] = result;
    }

    return base_pkmn(from_database);
}

vector<base_pkmn> get_pkmn_of_type(string type1, string type2, bool lax)
{
    vector<base_pkmn> pkmn_vector;
    SQLite::Database db("@PKMNSIM_PKG_DATA_PATH@/pkmnsim.db");
    string query_string;

    if(type2 == "None" and lax) query_string = str(boost::format("SELECT identifier FROM pokedex WHERE type1='%s'") % type1);
    else query_string = str(boost::format("SELECT identifier FROM pokedex WHERE type1='%s' AND type2='%s'") % type1 % type2);

    SQLite::Statement query(db, query_string.c_str());

    while(query.executeStep())
    {
        string identifier = query.getColumn(0);
        pkmn_vector.push_back(get_pokemon(identifier));
    }

    return pkmn_vector;
}
<commit_msg>base_pkmn: get_pokemon() doesn't care what order you give the type combination<commit_after>#include <iostream>
#include <map>
#include <stdexcept>
#include <stdio.h>
#include <sstream>
#include <string>
#include <boost/algorithm/string.hpp>
#include <boost/assign.hpp>
#include <boost/format.hpp>
#include <pkmnsim/base_pkmn.hpp>
#include <sqlitecpp/SQLiteCPP.h>

using namespace std;
typedef vector<string>::iterator str_iter;

base_pkmn::base_pkmn(string name, int num, string spec, string t1, string t2, string a1, string a2, string a3,
                        double ht, double wt, double cm, double cf,
                        int bhp, int batk, int bdef, int bsatk, int bsdef, int bspd,
                        int ehp, int eatk, int edef, int esatk, int esdef, int espd, int exp)
{
    display_name = name;
    nat_pokedex_num = num;
    species = spec;
    type1 = t1;
    type2 = t2;
    ability1 = a1;
    ability2 = a2;
    ability3 = a3;
    height = ht;
    weight = wt;
    chance_male = cm;
    chance_female = cf;
    baseHP = bhp;
    baseATK = batk;
    baseDEF = bdef;
    baseSATK = bsatk;
    baseSDEF = bsdef;
    baseSPD = bspd;
    evHP = ehp;
    evATK = eatk;
    evDEF = edef;
    evSATK = esatk;
    evSDEF = esdef;
    evSPD = espd;
    exp_yield = exp;
}

//Used when grabbing from databases
base_pkmn::base_pkmn(map<string,string> from_database)
{
    //Already strings
    display_name = from_database["display_name"];
    species = from_database["species"];
    type1 = from_database["type1"];
    type2 = from_database["type2"];
    ability1 = from_database["ability1"];
    ability2 = from_database["ability2"];
    ability3 = from_database["ability3"];

    //Need to be ints
    stringstream sin_num(from_database["pokedex_num"]);
    if(not (sin_num >> nat_pokedex_num)) throw runtime_error("Invalid input.");
    stringstream sin_bhp(from_database["base_hp"]);
    if(not (sin_bhp >> baseHP)) throw runtime_error("Invalid input.");
    stringstream sin_batk(from_database["base_atk"]);
    if(not (sin_batk >> baseATK)) throw runtime_error("Invalid input.");
    stringstream sin_bdef(from_database["base_def"]);
    if(not (sin_bdef >> baseDEF)) throw runtime_error("Invalid input.");
    stringstream sin_bsatk(from_database["base_satk"]);
    if(not (sin_bsatk >> baseSATK)) throw runtime_error("Invalid input.");
    stringstream sin_bsdef(from_database["base_sdef"]);
    if(not (sin_bsdef >> baseSDEF)) throw runtime_error("Invalid input.");
    stringstream sin_bspd(from_database["base_spd"]);
    if(not (sin_bspd >> baseSPD)) throw runtime_error("Invalid input.");
    stringstream sin_ehp(from_database["ev_hp"]);
    if(not (sin_ehp >> evHP)) throw runtime_error("Invalid input.");
    stringstream sin_eatk(from_database["ev_atk"]);
    if(not (sin_eatk >> evATK)) throw runtime_error("Invalid input.");
    stringstream sin_edef(from_database["ev_def"]);
    if(not (sin_edef >> evDEF)) throw runtime_error("Invalid input.");
    stringstream sin_esatk(from_database["ev_satk"]);
    if(not (sin_esatk >> evSATK)) throw runtime_error("Invalid input.");
    stringstream sin_esdef(from_database["ev_sdef"]);
    if(not (sin_esdef >> evSDEF)) throw runtime_error("Invalid input.");
    stringstream sin_espd(from_database["ev_spd"]);
    if(not (sin_espd >> evSPD)) throw runtime_error("Invalid input.");
    stringstream sin_exp(from_database["exp_yield"]);
    if(not (sin_exp >> exp_yield)) throw runtime_error("Invalid input.");

    //Need to be doubles
    stringstream sin_ht(from_database["height"]);
    if(not (sin_ht >> height)) throw runtime_error("Invalid input.");
    stringstream sin_wt(from_database["weight"]);
    if(not (sin_wt >> weight)) throw runtime_error("Invalid input.");
    stringstream sin_cm(from_database["chance_male"]);
    if(not (sin_cm >> chance_male)) throw runtime_error("Invalid input.");
    stringstream sin_cf(from_database["chance_female"]);
    if(not (sin_cf >> chance_female)) throw runtime_error("Invalid input.");
}

void base_pkmn::print()
{
    cout << boost::format("%s (#%d)") % display_name % nat_pokedex_num << endl;
    if(type2 == "None") cout << boost::format("Type: %s") % type1 << endl;
    else cout << boost::format("Type: %s/%s") % type1 % type2 << endl;
    cout << boost::format("Stats: %d,%d,%d,%d,%d,%d") %
                               baseHP % baseATK % baseDEF % baseSPD % baseSATK % baseSDEF << endl;
}

void base_pkmn::print_verbose()
{
    cout << boost::format("%s (#%d)") % display_name % nat_pokedex_num << endl;
    cout << boost::format("%s Pokemon") % species << endl;

    if(type2.empty()) cout << boost::format("Type: %s") % type1 << endl;
    else cout << boost::format("Type: %s/%s") % type1 % type2 << endl;

    if((ability2.empty()) && (ability3.empty())) cout << boost::format("Ability: %s") % ability1 << endl;
    else
    {
        cout << "Potential Abilities:" << endl;
        cout << boost::format(" - %s") % ability1 << endl;
        if(!ability2.empty()) cout << boost::format(" - %s") % ability2 << endl;
        if(!ability3.empty()) cout << boost::format(" - %s") % ability3 << endl;
    }

    cout << boost::format("%d m, %d kg") % height % weight << endl;
    if((chance_male + chance_female) == 0.0) cout << "Genderless" << endl;
    else cout << boost::format("%2.1f%s Male, %2.1f%s Female") % (chance_male*100) % "%" % (chance_female*100) % "%" << endl;

    cout << "Base Stats:" << endl;
    cout << boost::format(" - HP: %d") % baseHP << endl;
    cout << boost::format(" - Attack: %d") % baseATK << endl;
    cout << boost::format(" - Defense: %d") % baseDEF << endl;
    cout << boost::format(" - Speed: %d") % baseSPD << endl;
    cout << boost::format(" - Special Attack: %d") % baseSATK << endl;
    cout << boost::format(" - Special Defense: %d") % baseSDEF << endl;

    cout << "Effort Value Yields:" << endl;
    cout << boost::format(" - HP: %d") % evHP << endl;
    cout << boost::format(" - Attack: %d") % evATK << endl;
    cout << boost::format(" - Defense: %d") % evDEF << endl;
    cout << boost::format(" - Speed: %d") % evSPD << endl;
    cout << boost::format(" - Special Attack: %d") % evSATK << endl;
    cout << boost::format(" - Special Defense: %d") % evSDEF << endl;

    cout << boost::format("Experience Yield: %d") % exp_yield << endl;
}

string base_pkmn::get_display_name() {return display_name;}
int base_pkmn::get_nat_pokedex_num() {return nat_pokedex_num;}
string base_pkmn::get_species() {return species;}

string* base_pkmn::get_types()
{
    string *types = new string[2];
    types[0] = type1;
    types[1] = type2;
    return types;
}

string* base_pkmn::get_abilities()
{
    string *abilities = new string[3];
    abilities[0] = ability1;
    abilities[1] = ability2;
    abilities[2] = ability3;
    return abilities;
}

double base_pkmn::get_height() {return height;}
double base_pkmn::get_weight() {return weight;}
double base_pkmn::get_chance_male() {return chance_male;}
double base_pkmn::get_chance_female() {return chance_female;}

int* base_pkmn::get_base_stats()
{
    int *stats = new int[6];

    stats[0] = baseHP;
    stats[1] = baseATK;
    stats[2] = baseDEF;
    stats[3] = baseSPD;
    stats[4] = baseSATK;
    stats[5] = baseSDEF;

    return stats;
}

int* base_pkmn::get_ev_yields()
{
    int *yields = new int[7];

    yields[0] = evHP;
    yields[1] = evATK;
    yields[2] = evDEF;
    yields[3] = evSPD;
    yields[4] = evSATK;
    yields[5] = evSDEF;
    yields[6] = exp_yield;

    return yields;
}
//get_move_list

base_pkmn get_pokemon(string identifier)
{
    string db_fields[] = {"display_name","pokedex_num","species","type1","type2","ability1",
                          "ability2","ability3","height","weight","chance_male","chance_female",
                          "base_hp","base_atk","base_def","base_satk","base_sdef","base_spd",
                          "ev_hp","ev_atk","ev_def","ev_satk","ev_sdef","ev_spd","exp_yield"};
    map<string,string> from_database;
    SQLite::Database db("@PKMNSIM_PKG_DATA_PATH@/pkmnsim.db");

    transform(identifier.begin(), identifier.end(), identifier.begin(), ::tolower);
    for(int i = 0; i < 25; i++)
    {
        string query_string = str(boost::format("SELECT %s FROM pokedex WHERE identifier='%s'") % db_fields[i] % identifier);
        string result = db.execAndGet(query_string.c_str(), identifier);
        from_database[db_fields[i]] = result;
    }

    return base_pkmn(from_database);
}

vector<base_pkmn> get_pkmn_of_type(string type1, string type2, bool lax)
{
    vector<base_pkmn> pkmn_vector;
    SQLite::Database db("@PKMNSIM_PKG_DATA_PATH@/pkmnsim.db");
    string query_string;

    if(type2 == "None" and lax) query_string = str(boost::format("SELECT identifier FROM pokedex WHERE type1='%s' OR type2='%s'") % type1 % type1);
    else query_string = str(boost::format("SELECT identifier FROM pokedex WHERE (type1='%s' AND type2='%s') OR (type1='%s' AND type2='%s')")
                            % type1 % type2 % type2 % type1);

    SQLite::Statement query(db, query_string.c_str());

    while(query.executeStep())
    {
        string identifier = query.getColumn(0);
        pkmn_vector.push_back(get_pokemon(identifier));
    }

    return pkmn_vector;
}
<|endoftext|>


<commit_before>
#include <cppunit/XmlOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>

#include "SystemTest.h"

int main(int argc, char* argv[])
{
    CppUnit::TestResult controller;
    CppUnit::TestResultCollector result;
    controller.addListener(&result);

    // Get the top level suite from the registry
    CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();

    // Adds the test to the list of test to run
    CppUnit::TextUi::TestRunner runner;

    runner.addTest( suite );
    runner.run(controller);

    // Change the default outputter to a compiler error format
    // outputter
    std::ofstream xmlFileOut("cpptestresults.xml");
    CppUnit::XmlOutputter xmlOut(&result, xmlFileOut);
    xmlOut.write();

    // Return error code 1 if the one of test failed.
    return result.testFailuresTotal() ? 0 : 1;
}
<commit_msg>Send correct exit code on test failure.<commit_after>
#include <cppunit/XmlOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>

#include "SystemTest.h"

int main(int argc, char* argv[])
{
    CppUnit::TestResult controller;
    CppUnit::TestResultCollector result;
    controller.addListener(&result);

    // Get the top level suite from the registry
    CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();

    // Adds the test to the list of test to run
    CppUnit::TextUi::TestRunner runner;

    runner.addTest( suite );
    runner.run(controller);

    // Change the default outputter to a compiler error format
    // outputter
    std::ofstream xmlFileOut("cpptestresults.xml");
    CppUnit::XmlOutputter xmlOut(&result, xmlFileOut);
    xmlOut.write();

    // Return error code 1 if the one of test failed.
    return result.testFailuresTotal() ? 1 : 0 ;
}
<|endoftext|>


<commit_before>// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/fluid/inference/api/paddle_pass_builder.h"

#include <glog/logging.h>

namespace paddle {

void PaddlePassBuilder::AppendPass(const std::string &pass_type) {
  passes_.push_back(pass_type);
}

void PaddlePassBuilder::TurnOnDebug() {
  std::vector<std::string> passes;
  auto it = std::begin(passes_);
  while (it != std::end(passes_)) {
    if (*it != "graph_viz_pass") {
      it = passes_.insert(it + 1, "graph_viz_pass");
    } else {
      ++it;
    }
  }
}

std::string PaddlePassBuilder::DebugString() {
  std::stringstream ss;
  ss << "Passes to apply:\n";
  for (auto &pass : passes_) {
    ss << "  - " << pass << '\n';
  }
  return ss.str();
}

void PaddlePassBuilder::DeletePass(const std::string &pass_type) {
  auto it = std::begin(passes_);
  while (it != std::end(passes_)) {
    if (*it == pass_type) {
      it = passes_.erase(it);
    } else {
      ++it;
    }
  }
}

void PaddlePassBuilder::InsertPass(size_t idx, const std::string &pass_type) {
  passes_.insert(std::begin(passes_) + idx, pass_type);
}

void PaddlePassBuilder::DeletePass(size_t idx) {
  passes_.erase(std::begin(passes_) + idx);
}

void GpuPassStrategy::EnableMKLDNN() {
  LOG(ERROR) << "GPU not support MKLDNN yet";
}

GpuPassStrategy::GpuPassStrategy() : PassStrategy({}) {
  passes_.assign({
    "infer_clean_graph_pass",                        //
        "identity_scale_op_clean_pass",              //
        "conv_affine_channel_fuse_pass",             //
        "conv_eltwiseadd_affine_channel_fuse_pass",  //
        "conv_bn_fuse_pass",                         //
#if CUDNN_VERSION >= 7100  // To run conv_fusion, the version of cudnn must be
                           // guaranteed at least v7
        "conv_elementwise_add_act_fuse_pass",   //
        "conv_elementwise_add2_act_fuse_pass",  //
        "conv_elementwise_add_fuse_pass",       //
#endif
  });

  for (int i = 6; i >= 3; i--) {
    passes_.push_back("transpose_flatten" + std::to_string(i) +
                      "_concat_fuse_pass");
  }
  use_gpu_ = true;
}

void PaddlePassBuilder::AppendAnalysisPass(const std::string &pass) {
  analysis_passes_.push_back(pass);
}

CpuPassStrategy::CpuPassStrategy() : PassStrategy({}) {
  // NOTE the large fusions should be located in the front, so that they will
  // not be damaged by smaller ones.
  passes_.assign({
      "infer_clean_graph_pass",         //
      "attention_lstm_fuse_pass",       //
      "seqpool_concat_fuse_pass",       //
      "seqconv_eltadd_relu_fuse_pass",  //
      // "embedding_fc_lstm_fuse_pass", //
      "fc_lstm_fuse_pass",             //
      "mul_lstm_fuse_pass",            //
      "fc_gru_fuse_pass",              //
      "mul_gru_fuse_pass",             //
      "seq_concat_fc_fuse_pass",       //
      "fc_fuse_pass",                  //
      "repeated_fc_relu_fuse_pass",    //
      "squared_mat_sub_fuse_pass",     //
      "conv_bn_fuse_pass",             //
      "conv_eltwiseadd_bn_fuse_pass",  //
      "is_test_pass",                  //
      "identity_scale_op_clean_pass",  //
  });
  use_gpu_ = false;
}
}  // namespace paddle
<commit_msg>Add the include of cudnn.h to enable the use of CUDNN_VERSION. (#15961)<commit_after>// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/fluid/inference/api/paddle_pass_builder.h"
#ifdef PADDLE_WITH_CUDA
#include <cudnn.h>
#endif
#include <glog/logging.h>

namespace paddle {

void PaddlePassBuilder::AppendPass(const std::string &pass_type) {
  passes_.push_back(pass_type);
}

void PaddlePassBuilder::TurnOnDebug() {
  std::vector<std::string> passes;
  auto it = std::begin(passes_);
  while (it != std::end(passes_)) {
    if (*it != "graph_viz_pass") {
      it = passes_.insert(it + 1, "graph_viz_pass");
    } else {
      ++it;
    }
  }
}

std::string PaddlePassBuilder::DebugString() {
  std::stringstream ss;
  ss << "Passes to apply:\n";
  for (auto &pass : passes_) {
    ss << "  - " << pass << '\n';
  }
  return ss.str();
}

void PaddlePassBuilder::DeletePass(const std::string &pass_type) {
  auto it = std::begin(passes_);
  while (it != std::end(passes_)) {
    if (*it == pass_type) {
      it = passes_.erase(it);
    } else {
      ++it;
    }
  }
}

void PaddlePassBuilder::InsertPass(size_t idx, const std::string &pass_type) {
  passes_.insert(std::begin(passes_) + idx, pass_type);
}

void PaddlePassBuilder::DeletePass(size_t idx) {
  passes_.erase(std::begin(passes_) + idx);
}

void GpuPassStrategy::EnableMKLDNN() {
  LOG(ERROR) << "GPU not support MKLDNN yet";
}

GpuPassStrategy::GpuPassStrategy() : PassStrategy({}) {
  passes_.assign({
    "infer_clean_graph_pass",                        //
        "identity_scale_op_clean_pass",              //
        "conv_affine_channel_fuse_pass",             //
        "conv_eltwiseadd_affine_channel_fuse_pass",  //
        "conv_bn_fuse_pass",                         //
#if CUDNN_VERSION >= 7100  // To run conv_fusion, the version of cudnn must be
                           // guaranteed at least v7
        "conv_elementwise_add_act_fuse_pass",   //
        "conv_elementwise_add2_act_fuse_pass",  //
        "conv_elementwise_add_fuse_pass",       //
#endif
  });

  for (int i = 6; i >= 3; i--) {
    passes_.push_back("transpose_flatten" + std::to_string(i) +
                      "_concat_fuse_pass");
  }
  use_gpu_ = true;
}

void PaddlePassBuilder::AppendAnalysisPass(const std::string &pass) {
  analysis_passes_.push_back(pass);
}

CpuPassStrategy::CpuPassStrategy() : PassStrategy({}) {
  // NOTE the large fusions should be located in the front, so that they will
  // not be damaged by smaller ones.
  passes_.assign({
      "infer_clean_graph_pass",         //
      "attention_lstm_fuse_pass",       //
      "seqpool_concat_fuse_pass",       //
      "seqconv_eltadd_relu_fuse_pass",  //
      // "embedding_fc_lstm_fuse_pass", //
      "fc_lstm_fuse_pass",             //
      "mul_lstm_fuse_pass",            //
      "fc_gru_fuse_pass",              //
      "mul_gru_fuse_pass",             //
      "seq_concat_fc_fuse_pass",       //
      "fc_fuse_pass",                  //
      "repeated_fc_relu_fuse_pass",    //
      "squared_mat_sub_fuse_pass",     //
      "conv_bn_fuse_pass",             //
      "conv_eltwiseadd_bn_fuse_pass",  //
      "is_test_pass",                  //
      "identity_scale_op_clean_pass",  //
  });
  use_gpu_ = false;
}
}  // namespace paddle
<|endoftext|>


<commit_before>/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include <memory>
#include <string>
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_version_registry.h"

namespace paddle {
namespace operators {

class CudnnLSTMOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    OP_INOUT_CHECK(ctx->HasInput("Input"), "Input", "Input", "CudnnLSTM");
    OP_INOUT_CHECK(ctx->HasInput("InitH"), "Input", "InitH", "CudnnLSTM");
    OP_INOUT_CHECK(ctx->HasInput("InitC"), "Input", "InitC", "CudnnLSTM");

    OP_INOUT_CHECK(ctx->HasOutput("Reserve"), "Output", "Reserve", "CudnnLSTM");
    OP_INOUT_CHECK(ctx->HasOutput("StateOut"), "Output", "StateOut",
                   "CudnnLSTM");
    OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "CudnnLSTM");
    OP_INOUT_CHECK(ctx->HasOutput("LastH"), "Output", "LastH", "CudnnLSTM");
    OP_INOUT_CHECK(ctx->HasOutput("LastC"), "Output", "LastC", "CudnnLSTM");

    auto in_dims = ctx->GetInputDim("Input");
    auto init_h_dims = ctx->GetInputDim("InitH");
    auto init_c_dims = ctx->GetInputDim("InitC");

    PADDLE_ENFORCE_EQ(in_dims.size(), 3,
                      platform::errors::InvalidArgument(
                          "The rank of Input in CudnnLSTM  must be 3. But "
                          "received Input's rank is %d.",
                          in_dims.size()));
    PADDLE_ENFORCE_EQ(init_h_dims.size(), 3,
                      platform::errors::InvalidArgument(
                          "The rank of InitH in CudnnLSTM  must be 3. But "
                          "received InitH's rank is %d.",
                          init_h_dims.size()));

    if (ctx->HasInput("SequenceLength")) {
      auto seq_dims = ctx->GetInputDim("SequenceLength");
      PADDLE_ENFORCE_EQ(
          in_dims[1], seq_dims[0],
          platform::errors::InvalidArgument(
              "The size of SequenceLength has to equal the batch_size. But "
              "received batch_size is %d and the size of SequenceLength is %d.",
              in_dims[1], seq_dims[0]));
    }

    PADDLE_ENFORCE_EQ(
        in_dims[1], init_h_dims[1],
        platform::errors::InvalidArgument(
            "The in_dims[1] (Input dims) and init_h_dims[1] (InitH "
            "dims) should be equal. But "
            "received in_dims[1] is %d and init_h_dims[1] is %d.",
            in_dims[1], init_h_dims[1]));

    PADDLE_ENFORCE_EQ(init_c_dims, init_h_dims,
                      platform::errors::InvalidArgument(
                          "The InitC dims and InitH "
                          "dims should be equal. But "
                          "received init_c_dims is %d and init_h_dims is %d.",
                          init_c_dims, init_h_dims));

    auto out_dims = in_dims;
    auto hidden_size = ctx->Attrs().Get<int>("hidden_size");
    bool is_bidirec = ctx->Attrs().Get<bool>("is_bidirec");
    out_dims[2] = is_bidirec ? hidden_size * 2 : hidden_size;
    ctx->SetOutputDim("Out", out_dims);
    ctx->SetOutputDim("LastH", init_c_dims);
    ctx->SetOutputDim("LastC", init_h_dims);
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "Input"),
        ctx.device_context());
  }
};

class CudnnLSTMOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput(
        "Input",
        "(Tensor) RNN input tensor, which support variable-time length input "
        "sequence."
        "The shape of the Tensor MUST be ( seq_len * batch_size * input_size)"
        "seq_len is the total time step in this mini-batch (CAN be change in "
        "different batch)"
        "batch_size is the instance number of this batch"
        "input_size is the hidden size of the input."
        "input_size and the hidden_size in the next may not be same");
    AddInput("InitH",
             "(Tensor) the initial hidden state of the LSTM"
             "input. This is a tensor with shape (num_layers x batch_size x "
             "hidden_size)"
             "and When is_bidirec is True, the shape will be (num_layers*2 x "
             "batch_size x hidden_size)");
    AddInput("InitC",
             "(Tensor) the initial cell state of the LSTm "
             "input. This is a tensor with shape (num_layers x batch_size x "
             "hidden_size)"
             "and When is_bidirec is True, the shape will be (num_layers*2 x "
             "batch_size x hidden_size)");
    AddInput("W",
             "(Tensor) the learnable hidden-hidden weights."
             " The shape is (N), where N is total weight size of the LSTM. "
             " cudnn concatenate all the weight to one Tensor")
        .AsDispensable();
    AddInput("WeightList",
             "(vector<Tensor>), stores weight and bias data when the weight "
             "use the list format. ")
        .AsDispensable()
        .AsDuplicable();
    AddInput("SequenceLength",
             "(Tensor) When the input data is padding, "
             "set this parameter. This parameter represents "
             "the variable sequence lengths in a batch. "
             "The size of the vector has to equal the batch_size.")
        .AsDispensable();
    AddOutput("Reserve",
              "(Tensor, a temporary output Tensor to store the reserve_data "
              "of cudnn kernel.")
        .AsIntermediate();
    AddOutput("StateOut",
              "Share memory with State. "
              "Store the global drop state when training");
    AddOutput("Out",
              "(Tensor) the hidden state of LSTM operator. "
              "The shape is ( seq_len x batch_size x hidden_size) if "
              "is_bidirec is False"
              "and When is_bidirec is True, the shape will be ( seq_len x "
              "batch_size x hidden_size * 2) ");
    AddOutput("LastH",
              "(Tensor) the hidden state of the last step. "
              "The shape is ( num_layers x batch_size x hidden_size) if "
              "is_bidirec is False"
              "and When is_bidirec is True, the shape will be (num_layers*2 x "
              "batch_size x hidden_size)");
    AddOutput("LastC",
              "(Tensor) the cell state of the last step"
              "The shape is ( num_layers x batch_size x hidden_size) if "
              "is_bidirec is False"
              "and When is_bidirect is True, the shape will be (num_layers*2 x "
              "batch_size x hidden_size*2)");
    AddAttr<float>(
        "dropout_prob",
        "dropout prob of the dropout op"
        "the dropout ONLY work between lstm layers, not between time steps"
        "There is no dropout work on the Out tensor")
        .SetDefault(0.0);
    AddAttr<bool>("is_bidirec",
                  "is_bidirec"
                  "if it is bidirectional rnn"
                  "The will affect the shape of the Out, LastH, and LastC")
        .SetDefault(false);
    AddAttr<int>("input_size", "input size ot the Input Tensor").SetDefault(10);
    AddAttr<int>("hidden_size", "hidden size of the LSTM").SetDefault(100);
    AddAttr<int>("num_layers", "the total layer number of the LSTM")
        .SetDefault(1);
    AddAttr<bool>("is_test", "True if in test phase.").SetDefault(false);
    AddAttr<int>("seed", "seed to used if fix_seed is True").SetDefault(0);
    AddComment(R"DOC(
CUDNN LSTM implementation

A four-gate Long Short-Term Memory network with no peephole connections.
In the forward pass the output ht and cell output ct for a given iteration can be computed from the recurrent input ht-1,
the cell input ct-1 and the previous layer input xt given matrices W, R and biases bW, bR from the following equations:

$$ i_t = sigmoid(W_{ix}x_{t} + W_{ih}h_{t-1} + bx_i + bh_i) $$

$$ f_t = sigmoid(W_{fx}x_{t} + W_{fh}h_{t-1} + bx_f + bh_f) $$

$$ o_t = sigmoid(W_{ox}x_{t} + W_{oh}h_{t-1} + bx_o + bh_o) $$

$$ \\tilde{c_t} = tanh(W_{cx}x_t + W_{ch}h_{t-1} + bx_c + bh_c) $$

$$ c_t = f_t \\odot c_{t-1} + i_t \\odot \\tilde{c_t} $$

$$ h_t = o_t \\odot tanh(c_t) $$

- W terms denote weight matrices (e.g. $W_{ix}$ is the matrix
  of weights from the input gate to the input)
- The b terms denote bias vectors ($bx_i$ and $bh_i$ are the input gate bias vector).
- sigmoid is the logistic sigmoid function.
- $i, f, o$ and $c$ are the input gate, forget gate, output gate,
  and cell activation vectors, respectively, all of which have the same size as
  the cell output activation vector $h$.
- The $\odot$ is the element-wise product of the vectors.
- `tanh` is the activation functions.
- $\tilde{c_t}$ is also called candidate hidden state,
  which is computed based on the current input and the previous hidden state.

Where sigmoid is the sigmoid operator: sigmoid(x) = 1 / (1 + e^-x), * represents a point-wise multiplication,
X represensts a matrix multiplication

)DOC");
  }
};

class CudnnLSTMGradOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    OP_INOUT_CHECK(ctx->HasInput("Input"), "Input", "Input", "CudnnLSTMGrad");
    OP_INOUT_CHECK(ctx->HasInput("InitH"), "Input", "InitH", "CudnnLSTMGrad");
    OP_INOUT_CHECK(ctx->HasInput("InitC"), "Input", "InitC", "CudnnLSTMGrad");

    auto SetOutGradDim = [&ctx](const std::string& name) {
      auto g_name = framework::GradVarName(name);
      if (ctx->HasOutput(g_name)) {
        ctx->SetOutputDim(g_name, ctx->GetInputDim(name));
      }
    };

    SetOutGradDim("Input");
    if (ctx->HasInputs("WeightList")) {
      ctx->SetOutputsDim(framework::GradVarName("WeightList"),
                         ctx->GetInputsDim("WeightList"));
    }
    SetOutGradDim("InitH");
    SetOutGradDim("InitC");
  }
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
                                       ctx, framework::GradVarName("Out")),
                                   ctx.device_context());
  }
};

template <typename T>
class CudnnLSTMGradOpMaker : public framework::SingleGradOpMaker<T> {
 public:
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
  void Apply(GradOpPtr<T> op) const override {
    op->SetType("cudnn_lstm_grad");
    op->SetInput("Input", this->Input("Input"));
    op->SetInput("InitH", this->Input("InitH"));
    op->SetInput("InitC", this->Input("InitC"));
    if (this->HasInput("WeightList")) {
      op->SetInput("WeightList", this->Input("WeightList"));
    }
    if (this->HasInput("SequenceLength")) {
      op->SetInput("SequenceLength", this->Input("SequenceLength"));
    }
    op->SetInput("Reserve", this->Output("Reserve"));
    op->SetInput("StateOut", this->Output("StateOut"));
    op->SetInput("Out", this->Output("Out"));
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
    op->SetInput(framework::GradVarName("LastC"), this->OutputGrad("LastC"));
    op->SetInput(framework::GradVarName("LastH"), this->OutputGrad("LastH"));

    if (this->HasInput("WeightList")) {
      op->SetOutput(framework::GradVarName("WeightList"),
                    this->InputGrad("WeightList", false));
    }

    op->SetOutput(framework::GradVarName("Input"), this->InputGrad("Input"));
    op->SetOutput(framework::GradVarName("InitH"), this->InputGrad("InitH"));
    op->SetOutput(framework::GradVarName("InitC"), this->InputGrad("InitC"));
    op->SetAttrMap(this->Attrs());
  }
};

template <typename T>
class NotImpleKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    PADDLE_THROW(platform::errors::Unimplemented(
        "CPU is not support for this kernel now. Will be add in the future"));
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OPERATOR(cudnn_lstm, ops::CudnnLSTMOp, ops::CudnnLSTMOpMaker,
                  ops::CudnnLSTMGradOpMaker<paddle::framework::OpDesc>,
                  ops::CudnnLSTMGradOpMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(cudnn_lstm_grad, ops::CudnnLSTMGradOp);

REGISTER_OP_CPU_KERNEL(cudnn_lstm, ops::NotImpleKernel<float>);
REGISTER_OP_CPU_KERNEL(cudnn_lstm_grad, ops::NotImpleKernel<float>);

// TODO(Shixiaowei02) Add ModifyInput support
REGISTER_OP_VERSION(cudnn_lstm)
    .AddCheckpoint(
        R"ROC(
              Upgrade cudnn_lstm add a new input [WeightList] and modify input [W] to dispensable.)ROC",
        paddle::framework::compatible::OpVersionDesc()
            .NewInput(
                "WeightList",
                "The WeightList stores weight and bias data. WeightList is "
                "dispensable.")
            .NewInput("SequenceLength",
                      "When the input data is padding, set this parameter. "
                      "SequenceLength is dispensable.")
            .NewOutput("StateOut", "Store the global drop state when training")
            .NewOutput("Reserve",
                       "A temporary output Tensor to store the reserve_data"));
<commit_msg>add REGISTER_OP_VERSION for LSTM (#30038)<commit_after>/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include <memory>
#include <string>
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_version_registry.h"

namespace paddle {
namespace operators {

class CudnnLSTMOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    OP_INOUT_CHECK(ctx->HasInput("Input"), "Input", "Input", "CudnnLSTM");
    OP_INOUT_CHECK(ctx->HasInput("InitH"), "Input", "InitH", "CudnnLSTM");
    OP_INOUT_CHECK(ctx->HasInput("InitC"), "Input", "InitC", "CudnnLSTM");

    OP_INOUT_CHECK(ctx->HasOutput("Reserve"), "Output", "Reserve", "CudnnLSTM");
    OP_INOUT_CHECK(ctx->HasOutput("StateOut"), "Output", "StateOut",
                   "CudnnLSTM");
    OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "CudnnLSTM");
    OP_INOUT_CHECK(ctx->HasOutput("LastH"), "Output", "LastH", "CudnnLSTM");
    OP_INOUT_CHECK(ctx->HasOutput("LastC"), "Output", "LastC", "CudnnLSTM");

    auto in_dims = ctx->GetInputDim("Input");
    auto init_h_dims = ctx->GetInputDim("InitH");
    auto init_c_dims = ctx->GetInputDim("InitC");

    PADDLE_ENFORCE_EQ(in_dims.size(), 3,
                      platform::errors::InvalidArgument(
                          "The rank of Input in CudnnLSTM  must be 3. But "
                          "received Input's rank is %d.",
                          in_dims.size()));
    PADDLE_ENFORCE_EQ(init_h_dims.size(), 3,
                      platform::errors::InvalidArgument(
                          "The rank of InitH in CudnnLSTM  must be 3. But "
                          "received InitH's rank is %d.",
                          init_h_dims.size()));

    if (ctx->HasInput("SequenceLength")) {
      auto seq_dims = ctx->GetInputDim("SequenceLength");
      PADDLE_ENFORCE_EQ(
          in_dims[1], seq_dims[0],
          platform::errors::InvalidArgument(
              "The size of SequenceLength has to equal the batch_size. But "
              "received batch_size is %d and the size of SequenceLength is %d.",
              in_dims[1], seq_dims[0]));
    }

    PADDLE_ENFORCE_EQ(
        in_dims[1], init_h_dims[1],
        platform::errors::InvalidArgument(
            "The in_dims[1] (Input dims) and init_h_dims[1] (InitH "
            "dims) should be equal. But "
            "received in_dims[1] is %d and init_h_dims[1] is %d.",
            in_dims[1], init_h_dims[1]));

    PADDLE_ENFORCE_EQ(init_c_dims, init_h_dims,
                      platform::errors::InvalidArgument(
                          "The InitC dims and InitH "
                          "dims should be equal. But "
                          "received init_c_dims is %d and init_h_dims is %d.",
                          init_c_dims, init_h_dims));

    auto out_dims = in_dims;
    auto hidden_size = ctx->Attrs().Get<int>("hidden_size");
    bool is_bidirec = ctx->Attrs().Get<bool>("is_bidirec");
    out_dims[2] = is_bidirec ? hidden_size * 2 : hidden_size;
    ctx->SetOutputDim("Out", out_dims);
    ctx->SetOutputDim("LastH", init_c_dims);
    ctx->SetOutputDim("LastC", init_h_dims);
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "Input"),
        ctx.device_context());
  }
};

class CudnnLSTMOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput(
        "Input",
        "(Tensor) RNN input tensor, which support variable-time length input "
        "sequence."
        "The shape of the Tensor MUST be ( seq_len * batch_size * input_size)"
        "seq_len is the total time step in this mini-batch (CAN be change in "
        "different batch)"
        "batch_size is the instance number of this batch"
        "input_size is the hidden size of the input."
        "input_size and the hidden_size in the next may not be same");
    AddInput("InitH",
             "(Tensor) the initial hidden state of the LSTM"
             "input. This is a tensor with shape (num_layers x batch_size x "
             "hidden_size)"
             "and When is_bidirec is True, the shape will be (num_layers*2 x "
             "batch_size x hidden_size)");
    AddInput("InitC",
             "(Tensor) the initial cell state of the LSTm "
             "input. This is a tensor with shape (num_layers x batch_size x "
             "hidden_size)"
             "and When is_bidirec is True, the shape will be (num_layers*2 x "
             "batch_size x hidden_size)");
    AddInput("W",
             "(Tensor) the learnable hidden-hidden weights."
             " The shape is (N), where N is total weight size of the LSTM. "
             " cudnn concatenate all the weight to one Tensor")
        .AsDispensable();
    AddInput("WeightList",
             "(vector<Tensor>), stores weight and bias data when the weight "
             "use the list format. ")
        .AsDispensable()
        .AsDuplicable();
    AddInput("SequenceLength",
             "(Tensor) When the input data is padding, "
             "set this parameter. This parameter represents "
             "the variable sequence lengths in a batch. "
             "The size of the vector has to equal the batch_size.")
        .AsDispensable();
    AddOutput("Reserve",
              "(Tensor, a temporary output Tensor to store the reserve_data "
              "of cudnn kernel.")
        .AsIntermediate();
    AddOutput("StateOut",
              "Share memory with State. "
              "Store the global drop state when training");
    AddOutput("Out",
              "(Tensor) the hidden state of LSTM operator. "
              "The shape is ( seq_len x batch_size x hidden_size) if "
              "is_bidirec is False"
              "and When is_bidirec is True, the shape will be ( seq_len x "
              "batch_size x hidden_size * 2) ");
    AddOutput("LastH",
              "(Tensor) the hidden state of the last step. "
              "The shape is ( num_layers x batch_size x hidden_size) if "
              "is_bidirec is False"
              "and When is_bidirec is True, the shape will be (num_layers*2 x "
              "batch_size x hidden_size)");
    AddOutput("LastC",
              "(Tensor) the cell state of the last step"
              "The shape is ( num_layers x batch_size x hidden_size) if "
              "is_bidirec is False"
              "and When is_bidirect is True, the shape will be (num_layers*2 x "
              "batch_size x hidden_size*2)");
    AddAttr<float>(
        "dropout_prob",
        "dropout prob of the dropout op"
        "the dropout ONLY work between lstm layers, not between time steps"
        "There is no dropout work on the Out tensor")
        .SetDefault(0.0);
    AddAttr<bool>("is_bidirec",
                  "is_bidirec"
                  "if it is bidirectional rnn"
                  "The will affect the shape of the Out, LastH, and LastC")
        .SetDefault(false);
    AddAttr<int>("input_size", "input size ot the Input Tensor").SetDefault(10);
    AddAttr<int>("hidden_size", "hidden size of the LSTM").SetDefault(100);
    AddAttr<int>("num_layers", "the total layer number of the LSTM")
        .SetDefault(1);
    AddAttr<bool>("is_test", "True if in test phase.").SetDefault(false);
    AddAttr<int>("seed", "seed to used if fix_seed is True").SetDefault(0);
    AddComment(R"DOC(
CUDNN LSTM implementation

A four-gate Long Short-Term Memory network with no peephole connections.
In the forward pass the output ht and cell output ct for a given iteration can be computed from the recurrent input ht-1,
the cell input ct-1 and the previous layer input xt given matrices W, R and biases bW, bR from the following equations:

$$ i_t = sigmoid(W_{ix}x_{t} + W_{ih}h_{t-1} + bx_i + bh_i) $$

$$ f_t = sigmoid(W_{fx}x_{t} + W_{fh}h_{t-1} + bx_f + bh_f) $$

$$ o_t = sigmoid(W_{ox}x_{t} + W_{oh}h_{t-1} + bx_o + bh_o) $$

$$ \\tilde{c_t} = tanh(W_{cx}x_t + W_{ch}h_{t-1} + bx_c + bh_c) $$

$$ c_t = f_t \\odot c_{t-1} + i_t \\odot \\tilde{c_t} $$

$$ h_t = o_t \\odot tanh(c_t) $$

- W terms denote weight matrices (e.g. $W_{ix}$ is the matrix
  of weights from the input gate to the input)
- The b terms denote bias vectors ($bx_i$ and $bh_i$ are the input gate bias vector).
- sigmoid is the logistic sigmoid function.
- $i, f, o$ and $c$ are the input gate, forget gate, output gate,
  and cell activation vectors, respectively, all of which have the same size as
  the cell output activation vector $h$.
- The $\odot$ is the element-wise product of the vectors.
- `tanh` is the activation functions.
- $\tilde{c_t}$ is also called candidate hidden state,
  which is computed based on the current input and the previous hidden state.

Where sigmoid is the sigmoid operator: sigmoid(x) = 1 / (1 + e^-x), * represents a point-wise multiplication,
X represensts a matrix multiplication

)DOC");
  }
};

class CudnnLSTMGradOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    OP_INOUT_CHECK(ctx->HasInput("Input"), "Input", "Input", "CudnnLSTMGrad");
    OP_INOUT_CHECK(ctx->HasInput("InitH"), "Input", "InitH", "CudnnLSTMGrad");
    OP_INOUT_CHECK(ctx->HasInput("InitC"), "Input", "InitC", "CudnnLSTMGrad");

    auto SetOutGradDim = [&ctx](const std::string& name) {
      auto g_name = framework::GradVarName(name);
      if (ctx->HasOutput(g_name)) {
        ctx->SetOutputDim(g_name, ctx->GetInputDim(name));
      }
    };

    SetOutGradDim("Input");
    if (ctx->HasInputs("WeightList")) {
      ctx->SetOutputsDim(framework::GradVarName("WeightList"),
                         ctx->GetInputsDim("WeightList"));
    }
    SetOutGradDim("InitH");
    SetOutGradDim("InitC");
  }
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
                                       ctx, framework::GradVarName("Out")),
                                   ctx.device_context());
  }
};

template <typename T>
class CudnnLSTMGradOpMaker : public framework::SingleGradOpMaker<T> {
 public:
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
  void Apply(GradOpPtr<T> op) const override {
    op->SetType("cudnn_lstm_grad");
    op->SetInput("Input", this->Input("Input"));
    op->SetInput("InitH", this->Input("InitH"));
    op->SetInput("InitC", this->Input("InitC"));
    if (this->HasInput("WeightList")) {
      op->SetInput("WeightList", this->Input("WeightList"));
    }
    if (this->HasInput("SequenceLength")) {
      op->SetInput("SequenceLength", this->Input("SequenceLength"));
    }
    op->SetInput("Reserve", this->Output("Reserve"));
    op->SetInput("StateOut", this->Output("StateOut"));
    op->SetInput("Out", this->Output("Out"));
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
    op->SetInput(framework::GradVarName("LastC"), this->OutputGrad("LastC"));
    op->SetInput(framework::GradVarName("LastH"), this->OutputGrad("LastH"));

    if (this->HasInput("WeightList")) {
      op->SetOutput(framework::GradVarName("WeightList"),
                    this->InputGrad("WeightList", false));
    }

    op->SetOutput(framework::GradVarName("Input"), this->InputGrad("Input"));
    op->SetOutput(framework::GradVarName("InitH"), this->InputGrad("InitH"));
    op->SetOutput(framework::GradVarName("InitC"), this->InputGrad("InitC"));
    op->SetAttrMap(this->Attrs());
  }
};

template <typename T>
class NotImpleKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    PADDLE_THROW(platform::errors::Unimplemented(
        "CPU is not support for this kernel now. Will be add in the future"));
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OPERATOR(cudnn_lstm, ops::CudnnLSTMOp, ops::CudnnLSTMOpMaker,
                  ops::CudnnLSTMGradOpMaker<paddle::framework::OpDesc>,
                  ops::CudnnLSTMGradOpMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(cudnn_lstm_grad, ops::CudnnLSTMGradOp);

REGISTER_OP_CPU_KERNEL(cudnn_lstm, ops::NotImpleKernel<float>);
REGISTER_OP_CPU_KERNEL(cudnn_lstm_grad, ops::NotImpleKernel<float>);

// TODO(Shixiaowei02) Add ModifyInput support
REGISTER_OP_VERSION(cudnn_lstm)
    .AddCheckpoint(
        R"ROC(
              Upgrade cudnn_lstm add new inputs [WeightList, SequenceLength], modify the input [W] to dispensable, delete the input [Cache].
              Upgrade cudnn_lstm add new outputs [StateOut, Reserve, LastC, LastH], delete output [last_c, last_h].
              Upgrade cudnn_lstm modify the attr [seed] default value to 0, delete the attr [max_len].)ROC",
        paddle::framework::compatible::OpVersionDesc()
            .NewInput(
                "WeightList",
                "The WeightList stores weight and bias data. WeightList is "
                "dispensable.")
            .NewInput("SequenceLength",
                      "When the input data is padding, set this parameter. "
                      "SequenceLength is dispensable.")
            .ModifyInput("W",
                         "The new LSTM use WeightList instead of W. The W "
                         "concatenate all the weight to one Tensor.")
            .DeleteInput("Cache",
                         "The new LSTM use the Reserve Output to store the "
                         "data of dropout.")
            .NewOutput("StateOut", "Store the global drop state when training")
            .NewOutput("Reserve",
                       "A temporary output Tensor to store the reserve_data")
            .DeleteOutput(
                "last_c",
                "Modify the name of the output from 'last_c' to 'LastC'.")
            .NewOutput("LastC", "The cell state of the last step.")
            .DeleteOutput(
                "last_h",
                "Modify the name of the output from 'last_h' to 'LastH'.")
            .NewOutput("LastH", "The hidden state of the last step.")
            .ModifyAttr("seed",
                        "Set the default value of seed from '-1' to '0'.", 0)
            .DeleteAttr("max_len",
                        "The length of Inputs is achieved form the input data "
                        "which is difficult to know the information in "
                        "advance."));
<|endoftext|>


<commit_before>/*
===========================================================================

Daemon GPL Source Code
Copyright (C) 2012 Unvanquished Developers

This file is part of the Daemon GPL Source Code (Daemon Source Code).

Daemon Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Daemon Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Daemon Source Code.  If not, see <http://www.gnu.org/licenses/>.

In addition, the Daemon Source Code is also subject to certain additional terms.
You should have received a copy of these additional terms immediately following the
terms and conditions of the GNU General Public License which accompanied the Daemon
Source Code.  If not, please request a copy in writing from id Software at the address
below.

If you have questions concerning this license or the applicable additional terms, you
may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville,
Maryland 20850 USA.

===========================================================================
*/
extern "C"
{
	#include "../server/server.h"
}
#include "bot_local.h"

/*
====================
bot_local.cpp

All vectors used as inputs and outputs to functions here are assumed to
use detour/recast's coordinate system

Callers should use recast2quake() and quake2recast() where appropriate to
make sure the vectors use the same coordinate system
====================
*/

void BotCalcSteerDir( Bot_t *bot, vec3_t dir )
{
	const int ip0 = 0;
	const int ip1 = MIN( 1, bot->numCorners - 1 );
	const float* p0 = &bot->cornerVerts[ ip0 * 3 ];
	const float* p1 = &bot->cornerVerts[ ip1 * 3 ];
	vec3_t dir0, dir1;
	float len0, len1;
	vec3_t spos;

	VectorCopy( bot->corridor.getPos(), spos );

	VectorSubtract( p0, spos, dir0 );
	VectorSubtract( p1, spos, dir1 );
	dir0[ 1 ] = 0;
	dir1[ 1 ] = 0;

	len0 = VectorLength( dir0 );
	len1 = VectorLength( dir1 );
	if ( len1 > 0.001f )
	{
		VectorScale(dir1, 1.0f / len1, dir1);
	}

	dir[ 0 ] = dir0[ 0 ] - dir1[ 0 ]*len0*0.5f;
	dir[ 1 ] = 0;
	dir[ 2 ] = dir0[ 2 ] - dir1[ 2 ]*len0*0.5f;

	VectorNormalize( dir );
}

void FindWaypoints( Bot_t *bot, float *corners, unsigned char *cornerFlags, dtPolyRef *cornerPolys, int *numCorners, int maxCorners )
{
	if ( !bot->corridor.getPathCount() )
	{
		Com_Printf( S_COLOR_RED "ERROR: FindWaypoints Called without a path!\n" );
		*numCorners = 0;
		return;
	}

	*numCorners = bot->corridor.findCorners( corners, cornerFlags, cornerPolys, maxCorners, bot->nav->query, &bot->nav->filter );
}

qboolean PointInPoly( Bot_t *bot, dtPolyRef ref, const vec3_t point )
{
	vec3_t closest;

	if ( dtStatusFailed( bot->nav->query->closestPointOnPolyBoundary( ref, point, closest ) ) )
	{
		return qfalse;
	}

	// use the bot's bbox as an epsilon because the navmesh is always at least that far from a boundry
	sharedEntity_t *ent = SV_GentityNum( bot->clientNum );
	float maxRad = MAX( ent->r.maxs[ 0 ], ent->r.maxs[ 1 ] );

	if ( fabsf( point[ 0 ] - closest[ 0 ] ) > maxRad )
	{
		return qfalse;
	}

	if ( fabsf( point[ 2 ] - closest[ 2 ] ) > maxRad )
	{
		return qfalse;
	}

	return qtrue;
}

qboolean BotFindNearestPoly( Bot_t *bot, const vec3_t coord, dtPolyRef *nearestPoly, vec3_t nearPoint )
{
	vec3_t start, extents;
	dtStatus status;
	dtNavMeshQuery* navQuery = bot->nav->query;
	dtQueryFilter* navFilter = &bot->nav->filter;

	VectorSet( extents, 640, 96, 640 );
	VectorCopy( coord, start );

	status = navQuery->findNearestPoly( start, extents, navFilter, nearestPoly, nearPoint );
	if ( dtStatusFailed( status ) || *nearestPoly == 0 )
	{
		//try larger extents
		extents[ 1 ] += 900;
		status = navQuery->findNearestPoly( start, extents, navFilter, nearestPoly, nearPoint );
		if ( dtStatusFailed( status ) || *nearestPoly == 0 )
		{
			return qfalse; // failed
		}
	}
	return qtrue;
}

unsigned int FindRoute( Bot_t *bot, const vec3_t s, const vec3_t e )
{
	vec3_t start;
	vec3_t end;
	dtPolyRef startRef, endRef = 1;
	dtPolyRef pathPolys[ MAX_BOT_PATH ];
	dtStatus status;
	int pathNumPolys;
	qboolean result;
	int time = Sys_Milliseconds();

	//dont pathfind too much
	if ( time - bot->lastRouteTime < 200 )
	{
		return ROUTE_FAILED;
	}

	bot->lastRouteTime = time;

	result = BotFindNearestPoly( bot, s, &startRef, start );

	if ( !result )
	{
		return ROUTE_FAILED;
	}

	result = BotFindNearestPoly( bot, e, &endRef, end );

	if ( !result )
	{
		return ROUTE_FAILED;
	}

	status = bot->nav->query->findPath( startRef, endRef, start, end, &bot->nav->filter, pathPolys, &pathNumPolys, MAX_BOT_PATH );

	if ( dtStatusFailed( status ) )
	{
		return ROUTE_FAILED;
	}

	bot->corridor.reset( startRef, start );
	bot->corridor.setCorridor( end, pathPolys, pathNumPolys );

	if ( status & DT_PARTIAL_RESULT )
	{
		return ROUTE_SUCCEED | ROUTE_PARTIAL;
	}

	bot->needReplan = qfalse;
	return ROUTE_SUCCEED;
}<commit_msg>Only set lastRouteTime after we have found start/end polys<commit_after>/*
===========================================================================

Daemon GPL Source Code
Copyright (C) 2012 Unvanquished Developers

This file is part of the Daemon GPL Source Code (Daemon Source Code).

Daemon Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Daemon Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Daemon Source Code.  If not, see <http://www.gnu.org/licenses/>.

In addition, the Daemon Source Code is also subject to certain additional terms.
You should have received a copy of these additional terms immediately following the
terms and conditions of the GNU General Public License which accompanied the Daemon
Source Code.  If not, please request a copy in writing from id Software at the address
below.

If you have questions concerning this license or the applicable additional terms, you
may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville,
Maryland 20850 USA.

===========================================================================
*/
extern "C"
{
	#include "../server/server.h"
}
#include "bot_local.h"

/*
====================
bot_local.cpp

All vectors used as inputs and outputs to functions here are assumed to
use detour/recast's coordinate system

Callers should use recast2quake() and quake2recast() where appropriate to
make sure the vectors use the same coordinate system
====================
*/

void BotCalcSteerDir( Bot_t *bot, vec3_t dir )
{
	const int ip0 = 0;
	const int ip1 = MIN( 1, bot->numCorners - 1 );
	const float* p0 = &bot->cornerVerts[ ip0 * 3 ];
	const float* p1 = &bot->cornerVerts[ ip1 * 3 ];
	vec3_t dir0, dir1;
	float len0, len1;
	vec3_t spos;

	VectorCopy( bot->corridor.getPos(), spos );

	VectorSubtract( p0, spos, dir0 );
	VectorSubtract( p1, spos, dir1 );
	dir0[ 1 ] = 0;
	dir1[ 1 ] = 0;

	len0 = VectorLength( dir0 );
	len1 = VectorLength( dir1 );
	if ( len1 > 0.001f )
	{
		VectorScale(dir1, 1.0f / len1, dir1);
	}

	dir[ 0 ] = dir0[ 0 ] - dir1[ 0 ]*len0*0.5f;
	dir[ 1 ] = 0;
	dir[ 2 ] = dir0[ 2 ] - dir1[ 2 ]*len0*0.5f;

	VectorNormalize( dir );
}

void FindWaypoints( Bot_t *bot, float *corners, unsigned char *cornerFlags, dtPolyRef *cornerPolys, int *numCorners, int maxCorners )
{
	if ( !bot->corridor.getPathCount() )
	{
		Com_Printf( S_COLOR_RED "ERROR: FindWaypoints Called without a path!\n" );
		*numCorners = 0;
		return;
	}

	*numCorners = bot->corridor.findCorners( corners, cornerFlags, cornerPolys, maxCorners, bot->nav->query, &bot->nav->filter );
}

qboolean PointInPoly( Bot_t *bot, dtPolyRef ref, const vec3_t point )
{
	vec3_t closest;

	if ( dtStatusFailed( bot->nav->query->closestPointOnPolyBoundary( ref, point, closest ) ) )
	{
		return qfalse;
	}

	// use the bot's bbox as an epsilon because the navmesh is always at least that far from a boundry
	sharedEntity_t *ent = SV_GentityNum( bot->clientNum );
	float maxRad = MAX( ent->r.maxs[ 0 ], ent->r.maxs[ 1 ] );

	if ( fabsf( point[ 0 ] - closest[ 0 ] ) > maxRad )
	{
		return qfalse;
	}

	if ( fabsf( point[ 2 ] - closest[ 2 ] ) > maxRad )
	{
		return qfalse;
	}

	return qtrue;
}

qboolean BotFindNearestPoly( Bot_t *bot, const vec3_t coord, dtPolyRef *nearestPoly, vec3_t nearPoint )
{
	vec3_t start, extents;
	dtStatus status;
	dtNavMeshQuery* navQuery = bot->nav->query;
	dtQueryFilter* navFilter = &bot->nav->filter;

	VectorSet( extents, 640, 96, 640 );
	VectorCopy( coord, start );

	status = navQuery->findNearestPoly( start, extents, navFilter, nearestPoly, nearPoint );
	if ( dtStatusFailed( status ) || *nearestPoly == 0 )
	{
		//try larger extents
		extents[ 1 ] += 900;
		status = navQuery->findNearestPoly( start, extents, navFilter, nearestPoly, nearPoint );
		if ( dtStatusFailed( status ) || *nearestPoly == 0 )
		{
			return qfalse; // failed
		}
	}
	return qtrue;
}

unsigned int FindRoute( Bot_t *bot, const vec3_t s, const vec3_t e )
{
	vec3_t start;
	vec3_t end;
	dtPolyRef startRef, endRef = 1;
	dtPolyRef pathPolys[ MAX_BOT_PATH ];
	dtStatus status;
	int pathNumPolys;
	qboolean result;
	int time = svs.time;

	//dont pathfind too much
	if ( time - bot->lastRouteTime < 200 )
	{
		return ROUTE_FAILED;
	}

	result = BotFindNearestPoly( bot, s, &startRef, start );

	if ( !result )
	{
		return ROUTE_FAILED;
	}

	result = BotFindNearestPoly( bot, e, &endRef, end );

	if ( !result )
	{
		return ROUTE_FAILED;
	}

	bot->lastRouteTime = time;
	status = bot->nav->query->findPath( startRef, endRef, start, end, &bot->nav->filter, pathPolys, &pathNumPolys, MAX_BOT_PATH );

	if ( dtStatusFailed( status ) )
	{
		return ROUTE_FAILED;
	}

	bot->corridor.reset( startRef, start );
	bot->corridor.setCorridor( end, pathPolys, pathNumPolys );

	if ( dtStatusDetail( status, DT_PARTIAL_RESULT ) )
	{
		return ROUTE_SUCCEED | ROUTE_PARTIAL;
	}

	bot->needReplan = qfalse;
	return ROUTE_SUCCEED;
}<|endoftext|>


<commit_before>///////////////////////////////////////////////////////////////////////////////////
/// OpenGL Mathematics (glm.g-truc.net)
///
/// Copyright (c) 2005 - 2012 G-Truc Creation (www.g-truc.net)
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
///
/// @ref core
/// @file glm/core/dummy.cpp
/// @date 2011-01-19 / 2011-06-15
/// @author Christophe Riccio
///
/// GLM is a header only library. There is nothing to compile.
/// dummy.cpp exist only a wordaround for CMake file.
///////////////////////////////////////////////////////////////////////////////////

#define GLM_MESSAGES
#include "../glm.hpp"
#include "../ext.hpp"

//#error "GLM is a header only library"

int main()
{

}
<commit_msg>Remove dummy file causing glm compilation to fail<commit_after><|endoftext|>


<commit_before>// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/fastos/fastos.h>

#include "predicate_tree_analyzer.h"
#include <vespa/document/predicate/predicate.h>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vespa/log/log.h>

LOG_SETUP(".predicate_tree_analyzer");

using document::Predicate;
using std::map;
using std::min;
using std::string;
using vespalib::slime::Inspector;
using vespalib::slime::Memory;

namespace search {
namespace predicate {
namespace {
long getType(const Inspector &in, bool negated) {
    long type = in[Predicate::NODE_TYPE].asLong();
    if (negated) {
        if (type == Predicate::TYPE_CONJUNCTION) {
            return Predicate::TYPE_DISJUNCTION;
        } else if (type == Predicate::TYPE_DISJUNCTION) {
            return Predicate::TYPE_CONJUNCTION;
        }
    }
    return type;
}

void createOrIncrease(map<string, int> &counts, const string &key) {
    auto it = counts.find(key);
    if (it == counts.end()) {
        counts.insert(make_pair(key, 1));
    } else {
        ++(it->second);
    }
}
}  // namespace

void PredicateTreeAnalyzer::traverseTree(const Inspector &in) {
    switch (getType(in, _negated)) {
    case Predicate::TYPE_NEGATION:
        assert(in[Predicate::CHILDREN].children() == 1);
        _negated = !_negated;
        traverseTree(in[Predicate::CHILDREN][0]);
        _negated = !_negated;
        return;
    case Predicate::TYPE_CONJUNCTION: {
        int crumb_size = _crumbs.size();
        int size = 0;
        for (size_t i = 0; i < in[Predicate::CHILDREN].children(); ++i) {
            _crumbs.setChild(i, 'a');
            traverseTree(in[Predicate::CHILDREN][i]);
            size += _size;
            _size_map.insert(make_pair(_crumbs.getCrumb(), _size));
            _crumbs.resize(crumb_size);
        }
        _size = size;
        return;
    }
    case Predicate::TYPE_DISJUNCTION: {
        int crumb_size = _crumbs.size();
        int size = 0;
        for (size_t i = 0; i < in[Predicate::CHILDREN].children(); ++i) {
            _crumbs.setChild(i, 'o');
            traverseTree(in[Predicate::CHILDREN][i]);
            size += _size;
            _crumbs.resize(crumb_size);
        }
        _size = size;
        return;
    }
    case Predicate::TYPE_FEATURE_SET:
        if (_negated) {
            _size = 2;
            _has_not = true;
        } else {
            _size = 1;
            Memory label_mem = in[Predicate::KEY].asString();
            string label(label_mem.data, label_mem.size);
            label.push_back('=');
            const size_t prefix_size = label.size();
            for (size_t i = 0; i < in[Predicate::SET].children(); ++i) {
                Memory value = in[Predicate::SET][i].asString();
                label.resize(prefix_size);
                label.append(value.data, value.size);
                createOrIncrease(_key_counts, label);
            }
        }
        return;
    case Predicate::TYPE_FEATURE_RANGE: {
        if (_negated) {
            _size = 2;
            _has_not = true;
        } else {
            _size = 1;
            string key = in[Predicate::KEY].asString().make_string();
            createOrIncrease(_key_counts, key);
        }
    }
    }  // switch
}

float PredicateTreeAnalyzer::findMinFeature(const Inspector &in) {
    float min_feature = 0.0f;
    switch (getType(in, _negated)) {
    case Predicate::TYPE_CONJUNCTION:  // sum of children
        for (size_t i = 0; i < in[Predicate::CHILDREN].children(); ++i) {
            min_feature += findMinFeature(in[Predicate::CHILDREN][i]);
        }
        return min_feature;
    case Predicate::TYPE_DISJUNCTION:  // min of children
        min_feature = findMinFeature(in[Predicate::CHILDREN][0]);
        for (size_t i = 1; i < in[Predicate::CHILDREN].children(); ++i) {
            min_feature = min(min_feature,
                              findMinFeature(in[Predicate::CHILDREN][i]));
        }
        return min_feature;
    case Predicate::TYPE_NEGATION:  // == child
        assert(in[Predicate::CHILDREN].children() == 1);
        _negated = !_negated;
        min_feature = findMinFeature(in[Predicate::CHILDREN][0]);
        _negated = !_negated;
        return min_feature;
    case Predicate::TYPE_FEATURE_SET: {
        if (_negated) {
            return 0.0f;
        }
        Memory label_mem = in[Predicate::KEY].asString();
        string label(label_mem.data, label_mem.size);
        label.push_back('=');
        const size_t prefix_size = label.size();
        min_feature = 1.0f;
        for (size_t i = 0; i < in[Predicate::SET].children(); ++i) {
            Memory value = in[Predicate::SET][i].asString();
            label.resize(prefix_size);
            label.append(value.data, value.size);
            auto it = _key_counts.find(label);
            assert(it != _key_counts.end());
            min_feature = min(min_feature, 1.0f / it->second);
        }
        return min_feature;
    }
    case Predicate::TYPE_FEATURE_RANGE: {
        if (_negated) {
            return 0.0f;
        }
        string key = in[Predicate::KEY].asString().make_string();
        auto it = _key_counts.find(key);
        assert(it != _key_counts.end());
        return 1.0f / it->second;
    }
    }  // switch
    return 0.0f;
}

PredicateTreeAnalyzer::PredicateTreeAnalyzer(const Inspector &in) : _has_not(false), _negated(false) {
    traverseTree(in);
    _min_feature = static_cast<int>(std::ceil(findMinFeature(in)) + (_has_not? 1.0 : 0.0));
}

}  // namespace predicate
}  // namespace search
<commit_msg>keep "float" semantics<commit_after>// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/fastos/fastos.h>

#include "predicate_tree_analyzer.h"
#include <vespa/document/predicate/predicate.h>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vespa/log/log.h>

LOG_SETUP(".predicate_tree_analyzer");

using document::Predicate;
using std::map;
using std::min;
using std::string;
using vespalib::slime::Inspector;
using vespalib::slime::Memory;

namespace search {
namespace predicate {
namespace {
long getType(const Inspector &in, bool negated) {
    long type = in[Predicate::NODE_TYPE].asLong();
    if (negated) {
        if (type == Predicate::TYPE_CONJUNCTION) {
            return Predicate::TYPE_DISJUNCTION;
        } else if (type == Predicate::TYPE_DISJUNCTION) {
            return Predicate::TYPE_CONJUNCTION;
        }
    }
    return type;
}

void createOrIncrease(map<string, int> &counts, const string &key) {
    auto it = counts.find(key);
    if (it == counts.end()) {
        counts.insert(make_pair(key, 1));
    } else {
        ++(it->second);
    }
}
}  // namespace

void PredicateTreeAnalyzer::traverseTree(const Inspector &in) {
    switch (getType(in, _negated)) {
    case Predicate::TYPE_NEGATION:
        assert(in[Predicate::CHILDREN].children() == 1);
        _negated = !_negated;
        traverseTree(in[Predicate::CHILDREN][0]);
        _negated = !_negated;
        return;
    case Predicate::TYPE_CONJUNCTION: {
        int crumb_size = _crumbs.size();
        int size = 0;
        for (size_t i = 0; i < in[Predicate::CHILDREN].children(); ++i) {
            _crumbs.setChild(i, 'a');
            traverseTree(in[Predicate::CHILDREN][i]);
            size += _size;
            _size_map.insert(make_pair(_crumbs.getCrumb(), _size));
            _crumbs.resize(crumb_size);
        }
        _size = size;
        return;
    }
    case Predicate::TYPE_DISJUNCTION: {
        int crumb_size = _crumbs.size();
        int size = 0;
        for (size_t i = 0; i < in[Predicate::CHILDREN].children(); ++i) {
            _crumbs.setChild(i, 'o');
            traverseTree(in[Predicate::CHILDREN][i]);
            size += _size;
            _crumbs.resize(crumb_size);
        }
        _size = size;
        return;
    }
    case Predicate::TYPE_FEATURE_SET:
        if (_negated) {
            _size = 2;
            _has_not = true;
        } else {
            _size = 1;
            Memory label_mem = in[Predicate::KEY].asString();
            string label(label_mem.data, label_mem.size);
            label.push_back('=');
            const size_t prefix_size = label.size();
            for (size_t i = 0; i < in[Predicate::SET].children(); ++i) {
                Memory value = in[Predicate::SET][i].asString();
                label.resize(prefix_size);
                label.append(value.data, value.size);
                createOrIncrease(_key_counts, label);
            }
        }
        return;
    case Predicate::TYPE_FEATURE_RANGE: {
        if (_negated) {
            _size = 2;
            _has_not = true;
        } else {
            _size = 1;
            string key = in[Predicate::KEY].asString().make_string();
            createOrIncrease(_key_counts, key);
        }
    }
    }  // switch
}

float PredicateTreeAnalyzer::findMinFeature(const Inspector &in) {
    float min_feature = 0.0f;
    switch (getType(in, _negated)) {
    case Predicate::TYPE_CONJUNCTION:  // sum of children
        for (size_t i = 0; i < in[Predicate::CHILDREN].children(); ++i) {
            min_feature += findMinFeature(in[Predicate::CHILDREN][i]);
        }
        return min_feature;
    case Predicate::TYPE_DISJUNCTION:  // min of children
        min_feature = findMinFeature(in[Predicate::CHILDREN][0]);
        for (size_t i = 1; i < in[Predicate::CHILDREN].children(); ++i) {
            min_feature = min(min_feature,
                              findMinFeature(in[Predicate::CHILDREN][i]));
        }
        return min_feature;
    case Predicate::TYPE_NEGATION:  // == child
        assert(in[Predicate::CHILDREN].children() == 1);
        _negated = !_negated;
        min_feature = findMinFeature(in[Predicate::CHILDREN][0]);
        _negated = !_negated;
        return min_feature;
    case Predicate::TYPE_FEATURE_SET: {
        if (_negated) {
            return 0.0f;
        }
        Memory label_mem = in[Predicate::KEY].asString();
        string label(label_mem.data, label_mem.size);
        label.push_back('=');
        const size_t prefix_size = label.size();
        min_feature = 1.0f;
        for (size_t i = 0; i < in[Predicate::SET].children(); ++i) {
            Memory value = in[Predicate::SET][i].asString();
            label.resize(prefix_size);
            label.append(value.data, value.size);
            auto it = _key_counts.find(label);
            assert(it != _key_counts.end());
            min_feature = min(min_feature, 1.0f / it->second);
        }
        return min_feature;
    }
    case Predicate::TYPE_FEATURE_RANGE: {
        if (_negated) {
            return 0.0f;
        }
        string key = in[Predicate::KEY].asString().make_string();
        auto it = _key_counts.find(key);
        assert(it != _key_counts.end());
        return 1.0f / it->second;
    }
    }  // switch
    return 0.0f;
}

PredicateTreeAnalyzer::PredicateTreeAnalyzer(const Inspector &in) : _has_not(false), _negated(false) {
    traverseTree(in);
    _min_feature = static_cast<int>(std::ceil(float(findMinFeature(in)) + (_has_not? 1.0 : 0.0)));
}

}  // namespace predicate
}  // namespace search
<|endoftext|>


<commit_before>#include "UserDefineTable.h"

#include <boost/serialization/unordered_map.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/filesystem.hpp>

namespace sf1r
{
namespace Recommend
{
static std::string uuid = "UserDefineTable";
static std::string timestamp = ".UserDefineTimestamp";

UserDefineTable::UserDefineTable(const std::string& workdir)
    : timestamp_(0)
    , workdir_(workdir)
{
    if (!boost::filesystem::exists(workdir_))
    {
        boost::filesystem::create_directory(workdir_);
    }

    {
        std::string path = workdir_;
        path += "/";
        path += timestamp;

        std::ifstream in;
        in.open(path.c_str(), std::ifstream::in);
        in>>timestamp_;
        in.close();
    }

    {
        std::string path = workdir_;
        path += "/";
        path += uuid;
        if (boost::filesystem::exists(path.c_str()))
        {
            boost::filesystem::path p(path);
            if ((0 < timestamp_) && (timestamp_ >= boost::filesystem::last_write_time(p)))
            {
                std::ifstream ifs(path.c_str(), std::ios::binary);
                boost::archive::text_iarchive ia(ifs);
                ia >> table_;
                ifs.close();
            }
        }
    }
}

UserDefineTable::~UserDefineTable()
{
}

void UserDefineTable::build(const std::string& path)
{
    //if (!isNeedBuild)
    //    return;
    //clear();

    std::string resource;
    if (!boost::filesystem::exists(path))
        resource = workdir_;
    else if(!boost::filesystem::is_directory(path))
        resource = workdir_;
    else
        resource = path;

    try
    {
        if (!boost::filesystem::exists(resource))
            return ;
        else if(boost::filesystem::is_directory(resource))
        {
            boost::filesystem::directory_iterator end;
            for(boost::filesystem::directory_iterator it(resource) ; it != end ; ++it)
            {
                const std::string p = it->path().string();
                if(boost::filesystem::is_regular_file(p))
                {
                    if (isValid(p))
                        buildFromFile(p);
                }
            }
        }
    }
    catch (const boost::filesystem::filesystem_error& ex)
    {
        std::cout << ex.what() << '\n';
    }
    timestamp_ = time(NULL);
}

bool UserDefineTable::isNeedBuild(const std::string& path) const
{
    if (timestamp_ == 0)
        return true;

    std::string resource;
    if (!boost::filesystem::exists(path))
        resource = workdir_;
    else if(!boost::filesystem::is_directory(path))
        resource = workdir_;
    else
        resource = path;

    try
    {
        if (!boost::filesystem::exists(resource))
            return false;
        else if(boost::filesystem::is_directory(resource))
        {
            boost::filesystem::directory_iterator end;
            for(boost::filesystem::directory_iterator it(resource) ; it != end ; ++it)
            {
                const std::string p = it->path().string();
                if(boost::filesystem::is_regular_file(p))
                {
                    if (timestamp_ < boost::filesystem::last_write_time(it->path()))
                        return true;
                }
            }
        }
    }
    catch (const boost::filesystem::filesystem_error& ex)
    {
        std::cout << ex.what() << '\n';
    }
    return false;
}

bool UserDefineTable::isValid(const std::string& f) const
{
    const boost::filesystem::path p(f);
    return ".udef" == boost::filesystem::extension(p);
}

void UserDefineTable::buildFromFile(const std::string& f)
{
    std::ifstream ifs(f.c_str());
    int maxLine = 102400;
    char cLine[maxLine];
    memset(cLine, 0, maxLine);

    while(ifs.getline(cLine, maxLine))
    {
        std::string sLine(cLine);
        memset(cLine, 0, maxLine);

        std::size_t tabPos = sLine.find('\t');
        if (std::string::npos == tabPos)
            continue;
        std::string key = sLine.substr(0, tabPos);
        std::string value = sLine.substr(tabPos + 1);
        table_[key] = value;
    }
    ifs.close();
}

bool UserDefineTable::search(const std::string& userQuery, std::string& correct) const
{
    boost::unordered_map<std::string, std::string>::const_iterator it = table_.find(userQuery);
    if (table_.end() == it)
        return false;
    correct = it->second;
    return true;
}

void UserDefineTable::flush() const
{
    std::string path = workdir_;
    path += "/";
    path += uuid;
    std::ofstream ofs(path.c_str(), std::ofstream::trunc);
    boost::archive::text_oarchive oa(ofs);
    oa << table_;
    ofs.close();
}

void UserDefineTable::clear()
{
    table_.clear();
}

}
}
<commit_msg>fix reload bug for user-define resource<commit_after>#include "UserDefineTable.h"

#include <boost/serialization/unordered_map.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/filesystem.hpp>

namespace sf1r
{
namespace Recommend
{
static std::string uuid = "UserDefineTable";
static std::string timestamp = ".UserDefineTimestamp";

UserDefineTable::UserDefineTable(const std::string& workdir)
    : timestamp_(0)
    , workdir_(workdir)
{
    if (!boost::filesystem::exists(workdir_))
    {
        boost::filesystem::create_directory(workdir_);
    }

    {
        std::string path = workdir_;
        path += "/";
        path += timestamp;

        std::ifstream in;
        in.open(path.c_str(), std::ifstream::in);
        in>>timestamp_;
        in.close();
    }

    {
        std::string path = workdir_;
        path += "/";
        path += uuid;
        if (boost::filesystem::exists(path.c_str()))
        {
            //boost::filesystem::path p(path);
            //if ((0 < timestamp_) && (timestamp_ >= boost::filesystem::last_write_time(p)))
            {
                std::ifstream ifs(path.c_str(), std::ios::binary);
                boost::archive::text_iarchive ia(ifs);
                ia >> table_;
                ifs.close();
            }
        }
    }
}

UserDefineTable::~UserDefineTable()
{
}

void UserDefineTable::build(const std::string& path)
{
    //if (!isNeedBuild)
    //    return;
    //clear();

    std::string resource;
    if (!boost::filesystem::exists(path))
        resource = workdir_;
    else if(!boost::filesystem::is_directory(path))
        resource = workdir_;
    else
        resource = path;

    try
    {
        if (!boost::filesystem::exists(resource))
            return ;
        else if(boost::filesystem::is_directory(resource))
        {
            boost::filesystem::directory_iterator end;
            for(boost::filesystem::directory_iterator it(resource) ; it != end ; ++it)
            {
                const std::string p = it->path().string();
                if(boost::filesystem::is_regular_file(p))
                {
                    if (isValid(p))
                        buildFromFile(p);
                }
            }
        }
    }
    catch (const boost::filesystem::filesystem_error& ex)
    {
        std::cout << ex.what() << '\n';
    }
    timestamp_ = time(NULL);
}

bool UserDefineTable::isNeedBuild(const std::string& path) const
{
    if (timestamp_ == 0)
        return true;

    std::string resource;
    if (!boost::filesystem::exists(path))
        resource = workdir_;
    else if(!boost::filesystem::is_directory(path))
        resource = workdir_;
    else
        resource = path;

    try
    {
        if (!boost::filesystem::exists(resource))
            return false;
        else if(boost::filesystem::is_directory(resource))
        {
            boost::filesystem::directory_iterator end;
            for(boost::filesystem::directory_iterator it(resource) ; it != end ; ++it)
            {
                const std::string p = it->path().string();
                if(boost::filesystem::is_regular_file(p))
                {
                    if (timestamp_ < boost::filesystem::last_write_time(it->path()))
                        return true;
                }
            }
        }
    }
    catch (const boost::filesystem::filesystem_error& ex)
    {
        std::cout << ex.what() << '\n';
    }
    return false;
}

bool UserDefineTable::isValid(const std::string& f) const
{
    const boost::filesystem::path p(f);
    return ".udef" == boost::filesystem::extension(p);
}

void UserDefineTable::buildFromFile(const std::string& f)
{
    std::ifstream ifs(f.c_str());
    int maxLine = 102400;
    char cLine[maxLine];
    memset(cLine, 0, maxLine);

    while(ifs.getline(cLine, maxLine))
    {
        std::string sLine(cLine);
        memset(cLine, 0, maxLine);

        std::size_t tabPos = sLine.find('\t');
        if (std::string::npos == tabPos)
            continue;
        std::string key = sLine.substr(0, tabPos);
        std::string value = sLine.substr(tabPos + 1);
        table_[key] = value;
    }
    ifs.close();
}

bool UserDefineTable::search(const std::string& userQuery, std::string& correct) const
{
    boost::unordered_map<std::string, std::string>::const_iterator it = table_.find(userQuery);
    if (table_.end() == it)
        return false;
    correct = it->second;
    return true;
}

void UserDefineTable::flush() const
{
    std::string path = workdir_;
    path += "/";
    path += uuid;
    std::ofstream ofs(path.c_str(), std::ofstream::trunc);
    boost::archive::text_oarchive oa(ofs);
    oa << table_;
    ofs.close();

    path = workdir_;
    path += "/";
    path += timestamp;
    ofs.open(path.c_str());
    ofs<<timestamp_;
    ofs.close();
}

void UserDefineTable::clear()
{
    table_.clear();
}

}
}
<|endoftext|>


<commit_before>#include <iostream>
#include <cstring>
#include <fstream>

using namespace std;

void input(char firstString[], char secondString[])
{
	ifstream in("input.txt");
	in >> firstString;
	in >> secondString;
	in.close();
}

void calculatePrimePower(unsigned long long primeNumberInPower[], char firstString[])
{
	const int primeNumber = 37;
	primeNumberInPower[0] = 1;
	for (int i = 1; i < strlen(firstString); ++i)
	{
		primeNumberInPower[i] = primeNumberInPower[i - 1] * primeNumber;
	}
}

void haveHashOfFirstString(unsigned long long hashOfFirstString[],
	unsigned long long primeNumberInPower[], char firstString[])
{
	hashOfFirstString[0] = 0;
	for (int i = 0; i <= strlen(firstString); ++i)
	{
		hashOfFirstString[i] = primeNumberInPower[i] * firstString[i];
		if (i)
		{
			hashOfFirstString[i] += hashOfFirstString[i - 1];
		}
	}
}

void haveHashOfSecondString(unsigned long long &hashOfSecondString,
	unsigned long long primeNumberInPower[], char secondString[])
{
	for (int i = 0; i < strlen(secondString); ++i)
	{
		hashOfSecondString += primeNumberInPower[i] * secondString[i];
	}
}

bool isFindSubstring(char firstString[], char secondString[], unsigned long long hashOfFirstString[],
	unsigned long long hashOfSecondString, unsigned long long primeNumberInPower[])
{
	bool result = false;
	for (int i = 0; i + strlen(secondString) - 1 < strlen(firstString); ++i)
	{
		unsigned long long currentHash = hashOfFirstString[i + int(strlen(secondString)) - 1];
		if (i)
		{
			currentHash -= hashOfFirstString[i - 1];
		}
		if (currentHash == hashOfSecondString * primeNumberInPower[i])
		{
			if (result == false)
			{
				cout << " " << secondString << "    " << firstString << "   ";
			}
			result = true;
			cout << i + 1 << " ";
		}
	}
	return result;
}

void output(char currentString[])
{
	for (int i = 0; i < strlen(currentString); ++i)
	{
		cout << currentString[i];
	}
}

int main()
{
	setlocale(LC_CTYPE, "rus");
	const int maxSize = 1e2;
	char firstString[maxSize];
	char secondString[maxSize];
	input(firstString, secondString);
	unsigned long long primeNumberInPower[maxSize];
	calculatePrimePower(primeNumberInPower, firstString);
	unsigned long long hashOfFirstString[maxSize];
	haveHashOfFirstString(hashOfFirstString, primeNumberInPower, firstString);
	unsigned long long hashOfSecondString = 0;
	haveHashOfSecondString(hashOfSecondString, primeNumberInPower, secondString);
	if (!isFindSubstring(firstString, secondString, hashOfFirstString, hashOfSecondString, primeNumberInPower))
	{
		cout << " " << secondString << "     " << firstString;
	}
	cout << endl;
	return 0;
}<commit_msg>— removed arrays of char; — added char*;<commit_after>#include <iostream>
#include <string.h>
#include <cstdio>
#include <cstring>
#include <string>
#include <fstream>

using namespace std;

struct String
{
	char *data;
	int size;
};

String createString(int size, int capacity)
{
	String newString = { nullptr, size };
	return newString;
}

void input(String &currentString)
{
	const int capacity = int(1e3);
	char *newString = new char[capacity];
	cin.getline(newString, capacity);
	currentString.size = strlen(newString);
	currentString.data = newString;
}

void calculatePrimePower(unsigned long long primeNumberInPower[], String firstString)
{
	const int primeNumber = 37;
	primeNumberInPower[0] = 1;
	for (int i = 1; i < firstString.size; ++i)
	{
		primeNumberInPower[i] = primeNumberInPower[i - 1] * primeNumber;
	}
}

void haveHashOfFirstString(unsigned long long hashOfFirstString[],
	unsigned long long primeNumberInPower[], String firstString)
{
	hashOfFirstString[0] = 0;
	for (int i = 0; i <= firstString.size; ++i)
	{
		hashOfFirstString[i] = primeNumberInPower[i] * firstString.data[i];
		if (i)
		{
			hashOfFirstString[i] += hashOfFirstString[i - 1];
		}
	}
}

void haveHashOfSecondString(unsigned long long &hashOfSecondString,
	unsigned long long primeNumberInPower[], String secondString)
{
	for (int i = 0; i < secondString.size; ++i)
	{
		hashOfSecondString += primeNumberInPower[i] * secondString.data[i];
	}
}

void debugOutput(String currentString)
{
	for (int i = 0; i < currentString.size; ++i)
	{
		cout << currentString.data[i];
	}
}

bool isFindSubstring(String firstString, String secondString, unsigned long long hashOfFirstString[],
	unsigned long long hashOfSecondString, unsigned long long primeNumberInPower[])
{
	bool result = false;
	for (int i = 0; i + secondString.size - 1 < firstString.size; ++i)
	{
		unsigned long long currentHash = hashOfFirstString[i + secondString.size - 1];
		if (i)
		{
			currentHash -= hashOfFirstString[i - 1];
		}
		if (currentHash == hashOfSecondString * primeNumberInPower[i])
		{
			if (result == false)
			{
				cout << " ";
				debugOutput(secondString);
				cout << "    ";
				debugOutput(firstString);
				cout << "   ";
			}
			result = true;
			cout << i + 1 << " ";
		}
	}
	return result;
}

int main()
{
	setlocale(LC_CTYPE, "rus");
	const int size = 0;
	const int capacity = int(1e3);
	String firstString = createString(size, capacity);
	String secondString = createString(size, capacity);
	cout << " : ";
	input(firstString);
	cout << " : ";
	input(secondString);
	unsigned long long primeNumberInPower[capacity];
	calculatePrimePower(primeNumberInPower, firstString);
	unsigned long long hashOfFirstString[capacity];
	haveHashOfFirstString(hashOfFirstString, primeNumberInPower, firstString);
	unsigned long long hashOfSecondString = 0;
	haveHashOfSecondString(hashOfSecondString, primeNumberInPower, secondString);
	if (!isFindSubstring(firstString, secondString, hashOfFirstString, hashOfSecondString, primeNumberInPower))
	{
		cout << " ";
		debugOutput(secondString);
		cout << "     ";
		debugOutput(firstString);
	}
	cout << endl;
	return 0;
}
/*
asdasdffsdfgdhfghjfjhgkjgkfjgdhghasdasdffsdfgdhfghjfjhgkjgkfjgdhghasdasdffsdfgdhfghjfjhgkjgkfjgdhghasdasdffsdfgdhfghjfjhgkjgkfjgdhghasdasdffsdfgdhfghjfjhgkjgkfjgdhghasdasdffsdfgdhfghjfjhgkjgkfjgdhghasdasdffsdfgdhfghjfjhgkjgkfjgdhghasdasdffsdfgdhfghjfjhgkjgkfjgdhghasdasdffsdfgdhfghjfjhgkjgkfjgdhghasdasdffsdfgdhfghjfjhgkjgkfjgdhgh
asdasdffsdfgdhfghjfjhgkjgkfjgdhgh
*/<|endoftext|>