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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
/*
//
// Copyright 2010 SRI International
//
// This file is part of the Computational Morphometry Toolkit.
//
// http://www.nitrc.org/projects/cmtk/
//
// The Computational Morphometry Toolkit is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// The Computational Morphometry Toolkit is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with the Computational Morphometry Toolkit. If not, see
// <http://www.gnu.org/licenses/>.
//
// $Revision: 2012 $
//
// $LastChangedDate: 2010-07-19 15:14:26 -0700 (Mon, 19 Jul 2010) $
//
// $LastChangedBy: torstenrohlfing $
//
*/
#include "cmtkDeviceHistogram_kernels.h"
#include <cstdio>
#include <cstdlib>
extern __shared__ float shared[];
__global__
void
cmtkDeviceHistogramEntropyKernel( float* result, const float *dataPtr )
{
int tx = threadIdx.x;
// first, load data into shared memory
float *working = &shared[0];
working[tx] = dataPtr[tx];
__syncthreads();
// second, compute sum of all bin values via butterfly
for ( int bit = 1; bit < blockDim.x; bit <<= 1 )
{
const float sum = working[tx] + working[tx^bit];
__syncthreads();
working[tx] = sum;
__syncthreads();
}
// third, normalize
if ( working[tx] )
{
working[tx] = dataPtr[tx] / working[tx];
}
// fourth, do p*log(p)
if ( working[tx] > 0 )
{
working[tx] *= log( working[tx] );
}
else
{
working[tx] = 0;
}
__syncthreads();
// fifth, another butterfly to compute \sum[p*log(p)]
for ( int bit = 1; bit < blockDim.x; bit <<= 1 )
{
const float sum = working[tx] + working[tx^bit];
__syncthreads();
working[tx] = sum;
__syncthreads();
}
result[tx] = -working[tx];
}
void
cmtkDeviceHistogramEntropy( float* result, const float* dataPtr, int numberOfBins )
{
dim3 dimBlock( numberOfBins, 1 );
dim3 dimGrid( 1, 1 );
// start kernel and allocate shared memory for "numberOfBins" floats
cmtkDeviceHistogramEntropyKernel<<<dimGrid,dimBlock,numberOfBins*sizeof(float)>>>( result, dataPtr );
}
__global__
void
cmtkDeviceHistogramConsolidate( float* histPtr, float* localPtr, const int numberOfBins, const int numberOfThreads )
{
int tx = threadIdx.x;
// finally, add all thread working histograms to output histogram
for ( int idx = 1+tx; idx <= numberOfBins; idx += blockDim.x )
{
float sum = 0;
for ( int hx = 0; hx < numberOfThreads; ++hx )
{
sum += localPtr[ idx + hx*(1+numberOfBins) ];
}
histPtr[idx-1] += sum;
}
}
__global__
void
cmtkDeviceHistogramPopulateKernel( float* histPtr, float* localPtr, const float *dataPtr, const float rangeFrom, const float rangeTo, const int numberOfBins, const int numberOfSamples )
{
int tx = threadIdx.x;
int offs = tx + blockDim.x * blockIdx.x;
int skip = blockDim.x * gridDim.x;
// working histogram for this thread
float* working = localPtr + (numberOfBins+1)*offs;
// start by resetting all histogram bins to 0
for ( int i = 0; i < numberOfBins; ++i )
working[i] = 0;
// populate histogram bins
const float binScale = (numberOfBins-1) / (rangeTo - rangeFrom);
for ( int offset = offs; offset < numberOfSamples; offset += skip )
{
int index = 1+truncf( fmaxf( 0, fminf( numberOfBins-1, (dataPtr[offset] - rangeFrom) * binScale ) ) ); // 1+... for consistency with masked computation; bin0 is ignored in final analysis.
++working[ index ];
}
}
__global__
void
cmtkDeviceHistogramPopulateLogKernel( float* histPtr, float* localPtr, const float *dataPtr, const float rangeFrom, const float rangeTo, const int numberOfBins, const int numberOfSamples )
{
int tx = threadIdx.x;
int offs = tx + blockDim.x * blockIdx.x;
int skip = blockDim.x * gridDim.x;
// working histogram for this thread
float* working = localPtr + (numberOfBins+1)*offs;
// start by resetting all histogram bins to 0
for ( int i = 0; i < numberOfBins; ++i )
working[i] = 0;
// populate histogram bins
const float binScale = (numberOfBins-1) / (rangeTo - rangeFrom);
const float logNumBins = log( static_cast<float>( numberOfBins ) );
for ( int offset = offs; offset < numberOfSamples; offset += skip )
{
int index = 1+truncf( (numberOfBins-1) * fmaxf( 0, fminf( 1, log((1 + dataPtr[offset]-rangeFrom)*binScale)/logNumBins ) ) );
++working[ index ];
}
}
void
cmtkDeviceHistogramPopulate( float* histPtr, const float* dataPtr, const float rangeFrom, const float rangeTo, const bool logScale, const int numberOfBins, const int numberOfSamples )
{
dim3 dimBlock( 256, 1 );
dim3 dimGrid( 16, 1 );
const size_t nThreads = 16*256;
const size_t lBytes = sizeof(float) * (numberOfBins+1) * nThreads;
float* localPtr;
if ( cudaMalloc( &localPtr, lBytes ) != cudaSuccess )
{
fprintf( stderr, "ERROR: cudaMalloc() failed with error %s\n",cudaGetErrorString( cudaGetLastError() ) );
exit( 1 );
}
if ( cudaMemset( localPtr, 0, lBytes ) != cudaSuccess )
{
fprintf( stderr, "ERROR: cudaMemset() failed with error %s\n",cudaGetErrorString( cudaGetLastError() ) );
exit( 1 );
}
if ( logScale )
cmtkDeviceHistogramPopulateLogKernel<<<dimGrid,dimBlock>>>( histPtr, localPtr, dataPtr, rangeFrom, rangeTo, numberOfBins, numberOfSamples );
else
cmtkDeviceHistogramPopulateKernel<<<dimGrid,dimBlock>>>( histPtr, localPtr, dataPtr, rangeFrom, rangeTo, numberOfBins, numberOfSamples );
cudaError_t kernelError = cudaGetLastError();
if ( kernelError != cudaSuccess )
{
fprintf( stderr, "ERROR: CUDA kernel failed with error %s\n", cudaGetErrorString( kernelError ) );
exit( 1 );
}
dim3 dimBlock2( 256, 1 );
dim3 dimGrid2( 1, 1 );
cmtkDeviceHistogramConsolidate<<<dimGrid2,dimBlock2>>>( histPtr, localPtr, numberOfBins, nThreads );
kernelError = cudaGetLastError();
if ( kernelError != cudaSuccess )
{
fprintf( stderr, "ERROR: CUDA kernel failed with error %s\n", cudaGetErrorString( kernelError ) );
exit( 1 );
}
cudaFree( localPtr );
}
__global__
void
cmtkDeviceHistogramPopulateWithMaskKernel( float* histPtr, float* localPtr, const float *dataPtr, const int *maskPtr, const float rangeFrom, const float rangeTo, const int numberOfBins, const int numberOfSamples )
{
int tx = threadIdx.x;
int offs = tx + blockDim.x * blockIdx.x;
int skip = blockDim.x * gridDim.x;
// working histogram for this thread in shared memory
float* working = localPtr + (numberOfBins+1)*offs;
// populate histogram bins
const float binScale = (numberOfBins-1) / (rangeTo - rangeFrom);
for ( int offset = offs; offset < numberOfSamples; offset += skip )
{
const float d = (dataPtr[offset] - rangeFrom) * binScale;
const float m = maskPtr[offset];
float binIndex = fmaxf( 0, fminf( numberOfBins-1, d ) );
const int index = truncf( (1+binIndex) * m );
++working[ index ];
}
}
__global__
void
cmtkDeviceHistogramPopulateLogWithMaskKernel( float* histPtr, float* localPtr, const float *dataPtr, const int *maskPtr, const float rangeFrom, const float rangeTo, const int numberOfBins, const int numberOfSamples )
{
int tx = threadIdx.x;
int offs = tx + blockDim.x * blockIdx.x;
int skip = blockDim.x * gridDim.x;
// working histogram for this thread in shared memory
float* working = localPtr + (numberOfBins+1)*offs;
// populate histogram bins
const float binScale = (numberOfBins-1) / (rangeTo - rangeFrom);
const float logNumBins = log( static_cast<float>( numberOfBins ) );
for ( int offset = offs; offset < numberOfSamples; offset += skip )
{
const float d = log((1 + dataPtr[offset]-rangeFrom)*binScale);
const float m = maskPtr[offset];
float binIndex = (numberOfBins-1) * fmaxf( 0, fminf( 1, d / logNumBins ) );
const int index = truncf( (1+binIndex) * m );
++working[ index ];
}
}
void
cmtkDeviceHistogramPopulate( float* histPtr, const float* dataPtr, const int* maskPtr, const float rangeFrom, const float rangeTo, const bool logScale, const int numberOfBins, const int numberOfSamples )
{
dim3 dimBlock( 256, 1 );
dim3 dimGrid( 16, 1 );
const size_t nThreads = 16*256;
const size_t lBytes = sizeof(float) * (numberOfBins+1) * nThreads;
float* localPtr;
if ( cudaMalloc( &localPtr, lBytes ) != cudaSuccess )
{
fprintf( stderr, "ERROR: cudaMalloc() failed with error %s\n",cudaGetErrorString( cudaGetLastError() ) );
exit( 1 );
}
if ( cudaMemset( localPtr, 0, lBytes ) != cudaSuccess )
{
fprintf( stderr, "ERROR: cudaMemset() failed with error %s\n",cudaGetErrorString( cudaGetLastError() ) );
exit( 1 );
}
if ( logScale )
cmtkDeviceHistogramPopulateLogWithMaskKernel<<<dimGrid,dimBlock>>>( histPtr, localPtr, dataPtr, maskPtr, rangeFrom, rangeTo, numberOfBins, numberOfSamples );
else
cmtkDeviceHistogramPopulateWithMaskKernel<<<dimGrid,dimBlock>>>( histPtr, localPtr, dataPtr, maskPtr, rangeFrom, rangeTo, numberOfBins, numberOfSamples );
cudaError_t kernelError = cudaGetLastError();
if ( kernelError != cudaSuccess )
{
fprintf( stderr, "ERROR: CUDA kernel failed with error %s\n", cudaGetErrorString( kernelError ) );
exit( 1 );
}
dim3 dimBlock2( 256, 1 );
dim3 dimGrid2( 1, 1 );
cmtkDeviceHistogramConsolidate<<<dimGrid2,dimBlock2>>>( histPtr, localPtr, numberOfBins, nThreads );
kernelError = cudaGetLastError();
if ( kernelError != cudaSuccess )
{
fprintf( stderr, "ERROR: CUDA kernel failed with error %s\n", cudaGetErrorString( kernelError ) );
exit( 1 );
}
cudaFree( localPtr );
}
|