IAP GITLAB

Skip to content
Snippets Groups Projects
Commit 60faaf1f authored by Antonio Augusto Alves Junior's avatar Antonio Augusto Alves Junior
Browse files

extending the API to wrap references to engines

parent 88e7f28d
No related branches found
No related tags found
No related merge requests found
......@@ -3,11 +3,23 @@ project(examples)
#----------------------------
message(STATUS "Adding target to examples. Executable file name: histogram")
message(STATUS "Adding target to examples. Executable file name: histogram_stream")
add_executable( histogram histogram.cpp )
add_executable( histogram_stream histogram_stream.cpp )
target_link_libraries( histogram -L/usr/lib64 )
target_link_libraries( histogram_stream -L/usr/lib64 )
add_dependencies(examples histogram )
add_dependencies(examples histogram_stream )
#----------------------------
message(STATUS "Adding target to examples. Executable file name: histogram_stream_wrapper")
add_executable( histogram_stream_wrapper histogram_stream_wrapper.cpp )
target_link_libraries( histogram_stream_wrapper -L/usr/lib64 )
add_dependencies(examples histogram_stream_wrapper )
\ No newline at end of file
File moved
/*----------------------------------------------------------------------------
*
* Copyright (C) 2021 Antonio Augusto Alves Junior
*
* This file is part of RandomIterator.
*
* RandomIterator is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RandomIterator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RandomIterator. If not, see <http://www.gnu.org/licenses/>.
*
*---------------------------------------------------------------------------*/
/*
* example.cpp
*
* Created on: 11/05/2021
* Author: Antonio Augusto Alves Junior
*/
/**
* \example example.cpp
*
*/
#include <iostream>
#include <random>
#include <vector>
#include <string>
#include <random_iterator/Stream.hpp>
#include <random_iterator/StreamWrapper.hpp>
//command line
#define TCLAP_SETBASE_ZERO 1
#include <tclap/CmdLine.h>
//set a global seed
static const uint64_t default_seed= 0x548c9decbce65295 ;
int main(int argv, char** argc)
{
uint64_t seed = default_seed;
uint32_t stream = 0;
size_t nentries = 0;
try {
TCLAP::CmdLine cmd("Command line arguments for ", '=');
TCLAP::ValueArg<size_t> NEntriesArg("n", "nentries", "Number of entries.", false, 0, "size_t");
cmd.add(NEntriesArg);
TCLAP::ValueArg<uint64_t> SeedArg("s", "seed", "RNG seed.", false, default_seed, "uint64_t");
cmd.add(SeedArg);
TCLAP::ValueArg<uint32_t> StreamArg("S", "stream", "Stream number.", false, 0, "uint32_t") ;
cmd.add(StreamArg);
// Parse the argv array.
cmd.parse(argv, argc);
// Get the value parsed by each arg.
seed = SeedArg.getValue();
stream = StreamArg.getValue();
nentries = NEntriesArg.getValue();
}
catch (TCLAP::ArgException &e) {
std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;
}
random_iterator::philox RNG(seed, stream);
std::uniform_real_distribution<double> dist(0.0, 10.0);
auto rng_stream = random_iterator::wrap_stream( dist, RNG);
//histogram as a simple vector
std::vector<size_t> histogram(10);
//fill histogram
do {
++histogram[ size_t(rng_stream()) ];
} while( ( --nentries ) > 0 );
//print histogram nicely
size_t i =0;
size_t maxe = *std::max_element(histogram.begin(), histogram.end());
std::cout <<maxe << std::endl ;
std::cout << "--------"<< std::string(50 , '-') << std::endl ;
for(auto n: histogram){
double bin = 50.0*double(n)/maxe;
std::cout << "["<< i<<", "<< ++i << ") "<<
std::string( bin, '=') << ": "<< n <<std::endl;
}
std::cout << "--------"<< std::string(50, '-') << std::endl ;
}
......@@ -34,6 +34,7 @@
namespace random_iterator {
/*
/// philox engine
typedef detail::philox philox;
/// threefry engine
......@@ -51,7 +52,7 @@ typedef detail::squares4_64 squares4_64;
/// ars engine
typedef detail::ars ars;
#endif
*/
/**
* \class Stream
......@@ -302,4 +303,5 @@ make_stream( DistibutionType const& dist, EngineType eng, uint32_t stream ){
return Stream<DistibutionType, EngineType>( dist, eng.getSeed(), stream);
}
} // namespace random_iterator
/*----------------------------------------------------------------------------
*
* Copyright (C) 2021 Antonio Augusto Alves Junior
*
* This file is part of RandomIterator.
*
* RandomIterator is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RandomIterator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RandomIterator. If not, see <http://www.gnu.org/licenses/>.
*
*---------------------------------------------------------------------------*/
/*
* StreamWrapper.hpp
*
* Created on: 21/05/2021
* Author: Antonio Augusto Alves Junior
*/
#ifndef STREAMWRAPPER_HPP_
#define STREAMWRAPPER_HPP_
#pragma once
#include <stdint.h>
#include <random_iterator/detail/tbb/iterators.h>
#include <random_iterator/detail/Engine.hpp>
#include <random_iterator/detail/functors/EngineCaller.hpp>
namespace random_iterator {
/**
* \class Stream
*
* \brief representation for streams of pseudorandom numbers
*
* Objects of this class represent collections of up to \f$ {2}^{32} \f$ streams of random numbers for each seed. Each of such streams
* has a length of \f$ {2}^{64} \f$. If the chosen distribution generates data with 64bits, like ``double``, then
* each stream can handle up to 128 ExaByte of data. Stream objects are thread-safe and lazy-evaluated. Output is produced
* only when requested. It is user responsibility to retrieve and store, if needed, the generated output.
*
* \tparam DistibutionType STL compliant distribution type. For example: ```std::uniform_real_distribution```, ```std::exponential_distribution```.
*
* \tparam EngineType RNG type.
*
*/
template<typename DistibutionType, typename EngineType>
class StreamWrapper
{
public:
typedef EngineType engine_type; //! Engine type
typedef DistibutionType distribution_type; //! type of the STL compliant distribution
typedef typename distribution_type::result_type result_type;//! type of the result
StreamWrapper()=delete;
StreamWrapper(distribution_type distribution, engine_type& engine):
distribution_(distribution),
engine_(engine)
{}
StreamWrapper(StreamWrapper<DistibutionType, EngineType> const& other ):
distribution_(other.getDistribution()),
engine_(other.getEngine())
{}
inline StreamWrapper& operator=(StreamWrapper<DistibutionType, EngineType> const& other )
{
if(this ==&other) return *this;
distribution_ =other.getDistribution();
engine_ = other.getEngine();
return *this;
}
inline result_type operator()(void)
{
return distribution_(engine_);
}
inline distribution_type getDistribution() const {
return distribution_;
}
inline void setDistribution(distribution_type distribution) {
distribution_ = distribution;
}
inline engine_type& getEngine() {
return engine_;
}
private:
engine_type& engine_;
distribution_type distribution_;
};
/**
* Convenience function for making StreamWrapper objects using type deduction
* and avoiding open template parameter instantiation
*
* @param dist STL compliant distribution instance.
* @param eng Engine instance
* @return An StreamWrapper object
*/
template<typename DistibutionType, typename EngineType>
StreamWrapper<DistibutionType, EngineType>
wrap_stream( DistibutionType const& dist, EngineType& eng ){
return StreamWrapper<DistibutionType, EngineType>( dist, eng);
}
} // namespace random_iterator
#endif /* STREAMWRAPPER_HPP_ */
......@@ -230,6 +230,29 @@ typedef Engine<random_iterator_r123::ARS2x64> ars;
} // namespace detail
/// philox engine
using detail::philox;
/// threefry engine
using detail::threefry;
/// squares3_128 engine
using detail::squares3_128;
/// squares4_128 engine
using detail::squares4_128;
/// squares3_64 engine
using detail::squares3_64;
/// squares4_64 engine
using detail::squares4_64;
#if RANDOM_ITERATOR_R123_USE_AES_NI
/// ars engine
using detail::ars;
#endif
} // namespace random_iterator
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment