text:
#include "llvm/Pass.h"
#include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/MemoryBuiltins.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/ADT/SCCIterator.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/InstVisitor.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/CommandLine.h"
#include <boost/unordered_map.hpp>

using namespace llvm;

static llvm::cl::opt<std::string>
OutputFile("profile-outfile",
	llvm::cl::desc("Dump all counters to output filename"),
	llvm::cl::init(""));

static llvm::cl::opt<bool>
ShowCallGraphInfo("profile-callgraph",
        llvm::cl::desc ("Show call graph information"),
        llvm::cl::init (false));

static llvm::cl::opt<bool>
PrintDetails("profile-details",
        llvm::cl::desc ("Show more detailed statistics"),
        llvm::cl::init (false));

static llvm::cl::opt<bool>
DisplayDeclarations("profile-list-declarations",
        llvm::cl::desc ("List all the function declarations"),
	llvm::cl::init (false),
	llvm::cl::Hidden);

#include "llvm/IR/Instruction.def"

namespace previrt {

  struct Counter {
    const char*Name;
    const char* Desc;
    unsigned int Value;
    Counter (): Name(""), Desc (""), Value(0)  { }
    Counter (const char *name): Name(name), Desc (name), Value(0) { }
    Counter (const char *name, const char *desc): Name(name), Desc (desc), Value(0) { }
    bool operator<(const Counter&o) const
    { return (std::strcmp(getName(), o.getName()) < 0); }
    bool operator==(const Counter&o) const
    { return (std::strcmp(getName(), o.getName()) == 0); }
    unsigned int getValue() const { return Value; }
    const char *getName() const { return Name; }
    const char *getDesc() const { return Desc; }
    void operator++() { Value++; }
    void operator+=(unsigned val) { Value += val; }
  };

  class ProfilerPass : public ModulePass, public InstVisitor<ProfilerPass> {
    friend class InstVisitor<ProfilerPass>;

    void formatCounters (std::vector<Counter>& counters,
                         unsigned& MaxNameLen,
                         unsigned& MaxValLen,
                         bool sort = true) {
      // Figure out how long the biggest Value and Name fields are.
      for (auto c: counters) {
        MaxValLen = std::max(MaxValLen,
                             (unsigned)utostr(c.getValue()).size());
        MaxNameLen = std::max(MaxNameLen,
                              (unsigned)std::strlen(c.getName()));
      }

      if (sort) {
        // Sort the fields by name.
        std::stable_sort(counters.begin(), counters.end());
      }
    }

    const DataLayout *DL;
    TargetLibraryInfo *TLI;
    LLVMContext *Ctx;

    StringSet<> ExtFuncs;

    // -- to group all instruction counters
    boost::unordered_map <const char*, Counter> instCounters;
    void incrInstCounter (const char* name, unsigned val) {
      auto it = instCounters.find (name);
      if (it != instCounters.end ())
        it->second += val;
      else
        instCounters.insert (std::make_pair (name, Counter (name)));
    }

    // -- individual counters
    Counter TotalFuncs;
    Counter TotalBlocks;
    Counter TotalJoins;
    Counter TotalInsts;
    Counter TotalDirectCalls;
    Counter TotalIndirectCalls;
    Counter TotalExternalCalls;
    ///
    Counter SafeIntDiv;
    Counter SafeFPDiv;
    Counter UnsafeIntDiv;
    Counter UnsafeFPDiv;
    Counter DivIntUnknown;
    Counter DivFPUnknown;
    ///
    Counter TotalMemInst;
    Counter MemUnknown;
    Counter SafeMemAccess;
    Counter TotalAllocations;
    Counter InBoundGEP;
    Counter MemCpy;
    Counter MemMove;
    Counter MemSet;
    ///
    Counter SafeLeftShift;
    Counter UnsafeLeftShift;
    Counter UnknownLeftShift;

    void visitFunction  (Function &F) { ++TotalFuncs; }
    void visitBasicBlock(BasicBlock &BB) {
      ++TotalBlocks;
      if (!BB.getSinglePredecessor ())
        ++TotalJoins;
    }

    void visitCallSite(CallSite &CS) {
      Function* callee = CS.getCalledFunction ();
      if (callee) {
        ++TotalDirectCalls;
        if (callee->isDeclaration ()) {
          ++TotalExternalCalls;
          ExtFuncs.insert (callee->getName ());
        }
      }
      else
        ++TotalIndirectCalls;

      // new, malloc, calloc, realloc, and strdup.
      if (isAllocationFn (CS.getInstruction(), TLI, true))
        ++TotalAllocations;
    }

    bool isSafeMemAccess(Value *V) {
      ObjectSizeOpts opt;
      ObjectSizeOffsetVisitor OSOV(*DL, TLI, *Ctx, opt);
      // res.first is size and res.second is offset
      SizeOffsetType res = OSOV.compute(V);
      if (OSOV.bothKnown(res)) {
	// FIXME: need to add getPointerTypeSizeInBits to offset
	return (res.second.isNonNegative() && res.second.ult(res.first));
      }
      return false;
    }

    void processPtrOperand (Value* V) {
      if (isSafeMemAccess(V)) {
	++SafeMemAccess;
      } else {
	++MemUnknown;
      }
    }

    void processMemoryIntrinsicsPtrOperand (Value* V, Value*N) {
      if (ConstantInt *CI = dyn_cast<ConstantInt> (N)) {
	int64_t n = CI->getSExtValue();
	uint64_t size;
	ObjectSizeOpts opt;
	if (getObjectSize (V, size, *DL, TLI, opt)) {
	  if (n >= 0 && ((uint64_t) n < size)) {
	    ++SafeMemAccess;
	    return;
	  }
	}
      }
      ++MemUnknown;
    }

    void visitBinaryOperator (BinaryOperator* BI) {
      if (BI->getOpcode () == BinaryOperator::SDiv ||
          BI->getOpcode () == BinaryOperator::UDiv ||
          BI->getOpcode () == BinaryOperator::SRem ||
          BI->getOpcode () == BinaryOperator::URem ||
          BI->getOpcode () == BinaryOperator::FDiv ||
          BI->getOpcode () == BinaryOperator::FRem) {
        const Value* divisor = BI->getOperand (1);
        if (const ConstantInt *CI = dyn_cast<const ConstantInt> (divisor)) {
          if (CI->isZero ()) ++UnsafeIntDiv;
          else ++SafeIntDiv;
        }
        else if (const ConstantFP *CFP = dyn_cast<const ConstantFP> (divisor)) {
          if (CFP->isZero ()) ++UnsafeFPDiv;
          else ++SafeFPDiv;
        }
        else {
          // cannot figure out statically
          if (BI->getOpcode () == BinaryOperator::SDiv ||
              BI->getOpcode () == BinaryOperator::UDiv ||
              BI->getOpcode () == BinaryOperator::SRem ||
              BI->getOpcode () == BinaryOperator::URem)
            ++DivIntUnknown;
          else
            ++DivFPUnknown;
        }
      }
      else if (BI->getOpcode () == BinaryOperator::Shl) {
        // Check for oversized shift amounts
        if (const ConstantInt *CI = dyn_cast<const ConstantInt> (BI->getOperand (1))) {
          APInt shift = CI->getValue ();
          if (CI->getType ()->isIntegerTy ()) {
            APInt bitwidth (shift.getBitWidth(), CI->getType ()->getIntegerBitWidth (), true);
            if (shift.slt (bitwidth)) {
              ++SafeLeftShift;
	    } else {
              ++UnsafeLeftShift;
	    }
          }
          else
            ++UnknownLeftShift;
        }
        else
          ++UnknownLeftShift;
      }
    }

    #define HANDLE_INST(N, OPCODE, CLASS)                    \
    void visit##OPCODE(CLASS &I) {                           \
      incrInstCounter (#OPCODE, 1);                          \
      ++TotalInsts;                                          \
      if (MemTransferInst * MTI = dyn_cast<MemTransferInst>(&I)) {  \
         ++TotalMemInst;                                            \
         ++TotalMemInst;                                            \
         if (isa<MemCpyInst> (MTI)) ++MemCpy;                       \
         else if (isa<MemMoveInst> (MTI)) ++MemMove;                \
         processMemoryIntrinsicsPtrOperand (MTI->getSource(), MTI->getLength ()); \
         processMemoryIntrinsicsPtrOperand (MTI->getDest(), MTI->getLength ()); \
      }                                                             \
      else if (MemSetInst* MSI = dyn_cast<MemSetInst>(&I)) {        \
         ++TotalMemInst;                                            \
         ++MemSet;                                                  \
         processMemoryIntrinsicsPtrOperand (MSI->getDest(), MSI->getLength ()); \
      }                                                             \
      if (CallInst* CI = dyn_cast<CallInst>(&I)) {                  \
         CallSite CS (CI);                                          \
         visitCallSite (CS);                                        \
      }                                                             \
      else if (InvokeInst* II = dyn_cast<InvokeInst>(&I)) {         \
         CallSite CS (II);                                          \
         visitCallSite (CS);                                        \
      }                                                             \
      else if (BinaryOperator* BI = dyn_cast<BinaryOperator>(&I)) { \
         visitBinaryOperator (BI);                                  \
      }                                                             \
      else if (LoadInst* LI = dyn_cast<LoadInst>(&I)) {             \
         ++TotalMemInst;                                            \
         processPtrOperand (LI->getPointerOperand ());		    \
      }                                                             \
      else if (StoreInst* SI = dyn_cast<StoreInst>(&I)) {           \
         ++TotalMemInst;                                            \
         processPtrOperand (SI->getPointerOperand ());		    \
      }                                                             \
      else if (GetElementPtrInst* GEP = dyn_cast<GetElementPtrInst>(&I)) { \
        if (GEP->isInBounds ()) ++InBoundGEP;                       \
      }                                                             \
    }

    #include "llvm/IR/Instruction.def"

    void visitInstruction(Instruction &I) {
      errs() << "Instruction Count does not know about " << I;
      llvm_unreachable(nullptr);
    }

  public:

    static char ID;

    ProfilerPass() :
        ModulePass(ID),
        DL (nullptr), TLI (nullptr),
        TotalFuncs ("TotalFuncs", "Number of functions"),
        TotalBlocks ("TotalBlocks", "Number of basic blocks"),
        TotalJoins ("TotalJoins","Number of basic blocks with more than one predecessor"),
        TotalInsts ("TotalInsts","Number of instructions"),
        TotalDirectCalls ("TotalDirectCalls","Number of direct calls"),
        TotalIndirectCalls ("TotalIndirectCalls","Number of indirect calls"),
        TotalExternalCalls ("TotalExternalCalls","Number of external calls"),
        ////////
        SafeIntDiv ("SafeIntDiv","Number of safe integer div/rem"),
        SafeFPDiv ("SafeFPDiv","Number of safe FP div/rem"),
        UnsafeIntDiv ("UnsafeIntDiv","Number of definite unsafe integer div/rem"),
        UnsafeFPDiv ("UnsafeFPDiv","Number of definite unsafe FP div/rem"),
        DivIntUnknown ("DivIntUnknown","Number of unknown integer div/rem"),
        DivFPUnknown ("DivFPUnknown","Number of unknown FP div/rem"),
        /////////
        TotalMemInst ("TotalMemInst","Number of memory instructions"),
        MemUnknown ("MemUnknown","Statically unknown memory accesses"),
        SafeMemAccess ("SafeMemAccess","Statically safe memory accesses"),
        TotalAllocations ("TotalAllocations","Malloc-like allocations"),
        InBoundGEP ("InBoundGEP","Inbound GetElementPtr"),
        MemCpy ("MemCpy"),
        MemMove ("MemMove"),
        MemSet ("MemSet"),
        /////////
        SafeLeftShift ("SafeLeftShift","Number of safe left shifts"),
        UnsafeLeftShift ("UnsafeLeftShift", "Number of definite unsafe left shifts"),
        UnknownLeftShift("UnknownLeftShift", "Number of unknown left shifts")
    { }

    // Collect statistics
    bool runOnFunction(Function &F)  {
       visit(F);
       return false;
    }

    bool runOnModule (Module &M) override {

      DL = &M.getDataLayout ();
      TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
      Ctx = &M.getContext();

      /// Look at the callgraph
      // CallGraphWrapperPass *cgwp = &getAnalysis<CallGraphWrapperPass> ();
      // if (cgwp) {
      //   cgwp->print (errs (), &M);
      // }

      if (ShowCallGraphInfo) {
        CallGraph &CG = getAnalysis<CallGraphWrapperPass> ().getCallGraph ();
        typedef std::pair <Function*, std::pair <unsigned, unsigned> > func_ty;
        std::vector<func_ty> funcs;
        errs () << "[Call graph information]\n";
        errs () << "Total number of functions="
		<< std::distance(M.begin(), M.end()) << "\n";
        for (auto it = scc_begin (&CG); !it.isAtEnd (); ++it) {
          auto &scc = *it;
          for (CallGraphNode *cgn : scc) {
            if (cgn->getFunction () && !cgn->getFunction ()->isDeclaration ()) {
              funcs.push_back (std::make_pair (cgn->getFunction (),
                                  std::make_pair (cgn->getNumReferences (),
					 std::distance (cgn->begin (), cgn->end ()))));
            }
          }
        }

        bool has_rec_func = false;
        for (auto it = scc_begin (&CG); !it.isAtEnd (); ++it) {
          auto &scc = *it;
          if (std::distance (scc.begin (), scc.end ()) > 1) {
            has_rec_func = true;
            errs () << "Found recursive SCC={";
            for (CallGraphNode *cgn : scc) {
              if (cgn->getFunction ())
                errs () << cgn->getFunction ()->getName () << ";";
            }
          }
        }
        if (!has_rec_func)
          errs () << "No recursive functions found\n";

        std::sort (funcs.begin(), funcs.end (),
                   [](func_ty p1, func_ty p2) {
                     return   (p1.second.first + p1.second.second) >
                         (p2.second.first + p2.second.second);
                   });

        for (auto&p: funcs){
          Function* F = p.first;
          unsigned numInsts = std::distance(inst_begin(F), inst_end(F));
          errs () << F->getName () << ":"
                  << " num of instructions=" << numInsts
                  << " num of callers=" << p.second.first
                  << " num of callees=" << p.second.second << "\n";
        }
      }

      for (auto &F: M) { runOnFunction (F); }

      if (OutputFile != "") {
	std::error_code ec;
	llvm::tool_output_file out(OutputFile.c_str (), ec, sys::fs::F_Text);
	if (ec) {
	  errs () << "ERROR: Cannot open file: " << ec.message () << "\n";
	} else {
	  printCounters(out.os());
	  out.keep();
	}
      } else {
	printCounters(errs());
      }

      // if (DisplayDeclarations) {
      //   errs () << "[Non-analyzed (external) functions]\n";
      //   for (auto &p: ExtFuncs)
      //     errs () << p.getKey () << "\n";
      // }

      return false;
    }

    void getAnalysisUsage(AnalysisUsage &AU) const override {
      AU.setPreservesAll();
      AU.addRequired<llvm::CallGraphWrapperPass>();
      AU.addRequired<llvm::TargetLibraryInfoWrapperPass>();
      AU.addPreserved<CallGraphWrapperPass> ();
    }

    void printCounters(raw_ostream &O) {
      unsigned MaxNameLen = 0, MaxValLen = 0;

      {
        O << "[CFG analysis]\n";
        std::vector<Counter> cfg_counters
	{TotalFuncs, TotalBlocks, TotalInsts,
	 /*TotalJoins,*/
	 TotalDirectCalls, TotalExternalCalls, TotalIndirectCalls};
        formatCounters (cfg_counters, MaxNameLen, MaxValLen, false);
        for (auto c: cfg_counters) {
          O << format("%*u %-*s\n",
                      MaxValLen,
                      c.getValue(),
                      MaxNameLen,
                      c.getDesc());
        }

	if (PrintDetails) {
	  { // instruction counters
	    MaxNameLen = MaxValLen = 0;
	    std::vector<Counter> inst_counters;
	    inst_counters.reserve (instCounters.size ());
	    for(auto &p: instCounters) { inst_counters.push_back (p.second); }
	    formatCounters (inst_counters, MaxNameLen, MaxValLen);
	    O << "Number of each kind of instructions  \n";
	    for (auto c: inst_counters) {
	      O << format("%*u %-*s\n",
			  MaxValLen,
			  c.getValue(),
			  MaxNameLen,
			  c.getDesc());
	    }
	  }
	}
      }

      { // Memory counters
        MaxNameLen = MaxValLen = 0;
        std::vector<Counter> mem_counters;

	if (PrintDetails) {
	  mem_counters =
	    {TotalMemInst,instCounters["Store"],instCounters["Load"],MemCpy,MemMove,MemSet,
	     SafeMemAccess,MemUnknown,
	     instCounters["GetElementPtr"],InBoundGEP,
	     instCounters["Alloca"], TotalAllocations};
	} else {
	  mem_counters= {TotalMemInst, SafeMemAccess, MemUnknown};
	}

        formatCounters (mem_counters, MaxNameLen, MaxValLen,false);
        O << "[Memory analysis]\n";
        for (auto c: mem_counters) {
          O << format("%*u %-*s\n",
                      MaxValLen,
                      c.getValue(),
                      MaxNameLen,
                      c.getDesc());
        }
      }

      // { // Division counters
      //   MaxNameLen = MaxValLen = 0;
      //   std::vector<Counter> div_counters
      //   {SafeIntDiv,UnsafeIntDiv,SafeFPDiv,UnsafeFPDiv,DivIntUnknown,DivFPUnknown};
      //   formatCounters (div_counters, MaxNameLen, MaxValLen,false);
      //   O << "[Division by zero analysis]\n";
      //   for (auto c: div_counters) {
      //     O << format("%*u %-*s\n",
      //                 MaxValLen,
      //                 c.getValue(),
      //                 MaxNameLen,
      //                 c.getDesc());
      //   }
      // }

      // { // left shift
      //   MaxNameLen = MaxValLen = 0;
      //   std::vector<Counter> lsh_counters {SafeLeftShift,UnsafeLeftShift,UnknownLeftShift};
      //   formatCounters (lsh_counters, MaxNameLen, MaxValLen,false);
      //   O << "[Oversized left shifts analysis]\n";
      //   for (auto c: lsh_counters) {
      //     O << format("%*u %-*s\n",
      //                 MaxValLen,
      //                 c.getValue(),
      //                 MaxNameLen,
      //                 c.getDesc());
      //   }
      // }
    }

    virtual StringRef getPassName () const override {
      return "Count number of functions, instructions, memory accesses, etc";
    }
  };

  char ProfilerPass::ID = 0;

  static RegisterPass<ProfilerPass>
  X("Pprofiler",
    "Count number of functions, instructions, memory accesses, etc.",
    false,
    false);

} // end namespace


text:
/*=============================================================================
    Spirit v1.6.0
    Copyright (c) 2002 Jeff Westfahl
    http://spirit.sourceforge.net/

    Permission to copy, use, modify, sell and distribute this software is
    granted provided this copyright notice appears in all copies. This
    software is provided "as is" without express or implied warranty, and
    with no claim as to its suitability for any purpose.
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
//  A parser that echoes a file
//
//  [ JMW 8/05/2002 ]
//
///////////////////////////////////////////////////////////////////////////////

#include <boost/spirit/core.hpp>
#include <boost/spirit/iterator/file_iterator.hpp>
#include <iostream>

///////////////////////////////////////////////////////////////////////////////

using namespace boost::spirit;

////////////////////////////////////////////////////////////////////////////
//
//  Types
//
////////////////////////////////////////////////////////////////////////////

typedef char                  CharT;
typedef file_iterator <CharT> IteratorT;
typedef scanner <IteratorT>   ScannerT;
typedef rule <ScannerT>       RuleT;

////////////////////////////////////////////////////////////////////////////
//
//  Actions
//
////////////////////////////////////////////////////////////////////////////

void Echo (IteratorT const & rBegin, IteratorT const & rEnd)
{
   while (rBegin < rEnd)
   {
      std::cout << *rBegin++;
   }
}

////////////////////////////////////////////////////////////////////////////
//
//  Main program
//
////////////////////////////////////////////////////////////////////////////

int main (int argc, char * argv [])
{
   if (2 > argc)
   {
      std::cout << "Must specify a filename!\n";

      return -1;
   }

   // Create a file iterator for this file
   IteratorT _Start (argv [1]);

   if (!_Start)
   {
      std::cout << "Unable to open file!\n";

      return -1;
   }

   // Create an EOF iterator
   IteratorT _End = _Start.make_end ();

   // A simple rule
   RuleT _Rule = *(anychar_p);

   // Parse
   parse_info <IteratorT> _Info = parse (
      _Start,
      _End,
      _Rule [&Echo]
      );

   // This really shouldn't fail...
   if (_Info.full)
   {
      std::cout << "Parse succeeded!\n";
   }
   else
   {
      std::cout << "Parse failed!\n";
   }

   return 0;
}

ext: cpp


text:
/* Copyright (c) 2009-2010 Tyrell Corporation & Moyshe Ben Rabi.

   The contents of this file are subject to the Mozilla Public License
   Version 1.1 (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.mozilla.org/MPL/

   Software distributed under the License is distributed on an "AS IS"
   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
   License for the specific language governing rights and limitations
   under the License.

   The Original Code is an implementation of the Metaverse eXchange Protocol.

   The Initial Developer of the Original Code is Akos Maroy and Moyshe Ben Rabi.
   All Rights Reserved.

   Contributor(s): Akos Maroy and Moyshe Ben Rabi.

   Alternatively, the contents of this file may be used under the terms
   of the Affero General Public License (the  "AGPL"), in which case the
   provisions of the AGPL are applicable instead of those
   above. If you wish to allow use of your version of this file only
   under the terms of the AGPL and not to allow others to use
   your version of this file under the MPL, indicate your decision by
   deleting the provisions above and replace them with the notice and
   other provisions required by the AGPL. If you do not delete
   the provisions above, a recipient may use your version of this file
   under either the MPL or the AGPL.
*/

#include <vector>
#include <stdexcept>
#include <iostream>
#include <boost/smart_ptr.hpp>
#include <boost/format.hpp>
#include <boost/thread.hpp>
#include <boost/asio.hpp>

#include <mxp.h>

#include "generate_messages.h"

using namespace boost;
using namespace mxp;

/*! A very simple message handler class, that just stored the message
    details as they are received.
 */
class message_handler {
public:
    std::vector<shared_ptr<message::message> > messages;
    std::vector<uint32_t>                      session_ids;
    std::vector<asio::ip::udp::endpoint>       endpoints;

    void handler(shared_ptr<message::message>  message,
                 uint32_t                      session_id,
                 asio::ip::udp::endpoint       ep) {

        messages.push_back(message);
        session_ids.push_back(session_id);
        endpoints.push_back(ep);
    }
};

/*! Wait for a specific type of message to be received.

    \param handler the message handler to check for received messages
    \param response_type the type of message to wait for
    \param timeout the maximum amount of time to wait for
    \throws std::invalid_argument if the timeout is reached, and the
            requested session type is not received.
 */
static shared_ptr<message::message>
wait_for_message(message_handler          & handler,
                 message::type              response_type,
                 posix_time::time_duration  timeout) {

    this_thread::yield();
    this_thread::sleep(timeout);

    unsigned int limit = handler.messages.size();
    for (unsigned int i = 0; i < limit; ++i) {
        if (handler.messages[i]->get_type() == response_type) {
            return handler.messages[i];
        }
    }

    throw std::invalid_argument(
                   str(format("timeout exceeded waiting for message type %d")
                               % response_type));
}

/*! Exchange reference messages

    \param hostname the name of the IOT server to connect to
    \param port the port at the IOT server to connect to
    \throws std::invalid_argument on issues
*/
static void exchange_reference_messages(const char *hostname,
                                        const char *port) {

    asio::io_service                ioservice;
    asio::ip::udp::resolver         resolver(ioservice);
    asio::ip::udp::resolver::query  query(asio::ip::udp::v4(),
                                          hostname, port);
    asio::ip::udp::endpoint         server_address = *resolver.resolve(query);

    message_handler                 client_message_handler;
    shared_ptr<message::message>    response;
    shared_ptr<message::join_rsp>   join_response;
    uint32_t                        session_id;
    message::response_fragment    * rh;
    message::object_fragment      * of;
    message::object_fragment        off;

    const posix_time::time_duration     timeout = posix_time::milliseconds(200);

    mxp::net::communication     client;
    client.connect_message_handler(bind(&message_handler::handler,
                                        &client_message_handler, _1, _2, _3));

    // connect using a join request message
    shared_ptr<message::message> msg =  generate_join_rq();
    session_id    = client.open_session(server_address, msg);
    response = wait_for_message(client_message_handler,
                                message::JOIN_RSP,
                                timeout);
    join_response = static_pointer_cast<message::join_rsp>(response);

    // send an inject request message, and anticipate an inject response
    client.send(generate_inject_rq(), session_id, true);
    response = wait_for_message(client_message_handler,
                                message::INJECT_RSP,
                                timeout);
    rh = &(static_pointer_cast<message::inject_rsp>(response)->response_header);
    if (rh->failure_code != 0) {
        throw std::invalid_argument("failure code does not match");
    }

    // wait for a sync begin event
    response = wait_for_message(client_message_handler,
                                message::SYNCHRONIZATION_BEGIN_EVENT,
                                timeout * 2);
    if (static_pointer_cast<message::synchronization_begin_event>(response)
                                                        ->object_count != 1u) {
        throw std::invalid_argument("object count does not match");
    }

    // wait for a perception event
    response = wait_for_message(client_message_handler,
                                message::PERCEPTION_EVENT,
                                timeout);
    of = &(static_pointer_cast<message::perception_event>(response)
                                                              ->object_header);
    init_object_fragment(off);
    off.owner_id = join_response->participant_id;
    if (!(*of == off)) {
        throw std::invalid_argument("object fragments does not match");
    }

    // wait for a sync end event
    response = wait_for_message(client_message_handler,
                                message::SYNCHRONIZATION_END_EVENT,
                                timeout);

    // send a modify request and expect a modify response
    client.send(generate_modify_rq(), session_id, true);
    response = wait_for_message(client_message_handler,
                                message::MODIFY_RSP,
                                timeout);
    rh = &(static_pointer_cast<message::modify_rsp>(response)->response_header);
    if (rh->failure_code != 0) {
        throw std::invalid_argument("failure code does not match");
    }

    // send an interaction request message
    client.send(generate_interact_rq(), session_id, true);
    // interact response is not implemented on the test server...

    // send an examine request message
    client.send(generate_examine_rq(), session_id, true);
    // examine response is not implemented on the test server...

    // send an eject request message, and wait for a response
    client.send(generate_eject_rq(), session_id, true);
    response = wait_for_message(client_message_handler,
                                message::EJECT_RSP,
                                timeout);
    rh = &(static_pointer_cast<message::modify_rsp>(response)->response_header);
    if (rh->failure_code != 0) {
        throw std::invalid_argument("failure code does not match");
    }

    // after an eject, see that a disappearance event is sent
    response = wait_for_message(client_message_handler,
                                message::DISAPPEARANCE_EVENT,
                                timeout);
    if (static_pointer_cast<message::disappearance_event>(response)
                                                        ->object_index != 1u) {
        throw std::invalid_argument("disappearance object id does not match");
    }

    // send a leave request message, and wait for a response
    client.send(generate_leave_rq(), session_id, true);
    response = wait_for_message(client_message_handler,
                                message::LEAVE_RSP,
                                timeout);
    rh = &(static_pointer_cast<message::modify_rsp>(response)->response_header);
    if (rh->failure_code != 0) {
        throw std::invalid_argument("failure code does not match");
    }

    // that's all, folks!

    client.close_session(session_id);
}

/*! Program entry point

  \param argc the number of command line arguments
  \param argv the command line arguments. two arguments are expected: the
         IOT server host name and port to connect to.
  \return 0 on successful execution, <0 on failures
*/
int main(int argc, char * argv[]) {
    if (argc != 3) {
        std::cerr << "Usage: " << argv[0] << " host port" << std::endl
                << std::endl;

        return -1;
    }

    try {
        exchange_reference_messages(argv[1], argv[2]);
    } catch (const std::invalid_argument & e) {
        std::cerr << e.what() << std::endl;

        return -2;
    }

    return 0;
}

ext: cxx

max_stars_repo_name: MoysheBenRabi/setp

max_issues_repo_name: MoysheBenRabi/setp

max_forks_repo_name: MoysheBenRabi/setp


text:
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

#include "util/s3_storage_backend.h"

#include <aws/s3/S3Client.h>
#include <aws/s3/model/CopyObjectRequest.h>
#include <aws/s3/model/DeleteObjectRequest.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <aws/s3/model/HeadObjectRequest.h>
#include <aws/s3/model/ListObjectsRequest.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <boost/algorithm/string.hpp>

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

#include "common/logging.h"
#include "gutil/strings/strip.h"
#include "util/s3_uri.h"
#include "util/s3_util.h"

namespace doris {

#ifndef CHECK_S3_CLIENT
#define CHECK_S3_CLIENT(client)                                    \
    if (!client) {                                                 \
        return Status::InternalError("init aws s3 client error."); \
    }
#endif

#ifndef CHECK_S3_PATH
#define CHECK_S3_PATH(uri, path)                                      \
    S3URI uri(path);                                                  \
    if (!uri.parse()) {                                               \
        return Status::InvalidArgument("s3 uri is invalid: " + path); \
    }
#endif

#ifndef RETRUN_S3_STATUS
#define RETRUN_S3_STATUS(response)                         \
    if (response.IsSuccess()) {                            \
        return Status::OK();                               \
    } else {                                               \
        return Status::InternalError(error_msg(response)); \
    }
#endif

S3StorageBackend::S3StorageBackend(const std::map<std::string, std::string>& prop)
        : _properties(prop) {
    _client = ClientFactory::instance().create(_properties);
    DCHECK(_client) << "init aws s3 client error.";
}

S3StorageBackend::~S3StorageBackend() {}

Status S3StorageBackend::download(const std::string& remote, const std::string& local) {
    CHECK_S3_CLIENT(_client);
    CHECK_S3_PATH(uri, remote);
    Aws::S3::Model::GetObjectRequest request;
    request.WithBucket(uri.get_bucket()).WithKey(uri.get_key());
    Aws::S3::Model::GetObjectOutcome response = _client->GetObject(request);
    if (response.IsSuccess()) {
        Aws::OFStream local_file;
        local_file.open(local, std::ios::out | std::ios::binary);
        if (local_file.good()) {
            local_file << response.GetResult().GetBody().rdbuf();
        }
        if (!local_file.good()) {
            return Status::InternalError("failed to write file: " + local);
        }
    } else {
        return Status::IOError("s3 download error: " + error_msg(response));
    }
    return Status::OK();
}

Status S3StorageBackend::direct_download(const std::string& remote, std::string* content) {
    CHECK_S3_CLIENT(_client);
    CHECK_S3_PATH(uri, remote);
    Aws::S3::Model::GetObjectRequest request;
    request.WithBucket(uri.get_bucket()).WithKey(uri.get_key());
    Aws::S3::Model::GetObjectOutcome response = _client->GetObject(request);
    if (response.IsSuccess()) {
        std::stringstream ss;
        ss << response.GetResult().GetBody().rdbuf();
        *content = ss.str();
    } else {
        return Status::IOError("s3 direct_download error: " + error_msg(response));
    }
    return Status::OK();
}

Status S3StorageBackend::upload(const std::string& local, const std::string& remote) {
    CHECK_S3_CLIENT(_client);
    CHECK_S3_PATH(uri, remote);
    Aws::S3::Model::PutObjectRequest request;
    request.WithBucket(uri.get_bucket()).WithKey(uri.get_key());

    const std::shared_ptr<Aws::IOStream> input_data = Aws::MakeShared<Aws::FStream>(
            local.c_str(), local.c_str(), std::ios_base::in | std::ios_base::binary);
    if (input_data->good()) {
        request.SetBody(input_data);
    }
    if (!input_data->good()) {
        return Status::InternalError("failed to read file: " + local);
    }
    Aws::S3::Model::PutObjectOutcome response = _client->PutObject(request);

    RETRUN_S3_STATUS(response);
}

Status S3StorageBackend::list(const std::string& remote_path, bool contain_md5, bool recursion,
                              std::map<std::string, FileStat>* files) {
    std::string normal_str(remote_path);
    if (!normal_str.empty() && normal_str.at(normal_str.size() - 1) != '/') {
        normal_str += '/';
    }
    CHECK_S3_CLIENT(_client);
    CHECK_S3_PATH(uri, normal_str);

    Aws::S3::Model::ListObjectsRequest request;
    request.WithBucket(uri.get_bucket()).WithPrefix(uri.get_key());
    if (!recursion) {
        request.WithDelimiter("/");
    }
    Aws::S3::Model::ListObjectsOutcome response = _client->ListObjects(request);
    if (response.IsSuccess()) {
        Aws::Vector<Aws::S3::Model::Object> objects = response.GetResult().GetContents();

        for (Aws::S3::Model::Object& object : objects) {
            std::string key = object.GetKey();
            if (key.at(key.size() - 1) == '/') {
                continue;
            }
            key = boost::algorithm::replace_first_copy(key, uri.get_key(), "");
            if (contain_md5) {
                size_t pos = key.find_last_of(".");
                if (pos == std::string::npos || pos == key.size() - 1) {
                    // Not found checksum separator, ignore this file
                    continue;
                }
                FileStat stat = {std::string(key, 0, pos), std::string(key, pos + 1),
                                 object.GetSize()};
                files->emplace(std::string(key, 0, pos), stat);
            } else {
                FileStat stat = {key, "", object.GetSize()};
                files->emplace(key, stat);
            }
        }
        return Status::OK();
    } else {
        return Status::InternalError("list form s3 error: " + error_msg(response));
    }
}

Status S3StorageBackend::rename(const std::string& orig_name, const std::string& new_name) {
    RETURN_IF_ERROR(copy(orig_name, new_name));
    return rm(orig_name);
}

Status S3StorageBackend::rename_dir(const std::string& orig_name, const std::string& new_name) {
    RETURN_IF_ERROR(copy_dir(orig_name, new_name));
    return rmdir(orig_name);
}

Status S3StorageBackend::direct_upload(const std::string& remote, const std::string& content) {
    CHECK_S3_CLIENT(_client);
    CHECK_S3_PATH(uri, remote);
    Aws::S3::Model::PutObjectRequest request;
    request.WithBucket(uri.get_bucket()).WithKey(uri.get_key());
    const std::shared_ptr<Aws::IOStream> input_data =
            Aws::MakeShared<Aws::StringStream>("upload_directly");
    *input_data << content.c_str();
    if (input_data->good()) {
        request.SetBody(input_data);
    }
    if (!input_data->good()) {
        return Status::InternalError("failed to read from string");
    }
    Aws::S3::Model::PutObjectOutcome response = _client->PutObject(request);

    RETRUN_S3_STATUS(response);
}

Status S3StorageBackend::rm(const std::string& remote) {
    LOG(INFO) << "S3 rm file: " << remote;
    CHECK_S3_CLIENT(_client);
    CHECK_S3_PATH(uri, remote);
    Aws::S3::Model::DeleteObjectRequest request;

    request.WithKey(uri.get_key()).WithBucket(uri.get_bucket());

    Aws::S3::Model::DeleteObjectOutcome response = _client->DeleteObject(request);

    RETRUN_S3_STATUS(response);
}

Status S3StorageBackend::rmdir(const std::string& remote) {
    CHECK_S3_CLIENT(_client);
    std::map<std::string, FileStat> files;
    std::string normal_path(remote);
    if (!normal_path.empty() && normal_path.at(normal_path.size() - 1) != '/') {
        normal_path += '/';
    }
    LOG(INFO) << "Remove S3 dir: " << remote;
    RETURN_IF_ERROR(list(normal_path, false, true, &files));

    for (auto& file : files) {
        std::string file_path = normal_path + file.second.name;
        RETURN_IF_ERROR(rm(file_path));
    }
    return Status::OK();
}

Status S3StorageBackend::copy(const std::string& src, const std::string& dst) {
    CHECK_S3_CLIENT(_client);
    CHECK_S3_PATH(src_uri, src);
    CHECK_S3_PATH(dst_uri, dst);
    Aws::S3::Model::CopyObjectRequest request;
    request.WithCopySource(src_uri.get_bucket() + "/" + src_uri.get_key())
            .WithKey(dst_uri.get_key())
            .WithBucket(dst_uri.get_bucket());

    Aws::S3::Model::CopyObjectOutcome response = _client->CopyObject(request);

    RETRUN_S3_STATUS(response);
}

Status S3StorageBackend::copy_dir(const std::string& src, const std::string& dst) {
    std::map<std::string, FileStat> files;
    LOG(INFO) << "Copy S3 dir: " << src << " -> " << dst;
    RETURN_IF_ERROR(list(src, false, true, &files));
    if (files.size() == 0) {
        LOG(WARNING) << "Nothing need to copy: " << src << " -> " << dst;
        return Status::OK();
    }
    for (auto& kv : files) {
        RETURN_IF_ERROR(copy(src + "/" + kv.first, dst + "/" + kv.first));
    }
    return Status::OK();
}

// dir is not supported by s3, it will cause something confusion.
Status S3StorageBackend::mkdir(const std::string& path) {
    return Status::OK();
}

// dir is not supported by s3, it will cause something confusion.
Status S3StorageBackend::mkdirs(const std::string& path) {
    return Status::OK();
}

Status S3StorageBackend::exist(const std::string& path) {
    CHECK_S3_CLIENT(_client);
    CHECK_S3_PATH(uri, path);
    Aws::S3::Model::HeadObjectRequest request;
    request.WithBucket(uri.get_bucket()).WithKey(uri.get_key());
    Aws::S3::Model::HeadObjectOutcome response = _client->HeadObject(request);
    if (response.IsSuccess()) {
        return Status::OK();
    } else if (response.GetError().GetResponseCode() == Aws::Http::HttpResponseCode::NOT_FOUND) {
        return Status::NotFound(path + " not exists!");
    } else {
        return Status::InternalError(error_msg(response));
    }
}

Status S3StorageBackend::exist_dir(const std::string& path) {
    std::map<std::string, FileStat> files;
    RETURN_IF_ERROR(list(path, false, true, &files));
    if (files.size() > 0) {
        return Status::OK();
    }
    return Status::NotFound(path + " not exists!");
}

Status S3StorageBackend::upload_with_checksum(const std::string& local, const std::string& remote,
                                              const std::string& checksum) {
    return upload(local, remote + "." + checksum);
}

template <typename AwsOutcome>
std::string S3StorageBackend::error_msg(const AwsOutcome& outcome) {
    std::stringstream out;
    out << "Error: [" << outcome.GetError().GetExceptionName() << ":"
        << outcome.GetError().GetMessage();
    return out.str();
}

} // end namespace doris


text:
/*=============================================================================

  NifTK: A software platform for medical image computing.

  Copyright (c) University College London (UCL). All rights reserved.

  This software is distributed WITHOUT ANY WARRANTY; without even
  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  PURPOSE.

  See LICENSE.txt in the top level directory for details.

=============================================================================*/
/*!
* \file niftkVTKIterativeClosestPointRegister.cxx
* \page niftkVTKIterativeClosestPointRegister
* \section niftkVTKIterativeClosestPointRegsisterSummary Uses vtkIterativeClosestPointTransform to register two vtk polydata sets
*
* This program uses vtkIterativeClosestPointTransform via niftkVTKIterativeClosestPoint.
* Optionally the transformed source may be written to a vtkpolydata file.
*
* \section niftkVTKIterativeClosestPointRegisterCaveat Caveats
* \li vtkIterativeClosestPointTransform is a point to surface iterative closest point algorithm.
* Therefore at least one of the input polydata must contain surfaces.
*
*/

#include <niftkVTKIterativeClosestPointRegisterCLP.h>
#include <niftkVTKIterativeClosestPoint.h>
#include <niftkVTK4PointsReader.h>
#include <niftkVTKFunctions.h>

#include <niftkConversionUtils.h>
#include <vtkPolyData.h>
#include <vtkPolyDataReader.h>
#include <vtkPolyDataWriter.h>
#include <vtkMatrix4x4.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkDelaunay2D.h>
#include <vtkPolyDataMapper.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkObjectFactory.h>
#include <vtkDoubleArray.h>
#include <vtkLookupTable.h>
#include <vtkUnsignedCharArray.h>
#include <vtkPointData.h>
#include <vtkMinimalStandardRandomSequence.h>

#include <boost/algorithm/string/predicate.hpp>

struct arguments
{
  std::string targetPolyDataFile;
  std::string sourcePolyDataFile;
  int maxIterations;
  int maxPoints;
  bool visualise;
  bool randomTransform;
  bool perturbTarget;
  bool writeout;
  std::string outPolyDataFile;
};

// Define interaction style
class KeyPressInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
  static KeyPressInteractorStyle* New();
  vtkTypeMacro(KeyPressInteractorStyle, vtkInteractorStyleTrackballCamera);

  virtual void OnKeyPress() override
  {
    // Get the keypress
    vtkRenderWindowInteractor *rwi = this->Interactor;
    std::string key = rwi->GetKeySym();

    if(key == "Right")
    {
      if ( target->GetProperty()->GetOpacity() == 0.0 )
      {
        target->GetProperty()->SetOpacity(1.0);
        source->GetProperty()->SetOpacity(0.2);
      }
      else
      {
        if ( solution->GetProperty()->GetOpacity() < 1.0 )
        {
          solution->GetProperty()->SetOpacity(solution->GetProperty()->GetOpacity() + 0.5);
        }
      }
    }
    if(key == "Left")
    {
      if ( solution->GetProperty()->GetOpacity() > 0.0 )
      {
        solution->GetProperty()->SetOpacity(solution->GetProperty()->GetOpacity() - 0.5);
      }
      else
      {
        if ( target->GetProperty()->GetOpacity() == 1.0 )
        {
          target->GetProperty()->SetOpacity(0.0);
          source->GetProperty()->SetOpacity(1.0);
        }
      }
    }
    // Forward events
    vtkInteractorStyleTrackballCamera::OnKeyPress();
    this->GetCurrentRenderer()->GetRenderWindow()->Render();
  }

  vtkActor *source;
  vtkActor *solution;
  vtkActor *target;
};
vtkStandardNewMacro(KeyPressInteractorStyle);

/**
 * \brief Run a VTK ICP on two poly data
 */
int main(int argc, char** argv)
{
  // To parse command line args.
  PARSE_ARGS;

  // To pass around command line args
  struct arguments args;
  args.maxIterations = maxNumberOfIterations;
  args.maxPoints = maxNumberOfPoints;
  args.visualise = !noVisualisation;
  args.randomTransform = rndTrans;
  args.perturbTarget = rndPerturb;
  args.writeout = outputTransformedSource.length() > 0;
  args.sourcePolyDataFile = sourcePolyData;
  args.targetPolyDataFile = targetPolyData;
  args.outPolyDataFile = outputTransformedSource;

  // Validate command line args
  if (args.sourcePolyDataFile.length() == 0 || args.targetPolyDataFile.length() == 0)
    {
      commandLine.getOutput()->usage(commandLine);
      return EXIT_FAILURE;
    }

  vtkSmartPointer<vtkPolyData> source = vtkSmartPointer<vtkPolyData>::New();
  vtkSmartPointer<vtkPolyData> target = vtkSmartPointer<vtkPolyData>::New();
  vtkSmartPointer<vtkPolyData> c_source = vtkSmartPointer<vtkPolyData>::New();
  vtkSmartPointer<vtkPolyData> solution = vtkSmartPointer<vtkPolyData>::New();

  if ( boost::algorithm::ends_with(args.sourcePolyDataFile , ".txt") )
  {
    vtkSmartPointer<niftk::VTK4PointsReader> sourceReader = vtkSmartPointer<niftk::VTK4PointsReader>::New();
    sourceReader->SetFileName(args.sourcePolyDataFile.c_str());
    sourceReader->SetClippingOn(2,50, 200);
    sourceReader->Update();
    source->ShallowCopy (sourceReader->GetOutput());
    std::cout << "Loaded text file:" << args.sourcePolyDataFile << std::endl;
  }
  else
  {
    vtkSmartPointer<vtkPolyDataReader> sourceReader = vtkSmartPointer<vtkPolyDataReader>::New();
    sourceReader->SetFileName(args.sourcePolyDataFile.c_str());
    sourceReader->Update();
    source->ShallowCopy (sourceReader->GetOutput());
    std::cout << "Loaded PolyData:" << args.sourcePolyDataFile << std::endl;
  }
  if ( args.randomTransform )
  {
    c_source->DeepCopy (source);
  }

  if ( boost::algorithm::ends_with(args.targetPolyDataFile , ".txt") )
  {
    vtkSmartPointer<niftk::VTK4PointsReader> targetReader = vtkSmartPointer<niftk::VTK4PointsReader>::New();
    targetReader->SetFileName(args.targetPolyDataFile.c_str());
    targetReader->SetClippingOn(2,50, 200);
    targetReader->Update();
    target->ShallowCopy (targetReader->GetOutput());
    std::cout << "Loaded text file:" << args.targetPolyDataFile << std::endl;
  }
  else
  {
    vtkSmartPointer<vtkPolyDataReader> targetReader = vtkSmartPointer<vtkPolyDataReader>::New();
    targetReader->SetFileName(args.targetPolyDataFile.c_str());
    targetReader->Update();
    target->ShallowCopy (targetReader->GetOutput());
    std::cout << "Loaded PolyData:" << args.targetPolyDataFile << std::endl;
  }

  niftk::VTKIterativeClosestPoint * icp = new niftk::VTKIterativeClosestPoint();
  icp->SetICPMaxLandmarks(args.maxPoints);
  icp->SetICPMaxIterations(args.maxIterations);
  icp->SetSource(source);
  icp->SetTarget(target);

  vtkSmartPointer<vtkTransform> StartTrans = vtkSmartPointer<vtkTransform>::New();
  if ( args.randomTransform )
  {
    vtkSmartPointer<vtkMinimalStandardRandomSequence> Uni_Rand = vtkSmartPointer<vtkMinimalStandardRandomSequence>::New();
    Uni_Rand->SetSeed(time(NULL));
    double scaleSD = -1.0;
    StartTrans = niftk::RandomTransform ( 10.0 , 10.0 , 10.0, 10.0 , 10.0, 10.0, *Uni_Rand, scaleSD);
    niftk::TranslatePolyData ( source , StartTrans);
  }
  if ( args.perturbTarget )
  {
    niftk::PerturbPolyData(target, 1.0, 1.0 , 1.0);
  }
  icp->Run();

  vtkSmartPointer<vtkMatrix4x4> m = icp->GetTransform();
  std::cout << "The Resulting transform is " << *m << std::endl;
  if ( args.randomTransform || args.visualise )
  {
    icp->ApplyTransform(solution);
  }
  //If testing with random transform put out an error metric
  if ( args.randomTransform )
  {
    vtkSmartPointer<vtkMatrix4x4> Residual  = vtkSmartPointer<vtkMatrix4x4>::New();
    StartTrans->Concatenate(m);
    StartTrans->GetInverse(Residual);
    double * StartPoint = new double [4];
    double * EndPoint = new double [4];
    StartPoint [0 ] = 160;
    StartPoint [1] = 80;
    StartPoint [2] = 160;
    StartPoint [3] = 1;
    EndPoint= Residual->MultiplyDoublePoint(StartPoint);
    double MagError = 0;
    for ( int i = 0; i < 4; i ++ )
    {
      MagError += (EndPoint[i] - StartPoint[i]) * ( EndPoint[i] - StartPoint[i]);
    }
    MagError = sqrt(MagError);
    std::cout << "Residual Error = "  << MagError << std::endl;
    std::cout << "Residual Transform = " << *Residual;
    //do a difference image
    niftk::DistancesToColorMap(c_source, solution);

  }

  if ( args.writeout == true )
  {
     vtkSmartPointer<vtkPolyData> solution = vtkSmartPointer<vtkPolyData>::New();
     icp->ApplyTransform(solution);
     vtkPolyDataWriter *writer = vtkPolyDataWriter::New();
     writer->SetInputDataObject(solution);
     writer->SetFileName(args.outPolyDataFile.c_str());
     writer->Update();
  }
  if ( args.visualise )
  {

    vtkSmartPointer<vtkPolyDataMapper> sourceMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    if ( source->GetNumberOfCells() == 0 )
    {
      std::cerr << "There are no cells in the source data, running delaunay filter\n";
      vtkSmartPointer<vtkDelaunay2D> delaunay = vtkSmartPointer<vtkDelaunay2D>::New();
      delaunay->SetInputDataObject(source);
      sourceMapper->SetInputDataObject(delaunay->GetOutput());
      delaunay->Update();
    }
    else
    {
      sourceMapper->SetInputDataObject(source);
    }
    vtkSmartPointer<vtkActor> sourceActor = vtkSmartPointer<vtkActor>::New();
    sourceActor->SetMapper(sourceMapper);
    sourceActor->GetProperty()->SetColor(1,0,0);
    sourceActor->GetProperty()->SetPointSize(4);

    vtkSmartPointer<vtkPolyDataMapper> targetMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    if ( target->GetNumberOfCells() == 0 )
    {
      std::cerr << "There are no cells in the target data, running delaunay filter\n";
      vtkSmartPointer<vtkDelaunay2D> delaunay = vtkSmartPointer<vtkDelaunay2D>::New();
      delaunay->SetInputDataObject(target);
      targetMapper->SetInputDataObject(delaunay->GetOutput());
      delaunay->Update();
    }
    else
    {
      targetMapper->SetInputDataObject(target);
    }

    vtkSmartPointer<vtkActor> targetActor = vtkSmartPointer<vtkActor>::New();
    targetActor->SetMapper(targetMapper);
    targetActor->GetProperty()->SetColor(0,1,0);
    targetActor->GetProperty()->SetPointSize(4);
    targetActor->GetProperty()->SetOpacity(0.0);
    targetActor->GetProperty()->SetRepresentationToPoints();

    vtkSmartPointer<vtkPolyDataMapper> solutionMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    if ( solution->GetNumberOfCells() == 0 )
    {
      vtkSmartPointer<vtkDelaunay2D> delaunay = vtkSmartPointer<vtkDelaunay2D>::New();
      delaunay->SetInputDataObject(solution);
      solutionMapper->SetInputDataObject(delaunay->GetOutput());
      delaunay->Update();
    }
    else
    {
      solutionMapper->SetInputDataObject(solution);
    }
    vtkSmartPointer<vtkActor> solutionActor =
    vtkSmartPointer<vtkActor>::New();
    solutionActor->SetMapper(solutionMapper);
    solutionActor->GetProperty()->SetColor(0,0,1);
    solutionActor->GetProperty()->SetOpacity(0.0);
    solutionActor->GetProperty()->SetPointSize(3);

    // Create a renderer, render window, and interactor
    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    vtkSmartPointer<KeyPressInteractorStyle> style = vtkSmartPointer<KeyPressInteractorStyle>::New();
    style->target = targetActor;
    style->source = sourceActor;
    style->solution = solutionActor;
    renderWindowInteractor->SetInteractorStyle(style);
    style->SetCurrentRenderer(renderer);

    // Add the actor to the scene
    renderer->AddActor(sourceActor);
    renderer->AddActor(targetActor);
    renderer->AddActor(solutionActor);
    renderer->SetBackground(.3, .6, .3); // Background color green

    // Render and interact
    renderWindow->Render();
    renderWindowInteractor->Start();

  }
}

max_stars_repo_name: NifTK/NifTK

max_issues_repo_name: NifTK/NifTK

max_forks_repo_name: NifTK/NifTK


text:
#ifndef __COMPUTE_HPP
#define __COMPUTE_HPP

#include <boost/simd/pack.hpp>
#include <boost/simd/arch/common/spec.hpp>

//! [runtime-arch]
void compute(float *a, float *b, float *res, int size, boost::simd::avx2_ const&);
void compute(float *a, float *b, float *res, int size, boost::simd::avx_ const&);
void compute(float *a, float *b, float *res, int size, boost::simd::sse4_2_ const&);
void compute(float *a, float *b, float *res, int size, boost::simd::sse4_1_ const&);
void compute(float *a, float *b, float *res, int size, boost::simd::sse3_ const&);
void compute(float *a, float *b, float *res, int size, boost::simd::sse2_ const&);
void compute(float *a, float *b, float *res, int size, boost::dispatch::cpu_ const&);
//! [runtime-arch]

#endif

ext: hpp


text:
// Copyright (C) 2001-2003
// William E. Kempf
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/thread/detail/config.hpp>

#include <boost/thread/exceptions.hpp>
#include <cstring>
#include <string>

namespace boost {

thread_exception::thread_exception()
    : m_sys_err(0)
{
}

thread_exception::thread_exception(int sys_err_code)
    : m_sys_err(sys_err_code)
{
}

thread_exception::~thread_exception() throw()
{
}

int thread_exception::native_error() const
{
    return m_sys_err;
}

lock_error::lock_error()
{
}

lock_error::lock_error(int sys_err_code)
    : thread_exception(sys_err_code)
{
}

lock_error::~lock_error() throw()
{
}

const char* lock_error::what() const throw()
{
    return "boost::lock_error";
}

thread_resource_error::thread_resource_error()
{
}

thread_resource_error::thread_resource_error(int sys_err_code)
    : thread_exception(sys_err_code)
{
}

thread_resource_error::~thread_resource_error() throw()
{
}

const char* thread_resource_error::what() const throw()
{
    return "boost::thread_resource_error";
}

unsupported_thread_option::unsupported_thread_option()
{
}

unsupported_thread_option::unsupported_thread_option(int sys_err_code)
    : thread_exception(sys_err_code)
{
}

unsupported_thread_option::~unsupported_thread_option() throw()
{
}

const char* unsupported_thread_option::what() const throw()
{
    return "boost::unsupported_thread_option";
}

invalid_thread_argument::invalid_thread_argument()
{
}

invalid_thread_argument::invalid_thread_argument(int sys_err_code)
    : thread_exception(sys_err_code)
{
}

invalid_thread_argument::~invalid_thread_argument() throw()
{
}

const char* invalid_thread_argument::what() const throw()
{
    return "boost::invalid_thread_argument";
}

thread_permission_error::thread_permission_error()
{
}

thread_permission_error::thread_permission_error(int sys_err_code)
    : thread_exception(sys_err_code)
{
}

thread_permission_error::~thread_permission_error() throw()
{
}

const char* thread_permission_error::what() const throw()
{
    return "boost::thread_permission_error";
}

} // namespace boost

max_stars_repo_name: dbremner/framewave

max_issues_repo_name: dbremner/framewave

max_forks_repo_name: dbremner/framewave


text:
/*============================================================
  /   COPYRIGHT: JDSU T&M PTE LTD
  /   Filename    : kpi_throughput_csv.h
  /   Description: This class outputs the CSV output
  /                of the throughput records
  /
  /=============================================================
 */

#include <stdlib.h>
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/foreach.hpp>
#include <boost/functional/hash.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "xtreme/kpi_agent/internal/kpi_throughput_csv.h"
#include "xtreme/kpi_agent/internal/kpi_common_includes.h"
#include "xtreme/common/timestamp.h"
#include "xtreme/common/utils.h"

namespace {
// Prints an xtreme::Timestamp instance as sec_.nsec_.
std::ostream& operator<<(std::ostream& stream, const xtreme::Timestamp& t) {
    stream << t.sec_ << "." << t.nsec_;
    return stream;
}
}

// using namespace boost::gregorian;
// using namespace boost::local_time;
// using namespace boost::posix_time;

namespace xtreme {
namespace usart {

CKPIThroughputCsv::CKPIThroughputCsv(xtreme::jobm::JobPtr job, const std::string& appn_name, uint32_t buf_size, bool output_debug_columns)
    :CKPICsv(appn_name), job_(job) {
    csv_generator_ = new xtreme::aggregator::CSVGenerator(buf_size, output_debug_columns);
    csv_generator_->setApplianceName(m_appn_name);
}

CKPIThroughputCsv::~CKPIThroughputCsv() {
    if (csv_generator_) {
        delete csv_generator_;
    }
}

// Write the headers to the CSV file
void CKPIThroughputCsv::BeginCsv(KPI_CSV_INFO *pInfo) {
}

// Write the actual data to the CSV file
int CKPIThroughputCsv::OutputCsv(const EventPtr &event, const xtreme::Timestamp &start_time,
                                 const xtreme::Timestamp &end_time) {
    // USART_TRACE("Outputting the MDR..");
    const xtreme::BatchedMDREvent* mdr_event = static_cast<const xtreme::BatchedMDREvent*>(event.get());
    switch (event->type()) {
    case kEVENT_BATCHED_MDR:
        output_mdr(mdr_event, start_time, end_time);
        break;
    default:
        USART_WARN("Received an invalid event = " << static_cast<int>(event->type()));
        break;
    }
    return USART_SUCCESS;
}

bool CKPIThroughputCsv::output_mdr(const xtreme::BatchedMDREvent* mdr_event,
                                   const xtreme::Timestamp &start_time,
                                   const xtreme::Timestamp &end_time) {
  const xtreme::MDREventMap& combined_map = mdr_event->batch();
  IDMask measurement_set_id = mdr_event->measurement_set_id();
  // USART_DEBUG("CSV Filename = " << job_->csv_file());
  int measurement_set_id_number = measurement_set_id.GetHighestBitIndex();
  m_csv_rows += combined_map.size();

  std::size_t seed = 0;
  boost::hash_combine(seed, measurement_set_id_number);
  boost::hash_combine(seed, start_time.sec_);
  boost::hash_combine(seed, end_time.sec_);
  if (!csv_generator_->is_open(seed)) {
    const MDREventPtr &event = combined_map.begin()->second;
    BOOST_FOREACH(xtreme::jobm::MeasurementSet *meas_set, job_->get_measurement_set_list()) {
#if 0
      USART_DEBUG("meas_set->meas_set_id = " << meas_set->meas_set_id_
          << " measurement_set_id_number = " << measurement_set_id);
#endif
      if (job_->csv_create_option() == xtreme::jobm::kCsvContinuous) {
        start_time_str_ = end_time_str_ = "";
      } else {
        xtreme::convert_timestamp_to_string(start_time, start_time_str_);
        xtreme::convert_timestamp_to_string(end_time, end_time_str_);
      }
      if (meas_set->meas_set_id_ == measurement_set_id) {
        if (meas_set->record_type() == xtreme::jobm::kFLOW_BASE) {
          csv_generator_->open_csv_file(
              seed, job_->csv_file(), start_time_str_, end_time_str_, meas_set,
              job_->integration_interval(),
              event->context(),
              job_);
        } else if (meas_set->record_type() == xtreme::jobm::kINTERVAL_BASE) {
          csv_generator_->open_csv_file(
              seed, job_->csv_file(), start_time_str_, end_time_str_, meas_set,
              job_->integration_interval(),
              job_);
        }
      }
    }
  }

  aggregator::OFStreamHandle& stream = *(csv_generator_->get_stream_handle(seed));
  BOOST_FOREACH(const MDREventMap::value_type &pair, combined_map){
    csv_generator_->csv_out(pair, stream);
  }
  return true;
}

#if 1
void CKPIThroughputCsv::EndCsv() {
    csv_generator_->csv_end();
}
#endif

} }  // namespace xtreme::usart


text:
#include <iostream>

#include <boost/asio/read_until.hpp>
#include <boost/filesystem.hpp>
#include <boost/log/trivial.hpp>

#include "server.h"

Session::Session(TcpSocket t_socket)
    : m_socket(std::move(t_socket))
{
}

void Session::doRead()
{
    auto self = shared_from_this();
    async_read_until(m_socket, m_requestBuf_, "\n\n",
        [this, self](boost::system::error_code ec, size_t bytes)
        {
            if (!ec)
                processRead(bytes);
            else
                handleError(__FUNCTION__, ec);
        });
}

void Session::processRead(size_t t_bytesTransferred)
{
    BOOST_LOG_TRIVIAL(trace) << __FUNCTION__ << "(" << t_bytesTransferred << ")"
        << ", in_avail = " << m_requestBuf_.in_avail() << ", size = "
        << m_requestBuf_.size() << ", max_size = " << m_requestBuf_.max_size() << ".";

    std::istream requestStream(&m_requestBuf_);
    readData(requestStream);

    auto pos = m_fileName.find_last_of('\\');
    if (pos != std::string::npos)
        m_fileName = m_fileName.substr(pos + 1);

    createFile();

    // write extra bytes to file
    do {
        requestStream.read(m_buf.data(), m_buf.size());
        BOOST_LOG_TRIVIAL(trace) << __FUNCTION__ << " write " << requestStream.gcount() << " bytes.";
        m_outputFile.write(m_buf.data(), requestStream.gcount());
    } while (requestStream.gcount() > 0);

    auto self = shared_from_this();
    m_socket.async_read_some(boost::asio::buffer(m_buf.data(), m_buf.size()),
        [this, self](boost::system::error_code ec, size_t bytes)
        {
            if (!ec)
                doReadFileContent(bytes);
            else
                handleError(__FUNCTION__, ec);
        });
}

void Session::readData(std::istream &stream)
{
    stream >> m_fileName;
    stream >> m_fileSize;
    stream.read(m_buf.data(), 2);

    BOOST_LOG_TRIVIAL(trace) << m_fileName << " size is " << m_fileSize
        << ", tellg = " << stream.tellg();
}

void Session::createFile()
{
    m_outputFile.open(m_fileName, std::ios_base::binary);
    if (!m_outputFile) {
        BOOST_LOG_TRIVIAL(error) << __LINE__ << ": Failed to create: " << m_fileName;
        return;
    }
}

void Session::doReadFileContent(size_t t_bytesTransferred)
{
    if (t_bytesTransferred > 0) {
        m_outputFile.write(m_buf.data(), static_cast<std::streamsize>(t_bytesTransferred));

        BOOST_LOG_TRIVIAL(trace) << __FUNCTION__ << " recv " << m_outputFile.tellp() << " bytes";

        if (m_outputFile.tellp() >= static_cast<std::streamsize>(m_fileSize)) {
            std::cout << "Received file: " << m_fileName << std::endl;
            return;
        }
    }
    auto self = shared_from_this();
    m_socket.async_read_some(boost::asio::buffer(m_buf.data(), m_buf.size()),
        [this, self](boost::system::error_code ec, size_t bytes)
        {
            doReadFileContent(bytes);
        });
}

void Session::handleError(std::string const& t_functionName, boost::system::error_code const& t_ec)
{
    BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << " in " << t_functionName << " due to "
        << t_ec << " " << t_ec.message() << std::endl;
}

Server::Server(IoService& t_ioService, short t_port, std::string const& t_workDirectory)
    : m_socket(t_ioService),
    m_acceptor(t_ioService, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), t_port)),
    m_workDirectory(t_workDirectory)
{
    std::cout << "Server started\n";

    createWorkDirectory();

    doAccept();
}

void Server::doAccept()
{
    m_acceptor.async_accept(m_socket,
        [this](boost::system::error_code ec)
    {
        if (!ec)
            std::make_shared<Session>(std::move(m_socket))->start();

        doAccept();
    });
}

void Server::createWorkDirectory()
{
    using namespace boost::filesystem;
    auto currentPath = path(m_workDirectory);
    if (!exists(currentPath) && !create_directory(currentPath))
        BOOST_LOG_TRIVIAL(error) << "Coudn't create working directory: " << m_workDirectory;
    current_path(currentPath);
}

max_stars_repo_name: HoriaCool/FileTransfer

max_issues_repo_name: HoriaCool/FileTransfer

max_forks_repo_name: HoriaCool/FileTransfer


text:
// -----------------------------------------------------------------------------
// Fern © Geoneric
//
// This file is part of Geoneric Fern which is available under the terms of
// the GNU General Public License (GPL), version 2. If you do not want to
// be bound by the terms of the GPL, you may purchase a proprietary license
// from Geoneric (http://www.geoneric.eu/contact).
// -----------------------------------------------------------------------------
#define BOOST_TEST_MODULE fern algorithm accumulator range
#include <boost/test/unit_test.hpp>
#include "fern/algorithm/accumulator/range.h"

namespace faa = fern::algorithm::accumulator;

BOOST_AUTO_TEST_CASE(default_construct)
{
    faa::Range<int> range;
    // Don't calculate the range. It doesn't make sense.
}

BOOST_AUTO_TEST_CASE(accumulate)
{
    faa::Range<int> range(5);
    BOOST_CHECK_EQUAL(range(), 0);

    range(2);
    BOOST_CHECK_EQUAL(range(), 3);

    range(7);
    BOOST_CHECK_EQUAL(range(), 5);

    range = 3;
    BOOST_CHECK_EQUAL(range(), 0);
}

BOOST_AUTO_TEST_CASE(merge)
{
    auto range(faa::Range<int>(5) | faa::Range<int>(3));
    BOOST_CHECK_EQUAL(range(), 2);
}

ext: cc

max_stars_repo_name: quanpands/wflow

max_issues_repo_name: quanpands/wflow

max_forks_repo_name: quanpands/wflow


text:
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------

#include "stdafx.h"
#include "Common/WaitHandle.h"
#include "CompletedAsyncOperation.h"

#include <boost/test/unit_test.hpp>
#include "Common/boost-taef.h"

namespace Common
{
    using namespace std;

    class FabricComponentTestAsyncOperation : public AsyncOperation
    {
        DENY_COPY(FabricComponentTestAsyncOperation)

    public:
        FabricComponentTestAsyncOperation(
            ErrorCode error,
            TimeSpan timeout,
            AsyncCallback const & callback,
            AsyncOperationSPtr const & asyncOperationParent)
            : error_(error),
            timeoutHelper_(timeout),
            AsyncOperation(callback, asyncOperationParent)
        {
        }

        static ErrorCode End(AsyncOperationSPtr const& operation)
        {
            auto thisPtr = AsyncOperation::End<FabricComponentTestAsyncOperation>(operation);
            return thisPtr->Error;
        }

    protected:
        virtual void OnStart(AsyncOperationSPtr const & thisSPtr)
        {
            Threadpool::Post([this, thisSPtr]()
            {
                OnTimeout(thisSPtr);
            }, timeoutHelper_.GetRemainingTime());
        }

        virtual void OnTimeout(AsyncOperationSPtr const & operation)
        {
            TryComplete(operation, error_);
        }

    private:

        ErrorCode error_;
        TimeoutHelper timeoutHelper_;
    };

    class FauxAsyncFabricComponent : public AsyncFabricComponent
    {
    public:
        FauxAsyncFabricComponent()
            : error_(ErrorCodeValue::Success)
        {
            error_.ReadValue();
        }

        __declspec(property(put=set_Error)) ErrorCode Error;
        void set_Error(ErrorCode error)
        {
            error_ = error;
        }

    protected:
        Common::AsyncOperationSPtr OnBeginOpen(
            TimeSpan timeout,
            Common::AsyncCallback const & callback,
            Common::AsyncOperationSPtr const & parent)
        {
            return AsyncOperation::CreateAndStart<FabricComponentTestAsyncOperation>(error_, timeout, callback, parent);
        }

        Common::ErrorCode OnEndOpen(Common::AsyncOperationSPtr const & asyncOperation)
        {
            return FabricComponentTestAsyncOperation::End(asyncOperation);
        }

        Common::AsyncOperationSPtr OnBeginClose(
            TimeSpan timeout,
            Common::AsyncCallback const & callback,
            Common::AsyncOperationSPtr const & parent)
        {
            return AsyncOperation::CreateAndStart<FabricComponentTestAsyncOperation>(error_, timeout, callback, parent);
        }

        Common::ErrorCode OnEndClose(Common::AsyncOperationSPtr const & asyncOperation)
        {
            auto errorCode = FabricComponentTestAsyncOperation::End(asyncOperation);
            return errorCode;
        }

        void OnAbort()
        {
            error_.ReadValue();
        }

        ErrorCode error_;
    };

    class AsyncFabricComponentTest
    {
    };

    BOOST_FIXTURE_TEST_SUITE(AsyncFabricComponentTestSuite,AsyncFabricComponentTest)

    BOOST_AUTO_TEST_CASE(AsyncFabricComponentMemoryTest)
    {
        ManualResetEvent stopper(false);
        FauxAsyncFabricComponent component;
        component.BeginOpen(
            TimeSpan::Zero,
            [&component, &stopper] (AsyncOperationSPtr const& operation) -> void
        {
            VERIFY_IS_TRUE(component.EndOpen(operation).IsSuccess());
            stopper.Set();
        });
        stopper.WaitOne();
        stopper.Reset();
        component.BeginClose(
            TimeSpan::Zero,
            [&component, &stopper] (AsyncOperationSPtr const& operation) -> void
        {
            VERIFY_IS_TRUE(component.EndClose(operation).IsSuccess());
            stopper.Set();
        });
        stopper.WaitOne();
    }

    BOOST_AUTO_TEST_CASE(FabricComponentStateTest)
    {
        ManualResetEvent stopper(false);
        FauxAsyncFabricComponent component;
        TimeSpan openCloseTimeout = TimeSpan::FromSeconds(5);

        //Assert constructed component is in Created state
        VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Created);

        component.BeginOpen(
            openCloseTimeout,
            [&component, &stopper] (AsyncOperationSPtr const& operation) -> void
        {
            VERIFY_IS_TRUE(component.EndOpen(operation).IsSuccess());
            VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Opened);
            stopper.Set();
        });

        VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Opening);
        stopper.WaitOne();

        stopper.Reset();
        component.BeginClose(
            openCloseTimeout,
            [&component, &stopper] (AsyncOperationSPtr const& operation) -> void
        {
            VERIFY_IS_TRUE(component.EndClose(operation).IsSuccess());
            VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Closed);
            stopper.Set();
        });

        VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Closing);
        stopper.WaitOne();

        component.Abort();
        VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Aborted);
    }

    BOOST_AUTO_TEST_CASE(FabricComponentTransitionToAbortedTest)
    {
        ManualResetEvent stopper(false);
        //Assert Created->Aborted is valid
        FauxAsyncFabricComponent component1;
        VERIFY_IS_TRUE(component1.State.Value == FabricComponentState::Created);
        component1.Abort();
        VERIFY_IS_TRUE(component1.State.Value == FabricComponentState::Aborted);

        //Assert Opened->Aborted is valid
        FauxAsyncFabricComponent component2;
        TimeSpan openCloseTimeout = TimeSpan::Zero;
        component2.BeginOpen(
            openCloseTimeout,
            [&component2, &stopper] (AsyncOperationSPtr const& operation) -> void
        {
            VERIFY_IS_TRUE(component2.EndOpen(operation).IsSuccess());
            VERIFY_IS_TRUE(component2.State.Value == FabricComponentState::Opened);
            stopper.Set();
        });

        stopper.WaitOne();
        component2.Abort();
        VERIFY_IS_TRUE(component2.State.Value == FabricComponentState::Aborted);
        stopper.Reset();

        //Assert Opening->Aborted is valid and component stays in Opening state at
        //end of Abort and moves into Aborted once open completes
        FauxAsyncFabricComponent component3;
        openCloseTimeout = TimeSpan::FromSeconds(5);
        component3.BeginOpen(
            openCloseTimeout,
            [&component3, &stopper] (AsyncOperationSPtr const& operation) -> void
        {
            VERIFY_IS_TRUE(component3.EndOpen(operation).ReadValue() == ErrorCodeValue::FabricComponentAborted);
            VERIFY_IS_TRUE(component3.State.Value == FabricComponentState::Aborted);
            stopper.Set();
        });

        VERIFY_IS_TRUE(component3.State.Value == FabricComponentState::Opening);
        component3.Abort();
        VERIFY_IS_TRUE(component3.State.Value == FabricComponentState::Opening);
        stopper.WaitOne();

        stopper.Reset();

        //Assert Closed->Aborted is valid. Already verified in FabricComponentStateTest

        //Assert Closing->Aborted is valid and component stays in Closing state at
        //end of Abort and moves into Aborted once close completes
        FauxAsyncFabricComponent component4;
        openCloseTimeout = TimeSpan::Zero;
        component4.BeginOpen(
            openCloseTimeout,
            [&component4, &stopper] (AsyncOperationSPtr const& operation) -> void
        {
            VERIFY_IS_TRUE(component4.EndOpen(operation).IsSuccess());
            VERIFY_IS_TRUE(component4.State.Value == FabricComponentState::Opened);
            stopper.Set();
        });

        stopper.WaitOne();
        stopper.Reset();

        openCloseTimeout = TimeSpan::FromSeconds(5);
        component4.BeginClose(
            openCloseTimeout,
            [&component4, &stopper] (AsyncOperationSPtr const& operation) -> void
        {
            VERIFY_IS_TRUE(component4.EndClose(operation).ReadValue() == ErrorCodeValue::FabricComponentAborted);
            VERIFY_IS_TRUE(component4.State.Value == FabricComponentState::Aborted);
            stopper.Set();
        });

        VERIFY_IS_TRUE(component4.State.Value == FabricComponentState::Closing);
        component4.Abort();
        VERIFY_IS_TRUE(component4.State.Value == FabricComponentState::Closing);
        stopper.WaitOne();

        //Assert Aborted->Aborted is valid
        component4.Abort();
        VERIFY_IS_TRUE(component4.State.Value == FabricComponentState::Aborted);
    }

    BOOST_AUTO_TEST_CASE(FabricComponentFailOpenCloseTest)
    {
        ManualResetEvent stopper(false);
        FauxAsyncFabricComponent component1;
        ErrorCode error = ErrorCode(ErrorCodeValue::UnspecifiedError);
        component1.Error = error;

        TimeSpan openCloseTimeout = TimeSpan::Zero;
        component1.BeginOpen(
            openCloseTimeout,
            [&component1, &stopper, &error] (AsyncOperationSPtr const& operation) -> void
        {
            VERIFY_IS_TRUE(component1.EndOpen(operation).ReadValue() == error.ReadValue());
            VERIFY_IS_TRUE(component1.State.Value == FabricComponentState::Aborted);
            stopper.Set();
        });

        stopper.WaitOne();
        stopper.Reset();

        FauxAsyncFabricComponent component2;
        component2.BeginOpen(
            openCloseTimeout,
            [&component2, &stopper] (AsyncOperationSPtr const& operation) -> void
        {
            VERIFY_IS_TRUE(component2.EndOpen(operation).IsSuccess());
            VERIFY_IS_TRUE(component2.State.Value == FabricComponentState::Opened);
            stopper.Set();
        });

        stopper.WaitOne();
        stopper.Reset();

        component2.Error = error;
        component2.BeginClose(
            openCloseTimeout,
            [&component2, &stopper, &error] (AsyncOperationSPtr const& operation) -> void
        {
            VERIFY_IS_TRUE(component2.EndClose(operation).ReadValue() == error.ReadValue());
            VERIFY_IS_TRUE(component2.State.Value == FabricComponentState::Aborted);
            stopper.Set();
        });

        stopper.WaitOne();
    }

    BOOST_AUTO_TEST_CASE(FabricComponentInvalidTransitionTest)
    {
        ManualResetEvent stopper(false);
        TimeSpan openCloseTimeout = TimeSpan::Zero;
        {
            //Assert Created->Closing is invalid
            FauxAsyncFabricComponent component;
            VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Created);
            component.BeginClose(
                openCloseTimeout,
                [&component, &stopper] (AsyncOperationSPtr const& operation) -> void
            {
                VERIFY_IS_TRUE(component.EndClose(operation).ReadValue() == ErrorCodeValue::InvalidState);
                VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Created);
                stopper.Set();
            });

            stopper.WaitOne();
            stopper.Reset();

            //Assert Opening->Closing, Opening->Opening is invalid & at end of Opening->Closing the component is aborted
            openCloseTimeout = TimeSpan::FromSeconds(5);
            component.BeginOpen(
                openCloseTimeout,
                [&component, &stopper] (AsyncOperationSPtr const& operation) -> void
            {
                VERIFY_IS_TRUE(component.EndOpen(operation).ReadValue() == ErrorCodeValue::FabricComponentAborted);
                VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Aborted);
                stopper.Set();
            });

            VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Opening);
            component.BeginClose(
                openCloseTimeout,
                [&component, &stopper] (AsyncOperationSPtr const& operation) -> void
            {
                VERIFY_IS_TRUE(component.EndClose(operation).ReadValue() == ErrorCodeValue::FabricComponentAborted);
                VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Opening);
                stopper.Set();
            });

            stopper.WaitOne();
            stopper.Reset();

            component.BeginOpen(
                openCloseTimeout,
                [&component, &stopper] (AsyncOperationSPtr const& operation) -> void
            {
                VERIFY_IS_TRUE(component.EndOpen(operation).ReadValue() == ErrorCodeValue::FabricComponentAborted);
                VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Opening);
                stopper.Set();
            });

            stopper.WaitOne();
            stopper.Reset();

            stopper.WaitOne();
            VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Aborted);
        }

        {
            stopper.Reset();
            FauxAsyncFabricComponent component;
            openCloseTimeout = TimeSpan::Zero;
            component.BeginOpen(
                openCloseTimeout,
                [&component, &stopper] (AsyncOperationSPtr const& operation) -> void
            {
                VERIFY_IS_TRUE(component.EndOpen(operation).IsSuccess());
                VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Opened);
                stopper.Set();
            });

            stopper.WaitOne();
            stopper.Reset();

            //Assert Opened->Opening is invalid
            component.BeginOpen(
                openCloseTimeout,
                [&component, &stopper] (AsyncOperationSPtr const& operation) -> void
            {
                VERIFY_IS_TRUE(component.EndOpen(operation).ReadValue() == ErrorCodeValue::InvalidState);
                VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Opened);
                stopper.Set();
            });

            stopper.WaitOne();
            stopper.Reset();

            //Assert Closing->Closing, Closing->Opening is invalid
            openCloseTimeout = TimeSpan::FromSeconds(5);
            component.BeginClose(
                openCloseTimeout,
                [&component, &stopper] (AsyncOperationSPtr const& operation) -> void
            {
                VERIFY_IS_TRUE(component.EndClose(operation).IsSuccess());
                VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Closed);
                stopper.Set();
            });

            VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Closing);
            component.BeginOpen(
                openCloseTimeout,
                [&component, &stopper] (AsyncOperationSPtr const& operation) -> void
            {
                VERIFY_IS_TRUE(component.EndOpen(operation).ReadValue() == ErrorCodeValue::FabricComponentAborted);
                VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Closing);
                stopper.Set();
            });

            stopper.WaitOne();
            stopper.Reset();

            component.BeginClose(
                openCloseTimeout,
                [&component, &stopper] (AsyncOperationSPtr const& operation) -> void
            {
                VERIFY_IS_TRUE(component.EndClose(operation).ReadValue() == ErrorCodeValue::FabricComponentAborted);
                VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Closing);
                stopper.Set();
            });

            stopper.WaitOne();
            stopper.Reset();

            stopper.WaitOne();

            //Assert Closed->Closing, Closed->Opening is invalid
            component.BeginOpen(
                openCloseTimeout,
                [&component, &stopper] (AsyncOperationSPtr const& operation) -> void
            {
                VERIFY_IS_TRUE(component.EndOpen(operation).ReadValue() == ErrorCodeValue::FabricComponentAborted);
                VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Closed);
                stopper.Set();
            });

            stopper.WaitOne();
            stopper.Reset();

            component.BeginClose(
                openCloseTimeout,
                [&component, &stopper] (AsyncOperationSPtr const& operation) -> void
            {
                VERIFY_IS_TRUE(component.EndClose(operation).ReadValue() == ErrorCodeValue::FabricComponentAborted);
                VERIFY_IS_TRUE(component.State.Value == FabricComponentState::Closed);
                stopper.Set();
            });

            stopper.WaitOne();
            stopper.Reset();
        }
    }

    BOOST_AUTO_TEST_SUITE_END()
}

max_stars_repo_name: gridgentoo/ServiceFabricAzure

max_issues_repo_name: gridgentoo/ServiceFabricAzure

max_forks_repo_name: gridgentoo/ServiceFabricAzure


text:
// Copyright 2018 The Simons Foundation, Inc. - 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.

#ifndef NETKET_ABSTRACTMACHINE_HPP
#define NETKET_ABSTRACTMACHINE_HPP

#include <complex>
#include <functional>
#include <memory>
#include <string>
#include <vector>

#include <Python.h>
#include <Eigen/Core>

#include "Hilbert/abstract_hilbert.hpp"
#include "Utils/any.hpp"
#include "Utils/random_utils.hpp"
#include "common_types.hpp"

namespace netket {

/**
  Abstract class for Machines.
  This class prototypes the methods needed
  by a class satisfying the Machine concept.
*/
class AbstractMachine {
 public:
  using VectorType = Eigen::Matrix<Complex, Eigen::Dynamic, 1>;
  using MatrixType = Eigen::Matrix<Complex, Eigen::Dynamic, Eigen::Dynamic>;
  using VectorRefType = Eigen::Ref<VectorType>;
  using VectorConstRefType = Eigen::Ref<const VectorType>;
  using VisibleConstType = Eigen::Ref<const Eigen::VectorXd>;
  using VisibleType = Eigen::VectorXd;
  using RealVectorType = Eigen::Matrix<double, Eigen::Dynamic, 1>;
  using RealMatrixType = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>;
  using RealVectorConstRefType = Eigen::Ref<const RealVectorType>;

  /**
  Member function returning the number of variational parameters.
  @return Number of variational parameters in the Machine.
  */
  virtual int Npar() const = 0;

  /**
  Member function returning the current set of parameters in the machine.
  @return Current set of variational parameters of the machine.
  */
  virtual VectorType GetParameters() = 0;

  /**
  Member function setting the current set of parameters in the machine.
  */
  virtual void SetParameters(VectorConstRefType pars) = 0;

  /**
  Member function providing a random initialization of the parameters.
  @param sigma is the variance of the gaussian.
  @param seed is the seed of the random number generator. If seed is `nullopt`,
  one is generated using `std::random_device`.
  */
  virtual void InitRandomPars(double sigma, nonstd::optional<unsigned> seed,
                              default_random_engine *given_gen);

  /**
  Member function returning the number of visible units.
  @return Number of visible units in the Machine.
  */
  virtual int Nvisible() const = 0;

  /**
   * Computes logarithm of the wave function for a batch of visible
   * configurations.
   *
   * @param v a matrix of size `batch_size x Nvisible()`. Each row of the matrix
   * is a visible configuration.
   */
  virtual void LogVal(Eigen::Ref<const RowMatrix<double>> v,
                      Eigen::Ref<VectorType> out, const any &cache = any{});

  virtual VectorType LogVal(Eigen::Ref<const RowMatrix<double>> v,
                            const any &cache = any{});

  virtual void DerLog(Eigen::Ref<const RowMatrix<double>> v,
                      Eigen::Ref<RowMatrix<Complex>> out,
                      const any &cache = any{});

  virtual RowMatrix<Complex> DerLog(Eigen::Ref<const RowMatrix<double>> v,
                                    const any &cache = any{});

  virtual void VectorJacobianProd(Eigen::Ref<const RowMatrix<double>> v,
                                  Eigen::Ref<const VectorType> vec,
                                  Eigen::Ref<VectorType> out);

  /**
  Member function computing the logarithm of the wave function for a given
  visible vector. Given the current set of parameters, this function should
  comput the value of the logarithm of the wave function using the information
  provided in the look-up table, to speed up the computation.
  @param v a constant reference to a visible configuration.
  @param lt a constant eference to the look-up table.
  @return Logarithm of the wave function.
  */
  virtual Complex LogValSingle(VisibleConstType v, const any &lt = any{}) = 0;

  /**
  Member function initializing the look-up tables.
  If needed, a Machine can make use of look-up tables
  to speed up some critical functions. For example,
  to speed up the calculation of wave-function ratios.
  The state of a look-up table depends on the visible units.
  This function should initialize the look-up tables
  making sure that memory in the table is also being allocated.
  @param v a constant reference to the visible configuration.
  @param lt a reference to the look-up table to be initialized.
  */
  virtual any InitLookup(VisibleConstType v);

  /**
  Member function updating the look-up tables.
  If needed, a Machine can make use of look-up tables
  to speed up some critical functions. For example,
  to speed up the calculation of wave-function ratios.
  The state of a look-up table depends on the visible units.
  This function should update the look-up tables
  when the state of visible units is changed according
  to the information stored in toflip and newconf
  @param v a constant reference to the current visible configuration.
  @param tochange a constant reference to a vector containing the indeces of the
  units to be modified.
  @param newconf a constant reference to a vector containing the new values of
  the visible units: here newconf(i)=v'(tochange(i)), where v' is the new
  visible state.
  @param lt a reference to the look-up table to be updated.
  */
  virtual void UpdateLookup(VisibleConstType v,
                            const std::vector<int> &tochange,
                            const std::vector<double> &newconf, any &lt);

  /**
  Member function computing the difference between the logarithm of the
  wave-function computed at different values of the visible units (v, and a set
  of v').

  @note performance of the default implementation is pretty bad.

  @param v a constant reference to the current visible configuration.
  @param tochange a constant reference to a vector containing the indeces of the
  units to be modified.
  @param newconf a constant reference to a vector containing the new values of
  the visible units: here for each v', newconf(i)=v'(tochange(i)), where v' is
  the new visible state.
  @return A vector containing, for each v', log(Psi(v')) - log(Psi(v))
  */
  VectorType LogValDiff(VisibleConstType v,
                        const std::vector<std::vector<int>> &tochange,
                        const std::vector<std::vector<double>> &newconf);

  virtual void LogValDiff(VisibleConstType v,
                          const std::vector<std::vector<int>> &tochange,
                          const std::vector<std::vector<double>> &newconf,
                          Eigen::Ref<Eigen::VectorXcd> output);

  /**
  Member function computing the derivative of the logarithm of the wave function
  for a given visible vector.
  @param v a constant reference to a visible configuration.
  @return Derivatives of the logarithm of the wave function with respect to the
  set of parameters.
  */
  virtual VectorType DerLogSingle(VisibleConstType v,
                                  const any &cache = any{}) = 0;

  /**
  Member function computing O_k(v'), the derivative of
  the logarithm of the wave function at an update visible state v', given the
  current value at v. Specialized versions use the look-up tables to speed-up
  the calculation, otherwise it is computed from scratch.
  @param v a constant reference to the current visible configuration.
  @param tochange a constant reference to a vector containing the indeces of the
  units to be modified.
  @param newconf a constant reference to a vector containing the new values of
  the visible units: here newconf(i)=v'(tochange(i)), where v' is the new
  visible state.
  @param lt a constant eference to the look-up table.
  @return The value of DerLog(v')
  */
  virtual VectorType DerLogChanged(VisibleConstType v,
                                   const std::vector<int> &tochange,
                                   const std::vector<double> &newconf);

  /**
  Member function computing O_k(v'), the derivatives of the logarithm of the
  wave function at a vector of updates visible state v', given the current value
  at v.

  @param v a constant reference to the current visible configuration.
  @param tochange a constant reference to a vector containing the vectors of
  indeces of the units to be modified.
  @param newconf a constant reference to a vector containing the new values of
  the visible units: here newconf(i)=v'(tochange(i)), where v' is the new
  visible state.
  @param lt a constant eference to the look-up table.
  @return An integer referring to a row which has the value of v
  */
  virtual void DerLogChanged(VisibleConstType v,
                             const std::vector<std::vector<int>> &tochange,
                             const std::vector<std::vector<double>> &newconf,
                             Eigen::Ref<RowMatrix<Complex>> output);

  /**
  Member function computing the difference between the logarithm of the
  log-derivative computed at different values of the visible units (v, and a set
  of v').

  @note performance of the default implementation is pretty bad.

  @param v a constant reference to the current visible configuration.
  @param tochange a constant reference to a vector containing the indeces of the
  units to be modified.
  @param newconf a constant reference to a vector containing the new values of
  the visible units: here for each v', newconf(i)=v'(tochange(i)), where v' is
  the new visible state.
  @return A vector containing, for each v', log(Psi(v')) - log(Psi(v))
  */
  RowMatrix<Complex> DerLogDiff(
      VisibleConstType v, const std::vector<std::vector<int>> &tochange,
      const std::vector<std::vector<double>> &newconf);

  virtual void DerLogDiff(VisibleConstType v,
                          const std::vector<std::vector<int>> &tochange,
                          const std::vector<std::vector<double>> &newconf,
                          Eigen::Ref<RowMatrix<Complex>> output,
                          bool subtract_logv = true);

  virtual bool IsHolomorphic() const noexcept = 0;

  virtual PyObject *StateDict() {
    throw std::runtime_error{"Not implemented!"};
  }

  virtual PyObject *StateDict() const;
  void StateDict(PyObject *);

  virtual void Save(const std::string &filename) const;
  virtual void Load(const std::string &filename);

  const AbstractHilbert &GetHilbert() const { return *hilbert_; };
  std::shared_ptr<const AbstractHilbert> GetHilbertShared() const {
    return hilbert_;
  };

  virtual ~AbstractMachine() = default;

 protected:
  AbstractMachine(std::shared_ptr<const AbstractHilbert> hilbert)
      : hilbert_(std::move(hilbert)) {}

 private:
  std::shared_ptr<const AbstractHilbert> hilbert_;
};

}  // namespace netket

#endif  // NETKET_ABSTRACTMACHINE_HPP

max_stars_repo_name: vigsterkr/netket

max_issues_repo_name: vigsterkr/netket

max_forks_repo_name: vigsterkr/netket