Skip to content
Open
11 changes: 11 additions & 0 deletions include/lattice_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions lib/clover_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/color_spinor_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
4 changes: 2 additions & 2 deletions lib/gauge_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
29 changes: 29 additions & 0 deletions lib/lattice_field.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <cstdlib>
#include <cstring>
#include <typeinfo>
#include <quda_internal.h>
#include <lattice_field.h>
Expand Down Expand Up @@ -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)
{
Expand Down
7 changes: 5 additions & 2 deletions lib/unitarize_force_quda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,14 @@ namespace quda {
template <typename Float, typename Arg> void unitarizeForceCPU(Arg &arg)
{
using real = typename mapper<Float>::type;
Matrix<complex<double>, 3> v, result, oprod;
Matrix<complex<real>, 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<complex<double>, 3> v, result, oprod;
Matrix<complex<real>, 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);
Expand Down
6 changes: 4 additions & 2 deletions lib/unitarize_links_quda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ namespace quda {
if (checkLocation(outfield, infield) != QUDA_CPU_FIELD_LOCATION) errorQuda("Location must be CPU");
checkPrecision(outfield, infield);

Matrix<complex<double>,3> inlink, outlink;

#ifdef _OPENMP
#pragma omp parallel for
#endif
for (unsigned int i = 0; i < infield.Volume(); ++i) {
Matrix<complex<double>, 3> inlink, outlink;
for (int dir=0; dir<4; ++dir){
if (infield.Precision() == QUDA_SINGLE_PRECISION) {
copyArrayToLink(inlink, infield.data<float *>() + (i * 4 + dir) * 18); // order of arguments?
Expand Down
10 changes: 10 additions & 0 deletions tests/hisq_paths_force_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
********************************/
Expand Down
59 changes: 39 additions & 20 deletions tests/hisq_stencil_test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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 //
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}

/*--------------------------------------------------------------------*/
Expand Down Expand Up @@ -391,6 +408,8 @@ struct HisqStencilTestWrapper {

std::array<double, 2> verify()
{
if (!reference_built) build_reference();

////////////////////////////////////////////////////////////////////
// Layout change for fatlink, fatlink_eps, longlink, longlink_eps //
////////////////////////////////////////////////////////////////////
Expand Down
Loading