<commit_before>/*
 * Copyright (c) 2002-2005 The Regents of The University of Michigan
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Nathan Binkert
 *          Erik Hallnor
 *          Steve Reinhardt
 */

/* @file
 * Serialization Interface Declarations
 */

#ifndef __SERIALIZE_HH__
#define __SERIALIZE_HH__

#include <iostream>
#include <list>
#include <map>
#include <vector>

#include "base/types.hh"

class IniFile;
class Serializable;
class Checkpoint;
class SimObject;
class EventQueue;

/** The current version of the checkpoint format.
 * This should be incremented by 1 and only 1 for every new version, where a new
 * version is defined as a checkpoint created before this version won't work on
 * the current version until the checkpoint format is updated. Adding a new
 * SimObject shouldn't cause the version number to increase, only changes to
 * existing objects such as serializing/unserializing more state, changing sizes
 * of serialized arrays, etc. */
static const uint64_t gem5CheckpointVersion = 0x000000000000000d;

template <class T>
void paramOut(std::ostream &os, const std::string &name, const T &param);

template <class T>
void paramIn(Checkpoint *cp, const std::string &section,
             const std::string &name, T &param);

template <class T>
bool optParamIn(Checkpoint *cp, const std::string &section,
             const std::string &name, T &param);

template <class T>
void arrayParamOut(std::ostream &os, const std::string &name,
                   const T *param, unsigned size);

template <class T>
void arrayParamOut(std::ostream &os, const std::string &name,
                   const std::vector<T> &param);

template <class T>
void arrayParamOut(std::ostream &os, const std::string &name,
                   const std::list<T> &param);

template <class T>
void arrayParamIn(Checkpoint *cp, const std::string &section,
                  const std::string &name, T *param, unsigned size);

template <class T>
void arrayParamIn(Checkpoint *cp, const std::string &section,
                  const std::string &name, std::vector<T> &param);

template <class T>
void arrayParamIn(Checkpoint *cp, const std::string &section,
                  const std::string &name, std::list<T> &param);

void
objParamIn(Checkpoint *cp, const std::string &section,
           const std::string &name, SimObject * &param);

template <typename T>
void fromInt(T &t, int i)
{
    t = (T)i;
}

template <typename T>
void fromSimObject(T &t, SimObject *s)
{
    t = dynamic_cast<T>(s);
}

//
// These macros are streamlined to use in serialize/unserialize
// functions.  It's assumed that serialize() has a parameter 'os' for
// the ostream, and unserialize() has parameters 'cp' and 'section'.
#define SERIALIZE_SCALAR(scalar)        paramOut(os, #scalar, scalar)

#define UNSERIALIZE_SCALAR(scalar)      paramIn(cp, section, #scalar, scalar)
#define UNSERIALIZE_OPT_SCALAR(scalar)      optParamIn(cp, section, #scalar, scalar)

// ENUMs are like SCALARs, but we cast them to ints on the way out
#define SERIALIZE_ENUM(scalar)          paramOut(os, #scalar, (int)scalar)

#define UNSERIALIZE_ENUM(scalar)                \
 do {                                           \
    int tmp;                                    \
    paramIn(cp, section, #scalar, tmp);         \
    fromInt(scalar, tmp);                    \
  } while (0)

#define SERIALIZE_ARRAY(member, size)           \
        arrayParamOut(os, #member, member, size)

#define UNSERIALIZE_ARRAY(member, size)         \
        arrayParamIn(cp, section, #member, member, size)

#define SERIALIZE_OBJPTR(objptr)        paramOut(os, #objptr, (objptr)->name())

#define UNSERIALIZE_OBJPTR(objptr)                      \
  do {                                                  \
    SimObject *sptr;                                    \
    objParamIn(cp, section, #objptr, sptr);             \
    fromSimObject(objptr, sptr);                        \
  } while (0)

/**
 * Basic support for object serialization.
 *
 * @note Many objects that support serialization need to be put in a
 * consistent state when serialization takes place. We refer to the
 * action of forcing an object into a consistent state as
 * 'draining'. Objects that need draining inherit from Drainable. See
 * Drainable for more information.
 */
class Serializable
{
  protected:
    void nameOut(std::ostream &os);
    void nameOut(std::ostream &os, const std::string &_name);

  public:
    Serializable();
    virtual ~Serializable();

    // manditory virtual function, so objects must provide names
    virtual const std::string name() const = 0;

    virtual void serialize(std::ostream &os);
    virtual void unserialize(Checkpoint *cp, const std::string &section);

    static Serializable *create(Checkpoint *cp, const std::string &section);

    static int ckptCount;
    static int ckptMaxCount;
    static int ckptPrevCount;
    static void serializeAll(const std::string &cpt_dir);
    static void unserializeGlobals(Checkpoint *cp);
};

void debug_serialize(const std::string &cpt_dir);

//
// A SerializableBuilder serves as an evaluation context for a set of
// parameters that describe a specific instance of a Serializable.  This
// evaluation context corresponds to a section in the .ini file (as
// with the base ParamContext) plus an optional node in the
// configuration hierarchy (the configNode member) for resolving
// Serializable references.  SerializableBuilder is an abstract superclass;
// derived classes specialize the class for particular subclasses of
// Serializable (e.g., BaseCache).
//
// For typical usage, see the definition of
// SerializableClass::createObject().
//
class SerializableBuilder
{
  public:

    SerializableBuilder() {}

    virtual ~SerializableBuilder() {}

    // Create the actual Serializable corresponding to the parameter
    // values in this context.  This function is overridden in derived
    // classes to call a specific constructor for a particular
    // subclass of Serializable.
    virtual Serializable *create() = 0;
};

//
// An instance of SerializableClass corresponds to a class derived from
// Serializable.  The SerializableClass instance serves to bind the string
// name (found in the config file) to a function that creates an
// instance of the appropriate derived class.
//
// This would be much cleaner in Smalltalk or Objective-C, where types
// are first-class objects themselves.
//
class SerializableClass
{
  public:

    // Type CreateFunc is a pointer to a function that creates a new
    // simulation object builder based on a .ini-file parameter
    // section (specified by the first string argument), a unique name
    // for the object (specified by the second string argument), and
    // an optional config hierarchy node (specified by the third
    // argument).  A pointer to the new SerializableBuilder is returned.
    typedef Serializable *(*CreateFunc)(Checkpoint *cp,
                                        const std::string &section);

    static std::map<std::string,CreateFunc> *classMap;

    // Constructor.  For example:
    //
    // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
    //                         newBaseCacheSerializableBuilder);
    //
    SerializableClass(const std::string &className, CreateFunc createFunc);

    // create Serializable given name of class and pointer to
    // configuration hierarchy node
    static Serializable *createObject(Checkpoint *cp,
                                      const std::string &section);
};

//
// Macros to encapsulate the magic of declaring & defining
// SerializableBuilder and SerializableClass objects
//

#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS)                      \
SerializableClass the##OBJ_CLASS##Class(CLASS_NAME,                        \
                                         OBJ_CLASS::createForUnserialize);

// Base class to wrap object resolving functionality.  This can be
// provided to Checkpoint to allow it to map object names onto
// object C++ objects in which to unserialize
class SimObjectResolver
{
  public:
    virtual ~SimObjectResolver() { }

    // Find a SimObject given a full path name
    virtual SimObject *resolveSimObject(const std::string &name) = 0;
};

class Checkpoint
{
  private:

    IniFile *db;

    SimObjectResolver &objNameResolver;

  public:
    Checkpoint(const std::string &cpt_dir, SimObjectResolver &resolver);
    ~Checkpoint();

    const std::string cptDir;

    bool find(const std::string &section, const std::string &entry,
              std::string &value);

    bool findObj(const std::string &section, const std::string &entry,
                 SimObject *&value);

    bool sectionExists(const std::string &section);

    // The following static functions have to do with checkpoint
    // creation rather than restoration.  This class makes a handy
    // namespace for them though.  Currently no Checkpoint object is
    // created on serialization (only unserialization) so we track the
    // directory name as a global.  It would be nice to change this
    // someday

  private:
    // current directory we're serializing into.
    static std::string currentDirectory;

  public:
    // Set the current directory.  This function takes care of
    // inserting curTick() if there's a '%d' in the argument, and
    // appends a '/' if necessary.  The final name is returned.
    static std::string setDir(const std::string &base_name);

    // Export current checkpoint directory name so other objects can
    // derive filenames from it (e.g., memory).  The return value is
    // guaranteed to end in '/' so filenames can be directly appended.
    // This function is only valid while a checkpoint is being created.
    static std::string dir();

    // Filename for base checkpoint file within directory.
    static const char *baseFilename;
};

#endif // __SERIALIZE_HH__
<commit_msg>sim: Add support for serializing BitUnionXX<commit_after>/*
 * Copyright (c) 2002-2005 The Regents of The University of Michigan
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Nathan Binkert
 *          Erik Hallnor
 *          Steve Reinhardt
 */

/* @file
 * Serialization Interface Declarations
 */

#ifndef __SERIALIZE_HH__
#define __SERIALIZE_HH__

#include <iostream>
#include <list>
#include <map>
#include <vector>

#include "base/bitunion.hh"
#include "base/types.hh"

class IniFile;
class Serializable;
class Checkpoint;
class SimObject;
class EventQueue;

/** The current version of the checkpoint format.
 * This should be incremented by 1 and only 1 for every new version, where a new
 * version is defined as a checkpoint created before this version won't work on
 * the current version until the checkpoint format is updated. Adding a new
 * SimObject shouldn't cause the version number to increase, only changes to
 * existing objects such as serializing/unserializing more state, changing sizes
 * of serialized arrays, etc. */
static const uint64_t gem5CheckpointVersion = 0x000000000000000d;

template <class T>
void paramOut(std::ostream &os, const std::string &name, const T &param);

template <typename DataType, typename BitUnion>
void paramOut(std::ostream &os, const std::string &name,
              const BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
{
    paramOut(os, name, p.__data);
}

template <class T>
void paramIn(Checkpoint *cp, const std::string &section,
             const std::string &name, T &param);

template <typename DataType, typename BitUnion>
void paramIn(Checkpoint *cp, const std::string &section,
             const std::string &name,
             BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
{
    paramIn(cp, section, name, p.__data);
}

template <class T>
bool optParamIn(Checkpoint *cp, const std::string &section,
             const std::string &name, T &param);

template <typename DataType, typename BitUnion>
bool optParamIn(Checkpoint *cp, const std::string &section,
                const std::string &name,
                BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
{
    return optParamIn(cp, section, name, p.__data);
}

template <class T>
void arrayParamOut(std::ostream &os, const std::string &name,
                   const T *param, unsigned size);

template <class T>
void arrayParamOut(std::ostream &os, const std::string &name,
                   const std::vector<T> &param);

template <class T>
void arrayParamOut(std::ostream &os, const std::string &name,
                   const std::list<T> &param);

template <class T>
void arrayParamIn(Checkpoint *cp, const std::string &section,
                  const std::string &name, T *param, unsigned size);

template <class T>
void arrayParamIn(Checkpoint *cp, const std::string &section,
                  const std::string &name, std::vector<T> &param);

template <class T>
void arrayParamIn(Checkpoint *cp, const std::string &section,
                  const std::string &name, std::list<T> &param);

void
objParamIn(Checkpoint *cp, const std::string &section,
           const std::string &name, SimObject * &param);

template <typename T>
void fromInt(T &t, int i)
{
    t = (T)i;
}

template <typename T>
void fromSimObject(T &t, SimObject *s)
{
    t = dynamic_cast<T>(s);
}

//
// These macros are streamlined to use in serialize/unserialize
// functions.  It's assumed that serialize() has a parameter 'os' for
// the ostream, and unserialize() has parameters 'cp' and 'section'.
#define SERIALIZE_SCALAR(scalar)        paramOut(os, #scalar, scalar)

#define UNSERIALIZE_SCALAR(scalar)      paramIn(cp, section, #scalar, scalar)
#define UNSERIALIZE_OPT_SCALAR(scalar)      optParamIn(cp, section, #scalar, scalar)

// ENUMs are like SCALARs, but we cast them to ints on the way out
#define SERIALIZE_ENUM(scalar)          paramOut(os, #scalar, (int)scalar)

#define UNSERIALIZE_ENUM(scalar)                \
 do {                                           \
    int tmp;                                    \
    paramIn(cp, section, #scalar, tmp);         \
    fromInt(scalar, tmp);                    \
  } while (0)

#define SERIALIZE_ARRAY(member, size)           \
        arrayParamOut(os, #member, member, size)

#define UNSERIALIZE_ARRAY(member, size)         \
        arrayParamIn(cp, section, #member, member, size)

#define SERIALIZE_OBJPTR(objptr)        paramOut(os, #objptr, (objptr)->name())

#define UNSERIALIZE_OBJPTR(objptr)                      \
  do {                                                  \
    SimObject *sptr;                                    \
    objParamIn(cp, section, #objptr, sptr);             \
    fromSimObject(objptr, sptr);                        \
  } while (0)

/**
 * Basic support for object serialization.
 *
 * @note Many objects that support serialization need to be put in a
 * consistent state when serialization takes place. We refer to the
 * action of forcing an object into a consistent state as
 * 'draining'. Objects that need draining inherit from Drainable. See
 * Drainable for more information.
 */
class Serializable
{
  protected:
    void nameOut(std::ostream &os);
    void nameOut(std::ostream &os, const std::string &_name);

  public:
    Serializable();
    virtual ~Serializable();

    // manditory virtual function, so objects must provide names
    virtual const std::string name() const = 0;

    virtual void serialize(std::ostream &os);
    virtual void unserialize(Checkpoint *cp, const std::string &section);

    static Serializable *create(Checkpoint *cp, const std::string &section);

    static int ckptCount;
    static int ckptMaxCount;
    static int ckptPrevCount;
    static void serializeAll(const std::string &cpt_dir);
    static void unserializeGlobals(Checkpoint *cp);
};

void debug_serialize(const std::string &cpt_dir);

//
// A SerializableBuilder serves as an evaluation context for a set of
// parameters that describe a specific instance of a Serializable.  This
// evaluation context corresponds to a section in the .ini file (as
// with the base ParamContext) plus an optional node in the
// configuration hierarchy (the configNode member) for resolving
// Serializable references.  SerializableBuilder is an abstract superclass;
// derived classes specialize the class for particular subclasses of
// Serializable (e.g., BaseCache).
//
// For typical usage, see the definition of
// SerializableClass::createObject().
//
class SerializableBuilder
{
  public:

    SerializableBuilder() {}

    virtual ~SerializableBuilder() {}

    // Create the actual Serializable corresponding to the parameter
    // values in this context.  This function is overridden in derived
    // classes to call a specific constructor for a particular
    // subclass of Serializable.
    virtual Serializable *create() = 0;
};

//
// An instance of SerializableClass corresponds to a class derived from
// Serializable.  The SerializableClass instance serves to bind the string
// name (found in the config file) to a function that creates an
// instance of the appropriate derived class.
//
// This would be much cleaner in Smalltalk or Objective-C, where types
// are first-class objects themselves.
//
class SerializableClass
{
  public:

    // Type CreateFunc is a pointer to a function that creates a new
    // simulation object builder based on a .ini-file parameter
    // section (specified by the first string argument), a unique name
    // for the object (specified by the second string argument), and
    // an optional config hierarchy node (specified by the third
    // argument).  A pointer to the new SerializableBuilder is returned.
    typedef Serializable *(*CreateFunc)(Checkpoint *cp,
                                        const std::string &section);

    static std::map<std::string,CreateFunc> *classMap;

    // Constructor.  For example:
    //
    // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
    //                         newBaseCacheSerializableBuilder);
    //
    SerializableClass(const std::string &className, CreateFunc createFunc);

    // create Serializable given name of class and pointer to
    // configuration hierarchy node
    static Serializable *createObject(Checkpoint *cp,
                                      const std::string &section);
};

//
// Macros to encapsulate the magic of declaring & defining
// SerializableBuilder and SerializableClass objects
//

#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS)                      \
SerializableClass the##OBJ_CLASS##Class(CLASS_NAME,                        \
                                         OBJ_CLASS::createForUnserialize);

// Base class to wrap object resolving functionality.  This can be
// provided to Checkpoint to allow it to map object names onto
// object C++ objects in which to unserialize
class SimObjectResolver
{
  public:
    virtual ~SimObjectResolver() { }

    // Find a SimObject given a full path name
    virtual SimObject *resolveSimObject(const std::string &name) = 0;
};

class Checkpoint
{
  private:

    IniFile *db;

    SimObjectResolver &objNameResolver;

  public:
    Checkpoint(const std::string &cpt_dir, SimObjectResolver &resolver);
    ~Checkpoint();

    const std::string cptDir;

    bool find(const std::string &section, const std::string &entry,
              std::string &value);

    bool findObj(const std::string &section, const std::string &entry,
                 SimObject *&value);

    bool sectionExists(const std::string &section);

    // The following static functions have to do with checkpoint
    // creation rather than restoration.  This class makes a handy
    // namespace for them though.  Currently no Checkpoint object is
    // created on serialization (only unserialization) so we track the
    // directory name as a global.  It would be nice to change this
    // someday

  private:
    // current directory we're serializing into.
    static std::string currentDirectory;

  public:
    // Set the current directory.  This function takes care of
    // inserting curTick() if there's a '%d' in the argument, and
    // appends a '/' if necessary.  The final name is returned.
    static std::string setDir(const std::string &base_name);

    // Export current checkpoint directory name so other objects can
    // derive filenames from it (e.g., memory).  The return value is
    // guaranteed to end in '/' so filenames can be directly appended.
    // This function is only valid while a checkpoint is being created.
    static std::string dir();

    // Filename for base checkpoint file within directory.
    static const char *baseFilename;
};

#endif // __SERIALIZE_HH__
<|endoftext|>


<commit_before>/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */

/*
 *  Main authors:
 *     Guido Tack <guido.tack@monash.edu>
 */

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <minizinc/optimize_constraints.hh>
#include <minizinc/eval_par.hh>
#include <minizinc/flatten_internal.hh>

namespace MiniZinc {

  void
  OptimizeRegistry::reg(const MiniZinc::ASTString& call, optimizer opt) {
    _m.insert(std::make_pair(call, opt));
  }

  OptimizeRegistry::ConstraintStatus
  OptimizeRegistry::process(EnvI& env, MiniZinc::Item* i, MiniZinc::Call* c, Expression*& rewrite) {
    ASTStringMap<optimizer>::t::iterator it = _m.find(c->id());
    if (it != _m.end()) {
      return it->second(env,i,c,rewrite);
    }
    return CS_NONE;
  }

  OptimizeRegistry&
  OptimizeRegistry::registry(void) {
    static OptimizeRegistry reg;
    return reg;
  }

  namespace Optimizers {

    OptimizeRegistry::ConstraintStatus o_linear(EnvI& env, Item* i, Call* c, Expression*& rewrite) {
      ArrayLit* al_c = eval_array_lit(env,c->args()[0]);
      std::vector<IntVal> coeffs(al_c->v().size());
      for (unsigned int i=0; i<al_c->v().size(); i++) {
        coeffs[i] = eval_int(env,al_c->v()[i]);
      }
      ArrayLit* al_x = eval_array_lit(env,c->args()[1]);
      std::vector<KeepAlive> x(al_x->v().size());
      for (unsigned int i=0; i<al_x->v().size(); i++) {
        x[i] = al_x->v()[i];
      }
      IntVal d = 0;
      simplify_lin<IntLit>(coeffs, x, d);
      if (coeffs.size()==0) {
        bool failed;
        if (c->id()==constants().ids.int_.lin_le) {
          failed = (d > eval_int(env,c->args()[2]));
        } else if (c->id()==constants().ids.int_.lin_eq) {
          failed = (d != eval_int(env,c->args()[2]));
        } else {
          failed = (d == eval_int(env,c->args()[2]));
        }
        if (failed) {
          return OptimizeRegistry::CS_FAILED;
        } else {
          return OptimizeRegistry::CS_ENTAILED;
        }
      } else if (coeffs.size()==1 && (i->isa<ConstraintI>() || i->cast<VarDeclI>()->e()->ti()->domain()==constants().lit_true)) {
        VarDecl* vd = x[0]()->cast<Id>()->decl();
        IntSetVal* domain = vd->ti()->domain() ? eval_intset(env,vd->ti()->domain()) : NULL;
        if (c->id()==constants().ids.int_.lin_eq) {
          IntVal rd = eval_int(env,c->args()[2])-d;
          if (rd % coeffs[0] == 0) {
            IntVal nd = rd / coeffs[0];
            if (domain && !domain->contains(nd))
              return OptimizeRegistry::CS_FAILED;
            std::vector<Expression*> args(2);
            args[0] = x[0](); args[1] = IntLit::a(nd);
            Call* c = new Call(Location(), constants().ids.int_.eq, args);
            c->type(Type::varbool());
            rewrite = c;
            return OptimizeRegistry::CS_REWRITE;
          } else {
            return OptimizeRegistry::CS_FAILED;
          }
        } else if (c->id()==constants().ids.int_.lin_le) {
          IntVal ac = std::abs(coeffs[0]);
          IntVal rd = eval_int(env,c->args()[2])-d;
          IntVal ad = std::abs(rd);
          IntVal nd;
          if (ad % ac == 0) {
            nd = rd / coeffs[0];
          } else {
            double nd_d = static_cast<double>(ad.toInt()) / static_cast<double>(ac.toInt());
            if (coeffs[0] >= 0 && rd >= 0) {
              nd = std::floor(nd_d);
            } else if (rd >= 0) {
              nd = -std::floor(nd_d);
            } else if (coeffs[0] >= 0) {
              nd = -std::ceil(nd_d);
            } else {
              nd = std::ceil(nd_d);
            }
          }
          bool swapSign = coeffs[0] < 0;
          if (domain) {
            if (swapSign) {
              if (domain->max() < nd) {
                return OptimizeRegistry::CS_FAILED;
              }
              else if (domain->min() >= nd)
                return OptimizeRegistry::CS_ENTAILED;
            } else {
              if (domain->min() > nd) {
                return OptimizeRegistry::CS_FAILED;
              }
              else if (domain->max() <= nd)
                return OptimizeRegistry::CS_ENTAILED;
            }
            std::vector<Expression*> args(2);
            args[0] = x[0](); args[1] = IntLit::a(nd);
            if (swapSign)
              std::swap(args[0], args[1]);
            Call* nc = new Call(Location(), constants().ids.int_.le, args);
            nc->type(Type::varbool());
            rewrite = nc;
            return OptimizeRegistry::CS_REWRITE;
          }
        }
      } else if (c->id()==constants().ids.int_.lin_eq && coeffs.size()==2  &&
                 ((coeffs[0]==1 && coeffs[1]==-1) || (coeffs[1]==1 && coeffs[0]==-1)) && eval_int(env,c->args()[2])-d==0) {
        std::vector<Expression*> args(2);
        args[0] = x[0](); args[1] = x[1]();
        Call* c = new Call(Location(), constants().ids.int_.eq, args);
        rewrite = c;
        return OptimizeRegistry::CS_REWRITE;
      }
      if (coeffs.size() < al_c->v().size()) {
        std::vector<Expression*> coeffs_e(coeffs.size());
        std::vector<Expression*> x_e(coeffs.size());
        for (unsigned int i=0; i<coeffs.size(); i++) {
          coeffs_e[i] = IntLit::a(coeffs[i]);
          x_e[i] = x[i]();
        }
        ArrayLit* al_c_new = new ArrayLit(al_c->loc(),coeffs_e);
        al_c_new->type(Type::parint(1));
        ArrayLit* al_x_new = new ArrayLit(al_x->loc(),x_e);
        al_x_new->type(al_x->type());
        c->args()[0] = al_c_new;
        c->args()[1] = al_x_new;
        if (d != 0) {
          c->args()[2] = IntLit::a(eval_int(env,c->args()[2])-d);
        }
      }
      return OptimizeRegistry::CS_OK;
    }

    OptimizeRegistry::ConstraintStatus o_lin_exp(EnvI& env, Item* i, Call* c, Expression*& rewrite) {
      if (c->type().isint()) {
        ArrayLit* al_c = eval_array_lit(env,c->args()[0]);
        std::vector<IntVal> coeffs(al_c->v().size());
        for (unsigned int i=0; i<al_c->v().size(); i++) {
          coeffs[i] = eval_int(env,al_c->v()[i]);
        }
        ArrayLit* al_x = eval_array_lit(env,c->args()[1]);
        std::vector<KeepAlive> x(al_x->v().size());
        for (unsigned int i=0; i<al_x->v().size(); i++) {
          x[i] = al_x->v()[i];
        }
        IntVal d = eval_int(env,c->args()[2]);
        simplify_lin<IntLit>(coeffs, x, d);
        if (coeffs.size()==0) {
          rewrite = IntLit::a(d);
          return OptimizeRegistry::CS_REWRITE;
        } else if (coeffs.size() < al_c->v().size()) {
          if (coeffs.size()==1 && coeffs[0]==1 && d==0) {
            rewrite = x[0]();
            return OptimizeRegistry::CS_REWRITE;
          }

          std::vector<Expression*> coeffs_e(coeffs.size());
          std::vector<Expression*> x_e(coeffs.size());
          for (unsigned int i=0; i<coeffs.size(); i++) {
            coeffs_e[i] = IntLit::a(coeffs[i]);
            x_e[i] = x[i]();
          }
          ArrayLit* al_c_new = new ArrayLit(al_c->loc(),coeffs_e);
          al_c_new->type(Type::parint(1));
          ArrayLit* al_x_new = new ArrayLit(al_x->loc(),x_e);
          al_x_new->type(al_x->type());
          c->args()[0] = al_c_new;
          c->args()[1] = al_x_new;
          c->args()[2] = IntLit::a(d);
        }
      }
      return OptimizeRegistry::CS_OK;
    }

    OptimizeRegistry::ConstraintStatus o_element(EnvI& env, Item* i, Call* c, Expression*& rewrite) {
      if (c->args()[0]->isa<IntLit>()) {
        IntVal idx = eval_int(env,c->args()[0]);
        ArrayLit* al = eval_array_lit(env,c->args()[1]);
        if (idx < 1 || idx > al->v().size()) {
          return OptimizeRegistry::CS_FAILED;
        }
        Expression* result = al->v()[idx.toInt()-1];
        std::vector<Expression*> args(2);
        args[0] = result;
        args[1] = c->args()[2];
        Call* eq = new Call(Location(),constants().ids.int_.eq,args);
        rewrite = eq;
        return OptimizeRegistry::CS_REWRITE;
      }
      return OptimizeRegistry::CS_OK;
    }

    class Register {
    public:
      Register(void) {
        GCLock lock;
        Model* m = new Model;
        ASTString id_element("array_int_element");
        ASTString id_var_element("array_var_int_element");
        std::vector<Expression*> e;
        e.push_back(new StringLit(Location(),id_element));
        e.push_back(new StringLit(Location(),id_var_element));
        m->addItem(new ConstraintI(Location(),new ArrayLit(Location(),e)));
        OptimizeRegistry::registry().reg(constants().ids.int_.lin_eq, o_linear);
        OptimizeRegistry::registry().reg(constants().ids.int_.lin_le, o_linear);
        OptimizeRegistry::registry().reg(constants().ids.int_.lin_ne, o_linear);
        OptimizeRegistry::registry().reg(id_element, o_element);
        OptimizeRegistry::registry().reg(constants().ids.lin_exp, o_lin_exp);
        OptimizeRegistry::registry().reg(id_var_element, o_element);
      }
    } _r;

  }

}<commit_msg>Need to use rewrite when changing linear constraints during optimisation phase<commit_after>/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */

/*
 *  Main authors:
 *     Guido Tack <guido.tack@monash.edu>
 */

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <minizinc/optimize_constraints.hh>
#include <minizinc/eval_par.hh>
#include <minizinc/flatten_internal.hh>

namespace MiniZinc {

  void
  OptimizeRegistry::reg(const MiniZinc::ASTString& call, optimizer opt) {
    _m.insert(std::make_pair(call, opt));
  }

  OptimizeRegistry::ConstraintStatus
  OptimizeRegistry::process(EnvI& env, MiniZinc::Item* i, MiniZinc::Call* c, Expression*& rewrite) {
    ASTStringMap<optimizer>::t::iterator it = _m.find(c->id());
    if (it != _m.end()) {
      return it->second(env,i,c,rewrite);
    }
    return CS_NONE;
  }

  OptimizeRegistry&
  OptimizeRegistry::registry(void) {
    static OptimizeRegistry reg;
    return reg;
  }

  namespace Optimizers {

    OptimizeRegistry::ConstraintStatus o_linear(EnvI& env, Item* i, Call* c, Expression*& rewrite) {
      ArrayLit* al_c = eval_array_lit(env,c->args()[0]);
      std::vector<IntVal> coeffs(al_c->v().size());
      for (unsigned int i=0; i<al_c->v().size(); i++) {
        coeffs[i] = eval_int(env,al_c->v()[i]);
      }
      ArrayLit* al_x = eval_array_lit(env,c->args()[1]);
      std::vector<KeepAlive> x(al_x->v().size());
      for (unsigned int i=0; i<al_x->v().size(); i++) {
        x[i] = al_x->v()[i];
      }
      IntVal d = 0;
      simplify_lin<IntLit>(coeffs, x, d);
      if (coeffs.size()==0) {
        bool failed;
        if (c->id()==constants().ids.int_.lin_le) {
          failed = (d > eval_int(env,c->args()[2]));
        } else if (c->id()==constants().ids.int_.lin_eq) {
          failed = (d != eval_int(env,c->args()[2]));
        } else {
          failed = (d == eval_int(env,c->args()[2]));
        }
        if (failed) {
          return OptimizeRegistry::CS_FAILED;
        } else {
          return OptimizeRegistry::CS_ENTAILED;
        }
      } else if (coeffs.size()==1 && (i->isa<ConstraintI>() || i->cast<VarDeclI>()->e()->ti()->domain()==constants().lit_true)) {
        VarDecl* vd = x[0]()->cast<Id>()->decl();
        IntSetVal* domain = vd->ti()->domain() ? eval_intset(env,vd->ti()->domain()) : NULL;
        if (c->id()==constants().ids.int_.lin_eq) {
          IntVal rd = eval_int(env,c->args()[2])-d;
          if (rd % coeffs[0] == 0) {
            IntVal nd = rd / coeffs[0];
            if (domain && !domain->contains(nd))
              return OptimizeRegistry::CS_FAILED;
            std::vector<Expression*> args(2);
            args[0] = x[0](); args[1] = IntLit::a(nd);
            Call* c = new Call(Location(), constants().ids.int_.eq, args);
            c->type(Type::varbool());
            rewrite = c;
            return OptimizeRegistry::CS_REWRITE;
          } else {
            return OptimizeRegistry::CS_FAILED;
          }
        } else if (c->id()==constants().ids.int_.lin_le) {
          IntVal ac = std::abs(coeffs[0]);
          IntVal rd = eval_int(env,c->args()[2])-d;
          IntVal ad = std::abs(rd);
          IntVal nd;
          if (ad % ac == 0) {
            nd = rd / coeffs[0];
          } else {
            double nd_d = static_cast<double>(ad.toInt()) / static_cast<double>(ac.toInt());
            if (coeffs[0] >= 0 && rd >= 0) {
              nd = std::floor(nd_d);
            } else if (rd >= 0) {
              nd = -std::floor(nd_d);
            } else if (coeffs[0] >= 0) {
              nd = -std::ceil(nd_d);
            } else {
              nd = std::ceil(nd_d);
            }
          }
          bool swapSign = coeffs[0] < 0;
          if (domain) {
            if (swapSign) {
              if (domain->max() < nd) {
                return OptimizeRegistry::CS_FAILED;
              }
              else if (domain->min() >= nd)
                return OptimizeRegistry::CS_ENTAILED;
            } else {
              if (domain->min() > nd) {
                return OptimizeRegistry::CS_FAILED;
              }
              else if (domain->max() <= nd)
                return OptimizeRegistry::CS_ENTAILED;
            }
            std::vector<Expression*> args(2);
            args[0] = x[0](); args[1] = IntLit::a(nd);
            if (swapSign)
              std::swap(args[0], args[1]);
            Call* nc = new Call(Location(), constants().ids.int_.le, args);
            nc->type(Type::varbool());
            rewrite = nc;
            return OptimizeRegistry::CS_REWRITE;
          }
        }
      } else if (c->id()==constants().ids.int_.lin_eq && coeffs.size()==2  &&
                 ((coeffs[0]==1 && coeffs[1]==-1) || (coeffs[1]==1 && coeffs[0]==-1)) && eval_int(env,c->args()[2])-d==0) {
        std::vector<Expression*> args(2);
        args[0] = x[0](); args[1] = x[1]();
        Call* c = new Call(Location(), constants().ids.int_.eq, args);
        rewrite = c;
        return OptimizeRegistry::CS_REWRITE;
      }
      if (coeffs.size() < al_c->v().size()) {
        std::vector<Expression*> coeffs_e(coeffs.size());
        std::vector<Expression*> x_e(coeffs.size());
        for (unsigned int i=0; i<coeffs.size(); i++) {
          coeffs_e[i] = IntLit::a(coeffs[i]);
          x_e[i] = x[i]();
        }
        ArrayLit* al_c_new = new ArrayLit(al_c->loc(),coeffs_e);
        al_c_new->type(Type::parint(1));
        ArrayLit* al_x_new = new ArrayLit(al_x->loc(),x_e);
        al_x_new->type(al_x->type());

        std::vector<Expression*> args(3);
        args[0] = al_c_new;
        args[1] = al_x_new;
        args[2] = IntLit::a(eval_int(env,c->args()[2])-d);
        Call* nc = new Call(Location(), c->id(), args);
        nc->type(Type::varbool());
        rewrite = nc;
        return OptimizeRegistry::CS_REWRITE;
      }
      return OptimizeRegistry::CS_OK;
    }

    OptimizeRegistry::ConstraintStatus o_lin_exp(EnvI& env, Item* i, Call* c, Expression*& rewrite) {
      if (c->type().isint()) {
        ArrayLit* al_c = eval_array_lit(env,c->args()[0]);
        std::vector<IntVal> coeffs(al_c->v().size());
        for (unsigned int i=0; i<al_c->v().size(); i++) {
          coeffs[i] = eval_int(env,al_c->v()[i]);
        }
        ArrayLit* al_x = eval_array_lit(env,c->args()[1]);
        std::vector<KeepAlive> x(al_x->v().size());
        for (unsigned int i=0; i<al_x->v().size(); i++) {
          x[i] = al_x->v()[i];
        }
        IntVal d = eval_int(env,c->args()[2]);
        simplify_lin<IntLit>(coeffs, x, d);
        if (coeffs.size()==0) {
          rewrite = IntLit::a(d);
          return OptimizeRegistry::CS_REWRITE;
        } else if (coeffs.size() < al_c->v().size()) {
          if (coeffs.size()==1 && coeffs[0]==1 && d==0) {
            rewrite = x[0]();
            return OptimizeRegistry::CS_REWRITE;
          }

          std::vector<Expression*> coeffs_e(coeffs.size());
          std::vector<Expression*> x_e(coeffs.size());
          for (unsigned int i=0; i<coeffs.size(); i++) {
            coeffs_e[i] = IntLit::a(coeffs[i]);
            x_e[i] = x[i]();
          }
          ArrayLit* al_c_new = new ArrayLit(al_c->loc(),coeffs_e);
          al_c_new->type(Type::parint(1));
          ArrayLit* al_x_new = new ArrayLit(al_x->loc(),x_e);
          al_x_new->type(al_x->type());

          std::vector<Expression*> args(3);
          args[0] = al_c_new;
          args[1] = al_x_new;
          args[2] = IntLit::a(d);
          Call* nc = new Call(Location(),c->id(),args);
          nc->type(c->type());
          rewrite = nc;
          return OptimizeRegistry::CS_REWRITE;
        }
      }
      return OptimizeRegistry::CS_OK;
    }

    OptimizeRegistry::ConstraintStatus o_element(EnvI& env, Item* i, Call* c, Expression*& rewrite) {
      if (c->args()[0]->isa<IntLit>()) {
        IntVal idx = eval_int(env,c->args()[0]);
        ArrayLit* al = eval_array_lit(env,c->args()[1]);
        if (idx < 1 || idx > al->v().size()) {
          return OptimizeRegistry::CS_FAILED;
        }
        Expression* result = al->v()[idx.toInt()-1];
        std::vector<Expression*> args(2);
        args[0] = result;
        args[1] = c->args()[2];
        Call* eq = new Call(Location(),constants().ids.int_.eq,args);
        rewrite = eq;
        return OptimizeRegistry::CS_REWRITE;
      }
      return OptimizeRegistry::CS_OK;
    }

    class Register {
    public:
      Register(void) {
        GCLock lock;
        Model* m = new Model;
        ASTString id_element("array_int_element");
        ASTString id_var_element("array_var_int_element");
        std::vector<Expression*> e;
        e.push_back(new StringLit(Location(),id_element));
        e.push_back(new StringLit(Location(),id_var_element));
        m->addItem(new ConstraintI(Location(),new ArrayLit(Location(),e)));
        OptimizeRegistry::registry().reg(constants().ids.int_.lin_eq, o_linear);
        OptimizeRegistry::registry().reg(constants().ids.int_.lin_le, o_linear);
        OptimizeRegistry::registry().reg(constants().ids.int_.lin_ne, o_linear);
        OptimizeRegistry::registry().reg(id_element, o_element);
        OptimizeRegistry::registry().reg(constants().ids.lin_exp, o_lin_exp);
        OptimizeRegistry::registry().reg(id_var_element, o_element);
      }
    } _r;

  }

}<|endoftext|>


<commit_before>#include <math.h>
#include <limits.h>
#include <time.h>
#include "angort.h"

using namespace angort;
/*
 * Mappings for (some) standard maths library functions
 */

// macro for helping generate unary float functions
#define FN(f) a->pushDouble(f(a->popDouble()))

%name math

%wordargs atan2 dd (y x -- atan2(y,x))
{
    a->pushDouble(atan2(p0,p1));
}

%word round (x -- x to nearest int, as a double)
{
    FN(round);
}
%word cos (x -- cos x)
{
    FN(cos);
}
%word sin (x -- sin x)
{
    FN(sin);
}
%word tan (x -- tan x)
{
    FN(tan);
}
%word ln (x -- ln x)
{
    FN(log);
}
%word log (x -- ln x)
{
    FN(log10);
}
%word log2 (x -- log2 x)
{
    FN(log2);
}
%word sqrt (x -- sqrt x)
{
    FN(sqrt);
}

%word exp (x -- exp x)
{
    FN(exp);
}

%word pow (x y -- x^y)
{
    double y = a->popDouble();
    double x = a->popDouble();
    a->pushDouble(pow(x,y));
}

#define ABS(x) ((x)<0 ? -(x) : (x))

%word abs (x -- abs(x))
{
    Value *v = a->popval();
    if(v->t == Types::tInteger)
        a->pushInt(ABS(v->toInt()));
    else if(v->t == Types::tDouble)
        a->pushDouble(ABS(v->toDouble()));
    else if(v->t == Types::tLong)
        a->pushLong(ABS(v->toLong()));
    else if(v->t == Types::tFloat)
        a->pushFloat(ABS(v->toFloat()));
    else
        throw RUNT(EX_TYPE,"").set("Bad type for abs(): %s",v->t->name);
}

%word fmod (x y -- fmod(x,y))
{
    double y = a->popDouble();
    double x = a->popDouble();
    a->pushDouble(fmod(x,y));
}

%word srand (time/none --) set the random number generator seed.
Sets the random number generator seed. If none, use the timestamp.
{
    Value *v = a->popval();
    long t;
    if(v->isNone())
        time(&t);
    else
        t = v->toInt();
    srand48_r(t,&a->rnd);
}

%word rand (-- i) stack an integer random number
Stacks an integer random number from 0 to the maximum integer. Typically
used in "rand N %" to give a number from 0 to N-1.
{
    long r;
    lrand48_r(&a->rnd,&r);
    a->pushInt(r & UINT_MAX);
}

%word frand (-- random double [0.1])
Uses drand48 to generate a random number from 0 to 1.
{
    double r;
    drand48_r(&a->rnd,&r);
    a->pushDouble(r);
}

// Knuth, TAOCP vol2 p.122
inline double rand_gauss (Runtime *a,double mean,double sigma) {
    double v1,v2,s;
    do {
        drand48_r(&a->rnd,&v1);
        v1 = v1*2-1;
        drand48_r(&a->rnd,&v2);
        v2 = v2*2-1;
        s = v1*v1 + v2*v2;
    } while ( s >= 1.0 );

    if (s == 0.0)
        s=0.0;
    else
        s=(v1*sqrt(-2.0 * log(s) / s));
    return (s*sigma)+mean;
}

// http://azzalini.stat.unipd.it/SN/
inline double skewnormal(Runtime *a,double location,double scale, double shape){
    double p = rand_gauss(a,0,1);
    double q = rand_gauss(a,0,1);
    if(q>shape*p)
        p*=-1;
    return location+scale*p;
}

%wordargs normrand nn (mean sigma -- normally distributed random number)
Generate a normally-distributed random number, as a double.
{
    a->pushDouble(rand_gauss(a,p0,p1));
}

%wordargs skewnormrand nnn (location scale shape -- n) Skew-normal random number
Generate a skew-normal distributed random number as a double.
{
    a->pushDouble(skewnormal(a,p0,p1,p2));
}

%init
{
    // set up a constant PI
    Namespace *ns = a->ang->names.getSpaceByName("math");
    if(ns->get("PI")<0){
        int n = ns->addConst("PI",false);
        Value *v = ns->getVal(n);
        Types::tDouble->set(v,3.14159265358979323846);
    }

}
<commit_msg>missing trig funcs<commit_after>#include <math.h>
#include <limits.h>
#include <time.h>
#include "angort.h"

using namespace angort;
/*
 * Mappings for (some) standard maths library functions
 */

// macro for helping generate unary float functions
#define FN(f) a->pushDouble(f(a->popDouble()))

%name math

%wordargs atan2 dd (y x -- atan2(y,x))
{
    a->pushDouble(atan2(p0,p1));
}

%word round (x -- x to nearest int, as a double)
{
    FN(round);
}
%word cos (x -- cos x)
{
    FN(cos);
}
%word sin (x -- sin x)
{
    FN(sin);
}
%word tan (x -- tan x)
{
    FN(tan);
}
%word acos (x -- cos x)
{
    FN(acos);
}
%word asin (x -- sin x)
{
    FN(asin);
}
%word atan (x -- tan x)
{
    FN(atan);
}
%word ln (x -- ln x)
{
    FN(log);
}
%word log (x -- ln x)
{
    FN(log10);
}
%word log2 (x -- log2 x)
{
    FN(log2);
}
%word sqrt (x -- sqrt x)
{
    FN(sqrt);
}

%word exp (x -- exp x)
{
    FN(exp);
}

%word pow (x y -- x^y)
{
    double y = a->popDouble();
    double x = a->popDouble();
    a->pushDouble(pow(x,y));
}

#define ABS(x) ((x)<0 ? -(x) : (x))

%word abs (x -- abs(x))
{
    Value *v = a->popval();
    if(v->t == Types::tInteger)
        a->pushInt(ABS(v->toInt()));
    else if(v->t == Types::tDouble)
        a->pushDouble(ABS(v->toDouble()));
    else if(v->t == Types::tLong)
        a->pushLong(ABS(v->toLong()));
    else if(v->t == Types::tFloat)
        a->pushFloat(ABS(v->toFloat()));
    else
        throw RUNT(EX_TYPE,"").set("Bad type for abs(): %s",v->t->name);
}

%word fmod (x y -- fmod(x,y))
{
    double y = a->popDouble();
    double x = a->popDouble();
    a->pushDouble(fmod(x,y));
}

%word srand (time/none --) set the random number generator seed.
Sets the random number generator seed. If none, use the timestamp.
{
    Value *v = a->popval();
    long t;
    if(v->isNone())
        time(&t);
    else
        t = v->toInt();
    srand48_r(t,&a->rnd);
}

%word rand (-- i) stack an integer random number
Stacks an integer random number from 0 to the maximum integer. Typically
used in "rand N %" to give a number from 0 to N-1.
{
    long r;
    lrand48_r(&a->rnd,&r);
    a->pushInt(r & UINT_MAX);
}

%word frand (-- random double [0.1])
Uses drand48 to generate a random number from 0 to 1.
{
    double r;
    drand48_r(&a->rnd,&r);
    a->pushDouble(r);
}

// Knuth, TAOCP vol2 p.122
inline double rand_gauss (Runtime *a,double mean,double sigma) {
    double v1,v2,s;
    do {
        drand48_r(&a->rnd,&v1);
        v1 = v1*2-1;
        drand48_r(&a->rnd,&v2);
        v2 = v2*2-1;
        s = v1*v1 + v2*v2;
    } while ( s >= 1.0 );

    if (s == 0.0)
        s=0.0;
    else
        s=(v1*sqrt(-2.0 * log(s) / s));
    return (s*sigma)+mean;
}

// http://azzalini.stat.unipd.it/SN/
inline double skewnormal(Runtime *a,double location,double scale, double shape){
    double p = rand_gauss(a,0,1);
    double q = rand_gauss(a,0,1);
    if(q>shape*p)
        p*=-1;
    return location+scale*p;
}

%wordargs normrand nn (mean sigma -- normally distributed random number)
Generate a normally-distributed random number, as a double.
{
    a->pushDouble(rand_gauss(a,p0,p1));
}

%wordargs skewnormrand nnn (location scale shape -- n) Skew-normal random number
Generate a skew-normal distributed random number as a double.
{
    a->pushDouble(skewnormal(a,p0,p1,p2));
}

%init
{
    // set up a constant PI
    Namespace *ns = a->ang->names.getSpaceByName("math");
    if(ns->get("PI")<0){
        int n = ns->addConst("PI",false);
        Value *v = ns->getVal(n);
        Types::tDouble->set(v,3.14159265358979323846);
    }

}
<|endoftext|>


<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 *
 * Modified by Cloudius Systems
 * Copyright 2015 Cloudius Systems
 */

/*
 * This file is part of Scylla.
 *
 * Scylla is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Scylla 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 Scylla.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "dht/boot_strapper.hh"
#include "service/storage_service.hh"

namespace dht {

future<> boot_strapper::bootstrap() {
    // FIXME: Stream data from other nodes
    service::get_local_storage_service().finish_bootstrapping();
    return make_ready_future<>();
#if 0
    if (logger.isDebugEnabled())
        logger.debug("Beginning bootstrap process");

    RangeStreamer streamer = new RangeStreamer(tokenMetadata, tokens, address, "Bootstrap");
    streamer.addSourceFilter(new RangeStreamer.FailureDetectorSourceFilter(FailureDetector.instance));

    for (String keyspaceName : Schema.instance.getNonSystemKeyspaces())
    {
        AbstractReplicationStrategy strategy = Keyspace.open(keyspaceName).getReplicationStrategy();
        streamer.addRanges(keyspaceName, strategy.getPendingAddressRanges(tokenMetadata, tokens, address));
    }

    try
    {
        streamer.fetchAsync().get();
        StorageService.instance.finishBootstrapping();
    }
    catch (InterruptedException e)
    {
        throw new RuntimeException("Interrupted while waiting on boostrap to complete. Bootstrap will have to be restarted.");
    }
    catch (ExecutionException e)
    {
        throw new RuntimeException("Error during boostrap: " + e.getCause().getMessage(), e.getCause());
    }
#endif
}

std::unordered_set<token> boot_strapper::get_bootstrap_tokens(token_metadata metadata, database& db) {
#if 0
    Collection<String> initialTokens = DatabaseDescriptor.getInitialTokens();
    // if user specified tokens, use those
    if (initialTokens.size() > 0)
    {
        logger.debug("tokens manually specified as {}",  initialTokens);
        List<Token> tokens = new ArrayList<Token>(initialTokens.size());
        for (String tokenString : initialTokens)
        {
            Token token = StorageService.getPartitioner().getTokenFactory().fromString(tokenString);
            if (metadata.getEndpoint(token) != null)
                throw new ConfigurationException("Bootstrapping to existing token " + tokenString + " is not allowed (decommission/removenode the old node first).");
            tokens.add(token);
        }
        return tokens;
    }
#endif
    size_t num_tokens = db.get_config().num_tokens();
    if (num_tokens < 1) {
        throw std::runtime_error("num_tokens must be >= 1");
    }

    // if (numTokens == 1)
    //     logger.warn("Picking random token for a single vnode.  You should probably add more vnodes; failing that, you should probably specify the token manually");

    return get_random_tokens(metadata, num_tokens);
}

std::unordered_set<token> boot_strapper::get_random_tokens(token_metadata metadata, size_t num_tokens) {
    std::unordered_set<token> tokens;
    while (tokens.size() < num_tokens) {
        auto token = global_partitioner().get_random_token();
        auto ep = metadata.get_endpoint(token);
        if (!ep) {
            tokens.emplace(token);
        }
    }
    return tokens;
}

} // namespace dht
<commit_msg>boot_strapper: Add debug info for get_bootstrap_tokens<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 *
 * Modified by Cloudius Systems
 * Copyright 2015 Cloudius Systems
 */

/*
 * This file is part of Scylla.
 *
 * Scylla is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Scylla 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 Scylla.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "dht/boot_strapper.hh"
#include "service/storage_service.hh"

namespace dht {

future<> boot_strapper::bootstrap() {
    // FIXME: Stream data from other nodes
    service::get_local_storage_service().finish_bootstrapping();
    return make_ready_future<>();
#if 0
    if (logger.isDebugEnabled())
        logger.debug("Beginning bootstrap process");

    RangeStreamer streamer = new RangeStreamer(tokenMetadata, tokens, address, "Bootstrap");
    streamer.addSourceFilter(new RangeStreamer.FailureDetectorSourceFilter(FailureDetector.instance));

    for (String keyspaceName : Schema.instance.getNonSystemKeyspaces())
    {
        AbstractReplicationStrategy strategy = Keyspace.open(keyspaceName).getReplicationStrategy();
        streamer.addRanges(keyspaceName, strategy.getPendingAddressRanges(tokenMetadata, tokens, address));
    }

    try
    {
        streamer.fetchAsync().get();
        StorageService.instance.finishBootstrapping();
    }
    catch (InterruptedException e)
    {
        throw new RuntimeException("Interrupted while waiting on boostrap to complete. Bootstrap will have to be restarted.");
    }
    catch (ExecutionException e)
    {
        throw new RuntimeException("Error during boostrap: " + e.getCause().getMessage(), e.getCause());
    }
#endif
}

std::unordered_set<token> boot_strapper::get_bootstrap_tokens(token_metadata metadata, database& db) {
#if 0
    Collection<String> initialTokens = DatabaseDescriptor.getInitialTokens();
    // if user specified tokens, use those
    if (initialTokens.size() > 0)
    {
        logger.debug("tokens manually specified as {}",  initialTokens);
        List<Token> tokens = new ArrayList<Token>(initialTokens.size());
        for (String tokenString : initialTokens)
        {
            Token token = StorageService.getPartitioner().getTokenFactory().fromString(tokenString);
            if (metadata.getEndpoint(token) != null)
                throw new ConfigurationException("Bootstrapping to existing token " + tokenString + " is not allowed (decommission/removenode the old node first).");
            tokens.add(token);
        }
        return tokens;
    }
#endif
    size_t num_tokens = db.get_config().num_tokens();
    if (num_tokens < 1) {
        throw std::runtime_error("num_tokens must be >= 1");
    }

    // if (numTokens == 1)
    //     logger.warn("Picking random token for a single vnode.  You should probably add more vnodes; failing that, you should probably specify the token manually");

    auto tokens = get_random_tokens(metadata, num_tokens);
    logger.debug("Get bootstrap_tokens={}", tokens);
    return tokens;
}

std::unordered_set<token> boot_strapper::get_random_tokens(token_metadata metadata, size_t num_tokens) {
    std::unordered_set<token> tokens;
    while (tokens.size() < num_tokens) {
        auto token = global_partitioner().get_random_token();
        auto ep = metadata.get_endpoint(token);
        if (!ep) {
            tokens.emplace(token);
        }
    }
    return tokens;
}

} // namespace dht
<|endoftext|>


<commit_before>// We need optimization to convert target-specific masked loads/stores to llvm
// generic loads/stores
// RUN: %clangxx_asan -o %t %s -mavx -O1
// RUN: not %run %t l1 2>&1 | FileCheck -check-prefix=CHECK-L1 %s
// RUN: %run %t l6 | FileCheck -check-prefix=CHECK-L6 %s
// RUN: %run %t la | FileCheck -check-prefix=CHECK-LA %s
// RUN: not %run %t s1 2>&1 | FileCheck -check-prefix=CHECK-S1 %s
// RUN: %run %t s6 | FileCheck -check-prefix=CHECK-S6 %s
// RUN: %run %t sa | FileCheck -check-prefix=CHECK-SA %s
// REQUIRES: x86-target-arch
#include <assert.h>
#include <stdio.h>
#include <x86intrin.h>

float g_vec3[3] = {1802398064.0, 1881171305.0, 25961.0};

void maskedstore_ps_0110(__m128 a) {
  _mm_maskstore_ps(g_vec3, (__v4si){0, -1, -1, 0}, a);
}

void maskedstore_ps_0001(__m128 a) {
  _mm_maskstore_ps(g_vec3, (__v4si){0, 0, 0, -1}, a);
}

void maskedstore_ps_1010(__m128 a) {
  _mm_maskstore_ps(g_vec3, (__v4si){-1, 0, -1, 0}, a);
}

__m128i maskedload_ps_0110() {
  return _mm_maskload_ps(g_vec3, (__v4si){0, -1, -1, 0});
}

__m128i maskedload_ps_0001() {
  return _mm_maskload_ps(g_vec3, (__v4si){0, 0, 0, -1});
}

__m128i maskedload_ps_1010() {
  return _mm_maskload_ps(g_vec3, (__v4si){-1, 0, -1, 0});
}

__m128 a = (__v4sf){1.0, 2.0, 3.0, 4.0};

void print_vector(__v4sf v) {
  printf("%d,%d,%d,%d\n", (int)v[0], (int)v[1], (int)v[2], (int)v[3]);
}
void print_vector3(float * v) {
  printf("%d,%d,%d\n", (int)v[0], (int)v[1], (int)v[2]);
}

int main(int argc, char **argv) {
  assert(argc > 1);
  bool isLoad = argv[1][0] == 'l';
  assert(isLoad || argv[1][0] == 's');
  if (isLoad) {
    switch (argv[1][1]) {
    case '1': {
      // CHECK-L1: ERROR: AddressSanitizer
      __v4sf v = maskedload_ps_0001();
      print_vector(v);
      // Should have blown up
      break;
    }
    case '6': {
      // Safe
      __v4sf v = maskedload_ps_0110();
      // CHECK-L6: 0,1881171328,25961,0
      print_vector(v);
      return 0;
    }
    case 'a': {
      // TODO: Poison middle element
      // Safe
      __v4sf v = maskedload_ps_1010();
      // CHECK-LA: 1802398080,0,25961,0
      print_vector(v);
      return 0;
    }
    }
  } else {
    switch (argv[1][1]) {
    case '1':
      // CHECK-S1: ERROR: AddressSanitizer
      maskedstore_ps_0001(a);
      // Should have blown up
      break;
    case '6':
      // Safe
      maskedstore_ps_0110(a);
      // CHECK-S6: 1802398080,2,3
      print_vector3(g_vec3);
      return 0;
    case 'a':
      // TODO: Poison middle element
      // Safe
      maskedstore_ps_1010(a);
      // CHECK-SA: 1,1881171328,3
      print_vector3(g_vec3);
      return 0;
    }
  }
  assert(0);
}
<commit_msg>Revert "Compiler-rt part of D26230: Add (constant) masked load/store support"<commit_after><|endoftext|>


<commit_before>/*
 *
 * Copyright 2015-2016, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <signal.h>
#include <string.h>

#include <memory>
#include <mutex>
#include <sstream>
#include <string>

#include <grpc/support/log.h>

#include "src/core/lib/support/env.h"
#include "test/core/util/port.h"
#include "test/cpp/util/subprocess.h"

using grpc::SubProcess;
typedef std::unique_ptr<SubProcess> SubProcessPtr;
std::vector<SubProcessPtr> g_workers;
SubProcessPtr g_driver;

template <class T>
std::string as_string(const T& val) {
  std::ostringstream out;
  out << val;
  return out.str();
}

static void sighandler(int sig) {
  g_driver->Interrupt();
  for (auto it = g_workers.begin(); it != g_workers.end(); ++it) {
    (*it)->Interrupt();
  }
}

static void register_sighandler() {
  struct sigaction act;
  memset(&act, 0, sizeof(act));
  act.sa_handler = sighandler;

  sigaction(SIGINT, &act, NULL);
  sigaction(SIGTERM, &act, NULL);
}

int main(int argc, char** argv) {
  register_sighandler();

  std::string my_bin = argv[0];
  std::string bin_dir = my_bin.substr(0, my_bin.rfind('/'));

  std::ostringstream env;
  bool first = true;

  for (int i = 0; i < 2; i++) {
    auto port = grpc_pick_unused_port_or_die();
    std::vector<std::string> args = {bin_dir + "/qps_worker", "-driver_port",
                                     as_string(port)};
    g_workers.emplace_back(new SubProcess(args));
    if (!first) env << ",";
    env << "localhost:" << port;
    first = false;
  }

  gpr_setenv("QPS_WORKERS", env.str().c_str());
  std::vector<std::string> args = {bin_dir + "/qps_json_driver"};
  for (int i = 1; i < argc; i++) {
    args.push_back(argv[i]);
  }

  g_driver.reset(new SubProcess(args));
  const int driver_join_status = g_driver->Join();
  for (auto it = g_workers.begin(); it != g_workers.end(); ++it) {
    (*it)->Interrupt();
  }
  for (auto it = g_workers.begin(); it != g_workers.end(); ++it) {
    (*it)->Join();
  }
  GPR_ASSERT(driver_join_status == 0);
}
<commit_msg>PR comments<commit_after>/*
 *
 * Copyright 2015-2016, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <signal.h>
#include <string.h>

#include <memory>
#include <mutex>
#include <sstream>
#include <string>

#include <grpc/support/log.h>

#include "src/core/lib/support/env.h"
#include "test/core/util/port.h"
#include "test/cpp/util/subprocess.h"

using grpc::SubProcess;
typedef std::unique_ptr<SubProcess> SubProcessPtr;
SubProcessPtr g_driver;
constexpr auto kNumWorkers = 2;
std::vector<SubProcessPtr> g_workers(2);

template <class T>
std::string as_string(const T& val) {
  std::ostringstream out;
  out << val;
  return out.str();
}

static void sighandler(int sig) {
  const int errno_saved = errno;
  g_driver->Interrupt();
  for (const auto& worker : g_workers)
    if (worker) worker->Interrupt();
  errno = errno_saved;
}

static void register_sighandler() {
  struct sigaction act;
  memset(&act, 0, sizeof(act));
  act.sa_handler = sighandler;

  sigaction(SIGINT, &act, NULL);
  sigaction(SIGTERM, &act, NULL);
}

int main(int argc, char** argv) {
  register_sighandler();

  std::string my_bin = argv[0];
  std::string bin_dir = my_bin.substr(0, my_bin.rfind('/'));

  std::ostringstream env;
  bool first = true;

  for (int i = 0; i < kNumWorkers; i++) {
    const auto port = grpc_pick_unused_port_or_die();
    std::vector<std::string> args = {bin_dir + "/qps_worker", "-driver_port",
                                     as_string(port)};
    g_workers[i].reset(new SubProcess(args));
    if (!first) env << ",";
    env << "localhost:" << port;
    first = false;
  }

  gpr_setenv("QPS_WORKERS", env.str().c_str());
  std::vector<std::string> args = {bin_dir + "/qps_json_driver"};
  for (int i = 1; i < argc; i++) {
    args.push_back(argv[i]);
  }

  g_driver.reset(new SubProcess(args));
  const int driver_join_status = g_driver->Join();
  for (const auto& worker : g_workers)
    if (worker) worker->Interrupt();
  for (const auto& worker : g_workers)
    if (worker) worker->Join();
  GPR_ASSERT(driver_join_status == 0);
}
<|endoftext|>


<commit_before>/*
 *
 * Copyright 2016, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "test/cpp/util/proto_file_parser.h"

#include <algorithm>
#include <iostream>
#include <sstream>
#include <unordered_set>

#include <grpc++/support/config.h>

namespace grpc {
namespace testing {
namespace {

// Match the user input method string to the full_name from method descriptor.
bool MethodNameMatch(const grpc::string& full_name, const grpc::string& input) {
  grpc::string clean_input = input;
  std::replace(clean_input.begin(), clean_input.end(), '/', '.');
  if (clean_input.size() > full_name.size()) {
    return false;
  }
  return full_name.compare(full_name.size() - clean_input.size(),
                           clean_input.size(), clean_input) == 0;
}
}  // namespace

class ErrorPrinter : public protobuf::compiler::MultiFileErrorCollector {
 public:
  explicit ErrorPrinter(ProtoFileParser* parser) : parser_(parser) {}

  void AddError(const grpc::string& filename, int line, int column,
                const grpc::string& message) GRPC_OVERRIDE {
    std::ostringstream oss;
    oss << "error " << filename << " " << line << " " << column << " "
        << message << "\n";
    parser_->LogError(oss.str());
  }

  void AddWarning(const grpc::string& filename, int line, int column,
                  const grpc::string& message) GRPC_OVERRIDE {
    std::cerr << "warning " << filename << " " << line << " " << column << " "
              << message << std::endl;
  }

 private:
  ProtoFileParser* parser_;  // not owned
};

ProtoFileParser::ProtoFileParser(std::shared_ptr<grpc::Channel> channel,
                                 const grpc::string& proto_path,
                                 const grpc::string& protofiles)
    : has_error_(false) {
  std::vector<std::string> service_list;
  if (channel) {
    reflection_db_.reset(new grpc::ProtoReflectionDescriptorDatabase(channel));
    reflection_db_->GetServices(&service_list);
  }

  std::unordered_set<grpc::string> known_services;
  if (!protofiles.empty()) {
    source_tree_.MapPath("", proto_path);
    error_printer_.reset(new ErrorPrinter(this));
    importer_.reset(
        new protobuf::compiler::Importer(&source_tree_, error_printer_.get()));

    grpc::string file_name;
    std::stringstream ss(protofiles);
    while (std::getline(ss, file_name, ',')) {
      const auto* file_desc = importer_->Import(file_name);
      if (file_desc) {
        for (int i = 0; i < file_desc->service_count(); i++) {
          service_desc_list_.push_back(file_desc->service(i));
          known_services.insert(file_desc->service(i)->full_name());
        }
      } else {
        std::cerr << file_name << " not found" << std::endl;
      }
    }

    file_db_.reset(new protobuf::DescriptorPoolDatabase(*importer_->pool()));
  }

  if (!reflection_db_ && !file_db_) {
    LogError("No available proto database");
    return;
  }

  if (!reflection_db_) {
    desc_db_ = std::move(file_db_);
  } else if (!file_db_) {
    desc_db_ = std::move(reflection_db_);
  } else {
    desc_db_.reset(new protobuf::MergedDescriptorDatabase(reflection_db_.get(),
                                                          file_db_.get()));
  }

  desc_pool_.reset(new protobuf::DescriptorPool(desc_db_.get()));
  dynamic_factory_.reset(new protobuf::DynamicMessageFactory(desc_pool_.get()));

  for (auto it = service_list.begin(); it != service_list.end(); it++) {
    if (known_services.find(*it) == known_services.end()) {
      if (const protobuf::ServiceDescriptor* service_desc =
              desc_pool_->FindServiceByName(*it)) {
        service_desc_list_.push_back(service_desc);
        known_services.insert(*it);
      }
    }
  }
}

ProtoFileParser::~ProtoFileParser() {}

grpc::string ProtoFileParser::GetFullMethodName(const grpc::string& method) {
  has_error_ = false;
  const protobuf::MethodDescriptor* method_descriptor = nullptr;
  for (auto it = service_desc_list_.begin(); it != service_desc_list_.end();
       it++) {
    const auto* service_desc = *it;
    for (int j = 0; j < service_desc->method_count(); j++) {
      const auto* method_desc = service_desc->method(j);
      if (MethodNameMatch(method_desc->full_name(), method)) {
        if (method_descriptor) {
          std::ostringstream error_stream("Ambiguous method names: ");
          error_stream << method_descriptor->full_name() << " ";
          error_stream << method_desc->full_name();
          LogError(error_stream.str());
        }
        method_descriptor = method_desc;
      }
    }
  }
  if (!method_descriptor) {
    LogError("Method name not found");
  }
  if (has_error_) {
    return "";
  }

  return method_descriptor->full_name();
}

grpc::string ProtoFileParser::GetFormatedMethodName(
    const grpc::string& method) {
  has_error_ = false;
  grpc::string formated_method_name = GetFullMethodName(method);
  if (has_error_) {
    return "";
  }
  size_t last_dot = formated_method_name.find_last_of('.');
  if (last_dot != grpc::string::npos) {
    formated_method_name[last_dot] = '/';
  }
  formated_method_name.insert(formated_method_name.begin(), '/');
  return formated_method_name;
}

grpc::string ProtoFileParser::GetMessageTypeFromMethod(
    const grpc::string& method, bool is_request) {
  has_error_ = false;
  grpc::string full_method_name = GetFullMethodName(method);
  if (has_error_) {
    return "";
  }
  const protobuf::MethodDescriptor* method_desc =
      desc_pool_->FindMethodByName(full_method_name);
  if (!method_desc) {
    LogError("Method not found");
    return "";
  }

  return is_request ? method_desc->input_type()->full_name()
                    : method_desc->output_type()->full_name();
}

grpc::string ProtoFileParser::GetSerializedProtoFromMethod(
    const grpc::string& method, const grpc::string& text_format_proto,
    bool is_request) {
  has_error_ = false;
  grpc::string message_type_name = GetMessageTypeFromMethod(method, is_request);
  if (has_error_) {
    return "";
  }
  return GetSerializedProtoFromMessageType(message_type_name,
                                           text_format_proto);
}

grpc::string ProtoFileParser::GetTextFormatFromMethod(
    const grpc::string& method, const grpc::string& serialized_proto,
    bool is_request) {
  has_error_ = false;
  grpc::string message_type_name = GetMessageTypeFromMethod(method, is_request);
  if (has_error_) {
    return "";
  }
  return GetTextFormatFromMessageType(message_type_name, serialized_proto);
}

grpc::string ProtoFileParser::GetSerializedProtoFromMessageType(
    const grpc::string& message_type_name,
    const grpc::string& text_format_proto) {
  has_error_ = false;
  grpc::string serialized;
  const protobuf::Descriptor* desc =
      desc_pool_->FindMessageTypeByName(message_type_name);
  if (!desc) {
    LogError("Message type not found");
    return "";
  }
  std::unique_ptr<grpc::protobuf::Message> msg(
      dynamic_factory_->GetPrototype(desc)->New());
  bool ok = protobuf::TextFormat::ParseFromString(text_format_proto, msg.get());
  if (!ok) {
    LogError("Failed to parse text format to proto.");
    return "";
  }
  ok = msg->SerializeToString(&serialized);
  if (!ok) {
    LogError("Failed to serialize proto.");
    return "";
  }
  return serialized;
}

grpc::string ProtoFileParser::GetTextFormatFromMessageType(
    const grpc::string& message_type_name,
    const grpc::string& serialized_proto) {
  has_error_ = false;
  const protobuf::Descriptor* desc =
      desc_pool_->FindMessageTypeByName(message_type_name);
  if (!desc) {
    LogError("Message type not found");
    return "";
  }
  std::unique_ptr<grpc::protobuf::Message> msg(
      dynamic_factory_->GetPrototype(desc)->New());
  if (!msg->ParseFromString(serialized_proto)) {
    LogError("Failed to deserialize proto.");
    return "";
  }
  grpc::string text_format;
  if (!protobuf::TextFormat::PrintToString(*msg.get(), &text_format)) {
    LogError("Failed to print proto message to text format");
    return "";
  }
  return text_format;
}

void ProtoFileParser::LogError(const grpc::string& error_msg) {
  if (!error_msg.empty()) {
    std::cerr << error_msg << std::endl;
  }
  has_error_ = true;
}

}  // namespace testing
}  // namespace grpc
<commit_msg>Print more information about ambiguous method names<commit_after>/*
 *
 * Copyright 2016, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "test/cpp/util/proto_file_parser.h"

#include <algorithm>
#include <iostream>
#include <sstream>
#include <unordered_set>

#include <grpc++/support/config.h>

namespace grpc {
namespace testing {
namespace {

// Match the user input method string to the full_name from method descriptor.
bool MethodNameMatch(const grpc::string& full_name, const grpc::string& input) {
  grpc::string clean_input = input;
  std::replace(clean_input.begin(), clean_input.end(), '/', '.');
  if (clean_input.size() > full_name.size()) {
    return false;
  }
  return full_name.compare(full_name.size() - clean_input.size(),
                           clean_input.size(), clean_input) == 0;
}
}  // namespace

class ErrorPrinter : public protobuf::compiler::MultiFileErrorCollector {
 public:
  explicit ErrorPrinter(ProtoFileParser* parser) : parser_(parser) {}

  void AddError(const grpc::string& filename, int line, int column,
                const grpc::string& message) GRPC_OVERRIDE {
    std::ostringstream oss;
    oss << "error " << filename << " " << line << " " << column << " "
        << message << "\n";
    parser_->LogError(oss.str());
  }

  void AddWarning(const grpc::string& filename, int line, int column,
                  const grpc::string& message) GRPC_OVERRIDE {
    std::cerr << "warning " << filename << " " << line << " " << column << " "
              << message << std::endl;
  }

 private:
  ProtoFileParser* parser_;  // not owned
};

ProtoFileParser::ProtoFileParser(std::shared_ptr<grpc::Channel> channel,
                                 const grpc::string& proto_path,
                                 const grpc::string& protofiles)
    : has_error_(false) {
  std::vector<std::string> service_list;
  if (channel) {
    reflection_db_.reset(new grpc::ProtoReflectionDescriptorDatabase(channel));
    reflection_db_->GetServices(&service_list);
  }

  std::unordered_set<grpc::string> known_services;
  if (!protofiles.empty()) {
    source_tree_.MapPath("", proto_path);
    error_printer_.reset(new ErrorPrinter(this));
    importer_.reset(
        new protobuf::compiler::Importer(&source_tree_, error_printer_.get()));

    grpc::string file_name;
    std::stringstream ss(protofiles);
    while (std::getline(ss, file_name, ',')) {
      const auto* file_desc = importer_->Import(file_name);
      if (file_desc) {
        for (int i = 0; i < file_desc->service_count(); i++) {
          service_desc_list_.push_back(file_desc->service(i));
          known_services.insert(file_desc->service(i)->full_name());
        }
      } else {
        std::cerr << file_name << " not found" << std::endl;
      }
    }

    file_db_.reset(new protobuf::DescriptorPoolDatabase(*importer_->pool()));
  }

  if (!reflection_db_ && !file_db_) {
    LogError("No available proto database");
    return;
  }

  if (!reflection_db_) {
    desc_db_ = std::move(file_db_);
  } else if (!file_db_) {
    desc_db_ = std::move(reflection_db_);
  } else {
    desc_db_.reset(new protobuf::MergedDescriptorDatabase(reflection_db_.get(),
                                                          file_db_.get()));
  }

  desc_pool_.reset(new protobuf::DescriptorPool(desc_db_.get()));
  dynamic_factory_.reset(new protobuf::DynamicMessageFactory(desc_pool_.get()));

  for (auto it = service_list.begin(); it != service_list.end(); it++) {
    if (known_services.find(*it) == known_services.end()) {
      if (const protobuf::ServiceDescriptor* service_desc =
              desc_pool_->FindServiceByName(*it)) {
        service_desc_list_.push_back(service_desc);
        known_services.insert(*it);
      }
    }
  }
}

ProtoFileParser::~ProtoFileParser() {}

grpc::string ProtoFileParser::GetFullMethodName(const grpc::string& method) {
  has_error_ = false;
  const protobuf::MethodDescriptor* method_descriptor = nullptr;
  for (auto it = service_desc_list_.begin(); it != service_desc_list_.end();
       it++) {
    const auto* service_desc = *it;
    for (int j = 0; j < service_desc->method_count(); j++) {
      const auto* method_desc = service_desc->method(j);
      if (MethodNameMatch(method_desc->full_name(), method)) {
        if (method_descriptor) {
          std::ostringstream error_stream;
          error_stream << "Ambiguous method names: ";
          error_stream << method_descriptor->full_name() << " ";
          error_stream << method_desc->full_name();
          LogError(error_stream.str());
        }
        method_descriptor = method_desc;
      }
    }
  }
  if (!method_descriptor) {
    LogError("Method name not found");
  }
  if (has_error_) {
    return "";
  }

  return method_descriptor->full_name();
}

grpc::string ProtoFileParser::GetFormatedMethodName(
    const grpc::string& method) {
  has_error_ = false;
  grpc::string formated_method_name = GetFullMethodName(method);
  if (has_error_) {
    return "";
  }
  size_t last_dot = formated_method_name.find_last_of('.');
  if (last_dot != grpc::string::npos) {
    formated_method_name[last_dot] = '/';
  }
  formated_method_name.insert(formated_method_name.begin(), '/');
  return formated_method_name;
}

grpc::string ProtoFileParser::GetMessageTypeFromMethod(
    const grpc::string& method, bool is_request) {
  has_error_ = false;
  grpc::string full_method_name = GetFullMethodName(method);
  if (has_error_) {
    return "";
  }
  const protobuf::MethodDescriptor* method_desc =
      desc_pool_->FindMethodByName(full_method_name);
  if (!method_desc) {
    LogError("Method not found");
    return "";
  }

  return is_request ? method_desc->input_type()->full_name()
                    : method_desc->output_type()->full_name();
}

grpc::string ProtoFileParser::GetSerializedProtoFromMethod(
    const grpc::string& method, const grpc::string& text_format_proto,
    bool is_request) {
  has_error_ = false;
  grpc::string message_type_name = GetMessageTypeFromMethod(method, is_request);
  if (has_error_) {
    return "";
  }
  return GetSerializedProtoFromMessageType(message_type_name,
                                           text_format_proto);
}

grpc::string ProtoFileParser::GetTextFormatFromMethod(
    const grpc::string& method, const grpc::string& serialized_proto,
    bool is_request) {
  has_error_ = false;
  grpc::string message_type_name = GetMessageTypeFromMethod(method, is_request);
  if (has_error_) {
    return "";
  }
  return GetTextFormatFromMessageType(message_type_name, serialized_proto);
}

grpc::string ProtoFileParser::GetSerializedProtoFromMessageType(
    const grpc::string& message_type_name,
    const grpc::string& text_format_proto) {
  has_error_ = false;
  grpc::string serialized;
  const protobuf::Descriptor* desc =
      desc_pool_->FindMessageTypeByName(message_type_name);
  if (!desc) {
    LogError("Message type not found");
    return "";
  }
  std::unique_ptr<grpc::protobuf::Message> msg(
      dynamic_factory_->GetPrototype(desc)->New());
  bool ok = protobuf::TextFormat::ParseFromString(text_format_proto, msg.get());
  if (!ok) {
    LogError("Failed to parse text format to proto.");
    return "";
  }
  ok = msg->SerializeToString(&serialized);
  if (!ok) {
    LogError("Failed to serialize proto.");
    return "";
  }
  return serialized;
}

grpc::string ProtoFileParser::GetTextFormatFromMessageType(
    const grpc::string& message_type_name,
    const grpc::string& serialized_proto) {
  has_error_ = false;
  const protobuf::Descriptor* desc =
      desc_pool_->FindMessageTypeByName(message_type_name);
  if (!desc) {
    LogError("Message type not found");
    return "";
  }
  std::unique_ptr<grpc::protobuf::Message> msg(
      dynamic_factory_->GetPrototype(desc)->New());
  if (!msg->ParseFromString(serialized_proto)) {
    LogError("Failed to deserialize proto.");
    return "";
  }
  grpc::string text_format;
  if (!protobuf::TextFormat::PrintToString(*msg.get(), &text_format)) {
    LogError("Failed to print proto message to text format");
    return "";
  }
  return text_format;
}

void ProtoFileParser::LogError(const grpc::string& error_msg) {
  if (!error_msg.empty()) {
    std::cerr << error_msg << std::endl;
  }
  has_error_ = true;
}

}  // namespace testing
}  // namespace grpc
<|endoftext|>


<commit_before>#include "rangefilter.h"

namespace qqsfpm {

/*!
    \qmltype RangeFilter
    \inherits RoleFilter
    \inqmlmodule SortFilterProxyModel
    \brief Filters rows between boundary values

    A RangeFilter is a \l RoleFilter that accepts rows if their data is between the filter's minimum and maximum value.

    In the following example, only rows with their \c price role set to a value between the tow boundary of the slider will be accepted :
    \code
    RangeSlider {
       id: priceRangeSlider
    }

    SortFilterProxyModel {
       sourceModel: priceModel
       filters: RangeFilter {
           roleName: "price"
           minimumValue: priceRangeSlider.first.value
           maximumValue: priceRangeSlider.second.value
       }
    }
    \endcode
*/

/*!
    \qmlproperty int RangeFilter::minimumValue

    This property holds the minimumValue of the filter.
    Rows with a value lower than \c minimumValue will be rejected.

    By default, no value is set.

    \sa minimumInclusive
*/
QVariant RangeFilter::minimumValue() const
{
    return m_minimumValue;
}

void RangeFilter::setMinimumValue(QVariant minimumValue)
{
    if (m_minimumValue == minimumValue)
        return;

    m_minimumValue = minimumValue;
    Q_EMIT minimumValueChanged();
    invalidate();
}

/*!
    \qmlproperty int RangeFilter::minimumInclusive

    This property holds whether the \l minimumValue is inclusive.

    By default, the \l minimumValue is inclusive.

    \sa minimumValue
*/
bool RangeFilter::minimumInclusive() const
{
    return m_minimumInclusive;
}

void RangeFilter::setMinimumInclusive(bool minimumInclusive)
{
    if (m_minimumInclusive == minimumInclusive)
        return;

    m_minimumInclusive = minimumInclusive;
    Q_EMIT minimumInclusiveChanged();
    invalidate();
}

/*!
    \qmlproperty int RangeFilter::maximumValue

    This property holds the maximumValue of the filter.
    Rows with a value higher than \c maximumValue will be rejected.

    By default, no value is set.

    \sa maximumInclusive
*/
QVariant RangeFilter::maximumValue() const
{
    return m_maximumValue;
}

void RangeFilter::setMaximumValue(QVariant maximumValue)
{
    if (m_maximumValue == maximumValue)
        return;

    m_maximumValue = maximumValue;
    Q_EMIT maximumValueChanged();
    invalidate();
}

/*!
    \qmlproperty int RangeFilter::maximumInclusive

    This property holds whether the \l minimumValue is inclusive.

    By default, the \l minimumValue is inclusive.

    \sa minimumValue
*/
bool RangeFilter::maximumInclusive() const
{
    return m_maximumInclusive;
}

void RangeFilter::setMaximumInclusive(bool maximumInclusive)
{
    if (m_maximumInclusive == maximumInclusive)
        return;

    m_maximumInclusive = maximumInclusive;
    Q_EMIT maximumInclusiveChanged();
    invalidate();
}

bool RangeFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
    QVariant value = sourceData(sourceIndex, proxyModel);
    bool lessThanMin = m_minimumValue.isValid() &&
            m_minimumInclusive ? value < m_minimumValue : value <= m_minimumValue;
    bool moreThanMax = m_maximumValue.isValid() &&
            m_maximumInclusive ? value > m_maximumValue : value >= m_maximumValue;
    return !(lessThanMin || moreThanMax);
}

}
<commit_msg>fix: Fix RangeFilter bug<commit_after>#include "rangefilter.h"

namespace qqsfpm {

/*!
    \qmltype RangeFilter
    \inherits RoleFilter
    \inqmlmodule SortFilterProxyModel
    \brief Filters rows between boundary values

    A RangeFilter is a \l RoleFilter that accepts rows if their data is between the filter's minimum and maximum value.

    In the following example, only rows with their \c price role set to a value between the tow boundary of the slider will be accepted :
    \code
    RangeSlider {
       id: priceRangeSlider
    }

    SortFilterProxyModel {
       sourceModel: priceModel
       filters: RangeFilter {
           roleName: "price"
           minimumValue: priceRangeSlider.first.value
           maximumValue: priceRangeSlider.second.value
       }
    }
    \endcode
*/

/*!
    \qmlproperty int RangeFilter::minimumValue

    This property holds the minimumValue of the filter.
    Rows with a value lower than \c minimumValue will be rejected.

    By default, no value is set.

    \sa minimumInclusive
*/
QVariant RangeFilter::minimumValue() const
{
    return m_minimumValue;
}

void RangeFilter::setMinimumValue(QVariant minimumValue)
{
    if (m_minimumValue == minimumValue)
        return;

    m_minimumValue = minimumValue;
    Q_EMIT minimumValueChanged();
    invalidate();
}

/*!
    \qmlproperty int RangeFilter::minimumInclusive

    This property holds whether the \l minimumValue is inclusive.

    By default, the \l minimumValue is inclusive.

    \sa minimumValue
*/
bool RangeFilter::minimumInclusive() const
{
    return m_minimumInclusive;
}

void RangeFilter::setMinimumInclusive(bool minimumInclusive)
{
    if (m_minimumInclusive == minimumInclusive)
        return;

    m_minimumInclusive = minimumInclusive;
    Q_EMIT minimumInclusiveChanged();
    invalidate();
}

/*!
    \qmlproperty int RangeFilter::maximumValue

    This property holds the maximumValue of the filter.
    Rows with a value higher than \c maximumValue will be rejected.

    By default, no value is set.

    \sa maximumInclusive
*/
QVariant RangeFilter::maximumValue() const
{
    return m_maximumValue;
}

void RangeFilter::setMaximumValue(QVariant maximumValue)
{
    if (m_maximumValue == maximumValue)
        return;

    m_maximumValue = maximumValue;
    Q_EMIT maximumValueChanged();
    invalidate();
}

/*!
    \qmlproperty int RangeFilter::maximumInclusive

    This property holds whether the \l minimumValue is inclusive.

    By default, the \l minimumValue is inclusive.

    \sa minimumValue
*/
bool RangeFilter::maximumInclusive() const
{
    return m_maximumInclusive;
}

void RangeFilter::setMaximumInclusive(bool maximumInclusive)
{
    if (m_maximumInclusive == maximumInclusive)
        return;

    m_maximumInclusive = maximumInclusive;
    Q_EMIT maximumInclusiveChanged();
    invalidate();
}

bool RangeFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
    QVariant value = sourceData(sourceIndex, proxyModel);
    bool lessThanMin = m_minimumValue.isValid() &&
            (m_minimumInclusive ? value < m_minimumValue : value <= m_minimumValue);
    bool moreThanMax = m_maximumValue.isValid() &&
            (m_maximumInclusive ? value > m_maximumValue : value >= m_maximumValue);
    return !(lessThanMin || moreThanMax);
}

}
<|endoftext|>