diff --git a/include/lattice_field.h b/include/lattice_field.h index f6a9b0f39c..b429b3b64f 100644 --- a/include/lattice_field.h +++ b/include/lattice_field.h @@ -801,6 +801,17 @@ namespace quda { /** @brief Restores the LatticeField */ virtual void restore() const { errorQuda("Not implemented"); } + /** + @brief The memory type of the field's backups: host memory for a host field, and for a device field the one + set by the environment variable QUDA_TUNE_BACKUP_MEMORY, host (the default), pinned or device + */ + QudaMemoryType BackupMemType() const; + + /** + @brief Whether the field's backups are allocated from QUDA's memory pools: all but device-memory backups + */ + bool BackupMemPool() const { return BackupMemType() != QUDA_MEMORY_DEVICE; } + /** @brief If managed memory and prefetch is enabled, prefetch all relevant memory fields to the current device or to the CPU. diff --git a/lib/clover_field.cpp b/lib/clover_field.cpp index 67267a2e64..416866fefd 100644 --- a/lib/clover_field.cpp +++ b/lib/clover_field.cpp @@ -200,11 +200,11 @@ namespace quda { void CloverField::backup() const { if (backup_h.size()) errorQuda("Already allocated host backup"); - backup_h.resize(2); - for (auto &b : backup_h) b = quda_ptr(QUDA_MEMORY_HOST, bytes); + backup_h.resize(inverse ? 2 : 1); + for (auto &b : backup_h) b = quda_ptr(BackupMemType(), bytes, BackupMemPool()); backup(false); - if (inverse) backup(true); + if (backup_h.size() > 1) backup(true); } void CloverField::restore(bool which) const @@ -216,7 +216,7 @@ namespace quda { { if (!backup_h.size()) errorQuda("Cannot restore since not backed up"); restore(false); - if (inverse) restore(true); + if (backup_h.size() > 1) restore(true); backup_h.resize(0); } diff --git a/lib/color_spinor_field.cpp b/lib/color_spinor_field.cpp index b9f6dc7061..8f315ec1f1 100644 --- a/lib/color_spinor_field.cpp +++ b/lib/color_spinor_field.cpp @@ -1486,7 +1486,7 @@ namespace quda { if (backup_depth == 0) { backup_h.resize(1); - backup_h[0] = quda_ptr(QUDA_MEMORY_HOST, bytes); + backup_h[0] = quda_ptr(BackupMemType(), bytes, BackupMemPool()); qudaMemcpy(backup_h[0], v, bytes, qudaMemcpyDefault); } backup_depth++; diff --git a/lib/gauge_field.cpp b/lib/gauge_field.cpp index f260e7e22c..e810ebb4c3 100644 --- a/lib/gauge_field.cpp +++ b/lib/gauge_field.cpp @@ -1367,12 +1367,12 @@ namespace quda { if (order == QUDA_QDP_GAUGE_ORDER) { backup_h.resize(geometry); for (int d = 0; d < geometry; d++) { - backup_h[d] = quda_ptr(QUDA_MEMORY_HOST, bytes / geometry); + backup_h[d] = quda_ptr(BackupMemType(), bytes / geometry, BackupMemPool()); qudaMemcpy(backup_h[d], gauge_array[d], bytes / geometry, qudaMemcpyDefault); } } else { backup_h.resize(1); - backup_h[0] = quda_ptr(QUDA_MEMORY_HOST, bytes); + backup_h[0] = quda_ptr(BackupMemType(), bytes, BackupMemPool()); qudaMemcpy(backup_h[0], gauge, bytes, qudaMemcpyDefault); } } diff --git a/lib/lattice_field.cpp b/lib/lattice_field.cpp index bc71d1acf0..1bafc28c63 100644 --- a/lib/lattice_field.cpp +++ b/lib/lattice_field.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -629,6 +631,33 @@ namespace quda { return -1; } + /** + @return The memory type that QUDA_TUNE_BACKUP_MEMORY sets for the backups of device fields, read once + */ + static QudaMemoryType tune_backup_mem_type() + { + static const QudaMemoryType type = [] { + const char *env = getenv("QUDA_TUNE_BACKUP_MEMORY"); + if (!env || strcmp(env, "host") == 0) return QUDA_MEMORY_HOST; + if (strcmp(env, "pinned") == 0) { + logQuda(QUDA_SUMMARIZE, "Backing up device fields in pinned host memory while tuning\n"); + return QUDA_MEMORY_HOST_PINNED; + } + if (strcmp(env, "device") == 0) { + logQuda(QUDA_SUMMARIZE, "Backing up device fields in device memory while tuning\n"); + return QUDA_MEMORY_DEVICE; + } + errorQuda("Unknown QUDA_TUNE_BACKUP_MEMORY=%s, expected host, pinned or device", env); + return QUDA_MEMORY_INVALID; + }(); + return type; + } + + QudaMemoryType LatticeField::BackupMemType() const + { + return location == QUDA_CPU_FIELD_LOCATION ? QUDA_MEMORY_HOST : tune_backup_mem_type(); + } + // This doesn't really live here, but is fine for the moment std::ostream& operator<<(std::ostream& output, const LatticeFieldParam& param) { diff --git a/lib/unitarize_force_quda.cu b/lib/unitarize_force_quda.cu index 875f582884..b11343292e 100644 --- a/lib/unitarize_force_quda.cu +++ b/lib/unitarize_force_quda.cu @@ -72,11 +72,14 @@ namespace quda { template void unitarizeForceCPU(Arg &arg) { using real = typename mapper::type; - Matrix, 3> v, result, oprod; - Matrix, 3> v_tmp, result_tmp, oprod_tmp; for (int parity = 0; parity < 2; parity++) { +#ifdef _OPENMP +#pragma omp parallel for +#endif for (unsigned int i = 0; i < arg.threads.x; i++) { + Matrix, 3> v, result, oprod; + Matrix, 3> v_tmp, result_tmp, oprod_tmp; for (int dir = 0; dir < 4; dir++) { oprod_tmp = arg.force_old(dir, i, parity); v_tmp = arg.u(dir, i, parity); diff --git a/lib/unitarize_links_quda.cu b/lib/unitarize_links_quda.cu index 62307b22ac..b6f2f36f43 100644 --- a/lib/unitarize_links_quda.cu +++ b/lib/unitarize_links_quda.cu @@ -55,9 +55,11 @@ namespace quda { if (checkLocation(outfield, infield) != QUDA_CPU_FIELD_LOCATION) errorQuda("Location must be CPU"); checkPrecision(outfield, infield); - Matrix,3> inlink, outlink; - +#ifdef _OPENMP +#pragma omp parallel for +#endif for (unsigned int i = 0; i < infield.Volume(); ++i) { + Matrix, 3> inlink, outlink; for (int dir=0; dir<4; ++dir){ if (infield.Precision() == QUDA_SINGLE_PRECISION) { copyArrayToLink(inlink, infield.data() + (i * 4 + dir) * 18); // order of arguments? diff --git a/tests/hisq_paths_force_test.cpp b/tests/hisq_paths_force_test.cpp index df2ffeb2ae..f580c72744 100644 --- a/tests/hisq_paths_force_test.cpp +++ b/tests/hisq_paths_force_test.cpp @@ -413,6 +413,16 @@ static int hisq_force_test(bool lepage) double long_time_sec = 0.0; double complete_time_sec = 0.0; + // tune outside the timed calls; the fields this accumulates into are zeroed below + if (getTuning()) { + fermion_force::hisqStaplesForce(*cudaForce_ex, *cudaOprod_ex, *cudaGauge_ex, d_act_path_coeff); + if (lepage && d_act_path_coeff[1] != 0.) + fermion_force::hisqLongLinkForce(*cudaForce_ex, *cudaLongLinkOprod_ex, *cudaGauge_ex, d_act_path_coeff[1]); + fermion_force::hisqCompleteForce(*cudaForce_ex, *cudaGauge_ex); + updateMomentum(*cudaMom, 1.0, *cudaForce_ex, __func__); + qudaDeviceSynchronize(); + } + /******************************** * Zero momenta and force fields * ********************************/ diff --git a/tests/hisq_stencil_test_utils.h b/tests/hisq_stencil_test_utils.h index 6c27fc4ffb..c3a5285b5d 100644 --- a/tests/hisq_stencil_test_utils.h +++ b/tests/hisq_stencil_test_utils.h @@ -69,6 +69,10 @@ struct HisqStencilTestWrapper { static inline void *qdp_fatlink_eps[4] = {nullptr, nullptr, nullptr, nullptr}; static inline void *qdp_longlink_eps[4] = {nullptr, nullptr, nullptr, nullptr}; + // whether the CPU reference has been built, and whether it is built with the Naik whatever the test's eps_naik + static inline bool reference_built = false; + static inline bool naik_reference = false; + void set_naik(bool has_naik) { if (has_naik) { @@ -94,10 +98,9 @@ struct HisqStencilTestWrapper { static bool first_time = true; if (first_time) { - // force the Naik build up front, it doesn't effect the non-naik fields - set_naik(true); + // the CPU reference is built with the Naik, which doesn't affect the non-Naik fields, so it serves every test + naik_reference = true; init_host(); - set_naik(has_naik); first_time = false; } init(); @@ -179,6 +182,18 @@ struct HisqStencilTestWrapper { // Note: this could be replaced with loading a gauge field createSiteLinkCPU(qdp_sitelink, gauge_param.cpu_prec, SiteLinkType::SITELINK_PHASE_NO); + } + + /** + @brief Build the CPU reference fat and long links, and allocate the host fields the GPU results are compared in. + Only verify() needs them, so it builds them on first use. + */ + void build_reference() + { + // the ctest's reference holds the Naik fields too, whatever the current test's eps_naik + const double eps_naik_test = eps_naik; + const int n_naiks_test = n_naiks; + if (naik_reference) set_naik(true); /////////////////////// // Perform CPU Build // @@ -216,6 +231,10 @@ struct HisqStencilTestWrapper { #ifdef MULTI_GPU exchange_llfat_cleanup(); #endif + + eps_naik = eps_naik_test; + n_naiks = n_naiks_test; + reference_built = true; } void init() @@ -270,25 +289,23 @@ struct HisqStencilTestWrapper { static void destroy() { - - for (int i = 0; i < 4; i++) { - host_free(fat_reflink[i]); - host_free(long_reflink[i]); - if (n_naiks > 1) { - host_free(fat_reflink_eps[i]); - host_free(long_reflink_eps[i]); - } - } - + // the reference fields exist only if verify() ran, and the Naik ones only if they were built + auto free_if_allocated = [](void *&p) { + if (p) host_free(p); + p = nullptr; + }; for (int i = 0; i < 4; i++) { - host_free(qdp_sitelink[i]); - host_free(qdp_fatlink[i]); - host_free(qdp_longlink[i]); - if (n_naiks > 1) { - host_free(qdp_fatlink_eps[i]); - host_free(qdp_longlink_eps[i]); - } + free_if_allocated(fat_reflink[i]); + free_if_allocated(long_reflink[i]); + free_if_allocated(fat_reflink_eps[i]); + free_if_allocated(long_reflink_eps[i]); + free_if_allocated(qdp_sitelink[i]); + free_if_allocated(qdp_fatlink[i]); + free_if_allocated(qdp_longlink[i]); + free_if_allocated(qdp_fatlink_eps[i]); + free_if_allocated(qdp_longlink_eps[i]); } + reference_built = false; } /*--------------------------------------------------------------------*/ @@ -391,6 +408,8 @@ struct HisqStencilTestWrapper { std::array verify() { + if (!reference_built) build_reference(); + //////////////////////////////////////////////////////////////////// // Layout change for fatlink, fatlink_eps, longlink, longlink_eps // ////////////////////////////////////////////////////////////////////