Advertisement
Guest User

Untitled

a guest
Nov 11th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. import std.datetime, std.stdio, std.random;
  2. import ldc.attributes : fastmath;
  3.  
  4. @fastmath void compute_neq(float[] neq,
  5.                            const float[] ux,
  6.                            const float[] uy,
  7.                            const float[] rho,
  8.                            const float[] ex,
  9.                            const float[] ey,
  10.                            const float[] w,
  11.                            const size_t N)
  12. {
  13.     for (size_t idx = 0; idx < N * N; ++idx) {
  14.         float usqr = ux[idx] * ux[idx] + uy[idx] * uy[idx];
  15.         for (size_t q = 0; q < 9; ++q) {
  16.             float eu = 3.0f * (ex[q] * ux[idx] + ey[q] * uy[idx]);
  17.             float tmp = 1.0f + eu + 0.5f * eu * eu - 1.5f * usqr;
  18.             tmp *= w[q] * rho[idx];
  19.             neq[idx * 9 + q] = tmp;
  20.         }
  21.     }
  22. }
  23.  
  24.  
  25. void main(string[] args)
  26. {
  27.     immutable size_t N = 1000;
  28.     immutable int length = N * N;
  29.     immutable int size = float.sizeof * length;
  30.     immutable size_t benchmarkCount = 1000;    
  31.     const float[9] ex = [0., 1., 0., -1., 0., 1., -1., -1., 1.];
  32.     const float[9] ey = [0., 0., 1., 0., -1., 1., 1., -1., -1.];
  33.     const float[9] w = [4./9., 1./9., 1./9., 1./9., 1./9., 1./36., 1./36., 1./36., 1./36.];
  34.     float[] neq = new float[N*N*9];
  35.     foreach(int i,e; neq) neq[i] = 0.0f;
  36.     float[] ux = new float[N*N];
  37.     float[] uy = new float[N*N];
  38.     float[] rho = new float[N*N];
  39.     Random gen;
  40.     foreach(int i,e; ux) ux[i] = uniform(0.0f, 1.0f, gen); 
  41.     foreach(int i,e; uy) uy[i] = uniform(0.0f, 1.0f, gen);
  42.     foreach(int i,e; rho) rho[i] = 1.0f;
  43.     StopWatch sw;
  44.     sw.start();
  45.     for (uint c=0; c<benchmarkCount; ++c)        
  46.         compute_neq(neq, ux, uy, rho, ex, ey, w, N);        
  47.     sw.stop();
  48.     float t = sw.peek().usecs;
  49.     writef("neq[0]=%f\tt=%f ms", neq[0], t / 1000.0f / benchmarkCount);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement