【问题标题】:Random generation with TRNG使用 TRNG 随机生成
【发布时间】:2021-06-19 14:50:50
【问题描述】:

对于以下为蒙特卡罗模拟生成随机数的代码,我需要接收每次运行的确切总和,但这不会发生,尽管我已经修复了种子。如果有人能指出这段代码的问题,我将不胜感激

#include <cmath>
#include <random>
#include <iostream>
#include <chrono>
#include <cfloat>
#include <iomanip>
#include <cstdlib>
#include <omp.h>
#include <trng/yarn2.hpp>
#include <trng/mt19937_64.hpp>
#include <trng/uniform01_dist.hpp>

using namespace std;
using namespace chrono;
const double landa = 1; 
const double exact_solution = landa / (pow(landa, 2) + 1); 
double function(double x) {
    return cos(x) / landa;
}

int main() {
    int rank; 
    const int N = 1000000;
    double sum = 0.0;
    trng::yarn2 r[6];
    for (int i = 0; i <6; i++)
    { 
        r[i].seed(0); 
    }
    
    for (int i = 0; i < 6; i++)
    {
          r[i].split(6,i);
    }
    
     trng::uniform01_dist<double> u;
     auto start = high_resolution_clock::now();
     #pragma omp parallel  num_threads(6)  
     {
         rank=omp_get_thread_num();
         #pragma omp for reduction (+: sum)
         for (int i = 0; i<N; ++i) {
            //double x = distribution(g);
      
            double x= u(r[rank]); 
            x = (-1.0 / landa) * log(1.0 - x); 
            sum = sum+function(x);
         }
    }
     double app   = sum / static_cast<double> (N);
     auto end = high_resolution_clock::now();
        
    auto diff=duration_cast<milliseconds>(end-start);
    
    cout << "Approximation is: " <<setprecision(17) << app << "\t"<<"Time: "<< setprecision(17) << diff.count()<<" Error: "<<(app-exact_solution)<< endl; 

    return 0;
}

【问题讨论】:

    标签: c++ multithreading parallel-processing openmp trng


    【解决方案1】:

    TL;DR 问题有两个方面:

    1. 浮点加法不具有关联性;
    2. 您正在为每个线程生成不同的随机数。

    我需要收到每次运行的确切金额,但这不会 发生,虽然我已经固定了种子。如果 任何人都可以指出这段代码的问题

    首先,您在rank=omp_get_thread_num(); 上有一个race-condition,变量rank 在所有线程之间共享,为了解决您可以在并行区域内声明变量rank 的问题,因此,使其对每个线程都是私有的。

     #pragma omp parallel  num_threads(6)  
     {
         int rank=omp_get_thread_num();
         ...
      }
    

    在您的代码中,您不应该期望sum 的值对于不同数量的线程是相同的。为什么?

    1. 因为您要并行添加doubles

          double sum = 0.0;
          ...
          #pragma omp for reduction (+: sum)
          for (int i = 0; i<N; ++i) {
              //double x = distribution(g);
      
              double x= u(r[rank]); 
              x = (-1.0 / landa) * log(1.0 - x); 
              sum = sum+function(x);
          }
      

      从What Every Computer Scientist Should Know about Floating Point Arithmetic 可以阅读:

      另一个灰色区域涉及括号的解释。 由于舍入误差,代数的结合定律不一定适用于浮点数。例如, 表达式 (x+y)+z 的答案与 x+(y+z) 完全不同,当 x = 1e30, y = -1e30 和 z = 1(在前一种情况下为 1,在前一种情况下为 0 后者)。

      因此,您可以得出结论,浮点加法不是 关联性,以及为什么对于不同数量的线程,您可能具有不同的sum 值。

    2. 您正在生成不同的随机值每个线程:

        for (int i = 0; i < 6; i++)
        {
            r[i].split(6,i);
        }
      

      因此,对于不同数量的线程,变量sum 也会得到不同的结果。

    正如 jérôme-richard 在 cmets 中所指出的那样:

    请注意,像Kahan summation 这样更精确的算法可以 显着减少舍入问题,同时仍然相对 快。

    【讨论】:

    • @MAero2020 你对浮点数和双精度有错误的印象,双精度数是双浮点数。它们具有与 float 相同的问题,但不那么明显,因为它们可以以更高的精度表示数字。计算机不能像数学那样表示无限的精度,这就是问题的来源
    猜你喜欢
    • 1970-01-01
    • 2014-05-21
    • 2015-03-14
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 2014-09-21
    • 2021-12-29
    相关资源
    最近更新 更多