Example #1
0
OverlappingRowMatrix<MatrixType>::OverlappingRowMatrix(const Teuchos::RCP<const Tpetra::RowMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> >& Matrix_in,
						       int OverlapLevel_in) :
  A_(Matrix_in),
  OverlapLevel_(OverlapLevel_in),
  UseSubComm_(false)
{  
  using Teuchos::RCP;
  using Teuchos::rcp;
  using Teuchos::Array;

  // Short form names
  typedef typename Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node>        MapType;
  typedef typename Tpetra::Import<LocalOrdinal,GlobalOrdinal,Node>           ImportType;
  typedef typename Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> CrsMatrixType;
  typedef typename Tpetra::RowMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> RowMatrixType;

  TEUCHOS_TEST_FOR_EXCEPTION(OverlapLevel_ <= 0, std::runtime_error, "Ifpack2::OverlappingRowMatrix: OverlapLevel must be > 0.");
  TEUCHOS_TEST_FOR_EXCEPTION(A_->getComm()->getSize() == 1, std::runtime_error, "Ifpack2::OverlappingRowMatrix: Matrix must be parallel.");

  RCP<const CrsMatrixType> ACRS = Teuchos::rcp_dynamic_cast<const CrsMatrixType,const RowMatrixType>(A_);
  TEUCHOS_TEST_FOR_EXCEPTION(ACRS == Teuchos::null, std::runtime_error, "Ifpack2::OverlappingRowMatrix: Matrix must be Tpetra::CrsMatrix.");
  
  NumMyRowsA_ = A_->getNodeNumRows();

  GlobalOrdinal global_invalid = Teuchos::OrdinalTraits<global_size_t>::invalid();

  // Temp arrays
  Array<GlobalOrdinal> ExtElements;  
  RCP<MapType>         TmpMap;
  RCP<CrsMatrixType>   TmpMatrix; 
  RCP<ImportType>      TmpImporter;
  RCP<const MapType>   RowMap, ColMap;

  // The big import loop
  for (int overlap = 0 ; overlap < OverlapLevel_ ; ++overlap) {
    // Get the current maps
    if(overlap==0){
      RowMap = A_->getRowMap();
      ColMap = A_->getColMap(); 
    }
    else {
      RowMap = TmpMatrix->getRowMap();
      ColMap = TmpMatrix->getColMap(); 
    }

    size_t size = ColMap->getNodeNumElements() - RowMap->getNodeNumElements(); 
    Array<GlobalOrdinal> mylist(size); 
    size_t count = 0; 

    // define the set of rows that are in ColMap but not in RowMap 
    for (LocalOrdinal i = 0 ; (size_t) i < ColMap->getNodeNumElements() ; ++i) { 
      GlobalOrdinal GID = ColMap->getGlobalElement(i); 
      if (A_->getRowMap()->getLocalElement(GID) ==  global_invalid) { 
	typename Array<GlobalOrdinal>::iterator pos 
	  = std::find(ExtElements.begin(),ExtElements.end(),GID); 
        if (pos == ExtElements.end()) { 
          ExtElements.push_back(GID);
          mylist[count] = GID; 
          ++count; 
        } 
      } 
    }
    
    // Allocate & import new matrices, maps, etc.
    TmpMap      = rcp(new MapType(global_invalid,mylist(0,count),Teuchos::OrdinalTraits<GlobalOrdinal>::zero(),A_->getComm(),A_->getNode()));
    TmpMatrix   = rcp(new CrsMatrixType(TmpMap,0));
    TmpImporter = rcp(new ImportType(A_->getRowMap(),TmpMap));

    TmpMatrix->doImport(*ACRS,*TmpImporter,Tpetra::INSERT);
    TmpMatrix->fillComplete(A_->getDomainMap(),TmpMap);
  }

  // build the map containing all the nodes (original
  // matrix + extended matrix)
  Array<GlobalOrdinal> mylist(NumMyRowsA_ + ExtElements.size());
  for (LocalOrdinal i = 0 ; (size_t)i < NumMyRowsA_ ; ++i)
    mylist[i] = A_->getRowMap()->getGlobalElement(i);
  for (LocalOrdinal i = 0 ; i < ExtElements.size() ; ++i)
    mylist[i + NumMyRowsA_] = ExtElements[i];

  RowMap_= rcp(new MapType(global_invalid,mylist(),Teuchos::OrdinalTraits<GlobalOrdinal>::zero(),A_->getComm(),A_->getNode()));
  ColMap_= RowMap_;

  // now build the map corresponding to all the external nodes
  // (with respect to A().RowMatrixRowMap().
  ExtMap_      = rcp(new MapType(global_invalid,ExtElements(),Teuchos::OrdinalTraits<GlobalOrdinal>::zero(),A_->getComm(),A_->getNode()));
  ExtMatrix_   = rcp(new CrsMatrixType(ExtMap_,ColMap_,0));
  ExtImporter_ = rcp(new ImportType(A_->getRowMap(),ExtMap_));

  RCP<CrsMatrixType> ExtMatrixCRS = Teuchos::rcp_dynamic_cast<CrsMatrixType,RowMatrixType>(ExtMatrix_);
  ExtMatrixCRS->doImport(*ACRS,*ExtImporter_,Tpetra::INSERT);
  ExtMatrixCRS->fillComplete(A_->getDomainMap(),RowMap_);

  Importer_ = rcp(new ImportType(A_->getRowMap(),RowMap_));

  // fix indices for overlapping matrix
  NumMyRowsB_ = ExtMatrix_->getNodeNumRows();
  NumMyRows_ = NumMyRowsA_ + NumMyRowsB_;
  NumMyCols_ = NumMyRows_;

  NumMyDiagonals_ = A_->getNodeNumDiags() + ExtMatrix_->getNodeNumDiags(); 
  NumMyNonzeros_  = A_->getNodeNumEntries() + ExtMatrix_->getNodeNumEntries();

  // FIXME: Fix this later when Teuchos::Comm gets redone
  Tpetra::global_size_t NumMyNonzeros_tmp = NumMyNonzeros_;
  Teuchos::reduceAll<int,Tpetra::global_size_t>(*A_->getComm(),Teuchos::REDUCE_SUM,NumMyNonzeros_tmp,Teuchos::outArg(NumGlobalNonzeros_));  
  Tpetra::global_size_t NumMyRows_tmp = NumMyRows_;
  Teuchos::reduceAll<int,Tpetra::global_size_t>(*A_->getComm(),Teuchos::REDUCE_SUM,NumMyRows_tmp,Teuchos::outArg(NumGlobalRows_));  

  MaxNumEntries_ = A_->getNodeMaxNumRowEntries();  
  if (MaxNumEntries_ < ExtMatrix_->getNodeMaxNumRowEntries())
    MaxNumEntries_ = ExtMatrix_->getNodeMaxNumRowEntries();

  // Resize temp arrays
  Indices_.resize(MaxNumEntries_);
  Values_.resize(MaxNumEntries_);
}
Example #2
0
SynthesiserVoice* Synthesiser::findVoiceToSteal (SynthesiserSound* soundToPlay,
                                                 int /*midiChannel*/, int midiNoteNumber) const
{
    // This voice-stealing algorithm applies the following heuristics:
    // - Re-use the oldest notes first
    // - Protect the lowest & topmost notes, even if sustained, but not if they've been released.

    // apparently you are trying to render audio without having any voices...
    jassert (! voices.isEmpty());

    // These are the voices we want to protect (ie: only steal if unavoidable)
    SynthesiserVoice* low = nullptr; // Lowest sounding note, might be sustained, but NOT in release phase
    SynthesiserVoice* top = nullptr; // Highest sounding note, might be sustained, but NOT in release phase

    // this is a list of voices we can steal, sorted by how long they've been running
    Array<SynthesiserVoice*> usableVoices;
    usableVoices.ensureStorageAllocated (voices.size());

    for (auto* voice : voices)
    {
        if (voice->canPlaySound (soundToPlay))
        {
            jassert (voice->isVoiceActive()); // We wouldn't be here otherwise

            usableVoices.add (voice);

            // NB: Using a functor rather than a lambda here due to scare-stories about
            // compilers generating code containing heap allocations..
            struct Sorter
            {
                bool operator() (const SynthesiserVoice* a, const SynthesiserVoice* b) const noexcept { return a->wasStartedBefore (*b); }
            };

            std::sort (usableVoices.begin(), usableVoices.end(), Sorter());

            if (! voice->isPlayingButReleased()) // Don't protect released notes
            {
                auto note = voice->getCurrentlyPlayingNote();

                if (low == nullptr || note < low->getCurrentlyPlayingNote())
                    low = voice;

                if (top == nullptr || note > top->getCurrentlyPlayingNote())
                    top = voice;
            }
        }
    }

    // Eliminate pathological cases (ie: only 1 note playing): we always give precedence to the lowest note(s)
    if (top == low)
        top = nullptr;

    // The oldest note that's playing with the target pitch is ideal..
    for (auto* voice : usableVoices)
        if (voice->getCurrentlyPlayingNote() == midiNoteNumber)
            return voice;

    // Oldest voice that has been released (no finger on it and not held by sustain pedal)
    for (auto* voice : usableVoices)
        if (voice != low && voice != top && voice->isPlayingButReleased())
            return voice;

    // Oldest voice that doesn't have a finger on it:
    for (auto* voice : usableVoices)
        if (voice != low && voice != top && ! voice->isKeyDown())
            return voice;

    // Oldest voice that isn't protected
    for (auto* voice : usableVoices)
        if (voice != low && voice != top)
            return voice;

    // We've only got "protected" voices now: lowest note takes priority
    jassert (low != nullptr);

    // Duophonic synth: give priority to the bass note:
    if (top != nullptr)
        return top;

    return low;
}
Example #3
0
 inline double DotProduct(const Array& v1, const Array& v2) {
     QL_REQUIRE(v1.size() == v2.size(),
                "arrays with different sizes cannot be multiplied");
     return std::inner_product(v1.begin(),v1.end(),v2.begin(),0.0);
 }
Example #4
0
//==============================================================================
void Ssao::initInternal(const ConfigSet& config)
{
	m_enabled = config.get("pps.ssao.enabled");

	if(!m_enabled)
	{
		return;
	}

	m_blurringIterationsCount = 
		config.get("pps.ssao.blurringIterationsCount");

	//
	// Init the widths/heights
	//
	const F32 quality = config.get("pps.ssao.renderingQuality");

	m_width = quality * (F32)m_r->getWidth();
	alignRoundUp(16, m_width);
	m_height = quality * (F32)m_r->getHeight();
	alignRoundUp(16, m_height);

	//
	// create FBOs
	//
	createFb(m_hblurFb, m_hblurRt);
	createFb(m_vblurFb, m_vblurRt);

	//
	// noise texture
	//
	GlCommandBufferHandle cmdb;
	cmdb.create(&getGlDevice());

	GlClientBufferHandle noise;
	noise.create(
		cmdb, sizeof(Vec3) * NOISE_TEX_SIZE * NOISE_TEX_SIZE, nullptr);

	genNoise((Vec3*)noise.getBaseAddress(), 
		(Vec3*)((U8*)noise.getBaseAddress() + noise.getSize()));

	GlTextureHandle::Initializer tinit;

	tinit.m_width = tinit.m_height = NOISE_TEX_SIZE;
	tinit.m_target = GL_TEXTURE_2D;
	tinit.m_internalFormat = GL_RGB32F;
	tinit.m_format = GL_RGB;
	tinit.m_type = GL_FLOAT;
	tinit.m_filterType = GlTextureHandle::Filter::NEAREST;
	tinit.m_repeat = true;
	tinit.m_mipmapsCount = 1;
	tinit.m_data[0][0] = noise;

	m_noiseTex.create(cmdb, tinit);

	//
	// Kernel
	//
	String kernelStr(getAllocator());
	Array<Vec3, KERNEL_SIZE> kernel;

	genKernel(kernel.begin(), kernel.end());
	kernelStr = "vec3[](";
	for(U i = 0; i < kernel.size(); i++)
	{
		String tmp(getAllocator());

		tmp.sprintf("vec3(%f, %f, %f) %s",
			kernel[i].x(), kernel[i].y(), kernel[i].z(),
			(i != kernel.size() - 1) ? ", " : ")");

		kernelStr += tmp;
	}

	//
	// Shaders
	//
	m_uniformsBuff.create(cmdb, GL_SHADER_STORAGE_BUFFER, 
		sizeof(ShaderCommonUniforms), GL_DYNAMIC_STORAGE_BIT);

	String pps(getAllocator());

	// main pass prog
	pps.sprintf(
		"#define NOISE_MAP_SIZE %u\n"
		"#define WIDTH %u\n"
		"#define HEIGHT %u\n"
		"#define KERNEL_SIZE %u\n"
		"#define KERNEL_ARRAY %s\n",
		NOISE_TEX_SIZE, m_width, m_height, KERNEL_SIZE, &kernelStr[0]);

	m_ssaoFrag.loadToCache(&getResourceManager(),
		"shaders/PpsSsao.frag.glsl", pps.toCString(), "r_");


	m_ssaoPpline = m_r->createDrawQuadProgramPipeline(
		m_ssaoFrag->getGlProgram());

	// blurring progs
	const char* SHADER_FILENAME = 
		"shaders/VariableSamplingBlurGeneric.frag.glsl";

	pps.sprintf(
		"#define HPASS\n"
		"#define COL_R\n"
		"#define IMG_DIMENSION %u\n"
		"#define SAMPLES 7\n", 
		m_height);

	m_hblurFrag.loadToCache(&getResourceManager(),
		SHADER_FILENAME, pps.toCString(), "r_");

	m_hblurPpline = m_r->createDrawQuadProgramPipeline(
		m_hblurFrag->getGlProgram());

	pps.sprintf(
		"#define VPASS\n"
		"#define COL_R\n"
		"#define IMG_DIMENSION %u\n"
		"#define SAMPLES 7\n", 
		m_width);

	m_vblurFrag.loadToCache(&getResourceManager(),
		SHADER_FILENAME, pps.toCString(), "r_");

	m_vblurPpline = m_r->createDrawQuadProgramPipeline(
		m_vblurFrag->getGlProgram());

	cmdb.flush();
}
Example #5
0
void LoadSave::varToState(mopo::HelmEngine* synth,
                          std::map<std::string, String>& gui_state,
                          const CriticalSection& critical_section,
                          var state) {
    if (!state.isObject())
        return;

    DynamicObject* object_state = state.getDynamicObject();
    NamedValueSet properties = object_state->getProperties();

    // Version 0.4.1 was the last build before we saved the version number.
    String version = "0.4.1";
    if (properties.contains("synth_version"))
        version = properties["synth_version"];

    // After 0.4.1 there was a patch file restructure.
    if (compareVersionStrings(version, "0.4.1") <= 0) {
        NamedValueSet new_properties;
        new_properties.set("settings", object_state);
        properties = new_properties;
    }

    var settings = properties["settings"];
    DynamicObject* settings_object = settings.getDynamicObject();
    NamedValueSet settings_properties = settings_object->getProperties();
    Array<var>* modulations = settings_properties["modulations"].getArray();

    // After 0.5.0 mixer was added and osc_mix was removed. And scaling of oscillators was changed.
    if (compareVersionStrings(version, "0.5.0") <= 0) {

        // Fix control control values.
        if (settings_properties.contains("osc_mix")) {
            mopo::mopo_float osc_mix = settings_properties["osc_mix"];
            settings_properties.set("osc_1_volume", sqrt(1.0f - osc_mix));
            settings_properties.set("osc_2_volume", sqrt(osc_mix));
            settings_properties.remove("osc_mix");
        }

        // Fix modulation routing.
        var* modulation = modulations->begin();
        Array<var> old_modulations;
        Array<DynamicObject*> new_modulations;
        for (; modulation != modulations->end(); ++modulation) {
            DynamicObject* mod = modulation->getDynamicObject();
            String destination = mod->getProperty("destination").toString();

            if (destination == "osc_mix") {
                String source = mod->getProperty("source").toString();
                mopo::mopo_float amount = mod->getProperty("amount");
                old_modulations.add(mod);

                DynamicObject* osc_1_mod = new DynamicObject();
                osc_1_mod->setProperty("source", source);
                osc_1_mod->setProperty("destination", "osc_1_volume");
                osc_1_mod->setProperty("amount", -amount);
                new_modulations.add(osc_1_mod);

                DynamicObject* osc_2_mod = new DynamicObject();
                osc_2_mod->setProperty("source", source);
                osc_2_mod->setProperty("destination", "osc_2_volume");
                osc_2_mod->setProperty("amount", amount);
                new_modulations.add(osc_2_mod);
            }
        }

        for (var old_modulation : old_modulations)
            modulations->removeFirstMatchingValue(old_modulation);

        for (DynamicObject* modulation : new_modulations)
            modulations->add(modulation);
    }

    loadControls(synth, critical_section, settings_properties);
    loadModulations(synth, critical_section, modulations);
    loadGuiState(gui_state, properties);
}
/**
 * This method will NOT check the type and dimension of the data field!
 * \param dataPtr data set ( Scalar volume )
 * \param kernel filter kernel 
 */
TImagePtr CKernelFilter::applyKernel( TImagePtr dataPtr, Array<double, 3>& kernel ) throw()
{
BENCHSTART;
FBEGIN;
  size_t usDims[] = { dataPtr->getExtent(0), dataPtr->getExtent(1), 
		dataPtr->getExtent(2) };
  TImagePtr outputPtr ( new TImage( 3, dataPtr->getExtents(), 
		dataPtr->getDataDimension() ) );
	outputPtr->getDataRange().setMinimum( dataPtr->getDataRange().getMinimum() );
	outputPtr->getDataRange().setMaximum( dataPtr->getDataRange().getMaximum() );

  ushort usRadius = ( kernel.cols() - 1 ) / 2; // Filter kernel size

DBG2("Starting filter BLITZ++");
	PROG_MAX( dataPtr->getDataDimension() );

  for ( ushort usChannels = 0; usChannels < dataPtr->getDataDimension(); 
		++usChannels )
  {
  	
		PROG_VAL( usChannels + 1 );	
    Array<short, 3> work ( dataPtr->getArray( usChannels ),
      shape(usDims[0], usDims[1], usDims[2]), neverDeleteData, AIPSArray<3>() );

    Array<short, 3> output ( outputPtr->getArray( usChannels ),
      shape(usDims[0], usDims[1], usDims[2]), neverDeleteData, AIPSArray<3>() );

		int lastpos = 0;
		cerr << ">>>";
    for ( Array<short, 3>::iterator it = work.begin(); it != work.end(); ++it )
    {
      TinyVector<int, 3> position = it.position();
/*      if ( position[2] != lastpos )
      {
      	cerr << position[2] << " ";
      	lastpos = position[2];
      }*/
      double dTmp = 0;
      for ( int k = position[0] - usRadius; k < ( position[0] + usRadius + 1 ); ++k )
        for ( int l = position[1] - usRadius; l < ( position[1] + usRadius + 1 ); ++l )
          for ( int m = position[2] - usRadius; m < ( position[2] + usRadius + 1 ); ++m )
      {
        int i = k + usRadius - position[0];
        int j = l + usRadius - position[1];
        int n = m + usRadius - position[2];
        if ( k < work.rows() && k > -1 && l < work.cols()
          && l > -1 && m < work.depth() && m > -1 )
          dTmp += work( k, l, m ) * kernel( i, j, n );
      }
      output( position ) = static_cast<ushort>( std::abs( static_cast<long>( dTmp ) ) );
      if ( output( position ) > outputPtr->getDataRange().getMaximum() )
        outputPtr->getDataRange().setMaximum( output( position ) );
			else if ( output( position ) < outputPtr->getDataRange().getMinimum() )
        outputPtr->getDataRange().setMinimum( output( position ) );
    }
		cerr << "<<<" << endl;
  }
	PROG_RESET();	
BENCHSTOP;
FEND;
  return outputPtr;
}
    void Gaussian1dCapFloorEngine::calculate() const {

        for (Size i = 0; i < arguments_.spreads.size(); i++)
            QL_REQUIRE(arguments_.spreads[i] == 0.0,
                       "Non zero spreads (" << arguments_.spreads[i]
                                            << ") are not allowed.");

        Size optionlets = arguments_.startDates.size();
        std::vector<Real> values(optionlets, 0.0);
        std::vector<Real> forwards(optionlets, 0.0);
        Real value = 0.0;

        Date settlement = model_->termStructure()->referenceDate();

        CapFloor::Type type = arguments_.type;

        Array z = model_->yGrid(stddevs_, integrationPoints_);
        Array p(z.size());

        for (Size i = 0; i < optionlets; ++i) {

            Date valueDate = arguments_.startDates[i];
            Date paymentDate = arguments_.endDates[i];
            boost::shared_ptr<IborIndex> iborIndex =
                boost::dynamic_pointer_cast<IborIndex>(arguments_.indexes[i]);
            // if we do not find an ibor index with associated forwarding curve
            // we fall back on the model curve

            if (paymentDate > settlement) {

                Real f = arguments_.nominals[i] * arguments_.gearings[i];
                Date fixingDate = arguments_.fixingDates[i];
                Time fixingTime =
                    model_->termStructure()->timeFromReference(fixingDate);

                Real strike;

                if (type == CapFloor::Cap || type == CapFloor::Collar) {
                    strike = arguments_.capRates[i];
                    if (fixingDate <= settlement) {
                        values[i] =
                            std::max(arguments_.forwards[i] - strike, 0.0) * f *
                            arguments_.accrualTimes[i];
                    } else {
                        for (Size j = 0; j < z.size(); j++) {
                            Real floatingLegNpv;
                            if (iborIndex != NULL)
                                floatingLegNpv =
                                    arguments_.accrualTimes[i] *
                                    model_->forwardRate(fixingDate, fixingDate,
                                                        z[j], iborIndex) *
                                    model_->zerobond(paymentDate, fixingDate,
                                                     z[j], discountCurve_);
                            else
                                floatingLegNpv =
                                    (model_->zerobond(valueDate, fixingDate,
                                                      z[j]) -
                                     model_->zerobond(paymentDate, fixingDate,
                                                      z[j]));
                            Real fixedLegNpv =
                                arguments_.capRates[i] *
                                arguments_.accrualTimes[i] *
                                model_->zerobond(paymentDate, fixingDate, z[j]);
                            p[j] =
                                std::max((floatingLegNpv - fixedLegNpv), 0.0) /
                                model_->numeraire(fixingTime, z[j],
                                                  discountCurve_);
                        }
                        CubicInterpolation payoff(
                            z.begin(), z.end(), p.begin(),
                            CubicInterpolation::Spline, true,
                            CubicInterpolation::Lagrange, 0.0,
                            CubicInterpolation::Lagrange, 0.0);
                        Real price = 0.0;
                        for (Size j = 0; j < z.size() - 1; j++) {
                            price += model_->gaussianShiftedPolynomialIntegral(
                                0.0, payoff.cCoefficients()[j],
                                payoff.bCoefficients()[j],
                                payoff.aCoefficients()[j], p[j], z[j], z[j],
                                z[j + 1]);
                        }
                        if (extrapolatePayoff_) {
                            if (flatPayoffExtrapolation_) {
                                price +=
                                    model_->gaussianShiftedPolynomialIntegral(
                                        0.0, 0.0, 0.0, 0.0, p[z.size() - 2],
                                        z[z.size() - 2], z[z.size() - 1],
                                        100.0);
                                price +=
                                    model_->gaussianShiftedPolynomialIntegral(
                                        0.0, 0.0, 0.0, 0.0, p[0], z[0], -100.0,
                                        z[0]);
                            } else {
                                price +=
                                    model_->gaussianShiftedPolynomialIntegral(
                                        0.0,
                                        payoff.cCoefficients()[z.size() - 2],
                                        payoff.bCoefficients()[z.size() - 2],
                                        payoff.aCoefficients()[z.size() - 2],
                                        p[z.size() - 2], z[z.size() - 2],
                                        z[z.size() - 1], 100.0);
                            }
                        }
                        values[i] =
                            price *
                            model_->numeraire(0.0, 0.0, discountCurve_) * f;
                    }
                }
                if (type == CapFloor::Floor || type == CapFloor::Collar) {
                    strike = arguments_.floorRates[i];
                    Real floorlet;
                    if (fixingDate <= settlement) {
                        floorlet =
                            std::max(-(arguments_.forwards[i] - strike), 0.0) *
                            f * arguments_.accrualTimes[i];
                    } else {
                        for (Size j = 0; j < z.size(); j++) {
                            Real floatingLegNpv;
                            if (iborIndex != NULL)
                                floatingLegNpv =
                                    arguments_.accrualTimes[i] *
                                    model_->forwardRate(fixingDate, fixingDate,
                                                        z[j], iborIndex) *
                                    model_->zerobond(paymentDate, fixingDate,
                                                     z[j], discountCurve_);
                            else
                                floatingLegNpv =
                                    (model_->zerobond(valueDate, fixingDate,
                                                      z[j]) -
                                     model_->zerobond(paymentDate, fixingDate,
                                                      z[j]));
                            Real fixedLegNpv =
                                arguments_.floorRates[i] *
                                arguments_.accrualTimes[i] *
                                model_->zerobond(paymentDate, fixingDate, z[j]);
                            p[j] =
                                std::max(-(floatingLegNpv - fixedLegNpv), 0.0) /
                                model_->numeraire(fixingTime, z[j],
                                                  discountCurve_);
                        }
                        CubicInterpolation payoff(
                            z.begin(), z.end(), p.begin(),
                            CubicInterpolation::Spline, true,
                            CubicInterpolation::Lagrange, 0.0,
                            CubicInterpolation::Lagrange, 0.0);
                        Real price = 0.0;
                        for (Size j = 0; j < z.size() - 1; j++) {
                            price += model_->gaussianShiftedPolynomialIntegral(
                                0.0, payoff.cCoefficients()[j],
                                payoff.bCoefficients()[j],
                                payoff.aCoefficients()[j], p[j], z[j], z[j],
                                z[j + 1]);
                        }
                        if (extrapolatePayoff_) {
                            if (flatPayoffExtrapolation_) {
                                price +=
                                    model_->gaussianShiftedPolynomialIntegral(
                                        0.0, 0.0, 0.0, 0.0, p[z.size() - 2],
                                        z[z.size() - 2], z[z.size() - 1],
                                        100.0);
                                price +=
                                    model_->gaussianShiftedPolynomialIntegral(
                                        0.0, 0.0, 0.0, 0.0, p[0], z[0], -100.0,
                                        z[0]);
                            } else {
                                price +=
                                    model_->gaussianShiftedPolynomialIntegral(
                                        0.0, payoff.cCoefficients()[0],
                                        payoff.bCoefficients()[0],
                                        payoff.aCoefficients()[0], p[0], z[0],
                                        -100.0, z[0]);
                            }
                        }
                        floorlet = price *
                                   model_->numeraire(0.0, 0.0, discountCurve_) *
                                   f;
                    }
                    if (type == CapFloor::Floor) {
                        values[i] = floorlet;
                    } else {
                        // a collar is long a cap and short a floor
                        values[i] -= floorlet;
                    }
                }

                value += values[i];
            }
        }

        results_.value = value;

        results_.additionalResults["optionletsPrice"] = values;
        results_.additionalResults["optionletsAtmForward"] = forwards;
    }
Example #8
0
bool operator<(const Array<T, N>& left, const Array<T, N>& right) {
    return std::lexicographical_compare(left.begin(),left.end(),
                                        right.begin(),right.end());
}
    //! @name Constructor/Destructor
    //@{
    AMGXOperator(const Teuchos::RCP<Tpetra::CrsMatrix<SC,LO,GO,NO> > &inA, Teuchos::ParameterList &paramListIn) {
      RCP<const Teuchos::Comm<int> > comm = inA->getRowMap()->getComm();
      int numProcs = comm->getSize();
      int myRank   = comm->getRank();

      RCP<Teuchos::Time> amgxTimer = Teuchos::TimeMonitor::getNewTimer("MueLu: AMGX: initialize");
      amgxTimer->start();
      // Initialize
      AMGX_SAFE_CALL(AMGX_initialize());
      AMGX_SAFE_CALL(AMGX_initialize_plugins());

      /*system*/
      //AMGX_SAFE_CALL(AMGX_register_print_callback(&print_callback));
      AMGX_SAFE_CALL(AMGX_install_signal_handler());
      Teuchos::ParameterList configs = paramListIn.sublist("amgx:params", true);
      if (configs.isParameter("json file")) {
        AMGX_SAFE_CALL(AMGX_config_create_from_file(&Config_, (const char *) &configs.get<std::string>("json file")[0]));
      } else {
        std::ostringstream oss;
        oss << "";
        ParameterList::ConstIterator itr;
        for (itr = configs.begin(); itr != configs.end(); ++itr) {
          const std::string&    name  = configs.name(itr);
          const ParameterEntry& entry = configs.entry(itr);
          oss << name << "=" << filterValueToString(entry) << ", ";
        }
        oss << "\0";
        std::string configString = oss.str();
        if (configString == "") {
          //print msg that using defaults
          //GetOStream(Warnings0) << "Warning: No configuration parameters specified, using default AMGX configuration parameters. \n";
        }
        AMGX_SAFE_CALL(AMGX_config_create(&Config_, configString.c_str()));
      }

      // TODO: we probably need to add "exception_handling=1" to the parameter list
      // to switch on internal error handling (with no need for AMGX_SAFE_CALL)

#define NEW_COMM
#ifdef NEW_COMM
      // NOTE: MPI communicator used in AMGX_resources_create must exist in the scope of AMGX_matrix_comm_from_maps_one_ring
      // FIXME: fix for serial comm
      RCP<const Teuchos::MpiComm<int> > tmpic = Teuchos::rcp_dynamic_cast<const Teuchos::MpiComm<int> >(comm->duplicate());
      TEUCHOS_TEST_FOR_EXCEPTION(tmpic.is_null(), Exceptions::RuntimeError, "Communicator is not MpiComm");

      RCP<const Teuchos::OpaqueWrapper<MPI_Comm> > rawMpiComm = tmpic->getRawMpiComm();
      MPI_Comm mpiComm = *rawMpiComm;
#endif

      // Construct AMGX resources
      if (numProcs == 1) {
        AMGX_resources_create_simple(&Resources_, Config_);

      } else {
        int numGPUDevices;
        cudaGetDeviceCount(&numGPUDevices);
        int device[] = {(comm->getRank() % numGPUDevices)};

        AMGX_config_add_parameters(&Config_, "communicator=MPI");
#ifdef NEW_COMM
        AMGX_resources_create(&Resources_, Config_, &mpiComm, 1/* number of GPU devices utilized by this rank */, device);
#else
        AMGX_resources_create(&Resources_, Config_, MPI_COMM_WORLD, 1/* number of GPU devices utilized by this rank */, device);
#endif
      }

      AMGX_Mode mode = AMGX_mode_dDDI;
      AMGX_solver_create(&Solver_, Resources_, mode,  Config_);
      AMGX_matrix_create(&A_,      Resources_, mode);
      AMGX_vector_create(&X_,      Resources_, mode);
      AMGX_vector_create(&Y_,      Resources_, mode);

      amgxTimer->stop();
      amgxTimer->incrementNumCalls();

      std::vector<int> amgx2muelu;

      // Construct AMGX communication pattern
      if (numProcs > 1) {
        RCP<const Tpetra::Import<LO,GO> > importer = inA->getCrsGraph()->getImporter();

        TEUCHOS_TEST_FOR_EXCEPTION(importer.is_null(), MueLu::Exceptions::RuntimeError, "The matrix A has no Import object.");

        Tpetra::Distributor distributor = importer->getDistributor();

        Array<int> sendRanks = distributor.getImagesTo();
        Array<int> recvRanks = distributor.getImagesFrom();

        std::sort(sendRanks.begin(), sendRanks.end());
        std::sort(recvRanks.begin(), recvRanks.end());

        bool match = true;
        if (sendRanks.size() != recvRanks.size()) {
          match = false;
        } else {
          for (int i = 0; i < sendRanks.size(); i++) {
            if (recvRanks[i] != sendRanks[i])
              match = false;
              break;
          }
        }
        TEUCHOS_TEST_FOR_EXCEPTION(!match, MueLu::Exceptions::RuntimeError, "AMGX requires that the processors that we send to and receive from are the same. "
                                   "This is not the case: we send to {" << sendRanks << "} and receive from {" << recvRanks << "}");

        int        num_neighbors = sendRanks.size();  // does not include the calling process
        const int* neighbors     = &sendRanks[0];

        // Later on, we'll have to organize the send and recv data by PIDs,
        // i.e, a vector V of vectors, where V[i] is PID i's vector of data.
        // Hence we need to be able to quickly look up  an array index
        // associated with each PID.
        Tpetra::Details::HashTable<int,int> hashTable(3*num_neighbors);
        for (int i = 0; i < num_neighbors; i++)
          hashTable.add(neighbors[i], i);

        // Get some information out
        ArrayView<const int> exportLIDs = importer->getExportLIDs();
        ArrayView<const int> exportPIDs = importer->getExportPIDs();
        Array<int> importPIDs;
        Tpetra::Import_Util::getPids(*importer, importPIDs, true/* make local -1 */);

        // Construct the reordering for AMGX as in AMGX_matrix_upload_all documentation
        RCP<const Map> rowMap = inA->getRowMap();
        RCP<const Map> colMap = inA->getColMap();

        int N = rowMap->getNodeNumElements(), Nc = colMap->getNodeNumElements();
        muelu2amgx_.resize(Nc, -1);

        int numUniqExports = 0;
        for (int i = 0; i < exportLIDs.size(); i++)
          if (muelu2amgx_[exportLIDs[i]] == -1) {
            numUniqExports++;
            muelu2amgx_[exportLIDs[i]] = -2;
          }

        int localOffset = 0, exportOffset = N - numUniqExports;
        // Go through exported LIDs and put them at the end of LIDs
        for (int i = 0; i < exportLIDs.size(); i++)
          if (muelu2amgx_[exportLIDs[i]] < 0) // exportLIDs are not unique
            muelu2amgx_[exportLIDs[i]] = exportOffset++;
        // Go through all non-export LIDs, and put them at the beginning of LIDs
        for (int i = 0; i < N; i++)
          if (muelu2amgx_[i] == -1)
            muelu2amgx_[i] = localOffset++;
        // Go through the tail (imported LIDs), and order those by neighbors
        int importOffset = N;
        for (int k = 0; k < num_neighbors; k++)
          for (int i = 0; i < importPIDs.size(); i++)
            if (importPIDs[i] != -1 && hashTable.get(importPIDs[i]) == k)
              muelu2amgx_[i] = importOffset++;

        amgx2muelu.resize(muelu2amgx_.size());
        for (int i = 0; i < muelu2amgx_.size(); i++)
          amgx2muelu[muelu2amgx_[i]] = i;

        // Construct send arrays
        std::vector<std::vector<int> > sendDatas (num_neighbors);
        std::vector<int>               send_sizes(num_neighbors, 0);
        for (int i = 0; i < exportPIDs.size(); i++) {
          int index = hashTable.get(exportPIDs[i]);
          sendDatas [index].push_back(muelu2amgx_[exportLIDs[i]]);
          send_sizes[index]++;
        }
        // FIXME: sendDatas must be sorted (based on GIDs)

        std::vector<const int*> send_maps(num_neighbors);
        for (int i = 0; i < num_neighbors; i++)
          send_maps[i] = &(sendDatas[i][0]);

        // Debugging
        printMaps(comm, sendDatas, amgx2muelu, neighbors, *importer->getTargetMap(), "send_map_vector");

        // Construct recv arrays
        std::vector<std::vector<int> > recvDatas (num_neighbors);
        std::vector<int>               recv_sizes(num_neighbors, 0);
        for (int i = 0; i < importPIDs.size(); i++)
          if (importPIDs[i] != -1) {
            int index = hashTable.get(importPIDs[i]);
            recvDatas [index].push_back(muelu2amgx_[i]);
            recv_sizes[index]++;
        }
        // FIXME: recvDatas must be sorted (based on GIDs)

        std::vector<const int*> recv_maps(num_neighbors);
        for (int i = 0; i < num_neighbors; i++)
          recv_maps[i] = &(recvDatas[i][0]);

        // Debugging
        printMaps(comm, recvDatas, amgx2muelu, neighbors, *importer->getTargetMap(), "recv_map_vector");

        AMGX_SAFE_CALL(AMGX_matrix_comm_from_maps_one_ring(A_, 1, num_neighbors, neighbors, &send_sizes[0], &send_maps[0], &recv_sizes[0], &recv_maps[0]));

        AMGX_vector_bind(X_, A_);
        AMGX_vector_bind(Y_, A_);
      }

      RCP<Teuchos::Time> matrixTransformTimer = Teuchos::TimeMonitor::getNewTimer("MueLu: AMGX: transform matrix");
      matrixTransformTimer->start();

      ArrayRCP<const size_t> ia_s;
      ArrayRCP<const int>    ja;
      ArrayRCP<const double> a;
      inA->getAllValues(ia_s, ja, a);

      ArrayRCP<int> ia(ia_s.size());
      for (int i = 0; i < ia.size(); i++)
        ia[i] = Teuchos::as<int>(ia_s[i]);

      N_      = inA->getNodeNumRows();
      int nnz = inA->getNodeNumEntries();

      matrixTransformTimer->stop();
      matrixTransformTimer->incrementNumCalls();


      // Upload matrix
      // TODO Do we need to pin memory here through AMGX_pin_memory?
      RCP<Teuchos::Time> matrixTimer = Teuchos::TimeMonitor::getNewTimer("MueLu: AMGX: transfer matrix  CPU->GPU");
      matrixTimer->start();
      if (numProcs == 1) {
        AMGX_matrix_upload_all(A_, N_, nnz, 1, 1, &ia[0], &ja[0], &a[0], NULL);

      } else {
        // Transform the matrix
        std::vector<int>    ia_new(ia.size());
        std::vector<int>    ja_new(ja.size());
        std::vector<double> a_new (a.size());

        ia_new[0] = 0;
        for (int i = 0; i < N_; i++) {
          int oldRow = amgx2muelu[i];

          ia_new[i+1] = ia_new[i] + (ia[oldRow+1] - ia[oldRow]);

          for (int j = ia[oldRow]; j < ia[oldRow+1]; j++) {
            int offset = j - ia[oldRow];
            ja_new[ia_new[i] + offset] = muelu2amgx_[ja[j]];
            a_new [ia_new[i] + offset] = a[j];
          }
          // Do bubble sort on two arrays
          // NOTE: There are multiple possible optimizations here (even of bubble sort)
          bool swapped;
          do {
            swapped = false;

            for (int j = ia_new[i]; j < ia_new[i+1]-1; j++)
              if (ja_new[j] > ja_new[j+1]) {
                std::swap(ja_new[j], ja_new[j+1]);
                std::swap(a_new [j], a_new [j+1]);
                swapped = true;
              }
          } while (swapped == true);
        }

        AMGX_matrix_upload_all(A_, N_, nnz, 1, 1, &ia_new[0], &ja_new[0], &a_new[0], NULL);
      }
      matrixTimer->stop();
      matrixTimer->incrementNumCalls();

      domainMap_ = inA->getDomainMap();
      rangeMap_  = inA->getRangeMap();

      RCP<Teuchos::Time> realSetupTimer = Teuchos::TimeMonitor::getNewTimer("MueLu: AMGX: real setup");
      realSetupTimer->start();
      AMGX_solver_setup(Solver_, A_);
      realSetupTimer->stop();
      realSetupTimer->incrementNumCalls();

      vectorTimer1_ = Teuchos::TimeMonitor::getNewTimer("MueLu: AMGX: transfer vectors CPU->GPU");
      vectorTimer2_ = Teuchos::TimeMonitor::getNewTimer("MueLu: AMGX: transfer vector  GPU->CPU");
    }
void defaultGetPoints(
    const Scalar& t_old, // required inArg
    const Ptr<const VectorBase<Scalar> >& x_old, // optional inArg
    const Ptr<const VectorBase<Scalar> >& xdot_old, // optional inArg
    const Scalar& t, // required inArg
    const Ptr<const VectorBase<Scalar> >& x, // optional inArg
    const Ptr<const VectorBase<Scalar> >& xdot, // optional inArg
    const Array<Scalar>& time_vec, // required inArg
    const Ptr<Array<Teuchos::RCP<const Thyra::VectorBase<Scalar> > > >& x_vec, // optional outArg
    const Ptr<Array<Teuchos::RCP<const Thyra::VectorBase<Scalar> > > >& xdot_vec, // optional outArg
    const Ptr<Array<typename Teuchos::ScalarTraits<Scalar>::magnitudeType> >& accuracy_vec, // optional outArg
    const Ptr<InterpolatorBase<Scalar> > interpolator // optional inArg (note:  not const)
    ) 
{
  typedef Teuchos::ScalarTraits<Scalar> ST;
  assertTimePointsAreSorted(time_vec);
  TimeRange<Scalar> tr(t_old, t);
  TEUCHOS_ASSERT( tr.isValid() );
  if (!is_null(x_vec)) {
    x_vec->clear();
  }
  if (!is_null(xdot_vec)) {
    xdot_vec->clear();
  }
  if (!is_null(accuracy_vec)) {
    accuracy_vec->clear();
  }
  typename Array<Scalar>::const_iterator time_it = time_vec.begin();
  RCP<const VectorBase<Scalar> > tmpVec;
  RCP<const VectorBase<Scalar> > tmpVecDot;
  for (; time_it != time_vec.end() ; time_it++) {
    Scalar time = *time_it;
    asssertInTimeRange(tr, time);
    Scalar accuracy = ST::zero();
    if (compareTimeValues(time,t_old)==0) {
      if (!is_null(x_old)) {
        tmpVec = x_old->clone_v();
      }
      if (!is_null(xdot_old)) {
        tmpVecDot = xdot_old->clone_v();
      }
    } else if (compareTimeValues(time,t)==0) {
      if (!is_null(x)) {
        tmpVec = x->clone_v();
      }
      if (!is_null(xdot)) {
        tmpVecDot = xdot->clone_v();
      }
    } else {
      TEST_FOR_EXCEPTION(
          is_null(interpolator), std::logic_error,
          "Error, getPoints:  This stepper only supports time values on the boundaries!\n"
          );
      // At this point, we know time != t_old, time != t, interpolator != null, 
      // and time in [t_old,t], therefore, time in (t_old,t).  
      // t_old != t at this point because otherwise it would have been caught above.
      // Now use the interpolator to pass out the interior points
      typename DataStore<Scalar>::DataStoreVector_t ds_nodes;
      typename DataStore<Scalar>::DataStoreVector_t ds_out;
      {
        // t_old
        DataStore<Scalar> ds;
        ds.time = t_old;
        ds.x = rcp(x_old.get(),false);
        ds.xdot = rcp(xdot_old.get(),false);
        ds_nodes.push_back(ds);
      }
      {
        // t
        DataStore<Scalar> ds;
        ds.time = t;
        ds.x = rcp(x.get(),false);
        ds.xdot = rcp(xdot.get(),false);
        ds_nodes.push_back(ds);
      }
      Array<Scalar> time_vec_in;
      time_vec_in.push_back(time);
      interpolate<Scalar>(*interpolator,rcp(&ds_nodes,false),time_vec_in,&ds_out);
      Array<Scalar> time_vec_out;
      Array<RCP<const VectorBase<Scalar> > > x_vec_out;
      Array<RCP<const VectorBase<Scalar> > > xdot_vec_out;
      Array<typename Teuchos::ScalarTraits<Scalar>::magnitudeType> accuracy_vec_out;
      dataStoreVectorToVector(ds_out,&time_vec_out,&x_vec_out,&xdot_vec_out,&accuracy_vec_out);
      TEUCHOS_ASSERT( time_vec_out.length()==1 );
      tmpVec = x_vec_out[0];
      tmpVecDot = xdot_vec_out[0];
      accuracy = accuracy_vec_out[0];
    }
    if (!is_null(x_vec)) {
      x_vec->push_back(tmpVec);
    }
    if (!is_null(xdot_vec)) {
      xdot_vec->push_back(tmpVecDot);
    }
    if (!is_null(accuracy_vec)) {
      accuracy_vec->push_back(accuracy);
    }
    tmpVec = Teuchos::null;
    tmpVecDot = Teuchos::null;
  }
}
Example #11
0
bool operator==(const Array<T, N>& left, const Array<T, N>& right) {
    return std::equal(left.begin(), left.end(), right.begin());
}
    void Gaussian1dSwaptionEngine::calculate() const {

        QL_REQUIRE(arguments_.settlementType == Settlement::Physical,
                   "cash-settled swaptions not yet implemented ...");

        Date settlement = model_->termStructure()->referenceDate();

        if (arguments_.exercise->dates().back() <=
            settlement) { // swaption is expired, possibly generated swap is not
                          // valued
            results_.value = 0.0;
            return;
        }

        int idx = static_cast<int>(arguments_.exercise->dates().size()) - 1;
        int minIdxAlive = static_cast<int>(
            std::upper_bound(arguments_.exercise->dates().begin(),
                             arguments_.exercise->dates().end(), settlement) -
            arguments_.exercise->dates().begin());

        VanillaSwap swap = *arguments_.swap;
        Option::Type type =
            arguments_.type == VanillaSwap::Payer ? Option::Call : Option::Put;
        Schedule fixedSchedule = swap.fixedSchedule();
        Schedule floatSchedule = swap.floatingSchedule();

        Array npv0(2 * integrationPoints_ + 1, 0.0),
            npv1(2 * integrationPoints_ + 1, 0.0);
        Array z = model_->yGrid(stddevs_, integrationPoints_);
        Array p(z.size(), 0.0);

        Date expiry1 = Null<Date>(), expiry0;
        Time expiry1Time = Null<Real>(), expiry0Time;

        do {

            if (idx == minIdxAlive - 1)
                expiry0 = settlement;
            else
                expiry0 = arguments_.exercise->dates()[idx];

            expiry0Time = std::max(
                model_->termStructure()->timeFromReference(expiry0), 0.0);

            Size j1 =
                std::upper_bound(fixedSchedule.dates().begin(),
                                 fixedSchedule.dates().end(), expiry0 - 1) -
                fixedSchedule.dates().begin();
            Size k1 =
                std::upper_bound(floatSchedule.dates().begin(),
                                 floatSchedule.dates().end(), expiry0 - 1) -
                floatSchedule.dates().begin();

            // a lazy object is not thread safe, neither is the caching
            // in gsrprocess. therefore we trigger computations here such
            // that neither lazy object recalculation nor write access
            // during caching occurs in the parallized loop below.
            // this is known to work for the gsr and markov functional
            // model implementations of Gaussian1dModel
#ifdef _OPENMP
            if (expiry0 > settlement) {
                for (Size l = k1; l < arguments_.floatingCoupons.size(); l++) {
                    model_->forwardRate(arguments_.floatingFixingDates[l],
                                        expiry0, 0.0,
                                        arguments_.swap->iborIndex());
                    model_->zerobond(arguments_.floatingPayDates[l], expiry0,
                                     0.0, discountCurve_);
                }
                for (Size l = j1; l < arguments_.fixedCoupons.size(); l++) {
                    model_->zerobond(arguments_.fixedPayDates[l], expiry0, 0.0,
                                     discountCurve_);
                }
                model_->numeraire(expiry0Time, 0.0, discountCurve_);
            }
#endif

#pragma omp parallel for default(shared) firstprivate(p) if(expiry0>settlement)
            for (Size k = 0; k < (expiry0 > settlement ? npv0.size() : 1);
                 k++) {

                Real price = 0.0;
                if (expiry1Time != Null<Real>()) {
                    Array yg = model_->yGrid(stddevs_, integrationPoints_,
                                             expiry1Time, expiry0Time,
                                             expiry0 > settlement ? z[k] : 0.0);
                    CubicInterpolation payoff0(
                        z.begin(), z.end(), npv1.begin(),
                        CubicInterpolation::Spline, true,
                        CubicInterpolation::Lagrange, 0.0,
                        CubicInterpolation::Lagrange, 0.0);
                    for (Size i = 0; i < yg.size(); i++) {
                        p[i] = payoff0(yg[i], true);
                    }
                    CubicInterpolation payoff1(
                        z.begin(), z.end(), p.begin(),
                        CubicInterpolation::Spline, true,
                        CubicInterpolation::Lagrange, 0.0,
                        CubicInterpolation::Lagrange, 0.0);
                    for (Size i = 0; i < z.size() - 1; i++) {
                        price += model_->gaussianShiftedPolynomialIntegral(
                            0.0, payoff1.cCoefficients()[i],
                            payoff1.bCoefficients()[i],
                            payoff1.aCoefficients()[i], p[i], z[i], z[i],
                            z[i + 1]);
                    }
                    if (extrapolatePayoff_) {
                        if (flatPayoffExtrapolation_) {
                            price += model_->gaussianShiftedPolynomialIntegral(
                                0.0, 0.0, 0.0, 0.0, p[z.size() - 2],
                                z[z.size() - 2], z[z.size() - 1], 100.0);
                            price += model_->gaussianShiftedPolynomialIntegral(
                                0.0, 0.0, 0.0, 0.0, p[0], z[0], -100.0, z[0]);
                        } else {
                            if (type == Option::Call)
                                price +=
                                    model_->gaussianShiftedPolynomialIntegral(
                                        0.0,
                                        payoff1.cCoefficients()[z.size() - 2],
                                        payoff1.bCoefficients()[z.size() - 2],
                                        payoff1.aCoefficients()[z.size() - 2],
                                        p[z.size() - 2], z[z.size() - 2],
                                        z[z.size() - 1], 100.0);
                            if (type == Option::Put)
                                price +=
                                    model_->gaussianShiftedPolynomialIntegral(
                                        0.0, payoff1.cCoefficients()[0],
                                        payoff1.bCoefficients()[0],
                                        payoff1.aCoefficients()[0], p[0], z[0],
                                        -100.0, z[0]);
                        }
                    }
                }

                npv0[k] = price;

                if (expiry0 > settlement) {
                    Real floatingLegNpv = 0.0;
                    for (Size l = k1; l < arguments_.floatingCoupons.size();
                         l++) {
                        floatingLegNpv +=
                            arguments_.nominal *
                            arguments_.floatingAccrualTimes[l] *
                            (arguments_.floatingSpreads[l] +
                             model_->forwardRate(
                                 arguments_.floatingFixingDates[l], expiry0,
                                 z[k], arguments_.swap->iborIndex())) *
                            model_->zerobond(arguments_.floatingPayDates[l],
                                             expiry0, z[k], discountCurve_);
                    }
                    Real fixedLegNpv = 0.0;
                    for (Size l = j1; l < arguments_.fixedCoupons.size(); l++) {
                        fixedLegNpv +=
                            arguments_.fixedCoupons[l] *
                            model_->zerobond(arguments_.fixedPayDates[l],
                                             expiry0, z[k], discountCurve_);
                    }
                    npv0[k] = std::max(npv0[k],
                                       (type == Option::Call ? 1.0 : -1.0) *
                                           (floatingLegNpv - fixedLegNpv) /
                                           model_->numeraire(expiry0Time, z[k],
                                                             discountCurve_));
                }
            }

            npv1.swap(npv0);
            expiry1 = expiry0;
            expiry1Time = expiry0Time;

        } while (--idx >= minIdxAlive - 1);

        results_.value = npv1[0] * model_->numeraire(0.0, 0.0, discountCurve_);
    }
    EndCriteria::Type DifferentialEvolution::minimize(Problem& P,
											const EndCriteria& endCriteria) {

		EndCriteria::Type ecType = EndCriteria::MaxIterations;
	    QL_REQUIRE(P.currentValue().size() == nParam_,
			"Number of parameters mismatch between problem and DE optimizer");
        P.reset();		
		init();

		Real bestCost = QL_MAX_REAL;
		Size bestPop  = 0;
		for (Size p = 0; p < nPop_; ++p) {
			Array tmp(currGen_[p].pop_);
			try {
				currGen_[p].cost_ = P.costFunction().value(tmp);
			} catch (Error&) {
				currGen_[p].cost_ = QL_MAX_REAL;
			}
			if (currGen_[p].cost_ < bestCost) {
				bestPop = p;
				bestCost = currGen_[p].cost_;
			}
		}

		Size lastChange = 0;
		Size lastParamChange = 0;
		for(Size i=0; i<endCriteria.maxIterations(); ++i) {

			Size newBestPop = bestPop;
			Real newBestCost = bestCost;

			for (Size p=0; p<nPop_; ++p) {
				// Find 3 different populations randomly
				Size r1;
				do {
					r1 = static_cast <Size> (uniformRng_.nextInt32() % nPop_);
				}		
				while(r1 == p || r1 == bestPop);

				Size r2;
				do {
					r2 = static_cast <Size> (uniformRng_.nextInt32() % nPop_);
				}
				while ( r2 == p || r2 == bestPop || r2 == r1);

				Size r3;
				do {
					r3 = static_cast <Size> (uniformRng_.nextInt32() % nPop_);
				} while ( r3 == p || r3 == bestPop || r3 == r1 || r3 == r2);

				for(Size j=0; j<nParam_; ++j) {
					nextGen_[p].pop_[j] = currGen_[p].pop_[j];
				}

				Size j = static_cast <Size> (uniformRng_.nextInt32() % nParam_);
				Size L = 0;
				do {
					const double tmp = 
						currGen_[      p].pop_[j] * a0_
					  + currGen_[     r1].pop_[j] * a1_
					  + currGen_[     r2].pop_[j] * a2_
					  + currGen_[     r3].pop_[j] * a3_
					  + currGen_[bestPop].pop_[j] * aBest_;

					nextGen_[p].pop_[j] =
						std::min(maxParams_[j], std::max(minParams_[j], tmp));

					j = (j+1)%nParam_;
					++L;
				} while ((uniformRng_.nextReal() < CR_) && (L < nParam_));

				// Evaluate the new population
				Array tmp(nextGen_[p].pop_);
				try {
					nextGen_[p].cost_ = P.costFunction().value(tmp);
                } catch (Error&) {
					nextGen_[p].cost_ = QL_MAX_REAL;
                }

				// Not better, discard it and keep the old one.
				if (nextGen_[p].cost_ >= currGen_[p].cost_) {
					nextGen_[p] = currGen_[p];
				}
				// Better, keep it.
				else {
					// New best?
					if (nextGen_[p].cost_ < newBestCost) {
						newBestPop = p;
						newBestCost = nextGen_[p].cost_;
					}
				}
			}

			if(std::abs(newBestCost-bestCost) > endCriteria.functionEpsilon()) {
				lastChange = i;
			}
			const Array absDiff = Abs(nextGen_[newBestPop].pop_-currGen_[bestPop].pop_);
			if(*std::max_element(absDiff.begin(), absDiff.end()) > endCriteria.rootEpsilon()) {
				lastParamChange = i;
			}

			bestPop = newBestPop;
			bestCost = newBestCost;
			currGen_ = nextGen_;

            if(i-lastChange > endCriteria.maxStationaryStateIterations()) {
				ecType = EndCriteria::StationaryFunctionValue;
				break;
			}
			if(i-lastParamChange > endCriteria.maxStationaryStateIterations()) {
				ecType = EndCriteria::StationaryPoint;
				break;
			}

            if (adaptive_) adaptParameters();
		}
		
		const Array res(currGen_[bestPop].pop_);
        P.setCurrentValue(res);
        P.setFunctionValue(bestCost);
        
        return ecType;
    }
Example #14
0
 Real accumulate (const Array &a) const {
     return *std::max_element(a.begin(), a.end());
 }
int main(int argc, char** argv)
{
  int stat = 0;
  try
  {
    bool bad = false;
    GlobalMPISession session(&argc, &argv);

    Out::os() << endl;
    Array<int> tetVerts = tuple(234, 102, 371, 259);
    Array<int> triVerts = tuple(71, 43, 183);

    Array<Array<int> > testVerts = tuple(triVerts, tetVerts);
      
    for (int test=0; test<testVerts.size(); test++)
    {
      bool more = true;

      Array<int> exVerts = testVerts[test];
      int dim = exVerts.size()-1;

      while (more)
      {
        Out::os() << "exVerts = " << exVerts << endl;
        Array<int> ufcVerts = exVerts;
        int key = -1;
        vertexSort(ufcVerts, &key);
        Out::os() << "ufcVerts = " << ufcVerts << endl;

        for (int i=0; i<exVerts.size(); i++)
        {
          int u = exVertPosToUFCVertPos(dim, key, i);
          Out::os() << "ex vert=" << exVerts[i] << " is at pos=" 
                    << u << " in UFC array and has vertID=" << ufcVerts[u] << endl;
        }

        for (int f=0; f<dim+1; f++)
        {
          Out::os() << endl << "------------------------ " << endl;
          int missingEx = mapExSideToMissingVertex(dim, f);
          int missingUfc = exVertPosToUFCVertPos(dim, key, missingEx);
          Array<int> exFc = exSide(f, exVerts);
          Array<int> ufcFc = ufcSide(missingUfc, ufcVerts);
          Out::os() << "exodus face=" << f << ", missing vert pos="
                    << missingEx << ", id=" << exVerts[missingEx]<< endl;
          Out::os() << "verts = " << exFc << endl;
          Out::os() << "missing ufc vertex pos = "
                    << missingUfc << ", id=" << ufcVerts[missingUfc]
                    << endl;

          Out::os() << "ufc verts=" << ufcFc << endl;
          bool ok = true;
          insertionSort(exFc);
          for (int j=0; j<ufcFc.size(); j++)
          {
            if (exFc[j] != ufcFc[j]) ok = false;
          }
          if (ok) Out::os() << "check OK" << endl;
          else
          {
            Out::os() << "check FAILED: sort ex=" << exFc << ", ufc=" << ufcFc
                      << endl;
            bad = true;
          }
        }
        Out::os() << endl << endl;
        more = std::next_permutation(exVerts.begin(), exVerts.end());
      }
    }
    if (!bad) 
    {
      std::cerr << "all tests PASSED" << std::endl;
    }
    else
    {
      stat = -1;
      std::cerr << "a test has FAILED" << std::endl;
    }
  }
	catch(std::exception& e)
  {
    stat = -1;
    std::cerr << "detected exception " << e.what() << std::endl;
  }

  return stat;
}
Example #16
0
const Real Gaussian1dModel::zerobondOption(
    const Option::Type &type, const Date &expiry, const Date &valueDate,
    const Date &maturity, const Rate strike, const Date &referenceDate,
    const Real y, const Handle<YieldTermStructure> &yts, const Real yStdDevs,
    const Size yGridPoints, const bool extrapolatePayoff,
    const bool flatPayoffExtrapolation) const {

    calculate();

    Time fixingTime = termStructure()->timeFromReference(expiry);
    Time referenceTime =
        referenceDate == Null<Date>()
        ? 0.0
        : termStructure()->timeFromReference(referenceDate);

    Array yg = yGrid(yStdDevs, yGridPoints, fixingTime, referenceTime, y);
    Array z = yGrid(yStdDevs, yGridPoints);

    Array p(yg.size());

    for (Size i = 0; i < yg.size(); i++) {
        Real expValDsc = zerobond(valueDate, expiry, yg[i], yts);
        Real discount =
            zerobond(maturity, expiry, yg[i], yts) / expValDsc;
        p[i] =
            std::max((type == Option::Call ? 1.0 : -1.0) * (discount - strike),
                     0.0) /
            numeraire(fixingTime, yg[i], yts) * expValDsc;
    }

    CubicInterpolation payoff(
        z.begin(), z.end(), p.begin(), CubicInterpolation::Spline, true,
        CubicInterpolation::Lagrange, 0.0, CubicInterpolation::Lagrange, 0.0);

    Real price = 0.0;
    for (Size i = 0; i < z.size() - 1; i++) {
        price += gaussianShiftedPolynomialIntegral(
                     0.0, payoff.cCoefficients()[i], payoff.bCoefficients()[i],
                     payoff.aCoefficients()[i], p[i], z[i], z[i], z[i + 1]);
    }
    if (extrapolatePayoff) {
        if (flatPayoffExtrapolation) {
            price += gaussianShiftedPolynomialIntegral(
                         0.0, 0.0, 0.0, 0.0, p[z.size() - 2], z[z.size() - 2],
                         z[z.size() - 1], 100.0);
            price += gaussianShiftedPolynomialIntegral(0.0, 0.0, 0.0, 0.0, p[0],
                     z[0], -100.0, z[0]);
        } else {
            if (type == Option::Call)
                price += gaussianShiftedPolynomialIntegral(
                             0.0, payoff.cCoefficients()[z.size() - 2],
                             payoff.bCoefficients()[z.size() - 2],
                             payoff.aCoefficients()[z.size() - 2], p[z.size() - 2],
                             z[z.size() - 2], z[z.size() - 1], 100.0);
            if (type == Option::Put)
                price += gaussianShiftedPolynomialIntegral(
                             0.0, payoff.cCoefficients()[0], payoff.bCoefficients()[0],
                             payoff.aCoefficients()[0], p[0], z[0], -100.0, z[0]);
        }
    }

    return numeraire(referenceTime, y, yts) * price;
}
    void Gaussian1dNonstandardSwaptionEngine::calculate() const {

        QL_REQUIRE(arguments_.settlementType == Settlement::Physical,
                   "cash-settled swaptions not yet implemented ...");

        Date settlement = model_->termStructure()->referenceDate();

        if (arguments_.exercise->dates().back() <=
            settlement) { // swaption is expired, possibly generated swap is not
                          // valued
            results_.value = 0.0;
            return;
        }

        boost::shared_ptr<RebatedExercise> rebatedExercise =
            boost::dynamic_pointer_cast<RebatedExercise>(arguments_.exercise);

        int idx = arguments_.exercise->dates().size() - 1;
        int minIdxAlive = static_cast<int>(
            std::upper_bound(arguments_.exercise->dates().begin(),
                             arguments_.exercise->dates().end(), settlement) -
            arguments_.exercise->dates().begin());

        NonstandardSwap swap = *arguments_.swap;
        Option::Type type =
            arguments_.type == VanillaSwap::Payer ? Option::Call : Option::Put;
        Schedule schedule = swap.fixedSchedule();
        Schedule floatSchedule = swap.floatingSchedule();

        Array npv0(2 * integrationPoints_ + 1, 0.0),
            npv1(2 * integrationPoints_ + 1, 0.0);
        Array z = model_->yGrid(stddevs_, integrationPoints_);
        Array p(z.size(), 0.0);

        // for probability computation
        std::vector<Array> npvp0, npvp1;
        if (probabilities_ != None) {
            for (Size i = 0; i < static_cast<Size>(idx - minIdxAlive + 2); ++i) {
                Array npvTmp0(2 * integrationPoints_ + 1, 0.0);
                Array npvTmp1(2 * integrationPoints_ + 1, 0.0);
                npvp0.push_back(npvTmp0);
                npvp1.push_back(npvTmp1);
            }
        }
        // end probabkility computation

        Date expiry1 = Null<Date>(), expiry0;
        Time expiry1Time = Null<Real>(), expiry0Time;

        do {

            if (idx == minIdxAlive - 1)
                expiry0 = settlement;
            else
                expiry0 = arguments_.exercise->dates()[idx];

            expiry0Time = std::max(
                model_->termStructure()->timeFromReference(expiry0), 0.0);

            Size j1 = std::upper_bound(schedule.dates().begin(),
                                       schedule.dates().end(), expiry0 - 1) -
                      schedule.dates().begin();
            Size k1 =
                std::upper_bound(floatSchedule.dates().begin(),
                                 floatSchedule.dates().end(), expiry0 - 1) -
                floatSchedule.dates().begin();

            // todo add openmp support later on (as in gaussian1dswaptionengine)

            for (Size k = 0; k < (expiry0 > settlement ? npv0.size() : 1);
                 k++) {

                Real price = 0.0;
                if (expiry1Time != Null<Real>()) {
                    Real zSpreadDf =
                        oas_.empty() ? 1.0
                                     : std::exp(-oas_->value() *
                                                (expiry1Time - expiry0Time));
                    Array yg = model_->yGrid(stddevs_, integrationPoints_,
                                             expiry1Time, expiry0Time,
                                             expiry0 > settlement ? z[k] : 0.0);
                    CubicInterpolation payoff0(
                        z.begin(), z.end(), npv1.begin(),
                        CubicInterpolation::Spline, true,
                        CubicInterpolation::Lagrange, 0.0,
                        CubicInterpolation::Lagrange, 0.0);
                    for (Size i = 0; i < yg.size(); i++) {
                        p[i] = payoff0(yg[i], true);
                    }
                    CubicInterpolation payoff1(
                        z.begin(), z.end(), p.begin(),
                        CubicInterpolation::Spline, true,
                        CubicInterpolation::Lagrange, 0.0,
                        CubicInterpolation::Lagrange, 0.0);
                    for (Size i = 0; i < z.size() - 1; i++) {
                        price += model_->gaussianShiftedPolynomialIntegral(
                                     0.0, payoff1.cCoefficients()[i],
                                     payoff1.bCoefficients()[i],
                                     payoff1.aCoefficients()[i], p[i], z[i],
                                     z[i], z[i + 1]) *
                                 zSpreadDf;
                    }
                    if (extrapolatePayoff_) {
                        if (flatPayoffExtrapolation_) {
                            price +=
                                model_->gaussianShiftedPolynomialIntegral(
                                    0.0, 0.0, 0.0, 0.0, p[z.size() - 2],
                                    z[z.size() - 2], z[z.size() - 1], 100.0) *
                                zSpreadDf;
                            price += model_->gaussianShiftedPolynomialIntegral(
                                         0.0, 0.0, 0.0, 0.0, p[0], z[0], -100.0,
                                         z[0]) *
                                     zSpreadDf;
                        } else {
                            if (type == Option::Call)
                                price +=
                                    model_->gaussianShiftedPolynomialIntegral(
                                        0.0,
                                        payoff1.cCoefficients()[z.size() - 2],
                                        payoff1.bCoefficients()[z.size() - 2],
                                        payoff1.aCoefficients()[z.size() - 2],
                                        p[z.size() - 2], z[z.size() - 2],
                                        z[z.size() - 1], 100.0) *
                                    zSpreadDf;
                            if (type == Option::Put)
                                price +=
                                    model_->gaussianShiftedPolynomialIntegral(
                                        0.0, payoff1.cCoefficients()[0],
                                        payoff1.bCoefficients()[0],
                                        payoff1.aCoefficients()[0], p[0], z[0],
                                        -100.0, z[0]) *
                                    zSpreadDf;
                        }
                    }
                }

                npv0[k] = price;

                // for probability computation
                if (probabilities_ != None) {
                    for (Size m = 0; m < npvp0.size(); m++) {
                        Real price = 0.0;
                        if (expiry1Time != Null<Real>()) {
                            Real zSpreadDf =
                                oas_.empty()
                                    ? 1.0
                                    : std::exp(-oas_->value() *
                                               (expiry1Time - expiry0Time));
                            Array yg = model_->yGrid(
                                stddevs_, integrationPoints_, expiry1Time,
                                expiry0Time, expiry0 > settlement ? z[k] : 0.0);
                            CubicInterpolation payoff0(
                                z.begin(), z.end(), npvp1[m].begin(),
                                CubicInterpolation::Spline, true,
                                CubicInterpolation::Lagrange, 0.0,
                                CubicInterpolation::Lagrange, 0.0);
                            for (Size i = 0; i < yg.size(); i++) {
                                p[i] = payoff0(yg[i], true);
                            }
                            CubicInterpolation payoff1(
                                z.begin(), z.end(), p.begin(),
                                CubicInterpolation::Spline, true,
                                CubicInterpolation::Lagrange, 0.0,
                                CubicInterpolation::Lagrange, 0.0);
                            for (Size i = 0; i < z.size() - 1; i++) {
                                price +=
                                    model_->gaussianShiftedPolynomialIntegral(
                                        0.0, payoff1.cCoefficients()[i],
                                        payoff1.bCoefficients()[i],
                                        payoff1.aCoefficients()[i], p[i], z[i],
                                        z[i], z[i + 1]) *
                                    zSpreadDf;
                            }
                            if (extrapolatePayoff_) {
                                if (flatPayoffExtrapolation_) {
                                    price +=
                                        model_
                                            ->gaussianShiftedPolynomialIntegral(
                                                  0.0, 0.0, 0.0, 0.0,
                                                  p[z.size() - 2],
                                                  z[z.size() - 2],
                                                  z[z.size() - 1], 100.0) *
                                        zSpreadDf;
                                    price +=
                                        model_
                                            ->gaussianShiftedPolynomialIntegral(
                                                  0.0, 0.0, 0.0, 0.0, p[0],
                                                  z[0], -100.0, z[0]) *
                                        zSpreadDf;
                                } else {
                                    if (type == Option::Call)
                                        price +=
                                            model_
                                                ->gaussianShiftedPolynomialIntegral(
                                                      0.0,
                                                      payoff1.cCoefficients()
                                                          [z.size() - 2],
                                                      payoff1.bCoefficients()
                                                          [z.size() - 2],
                                                      payoff1.aCoefficients()
                                                          [z.size() - 2],
                                                      p[z.size() - 2],
                                                      z[z.size() - 2],
                                                      z[z.size() - 1], 100.0) *
                                            zSpreadDf;
                                    if (type == Option::Put)
                                        price +=
                                            model_
                                                ->gaussianShiftedPolynomialIntegral(
                                                      0.0,
                                                      payoff1
                                                          .cCoefficients()[0],
                                                      payoff1
                                                          .bCoefficients()[0],
                                                      payoff1
                                                          .aCoefficients()[0],
                                                      p[0], z[0], -100.0,
                                                      z[0]) *
                                            zSpreadDf;
                                }
                            }
                        }

                        npvp0[m][k] = price;
                    }
                }
                // end probability computation

                if (expiry0 > settlement) {
                    Real floatingLegNpv = 0.0;
                    for (Size l = k1; l < arguments_.floatingCoupons.size();
                         l++) {
                        Real zSpreadDf =
                            oas_.empty()
                                ? 1.0
                                : std::exp(
                                      -oas_->value() *
                                      (model_->termStructure()
                                           ->dayCounter()
                                           .yearFraction(
                                                expiry0,
                                                arguments_
                                                    .floatingPayDates[l])));
                        Real amount;
                        if (arguments_.floatingIsRedemptionFlow[l])
                            amount = arguments_.floatingCoupons[l];
                        else
                            amount = arguments_.floatingNominal[l] *
                                     arguments_.floatingAccrualTimes[l] *
                                     (arguments_.floatingGearings[l] *
                                          model_->forwardRate(
                                              arguments_.floatingFixingDates[l],
                                              expiry0, z[k],
                                              arguments_.swap->iborIndex()) +
                                      arguments_.floatingSpreads[l]);
                        floatingLegNpv +=
                            amount *
                            model_->zerobond(arguments_.floatingPayDates[l],
                                             expiry0, z[k], discountCurve_) *
                            zSpreadDf;
                    }
                    Real fixedLegNpv = 0.0;
                    for (Size l = j1; l < arguments_.fixedCoupons.size(); l++) {
                        Real zSpreadDf =
                            oas_.empty()
                                ? 1.0
                                : std::exp(
                                      -oas_->value() *
                                      (model_->termStructure()
                                           ->dayCounter()
                                           .yearFraction(
                                                expiry0,
                                                arguments_.fixedPayDates[l])));
                        fixedLegNpv +=
                            arguments_.fixedCoupons[l] *
                            model_->zerobond(arguments_.fixedPayDates[l],
                                             expiry0, z[k], discountCurve_) *
                            zSpreadDf;
                    }
                    Real rebate = 0.0;
                    Real zSpreadDf = 1.0;
                    Date rebateDate = expiry0;
                    if (rebatedExercise != NULL) {
                        rebate = rebatedExercise->rebate(idx);
                        rebateDate = rebatedExercise->rebatePaymentDate(idx);
                        zSpreadDf =
                            oas_.empty()
                                ? 1.0
                                : std::exp(
                                      -oas_->value() *
                                      (model_->termStructure()
                                           ->dayCounter()
                                           .yearFraction(expiry0, rebateDate)));
                    }
                    Real exerciseValue =
                        ((type == Option::Call ? 1.0 : -1.0) *
                             (floatingLegNpv - fixedLegNpv) +
                         rebate * model_->zerobond(rebateDate, expiry0, z[k],
                                                   discountCurve_) *
                             zSpreadDf) /
                        model_->numeraire(expiry0Time, z[k], discountCurve_);

                    // for probability computation
                    if (probabilities_ != None) {
                        if (idx == static_cast<int>(
                                       arguments_.exercise->dates().size()) -
                                       1) // if true we are at the latest date,
                                          // so we init
                                          // the no call probability
                            npvp0.back()[k] =
                                probabilities_ == Naive
                                    ? 1.0
                                    : 1.0 / (model_->zerobond(expiry0Time, 0.0,
                                                              0.0,
                                                              discountCurve_) *
                                             model_->numeraire(expiry0, z[k],
                                                               discountCurve_));
                        if (exerciseValue >= npv0[k]) {
                            npvp0[idx - minIdxAlive][k] =
                                probabilities_ == Naive
                                    ? 1.0
                                    : 1.0 /
                                          (model_->zerobond(expiry0Time, 0.0,
                                                            0.0,
                                                            discountCurve_) *
                                           model_->numeraire(expiry0Time, z[k],
                                                             discountCurve_));
                            for (Size ii = idx - minIdxAlive + 1;
                                 ii < npvp0.size(); ii++)
                                npvp0[ii][k] = 0.0;
                        }
                    }
                    // end probability computation

                    npv0[k] = std::max(npv0[k], exerciseValue);
                }
            }

            npv1.swap(npv0);

            // for probability computation
            if (probabilities_ != None) {
                for (Size i = 0; i < npvp0.size(); i++)
                    npvp1[i].swap(npvp0[i]);
            }
            // end probability computation

            expiry1 = expiry0;
            expiry1Time = expiry0Time;

        } while (--idx >= minIdxAlive - 1);

        results_.value = npv1[0] * model_->numeraire(0.0, 0.0, discountCurve_);

        // for probability computation
        if (probabilities_ != None) {
            std::vector<Real> prob(npvp0.size());
            for (Size i = 0; i < npvp0.size(); i++) {
                prob[i] = npvp1[i][0] *
                          (probabilities_ == Naive
                               ? 1.0
                               : model_->numeraire(0.0, 0.0, discountCurve_));
            }
            results_.additionalResults["probabilities"] = prob;
        }
        // end probability computation
    }
Example #18
0
 //! method to overload to compute the cost function value in x
 virtual Real value(const Array& x) const {
     Array v = values(x);
     std::transform(v.begin(), v.end(), v.begin(), square<Real>());
     return std::sqrt(std::accumulate(v.begin(), v.end(), 0.0) /
                      static_cast<Real>(v.size()));
 }
Example #19
0
	inline bool operator<(const Array &k) const throw() { return std::lexicographical_compare(begin(),end(),k.begin(),k.end()); }
Example #20
0
 Array(const Array<Data>& rhs)
     : mCount(rhs.size())
     , mData(rhs.size() != 0 ? Alloc().allocate(rhs.size()) : 0)
 {
     std::copy(rhs.begin(), rhs.end(), mData);
 }
OverlappingRowMatrix<MatrixType>::
OverlappingRowMatrix (const Teuchos::RCP<const row_matrix_type>& A,
                      const int overlapLevel) :
  A_ (A),
  OverlapLevel_ (overlapLevel),
  UseSubComm_ (false)
{
  using Teuchos::RCP;
  using Teuchos::rcp;
  using Teuchos::Array;
  using Teuchos::outArg;
  using Teuchos::rcp_const_cast;
  using Teuchos::rcp_dynamic_cast;
  using Teuchos::rcp_implicit_cast;
  using Teuchos::REDUCE_SUM;
  using Teuchos::reduceAll;
  typedef Tpetra::global_size_t GST;
  typedef Tpetra::CrsGraph<local_ordinal_type,
                           global_ordinal_type, node_type> crs_graph_type;
  TEUCHOS_TEST_FOR_EXCEPTION(
    OverlapLevel_ <= 0, std::runtime_error,
    "Ifpack2::OverlappingRowMatrix: OverlapLevel must be > 0.");
  TEUCHOS_TEST_FOR_EXCEPTION(
    A_->getComm()->getSize() == 1, std::runtime_error,
    "Ifpack2::OverlappingRowMatrix: Matrix must be "
    "distributed over more than one MPI process.");

  RCP<const crs_matrix_type> ACRS =
    rcp_dynamic_cast<const crs_matrix_type, const row_matrix_type> (A_);
  TEUCHOS_TEST_FOR_EXCEPTION(
    ACRS.is_null (), std::runtime_error,
    "Ifpack2::OverlappingRowMatrix: The input matrix must be a Tpetra::"
    "CrsMatrix with matching template parameters.  This class currently "
    "requires that CrsMatrix's fifth template parameter be the default.");
  RCP<const crs_graph_type> A_crsGraph = ACRS->getCrsGraph ();

  const size_t numMyRowsA = A_->getNodeNumRows ();
  const global_ordinal_type global_invalid =
    Teuchos::OrdinalTraits<global_ordinal_type>::invalid ();

  // Temp arrays
  Array<global_ordinal_type> ExtElements;
  RCP<map_type>        TmpMap;
  RCP<crs_graph_type>  TmpGraph;
  RCP<import_type>     TmpImporter;
  RCP<const map_type>  RowMap, ColMap;

  // The big import loop
  for (int overlap = 0 ; overlap < OverlapLevel_ ; ++overlap) {
    // Get the current maps
    if (overlap == 0) {
      RowMap = A_->getRowMap ();
      ColMap = A_->getColMap ();
    }
    else {
      RowMap = TmpGraph->getRowMap ();
      ColMap = TmpGraph->getColMap ();
    }

    const size_t size = ColMap->getNodeNumElements () - RowMap->getNodeNumElements ();
    Array<global_ordinal_type> mylist (size);
    size_t count = 0;

    // define the set of rows that are in ColMap but not in RowMap
    for (local_ordinal_type i = 0 ; (size_t) i < ColMap->getNodeNumElements() ; ++i) {
      const global_ordinal_type GID = ColMap->getGlobalElement (i);
      if (A_->getRowMap ()->getLocalElement (GID) == global_invalid) {
        typedef typename Array<global_ordinal_type>::iterator iter_type;
        const iter_type end = ExtElements.end ();
        const iter_type pos = std::find (ExtElements.begin (), end, GID);
        if (pos == end) {
          ExtElements.push_back (GID);
          mylist[count] = GID;
          ++count;
        }
      }
    }

    // mfh 24 Nov 2013: We don't need TmpMap, TmpGraph, or
    // TmpImporter after this loop, so we don't have to construct them
    // on the last round.
    if (overlap + 1 < OverlapLevel_) {
      // Allocate & import new matrices, maps, etc.
      //
      // FIXME (mfh 24 Nov 2013) Do we always want to use index base
      // zero?  It doesn't really matter, since the actual index base
      // (in the current implementation of Map) will always be the
      // globally least GID.
      TmpMap = rcp (new map_type (global_invalid, mylist (0, count),
                                  Teuchos::OrdinalTraits<global_ordinal_type>::zero (),
                                  A_->getComm (), A_->getNode ()));
      TmpGraph = rcp (new crs_graph_type (TmpMap, 0));
      TmpImporter = rcp (new import_type (A_->getRowMap (), TmpMap));

      TmpGraph->doImport (*A_crsGraph, *TmpImporter, Tpetra::INSERT);
      TmpGraph->fillComplete (A_->getDomainMap (), TmpMap);
    }
  }

  // build the map containing all the nodes (original
  // matrix + extended matrix)
  Array<global_ordinal_type> mylist (numMyRowsA + ExtElements.size ());
  for (local_ordinal_type i = 0; (size_t)i < numMyRowsA; ++i) {
    mylist[i] = A_->getRowMap ()->getGlobalElement (i);
  }
  for (local_ordinal_type i = 0; i < ExtElements.size (); ++i) {
    mylist[i + numMyRowsA] = ExtElements[i];
  }

  RowMap_ = rcp (new map_type (global_invalid, mylist (),
                               Teuchos::OrdinalTraits<global_ordinal_type>::zero (),
                               A_->getComm (), A_->getNode ()));
  ColMap_ = RowMap_;

  // now build the map corresponding to all the external nodes
  // (with respect to A().RowMatrixRowMap().
  ExtMap_ = rcp (new map_type (global_invalid, ExtElements (),
                               Teuchos::OrdinalTraits<global_ordinal_type>::zero (),
                               A_->getComm (), A_->getNode ()));
  ExtMatrix_ = rcp (new crs_matrix_type (ExtMap_, ColMap_, 0));
  ExtImporter_ = rcp (new import_type (A_->getRowMap (), ExtMap_));

  RCP<crs_matrix_type> ExtMatrixCRS =
    rcp_dynamic_cast<crs_matrix_type, row_matrix_type> (ExtMatrix_);
  ExtMatrixCRS->doImport (*ACRS, *ExtImporter_, Tpetra::INSERT);
  ExtMatrixCRS->fillComplete (A_->getDomainMap (), RowMap_);

  Importer_ = rcp (new import_type (A_->getRowMap (), RowMap_));

  // fix indices for overlapping matrix
  const size_t numMyRowsB = ExtMatrix_->getNodeNumRows ();

  GST NumMyNonzeros_tmp = A_->getNodeNumEntries () + ExtMatrix_->getNodeNumEntries ();
  GST NumMyRows_tmp = numMyRowsA + numMyRowsB;
  {
    GST inArray[2], outArray[2];
    inArray[0] = NumMyNonzeros_tmp;
    inArray[1] = NumMyRows_tmp;
    outArray[0] = 0;
    outArray[1] = 0;
    reduceAll<int, GST> (* (A_->getComm ()), REDUCE_SUM, 2, inArray, outArray);
    NumGlobalNonzeros_ = outArray[0];
    NumGlobalRows_ = outArray[1];
  }
  // reduceAll<int, GST> (* (A_->getComm ()), REDUCE_SUM, NumMyNonzeros_tmp,
  //                      outArg (NumGlobalNonzeros_));
  // reduceAll<int, GST> (* (A_->getComm ()), REDUCE_SUM, NumMyRows_tmp,
  //                      outArg (NumGlobalRows_));

  MaxNumEntries_ = A_->getNodeMaxNumRowEntries ();
  if (MaxNumEntries_ < ExtMatrix_->getNodeMaxNumRowEntries ()) {
    MaxNumEntries_ = ExtMatrix_->getNodeMaxNumRowEntries ();
  }

  // Create the graph (returned by getGraph()).
  typedef Details::OverlappingRowGraph<row_graph_type> row_graph_impl_type;
  RCP<row_graph_impl_type> graph =
    rcp (new row_graph_impl_type (A_->getGraph (),
                                  ExtMatrix_->getGraph (),
                                  RowMap_,
                                  ColMap_,
                                  NumGlobalRows_,
                                  NumGlobalRows_, // # global cols == # global rows
                                  NumGlobalNonzeros_,
                                  MaxNumEntries_,
                                  rcp_const_cast<const import_type> (Importer_),
                                  rcp_const_cast<const import_type> (ExtImporter_)));
  graph_ = rcp_const_cast<const row_graph_type> (rcp_implicit_cast<row_graph_type> (graph));
  // Resize temp arrays
  Indices_.resize (MaxNumEntries_);
  Values_.resize (MaxNumEntries_);
}
Example #22
0
// This test works (and exercises the interesting case) in serial mode
// or for 1 MPI process, but it was originally written for 2 MPI
// processes.
TEUCHOS_UNIT_TEST( Map, Bug5822_StartWithZeroThenSkipTo3Billion )
{
  int locallyThrew = 0;
  int globallyThrew = 0;

  RCP<const Comm<int> > comm = Teuchos::DefaultComm<int>::getComm ();
  const int myRank = comm->getRank ();
  const int numProcs = comm->getSize ();
  TEUCHOS_TEST_FOR_EXCEPTION(numProcs != 2, std::logic_error,
    "This test only makes sense to run with 2 MPI processes.");

  // Pick the global index type to have 64 bits.
#if ! defined (HAVE_TPETRA_INT_LONG_LONG) && ! defined (HAVE_TPETRA_INT_LONG)
  typedef Tpetra::Map<>::global_ordinal_type GO; // just to make it compile
  out << "This test only makes sense if GO = long or long long is enabled.  "
    "That is because the test is supposed to exercise global indices greater "
    "than the maximum that int can represent (about 2 billion)." << endl;
  return;
#else
#  if defined (HAVE_TPETRA_INT_LONG_LONG)
  typedef long long GO;
  if (sizeof (long long) <= 4) {
    out << "sizeof (long long) = " << sizeof (long long) << " <= 4.  "
      "This test only makes sense if sizeof (long long) >= 8.  "
      "That is because the test is supposed to exercise global indices "
      "greater than the maximum that int can represent (about 2 billion)."
        << endl;
    return;
  }
#  elif defined (HAVE_TPETRA_INT_LONG)
  typedef long GO;
  if (sizeof (long) <= 4) {
    out << "sizeof (long) = " << sizeof (long) << " <= 4.  "
      "This test only makes sense if sizeof (long) >= 8.  "
      "That is because the test is supposed to exercise global indices "
      "greater than the maximum that int can represent (about 2 billion)."
        << endl;
    return;
  }
#  endif
#endif
  typedef Tpetra::Map<>::local_ordinal_type LO;
  typedef Tpetra::Map<>::node_type NT;
  typedef Tpetra::Map<LO, GO, NT> map_type;

  // Proc 0 gets [0, 3B, 3B+2, 3B+4, 3B+8, 3B+10] (6 GIDs).
  // Proc 1 gets [3B+12, 3B+14, 3B+16, 3B+18, 3B+20] (5 GIDs).
  //
  // The GIDs are not contiguous in order to prevent Map from
  // detecting contiguity and bypassing the noncontiguous Map
  // case.
  const size_t localNumElts = (myRank == 0) ? 6 : 5;
  const global_size_t globalNumElts = 1 + 5*comm->getSize ();
  const GO globalFirstGid = 1L;
  const GO threeBillion = static_cast<GO> (3000000000L);

  Array<GO> myGids (localNumElts);
  // Make a copy, just to make sure that Map's constructor didn't
  // sneakily change the input ArrayView.
  Array<GO> myGidsExpected (localNumElts);
  if (myRank == 0) {
    myGids[0] = globalFirstGid;
    myGidsExpected[0] = myGids[0];
    myGids[1] = threeBillion;
    myGidsExpected[1] = myGids[1];
    for (size_t k = 2; k < localNumElts; ++k) {
      myGids[k] = myGids[k-1] + 2;
      myGidsExpected[k] = myGids[k];
    }
  } else {
    myGids[0] = threeBillion + as<GO> ((localNumElts+1) * 2);
    myGidsExpected[0] = myGids[0];
    for (size_t k = 1; k < localNumElts; ++k) {
      myGids[k] = myGids[k-1] + 2;
      myGidsExpected[k] = myGids[k];
    }
  }

  RCP<NT> node;
  {
    Teuchos::ParameterList defaultParams;
    node = Teuchos::rcp (new NT (defaultParams));
  }
  // if (myRank == 0) {
  //   std::cout << "type '0' and hit enter" << std::endl;
  //   int zero;
  //   cin >> zero;
  // }
  // comm->barrier();
  // Tpetra::Map requires that the index base is less than the minimum GID.
  const GO indexBase = 0L;
  out << "Constructing the Map" << endl;
  RCP<const map_type> map;
  try {
    TEUCHOS_FUNC_TIME_MONITOR("Construct Map");
    map = Teuchos::rcp (new map_type (globalNumElts, myGids (), indexBase, comm, node));
  }
  catch (std::exception& e) {
    locallyThrew = 1;
    std::ostringstream os;
    os << "Proc " << myRank << ": At Map creation, locally threw exception: "
       << e.what () << endl;
    cerr << os.str ();
  }
  globallyThrew = 0;
  reduceAll<int, int> (*comm, REDUCE_MAX, locallyThrew, outArg (globallyThrew));
  TEST_EQUALITY_CONST( globallyThrew, 0 );

  cerr << myRank << ": Querying the Map for local elements" << endl;
  {
    TEUCHOS_FUNC_TIME_MONITOR("Querying the Map for local elements");
    ArrayView<const GO> myGidsFound = map->getNodeElementList ();
    TEST_COMPARE_ARRAYS( myGidsExpected (), myGidsFound () );
  }

  cerr << myRank << ": Querying the Map for remote elements" << endl;
  // Proc 0 gets [0, 3B, 3B+2, 3B+4, 3B+8, 3B+10] (6 GIDs).
  // Proc 1 gets [3B+12, 3B+14, 3B+16, 3B+18, 3B+20] (5 GIDs).
  {
    TEUCHOS_FUNC_TIME_MONITOR("Querying the Map for remote elements");

    const int numRemoteGids = (myRank == 0) ? 5 : 6;

    Array<GO> remoteGids (numRemoteGids);
    Array<int> remotePids (numRemoteGids, -1);
    Array<LO> remoteLids (numRemoteGids, Teuchos::OrdinalTraits<LO>::invalid ());
    if (myRank == 0) {
      try {
        Array<int> expectedRemotePids (numRemoteGids);
        std::fill (expectedRemotePids.begin (), expectedRemotePids.end (), 1);
        Array<int> expectedRemoteLids (numRemoteGids);
        expectedRemoteLids[0] = 0;
        expectedRemoteLids[1] = 1;
        expectedRemoteLids[2] = 2;
        expectedRemoteLids[3] = 3;
        expectedRemoteLids[4] = 4;
        remoteGids[0] = threeBillion + 12;
        remoteGids[1] = threeBillion + 14;
        remoteGids[2] = threeBillion + 16;
        remoteGids[3] = threeBillion + 18;
        remoteGids[4] = threeBillion + 20;

        comm->barrier ();
        cerr << myRank << ": Calling getRemoteIndexList" << endl;
        comm->barrier ();
        map->getRemoteIndexList (remoteGids (), remotePids (), remoteLids ());

        TEST_COMPARE_ARRAYS( remotePids (), expectedRemotePids () );
        TEST_COMPARE_ARRAYS( remoteLids (), expectedRemoteLids () );
      }
      catch (std::exception& e) {
        locallyThrew = 1;
        std::ostringstream os;
        os << "Proc " << myRank << ": getRemoteIndexList locally threw "
          "exception: " << e.what () << endl;
        cerr << os.str ();
      }
    }
    else if (myRank == 1) {
      try {
        Array<int> expectedRemotePids (numRemoteGids);
        std::fill (expectedRemotePids.begin (), expectedRemotePids.end (), 0);
        Array<int> expectedRemoteLids (numRemoteGids);
        expectedRemoteLids[0] = 0;
        expectedRemoteLids[1] = 1;
        expectedRemoteLids[2] = 2;
        expectedRemoteLids[3] = 3;
        expectedRemoteLids[4] = 4;
        expectedRemoteLids[5] = 5;
        remoteGids[0] = 0;
        remoteGids[1] = threeBillion;
        remoteGids[2] = threeBillion + 2;
        remoteGids[3] = threeBillion + 4;
        remoteGids[4] = threeBillion + 8;
        remoteGids[5] = threeBillion + 10;

        comm->barrier ();
        cerr << myRank << ": Calling getRemoteIndexList" << endl;
        comm->barrier ();
        map->getRemoteIndexList (remoteGids (), remotePids (), remoteLids ());

        TEST_COMPARE_ARRAYS( remotePids (), expectedRemotePids () );
        TEST_COMPARE_ARRAYS( remoteLids (), expectedRemoteLids () );
      }
      catch (std::exception& e) {
        locallyThrew = 1;
        std::ostringstream os;
        os << "Proc " << myRank << ": getRemoteIndexList locally threw "
          "exception: " << e.what () << endl;
        cerr << os.str ();
      }
    }

    reduceAll<int, int> (*comm, REDUCE_MAX, locallyThrew, outArg (globallyThrew));
    TEST_EQUALITY_CONST( globallyThrew, 0 );
  }

  cout << endl; // make TimeMonitor output neat on test line
  Teuchos::TimeMonitor::summarize();
}
Example #23
0
 void operator()(SV **&sp, Array &result) {
     EXTEND(sp, result.length());
     for (ArrayIterator it = result.begin(); it != result.end(); ++it) {
         PUSHs(*it);
     }
 }
Example #24
0
int main() {
	plan(65);
	Interpreter universe;
	Array array = universe.list();
	is(array.length(), 0u, "array.length() == 0");

	array.push(1);
	note("array.push(1)");
	is(array[0], 1, "array[0] == 1");
	is(array[0], "1", "array[0] == \"1\"");

	++array[0];
	note("++array[0]");
	is(array[0], 2, "array[0] == 1");
	is(array[0], "2", "array[0] == \"1\"");

	note("array.push(UV_MAX)");
	array.push(UV_MAX);
	is(array[1], UV_MAX, "array[1] == UV_MAX");
	ok(array[1] > static_cast<UV>(IV_MAX), "array[1] > (unsigned)IV_MAX");
	is(array[1], -1, "array[1] == -1");

	array.push(NV_MAX);
	note("array.push(NV_MAX)");
	is(array[2], NV_MAX, "array[2] == NV_MAX");

	array.push("test");
	note("array.push(\"test\")");
	is(array[3], "test", "array[3] == \"test\"");

	array.push(Raw_string("test"));
	note("array.push(Raw_string(\"test\"))");
	is(array[4], "test", "array[4] == \"test\"");

	array.push(universe.value_of(1));
	note("array.push(universe.value_of(1))");
	is(array[5], 1, "array[4] == 1");

	array.clear();
	note("array.clear()");
	is(array.length(), 0u, "array.length() == 0");
	array.push(1, UV_MAX, NV_MAX, "test", universe.undef());
	note("array.push(1, UV_MAX, NV_MAX, \"test\")");

	is(array[0], 1, "array[0] == 1");
	is(array[1], UV_MAX, "array[1] == UV_MAX");
	is(array[2], NV_MAX, "array[2] == NV_MAX");
	is(array[3], "test", "array[3] == \"test\"");
	ok(array.exists(4), "exists array[4]");
	ok(!array[4].defined(), "not: defined array[4]");

	array.push(array);
	note("array.push(array)");
	is(array[5], 1, "array[5] == 1");
	is(array[6], UV_MAX, "array[6] == UV_MAX");
	is(array[7], NV_MAX, "array[7] == NV_MAX");
	is(array[8], "test", "array[8] == \"test\"");
	ok(array.exists(9), "exists array[9]");
	ok(!array[9].defined(), "not: defined array[9]");

	array.clear();
	array.unshift("test");
	array.unshift(NV_MAX);
	array.unshift(UV_MAX);
	array.unshift(1);
	note("array.unshift(1, UV_MAX, NV_MAX, \"test\")");

	is(array[0], 1, "array[0] == 1");
	is(array[1], UV_MAX, "array[1] == UV_MAX");
	is(array[2], NV_MAX, "array[2] == NV_MAX");
	is(array[3], "test", "array[3] == \"test\"");

	ok(array.exists(3), "exists array[3]");
	String string = array.remove(3);
	note("string = array.remove(3)");
	ok(string.length(), "string.length()");
	ok(!array.exists(3), "not: exists array[3]");
	is(array.length(), 3u, "array.length() == 3");

	array.length() = 2;
	note("array.length() = 2");
	ok(!array.exists(2), "not: exists array[2]");
	is(array.length(), 2u, "array.length() == 2");
	array.extend(4);
	note("array.extend(4)");
	is(array.length(), 2u, "array.length == 2");

	Array numbers = universe.list(1, 2, 3, 4);
	Array squares = numbers.map( [](IV x) { return x * x; } );
	for (int i = 0; i < 4; ++i) {
		std::string num = boost::lexical_cast<std::string>(i);
		is(squares[i], (IV)(numbers[i]) * numbers[i], std::string("squares[") + num + "] is numbers[" + num + "] ** 2");
	}

	Array big = squares.grep([](IV x){ return x > 8; });
	note("Array big = squares.grep($_ > 8)");
	is(big.length(), 2u, "big.length == 2");
	is(big[0], 9, "big[0] == 9");

	Array doubles = numbers.map([] (IV x) { return x * 2; } );
	is(doubles[3], 8, "numbers.map(_1 * 2)[3] == 8");

	IV sum = squares.reduce([] (IV left, IV right) { return left + right;}, 0);
	note("sum = squares.reduce(_1 + _2, 0u)");
	is(sum, 30, "sum == 30");

	big.each([] (Scalar::Value& x) { x *= 2; });
	note("big.each(_1 *= 2)");
	is(big[0], 18, "big[0] == 18");

	const Array forties = universe.list(46, 47, 48, 49);
	pass(); //XXX
	std::for_each(forties.begin(), forties.end(), [](IV x) { is(TAP::encountered(), x, "encountered == 4x");} );

	ok(forties.any([](IV x) { return x == 48; }), "forties.any(_1 == 48)");
	ok(forties.all([](IV x) { return x > 45; }), "forties.all(_1 > 45)");
	ok(forties.none([](IV x) { return x == 30; }), "forties.none(_1 == 30)");
	ok(forties.notall([](IV x) { return x < 48; }), "forties.notall(_1 < 48");

	is_convertible<Ref<Array>, Ref<Any> >("Ref<Array> is convertible into a Ref<Any>");
	is_inconvertible<Ref<Any>, Ref<Array> >("Ref<Any> is not convertible into a Ref<Array>");//XXX
	is_convertible<Scalar::Temp, Ref<Array> >("Ref<Array> is convertible into a Ref<Any>");
	is_inconvertible<Ref<Array>, Ref<Hash> >("Ref<Array> is not convertible into a Ref<Hash>");

	Ref<Array> ref = forties.take_ref();

	is(ref[0], 46, "ref[0] == 46");
	Array fifties = *ref;
	note("++ref[0]");
	++ref[0];
	is(ref[0], 47, "ref[0] == 47");
	is(forties[0], 47, "forties[0] == 47");
	is(fifties[0], 46, "fiftes[0] == 46");

	*ref = fifties;
	note("*ref = fifties");
	is(ref[0], 46, "ref[0] == 46");
	is(forties[0], 46, "forties[0] == 46");

	Ref<Any> last = ref;
	ok(true, "Ref<Any> last = ref");

	return exit_status();
}
Example #25
0
void HMM::em_loop(Perplexity& perp, SentenceHandler& sHandler1,
                  bool dump_alignment, const char* alignfile, Perplexity& viterbi_perp,
                  bool test,bool doInit,int) {
  WordIndex i, j, l, m;
  double cross_entropy;
  int pair_no=0;
  perp.clear();
  viterbi_perp.clear();
  ofstream of2;
  // for each sentence pair in the corpus
  if (dump_alignment||FEWDUMPS)
    of2.open(alignfile);
  SentencePair sent;
  sHandler1.rewind();
  while (sHandler1.getNextSentence(sent)) {
    const Vector<WordIndex>& es = sent.get_eSent();
    const Vector<WordIndex>& fs = sent.get_fSent();
    const float so  = sent.getCount();
    l = es.size() - 1;
    m = fs.size() - 1;
    cross_entropy = log(1.0);
    Vector<WordIndex> viterbi_alignment(fs.size());

    unsigned int I=2*l,J=m;
    bool DependencyOfJ=(CompareAlDeps&(16|8))||(g_prediction_in_alignments==2);
    bool DependencyOfPrevAJ=(CompareAlDeps&(2|4))||(g_prediction_in_alignments==0);
    HMMNetwork *net= makeHMMNetwork(es,fs,doInit);
    Array<double> gamma;
    Array<Array2<double> > epsilon(DependencyOfJ?(m-1):1);
    double trainProb;
    trainProb=ForwardBackwardTraining(*net,gamma,epsilon);
    if (!test)
    {
      double *gp=conv<double>(gamma.begin());
      for (unsigned int i2=0;i2<J;i2++)for (unsigned int i1=0;i1<I;++i1,++gp)
                                        if (*gp>MINCOUNTINCREASE)
                                        {
                                          COUNT add= *gp*so;
                                          if (i1>=l)
                                          {
                                            tTable.incCount(es[0],fs[1+i2],add);
                                            aCountTable.getRef(0,i2+1,l,m)+=add;
                                          }
                                          else
                                          {
                                            tTable.incCount(es[1+i1],fs[1+i2],add);
                                            aCountTable.getRef(1+i1,1+i2,l,m)+=add;
                                          }
                                        }
      double p0c=0.0,np0c=0.0;
      for (unsigned int jj=0;jj<epsilon.size();jj++)
      {
        int frenchClass=fwordclasses.getClass(fs[1+min(int(m)-1,int(jj)+1)]);
        double *ep=epsilon[jj].begin();
        if (ep)
        {
          //for (i=0;i<I;i++)
          //  normalize_if_possible_with_increment(ep+i,ep+i+I*I,I);
          //    for (i=0;i<I*I;++i)
          //  ep[i] *= I;
          //if (DependencyOfJ)
          //  if (J-1)
          //    for (i=0;i<I*I;++i)
          //      ep[i] /= (J-1);
          double mult=1.0;
          mult*=l;
          //if (DependencyOfJ && J-1)
          //  mult/=(J-1);
          for (i=0;i<I;i++)
          {
            for (unsigned int i_bef=0;i_bef<I;i_bef++,ep++)
            {
              CLASSIFY(i,i_empty,ireal);
              CLASSIFY2(i_bef,i_befreal);
              if (i_empty)
                p0c+=*ep * mult;
              else
              {
                counts.addAlCount(i_befreal,ireal,l,m,ewordclasses.getClass(es[1+i_befreal]),
                                  frenchClass ,jj+1,*ep * mult,0.0);
                np0c+=*ep * mult;
              }
              MASSERT( &epsilon[jj](i,i_bef)== ep);
            }
          }
        }
      }
      double *gp1=conv<double>(gamma.begin()),*gp2=conv<double>(gamma.end())-I;
      Array<double>&ai=counts.doGetAlphaInit(I);
      Array<double>&bi=counts.doGetBetaInit(I);
      int firstFrenchClass=(fs.size()>1)?(fwordclasses.getClass(fs[1+0])):0;
      for (i=0;i<I;i++,gp1++,gp2++)
      {
        CLASSIFY(i,i_empty,ireal);
        ai[i]+= *gp1;
        bi[i]+= *gp2;
        if (DependencyOfPrevAJ==0)
        {
          if (i_empty)
            p0c+=*gp1;
          else
          {
            counts.addAlCount(-1,ireal,l,m,0,firstFrenchClass,0,*gp1,0.0);
            np0c+=*gp1;
          }
        }
      }
      if (g_is_verbose)
        cout << "l: " << l << "m: " << m << " p0c: " << p0c << " np0c: " << np0c << endl;
    }
    cross_entropy+=log(max(trainProb,1e-100))+log(max(net->finalMultiply,1e-100));
    Array<int>vit;
    double viterbi_score=1.0;
    if ((g_hmm_training_special_flags&1))
      HMMViterbi(*net,gamma,vit);
    else
      viterbi_score=HMMRealViterbi(*net,vit);
    for (j=1;j<=m;j++)
    {
      viterbi_alignment[j]=vit[j-1]+1;
      if (viterbi_alignment[j]>l)
        viterbi_alignment[j]=0;
    }
    sHandler1.setProbOfSentence(sent,cross_entropy);
    perp.addFactor(cross_entropy, so, l, m,1);
    viterbi_perp.addFactor(log(viterbi_score)+log(max(net->finalMultiply,1e-100)), so, l, m,1);

    if (g_is_verbose) {
      cout << "Viterbi-perp: " << log(viterbi_score) << ' '
           << log(max(net->finalMultiply,1e-100)) << ' '
           << viterbi_score << ' ' << net->finalMultiply
           << ' ' << *net << "gamma: " << gamma << endl;
    }

    // TODO: Use more safe resource management like RAII.
    delete net;
    net = 0;

    if (dump_alignment||(FEWDUMPS&&sent.getSentenceNo()<1000))
      printAlignToFile(es, fs, Elist.getVocabList(), Flist.getVocabList(), of2, viterbi_alignment, sent.getSentenceNo(), viterbi_score);
    addAL(viterbi_alignment,sent.getSentenceNo(),l);
    pair_no++;
  } /* of while */
  sHandler1.rewind();
  perp.record("HMM");
  viterbi_perp.record("HMM");
  errorReportAL(cout,"HMM");
}
Example #26
0
 gcc_pure
 const_iterator end() const {
   return data.end();
 }
Example #27
0
 inline void Array::copy(const Array& from) {
     std::copy(from.begin(),from.end(),begin());
 }
Example #28
0
ElfLoader::DebuggerHelper::DebuggerHelper(): dbg(nullptr), firstAdded(nullptr)
{
    /* Find ELF auxiliary vectors.
     *
     * The kernel stores the following data on the stack when starting a
     * program:
     *   argc
     *   argv[0] (pointer into argv strings defined below)
     *   argv[1] (likewise)
     *   ...
     *   argv[argc - 1] (likewise)
     *   nullptr
     *   envp[0] (pointer into environment strings defined below)
     *   envp[1] (likewise)
     *   ...
     *   envp[n] (likewise)
     *   nullptr
     *   ... (more NULLs on some platforms such as Android 4.3)
     *   auxv[0] (first ELF auxiliary vector)
     *   auxv[1] (second ELF auxiliary vector)
     *   ...
     *   auxv[p] (last ELF auxiliary vector)
     *   (AT_NULL, nullptr)
     *   padding
     *   argv strings, separated with '\0'
     *   environment strings, separated with '\0'
     *   nullptr
     *
     * What we are after are the auxv values defined by the following struct.
     */
    struct AuxVector {
        Elf::Addr type;
        Elf::Addr value;
    };

    /* Pointer to the environment variables list */
    extern char **environ;

    /* The environment may have changed since the program started, in which
     * case the environ variables list isn't the list the kernel put on stack
     * anymore. But in this new list, variables that didn't change still point
     * to the strings the kernel put on stack. It is quite unlikely that two
     * modified environment variables point to two consecutive strings in memory,
     * so we assume that if two consecutive environment variables point to two
     * consecutive strings, we found strings the kernel put on stack. */
    char **env;
    for (env = environ; *env; env++)
        if (*env + strlen(*env) + 1 == env[1])
            break;
    if (!*env)
        return;

    /* Next, we scan the stack backwards to find a pointer to one of those
     * strings we found above, which will give us the location of the original
     * envp list. As we are looking for pointers, we need to look at 32-bits or
     * 64-bits aligned values, depening on the architecture. */
    char **scan = reinterpret_cast<char **>(
                      reinterpret_cast<uintptr_t>(*env) & ~(sizeof(void *) - 1));
    while (*env != *scan)
        scan--;

    /* Finally, scan forward to find the last environment variable pointer and
     * thus the first auxiliary vector. */
    while (*scan++);

    /* Some platforms have more NULLs here, so skip them if we encounter them */
    while (!*scan)
        scan++;

    AuxVector *auxv = reinterpret_cast<AuxVector *>(scan);

    /* The two values of interest in the auxiliary vectors are AT_PHDR and
     * AT_PHNUM, which gives us the the location and size of the ELF program
     * headers. */
    Array<Elf::Phdr> phdrs;
    char *base = nullptr;
    while (auxv->type) {
        if (auxv->type == AT_PHDR) {
            phdrs.Init(reinterpret_cast<Elf::Phdr*>(auxv->value));
            /* Assume the base address is the first byte of the same page */
            base = reinterpret_cast<char *>(PageAlignedPtr(auxv->value));
        }
        if (auxv->type == AT_PHNUM)
            phdrs.Init(auxv->value);
        auxv++;
    }

    if (!phdrs) {
        DEBUG_LOG("Couldn't find program headers");
        return;
    }

    /* In some cases, the address for the program headers we get from the
     * auxiliary vectors is not mapped, because of the PT_LOAD segments
     * definitions in the program executable. Trying to map anonymous memory
     * with a hint giving the base address will return a different address
     * if something is mapped there, and the base address otherwise. */
    MappedPtr mem(MemoryRange::mmap(base, PageSize(), PROT_NONE,
                                    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
    if (mem == base) {
        /* If program headers aren't mapped, try to map them */
        int fd = open("/proc/self/exe", O_RDONLY);
        if (fd == -1) {
            DEBUG_LOG("Failed to open /proc/self/exe");
            return;
        }
        mem.Assign(MemoryRange::mmap(base, PageSize(), PROT_READ, MAP_PRIVATE,
                                     fd, 0));
        /* If we don't manage to map at the right address, just give up. */
        if (mem != base) {
            DEBUG_LOG("Couldn't read program headers");
            return;
        }
    }
    /* Sanity check: the first bytes at the base address should be an ELF
     * header. */
    if (!Elf::Ehdr::validate(base)) {
        DEBUG_LOG("Couldn't find program base");
        return;
    }

    /* Search for the program PT_DYNAMIC segment */
    Array<Elf::Dyn> dyns;
    for (Array<Elf::Phdr>::iterator phdr = phdrs.begin(); phdr < phdrs.end();
            ++phdr) {
        /* While the program headers are expected within the first mapped page of
         * the program executable, the executable PT_LOADs may actually make them
         * loaded at an address that is not the wanted base address of the
         * library. We thus need to adjust the base address, compensating for the
         * virtual address of the PT_LOAD segment corresponding to offset 0. */
        if (phdr->p_type == PT_LOAD && phdr->p_offset == 0)
            base -= phdr->p_vaddr;
        if (phdr->p_type == PT_DYNAMIC)
            dyns.Init(base + phdr->p_vaddr, phdr->p_filesz);
    }
    if (!dyns) {
        DEBUG_LOG("Failed to find PT_DYNAMIC section in program");
        return;
    }

    /* Search for the DT_DEBUG information */
    for (Array<Elf::Dyn>::iterator dyn = dyns.begin(); dyn < dyns.end(); ++dyn) {
        if (dyn->d_tag == DT_DEBUG) {
            dbg = reinterpret_cast<r_debug *>(dyn->d_un.d_ptr);
            break;
        }
    }
    DEBUG_LOG("DT_DEBUG points at %p", static_cast<void *>(dbg));
}
Example #29
0
 inline Disposable<Array> operator-(const Array& v) {
     Array result(v.size());
     std::transform(v.begin(),v.end(),result.begin(),
                    std::negate<double>());
     return result;
 }
Example #30
0
    void ModelContainerView::onGraphics(RenderDevice* rd, Array<PosedModelRef> &posed3D, Array<PosedModel2DRef> &posed2D) {
        Array<PosedModel::Ref>        opaque, transparent;
        LightingRef   localLighting = toneMap->prepareLighting(iLighting);
        SkyParameters localSky      = toneMap->prepareSkyParameters(iSkyParameters);


        toneMap->beginFrame(rd);
        rd->setProjectionAndCameraMatrix(defaultCamera);

        rd->setColorClearValue(Color3::black());
        rd->clear();
        //iSky->render(rd, localSky);

        // Setup lighting
        rd->enableLighting();
        //rd->setLight(0, localLighting->lightArray[0]);
        //rd->setAmbientLightColor(localLighting->ambientAverage());

        GLight light =GLight::directional(defaultController.pointer()->position() + defaultController.pointer()->lookVector()*2,Color3::white());
        rd->setLight(0,light);

        rd->setColor(Color3::blue());

        Array<std::string > keys = iTriVarTable.getKeys();
        Array<std::string>::ConstIterator i = keys.begin();
        while(i != keys.end()) {
            VAR* var = iTriVarTable.get(*i);
            Array<int> indexArray = iTriIndexTable.get(*i);

            rd->beginIndexedPrimitives();
            rd->setVertexArray(*var);
            rd->sendIndices(RenderDevice::LINES, indexArray);
            rd->endIndexedPrimitives();
            ++i;
        }
        for(int i=0; i<gBoxArray.size(); ++i) {
            AABox b = gBoxArray[i];
            Draw::box(b,rd,Color4(Color3::red(),0.9f));
        }
        //-------
        //triangles
        {
            if(iTriDebugArray.size() > 0)
            {
                rd->setColor(Color3::red());
                rd->beginIndexedPrimitives();
                rd->setVertexArray(iTriDebugVar);
                rd->sendIndices(RenderDevice::LINES, iTriDebugArray);
                rd->endIndexedPrimitives();
            }
        }
        //--------
        if(iDrawLine) {
            Draw::lineSegment(LineSegment::fromTwoPoints(iPos1, iPos2), rd, iColor, 3);

            if(myfound) {
                //Draw::lineSegment(LineSegment::fromTwoPoints(p1, p2), rd, iColor, 3);
                //Draw::lineSegment(LineSegment::fromTwoPoints(p2, p3), rd, iColor, 3);
                //Draw::lineSegment(LineSegment::fromTwoPoints(p3, p1), rd, iColor, 3);
                Draw::sphere(Sphere(p4,0.5),rd, iColor);
                //Draw::sphere(Sphere(p5,0.5),rd, Color3::green());
            }
        }


        // Always render the posed models passed in or the Developer Window and
        // other Widget features will not appear.
        if (posed3D.size() > 0) {
            Vector3 lookVector = renderDevice->getCameraToWorldMatrix().lookVector();
            PosedModel::sort(posed3D, lookVector, opaque, transparent);

            for (int i = 0; i < opaque.size(); ++i) {
                opaque[i]->render(renderDevice);
            }

            for (int i = 0; i < transparent.size(); ++i) {
                transparent[i]->render(renderDevice);
            }
        }

        rd->disableLighting();

        toneMap->endFrame(rd);
        PosedModel2D::sortAndRender(rd, posed2D);
    }