Ejemplo n.º 1
0
void
RandomNumbers::init( SLIInterpreter* i )
{
  RngType.settypename( "rngtype" );
  RngType.setdefaultaction( SLIInterpreter::datatypefunction );

  RngFactoryType.settypename( "rngfactorytype" );
  RngFactoryType.setdefaultaction( SLIInterpreter::datatypefunction );

  RdvType.settypename( "rdvtype" );
  RdvType.setdefaultaction( SLIInterpreter::datatypefunction );
  RdvFactoryType.settypename( "rdvfactorytype" );
  RdvFactoryType.setdefaultaction( SLIInterpreter::datatypefunction );

  if ( rngdict_ || rdvdict_ )
    throw DynamicModuleManagementError( "RandomNumbers module has been initialized previously." );

  // create random number generator type dictionary
  rngdict_ = new Dictionary();
  assert( rngdict_ );
  i->def( "rngdict", DictionaryDatum( rngdict_ ) );

  // add built-in rngs
  register_rng_< librandom::KnuthLFG >( "knuthlfg", *rngdict_ );
  register_rng_< librandom::MT19937 >( "MT19937", *rngdict_ );

  // let GslRandomGen add all of the GSL rngs
  librandom::GslRandomGen::add_gsl_rngs( *rngdict_ );

  // create random deviate generator dictionary
  rdvdict_ = new Dictionary();
  assert( rdvdict_ );
  i->def( "rdevdict", DictionaryDatum( rdvdict_ ) );

  register_rdv_< librandom::BinomialRandomDev >( "binomial", *rdvdict_ );
  register_rdv_< librandom::ClippedRedrawDiscreteRandomDev< librandom::BinomialRandomDev > >(
    "binomial_clipped", *rdvdict_ );
  register_rdv_< librandom::ClippedToBoundaryDiscreteRandomDev< librandom::BinomialRandomDev > >(
    "binomial_clipped_to_boundary", *rdvdict_ );
  register_rdv_< librandom::PoissonRandomDev >( "poisson", *rdvdict_ );
  register_rdv_< librandom::ClippedRedrawDiscreteRandomDev< librandom::PoissonRandomDev > >(
    "poisson_clipped", *rdvdict_ );
  register_rdv_< librandom::ClippedToBoundaryDiscreteRandomDev< librandom::PoissonRandomDev > >(
    "poisson_clipped_to_boundary", *rdvdict_ );
  register_rdv_< librandom::UniformRandomDev >( "uniform", *rdvdict_ );
  register_rdv_< librandom::UniformIntRandomDev >( "uniform_int", *rdvdict_ );

  register_rdv_< librandom::NormalRandomDev >( "normal", *rdvdict_ );
  register_rdv_< librandom::ClippedRedrawContinuousRandomDev< librandom::NormalRandomDev > >(
    "normal_clipped", *rdvdict_ );
  register_rdv_< librandom::ClippedToBoundaryContinuousRandomDev< librandom::NormalRandomDev > >(
    "normal_clipped_to_boundary", *rdvdict_ );
  register_rdv_< librandom::LognormalRandomDev >( "lognormal", *rdvdict_ );
  register_rdv_< librandom::ClippedRedrawContinuousRandomDev< librandom::LognormalRandomDev > >(
    "lognormal_clipped", *rdvdict_ );
  register_rdv_< librandom::ClippedToBoundaryContinuousRandomDev< librandom::LognormalRandomDev > >(
    "lognormal_clipped_to_boundary", *rdvdict_ );

  register_rdv_< librandom::ExpRandomDev >( "exponential", *rdvdict_ );
  register_rdv_< librandom::ClippedRedrawContinuousRandomDev< librandom::ExpRandomDev > >(
    "exponential_clipped", *rdvdict_ );
  register_rdv_< librandom::ClippedToBoundaryContinuousRandomDev< librandom::ExpRandomDev > >(
    "exponential_clipped_to_boundary", *rdvdict_ );
  register_rdv_< librandom::GammaRandomDev >( "gamma", *rdvdict_ );
  register_rdv_< librandom::ClippedRedrawContinuousRandomDev< librandom::GammaRandomDev > >(
    "gamma_clipped", *rdvdict_ );
  register_rdv_< librandom::ClippedToBoundaryContinuousRandomDev< librandom::GammaRandomDev > >(
    "gamma_clipped_to_boundary", *rdvdict_ );

#ifdef HAVE_GSL
  register_rdv_< librandom::GSL_BinomialRandomDev >( "gsl_binomial", *rdvdict_ );
#endif

  // create function
  i->createcommand( "CreateRNG_gt_i", &createrngfunction );
  i->createcommand( "CreateRDV_g_vf", &createrdvfunction );

  i->createcommand( "SetStatus_v", &setstatus_vdfunction );
  i->createcommand( "GetStatus_v", &getstatus_vfunction );

  // access functions
  i->createcommand( "seed_g_i", &seedfunction );
  i->createcommand( "irand_g_i", &irandfunction );
  i->createcommand( "drand_g", &drandfunction );

  i->createcommand( "RandomArray_v_i", &randomarrayfunction );
  i->createcommand( "Random_i", &randomfunction );
}
Ejemplo n.º 2
0
void
DynamicLoaderModule::LoadModuleFunction::execute( SLIInterpreter* i ) const
{
  i->assert_stack_load( 1 );

  sDynModule new_module;

  new_module.name = getValue< std::string >( i->OStack.top() );
  if ( new_module.name.empty() )
    throw DynamicModuleManagementError( "Module name must not be empty." );

  // check if module already loaded
  // this check can happen here, since we are comparing dynamically loaded modules
  // based on the name given to the Install command
  if ( std::find( dyn_modules_.begin(), dyn_modules_.end(), new_module ) != dyn_modules_.end() )
    throw DynamicModuleManagementError( "Module '" + new_module.name + "' is loaded already." );

  // call lt_dlerror() to reset any error messages hanging around
  lt_dlerror();

  // try to open the module
  const lt_dlhandle hModule = lt_dlopenext( new_module.name.c_str() );

  if ( !hModule )
  {
    char* errstr = ( char* ) lt_dlerror();
    std::string msg = "Module '" + new_module.name + "' could not be opened.";
    if ( errstr )
      msg += "\nThe dynamic loader returned the following error: '" + std::string( errstr ) + "'.";
    msg += "\n\nPlease check LD_LIBRARY_PATH (OSX: DYLD_LIBRARY_PATH)!";
    throw DynamicModuleManagementError( msg );
  }

  // see if we can find the mod symbol in the module
  SLIModule* pModule = ( SLIModule* ) lt_dlsym( hModule, "mod" );
  char* errstr = ( char* ) lt_dlerror();
  if ( errstr )
  {
    lt_dlclose( hModule ); // close module again
    lt_dlerror();          // remove any error caused by lt_dlclose()
    throw DynamicModuleManagementError(
            "Module '" + new_module.name + "' could not be loaded.\n"
            "The dynamic loader returned the following error: '" 
            + std::string(errstr) + "'.");
  }

  // check if module is linked in. This test is based on the module name
  // returned by DynModule::name(), since we have no file names for linked modules.
  // We can only perform it after we have loaded the module.
  if ( std::find_if( DynamicLoaderModule::getLinkedModules().begin(),
         DynamicLoaderModule::getLinkedModules().end(),
         std::bind2nd( std::ptr_fun( has_name ), pModule->name() ) )
    != DynamicLoaderModule::getLinkedModules().end() )
  {
    lt_dlclose( hModule ); // close module again
    lt_dlerror();          // remove any error caused by lt_dlclose()
    throw DynamicModuleManagementError(
            "Module '" + new_module.name + "' is linked into NEST.\n"
            "You neither need nor may load it dynamically in addition.");
  }

  // all is well an we can register the module with the interpreter
  try
  {
    pModule->install( std::cerr, i );
  }
  catch ( std::exception& e )
  {
    // We should uninstall the partially installed module here, but
    // this must wait for #152.
    // For now, we just close the module file and rethrow the exception.

    lt_dlclose( hModule );
    lt_dlerror(); // remove any error caused by lt_dlclose()
    throw;        // no arg re-throws entire exception, see Stroustrup 14.3.1
  }

  // add the handle to list of loaded modules
  new_module.handle = hModule;
  new_module.pModule = pModule;
  dyn_modules_.push_back( new_module );

  i->message( SLIInterpreter::M_INFO, "Install", ( "loaded module " + pModule->name() ).c_str() );

  // remove operand and operator from stack
  i->OStack.pop();
  i->EStack.pop();

  // put handle to module onto stack
  int moduleid = dyn_modules_.size() - 1;
  i->OStack.push( moduleid );
  ( *moduledict_ )[ new_module.name ] = moduleid;

  // now we can run the module initializer, after we have cleared the EStack
  if ( !pModule->commandstring().empty() )
  {
    Token t = new StringDatum( pModule->commandstring() );
    i->OStack.push_move( t );
    Token c = new NameDatum( "initialize_module" );
    i->EStack.push_move( c );
  }
}