/* * Copyright (C) 2011 Thomas Preud'homme * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include #include #include #include /* Non standard include */ #include #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; }