Add elapsed time in logs

milestone_5_without_improvements-logs
chortas 3 years ago
parent 1e9aefebf0
commit 2ec7f40125

@ -12,6 +12,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
#define PyIntObject PyLongObject #define PyIntObject PyLongObject
@ -42,6 +43,8 @@
#endif #endif
int Py_getvalues(PyObject* args, long* seed, struct grid_mod* grid, struct vario_mod* variogram, struct statistic_mod* stat) { int Py_getvalues(PyObject* args, long* seed, struct grid_mod* grid, struct vario_mod* variogram, struct statistic_mod* stat) {
clock_t t = clock();
log_info("RESULT = in progress"); log_info("RESULT = in progress");
int i, varioNargs = 12, j = 0; int i, varioNargs = 12, j = 0;
PyObject* listvario; PyObject* listvario;
@ -146,6 +149,9 @@ int Py_getvalues(PyObject* args, long* seed, struct grid_mod* grid, struct vario
(variogram->ap)[i * 9 + 5] = PyFloat_AsDouble(PyTuple_GetItem(vgr, j++)); (variogram->ap)[i * 9 + 5] = PyFloat_AsDouble(PyTuple_GetItem(vgr, j++));
} }
log_info("RESULT = success"); t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, ELAPSED = %f seconds", time_taken);
return 1; return 1;
} }

@ -12,13 +12,15 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
/* kgeneration */ /* kgeneration */
/* Z is the GWN with 0-mean and 1-variance */ /* Z is the GWN with 0-mean and 1-variance */
/* Y is the realization with mean and variance wanted */ /* Y is the realization with mean and variance wanted */
void Py_kgeneration(long seed, struct grid_mod grid, struct statistic_mod stat, struct vario_mod variogram, struct realization_mod* Z, struct realization_mod* Y, int n[3]) void Py_kgeneration(long seed, struct grid_mod grid, struct statistic_mod stat, struct vario_mod variogram, struct realization_mod* Z, struct realization_mod* Y, int n[3]) {
{ clock_t t = clock();
int i, N; int i, N;
int typelog; int typelog;
@ -40,6 +42,9 @@ void Py_kgeneration(long seed, struct grid_mod grid, struct statistic_mod stat,
typelog = stat.type + 2; typelog = stat.type + 2;
nor2log(Y, typelog, Y); nor2log(Y, typelog, Y);
} }
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success"); log_info("RESULT = success, ELAPSED = %f seconds", time_taken);
} }

@ -6,6 +6,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
/* build_real */ /* build_real */
/* build a realization in the spectral domain */ /* build a realization in the spectral domain */
@ -20,6 +21,8 @@
/*ireal: vector defining the i-part */ /*ireal: vector defining the i-part */
void build_real(int n[3], int NTOT, double* covar, double* realization, double* ireal) { void build_real(int n[3], int NTOT, double* covar, double* realization, double* ireal) {
clock_t t = clock();
int i, j, k, maille1; int i, j, k, maille1;
double temp; double temp;
log_info("RESULT = in progress, NTOT = %d, covar = %f, n[0] = %d, n[1] = %d, n[2] = %d", NTOT, *covar, n[0], n[1], n[2]); log_info("RESULT = in progress, NTOT = %d, covar = %f, n[0] = %d, n[1] = %d, n[2] = %d", NTOT, *covar, n[0], n[1], n[2]);
@ -41,5 +44,8 @@ void build_real(int n[3], int NTOT, double* covar, double* realization, double*
} }
} }
log_info("RESULT = success, realization = %f, ireal = %f", *realization, *ireal); t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, realization = %f, ireal = %f, ELAPSED = %f seconds", *realization, *ireal, time_taken);
} }

@ -2,9 +2,12 @@
#include "log.h" #include "log.h"
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <time.h>
/*cardsin covariance function*/ /*cardsin covariance function*/
double cardsin(double h) { double cardsin(double h) {
clock_t t = clock();
log_info("RESULT = in progress, h = %f", h); log_info("RESULT = in progress, h = %f", h);
float delta = 20.371; float delta = 20.371;
@ -17,7 +20,10 @@ double cardsin(double h) {
z = 1.; z = 1.;
} }
log_info("RESULT = success, z = %f", z); t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, z = %f, ELAPSED = %f seconds", z, time_taken);
return z; return z;
} }

@ -1,6 +1,7 @@
#include "geostat.h" #include "geostat.h"
#include "log.h" #include "log.h"
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
/*computes the size of the grid for FFTs*/ /*computes the size of the grid for FFTs*/
/*input: */ /*input: */
@ -11,6 +12,8 @@
/* X, Y and Z axes for the underlying grid */ /* X, Y and Z axes for the underlying grid */
/* i = [0 1 2] */ /* i = [0 1 2] */
void cgrid(struct vario_mod variogram, struct grid_mod grid, int n[3]) { void cgrid(struct vario_mod variogram, struct grid_mod grid, int n[3]) {
clock_t t = clock();
int i, N; int i, N;
double D; double D;
@ -41,5 +44,8 @@ void cgrid(struct vario_mod variogram, struct grid_mod grid, int n[3]) {
} }
} }
log_info("RESULT = success, n[0] = %d, n[1] = %d, n[2] = %d", n[0], n[1], n[2]); t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, n[0] = %d, n[1] = %d, n[2] = %d, ELAPSED = %f seconds", n[0], n[1], n[2], time_taken);
} }

@ -6,8 +6,11 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
void clean_real(struct realization_mod* realin, int n[3], struct grid_mod grid, double* vectorresult, struct realization_mod* realout) { void clean_real(struct realization_mod* realin, int n[3], struct grid_mod grid, double* vectorresult, struct realization_mod* realout) {
clock_t t = clock();
int i, j, k, maille0, maille1; int i, j, k, maille0, maille1;
double NTOT; double NTOT;
@ -39,5 +42,8 @@ void clean_real(struct realization_mod* realin, int n[3], struct grid_mod grid,
} }
} }
log_info("RESULT = success"); t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, ELAPSED = %f seconds", time_taken);
} }

@ -2,9 +2,12 @@
#include "geostat.h" #include "geostat.h"
#include "log.h" #include "log.h"
#include <math.h> #include <math.h>
#include <time.h>
/*selection of model covariance*/ /*selection of model covariance*/
double cov_value(struct vario_mod variogram, double di, double dj, double dk) { double cov_value(struct vario_mod variogram, double di, double dj, double dk) {
clock_t t = clock();
log_info("RESULT = in progress, di = %f, dj = %f, dk = %f", di, dj, dk); log_info("RESULT = in progress, di = %f, dj = %f, dk = %f", di, dj, dk);
double hx, hy, hz, h; double hx, hy, hz, h;
@ -51,7 +54,10 @@ double cov_value(struct vario_mod variogram, double di, double dj, double dk) {
} }
} }
log_info("RESULT = success, hx = %f, hy = %f, hz = %f, h = %f, cov = %f", hx, hy, hz, h, cov); t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, hx = %f, hy = %f, hz = %f, h = %f, cov = %f, ELAPSED = %f seconds", hx, hy, hz, h, cov, time_taken);
return cov; return cov;
} }

@ -1,9 +1,12 @@
#include "geostat.h" #include "geostat.h"
#include "log.h" #include "log.h"
#include <time.h>
/*builds the sampled covariance function*/ /*builds the sampled covariance function*/
/*dimensions are even*/ /*dimensions are even*/
void covariance(double* covar, struct vario_mod variogram, struct grid_mod mesh, int n[3]) { void covariance(double* covar, struct vario_mod variogram, struct grid_mod mesh, int n[3]) {
clock_t t = clock();
int i, j, k, maille, n2[3], symmetric; int i, j, k, maille, n2[3], symmetric;
double di, dj, dk; double di, dj, dk;
@ -77,5 +80,8 @@ void covariance(double* covar, struct vario_mod variogram, struct grid_mod mesh,
} }
} }
log_info("RESULT = success, di = %f, dj = %f, dk = %f", di, dj, dk); t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, di = %f, dj = %f, dk = %f, ELAPSED = %f seconds", di, dj, dk, time_taken);
} }

@ -2,9 +2,12 @@
#include "log.h" #include "log.h"
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <time.h>
/*cubic covariance function*/ /*cubic covariance function*/
double cubic(double h) { double cubic(double h) {
clock_t t = clock();
log_info("RESULT = in progress, h = %f", h); log_info("RESULT = in progress, h = %f", h);
double z; double z;
@ -15,7 +18,10 @@ double cubic(double h) {
z = 1. - 7. * (double)(h * h) + (35. / 4.) * (double)(h * h * h) - 3.5 * (double)(h * h * h * h * h) + .75 * (double)(h * h * h * h * h * h * h); z = 1. - 7. * (double)(h * h) + (35. / 4.) * (double)(h * h * h) - 3.5 * (double)(h * h * h * h * h) + .75 * (double)(h * h * h * h * h * h * h);
} }
log_info("RESULT = success, z = %f"); t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, z = %f, ELAPSED = %f seconds", z, time_taken);
return z; return z;
} }

@ -3,6 +3,7 @@
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
/*FAST FOURIER TRANSFORM MOVING AVERAGE METHOD */ /*FAST FOURIER TRANSFORM MOVING AVERAGE METHOD */
/*Turns a Gaussian white noise vector into a */ /*Turns a Gaussian white noise vector into a */
@ -23,6 +24,8 @@
/*realout: structure defining a realization - */ /*realout: structure defining a realization - */
void FFTMA2(struct vario_mod variogram, struct grid_mod grid, int n[3], struct realization_mod* realin, struct realization_mod* realout) { void FFTMA2(struct vario_mod variogram, struct grid_mod grid, int n[3], struct realization_mod* realin, struct realization_mod* realout) {
clock_t t = clock();
log_info("RESULT = in progress"); log_info("RESULT = in progress");
int NTOT, i, j, k, NMAX, NDIM, ntot, nmax, NXYZ, nxyz; int NTOT, i, j, k, NMAX, NDIM, ntot, nmax, NXYZ, nxyz;
@ -97,5 +100,8 @@ void FFTMA2(struct vario_mod variogram, struct grid_mod grid, int n[3], struct r
free(realization); free(realization);
log_info("RESULT = success, NTOT = %d, NMAX = %d, NDIM = %d, ntot = %d, nmax = %d, NXYZ = %d, nxyz = %d", NTOT, NMAX, NDIM, ntot, nmax, NXYZ, nxyz); t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, NTOT = %d, NMAX = %d, NDIM = %d, ntot = %d, nmax = %d, NXYZ = %d, nxyz = %d, ELAPSED = %f seconds", NTOT, NMAX, NDIM, ntot, nmax, NXYZ, nxyz, time_taken);
} }

@ -1,6 +1,7 @@
#include "log.h" #include "log.h"
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <time.h>
/*fast fourier transform */ /*fast fourier transform */
/* THE COOLEY-TUKEY FAST FOURIER TRANSFORM */ /* THE COOLEY-TUKEY FAST FOURIER TRANSFORM */
@ -91,6 +92,8 @@
/* 10-06-2000, MLR */ /* 10-06-2000, MLR */
void fourt(double* datar, double* datai, int nn[3], int ndim, int ifrwd, int icplx, double* workr, double* worki) { void fourt(double* datar, double* datai, int nn[3], int ndim, int ifrwd, int icplx, double* workr, double* worki) {
clock_t t = clock();
log_info("RESULT = in progress"); log_info("RESULT = in progress");
int ifact[21], ntot, idim, np1, n, np2, m, ntwo, iff, idiv, iquot, irem, inon2, non2p, np0, nprev, icase, ifmin, i, j, jmax, np2hf, i2, i1max, i3, j3, i1, ifp1, ifp2, i2max, i1rng, istep, imin, imax, mmax, mmin, mstep, j1, j2max, j2, jmin, j3max, nhalf; int ifact[21], ntot, idim, np1, n, np2, m, ntwo, iff, idiv, iquot, irem, inon2, non2p, np0, nprev, icase, ifmin, i, j, jmax, np2hf, i2, i1max, i3, j3, i1, ifp1, ifp2, i2max, i1rng, istep, imin, imax, mmax, mmin, mstep, j1, j2max, j2, jmin, j3max, nhalf;
double theta, wstpr, wstpi, wminr, wmini, wr, wi, wtemp, thetm, wmstr, wmsti, twowr, sr, si, oldsr, oldsi, stmpr, stmpi, tempr, tempi, difi, difr, sumr, sumi, TWOPI = 6.283185307179586476925286766559; double theta, wstpr, wstpi, wminr, wmini, wr, wi, wtemp, thetm, wmstr, wmsti, twowr, sr, si, oldsr, oldsi, stmpr, stmpi, tempr, tempi, difi, difr, sumr, sumi, TWOPI = 6.283185307179586476925286766559;
@ -584,6 +587,9 @@ void fourt(double* datar, double* datai, int nn[3], int ndim, int ifrwd, int icp
nprev = n; nprev = n;
} }
L920: L920:
log_info("RESULT = succes"); t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, ELAPSED = %f", time_taken);
return; return;
} }

@ -2,9 +2,12 @@
#include "log.h" #include "log.h"
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <time.h>
/*gamma covariance function*/ /*gamma covariance function*/
double gammf(double h, double alpha) { double gammf(double h, double alpha) {
clock_t t = clock();
log_info("RESULT = in progress, h = %f, alpha = %f", h, alpha); log_info("RESULT = in progress, h = %f, alpha = %f", h, alpha);
float delta; float delta;
@ -13,6 +16,9 @@ double gammf(double h, double alpha) {
delta = pow(20., 1. / alpha) - 1.; delta = pow(20., 1. / alpha) - 1.;
z = 1. / (double)(pow(1. + h * delta, alpha)); z = 1. / (double)(pow(1. + h * delta, alpha));
log_info("RESULT = success, delta = %f, z = %f", delta, z); t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, delta = %f, z = %f, ELAPSED = %f seconds", delta, z, time_taken);
return z; return z;
} }

@ -1,6 +1,7 @@
#include "genlib.h" #include "genlib.h"
#include "log.h" #include "log.h"
#include <math.h> #include <math.h>
#include <time.h>
#define NTAB 32 #define NTAB 32
@ -8,6 +9,8 @@ double gasdev(long* idum, long* idum2, long* iy, long iv[NTAB]) {
/*returns a normally distributed deviate with 0 mean*/ /*returns a normally distributed deviate with 0 mean*/
/*and unit variance, using ran2(idum) as the source */ /*and unit variance, using ran2(idum) as the source */
/*of uniform deviates */ /*of uniform deviates */
clock_t t = clock();
log_info("RESULT = in progress, idum = %f, idum2 = %f, iy = %f", *idum, *idum2, *iy); log_info("RESULT = in progress, idum = %f, idum2 = %f, iy = %f", *idum, *idum2, *iy);
double ran2(long* idum, long* idum2, long* iy, long iv[NTAB]); double ran2(long* idum, long* idum2, long* iy, long iv[NTAB]);
@ -25,11 +28,19 @@ double gasdev(long* idum, long* idum2, long* iy, long iv[NTAB]) {
fac = sqrt(-2.0 * log(rsq) / rsq); fac = sqrt(-2.0 * log(rsq) / rsq);
gset = v1 * fac; gset = v1 * fac;
iset = 1; iset = 1;
log_info("RESULT = success, gset = %f, fac = %f, v1 = %f", gset, fac, v1);
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, gset = %f, fac = %f, v1 = %f, ELAPSED = %f seconds", gset, fac, v1, time_taken);
return (v2 * fac); return (v2 * fac);
} else { } else {
iset = 0; iset = 0;
log_info("RESULT = success, gset = %f", gset);
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, gset = %f, ELAPSED = %f seconds", gset, time_taken);
return gset; return gset;
} }
} }

@ -1,6 +1,7 @@
#include "geostat.h" #include "geostat.h"
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
#include "log.h" #include "log.h"
/* GENERATION OF A GAUSSIAN WHITE NOISE VECTOR */ /* GENERATION OF A GAUSSIAN WHITE NOISE VECTOR */
@ -11,6 +12,8 @@
/* realization: structure defining the realization*/ /* realization: structure defining the realization*/
void generate(long* seed, int n, struct realization_mod* realization) { void generate(long* seed, int n, struct realization_mod* realization) {
clock_t t = clock();
log_info("RESULT = in progress, n = %d", n); log_info("RESULT = in progress, n = %d", n);
int i; int i;
@ -37,6 +40,9 @@ void generate(long* seed, int n, struct realization_mod* realization) {
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
(*realization).vector[i] = gasdev(seed, &idum2, &iy, iv, &iset); (*realization).vector[i] = gasdev(seed, &idum2, &iy, iv, &iset);
log_info("RESULT = success"); t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, ELAPSED = %f seconds", time_taken);
free(iv); free(iv);
} }

@ -1,8 +1,11 @@
#include "log.h" #include "log.h"
#include <math.h> #include <math.h>
#include <time.h>
/* compute the length for one dimension*/ /* compute the length for one dimension*/
int length(int N, int i, double* scf, double* ap, double D, int Nvari) { int length(int N, int i, double* scf, double* ap, double D, int Nvari) {
clock_t t = clock();
log_info("RESULT = in progress, N = %d, i = %d, D = %f, Nvari = %d", N, i, D, Nvari); log_info("RESULT = in progress, N = %d, i = %d, D = %f, Nvari = %d", N, i, D, Nvari);
int maxfactor(int n); int maxfactor(int n);
@ -38,6 +41,9 @@ int length(int N, int i, double* scf, double* ap, double D, int Nvari) {
} }
} }
log_info("RESULT = success, n = %d", n); t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, n = %d, ELAPSED = %f seconds", n, time_taken);
return n; return n;
} }

@ -0,0 +1,168 @@
/*
* Copyright (c) 2020 rxi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "log.h"
#define MAX_CALLBACKS 32
typedef struct {
log_LogFn fn;
void *udata;
int level;
} Callback;
static struct {
void *udata;
log_LockFn lock;
int level;
bool quiet;
Callback callbacks[MAX_CALLBACKS];
} L;
static const char *level_strings[] = {
"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
};
#ifdef LOG_USE_COLOR
static const char *level_colors[] = {
"\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"
};
#endif
static void stdout_callback(log_Event *ev) {
char buf[16];
buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0';
#ifdef LOG_USE_COLOR
fprintf(
ev->udata, "%s %s%-5s\x1b[0m \x1b[0m%s:%d:\x1b[0m ",
buf, level_colors[ev->level], level_strings[ev->level],
ev->file, ev->line);
#else
fprintf(
ev->udata, "%s %-5s %s:%d: ",
buf, level_strings[ev->level], ev->file, ev->line);
#endif
vfprintf(ev->udata, ev->fmt, ev->ap);
fprintf(ev->udata, "\n");
fflush(ev->udata);
}
static void file_callback(log_Event *ev) {
char buf[64];
buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0';
fprintf(
ev->udata, "%s %-5s %s:%d: ",
buf, level_strings[ev->level], ev->file, ev->line);
vfprintf(ev->udata, ev->fmt, ev->ap);
fprintf(ev->udata, "\n");
fflush(ev->udata);
}
static void lock(void) {
if (L.lock) { L.lock(true, L.udata); }
}
static void unlock(void) {
if (L.lock) { L.lock(false, L.udata); }
}
const char* log_level_string(int level) {
return level_strings[level];
}
void log_set_lock(log_LockFn fn, void *udata) {
L.lock = fn;
L.udata = udata;
}
void log_set_level(int level) {
L.level = level;
}
void log_set_quiet(bool enable) {
L.quiet = enable;
}
int log_add_callback(log_LogFn fn, void *udata, int level) {
for (int i = 0; i < MAX_CALLBACKS; i++) {
if (!L.callbacks[i].fn) {
L.callbacks[i] = (Callback) { fn, udata, level };
return 0;
}
}
return -1;
}
int log_add_fp(FILE *fp, int level) {
return log_add_callback(file_callback, fp, level);
}
static void init_event(log_Event *ev, void *udata) {
if (!ev->time) {
time_t t = time(NULL);
ev->time = localtime(&t);
}
ev->udata = udata;
}
void log_log(int level, const char *file, int line, const char *fmt, ...) {
log_Event ev = {
.fmt = fmt,
.file = file,
.line = line,
.level = level,
};
lock();
if (!L.quiet && level >= L.level) {
init_event(&ev, stderr);
va_start(ev.ap, fmt);
stdout_callback(&ev);
va_end(ev.ap);
}
for (int i = 0; i < MAX_CALLBACKS && L.callbacks[i].fn; i++) {
Callback *cb = &L.callbacks[i];
if (level >= cb->level) {
init_event(&ev, cb->udata);
va_start(ev.ap, fmt);
cb->fn(&ev);
va_end(ev.ap);
}
}
unlock();
}

@ -0,0 +1,49 @@
/**
* Copyright (c) 2020 rxi
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MIT license. See `log.c` for details.
*/
#ifndef LOG_H
#define LOG_H
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <time.h>
#define LOG_VERSION "0.1.0"
typedef struct {
va_list ap;
const char *fmt;
const char *file;
struct tm *time;
void *udata;
int line;
int level;
} log_Event;
typedef void (*log_LogFn)(log_Event *ev);
typedef void (*log_LockFn)(bool lock, void *udata);
enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL };
#define log_trace(...) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__)
#define log_debug(...) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
#define log_info(...) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__)
#define log_warn(...) log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__)
#define log_error(...) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
#define log_fatal(...) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__)
const char* log_level_string(int level);
void log_set_lock(log_LockFn fn, void *udata);
void log_set_level(int level);
void log_set_quiet(bool enable);
int log_add_callback(log_LogFn fn, void *udata, int level);
int log_add_fp(FILE *fp, int level);
void log_log(int level, const char *file, int line, const char *fmt, ...);
#endif

@ -3,6 +3,8 @@
/*determines the greatest prime factor of an integer*/ /*determines the greatest prime factor of an integer*/
int maxfactor(int n) { int maxfactor(int n) {
clock_t t = clock();
log_info("RESULT = in progress, n = %d", n); log_info("RESULT = in progress, n = %d", n);
int test_fact(int* pnum, int fact, int* pmaxfac); int test_fact(int* pnum, int fact, int* pmaxfac);
@ -34,6 +36,9 @@ int maxfactor(int n) {
} }
} }
log_info("RESULT = success, maxfac = %d", maxfac); t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, maxfac = %d, ELAPSED = %f seconds", maxfac, time_taken);
return maxfac; return maxfac;
} }

@ -2,6 +2,7 @@
#include "log.h" #include "log.h"
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
/*TURNS NORMAL NUMBERS INTO LOGNORMAL NUMBERS */ /*TURNS NORMAL NUMBERS INTO LOGNORMAL NUMBERS */
/*input: */ /*input: */
@ -14,6 +15,8 @@
/* lognormal numbers */ /* lognormal numbers */
void nor2log(struct realization_mod* realin, int typelog, struct realization_mod* realout) { void nor2log(struct realization_mod* realin, int typelog, struct realization_mod* realout) {
clock_t t = clock();
log_info("RESULT = in progress"); log_info("RESULT = in progress");
int i; int i;
double coeff; double coeff;
@ -69,5 +72,8 @@ void nor2log(struct realization_mod* realin, int typelog, struct realization_mod
} }
} }
log_info("RESULT = success"); t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, ELAPSED = %f seconds", time_taken);
} }

@ -6,6 +6,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
/* prebuild_gwn */ /* prebuild_gwn */
/* Produce a first construction in real space of the Gaussian white noise */ /* Produce a first construction in real space of the Gaussian white noise */
@ -21,6 +22,8 @@
/*realization: structure defining a realization*/ /*realization: structure defining a realization*/
void prebuild_gwn(struct grid_mod grid, int n[3], struct realization_mod* realin, double* realization, int solver) { void prebuild_gwn(struct grid_mod grid, int n[3], struct realization_mod* realin, double* realization, int solver) {
clock_t t = clock();
int i, j, k, maille0, maille1; int i, j, k, maille0, maille1;
int ntot; int ntot;
@ -47,5 +50,9 @@ void prebuild_gwn(struct grid_mod grid, int n[3], struct realization_mod* realin
} }
} }
} }
log_info("RESULT = success");
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
log_info("RESULT = success, ELAPSED = %f seconds");
} }

@ -1,3 +1,4 @@
#include <time.h>
#include "genlib.h" #include "genlib.h"
#include "log.h" #include "log.h"
@ -17,6 +18,8 @@
#define RNMX (1.0 - EPS) #define RNMX (1.0 - EPS)
double ran2(long* idum, long* idum2, long* iy, long iv[NTAB]) { double ran2(long* idum, long* idum2, long* iy, long iv[NTAB]) {
clock_t t = clock();
int j; int j;
long k; long k;
double temp; double temp;
@ -53,12 +56,16 @@ double ran2(long* idum, long* idum2, long* iy, long iv[NTAB]) {
iv[j] = *idum; iv[j] = *idum;
if (*iy < 1) if (*iy < 1)
(*iy) += IMM1; (*iy) += IMM1;
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
if ((temp = AM * (*iy)) > RNMX) { if ((temp = AM * (*iy)) > RNMX) {
log_info("RESULT = success"); log_info("RESULT = success, ELAPSED = %f", time_taken);
return (RNMX); return (RNMX);
} }
else { else {
log_info("RESULT = success, temp = %f, temp"); log_info("RESULT = success, temp = %f, ELAPSED = %f seconds", temp, time_taken);
return temp; return temp;
} }
} }

@ -0,0 +1 @@
python3 test.py 2>&1 | tee log.txt
Loading…
Cancel
Save