61 lines
1.4 KiB
C
61 lines
1.4 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <assert.h>
|
|
|
|
/* Non standard include */
|
|
#include <commtech.h>
|
|
|
|
#define CACHE_SIZE (32 * 1024)
|
|
|
|
|
|
static volatile uintptr_t *line_array;
|
|
static int write_idx, read_idx;
|
|
static int lines_per_calc;
|
|
|
|
#define CACHE_LINE_ENTRIES (CACHE_LINE_SIZE / sizeof(*line_array))
|
|
|
|
int init_calc(int lines_per_calc_param)
|
|
{
|
|
unsigned int i;
|
|
|
|
assert(!(CACHE_SIZE % (lines_per_calc_param * CACHE_LINE_SIZE)));
|
|
lines_per_calc = lines_per_calc_param;
|
|
write_idx = 0;
|
|
read_idx = 0;
|
|
srand(42);
|
|
if (posix_memalign((void **) &line_array, CACHE_LINE_SIZE, CACHE_SIZE))
|
|
{
|
|
fprintf(stderr, "calc_line: Unable to allocate memory for random line-sized calculation\n");
|
|
return -1;
|
|
}
|
|
for (i = 0; i < CACHE_SIZE / sizeof(*line_array); i++)
|
|
line_array[i] = rand();
|
|
return 0;
|
|
}
|
|
|
|
void **do_calc(void)
|
|
{
|
|
int next_stop_read_idx, mres, cur_write_idx;
|
|
|
|
mres = 0;
|
|
next_stop_read_idx = read_idx + lines_per_calc * CACHE_LINE_ENTRIES;
|
|
for (; read_idx < next_stop_read_idx; read_idx += CACHE_LINE_ENTRIES)
|
|
{
|
|
mres += line_array[read_idx];
|
|
line_array[read_idx]++;
|
|
}
|
|
read_idx %= CACHE_SIZE / sizeof(*line_array);
|
|
cur_write_idx = write_idx;
|
|
write_idx++;
|
|
write_idx %= CACHE_SIZE / sizeof(*line_array);
|
|
line_array[cur_write_idx] = mres;
|
|
return (void **) &line_array[cur_write_idx];
|
|
}
|
|
|
|
int end_calc(void)
|
|
{
|
|
free((void *) line_array);
|
|
return 0;
|
|
}
|