简体   繁体   English

在不同的浮点精度之间切换时,使用我自己的std :: mersenne_twister_engine模板参数

[英]Using my own template parameters for std::mersenne_twister_engine when switching between different floating point precisions

std::mt19937 is a typedef of std::mersenne_twister_engine . std::mt19937std::mersenne_twister_engine Should I be using my own template parameters for the latter if I'm switching between different floating point precisions in my sampling? 如果我在采样中在不同浮点精度之间切换,是否应该为后者使用自己的模板参数? If so, how? 如果是这样,怎么办?

Right now I have something like this 现在我有这样的事情

#include <random>
#include <iostream>

int main()
{
    using float_type = double;

    std::random_device rd;  
    std::mt19937 gen(rd()); 
    std::uniform_real_distribution<float_type> dis(1.0, 2.0);
    for (int n = 0; n < 1e6; ++n) {
        std::cout << dis(gen) << ' ';
    }
    std::cout << '\n';
}

but when I switch using float_type = double; 但是当我切换using float_type = double; to using float_type = float; using float_type = float; there isn't much of a speedup. 没有太多的提速。 Actually, in some other code I have, using float is actually much slower! 实际上,在我拥有的其他代码中,使用float实际上要慢得多!

Here's a makefile if it helps. 这是一个makefile,如果有帮助的话。 I used time ./prog after I compile with make as a rough timer, and I am running Ubuntu 18.04.2 LTS and my processor is a Intel® Xeon(R) CPU E3-1241 v3 @ 3.50GHz × 8 . 在使用make编译后,我使用了time ./prog作为粗略的计时器,并且我正在运行Ubuntu 18.04.2 LTS,并且我的处理器是Intel®Xeon(R)CPU E3-1241 v3 @ 3.50GHz×8。

PROG = prog
SRCS = main.cpp
OBJS = main.o
CXX = g++
CXXFLAGS = -std=c++11 -O3 $(INCLUDES) -pg


all: $(PROG)

$(PROG): $(OBJS)
        $(CXX) -o $@ $(OBJS) 

main.cpp :
        $(CXX) $(CXXFLAGS) -c 

main.o : main.cpp
        $(CXX) $(CXXFLAGS) -c main.cpp

.PHONY: clean
clean:
        rm -f $(PROG) $(OBJS) 

Should I be using my own template parameters for the latter if I'm switching between different floating point precisions in my sampling? 如果我在采样中在不同浮点精度之间切换,是否应该为后者使用自己的模板参数? If so, how? 如果是这样,怎么办?

Sure, you could use 64bit engine for sampling doubles (mantissa is 53bit long) and 32bit engine for sampling floats. 当然,您可以使用64位引擎对双精度采样(尾数为53位长),并使用32位引擎对浮点采样。

#define USE_DOUBLES

...

#ifdef USE_DOUBLES
    using float_type = double;
    using rng        = std::mt19937_64;
#else
    using float_type = float;
    using rng        = std::mt19937;
#endif

mt19937_64 is alias for MT with generated 64bits of randomness per call mt19937_64是MT的别名,每个呼叫生成64位随机性

不匹配调用 '(const Y) (int&amp;, std::mersenne_twister_engine <long unsigned int,< div><div id="text_translate"><p> 有我的.hpp:</p><pre> #include &lt;random&gt; #include &lt;ctime&gt; #include &lt;cmath&gt; #ifndef RECUIT_HPP #define RECUIT_HPP template &lt; class E, class Func, class TempSeq, class RandomY, class RNG&gt; E recuit_simule(const Func &amp; phi, E x0, const TempSeq &amp; T, const RandomY &amp; Y, RNG &amp; G, long unsigned N) { std::uniform_real_distribution&lt;double&gt; U(0,1); E y; double u; for(int i=0; i&lt;N; i++) { y=Y(x0, G); u=U(G); if(u &lt;= fmin(1, exp((phi(x0) - phi(y))/T(N)))) { x0=y; } } return x0; } #endif</pre><p> 和我的.cpp:</p><pre> #include "recuit.hpp" #include &lt;iostream&gt; class Y { private: std::normal_distribution&lt;double&gt; N; public: Y(): N(0,1) {} double operator()(const double &amp; x, std::mt19937 &amp; G) { return x + N(G); } }; int main() { auto phi=[](const double &amp; x) { return x*x*x*x*x*x - 48*x*x; }; auto T=[] (long unsigned n) { return 10 * pow(0.9, n); }; YA; std::mt19937 G; double x = recuit_simule(phi, 0, T, A, G, 1000); std::cout &lt;&lt; x &lt;&lt; std::endl; return 0; }</pre><p> 当我编译 my.cpp 时,my.hpp 中有以下错误:</p><blockquote><p> recuit.hpp:17:6: 错误: 不匹配调用 '(const Y) (int&amp;, std::mersenne_twister_engine&amp;)'</p></blockquote><p> 对于该行:</p><pre> y=Y(x0, G);</pre><p> 我不明白为什么</p></div></long> - no match for call to ‘(const Y) (int&, std::mersenne_twister_engine<long unsigned int,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何为小于标准的 std::mersenne_twister_engine 选择参数? - How should I choose parameters for a smaller-than-standard std::mersenne_twister_engine? std::mersenne_twister_engine 和随机数生成 - std::mersenne_twister_engine and random number generation 如何使用 Boost 序列化 mersenne_twister_engine? - How can a mersenne_twister_engine be serialized using Boost? 如何保存std :: mersenne_twister_engine的状态以便以后恢复? - How do I save the state of std::mersenne_twister_engine to restore it later? 不匹配调用 '(const Y) (int&amp;, std::mersenne_twister_engine <long unsigned int,< div><div id="text_translate"><p> 有我的.hpp:</p><pre> #include &lt;random&gt; #include &lt;ctime&gt; #include &lt;cmath&gt; #ifndef RECUIT_HPP #define RECUIT_HPP template &lt; class E, class Func, class TempSeq, class RandomY, class RNG&gt; E recuit_simule(const Func &amp; phi, E x0, const TempSeq &amp; T, const RandomY &amp; Y, RNG &amp; G, long unsigned N) { std::uniform_real_distribution&lt;double&gt; U(0,1); E y; double u; for(int i=0; i&lt;N; i++) { y=Y(x0, G); u=U(G); if(u &lt;= fmin(1, exp((phi(x0) - phi(y))/T(N)))) { x0=y; } } return x0; } #endif</pre><p> 和我的.cpp:</p><pre> #include "recuit.hpp" #include &lt;iostream&gt; class Y { private: std::normal_distribution&lt;double&gt; N; public: Y(): N(0,1) {} double operator()(const double &amp; x, std::mt19937 &amp; G) { return x + N(G); } }; int main() { auto phi=[](const double &amp; x) { return x*x*x*x*x*x - 48*x*x; }; auto T=[] (long unsigned n) { return 10 * pow(0.9, n); }; YA; std::mt19937 G; double x = recuit_simule(phi, 0, T, A, G, 1000); std::cout &lt;&lt; x &lt;&lt; std::endl; return 0; }</pre><p> 当我编译 my.cpp 时,my.hpp 中有以下错误:</p><blockquote><p> recuit.hpp:17:6: 错误: 不匹配调用 '(const Y) (int&amp;, std::mersenne_twister_engine&amp;)'</p></blockquote><p> 对于该行:</p><pre> y=Y(x0, G);</pre><p> 我不明白为什么</p></div></long> - no match for call to ‘(const Y) (int&, std::mersenne_twister_engine<long unsigned int, 为什么 mersenne_twister_engine 保证某些结果? - Why does the mersenne_twister_engine guarantee certain results? C ++ 11 mersenne_twister_engine类的问题 - Issues with c++ 11 mersenne_twister_engine class 当使用float精度时,C ++ random为相同的Mersenne Twister种子产生不同的数字 - C++ random yields different numbers for same Mersenne Twister seed when using float precision 由于 Mersenne Twister 引擎上的负索引,libstdc++ std::random 上的未定义行为(根据 clang -fsanitize=integer) - Undefined behavior (according to clang -fsanitize=integer) on libstdc++ std::random due to negative index on Mersenne Twister engine boost c ++使用Mersenne Twister算法生成0到1之间的相同随机数吗? - boost c++ is generating the same random number between 0 and 1, using the Mersenne Twister algorithm?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM