[英]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::mt19937
是std::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位随机性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.