montecarlo_GPU

PURPOSE ^

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 close all; clear all
0002 
0003 N = 1e8;
0004 
0005 gpu = gpuDevice
0006 
0007 for pass = 1:2
0008   disp(' ')
0009   if (pass==1)
0010     disp('Single precision:')
0011     prec = 'single';
0012   else
0013     disp('Double precision:')
0014     prec = 'double';
0015   end
0016   disp(' ')
0017   disp('GPU')
0018 
0019   for M = [1e5 1e6 1e7]
0020     parallel.gpu.rng('default');
0021     tic
0022     e4 = 0;
0023     for m = 1:M:N
0024       n  = min(M,N-m+1);
0025       x  = gpuArray.randn(1,n, prec);
0026       e4 = e4 + sum(x.^4);
0027     end
0028     e4 = e4 / N;
0029     wait(gpu);    % ensure it has completed)
0030     fprintf(' ave = %f, elapsed time = %f \n',e4,toc)
0031   end
0032 
0033   disp(' ')
0034   disp('CPU')
0035 
0036   for M = [1e4 1e5 1e6 1e7]
0037     rng('default');
0038     tic
0039     e4 = 0;
0040     for m = 1:M:N
0041       n  = min(M,N-m+1);
0042       y  = randn(1,n, prec);
0043       e4 = e4 + sum(y.^4);
0044     end
0045     e4 = e4 / N;
0046     fprintf(' ave = %f, elapsed time = %f \n',e4,toc)
0047   end
0048 end
0049 
0050 disp(' ')
0051 whos

Community support and wiki are available on Redmine. Last update: 18-Apr-2019.