You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.8 KiB
C
71 lines
1.8 KiB
C
#include "genlib.h"
|
|
#include "log.h"
|
|
#include "memory.h"
|
|
|
|
/*determines the greatest prime factor of an integer*/
|
|
int maxfactor(int n, int cores) {
|
|
double* used_ram_t0 = malloc(sizeof(double));
|
|
getVirtualMemUsed(used_ram_t0);
|
|
|
|
clock_t t = clock();
|
|
|
|
log_info("RESULT = in progress, n = %d", n);
|
|
|
|
struct cpustat initial[cores];
|
|
struct cpustat final[cores];
|
|
|
|
for (int i = 0; i < cores; i++) {
|
|
get_stats(&initial[i], i - 1);
|
|
}
|
|
|
|
int test_fact(int* pnum, int fact, int* pmaxfac);
|
|
int lnum, fact;
|
|
int maxfac;
|
|
|
|
maxfac = 1;
|
|
lnum = n;
|
|
|
|
if (lnum != 0 && lnum != 1) {
|
|
fact = 2;
|
|
if (test_fact(&lnum, fact, &maxfac)) {
|
|
fact = 3;
|
|
if (test_fact(&lnum, fact, &maxfac)) {
|
|
fact = 5;
|
|
for (;;) {
|
|
if (!test_fact(&lnum, fact, &maxfac))
|
|
break;
|
|
fact += 2;
|
|
if (!test_fact(&lnum, fact, &maxfac))
|
|
break;
|
|
fact += 4;
|
|
}
|
|
}
|
|
}
|
|
if (lnum != 1) {
|
|
if (lnum > maxfac)
|
|
maxfac = lnum;
|
|
}
|
|
}
|
|
|
|
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, maxfac = %d, ELAPSED = %f seconds, DIF USED VIRTUAL MEM = %5.1f MB", maxfac, time_taken, *used_ram_tf - *used_ram_t0);
|
|
|
|
free(used_ram_t0);
|
|
free(used_ram_tf);
|
|
|
|
return maxfac;
|
|
}
|