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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
#include "stdafx.h"
/*
This file is a part of KMC software distributed under GNU GPL 3 licence.
The homepage of the KMC project is http://sun.aei.polsl.pl/kmc
Authors: Sebastian Deorowicz, Agnieszka Debudaj-Grabysz, Marek Kokot
Version: 2.3.0
Date : 2015-08-21
*/
#include <stdio.h>
#include "radix.h"
//----------------------------------------------------------------------------------
/*Parallel radix sort. The input data to be sorted are divided evenly among threads.
Each thread is responsible for building a local histogram to enable sorting keys
according to a given digit. Then a global histogram is created as a combination
of local ones and the write offset (location) to which each digit should be written
is computed. Finally, threads scatter the data to the appropriate locations.*/
template<typename COUNTER_TYPE>
void RadixOMP_uint8(uint32 *SourcePtr, uint32 *DestPtr, const int64 SourceSize, unsigned rec_size, unsigned data_offset, unsigned data_size, const unsigned n_phases, const unsigned n_threads)
{
/* SourceSize - number of records */
/* rec_size - in bytes */
/* data_offset - in bytes*/
/* data_size - in bytes - not used now */
#ifdef WIN32
__declspec( align( WIN_ALIGNMENT ) ) COUNTER_TYPE ByteCounter[MAX_NUM_THREADS][256];
#else
COUNTER_TYPE ByteCounter[MAX_NUM_THREADS][256] __attribute__((aligned(ALIGNMENT)));
#endif
#ifdef WIN32
__declspec( align( WIN_ALIGNMENT ) ) COUNTER_TYPE globalHisto[256];
#else
COUNTER_TYPE globalHisto[256] __attribute__((aligned(ALIGNMENT)));
#endif
#pragma omp parallel num_threads(n_threads)
{
int myID = omp_get_thread_num();
uint8_t ByteIndex = 0;
long long i;
COUNTER_TYPE prevSum;
COUNTER_TYPE temp;
uint32 n;
int private_i;
int byteValue;
int64 SourceSize_in_bytes = SourceSize * rec_size;
uint8_t *char_ptr_tempSource = (uint8_t*)(SourcePtr);
uint8_t *char_ptr_tempDest = (uint8_t*)(DestPtr);
uint8_t *char_tempPtr;
#ifdef WIN32
__declspec( align( WIN_ALIGNMENT ) ) COUNTER_TYPE privateByteCounter[256] = {0};
#else
__attribute__((aligned(ALIGNMENT))) COUNTER_TYPE privateByteCounter[256] = {0};
#endif
for(uint32 privatePhaseCounter = 0; privatePhaseCounter < n_phases; privatePhaseCounter++)
{
#pragma omp for private(i) schedule(static)
for(i = data_offset; i < SourceSize_in_bytes; i = i + rec_size)
{
byteValue = *(&char_ptr_tempSource[i] + ByteIndex);
++privateByteCounter[byteValue];
}
A_memcpy(&ByteCounter[myID][0], privateByteCounter, sizeof(privateByteCounter));
#pragma omp barrier
#pragma omp for schedule(static)
for(i = 0; i < 256; ++i)
{
prevSum = 0;
for(n = 0; n < n_threads; n++)
{
temp = ByteCounter[n][i];
ByteCounter[n][i] = prevSum;
prevSum += temp;
}
globalHisto[i] = prevSum;
}
#pragma omp single
{
prevSum = 0;
for(i = 0; i < 256; ++i)
{
temp = globalHisto[i];
globalHisto[i] = prevSum;
prevSum += temp;
}
}
for (private_i = 0; private_i < 256; private_i++)
ByteCounter[myID][private_i] += globalHisto[private_i];
A_memcpy(privateByteCounter, &ByteCounter[myID][0], sizeof(privateByteCounter));
#pragma omp for schedule(static)
for(i = data_offset; i < SourceSize_in_bytes; i = i + rec_size)
{
byteValue = *(&char_ptr_tempSource[i] + ByteIndex);
memcpy(&char_ptr_tempDest[privateByteCounter[byteValue] * rec_size], &char_ptr_tempSource[i - data_offset], rec_size);
(privateByteCounter[byteValue])++;
}
#pragma omp barrier
char_tempPtr = char_ptr_tempDest;
char_ptr_tempDest = char_ptr_tempSource;
char_ptr_tempSource = char_tempPtr;
ByteIndex++;
memset(privateByteCounter, 0, sizeof(privateByteCounter));
}
}
}
//----------------------------------------------------------------------------------
void RadixSort_uint8(uint32 *&data_ptr, uint32 *&tmp_ptr, uint64 size, unsigned rec_size, unsigned data_offset, unsigned data_size, const unsigned n_phases, const unsigned n_threads)
{
if(size * rec_size >= (1ull << 32))
RadixOMP_uint8<uint64>(data_ptr, tmp_ptr, size, rec_size, data_offset, data_size, n_phases, n_threads);
else
RadixOMP_uint8<uint32>(data_ptr, tmp_ptr, size, rec_size, data_offset, data_size, n_phases, n_threads);
}
//----------------------------------------------------------------------------------
/*Parallel radix sort. Parallelization scheme taken from
Satish, N., Kim, C., Chhugani, J., Nguyen, A.D., Lee, V.W., Kim, D., Dubey, P. (2010).
Fast Sort on CPUs and GPUs. A Case for Bandwidth Oblivious SIMD Sort.
Proc. of the 2010 Int. Conf. on Management of data, pp. 351362.
The usage of software-managed buffers in the writing phase results in diminishing
the influence of irregular memory accesses. As the number of cache conflict misses
is reduced better efficiency is reached.*/
template<typename COUNTER_TYPE, typename INT_TYPE>
void RadixOMP_buffer(CMemoryPool *pmm_radix_buf, uint64 *Source, uint64 *Dest, const int64 SourceSize, const unsigned n_phases, const unsigned n_threads)
{
#ifdef WIN32
__declspec( align( WIN_ALIGNMENT ) ) COUNTER_TYPE ByteCounter[MAX_NUM_THREADS][256];
#else
COUNTER_TYPE ByteCounter[MAX_NUM_THREADS][256] __attribute__((aligned(ALIGNMENT)));
#endif
#ifdef WIN32
__declspec( align( WIN_ALIGNMENT ) ) COUNTER_TYPE globalHisto[256];
#else
COUNTER_TYPE globalHisto[256] __attribute__((aligned(ALIGNMENT)));
#endif
#pragma omp parallel num_threads(n_threads)
{
int myID = omp_get_thread_num();
uint8_t ByteIndex = 0;
long long i;
COUNTER_TYPE prevSum;
COUNTER_TYPE temp;
uint32 n;
int index_x;
int private_i;
int byteValue;
uint64 *tempSource = Source;
uint64 *tempDest = Dest;
uint64 *tempPtr;
uint64 *raw_Buffer;
pmm_radix_buf->reserve(raw_Buffer);
uint64 *Buffer = raw_Buffer;
while(((unsigned long long) Buffer) % ALIGNMENT)
Buffer++;
#ifdef WIN32
__declspec( align( WIN_ALIGNMENT ) ) COUNTER_TYPE privateByteCounter[256] = {0};
#else
__attribute__((aligned(ALIGNMENT))) COUNTER_TYPE privateByteCounter[256] = {0};
#endif
for(uint32 privatePhaseCounter = 0; privatePhaseCounter < n_phases; privatePhaseCounter++)
{
#pragma omp for private(i) schedule(static)
for(i = 0; i < SourceSize; ++i)
{
byteValue = *(reinterpret_cast<const uint8_t*>(&tempSource[i]) + ByteIndex);
++privateByteCounter[byteValue];
}
A_memcpy(&ByteCounter[myID][0], privateByteCounter, sizeof(privateByteCounter));
#pragma omp barrier
#pragma omp for schedule(static)
for(i = 0; i < 256; ++i)
{
prevSum = 0;
for(n = 0; n < n_threads; n++)
{
temp = ByteCounter[n][i];
ByteCounter[n][i] = prevSum;
prevSum += temp;
}
globalHisto[i] = prevSum;
}
#pragma omp single
{
prevSum = 0;
for(i = 0; i < 256; ++i)
{
temp = globalHisto[i];
globalHisto[i] = prevSum;
prevSum += temp;
}
}
for (private_i = 0; private_i < 256; private_i++)
ByteCounter[myID][private_i] += globalHisto[private_i];
A_memcpy(privateByteCounter, &ByteCounter[myID][0], sizeof(privateByteCounter));
#pragma omp for schedule(static)
for(i = 0; i < SourceSize; ++i)
{
byteValue = *(reinterpret_cast<const uint8_t*>(&tempSource[i]) + ByteIndex);
index_x = privateByteCounter[byteValue] % BUFFER_WIDTH;
Buffer[byteValue * BUFFER_WIDTH + index_x] = tempSource[i];
privateByteCounter[byteValue]++;
if(index_x == (BUFFER_WIDTH -1))
A_memcpy ( &tempDest[privateByteCounter[byteValue] - (BUFFER_WIDTH)], &Buffer[byteValue * BUFFER_WIDTH], BUFFER_WIDTH *sizeof(uint64) );
} //end_for
INT_TYPE elemInBuffer;
INT_TYPE index_stop;
INT_TYPE index_start;
INT_TYPE elemWrittenIntoBuffer;
for(private_i = 0; private_i < 256; private_i++)
{
index_stop = privateByteCounter[private_i] % BUFFER_WIDTH;
index_start = ByteCounter[myID][private_i] % BUFFER_WIDTH;
elemWrittenIntoBuffer = privateByteCounter[private_i] - ByteCounter[myID][private_i];
if((index_stop - elemWrittenIntoBuffer) <= 0)
elemInBuffer = index_stop;
else
elemInBuffer = index_stop - index_start;
if(elemInBuffer != 0)
A_memcpy ( &tempDest[privateByteCounter[private_i] - elemInBuffer], &Buffer[private_i * BUFFER_WIDTH + (privateByteCounter[private_i] - elemInBuffer)%BUFFER_WIDTH], (elemInBuffer)*sizeof(uint64) );
}
#pragma omp barrier
tempPtr = tempDest;
tempDest = tempSource;
tempSource = tempPtr;
ByteIndex++;
memset(privateByteCounter, 0, sizeof(privateByteCounter));
}
pmm_radix_buf->free(raw_Buffer);
}
}
//----------------------------------------------------------------------------------
void RadixSort_buffer(CMemoryPool *pmm_radix_buf, uint64 *&data, uint64 *&tmp, uint64 size, const unsigned n_phases, const unsigned n_threads)
{
if(size >= (1ull << 31))
RadixOMP_buffer<uint64, int64>(pmm_radix_buf, data, tmp, size, n_phases, n_threads);
else
RadixOMP_buffer<uint32, int32>(pmm_radix_buf, data, tmp, size, n_phases, n_threads);
}
// ***** EOF
|