-
-
Save rbabich/9359c850aff56e17463f to your computer and use it in GitHub Desktop.
Simple example of reading parameters from a file in plain C (for possible inclusion in QUDA)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "input.h" | |
typedef struct { | |
int Nt; | |
int Lx; | |
int Ly; | |
int xyVol; | |
int rVol; | |
int latVol; | |
double beta; | |
double delta; // delta = beta/Nt | |
double kappa; | |
double r0; // r0 = old "alpha" | |
double e; | |
double ma; | |
double mb; | |
int flip; | |
int disc; | |
int niter; | |
int seed; | |
double resEpsilon; | |
int hmc_on; | |
double dt; | |
int nstep; | |
int ntr; | |
int maxIter; | |
int nsigma; | |
double *V; | |
double *Vinv; | |
double complex *VinvFTaa; | |
double complex *VinvFTab; | |
int verbose; | |
int bcV; | |
} param_t; | |
void setParams(char *file, param_t *param) | |
{ | |
param_list_t *plist; | |
param_t p; | |
readParamList(file, &plist); | |
getParam(p, plist, flip, "%d"); | |
getParam(p, plist, disc, "%d"); | |
getParam(p, plist, niter, "%d"); | |
getParam(p, plist, verbose, "%d"); | |
getParam(p, plist, Lx, "%d"); | |
getParam(p, plist, Ly, "%d"); | |
getParam(p, plist, Nt, "%d"); | |
getParam(p, plist, delta, "%lg"); | |
getParam(p, plist, kappa, "%lg"); | |
getParam(p, plist, r0, "%lg"); | |
getParam(p, plist, e, "%lg"); | |
getParam(p, plist, ma, "%lg"); | |
getParam(p, plist, mb, "%lg"); | |
getParam(p, plist, resEpsilon, "%lg"); | |
getParam(p, plist, maxIter, "%d"); | |
getParam(p, plist, hmc_on, "%d"); | |
getParam(p, plist, dt, "%lg"); | |
getParam(p, plist, nstep, "%d"); | |
getParam(p, plist, ntr, "%d"); | |
getParam(p, plist, nsigma, "%d"); | |
getParam(p, plist, seed, "%d"); | |
getParam(p, plist, bcV, "%d"); | |
freeParamList(plist); | |
// derived parameters | |
p.beta = p.delta * p.Nt; | |
p.xyVol = p.Lx * p.Ly; | |
p.rVol = 2 * p.xyVol; | |
p.latVol = p.Nt * p.rVol; | |
*param = p; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "graphene.h" | |
#include "input.h" | |
#include <string.h> | |
#define BUFSIZE (2*FIELDSIZE + 10) | |
/*! | |
* Read the parameter list from a file. Each line of the file should | |
* contain a parameter name and parameter value, separated by | |
* whitespace and/or a colon or equals sign. Blank lines and comments | |
* (beginning with "#") are also allowed. | |
*/ | |
void readParamList(char *filename, param_list_t **plist) | |
{ | |
FILE *fp; | |
char line[BUFSIZE]; | |
char *key, *value; | |
param_list_t *p, *prev; | |
fp = fopen(filename, "r"); | |
if (!fp) { | |
printf("Error: Failed to open parameter file %s\n", filename); | |
exit(1); | |
} | |
*plist = NULL; | |
prev = NULL; | |
fgets(line, BUFSIZE, fp); | |
while (!feof(fp)) { | |
key = strtok(line, " \t\n=:"); | |
// discard blank lines and comments | |
if (!key || (*key == '#')) { | |
fgets(line, BUFSIZE, fp); | |
continue; | |
} | |
value = strtok(NULL, " \t\n=:"); | |
if (!value) { | |
printf("Error: Bad format in parameter file \"%s\"\n", filename); | |
exit(1); | |
} | |
p = (param_list_t *) malloc(sizeof(param_list_t)); | |
p->next = NULL; | |
p->key[FIELDSIZE-1] = '\0'; | |
p->value[FIELDSIZE-1] = '\0'; | |
strncpy(p->key, key, FIELDSIZE-1); | |
strncpy(p->value, value, FIELDSIZE-1); | |
if (prev) { | |
prev->next = p; | |
} else { | |
*plist = p; | |
} | |
prev = p; | |
fgets(line, BUFSIZE, fp); | |
} | |
fclose(fp); | |
} | |
/*! | |
* Clear the parameter list, and free associated memory. | |
*/ | |
void freeParamList(param_list_t *plist) | |
{ | |
param_list_t *next; | |
while (plist) { | |
next = plist->next; | |
free(plist); | |
plist = next; | |
} | |
} | |
/*! | |
* Return a string containing the parameter whose name matches "key". | |
*/ | |
char *findParam(param_list_t *plist, char *key) | |
{ | |
while (plist) { | |
if (!strcmp(plist->key, key)) break; | |
plist = plist->next; | |
} | |
if (!plist) { | |
printf("Error: Parameter \"%s\" not found in input file.\n", key); | |
exit(1); | |
} | |
return plist->value; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _INPUT_H | |
#define _INPUT_H | |
#define FIELDSIZE 100 | |
typedef struct param_list_s { | |
char key[FIELDSIZE]; | |
char value[FIELDSIZE]; | |
struct param_list_s *next; | |
} param_list_t; | |
/*! | |
* The getParam() macro takes the name of a parameter and sets the | |
* corresponding member of struct_t based on the value found in the | |
* parameter list. Here "format" is a scanf() format specifier. | |
*/ | |
#define getParam(p, plist, key, format) \ | |
sscanf(findParam(plist, #key), format, &(p).key) | |
// functions defined in input.c: | |
void readParamList(char *filename, param_list_t **plist); | |
void freeParamList(param_list_t *plist); | |
char *findParam(param_list_t *plist, char *key); | |
#endif // _INPUT_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment