<commit_before>/*
 * SessionBuild.cpp
 *
 * Copyright (C) 2009-12 by RStudio, Inc.
 *
 * This program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */

#include "SessionBuild.hpp"

#include <boost/utility.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/format.hpp>
#include <boost/scope_exit.hpp>
#include <boost/enable_shared_from_this.hpp>

#include <core/Exec.hpp>
#include <core/FileSerializer.hpp>
#include <core/text/DcfParser.hpp>
#include <core/system/Process.hpp>
#include <core/system/ShellUtils.hpp>
#include <core/r_util/RPackageInfo.hpp>

#include <session/projects/SessionProjects.hpp>
#include <session/SessionModuleContext.hpp>

using namespace core;

namespace session {
namespace modules {
namespace build {

namespace {

FilePath restartContextFilePath()
{
   return module_context::scopedScratchPath().childPath(
                                                   "build_restart_context");
}

void saveRestartContext(const FilePath& packageDir,
                        const std::string& buildOutput)
{
   // read package info
   r_util::RPackageInfo pkgInfo;
   Error error = pkgInfo.read(packageDir);
   if (error)
   {
      LOG_ERROR(error);
      return;
   }

   // save restart context
   core::Settings restartSettings;
   error = restartSettings.initialize(restartContextFilePath());
   if (error)
   {
      LOG_ERROR(error);
      return;
   }

   restartSettings.beginUpdate();
   restartSettings.set("package_name", pkgInfo.name());
   restartSettings.set("build_output", buildOutput);
   restartSettings.endUpdate();
}

json::Value collectRestartContext()
{
   FilePath restartSettingsPath = restartContextFilePath();
   if (restartSettingsPath.exists())
   {
      // always cleanup the restart context on scope exit
      BOOST_SCOPE_EXIT( (&restartSettingsPath) )
      {
         Error error = restartSettingsPath.remove();
         if (error)
            LOG_ERROR(error);
      }
      BOOST_SCOPE_EXIT_END

      core::Settings restartSettings;
      Error error = restartSettings.initialize(restartContextFilePath());
      if (error)
      {
         LOG_ERROR(error);
         return json::Value();
      }

      json::Object restartJson;
      restartJson["package_name"] = restartSettings.get("package_name");
      restartJson["build_output"] = restartSettings.get("build_output");
      return restartJson;
   }
   else
   {
      return json::Value();
   }
}

// R command invocation -- has two representations, one to be submitted
// (shellCmd_) and one to show the user (cmdString_)
class RCommand
{
public:
   explicit RCommand(const FilePath& rBinDir)
#ifdef _WIN32
      : shellCmd_(rBinDir.childPath("Rcmd.exe"))
#else
      : shellCmd_(rBinDir.childPath("R"))
#endif
   {
#ifdef _WIN32
      cmdString_ = "Rcmd.exe";
#else
      shellCmd_ << "CMD";
      cmdString_ = "R CMD";
#endif
   }

   RCommand& operator<<(const std::string& arg)
   {
      cmdString_ += " " + arg;
      shellCmd_ << arg;
      return *this;
   }

   const std::string& commandString() const
   {
      return cmdString_;
   }

   const shell_utils::ShellCommand& shellCommand() const
   {
      return shellCmd_;
   }

private:
   std::string cmdString_;
   shell_utils::ShellCommand shellCmd_;
};

class Build : boost::noncopyable,
              public boost::enable_shared_from_this<Build>
{
public:
   static boost::shared_ptr<Build> create(const std::string& type)
   {
      boost::shared_ptr<Build> pBuild(new Build());
      pBuild->start(type);
      return pBuild;
   }

private:
   Build()
      : isRunning_(false), terminationRequested_(false), restartR_(false)
   {
   }

   void start(const std::string& type)
   {
      ClientEvent event(client_events::kBuildStarted);
      module_context::enqueClientEvent(event);

      isRunning_ = true;

      // read build options
      Error error = projects::projectContext().readBuildOptions(&options_);
      if (error)
      {
         terminateWithError("reading build options file", error);
         return;
      }

      // callbacks
      core::system::ProcessCallbacks cb;
      cb.onContinue = boost::bind(&Build::onContinue,
                                  Build::shared_from_this());
      cb.onStdout = boost::bind(&Build::onOutput,
                                Build::shared_from_this(), _2);
      cb.onStderr = boost::bind(&Build::onOutput,
                                Build::shared_from_this(), _2);
      cb.onExit =  boost::bind(&Build::onCompleted,
                                Build::shared_from_this(),
                                _1);

      // execute build
      executeBuild(type, cb);
   }

   void executeBuild(const std::string& type,
                     const core::system::ProcessCallbacks& cb)
   {
      // options
      core::system::ProcessOptions options;
      options.terminateChildren = true;
      options.redirectStdErrToStdOut = true;

      const core::r_util::RProjectConfig& config = projectConfig();
      if (config.buildType == r_util::kBuildTypePackage)
      {
         FilePath packagePath = projectPath(config.packagePath);
         options.workingDir = packagePath.parent();
         executePackageBuild(type, packagePath, options, cb);
      }
      else if (config.buildType == r_util::kBuildTypeMakefile)
      {
         FilePath makefilePath = projectPath(config.makefilePath);
         options.workingDir = makefilePath;
         executeMakefileBuild(type, options, cb);
      }
      else if (config.buildType == r_util::kBuildTypeCustom)
      {
         FilePath scriptPath = projectPath(config.customScriptPath);
         options.workingDir = scriptPath.parent();
         executeCustomBuild(type, scriptPath, options, cb);
      }
      else
      {
         terminateWithError("Unrecognized build type: " + config.buildType);
      }
   }

   void executePackageBuild(const std::string& type,
                            const FilePath& packagePath,
                            const core::system::ProcessOptions& options,
                            const core::system::ProcessCallbacks& cb)
   {
      // get package info
      r_util::RPackageInfo pkgInfo;
      Error error = pkgInfo.read(packagePath);
      if (error)
      {
         terminateWithError("Reading package DESCRIPTION", error);
         return;
      }

      // get R bin directory
      FilePath rBinDir;
      error = module_context::rBinDir(&rBinDir);
      if (error)
      {
         terminateWithError("attempting to locate R binary", error);
         return;
      }

      // build command
      if (type == "build-all")
      {
         // restart R after build is completed
         restartR_ = true;

         // build command
         RCommand rCmd(rBinDir);
         rCmd << "INSTALL";
         rCmd << packagePath.filename();

         // show the user the command
         enqueBuildOutput(rCmd.commandString() + "\n\n");

         // run R CMD INSTALL <package-dir>
         module_context::processSupervisor().runCommand(rCmd.shellCommand(),
                                                        options,
                                                        cb);
      }

      else if (type == "build-source-package")
      {
         // compose the build command
         RCommand rCmd(rBinDir);
         rCmd << "build";
         rCmd << packagePath.filename();

         // show the user the command
         enqueBuildOutput(rCmd.commandString() + "\n\n");

         // set a success message
         successMessage_ = buildPackageSuccessMsg("Source");

         // run R CMD build <package-dir>
         module_context::processSupervisor().runCommand(rCmd.shellCommand(),
                                                        options,
                                                        cb);
      }

      else if (type == "build-binary-package")
      {
         // compose the INSTALL --binary
         RCommand rCmd(rBinDir);
         rCmd << "INSTALL";
         rCmd << "--build";
         rCmd << packagePath.filename();

         // show the user the command
         enqueBuildOutput(rCmd.commandString() + "\n\n");

         // set a success message
         successMessage_ = "\n" + buildPackageSuccessMsg("Binary");

         // run R CMD INSTALL --build <package-dir>
         module_context::processSupervisor().runCommand(rCmd.shellCommand(),
                                                        options,
                                                        cb);
      }

      else if (type == "check-package")
      {
         // first build then check

         // compose the build command
         RCommand rCmd(rBinDir);
         rCmd << "build";
         rCmd << packagePath.filename();

         // compose the check command (will be executed by the onExit
         // handler of the build cmd)
         RCommand rCheckCmd(rBinDir);
         rCheckCmd << "check";
         rCheckCmd << pkgInfo.sourcePackageFilename();

         // special callback for build result
         system::ProcessCallbacks buildCb = cb;
         buildCb.onExit =  boost::bind(&Build::onBuildForCheckCompleted,
                                       Build::shared_from_this(),
                                       _1,
                                       rCheckCmd,
                                       options,
                                       buildCb);

         // show the user the command
         enqueBuildOutput(rCmd.commandString() + "\n\n");

         // set a success message
         successMessage_ = "R CMD check succeeded\n";

         // run the source build
         module_context::processSupervisor().runCommand(rCmd.shellCommand(),
                                                        options,
                                                        buildCb);
      }
   }

   void onBuildForCheckCompleted(
                         int exitStatus,
                         const RCommand& checkCmd,
                         const core::system::ProcessOptions& checkOptions,
                         const core::system::ProcessCallbacks& checkCb)
   {
      if (exitStatus == EXIT_SUCCESS)
      {
         // show the user the buld command
         enqueBuildOutput(checkCmd.commandString() + "\n\n");

         // run the check
         module_context::processSupervisor().runCommand(checkCmd.shellCommand(),
                                                        checkOptions,
                                                        checkCb);
      }
      else
      {
         boost::format fmt("\nExited with status %1%.\n\n");
         enqueBuildOutput(boost::str(fmt % exitStatus));
         enqueBuildCompleted();
      }
   }

   void executeMakefileBuild(const std::string& type,
                             const core::system::ProcessOptions& options,
                             const core::system::ProcessCallbacks& cb)
   {
      std::string make = "make";
      if (!options_.makefileArgs.empty())
         make += " " + options_.makefileArgs;

      std::string makeClean = make + " clean";

      std::string cmd;
      if (type == "build-all")
      {
         cmd = make;
      }
      else if (type == "clean-all")
      {
         cmd = makeClean;
      }
      else if (type == "rebuild-all")
      {
         cmd = shell_utils::join_and(makeClean, make);
      }

      module_context::processSupervisor().runCommand(cmd,
                                                     options,
                                                     cb);
   }

   void executeCustomBuild(const std::string& type,
                           const FilePath& customScriptPath,
                           const core::system::ProcessOptions& options,
                           const core::system::ProcessCallbacks& cb)
   {
      module_context::processSupervisor().runCommand(
                           shell_utils::ShellCommand(customScriptPath),
                           options,
                           cb);
   }

   FilePath projectPath(const std::string& path)
   {
      if (boost::algorithm::starts_with(path, "~/") ||
          FilePath::isRootPath(path))
      {
         return module_context::resolveAliasedPath(path);
      }
      else
      {
         return projects::projectContext().directory().complete(path);
      }
   }

   void terminateWithError(const std::string& context,
                           const Error& error)
   {
      std::string msg = "Error " + context + ": " + error.summary();
      terminateWithError(msg);
   }

   void terminateWithError(const std::string& msg)
   {
      enqueBuildOutput(msg);
      enqueBuildCompleted();
   }

public:
   virtual ~Build()
   {
   }

   bool isRunning() const { return isRunning_; }

   const std::string& output() const { return output_; }

   void terminate()
   {
      enqueBuildOutput("\n");
      terminationRequested_ = true;
   }

private:
   bool onContinue()
   {
      return !terminationRequested_;
   }

   void onOutput(const std::string& output)
   {
      enqueBuildOutput(output);
   }

   void onCompleted(int exitStatus)
   {
      if (exitStatus != EXIT_SUCCESS)
      {
         boost::format fmt("\nExited with status %1%.\n\n");
         enqueBuildOutput(boost::str(fmt % exitStatus));

         // never restart R after a failed build
         restartR_ = false;
      }
      else
      {
         if (!successMessage_.empty())
            enqueBuildOutput(successMessage_ + "\n");
      }

      enqueBuildCompleted();
   }

   void enqueBuildOutput(const std::string& output)
   {
      output_.append(output);

      ClientEvent event(client_events::kBuildOutput, output);
      module_context::enqueClientEvent(event);
   }

   void enqueBuildCompleted()
   {
      isRunning_ = false;

      // save the restart context if necessary
      if ((projectConfig().buildType == r_util::kBuildTypePackage) && restartR_)
      {
         FilePath packagePath = projectPath(projectConfig().packagePath);
         saveRestartContext(packagePath, output_);
      }

      ClientEvent event(client_events::kBuildCompleted, restartR_);
      module_context::enqueClientEvent(event);
   }

   const r_util::RProjectConfig& projectConfig()
   {
      return projects::projectContext().config();
   }

   std::string buildPackageSuccessMsg(const std::string& type)
   {
      FilePath writtenPath = projectPath(projectConfig().packagePath).parent();
      std::string written = module_context::createAliasedPath(writtenPath);
      if (written == "~")
         written = writtenPath.absolutePath();

      return type + " package written to " + written;
   }

private:
   bool isRunning_;
   bool terminationRequested_;
   std::string output_;
   projects::RProjectBuildOptions options_;
   std::string successMessage_;
   bool restartR_;
};

boost::shared_ptr<Build> s_pBuild;

bool isBuildRunning()
{
   return s_pBuild && s_pBuild->isRunning();
}

Error startBuild(const json::JsonRpcRequest& request,
                 json::JsonRpcResponse* pResponse)
{
   // get type
   std::string type;
   Error error = json::readParam(request.params, 0, &type);
   if (error)
      return error;

   // if we have a build already running then just return false
   if (isBuildRunning())
   {
      pResponse->setResult(false);
   }
   else
   {
      s_pBuild = Build::create(type);
      pResponse->setResult(true);
   }

   return Success();
}

Error terminateBuild(const json::JsonRpcRequest& request,
                     json::JsonRpcResponse* pResponse)
{
   if (isBuildRunning())
      s_pBuild->terminate();

   pResponse->setResult(true);

   return Success();
}

} // anonymous namespace

json::Value buildStateAsJson()
{
   if (s_pBuild)
   {
      json::Object stateJson;
      stateJson["running"] = s_pBuild->isRunning();
      stateJson["output"] = s_pBuild->output();
      return stateJson;
   }
   else
   {
      return json::Value();
   }
}

json::Value buildRestartContext()
{
   return collectRestartContext();
}

Error initialize()
{
   // install rpc methods
   using boost::bind;
   using namespace module_context;
   ExecBlock initBlock ;
   initBlock.addFunctions()
      (bind(registerRpcMethod, "start_build", startBuild))
      (bind(registerRpcMethod, "terminate_build", terminateBuild));
   return initBlock.execute();
}

} // namespace build
} // namespace modules
} // namesapce session

<commit_msg>deliniate command strings in build output<commit_after>/*
 * SessionBuild.cpp
 *
 * Copyright (C) 2009-12 by RStudio, Inc.
 *
 * This program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */

#include "SessionBuild.hpp"

#include <boost/utility.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/format.hpp>
#include <boost/scope_exit.hpp>
#include <boost/enable_shared_from_this.hpp>

#include <core/Exec.hpp>
#include <core/FileSerializer.hpp>
#include <core/text/DcfParser.hpp>
#include <core/system/Process.hpp>
#include <core/system/ShellUtils.hpp>
#include <core/r_util/RPackageInfo.hpp>

#include <session/projects/SessionProjects.hpp>
#include <session/SessionModuleContext.hpp>

using namespace core;

namespace session {
namespace modules {
namespace build {

namespace {

FilePath restartContextFilePath()
{
   return module_context::scopedScratchPath().childPath(
                                                   "build_restart_context");
}

void saveRestartContext(const FilePath& packageDir,
                        const std::string& buildOutput)
{
   // read package info
   r_util::RPackageInfo pkgInfo;
   Error error = pkgInfo.read(packageDir);
   if (error)
   {
      LOG_ERROR(error);
      return;
   }

   // save restart context
   core::Settings restartSettings;
   error = restartSettings.initialize(restartContextFilePath());
   if (error)
   {
      LOG_ERROR(error);
      return;
   }

   restartSettings.beginUpdate();
   restartSettings.set("package_name", pkgInfo.name());
   restartSettings.set("build_output", buildOutput);
   restartSettings.endUpdate();
}

json::Value collectRestartContext()
{
   FilePath restartSettingsPath = restartContextFilePath();
   if (restartSettingsPath.exists())
   {
      // always cleanup the restart context on scope exit
      BOOST_SCOPE_EXIT( (&restartSettingsPath) )
      {
         Error error = restartSettingsPath.remove();
         if (error)
            LOG_ERROR(error);
      }
      BOOST_SCOPE_EXIT_END

      core::Settings restartSettings;
      Error error = restartSettings.initialize(restartContextFilePath());
      if (error)
      {
         LOG_ERROR(error);
         return json::Value();
      }

      json::Object restartJson;
      restartJson["package_name"] = restartSettings.get("package_name");
      restartJson["build_output"] = restartSettings.get("build_output");
      return restartJson;
   }
   else
   {
      return json::Value();
   }
}

// R command invocation -- has two representations, one to be submitted
// (shellCmd_) and one to show the user (cmdString_)
class RCommand
{
public:
   explicit RCommand(const FilePath& rBinDir)
#ifdef _WIN32
      : shellCmd_(rBinDir.childPath("Rcmd.exe"))
#else
      : shellCmd_(rBinDir.childPath("R"))
#endif
   {
#ifdef _WIN32
      cmdString_ = "Rcmd.exe";
#else
      shellCmd_ << "CMD";
      cmdString_ = "R CMD";
#endif
   }

   RCommand& operator<<(const std::string& arg)
   {
      cmdString_ += " " + arg;
      shellCmd_ << arg;
      return *this;
   }

   const std::string& commandString() const
   {
      return cmdString_;
   }

   const shell_utils::ShellCommand& shellCommand() const
   {
      return shellCmd_;
   }

private:
   std::string cmdString_;
   shell_utils::ShellCommand shellCmd_;
};

class Build : boost::noncopyable,
              public boost::enable_shared_from_this<Build>
{
public:
   static boost::shared_ptr<Build> create(const std::string& type)
   {
      boost::shared_ptr<Build> pBuild(new Build());
      pBuild->start(type);
      return pBuild;
   }

private:
   Build()
      : isRunning_(false), terminationRequested_(false), restartR_(false)
   {
   }

   void start(const std::string& type)
   {
      ClientEvent event(client_events::kBuildStarted);
      module_context::enqueClientEvent(event);

      isRunning_ = true;

      // read build options
      Error error = projects::projectContext().readBuildOptions(&options_);
      if (error)
      {
         terminateWithError("reading build options file", error);
         return;
      }

      // callbacks
      core::system::ProcessCallbacks cb;
      cb.onContinue = boost::bind(&Build::onContinue,
                                  Build::shared_from_this());
      cb.onStdout = boost::bind(&Build::onOutput,
                                Build::shared_from_this(), _2);
      cb.onStderr = boost::bind(&Build::onOutput,
                                Build::shared_from_this(), _2);
      cb.onExit =  boost::bind(&Build::onCompleted,
                                Build::shared_from_this(),
                                _1);

      // execute build
      executeBuild(type, cb);
   }

   void executeBuild(const std::string& type,
                     const core::system::ProcessCallbacks& cb)
   {
      // options
      core::system::ProcessOptions options;
      options.terminateChildren = true;
      options.redirectStdErrToStdOut = true;

      const core::r_util::RProjectConfig& config = projectConfig();
      if (config.buildType == r_util::kBuildTypePackage)
      {
         FilePath packagePath = projectPath(config.packagePath);
         options.workingDir = packagePath.parent();
         executePackageBuild(type, packagePath, options, cb);
      }
      else if (config.buildType == r_util::kBuildTypeMakefile)
      {
         FilePath makefilePath = projectPath(config.makefilePath);
         options.workingDir = makefilePath;
         executeMakefileBuild(type, options, cb);
      }
      else if (config.buildType == r_util::kBuildTypeCustom)
      {
         FilePath scriptPath = projectPath(config.customScriptPath);
         options.workingDir = scriptPath.parent();
         executeCustomBuild(type, scriptPath, options, cb);
      }
      else
      {
         terminateWithError("Unrecognized build type: " + config.buildType);
      }
   }

   void executePackageBuild(const std::string& type,
                            const FilePath& packagePath,
                            const core::system::ProcessOptions& options,
                            const core::system::ProcessCallbacks& cb)
   {
      // get package info
      r_util::RPackageInfo pkgInfo;
      Error error = pkgInfo.read(packagePath);
      if (error)
      {
         terminateWithError("Reading package DESCRIPTION", error);
         return;
      }

      // get R bin directory
      FilePath rBinDir;
      error = module_context::rBinDir(&rBinDir);
      if (error)
      {
         terminateWithError("attempting to locate R binary", error);
         return;
      }

      // build command
      if (type == "build-all")
      {
         // restart R after build is completed
         restartR_ = true;

         // build command
         RCommand rCmd(rBinDir);
         rCmd << "INSTALL";
         rCmd << packagePath.filename();

         // show the user the command
         enqueCommandString(rCmd.commandString());

         // run R CMD INSTALL <package-dir>
         module_context::processSupervisor().runCommand(rCmd.shellCommand(),
                                                        options,
                                                        cb);
      }

      else if (type == "build-source-package")
      {
         // compose the build command
         RCommand rCmd(rBinDir);
         rCmd << "build";
         rCmd << packagePath.filename();

         // show the user the command
         enqueCommandString(rCmd.commandString());

         // set a success message
         successMessage_ = buildPackageSuccessMsg("Source");

         // run R CMD build <package-dir>
         module_context::processSupervisor().runCommand(rCmd.shellCommand(),
                                                        options,
                                                        cb);
      }

      else if (type == "build-binary-package")
      {
         // compose the INSTALL --binary
         RCommand rCmd(rBinDir);
         rCmd << "INSTALL";
         rCmd << "--build";
         rCmd << packagePath.filename();

         // show the user the command
         enqueCommandString(rCmd.commandString());

         // set a success message
         successMessage_ = "\n" + buildPackageSuccessMsg("Binary");

         // run R CMD INSTALL --build <package-dir>
         module_context::processSupervisor().runCommand(rCmd.shellCommand(),
                                                        options,
                                                        cb);
      }

      else if (type == "check-package")
      {
         // first build then check

         // compose the build command
         RCommand rCmd(rBinDir);
         rCmd << "build";
         rCmd << packagePath.filename();

         // compose the check command (will be executed by the onExit
         // handler of the build cmd)
         RCommand rCheckCmd(rBinDir);
         rCheckCmd << "check";
         rCheckCmd << pkgInfo.sourcePackageFilename();

         // special callback for build result
         system::ProcessCallbacks buildCb = cb;
         buildCb.onExit =  boost::bind(&Build::onBuildForCheckCompleted,
                                       Build::shared_from_this(),
                                       _1,
                                       rCheckCmd,
                                       options,
                                       buildCb);

         // show the user the command
         enqueCommandString(rCmd.commandString());

         // set a success message
         successMessage_ = "R CMD check succeeded\n";

         // run the source build
         module_context::processSupervisor().runCommand(rCmd.shellCommand(),
                                                        options,
                                                        buildCb);
      }
   }

   void onBuildForCheckCompleted(
                         int exitStatus,
                         const RCommand& checkCmd,
                         const core::system::ProcessOptions& checkOptions,
                         const core::system::ProcessCallbacks& checkCb)
   {
      if (exitStatus == EXIT_SUCCESS)
      {
         // show the user the buld command
         enqueCommandString(checkCmd.commandString());

         // run the check
         module_context::processSupervisor().runCommand(checkCmd.shellCommand(),
                                                        checkOptions,
                                                        checkCb);
      }
      else
      {
         boost::format fmt("\nExited with status %1%.\n\n");
         enqueBuildOutput(boost::str(fmt % exitStatus));
         enqueBuildCompleted();
      }
   }

   void executeMakefileBuild(const std::string& type,
                             const core::system::ProcessOptions& options,
                             const core::system::ProcessCallbacks& cb)
   {
      std::string make = "make";
      if (!options_.makefileArgs.empty())
         make += " " + options_.makefileArgs;

      std::string makeClean = make + " clean";

      std::string cmd;
      if (type == "build-all")
      {
         cmd = make;
      }
      else if (type == "clean-all")
      {
         cmd = makeClean;
      }
      else if (type == "rebuild-all")
      {
         cmd = shell_utils::join_and(makeClean, make);
      }

      module_context::processSupervisor().runCommand(cmd,
                                                     options,
                                                     cb);
   }

   void executeCustomBuild(const std::string& type,
                           const FilePath& customScriptPath,
                           const core::system::ProcessOptions& options,
                           const core::system::ProcessCallbacks& cb)
   {
      module_context::processSupervisor().runCommand(
                           shell_utils::ShellCommand(customScriptPath),
                           options,
                           cb);
   }

   FilePath projectPath(const std::string& path)
   {
      if (boost::algorithm::starts_with(path, "~/") ||
          FilePath::isRootPath(path))
      {
         return module_context::resolveAliasedPath(path);
      }
      else
      {
         return projects::projectContext().directory().complete(path);
      }
   }

   void terminateWithError(const std::string& context,
                           const Error& error)
   {
      std::string msg = "Error " + context + ": " + error.summary();
      terminateWithError(msg);
   }

   void terminateWithError(const std::string& msg)
   {
      enqueBuildOutput(msg);
      enqueBuildCompleted();
   }

public:
   virtual ~Build()
   {
   }

   bool isRunning() const { return isRunning_; }

   const std::string& output() const { return output_; }

   void terminate()
   {
      enqueBuildOutput("\n");
      terminationRequested_ = true;
   }

private:
   bool onContinue()
   {
      return !terminationRequested_;
   }

   void onOutput(const std::string& output)
   {
      enqueBuildOutput(output);
   }

   void onCompleted(int exitStatus)
   {
      if (exitStatus != EXIT_SUCCESS)
      {
         boost::format fmt("\nExited with status %1%.\n\n");
         enqueBuildOutput(boost::str(fmt % exitStatus));

         // never restart R after a failed build
         restartR_ = false;
      }
      else
      {
         if (!successMessage_.empty())
            enqueBuildOutput(successMessage_ + "\n");
      }

      enqueBuildCompleted();
   }

   void enqueBuildOutput(const std::string& output)
   {
      output_.append(output);

      ClientEvent event(client_events::kBuildOutput, output);
      module_context::enqueClientEvent(event);
   }

   void enqueCommandString(const std::string& cmd)
   {
      enqueBuildOutput("==> " + cmd + "\n\n");
   }

   void enqueBuildCompleted()
   {
      isRunning_ = false;

      // save the restart context if necessary
      if ((projectConfig().buildType == r_util::kBuildTypePackage) && restartR_)
      {
         FilePath packagePath = projectPath(projectConfig().packagePath);
         saveRestartContext(packagePath, output_);
      }

      ClientEvent event(client_events::kBuildCompleted, restartR_);
      module_context::enqueClientEvent(event);
   }

   const r_util::RProjectConfig& projectConfig()
   {
      return projects::projectContext().config();
   }

   std::string buildPackageSuccessMsg(const std::string& type)
   {
      FilePath writtenPath = projectPath(projectConfig().packagePath).parent();
      std::string written = module_context::createAliasedPath(writtenPath);
      if (written == "~")
         written = writtenPath.absolutePath();

      return type + " package written to " + written;
   }

private:
   bool isRunning_;
   bool terminationRequested_;
   std::string output_;
   projects::RProjectBuildOptions options_;
   std::string successMessage_;
   bool restartR_;
};

boost::shared_ptr<Build> s_pBuild;

bool isBuildRunning()
{
   return s_pBuild && s_pBuild->isRunning();
}

Error startBuild(const json::JsonRpcRequest& request,
                 json::JsonRpcResponse* pResponse)
{
   // get type
   std::string type;
   Error error = json::readParam(request.params, 0, &type);
   if (error)
      return error;

   // if we have a build already running then just return false
   if (isBuildRunning())
   {
      pResponse->setResult(false);
   }
   else
   {
      s_pBuild = Build::create(type);
      pResponse->setResult(true);
   }

   return Success();
}

Error terminateBuild(const json::JsonRpcRequest& request,
                     json::JsonRpcResponse* pResponse)
{
   if (isBuildRunning())
      s_pBuild->terminate();

   pResponse->setResult(true);

   return Success();
}

} // anonymous namespace

json::Value buildStateAsJson()
{
   if (s_pBuild)
   {
      json::Object stateJson;
      stateJson["running"] = s_pBuild->isRunning();
      stateJson["output"] = s_pBuild->output();
      return stateJson;
   }
   else
   {
      return json::Value();
   }
}

json::Value buildRestartContext()
{
   return collectRestartContext();
}

Error initialize()
{
   // install rpc methods
   using boost::bind;
   using namespace module_context;
   ExecBlock initBlock ;
   initBlock.addFunctions()
      (bind(registerRpcMethod, "start_build", startBuild))
      (bind(registerRpcMethod, "terminate_build", terminateBuild));
   return initBlock.execute();
}

} // namespace build
} // namespace modules
} // namesapce session

<|endoftext|>


<commit_before>#include "third_party/tensorflow/core/framework/op_kernel.h"
#include "third_party/tensorflow/core/framework/shape_inference.h"
#include "third_party/tensorflow/core/framework/tensor.h"
#include "third_party/tensorflow/core/framework/types.h"

namespace tensor2tensor {
namespace {

using ::tensorflow::DEVICE_CPU;
using ::tensorflow::OpKernel;
using ::tensorflow::OpKernelConstruction;
using ::tensorflow::OpKernelContext;
using ::tensorflow::Status;
using ::tensorflow::Tensor;
using ::tensorflow::TensorShape;
using ::tensorflow::shape_inference::InferenceContext;

// TODO(noam): this op packs a dataset of pairs of sequences (inputs, targets)
// Generalize later to an arbitrary number of sequences.
REGISTER_OP("PackSequences2")
    .Input("inputs: int64")
    .Input("targets: int64")
    .Input("inputs_max_length: int32")
    .Input("targets_max_length: int32")
    .Output("inputs_packed: int64")
    .Output("inputs_segmentation: int32")
    .Output("inputs_position: int32")
    .Output("targets_packed: int64")
    .Output("targets_segmentation: int32")
    .Output("targets_position: int32")
    .SetShapeFn([](InferenceContext* ctx) {
                  for (int i=0; i < ctx->num_outputs(); i++) {
                    ctx->set_output(i, ctx->Matrix(ctx->UnknownDim(),
                                                   ctx->UnknownDim()));
                  }
                  return Status::OK();
                });

class PackSequences2Op : public OpKernel {
 public:
  explicit PackSequences2Op(
      OpKernelConstruction* ctx) : OpKernel(ctx) {
  }

  void Compute(OpKernelContext* ctx) override {
    auto inputs = ctx->input(0).matrix<int64>();
    auto targets = ctx->input(1).matrix<int64>();
    int inputs_max_length = ctx->input(2).scalar<int32>()();
    int targets_max_length = ctx->input(3).scalar<int32>()();
    int n = inputs.dimension(0);
    std::vector<int> inputs_lengths(n);
    std::vector<int> targets_lengths(n);
    int padded_inputs_length =
        std::min(static_cast<int>(inputs.dimension(1)), inputs_max_length);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < padded_inputs_length; j++) {
          if (inputs(i, j) != 0)
            inputs_lengths[i]++;
      }
    }
    int padded_targets_length =
        std::min(static_cast<int>(targets.dimension(1)), targets_max_length);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < padded_targets_length; j++) {
          if (targets(i, j) != 0)
            targets_lengths[i]++;
      }
    }
    int num_combined = 0;
    std::vector<int> combined_inputs_length;
    std::vector<int> combined_targets_length;
    std::vector<std::vector<int> > combined_sequence_ids;
    for (int seq_id = 0; seq_id < n; seq_id++) {
      int inputs_length = inputs_lengths[seq_id];
      int targets_length = targets_lengths[seq_id];
      for (int combined_id = std::max(0, num_combined - 10); true;
           combined_id++) {
        if (combined_id == num_combined) {
          combined_inputs_length.push_back(inputs_length);
          combined_targets_length.push_back(targets_length);
          combined_sequence_ids.push_back(std::vector<int>(1, seq_id));
          num_combined++;
          break;
        } else if (
            (combined_inputs_length[combined_id] + inputs_length
             <= inputs_max_length) &&
            (combined_targets_length[combined_id] + targets_length
             <= targets_max_length)) {
          combined_inputs_length[combined_id] += inputs_length;
          combined_targets_length[combined_id] += targets_length;
          combined_sequence_ids[combined_id].push_back(seq_id);
          break;
        }
      }
    }

    auto output_shape_inputs = TensorShape(
        {static_cast<int64>(num_combined),
         static_cast<int64>(inputs_max_length)});
    auto output_shape_targets = TensorShape(
        {static_cast<int64>(num_combined),
         static_cast<int64>(targets_max_length)});

    Tensor* inputs_packed;
    OP_REQUIRES_OK(ctx, ctx->allocate_output(
        0, output_shape_inputs, &inputs_packed));
    auto inputs_packed_m = inputs_packed->matrix<int64>();
    inputs_packed_m.setZero();

    Tensor* inputs_segmentation;
    OP_REQUIRES_OK(
        ctx, ctx->allocate_output(
            1, output_shape_inputs, &inputs_segmentation));
    auto inputs_segmentation_m = inputs_segmentation->matrix<int32>();
    inputs_segmentation_m.setZero();

    Tensor* inputs_position;
    OP_REQUIRES_OK(
        ctx, ctx->allocate_output(2, output_shape_inputs, &inputs_position));
    auto inputs_position_m = inputs_position->matrix<int32>();
    inputs_position_m.setZero();

    Tensor* targets_packed;
    OP_REQUIRES_OK(ctx, ctx->allocate_output(
        3, output_shape_targets, &targets_packed));
    auto targets_packed_m = targets_packed->matrix<int64>();
    targets_packed_m.setZero();

    Tensor* targets_segmentation;
    OP_REQUIRES_OK(
        ctx, ctx->allocate_output(
            4, output_shape_targets, &targets_segmentation));
    auto targets_segmentation_m = targets_segmentation->matrix<int32>();
    targets_segmentation_m.setZero();

    Tensor* targets_position;
    OP_REQUIRES_OK(
        ctx, ctx->allocate_output(5, output_shape_targets, &targets_position));
    auto targets_position_m = targets_position->matrix<int32>();
    targets_position_m.setZero();

    for (int combined_id = 0; combined_id < num_combined; combined_id++) {
      int inputs_pos = 0;
      int targets_pos = 0;
      for (int i=0; i < combined_sequence_ids[combined_id].size(); i++) {
        int seq_id = combined_sequence_ids[combined_id][i];
        for (int j=0; j < inputs_lengths[seq_id]; j++) {
          inputs_packed_m(combined_id, inputs_pos) = inputs(seq_id, j);
          inputs_segmentation_m(combined_id, inputs_pos) = i + 1;
          inputs_position_m(combined_id, inputs_pos) = j;
          inputs_pos++;
        }
        for (int j=0; j < targets_lengths[seq_id]; j++) {
          targets_packed_m(combined_id, targets_pos) = targets(seq_id, j);
          targets_segmentation_m(combined_id, targets_pos) = i + 1;
          targets_position_m(combined_id, targets_pos) = j;
          targets_pos++;
        }
      }
    }
  }
};

REGISTER_KERNEL_BUILDER(Name("PackSequences2").Device(DEVICE_CPU),
                        PackSequences2Op);

}  // namespace
}  // namespace tensor2tensor
<commit_msg>increase window size of partially-built sequences in c++ packing op - results in more efficient packing.<commit_after>#include "third_party/tensorflow/core/framework/op_kernel.h"
#include "third_party/tensorflow/core/framework/shape_inference.h"
#include "third_party/tensorflow/core/framework/tensor.h"
#include "third_party/tensorflow/core/framework/types.h"

namespace tensor2tensor {
namespace {

using ::tensorflow::DEVICE_CPU;
using ::tensorflow::OpKernel;
using ::tensorflow::OpKernelConstruction;
using ::tensorflow::OpKernelContext;
using ::tensorflow::Status;
using ::tensorflow::Tensor;
using ::tensorflow::TensorShape;
using ::tensorflow::shape_inference::InferenceContext;

// TODO(noam): this op packs a dataset of pairs of sequences (inputs, targets)
// Generalize later to an arbitrary number of sequences.
REGISTER_OP("PackSequences2")
    .Input("inputs: int64")
    .Input("targets: int64")
    .Input("inputs_max_length: int32")
    .Input("targets_max_length: int32")
    .Output("inputs_packed: int64")
    .Output("inputs_segmentation: int32")
    .Output("inputs_position: int32")
    .Output("targets_packed: int64")
    .Output("targets_segmentation: int32")
    .Output("targets_position: int32")
    .SetShapeFn([](InferenceContext* ctx) {
                  for (int i=0; i < ctx->num_outputs(); i++) {
                    ctx->set_output(i, ctx->Matrix(ctx->UnknownDim(),
                                                   ctx->UnknownDim()));
                  }
                  return Status::OK();
                });

class PackSequences2Op : public OpKernel {
 public:
  explicit PackSequences2Op(
      OpKernelConstruction* ctx) : OpKernel(ctx) {
  }

  void Compute(OpKernelContext* ctx) override {
    auto inputs = ctx->input(0).matrix<int64>();
    auto targets = ctx->input(1).matrix<int64>();
    int inputs_max_length = ctx->input(2).scalar<int32>()();
    int targets_max_length = ctx->input(3).scalar<int32>()();
    int n = inputs.dimension(0);
    std::vector<int> inputs_lengths(n);
    std::vector<int> targets_lengths(n);
    int padded_inputs_length =
        std::min(static_cast<int>(inputs.dimension(1)), inputs_max_length);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < padded_inputs_length; j++) {
          if (inputs(i, j) != 0)
            inputs_lengths[i]++;
      }
    }
    int padded_targets_length =
        std::min(static_cast<int>(targets.dimension(1)), targets_max_length);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < padded_targets_length; j++) {
          if (targets(i, j) != 0)
            targets_lengths[i]++;
      }
    }
    int num_combined = 0;
    std::vector<int> combined_inputs_length;
    std::vector<int> combined_targets_length;
    std::vector<std::vector<int> > combined_sequence_ids;
    for (int seq_id = 0; seq_id < n; seq_id++) {
      int inputs_length = inputs_lengths[seq_id];
      int targets_length = targets_lengths[seq_id];
      for (int combined_id = std::max(0, num_combined - 1000); true;
           combined_id++) {
        if (combined_id == num_combined) {
          combined_inputs_length.push_back(inputs_length);
          combined_targets_length.push_back(targets_length);
          combined_sequence_ids.push_back(std::vector<int>(1, seq_id));
          num_combined++;
          break;
        } else if (
            (combined_inputs_length[combined_id] + inputs_length
             <= inputs_max_length) &&
            (combined_targets_length[combined_id] + targets_length
             <= targets_max_length)) {
          combined_inputs_length[combined_id] += inputs_length;
          combined_targets_length[combined_id] += targets_length;
          combined_sequence_ids[combined_id].push_back(seq_id);
          break;
        }
      }
    }

    auto output_shape_inputs = TensorShape(
        {static_cast<int64>(num_combined),
         static_cast<int64>(inputs_max_length)});
    auto output_shape_targets = TensorShape(
        {static_cast<int64>(num_combined),
         static_cast<int64>(targets_max_length)});

    Tensor* inputs_packed;
    OP_REQUIRES_OK(ctx, ctx->allocate_output(
        0, output_shape_inputs, &inputs_packed));
    auto inputs_packed_m = inputs_packed->matrix<int64>();
    inputs_packed_m.setZero();

    Tensor* inputs_segmentation;
    OP_REQUIRES_OK(
        ctx, ctx->allocate_output(
            1, output_shape_inputs, &inputs_segmentation));
    auto inputs_segmentation_m = inputs_segmentation->matrix<int32>();
    inputs_segmentation_m.setZero();

    Tensor* inputs_position;
    OP_REQUIRES_OK(
        ctx, ctx->allocate_output(2, output_shape_inputs, &inputs_position));
    auto inputs_position_m = inputs_position->matrix<int32>();
    inputs_position_m.setZero();

    Tensor* targets_packed;
    OP_REQUIRES_OK(ctx, ctx->allocate_output(
        3, output_shape_targets, &targets_packed));
    auto targets_packed_m = targets_packed->matrix<int64>();
    targets_packed_m.setZero();

    Tensor* targets_segmentation;
    OP_REQUIRES_OK(
        ctx, ctx->allocate_output(
            4, output_shape_targets, &targets_segmentation));
    auto targets_segmentation_m = targets_segmentation->matrix<int32>();
    targets_segmentation_m.setZero();

    Tensor* targets_position;
    OP_REQUIRES_OK(
        ctx, ctx->allocate_output(5, output_shape_targets, &targets_position));
    auto targets_position_m = targets_position->matrix<int32>();
    targets_position_m.setZero();

    for (int combined_id = 0; combined_id < num_combined; combined_id++) {
      int inputs_pos = 0;
      int targets_pos = 0;
      for (int i=0; i < combined_sequence_ids[combined_id].size(); i++) {
        int seq_id = combined_sequence_ids[combined_id][i];
        for (int j=0; j < inputs_lengths[seq_id]; j++) {
          inputs_packed_m(combined_id, inputs_pos) = inputs(seq_id, j);
          inputs_segmentation_m(combined_id, inputs_pos) = i + 1;
          inputs_position_m(combined_id, inputs_pos) = j;
          inputs_pos++;
        }
        for (int j=0; j < targets_lengths[seq_id]; j++) {
          targets_packed_m(combined_id, targets_pos) = targets(seq_id, j);
          targets_segmentation_m(combined_id, targets_pos) = i + 1;
          targets_position_m(combined_id, targets_pos) = j;
          targets_pos++;
        }
      }
    }
  }
};

REGISTER_KERNEL_BUILDER(Name("PackSequences2").Device(DEVICE_CPU),
                        PackSequences2Op);

}  // namespace
}  // namespace tensor2tensor
<|endoftext|>


<commit_before>
/**
 * This file is part of the boostcache package.
 *
 * (c) Azat Khuzhin <a3at.mail@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

#include "session.h"
#include "util/log.h"

#include <boost/bind.hpp>
#include <boost/spirit/include/qi.hpp>
#include <sstream>

using namespace boost::spirit::qi;

void Session::start()
{
    asyncRead();
}

void Session::asyncRead()
{
    m_socket.async_read_some(boost::asio::buffer(m_buffer, MAX_BUFFER_LENGTH),
                             boost::bind(&Session::handleRead, this,
                                         boost::asio::placeholders::error,
                                         boost::asio::placeholders::bytes_transferred));
}

void Session::asyncWrite(const std::string& message)
{
    boost::asio::async_write(m_socket,
                             boost::asio::buffer(message),
                             boost::bind(&Session::handleWrite, this,
                                         boost::asio::placeholders::error));
}

void Session::handleRead(const boost::system::error_code& error, size_t /* bytesTransferred */)
{
    if (error) {
        delete this;
        return;
    }

    handleReadParseCommand();
}

void Session::handleWrite(const boost::system::error_code& error)
{
    if (error) {
        delete this;
        return;
    }

    asyncRead();
}

/**
 * TODO: speedup parsing
 * TODO: more error-friendly parsing
 * TODO: add logging before every reset() call
 */
void Session::handleReadParseCommand()
{
    LOG(debug) << "Try to read/parser command " << m_buffer
               << "with " << m_numberOfArguments << " arguments, "
               "for " << this;
    m_commandString += m_buffer;

    std::string line;
    std::istringstream stream(m_commandString);
    stream.seekg(m_commandOffset);

    // Number of arguments
    if (m_numberOfArguments < 0) {
        std::getline(stream, line);
        parse(line.begin(), line.end(),
              '*' >> int_ >> "\r",
              m_numberOfArguments);

        if (m_numberOfArguments < 0) {
            LOG(debug) << "Don't have number of arguments, for " << this;

            reset();
            asyncRead();
            return;
        }

        commandArguments.reserve(m_numberOfArguments);
        m_numberOfArgumentsLeft = m_numberOfArguments;
        m_commandOffset = stream.tellg();

        LOG(info) << "Have " << m_numberOfArguments << " number of arguments, "
                  << "for " << this;
    }

    char crLf[2];
    char *argument = NULL;
    int argumentLength = 0;
    while (m_numberOfArgumentsLeft && std::getline(stream, line)) {
        if (!parse(line.begin(), line.end(),
                   '$' >> int_ >> "\r",
                   m_lastArgumentLength)
        ) {
            LOG(debug) << "Can't find valid argument length, for " << this;
            break;
        }
        LOG(debug) << "Reading " << m_lastArgumentLength << " bytes, for " << this;

        if (argumentLength < m_lastArgumentLength) {
            argument = (char *)realloc(argument, argumentLength + 1 /* NULL byte */
                                       + m_lastArgumentLength);
            argumentLength += m_lastArgumentLength;
        }
        stream.read(argument, m_lastArgumentLength);
        argument[m_lastArgumentLength] = 0;
        if (!stream.good()) {
            /**
             * TODO: add some internal counters for failover,
             * or smth like this
             */
            if (stream.bad()) {
                LOG(debug) << "Bad stream, for " << this;
                reset();
            }
            break;
        }
        // Read CRLF separator
        stream.read(crLf, 2);
        if (!stream.good()) {
            /**
             * TODO: add some internal counters for failover,
             * or smth like this
             */
            if (stream.bad()) {
                LOG(debug) << "Bad stream, for " << this;
                reset();
            }
            break;
        }
        if (memcmp(crLf, "\r\n", 2) != 0) {
            LOG(debug) << "Malfomed end of argument, for " << this;
            reset();
            break;
        }
        // Save command argument
        LOG(debug) << "Saving " << argument << " argument, for " << this;
        commandArguments.push_back(argument);
        m_lastArgumentLength = -1;

        // Update some counters/offsets
        --m_numberOfArgumentsLeft;
        m_commandOffset = stream.tellg();
    }

    if (!m_numberOfArgumentsLeft) {
        handleCommand();
        // Will be called asyncRead() in write callback
    } else {
        asyncRead();
    }
}

void Session::handleCommand()
{
    LOG(info) << "Handling new command, for " << this;

    writeCommand();

    reset();
}

void Session::writeCommand()
{
    std::string arguments;
    int i;
    for_each(commandArguments.begin(), commandArguments.end(),
             [&arguments, &i] (std::string argument)
             {
                  arguments += ++i;
                  arguments += " ";
                  arguments += argument;
                  arguments += "\n";
             }
    );

    asyncWrite(arguments);
}

void Session::reset()
{
    m_commandString = "";
    m_commandOffset = 0;
    m_numberOfArguments = -1;
    m_numberOfArgumentsLeft = -1;
    m_lastArgumentLength = -1;

    commandArguments.clear();
}<commit_msg>[net/session] we must reset internal counters when parsing of argLen fail<commit_after>
/**
 * This file is part of the boostcache package.
 *
 * (c) Azat Khuzhin <a3at.mail@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

#include "session.h"
#include "util/log.h"

#include <boost/bind.hpp>
#include <boost/spirit/include/qi.hpp>
#include <sstream>

using namespace boost::spirit::qi;

void Session::start()
{
    asyncRead();
}

void Session::asyncRead()
{
    m_socket.async_read_some(boost::asio::buffer(m_buffer, MAX_BUFFER_LENGTH),
                             boost::bind(&Session::handleRead, this,
                                         boost::asio::placeholders::error,
                                         boost::asio::placeholders::bytes_transferred));
}

void Session::asyncWrite(const std::string& message)
{
    boost::asio::async_write(m_socket,
                             boost::asio::buffer(message),
                             boost::bind(&Session::handleWrite, this,
                                         boost::asio::placeholders::error));
}

void Session::handleRead(const boost::system::error_code& error, size_t /* bytesTransferred */)
{
    if (error) {
        delete this;
        return;
    }

    handleReadParseCommand();
}

void Session::handleWrite(const boost::system::error_code& error)
{
    if (error) {
        delete this;
        return;
    }

    asyncRead();
}

/**
 * TODO: speedup parsing
 * TODO: more error-friendly parsing
 * TODO: add logging before every reset() call
 */
void Session::handleReadParseCommand()
{
    LOG(debug) << "Try to read/parser command " << m_buffer
               << "with " << m_numberOfArguments << " arguments, "
               "for " << this;
    m_commandString += m_buffer;

    std::string line;
    std::istringstream stream(m_commandString);
    stream.seekg(m_commandOffset);

    // Number of arguments
    if (m_numberOfArguments < 0) {
        std::getline(stream, line);
        parse(line.begin(), line.end(),
              '*' >> int_ >> "\r",
              m_numberOfArguments);

        if (m_numberOfArguments < 0) {
            LOG(debug) << "Don't have number of arguments, for " << this;

            reset();
            asyncRead();
            return;
        }

        commandArguments.reserve(m_numberOfArguments);
        m_numberOfArgumentsLeft = m_numberOfArguments;
        m_commandOffset = stream.tellg();

        LOG(info) << "Have " << m_numberOfArguments << " number of arguments, "
                  << "for " << this;
    }

    char crLf[2];
    char *argument = NULL;
    int argumentLength = 0;
    while (m_numberOfArgumentsLeft && std::getline(stream, line)) {
        if (!parse(line.begin(), line.end(),
                   '$' >> int_ >> "\r",
                   m_lastArgumentLength)
        ) {
            LOG(debug) << "Can't find valid argument length, for " << this;
            reset();
            break;
        }
        LOG(debug) << "Reading " << m_lastArgumentLength << " bytes, for " << this;

        if (argumentLength < m_lastArgumentLength) {
            argument = (char *)realloc(argument, argumentLength + 1 /* NULL byte */
                                       + m_lastArgumentLength);
            argumentLength += m_lastArgumentLength;
        }
        stream.read(argument, m_lastArgumentLength);
        argument[m_lastArgumentLength] = 0;
        if (!stream.good()) {
            /**
             * TODO: add some internal counters for failover,
             * or smth like this
             */
            if (stream.bad()) {
                LOG(debug) << "Bad stream, for " << this;
                reset();
            }
            break;
        }
        // Read CRLF separator
        stream.read(crLf, 2);
        if (!stream.good()) {
            /**
             * TODO: add some internal counters for failover,
             * or smth like this
             */
            if (stream.bad()) {
                LOG(debug) << "Bad stream, for " << this;
                reset();
            }
            break;
        }
        if (memcmp(crLf, "\r\n", 2) != 0) {
            LOG(debug) << "Malfomed end of argument, for " << this;
            reset();
            break;
        }
        // Save command argument
        LOG(debug) << "Saving " << argument << " argument, for " << this;
        commandArguments.push_back(argument);
        m_lastArgumentLength = -1;

        // Update some counters/offsets
        --m_numberOfArgumentsLeft;
        m_commandOffset = stream.tellg();
    }

    if (!m_numberOfArgumentsLeft) {
        handleCommand();
        // Will be called asyncRead() in write callback
    } else {
        asyncRead();
    }
}

void Session::handleCommand()
{
    LOG(info) << "Handling new command, for " << this;

    writeCommand();

    reset();
}

void Session::writeCommand()
{
    std::string arguments;
    int i;
    for_each(commandArguments.begin(), commandArguments.end(),
             [&arguments, &i] (std::string argument)
             {
                  arguments += ++i;
                  arguments += " ";
                  arguments += argument;
                  arguments += "\n";
             }
    );

    asyncWrite(arguments);
}

void Session::reset()
{
    m_commandString = "";
    m_commandOffset = 0;
    m_numberOfArguments = -1;
    m_numberOfArgumentsLeft = -1;
    m_lastArgumentLength = -1;

    commandArguments.clear();
}<|endoftext|>


<commit_before>#ifdef DARWIN
#include <sys/sysctl.h>
#endif // DARWIN

#ifdef LINUX
#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#endif // LINUX

#if defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#if defined(BSD)
#include <sys/sysctl.h>
#endif
#endif

#include <stdint.h>

#include <cxxu/system/info.hpp>
#include <cxxu/logging.hpp>

namespace cxxu {
namespace system {

info::info()
{
    load();
}

info::~info()
{}

uint32_t
info::logical_cpus() const
{ return logical_cpus_; }

uint64_t
info::physical_memory() const
{ return physical_memory_; }

void
info::load()
{
    cpu_info();
    mem_info();
}

void
info::cpu_info()
{
    logical_cpus_ = 1;
    bool ok = true;

#ifdef DARWIN
    std::size_t length = sizeof(logical_cpus_);

    if (sysctlbyname("hw.logicalcpu_max", &logical_cpus_, &length, NULL, 0) != 0) {
        ok = false;
    }
#endif // DARWIN

#ifdef LINUX
    long nprocs = sysconf(_SC_NPROCESSORS_ONLN);

    if (nprocs == -1) {
        ok = false;
    }

    logical_cpus_ = static_cast<uint32_t>(nprocs);
#endif // LINUX

    if (!ok) {
        CXXU_DIE("failed to get logical cpu count");
    }
}

void
info::mem_info()
{
    physical_memory_ = 0;

#if defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
    /* UNIX variants. ------------------------------------------- */
    /* Prefer sysctl() over sysconf() except sysctl() HW_REALMEM and HW_PHYSMEM */

#if defined(CTL_HW) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64))
    int mib[2];
    mib[0] = CTL_HW;
        #if defined(HW_MEMSIZE)
    mib[1] = HW_MEMSIZE;            /* OSX. --------------------- */
        #elif defined(HW_PHYSMEM64)
    mib[1] = HW_PHYSMEM64;          /* NetBSD, OpenBSD. --------- */
        #endif
    int64_t size = 0;               /* 64-bit */
    size_t len = sizeof( size );
    if ( sysctl( mib, 2, &size, &len, NULL, 0 ) == 0 ) {
        physical_memory_ (uint64_t)size;
    }

#elif defined(_SC_AIX_REALMEM)
    /* AIX. ----------------------------------------------------- */
    physical_memory_ = (uint64_t)sysconf( _SC_AIX_REALMEM ) * (uint64_t)1024L;

#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
    /* FreeBSD, Linux, OpenBSD, and Solaris. -------------------- */
    physical_memory_ = (uint64_t)sysconf( _SC_PHYS_PAGES ) * (uint64_t)sysconf( _SC_PAGESIZE );

#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGE_SIZE)
    /* Legacy. -------------------------------------------------- */
    physical_memory_ = (uint64_t)sysconf( _SC_PHYS_PAGES ) * (size_t)sysconf( _SC_PAGE_SIZE );

#elif defined(CTL_HW) && (defined(HW_PHYSMEM) || defined(HW_REALMEM))
    /* DragonFly BSD, FreeBSD, NetBSD, OpenBSD, and OSX. -------- */
    int mib[2];
    mib[0] = CTL_HW;
#if defined(HW_REALMEM)
    mib[1] = HW_REALMEM;        /* FreeBSD. ----------------- */
#elif defined(HW_PYSMEM)
    mib[1] = HW_PHYSMEM;        /* Others. ------------------ */
#endif
    unsigned int size = 0;      /* 32-bit */
    size_t len = sizeof( size );
    if ( sysctl( mib, 2, &size, &len, NULL, 0 ) == 0 ) {
        physical_memory_ = (uint64_t)size;
    }

#endif /* sysctl and sysconf variants */
#endif
    if (physical_memory_ == 0) {
        CXXU_DIE("failed to get physical memory size");
    }
}

} // namespace system
} // namespace cxxu
<commit_msg>added missing include<commit_after>#ifdef DARWIN
#include <sys/sysctl.h>
#endif // DARWIN

#ifdef LINUX
#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#endif // LINUX

#if defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#if defined(BSD)
#include <sys/sysctl.h>
#endif
#endif

#include <stdint.h>

#include <cxxu/system/info.hpp>
#include <cxxu/logging.hpp>

namespace cxxu {
namespace system {

info::info()
{
    load();
}

info::~info()
{}

uint32_t
info::logical_cpus() const
{ return logical_cpus_; }

uint64_t
info::physical_memory() const
{ return physical_memory_; }

void
info::load()
{
    cpu_info();
    mem_info();
}

void
info::cpu_info()
{
    logical_cpus_ = 1;
    bool ok = true;

#ifdef DARWIN
    std::size_t length = sizeof(logical_cpus_);

    if (sysctlbyname("hw.logicalcpu_max", &logical_cpus_, &length, NULL, 0) != 0) {
        ok = false;
    }
#endif // DARWIN

#ifdef LINUX
    long nprocs = sysconf(_SC_NPROCESSORS_ONLN);

    if (nprocs == -1) {
        ok = false;
    }

    logical_cpus_ = static_cast<uint32_t>(nprocs);
#endif // LINUX

    if (!ok) {
        CXXU_DIE("failed to get logical cpu count");
    }
}

void
info::mem_info()
{
    physical_memory_ = 0;

#if defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
    /* UNIX variants. ------------------------------------------- */
    /* Prefer sysctl() over sysconf() except sysctl() HW_REALMEM and HW_PHYSMEM */

#if defined(CTL_HW) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64))
    int mib[2];
    mib[0] = CTL_HW;
        #if defined(HW_MEMSIZE)
    mib[1] = HW_MEMSIZE;            /* OSX. --------------------- */
        #elif defined(HW_PHYSMEM64)
    mib[1] = HW_PHYSMEM64;          /* NetBSD, OpenBSD. --------- */
        #endif
    int64_t size = 0;               /* 64-bit */
    size_t len = sizeof( size );
    if ( sysctl( mib, 2, &size, &len, NULL, 0 ) == 0 ) {
        physical_memory_ = (uint64_t)size;
    }

#elif defined(_SC_AIX_REALMEM)
    /* AIX. ----------------------------------------------------- */
    physical_memory_ = (uint64_t)sysconf( _SC_AIX_REALMEM ) * (uint64_t)1024L;

#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
    /* FreeBSD, Linux, OpenBSD, and Solaris. -------------------- */
    physical_memory_ = (uint64_t)sysconf( _SC_PHYS_PAGES ) * (uint64_t)sysconf( _SC_PAGESIZE );

#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGE_SIZE)
    /* Legacy. -------------------------------------------------- */
    physical_memory_ = (uint64_t)sysconf( _SC_PHYS_PAGES ) * (size_t)sysconf( _SC_PAGE_SIZE );

#elif defined(CTL_HW) && (defined(HW_PHYSMEM) || defined(HW_REALMEM))
    /* DragonFly BSD, FreeBSD, NetBSD, OpenBSD, and OSX. -------- */
    int mib[2];
    mib[0] = CTL_HW;
#if defined(HW_REALMEM)
    mib[1] = HW_REALMEM;        /* FreeBSD. ----------------- */
#elif defined(HW_PYSMEM)
    mib[1] = HW_PHYSMEM;        /* Others. ------------------ */
#endif
    unsigned int size = 0;      /* 32-bit */
    size_t len = sizeof( size );
    if ( sysctl( mib, 2, &size, &len, NULL, 0 ) == 0 ) {
        physical_memory_ = (uint64_t)size;
    }

#endif /* sysctl and sysconf variants */
#endif
    if (physical_memory_ == 0) {
        CXXU_DIE("failed to get physical memory size");
    }
}

} // namespace system
} // namespace cxxu
<|endoftext|>


<commit_before>/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

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

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

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

#include "tensorflow/compiler/mlir/tensorflow/utils/error_util.h"

#include "tensorflow/core/lib/core/errors.h"

namespace mlir {

StatusScopedDiagnosticHandler::StatusScopedDiagnosticHandler(
    MLIRContext* context, bool propagate)
    : SourceMgrDiagnosticHandler(source_mgr_, context, diag_stream_),
      diag_stream_(diag_str_),
      propagate_(propagate) {
  setHandler([this](Diagnostic& diag) { return this->handler(&diag); });
}

StatusScopedDiagnosticHandler::~StatusScopedDiagnosticHandler() {
  // Verify errors were consumed and re-register old handler.
  bool all_errors_produced_were_consumed = ok();
  DCHECK(all_errors_produced_were_consumed) << "Error status not consumed:\n"
                                            << diag_str_;
}

bool StatusScopedDiagnosticHandler::ok() const { return diag_str_.empty(); }

Status StatusScopedDiagnosticHandler::ConsumeStatus() {
  if (ok()) return Status::OK();

  // TODO(jpienaar) This should be combining status with one previously built
  // up.
  Status s = tensorflow::errors::Unknown(diag_str_);
  diag_str_.clear();
  return s;
}

Status StatusScopedDiagnosticHandler::Combine(Status status) {
  if (status.ok()) return ConsumeStatus();

  // status is not-OK here, so if there was no diagnostics reported
  // additionally then return this error.
  if (ok()) return status;

  // Append the diagnostics reported to the status. This repeats the behavior of
  // TensorFlow's AppendToMessage without the additional formatting inserted
  // there.
  status = ::tensorflow::Status(
      status.code(), absl::StrCat(status.error_message(), diag_str_));
  diag_str_.clear();
  return status;
}

LogicalResult StatusScopedDiagnosticHandler::handler(Diagnostic* diag) {
#ifndef NDEBUG
  size_t current_diag_str_size_ = diag_str_.size();
#endif

  // Emit the diagnostic and flush the stream.
  emitDiagnostic(*diag);
  diag_stream_.flush();

#ifndef NDEBUG
  // Emit non-errors to VLOG instead of the internal status.
  if (diag->getSeverity() != DiagnosticSeverity::Error) {
    VLOG(1) << diag_str_.substr(current_diag_str_size_);
    diag_str_.resize(current_diag_str_size_);
  }
#endif

  // Return failure to signal propagation if necessary.
  return failure(propagate_);
}

}  // namespace mlir
<commit_msg>Do not issue an error to TensorFlow when MLIR issues a warning<commit_after>/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

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

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

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

#include "tensorflow/compiler/mlir/tensorflow/utils/error_util.h"

#include "tensorflow/core/lib/core/errors.h"

namespace mlir {

StatusScopedDiagnosticHandler::StatusScopedDiagnosticHandler(
    MLIRContext* context, bool propagate)
    : SourceMgrDiagnosticHandler(source_mgr_, context, diag_stream_),
      diag_stream_(diag_str_),
      propagate_(propagate) {
  setHandler([this](Diagnostic& diag) { return this->handler(&diag); });
}

StatusScopedDiagnosticHandler::~StatusScopedDiagnosticHandler() {
  // Verify errors were consumed and re-register old handler.
  bool all_errors_produced_were_consumed = ok();
  DCHECK(all_errors_produced_were_consumed) << "Error status not consumed:\n"
                                            << diag_str_;
}

bool StatusScopedDiagnosticHandler::ok() const { return diag_str_.empty(); }

Status StatusScopedDiagnosticHandler::ConsumeStatus() {
  if (ok()) return Status::OK();

  // TODO(jpienaar) This should be combining status with one previously built
  // up.
  Status s = tensorflow::errors::Unknown(diag_str_);
  diag_str_.clear();
  return s;
}

Status StatusScopedDiagnosticHandler::Combine(Status status) {
  if (status.ok()) return ConsumeStatus();

  // status is not-OK here, so if there was no diagnostics reported
  // additionally then return this error.
  if (ok()) return status;

  // Append the diagnostics reported to the status. This repeats the behavior of
  // TensorFlow's AppendToMessage without the additional formatting inserted
  // there.
  status = ::tensorflow::Status(
      status.code(), absl::StrCat(status.error_message(), diag_str_));
  diag_str_.clear();
  return status;
}

LogicalResult StatusScopedDiagnosticHandler::handler(Diagnostic* diag) {
  // Non-error diagnostic are ignored when VLOG isn't enabled.
  if (diag->getSeverity() != DiagnosticSeverity::Error && VLOG_IS_ON(1))
    return success();

  size_t current_diag_str_size_ = diag_str_.size();

  // Emit the diagnostic and flush the stream.
  emitDiagnostic(*diag);
  diag_stream_.flush();

  // Emit non-errors to VLOG instead of the internal status.
  if (diag->getSeverity() != DiagnosticSeverity::Error) {
    VLOG(1) << diag_str_.substr(current_diag_str_size_);
    diag_str_.resize(current_diag_str_size_);
  }

  // Return failure to signal propagation if necessary.
  return failure(propagate_);
}

}  // namespace mlir
<|endoftext|>


<commit_before>#include "CHPFCRFFSplitVariablesAction.h"
#include "Factory.h"
#include "Parser.h"
#include "FEProblem.h"

#include <sstream>
#include <stdexcept>

// libMesh includes
#include "libmesh/string_to_enum.h"

const Real CHPFCRFFSplitVariablesAction::_abs_zero_tol = 1e-12;

template<>
InputParameters validParams<CHPFCRFFSplitVariablesAction>()
{
  InputParameters params = validParams<Action>();
  params.addParam<std::string>("family", "LAGRANGE", "Specifies the family of FE shape functions to use for the L variables");
  params.addParam<std::string>("order", "FIRST",  "Specifies the order of the FE shape function to use for the L variables");
  params.addParam<Real>("scaling", 1.0, "Specifies a scaling factor to apply to the L variables");
  params.addRequiredParam<unsigned int>("num_L", "specifies the number of complex L variables will be solved for");
  params.addRequiredParam<std::string>("L_name_base", "Base name for the complex L variables");
  params.addRequiredParam<std::vector<std::string> >("sub_filenames", "This is the filename of the sub.i file");
  params.addRequiredParam<std::string>("n_name", "Name of atomic density variable");

  return params;
}

CHPFCRFFSplitVariablesAction::CHPFCRFFSplitVariablesAction(const std::string & name,
                                                           InputParameters params) :
    Action(name, params),
    _num_L(getParam<unsigned int>("num_L")),
    _L_name_base(getParam<std::string>("L_name_base")),
    _sub_filenames(getParam<std::vector<std::string> >("sub_filenames")),
    _n_name(getParam<std::string>("n_name"))
{
}

void
CHPFCRFFSplitVariablesAction::act()
{
  std::vector<MooseEnum> execute_options = SetupInterface::getExecuteOptions();
  execute_options[0] = "timestep_begin";

  // Setup MultiApp
  InputParameters poly_params = _factory.getValidParams("TransientMultiApp");
  poly_params.set<MooseEnum>("app_type") = "MarmotApp";
  poly_params.set<std::vector<MooseEnum> >("execute_on") = execute_options;
  poly_params.set<std::vector<std::string> >("input_files") = _sub_filenames;
  poly_params.set<unsigned int>("max_procs_per_app") = 1;

  Point one(0.0, 0.0, 0.0);

  std::vector<Point > positions;
  positions.push_back(one);

  poly_params.set<std::vector<Point> >("positions") = positions;

  _problem->addMultiApp("TransientMultiApp", "HHEquationSolver", poly_params);

  poly_params = _factory.getValidParams("MultiAppNearestNodeTransfer");
  poly_params.set<MooseEnum>("direction") = "to_multiapp";
  poly_params.set<std::vector<MooseEnum> >("execute_on") = execute_options;
  poly_params.set<AuxVariableName>("variable") = _n_name;
  poly_params.set<VariableName>("source_variable") = _n_name;
  poly_params.set<MultiAppName>("multi_app") = "HHEquationSolver";

  // Create Name for Transfer
  std::string trans_name = _n_name;
  trans_name.append("_trans");

  _problem->addTransfer("MultiAppNearestNodeTransfer", trans_name, poly_params);

#ifdef DEBUG
  Moose::err << "Inside the CHPFCRFFSplitVariablesAction Object\n";
  Moose::err << "VariableBase: " << _L_name_base
            << "\torder: " << getParam<std::string>("order")
            << "\tfamily: " << getParam<std::string>("family") << std::endl;
#endif

  // Loop through the number of L variables
  for (unsigned int l = 0; l < _num_L; ++l)
  {
    // Create L base name
    std::string L_name = _L_name_base;
    std::stringstream out;
    out << l;
    L_name.append(out.str());

    // Create real L variable
    std::string real_name = L_name;
    real_name.append("_real");

#ifdef DEBUG
    Moose::err << "Real name = " << real_name << std::endl;
#endif

    _problem->addAuxVariable(real_name,
                             FEType(Utility::string_to_enum<Order>(getParam<std::string>("order")),
                                    Utility::string_to_enum<FEFamily>(getParam<std::string>("family"))));

    poly_params = _factory.getValidParams("MultiAppNearestNodeTransfer");
    poly_params.set<MooseEnum>("direction") = "from_multiapp";
    poly_params.set<AuxVariableName>("variable") = real_name;
    poly_params.set<VariableName>("source_variable") = real_name;
    poly_params.set<MultiAppName>("multi_app") = "HHEquationSolver";

    // Create Name for Transfer
    std::string trans_name = real_name;
    trans_name.append("_trans");

    _problem->addTransfer("MultiAppNearestNodeTransfer", trans_name, poly_params);

    if (l > 0)
    {
      // Create imaginary L variable IF l > 0
      std::string imag_name = L_name;
      imag_name.append("_imag");

#ifdef DEBUG
      Moose::err << "Imaginary name = " << imag_name << std::endl;
#endif

      _problem->addAuxVariable(imag_name,
                               FEType(Utility::string_to_enum<Order>(getParam<std::string>("order")),
                                      Utility::string_to_enum<FEFamily>(getParam<std::string>("family"))));

      poly_params = _factory.getValidParams("MultiAppNearestNodeTransfer");
      poly_params.set<MooseEnum>("direction") = "from_multiapp";
      poly_params.set<AuxVariableName>("variable") = imag_name;
      poly_params.set<VariableName>("source_variable") = imag_name;
      poly_params.set<MultiAppName>("multi_app") = "HHEquationSolver";

      // Create Name for Transfer
      std::string trans_name = imag_name;
      trans_name.append("_trans");

      _problem->addTransfer("MultiAppNearestNodeTransfer", trans_name, poly_params);
    }

  }

}
<commit_msg>Convert "execute_on" parameter to MultiMooseEnum closes #53<commit_after>#include "CHPFCRFFSplitVariablesAction.h"
#include "Factory.h"
#include "Parser.h"
#include "FEProblem.h"

#include <sstream>
#include <stdexcept>

// libMesh includes
#include "libmesh/string_to_enum.h"

const Real CHPFCRFFSplitVariablesAction::_abs_zero_tol = 1e-12;

template<>
InputParameters validParams<CHPFCRFFSplitVariablesAction>()
{
  InputParameters params = validParams<Action>();
  params.addParam<std::string>("family", "LAGRANGE", "Specifies the family of FE shape functions to use for the L variables");
  params.addParam<std::string>("order", "FIRST",  "Specifies the order of the FE shape function to use for the L variables");
  params.addParam<Real>("scaling", 1.0, "Specifies a scaling factor to apply to the L variables");
  params.addRequiredParam<unsigned int>("num_L", "specifies the number of complex L variables will be solved for");
  params.addRequiredParam<std::string>("L_name_base", "Base name for the complex L variables");
  params.addRequiredParam<std::vector<std::string> >("sub_filenames", "This is the filename of the sub.i file");
  params.addRequiredParam<std::string>("n_name", "Name of atomic density variable");

  return params;
}

CHPFCRFFSplitVariablesAction::CHPFCRFFSplitVariablesAction(const std::string & name,
                                                           InputParameters params) :
    Action(name, params),
    _num_L(getParam<unsigned int>("num_L")),
    _L_name_base(getParam<std::string>("L_name_base")),
    _sub_filenames(getParam<std::vector<std::string> >("sub_filenames")),
    _n_name(getParam<std::string>("n_name"))
{
}

void
CHPFCRFFSplitVariablesAction::act()
{
  MultiMooseEnum execute_options(SetupInterface::getExecuteOptions());
  execute_options = "timestep_begin";

  // Setup MultiApp
  InputParameters poly_params = _factory.getValidParams("TransientMultiApp");
  poly_params.set<MooseEnum>("app_type") = "MarmotApp";
  poly_params.set<MultiMooseEnum>("execute_on") = execute_options;
  poly_params.set<std::vector<std::string> >("input_files") = _sub_filenames;
  poly_params.set<unsigned int>("max_procs_per_app") = 1;

  Point one(0.0, 0.0, 0.0);

  std::vector<Point > positions;
  positions.push_back(one);

  poly_params.set<std::vector<Point> >("positions") = positions;

  _problem->addMultiApp("TransientMultiApp", "HHEquationSolver", poly_params);

  poly_params = _factory.getValidParams("MultiAppNearestNodeTransfer");
  poly_params.set<MooseEnum>("direction") = "to_multiapp";
  poly_params.set<MultiMooseEnum>("execute_on") = execute_options;
  poly_params.set<AuxVariableName>("variable") = _n_name;
  poly_params.set<VariableName>("source_variable") = _n_name;
  poly_params.set<MultiAppName>("multi_app") = "HHEquationSolver";

  // Create Name for Transfer
  std::string trans_name = _n_name;
  trans_name.append("_trans");

  _problem->addTransfer("MultiAppNearestNodeTransfer", trans_name, poly_params);

#ifdef DEBUG
  Moose::err << "Inside the CHPFCRFFSplitVariablesAction Object\n";
  Moose::err << "VariableBase: " << _L_name_base
            << "\torder: " << getParam<std::string>("order")
            << "\tfamily: " << getParam<std::string>("family") << std::endl;
#endif

  // Loop through the number of L variables
  for (unsigned int l = 0; l < _num_L; ++l)
  {
    // Create L base name
    std::string L_name = _L_name_base;
    std::stringstream out;
    out << l;
    L_name.append(out.str());

    // Create real L variable
    std::string real_name = L_name;
    real_name.append("_real");

#ifdef DEBUG
    Moose::err << "Real name = " << real_name << std::endl;
#endif

    _problem->addAuxVariable(real_name,
                             FEType(Utility::string_to_enum<Order>(getParam<std::string>("order")),
                                    Utility::string_to_enum<FEFamily>(getParam<std::string>("family"))));

    poly_params = _factory.getValidParams("MultiAppNearestNodeTransfer");
    poly_params.set<MooseEnum>("direction") = "from_multiapp";
    poly_params.set<AuxVariableName>("variable") = real_name;
    poly_params.set<VariableName>("source_variable") = real_name;
    poly_params.set<MultiAppName>("multi_app") = "HHEquationSolver";

    // Create Name for Transfer
    std::string trans_name = real_name;
    trans_name.append("_trans");

    _problem->addTransfer("MultiAppNearestNodeTransfer", trans_name, poly_params);

    if (l > 0)
    {
      // Create imaginary L variable IF l > 0
      std::string imag_name = L_name;
      imag_name.append("_imag");

#ifdef DEBUG
      Moose::err << "Imaginary name = " << imag_name << std::endl;
#endif

      _problem->addAuxVariable(imag_name,
                               FEType(Utility::string_to_enum<Order>(getParam<std::string>("order")),
                                      Utility::string_to_enum<FEFamily>(getParam<std::string>("family"))));

      poly_params = _factory.getValidParams("MultiAppNearestNodeTransfer");
      poly_params.set<MooseEnum>("direction") = "from_multiapp";
      poly_params.set<AuxVariableName>("variable") = imag_name;
      poly_params.set<VariableName>("source_variable") = imag_name;
      poly_params.set<MultiAppName>("multi_app") = "HHEquationSolver";

      // Create Name for Transfer
      std::string trans_name = imag_name;
      trans_name.append("_trans");

      _problem->addTransfer("MultiAppNearestNodeTransfer", trans_name, poly_params);
    }

  }

}
<|endoftext|>


<commit_before>/*
* DSA
* (C) 1999-2010,2014 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/dsa.h>
#include <botan/numthry.h>
#include <botan/keypair.h>
#include <botan/rfc6979.h>
#include <future>

namespace Botan {

/*
* DSA_PublicKey Constructor
*/
DSA_PublicKey::DSA_PublicKey(const DL_Group& grp, const BigInt& y1)
   {
   group = grp;
   y = y1;
   }

/*
* Create a DSA private key
*/
DSA_PrivateKey::DSA_PrivateKey(RandomNumberGenerator& rng,
                               const DL_Group& grp,
                               const BigInt& x_arg)
   {
   group = grp;
   x = x_arg;

   if(x == 0)
      x = BigInt::random_integer(rng, 2, group_q() - 1);

   y = power_mod(group_g(), x, group_p());

   if(x_arg == 0)
      gen_check(rng);
   else
      load_check(rng);
   }

DSA_PrivateKey::DSA_PrivateKey(const AlgorithmIdentifier& alg_id,
                               const secure_vector<byte>& key_bits,
                               RandomNumberGenerator& rng) :
   DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_57)
   {
   y = power_mod(group_g(), x, group_p());

   load_check(rng);
   }

/*
* Check Private DSA Parameters
*/
bool DSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const
   {
   if(!DL_Scheme_PrivateKey::check_key(rng, strong) || x >= group_q())
      return false;

   if(!strong)
      return true;

   return KeyPair::signature_consistency_check(rng, *this, "EMSA1(SHA-1)");
   }

DSA_Signature_Operation::DSA_Signature_Operation(const DSA_PrivateKey& dsa,
                                                 const std::string& emsa) :
   q(dsa.group_q()),
   x(dsa.get_x()),
   powermod_g_p(dsa.group_g(), dsa.group_p()),
   mod_q(dsa.group_q()),
   m_hash(hash_for_deterministic_signature(emsa))
   {
   }

secure_vector<byte>
DSA_Signature_Operation::sign(const byte msg[], size_t msg_len,
                              RandomNumberGenerator& rng)
   {
   rng.add_entropy(msg, msg_len);

   BigInt i(msg, msg_len);

   if(i >= q)
      i -= q;

   const BigInt k = generate_rfc6979_nonce(x, q, i, m_hash);

   auto future_r = std::async(std::launch::async,
                              [&]() { return mod_q.reduce(powermod_g_p(k)); });

   BigInt s = inverse_mod(k, q);
   const BigInt r = future_r.get();
   s = mod_q.multiply(s, mul_add(x, r, i));

   // With overwhelming probability, a bug rather than actual zero r/s
   BOTAN_ASSERT(s != 0, "invalid s");
   BOTAN_ASSERT(r != 0, "invalid r");

   secure_vector<byte> output(2*q.bytes());
   r.binary_encode(&output[output.size() / 2 - r.bytes()]);
   s.binary_encode(&output[output.size() - s.bytes()]);
   return output;
   }

DSA_Verification_Operation::DSA_Verification_Operation(const DSA_PublicKey& dsa) :
   q(dsa.group_q()), y(dsa.get_y())
   {
   powermod_g_p = Fixed_Base_Power_Mod(dsa.group_g(), dsa.group_p());
   powermod_y_p = Fixed_Base_Power_Mod(y, dsa.group_p());
   mod_p = Modular_Reducer(dsa.group_p());
   mod_q = Modular_Reducer(dsa.group_q());
   }

bool DSA_Verification_Operation::verify(const byte msg[], size_t msg_len,
                                        const byte sig[], size_t sig_len)
   {
   if(sig_len != 2*q.bytes() || msg_len > q.bytes())
      return false;

   BigInt r(sig, q.bytes());
   BigInt s(sig + q.bytes(), q.bytes());
   BigInt i(msg, msg_len);

   if(r <= 0 || r >= q || s <= 0 || s >= q)
      return false;

   s = inverse_mod(s, q);

   auto future_s_i = std::async(std::launch::async,
      [&]() { return powermod_g_p(mod_q.multiply(s, i)); });

   BigInt s_r = powermod_y_p(mod_q.multiply(s, r));
   BigInt s_i = future_s_i.get();

   s = mod_p.multiply(s_i, s_r);

   return (mod_q.reduce(s) == r);
   }

}
<commit_msg>No need to reseed RNG in DSA sign as RNG is no longer used<commit_after>/*
* DSA
* (C) 1999-2010,2014 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/dsa.h>
#include <botan/numthry.h>
#include <botan/keypair.h>
#include <botan/rfc6979.h>
#include <future>

namespace Botan {

/*
* DSA_PublicKey Constructor
*/
DSA_PublicKey::DSA_PublicKey(const DL_Group& grp, const BigInt& y1)
   {
   group = grp;
   y = y1;
   }

/*
* Create a DSA private key
*/
DSA_PrivateKey::DSA_PrivateKey(RandomNumberGenerator& rng,
                               const DL_Group& grp,
                               const BigInt& x_arg)
   {
   group = grp;
   x = x_arg;

   if(x == 0)
      x = BigInt::random_integer(rng, 2, group_q() - 1);

   y = power_mod(group_g(), x, group_p());

   if(x_arg == 0)
      gen_check(rng);
   else
      load_check(rng);
   }

DSA_PrivateKey::DSA_PrivateKey(const AlgorithmIdentifier& alg_id,
                               const secure_vector<byte>& key_bits,
                               RandomNumberGenerator& rng) :
   DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_57)
   {
   y = power_mod(group_g(), x, group_p());

   load_check(rng);
   }

/*
* Check Private DSA Parameters
*/
bool DSA_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const
   {
   if(!DL_Scheme_PrivateKey::check_key(rng, strong) || x >= group_q())
      return false;

   if(!strong)
      return true;

   return KeyPair::signature_consistency_check(rng, *this, "EMSA1(SHA-1)");
   }

DSA_Signature_Operation::DSA_Signature_Operation(const DSA_PrivateKey& dsa,
                                                 const std::string& emsa) :
   q(dsa.group_q()),
   x(dsa.get_x()),
   powermod_g_p(dsa.group_g(), dsa.group_p()),
   mod_q(dsa.group_q()),
   m_hash(hash_for_deterministic_signature(emsa))
   {
   }

secure_vector<byte>
DSA_Signature_Operation::sign(const byte msg[], size_t msg_len,
                              RandomNumberGenerator&)
   {
   BigInt i(msg, msg_len);

   while(i >= q)
      i -= q;

   const BigInt k = generate_rfc6979_nonce(x, q, i, m_hash);

   auto future_r = std::async(std::launch::async,
                              [&]() { return mod_q.reduce(powermod_g_p(k)); });

   BigInt s = inverse_mod(k, q);
   const BigInt r = future_r.get();
   s = mod_q.multiply(s, mul_add(x, r, i));

   // With overwhelming probability, a bug rather than actual zero r/s
   BOTAN_ASSERT(s != 0, "invalid s");
   BOTAN_ASSERT(r != 0, "invalid r");

   secure_vector<byte> output(2*q.bytes());
   r.binary_encode(&output[output.size() / 2 - r.bytes()]);
   s.binary_encode(&output[output.size() - s.bytes()]);
   return output;
   }

DSA_Verification_Operation::DSA_Verification_Operation(const DSA_PublicKey& dsa) :
   q(dsa.group_q()), y(dsa.get_y())
   {
   powermod_g_p = Fixed_Base_Power_Mod(dsa.group_g(), dsa.group_p());
   powermod_y_p = Fixed_Base_Power_Mod(y, dsa.group_p());
   mod_p = Modular_Reducer(dsa.group_p());
   mod_q = Modular_Reducer(dsa.group_q());
   }

bool DSA_Verification_Operation::verify(const byte msg[], size_t msg_len,
                                        const byte sig[], size_t sig_len)
   {
   if(sig_len != 2*q.bytes() || msg_len > q.bytes())
      return false;

   BigInt r(sig, q.bytes());
   BigInt s(sig + q.bytes(), q.bytes());
   BigInt i(msg, msg_len);

   if(r <= 0 || r >= q || s <= 0 || s >= q)
      return false;

   s = inverse_mod(s, q);

   auto future_s_i = std::async(std::launch::async,
      [&]() { return powermod_g_p(mod_q.multiply(s, i)); });

   BigInt s_r = powermod_y_p(mod_q.multiply(s, r));
   BigInt s_i = future_s_i.get();

   s = mod_p.multiply(s_i, s_r);

   return (mod_q.reduce(s) == r);
   }

}
<|endoftext|>