sandbox/acastillo/grid/gpu/grid.h

    Grids on GPUs

    The files in this directory implement Cartesian and Multigrid grids on Graphics Processing Units (GPUs). The ultimate goal is to allow running any Basilisk solver on GPUs without any modification to the original source code.

    To do so the Basilisk preprocessor automatically generates “computation kernels” for each loop iterator. These kernels are then dynamically compiled (at runtime) by the OpenGL Shading Language (GLSL) compiler which is part of the (OpenGL) graphics card driver, or by the equivalent runtime compilers provided by the CUDA, HIP, OpenCL or Metal libraries. If compilation is successful, the corresponding loop is performed on the GPU, otherwise the CPU is used. If this hybrid GPU/CPU hybrid mode of operation is used, synchronisation between the GPU and CPU memory is necessary and is done automatically.

    OpenGL is an open standard (unlike e.g. CUDA) and is widely supported by graphics cards (with the notable exception of Apple graphics cards and some high-end “professional” Nvidia cards).

    Note that this is not the only option to run on GPUs and/or other accelerators: there are also Basilisk backends which support CUDA, HIP, OpenCL and, on macOS, Metal.

    Running on GPUs

    As described above, from a Basilisk perspective GPUs are just another type of grid. Selecting a “GPU grid” can simply be done using either

    #include "grid/gpu/multigrid.h"

    in the source code, or using the -grid command line option of qcc like this

    qcc -autolink -Wall -O2 -grid=gpu/multigrid code.c -o code -lm

    The standard Basilisk Makefile also includes the handy recipe

    make code.gpu.tst

    which will compile and run code.c using the gpu/multigrid grid.

    Note that for all this to work properly you first need to install the Basilisk GPU libraries.

    Similar simple compilation targets are provided for the other drivers (i.e. .cuda.tst, .hip.tst, .ocl.tst, .metal.tst).

    Installation

    Basilisk uses the GLFW library to configure and access the graphics card and OpenGL (version >= 4.3) for the rest. These libraries and the associated Basilisk libraries can be easily installed on Debian-like systems using

    sudo apt install libglfw3-dev
    cd $BASILISK/grid/gpu
    make libgpu.a

    Note that you will also need the appropriate graphics card drivers (often proprietary for Nvidia). Note also that (reasonably high-end) laptop computers often have two graphics cards: a low-power, slow one and a high-power, fast one. To check which one you are currently using you can use something like

    sudo apt install mesa-utils
    glxinfo -B

    On my Dell XPS laptop I can switch to the (proprietary driver of the) fast Nvidia graphics card using

    __NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia glxinfo -B

    See here for the installation of other drivers (CUDA, HIP etc.).

    Tests

    There are several test cases for GPUs you can try. For example

    cd $BASILISK/test
    CFLAGS=-DPRINTNSHADERS make gpu.gpu.tst

    If this worked, you can then try a more interesting example

    CFLAGS='-DSHOW' make bump2D-gpu.tst
    __NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia ./bump2D-gpu/bump2D-gpu 10

    and also

    cd $BASILISK/examples
    CFLAGS='-DSHOW -DBENCHMARK' make turbulence.gpu.tst
    __NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia ./turbulence.gpu/turbulence.gpu 1024

    Writing code compatible with GPUs

    GPUs are fast compared to CPUs because they use specialised hardware which relies on highly-parallel (tens of thousands of execution threads) asynchronous accesses to fast video memory channels. This imposes strong constraints on programs which can run efficiently on these systems, in particular regarding memory allocation and accesses. These constraints are reflected in the programming languages usable on GPUs, for example the OpenGL Shading Language (GLSL) which underlies the GPU grid in Basilisk.

    GLSL is mostly a subset of C99 and the good news is that this subset happens to be what is used within most foreach loops in Basilisk (this is not a coincidence…). Thus, in many cases, simple and efficient Basilisk code will also run transparently and efficiently on GPUs.

    Note that GLSL is only used by the OpenGL driver. CUDA kernels use a general C++ implementation which consequently has less restrictions than GLSL, as does the AMD kernel compiler. OpenCL uses a variant of C99, which is also less restrictive than GLSL. Generally speaking, writing code compatible with GLSL, as described in what follows, will ensure portability for all these drivers.

    GPU/CPU hybrid mode

    There are obvious cases where foreach loops will not run on GPUs (see the next section). In theses cases, Basilisk will automatically switch to running the loop on the CPU and will synchronize the CPU and GPU memories. Note that this also means that the memory (i.e. scalar fields etc.) for a program is always allocated twice: once on the CPU and once on the GPU.

    As an example, consider the following simple code

    int main()
    {
      init_grid (16);
      scalar s[];
      double k = 2.;
      foreach()
        s[] = cos(k*x)*sin(k*y);
      foreach()
        printf ("%g %g %g\n", x, y, s[]);
    }

    this can be run on the CPU using e.g.

    make test.tst

    If we now run on the GPU using

    make test.gpu.tst

    we get

    test.gpu.c:9: GLSL: error: unknown function 'printf'
    test.gpu.c:8: warning: foreach() done on CPU (see GLSL errors above)

    Basilisk warns us that “printf” is not known in GLSL (at line 9) and that, as a consequence, the loop at line 8 (i.e. the second loop which includes “printf”) was run on the CPU. Note that the first message is a “GLSL: error” but that the code still ran OK on the CPU. Note also that this error happened at runtime and not during compilation. That’s because foreach loops are compiled dynamically at runtime by the graphics card driver.

    Since GPUs have a very limited access to the operating system (i.e. only through the OpenGL interface) we cannot expect the loop including “printf” (or any other output) to run on the GPU. Note also that the second loop should be “serial” rather than parallel (see Parallel Programming). So we need to modify the code to

    ...
      foreach (serial)
        printf ("%g %g %g\n", x, y, s[]);
    ...

    If we now recompile and run with make test.gpu.tst, the GLSL error and warnings are gone since we explicitely specified that the second loop should run on the CPU (and in serial mode).

    Another way to specify that a given loop should run on the CPU (either in serial or parallel mode) is to use

    foreach (cpu)
      ...

    Similarly one could use foreach (gpu) to force running on the GPU, in which case the GLSL warning above would become an error. This can be useful when debugging GPU codes and used in combination with the -cpu compilation flag which will force loops to run on the CPU by default.

    Variable-size arrays

    In C99 variable-size arrays can be defined simply using for example

    void func1 (int n, double a[n]) {
      ...
    }
    ...
    {
      int m = ...;
      double b[m];
      func (m, b);
    }

    Since this relies on dynamic memory allocation on the stack, this is not possible in general in GLSL. The only cases where this will work is if the size of the array can be computed “statically” i.e. at the time the GLSL kernel is compiled. Furthermore, the GLSL compiler is strict (or not very clever) and a code looking like

    int n = 3;
    double a[n];

    will fail with an error like

    GLSL: error: array size must be a constant valued expression

    To fix this one needs to write instead

    const int n = 3;
    double a[n];

    Note that the size of the array must be a constant, but only at the time when the GLSL kernel is compiled. This allows using variable-size arrays also in GLSL, provided their size is constant within the kernel. For example the following code will work fine on the GPU, even if n changes between calls to func2().

    void func2 (const int n) {
      foreach() {
        const int size = n + 1;
        double a[size];
        ...
      }
    }

    Finally, using variable-sized arrays as function parameters, as done in func1() above, is not allowed in GLSL. To work around this strong limitation, the kernel preprocessor will expand calls to functions using variable-size arrays (using the macro engine). Note that this means that the function must respect the constraints applying to macros, in particular they can return only at the end of the function.

    What cannot be done on GPUs

    • Inputs/Outputs: The only possible direct output on GPUs is the screen (see output_ppm on GPUs). All other inputs or outputs must go through the CPU.
    • Complex memory allocation and access: There is no notion of “memory stack” on GPUs, all memory requirements are static and must be defined at compilation time. This means that variable/dynamical arrays, dynamic memory allocation (malloc etc.) and pointers do not exist on GPUs (and in GLSL). Using any of these in a foreach loop will give you a GLSL error as above (note that these restrictions can be lifted on CUDA, HIP etc. drivers).
    • Limited support for function pointers: function pointers are fundamentally different from memory pointers. Basilisk includes limited support for function pointers i.e. what is sufficient for field attributes implementing custom boundary conditions or coarsening/refinement functions.
    • Using external libraries: GPUs cannot (obviously) use functions defined in external (CPU) libraries.

    Current limitations

    Aside from the fundamental constraints above, the current implementation also has the following limitations, some of which will be lifted in the (not-too-distant) future. In rough order of “lifting priority” these are:

    • Only 2D and 3D Cartesian and Multigrid grids for now: quadtrees and octrees are more difficult.
    • The maximum size of any scalar field is limited to what can be indexed using a 32-bits unsigned integer i.e. 232 floats or 16 GB.
    • Boundary conditions have only been implemented for 3x3 stencils.
    • At this stage only a few solvers have been tested. Other solvers may or may not work. In particular surface tension will not work yet because the estimation of curvature relies on code which is not portable to GPUs.
    • Only simple external types (int, bool, float, double, coord etc.) are currently supported: for example custom typedefs are not supported yet for external variables. Loop-local variables and functions can use (locally-defined) custom types.
    • Loop-local lists of scalars have very limited support (but are not used much anyway): external loops support is OK.
    • Double precision (64-bits floats) is supported by Basilisk (use CFLAGS='-DDOUBLE_PRECISION') but depends on the (often limited) support by graphics cards and their drivers. Note also that using single precision can have an important impact on the convergence and accuracy of multigrid solvers.

    Performance

    To convince yourself that GPUs are worth the trouble, see the GPU Benchmarks: speedups of one to two orders of magnitude compared to CPUs are achievable.

    To maximize performance, here are a few tips and observations:

    • Make sure that you are using the correct graphics card and driver (see glxinfo above).
    • GPUs are highly parallel so will only provide speedups for large enough simulations (e.g. larger than 1282), increasingly so as resolution is increased.
    • Frequent CPU/GPU memory synchronisation will kill performance. Be careful to control how often you output data for example, much more so than when running on CPUs. An exception is graphical outputs which are much cheaper on GPUs and can be done often with little overhead. Loops done on the CPU within e.g. timestep iterations will generally kill performance.
    • Use built-in profiling to check where time is spent. Use the -DTRACE=3 compilation flag to get profiling information at the level of foreach loops.
    • On Nvidia cards the CUDA driver generally gives better performance, especially for large problems.

    Profiling with Nsight Compute or Nsight Systems

    ncu --set default ./my_app
    
    nsys profile --stats=true <your_executable> [args]
    nsys-ui report*.nsys-rep

    Bugs

    • Trying to use more than one shader storage buffer (SSBO) with Intel drivers does not seem work. This limits the amount of available video memory to a single SSBO with a maximum size which is usually 2GB (or less).

    See also

    Implementation

    bool on_cpu = false;
    
    #define gpu_grid ((GridGPU *)grid)
    
    GPUContext_t GPUContext = {
      .current_shader = -1,
    };
    
    #if _METAL

    The Metal Shading Language is a dialect of C++14 and is thus close to CUDA, with two important differences: pointers cannot be stored in constant buffers (so the field data are passed as a separate kernel argument, like in OpenCL) and variables declared at program scope must reside in the constant address space (this is what the _CONST_ macro is for).

    # define TYPEDEF(NAME, BODY) "struct " NAME " " BODY ";\n"
    # define CAST(NAME, BODY) NAME "{" BODY "}\n"
    # define _GLOB_PARAMS0 "device real * _data, constant struct _Ctx_ * _ctx_"
    # define DEFINITIONS                       \
      "#include <metal_stdlib>\n"              \
      "using namespace metal;\n"               \
      "#pragma clang diagnostic ignored \"-Wunused-variable\"\n"           \
      "#pragma clang diagnostic ignored \"-Wunused-function\"\n"           \
      "#pragma clang diagnostic ignored \"-Wsign-compare\"\n"               \
      "#define _CONST_ constant\n"             \
      "#define tensor _bsk_tensor\n" /* 'tensor' clashes with metal::tensor */ \
      TYPEDEF("ivec2", "{ int x, y; }")        \
      TYPEDEF("ivec3", "{ int x, y, z; }")     \
      TYPEDEF("vec2", "{ float x, y; }")       \
      TYPEDEF("vec3", "{ float x, y, z; }")    \
      TYPEDEF("dvec2", "{ double x, y; }")     \
      TYPEDEF("dvec3", "{ double x, y, z; }")  \
      TYPEDEF("vec4", "{ float r, g, b, a; }") \
      "#define forin(type,s,list) for (int _i = 0; _i < int(sizeof(list)/sizeof(type)) - 1; _i++) { type s = list[_i];\n" \
      "#define forin2(a,b,c,d) for (int _i = 0; _i < int(sizeof(c)/sizeof(a)) - 1; _i++)" \
      "  { a = c[_i]; b = d[_i];\n"                                         \
      "#define forin3(a,b,e,c,d,f) for (int _i = 0; _i < int(sizeof(c)/sizeof(a)) - 1; _i++)" \
      "  { a = c[_i]; b = d[_i]; e = f[_i];\n"                              \
      "#define layout(x)\n"                                                 \
      "#define _GLOB0_ _data, _ctx_\n"                                      \
      "#define _GLOB_ _data, _ctx_,\n"                                      \
      "#define _GLOB_PARAMS0_ " _GLOB_PARAMS0 "\n"                          \
      "#define _GLOB_PARAMS_ " _GLOB_PARAMS0 ",\n"                          \
      "#define _GLOB_VAL_(x) (_ctx_->x)\n"                                  \
      "#define _LOC_VAL_(x) (_local_.x)\n"                                  \
      "#define __global\n"
    #elif _OPENCL
    # define TYPEDEF(NAME, BODY) "typedef struct " BODY " " NAME ";\n"
    # define CAST(NAME, BODY) "(" NAME "){" BODY "}\n"
    # define _GLOB_PARAMS0 "__global real * _data, __constant struct _Ctx_ *_ctx_"
    # define DEFINITIONS                       \
      "#define bool int\n"                     \
      TYPEDEF("ivec2", "{ int x, y; }")        \
      TYPEDEF("ivec3", "{ int x, y, z; }")     \
      TYPEDEF("vec2", "{ float x, y; }")       \
      TYPEDEF("vec3", "{ float x, y, z; }")    \
      TYPEDEF("dvec2", "{ double x, y; }")     \
      TYPEDEF("dvec3", "{ double x, y, z; }")  \
      "#define forin(type,s,list) for (int _i = 0; _i < sizeof(list)/sizeof(type) - 1; _i++) { type s = list[_i];\n" \
      "#define forin2(a,b,c,d) for (int _i = 0; _i < sizeof(c)/sizeof(a) - 1; _i++)" \
      "  { a = c[_i]; b = d[_i];\n"                                         \
      "#define forin3(a,b,e,c,d,f) for (int _i = 0; _i < sizeof(c)/sizeof(a) - 1; _i++)" \
      "  { a = c[_i]; b = d[_i]; e = f[_i];\n"                              \
      "#define layout(x)\n"                                                 \
      "#define _GLOB0_ _data, _ctx_\n"                                      \
      "#define _GLOB_ _data, _ctx_,\n"                                      \
      "#define _GLOB_PARAMS0_ " _GLOB_PARAMS0 "\n"                          \
      "#define _GLOB_PARAMS_ " _GLOB_PARAMS0 ",\n"                          \
      "#define _GLOB_VAL_(x) (_ctx_->x)\n"            \
      "#define _LOC_VAL_(x) (_local_.x)\n"                 \
      "#define _CONST_ const\n"
    #elif _CUDA
    # define TYPEDEF(NAME, BODY) "struct " NAME " " BODY ";\n"
    # define CAST(NAME, BODY) NAME "{" BODY "}\n"
    # define _GLOB_PARAMS0 "const struct _Ctx_ *_ctx_"
    # define DEFINITIONS                       \
      "typedef unsigned int uint;\n"           \
      TYPEDEF("ivec2", "{ int x, y; }")        \
      TYPEDEF("ivec3", "{ int x, y, z; }")     \
      TYPEDEF("vec2", "{ float x, y; }")       \
      TYPEDEF("vec3", "{ float x, y, z; }")    \
      TYPEDEF("dvec2", "{ double x, y; }")     \
      TYPEDEF("dvec3", "{ double x, y, z; }")  \
      "__host__ __device__ inline int clamp(int x, int a, int b) { return max(a, min(x, b)); }\n" \
      "#define forin(type,s,list) for (int _i = 0; _i < sizeof(list)/sizeof(type) - 1; _i++) { type s = list[_i];\n" \
      "#define forin2(a,b,c,d) for (int _i = 0; _i < sizeof(c)/sizeof(a) - 1; _i++)" \
      "  { a = c[_i]; b = d[_i];\n"                                         \
      "#define forin3(a,b,e,c,d,f) for (int _i = 0; _i < sizeof(c)/sizeof(a) - 1; _i++)" \
      "  { a = c[_i]; b = d[_i]; e = f[_i];\n"                              \
      "#define layout(x)\n"                                                 \
      "#define _GLOB0_ _ctx_\n"                                             \
      "#define _GLOB_ _ctx_,\n"                                             \
      "#define _GLOB_PARAMS0_ " _GLOB_PARAMS0 "\n"                          \
      "#define _GLOB_PARAMS_ " _GLOB_PARAMS0 ",\n"                          \
      "#define _GLOB_VAL_(x) (_ctx_->x)\n"                                  \
      "#define _LOC_VAL_(x) (_local_.x)\n"                                  \
      "#define __global\n"                                                  \
      "#define _CONST_ const\n"                                            \
      "#define fabs(x) abs(x)\n"
    #else // GLSL
    # define TYPEDEF(NAME, BODY) "struct " NAME " " BODY ";\n"
    # define CAST(NAME, BODY) NAME "(" BODY ")\n"
    # define DEFINITIONS                                                    \
      "#define forin(type,s,list) for (int _i = 0; _i < list.length() - 1; _i++) { type s = list[_i];\n" \
      "#define forin2(a,b,c,d) for (int _i = 0; _i < c.length() - 1; _i++)" \
      "  { a = c[_i]; b = d[_i];\n"                                         \
      "#define forin3(a,b,e,c,d,f) for (int _i = 0; _i < c.length() - 1; _i++)" \
      "  { a = c[_i]; b = d[_i]; e = f[_i];\n"                              \
      "#define _GLOB0_\n"                                                   \
      "#define _GLOB_\n"                                                    \
      "#define _GLOB_PARAMS0_\n"                                            \
      "#define _GLOB_PARAMS_\n"                                             \
      "#define _GLOB_VAL_(x) x\n"                                           \
      "#define _LOC_VAL_(x) (x)\n"                                          \
      "#define _CONST_ const\n"                                            \
      "#define fabs(x) abs(x)\n"
    #endif // GLSL
    
    #if SINGLE_PRECISION
    # define GPU_REAL_TYPE "float"
    # define GPU_COORD2_TYPE "vec2"
    # define GPU_COORD3_TYPE "vec3"
    # define GPU_MATH_DECLS
    #else // DOUBLE_PRECISION
    # define GPU_REAL_TYPE "double"
    # define GPU_COORD2_TYPE "dvec2"
    # define GPU_COORD3_TYPE "dvec3"
    # if !_CUDA
    #  define GPU_MATH_DECLS                                            \
      "#define cos(x) cos(float(x))\n"                                  \
      "#define sin(x) sin(float(x))\n"                                  \
      "#define exp(x) exp(float(x))\n"                                  \
      "#define pow(x,y) pow(float(x), float(y))\n"
    # else
    #  define GPU_MATH_DECLS
    # endif
    #endif
    
    #if dimension == 2
    # define GPU_IVEC_TYPE "ivec2"
    # define GPU_DIM_COORD_TYPE GPU_COORD2_TYPE
    # define GPU_VECTOR_BODY "{ scalar x, y; }"
    # define GPU_TENSOR_BODY "{ vector x, y; }"
    # define GPU_POINT_INDICES "int i, j, level;"
    # define GPU_NEIGHBOR_INDICES "point.i+_i,point.j+_j,point.level,point.n"
    # define GPU_ORIGIN_INIT "{0,0}"
    # define GPU_UNUSED_COORDS "_CONST_ real z = 0.;\n"
    #elif dimension == 3
    # define GPU_IVEC_TYPE "ivec3"
    # define GPU_DIM_COORD_TYPE GPU_COORD3_TYPE
    # define GPU_VECTOR_BODY "{ scalar x, y, z; }"
    # define GPU_TENSOR_BODY "{ vector x, y, z; }"
    # define GPU_POINT_INDICES "int i, j, k, level;"
    # define GPU_NEIGHBOR_INDICES "point.i+_i,point.j+_j,point.k+_k,point.level,point.n"
    # define GPU_ORIGIN_INIT "{0,0,0}"
    # define GPU_UNUSED_COORDS
    #endif
    
    #if MULTIGRID
    # define DIMDECL " ivec n;"
    #else
    # define DIMDECL " uint n;"
    #endif
    
    #if LAYERS
    # define LAYERSDECL " int l;"
    # define GPU_POINT_LAYER ",point.l"
    # define GPU_INDEX_DECL                                               \
      "#define _index(a,m) ((a).i + (point.l + _GLOB_VAL_(_layer) + (m) < _attr(a,block) ? " \
      "point.l + _GLOB_VAL_(_layer) + (m) : 0))\n"
    #else
    # define LAYERSDECL
    # define GPU_POINT_LAYER
    # define GPU_INDEX_DECL "#define _index(a,m) ((a).i)\n"
    #endif
    
    #define GPU_TYPE_DECLS                                                \
      "#define real " GPU_REAL_TYPE "\n"                                  \
      "#define coord " GPU_COORD3_TYPE "\n"                               \
      "#define _coord " GPU_DIM_COORD_TYPE "\n"                           \
      "#define ivec " GPU_IVEC_TYPE "\n"                                  \
      GPU_MATH_DECLS                                                      \
      TYPEDEF("scalar", "{ int i, index; }")                              \
      TYPEDEF("vector", GPU_VECTOR_BODY)                                  \
      TYPEDEF("tensor", GPU_TENSOR_BODY)                                  \
      TYPEDEF("Point", "{ " GPU_POINT_INDICES DIMDECL LAYERSDECL "}")
    
    #define GPU_NEIGHBOR_DECL                                             \
      "#define neighborp(_i,_j,_k) " CAST("Point", GPU_NEIGHBOR_INDICES GPU_POINT_LAYER)
    
    #if _CUDA
    # define GPU_UNIFORM_DECLS
    #else
    # define GPU_UNIFORM_DECLS                                             \
      "layout (location = 0) uniform ivec csOrigin = " GPU_ORIGIN_INIT ";\n" \
      "layout (location = 1) uniform vec2 vsOrigin = {0.f,0.f};\n"       \
      "layout (location = 2) uniform vec2 vsScale = {1.f,1.f};\n"
    #endif
    
    const char glsl_preproc[] =
      "// #line " xstr(LINENO) " " __FILE__ "\n"
      DEFINITIONS
      GPU_NEIGHBOR_DECL
      "#define dimensional(x)\n"
      "#define fmin(a,b) min(a,b)\n"
      "#define fmax(a,b) max(a,b)\n"
      GPU_TYPE_DECLS
      "#define GHOSTS " xstr(GHOSTS) "\n"
      "#define field_size() _field_size\n"
      "#define ast_pointer(x) (x)\n"
      GPU_CODE()
      "#define _NVARMAX 65536\n"
      "#define is_constant(v) ((v).i >= _NVARMAX)\n"
      "#define NULL 0\n"
      "#define val(s,k,l,m) ((s).i < _NVARMAX ? valt(s,k,l,m)"
      " : _constant[clamp((s).i -_NVARMAX,0,_nconst-1)])\n"
      "#define val_out_(s,i,j,k) valt(s,i,j,k)\n"
      "#define diagonalize(a)\n"
      "#define val_diagonal(s,i,j,k) real((i) == 0 && (j) == 0 && (k) == 0)\n"
      "#define _attr(s,member) (_attr[(s).index].member)\n"
      "#define endforin() }\n"
      GPU_INDEX_DECL
      "#define endforin2() }\n"
      "#define endforin3() }\n"
      "#define NOT_UNUSED(x)\n"
    #if SINGLE_PRECISION
      "#define pi 3.14159265359f\n"
      "#define nodata (1e30f)\n"
    #elif !_CUDA // GLSL DOUBLE_PRECISION
      "#define pi 3.14159265358979323846lf\n"
      "#define nodata (1e30lf)\n"
    #else // CUDA DOUBLE_PRECISION
      "#define pi 3.14159265358979323846\n"
      "#define nodata (1e30)\n"
    #endif
      GPU_UNUSED_COORDS
      "_CONST_ int ig = 0, jg = 0, kg = 0;\n"
      GPU_UNIFORM_DECLS
      ;
    
    #undef GPU_REAL_TYPE
    #undef GPU_COORD2_TYPE
    #undef GPU_COORD3_TYPE
    #undef GPU_MATH_DECLS
    #undef GPU_IVEC_TYPE
    #undef GPU_DIM_COORD_TYPE
    #undef GPU_VECTOR_BODY
    #undef GPU_TENSOR_BODY
    #undef GPU_POINT_INDICES
    #undef GPU_NEIGHBOR_INDICES
    #undef GPU_ORIGIN_INIT
    #undef GPU_UNUSED_COORDS
    #undef GPU_POINT_LAYER
    #undef GPU_INDEX_DECL
    #undef GPU_TYPE_DECLS
    #undef GPU_NEIGHBOR_DECL
    #undef GPU_UNIFORM_DECLS
    
    #if _METAL

    Metal requires an explicit address space qualifier for the pointer parameters of any function (not just kernels). Any pointer passed to a function generated by the Basilisk preprocessor points to a local variable of the calling kernel i.e. to the thread address space.

    This adds the thread qualifier to each parameter of the function definition in code which is a pointer.

    static char * metal_thread_qualifiers (const char * code)
    {
      const char * nl = strchr (code, '\n');
      const char * open = nl ? strchr (nl, '(') : NULL;
      if (!open)
        return str_append (NULL, code);
    
      const char * close = open;
      int depth = 0;
      while (*close) {
        if (*close == '(') depth++;
        else if (*close == ')' && !--depth) break;
        close++;
      }
      if (!*close)
        return str_append (NULL, code);
    
      int ncommas = 1;
      for (const char * q = open; q < close; q++)
        if (*q == ',') ncommas++;
      char * out = sysmalloc (strlen (code) + 7*ncommas + 1), * o = out;
    
      const char * p = code;
      while (p <= open) *o++ = *p++;
      const char * start = p;
      depth = 0;
      for (const char * q = p; q <= close; q++) {
        if (q == close || (*q == ',' && !depth)) {
          const char * a = start;
          while (a < q && strchr (" \t\n", *a)) a++;

    The _GLOB_PARAMS_ macro expands to several kernel arguments which already carry their own address space qualifiers.

          for (const char ** m = (const char *[]){ "_GLOB_PARAMS_", "_GLOB_PARAMS0_", NULL };
    	   *m; m++) {
    	size_t len = strlen (*m);
    	if ((size_t)(q - a) >= len && !strncmp (a, *m, len)) {
    	  a += len;
    	  while (a < q && strchr (" \t\n", *a)) a++;
    	  break;
    	}
          }
    
          bool pointer = false;
          for (const char * b = a; b < q; b++)
    	if (*b == '*') pointer = true;
          while (start < a) *o++ = *start++;
          if (pointer) {
    	strcpy (o, "thread "); o += 7;
          }
          while (start < q) *o++ = *start++;
          if (q == close) break;
          *o++ = *q;
          start = q + 1;
        }
        else if (*q == '(') depth++;
        else if (*q == ')') depth--;
      }
      while (*close) *o++ = *close++;
      *o = '\0';
      return out;
    }
    #endif // _METAL
    
    static inline int list_size (const External * i)
    {
      int size = 0;
      if (i->type == sym_SCALAR) {
        size = 1;
        if (i->nd == 1)
          for (scalar s in i->pointer) size++;
      }
      else if (i->type == sym_VECTOR) {
        size = 1;
        if (i->nd == 1)
          for (vector v in i->pointer) size++;
      }
      else if (i->type == sym_TENSOR) {
        size = 1;
        if (i->nd == 1)
          for (tensor t in i->pointer) size++;
      }
      else
        return 0;
      return size;
    }
    
    static char * write_scalar (char * fs, scalar s)
    {
      char i[20], index[20];
      snprintf (i, 19, "%d", s.i);
      if (s.i < 0 || is_constant(s))
        strcpy (index, "0");
      else {
        if (s.block < 0)
          s.i += s.block;
        snprintf (index, 19, "%d", s.gpu.index - 1);
      }
      return str_append (fs, "{", i, ",", index, "}");
    }
    
    static char * write_vector (char * fs, vector v)
    {
      fs = str_append (fs, "{");
      fs = write_scalar (fs, v.x);
      fs = str_append (fs, ",");
      fs = write_scalar (fs, v.y);
    #if dimension == 3
      fs = str_append (fs, ",");
      fs = write_scalar (fs, v.z);
    #endif
      fs = str_append (fs, "}");
      return fs;
    }
    
    static char * write_tensor (char * fs, tensor t)
    {
      fs = str_append (fs, "{");
      fs = write_vector (fs, t.x);
      fs = str_append (fs, ",");
      fs = write_vector (fs, t.y);
    #if dimension == 3
      fs = str_append (fs, ",");
      fs = write_vector (fs, t.z);
    #endif
      fs = str_append (fs, "}");
      return fs;
    }
    
    static scalar * apply_bc_list;
    
    static const int bc_period_x = -1, bc_period_y = -1;
    #if dimension == 3
    static const int bc_period_z = -1;
    #endif
    static const int _mpi_wall = 63;
    
    #ifdef _LOCALDIMS
    # define _GPU_DIMS LocalDims
    #else
    # define _GPU_DIMS Dimensions
    #endif

    Return a 6-bit mask of the local subdomain faces that are physical (non-periodic, non-MPI-seam) boundaries: bits 0..5 correspond to left, right, bottom, top, back, front. Injected as _CONST_ int _mpi_wall at shader compile time so apply_bc() only applies physical boundary conditions on real walls and leaves internal MPI/periodic ghost layers to mpi_boundary_level_gpu().

    static inline int mpi_wall_mask (void)
    {
    #if _MPI && defined(_LOCALDIMS)
      int wmask = 0;
      struct { int x, y, z; } dir = {0, 1, 2};
      foreach_dimension()
        if (!Period.x) {
          if (mpi_coords[dir.x] == 0) wmask |= 1 << (2*dir.x);
          if (mpi_coords[dir.x] == MpiDims.x - 1) wmask |= 1 << (2*dir.x + 1);
        }
      return wmask;
    #else
      return 63;
    #endif
    }

    Return a 3-bit mask of axes where this rank has an internal upper MPI neighbor (mpi_coords[d] < MpiDims[d] - 1). Used in + reductions on foreach_face() / foreach_vertex() so shared seam faces/vertices are counted by only one rank.

    static inline int mpi_seam_mask (void)
    {
    #if _MPI && defined(_LOCALDIMS)
      int smask = 0;
      struct { int x, y, z; } dir = {0, 1, 2};
      foreach_dimension()
        if (mpi_coords[dir.x] < MpiDims.x - 1)
          smask |= 1 << dir.x;
      return smask;
    #else
      return 0;
    #endif
    }
    
    static void boundary_top (Point point, int i, int k)
    {
      bool data = false;
      for (scalar s in apply_bc_list)
        if (!s.face || s.i != s.v.y.i) {
    #if dimension == 2
          scalar b = (s.v.x.i < 0 ? s : s.i == s.v.y.i ? s.v.x : s.v.y);
    #elif dimension == 3
          scalar b = (s.v.x.i < 0 ? s : s.i == s.v.y.i ? s.v.x : s.i == s.v.z.i ? s.v.y : s.v.z);
    #endif
          if (b.boundary_top)
    	foreach_blockf(s)
    	  s[i,-bc_period_y,k] = b.boundary_top (neighborp(i,0,k), neighborp(i,-bc_period_y,k), s, &data);
        }
    }
    
    static void boundary_bottom (Point point, int i, int k)
    {
      bool data = false;
      for (scalar s in apply_bc_list)
        if (!s.face || s.i != s.v.y.i) {
    #if dimension == 2
          scalar b = (s.v.x.i < 0 ? s : s.i == s.v.y.i ? s.v.x : s.v.y);
    #elif dimension == 3
          scalar b = (s.v.x.i < 0 ? s : s.i == s.v.y.i ? s.v.x : s.i == s.v.z.i ? s.v.y : s.v.z);
    #endif
          if (b.boundary_bottom)
    	foreach_blockf(s)
    	  s[i,bc_period_y,k] = b.boundary_bottom (neighborp(i,0,k), neighborp(i,bc_period_y,k), s, &data);
        }
    }
    
    #if dimension == 3
    static void boundary_front (Point point, int i, int j)
    {
      bool data = false;
      for (scalar s in apply_bc_list)
        if (!s.face || s.i != s.v.z.i) {
          scalar b = (s.v.x.i < 0 ? s : s.i == s.v.z.i ? s.v.x : s.i == s.v.x.i ? s.v.y : s.v.z);
          if (b.boundary_front)
    	foreach_blockf(s)
    	  s[i,j,-bc_period_z] = b.boundary_front (neighborp(i,j,0), neighborp(i,j,-bc_period_z), s, &data);
        }
    }
    
    static void boundary_back (Point point, int i, int j)
    {
      bool data = false;
      for (scalar s in apply_bc_list)
        if (!s.face || s.i != s.v.z.i) {
          scalar b = (s.v.x.i < 0 ? s : s.i == s.v.z.i ? s.v.x : s.i == s.v.x.i ? s.v.y : s.v.z);
          if (b.boundary_back)
    	foreach_blockf(s)
    	  s[i,j,bc_period_z] = b.boundary_back (neighborp(i,j,0), neighborp(i,j,bc_period_z), s, &data);
        }
    }
    #endif
    
    static
    void apply_bc (Point point)
    {
      bool data = false;
      // face BCs
      if (((_mpi_wall & 1) != 0) && point.i == GHOSTS)
        for (scalar s in apply_bc_list)
          if (s.face && s.i == s.v.x.i && s.boundary_left)
    	foreach_blockf(s)
    	  s[] = s.boundary_left (point, neighborp(bc_period_x,0,0), s, &data);
      if (((_mpi_wall & 2) != 0) && point.i == N*Dimensions.x + GHOSTS)
        for (scalar s in apply_bc_list)
          if (s.face && s.i == s.v.x.i && s.boundary_right)
    	foreach_blockf(s)
    	  s[] = s.boundary_right (neighborp(bc_period_x,0,0), point, s, &data);
      if (((_mpi_wall & 4) != 0) && point.j == GHOSTS)
        for (scalar s in apply_bc_list)
          if (s.face && s.i == s.v.y.i) {
    	scalar b = s.v.x;
    	if (b.boundary_bottom)
    	  foreach_blockf(s)
    	    s[] = b.boundary_bottom (point, neighborp(0,bc_period_y,0), s, &data);
          }
      if (((_mpi_wall & 8) != 0) && point.j == N*Dimensions.y + GHOSTS)
        for (scalar s in apply_bc_list)
          if (s.face && s.i == s.v.y.i) {
    	scalar b = s.v.x;
    	if (b.boundary_top)
    	  foreach_blockf(s)
    	    s[] = b.boundary_top (neighborp(0,bc_period_y,0), point, s, &data);
          }
    #if dimension == 3
      if (((_mpi_wall & 16) != 0) && point.k == GHOSTS)
        for (scalar s in apply_bc_list)
          if (s.face && s.i == s.v.z.i) {
    	scalar b = s.v.x;
    	if (b.boundary_back)
    	  foreach_blockf(s)
    	    s[] = b.boundary_back (point, neighborp(0,0,bc_period_z), s, &data);
          }
      if (((_mpi_wall & 32) != 0) && point.k == N*Dimensions.z + GHOSTS)
        for (scalar s in apply_bc_list)
          if (s.face && s.i == s.v.z.i) {
    	scalar b = s.v.x;
    	if (b.boundary_front)
    	  foreach_blockf(s)
    	    s[] = b.boundary_front (neighborp(0,0,bc_period_z), point, s, &data);
          }
    #endif
      // centered BCs (_mpi_wall bits: 0..5 = left,right,bottom,top,back,front)
      if (((_mpi_wall & 1) != 0) && point.i == GHOSTS) { // left
        for (scalar s in apply_bc_list)
          if ((!s.face || s.i != s.v.x.i) && s.boundary_left)
    	foreach_blockf(s)
    	  s[bc_period_x,0,0] = s.boundary_left (point, neighborp(bc_period_x,0,0), s, &data);
        if (((_mpi_wall & 4) != 0) && point.j == GHOSTS) {
          boundary_bottom (point, bc_period_x, 0);
    #if dimension == 3
          if (((_mpi_wall & 16) != 0) && point.k == GHOSTS)
    	boundary_back (point, bc_period_x, bc_period_y);
          if (((_mpi_wall & 32) != 0) && point.k == N*Dimensions.z + GHOSTS - 1)
    	boundary_front (point, bc_period_x, bc_period_y);
    #endif
        }
        if (((_mpi_wall & 8) != 0) && point.j == N*Dimensions.y + GHOSTS - 1) {
          boundary_top (point, bc_period_x, 0);
    #if dimension == 3
          if (((_mpi_wall & 16) != 0) && point.k == GHOSTS)
    	boundary_back (point, bc_period_x, -bc_period_y);
          if (((_mpi_wall & 32) != 0) && point.k == N*Dimensions.z + GHOSTS - 1)
    	boundary_front (point, bc_period_x, -bc_period_y);
    #endif
        }
    #if dimension == 3
        if (((_mpi_wall & 16) != 0) && point.k == GHOSTS)
          boundary_back (point, bc_period_x, 0);
        if (((_mpi_wall & 32) != 0) && point.k == N*Dimensions.z + GHOSTS - 1)
          boundary_front (point, bc_period_x, 0);
    #endif
      }
      if (((_mpi_wall & 2) != 0) &&
          point.i == N*Dimensions.x + GHOSTS - 1) { // right
        for (scalar s in apply_bc_list)
          if ((!s.face || s.i != s.v.x.i) && s.boundary_right)
    	foreach_blockf(s)
    	  s[-bc_period_x,0,0] = s.boundary_right (point, neighborp(-bc_period_x,0,0), s, &data);
        if (((_mpi_wall & 4) != 0) && point.j == GHOSTS) {
          boundary_bottom (point, -bc_period_x, 0);
    #if dimension == 3
          if (((_mpi_wall & 16) != 0) && point.k == GHOSTS)
    	boundary_back (point, -bc_period_x, bc_period_y);
          if (((_mpi_wall & 32) != 0) && point.k == N*Dimensions.z + GHOSTS - 1)
    	boundary_front (point, -bc_period_x, bc_period_y);
    #endif
        }
        if (((_mpi_wall & 8) != 0) && point.j == N*Dimensions.y + GHOSTS - 1) {
          boundary_top (point, -bc_period_x, 0);
    #if dimension == 3
          if (((_mpi_wall & 16) != 0) && point.k == GHOSTS)
    	boundary_back (point, -bc_period_x, -bc_period_y);
          if (((_mpi_wall & 32) != 0) && point.k == N*Dimensions.z + GHOSTS - 1)
    	boundary_front (point, -bc_period_x, -bc_period_y);
    #endif
        }
    #if dimension == 3
        if (((_mpi_wall & 16) != 0) && point.k == GHOSTS)
          boundary_back (point, -bc_period_x, 0);
        if (((_mpi_wall & 32) != 0) && point.k == N*Dimensions.z + GHOSTS - 1)
          boundary_front (point, -bc_period_x, 0);
    #endif
      }
      if (((_mpi_wall & 4) != 0) && point.j == GHOSTS) {
        boundary_bottom (point, 0, 0);
    #if dimension == 3
        if (((_mpi_wall & 16) != 0) && point.k == GHOSTS)
          boundary_back (point, 0, bc_period_y);
        if (((_mpi_wall & 32) != 0) && point.k == N*Dimensions.z + GHOSTS - 1)
          boundary_front (point, 0, bc_period_y);
    #endif
      }
      if (((_mpi_wall & 8) != 0) && point.j == N*Dimensions.y + GHOSTS - 1) {
        boundary_top (point, 0, 0);
    #if dimension == 3
        if (((_mpi_wall & 16) != 0) && point.k == GHOSTS)
          boundary_back (point, 0, -bc_period_y);
        if (((_mpi_wall & 32) != 0) && point.k == N*Dimensions.z + GHOSTS - 1)
          boundary_front (point, 0, -bc_period_y);
    #endif
      }
    #if dimension == 3
      if (((_mpi_wall & 16) != 0) && point.k == GHOSTS)
        boundary_back (point, 0, 0);
      if (((_mpi_wall & 32) != 0) && point.k == N*Dimensions.z + GHOSTS - 1)
        boundary_front (point, 0, 0);
    #endif
    }
    
    static bool is_boundary_attribute (const External * g)
    {
      return (g->name[0] == '.' &&
    	  (!strcmp (g->name, ".boundary_left") ||
    	   !strcmp (g->name, ".boundary_right") ||
    	   !strcmp (g->name, ".boundary_bottom") ||
    	   !strcmp (g->name, ".boundary_top")
    #if dimension == 3
               || !strcmp (g->name, ".boundary_front")
               || !strcmp (g->name, ".boundary_back")
    #endif
               ));
    }
    
    static
    void hash_external (Adler32Hash * hash, const External * g, const ForeachData * loop, int indent)
    {
      if (g->type == sym_function_declaration || g->type == sym_function_definition) {
        bool boundary = is_boundary_attribute (g);
        for (scalar s in baseblock)
          if (g->name[0] != '.' || (!boundary && s.gpu.index) ||
              (boundary && (s.stencil.io & s_output))) {
    	void * ptr = g->name[0] != '.' ? g->pointer :
    	  *((void **)(((char *) &_attribute[s.i]) + g->nd));
    	if (ptr) {
    	  External * p = _get_function ((long) ptr);
    	  if (p && !p->used) {
    	    p->used = true;
    	    a32_hash_add (hash, &ptr, sizeof (void *));
    	    for (const External * e = p->externals; e && e->name; e++)
    	      hash_external (hash, e, loop, indent + 2);
    	  }
    	}
    	if (g->name[0] != '.')
    	    break;
          }
      }
      else if (g->name[0] == '.') {
        int size = 0;
        switch (g->type) {
        case sym_INT: size = sizeof (int); break;
        case sym_BOOL: size = sizeof (bool); break;
        case sym_FLOAT: size = sizeof (float); break;
        case sym_DOUBLE: size = sizeof (double); break;
        case sym_IVEC: size = sizeof (ivec); break;
        case sym_SCALAR: size = sizeof (scalar); break;
        case sym_VECTOR: size = sizeof (vector); break;
        case sym_TENSOR: size = sizeof (tensor); break;
        default: return;
        }
        for (scalar s in baseblock)
          if (s.gpu.index) {
    	char * data = (char *) &_attribute[s.i];
    	a32_hash_add (hash, data + g->nd, size);
          }
      }
      else if (g->type == sym_SCALAR || g->type == sym_VECTOR || g->type == sym_TENSOR) {
        assert (g->nd == 0 || g->nd == 1);
        void * pointer = g->pointer;
        int size;
        if (g->nd == 1) {
          size = 0;
          for (scalar s in pointer)
    	size += sizeof (scalar);
        }
        else if (g->type == sym_SCALAR)
          size = sizeof (scalar);
        else if (g->type == sym_VECTOR)
          size = sizeof (vector);
        else // sym_TENSOR
          size = sizeof (tensor);
        a32_hash_add (hash, pointer, size);
      }
      else if (is_external_constant(g))
        a32_hash_add (hash, g->pointer, external_size (g));
    }
    
    static
    uint32_t hash_shader (const External * externals,
    		      const ForeachData * loop,
    		      const RegionParameters * region, const char * kernel)
    {
      Adler32Hash hash;
      a32_hash_init (&hash);
      a32_hash_add (&hash, &region->level, sizeof (region->level));
      a32_hash_add (&hash, &N, sizeof (N));
    #if LAYERS
      a32_hash_add (&hash, &nl, sizeof (nl));
    #endif
      a32_hash_add (&hash, &_GPU_DIMS, sizeof (_GPU_DIMS));
      a32_hash_add (&hash, &Period, sizeof (Period));
    #if _MPI && defined(_LOCALDIMS)
      a32_hash_add (&hash, &MpiDims, sizeof (MpiDims));
      a32_hash_add (&hash, mpi_coords, sizeof (mpi_coords));
    #endif
      a32_hash_add (&hash, kernel, strlen (kernel));
      a32_hash_add (&hash, &nconst, sizeof (nconst));
      a32_hash_add (&hash, _constant, sizeof (*_constant)*nconst);
      static External ext[] = {
        { .name = ".boundary_left",   .type = sym_function_declaration,
          .nd = attroffset (boundary_left) },
        { .name = ".boundary_right",  .type = sym_function_declaration,
          .nd = attroffset (boundary_right) },
        { .name = ".boundary_top",    .type = sym_function_declaration,
          .nd = attroffset (boundary_top) },
        { .name = ".boundary_bottom", .type = sym_function_declaration,
          .nd = attroffset (boundary_bottom) },
    #if dimension == 3
        { .name = ".boundary_front",    .type = sym_function_declaration,
          .nd = attroffset (boundary_front) },
        { .name = ".boundary_back", .type = sym_function_declaration,
          .nd = attroffset (boundary_back) },
    #endif
    #if LAYERS
        { .name = ".block", .type = sym_INT, .nd = attroffset (block) },
    #endif
        { .name = NULL }
      };
      foreach_function (f, f->used = false);
      for (const External * g = loop->dirty ? ext : ext + 2*dimension; g->name; g++)
        hash_external (&hash, g, loop, 2);
      int imax = 1;
      if (GPUContext.nssbo > 1)
        for (scalar s in baseblock)
          if (s.gpu.index && s.i + s.block > imax)
    	imax = s.i + s.block;
      for (const External * g = externals; g && g->name; g++) {
        if (g->reduct) {
          a32_hash_add (&hash, &g->s, sizeof (scalar));
          if (GPUContext.nssbo > 1) {
    	scalar s = g->s;
    	if (s.i + s.block > imax)
    	  imax = s.i + s.block;
          }
        }
        hash_external (&hash, g, loop, 2);
      }
      a32_hash_add (&hash, &imax, sizeof (imax));
      return a32_hash (&hash);
    }
    
    static
    bool is_void_function (char * code)
    {
      while (*code != '/') code++; code++;
      while (*code != '/') code++; code++;
      while (*code != '\n') code++; code++;
      while (strchr ("\n\r\t ", *code)) code++;
      return !strncmp (code, "void ", 5);
    }
    
    static char * type_string (const External * g)
    {
      switch (g->type) {
      case sym_DOUBLE:
    #if !SINGLE_PRECISION
        return "double"; break;
    #endif
      case sym_FLOAT: return "float";
      case sym_function_declaration: case sym_enumeration_constant:
      case sym_INT: case sym_LONG: return "int";
      case sym_BOOL: return "bool";
      case sym_SCALAR: return "scalar";
      case sym_VECTOR: return "vector";
      case sym_TENSOR: return "tensor";
      case sym_COORD:  return "coord";
      case sym__COORD: return "_coord";
      case sym_VEC4:   return "vec4";
      case sym_IVEC:   return "ivec";
      }
      return "unknown_type";
    }
    
    static
    char * external_declaration (char * fs, const External * g)
    {
      char * type = type_string (g);
      fs = str_append (fs, type, " ", EXTERNAL_NAME (g));
      for (int * d = g->data; d && *d > 0; d++) {
        char s[20]; snprintf (s, 19, "%d", *d);
        fs = str_append (fs, "[", s, "]");
      }
      return fs;
    }
    
    static
    char * external_write (char * fs, const External * g, const ForeachData * loop,
                           const RegionParameters * region, const unsigned nwg[dimension],
    		       bool local)
    {

    Variables declared within the body of the kernel cannot use the constant address space of Metal (see _CONST_ in DEFINITIONS).

      const char * _const_ = local ? "const " : "_CONST_ ";
      if (g->name[0] == '.') {
        if (g->type == sym_function_declaration) {
          bool boundary = is_boundary_attribute (g);
          fs = str_append (fs, "#define _attr_", g->name + 1, "(s,args) (");
          foreach_function (f, f->used = false);
          char * expr = NULL;
          for (scalar s in baseblock)
            if (s.gpu.index) {
              char * data = (char *) &_attribute[s.i];
              void * func = *((void **)(data + g->nd));
              if (func) {
                External * f = _get_function ((long) func);
                if (!f->used && (!boundary || (s.stencil.io & s_output))) {
                  f->used = true;
                  char * args = is_void_function (f->data) ? " args,0" : " args";
                  if (!expr)
                    expr = str_append (NULL, "(", f->name, args, ")");
                  else {
                    char index[20];
                    snprintf (index, 19, "%d", f->nd);
                    char * s = str_append (NULL, "_attr(s,", g->name + 1, ")==", index,
                                           "?(", f->name, args, "):", expr);
                    sysfree (expr);
                    expr = s;
                  }
                }
              }
            }
          if (expr) {
            fs = str_append (fs, expr, ")\n");
            sysfree (expr);
          }
          else
            fs = str_append (fs, "0)\n");
        }
      }
      else if (g->type == sym_function_definition) {
        External * f = _get_function ((long) g->pointer);
    #if _HIP_AMD
        fs = str_append (fs, "\n__device__ ", f->data, "\n");
    #elif _METAL
        char * code = metal_thread_qualifiers (f->data);
        fs = str_append (fs, "\n", code, "\n");
        sysfree (code);
    #else
        fs = str_append (fs, "\n", f->data, "\n");
    #endif
        char s[20]; snprintf (s, 19, "%d", f->nd);
        fs = str_append (fs, "_CONST_ int _p", g->name, " = ", s, ";\n");
      }
      else if (g->type == sym_function_declaration) {
        External * f = _get_function ((long) g->pointer);
        char s[20]; snprintf (s, 19, "%d", f->nd);
        fs = str_append (fs, "_CONST_ int ", g->name, " = ", s, ";\n",
                         "#define _f", g->name, "(args) (", f->name, " args)\n");
      }
      else if (g->type != sym_SCALAR &&
               g->type != sym_VECTOR &&
               g->type != sym_TENSOR) {
        if (g->type == sym_INT && g->global && !strcmp (g->name, "N")) {
          int Nl = region->level > 0 ? 1 << (region->level - 1) : N/Dimensions.x;
          int level = region->level > 0 ? region->level - 1 : depth();
          char s[20], d[20], l[20];
          snprintf (s, 19, "%d", Nl);
          snprintf (d, 19, "%d", depth());
          snprintf (l, 19, "%d", level);
          fs = str_append (fs,
                           "_CONST_ uint N = ", s, ", _depth = ", d, ";\n");
          if (GPUContext.nssbo <= 1) {
            char size[30];
            snprintf (size, 29, "%zu", (size_t) field_size());
    #if _CUDA
            fs = str_append (fs,
                             "_CONST_ size_t _field_size = ", size, "UL;\n");
    #else
            fs = str_append (fs,
                             "_CONST_ uint _field_size = ", size, ";\n");
    #endif
          }
    #ifdef shift_level // multigrid
          char nd[20]; snprintf (nd, 19, "%d", depth() + 1);
    #if _CUDA
          fs = str_append (fs,
                           "_CONST_ size_t _shift[", nd, "] = {");
          for (int d = 0; d <= depth(); d++) {
            snprintf (s, 19, "%zuUL", (size_t) shift_level(d));
            fs = str_append (fs,  d > 0 ? "," : "", s);
          }
    #else
          fs = str_append (fs,
                           "_CONST_ uint _shift[", nd, "] = {");
          for (int d = 0; d <= depth(); d++) {
            snprintf (s, 19, "%ld", shift_level(d));
            fs = str_append (fs,  d > 0 ? "," : "", s);
          }
    #endif
          fs = str_append (fs, "};\n");
    #endif // ifdef shift_level
          char seam_s[20];
          snprintf (seam_s, 19, "%d", mpi_seam_mask ());
          snprintf (s, 19, "%d", _GPU_DIMS.x);
          snprintf (d, 19, "%d", _GPU_DIMS.y);
    #if dimension == 2
          fs = str_append (fs,
                           "_CONST_ int _mpi_seam = ", seam_s, ";\n"
                           "_CONST_ ivec2 Dimensions = {", s, ",", d, "};\n"
                           "_CONST_ uint NX = N*", s,
                           (loop->face & 1) || loop->vertex ? "+1" : "", ";\n"
                           "_CONST_ uint NY = N*", d,
                           loop->face > 1 || loop->vertex ? "+1" : "", ";\n");
    #elif dimension == 3
          char e[20];
          snprintf (e, 19, "%d", _GPU_DIMS.z);
          fs = str_append (fs,
                           "_CONST_ int _mpi_seam = ", seam_s, ";\n"
                           "_CONST_ ivec3 Dimensions = {", s, ",", d, ",", e, "};\n"
                           "_CONST_ uint NX = N*", s,
                           (loop->face & 1) || loop->vertex ? "+1" : "", ";\n"
                           "_CONST_ uint NY = N*", d,
                           (loop->face & (1 << 1)) || loop->vertex ? "+1" : "", ";\n"
                           "_CONST_ uint NZ = N*", e,
                           (loop->face & (1 << 2)) || loop->vertex ? "+1" : "", ";\n");
    #endif
    #if !_CUDA
    #if dimension == 2
          if (GPUContext.fragment_shader)
            fs = str_append (fs, "in vec2 vsPoint;\n"
                             "Point point = {int((vsPoint.x*vsScale.x + vsOrigin.x)*N*Dimensions.x)"
                             " + GHOSTS,"
                             "int((vsPoint.y*vsScale.y + vsOrigin.y)*N*Dimensions.x) + GHOSTS,", l,
    #if MULTIGRID
                             ",{int(N*Dimensions.x),int(N*Dimensions.y)}"
    #else
                             ",N"
    #endif
    #if LAYERS
                             ",0"
    #endif
                             "};\n"
                             "out vec4 FragColor;\n");
    #elif dimension == 3
          assert (!GPUContext.fragment_shader);
    #endif
          else {
            char nwgx[20], nwgy[20];
            snprintf (nwgx, 19, "%d", nwg[0]);
            snprintf (nwgy, 19, "%d", nwg[1]);
    #if dimension == 2
            fs = str_append (fs, "layout (local_size_x = ", nwgx,
                             ", local_size_y = ", nwgy, ") in;\n");
    #elif dimension == 3
            char nwgz[20];
            snprintf (nwgz, 19, "%d", nwg[2]);
            fs = str_append (fs, "layout (local_size_x = ", nwgx,
                             ", local_size_y = ", nwgy,
                             ", local_size_z = ", nwgz, ") in;\n");
    #endif
          }
    #endif // !_CUDA
        }
        else if (g->type == sym_INT && g->global && !strcmp (g->name, "nl")) {

    ‘int nl’ gets special treatment.

          char nl[20];
          snprintf (nl, 19, "%d", *((int *)g->pointer));
          fs = str_append (fs, "_CONST_ int nl = ", nl, ";\n");
        }
        else if (g->type == sym_INT && g->global &&
                 !strcmp (g->name, "_mpi_wall")) {
          char s[20];
          snprintf (s, 19, "%d", mpi_wall_mask ());
          fs = str_append (fs, "_CONST_ int _mpi_wall = ", s, ";\n");
        }
        else if (g->type == sym_INT && g->global && !strcmp (g->name, "bc_period_x")) {
          char s[20] = "-1";
    #if !_MPI
          if (Period.x)
    #if _OPENCL
            snprintf (s, 19, "(int)(N*%d)", _GPU_DIMS.x);
    #else
            snprintf (s, 19, "int(N*%d)", _GPU_DIMS.x);
    #endif
    #endif
          fs = str_append (fs, "_CONST_ int bc_period_x = ", s, ";\n");
        }
        else if (g->type == sym_INT && g->global && !strcmp (g->name, "bc_period_y")) {
          char s[20] = "-1";
    #if !_MPI
          if (Period.y)
    #if _OPENCL
            snprintf (s, 19, "(int)(N*%d)", _GPU_DIMS.y);
    #else
            snprintf (s, 19, "int(N*%d)", _GPU_DIMS.y);
    #endif
    #endif
          fs = str_append (fs, "_CONST_ int bc_period_y = ", s, ";\n");
        }
    #if dimension == 3
        else if (g->type == sym_INT && g->global && !strcmp (g->name, "bc_period_z")) {
          char s[20] = "-1";
    #if !_MPI
          if (Period.z)
    #if _OPENCL
            snprintf (s, 19, "(int)(N*%d)", _GPU_DIMS.z);
    #else
            snprintf (s, 19, "int(N*%d)", _GPU_DIMS.z);
    #endif
    #endif
          fs = str_append (fs, "_CONST_ int bc_period_z = ", s, ";\n");
        }
    #endif
    #if dimension == 2
        else if (GPUContext.fragment_shader && (region->n.x > 1 || region->n.y > 1) &&
                 g->type == sym_COORD && !strcmp (g->name, "p")) {

    ‘coord p’ is assumed to be the parameter of a region. This is not flexible (the parameter must be called ‘p’) and should be improved.

    #if SINGLE_PRECISION
          fs = str_append (fs, "coord p = vec3((vsPoint*vsScale + vsOrigin)*L0 + vec2(X0, Y0),0);\n");
    #else
          fs = str_append (fs, "coord p = dvec3((dvec2(vsPoint)*dvec2(vsScale) + dvec2(vsOrigin))*L0 + dvec2(X0, Y0),0);\n");
    #endif
        }
    #endif
        else if (is_external_constant (g)) {
          char value[30];
          assert (g->pointer);
          switch (g->type) {
          case sym_INT: snprintf (value, 29, "%d", *((int *)g->pointer)); break;
          case sym_LONG: snprintf (value, 29, "%ld", *((long *)g->pointer)); break;
          case sym_FLOAT: snprintf (value, 29, "%.9gf", *((float *)g->pointer)); break;
          case sym_DOUBLE: snprintf (value, 29, "%#.17g", *((double *)g->pointer)); break;
          case sym_BOOL: snprintf (value, 29, "%d", *((bool *)g->pointer)); break;
          default:
            assert (false); // not implemented yet
          }
          fs = str_append (fs, _const_, type_string (g), " ", EXTERNAL_NAME (g), "=", value, ";\n");
        }
        else if (strcmp (g->name, "Dimensions") && g->reduct != '+') {
    #if !_CUDA
          fs = str_append (fs, "uniform ");
          fs = external_declaration (fs, g);
          fs = str_append (fs, ";\n");
    #endif // !_CUDA
        }
      }
      else { // scalar, vector and tensor fields
        int size = list_size (g);
        for (int j = 0; j < size; j++) {
          if (j == 0) {
            fs = str_append (fs, _const_, type_string (g), " ", EXTERNAL_NAME (g));
            if (g->nd == 0)
              fs = str_append (fs, " = ");
            else {
              char s[20]; snprintf (s, 19, "%d", size);
              fs = str_append (fs, "[", s, "] = {");
            }
          }
          if (g->nd == 0 || j < size - 1) {
            if (g->type == sym_SCALAR)
              fs = write_scalar (fs, ((scalar *)g->pointer)[j]);
            else if (g->type == sym_VECTOR)
              fs = write_vector (fs, ((vector *)g->pointer)[j]);
            else if (g->type == sym_TENSOR)
              fs = write_tensor (fs, ((tensor *)g->pointer)[j]);
            else
              assert (false);
          }
          else { // last element of a list is always ignored (this is necessary for empty lists)
            if (g->type == sym_SCALAR)
              fs = str_append (fs, "{0,0}");
            else if (g->type == sym_VECTOR)
    #if dimension == 2
              fs = str_append (fs, "{{0,0},{0,0}}");
    #elif dimension == 3
              fs = str_append (fs, "{{0,0},{0,0},{0,0}}");
    #endif
            else if (g->type == sym_TENSOR)
    #if dimension == 2
              fs = str_append (fs, "{{{0,0},{0,0}},{{0,0},{0,0}}}");
    #elif dimension == 3
              fs = str_append (fs, "{{{0,0},{0,0},{0,0}},{{0,0},{0,0},{0,0}},{{0,0},{0,0},{0,0}}}");
    #endif
            else
              assert (false);
          }
          if (g->nd == 0)
            fs = str_append (fs, ";\n");
          else if (j < size - 1)
            fs = str_append (fs, ",");
          else
            fs = str_append (fs, "};\n");
        }
      }
      return fs;
    }
    
    trace
    char * build_shader (External * externals, const ForeachData * loop,
    		     const RegionParameters * region, const unsigned nwg[dimension])
    {
      char s[30];
      snprintf (s, 19, "%d", nconst > 0 ? nconst : 1);
      char a[36];
    #if !_CUDA && !SINGLE_PRECISION
      const char * fmt = "%#.17glf";
    #else
      const char * fmt = "%#.17g";
    #endif
      snprintf (a, 35, fmt, nconst > 0 ? _constant[0] : 0.);
      char * fs = str_append (NULL,
    #if !_CUDA
                              "#version 430\n",
    #endif
                              glsl_preproc,
    			  "_CONST_ int _nconst = ", s, ";\n"
    			  "_CONST_ real _constant[", s, "] = {", a);
      for (int i = 1; i < nconst; i++) {
        snprintf (a, 35, fmt, _constant[i]);
        fs = str_append (fs, ",", a);
      }
      fs = str_append (fs, "};\n"
    #if !_CUDA
    		   "layout(std430, binding = 0)"
    		   " restrict buffer _data_layout { real f[]; } _data"
    #endif
                       );
      if (GPUContext.nssbo > 1) {
        snprintf (a, 19, "%d", GPUContext.nssbo);
        snprintf (s, 29, "%ld", GPUContext.max_ssbo_size/sizeof(real));
        fs = str_append (fs, "[", a, "];\n"
    		     "#define _data_val(field,index) _data[_offset[(field)].i+(_offset[(field)].j+(index))/", s,
    		     "].f[(_offset[(field)].j+(index))%", s, "]\n");
      }
      else
    #if _OPENCL || _METAL
        fs = str_append (fs,
    		     "#define _data_val(field,index) _data[(field)*field_size() + (index)]\n");
    #elif _CUDA
        fs = str_append (fs,
    		     "#define _data_val(field,index)"
    		     " _ctx_->_data[(size_t)(field)*field_size()"
    		     " + (size_t)(index)]\n");
    #else
        fs = str_append (fs, ";\n"
    		     "#define _data_val(field,index) _data.f[(field)*field_size() + (index)]\n");
    #endif

    Scalar field attributes

      char * attributes = NULL;
      for (const External * g = externals; g; g = g->next)
        if (g->name[0] == '.') {
          attributes = str_append (attributes, "  ", type_string (g), " ", g->name + 1);
          for (int * d = g->data; d && *d > 0; d++) {
    	char s[20]; snprintf (s, 19, "%d", *d);
    	attributes = str_append (attributes, "[", s, "]");
          }
          attributes = str_append (attributes, ";\n");
        }
      
      if (attributes) {
    #if _OPENCL
        fs = str_append (fs, "typedef struct {\n", attributes, "} _Attributes;\n");
    #else
        fs = str_append (fs, "struct _Attributes {\n", attributes, "};\n");
    #endif
        sysfree (attributes);
        int nindex = 0;
        for (scalar s in baseblock)
          if (s.gpu.index)
    	nindex++;
        assert (nindex > 0);
        char s[20]; snprintf (s, 19, "%d", nindex);
        fs = str_append (fs, "_CONST_ _Attributes _attr[", s, "]={");
        nindex = 0;
        for (scalar s in baseblock)
          if (s.gpu.index) {
    	fs = str_append (fs, nindex ? ",{" : "{"); nindex++;
    	bool first = true;
    	char * data = (char *) &_attribute[s.i];
    	for (const External * g = externals; g; g = g->next)
    	  if (g->name[0] == '.') {
    	    if (!first) fs = str_append (fs, ",");
    	    first = false;
    	    if (g->type == sym_INT) {
    	      char s[20]; snprintf (s, 19, "%d", *((int *)(data + g->nd)));
    	      fs = str_append (fs, s);
    	    }
    	    else if (g->type == sym_BOOL)
    	      fs = str_append (fs, *((bool *)(data + g->nd)) ? "true" : "false");
    	    else if (g->type == sym_FLOAT) {
    	      char s[20]; snprintf (s, 19, "%g", *((float *)(data + g->nd)));
    	      fs = str_append (fs, s);
    	    }
    	    else if (g->type == sym_DOUBLE) {
    #if !_CUDA && !SINGLE_PRECISION
    	      char s[36]; snprintf (s, 35, "%#.17glf", *((double *)(data + g->nd)));
    #else
    	      char s[32]; snprintf (s, 31, "%#.17g", *((double *)(data + g->nd)));
    #endif
    	      fs = str_append (fs, s);
    	    }
    	    else if (g->type == sym_IVEC) {
    	      ivec * v = (ivec *)(data + g->nd);
    #if dimension == 2
    	      char s[20]; snprintf (s, 19, "{%d,%d}", v->x, v->y);
    #elif dimension == 3
    	      char s[20]; snprintf (s, 19, "{%d,%d,%d}", v->x, v->y, v->z);
    #endif
    	      fs = str_append (fs, s);
    	    }
    	    else if (g->type == sym_function_declaration) {
    	      void * func = *((void **)(data + g->nd));
    	      if (!func)
    		fs = str_append (fs, "0");
    	      else {
    		External * ptr = _get_function ((long) func);
    		char s[20]; snprintf (s, 19, "%d", ptr->nd);
    		fs = str_append (fs, s);
    	      }
    	    }
    	    else if (g->type == sym_SCALAR)
    	      fs = write_scalar (fs, *((scalar *)(data + g->nd)));
    	    else if (g->type == sym_VECTOR)
    	      fs = write_vector (fs, *((vector *)(data + g->nd)));
    	    else if (g->type == sym_TENSOR)
    	      fs = write_tensor (fs, *((tensor *)(data + g->nd)));
    	    else
    	      fs = str_append (fs, "not implemented");
    	  }
    	fs = str_append (fs, "}");
          }
        fs = str_append (fs, "};\n");
      }

    Field offsets when using multiple SSBOs

      if (GPUContext.nssbo > 1) {
        int imax = 1;
        for (scalar s in baseblock)
          if (s.gpu.index && s.i + s.block > imax)
    	imax = s.i + s.block;
        for (External * g = externals; g; g = g->next)
          if (g->reduct) {
    	scalar s = g->s;
    	if (s.i + s.block > imax)
    	  imax = s.i + s.block;
          }
        char string[100]; snprintf (string, 19, "%d", imax);
        fs = str_append (fs, "_CONST_ struct { uint i, j; } _offset[", string, "]={");
        for (int s = 0; s < imax; s++) {
          fs = str_append (fs, s ? ",{" : "{");
          size_t offset = s*field_size(), size = GPUContext.max_ssbo_size/sizeof(real);
          size_t i = offset/size, j = offset%size;
          snprintf (string, 99, "%ld,%ld", i, j);
          fs = str_append (fs, string, "}");
    
          if (j + field_size() > 1L << 32) {
    	fprintf (stderr, "%s:%d: error: resolution is too high for 32-bits indexing\n",
    		 __FILE__, LINENO);
    	exit (1);
          }
        }
        fs = str_append (fs, "};\n");
      }

    Global variables

    #if _METAL

    Metal does not allow pointers in constant buffers: the first field is only a placeholder which matches the layout of the corresponding structure on the host.

      fs = str_append (fs, "struct _Ctx_ {\n  ulong _data;\n");
      for (const External * g = externals; g; g = g->next)
        if (g->global && is_external_variable (g)) {
          fs = str_append (fs, "  ");
          fs = external_declaration (fs, g);
          fs = str_append (fs, ";\n");
        }
      fs = str_append (fs, "};\n");
    #elif _CUDA
      fs = str_append (fs, "struct _Ctx_ {\n  __global real * _data;\n");
      for (const External * g = externals; g; g = g->next)
        if (g->global && strcmp (g->name, "_mpi_wall") &&
            (is_external_variable (g) ||
             (g->type == sym_IVEC && !g->nd &&
              (!g->data || ((int *)g->data)[1] == 0)))) {
          fs = str_append (fs, "  const ");
          fs = external_declaration (fs, g);
          fs = str_append (fs, ";\n");
        }
      fs = str_append (fs, "};\n");
    #endif // _CUDA

    Global definitions

      for (External * g = externals; g; g = g->next)    
        if (g->global || !is_normal_variable (g) || is_external_variable (g))
          fs = external_write (fs, g, loop, region, nwg, false);
          
      return fs;
    }
    
    trace
    Shader * load_shader (const char * fs, uint32_t hash, const ForeachData * loop)
    {
      assert (gpu_grid->shaders);
      khiter_t k = kh_get (INT, gpu_grid->shaders, hash);
      if (k != kh_end (gpu_grid->shaders)) {
        sysfree ((void *)fs);
        return kh_value (gpu_grid->shaders, k);
      }
    #if PRINTSHADER
      {
        static int n = 1;
        fprintf (stderr, "=================== %s:%d: shader #%d ===================\n",
    	     loop ? loop->fname : "reduction", loop ? loop->line : 0, n++);
        fputs (fs, stderr);
      }
    #endif
      Shader * shader;
      if (loop && loop->func) {
        char func[strlen(loop->func) + 20];
        sprintf (func, "%s_%d", loop->func, loop->line);
        shader = load_normal_shader (fs, func, loop->fname, loop->line);
      }
      else
        shader = load_normal_shader (fs, __func__, __FILE__, LINENO);
      if (shader) {
        int ret;
        khiter_t k = kh_put (INT, gpu_grid->shaders, hash, &ret);
        assert (ret > 0);
        kh_value (gpu_grid->shaders, k) = shader;
      }
    #if PRINTSHADERERROR
      else
        fputs (fs, stderr);
    #endif
      sysfree ((void *)fs);
      return shader;
    }
    
    void gpu_free()
    {
      if (!grid)
        return;
      free_boundaries();
      if (gpu_grid->shaders) {
        Shader * shader;
        int nshaders = 0;
        kh_foreach_value (gpu_grid->shaders, shader,
                          free_shader (shader);
                          nshaders++; );
    #if PRINTNSHADERS  
        fprintf (stderr, "# %d shaders\n", nshaders);
    #endif
        kh_destroy (INT, gpu_grid->shaders);
        gpu_grid->shaders = NULL;
        gpu_free_context (gpu_grid->data);
        gpu_grid->data = NULL;
      }
    }
    
    void gpu_free_grid (void)
    {
      if (!grid)
        return;
      gpu_free();
      free_grid();
    }
    
    attribute {
      double (* boundary_left)   (Point, Point, scalar, bool *);
      double (* boundary_right)  (Point, Point, scalar, bool *);
      double (* boundary_top)    (Point, Point, scalar, bool *);
      double (* boundary_bottom) (Point, Point, scalar, bool *);
    #if dimension == 3
      double (* boundary_front)  (Point, Point, scalar, bool *);
      double (* boundary_back)   (Point, Point, scalar, bool *);
    #endif
    }

    The stored attibute tracks where the up-to-date field is stored:

    • 0: on both the CPU and GPU (i.e. synchronized).
    • 1: on the CPU.
      • 1: on the GPU.
    #define s_phys_gpu (1 << 6)
    
    attribute {
      struct {
        int stored, index;
        unsigned int mpi_clean;
      } gpu;
    }
    
    #define reset(...) reset_gpu (__VA_ARGS__)
    
    trace
    void reset_gpu (void * alist, double val)
    {
      scalar * list = alist;
      for (scalar s in list)
        if (!is_constant(s)) {
          reset_scalar (s.i, s.block, field_size(), val);
          s.gpu.stored = -1;
          s.gpu.mpi_clean = 0;
          s.stencil.bc = 0;
        }
    }
    
    void gpu_init_grid (int n)
    {
    #ifdef _LOCALDIMS
      static ivec prev_udim = {0}, prev_mpidim = {0};
      if (grid && n == N &&
          !memcmp (&UserDims, &prev_udim, sizeof (UserDims)) &&
          !memcmp (&UserMpiDims, &prev_mpidim, sizeof (UserMpiDims)))
        return;
      prev_udim = UserDims;
      prev_mpidim = UserMpiDims;
    #else
      static ivec prev_dim = {0};
      if (grid && n == N && !memcmp (&Dimensions, &prev_dim, sizeof (Dimensions)))
        return;
      prev_dim = Dimensions;
    #endif
      gpu_free_grid();
      init_grid (n);
      grid = realloc (grid, sizeof (GridGPU));
      /* GPUs drivers often generate floating-point exceptions... turn them off */
      disable_fpe (FE_DIVBYZERO|FE_INVALID);
      if (gpu_init_context (&gpu_grid->data)) {
        free_solver_func_add (gpu_free);
        free_solver_func_add (gpu_free_solver);
      }
      gpu_grid->shaders = kh_init (INT);
      realloc_ssbo (field_size());
    }
    
    // overload the default various functions
    
    #define init_grid(n)  gpu_init_grid(n)
    #undef  free_grid
    #define free_grid()   gpu_free_grid()
    
    static External * append_external (External * externals, External ** end, External * g)
    {
      if (externals)
        (*end)->next = g;
      else
        externals = g;
      *end = g;
      (*end)->next = NULL;
      return externals;
    }
    
    static External * merge_external (External * externals, External ** end, External * g,
    				  const ForeachData * loop)
    {
      if (!strcmp (g->name, "ferr") || !strcmp (g->name, "fout"))
        return externals;
      if (g->type == sym_function_declaration || g->type == sym_function_definition) {
        bool boundary = is_boundary_attribute (g);
        for (scalar s in baseblock)
          if (g->name[0] != '.' || (!boundary && s.gpu.index) ||
              (boundary && (s.stencil.io & s_output))) {
    	void * ptr = g->name[0] != '.' ? g->pointer :
    	  *((void **)(((char *) &_attribute[s.i]) + g->nd));
    	if (ptr) {
    	  External * p = _get_function ((long) ptr);
    	  if (!p) {
    	    fprintf (stderr, "%s:%d: GLSL: error: unregistered function pointer '%s'\n",
    		     loop->fname, loop->line, g->name);
    	    return NULL;
    	  }
    	  if (!p->used) {
    	    p->used = true;
    	    for (External * i = p->externals; i && i->name; i++)
    	      externals = merge_external (externals, end, i, loop);
    	    externals = append_external (externals, end, p);
    	  }
    	}
    	if (g->name[0] != '.')
    	    break;
          }
      }
      for (External * i = externals; i; i = i->next)
        if (!strcmp (g->name, i->name)) {
          if (g->global == i->global) // already in the list
            return externals;
          
    #if !_CUDA

    Check whether a local g (resp. i) shadows a global i (resp g).

          if (g->global == 0 && i->global == 1 && is_external_variable (g))
            g->global = 2;
          else if (g->global == 1 && i->global == 0 && is_external_variable (i))
            i->global = 2;
    #endif // !_CUDA
        }
      return append_external (externals, end, g);
    }
    
    static External * merge_externals (External * externals, const ForeachData * loop)
    {
      External * merged = NULL, * end = NULL;
      static External ext[] = {
        { .name = "X0", .type = sym_DOUBLE, .pointer = &X0, .global = 1 },
        { .name = "Y0", .type = sym_DOUBLE, .pointer = &Y0, .global = 1 },
        { .name = "Z0", .type = sym_DOUBLE, .pointer = &Z0, .global = 1 },
        { .name = "L0", .type = sym_DOUBLE, .pointer = &L0, .global = 1 },
        { .name = "N",  .type = sym_INT,    .pointer = &N, .global = 1 },
    #if LAYERS
        { .name = "nl",  .type = sym_INT, .pointer = &nl, .global = 1 },
        { .name = "_layer",  .type = sym_INT, .pointer = &_layer, .global = 1 },
        { .name = ".block", .type = sym_INT, .nd = attroffset (block) },
    #endif
        { .name = NULL }
      };
      static External bc = {
        .name = "apply_bc", .type = sym_function_declaration, .pointer = (void *)(long)apply_bc
      };
    
      for (External * g = externals; g->name; g++) {
        g->used = false;
        if (g->global == 2) g->global = 0;
      }
      foreach_function (f, f->used = false);
      for (External * g = ext; g->name; g++) {
        g->used = false;
        merged = merge_external (merged, &end, g, loop);
      }
    #if 1
      if (loop->dirty) {
        bc.used = false;
        merged = merge_external (merged, &end, &bc, loop);
      }
    #endif
      for (External * g = externals; g->name; g++)
        merged = merge_external (merged, &end, g, loop);
    #if PRINTEXTERNAL  
      for (External * i = merged; i; i = i->next)
        fprintf (stderr, "external %s %d %p %d\n", i->name, i->type, i->pointer, i->global);
    #endif
      if (loop->dirty)
        for (External * g = merged; g; g = g->next)
          if (g->global && !strcmp (g->name, "apply_bc_list"))
    	g->pointer = loop->dirty;
      return merged;
    }
    
    static char * shader_append_func (char * s, const ForeachData * loop, const External * externals)
    {
    #if _CUDA
      char func[strlen(loop->func) + 20];
      sprintf (func, "%s_%d", loop->func, loop->line);
      char * locals = NULL;
      for (const External * g = externals; g; g = g->next)
        if (!g->global && is_external_variable (g)) {
          locals = str_append (locals, "  ");
          locals = external_declaration (locals, g);
          locals = str_append (locals, ";\n");
        }
      if (locals)
        s = str_append (s, "struct _Local {\n", locals, "};\n");
    #if _METAL

    Metal kernel arguments must be explicitly bound to buffer indices and the position of the thread in the grid is obtained through an attribute qualifier.

      s = str_append (s,
                      "kernel\n"
                      "void ", func, "("
                      "device real * _data [[buffer(0)]], "
                      "constant struct _Ctx_ * _ctx_ [[buffer(1)]], "
                      "constant ivec & csOrigin [[buffer(2)]]");
      if (locals)
        s = str_append (s, ", constant struct _Local & _local_ [[buffer(3)]]");
      s = str_append (s, ", uint" xstr(dimension) " _tpig [[thread_position_in_grid]]){\n");
    #else // !_METAL
      s = str_append (s,
    #if _OPENCL
                      "__kernel\n"
    #else // _CUDA
                      "extern \"C\" __global__\n"
    #endif
                      "void ", func, "(", _GLOB_PARAMS0 ", const ivec csOrigin"
                      );
      if (locals)
        s = str_append (s, ", const struct _Local _local_");
      s = str_append (s, "){\n");
    #endif // !_METAL
      return s;
    #else // !_CUDA
      return str_append (s, "void main() {\n");
    #endif
    }
    
    trace
    static Shader * compile_shader (ForeachData * loop,
    				uint32_t hash,
    				const RegionParameters * region,
    				External * externals,
    				const char * kernel)
    {  
      const char * error = strstr (kernel, "@error ");
      if (error) {
        for (const char * s = error + 7; *s != '\n' && *s != '\0'; s++)
          fputc (*s, stderr);
        fputc ('\n', stderr);
        loop->data = NULL;
        return NULL;
      }
    
      External * merged = merge_externals (externals, loop);
      if (!merged) {
        loop->data = NULL;
        return NULL;
      }
      
      int local = false;
      for (const External * g = merged; g; g = g->next) {
        if (g->global == 2)
          local = true;
        if (g->type != sym_SCALAR && g->type != sym_VECTOR && g->type != sym_TENSOR) {
          if (g->reduct && !strchr ("+mM", g->reduct)) {
    	if (loop->first)
    	  fprintf (stderr,
    		   "%s:%d: GLSL: error: unknown reduction operation '%c'\n",
    		   loop->fname, loop->line, g->reduct);
    	return NULL;
          }
          if (g->type == sym_COORD || g->type == sym__COORD || g->type == sym_VEC4) {
    	if (g->reduct) {
    	  if (loop->first)
    	    fprintf (stderr,
    		     "%s:%d: GLSL: error: reductions not implemented for '%s' type\n",
    		     loop->fname, loop->line, type_string (g));
    	  return NULL;	
    	}
          }
          else if (g->type != sym_FLOAT &&
    	       g->type != sym_DOUBLE &&
    	       g->type != sym_INT &&
    	       g->type != sym_LONG &&
    	       g->type != sym_BOOL &&
    	       g->type != sym_enumeration_constant &&
    	       g->type != sym_IVEC &&
    	       g->type != sym_function_declaration &&
    	       g->type != sym_function_definition) {
    	if (loop->first)
    	  fprintf (stderr, "%s:%d: GLSL: error: unknown type %d for '%s'\n",
    		   loop->fname, loop->line, g->type, g->name);
    	return NULL;
          }
        }
      }

    Number of compute shader work groups and groups

    #if dimension == 2
      static const int NWG[2] = {16, 16};
    #elif dimension == 3
      static const int NWG[3] = {8, 8, 8};
    #endif
      unsigned ng[3] = {1, 1, 1};
      unsigned nwg[3] = {1, 1, 1};
      int Nl = region->level > 0 ? 1 << (region->level - 1) : N/Dimensions.x;
      int * dims = &_GPU_DIMS.x;
      if (loop->face || loop->vertex)
        for (int i = 0; i < dimension; i++) {
          int total = Nl*dims[dimension - 1 - i] + 1;
          nwg[i] = total <= NWG[i] + 1 ? total : NWG[i] + 1;
          ng[i] = (total + nwg[i] - 1)/nwg[i];
          assert (nwg[i]*ng[i] >= total);
        }
      else
        for (int i = 0; i < dimension; i++) {
          int total = Nl*dims[dimension - 1 - i];
          nwg[i] = total < NWG[i] ? total : NWG[i];
          ng[i] = (total + nwg[i] - 1)/nwg[i];
          assert (nwg[i]*ng[i] >= total);
        }
     
      char * shader = build_shader (merged, loop, region, nwg);
      if (!shader)
        return NULL;

    main()

      if (local) {
    #if _CUDA
        assert (false); // never true in _CUDA
    #endif
        shader = str_append (shader, "void _loop (");
        for (const External * g = merged; g; g = g->next)
          if (g->global == 2) {
    	shader = str_append (shader, local++ == 1 ? "" : ", ", type_string (g), " ", g->name);
    	if (g->nd) {
    	  int size = list_size (g);
    	  if (size > 0) {
    	    char s[20]; snprintf (s, 19, "%d", size);
    	    shader = str_append (shader, "[", s, "]");
    	  }
    	}
          }
        shader = str_append (shader, ") {\n");
      }
      else
        shader = shader_append_func (shader, loop, merged);
    
      if (!GPUContext.fragment_shader) {
        char d[20];
        snprintf (d, 19, "%d", region->level > 0 ? region->level - 1 : depth());
        shader = str_append (shader,
    #if dimension == 2
    #if _METAL
                             "Point point = {csOrigin.x + int(_tpig.y) + GHOSTS,"
    			 "csOrigin.y + int(_tpig.x) + GHOSTS,", d,
    #elif _OPENCL
                             "Point point = {csOrigin.x + get_group_id(1)*get_local_size(1) + get_local_id(1) + GHOSTS,"
                             "csOrigin.y + get_group_id(0)*get_local_size(0) + get_local_id(0) + GHOSTS,", d,
    #elif _CUDA
                             "Point point = {csOrigin.x + int(blockIdx.y*blockDim.y + threadIdx.y) + GHOSTS,"
    			 "csOrigin.y + int(blockIdx.x*blockDim.x + threadIdx.x) + GHOSTS,", d,
    #else // GLSL
                             "Point point = {csOrigin.x + int(gl_GlobalInvocationID.y) + GHOSTS,"
    			 "csOrigin.y + int(gl_GlobalInvocationID.x) + GHOSTS,", d,
    #endif
    #elif dimension == 3
    #if _METAL
                             "Point point = {csOrigin.x + int(_tpig.z) + GHOSTS,"
    			 "csOrigin.y + int(_tpig.y) + GHOSTS,"
    			 "csOrigin.z + int(_tpig.x) + GHOSTS,", d,
    #elif _OPENCL
                             "Point point = {csOrigin.x + get_group_id(2)*get_local_size(2) + get_local_id(2) + GHOSTS,"
                             "csOrigin.y + get_group_id(1)*get_local_size(1) + get_local_id(1) + GHOSTS,"
                             "csOrigin.z + get_group_id(0)*get_local_size(0) + get_local_id(0) + GHOSTS,", d,
    #elif _CUDA
                             "Point point = {csOrigin.x + int(blockIdx.z*blockDim.z + threadIdx.z) + GHOSTS,"
    			 "csOrigin.y + int(blockIdx.y*blockDim.y + threadIdx.y) + GHOSTS,"
    			 "csOrigin.z + int(blockIdx.x*blockDim.x + threadIdx.x) + GHOSTS,", d,
    #else // GLSL
                             "Point point = {csOrigin.x + int(gl_GlobalInvocationID.z) + GHOSTS,"
    			 "csOrigin.y + int(gl_GlobalInvocationID.y) + GHOSTS,"
    			 "csOrigin.z + int(gl_GlobalInvocationID.x) + GHOSTS,", d,
    #endif
    #endif
    #if MULTIGRID
    #if dimension == 2
    			 ",{(1<<",d,")*Dimensions.x,(1<<",d,")*Dimensions.y}"
    #elif dimension == 3
    			 ",{(1<<",d,")*Dimensions.x,(1<<",d,")*Dimensions.y,(1<<",d,")*Dimensions.z}"
    #endif
    #else
    			 ",N"
    #endif
    #if LAYERS
    			 ",0};\n"
    #else
    			 "};\n"
    #endif
    			 );
      }
    #if dimension == 2
      shader = str_append (shader,
    		       "if (point.i < N*Dimensions.x + GHOSTS",
    		       loop->face || loop->vertex ? " + 1" : "",
    		       " && point.j < N*Dimensions.y + GHOSTS",
    		       loop->face || loop->vertex ? " + 1" : "", ") {\n");
      if (loop->vertex)
        shader = str_append (shader, "  int ig = -1, jg = -1;\n");
    #elif dimension == 3
      shader = str_append (shader,
    		       "if (point.i < N*Dimensions.x + GHOSTS",
    		       loop->face || loop->vertex ? " + 1" : "",
    		       " && point.j < N*Dimensions.y + GHOSTS",
    		       loop->face || loop->vertex ? " + 1" : "",
    		       " && point.k < N*Dimensions.z + GHOSTS",
    		       loop->face || loop->vertex ? " + 1" : "", ") {\n");
      if (loop->vertex)
        shader = str_append (shader, "  int ig = -1, jg = -1, kg = -1;\n");
    #endif
      for (const External * g = merged; g; g = g->next) {
        if (!(g->global || !is_normal_variable (g) || is_external_variable (g)))
          shader = external_write (shader, g, loop, region, nwg, true);    
        if (g->reduct) {
          shader = str_append (shader, type_string (g), " ",
                               g->global == 2 ? "_loc_" : "", g->name, " = ");
          if (g->reduct == '+')
            shader = str_append (shader, "0.;\n");
          else
            shader = str_append (shader, g->global ? "_GLOB_VAL_(" : "_LOC_VAL_(", EXTERNAL_NAME (g), ");\n");
          shader = str_append (shader, "const scalar ", g->name, "_out_ = ");
          shader = write_scalar (shader, g->s);
          shader = str_append (shader, ";\n");
        }
      }
      shader = str_append (shader, kernel);
    #if dimension == 2
      shader = str_append (shader,
    		       "\nif (point.i - GHOSTS < NX && point.j - GHOSTS < NY) {");
    #if _MPI
      if (loop->face || loop->vertex)
        shader = str_append (shader,
    			 "\n  const bool _on_seam = (((_mpi_seam & 1) != 0) && "
    			 "point.i == N*Dimensions.x + GHOSTS) || "
    			 "(((_mpi_seam & 2) != 0) && "
    			 "point.j == N*Dimensions.y + GHOSTS);");
    #endif
    #elif dimension == 3
      shader = str_append (shader,
    		       "\nif (point.i - GHOSTS < NX && "
    		       "point.j - GHOSTS < NY && point.k - GHOSTS < NZ) {");
    #if _MPI
      if (loop->face || loop->vertex)
        shader = str_append (shader,
    			 "\n  const bool _on_seam = (((_mpi_seam & 1) != 0) && "
    			 "point.i == N*Dimensions.x + GHOSTS) || "
    			 "(((_mpi_seam & 2) != 0) && "
    			 "point.j == N*Dimensions.y + GHOSTS) || "
    			 "(((_mpi_seam & 4) != 0) && "
    			 "point.k == N*Dimensions.z + GHOSTS);");
    #endif
    #endif
      for (const External * g = merged; g; g = g->next)
        if (g->reduct) {
    #if _MPI
          if ((loop->face || loop->vertex) && g->reduct == '+')
    	shader = str_append (shader, "\n  val_red_(", g->name,
    			     "_out_) = _on_seam ? 0 : ", g->name, ";");
          else
    #endif
    	shader = str_append (shader, "\n  val_red_(", g->name,
    			     "_out_) = ", g->name, ";");
          scalar s = g->s;
          s.gpu.stored = -1;
        }
      shader = str_append (shader, "\n}",
    		       loop->dirty ? "apply_bc(_GLOB_ point);" : "",
    		       "}}\n");
    
      if (local) {
        shader = shader_append_func (shader, loop, merged);
        shader = str_append (shader, "_loop(");
        local = 1;
        for (const External * g = merged; g; g = g->next)
          if (g->global == 2)
    	shader = str_append (shader, local++ == 1 ? "" : ",", EXTERNAL_NAME (g));
        shader = str_append (shader, ");}\n");
      }
        
      Shader * s = load_shader (shader, hash, loop);
      loop->data = s;
      if (!s)
        return NULL;
    
      finalize_shader (s, externals, merged, ng, nwg, dimension);
      
      return s;
    }
    
    static
    void free_reduction_fields (const External * externals)
    {
      for (const External * g = externals; g; g = g->next)
        if (g->reduct) {
          scalar s = g->s;
          delete ({s});
        }
    }
    
    trace
    static void gpu_cpu_sync (scalar * list, SyncMode mode, const char * fname, int line)
    {
    #if PRINTCOPYGPU
      bool copy = false;
    #endif
      for (scalar s in list)
        if (((s.stencil.io & s_input) || (s.stencil.io & s_output)) &&
            ((mode == GPU_READ && s.gpu.stored < 0) ||
             (mode == GPU_WRITE && s.gpu.stored > 0))) {
          if (s.gpu.stored > 0 && !(s.stencil.bc & s_centered))
            boundary ({s});
    #if PRINTCOPYGPU
          if (!copy) {
    	fprintf (stderr, "%s:%d: %s ", fname, line,
    		 mode == GPU_READ ? "importing" : "exporting");
    	copy = true;
    	gpu_cpu_sync_scalar (s.i, s.block, grid_data(), field_size(), mode);
            fprintf (stderr, "{%s", s.name);
          }
          else {
    	gpu_cpu_sync_scalar (s.i, s.block, grid_data(), field_size(), mode);
            fprintf (stderr, ",%s", s.name);
          }
    #else
          gpu_cpu_sync_scalar (s.i, s.block, grid_data(), field_size(), mode);
    #endif
          if (mode == GPU_WRITE)
    	s.gpu.mpi_clean = 0;
          s.gpu.stored = 0;
        }
    #if PRINTCOPYGPU
      if (copy)
        fprintf (stderr, "} %s GPU\n", mode == GPU_READ ? "from" : "to");
    #endif
    }
    
    #if _MPI && _CUDA
    void mpi_boundary_level_gpu (scalar * list, int level);

    Apply physical GPU boundary conditions (if not yet applied on GPU) and exchange MPI ghost layers at lvl (< 0 for leaves) for list.

    static void gpu_mpi_sync_boundaries (scalar * list, int lvl, bool mark_gpu)
    {
      if (!list)
        return;
      scalar * need_phys = NULL;
      for (scalar s in list) {
        if (!(s.stencil.bc & (s_centered | s_face | s_phys_gpu)))
          s.gpu.mpi_clean = 0;
        if (!(s.stencil.bc & s_phys_gpu))
          need_phys = list_append (need_phys, s);
      }
      if (need_phys) {
        int nvar = datasize/sizeof(real);
        _Attributes backup[nvar];
        memcpy (backup, _attribute, nvar*sizeof(_Attributes));
        if (lvl < 0 || mark_gpu)
          foreach (gpu)
    	for (scalar s in need_phys)
    	  s[] = s[];
        if (lvl >= 0 && (!mark_gpu || lvl != depth()))
          foreach_level (lvl, gpu)
    	for (scalar s in need_phys)
    	  s[] = s[];
        memcpy (_attribute, backup, nvar*sizeof(_Attributes));
        for (scalar s in need_phys) {
          s.stencil.bc |= s_phys_gpu;
          s.gpu.stored = -1;
        }
        free (need_phys);
      }
      mpi_boundary_level_gpu (list, lvl);
      for (scalar s in list) {
        if (lvl < 0)
          s.stencil.bc |= s_centered | s_face | s_phys_gpu;
        if (mark_gpu)
          s.gpu.stored = -1;
      }
    }
    #endif
    
    trace
    static Shader * setup_shader (ForeachData * loop, const RegionParameters * region,
    			      External * externals,
    			      const char * kernel)
    {

    We will directly apply boundary conditions to fields marked ‘dirty’ by automatic stencils.

      apply_bc_list = loop->dirty;
      for (scalar s in loop->dirty) {

    We also apply boundary stencils so that input/output are also set properly for boundary conditions which may use external fields.

        for (int b = 0; b < nboundary; b++)
          if (s.boundary_stencil[b])
            s.boundary_stencil[b] ((Point){0}, (Point){0}, s, NULL);

    We make sure all fields marked dirty are also outputs.

        if (!(s.stencil.io & s_output))
          s.stencil.io |= s_output;
      }
    
      for (scalar s in baseblock)
        s.gpu.index = 0;
      int index = 1;
      for (scalar s in baseblock)
        if (((s.stencil.io & s_input) || (s.stencil.io & s_output)) && !s.gpu.index) {
    #if PRINTBOUNDARY
          fprintf (stderr, "%s:%d: %s:%s%s index: %d\n",
                   loop->fname, loop->line, 
                   s.name, (s.stencil.io & s_input) ? " input" : "",
                   (s.stencil.io & s_output) ? " output" : "", index);
    #endif
          if (s.v.x.i == -1) // scalar
    	s.gpu.index = index++;
          else { // vector
    	vector v = s.v;
    	for (scalar c in {v})
    	  if (!c.gpu.index)
    	    c.gpu.index = index++;
          }
        }
      for (scalar s in loop->dirty) {
    #if PRINTBOUNDARY
        fprintf (stderr, "%s:%d: dirty: %s:%s%s index: %d\n",
                 loop->fname, loop->line, s.name,
                 (s.stencil.io & s_input) ? " input" : "",
                 (s.stencil.io & s_output) ? " output" : "",
                 s.gpu.index);
    #endif
    #if _MPI
        int wmask = mpi_wall_mask ();
        s.boundary_left   = (wmask & 1) ? s.boundary[left]   : NULL;
        s.boundary_right  = (wmask & 2) ? s.boundary[right]  : NULL;
        s.boundary_bottom = (wmask & 4) ? s.boundary[bottom] : NULL;
        s.boundary_top    = (wmask & 8) ? s.boundary[top]    : NULL;
    # if dimension == 3
        s.boundary_back   = (wmask & 16) ? s.boundary[back]  : NULL;
        s.boundary_front  = (wmask & 32) ? s.boundary[front] : NULL;
    # endif
    #else
        s.boundary_left   = s.boundary[left];
        s.boundary_right  = s.boundary[right];
        s.boundary_top    = s.boundary[top];
        s.boundary_bottom = s.boundary[bottom];
    # if dimension == 3
        s.boundary_front  = s.boundary[front];
        s.boundary_back   = s.boundary[back];
    # endif
    #endif
      }

    Allocate reduction fields

      for (External * g = externals; g && g->name; g++)
        if (g->reduct) {
          scalar s = new scalar;
          s.stencil.io |= s_output;
          g->s = s;
    #if PRINTREDUCT
          fprintf (stderr, "%s:%d: new reduction field %d for %s\n",
    	       loop->fname, loop->line, s.i, g->name);
    #endif
        }

    Reuse or compile a new shader

      Shader * shader;
      uint32_t hash = hash_shader (externals, loop, region, kernel);
      assert (gpu_grid->shaders);
      khiter_t k = kh_get (INT, gpu_grid->shaders, hash);
      if (k != kh_end (gpu_grid->shaders))
        shader = kh_value (gpu_grid->shaders, k);
      else {
        shader = compile_shader (loop, hash, region, externals, kernel);  
        if (!shader) {
          free_reduction_fields (externals);
          return NULL;
        }
      }
    
      gpu_cpu_sync (baseblock, GPU_WRITE, loop->fname, loop->line);

    Apply boundary conditions

    This can be required if boundary conditions have been modified between loops.

      scalar * listc = NULL;
      for (scalar s in loop->listc)
        if (!(s.stencil.bc & s_centered))
          listc = list_prepend (listc, s);
      scalar * listf_x = NULL, * listf_y = NULL;
    #if dimension == 3
      scalar * listf_z = NULL;
    #endif
      foreach_dimension()
        for (scalar s in loop->listf.x)
          if (!(s.stencil.bc & s_face))
    	listf_x = list_prepend (listf_x, s);
      if (listc || listf_x || listf_y
    #if dimension == 3
          || listf_z
    #endif
          ) {
    #if PRINTBC
        fprintf (stderr, "%s:%d: applying BCs for", loop->fname, loop->line);
        for (scalar s in listc)
          fprintf (stderr, " %s", s.name);
        foreach_dimension()
          for (scalar s in listf_x)
    	fprintf (stderr, " %s", s.name);
        fputc ('\n', stderr);
    #endif
    #if _MPI && _CUDA
        if (listc) {
          gpu_mpi_sync_boundaries (listc,
    			       region->level > 0 ? region->level - 1 : -1,
    			       true);
          free (listc);
        }
        foreach_dimension()
          free (listf_x);
        apply_bc_list = loop->dirty;
    #else
        int nvar = datasize/sizeof(real);
        _Attributes backup[nvar];
        memcpy (backup, _attribute, nvar*sizeof(_Attributes));
        foreach (gpu) {
          for (scalar s in listc)
    	s[] = s[]; // does nothing but ensures that BCs are applied at the end of the loop
          foreach_dimension()
    	for (scalar s in listf_x)
    	  s[] = s[];
        }
        memcpy (_attribute, backup, nvar*sizeof(_Attributes));
        for (scalar s in listc) {
          s.stencil.bc |= s_centered;
          s.gpu.stored = -1;
        }
        free (listc); 
        foreach_dimension() {
          for (scalar s in listf_x) {
            s.stencil.bc |= s_face;
    	s.gpu.stored = -1;
          }
          free (listf_x);
        }
        apply_bc_list = loop->dirty;
    #endif
      }
      
      post_setup_shader (shader, externals);
      
      return shader;
    }
    
    static bool doloop_on_gpu (ForeachData * loop, const RegionParameters * region,
    			   External * externals,
    			   const char * kernel)
    {
      Shader * shader = setup_shader (loop, region, externals, kernel);
      if (!shader)
        return false;

    Render

      int Nl = run_shader (shader, region);

    Perform reductions and cleanup

      bool nreductions = false;
      for (const External * g = externals; g && g->name; g++)
        if (g->reduct) {
          nreductions = true;
          break;
        }
      if (nreductions)
        tracing ("gpu_reduction", loop->fname, loop->line);
      for (const External * g = externals; g && g->name; g++)
        if (g->reduct) {
          scalar s = g->s;
          size_t nb = 1;
          int * dims = &_GPU_DIMS.x;
          for (int d = 0; d < dimension; d++)
            nb *= (size_t)(Nl*dims[d] + ((loop->face & (1 << d)) || loop->vertex));
          double result = gpu_reduction (field_offset(s, region->level), g->reduct, region,
                                         gpu_grid->data,
    				     nb, dimension);
    #if _MPI
          {
    	MPI_Op mop = (g->reduct == '+' ? MPI_SUM :
    		      g->reduct == 'm' ? MPI_MIN : MPI_MAX);
    	MPI_Allreduce (MPI_IN_PLACE, &result, 1, MPI_DOUBLE, mop,
    		       MPI_COMM_WORLD);
          }
    #endif
    #if PRINTREDUCT
          fprintf (stderr, "%s:%d: %s %c %g\n",
    	       loop->fname, loop->line, g->name, g->reduct, result);
    #endif
          if (g->reduct == '+') {
            if (g->type == sym_DOUBLE) *((double *)g->pointer) += result;
            else if (g->type == sym_FLOAT) *((float *)g->pointer) += result;
            else if (g->type == sym_INT) *((int *)g->pointer) += result;
            else
              assert (false);
          }
          else {
            if (g->type == sym_DOUBLE) *((double *)g->pointer) = result;
            else if (g->type == sym_FLOAT) *((float *)g->pointer) = result;
            else if (g->type == sym_INT) *((int *)g->pointer) = result;
            else
              assert (false);
          }
          delete ({s});
        }
      if (nreductions)
        end_tracing ("gpu_reduction", loop->fname, loop->line);
    
      return true;
    }
    
    #ifndef STRICT_GPU
    # define STRICT_GPU 0
    #endif
    
    static void gpu_periodic_face_bc (scalar * dirty, int level)
    {
      static bool in_bc = false;
      if (in_bc)
        return;
      in_bc = true;
      for (scalar s in dirty)
        if (s.face) {
          int dir = (Period.x && s.i == s.v.x.i ? 1 :
    		 Period.y && s.i == s.v.y.i ? 2 :
    #if dimension == 3
    		 Period.z && s.i == s.v.z.i ? 3 :
    #endif
    		 0);
          if (!dir)
    	continue;
    #ifdef shift_level // multigrid
          if (level > 0) {
    	int l = level - 1;
    	foreach_level (l) {
    	  if (dir == 1 && point.i == GHOSTS)
    	    foreach_blockf (s)
    	      s[bc_period_x,0,0] = s[];
    	  else if (dir == 2 && point.j == GHOSTS)
    	    foreach_blockf (s)
    	      s[0,bc_period_y,0] = s[];
    # if dimension == 3
    	  else if (dir == 3 && point.k == GHOSTS)
    	    foreach_blockf (s)
    	      s[0,0,bc_period_z] = s[];
    # endif
    	}
          }
          else
    #endif // shift_level
    	foreach () {
    	  if (dir == 1 && point.i == GHOSTS)
    	    foreach_blockf (s)
    	      s[bc_period_x,0,0] = s[];
    	  else if (dir == 2 && point.j == GHOSTS)
    	    foreach_blockf (s)
    	      s[0,bc_period_y,0] = s[];
    #if dimension == 3
    	  else if (dir == 3 && point.k == GHOSTS)
    	    foreach_blockf (s)
    	      s[0,0,bc_period_z] = s[];
    #endif
    	}
        }
      in_bc = false;
    }
    
    bool gpu_end_stencil (ForeachData * loop,
    		      const RegionParameters * region,
    		      External * externals,
    		      const char * kernel)
    {
      bool on_gpu = ((loop->parallel == 1 && !on_cpu) || loop->parallel == 3) &&
        (loop->first || loop->data);
      if (on_gpu) {
        on_gpu = doloop_on_gpu (loop, region, externals, kernel);
        if (!on_gpu) {
          fprintf (stderr, "%s:%d: %s: foreach() done on CPU (see "
    #if _METAL
                   "Metal"
    #elif _OPENCL
                   "OpenCL"
    #elif _CUDA
                   "CUDA"
    #else
                   "GLSL"
    #endif
                   " errors above)\n",
    	       loop->fname, loop->line,
    	       (loop->parallel == 3 || STRICT_GPU) ? "error" : "warning");
          if (loop->parallel == 3 || STRICT_GPU) // must run on GPU
    	exit (1);
          loop->data = NULL;
        }
      }
      scalar * periodic_dirty = NULL;
      if (on_gpu) {
        // do not apply BCs on CPU
        free (loop->listc), loop->listc = NULL;
        foreach_dimension()
          free (loop->listf.x), loop->listf.x = NULL;
    #if _MPI
        int lvl = region->level > 0 ? region->level - 1 : depth();
        for (scalar s in loop->dirty) {
          if (region->level > 0)
    	s.gpu.mpi_clean &= ~(1u << lvl);
          else
    	s.gpu.mpi_clean = 0;
          s.stencil.bc = s_phys_gpu;
        }
        for (scalar s in baseblock)
          if (s.stencil.io & s_output) {
    	if (region->level > 0)
    	  s.gpu.mpi_clean &= ~(1u << lvl);
    	else
    	  s.gpu.mpi_clean = 0;
    	s.stencil.bc = s_phys_gpu;
          }
        free (loop->dirty), loop->dirty = NULL;
    #else
        for (scalar s in loop->dirty)
          s.stencil.bc = s_centered|s_face;
        periodic_dirty = loop->dirty, loop->dirty = NULL;
    #endif
      }
      else {
    #if _MPI && _CUDA
        if (loop->listc) {
          int lvl = region->level > 0 ? region->level - 1 : -1;
          scalar * gpu_list = NULL, * cpu_list = NULL;
          for (scalar s in loop->listc) {
    	if (s.gpu.stored > 0)
    	  cpu_list = list_append (cpu_list, s);
    	else
    	  gpu_list = list_append (gpu_list, s);
          }
          if (gpu_list) {
    	gpu_mpi_sync_boundaries (gpu_list, lvl, false);
    	free (gpu_list);
          }
          free (loop->listc);
          loop->listc = cpu_list;
        }
    #endif
        gpu_cpu_sync (baseblock, GPU_READ, loop->fname, loop->line);
        boundary_stencil (loop);
    #if _MPI
        for (scalar s in baseblock)
          if (s.stencil.io & s_output)
    	s.gpu.mpi_clean = 0;
    #endif
      }
      
      for (scalar s in baseblock)
        if (s.stencil.io & s_output)
          s.gpu.stored = on_gpu ? -1 : 1;
    
      if (periodic_dirty) {
        gpu_periodic_face_bc (periodic_dirty, region->level);
        free (periodic_dirty);
      }
    
      return on_gpu && loop->parallel != 3;
    }