Advertisement
Guest User

Untitled

a guest
Nov 11th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <chrono>
  2. #include <iostream>
  3. #include <random>
  4.  
  5. static void compute_neq(float* __restrict__ neq,
  6.                         const float* __restrict__ ux,
  7.                         const float* __restrict__ uy,
  8.                         const float* __restrict__ rho,
  9.                         const float* __restrict__ ex,
  10.                         const float* __restrict__ ey,
  11.                         const float* __restrict__ w,
  12.                         const size_t N)
  13. {
  14.     for (size_t idx = 0; idx < N * N; ++idx) {
  15.         float usqr = ux[idx] * ux[idx] + uy[idx] * uy[idx];
  16.         for (size_t q = 0; q < 9; ++q) {
  17.             float eu = 3.0f * (ex[q] * ux[idx] + ey[q] * uy[idx]);
  18.             float tmp = 1.0f + eu + 0.5f * eu * eu - 1.5f * usqr;
  19.             tmp *= w[q] * rho[idx];
  20.             neq[idx * 9 + q] = tmp;
  21.         }
  22.     }
  23. }
  24.  
  25. int main() {
  26.   std::random_device rd;
  27.   std::mt19937 mt(rd());
  28.   std::uniform_real_distribution<float> dist(0.0f, 1.0f);
  29.   const size_t N = 1000;
  30.   const size_t benchmarkCount = 1000;
  31.   const auto length = N * N;
  32.   const float ex[9] = {0., 1., 0., -1., 0., 1., -1., -1., 1.};
  33.   const float ey[9] = {0., 0., 1., 0., -1., 1., 1., -1., -1.};
  34.   const float w[9] = {4./9., 1./9., 1./9., 1./9., 1./9., 1./36., 1./36., 1./36., 1./36.};
  35.   auto neq = new float[length * 9];
  36.   auto ux = new float[length];
  37.   auto uy = new float[length];
  38.   auto rho = new float[length];
  39.   for (size_t i = 0; i < length; ++i) {
  40.       ux[i] = dist(mt);
  41.       uy[i] = dist(mt);
  42.       rho[i] = 1.0f;
  43.       for (size_t q = 0; q < 9; ++q) neq[9 * i + q] = 0.0f;
  44.   }
  45.   auto start = std::chrono::steady_clock::now();
  46.   for (size_t t = 0; t < benchmarkCount; ++t)
  47.       compute_neq(neq, ux, uy, rho, ex, ey, w, N);
  48.   auto end = std::chrono::steady_clock::now();
  49.   auto diff = end - start;
  50.   std::cout << std::chrono::duration<double, std::milli>(diff).count() / benchmarkCount << " ms" << std::endl;
  51.   std::cout << neq[0] << std::endl;
  52.   return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement