From 44fd76fa55667f2929890b4f1c0b588b94bd206a Mon Sep 17 00:00:00 2001
From: Frederik Hennig <fredy.hng@gmail.com>
Date: Fri, 25 Sep 2020 14:33:50 +0200
Subject: [PATCH] small fix: retrieve codegen target earlier

---
 .../codegen/03_AdvancedLBMCodegen.py          | 95 ++++++++++---------
 1 file changed, 50 insertions(+), 45 deletions(-)

diff --git a/apps/tutorials/codegen/03_AdvancedLBMCodegen.py b/apps/tutorials/codegen/03_AdvancedLBMCodegen.py
index f659051f6..400237e63 100644
--- a/apps/tutorials/codegen/03_AdvancedLBMCodegen.py
+++ b/apps/tutorials/codegen/03_AdvancedLBMCodegen.py
@@ -9,65 +9,70 @@ from pystencils_walberla import CodeGeneration, generate_sweep, generate_pack_in
 from lbmpy_walberla import generate_boundary
 
 
-#   ========================
-#      General Parameters
-#   ========================
+with CodeGeneration() as ctx:
 
-stencil = 'D2Q9'
-omega = sp.Symbol('omega')
-layout = 'fzyx'
+    #   ========================
+    #      Target Selection
+    #   ========================
 
-#   PDF Fields
-pdfs, pdfs_tmp = ps.fields('pdfs(9), pdfs_tmp(9): [2D]', layout=layout)
+    if ctx.cuda:
+        target = 'gpu'
+    else:
+        target = 'cpu'
 
-#   Velocity Output Field
-velocity = ps.fields("velocity(2): [2D]", layout=layout)
-output = {'velocity': velocity}
+    #   ========================
+    #      General Parameters
+    #   ========================
 
-#   Optimization
-optimization = {'target': 'cpu',
-                'cse_global': True,
-                'symbolic_field': pdfs,
-                'symbolic_temporary_field': pdfs_tmp,
-                'field_layout': layout}
+    stencil = 'D2Q9'
+    omega = sp.Symbol('omega')
+    layout = 'fzyx'
 
+    #   PDF Fields
+    pdfs, pdfs_tmp = ps.fields('pdfs(9), pdfs_tmp(9): [2D]', layout=layout)
 
-#   ==================
-#      Method Setup
-#   ==================
+    #   Velocity Output Field
+    velocity = ps.fields("velocity(2): [2D]", layout=layout)
+    output = {'velocity': velocity}
 
-lbm_params = {'stencil': stencil,
-              'method': 'mrt_raw',
-              'relaxation_rates': [0, 0, 0, omega, omega, omega, 1, 1, 1],
-              'cumulant': True,
-              'compressible': True}
+    #   Optimization
+    optimization = {'target': target,
+                    'cse_global': True,
+                    'symbolic_field': pdfs,
+                    'symbolic_temporary_field': pdfs_tmp,
+                    'field_layout': layout}
 
-lbm_update_rule = create_lb_update_rule(optimization=optimization,
-                                        output=output,
-                                        **lbm_params)
 
-lbm_method = lbm_update_rule.method
+    #   ==================
+    #      Method Setup
+    #   ==================
 
-#   ========================
-#      PDF Initialization
-#   ========================
+    lbm_params = {'stencil': stencil,
+                'method': 'mrt_raw',
+                'relaxation_rates': [0, 0, 0, omega, omega, omega, 1, 1, 1],
+                'cumulant': True,
+                'compressible': True}
 
-initial_rho = sp.Symbol('rho_0')
+    lbm_update_rule = create_lb_update_rule(optimization=optimization,
+                                            output=output,
+                                            **lbm_params)
 
-pdfs_setter = macroscopic_values_setter(lbm_method,
-                                        initial_rho,
-                                        velocity.center_vector,
-                                        pdfs.center_vector)
+    lbm_method = lbm_update_rule.method
 
-#   =====================
-#      Code Generation
-#   =====================
+    #   ========================
+    #      PDF Initialization
+    #   ========================
 
-with CodeGeneration() as ctx:
-    if ctx.cuda:
-        target = 'gpu'
-    else:
-        target = 'cpu'
+    initial_rho = sp.Symbol('rho_0')
+
+    pdfs_setter = macroscopic_values_setter(lbm_method,
+                                            initial_rho,
+                                            velocity.center_vector,
+                                            pdfs.center_vector)
+
+    #   =====================
+    #      Code Generation
+    #   =====================
 
     #   LBM Sweep
     generate_sweep(ctx, "CumulantMRTSweep", lbm_update_rule, field_swaps=[(pdfs, pdfs_tmp)], target=target)
-- 
GitLab