#include "genlib.h" #include "log.h" #include "memory.h" #include #include #define NTAB 32 double gasdev(long* idum, long* idum2, long* iy, long iv[NTAB], int cores) { /*returns a normally distributed deviate with 0 mean*/ /*and unit variance, using ran2(idum) as the source */ /*of uniform deviates */ double* used_ram_t0 = malloc(sizeof(double)); getVirtualMemUsed(used_ram_t0); clock_t t = clock(); log_info("RESULT = in progress, idum = %f, idum2 = %f, iy = %f", *idum, *idum2, *iy); struct cpustat initial[cores]; struct cpustat final[cores]; for (int i = 0; i < cores; i++) { get_stats(&initial[i], i - 1); } double ran2(long* idum, long* idum2, long* iy, long iv[NTAB], int cores); static int iset = 0; static double gset; double fac, rsq, v1, v2; if (iset == 0) { do { v1 = 2.0 * ran2(idum, idum2, iy, iv, cores) - 1.0; v2 = 2.0 * ran2(idum, idum2, iy, iv, cores) - 1.0; rsq = v1 * v1 + v2 * v2; } while (rsq >= 1.0 || rsq == 0.0); fac = sqrt(-2.0 * log(rsq) / rsq); gset = v1 * fac; iset = 1; t = clock() - t; double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time for (int i = 0; i < cores; i++) { get_stats(&final[i], i - 1); } for (int i = 0; i < cores; i++) { log_info("CPU %d: %lf%%", i, calculate_load(&initial[i], &final[i])); } double* used_ram_tf = malloc(sizeof(double)); getVirtualMemUsed(used_ram_tf); log_info("RESULT = success, gset = %f, fac = %f, v1 = %f, ELAPSED = %f seconds, DIF USED VIRTUAL MEM = %5.1f MB", gset, fac, v1, time_taken, *used_ram_tf - *used_ram_t0); free(used_ram_t0); free(used_ram_tf); return (v2 * fac); } else { iset = 0; t = clock() - t; double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time for (int i = 0; i < cores; i++) { get_stats(&final[i], i - 1); } for (int i = 0; i < cores; i++) { log_info("CPU %d: %lf%%", i, calculate_load(&initial[i], &final[i])); } double* used_ram_tf = malloc(sizeof(double)); getVirtualMemUsed(used_ram_tf); log_info("RESULT = success, gset = %f, fac = %f, v1 = %f, ELAPSED = %f seconds, DIF USED VIRTUAL MEM = %5.1f MB", gset, fac, v1, time_taken, *used_ram_tf - *used_ram_t0); free(used_ram_t0); free(used_ram_tf); return gset; } }