<commit_before>/*
 * W.J. van der Laan 2011-2012
 * The SLIMCoin Developers 2013
 */
#include "bitcoingui.h"
#include "clientmodel.h"
#include "walletmodel.h"
#include "optionsmodel.h"
#include "guiutil.h"

#include "init.h"
#include "ui_interface.h"
#include "qtipcserver.h"

#include <QApplication>
#include <QMessageBox>
#if QT_VERSION < 0x050000
    #include <QTextCodec>
#endif
#include <QLocale>
#include <QTranslator>
#include <QSplashScreen>
#include <QLibraryInfo>

#include <boost/interprocess/ipc/message_queue.hpp>

#if defined(BITCOIN_NEED_QT_PLUGINS) && !defined(_BITCOIN_QT_PLUGINS_INCLUDED)
#define _BITCOIN_QT_PLUGINS_INCLUDED
#define __INSURE__
#include <QtPlugin>
Q_IMPORT_PLUGIN(qcncodecs)
Q_IMPORT_PLUGIN(qjpcodecs)
Q_IMPORT_PLUGIN(qtwcodecs)
Q_IMPORT_PLUGIN(qkrcodecs)
Q_IMPORT_PLUGIN(qtaccessiblewidgets)
#endif

// Need a global reference for the notifications to find the GUI
static BitcoinGUI *guiref;
static QSplashScreen *splashref;
static WalletModel *walletmodel;
static ClientModel *clientmodel;

int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style)
{
  // Message from network thread
  if(guiref)
  {
    bool modal = (style & wxMODAL);
    // in case of modal message, use blocking connection to wait for user to click OK
    QMetaObject::invokeMethod(guiref, "error",
                              modal ? GUIUtil::blockingGUIThreadConnection() : Qt::QueuedConnection,
                              Q_ARG(QString, QString::fromStdString(caption)),
                              Q_ARG(QString, QString::fromStdString(message)),
                              Q_ARG(bool, modal));
  }
  else
  {
    printf("%s: %s\n", caption.c_str(), message.c_str());
    fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
  }
  return 4;
}

bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption)
{
  if(!guiref)
    return false;
  if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
    return true;
  bool payFee = false;

  QMetaObject::invokeMethod(guiref, "askFee", GUIUtil::blockingGUIThreadConnection(),
                            Q_ARG(qint64, nFeeRequired),
                            Q_ARG(bool*, &payFee));

  return payFee;
}

void ThreadSafeHandleURI(const std::string& strURI)
{
  if(!guiref)
    return;

  QMetaObject::invokeMethod(guiref, "handleURI", GUIUtil::blockingGUIThreadConnection(),
                            Q_ARG(QString, QString::fromStdString(strURI)));
}

void MainFrameRepaint()
{
  if(clientmodel)
    QMetaObject::invokeMethod(clientmodel, "update", Qt::QueuedConnection);
  if(walletmodel)
    QMetaObject::invokeMethod(walletmodel, "update", Qt::QueuedConnection);
}

void AddressBookRepaint()
{
  if(walletmodel)
    QMetaObject::invokeMethod(walletmodel, "updateAddressList", Qt::QueuedConnection);
}

void InitMessage(const std::string &message)
{
  if(splashref)
  {
    //the addition of the newline is there to bump the text up so it fully fits in the coin's picture
    splashref->showMessage(QString::fromStdString(message + "\n"), Qt::AlignBottom | Qt::AlignHCenter, QColor(55,55,55));
    QApplication::instance()->processEvents();
  }
}

void QueueShutdown()
{
  QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
}

/*
  Translate string to current locale using Qt.
*/
std::string _(const char* psz)
{
  return QCoreApplication::translate("bitcoin-core", psz).toStdString();
}

/* Handle runaway exceptions. Shows a message box with the problem and quits the program.
 */
static void handleRunawayException(std::exception *e)
{
  PrintExceptionContinue(e, "Runaway exception");
  QMessageBox::critical(0, "Runaway exception", BitcoinGUI::tr("A fatal error occured. Slimcoin can no longer continue safely and will quit.") + QString("\n\n") + QString::fromStdString(strMiscWarning));
  exit(1);
}

#ifdef WIN32
#define strncasecmp strnicmp
#endif
#ifndef BITCOIN_QT_TEST
int main(int argc, char *argv[])
{
#if !defined(MAC_OSX) && !defined(WIN32)
// TODO: implement qtipcserver.cpp for Mac and Windows

  // Do this early as we don't want to bother initializing if we are just calling IPC
  for(int i = 1; i < argc; i++)
  {
    if(strlen(argv[i]) >= 8 && strncasecmp(argv[i], "slimcoin:", 9) == 0)
    {
      const char *strURI = argv[i];
      try {
        boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
        if(mq.try_send(strURI, strlen(strURI), 0))
          exit(0);
        else
          break;
      }
      catch (boost::interprocess::interprocess_exception &ex) {
        break;
      }
    }
  }
#endif

  Q_INIT_RESOURCE(bitcoin);
  QApplication app(argc, argv);

  // Command-line options take precedence:
  ParseParameters(argc, argv);

  // Basic Qt initialization (not dependent on parameters or configuration)
#if QT_VERSION < 0x050000
  // Internal string conversion is all UTF-8
  QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
  QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
#endif

#if QT_VERSION > 0x050100
    // Generate high-dpi pixmaps
    QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif
#if QT_VERSION >= 0x050600
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
#ifdef MAC_OSX
    QApplication::setAttribute(Qt::AA_DontShowIconsInMenus);
#endif
/* FIXME: refactor to Qt5.6+
#if QT_VERSION >= 0x050500
    // Because of the POODLE attack it is recommended to disable SSLv3 (https://disablessl3.com/),
    // so set SSL protocols to TLS1.0+.
    QSslConfiguration sslconf = QSslConfiguration::defaultConfiguration();
    sslconf.setProtocol(QSsl::TlsV1_0OrLater);
    QSslConfiguration::setDefaultConfiguration(sslconf);
#endif
*/

  // TODO: Do not refer to data directory yet, this can be overridden by Intro::pickDataDirectory

  // ... then bitcoin.conf:
  if(!boost::filesystem::is_directory(GetDataDir(false)))
  {
    fprintf(stderr, "Error: Specified directory does not exist\n");
    return 1;
  }
  ReadConfigFile(mapArgs, mapMultiArgs);

  // Application identification (must be set before OptionsModel is initialized,
  // as it is used to locate QSettings)
  app.setOrganizationName("Slimcoin");
  app.setOrganizationDomain("slimcoin.org");
  if(GetBoolArg("-testnet")) // Separate UI settings for testnet
    app.setApplicationName("Slimcoin-Qt-testnet");
  else
    app.setApplicationName("Slimcoin-Qt");

  // ... then GUI settings:
  OptionsModel optionsModel;

  // Get desired locale ("en_US") from command line or system locale
  QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
  // Load language files for configured locale:
  // - First load the translator for the base language, without territory
  // - Then load the more specific locale translator
  QString lang = lang_territory;

  lang.truncate(lang_territory.lastIndexOf('_')); // "en"
  QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;

  qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
  if(!qtTranslatorBase.isEmpty())
    app.installTranslator(&qtTranslatorBase);

  qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
  if(!qtTranslator.isEmpty())
    app.installTranslator(&qtTranslator);

  translatorBase.load(":/translations/"+lang);
  if(!translatorBase.isEmpty())
    app.installTranslator(&translatorBase);

  translator.load(":/translations/"+lang_territory);
  if(!translator.isEmpty())
    app.installTranslator(&translator);

  QPixmap testnet_splash_pixmap = QPixmap(":/images/splash_testnet");
  QSplashScreen splash(QPixmap(":/images/splash"), 0);
  if(GetBoolArg("-splash", true) && !GetBoolArg("-min"))
  {
    if(GetBoolArg("-testnet", false))
      splash.setPixmap(testnet_splash_pixmap);
    splash.show();
    splash.setAutoFillBackground(true);
    splashref = &splash;
  }

  app.processEvents();

  app.setQuitOnLastWindowClosed(false);

  try
  {
    BitcoinGUI window;
    guiref = &window;
    if(AppInit2(argc, argv))
    {
      {
        // Put this in a block, so that the Model objects are cleaned up before
        // calling Shutdown().

        optionsModel.Upgrade(); // Must be done after AppInit2

        if(splashref)
          splash.finish(&window);

        ClientModel clientModel(&optionsModel);
        clientmodel = &clientModel;
        WalletModel walletModel(pwalletMain, &optionsModel);
        walletmodel = &walletModel;

        window.setClientModel(&clientModel);
        window.setWalletModel(&walletModel);

        // If -min option passed, start window minimized.
        if(GetBoolArg("-min"))
                {
          window.showMinimized();
                }
        else
                {
          window.show();
                }

        // Place this here as guiref has to be defined if we dont want to lose URIs
        ipcInit();

#if !defined(MAC_OSX) && !defined(WIN32)
// TODO: implement qtipcserver.cpp for Mac and Windows

        // Check for URI in argv
        for(int i = 1; i < argc; i++)
        {
          if(strlen(argv[i]) > 8 && strncasecmp(argv[i], "slimcoin:", 9) == 0)
          {
            const char *strURI = argv[i];
            try {
              boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
              mq.try_send(strURI, strlen(strURI), 0);
            }
            catch (boost::interprocess::interprocess_exception &ex) {
            }
          }
        }
#endif
        app.exec();

        window.hide();
        window.setClientModel(0);
        window.setWalletModel(0);
        guiref = 0;
        clientmodel = 0;
        walletmodel = 0;
      }
      // Shutdown the core and it's threads, but don't exit Bitcoin-Qt here
      Shutdown(NULL);
    }
    else
    {
      return 1;
    }
  } catch (std::exception& e) {
    handleRunawayException(&e);
  } catch (...) {
    handleRunawayException(NULL);
  }
  return 0;
}
#endif // BITCOIN_QT_TEST
<commit_msg>show version info on splashscreen<commit_after>/*
 * W.J. van der Laan 2011-2012
 * The SLIMCoin Developers 2013
 */
#include "bitcoingui.h"
#include "clientmodel.h"
#include "walletmodel.h"
#include "optionsmodel.h"
#include "guiutil.h"
#include "util.h"

#include "init.h"
#include "ui_interface.h"
#include "qtipcserver.h"

#include <QApplication>
#include <QMessageBox>
#if QT_VERSION < 0x050000
    #include <QTextCodec>
#endif
#include <QLocale>
#include <QTranslator>
#include <QSplashScreen>
#include <QLibraryInfo>

#include <boost/interprocess/ipc/message_queue.hpp>

#if defined(BITCOIN_NEED_QT_PLUGINS) && !defined(_BITCOIN_QT_PLUGINS_INCLUDED)
#define _BITCOIN_QT_PLUGINS_INCLUDED
#define __INSURE__
#include <QtPlugin>
Q_IMPORT_PLUGIN(qcncodecs)
Q_IMPORT_PLUGIN(qjpcodecs)
Q_IMPORT_PLUGIN(qtwcodecs)
Q_IMPORT_PLUGIN(qkrcodecs)
Q_IMPORT_PLUGIN(qtaccessiblewidgets)
#endif

// Need a global reference for the notifications to find the GUI
static BitcoinGUI *guiref;
static QSplashScreen *splashref;
static WalletModel *walletmodel;
static ClientModel *clientmodel;

int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style)
{
  // Message from network thread
  if(guiref)
  {
    bool modal = (style & wxMODAL);
    // in case of modal message, use blocking connection to wait for user to click OK
    QMetaObject::invokeMethod(guiref, "error",
                              modal ? GUIUtil::blockingGUIThreadConnection() : Qt::QueuedConnection,
                              Q_ARG(QString, QString::fromStdString(caption)),
                              Q_ARG(QString, QString::fromStdString(message)),
                              Q_ARG(bool, modal));
  }
  else
  {
    printf("%s: %s\n", caption.c_str(), message.c_str());
    fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
  }
  return 4;
}

bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption)
{
  if(!guiref)
    return false;
  if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
    return true;
  bool payFee = false;

  QMetaObject::invokeMethod(guiref, "askFee", GUIUtil::blockingGUIThreadConnection(),
                            Q_ARG(qint64, nFeeRequired),
                            Q_ARG(bool*, &payFee));

  return payFee;
}

void ThreadSafeHandleURI(const std::string& strURI)
{
  if(!guiref)
    return;

  QMetaObject::invokeMethod(guiref, "handleURI", GUIUtil::blockingGUIThreadConnection(),
                            Q_ARG(QString, QString::fromStdString(strURI)));
}

void MainFrameRepaint()
{
  if(clientmodel)
    QMetaObject::invokeMethod(clientmodel, "update", Qt::QueuedConnection);
  if(walletmodel)
    QMetaObject::invokeMethod(walletmodel, "update", Qt::QueuedConnection);
}

void AddressBookRepaint()
{
  if(walletmodel)
    QMetaObject::invokeMethod(walletmodel, "updateAddressList", Qt::QueuedConnection);
}

void InitMessage(const std::string &message)
{
  if(splashref)
  {
    //the addition of the newline is there to bump the text up so it fully fits in the coin's picture
    // splashref->showMessage(QString::fromStdString(message + "\n"), Qt::AlignBottom | Qt::AlignHCenter, QColor(55,55,55));
    splashref->showMessage(QString::fromStdString(message+"\n\n") + QString::fromStdString(FormatFullVersion().c_str()), Qt::AlignBottom|Qt::AlignHCenter, QColor(55,55,55));
    QApplication::instance()->processEvents();

  }
}

void QueueShutdown()
{
  QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
}

/*
  Translate string to current locale using Qt.
*/
std::string _(const char* psz)
{
  return QCoreApplication::translate("bitcoin-core", psz).toStdString();
}

/* Handle runaway exceptions. Shows a message box with the problem and quits the program.
 */
static void handleRunawayException(std::exception *e)
{
  PrintExceptionContinue(e, "Runaway exception");
  QMessageBox::critical(0, "Runaway exception", BitcoinGUI::tr("A fatal error occured. Slimcoin can no longer continue safely and will quit.") + QString("\n\n") + QString::fromStdString(strMiscWarning));
  exit(1);
}

#ifdef WIN32
#define strncasecmp strnicmp
#endif
#ifndef BITCOIN_QT_TEST
int main(int argc, char *argv[])
{
#if !defined(MAC_OSX) && !defined(WIN32)
// TODO: implement qtipcserver.cpp for Mac and Windows

  // Do this early as we don't want to bother initializing if we are just calling IPC
  for(int i = 1; i < argc; i++)
  {
    if(strlen(argv[i]) >= 8 && strncasecmp(argv[i], "slimcoin:", 9) == 0)
    {
      const char *strURI = argv[i];
      try {
        boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
        if(mq.try_send(strURI, strlen(strURI), 0))
          exit(0);
        else
          break;
      }
      catch (boost::interprocess::interprocess_exception &ex) {
        break;
      }
    }
  }
#endif

  Q_INIT_RESOURCE(bitcoin);
  QApplication app(argc, argv);

  // Command-line options take precedence:
  ParseParameters(argc, argv);

  // Basic Qt initialization (not dependent on parameters or configuration)
#if QT_VERSION < 0x050000
  // Internal string conversion is all UTF-8
  QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
  QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
#endif

#if QT_VERSION > 0x050100
    // Generate high-dpi pixmaps
    QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif
#if QT_VERSION >= 0x050600
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
#ifdef MAC_OSX
    QApplication::setAttribute(Qt::AA_DontShowIconsInMenus);
#endif
/* FIXME: refactor to Qt5.6+
#if QT_VERSION >= 0x050500
    // Because of the POODLE attack it is recommended to disable SSLv3 (https://disablessl3.com/),
    // so set SSL protocols to TLS1.0+.
    QSslConfiguration sslconf = QSslConfiguration::defaultConfiguration();
    sslconf.setProtocol(QSsl::TlsV1_0OrLater);
    QSslConfiguration::setDefaultConfiguration(sslconf);
#endif
*/

  // TODO: Do not refer to data directory yet, this can be overridden by Intro::pickDataDirectory

  // ... then bitcoin.conf:
  if(!boost::filesystem::is_directory(GetDataDir(false)))
  {
    fprintf(stderr, "Error: Specified directory does not exist\n");
    return 1;
  }
  ReadConfigFile(mapArgs, mapMultiArgs);

  // Application identification (must be set before OptionsModel is initialized,
  // as it is used to locate QSettings)
  app.setOrganizationName("Slimcoin");
  app.setOrganizationDomain("slimcoin.org");
  if(GetBoolArg("-testnet")) // Separate UI settings for testnet
    app.setApplicationName("Slimcoin-Qt-testnet");
  else
    app.setApplicationName("Slimcoin-Qt");

  // ... then GUI settings:
  OptionsModel optionsModel;

  // Get desired locale ("en_US") from command line or system locale
  QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
  // Load language files for configured locale:
  // - First load the translator for the base language, without territory
  // - Then load the more specific locale translator
  QString lang = lang_territory;

  lang.truncate(lang_territory.lastIndexOf('_')); // "en"
  QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;

  qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
  if(!qtTranslatorBase.isEmpty())
    app.installTranslator(&qtTranslatorBase);

  qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
  if(!qtTranslator.isEmpty())
    app.installTranslator(&qtTranslator);

  translatorBase.load(":/translations/"+lang);
  if(!translatorBase.isEmpty())
    app.installTranslator(&translatorBase);

  translator.load(":/translations/"+lang_territory);
  if(!translator.isEmpty())
    app.installTranslator(&translator);

  QPixmap testnet_splash_pixmap = QPixmap(":/images/splash_testnet");
  QSplashScreen splash(QPixmap(":/images/splash"), 0);
  if(GetBoolArg("-splash", true) && !GetBoolArg("-min"))
  {
    if(GetBoolArg("-testnet", false))
      splash.setPixmap(testnet_splash_pixmap);
    splash.show();
    splash.setAutoFillBackground(true);
    splashref = &splash;
  }

  app.processEvents();

  app.setQuitOnLastWindowClosed(false);

  try
  {
    BitcoinGUI window;
    guiref = &window;
    if(AppInit2(argc, argv))
    {
      {
        // Put this in a block, so that the Model objects are cleaned up before
        // calling Shutdown().

        optionsModel.Upgrade(); // Must be done after AppInit2

        if(splashref)
          splash.finish(&window);

        ClientModel clientModel(&optionsModel);
        clientmodel = &clientModel;
        WalletModel walletModel(pwalletMain, &optionsModel);
        walletmodel = &walletModel;

        window.setClientModel(&clientModel);
        window.setWalletModel(&walletModel);

        // If -min option passed, start window minimized.
        if(GetBoolArg("-min"))
                {
          window.showMinimized();
                }
        else
                {
          window.show();
                }

        // Place this here as guiref has to be defined if we dont want to lose URIs
        ipcInit();

#if !defined(MAC_OSX) && !defined(WIN32)
// TODO: implement qtipcserver.cpp for Mac and Windows

        // Check for URI in argv
        for(int i = 1; i < argc; i++)
        {
          if(strlen(argv[i]) > 8 && strncasecmp(argv[i], "slimcoin:", 9) == 0)
          {
            const char *strURI = argv[i];
            try {
              boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
              mq.try_send(strURI, strlen(strURI), 0);
            }
            catch (boost::interprocess::interprocess_exception &ex) {
            }
          }
        }
#endif
        app.exec();

        window.hide();
        window.setClientModel(0);
        window.setWalletModel(0);
        guiref = 0;
        clientmodel = 0;
        walletmodel = 0;
      }
      // Shutdown the core and it's threads, but don't exit Bitcoin-Qt here
      Shutdown(NULL);
    }
    else
    {
      return 1;
    }
  } catch (std::exception& e) {
    handleRunawayException(&e);
  } catch (...) {
    handleRunawayException(NULL);
  }
  return 0;
}
#endif // BITCOIN_QT_TEST
<|endoftext|>


<commit_before>//=======================================================================
// Copyright (c) 2014-2017 Baptiste Wicht
// Distributed under the terms of the MIT License.
// (See accompanying file LICENSE or copy at
//  http://opensource.org/licenses/MIT)
//=======================================================================

#include "dll/neural/dense_layer.hpp"
#include "dll/test.hpp"
#include "dll/network.hpp"

#include "mnist/mnist_reader.hpp"
#include "mnist/mnist_utils.hpp"

int main(int /*argc*/, char* /*argv*/ []) {
    // Load the dataset
    auto dataset = mnist::read_dataset_direct<std::vector, etl::fast_dyn_matrix<float, 28 * 28>>();

    // Build the network

    using network_t = dll::dyn_network_desc<
        dll::network_layers<
            dll::dense_desc<28 * 28, 32, dll::relu>::layer_t,
            dll::dense_desc<32, 28 * 28, dll::sigmoid>::layer_t
        >
        , dll::batch_size<256>       // The mini-batch size
        , dll::shuffle               // Shuffle the dataset before each epoch
        , dll::binary_cross_entropy  // Use a Binary Cross Entropy Loss
        , dll::scale_pre<255>        // Scale the images (divide by 255)
        , dll::autoencoder           // Indicate auto-encoder
        , dll::adadelta              // Adadelta updates for gradient descent
    >::network_t;

    auto net = std::make_unique<network_t>();

    // Display the network
    net->display();

    // Train the network as auto-encoder
    net->fine_tune_ae(dataset.training_images, 50);

    // Test the network on test set
    net->evaluate_ae(dataset.test_images);

    return 0;
}
<commit_msg>Use the new utilities<commit_after>//=======================================================================
// Copyright (c) 2014-2017 Baptiste Wicht
// Distributed under the terms of the MIT License.
// (See accompanying file LICENSE or copy at
//  http://opensource.org/licenses/MIT)
//=======================================================================

#include "dll/neural/dense_layer.hpp"
#include "dll/test.hpp"
#include "dll/network.hpp"
#include "dll/datasets.hpp"

#include "mnist/mnist_reader.hpp"
#include "mnist/mnist_utils.hpp"

int main(int /*argc*/, char* /*argv*/ []) {
    // Load the dataset
    auto dataset = dll::make_mnist_ae_dataset(0, dll::batch_size<256>{}, dll::scale_pre<255>{});

    // Build the network

    using network_t = dll::dyn_network_desc<
        dll::network_layers<
            dll::dense_desc<28 * 28, 32, dll::relu>::layer_t,
            dll::dense_desc<32, 28 * 28, dll::sigmoid>::layer_t
        >
        , dll::batch_size<256>       // The mini-batch size
        , dll::shuffle               // Shuffle the dataset before each epoch
        , dll::binary_cross_entropy  // Use a Binary Cross Entropy Loss
        , dll::adadelta              // Adadelta updates for gradient descent
    >::network_t;

    auto net = std::make_unique<network_t>();

    // Display the network
    net->display();

    // Train the network as auto-encoder
    net->fine_tune_ae(dataset.train(), 50);

    // Test the network on test set
    net->evaluate_ae(dataset.test());

    return 0;
}
<|endoftext|>


<commit_before>///////////////////////////////////////////////////////////////////////////////
//
// For more information, please see: http://www.nektar.info
//
// The MIT License
//
// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
// Department of Aeronautics, Imperial College London (UK), and Scientific
// Computing and Imaging Institute, University of Utah (USA).
//
// License for the specific language governing rights and limitations under
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef NEKTAR_USE_EXPRESSION_TEMPLATES
#define NEKTAR_USE_EXPRESSION_TEMPLATES
#endif //NEKTAR_USE_EXPRESSION_TEMPLATES

#include <boost/test/auto_unit_test.hpp>
#include <boost/test/test_case_template.hpp>
#include <boost/test/floating_point_comparison.hpp>
#include <boost/test/unit_test.hpp>

#include <LibUtilities/LinearAlgebra/NekTypeDefs.hpp>
#include <UnitTests/LibUtilities/ExpressionTemplates/CountedObjectExpression.h>
#include <LibUtilities/LinearAlgebra/NekMatrix.hpp>
#include <ExpressionTemplates/Node.hpp>
#include <ExpressionTemplates/RemoveAllUnecessaryTemporaries.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/type_traits.hpp>
#include <LibUtilities/LinearAlgebra/MatrixSize.hpp>

// Tests the error in CoupledLinearNS.cpp:963
// DNekScalBlkMatSharedPtr  m_Cinv;
// NekVector< NekDouble > F_int
// DNekScalBlkMatSharedPtr  m_D_int;
// NekVector<  NekDouble > F_p
// DNekScalBlkMatSharedPtr m_Btilde;
// NekVector< NekDouble > F_bnd
// F_int = (*m_Cinv)*(F_int + Transpose(*m_D_int)*F_p - Transpose(*m_Btilde)*F_bnd);

namespace Nektar
{
    namespace UnitTests
    {
        // Start my testing individual portions to help isolate the problem.
        BOOST_AUTO_TEST_CASE(TestScalBlkMatTimeVector)
        {
            // 2x2 block matrix, with submatrices also 2x2
            double data_buf[] = {1, 2, 3, 4};
            DNekMatSharedPtr m0(new DNekMat(2,2, data_buf));
            DNekMatSharedPtr m1(new DNekMat(2,2, data_buf));
            DNekMatSharedPtr m2(new DNekMat(2,2, data_buf));
            DNekMatSharedPtr m3(new DNekMat(2,2, data_buf));

            DNekScalMatSharedPtr s0(new DNekScalMat(2.0, m0));
            DNekScalMatSharedPtr s1(new DNekScalMat(3.0, m1));
            DNekScalMatSharedPtr s2(new DNekScalMat(4.0, m2));
            DNekScalMatSharedPtr s3(new DNekScalMat(5.0, m3));

            DNekScalBlkMatSharedPtr m(new DNekScalBlkMat(2,2, 2, 2));
            m->SetBlock(0, 0, s0);
            m->SetBlock(0, 1, s1);
            m->SetBlock(1, 0, s2);
            m->SetBlock(1, 1, s3);

            double v_buf[] = {1, 2, 3, 4, 5, 6, 7, 8};
            NekVector<NekDouble> v(8, v_buf);

            NekVector<NekDouble> result = (*m)*v;
        }

        // The multiplication compiles, so I assume the problem is in a tree transformation.
        BOOST_AUTO_TEST_CASE(TestScaleBlkMatTermWithTransformation)
        {
            double data_buf[] = {1, 2, 3, 4};
            DNekMatSharedPtr m0(new DNekMat(2,2, data_buf));
            DNekMatSharedPtr m1(new DNekMat(2,2, data_buf));
            DNekMatSharedPtr m2(new DNekMat(2,2, data_buf));
            DNekMatSharedPtr m3(new DNekMat(2,2, data_buf));

            DNekScalMatSharedPtr s0(new DNekScalMat(2.0, m0));
            DNekScalMatSharedPtr s1(new DNekScalMat(3.0, m1));
            DNekScalMatSharedPtr s2(new DNekScalMat(4.0, m2));
            DNekScalMatSharedPtr s3(new DNekScalMat(5.0, m3));

            DNekScalBlkMatSharedPtr m(new DNekScalBlkMat(2,2, 2, 2));
            m->SetBlock(0, 0, s0);
            m->SetBlock(0, 1, s1);
            m->SetBlock(1, 0, s2);
            m->SetBlock(1, 1, s3);

            double rhs_buf[] = {1, 2, 3, 4, 5, 6, 7, 8};
            NekVector<NekDouble> rhs(8, rhs_buf);

            double lhs_buf[] = {1, 2, 3, 4, 5, 6, 7, 8};
            NekVector<NekDouble> lhs(8, lhs_buf);

            NekVector<NekDouble> result = lhs + (*m)*rhs;

            DNekMat result2 = (*m)*((*m)*(*m));

            // This fails.  It appears that it is a failure of the commutative property
            // for matrix/vector multiplication.
            NekVector<NekDouble> result3 = (*m)*(lhs+rhs);

            NekVector<NekDouble> result1 = (*m)*(lhs + (*m)*rhs - (*m)*rhs);
        }

    }
}
<commit_msg><commit_after>///////////////////////////////////////////////////////////////////////////////
//
// For more information, please see: http://www.nektar.info
//
// The MIT License
//
// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
// Department of Aeronautics, Imperial College London (UK), and Scientific
// Computing and Imaging Institute, University of Utah (USA).
//
// License for the specific language governing rights and limitations under
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef NEKTAR_USE_EXPRESSION_TEMPLATES
#define NEKTAR_USE_EXPRESSION_TEMPLATES
#endif //NEKTAR_USE_EXPRESSION_TEMPLATES

#include <boost/test/auto_unit_test.hpp>
#include <boost/test/test_case_template.hpp>
#include <boost/test/floating_point_comparison.hpp>
#include <boost/test/unit_test.hpp>

#include <LibUtilities/LinearAlgebra/NekTypeDefs.hpp>
#include <UnitTests/LibUtilities/ExpressionTemplates/CountedObjectExpression.h>
#include <LibUtilities/LinearAlgebra/NekMatrix.hpp>
#include <ExpressionTemplates/Node.hpp>
#include <ExpressionTemplates/RemoveAllUnecessaryTemporaries.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/type_traits.hpp>
#include <LibUtilities/LinearAlgebra/MatrixSize.hpp>

// Tests the error in CoupledLinearNS.cpp:963
// DNekScalBlkMatSharedPtr  m_Cinv;
// NekVector< NekDouble > F_int
// DNekScalBlkMatSharedPtr  m_D_int;
// NekVector<  NekDouble > F_p
// DNekScalBlkMatSharedPtr m_Btilde;
// NekVector< NekDouble > F_bnd
// F_int = (*m_Cinv)*(F_int + Transpose(*m_D_int)*F_p - Transpose(*m_Btilde)*F_bnd);

namespace Nektar
{
    namespace UnitTests
    {
        // Start my testing individual portions to help isolate the problem.
        BOOST_AUTO_TEST_CASE(TestScalBlkMatTimeVector)
        {
            // 2x2 block matrix, with submatrices also 2x2
            double data_buf[] = {1, 2, 3, 4};
            DNekMatSharedPtr m0(new DNekMat(2,2, data_buf));
            DNekMatSharedPtr m1(new DNekMat(2,2, data_buf));
            DNekMatSharedPtr m2(new DNekMat(2,2, data_buf));
            DNekMatSharedPtr m3(new DNekMat(2,2, data_buf));

            DNekScalMatSharedPtr s0(new DNekScalMat(2.0, m0));
            DNekScalMatSharedPtr s1(new DNekScalMat(3.0, m1));
            DNekScalMatSharedPtr s2(new DNekScalMat(4.0, m2));
            DNekScalMatSharedPtr s3(new DNekScalMat(5.0, m3));

            DNekScalBlkMatSharedPtr m(new DNekScalBlkMat(2,2, 2, 2));
            m->SetBlock(0, 0, s0);
            m->SetBlock(0, 1, s1);
            m->SetBlock(1, 0, s2);
            m->SetBlock(1, 1, s3);

            double v_buf[] = {1, 2, 3, 4, 5, 6, 7, 8};
            NekVector<NekDouble> v(8, v_buf);

            NekVector<NekDouble> result = (*m)*v;
        }

        // The multiplication compiles, so I assume the problem is in a tree transformation.
        BOOST_AUTO_TEST_CASE(TestScaleBlkMatTermWithTransformation)
        {
            double data_buf[] = {1, 2, 3, 4};
            DNekMatSharedPtr m0(new DNekMat(2,2, data_buf));
            DNekMatSharedPtr m1(new DNekMat(2,2, data_buf));
            DNekMatSharedPtr m2(new DNekMat(2,2, data_buf));
            DNekMatSharedPtr m3(new DNekMat(2,2, data_buf));

            DNekScalMatSharedPtr s0(new DNekScalMat(2.0, m0));
            DNekScalMatSharedPtr s1(new DNekScalMat(3.0, m1));
            DNekScalMatSharedPtr s2(new DNekScalMat(4.0, m2));
            DNekScalMatSharedPtr s3(new DNekScalMat(5.0, m3));

            DNekScalBlkMatSharedPtr m(new DNekScalBlkMat(2,2, 2, 2));
            m->SetBlock(0, 0, s0);
            m->SetBlock(0, 1, s1);
            m->SetBlock(1, 0, s2);
            m->SetBlock(1, 1, s3);

            double rhs_buf[] = {1, 2, 3, 4};
            NekVector<NekDouble> rhs(4, rhs_buf);

            double lhs_buf[] = {1, 2, 3, 4};
            NekVector<NekDouble> lhs(4, lhs_buf);

            NekVector<NekDouble> result = lhs + (*m)*rhs;

            DNekMat result2 = (*m)*((*m)*(*m));

            // This fails.  It appears that it is a failure of the commutative property
            // for matrix/vector multiplication.
            NekVector<NekDouble> result3 = (*m)*(lhs+rhs);

            NekVector<NekDouble> result1 = (*m)*(lhs + (*m)*rhs - (*m)*rhs);
        }

    }
}
<|endoftext|>


<commit_before>#include <sys/resource.h>
#include <sys/time.h>

#include <fstream>
#include <iostream>
#include <memory>
#include <string>

#include <boost/program_options.hpp>

#include "HyperscanEngine.h"
#include "PcapSource.h"
#include "PCRE2Engine.h"
#include "regexbench.h"
#include "RE2Engine.h"
#include "REmatchEngine.h"
#include "Rule.h"

namespace po = boost::program_options;

enum EngineType : uint64_t {
  ENGINE_HYPERSCAN,
  ENGINE_PCRE2,
  ENGINE_PCRE2JIT,
  ENGINE_RE2,
  ENGINE_REMATCH
};

struct Arguments {
  std::string rule_file;
  std::string pcap_file;
  EngineType engine;
  int32_t repeat;
  uint32_t pcre2_concat;
};

static bool endsWith(const std::string &, const char *);
static std::vector<regexbench::Rule> loadRules(const std::string &);
static Arguments parse_options(int argc, const char *argv[]);

int main(int argc, const char *argv[]) {
  try {
    auto args = parse_options(argc, argv);
    std::unique_ptr<regexbench::Engine> engine;
    switch (args.engine) {
    case ENGINE_HYPERSCAN:
      engine = std::make_unique<regexbench::HyperscanEngine>();
      engine->compile(loadRules(args.rule_file));
      break;
    case ENGINE_PCRE2:
      engine = std::make_unique<regexbench::PCRE2Engine>();
      if (!args.pcre2_concat)
        engine->compile(loadRules(args.rule_file));
      else {
        auto rules = loadRules(args.rule_file);
        concatRules(rules);
        engine->compile(rules);
      }
      break;
    case ENGINE_PCRE2JIT:
      engine = std::make_unique<regexbench::PCRE2JITEngine>();
      engine->compile(loadRules(args.rule_file));
      break;
    case ENGINE_RE2:
      engine = std::make_unique<regexbench::RE2Engine>();
      engine->compile(loadRules(args.rule_file));
      break;
    case ENGINE_REMATCH:
      if (endsWith(args.rule_file, ".nfa")) {
        engine = std::make_unique<regexbench::REmatchAutomataEngine>();
        engine->load(args.rule_file);
      } else if (endsWith(args.rule_file, ".so")) {
        engine = std::make_unique<regexbench::REmatchSOEngine>();
        engine->load(args.rule_file);
      } else {
        engine = std::make_unique<regexbench::REmatchAutomataEngine>();
        engine->compile(loadRules(args.rule_file));
      }
      break;
    }

    regexbench::PcapSource pcap(args.pcap_file);
    auto result = match(*engine, pcap, args.repeat);
    std::cout << result.nmatches << " packets matched." << std::endl;
    std::cout << result.udiff.tv_sec << '.';
    std::cout.width(6);
    std::cout.fill('0');
    std::cout << result.udiff.tv_usec << "s user " << std::endl;
    std::cout << result.sdiff.tv_sec << '.';
    std::cout.width(6);
    std::cout.fill('0');
    std::cout << result.sdiff.tv_usec << "s system" << std::endl;
    struct timeval total;
    timeradd(&result.udiff, &result.sdiff, &total);
    std::cout
      << static_cast<double>(pcap.getNumberOfBytes() *
                             static_cast<unsigned long>(args.repeat)) /
        (total.tv_sec + total.tv_usec * 1e-6) / 1000000 * 8
        << " Mbps" << std::endl;
    std::cout
        << static_cast<double>(pcap.getNumberOfPackets() *
                               static_cast<unsigned long>(args.repeat)) /
        (total.tv_sec + total.tv_usec * 1e-6) / 1000000
        << " Mpps" << std::endl;
  } catch (const std::exception &e) {
    std::cerr << e.what() << std::endl;
    return EXIT_FAILURE;
  }

  struct rusage stat;
  getrusage(RUSAGE_SELF, &stat);
  std::cout << stat.ru_maxrss / 1000 << " kB\n";
  return EXIT_SUCCESS;
}

bool endsWith(const std::string &obj, const char *end) {
  auto r = obj.rfind(end);
  if ((r != std::string::npos) && (r == obj.size() - std::strlen(end)))
    return true;
  return false;
}

static std::vector<regexbench::Rule> loadRules(const std::string &filename) {
  std::ifstream ruleifs(filename);
  if (!ruleifs) {
    std::cerr << "cannot open rule file: " << filename << std::endl;
    std::exit(EXIT_FAILURE);
  }
  return regexbench::loadRules(ruleifs);
}

Arguments parse_options(int argc, const char *argv[]) {
  Arguments args;
  std::string engine;

  po::options_description posargs;
  posargs.add_options()("rule_file",
                        po::value<std::string>(&args.rule_file),
                        "Rule (regular expression) file name");
  posargs.add_options()("pcap_file",
                        po::value<std::string>(&args.pcap_file),
                        "pcap file name");
  po::positional_options_description positions;
  positions.add("rule_file", 1).add("pcap_file", 1);

  po::options_description optargs("Options");
  optargs.add_options()("help,h", "Print usage information.");
  optargs.add_options()(
    "engine,e",
    po::value<std::string>(&engine)->default_value("hyperscan"),
    "Matching engine to run.");
  optargs.add_options()(
    "repeat,r",
    po::value<int32_t>(&args.repeat)->default_value(1),
    "Repeat pcap multiple times.");
  optargs.add_options()(
    "concat,c",
    po::value<uint32_t>(&args.pcre2_concat)->default_value(0),
    "Concatenate PCRE2 rules.");

  po::options_description cliargs;
  cliargs.add(posargs).add(optargs);
  po::variables_map vm;
  po::store(po::command_line_parser(argc, argv)
            .options(cliargs)
            .positional(positions)
            .run(),
            vm);
  po::notify(vm);

  if (vm.count("help")) {
    std::cout << "Usage: regexbench <rule_file> <pcap_file>" << std::endl;
    std::exit(EXIT_SUCCESS);
  }
  if (engine == "hyperscan")
    args.engine = ENGINE_HYPERSCAN;
  else if (engine == "pcre2")
    args.engine = ENGINE_PCRE2;
  else if (engine == "pcre2jit")
    args.engine = ENGINE_PCRE2JIT;
  else if (engine == "re2")
    args.engine = ENGINE_RE2;
  else if (engine == "rematch")
    args.engine = ENGINE_REMATCH;
  else {
    std::cerr << "unknown engine: " << engine << std::endl;
    std::exit(EXIT_FAILURE);
  }
  if (args.repeat <= 0) {
    std::cerr << "invalid repeat value: " << args.repeat << std::endl;
    std::exit(EXIT_FAILURE);
  }

  if (!vm.count("rule_file")) {
    std::cerr << "error: no rule file" << std::endl;
    std::exit(EXIT_FAILURE);
  }
  if (!vm.count("pcap_file")) {
    std::cerr << "error: no pcap file" << std::endl;
    std::exit(EXIT_FAILURE);
  }
  return args;
}
<commit_msg>Enable rule concatenation for pcre2JIT engine<commit_after>#include <sys/resource.h>
#include <sys/time.h>

#include <fstream>
#include <iostream>
#include <memory>
#include <string>

#include <boost/program_options.hpp>

#include "HyperscanEngine.h"
#include "PcapSource.h"
#include "PCRE2Engine.h"
#include "regexbench.h"
#include "RE2Engine.h"
#include "REmatchEngine.h"
#include "Rule.h"

namespace po = boost::program_options;

enum EngineType : uint64_t {
  ENGINE_HYPERSCAN,
  ENGINE_PCRE2,
  ENGINE_PCRE2JIT,
  ENGINE_RE2,
  ENGINE_REMATCH
};

struct Arguments {
  std::string rule_file;
  std::string pcap_file;
  EngineType engine;
  int32_t repeat;
  uint32_t pcre2_concat;
};

static bool endsWith(const std::string &, const char *);
static std::vector<regexbench::Rule> loadRules(const std::string &);
static Arguments parse_options(int argc, const char *argv[]);
static void compilePCRE2(const Arguments &, std::unique_ptr<regexbench::Engine> &);

int main(int argc, const char *argv[]) {
  try {
    auto args = parse_options(argc, argv);
    std::unique_ptr<regexbench::Engine> engine;
    switch (args.engine) {
    case ENGINE_HYPERSCAN:
      engine = std::make_unique<regexbench::HyperscanEngine>();
      engine->compile(loadRules(args.rule_file));
      break;
    case ENGINE_PCRE2:
      engine = std::make_unique<regexbench::PCRE2Engine>();
      compilePCRE2(args, engine);
      break;
    case ENGINE_PCRE2JIT:
      engine = std::make_unique<regexbench::PCRE2JITEngine>();
      compilePCRE2(args, engine);
      break;
    case ENGINE_RE2:
      engine = std::make_unique<regexbench::RE2Engine>();
      engine->compile(loadRules(args.rule_file));
      break;
    case ENGINE_REMATCH:
      if (endsWith(args.rule_file, ".nfa")) {
        engine = std::make_unique<regexbench::REmatchAutomataEngine>();
        engine->load(args.rule_file);
      } else if (endsWith(args.rule_file, ".so")) {
        engine = std::make_unique<regexbench::REmatchSOEngine>();
        engine->load(args.rule_file);
      } else {
        engine = std::make_unique<regexbench::REmatchAutomataEngine>();
        engine->compile(loadRules(args.rule_file));
      }
      break;
    }

    regexbench::PcapSource pcap(args.pcap_file);
    auto result = match(*engine, pcap, args.repeat);
    std::cout << result.nmatches << " packets matched." << std::endl;
    std::cout << result.udiff.tv_sec << '.';
    std::cout.width(6);
    std::cout.fill('0');
    std::cout << result.udiff.tv_usec << "s user " << std::endl;
    std::cout << result.sdiff.tv_sec << '.';
    std::cout.width(6);
    std::cout.fill('0');
    std::cout << result.sdiff.tv_usec << "s system" << std::endl;
    struct timeval total;
    timeradd(&result.udiff, &result.sdiff, &total);
    std::cout
      << static_cast<double>(pcap.getNumberOfBytes() *
                             static_cast<unsigned long>(args.repeat)) /
        (total.tv_sec + total.tv_usec * 1e-6) / 1000000 * 8
        << " Mbps" << std::endl;
    std::cout
        << static_cast<double>(pcap.getNumberOfPackets() *
                               static_cast<unsigned long>(args.repeat)) /
        (total.tv_sec + total.tv_usec * 1e-6) / 1000000
        << " Mpps" << std::endl;
  } catch (const std::exception &e) {
    std::cerr << e.what() << std::endl;
    return EXIT_FAILURE;
  }

  struct rusage stat;
  getrusage(RUSAGE_SELF, &stat);
  std::cout << stat.ru_maxrss / 1000 << " kB\n";
  return EXIT_SUCCESS;
}

bool endsWith(const std::string &obj, const char *end) {
  auto r = obj.rfind(end);
  if ((r != std::string::npos) && (r == obj.size() - std::strlen(end)))
    return true;
  return false;
}

static std::vector<regexbench::Rule> loadRules(const std::string &filename) {
  std::ifstream ruleifs(filename);
  if (!ruleifs) {
    std::cerr << "cannot open rule file: " << filename << std::endl;
    std::exit(EXIT_FAILURE);
  }
  return regexbench::loadRules(ruleifs);
}

Arguments parse_options(int argc, const char *argv[]) {
  Arguments args;
  std::string engine;

  po::options_description posargs;
  posargs.add_options()("rule_file",
                        po::value<std::string>(&args.rule_file),
                        "Rule (regular expression) file name");
  posargs.add_options()("pcap_file",
                        po::value<std::string>(&args.pcap_file),
                        "pcap file name");
  po::positional_options_description positions;
  positions.add("rule_file", 1).add("pcap_file", 1);

  po::options_description optargs("Options");
  optargs.add_options()("help,h", "Print usage information.");
  optargs.add_options()(
    "engine,e",
    po::value<std::string>(&engine)->default_value("hyperscan"),
    "Matching engine to run.");
  optargs.add_options()(
    "repeat,r",
    po::value<int32_t>(&args.repeat)->default_value(1),
    "Repeat pcap multiple times.");
  optargs.add_options()(
    "concat,c",
    po::value<uint32_t>(&args.pcre2_concat)->default_value(0),
    "Concatenate PCRE2 rules.");

  po::options_description cliargs;
  cliargs.add(posargs).add(optargs);
  po::variables_map vm;
  po::store(po::command_line_parser(argc, argv)
            .options(cliargs)
            .positional(positions)
            .run(),
            vm);
  po::notify(vm);

  if (vm.count("help")) {
    std::cout << "Usage: regexbench <rule_file> <pcap_file>" << std::endl;
    std::exit(EXIT_SUCCESS);
  }
  if (engine == "hyperscan")
    args.engine = ENGINE_HYPERSCAN;
  else if (engine == "pcre2")
    args.engine = ENGINE_PCRE2;
  else if (engine == "pcre2jit")
    args.engine = ENGINE_PCRE2JIT;
  else if (engine == "re2")
    args.engine = ENGINE_RE2;
  else if (engine == "rematch")
    args.engine = ENGINE_REMATCH;
  else {
    std::cerr << "unknown engine: " << engine << std::endl;
    std::exit(EXIT_FAILURE);
  }
  if (args.repeat <= 0) {
    std::cerr << "invalid repeat value: " << args.repeat << std::endl;
    std::exit(EXIT_FAILURE);
  }

  if (!vm.count("rule_file")) {
    std::cerr << "error: no rule file" << std::endl;
    std::exit(EXIT_FAILURE);
  }
  if (!vm.count("pcap_file")) {
    std::cerr << "error: no pcap file" << std::endl;
    std::exit(EXIT_FAILURE);
  }
  return args;
}

static void compilePCRE2(const Arguments &args,
                         std::unique_ptr<regexbench::Engine> &engine) {
  if (!args.pcre2_concat)
    engine->compile(loadRules(args.rule_file));
  else {
    auto rules = loadRules(args.rule_file);
    concatRules(rules);
    engine->compile(rules);
  }
}
<|endoftext|>


<commit_before>//=======================================================================
// Copyright (c) 2013-2018 Baptiste Wicht.
// Distributed under the terms of the MIT License.
// (See accompanying file LICENSE or copy at
//  http://opensource.org/licenses/MIT)
//=======================================================================

#include <iostream>

#include "retirement.hpp"
#include "assets.hpp"
#include "accounts.hpp"
#include "expenses.hpp"
#include "earnings.hpp"
#include "budget_exception.hpp"
#include "config.hpp"
#include "console.hpp"
#include "writer.hpp"

using namespace budget;

namespace {

constexpr size_t running_limit = 12;

money running_expenses(){
    auto today = budget::local_day();

    budget::date end = today - budget::days(today.day() - 1);
    budget::date start = end - budget::months(running_limit);

    budget::money total;

    for(auto& expense : all_expenses()){
        if(expense.date >= start && expense.date < end){
            total += expense.amount;
        }
    }

    return total;
}

double running_savings_rate(){
    auto today = budget::local_day();

    double savings_rate = 0.0;

    for(size_t i = 1; i <= running_limit; ++i){
        auto d = today - budget::months(i);

        budget::money expenses;

        for (auto& expense : all_expenses()) {
            if (expense.date.year() == d.year() && expense.date.month() == d.month()) {
                expenses += expense.amount;
            }
        }

        budget::money earnings;

        for (auto& earning : all_earnings()) {
            if (earning.date.year() == d.year() && earning.date.month() == d.month()) {
                earnings += earning.amount;
            }
        }

        budget::money income;

        for (auto& account : all_accounts(d.year(), d.month())){
            income += account.amount;
        }

        auto balance = income + earnings - expenses;
        auto local   = balance / (income + earnings);

        if(local < 0){
            local = 0;
        }

        savings_rate += local;
    }

    return savings_rate / running_limit;
}

void retirement_set() {
    double wrate = 4.0;
    double roi = 4.0;

    if(internal_config_contains("withdrawal_rate")){
        wrate = to_number<double>(internal_config_value("withdrawal_rate"));
    }

    if(internal_config_contains("expected_roi")){
        roi = to_number<double>(internal_config_value("expected_roi"));
    }

    edit_double(wrate, "Withdrawal Rate (%)");
    edit_double(roi, "Expected Annual Return (%)");

    // Save the configuration
    internal_config_value("withdrawal_rate") = to_string(wrate);
    internal_config_value("expected_roi") = to_string(roi);
}

} // end of anonymous namespace

void budget::retirement_module::load() {
    load_accounts();
    load_assets();
    load_expenses();
    load_earnings();
}

void budget::retirement_module::handle(std::vector<std::string>& args) {
    console_writer w(std::cout);

    if (args.empty() || args.size() == 1) {
        retirement_status(w);
    } else {
        auto& subcommand = args[1];

        if (subcommand == "status") {
            retirement_status(w);
        } else if (subcommand == "set") {
            retirement_set();
            std::cout << std::endl;
            retirement_status(w);
        } else {
            throw budget_exception("Invalid subcommand \"" + subcommand + "\"");
        }
    }
}

void budget::retirement_status(budget::writer& w) {
    if (!w.is_web()) {
        if (!internal_config_contains("withdrawal_rate")) {
            w << "Not enough information, please configure first with retirement set" << end_of_line;
            return;
        }

        if (!internal_config_contains("expected_roi")) {
            w << "Not enough information, please configure first with retirement set" << end_of_line;
            return;
        }
    }

    auto currency       = get_default_currency();
    auto wrate          = to_number<double>(internal_config_value("withdrawal_rate"));
    auto roi            = to_number<double>(internal_config_value("expected_roi"));
    auto years          = double(int(100.0 / wrate));
    auto expenses       = running_expenses();
    auto savings_rate   = running_savings_rate();
    auto nw             = get_net_worth();
    auto missing        = years * expenses - nw;
    auto income         = 12 * get_base_income();
    auto a_savings_rate = (income - expenses) / income;

    size_t base_months   = 0;
    size_t a_base_months = 0;

    auto current_nw = nw;
    while (current_nw < years * expenses) {
        current_nw *= 1.0 + (roi / 100.0) / 12;
        current_nw += (savings_rate * income) / 12;

        ++base_months;
    }

    current_nw = nw;
    while (current_nw < years * expenses) {
        current_nw *= 1.0 + (roi / 100.0) / 12;
        current_nw += (a_savings_rate * income) / 12;

        ++a_base_months;
    }

    std::vector<std::string> columns = {};
    std::vector<std::vector<std::string>> contents;

    using namespace std::string_literals;

    contents.push_back({"Withdrawal rate"s, to_string(wrate) + "%"});
    contents.push_back({"Annual Return"s, to_string(roi) + "%"});
    contents.push_back({"Years of expense"s, to_string(years)});
    contents.push_back({"Running expenses"s, to_string(expenses) + " " + currency});
    contents.push_back({"Monthly expenses"s, to_string(expenses / 12) + " " + currency});
    contents.push_back({"Target Net Worth"s, to_string(years * expenses) + " " + currency});
    contents.push_back({"Current Net Worth"s, to_string(nw) + " " + currency});
    contents.push_back({"Missing Net Worth"s, to_string(missing) + " " + currency});
    contents.push_back({"FI Ratio"s, to_string(100 * (nw / missing)) + "%"});
    contents.push_back({"Months of FI"s, to_string(nw / (expenses / 12))});
    contents.push_back({"Yearly income"s, to_string(income) + " " + currency});
    contents.push_back({"Running Savings Rate"s, to_string(100 * savings_rate) + "%"});
    contents.push_back({"Yearly savings"s, to_string(savings_rate * income) + " " + currency});
    contents.push_back({"Time to FI (w/o returns)"s, to_string(missing / (savings_rate * income)) + " years"});
    contents.push_back({"Time to FI (w/ returns)"s, to_string(base_months / 12.0) + " years"});

    contents.push_back({"Adjusted Savings Rate"s, to_string(100 * a_savings_rate) + "%"});
    contents.push_back({"Adjusted Yearly savings"s, to_string(a_savings_rate * income) + " " + currency});
    contents.push_back({"Adjusted Time to FI (w/o returns)"s, to_string(missing / (a_savings_rate * income)) + " years"});
    contents.push_back({"Adjusted Time to FI (w/ returns)"s, to_string(a_base_months / 12.0) + " years"});

    w.display_table(columns, contents);

    std::array<int, 5> rate_decs{1, 2, 5, 10, 20};

    // Note: this not totally correct since we ignore the
    // correlation between the savings rate and the expenses

    for (auto dec : rate_decs) {
        auto dec_savings_rate = savings_rate + 0.01 * dec;

        auto current_nw = nw;
        size_t months   = 0;

        while (current_nw < years * expenses) {
            current_nw *= 1.0 + (roi / 100.0) / 12;
            current_nw += (dec_savings_rate * income) / 12;

            ++months;
        }

        w << p_begin << "Increasing Savings Rate by " << dec << "% would save " << (base_months - months) / 12.0 << " years (in " << months / 12.0 << " years)" << p_end;
    }

    std::array<int, 5> exp_decs{10, 50, 100, 200, 500};

    for (auto dec : exp_decs) {
        auto current_nw = nw;
        size_t months   = 0;

        auto new_savings_rate = (income - (expenses - dec * 12)) / income;;

        while (current_nw < years * (expenses - (dec * 12))) {
            current_nw *= 1.0 + (roi / 100.0) / 12;
            current_nw += (new_savings_rate * income) / 12;

            ++months;
        }

        w << p_begin << "Decreasing monthly expenses by " << dec << " " << currency << " would save " << (base_months - months) / 12.0 << " years (in " << months / 12.0 << " (adjusted) years)" << p_end;
    }
}
<commit_msg>Improve FI metrics<commit_after>//=======================================================================
// Copyright (c) 2013-2018 Baptiste Wicht.
// Distributed under the terms of the MIT License.
// (See accompanying file LICENSE or copy at
//  http://opensource.org/licenses/MIT)
//=======================================================================

#include <iostream>

#include "retirement.hpp"
#include "assets.hpp"
#include "accounts.hpp"
#include "expenses.hpp"
#include "earnings.hpp"
#include "budget_exception.hpp"
#include "config.hpp"
#include "console.hpp"
#include "writer.hpp"

using namespace budget;

namespace {

constexpr size_t running_limit = 12;

money running_expenses(){
    auto today = budget::local_day();

    budget::date end = today - budget::days(today.day() - 1);
    budget::date start = end - budget::months(running_limit);

    budget::money total;

    for(auto& expense : all_expenses()){
        if(expense.date >= start && expense.date < end){
            total += expense.amount;
        }
    }

    return total;
}

double running_savings_rate(){
    auto today = budget::local_day();

    double savings_rate = 0.0;

    for(size_t i = 1; i <= running_limit; ++i){
        auto d = today - budget::months(i);

        budget::money expenses;

        for (auto& expense : all_expenses()) {
            if (expense.date.year() == d.year() && expense.date.month() == d.month()) {
                expenses += expense.amount;
            }
        }

        budget::money earnings;

        for (auto& earning : all_earnings()) {
            if (earning.date.year() == d.year() && earning.date.month() == d.month()) {
                earnings += earning.amount;
            }
        }

        budget::money income;

        for (auto& account : all_accounts(d.year(), d.month())){
            income += account.amount;
        }

        auto balance = income + earnings - expenses;
        auto local   = balance / (income + earnings);

        if(local < 0){
            local = 0;
        }

        savings_rate += local;
    }

    return savings_rate / running_limit;
}

void retirement_set() {
    double wrate = 4.0;
    double roi = 4.0;

    if(internal_config_contains("withdrawal_rate")){
        wrate = to_number<double>(internal_config_value("withdrawal_rate"));
    }

    if(internal_config_contains("expected_roi")){
        roi = to_number<double>(internal_config_value("expected_roi"));
    }

    edit_double(wrate, "Withdrawal Rate (%)");
    edit_double(roi, "Expected Annual Return (%)");

    // Save the configuration
    internal_config_value("withdrawal_rate") = to_string(wrate);
    internal_config_value("expected_roi") = to_string(roi);
}

} // end of anonymous namespace

void budget::retirement_module::load() {
    load_accounts();
    load_assets();
    load_expenses();
    load_earnings();
}

void budget::retirement_module::handle(std::vector<std::string>& args) {
    console_writer w(std::cout);

    if (args.empty() || args.size() == 1) {
        retirement_status(w);
    } else {
        auto& subcommand = args[1];

        if (subcommand == "status") {
            retirement_status(w);
        } else if (subcommand == "set") {
            retirement_set();
            std::cout << std::endl;
            retirement_status(w);
        } else {
            throw budget_exception("Invalid subcommand \"" + subcommand + "\"");
        }
    }
}

void budget::retirement_status(budget::writer& w) {
    if (!w.is_web()) {
        if (!internal_config_contains("withdrawal_rate")) {
            w << "Not enough information, please configure first with retirement set" << end_of_line;
            return;
        }

        if (!internal_config_contains("expected_roi")) {
            w << "Not enough information, please configure first with retirement set" << end_of_line;
            return;
        }
    }

    auto currency       = get_default_currency();
    auto wrate          = to_number<double>(internal_config_value("withdrawal_rate"));
    auto roi            = to_number<double>(internal_config_value("expected_roi"));
    auto years          = double(int(100.0 / wrate));
    auto expenses       = running_expenses();
    auto savings_rate   = running_savings_rate();
    auto nw             = get_net_worth();
    auto missing        = years * expenses - nw;
    auto income         = 12 * get_base_income();
    auto a_savings_rate = (income - expenses) / income;

    size_t base_months   = 0;
    size_t a_base_months = 0;

    auto current_nw = nw;
    while (current_nw < years * expenses) {
        current_nw *= 1.0 + (roi / 100.0) / 12;
        current_nw += (savings_rate * income) / 12;

        ++base_months;
    }

    current_nw = nw;
    while (current_nw < years * expenses) {
        current_nw *= 1.0 + (roi / 100.0) / 12;
        current_nw += (a_savings_rate * income) / 12;

        ++a_base_months;
    }

    std::vector<std::string> columns = {};
    std::vector<std::vector<std::string>> contents;

    using namespace std::string_literals;

    // The configuration
    contents.push_back({"Withdrawal rate"s, to_string(wrate) + "%"});
    contents.push_back({"Annual Return"s, to_string(roi) + "%"});
    contents.push_back({"Years of expense"s, to_string(years)});

    // The target
    contents.push_back({""s, ""s});
    contents.push_back({"Running expenses"s, to_string(expenses) + " " + currency});
    contents.push_back({"Monthly expenses"s, to_string(expenses / 12) + " " + currency});
    contents.push_back({"Target Net Worth"s, to_string(years * expenses) + " " + currency});

    contents.push_back({""s, ""s});
    contents.push_back({"Current Net Worth"s, to_string(nw) + " " + currency});
    contents.push_back({"Missing Net Worth"s, to_string(missing) + " " + currency});
    contents.push_back({"Yearly income"s, to_string(income) + " " + currency});
    contents.push_back({"Running Savings Rate"s, to_string(100 * savings_rate) + "%"});
    contents.push_back({"Yearly savings"s, to_string(savings_rate * income) + " " + currency});
    contents.push_back({"FI Ratio"s, to_string(100 * (nw / missing)) + "%"});

    auto fi_date = budget::local_day() + budget::months(base_months);
    contents.push_back({""s, ""s});
    contents.push_back({"Months to FI"s, to_string(base_months)});
    contents.push_back({"Years to FI"s, to_string(base_months / 12.0)});
    contents.push_back({"Date to FI"s, to_string(fi_date)});

    contents.push_back({""s, ""s});
    contents.push_back({"Current Withdrawal Rate"s, to_string(100.0 * (expenses / nw)) + "%"});
    contents.push_back({"Months of FI"s, to_string(nw / (expenses / 12))});
    contents.push_back({"Years of FI"s, to_string(nw / (expenses))});

    contents.push_back({""s, ""s});
    contents.push_back({"Current Yearly Allowance"s, to_string(nw * (wrate / 100.0))});
    contents.push_back({"Current Monthly Allowance"s, to_string((nw * (wrate / 100.0)) / 12)});

    auto a_fi_date = budget::local_day() + budget::months(a_base_months);
    contents.push_back({""s, ""s});
    contents.push_back({"Adjusted Savings Rate"s, to_string(100 * a_savings_rate) + "%"});
    contents.push_back({"Adjusted Yearly savings"s, to_string(a_savings_rate * income) + " " + currency});
    contents.push_back({"Adjusted Months to FI"s, to_string(a_base_months)});
    contents.push_back({"Adjusted Years to FI"s, to_string(a_base_months / 12.0)});
    contents.push_back({"Adjusted Date to FI"s, to_string(a_fi_date)});

    w.display_table(columns, contents);

    std::array<int, 5> rate_decs{1, 2, 5, 10, 20};

    // Note: this not totally correct since we ignore the
    // correlation between the savings rate and the expenses

    for (auto dec : rate_decs) {
        auto dec_savings_rate = savings_rate + 0.01 * dec;

        auto current_nw = nw;
        size_t months   = 0;

        while (current_nw < years * expenses) {
            current_nw *= 1.0 + (roi / 100.0) / 12;
            current_nw += (dec_savings_rate * income) / 12;

            ++months;
        }

        w << p_begin << "Increasing Savings Rate by " << dec << "% would save " << (base_months - months) / 12.0 << " years (in " << months / 12.0 << " years)" << p_end;
    }

    std::array<int, 5> exp_decs{10, 50, 100, 200, 500};

    for (auto dec : exp_decs) {
        auto current_nw = nw;
        size_t months   = 0;

        auto new_savings_rate = (income - (expenses - dec * 12)) / income;;

        while (current_nw < years * (expenses - (dec * 12))) {
            current_nw *= 1.0 + (roi / 100.0) / 12;
            current_nw += (new_savings_rate * income) / 12;

            ++months;
        }

        w << p_begin << "Decreasing monthly expenses by " << dec << " " << currency << " would save " << (base_months - months) / 12.0 << " years (in " << months / 12.0 << " (adjusted) years)" << p_end;
    }
}
<|endoftext|>


<commit_before>/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/chips/p9/procedures/hwp/lib/p9_common_poweronoff.H $ */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2019                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
///
/// @file  p9_common_poweronoff.H
/// @brief common procedure for power on/off
///
/// *HWP HWP Owner          : David Du       <daviddu@us.ibm.com>
/// *HWP Backup HWP Owner   : Greg Still     <stillgs@us.ibm.com>
/// *HWP FW Owner           : Sangeetha T S  <sangeet2@in.ibm.com>
/// *HWP Team               : PM
/// *HWP Consumed by        : SBE:SGPE:CME
/// *HWP Level              : 2
///

#ifndef __P9_COMMON_POWERONOFF_H__
#define __P9_COMMON_POWERONOFF_H__

namespace p9power
{
enum powerOperation_t
{
    POWER_ON      = 0x0,
    POWER_OFF     = 0xFF,
    POWER_ON_VDD  = 0x1,
    POWER_OFF_VDD = 0xFE
};
}

// Define only address offset to be compatible with both core and cache domain
#define    PPM_PFCS      0x000f0118
#define    PPM_PFCS_CLR  0x000f0119
#define    PPM_PFCS_OR   0x000f011a
#define    PPM_PFDLY     0x000f011b
#define    PPM_PFSNS     0x000f011c
#define    PPM_PFOFF     0x000f011d

/// @typedef p9_common_poweronoff_FP_t
/// function pointer typedef definition for HWP call support
typedef fapi2::ReturnCode (*p9_common_poweronoff_FP_t) (
    const fapi2::Target < fapi2::TARGET_TYPE_EQ |
    fapi2::TARGET_TYPE_CORE > &,
    const p9power::powerOperation_t i_operation);

extern "C"
{
/// @brief common procedure for power on/off
///
/// @param [in] i_target    TARGET_TYPE_EQ|TARGET_TYPE_CORE target
/// @param [in] i_operation ENUM(ON,OFF)
///
/// @attr
/// @attritem ATTR_PFET_TIMING - EX target, uint32
///
/// @retval FAPI_RC_SUCCESS
    fapi2::ReturnCode
    p9_common_poweronoff(
        const fapi2::Target < fapi2::TARGET_TYPE_EQ |
        fapi2::TARGET_TYPE_CORE > & i_target,
        const p9power::powerOperation_t i_operation);

} // extern C

#endif  // __P9_COMMON_POWERONOFF_H__
<commit_msg>HWP-CORE/CACHE: Update Istep 4 procedures regressed on model 34<commit_after>/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/chips/p9/procedures/hwp/lib/p9_common_poweronoff.H $ */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2019                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
///
/// @file  p9_common_poweronoff.H
/// @brief common procedure for power on/off
///

// *HWP HWP Owner          : David Du       <daviddu@us.ibm.com>
// *HWP Backup HWP Owner   : Greg Still     <stillgs@us.ibm.com>
// *HWP FW Owner           : Sangeetha T S  <sangeet2@in.ibm.com>
// *HWP Team               : PM
// *HWP Consumed by        : SBE:SGPE:CME
// *HWP Level              : 2

#ifndef __P9_COMMON_POWERONOFF_H__
#define __P9_COMMON_POWERONOFF_H__

#include <fapi2.H>

namespace p9power
{
enum powerOperation_t
{
    POWER_ON      = 0x0,
    POWER_OFF     = 0xFF,
    POWER_ON_VDD  = 0x1,
    POWER_OFF_VDD = 0xFE
};
}

/// @typedef p9_common_poweronoff_FP_t
/// function pointer typedef definition for HWP call support
/// @todo: consider template solution here
typedef fapi2::ReturnCode (*p9_common_poweronoff_FP_t) (
    const fapi2::Target < fapi2::TARGET_TYPE_EQ |
    fapi2::TARGET_TYPE_CORE > &,
    const p9power::powerOperation_t i_operation);

/// @brief common procedure for power on/off
///
/// @param [in] i_target    TARGET_TYPE_EQ|TARGET_TYPE_CORE target
/// @param [in] i_operation ENUM(ON,OFF)
///
/// @attr
/// @attritem ATTR_PFET_TIMING - EX target, uint32
///
/// @retval FAPI_RC_SUCCESS
template <fapi2::TargetType K>
fapi2::ReturnCode
p9_common_poweronoff(
    const fapi2::Target<K>& i_target,
    const p9power::powerOperation_t i_operation);

#endif  // __P9_COMMON_POWERONOFF_H__
<|endoftext|>


<commit_before>/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/chips/p9/procedures/hwp/memory/p9_mss_eff_config.H $ */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2019                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

///
/// @file p9_mss_eff_config.H
/// @brief Command and Control for the memory subsystem - populate attributes
///
// *HWP HWP Owner: Andre Marin <aamarin@us.ibm.com>
// *HWP HWP Backup: Brian Silver <bsilver@us.ibm.com>
// *HWP Team: Memory
// *HWP Level: 1
// *HWP Consumed by: FSP:HB

#ifndef __P9_MSS_EFF_CONFIG__
#define __P9_MSS_EFF_CONFIG__

#include <fapi2.H>

typedef fapi2::ReturnCode (*p9_mss_eff_config_FP_t) (const fapi2::Target<fapi2::TARGET_TYPE_MCS>&);

extern "C"
{

///
/// @brief Configure the attributes for each controller
/// @param[in] i_target, the controller (e.g., MCS)
/// @return FAPI2_RC_SUCCESS iff ok
///
    fapi2::ReturnCode p9_mss_eff_config( const fapi2::Target<fapi2::TARGET_TYPE_MCS>& i_target );

}

#endif
<commit_msg>Modify spd_decoder, eff_config, unit tests. Modify dependent files<commit_after>/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/chips/p9/procedures/hwp/memory/p9_mss_eff_config.H $ */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2019                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

///
/// @file p9_mss_eff_config.H
/// @brief Command and Control for the memory subsystem - populate attributes
///
// *HWP HWP Owner: Andre Marin <aamarin@us.ibm.com>
// *HWP HWP Backup: Brian Silver <bsilver@us.ibm.com>
// *HWP Team: Memory
// *HWP Level: 1
// *HWP Consumed by: FSP:HB

#ifndef __P9_MSS_EFF_CONFIG__
#define __P9_MSS_EFF_CONFIG__

#include <fapi2.H>

typedef fapi2::ReturnCode (*p9_mss_eff_config_FP_t) (const fapi2::Target<fapi2::TARGET_TYPE_MCS>&);

extern "C"
{

///
/// @brief Configure the attributes for each controller
/// @param[in] i_target the controller (e.g., MCS)
/// @return FAPI2_RC_SUCCESS iff ok
///
    fapi2::ReturnCode p9_mss_eff_config( const fapi2::Target<fapi2::TARGET_TYPE_MCS>& i_target );

}

#endif
<|endoftext|>


<commit_before>#include "serializer.h"
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <limits>

namespace rapscallion {

void serializer<std::uint_least64_t>::write(Serializer& s, std::uint_least64_t value) {
  while (value >= 0x80) {
    s.addByte((uint8_t)(value & 0x7F) | 0x80);
    value >>= 7;
  }
  s.addByte((uint8_t)value);
}
void serializer<long>::write(Serializer& s, const long v) {
  std::uint_least64_t val = (std::abs(v) << 1) | (v < 0 ? 1 : 0);
  serializer<std::uint_least64_t>::write(s, val);
}
void serializer<int>::write(Serializer& s, const int v) {
  serializer<long>::write(s, v);
}
void serializer<std::string>::write(Serializer& s, const std::string& value) {
  serializer<std::uint_least64_t>::write(s, value.size());
  for (auto& c : value) s.addByte(c);
}
void serializer<bool>::write(Serializer& s, const bool b) {
  serializer<std::uint_least64_t>::write(s, b ? 1 : 0);
}

namespace {
  enum Type {
    Zero,
    Infinity,
    NaN,
    Normal
  };
  struct float_repr {
    // Can fit an exponent of 32-2=20 bits, can support octuple-precision IEEE754 floats
    std::int_least32_t exponent;
    // Can support double precision IEEE754 floats (quadruple requires 112 bits, octuple 236 bits)
    std::uint_least64_t fraction;
    // One bit reserved for sign bit
    static constexpr unsigned fraction_bits = 63 - 1;
    bool is_negative;
    Type type;
  };
  auto bitreverse(std::uint_least64_t b) -> decltype(b)
  {
    b = ((b & 0b1111111111111111111111111111111100000000000000000000000000000000ULL) >> 32) | ((b & 0b0000000000000000000000000000000011111111111111111111111111111111ULL) << 32);
    b = ((b & 0b1111111111111111000000000000000011111111111111110000000000000000ULL) >> 16) | ((b & 0b0000000000000000111111111111111100000000000000001111111111111111ULL) << 16);
    b = ((b & 0b1111111100000000111111110000000011111111000000001111111100000000ULL) >>  8) | ((b & 0b0000000011111111000000001111111100000000111111110000000011111111ULL) <<  8);
    b = ((b & 0b1111000011110000111100001111000011110000111100001111000011110000ULL) >>  4) | ((b & 0b0000111100001111000011110000111100001111000011110000111100001111ULL) <<  4);
    b = ((b & 0b1100110011001100110011001100110011001100110011001100110011001100ULL) >>  2) | ((b & 0b0011001100110011001100110011001100110011001100110011001100110011ULL) <<  2);
    b = ((b & 0b1010101010101010101010101010101010101010101010101010101010101010ULL) >>  1) | ((b & 0b0101010101010101010101010101010101010101010101010101010101010101ULL) <<  1);
    return b;
  }
}

template <>
struct serializer<float_repr> {
  static void write(Serializer& s, float_repr const &b) {
    // Using multiplication to avoid bit shifting a signed integer
    switch(b.type) {
      default:
        {
          serializer<decltype(+b.exponent)>::write(s, b.is_negative ? -b.type : b.type);
        }
        break;
      case Normal:
        {
          const decltype(b.exponent) exponent
            = b.exponent * 2 + (b.exponent < 0 ? -b.is_negative : b.is_negative);
          const decltype(b.fraction) reversed_fraction
            = bitreverse(b.fraction);
          serializer<decltype(+exponent)>::write(s, exponent < 0 ? exponent - 2 : exponent + 3);
          serializer<decltype(+b.fraction)>::write(s, reversed_fraction);
        }
        break;
    };
  }
  static float_repr read(Deserializer& s) {
    float_repr result;
    auto exponent = serializer<decltype(result.exponent)>::read(s);
    if (exponent < 3 && exponent >= -2) {
      result.is_negative   = (exponent < 0);
      result.type          = (Type)std::abs(exponent);
    } else {
      exponent = (exponent < 0) ? exponent + 2 : exponent - 3;
      result.is_negative   = !!((exponent/ 1) % 2);
      result.type = Normal;
      result.exponent      = exponent / 2;
      const auto reversed_fraction = serializer<decltype(result.fraction)>::read(s);
      result.fraction      = bitreverse(reversed_fraction);
      return result;
    }
  }
};

void serializer<double>::write(Serializer& s, double const b) {
  switch (std::fpclassify(b)) {
    case FP_ZERO:
      serializer<float_repr>::write(s, { 0, 0, !!std::signbit(b), Zero });
      break;
    case FP_INFINITE:
      serializer<float_repr>::write(s, { 0, 0, !!std::signbit(b), Infinity });
      break;
    case FP_NAN:
      // The bit reversal is to ensure the most efficient encoding can be used
      serializer<float_repr>::write(s, { 0, 0, false, NaN });
      break;
    case FP_NORMAL:
    case FP_SUBNORMAL:
      float_repr repr;
      repr.type = Normal;
      repr.is_negative = !!std::signbit(b);
      // Make the fraction a positive integer
      repr.fraction = std::ldexp(std::abs(std::frexp(b, &repr.exponent)), repr.fraction_bits);
      serializer<decltype(repr)>::write(s, repr);
      break;
  }
}

double serializer<double>::read(Deserializer& s) {
  const auto repr = serializer<float_repr>::read(s);
  switch(repr.type) {
    case Zero:
      return (repr.is_negative ? -0.0 : 0.0);
    case Infinity:
      return repr.is_negative ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity();
    case NaN:
      return std::numeric_limits<double>::quiet_NaN();
    default:
      return (repr.is_negative ? -1.0 : 1.0) * std::ldexp(static_cast<double>(repr.fraction), repr.exponent - repr.fraction_bits);
  }
}

void serializer<float>::write(Serializer& s, float const b) {
  serializer<double>::write(s, b);
}
float serializer<float>::read(Deserializer& s) {
  return serializer<double>::read(s);
}

std::uint_least64_t serializer<std::uint_least64_t>::read(Deserializer& s) {
  std::uint_least64_t val = 0,
    offs = 0,
    b = s.getByte();
  while (b & 0x80) {
    val |= (b & 0x7F) << offs;
    offs += 7;
    b = s.getByte();
  }
  val |= b << offs;
  return val;
}
long serializer<long>::read(Deserializer& s) {
  const auto val = serializer<std::uint_least64_t>::read(s);
  auto value = static_cast<long>(val >> 1);
  if (val & 1) value = -value;
  return value;
}
int serializer<int>::read(Deserializer& s) {
  return serializer<long>::read(s);
}
std::string serializer<std::string>::read(Deserializer& s) {
  const auto length = serializer<std::uint_least64_t>::read(s);
  const uint8_t* ptr = s.ptr;
  const uint8_t* end = ptr + length;
  if (end > s.end)
    throw parse_exception("buffer underrun");
  s.ptr = end;
  return std::string(reinterpret_cast<const char*>(ptr), length);
}
bool serializer<bool>::read(Deserializer& s) {
  return serializer<std::uint_least64_t>::read(s) > 0;
}

}
<commit_msg>Fix warning<commit_after>#include "serializer.h"
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <limits>

namespace rapscallion {

void serializer<std::uint_least64_t>::write(Serializer& s, std::uint_least64_t value) {
  while (value >= 0x80) {
    s.addByte((uint8_t)(value & 0x7F) | 0x80);
    value >>= 7;
  }
  s.addByte((uint8_t)value);
}
void serializer<long>::write(Serializer& s, const long v) {
  std::uint_least64_t val = (std::abs(v) << 1) | (v < 0 ? 1 : 0);
  serializer<std::uint_least64_t>::write(s, val);
}
void serializer<int>::write(Serializer& s, const int v) {
  serializer<long>::write(s, v);
}
void serializer<std::string>::write(Serializer& s, const std::string& value) {
  serializer<std::uint_least64_t>::write(s, value.size());
  for (auto& c : value) s.addByte(c);
}
void serializer<bool>::write(Serializer& s, const bool b) {
  serializer<std::uint_least64_t>::write(s, b ? 1 : 0);
}

namespace {
  enum Type {
    Zero,
    Infinity,
    NaN,
    Normal
  };
  struct float_repr {
    // Can fit an exponent of 32-2=20 bits, can support octuple-precision IEEE754 floats
    std::int_least32_t exponent;
    // Can support double precision IEEE754 floats (quadruple requires 112 bits, octuple 236 bits)
    std::uint_least64_t fraction;
    // One bit reserved for sign bit
    static constexpr unsigned fraction_bits = 63 - 1;
    bool is_negative;
    Type type;
  };
  auto bitreverse(std::uint_least64_t b) -> decltype(b)
  {
    b = ((b & 0b1111111111111111111111111111111100000000000000000000000000000000ULL) >> 32) | ((b & 0b0000000000000000000000000000000011111111111111111111111111111111ULL) << 32);
    b = ((b & 0b1111111111111111000000000000000011111111111111110000000000000000ULL) >> 16) | ((b & 0b0000000000000000111111111111111100000000000000001111111111111111ULL) << 16);
    b = ((b & 0b1111111100000000111111110000000011111111000000001111111100000000ULL) >>  8) | ((b & 0b0000000011111111000000001111111100000000111111110000000011111111ULL) <<  8);
    b = ((b & 0b1111000011110000111100001111000011110000111100001111000011110000ULL) >>  4) | ((b & 0b0000111100001111000011110000111100001111000011110000111100001111ULL) <<  4);
    b = ((b & 0b1100110011001100110011001100110011001100110011001100110011001100ULL) >>  2) | ((b & 0b0011001100110011001100110011001100110011001100110011001100110011ULL) <<  2);
    b = ((b & 0b1010101010101010101010101010101010101010101010101010101010101010ULL) >>  1) | ((b & 0b0101010101010101010101010101010101010101010101010101010101010101ULL) <<  1);
    return b;
  }
}

template <>
struct serializer<float_repr> {
  static void write(Serializer& s, float_repr const &b) {
    // Using multiplication to avoid bit shifting a signed integer
    switch(b.type) {
      default:
        {
          serializer<decltype(+b.exponent)>::write(s, b.is_negative ? -b.type : b.type);
        }
        break;
      case Normal:
        {
          const decltype(b.exponent) exponent
            = b.exponent * 2 + (b.exponent < 0 ? -b.is_negative : b.is_negative);
          const decltype(b.fraction) reversed_fraction
            = bitreverse(b.fraction);
          serializer<decltype(+exponent)>::write(s, exponent < 0 ? exponent - 2 : exponent + 3);
          serializer<decltype(+b.fraction)>::write(s, reversed_fraction);
        }
        break;
    };
  }
  static float_repr read(Deserializer& s) {
    float_repr result;
    auto exponent = serializer<decltype(result.exponent)>::read(s);
    if (exponent < 3 && exponent >= -2) {
      result.is_negative   = (exponent < 0);
      result.type          = (Type)std::abs(exponent);
    } else {
      exponent = (exponent < 0) ? exponent + 2 : exponent - 3;
      result.is_negative   = !!((exponent/ 1) % 2);
      result.type = Normal;
      result.exponent      = exponent / 2;
      const auto reversed_fraction = serializer<decltype(result.fraction)>::read(s);
      result.fraction      = bitreverse(reversed_fraction);
    }
    return result;
  }
};

void serializer<double>::write(Serializer& s, double const b) {
  switch (std::fpclassify(b)) {
    case FP_ZERO:
      serializer<float_repr>::write(s, { 0, 0, !!std::signbit(b), Zero });
      break;
    case FP_INFINITE:
      serializer<float_repr>::write(s, { 0, 0, !!std::signbit(b), Infinity });
      break;
    case FP_NAN:
      // The bit reversal is to ensure the most efficient encoding can be used
      serializer<float_repr>::write(s, { 0, 0, false, NaN });
      break;
    case FP_NORMAL:
    case FP_SUBNORMAL:
      float_repr repr;
      repr.type = Normal;
      repr.is_negative = !!std::signbit(b);
      // Make the fraction a positive integer
      repr.fraction = std::ldexp(std::abs(std::frexp(b, &repr.exponent)), repr.fraction_bits);
      serializer<decltype(repr)>::write(s, repr);
      break;
  }
}

double serializer<double>::read(Deserializer& s) {
  const auto repr = serializer<float_repr>::read(s);
  switch(repr.type) {
    case Zero:
      return (repr.is_negative ? -0.0 : 0.0);
    case Infinity:
      return repr.is_negative ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity();
    case NaN:
      return std::numeric_limits<double>::quiet_NaN();
    default:
      return (repr.is_negative ? -1.0 : 1.0) * std::ldexp(static_cast<double>(repr.fraction), repr.exponent - repr.fraction_bits);
  }
}

void serializer<float>::write(Serializer& s, float const b) {
  serializer<double>::write(s, b);
}
float serializer<float>::read(Deserializer& s) {
  return serializer<double>::read(s);
}

std::uint_least64_t serializer<std::uint_least64_t>::read(Deserializer& s) {
  std::uint_least64_t val = 0,
    offs = 0,
    b = s.getByte();
  while (b & 0x80) {
    val |= (b & 0x7F) << offs;
    offs += 7;
    b = s.getByte();
  }
  val |= b << offs;
  return val;
}
long serializer<long>::read(Deserializer& s) {
  const auto val = serializer<std::uint_least64_t>::read(s);
  auto value = static_cast<long>(val >> 1);
  if (val & 1) value = -value;
  return value;
}
int serializer<int>::read(Deserializer& s) {
  return serializer<long>::read(s);
}
std::string serializer<std::string>::read(Deserializer& s) {
  const auto length = serializer<std::uint_least64_t>::read(s);
  const uint8_t* ptr = s.ptr;
  const uint8_t* end = ptr + length;
  if (end > s.end)
    throw parse_exception("buffer underrun");
  s.ptr = end;
  return std::string(reinterpret_cast<const char*>(ptr), length);
}
bool serializer<bool>::read(Deserializer& s) {
  return serializer<std::uint_least64_t>::read(s) > 0;
}

}
<|endoftext|>