Skip to content

Commit

Permalink
calculate cpu freq
Browse files Browse the repository at this point in the history
not hardcoded to 3 GHz.
Some code is based on GH #125, but this result is not really good.
On linux I found an easy way.
  • Loading branch information
rurban committed Oct 1, 2020
1 parent 1940215 commit 8e66151
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
56 changes: 56 additions & 0 deletions Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,59 @@ void SetAffinity ( int /*cpu*/ )
}

#endif


// From https://encode.su/threads/3389-Code-snippet-to-compute-CPU-frequency
// Perform CYCLES simple in-order operations
static unsigned cycles_loop(unsigned cycles)
{
unsigned a = rand(), b = rand(), x = rand();
for (unsigned i=0; i < cycles/10; i++)
{
x = (x + a) ^ b;
x = (x + a) ^ b;
x = (x + a) ^ b;
x = (x + a) ^ b;
x = (x + a) ^ b;
}
return x;
}

#ifdef __linux__
#include <string>
#include <iostream>
#include <fstream>
#endif

double cpu_freq()
{
const unsigned cycles = 100*1000*1000;
unsigned x = cycles_loop(cycles/10); // warmup
#ifdef __linux__
std::ifstream curfreqfile("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
if (curfreqfile.is_open()) {
std::string line;
getline(curfreqfile, line);
long res = std::stol(line);
curfreqfile.close();
fprintf (stderr, "scaling_cpu_freq: %lu\n", res);
if (res > 1000)
return (double) res / 1000000.0; // 1801310, 2452626
}
#endif
double result = 3.0;
const uint64_t t1 = timer_start();
x += cycles_loop(cycles);
const uint64_t t2 = timer_end();
// constant adjustment factor, when compared to scaling_cur_freq
// ADJ = (res / 1000000.0) / (t2-t1)
const double ADJ = 236163824.32526;
if (x)
result = (double) (ADJ / (t2-t1));
if (result < 0.001) {
fprintf (stderr, "%u pseudo-cycles, %lu time-cycles\n", cycles, (unsigned long)(t2-t1));
return 3.0;
}
return result;
//printf("CPU freq %.2f GHz", (cycles/1e9)/(t2-t1));
}
1 change: 1 addition & 0 deletions Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

void SetAffinity ( int cpu );
double cpu_freq();

#ifndef __x86_64__
#if defined(__x86_64) || defined(_M_AMD64) || defined(_M_X64)
Expand Down
9 changes: 6 additions & 3 deletions SpeedTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ void BulkSpeedTest ( pfHash hash, uint32_t seed )

printf("Bulk speed test - %d-byte keys\n",blocksize);
double sumbpc = 0.0;
double freq = cpu_freq();

volatile double warmup_cycles = SpeedTest(hash,seed,trials,blocksize,0);

Expand All @@ -277,12 +278,14 @@ void BulkSpeedTest ( pfHash hash, uint32_t seed )

double bestbpc = double(blocksize)/cycles;

double bestbps = (bestbpc * 3000000000.0 / 1048576.0);
printf("Alignment %2d - %6.3f bytes/cycle - %7.2f MiB/sec @ 3 ghz\n",align,bestbpc,bestbps);
double bestbps = (bestbpc * freq * 1000000000.0 / 1048576.0);
printf("Alignment %2d - %6.3f bytes/cycle - %7.2f MiB/sec @ %0.2f GHz\n",align,bestbpc,bestbps,freq);
sumbpc += bestbpc;
}
sumbpc = sumbpc / 8.0;
printf("Average - %6.3f bytes/cycle - %7.2f MiB/sec @ 3 ghz\n",sumbpc,(sumbpc * 3000000000.0 / 1048576.0));
printf("Average - %6.3f bytes/cycle - %7.2f MiB/sec @ %0.2f GHz\n",sumbpc,
(sumbpc * freq * 1000000000.0 / 1048576.0),
freq);
fflush(NULL);
}

Expand Down
4 changes: 2 additions & 2 deletions doc/ryzen3.html
Original file line number Diff line number Diff line change
Expand Up @@ -1370,9 +1370,9 @@
</tr>
<tr class="good">
<td align="left"><a href="wyhash.txt">wyhash</a></td>
<td align="right">15817.88</td>
<td align="right">15733.67</td>
<td align="right">22.41</td>
<td align="right">432.69 (3)</td>
<td align="right">431.48 (1)</td>
<td align="right">938</td>
<td align="left"></td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ void test ( hashfunc<hashtype> hash, HashInfo* info )
sum += TinySpeedTest(hashfunc<hashtype>(info->hash),sizeof(hashtype),j,info->verification,true);
}
g_speed = sum = sum / 31.0;
printf("Average %6.3f cycles/hash\n",sum);
printf("Average %8.3f cycles/hash\n",sum);
printf("\n");
fflush(NULL);
} else {
Expand Down

0 comments on commit 8e66151

Please sign in to comment.