Skip to content

Instantly share code, notes, and snippets.

@codyd51
Created October 9, 2016 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codyd51/4df2e27892ec333021ea35323a16a618 to your computer and use it in GitHub Desktop.
Save codyd51/4df2e27892ec333021ea35323a16a618 to your computer and use it in GitHub Desktop.
Interpolate vs interpolatev speed testing
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "eam.h"
#include "CoMDTypes.h"
#include "interpolatev.h"
#include "interpolate.h"
#define SINE_TABLE_SIZE 1000
#define TABLE_SIZE 1024
#define RUN_COUNT 1000000
//#define RUN_COUNT 5
#define DEBUG 1
typedef enum result_type {
V_RESULT = 0,
ORIG_RESULT,
} result_type;
static FILE* fp;
void log_result(result_type type, double t) {
fprintf(fp, "%f", t);
switch (type) {
case V_RESULT:
fprintf(fp, " ");
break;
case ORIG_RESULT:
fprintf(fp, "\n");
break;
}
}
int main(){
srand(time(0));
//create actual array of 1000 evenly spaced values from pi / 4 to pi / 2
double sine_increment = (2 * M_PI) / SINE_TABLE_SIZE;
double* array = malloc(sizeof(double) * SINE_TABLE_SIZE);
for (int i = 0; i < SINE_TABLE_SIZE; i++) {
array[i] = -M_PI + (sine_increment * i);
#ifdef DEBUG
printf("array[%d] %f\n", i, array[i]);
#endif
}
double increment = (2 * M_PI) / TABLE_SIZE;
double* shifted_array = malloc(sizeof(double) * TABLE_SIZE);
for (int i = 0; i < SINE_TABLE_SIZE; i++) {
shifted_array[i] = -M_PI + (increment * i);
}
//create array of lookup indexes
//the indexes we will use to reference the above values are randomized
//this is so it can't be optimized and put in a vector reg
int* lookup_idxs = malloc(sizeof(int) * TABLE_SIZE);
for (int i = 0; i < TABLE_SIZE; i++) {
lookup_idxs[i] = rand() % TABLE_SIZE;
#ifdef DEBUG
printf("lookup[%d] %d\n", i, lookup_idxs[i]);
#endif
}
//construct new table of sin values mapped to random locations
double* array_unsorted = malloc(sizeof(double) * TABLE_SIZE);
for (int i = 0; i < TABLE_SIZE; i++) {
array_unsorted[i] = shifted_array[lookup_idxs[i]];
#ifdef DEBUG
printf("%f mapped to index %d\n", array_unsorted[i], lookup_idxs[i]);
#endif
}
//build InterpolationObject
InterpolationObject* interp_table = malloc(sizeof(InterpolationObject));
interp_table->n = SINE_TABLE_SIZE;
interp_table->x0 = -M_PI;
interp_table->invDx = 1/sine_increment;
interp_table->values = array;
double* out_vals = malloc(sizeof(TABLE_SIZE));
double* deriv_out_vals = malloc(sizeof(TABLE_SIZE));
fp = fopen("splitv.txt", "w");
for (int split_exp = 0; split_exp < 32; split_exp++) {
int split = pow(2, split_exp);
int subarray_size = TABLE_SIZE / split;
fprintf(fp, "%d subsections of size %d\n", split, subarray_size);
for (int runs = 0; runs < 5; runs++) {
clock_t begin = clock();
for (int i = 0; i < RUN_COUNT; i++) {
for (int j = 0; j < split; j++) {
double* subarray = *(&array_unsorted + (subarray_size * j));
interpolatev(interp_table, subarray, out_vals, deriv_out_vals, subarray_size);
}
}
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
log_result(V_RESULT, time_spent);
begin = clock();
for (int i = 0; i < RUN_COUNT; i++) {
for (int i = 0; i < TABLE_SIZE; i++) {
interpolateOrig(interp_table, array_unsorted[i], &out_vals[i], &deriv_out_vals[i]);
//interpolateOrig(interp_table, shifted_array[lookup_idxs[i]], &out_vals[i], &deriv_out_vals[i]);
}
}
end = clock();
FILE* null = fopen("/dev/null", "w");
fwrite(out_vals, sizeof(double), TABLE_SIZE, null);
fwrite(deriv_out_vals, sizeof(double), TABLE_SIZE, null);
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
log_result(ORIG_RESULT, time_spent);
}
fprintf(fp, "\n");
fflush(fp);
}
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment