Skip to content

Commit

Permalink
Removed all use of sprintf in favour of snprintf for more robust stri…
Browse files Browse the repository at this point in the history
…ng creation for TuneKey creation. This should close #177.
  • Loading branch information
maddyscientist committed Dec 2, 2014
1 parent 60f7aaf commit 884fe53
Show file tree
Hide file tree
Showing 27 changed files with 76 additions and 45 deletions.
2 changes: 1 addition & 1 deletion include/color_spinor_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ namespace quda {
static void checkField(const ColorSpinorField &, const ColorSpinorField &);
void clearGhostPointers();

char aux_string[256]; // used as a label in the autotuner
char aux_string[TuneKey::aux_n]; // used as a label in the autotuner
void setTuningString(); // set the vol_string and aux_string for use in tuning

public:
Expand Down
3 changes: 1 addition & 2 deletions include/lattice_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ namespace quda {
/** Message handles for sending backwards */
MsgHandle ***mh_send_back[2];


char vol_string[32]; /** used as a label in the autotuner */
char vol_string[TuneKey::volume_n]; /** used as a label in the autotuner */
virtual void setTuningString(); /** set the vol_string for use in tuning */

public:
Expand Down
9 changes: 6 additions & 3 deletions include/tune_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ namespace quda {

struct TuneKey {

char volume[32];
char name[512];
char aux[512];
static const int volume_n = 32;
static const int name_n = 256;
static const int aux_n = 256;
char volume[volume_n];
char name[name_n];
char aux[aux_n];

TuneKey() { }
TuneKey(const char v[], const char n[], const char a[]="type=default") {
Expand Down
11 changes: 10 additions & 1 deletion include/tune_quda.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,16 @@ namespace quda {
}
}

char aux[512];
char aux[TuneKey::aux_n];

void writeAuxString(const char *format, ...) {
va_list arguments;
va_start (arguments, format);
int n = vsnprintf(aux, TuneKey::aux_n, format, arguments);
//int n = snprintf(aux, QUDA_TUNE_AUX_STR_LENGTH, "threads=%d,prec=%lu,stride=%d,geometery=%d",
// arg.volumeCB,sizeof(Complex)/2,arg.forceOffset);
if (n < 0 || n >= 512) errorQuda("Error writing auxiliary string");
}

public:
Tunable() { }
Expand Down
2 changes: 1 addition & 1 deletion lib/blas_quda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace quda {
static struct {
const char *vol_str;
const char *aux_str;
char aux_tmp[512];
char aux_tmp[TuneKey::aux_n];
} blasStrings;

void initReduce();
Expand Down
3 changes: 1 addition & 2 deletions lib/clover_deriv_quda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,7 @@ namespace quda {
public:
CloverDerivative(const CloverDerivArg<Complex> &arg, const GaugeField &meta)
: arg(arg), meta(meta) {
sprintf(aux,"threads=%d,prec=%lu,stride=%d,geometery=%d",
arg.volumeCB,sizeof(Complex)/2,arg.forceOffset);
writeAuxString("threads=%d,prec=%lu,stride=%d,geometery=%d",arg.volumeCB,sizeof(Complex)/2,arg.forceOffset);
}
virtual ~CloverDerivative() {}

Expand Down
2 changes: 1 addition & 1 deletion lib/clover_invert.cu
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ namespace quda {
public:
CloverInvert(CloverInvertArg<Clover> &arg, const CloverField &meta, QudaFieldLocation location)
: arg(arg), meta(meta), location(location) {
sprintf(aux,"stride=%d,prec=%lu",arg.clover.stride,sizeof(Float));
writeAuxString("stride=%d,prec=%lu",arg.clover.stride,sizeof(Float));
}
virtual ~CloverInvert() { ; }

Expand Down
4 changes: 2 additions & 2 deletions lib/clover_quda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ namespace quda {
public:
FmunuCompute(CloverArg<Float,Clover,Gauge> &arg, const GaugeField &meta, QudaFieldLocation location)
: arg(arg), meta(meta), location(location) {
sprintf(aux,"threads=%d,stride=%d,prec=%lu",arg.threads,arg.clover.stride,sizeof(Float));
writeAuxString("threads=%d,stride=%d,prec=%lu",arg.threads,arg.clover.stride,sizeof(Float));
}
virtual ~FmunuCompute() {}

Expand Down Expand Up @@ -487,7 +487,7 @@ namespace quda {
public:
CloverCompute(CloverArg<Float,Clover,Gauge> &arg, const GaugeField &meta, QudaFieldLocation location)
: arg(arg), meta(meta), location(location) {
sprintf(aux,"threads=%d,stride=%d,prec=%lu",arg.threads,arg.clover.stride,sizeof(Float));
writeAuxString("threads=%d,stride=%d,prec=%lu",arg.threads,arg.clover.stride,sizeof(Float));
}

virtual ~CloverCompute() {}
Expand Down
2 changes: 1 addition & 1 deletion lib/clover_trace_quda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ namespace quda {
public:
CloverSigmaTrace(CloverTraceArg<Clover1,Clover2,Gauge> &arg, const GaugeField &meta, QudaFieldLocation location)
: arg(arg), meta(meta), location(location) {
sprintf(aux, "stride=%d", arg.clover1.stride);
writeAuxString("stride=%d", arg.clover1.stride);
}
virtual ~CloverSigmaTrace() {;}

Expand Down
21 changes: 16 additions & 5 deletions lib/color_spinor_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,22 @@ namespace quda {
}

void ColorSpinorField::setTuningString() {
sprintf(vol_string, "%d", x[0]);
for (int d=1; d<nDim; d++) sprintf(vol_string, "%sx%d", vol_string, x[d]);
sprintf(aux_string, "vol=%d,stride=%d,precision=%d", volume, stride, precision);
if (twistFlavor != QUDA_TWIST_NO && twistFlavor != QUDA_TWIST_INVALID)
sprintf(aux_string, "%s,TwistFlavour=%d", aux_string, twistFlavor);
int check;
check = snprintf(vol_string, TuneKey::volume_n, "%d", x[0]);
if (check < 0 || check >= TuneKey::volume_n) errorQuda("Error writing volume string");
for (int d=1; d<nDim; d++) {
check = snprintf(vol_string, TuneKey::volume_n, "%sx%d", vol_string, x[d]);
if (check < 0 || check >= TuneKey::volume_n) errorQuda("Error writing volume string");
}

int aux_string_n = TuneKey::aux_n / 2;
check = snprintf(aux_string, aux_string_n, "vol=%d,stride=%d,precision=%d", volume, stride, precision);
if (check < 0 || check >= aux_string_n) errorQuda("Error writing aux string");

if (twistFlavor != QUDA_TWIST_NO && twistFlavor != QUDA_TWIST_INVALID) {
check = snprintf(aux_string, aux_string_n, "%s,TwistFlavour=%d", aux_string, twistFlavor);
if (check < 0 || check >= aux_string_n) errorQuda("Error writing aux string");
}
}

void ColorSpinorField::destroy() {
Expand Down
2 changes: 1 addition & 1 deletion lib/contract.cu
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ namespace quda

const int nTSlice; // Time-slice in case of time-dilution

char aux[16][512]; // For tuning purposes
char aux[16][TuneKey::aux_n]; // For tuning purposes

unsigned int sharedBytesPerThread() const { return 16*sizeof(rFloat); }
unsigned int sharedBytesPerBlock(const TuneParam &param) const { return 0; }
Expand Down
2 changes: 1 addition & 1 deletion lib/copy_clover.cu
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace quda {

public:
CopyClover(CopyCloverArg<Out,In> &arg, const CloverField &meta) : arg(arg), meta(meta) {
sprintf(aux, "out_stride=%d,in_stride=%d", arg.out.stride, arg.in.stride);
writeAuxString("out_stride=%d,in_stride=%d", arg.out.stride, arg.in.stride);
}
virtual ~CopyClover() { ; }

Expand Down
2 changes: 1 addition & 1 deletion lib/copy_color_spinor.cu
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ namespace quda {
public:
PackSpinor(OutOrder &out, const InOrder &in, Basis &basis, const ColorSpinorField &meta)
: out(out), in(in), basis(basis), meta(meta) {
sprintf(aux, "out_stride=%d,in_stride=%d", out.stride, in.stride);
writeAuxString("out_stride=%d,in_stride=%d", out.stride, in.stride);
}
virtual ~PackSpinor() { ; }

Expand Down
6 changes: 3 additions & 3 deletions lib/copy_gauge_extended.cu
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ namespace quda {
unsigned int minThreads() const { return arg.volume/2; }

public:
CopyGaugeEx(CopyGaugeExArg<OutOrder,InOrder> &arg, const GaugeField &meta, QudaFieldLocation location)
: arg(arg), meta(meta), location(location) {
sprintf(aux,"out_stride=%d,in_stride=%d,geometery=%d",arg.out.stride,arg.in.stride,arg.geometry);
CopyGaugeEx(CopyGaugeExArg<OutOrder,InOrder> &arg, const GaugeField &meta, QudaFieldLocation location)
: arg(arg), meta(meta), location(location) {
writeAuxString("out_stride=%d,in_stride=%d,geometery=%d",arg.out.stride,arg.in.stride,arg.geometry);
}
virtual ~CopyGaugeEx() { ; }

Expand Down
2 changes: 1 addition & 1 deletion lib/copy_gauge_inc.cu
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ namespace quda {
faceMax = (arg.faceVolumeCB[d] > faceMax ) ? arg.faceVolumeCB[d] : faceMax;
}
size = isGhost ? faceMax : arg.volume/2;
sprintf(aux, "out_stride=%d,in_stride=%d", arg.out.stride, arg.in.stride);
writeAuxString("out_stride=%d,in_stride=%d", arg.out.stride, arg.in.stride);
}

virtual ~CopyGauge() { ; }
Expand Down
2 changes: 1 addition & 1 deletion lib/dslash_quda.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@
bool tuneGridDim() const { return false; } // Don't tune the grid dimensions.
// all dslashes expect a 4-d volume here (dwf Ls is y thread dimension)
unsigned int minThreads() const { return in->X(0) * in->X(1) * in->X(2) * in->X(3); }
char aux[7][512];
char aux[7][TuneKey::aux_n];

void fillAux(KernelType kernel_type, const char *kernel_str) {
strcpy(aux[kernel_type],kernel_str);
Expand Down
2 changes: 1 addition & 1 deletion lib/extended_color_spinor_utilities.cu
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ namespace quda {
public:
CopySpinorEx(CopySpinorExArg<OutOrder,InOrder,Basis> &arg, const ColorSpinorField &meta, QudaFieldLocation location)
: arg(arg), meta(meta), location(location) {
sprintf(aux,"out_stride=%d,in_stride=%d",arg.out.stride,arg.in.stride);
writeAuxString("out_stride=%d,in_stride=%d",arg.out.stride,arg.in.stride);
}
virtual ~CopySpinorEx() {}

Expand Down
2 changes: 1 addition & 1 deletion lib/extract_gauge_ghost.cu
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ namespace quda {
? arg.order.faceVolumeCB[d] : faceMax;
size = 2 * faceMax; // factor of comes from parity

sprintf(aux, "stride=%d", arg.order.stride);
writeAuxString("stride=%d", arg.order.stride);
}

virtual ~ExtractGhost() { ; }
Expand Down
4 changes: 2 additions & 2 deletions lib/extract_gauge_ghost_extended.cu
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ namespace quda {
int dB = arg.B1[arg.dim]-arg.B0[arg.dim];
int dC = arg.C1[arg.dim]-arg.C0[arg.dim];
size = arg.R[arg.dim]*dA*dB*dC*arg.order.geometry;
sprintf(aux,"prec=%lu,stride=%d,extract=%d,dimension=%d",
sizeof(Float),arg.order.stride, extract, arg.dim);
writeAuxString("prec=%lu,stride=%d,extract=%d,dimension=%d",
sizeof(Float),arg.order.stride, extract, arg.dim);
}
virtual ~ExtractGhostEx() { ; }

Expand Down
2 changes: 1 addition & 1 deletion lib/gauge_phase.cu
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ namespace quda {
public:
GaugePhase(Arg &arg, const GaugeField &meta, QudaFieldLocation location)
: arg(arg), meta(meta), location(location) {
sprintf(aux,"stride=%d,prec=%lu",arg.order.stride,sizeof(Float));
writeAuxString("stride=%d,prec=%lu",arg.order.stride,sizeof(Float));
}
virtual ~GaugePhase() { ; }

Expand Down
4 changes: 2 additions & 2 deletions lib/gauge_update_quda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ namespace quda {
UpdateGaugeField(const UpdateGaugeArg<Complex,Gauge,Mom> &arg,
const GaugeField &meta, QudaFieldLocation location)
: arg(arg), meta(meta), location(location) {
sprintf(aux, "threads=%d,prec=%lu,stride=%d",
2*arg.in.volumeCB, sizeof(Complex)/2, arg.in.stride);
writeAuxString("threads=%d,prec=%lu,stride=%d",
2*arg.in.volumeCB, sizeof(Complex)/2, arg.in.stride);
}
virtual ~UpdateGaugeField() { }

Expand Down
4 changes: 2 additions & 2 deletions lib/ks_force_quda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ namespace quda {
public:
KSForceComplete(KSForceArg<Oprod,Gauge,Mom> &arg, const GaugeField &meta, QudaFieldLocation location)
: arg(arg), meta(meta), location(location) {
sprintf(aux,"prec=%lu,stride=%d",sizeof(Float),arg.mom.stride);
writeAuxString("prec=%lu,stride=%d",sizeof(Float),arg.mom.stride);
}

virtual ~KSForceComplete() {}
Expand Down Expand Up @@ -378,7 +378,7 @@ class KSLongLinkForce : Tunable {
public:
KSLongLinkForce(KSLongLinkArg<Result,Oprod,Gauge> &arg, const GaugeField &meta, QudaFieldLocation location)
: arg(arg), meta(meta), location(location) {
sprintf(aux,"prec=%lu,stride=%d",sizeof(Float),arg.res.stride);
writeAuxString("prec=%lu,stride=%d",sizeof(Float),arg.res.stride);
}

virtual ~KSLongLinkForce() {}
Expand Down
9 changes: 7 additions & 2 deletions lib/lattice_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ namespace quda {


void LatticeField::setTuningString() {
sprintf(vol_string, "%d", x[0]);
for (int d=1; d<nDim; d++) sprintf(vol_string, "%sx%d", vol_string, x[d]);
int check;
check = snprintf(vol_string, TuneKey::volume_n, "%d", x[0]);
if (check < 0 || check >= TuneKey::volume_n) errorQuda("Error writing volume string");
for (int d=1; d<nDim; d++) {
check = snprintf(vol_string, TuneKey::volume_n, "%sx%d", vol_string, x[d]);
if (check < 0 || check >= TuneKey::volume_n) errorQuda("Error writing volume string");
}
}

void LatticeField::checkField(const LatticeField &a) {
Expand Down
2 changes: 1 addition & 1 deletion lib/reduce_quda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
static struct {
const char *vol_str;
const char *aux_str;
char aux_tmp[512];
char aux_tmp[quda::TuneKey::aux_n];
} blasStrings;

// These are used for reduction kernels
Expand Down
2 changes: 1 addition & 1 deletion lib/staggered_oprod.cu
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ namespace quda {
StaggeredOprodField(const StaggeredOprodArg<Complex,Output,InputA,InputB> &arg,
const GaugeField &meta, QudaFieldLocation location)
: arg(arg), meta(meta), location(location) {
sprintf(aux,"threads=%d,prec=%lu,stride=%d",arg.length,sizeof(Complex)/2,arg.inA.Stride());
writeAuxString("threads=%d,prec=%lu,stride=%d",arg.length,sizeof(Complex)/2,arg.inA.Stride());
// this sets the communications pattern for the packing kernel
int comms[QUDA_MAX_DIM] = { commDimPartitioned(0), commDimPartitioned(1), commDimPartitioned(2), commDimPartitioned(3) };
setPackComms(comms);
Expand Down
11 changes: 8 additions & 3 deletions lib/tune.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,20 @@ namespace quda {
std::string n;
std::string a;

int check;

while (in.good()) {
getline(in, line);
if (!line.length()) continue; // skip blank lines (e.g., at end of file)
ls.clear();
ls.str(line);
ls >> v >> n >> a >> param.block.x >> param.block.y >> param.block.z;
sprintf(key.volume, "%s", v.c_str());
sprintf(key.name, "%s", n.c_str());
sprintf(key.aux, "%s", a.c_str());
check = snprintf(key.volume, key.volume_n, "%s", v.c_str());
if (check < 0 || check >= key.volume_n) errorQuda("Error writing volume string");
check = snprintf(key.name, key.name_n, "%s", n.c_str());
if (check < 0 || check >= key.name_n) errorQuda("Error writing name string");
check = snprintf(key.aux, key.aux_n, "%s", a.c_str());
if (check < 0 || check >= key.aux_n) errorQuda("Error writing aux string");
ls >> param.grid.x >> param.grid.y >> param.grid.z >> param.shared_bytes;
ls.ignore(1); // throw away tab before comment
getline(ls, param.comment); // assume anything remaining on the line is a comment
Expand Down
4 changes: 2 additions & 2 deletions lib/unitarize_force_quda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,8 @@ static double HOST_REUNIT_SVD_ABS_ERROR;
UnitarizeForceCuda(const cudaGaugeField& oldForce, const cudaGaugeField& gauge,
cudaGaugeField& newForce, int* fails) :
oldForce(oldForce), gauge(gauge), newForce(newForce), fails(fails) {
sprintf(aux, "threads=%d,prec=%lu,stride=%d",
gauge.Volume(), gauge.Precision(), gauge.Stride());
writeAuxString("threads=%d,prec=%lu,stride=%d",
gauge.Volume(), gauge.Precision(), gauge.Stride());
}
virtual ~UnitarizeForceCuda() { ; }

Expand Down

0 comments on commit 884fe53

Please sign in to comment.