void SobolGenerator::GetUniforms(MJArray& variates)
{//this is the function to use to Sobel vectors	void i4_sobol ( int dim_num, int *seed, float quasi[ ] )
	float* quasi=(float *)malloc(sizeof(float)*GetDimensionality());
	i4_sobol ( GetDimensionality(), (int*)SobolGenerator::seed,quasi );
	for (unsigned long j=0; j < (unsigned long)GetDimensionality(); j++)
        variates[j] = quasi[j];
		free(quasi);

}
void SobolGenerator::GetUniforms(MJArray& variates)
{// here is the Sobol sequence generated. Note that I have a static value to remember the seed.
	float *quasi= (float*)malloc( GetDimensionality());
   static int seed_1=(int)SobolGenerator::seed;
	
	i4_sobol ( GetDimensionality(),&seed_1, quasi );
	for (int j=0; j < GetDimensionality(); j++){
		variates[j] =(double) quasi[j];
	}
	free(quasi);
}
Beispiel #3
0
void RandomParkMiller::Skip(unsigned long numberOfPaths){
    MJArray tmp(GetDimensionality());
    for(unsigned long j=0;j<numberOfPaths;j++){
        GetUniforms(tmp);
    }

}
void TransformUnrolledArraysPass::CollectArrays(int dimensionality)
{
  ClearReferences() ;

  assert(innermost != NULL) ;

  list<ArrayReferenceExpression*>* allRefs = 
    collect_objects<ArrayReferenceExpression>(innermost) ;

  list<ArrayReferenceExpression*>::iterator refIter = allRefs->begin() ;
  while(refIter != allRefs->end())
  {
    if (GetDimensionality(*refIter) == dimensionality)
    {
      // Create a new Equivalent reference 
      EquivalentReferences* nextReference = new EquivalentReferences() ;
      nextReference->original = (*refIter) ;
      currentReferences.push_back(nextReference) ;
    }
    ++refIter ;
  }
  delete allRefs ;

  Uniquify() ;
}
AntiThetic::AntiThetic(const Wrapper<RandomBase>& finnerGenerator):
	RandomBase(*finnerGenerator), InnerGenerator(finnerGenerator)
{
	InnerGenerator->Reset();
	OddEven = true;
	NextVariates.resize(GetDimensionality());
}
// *innerGenerator: get underlying RandomBase pbject ("*" is overloading in Wrapper class)
// specify the inherited random number generator
AntiThetic::AntiThetic(const Wrapper<RandomBase>& innerGenerator):RandomBase(*innerGenerator),InnerGenerator(innerGenerator)
{
    // perform intialization
    InnerGenerator->Reset();
    OddEven = true;
    NextVariates.resize(GetDimensionality());
    
}
void AntiThetic::GetGaussians(MJArray& variates)
{
    if (OddEven){
        InnerGenerator->GetGaussians(variates);

        for (unsigned long i =0; i < GetDimensionality(); i++)
            NextVariates[i] = - variates[i];

        OddEven = false;
    }else{
        variates = NextVariates;
        OddEven = true;
    }
}
void AntiThetic::GetGaussians(vector<double>& variates, double m, double s)
{
	if (OddEven){
		InnerGenerator->GetGaussians(variates,m,s);

		for (unsigned long i = 0; i < GetDimensionality(); i++)
			NextVariates[i] = - variates[i];

		OddEven = false;
	}
	else{
		variates = _NextVariates;
		OddEven = true;
	}
}
void AntiThetic::GetUniforms(vector<double>& variates, double a, double b)
{
	if (OddEven){
		InnerGenerator->GetUniforms(variates,a,b);

		for (unsigned long i = 0; i < GetDimensionality(); i++)
			NextVariates[i] = b - variates[i];

		OddEven = false;
	}
	else{
		variates = _NextVariates;
		OddEven = true;
	}
}
void AntiThetic::GetUniforms(std::vector<double>& variates)
{
	if (OddEven) {
		InnerGenerator->GetUniforms(variates);

		for (unsigned long i = 0; i < GetDimensionality(); ++i) {
			NextVariates[i] = 1.0 - variates[i];
		}
		OddEven = false;
	}
	else {
		variates = NextVariates;
		OddEven = true;
	}
}
void AntiThetic::Skip(unsigned long numberOfPaths)
{
    if (numberOfPaths ==0)  return;

    if (OddEven){
        OddEven = false;
        numberOfPaths--;
    }

    InnerGenerator->Skip(numberOfPaths  / 2);

    if (numberOfPaths % 2){
        MJArray tmp(GetDimensionality());
        GetUniforms(tmp);
    }
}
void AntiThetic::GetUniforms(MJArray &variates)
{
    if (OddEven)// if true, use anti-thetic
    {
        InnerGenerator->GetUniforms(variates); // generate one path using undering random generator
        // make anti-thetic
        for (unsigned long i=0; i<GetDimensionality(); i++)
        {
            NextVariates[i] = 1.0 - variates[i];
        }
        OddEven = false;
    }
    else
    {
        variates = NextVariates;
        OddEven= true;
    }
}
void AntiThetic::Skip(unsigned long numberOfPaths)
{
	if (numberOfPaths == 0)
		return;

	if (!OddEven) {
		OddEven = true; // ensure the skip is considered from an odd position
		--numberOfPaths;
	}

	InnerGenerator->Skip(numberOfPaths / 2);

	if (numberOfPaths % 2)
	{
		std::vector<double> tmp(GetDimensionality());
		GetUniforms(tmp);
	}
}
int TransformUnrolledArraysPass::MaxDimension()
{
  assert(innermost != NULL) ;

  int maxSize = 0 ;
  list<ArrayReferenceExpression*>* allRefs = 
    collect_objects<ArrayReferenceExpression>(innermost) ;
  list<ArrayReferenceExpression*>::iterator refIter = allRefs->begin() ;
  refIter = allRefs->begin() ;
  while (refIter != allRefs->end())
  {
    int currentSize = GetDimensionality(*refIter) ;
    if (currentSize > maxSize)
    {
      maxSize = currentSize ;
    }
    ++refIter ;
  }
  delete allRefs ;
  return maxSize ;
}
void AntiThetic::Skip(unsigned long numberOfPaths)
{
    //not skip
    if (numberOfPaths==0) return;
    
    // if next step have to do anti-thetic, reverse the flag and reduce one path
    if (OddEven)
    {
        OddEven = false;
        numberOfPaths--;
    }
    
    
    InnerGenerator -> Skip(numberOfPaths/2);
    
    if (numberOfPaths %2)
    {
        MJArray tmp(GetDimensionality());
        GetUniforms(tmp);
    }
    
}
// All of the array references expressions in the passed in the struct are
//  equivalent, so we can determine types of the original and use that
//  to create a new expression with which to replace everything.
bool TransformUnrolledArraysPass::ReplaceNDReference(EquivalentReferences* a)
{
  assert(a != NULL) ;
  assert(a->original != NULL) ;

  // Check to see if the reference at this stage is a constant or not
  IntConstant* constantIndex = 
    dynamic_cast<IntConstant*>(a->original->get_index()) ;
  
  if (constantIndex == NULL)
  {
    // There was no replacement made
    return false ;
  }

  Expression* baseAddress = a->original->get_base_array_address() ;
  assert(baseAddress != NULL) ;
  assert(constantIndex != NULL) ;

  // Create a replacement expression for this value.  This will either
  //  be another array reference expression or a single variable.
  Expression* replacementExp = NULL ;
  //  QualifiedType* elementType = GetQualifiedTypeOfElement(a->original) ;
  VariableSymbol* originalSymbol = GetArrayVariable(a->original) ;
  assert(originalSymbol != NULL) ;
  LString replacementName = 
    GetReplacementName(originalSymbol->get_name(), 
		       constantIndex->get_value().c_int()) ;
  int dimensionality = GetDimensionality(a->original) ;
  
  QualifiedType* elementType = originalSymbol->get_type() ;
  while (dynamic_cast<ArrayType*>(elementType->get_base_type()) != NULL)
  {
    elementType = dynamic_cast<ArrayType*>(elementType->get_base_type())->get_element_type() ;
  }
  
  // There is a special case for one dimensional arrays as opposed to all
  //  other dimensional arrays.  It only should happen if we are truly
  //  replacing an array with a one dimensional array.
  if (dimensionality == 1 && 
      dynamic_cast<ArrayReferenceExpression*>(a->original->get_parent())==NULL)
  {

    VariableSymbol* replacementVar = 
      create_variable_symbol(theEnv,
			     GetQualifiedTypeOfElement(a->original),
			     TempName(replacementName)) ;
    procDef->get_symbol_table()->append_symbol_table_object(replacementVar) ;
    
    replacementExp = 
      create_load_variable_expression(theEnv,
				      elementType->get_base_type(),
				      replacementVar) ;
  }
  else
  {
    // Create a new array with one less dimension.  This requires a new
    //  array type.
    ArrayType* varType = 
      dynamic_cast<ArrayType*>(originalSymbol->get_type()->get_base_type()) ;
    assert(varType != NULL) ;
   
    ArrayType* replacementArrayType =
      create_array_type(theEnv,
	varType->get_element_type()->get_base_type()->get_bit_size(),
	0, // bit alignment
	OneLessDimension(originalSymbol->get_type(), dimensionality),
	dynamic_cast<Expression*>(varType->get_lower_bound()->deep_clone()),
	dynamic_cast<Expression*>(varType->get_upper_bound()->deep_clone()),
	TempName(varType->get_name())) ;

    procDef->get_symbol_table()->append_symbol_table_object(replacementArrayType) ;

    VariableSymbol* replacementArraySymbol = 
      create_variable_symbol(theEnv,
			     create_qualified_type(theEnv,
						   replacementArrayType,
						   TempName(LString("qualType"))),
			     TempName(replacementName)) ;

    procDef->get_symbol_table()->append_symbol_table_object(replacementArraySymbol) ;

    // Create a new symbol address expression for this variable symbol
    SymbolAddressExpression* replacementAddrExp =
      create_symbol_address_expression(theEnv,
				       replacementArrayType,
				       replacementArraySymbol) ;

    // Now, replace the symbol address expression in the base
    //  array address with this symbol.
    ReplaceSymbol(a->original, replacementAddrExp) ;
    
    // And replace this reference with the base array address.
    replacementExp = a->original->get_base_array_address() ;
    a->original->set_base_array_address(NULL) ;
    replacementExp->set_parent(NULL) ;
  }

  // Replace all of the equivalent expressions with the newly generated
  //  replacement expression.
  assert(replacementExp != NULL) ;
  a->original->get_parent()->replace(a->original, replacementExp) ;
   
  //  ReplaceChildExpression(a->original->get_parent(),
  //			 a->original,
  //			 replacementExp) ;

  list<ArrayReferenceExpression*>::iterator equivIter = 
    a->allEquivalent.begin() ;
  while (equivIter != a->allEquivalent.end()) 
  {
    (*equivIter)->get_parent()->replace((*equivIter),
					dynamic_cast<Expression*>(replacementExp->deep_clone())) ;
    //    ReplaceChildExpression((*equivIter)->get_parent(),
    //			   (*equivIter),
    //			   dynamic_cast<Expression*>(replacementExp->deep_clone())) ;
    ++equivIter ;
  }

  return true ;
}
//below are just some functions override the RandomBase class methods.
void SobolGenerator::Skip(unsigned long numberOfPaths){
MJArray tmp(GetDimensionality());
    for (unsigned long j=0; j < numberOfPaths; j++)
        SobolGenerator::GetUniforms(tmp);
}
Beispiel #18
0
void RandomParkMiller::Skip(unsigned long NumberOfPaths)
{
	std::vector<double> tmp(GetDimensionality());
	for(unsigned long j=0; j<NumberOfPaths; j++) 
		GetUniforms(tmp);
}
Beispiel #19
0
void RandomParkMiller::GetUniforms(std::vector<double>& variates)
{
	for(unsigned long j=0; j<GetDimensionality(); j++)
		variates[j] = InnerGenerator.GetOneRandomInteger() * Reciprocal;
}
Beispiel #20
0
void RandomParkMiller::GetUniforms(MJArray& variates){
    for (unsigned long j=0;j<GetDimensionality();j++){
        variates[j]=InnerGenerator.GetOneRandomInteger()*Reciprocal;
    }
}