| "slab.cc": "// TODO:\n// Provide facilities for dim > 3\n// Set up the slab exchange information in advance\n// Slab in several stages\n// Test slabbing without MPI\n// Allow not setting the ghost zones\n// Allow not using / not setting the boundaries\n// Allow different numbers of ghost zones at the lower and upper boundary\n\n// Print debug information?\n#undef DEBUG\n\n// Perform expensive self-checks?\n#undef CHECK\n\n// Omit all self-checks? (Overrides CHECK)\n#undef NDEBUG\n\n// Byte value for poison checks: use 255 for nan, or e.g. 113 for a\n// large value\n#define POISON_VALUE 254\n\n#include <algorithm>\n#include <cassert>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <limits>\n#include <vector>\n\n#include <cctk.h>\n#include <cctk_Arguments.h>\n#include <cctk_DefineThorn.h>\n#include <cctk_Parameters.h>\n#include <util_ErrorCodes.h>\n#include <util_Table.h>\n\n#include <loopcontrol.h>\n\n#ifdef CCTK_MPI\n#include <mpi.h>\n#define HAVE_MPI 1\n#else\n#define HAVE_MPI 0\n#endif\n\n#include \"slab.h\"\n\nusing namespace std;\n\n#ifdef DEBUG\n#define ifdebug\n#else\n#define ifdebug while (0)\n#endif\n\n#ifdef CHECK\n#define ifcheck\n#else\n#define ifcheck while (0)\n#endif\n\n#ifdef NDEBUG\n#define check(x) ((x) ? 0 : CCTK_WARN(0, \"internal error\"))\n#else\n#define check(x) assert(x)\n#endif\n\nstatic int timer_init = -1;\nstatic int timer_apply = -1;\nstatic int timer_copy_in = -1;\nstatic int timer_copy_in_noxpose = -1;\nstatic int timer_copy_in_xposexy = -1;\nstatic int timer_copy_in_xposegeneral = -1;\nstatic int timer_copy_in_general = -1;\nstatic int timer_xfer = -1;\nstatic int timer_copy_back = -1;\nstatic int timer_copy_back_noflip = -1;\nstatic int timer_copy_back_flipx = -1;\nstatic int timer_copy_back_flipy = -1;\nstatic int timer_copy_back_flipxy = -1;\nstatic int timer_copy_back_flipgeneral = -1;\nstatic int timer_copy_back_general = -1;\n\nextern \"C\" void Slab_InitTimers(CCTK_ARGUMENTS) {\n DECLARE_CCTK_ARGUMENTS_Slab_InitTimers;\n timer_init = CCTK_TimerCreate(\"Slab/init\");\n timer_apply = CCTK_TimerCreate(\"Slab/apply\");\n timer_copy_in = CCTK_TimerCreate(\"Slab/copy_in\");\n timer_copy_in_noxpose = CCTK_TimerCreate(\"Slab/copy_in_noxpose\");\n timer_copy_in_xposexy = CCTK_TimerCreate(\"Slab/copy_in_xposexy\");\n timer_copy_in_xposegeneral = CCTK_TimerCreate(\"Slab/copy_in_xposegeneral\");\n timer_copy_in_general = CCTK_TimerCreate(\"Slab/copy_in_general\");\n timer_xfer = CCTK_TimerCreate(\"Slab/xfer\");\n timer_copy_back = CCTK_TimerCreate(\"Slab/copy_back\");\n timer_copy_back_noflip = CCTK_TimerCreate(\"Slab/copy_back_noflip\");\n timer_copy_back_flipx = CCTK_TimerCreate(\"Slab/copy_back_flipx\");\n timer_copy_back_flipy = CCTK_TimerCreate(\"Slab/copy_back_flipy\");\n timer_copy_back_flipxy = CCTK_TimerCreate(\"Slab/copy_back_flipxy\");\n timer_copy_back_flipgeneral = CCTK_TimerCreate(\"Slab/copy_back_flipgeneral\");\n timer_copy_back_general = CCTK_TimerCreate(\"Slab/copy_back_general\");\n}\n\nextern \"C\" int Slab_PrintTimers() {\n CCTK_TimerPrintDataI(timer_init, -1);\n CCTK_TimerPrintDataI(timer_apply, -1);\n CCTK_TimerPrintDataI(timer_copy_in, -1);\n CCTK_TimerPrintDataI(timer_copy_in_noxpose, -1);\n CCTK_TimerPrintDataI(timer_copy_in_xposexy, -1);\n CCTK_TimerPrintDataI(timer_copy_in_xposegeneral, -1);\n CCTK_TimerPrintDataI(timer_copy_in_general, -1);\n CCTK_TimerPrintDataI(timer_xfer, -1);\n CCTK_TimerPrintDataI(timer_copy_back, -1);\n CCTK_TimerPrintDataI(timer_copy_back_noflip, -1);\n CCTK_TimerPrintDataI(timer_copy_back_flipx, -1);\n CCTK_TimerPrintDataI(timer_copy_back_flipy, -1);\n CCTK_TimerPrintDataI(timer_copy_back_flipxy, -1);\n CCTK_TimerPrintDataI(timer_copy_back_flipgeneral, -1);\n CCTK_TimerPrintDataI(timer_copy_back_general, -1);\n return 0;\n}\n\n// Find out which driver to use\n#ifdef CCTK_MPI\n#if defined CARPET_CARPET\n#include \"Carpet/Carpet/src/carpet_public.h\"\n#endif\n#if defined CACTUSPUGH_PUGH\n#include \"CactusPUGH/PUGH/src/include/pugh.h\"\n#endif\n#endif\n\n#ifdef CCTK_MPI\n// Determine MPI type sizes\n\n#define CACTUS_MPI_BYTE MPI_CHAR\n\n#define CACTUS_MPI_INT1 MPI_CHAR\n\n#if SIZEOF_SHORT_INT == 2\n#define CACTUS_MPI_INT2 MPI_SHORT\n#elif SIZEOF_INT == 2\n#define CACTUS_MPI_INT2 MPI_INT\n#elif SIZEOF_LONG_INT == 2\n#define CACTUS_MPI_INT2 MPI_LONG\n#elif SIZEOF_LONG_LONG == 2\n#define CACTUS_MPI_INT2 MPI_LONG_LONG_INT\n#endif\n\n#if SIZEOF_SHORT_INT == 4\n#define CACTUS_MPI_INT4 MPI_SHORT\n#elif SIZEOF_INT == 4\n#define CACTUS_MPI_INT4 MPI_INT\n#elif SIZEOF_LONG_INT == 4\n#define CACTUS_MPI_INT4 MPI_LONG\n#elif SIZEOF_LONG_LONG == 4\n#define CACTUS_MPI_INT4 MPI_LONG_LONG_INT\n#endif\n\n#if SIZEOF_SHORT_INT == 8\n#define CACTUS_MPI_INT8 MPI_SHORT\n#elif SIZEOF_INT == 8\n#define CACTUS_MPI_INT8 MPI_INT\n#elif SIZEOF_LONG_INT == 8\n#define CACTUS_MPI_INT8 MPI_LONG\n#elif SIZEOF_LONG_LONG == 8\n#define CACTUS_MPI_INT8 MPI_LONG_LONG_INT\n#endif\n\n#if SIZEOF_SHORT_INT == 16\n#define CACTUS_MPI_INT16 MPI_SHORT\n#elif SIZEOF_INT == 16\n#define CACTUS_MPI_INT16 MPI_INT\n#elif SIZEOF_LONG_INT == 16\n#define CACTUS_MPI_INT16 MPI_LONG\n#elif SIZEOF_LONG_LONG == 16\n#define CACTUS_MPI_INT16 MPI_LONG_LONG_INT\n#endif\n\n#if SIZEOF_FLOAT == 4\n#define CACTUS_MPI_REAL4 MPI_FLOAT\n#elif SIZEOF_DOUBLE == 4\n#define CACTUS_MPI_REAL4 MPI_DOUBLE\n#elif SIZEOF_LONG_DOUBLE == 4\n#define CACTUS_MPI_REAL4 MPI_LONG_DOUBLE\n#endif\n\n#if SIZEOF_FLOAT == 8\n#define CACTUS_MPI_REAL8 MPI_FLOAT\n#elif SIZEOF_DOUBLE == 8\n#define CACTUS_MPI_REAL8 MPI_DOUBLE\n#elif SIZEOF_LONG_DOUBLE == 8\n#define CACTUS_MPI_REAL8 MPI_LONG_DOUBLE\n#endif\n\n#if SIZEOF_FLOAT == 16\n#define CACTUS_MPI_REAL16 MPI_FLOAT\n#elif SIZEOF_DOUBLE == 16\n#define CACTUS_MPI_REAL16 MPI_DOUBLE\n#elif SIZEOF_LONG_DOUBLE == 16\n#define CACTUS_MPI_REAL16 MPI_LONG_DOUBLE\n#endif\n\nstatic MPI_Datatype CACTUS_MPI_COMPLEX8;\nstatic MPI_Datatype CACTUS_MPI_COMPLEX16;\nstatic MPI_Datatype CACTUS_MPI_COMPLEX32;\n\n#endif\n\n// Replace MPI functions if MPI is disabled\n#ifndef CCTK_MPI\n\ntypedef int MPI_Comm;\n\ntypedef enum {\n CACTUS_MPI_BYTE = CCTK_VARIABLE_BYTE,\n CACTUS_MPI_INT = CCTK_VARIABLE_INT,\n CACTUS_MPI_INT1 = CCTK_VARIABLE_INT1,\n CACTUS_MPI_INT2 = CCTK_VARIABLE_INT2,\n CACTUS_MPI_INT4 = CCTK_VARIABLE_INT4,\n CACTUS_MPI_INT8 = CCTK_VARIABLE_INT8,\n CACTUS_MPI_INT16 = CCTK_VARIABLE_INT16,\n CACTUS_MPI_REAL = CCTK_VARIABLE_REAL,\n CACTUS_MPI_REAL4 = CCTK_VARIABLE_REAL4,\n CACTUS_MPI_REAL8 = CCTK_VARIABLE_REAL8,\n CACTUS_MPI_REAL16 = CCTK_VARIABLE_REAL16,\n CACTUS_MPI_COMPLEX = CCTK_VARIABLE_COMPLEX,\n CACTUS_MPI_COMPLEX8 = CCTK_VARIABLE_COMPLEX8,\n CACTUS_MPI_COMPLEX16 = CCTK_VARIABLE_COMPLEX16,\n CACTUS_MPI_COMPLEX32 = CCTK_VARIABLE_COMPLEX32\n} MPI_Datatype;\n\nstatic MPI_Datatype MPI_INT;\n\ntypedef enum { MPI_MIN, MPI_MAX } MPI_Op;\n\nstatic int MPI_Barrier(MPI_Comm comm) { return 0; }\n\nstatic int MPI_Bcast(void *buffer, int count, MPI_Datatype datatype, int root,\n MPI_Comm comm) {\n assert(buffer);\n assert(count >= 0);\n assert(root == 0);\n return 0;\n}\n\nstatic int MPI_Comm_size(MPI_Comm comm, int *size) {\n *size = 1;\n return 0;\n}\n\nstatic int MPI_Comm_rank(MPI_Comm comm, int *rank) {\n *rank = 0;\n return 0;\n}\n\nstatic int MPI_Allgather(void *sendbuf, int sendcnt, int sendtype,\n void *recvbuf, int recvcnt, int recvtype,\n MPI_Comm comm) {\n int recvsize;\n assert(sendbuf);\n assert(recvbuf);\n assert(sendcnt == recvcnt);\n assert(recvcnt >= 0);\n assert(sendtype == recvtype);\n recvsize = CCTK_VarTypeSize(recvtype);\n assert(recvsize > 0);\n memcpy(recvbuf, sendbuf, recvcnt * recvsize);\n return 0;\n}\n\nstatic int MPI_Alltoall(void *sendbuf, int sendcnt, int sendtype, void *recvbuf,\n int recvcnt, int recvtype, MPI_Comm comm) {\n int recvsize;\n assert(sendbuf);\n assert(recvbuf);\n assert(sendcnt == recvcnt);\n assert(recvcnt >= 0);\n assert(sendtype == recvtype);\n recvsize = CCTK_VarTypeSize(recvtype);\n assert(recvsize > 0);\n memcpy(recvbuf, sendbuf, recvcnt * recvsize);\n return 0;\n}\n\nstatic int MPI_Alltoallv(void *sendbuf, int *sendcnt, int *sendoff,\n int sendtype, void *recvbuf, int *recvcnt,\n int *recvoff, int recvtype, MPI_Comm comm) {\n int recvsize;\n assert(sendbuf);\n assert(recvbuf);\n assert(sendcnt);\n assert(recvcnt);\n assert(*sendcnt == *recvcnt);\n assert(*recvcnt >= 0);\n assert(sendoff);\n assert(recvoff);\n assert(*sendoff == 0);\n assert(*recvoff == 0);\n assert(sendtype == recvtype);\n recvsize = CCTK_VarTypeSize(recvtype);\n assert(recvsize > 0);\n memcpy(recvbuf, sendbuf, *recvcnt * recvsize);\n return 0;\n}\n\nstatic int MPI_Allreduce(void *sendbuf, void *recvbuf, int count,\n MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {\n int recvsize;\n assert(sendbuf);\n assert(recvbuf);\n assert(count >= 0);\n recvsize = CCTK_VarTypeSize(datatype);\n assert(recvsize > 0);\n memcpy(recvbuf, sendbuf, count * recvsize);\n return 0;\n}\n\ntypedef int MPI_Request; // dummy\ntypedef int MPI_Status; // dummy\n#define MPI_REQUEST_NULL 0 // dummy\n#define MPI_STATUSES_IGNORE 0 // dummy\n\nstatic int MPI_Irecv(void *buf, int count, MPI_Datatype datatype, int source,\n int tag, MPI_Comm comm, MPI_Request *request) {\n abort();\n}\n\nstatic int MPI_Isend(void *buf, int count, MPI_Datatype datatype, int dest,\n int tag, MPI_Comm comm, MPI_Request *request) {\n abort();\n}\n\nstatic int MPI_Waitall(int count, MPI_Request *array_of_requests,\n MPI_Status *array_of_statuses) {\n abort();\n}\n\n#endif\n\n// Get the MPI COMM_WOLRD communicator from the driver\nstatic MPI_Comm get_mpi_comm(cGH const *restrict const cctkGH) {\n#ifdef CCTK_MPI\n if (CCTK_IsFunctionAliased(\"GetMPICommWorld\")) {\n return *(MPI_Comm const *)GetMPICommWorld(cctkGH);\n }\n#if defined CACTUSPUGH_PUGH\n {\n static int PUGH_active = -1;\n if (PUGH_active == -1)\n PUGH_active = CCTK_IsThornActive(\"PUGH\");\n assert(PUGH_active >= 0);\n if (PUGH_active)\n return PUGH_pGH(cctkGH)->PUGH_COMM_WORLD;\n }\n#endif\n return MPI_COMM_WORLD;\n#else\n return 0;\n#endif\n}\n\n// Initialise the MPI datatypes for complex variables\nextern \"C\" int Slab_InitMPIDatatypes() {\n#ifdef CCTK_MPI\n#ifdef HAVE_CCTK_REAL4\n assert(CACTUS_MPI_REAL4 != MPI_DATATYPE_NULL);\n MPI_Type_contiguous(2, CACTUS_MPI_REAL4, &CACTUS_MPI_COMPLEX8);\n MPI_Type_commit(&CACTUS_MPI_COMPLEX8);\n#endif\n#ifdef HAVE_CCTK_REAL8\n assert(CACTUS_MPI_REAL8 != MPI_DATATYPE_NULL);\n MPI_Type_contiguous(2, CACTUS_MPI_REAL8, &CACTUS_MPI_COMPLEX16);\n MPI_Type_commit(&CACTUS_MPI_COMPLEX16);\n#endif\n#ifdef HAVE_CCTK_REAL16\n if (CACTUS_MPI_REAL16 != MPI_DATATYPE_NULL) {\n MPI_Type_contiguous(2, CACTUS_MPI_REAL16, &CACTUS_MPI_COMPLEX32);\n MPI_Type_commit(&CACTUS_MPI_COMPLEX32);\n } else {\n // CCTK_REAL16 is not supported by MPI\n CCTK_WARN(CCTK_WARN_ALERT, \"CCTK_REAL16 support is enabled in Cactus, but \"\n \"is not supported by MPI. All MPI operations \"\n \"with this datatype will fail.\");\n CACTUS_MPI_COMPLEX32 = MPI_DATATYPE_NULL;\n }\n#endif\n#endif\n\n#ifndef CCTK_MPI\n switch (sizeof(int)) {\n#ifdef HAVE_CCTK_INT1\n case sizeof(CCTK_INT1):\n MPI_INT = CACTUS_MPI_INT1;\n break;\n#endif\n#ifdef HAVE_CCTK_INT2\n case sizeof(CCTK_INT2):\n MPI_INT = CACTUS_MPI_INT2;\n break;\n#endif\n#ifdef HAVE_CCTK_INT4\n case sizeof(CCTK_INT4):\n MPI_INT = CACTUS_MPI_INT4;\n break;\n#endif\n#ifdef HAVE_CCTK_INT8\n case sizeof(CCTK_INT8):\n MPI_INT = CACTUS_MPI_INT8;\n break;\n#endif\n default:\n CCTK_BUILTIN_UNREACHABLE();\n }\n#endif\n\n return 0;\n}\n\n// Normalise a Cactus datatype\nstatic int normal_type(int cactustype) {\n switch (cactustype) {\n case CCTK_VARIABLE_INT:\n#ifdef CCTK_INTEGER_PRECISION_1\n return CCTK_VARIABLE_INT1;\n#endif\n#ifdef CCTK_INTEGER_PRECISION_2\n return CCTK_VARIABLE_INT2;\n#endif\n#ifdef CCTK_INTEGER_PRECISION_4\n return CCTK_VARIABLE_INT4;\n#endif\n#ifdef CCTK_INTEGER_PRECISION_8\n return CCTK_VARIABLE_INT8;\n#endif\n#ifdef CCTK_INTEGER_PRECISION_16\n return CCTK_VARIABLE_INT16;\n#endif\n CCTK_BUILTIN_UNREACHABLE();\n case CCTK_VARIABLE_REAL:\n#ifdef CCTK_REAL_PRECISION_4\n return CCTK_VARIABLE_REAL4;\n#endif\n#ifdef CCTK_REAL_PRECISION_8\n return CCTK_VARIABLE_REAL8;\n#endif\n#ifdef CCTK_REAL_PRECISION_16\n return CCTK_VARIABLE_REAL16;\n#endif\n CCTK_BUILTIN_UNREACHABLE();\n case CCTK_VARIABLE_COMPLEX:\n#ifdef CCTK_REAL_PRECISION_4\n return CCTK_VARIABLE_COMPLEX8;\n#endif\n#ifdef CCTK_REAL_PRECISION_8\n return CCTK_VARIABLE_COMPLEX16;\n#endif\n#ifdef CCTK_REAL_PRECISION_16\n return CCTK_VARIABLE_COMPLEX32;\n#endif\n CCTK_BUILTIN_UNREACHABLE();\n }\n return cactustype;\n}\n\n// Find the MPI datatype corresponding to a Cactus datatype\nstatic MPI_Datatype mpi_type(int const cactustype) {\n int const normaltype = normal_type(cactustype);\n switch (normaltype) {\n case CCTK_VARIABLE_BYTE:\n return CACTUS_MPI_BYTE;\n#ifdef HAVE_CCTK_INT1\n case CCTK_VARIABLE_INT1:\n return CACTUS_MPI_INT1;\n#endif\n#ifdef HAVE_CCTK_INT2\n case CCTK_VARIABLE_INT2:\n return CACTUS_MPI_INT2;\n#endif\n#ifdef HAVE_CCTK_INT4\n case CCTK_VARIABLE_INT4:\n return CACTUS_MPI_INT4;\n#endif\n#ifdef HAVE_CCTK_INT8\n case CCTK_VARIABLE_INT8:\n return CACTUS_MPI_INT8;\n#endif\n// #ifdef HAVE_CCTK_INT16\n// case CCTK_VARIABLE_INT16: return CACTUS_MPI_INT16;\n// #endif\n#ifdef HAVE_CCTK_REAL4\n case CCTK_VARIABLE_REAL4:\n return CACTUS_MPI_REAL4;\n case CCTK_VARIABLE_COMPLEX8:\n return CACTUS_MPI_COMPLEX8;\n#endif\n#ifdef HAVE_CCTK_REAL8\n case CCTK_VARIABLE_REAL8:\n return CACTUS_MPI_REAL8;\n case CCTK_VARIABLE_COMPLEX16:\n return CACTUS_MPI_COMPLEX16;\n#endif\n#ifdef HAVE_CCTK_REAL16\n case CCTK_VARIABLE_REAL16:\n return CACTUS_MPI_REAL16;\n case CCTK_VARIABLE_COMPLEX32:\n return CACTUS_MPI_COMPLEX32;\n#endif\n }\n CCTK_ERROR(\"Unsupported datatype\");\n}\n\n////////////////////////////////////////////////////////////////////////////////\n\nstruct bbox {\n int off; // offset\n int len; // length\n int alen; // allocated length\n int str; // stride\n};\n\nstruct arrays {\n bbox global; // global region (all processors)\n bbox local; // this processor's region\n bbox active; // ???\n bbox slab; // the slab that should be transferred\n};\n\nstruct xfer {\n arrays src; // source region\n arrays dst; // destination region\n int xpose; // exchange axes\n int flip; // change directions of axes\n};\n\nstatic inline int roundup(int const x, int const y) {\n assert(x >= 0);\n assert(y > 0);\n return (x + y - 1) / y * y;\n}\n\nstatic void bbox_print(bbox const *restrict const bbox) {\n assert(bbox);\n printf(\"[%d:%d:%d]\", bbox->off, bbox->off + (bbox->len - 1) * bbox->str,\n bbox->str);\n}\n\nstatic void bbox_check(bbox const *restrict const bbox) {\n assert(bbox);\n assert(bbox->len >= 0);\n assert(bbox->alen >= bbox->len);\n assert(bbox->str > 0);\n}\n\nstatic void global2bbox(slabinfo const *restrict const slab,\n bbox *restrict const bbox) {\n assert(slab);\n assert(bbox);\n assert(slab->gsh >= 0);\n bbox->off = 0;\n bbox->len = slab->gsh;\n bbox->alen = slab->gsh;\n bbox->str = 1;\n bbox_check(bbox);\n}\n\nstatic void local2bbox(slabinfo const *restrict const slab,\n bbox *restrict const bbox) {\n assert(slab);\n assert(bbox);\n assert(slab->lbnd >= 0);\n assert(slab->lsh >= 0);\n assert(slab->ash >= slab->lsh);\n assert(slab->lbnd + slab->lsh <= slab->gsh);\n bbox->off = slab->lbnd;\n bbox->len = slab->lsh;\n bbox->alen = slab->ash;\n bbox->str = 1;\n bbox_check(bbox);\n}\n\nstatic void active2bbox(slabinfo const *restrict const slab,\n bbox *restrict const bbox, int const useghosts) {\n assert(slab);\n assert(bbox);\n assert(useghosts == 0 or useghosts == 1);\n assert(slab->lbnd >= 0);\n assert(slab->lsh >= 0);\n assert(slab->ash >= slab->lsh);\n assert(slab->lbnd + slab->lsh <= slab->gsh);\n assert(slab->lbbox == 0 or slab->lbbox == 1);\n assert(slab->ubbox == 0 or slab->ubbox == 1);\n assert(slab->nghostzones >= 0);\n int const nlghostzones = slab->lbbox or useghosts ? 0 : slab->nghostzones;\n int const nughostzones = slab->ubbox or useghosts ? 0 : slab->nghostzones;\n bbox->off = slab->lbnd + nlghostzones;\n bbox->len = slab->lsh - nlghostzones - nughostzones;\n bbox->alen = slab->ash;\n bbox->str = 1;\n bbox_check(bbox);\n}\n\nstatic void slab2bbox(slabinfo const *restrict const slab,\n bbox *restrict const bbox) {\n assert(slab);\n assert(bbox);\n bbox->off = slab->off;\n bbox->len = slab->len;\n // bbox->alen = slab->len;\n bbox->alen = 1000000; // undefined\n bbox->str = slab->str;\n bbox_check(bbox);\n}\n\nstatic int bbox_iscontained(bbox const *restrict const inner,\n bbox const *restrict const outer) {\n bbox_check(inner);\n bbox_check(outer);\n int const inner_last = inner->off + (inner->len - 1) * inner->str;\n int const outer_last = outer->off + (outer->len - 1) * outer->str;\n return inner->off >= outer->off and inner_last <= outer_last;\n}\n\nstatic void bbox_clip(bbox *restrict const inner,\n bbox const *restrict const outer) {\n bbox_check(inner);\n bbox_check(outer);\n int inner_last = inner->off + (inner->len - 1) * inner->str;\n int const outer_last = outer->off + (outer->len - 1) * outer->str;\n if (inner->off < outer->off) {\n inner->off += roundup(outer->off - inner->off, inner->str);\n }\n if (inner_last > outer_last) {\n inner_last -= roundup(inner_last - outer_last, inner->str);\n }\n assert((inner_last - inner->off) % inner->str == 0);\n if (inner_last >= inner->off) {\n inner->len = (inner_last - inner->off + inner->str) / inner->str;\n } else {\n inner->len = 0;\n }\n bbox_check(inner);\n}\n\n// ydst = xdst + Flip (ysrc - xsrc)]\n// This function is its own inverse.\nstatic void bbox_xform(bbox *restrict const ydst,\n bbox const *restrict const ysrc,\n bbox const *restrict const xdst,\n bbox const *restrict const xsrc, int const flip) {\n assert(ydst);\n bbox_check(ysrc);\n bbox_check(xdst);\n bbox_check(xsrc);\n assert(ysrc->str == xsrc->str);\n /* int const xsrc_last = xsrc->off + (xsrc->len - 1) * xsrc->str; */\n int const xdst_last = xdst->off + (xdst->len - 1) * xdst->str;\n int const ysrc_last = ysrc->off + (ysrc->len - 1) * ysrc->str;\n ydst->str = xdst->str;\n assert((ysrc->off - xsrc->off) % ysrc->str == 0);\n ydst->off = xdst->off + (ysrc->off - xsrc->off) / ysrc->str * ydst->str;\n int ydst_last = xdst->off + (ysrc_last - xsrc->off) / ysrc->str * ydst->str;\n if (flip) {\n int const off = ydst->off;\n int const last = ydst_last;\n ydst->off = xdst->off + xdst_last - last;\n ydst_last = xdst_last - (off - xdst->off);\n }\n assert((ysrc_last - xsrc->off) % ysrc->str == 0);\n assert(ydst_last - ydst->off + ydst->str >= 0);\n ydst->len = (ydst_last - ydst->off + ydst->str) / ydst->str;\n ydst->alen = xdst->alen;\n bbox_check(ydst);\n}\n\nextern \"C\" void print_slabinfo(FILE *const out,\n slabinfo const *restrict const slabinfo) {\n fprintf(out, \" gsh: %d\\n\", slabinfo->gsh);\n fprintf(out, \" lbnd: %d, lsh: %d\\n\", slabinfo->lbnd, slabinfo->lsh);\n fprintf(out, \" ash: %d\\n\", slabinfo->ash);\n fprintf(out, \" lbbox: %d, ubbox: %d, nghostzones: %d\\n\", slabinfo->lbbox,\n slabinfo->ubbox, slabinfo->nghostzones);\n fprintf(out, \" off: %d, str: %d, len: %d\\n\", slabinfo->off, slabinfo->str,\n slabinfo->len);\n}\n\nextern \"C\" void print_xferinfo(FILE *const out,\n xferinfo const *restrict const xferinfo) {\n fprintf(out, \" src:\\n\");\n print_slabinfo(out, &xferinfo->src);\n fprintf(out, \" dst:\\n\");\n print_slabinfo(out, &xferinfo->dst);\n fprintf(out, \" xpose: %d\\n\", xferinfo->xpose);\n fprintf(out, \" flip: %d\\n\", xferinfo->flip);\n}\n\n// workhorse routines for the actual copying transposing, and flipping\n// of data\ntemplate <typename T>\ninline void copy_data(const vector<xfer> &info, const vector<bbox> &srcdetail,\n const vector<ptrdiff_t> &srcoffset,\n const vector<ptrdiff_t> &srcelems, vector<char> &srcdata,\n void const *restrict const *restrict const srcptrs,\n const int n, const vector<int> &varis, const int nvaris,\n const int xpose_x, const int xpose_y, const int xpose_z) {\n assert(srcptrs);\n\n ptrdiff_t const srcoffi = info[0].src.local.off;\n ptrdiff_t const srcoffj = info[1].src.local.off;\n ptrdiff_t const srcoffk = info[2].src.local.off;\n\n ptrdiff_t const srcleni = info[0].src.local.len;\n ptrdiff_t const srclenj = info[1].src.local.len;\n ptrdiff_t const srclenk = info[2].src.local.len;\n\n ptrdiff_t const srcaleni = info[0].src.local.alen;\n ptrdiff_t const srcalenj = info[1].src.local.alen;\n ptrdiff_t const srcalenk = info[2].src.local.alen;\n\n ptrdiff_t const srcdetailoffi = srcdetail[n * SLAB_MAXDIM + 0].off;\n ptrdiff_t const srcdetailoffj = srcdetail[n * SLAB_MAXDIM + 1].off;\n ptrdiff_t const srcdetailoffk = srcdetail[n * SLAB_MAXDIM + 2].off;\n\n ptrdiff_t const srcdetailleni = srcdetail[n * SLAB_MAXDIM + 0].len;\n ptrdiff_t const srcdetaillenj = srcdetail[n * SLAB_MAXDIM + 1].len;\n ptrdiff_t const srcdetaillenk = srcdetail[n * SLAB_MAXDIM + 2].len;\n\n ptrdiff_t const dstdetailleni = srcdetail[n * SLAB_MAXDIM + xpose_x].len;\n ptrdiff_t const dstdetaillenj = srcdetail[n * SLAB_MAXDIM + xpose_y].len;\n ptrdiff_t const dstdetaillenk = srcdetail[n * SLAB_MAXDIM + xpose_z].len;\n\n if (n == 0)\n assert(srcoffset[n] == 0);\n // TODO: This does not take nvaris into account\n // if (n<size-1) assert\n // (srcoffset[n+1]==srcoffset[n]+srcdetailleni*srcdetaillenj*srcdetaillenk);\n\n ifcheck {\n const int xpose[SLAB_MAXDIM] = {xpose_x, xpose_y, xpose_z};\n for (int i = 0; i < SLAB_MAXDIM; ++i) {\n for (int j = i + 1; j < SLAB_MAXDIM; ++j) {\n assert(xpose[i] != xpose[j]);\n }\n }\n }\n\n assert(dstdetailleni * dstdetaillenj * dstdetaillenk == srcelems[n]);\n if (srcelems[n] == 0)\n return;\n\n for (int vari = 0; vari < nvaris; ++vari) {\n T *restrict const srcdataptr =\n (T *)&srcdata.front() + srcoffset[n] + vari * srcelems[n];\n T const *restrict const srcptr = (T const *)srcptrs[varis[vari]];\n assert(srcptr);\n\n if (xpose_x == 0 and xpose_y == 1 and xpose_z == 2) {\n // no transposition\n\n CCTK_TimerStartI(timer_copy_in_noxpose);\n // #pragma omp parallel\n CCTK_LOOP3(Slab_copy_in_noxpose, i, j, k, 0, 0, 0, srcdetailleni,\n srcdetaillenj, srcdetaillenk, srcaleni, srcalenj, srcalenk) {\n ptrdiff_t const srcindi = srcdetailoffi + i - srcoffi;\n ptrdiff_t const srcindj = srcdetailoffj + j - srcoffj;\n ptrdiff_t const srcindk = srcdetailoffk + k - srcoffk;\n ifcheck assert(srcindi >= 0 and srcindi < srcleni);\n ifcheck assert(srcindj >= 0 and srcindj < srclenj);\n ifcheck assert(srcindk >= 0 and srcindk < srclenk);\n ptrdiff_t const srcind =\n srcindi + srcaleni * (srcindj + srcalenj * srcindk);\n ptrdiff_t const bufind = i + dstdetailleni * (j + dstdetaillenj * k);\n srcdataptr[bufind] = srcptr[srcind];\n }\n CCTK_ENDLOOP3(Slab_copy_in_noxpose);\n CCTK_TimerStopI(timer_copy_in_noxpose);\n\n } else if (xpose_x == 1 and xpose_y == 0 and xpose_z == 2) {\n // transpose x and y\n\n CCTK_TimerStartI(timer_copy_in_xposexy);\n // #pragma omp parallel\n // Interchange i and j loops\n CCTK_LOOP3(Slab_copy_in_xposexy, j, i, k, 0, 0, 0, srcdetaillenj,\n srcdetailleni, srcdetaillenk, srcalenj, srcaleni, srcalenk) {\n int const srcindi = srcdetailoffi + i - srcoffi;\n int const srcindj = srcdetailoffj + j - srcoffj;\n int const srcindk = srcdetailoffk + k - srcoffk;\n ifcheck assert(srcindi >= 0 and srcindi < srcleni);\n ifcheck assert(srcindj >= 0 and srcindj < srclenj);\n ifcheck assert(srcindk >= 0 and srcindk < srclenk);\n ptrdiff_t const srcind =\n srcindi + srcaleni * (srcindj + srcalenj * srcindk);\n ptrdiff_t const bufind = j + dstdetailleni * (i + dstdetaillenj * k);\n srcdataptr[bufind] = srcptr[srcind];\n }\n CCTK_ENDLOOP3(Slab_copy_in_xposexy);\n CCTK_TimerStopI(timer_copy_in_xposexy);\n\n } else {\n // general transposition\n\n CCTK_TimerStartI(timer_copy_in_xposegeneral);\n // #pragma omp parallel\n CCTK_LOOP3(Slab_copy_in_xposegeneral, i, j, k, 0, 0, 0, srcdetailleni,\n srcdetaillenj, srcdetaillenk, srcaleni, srcalenj, srcalenk) {\n int ipos[SLAB_MAXDIM];\n ipos[0] = i;\n ipos[1] = j;\n ipos[2] = k;\n int const srcindi = srcdetailoffi + i - srcoffi;\n int const srcindj = srcdetailoffj + j - srcoffj;\n int const srcindk = srcdetailoffk + k - srcoffk;\n ifcheck assert(srcindi >= 0 and srcindi < srcleni);\n ifcheck assert(srcindj >= 0 and srcindj < srclenj);\n ifcheck assert(srcindk >= 0 and srcindk < srclenk);\n ptrdiff_t const srcind =\n srcindi + srcaleni * (srcindj + srcalenj * srcindk);\n // TODO: rewrite this, moving the index onto dstdetaillen and\n // outside the loop; then collapse all loop specialisations\n ptrdiff_t const bufind =\n (ipos[xpose_x] +\n dstdetailleni * (ipos[xpose_y] + dstdetaillenj * ipos[xpose_z]));\n srcdataptr[bufind] = srcptr[srcind];\n }\n CCTK_ENDLOOP3(Slab_copy_in_xposegeneral);\n CCTK_TimerStopI(timer_copy_in_xposegeneral);\n }\n\n } // for vari\n}\n\n// workhorse routine responsible for the actual copying/flipping of data\ntemplate <typename T>\ninline void\ncopy_data_back(const vector<xfer> &info, const vector<bbox> &dstdetail,\n const vector<ptrdiff_t> &dstoffset,\n const vector<ptrdiff_t> &dstelems, const vector<char> &dstdata,\n void *restrict const *restrict const dstptrs, const int n,\n const vector<int> &varis, const int nvaris, const bool flip_x,\n const bool flip_y, const bool flip_z) {\n assert(dstptrs);\n\n ptrdiff_t const dstoffi = info[0].dst.local.off;\n ptrdiff_t const dstoffj = info[1].dst.local.off;\n ptrdiff_t const dstoffk = info[2].dst.local.off;\n\n ptrdiff_t const dstleni = info[0].dst.local.len;\n ptrdiff_t const dstlenj = info[1].dst.local.len;\n ptrdiff_t const dstlenk = info[2].dst.local.len;\n\n ptrdiff_t const dstaleni = info[0].dst.local.alen;\n ptrdiff_t const dstalenj = info[1].dst.local.alen;\n ptrdiff_t const dstalenk = info[2].dst.local.alen;\n\n ptrdiff_t const dstdetailoffi = dstdetail[n * SLAB_MAXDIM + 0].off;\n ptrdiff_t const dstdetailoffj = dstdetail[n * SLAB_MAXDIM + 1].off;\n ptrdiff_t const dstdetailoffk = dstdetail[n * SLAB_MAXDIM + 2].off;\n\n ptrdiff_t const dstdetailleni = dstdetail[n * SLAB_MAXDIM + 0].len;\n ptrdiff_t const dstdetaillenj = dstdetail[n * SLAB_MAXDIM + 1].len;\n ptrdiff_t const dstdetaillenk = dstdetail[n * SLAB_MAXDIM + 2].len;\n\n assert(dstdetailleni * dstdetaillenj * dstdetaillenk == dstelems[n]);\n if (dstelems[n] == 0)\n return;\n\n for (int vari = 0; vari < nvaris; ++vari) {\n T *restrict const dstptr = (T * restrict)dstptrs[varis[vari]];\n assert(dstptr);\n T const *restrict const dstdataptr =\n (T const *)&dstdata.front() + dstoffset[n] + vari * dstelems[n];\n\n if (not flip_x and not flip_y and not flip_z) {\n // no flipping\n\n CCTK_TimerStartI(timer_copy_back_noflip);\n // #pragma omp parallel\n CCTK_LOOP3(Slab_copy_back_noflip, i, j, k, 0, 0, 0, dstdetailleni,\n dstdetaillenj, dstdetaillenk, dstaleni, dstalenj, dstalenk) {\n ptrdiff_t const dstindi = dstdetailoffi + i - dstoffi;\n ptrdiff_t const dstindj = dstdetailoffj + j - dstoffj;\n ptrdiff_t const dstindk = dstdetailoffk + k - dstoffk;\n ifcheck assert(dstindi >= 0 and dstindi < dstleni);\n ifcheck assert(dstindj >= 0 and dstindj < dstlenj);\n ifcheck assert(dstindk >= 0 and dstindk < dstlenk);\n ptrdiff_t const dstind =\n dstindi + dstaleni * (dstindj + dstalenj * dstindk);\n ptrdiff_t const bufind = i + dstdetailleni * (j + dstdetaillenj * k);\n dstptr[dstind] = dstdataptr[bufind];\n }\n CCTK_ENDLOOP3(Slab_copy_back_noflip);\n CCTK_TimerStopI(timer_copy_back_noflip);\n\n } else if (flip_x and not flip_y and not flip_z) {\n // flip in x direction\n\n CCTK_TimerStartI(timer_copy_back_flipx);\n // #pragma omp parallel\n CCTK_LOOP3(Slab_copy_back_flipx, i, j, k, 0, 0, 0, dstdetailleni,\n dstdetaillenj, dstdetaillenk, dstaleni, dstalenj, dstalenk) {\n ptrdiff_t const dstindi =\n dstdetailoffi + (dstdetailleni - 1 - i) - dstoffi;\n ptrdiff_t const dstindj = dstdetailoffj + j - dstoffj;\n ptrdiff_t const dstindk = dstdetailoffk + k - dstoffk;\n ifcheck assert(dstindi >= 0 and dstindi < dstleni);\n ifcheck assert(dstindj >= 0 and dstindj < dstlenj);\n ifcheck assert(dstindk >= 0 and dstindk < dstlenk);\n ptrdiff_t const dstind =\n dstindi + dstaleni * (dstindj + dstalenj * dstindk);\n ptrdiff_t const bufind = i + dstdetailleni * (j + dstdetaillenj * k);\n dstptr[dstind] = dstdataptr[bufind];\n }\n CCTK_ENDLOOP3(Slab_copy_back_flipx);\n CCTK_TimerStopI(timer_copy_back_flipx);\n\n } else if (not flip_x and flip_y and not flip_z) {\n // flip in y direction\n\n CCTK_TimerStartI(timer_copy_back_flipy);\n // #pragma omp parallel\n CCTK_LOOP3(Slab_copy_back_flipy, i, j, k, 0, 0, 0, dstdetailleni,\n dstdetaillenj, dstdetaillenk, dstaleni, dstalenj, dstalenk) {\n ptrdiff_t const dstindi = dstdetailoffi + i - dstoffi;\n ptrdiff_t const dstindj =\n dstdetailoffj + (dstdetaillenj - 1 - j) - dstoffj;\n ptrdiff_t const dstindk = dstdetailoffk + k - dstoffk;\n ifcheck assert(dstindi >= 0 and dstindi < dstleni);\n ifcheck assert(dstindj >= 0 and dstindj < dstlenj);\n ifcheck assert(dstindk >= 0 and dstindk < dstlenk);\n ptrdiff_t const dstind =\n dstindi + dstaleni * (dstindj + dstalenj * dstindk);\n ptrdiff_t const bufind = i + dstdetailleni * (j + dstdetaillenj * k);\n dstptr[dstind] = dstdataptr[bufind];\n }\n CCTK_ENDLOOP3(Slab_copy_back_flipy);\n CCTK_TimerStopI(timer_copy_back_flipy);\n\n } else if (flip_x and flip_y and not flip_z) {\n // flip in both x and y direction\n\n CCTK_TimerStartI(timer_copy_back_flipxy);\n // #pragma omp parallel\n CCTK_LOOP3(Slab_copy_back_flipxy, i, j, k, 0, 0, 0, dstdetailleni,\n dstdetaillenj, dstdetaillenk, dstaleni, dstalenj, dstalenk) {\n ptrdiff_t const dstindi =\n dstdetailoffi + (dstdetailleni - 1 - i) - dstoffi;\n ptrdiff_t const dstindj =\n dstdetailoffj + (dstdetaillenj - 1 - j) - dstoffj;\n ptrdiff_t const dstindk = dstdetailoffk + k - dstoffk;\n ifcheck assert(dstindi >= 0 and dstindi < dstleni);\n ifcheck assert(dstindj >= 0 and dstindj < dstlenj);\n ifcheck assert(dstindk >= 0 and dstindk < dstlenk);\n ptrdiff_t const dstind =\n dstindi + dstaleni * (dstindj + dstalenj * dstindk);\n ptrdiff_t const bufind = i + dstdetailleni * (j + dstdetaillenj * k);\n dstptr[dstind] = dstdataptr[bufind];\n }\n CCTK_ENDLOOP3(Slab_copy_back_flipxy);\n CCTK_TimerStopI(timer_copy_back_flipxy);\n\n } else {\n // general flipping\n\n CCTK_TimerStartI(timer_copy_back_flipgeneral);\n // #pragma omp parallel\n CCTK_LOOP3(Slab_copy_back_flipgeneral, i, j, k, 0, 0, 0, dstdetailleni,\n dstdetaillenj, dstdetaillenk, dstaleni, dstalenj, dstalenk) {\n // TODO: rewrite this, moving the flipping onto dstlen and\n // outside the loop; then collapse all loop specialisations\n ptrdiff_t const dstindi =\n dstdetailoffi + (flip_x ? dstdetailleni - 1 - i : i) - dstoffi;\n ptrdiff_t const dstindj =\n dstdetailoffj + (flip_y ? dstdetaillenj - 1 - j : j) - dstoffj;\n ptrdiff_t const dstindk =\n dstdetailoffk + (flip_z ? dstdetaillenk - 1 - k : k) - dstoffk;\n ifcheck assert(dstindi >= 0 and dstindi < dstleni);\n ifcheck assert(dstindj >= 0 and dstindj < dstlenj);\n ifcheck assert(dstindk >= 0 and dstindk < dstlenk);\n ptrdiff_t const dstind =\n dstindi + dstaleni * (dstindj + dstalenj * dstindk);\n ptrdiff_t const bufind = i + dstdetailleni * (j + dstdetaillenj * k);\n dstptr[dstind] = dstdataptr[bufind];\n }\n CCTK_ENDLOOP3(Slab_copy_back_flipgeneral);\n CCTK_TimerStopI(timer_copy_back_flipgeneral);\n }\n\n } // for vari\n}\n\nstruct slabsetup {\n MPI_Comm comm;\n vector<xfer> info;\n vector<xfer> allinfo;\n vector<bbox> srcdetail, dstdetail;\n ptrdiff_t srclentot, dstlentot;\n};\n\nextern \"C\" slabsetup *\nSlab_MultiTransfer_Init(cGH const *restrict const cctkGH, int const dim,\n xferinfo const *restrict const xferinfo,\n int const options) {\n DECLARE_CCTK_PARAMETERS;\n\n // Check arguments\n check(cctkGH);\n check(dim >= 0);\n check(xferinfo);\n\n CCTK_TimerStartI(timer_init);\n\n slabsetup *restrict const slabsetup = new struct slabsetup;\n\n bool useghosts;\n {\n CCTK_INT tmp;\n int const iret = Util_TableGetInt(options, &tmp, \"useghosts\");\n if (iret == 1) {\n // There was an entry, use it\n useghosts = tmp;\n } else if (iret == UTIL_ERROR_BAD_HANDLE or\n iret == UTIL_ERROR_TABLE_NO_SUCH_KEY) {\n // There was no entry, use a default\n useghosts = false;\n } else {\n // Something went wrong, abort\n check(0);\n }\n }\n\n check(dim <= SLAB_MAXDIM);\n vector<xfer> &info = slabsetup->info;\n info.resize(SLAB_MAXDIM);\n for (int d = 0; d < dim; ++d) {\n global2bbox(&xferinfo[d].src, &info[d].src.global);\n local2bbox(&xferinfo[d].src, &info[d].src.local);\n active2bbox(&xferinfo[d].src, &info[d].src.active, useghosts);\n slab2bbox(&xferinfo[d].src, &info[d].src.slab);\n check(bbox_iscontained(&info[d].src.active, &info[d].src.local));\n check(bbox_iscontained(&info[d].src.local, &info[d].src.global));\n\n global2bbox(&xferinfo[d].dst, &info[d].dst.global);\n local2bbox(&xferinfo[d].dst, &info[d].dst.local);\n active2bbox(&xferinfo[d].dst, &info[d].dst.active, 1); // fill ghosts\n slab2bbox(&xferinfo[d].dst, &info[d].dst.slab);\n check(bbox_iscontained(&info[d].dst.active, &info[d].dst.local));\n check(bbox_iscontained(&info[d].dst.local, &info[d].dst.global));\n\n info[d].xpose = xferinfo[d].xpose;\n check(info[d].xpose >= 0 and info[d].xpose < dim);\n info[d].flip = xferinfo[d].flip;\n check(info[d].flip == 0 or info[d].flip == 1);\n }\n for (int d = dim; d < SLAB_MAXDIM; ++d) {\n static bbox const fake_bbox = {0, 1, 1, 1};\n static arrays const fake_arrays = {\n {0, 1, 1, 1}, {0, 1, 1, 1}, {0, 1, 1, 1}, {0, 1, 1, 1}};\n\n bbox_check(&fake_bbox);\n\n info[d].src = fake_arrays;\n check(bbox_iscontained(&info[d].src.active, &info[d].src.local));\n check(bbox_iscontained(&info[d].src.local, &info[d].src.global));\n\n info[d].dst = fake_arrays;\n check(bbox_iscontained(&info[d].dst.active, &info[d].dst.local));\n check(bbox_iscontained(&info[d].dst.local, &info[d].dst.global));\n\n info[d].xpose = d;\n check(info[d].xpose >= 0 and info[d].xpose < SLAB_MAXDIM);\n info[d].flip = 0;\n check(info[d].flip == 0 or info[d].flip == 1);\n }\n\n ifcheck {\n ifdebug printf(\"srcinfo:\\n\");\n for (int d = 0; d < SLAB_MAXDIM; ++d) {\n printf(\" src.global d=%d \", d);\n bbox_print(&info[d].src.global);\n printf(\"\\n\");\n printf(\" src.local d=%d \", d);\n bbox_print(&info[d].src.local);\n printf(\"\\n\");\n printf(\" src.active d=%d \", d);\n bbox_print(&info[d].src.active);\n printf(\"\\n\");\n printf(\" src.slab d=%d \", d);\n bbox_print(&info[d].src.slab);\n printf(\"\\n\");\n }\n ifdebug printf(\"dstinfo:\\n\");\n for (int d = 0; d < SLAB_MAXDIM; ++d) {\n printf(\" dst.global d=%d \", d);\n bbox_print(&info[d].dst.global);\n printf(\"\\n\");\n printf(\" dst.local d=%d \", d);\n bbox_print(&info[d].dst.local);\n printf(\"\\n\");\n printf(\" dst.active d=%d \", d);\n bbox_print(&info[d].dst.active);\n printf(\"\\n\");\n printf(\" dst.slab d=%d \", d);\n bbox_print(&info[d].dst.slab);\n printf(\"\\n\");\n }\n ifdebug printf(\"info:\\n\");\n for (int d = 0; d < SLAB_MAXDIM; ++d) {\n printf(\" xpose d=%d %d\\n\", d, info[d].xpose);\n printf(\" flip d=%d %d\\n\", d, info[d].flip);\n }\n }\n\n {\n bool iflag[SLAB_MAXDIM];\n for (int d = 0; d < SLAB_MAXDIM; ++d) {\n iflag[d] = false;\n }\n for (int d = 0; d < SLAB_MAXDIM; ++d) {\n assert(not iflag[info[d].xpose]);\n iflag[info[d].xpose] = true;\n }\n for (int d = 0; d < SLAB_MAXDIM; ++d) {\n assert(iflag[d]);\n }\n // Allow non-contributing processors to be non-knowledgeable\n for (int d = 0; d < SLAB_MAXDIM; ++d) {\n if (info[info[d].xpose].src.slab.len and info[d].dst.slab.len > 0) {\n assert(info[info[d].xpose].src.slab.len == info[d].dst.slab.len);\n }\n }\n }\n\n ptrdiff_t &srclentot = slabsetup->srclentot;\n ptrdiff_t &dstlentot = slabsetup->dstlentot;\n srclentot = 1;\n dstlentot = 1;\n for (int d = 0; d < SLAB_MAXDIM; ++d) {\n srclentot *= info[d].src.local.len;\n dstlentot *= info[d].dst.local.len;\n }\n\n MPI_Comm &comm = slabsetup->comm;\n {\n CCTK_POINTER_TO_CONST tmp1;\n int const iret1 = Util_TableGetPointerToConst(options, &tmp1, \"comm\");\n if (iret1 == 1) {\n // There was an entry, use it\n comm = *(MPI_Comm const *)tmp1;\n } else if (iret1 == UTIL_ERROR_TABLE_WRONG_DATA_TYPE) {\n // Entry has wrong type, fall back\n CCTK_POINTER tmp2;\n int const iret2 = Util_TableGetPointer(options, &tmp2, \"comm\");\n if (iret2 == 1) {\n // There was an entry, use it\n comm = *(MPI_Comm const *)tmp2;\n } else {\n // Something went wrong, abort\n check(0);\n }\n } else if (iret1 == UTIL_ERROR_BAD_HANDLE or\n iret1 == UTIL_ERROR_TABLE_NO_SUCH_KEY) {\n // There was no entry, use a default\n comm = get_mpi_comm(cctkGH);\n } else {\n // Something went wrong, abort\n check(0);\n }\n }\n\n ifcheck {\n ifdebug fflush(stdout);\n MPI_Barrier(comm);\n }\n\n int size, rank;\n MPI_Comm_size(comm, &size);\n MPI_Comm_rank(comm, &rank);\n\n ifcheck {\n static int count = 424242;\n int mycount = count;\n ifdebug fflush(stdout);\n MPI_Bcast(&mycount, 1, MPI_INT, 0, comm);\n assert(mycount == count);\n ++count;\n }\n\n vector<xfer> &allinfo = slabsetup->allinfo;\n allinfo.resize(size * SLAB_MAXDIM);\n {\n int const info_nints = sizeof(xfer) / sizeof(int);\n ifdebug fflush(stdout);\n MPI_Allgather(&info.front(), SLAB_MAXDIM * info_nints, MPI_INT,\n &allinfo.front(), SLAB_MAXDIM * info_nints, MPI_INT, comm);\n }\n\n for (int n = 0; n < size; ++n) {\n for (int d = 0; d < SLAB_MAXDIM; ++d) {\n // Allow non-contributing processors to be non-knowledgeable\n if (allinfo[n * SLAB_MAXDIM + d].src.slab.len > 0 and\n info[d].src.slab.len > 0) {\n assert(allinfo[n * SLAB_MAXDIM + d].src.global.off ==\n info[d].src.global.off);\n assert(allinfo[n * SLAB_MAXDIM + d].src.global.len ==\n info[d].src.global.len);\n assert(allinfo[n * SLAB_MAXDIM + d].src.global.str ==\n info[d].src.global.str);\n assert(allinfo[n * SLAB_MAXDIM + d].src.local.str ==\n info[d].src.local.str);\n assert(allinfo[n * SLAB_MAXDIM + d].src.active.str ==\n info[d].src.active.str);\n // 2003-03-01 eschnett: I don't know why the following should\n // be necessary\n assert(allinfo[n * SLAB_MAXDIM + d].src.slab.str ==\n info[d].src.slab.str);\n }\n if (allinfo[n * SLAB_MAXDIM + d].dst.slab.len > 0 and\n info[d].dst.slab.len > 0) {\n assert(allinfo[n * SLAB_MAXDIM + d].dst.global.off ==\n info[d].dst.global.off);\n assert(allinfo[n * SLAB_MAXDIM + d].dst.global.len ==\n info[d].dst.global.len);\n assert(allinfo[n * SLAB_MAXDIM + d].dst.global.str ==\n info[d].dst.global.str);\n assert(allinfo[n * SLAB_MAXDIM + d].dst.local.str ==\n info[d].dst.local.str);\n assert(allinfo[n * SLAB_MAXDIM + d].dst.active.str ==\n info[d].dst.active.str);\n assert(allinfo[n * SLAB_MAXDIM + d].dst.slab.str ==\n info[d].dst.slab.str);\n }\n assert(allinfo[n * SLAB_MAXDIM + d].xpose == info[d].xpose);\n assert(allinfo[n * SLAB_MAXDIM + d].flip == info[d].flip);\n }\n }\n\n vector<bbox> &srcdetail = slabsetup->srcdetail;\n srcdetail.resize(size * SLAB_MAXDIM);\n for (int n = 0; n < size; ++n) {\n ifdebug printf(\"srcdetail n=%d:\\n\", n);\n for (int d = 0; d < SLAB_MAXDIM; ++d) {\n srcdetail[n * SLAB_MAXDIM + d] = allinfo[n * SLAB_MAXDIM + d].src.slab;\n ifdebug printf(\" src.slab d=%d \", d);\n ifdebug bbox_print(&srcdetail[n * SLAB_MAXDIM + d]);\n ifdebug printf(\"\\n\");\n bbox_clip(&srcdetail[n * SLAB_MAXDIM + d], &info[d].src.active);\n ifdebug printf(\" clipped with src.active d=%d \", d);\n ifdebug bbox_print(&srcdetail[n * SLAB_MAXDIM + d]);\n ifdebug printf(\"\\n\");\n }\n for (int d = 0; d < SLAB_MAXDIM; ++d) {\n bbox whereto;\n bbox wherefrom;\n whereto = allinfo[n * SLAB_MAXDIM + d].dst.slab;\n ifdebug printf(\" dst.slab d=%d \", info[d].xpose);\n ifdebug bbox_print(&whereto);\n ifdebug printf(\"\\n\");\n bbox_clip(&whereto, &allinfo[n * SLAB_MAXDIM + d].dst.active);\n ifdebug printf(\" whereto d=%d \", info[d].xpose);\n ifdebug bbox_print(&whereto);\n ifdebug printf(\"\\n\");\n bbox_xform(&wherefrom, &whereto,\n &allinfo[n * SLAB_MAXDIM + info[d].xpose].src.slab,\n &allinfo[n * SLAB_MAXDIM + d].dst.slab, info[d].flip);\n ifdebug printf(\" wherefrom d=%d \", info[d].xpose);\n ifdebug bbox_print(&wherefrom);\n ifdebug printf(\"\\n\");\n bbox_clip(&srcdetail[n * SLAB_MAXDIM + info[d].xpose], &wherefrom);\n ifdebug printf(\" clipped with wherefrom d=%d \", info[d].xpose);\n ifdebug bbox_print(&srcdetail[n * SLAB_MAXDIM + info[d].xpose]);\n ifdebug printf(\"\\n\");\n }\n }\n\n vector<bbox> &dstdetail = slabsetup->dstdetail;\n dstdetail.resize(size * SLAB_MAXDIM);\n for (int n = 0; n < size; ++n) {\n ifdebug printf(\"dstdetail n=%d:\\n\", n);\n for (int d = 0; d < SLAB_MAXDIM; ++d) {\n // dstdetail[n*SLAB_MAXDIM+d] = allinfo[n*SLAB_MAXDIM+d].dst.slab;\n dstdetail[n * SLAB_MAXDIM + d] = info[d].dst.slab;\n ifdebug printf(\" dst.slab d=%d \", d);\n ifdebug bbox_print(&dstdetail[n * SLAB_MAXDIM + d]);\n ifdebug printf(\"\\n\");\n bbox_clip(&dstdetail[n * SLAB_MAXDIM + d], &info[d].dst.active);\n ifdebug printf(\" clipped with dst.active d=%d \", d);\n ifdebug bbox_print(&dstdetail[n * SLAB_MAXDIM + d]);\n ifdebug printf(\"\\n\");\n }\n for (int d = 0; d < SLAB_MAXDIM; ++d) {\n bbox wherefrom;\n bbox whereto;\n // wherefrom = allinfo[n*SLAB_MAXDIM+info[d].xpose].src.slab;\n wherefrom = info[info[d].xpose].src.slab;\n ifdebug printf(\" src.slab d=%d \", d);\n // ifdebug bbox_print (&dstdetail[n*SLAB_MAXDIM+d]);\n ifdebug bbox_print(&wherefrom);\n ifdebug printf(\"\\n\");\n bbox_clip(&wherefrom,\n &allinfo[n * SLAB_MAXDIM + info[d].xpose].src.active);\n ifdebug printf(\" wherefrom d=%d \", d);\n // ifdebug bbox_print (&dstdetail[n*SLAB_MAXDIM+d]);\n ifdebug bbox_print(&wherefrom);\n ifdebug printf(\"\\n\");\n bbox_xform(&whereto, &wherefrom, &allinfo[n * SLAB_MAXDIM + d].dst.slab,\n // &allinfo[n*SLAB_MAXDIM+info[d].xpose].src.slab,\n &info[info[d].xpose].src.slab, info[d].flip);\n ifdebug printf(\" whereto d=%d \", d);\n // ifdebug bbox_print (&dstdetail[n*SLAB_MAXDIM+d]);\n ifdebug bbox_print(&whereto);\n ifdebug printf(\"\\n\");\n bbox_clip(&dstdetail[n * SLAB_MAXDIM + d], &whereto);\n ifdebug printf(\" clipped with whereto d=%d \", d);\n ifdebug bbox_print(&dstdetail[n * SLAB_MAXDIM + d]);\n ifdebug printf(\"\\n\");\n }\n }\n\n CCTK_TimerStopI(timer_init);\n\n return slabsetup;\n}\n\nextern \"C\" int\nSlab_MultiTransfer_Apply(cGH const *restrict const cctkGH,\n slabsetup const *restrict const slabsetup,\n int const nvars, int const *restrict const srctypes,\n void const *restrict const *restrict const srcptrs,\n int const *restrict const dsttypes,\n void *restrict const *restrict const dstptrs) {\n DECLARE_CCTK_PARAMETERS;\n\n // Check arguments\n check(cctkGH);\n check(slabsetup);\n check(nvars >= 0);\n check(nvars == 0 or srctypes);\n for (int var = 0; var < nvars; ++var)\n check(srctypes[var] >= 0);\n check(nvars == 0 or srcptrs);\n ptrdiff_t const &srclentot = slabsetup->srclentot;\n for (int var = 0; var < nvars; ++var)\n if (srclentot > 0)\n assert(srcptrs[var]);\n check(nvars == 0 or dsttypes);\n for (int var = 0; var < nvars; ++var)\n check(dsttypes[var] >= 0);\n check(nvars == 0 or dstptrs);\n ptrdiff_t const &dstlentot = slabsetup->dstlentot;\n for (int var = 0; var < nvars; ++var)\n if (dstlentot > 0)\n assert(dstptrs[var]);\n\n if (nvars == 0)\n return 0;\n\n CCTK_TimerStartI(timer_apply);\n\n MPI_Comm const &comm = slabsetup->comm;\n\n ifcheck {\n ifdebug fflush(stdout);\n MPI_Barrier(comm);\n }\n\n int size, rank;\n MPI_Comm_size(comm, &size);\n MPI_Comm_rank(comm, &rank);\n\n ifcheck {\n static int count = 424242;\n int mycount = count;\n ifdebug fflush(stdout);\n MPI_Bcast(&mycount, 1, MPI_INT, 0, comm);\n assert(mycount == count);\n ++count;\n }\n\n vector<xfer> const &info = slabsetup->info;\n vector<xfer> const &allinfo = slabsetup->allinfo;\n vector<bbox> const &srcdetail = slabsetup->srcdetail;\n vector<bbox> const &dstdetail = slabsetup->dstdetail;\n\n vector<ptrdiff_t> srcelems(size);\n vector<ptrdiff_t> srccount(size);\n vector<ptrdiff_t> srcoffset(size + 1);\n\n vector<ptrdiff_t> dstelems(size);\n vector<ptrdiff_t> dstcount(size);\n vector<ptrdiff_t> dstoffset(size + 1);\n\n int nvartypes = 0;\n vector<int> vartypes(nvars);\n vector<int> vartypecount(nvars);\n for (int var = 0; var < nvars; ++var) {\n int const srctype = srctypes[var];\n int const dsttype = dsttypes[var];\n check(srctype == dsttype);\n int vartypei;\n for (vartypei = 0; vartypei < nvartypes; ++vartypei) {\n if (srctype == vartypes[vartypei])\n break;\n }\n if (vartypei >= nvartypes) {\n vartypes[nvartypes] = srctype;\n vartypecount[nvartypes] = 0;\n ++nvartypes;\n }\n assert(vartypei < nvartypes);\n ++vartypecount[vartypei];\n }\n\n for (int vartypei = 0; vartypei < nvartypes; ++vartypei) {\n int const vartype = vartypes[vartypei];\n int nvaris = 0;\n vector<int> varis(nvars);\n for (int var = 0; var < nvars; ++var) {\n if (srctypes[var] == vartype) {\n varis[nvaris] = var;\n ++nvaris;\n }\n }\n assert(nvaris == vartypecount[vartypei]);\n assert(nvaris > 0);\n\n ptrdiff_t const vartypesize = CCTK_VarTypeSize(vartype);\n check(vartypesize > 0);\n MPI_Datatype const vardatatype = mpi_type(vartype);\n\n srcoffset[0] = 0;\n for (int n = 0; n < size; ++n) {\n srcelems[n] = 1;\n for (int d = 0; d < SLAB_MAXDIM; ++d) {\n srcelems[n] *= srcdetail[n * SLAB_MAXDIM + d].len;\n }\n srccount[n] = nvaris * srcelems[n];\n ifdebug printf(\"srccnt n=%d offset=%td count=%td\\n\", n, srcoffset[n],\n srccount[n]);\n srcoffset[n + 1] = srcoffset[n] + srccount[n];\n }\n vector<char> srcdata(srcoffset[size] * vartypesize);\n ifcheck {\n if (vartype == CCTK_VARIABLE_REAL) {\n CCTK_REAL *restrict const srcdataptr = (CCTK_REAL *)&srcdata.front();\n CCTK_REAL marker;\n memset(&marker, POISON_VALUE, sizeof marker);\n for (int i = 0; i < srcoffset[size]; ++i) {\n memcpy(&srcdataptr[i], &marker, sizeof marker);\n }\n }\n }\n\n dstoffset[0] = 0;\n for (int n = 0; n < size; ++n) {\n dstelems[n] = 1;\n for (int d = 0; d < SLAB_MAXDIM; ++d) {\n dstelems[n] *= dstdetail[n * SLAB_MAXDIM + d].len;\n }\n dstcount[n] = nvaris * dstelems[n];\n ifdebug printf(\"dstcnt n=%d offset=%td count=%td\\n\", n, dstoffset[n],\n dstcount[n]);\n dstoffset[n + 1] = dstoffset[n] + dstcount[n];\n }\n vector<char> dstdata(dstoffset[size] * vartypesize);\n ifcheck {\n if (vartype == CCTK_VARIABLE_REAL) {\n CCTK_REAL *restrict const dstdataptr = (CCTK_REAL *)&dstdata.front();\n CCTK_REAL marker;\n memset(&marker, POISON_VALUE, sizeof marker);\n for (int i = 0; i < dstoffset[size]; ++i) {\n memcpy(&dstdataptr[i], &marker, sizeof marker);\n }\n }\n }\n\n check(srccount[rank] == dstcount[rank]);\n\n ifcheck {\n vector<long long> src2count(size);\n vector<long long> dst2count(size);\n ifdebug fflush(stdout);\n MPI_Alltoall(&srccount.front(), 1, MPI_LONG_LONG, &src2count.front(), 1,\n MPI_LONG_LONG, comm);\n MPI_Alltoall(&dstcount.front(), 1, MPI_LONG_LONG, &dst2count.front(), 1,\n MPI_LONG_LONG, comm);\n for (int n = 0; n < size; ++n) {\n check(src2count[n] == dstcount[n]);\n check(dst2count[n] == srccount[n]);\n }\n }\n\n CCTK_TimerStartI(timer_copy_in);\n\n for (int n = 0; n < size; ++n) {\n check(SLAB_MAXDIM == 3);\n\n if (srcdetail[n * SLAB_MAXDIM].str == 1 and\n srcdetail[n * SLAB_MAXDIM + 1].str == 1 and\n srcdetail[n * SLAB_MAXDIM + 2].str == 1 and\n vartype == CCTK_VARIABLE_REAL) {\n // Optimised for stride 1 and CCTK_REAL\n copy_data<CCTK_REAL>(info, srcdetail, srcoffset, srcelems, srcdata,\n srcptrs, n, varis, nvaris, info[0].xpose,\n info[1].xpose, info[2].xpose);\n } else if (srcdetail[n * SLAB_MAXDIM].str == 1 and\n srcdetail[n * SLAB_MAXDIM + 1].str == 1 and\n srcdetail[n * SLAB_MAXDIM + 2].str == 1 and\n vartype == CCTK_VARIABLE_COMPLEX) {\n // Optimised for stride 1 and CCTK_COMPLEX\n copy_data<CCTK_COMPLEX>(info, srcdetail, srcoffset, srcelems, srcdata,\n srcptrs, n, varis, nvaris, info[0].xpose,\n info[1].xpose, info[2].xpose);\n } else if (srcdetail[n * SLAB_MAXDIM].str == 1 and\n srcdetail[n * SLAB_MAXDIM + 1].str == 1 and\n srcdetail[n * SLAB_MAXDIM + 2].str == 1 and\n vartype == CCTK_VARIABLE_INT) {\n // Optimised for stride 1 and CCTK_INT\n copy_data<CCTK_INT>(info, srcdetail, srcoffset, srcelems, srcdata,\n srcptrs, n, varis, nvaris, info[0].xpose,\n info[1].xpose, info[2].xpose);\n } else {\n // Generic, unoptimised version\n CCTK_TimerStartI(timer_copy_in_general);\n\n ptrdiff_t const srcdetailleni =\n srcdetail[n * SLAB_MAXDIM + info[0].xpose].len;\n ptrdiff_t const srcdetaillenj =\n srcdetail[n * SLAB_MAXDIM + info[1].xpose].len;\n ptrdiff_t const srcdetaillenk =\n srcdetail[n * SLAB_MAXDIM + info[2].xpose].len;\n\n for (int vari = 0; vari < nvaris; ++vari) {\n char *restrict const srcdataptr =\n &srcdata[vartypesize * (srcoffset[n] + vari * srcelems[n])];\n char const *restrict const srcptr =\n (char const *)srcptrs[varis[vari]];\n\n // #pragma omp parallel for\n for (int k = 0; k < srcdetaillenk; ++k) {\n for (int j = 0; j < srcdetaillenj; ++j) {\n for (int i = 0; i < srcdetailleni; ++i) {\n ptrdiff_t ipos[SLAB_MAXDIM];\n ipos[0] = i;\n ipos[1] = j;\n ipos[2] = k;\n ptrdiff_t srcipos[SLAB_MAXDIM];\n ptrdiff_t bufipos[SLAB_MAXDIM];\n for (int d = 0; d < SLAB_MAXDIM; ++d) {\n int const c = info[d].xpose;\n srcipos[c] = srcdetail[n * SLAB_MAXDIM + c].off +\n ipos[d] * srcdetail[n * SLAB_MAXDIM + c].str;\n assert(srcipos[c] >= info[c].src.local.off and\n srcipos[c] <\n info[c].src.local.off + info[c].src.local.len);\n assert(\n srcipos[c] >=\n allinfo[n * SLAB_MAXDIM + c].src.slab.off and\n srcipos[c] <=\n allinfo[n * SLAB_MAXDIM + c].src.slab.off +\n (allinfo[n * SLAB_MAXDIM + c].src.slab.len - 1) *\n allinfo[n * SLAB_MAXDIM + c].src.slab.str);\n assert(\n (srcipos[c] - allinfo[n * SLAB_MAXDIM + c].src.slab.off) %\n allinfo[n * SLAB_MAXDIM + c].src.slab.str ==\n 0);\n bufipos[d] = ipos[d];\n assert(bufipos[d] >= 0 and\n bufipos[d] < srcdetail[n * SLAB_MAXDIM + c].len);\n }\n ptrdiff_t srcind = 0;\n ptrdiff_t bufind = 0;\n for (int d = SLAB_MAXDIM - 1; d >= 0; --d) {\n int const c = info[d].xpose;\n srcind = srcind * info[d].src.local.alen + srcipos[d] -\n info[d].src.local.off;\n bufind =\n bufind * srcdetail[n * SLAB_MAXDIM + c].len + bufipos[d];\n }\n assert(srcind < srclentot);\n assert(bufind < (ptrdiff_t)srccount[n]);\n memcpy(srcdataptr + vartypesize * bufind,\n srcptr + vartypesize * srcind, vartypesize);\n }\n }\n }\n\n } // for vari\n CCTK_TimerStopI(timer_copy_in_general);\n }\n } // for n\n\n ifcheck {\n if (vartype == CCTK_VARIABLE_REAL) {\n CCTK_REAL const *restrict const srcdataptr =\n (CCTK_REAL const *)&srcdata.front();\n CCTK_REAL marker;\n memset(&marker, POISON_VALUE, sizeof marker);\n for (int i = 0; i < srcoffset[size]; ++i) {\n assert(memcmp(&srcdataptr[i], &marker, sizeof marker) != 0);\n }\n }\n }\n CCTK_TimerStopI(timer_copy_in);\n\n CCTK_TimerStartI(timer_xfer);\n ifdebug fflush(stdout);\n if (not HAVE_MPI or use_alltoallv) {\n // MPI_Alltoallv does not support 64-bit integers\n CCTK_ERROR(\"Cannot use MPI_Alltoallv\");\n // MPI_Alltoallv(&srcdata.front(), &srccount.front(), &srcoffset.front(),\n // vardatatype, &dstdata.front(), &dstcount.front(),\n // &dstoffset.front(), vardatatype, comm);\n } else {\n vector<MPI_Request> requests;\n requests.reserve(2 * size);\n // Start receive\n for (int n = 0; n < size; ++n) {\n if (n != rank and dstcount[n] > 0) {\n ptrdiff_t offset = vartypesize * dstoffset[n];\n ptrdiff_t count = dstcount[n];\n while (count > 0) {\n ptrdiff_t thiscount =\n min(ptrdiff_t(numeric_limits<int>::max()), count);\n MPI_Request req;\n MPI_Irecv(&dstdata[offset], thiscount, vardatatype, n, 0, comm,\n &req);\n requests.push_back(req);\n offset += thiscount;\n count -= thiscount;\n }\n }\n }\n // Start send\n for (int n = 0; n < size; ++n) {\n if (n != rank and srccount[n] > 0) {\n ptrdiff_t offset = vartypesize * srcoffset[n];\n ptrdiff_t count = srccount[n];\n while (count > 0) {\n ptrdiff_t thiscount =\n min(ptrdiff_t(numeric_limits<int>::max()), count);\n MPI_Request req;\n MPI_Isend(&srcdata[offset], count, vardatatype, n, 0, comm, &req);\n requests.push_back(req);\n offset += thiscount;\n count -= thiscount;\n }\n }\n }\n // Self communication\n {\n int const n = rank;\n assert(dstcount[n] == srccount[n]);\n memcpy(&dstdata[vartypesize * dstoffset[n]],\n &srcdata[vartypesize * srcoffset[n]], dstcount[n] * vartypesize);\n }\n // Wait\n MPI_Waitall(requests.size(), &requests.front(), MPI_STATUSES_IGNORE);\n }\n\n ifcheck {\n if (vartype == CCTK_VARIABLE_REAL) {\n for (int vari = 0; vari < nvaris; ++vari) {\n CCTK_REAL const *restrict const dstdataptr =\n (CCTK_REAL const *)&dstdata.front();\n CCTK_REAL marker;\n memset(&marker, POISON_VALUE, sizeof marker);\n for (int i = 0; i < dstoffset[size]; ++i) {\n assert(memcmp(&dstdataptr[i], &marker, sizeof marker) != 0);\n }\n }\n }\n }\n CCTK_TimerStopI(timer_xfer);\n\n CCTK_TimerStartI(timer_copy_back);\n for (int n = 0; n < size; ++n) {\n check(SLAB_MAXDIM == 3);\n\n if (dstdetail[n * SLAB_MAXDIM].str == 1 and\n dstdetail[n * SLAB_MAXDIM + 1].str == 1 and\n dstdetail[n * SLAB_MAXDIM + 2].str == 1 and\n vartype == CCTK_VARIABLE_REAL) {\n // Optimised version for stride 1 and CCTK_REAL\n copy_data_back<CCTK_REAL>(info, dstdetail, dstoffset, dstelems, dstdata,\n dstptrs, n, varis, nvaris, info[0].flip,\n info[1].flip, info[2].flip);\n } else if (dstdetail[n * SLAB_MAXDIM].str == 1 and\n dstdetail[n * SLAB_MAXDIM + 1].str == 1 and\n dstdetail[n * SLAB_MAXDIM + 2].str == 1 and\n vartype == CCTK_VARIABLE_COMPLEX) {\n // Optimised version for stride 1 and CCTK_COMPLEX\n copy_data_back<CCTK_COMPLEX>(info, dstdetail, dstoffset, dstelems,\n dstdata, dstptrs, n, varis, nvaris,\n info[0].flip, info[1].flip, info[2].flip);\n } else if (dstdetail[n * SLAB_MAXDIM].str == 1 and\n dstdetail[n * SLAB_MAXDIM + 1].str == 1 and\n dstdetail[n * SLAB_MAXDIM + 2].str == 1 and\n vartype == CCTK_VARIABLE_INT) {\n // Optimised version for stride 1 and CCTK_INT\n copy_data_back<CCTK_INT>(info, dstdetail, dstoffset, dstelems, dstdata,\n dstptrs, n, varis, nvaris, info[0].flip,\n info[1].flip, info[2].flip);\n } else {\n // Generic, unoptimised version\n CCTK_TimerStartI(timer_copy_back_general);\n\n ptrdiff_t const dstdetailleni = dstdetail[n * SLAB_MAXDIM + 0].len;\n ptrdiff_t const dstdetaillenj = dstdetail[n * SLAB_MAXDIM + 1].len;\n ptrdiff_t const dstdetaillenk = dstdetail[n * SLAB_MAXDIM + 2].len;\n\n for (int vari = 0; vari < nvaris; ++vari) {\n char *restrict const dstptr = (char *restrict)dstptrs[varis[vari]];\n char const *restrict const dstdataptr =\n &dstdata[vartypesize * (dstoffset[n] + vari * dstelems[n])];\n\n // #pragma omp parallel for\n for (int k = 0; k < dstdetaillenk; ++k) {\n for (int j = 0; j < dstdetaillenj; ++j) {\n for (int i = 0; i < dstdetailleni; ++i) {\n ptrdiff_t ipos[SLAB_MAXDIM];\n ipos[0] = i;\n ipos[1] = j;\n ipos[2] = k;\n ptrdiff_t bufipos[SLAB_MAXDIM];\n ptrdiff_t dstipos[SLAB_MAXDIM];\n for (int d = 0; d < SLAB_MAXDIM; ++d) {\n if (not info[d].flip) {\n bufipos[d] = ipos[d];\n } else {\n bufipos[d] =\n dstdetail[n * SLAB_MAXDIM + d].len - 1 - ipos[d];\n }\n ifcheck assert(bufipos[d] >= 0 and\n bufipos[d] <\n dstdetail[n * SLAB_MAXDIM + d].len);\n dstipos[d] = dstdetail[n * SLAB_MAXDIM + d].off +\n ipos[d] * info[d].dst.slab.str;\n ifcheck assert(dstipos[d] >= info[d].dst.local.off and\n dstipos[d] < info[d].dst.local.off +\n info[d].dst.local.len);\n ifcheck assert(dstipos[d] >= info[d].dst.slab.off and\n dstipos[d] <= info[d].dst.slab.off +\n (info[d].dst.slab.len - 1) *\n info[d].dst.slab.str);\n ifcheck assert((dstipos[d] - info[d].dst.slab.off) %\n info[d].dst.slab.str ==\n 0);\n }\n ptrdiff_t bufind = 0;\n ptrdiff_t dstind = 0;\n for (int d = SLAB_MAXDIM - 1; d >= 0; --d) {\n bufind =\n bufind * dstdetail[n * SLAB_MAXDIM + d].len + bufipos[d];\n dstind = dstind * info[d].dst.local.alen + dstipos[d] -\n info[d].dst.local.off;\n }\n ifcheck assert(bufind < (ptrdiff_t)dstcount[n]);\n ifcheck assert(dstind < dstlentot);\n memcpy(dstptr + vartypesize * dstind,\n dstdataptr + vartypesize * bufind, vartypesize);\n }\n }\n }\n\n } // for vari\n CCTK_TimerStopI(timer_copy_back_general);\n }\n\n } // for n\n CCTK_TimerStopI(timer_copy_back);\n\n } // for vartypei\n\n ifcheck {\n ifdebug fflush(stdout);\n MPI_Barrier(comm);\n }\n\n CCTK_TimerStopI(timer_apply);\n\n return 0;\n}\n\nextern \"C\" int\nSlab_MultiTransfer_Finalize(cGH const *restrict const cctkGH,\n slabsetup *restrict const slabsetup) {\n DECLARE_CCTK_PARAMETERS;\n\n // Check arguments\n check(cctkGH);\n check(slabsetup);\n\n delete slabsetup;\n\n return 0;\n}\n\n// Interface for transferring a variable in one go\nextern \"C\" int\nSlab_MultiTransfer(cGH const *restrict const cctkGH, int const dim,\n xferinfo const *restrict const xferinfo, int const options,\n int const nvars, int const *restrict const srctypes,\n void const *restrict const *restrict const srcptrs,\n int const *restrict const dsttypes,\n void *restrict const *restrict const dstptrs) {\n slabsetup *restrict const slabsetup =\n Slab_MultiTransfer_Init(cctkGH, dim, xferinfo, options);\n Slab_MultiTransfer_Apply(cctkGH, slabsetup, nvars, srctypes, srcptrs,\n dsttypes, dstptrs);\n Slab_MultiTransfer_Finalize(cctkGH, slabsetup);\n return 0;\n}\n\n// Old interface for transferring a single variable\nextern \"C\" int Slab_Transfer(cGH const *restrict const cctkGH, int const dim,\n xferinfo const *restrict const xferinfo,\n int const options, int const srctype,\n void const *restrict const srcptr,\n int const dsttype, void *restrict const dstptr) {\n int const nvars = 1;\n int const srctypes[] = {srctype};\n void const *restrict const srcptrs[] = {srcptr};\n int const dsttypes[] = {dsttype};\n void *restrict const dstptrs[] = {dstptr};\n return Slab_MultiTransfer(cctkGH, dim, xferinfo, options, nvars, srctypes,\n srcptrs, dsttypes, dstptrs);\n}\n\n// Fortran wrapper\nextern \"C\" void CCTK_FCALL CCTK_FNAME(Slab_Transfer)(\n int *restrict const ierr, cGH const *const *restrict const cctkGH,\n int const *restrict const dim, int const *restrict const src_gsh,\n int const *restrict const src_lbnd, int const *restrict const src_lsh,\n int const *restrict const src_ash, int const *restrict const src_lbbox,\n int const *restrict const src_ubbox,\n int const *restrict const src_nghostzones,\n int const *restrict const src_off, int const *restrict const src_str,\n int const *restrict const src_len, int const *restrict const dst_gsh,\n int const *restrict const dst_lbnd, int const *restrict const dst_lsh,\n int const *restrict const dst_ash, int const *restrict const dst_lbbox,\n int const *restrict const dst_ubbox,\n int const *restrict const dst_nghostzones,\n int const *restrict const dst_off, int const *restrict const dst_str,\n int const *restrict const dst_len, int const *restrict const xpose,\n int const *restrict const flip, int const *restrict const options,\n int const *restrict const srctype, void const *restrict const srcptr,\n int const *restrict const dsttype, void *restrict const dstptr) {\n vector<xferinfo> xferinfo(*dim);\n\n for (int d = 0; d < *dim; ++d) {\n xferinfo[d].src.gsh = src_gsh[d];\n xferinfo[d].src.lbnd = src_lbnd[d];\n xferinfo[d].src.lsh = src_lsh[d];\n xferinfo[d].src.ash = src_ash[d];\n xferinfo[d].src.lbbox = src_lbbox[d];\n xferinfo[d].src.ubbox = src_ubbox[d];\n xferinfo[d].src.nghostzones = src_nghostzones[d];\n xferinfo[d].src.off = src_off[d];\n xferinfo[d].src.str = src_str[d];\n xferinfo[d].src.len = src_len[d];\n\n xferinfo[d].dst.gsh = dst_gsh[d];\n xferinfo[d].dst.lbnd = dst_lbnd[d];\n xferinfo[d].dst.lsh = dst_lsh[d];\n xferinfo[d].dst.ash = dst_ash[d];\n xferinfo[d].dst.lbbox = dst_lbbox[d];\n xferinfo[d].dst.ubbox = dst_ubbox[d];\n xferinfo[d].dst.nghostzones = dst_nghostzones[d];\n xferinfo[d].dst.off = dst_off[d];\n xferinfo[d].dst.str = dst_str[d];\n xferinfo[d].dst.len = dst_len[d];\n\n xferinfo[d].xpose = xpose[d];\n xferinfo[d].flip = flip[d];\n }\n\n *ierr = Slab_Transfer(*cctkGH, *dim, &xferinfo.front(), *options, *srctype,\n srcptr, *dsttype, dstptr);\n}\n", |