1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
/*
* The MIT License
*
* Wavefront Alignment Algorithms
* Copyright (c) 2017 by Santiago Marco-Sola <santiagomsola@gmail.com>
*
* This file is part of Wavefront Alignment Algorithms.
*
* 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.
*
* PROJECT: Wavefront Alignment Algorithms
* AUTHOR(S): Santiago Marco-Sola <santiagomsola@gmail.com>
* DESCRIPTION: Individual WaveFront data structure
*/
#include "utils/commons.h"
#include "system/mm_allocator.h"
#include "wavefront.h"
/*
* Setup
*/
void wavefront_allocate(
wavefront_t* const wavefront,
const int wf_elements_allocated,
const bool allocate_backtrace,
mm_allocator_t* const mm_allocator) {
// Allocate memory
wavefront->wf_elements_allocated = wf_elements_allocated;
wavefront->offsets_mem = mm_allocator_calloc(
mm_allocator,wf_elements_allocated,wf_offset_t,false);
if (allocate_backtrace) {
wavefront->bt_pcigar_mem = mm_allocator_calloc(
mm_allocator,wf_elements_allocated,pcigar_t,false);
wavefront->bt_prev_mem = mm_allocator_calloc(
mm_allocator,wf_elements_allocated,bt_block_idx_t,false);
} else {
wavefront->bt_pcigar_mem = NULL;
}
}
void wavefront_resize(
wavefront_t* const wavefront,
const int wf_elements_allocated,
mm_allocator_t* const mm_allocator) {
// Set new size
wavefront->wf_elements_allocated = wf_elements_allocated;
// Reallocate offsets (Content is lost)
mm_allocator_free(mm_allocator,wavefront->offsets_mem);
wavefront->offsets_mem = mm_allocator_calloc(
mm_allocator,wf_elements_allocated,wf_offset_t,false);
// Reallocate backtrace (Content is lost)
if (wavefront->bt_pcigar_mem) {
mm_allocator_free(mm_allocator,wavefront->bt_pcigar_mem);
mm_allocator_free(mm_allocator,wavefront->bt_prev_mem);
wavefront->bt_pcigar_mem = mm_allocator_calloc(
mm_allocator,wf_elements_allocated,pcigar_t,false);
wavefront->bt_prev_mem = mm_allocator_calloc(
mm_allocator,wf_elements_allocated,bt_block_idx_t,false);
}
}
void wavefront_free(
wavefront_t* const wavefront,
mm_allocator_t* const mm_allocator) {
mm_allocator_free(mm_allocator,wavefront->offsets_mem);
if (wavefront->bt_pcigar_mem) {
mm_allocator_free(mm_allocator,wavefront->bt_pcigar_mem);
mm_allocator_free(mm_allocator,wavefront->bt_prev_mem);
}
}
/*
* Initialization
*/
void wavefront_init(
wavefront_t* const wavefront,
const int min_lo,
const int max_hi) {
// Limits
wavefront->null = false;
wavefront->lo = 1;
wavefront->hi = -1;
// Elements
wavefront->offsets = wavefront->offsets_mem - min_lo; // Center at k=0
if (wavefront->bt_pcigar_mem) {
wavefront->bt_occupancy_max = 0;
wavefront->bt_pcigar = wavefront->bt_pcigar_mem - min_lo; // Center at k=0
wavefront->bt_prev = wavefront->bt_prev_mem - min_lo; // Center at k=0
}
// Internals
wavefront->wf_elements_allocated_min = min_lo;
wavefront->wf_elements_allocated_max = max_hi;
wavefront->wf_elements_init_min = 0;
wavefront->wf_elements_init_max = 0;
}
void wavefront_init_null(
wavefront_t* const wavefront,
const int min_lo,
const int max_hi) {
// Limits
wavefront->null = true;
wavefront->lo = 1;
wavefront->hi = -1;
// Elements
wavefront->offsets = wavefront->offsets_mem - min_lo; // Center at k=0
if (wavefront->bt_pcigar_mem) {
wavefront->bt_occupancy_max = 0;
wavefront->bt_pcigar = wavefront->bt_pcigar_mem - min_lo; // Center at k=0
wavefront->bt_prev = wavefront->bt_prev_mem - min_lo; // Center at k=0
}
// Initialize
const int wf_elements = WAVEFRONT_LENGTH(min_lo,max_hi);
int i;
for (i=0;i<wf_elements;++i) {
wavefront->offsets_mem[i] = WAVEFRONT_OFFSET_NULL;
}
if (wavefront->bt_pcigar_mem) { // TODO: Really needed?
memset(wavefront->bt_pcigar_mem,0,wf_elements*sizeof(pcigar_t));
memset(wavefront->bt_prev_mem,0,wf_elements*sizeof(bt_block_idx_t));
}
// Internals
wavefront->wf_elements_allocated_min = min_lo;
wavefront->wf_elements_allocated_max = max_hi;
wavefront->wf_elements_init_min = min_lo;
wavefront->wf_elements_init_max = max_hi;
}
void wavefront_init_victim(
wavefront_t* const wavefront,
const int min_lo,
const int max_hi) {
// Delegate init
wavefront_init(wavefront,min_lo,max_hi);
// Set Null
wavefront->null = true;
}
/*
* Accessors
*/
void wavefront_set_limits(
wavefront_t* const wavefront,
const int lo,
const int hi) {
// Set effective limits
wavefront->lo = lo;
wavefront->hi = hi;
// Set initialization limits (all elms beyond are treated as Nulls)
wavefront->wf_elements_init_min = lo;
wavefront->wf_elements_init_max = hi;
}
/*
* Utils
*/
uint64_t wavefront_get_size(
wavefront_t* const wavefront) {
uint64_t total_size = wavefront->wf_elements_allocated*sizeof(wf_offset_t);
if (wavefront->bt_pcigar_mem) {
total_size += wavefront->wf_elements_allocated*(sizeof(pcigar_t)+sizeof(bt_block_idx_t));
}
return total_size;
}
|