Skip to content

Commit

Permalink
Print the range including 95% of the hash rate values
Browse files Browse the repository at this point in the history
Implement functions of getting the average value and standard deviation.
Print the hash rate range when the `BUILD_STAT` option is enabled.
Close #77.
  • Loading branch information
marktwtn committed Oct 7, 2018
1 parent 3fd323c commit f11fc40
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 24 deletions.
1 change: 0 additions & 1 deletion tests/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "pow_cl.h"
#endif

#include <inttypes.h>
#include <stdint.h>
#include <stdlib.h>

Expand Down
89 changes: 66 additions & 23 deletions tests/test-pow.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* Test program for pow_*.c */
#include "common.h"
#include "implcontext.h"
#include "math.h"

#define POW_TOTAL 100

#if defined(ENABLE_AVX)
extern ImplContext PoWAVX_Context;
Expand Down Expand Up @@ -36,6 +39,29 @@ const char *description[ ] = {
#endif
};

double getAvg(const double arr[], int arrLen) {
double avg, sum = 0;

for (int idx = 0; idx < arrLen; idx++) {
sum += arr[idx];
}
avg = sum / arrLen;

return avg;
}

double getStdDeviation(const double arr[], int arrLen) {
double sigma, variance = 0;
double avg = getAvg(arr, arrLen);

for (int idx = 0; idx < arrLen; idx++) {
variance += pow(arr[idx] - avg, 2);
}
sigma = sqrt(variance / arrLen);

return sigma;
}

int main()
{
char *trytes =
Expand Down Expand Up @@ -98,44 +124,61 @@ int main()
PoWFPGAAccel_Context,
#endif
};
#if defined(ENABLE_STAT)
double hashRateArr[POW_TOTAL];
int pow_total = POW_TOTAL;
#else
int pow_total = 1;
#endif


for (int idx = 0; idx < sizeof(ImplContextArr) / sizeof(ImplContext); idx++) {
printf("%s\n",description[idx]);
printf("%s\n", description[idx]);

ImplContext *PoW_Context_ptr = &ImplContextArr[idx];

/* test implementation with mwm = 14 */
initializeImplContext(PoW_Context_ptr);
void *pow_ctx = getPoWContext(PoW_Context_ptr, (int8_t *) trytes, mwm);
assert(pow_ctx);
doThePoW(PoW_Context_ptr, pow_ctx);
int8_t *ret_trytes = getPoWResult(PoW_Context_ptr, pow_ctx);
assert(ret_trytes);
PoW_Info pow_info = getPoWInfo(PoW_Context_ptr, pow_ctx);
freePoWContext(PoW_Context_ptr, pow_ctx);
destroyImplContext(PoW_Context_ptr);

Trytes_t *trytes_t = initTrytes(ret_trytes, TRANSACTION_TRYTES_LENGTH);
assert(trytes_t);
Trytes_t *hash_trytes = hashTrytes(trytes_t);
assert(hash_trytes);
Trits_t *ret_trits = trits_from_trytes(hash_trytes);
assert(ret_trits);
for (int count = 0; count < pow_total; count++) {
doThePoW(PoW_Context_ptr, pow_ctx);
int8_t *ret_trytes = getPoWResult(PoW_Context_ptr, pow_ctx);
assert(ret_trytes);
#if defined(ENABLE_STAT)
PoW_Info pow_info = getPoWInfo(PoW_Context_ptr, pow_ctx);
#endif

Trytes_t *trytes_t = initTrytes(ret_trytes, TRANSACTION_TRYTES_LENGTH);
assert(trytes_t);
Trytes_t *hash_trytes = hashTrytes(trytes_t);
assert(hash_trytes);
Trits_t *ret_trits = trits_from_trytes(hash_trytes);
assert(ret_trits);

/* Validation */
for (int i = HASH_TRITS_LENGTH - 1; i >= HASH_TRITS_LENGTH - mwm; i--) {
assert(ret_trits->data[i] == 0);
/* Validation */
for (int i = HASH_TRITS_LENGTH - 1; i >= HASH_TRITS_LENGTH - mwm; i--) {
assert(ret_trits->data[i] == 0);
}

free(ret_trytes);
freeTrobject(trytes_t);
freeTrobject(hash_trytes);
freeTrobject(ret_trits);

#if defined(ENABLE_STAT)
hashRateArr[count] = pow_info.hash_count / pow_info.time;
#endif
}

free(ret_trytes);
freeTrobject(trytes_t);
freeTrobject(hash_trytes);
freeTrobject(ret_trits);
freePoWContext(PoW_Context_ptr, pow_ctx);
destroyImplContext(PoW_Context_ptr);

printf("PoW execution times: %d times.\n", pow_total);
#if defined(ENABLE_STAT)
printf("Hash count: %"PRIu64"\n", pow_info.hash_count);
printf("PoW execution time: %.3f sec\n", pow_info.time);
printf("Hash rate: %.3lf kH/sec\n", pow_info.hash_count / pow_info.time / 1000);
printf("Hash rate average value: %.3lf kH/sec,\n", getAvg(hashRateArr, pow_total) / 1000);
printf("with the range +- %.3lf kH/sec including 95%% of the hash rate values.\n", 2 * getStdDeviation(hashRateArr, pow_total) / 1000);
#endif
printf("Success.\n");
}
Expand Down

0 comments on commit f11fc40

Please sign in to comment.