#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <yepCore.h>
#include <yepMath.h>
#include <yepLibrary.h>
double compute_entropy_naive(const double *pPointer, size_t pLength) {
double entropy = 0.0;
for (; pLength != 0; pLength--) {
const double p = *pPointer++;
entropy -= p * log(p);
}
return entropy;
}
#define BLOCK_SIZE 1000
double compute_entropy_yeppp(const double *p, size_t length) {
Yep64f entropy = 0.0;
Yep64f logP[BLOCK_SIZE];
for (size_t index = 0; index < length; index += BLOCK_SIZE) {
Yep64f dotProduct;
size_t blockLength = length - index;
if (blockLength > BLOCK_SIZE) {
blockLength = BLOCK_SIZE;
}
entropy -= dotProduct;
}
return entropy;
}
#define ARRAY_SIZE (1024*1024*128)
int main(int argc, char **argv) {
Yep64u startTimeNaive, startTimeYeppp, endTimeNaive, endTimeYeppp, frequency;
Yep64f *p = (Yep64f*)calloc(ARRAY_SIZE, sizeof(Yep64f));
for (size_t i = 0; i < ARRAY_SIZE; i++) {
p[i] = ((double)(rand() + 1)) / ((double)(RAND_MAX) + 1.0);
}
double entropy_naive = compute_entropy_naive(p, ARRAY_SIZE);
double entropy_yeppp = compute_entropy_yeppp(p, ARRAY_SIZE);
printf("Naive implementation:\n");
printf("\tEntropy = %lf\n", entropy_naive);
printf("\tTime = %lf\n", ((double)(endTimeNaive - startTimeNaive)) / ((double)(frequency)));
printf("Yeppp! implementation:\n");
printf("\tEntropy = %lf\n", entropy_yeppp);
printf("\tTime = %lf\n", ((double)(endTimeYeppp - startTimeYeppp)) / ((double)(frequency)));
free(p);
return 0;
}