GPU azimuthal profile: sum the corrected values in fixed point
The fused adaptive spot finder's azimuthal profile (and the standalone AzIntEngineGPU) summed the corrected pixel values - pixel times a float correction - with float atomicAdd, in shared memory and then across blocks. Float addition is not associative, so the per-ring sums depended on the order the threads and blocks happened to arrive in, and on the grid size chosen for the GPU. The profile, and the per-image background estimate read from it, moved in their last bits between two runs of the same command: on a thaumatin JUNGFRAU 4M sweep one bkg value in _plot.txt differed in the 4th digit between runs. The raw ring sums that drive detection were already exact integers; only the reported profile was affected. Each corrected value (and its square) is now quantised to 2^-12 on its own and summed as a signed 64-bit integer, the convention the raw ring sums and the Bragg-integration profile already use. Integer addition is associative, so the sums are the same whatever the order or grid. The repeat test of the fused finder now also requires the profile and its std to repeat bit for bit (it failed on the old kernel at the first repeat); the AzIntEngineGPU test re-runs 20 times. Fused finder benchmark: 0.313 -> 0.324 ms/frame. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SVmAWnzCmRKAXVUCdc4iNi
This commit is contained in:
1 parent
af3ea9ccad
commit
f290e40d20
5 files changed
+126
-65
No files matched your search
@@ -8,9 +8,21 @@ inline void cuda_err(cudaError_t val) {
|
||||
throw JFJochException(JFJochExceptionCategory::GPUCUDAError, cudaGetErrorString(val));
|
||||
}
|
||||
|
||||
// The sums are fixed point, as the adaptive spot finder's corrected sums are (CORR_FIXED there): a
|
||||
// float atomicAdd adds in whatever order the threads and blocks arrive, so the profile moved in its
|
||||
// last bits between two runs on the same image. Quantised value by value, the sum is integer addition
|
||||
// and no longer depends on the order. 2^12: a quantum of 2.4e-4 of a count, room for ~2e15 per ring.
|
||||
constexpr float AZINT_FIXED = 4096.0f; // 2^12
|
||||
|
||||
// Signed, carried as two's complement for the unsigned 64-bit atomicAdd.
|
||||
__device__ __forceinline__ unsigned long long azint_fixed(float v) {
|
||||
return static_cast<unsigned long long>(static_cast<long long>(llrintf(v * AZINT_FIXED)));
|
||||
}
|
||||
|
||||
// Pushes one ring run's totals to the shared accumulators; nothing for an empty run.
|
||||
__device__ __forceinline__ void flush_azim_run(float *s_sum, float *s_sum2, uint32_t *s_count,
|
||||
int b, float r_sum, float r_sum2, uint32_t r_count) {
|
||||
__device__ __forceinline__ void flush_azim_run(unsigned long long *s_sum, unsigned long long *s_sum2,
|
||||
uint32_t *s_count, int b, unsigned long long r_sum,
|
||||
unsigned long long r_sum2, uint32_t r_count) {
|
||||
if (r_count == 0) return; // also covers the initial "no ring yet"
|
||||
atomicAdd(&s_sum[b], r_sum);
|
||||
atomicAdd(&s_sum2[b], r_sum2);
|
||||
@@ -22,21 +34,21 @@ void gpu_azim_shared(
|
||||
const uint16_t *__restrict__ pixel_to_bin,
|
||||
const float *__restrict__ corrections,
|
||||
const int32_t *__restrict__ input_buffer,
|
||||
float *__restrict__ azint_sum,
|
||||
float *__restrict__ azint_sum2,
|
||||
unsigned long long *__restrict__ azint_sum,
|
||||
unsigned long long *__restrict__ azint_sum2,
|
||||
uint32_t *__restrict__ azint_count,
|
||||
size_t num_pixels,
|
||||
int azint_bins) {
|
||||
extern __shared__ float shared[];
|
||||
extern __shared__ unsigned long long shared[];
|
||||
|
||||
float *s_sum = shared;
|
||||
float *s_sum2 = &s_sum[azint_bins];
|
||||
unsigned long long *s_sum = shared;
|
||||
unsigned long long *s_sum2 = &s_sum[azint_bins];
|
||||
uint32_t *s_count = (uint32_t *) &s_sum2[azint_bins];
|
||||
|
||||
// Initialize shared memory
|
||||
for (int i = threadIdx.x; i < azint_bins; i += blockDim.x) {
|
||||
s_sum[i] = 0.0f;
|
||||
s_sum2[i] = 0.0f;
|
||||
s_sum[i] = 0;
|
||||
s_sum2[i] = 0;
|
||||
s_count[i] = 0;
|
||||
}
|
||||
|
||||
@@ -58,7 +70,7 @@ void gpu_azim_shared(
|
||||
const float cq[4] = {c4.x, c4.y, c4.z, c4.w};
|
||||
|
||||
int r_b = -1;
|
||||
float r_sum = 0.0f, r_sum2 = 0.0f;
|
||||
unsigned long long r_sum = 0, r_sum2 = 0;
|
||||
uint32_t r_count = 0;
|
||||
#pragma unroll
|
||||
for (int k = 0; k < 4; k++) {
|
||||
@@ -68,11 +80,11 @@ void gpu_azim_shared(
|
||||
if (b != r_b) {
|
||||
flush_azim_run(s_sum, s_sum2, s_count, r_b, r_sum, r_sum2, r_count);
|
||||
r_b = b;
|
||||
r_sum = 0.0f; r_sum2 = 0.0f; r_count = 0;
|
||||
r_sum = 0; r_sum2 = 0; r_count = 0;
|
||||
}
|
||||
const float val = static_cast<float>(v) * cq[k];
|
||||
r_sum += val;
|
||||
r_sum2 += val * val;
|
||||
r_sum += azint_fixed(val);
|
||||
r_sum2 += azint_fixed(val * val);
|
||||
r_count += 1;
|
||||
}
|
||||
flush_azim_run(s_sum, s_sum2, s_count, r_b, r_sum, r_sum2, r_count);
|
||||
@@ -83,8 +95,8 @@ void gpu_azim_shared(
|
||||
const int32_t v = input_buffer[idx];
|
||||
if (bin < azint_bins && v != INT32_MIN && v != INT32_MAX) {
|
||||
const float val = static_cast<float>(v) * corrections[idx];
|
||||
atomicAdd(&s_sum[bin], val);
|
||||
atomicAdd(&s_sum2[bin], val * val);
|
||||
atomicAdd(&s_sum[bin], azint_fixed(val));
|
||||
atomicAdd(&s_sum2[bin], azint_fixed(val * val));
|
||||
atomicAdd(&s_count[bin], 1);
|
||||
}
|
||||
}
|
||||
@@ -104,8 +116,8 @@ void gpu_azim(
|
||||
const uint16_t *__restrict__ pixel_to_bin,
|
||||
const float *__restrict__ corrections,
|
||||
const int32_t *__restrict__ input_buffer,
|
||||
float *__restrict__ azint_sum,
|
||||
float *__restrict__ azint_sum2,
|
||||
unsigned long long *__restrict__ azint_sum,
|
||||
unsigned long long *__restrict__ azint_sum2,
|
||||
uint32_t *__restrict__ azint_count,
|
||||
size_t num_pixels,
|
||||
int azint_bins) {
|
||||
@@ -119,9 +131,8 @@ void gpu_azim(
|
||||
|
||||
if (bin < azint_bins && valid) {
|
||||
const float val = static_cast<float>(v) * corrections[idx];
|
||||
const float val2 = val * val;
|
||||
atomicAdd(&azint_sum[bin], val);
|
||||
atomicAdd(&azint_sum2[bin], val2);
|
||||
atomicAdd(&azint_sum[bin], azint_fixed(val));
|
||||
atomicAdd(&azint_sum2[bin], azint_fixed(val * val));
|
||||
atomicAdd(&azint_count[bin], 1);
|
||||
}
|
||||
}
|
||||
@@ -133,8 +144,10 @@ AzIntEngineGPU::AzIntEngineGPU(const AzimuthalIntegrationMapping &integration, s
|
||||
gpu_sum(azint_bins),
|
||||
gpu_sum2(azint_bins),
|
||||
gpu_count(azint_bins),
|
||||
cpu_sum_reg(azint_sum),
|
||||
cpu_sum2_reg(azint_sum2),
|
||||
fixed_sum(azint_bins),
|
||||
fixed_sum2(azint_bins),
|
||||
cpu_sum_reg(fixed_sum),
|
||||
cpu_sum2_reg(fixed_sum2),
|
||||
cpu_count_reg(azint_count) {
|
||||
|
||||
int device = 0;
|
||||
@@ -145,7 +158,7 @@ AzIntEngineGPU::AzIntEngineGPU(const AzimuthalIntegrationMapping &integration, s
|
||||
threads = 128;
|
||||
blocks = 4 * prop.multiProcessorCount;
|
||||
shared_size = prop.sharedMemPerBlock;
|
||||
shared_needed = azint_bins * (2 * sizeof(float) + sizeof(uint32_t));
|
||||
shared_needed = azint_bins * (2 * sizeof(unsigned long long) + sizeof(uint32_t));
|
||||
|
||||
// Geometry-only, so shared per GPU: the first engine on this device uploads them, the rest reuse
|
||||
// them. Keyed by the mapping's own vectors, which outlive every engine built from it.
|
||||
@@ -160,8 +173,8 @@ AzIntEngineGPU::AzIntEngineGPU(const AzimuthalIntegrationMapping &integration, s
|
||||
void AzIntEngineGPU::Run(const ImagePreprocessorBuffer &image, AzimuthalIntegrationProfile &profile) {
|
||||
if (image.size() != integration.GetPixelToBin().size())
|
||||
throw std::runtime_error("ImageSpotFinder::AzimIntegration: Mismatch in size");
|
||||
cuda_err(cudaMemsetAsync(gpu_sum, 0, sizeof(float) * azint_bins, *stream));
|
||||
cuda_err(cudaMemsetAsync(gpu_sum2, 0, sizeof(float) * azint_bins, *stream));
|
||||
cuda_err(cudaMemsetAsync(gpu_sum, 0, sizeof(unsigned long long) * azint_bins, *stream));
|
||||
cuda_err(cudaMemsetAsync(gpu_sum2, 0, sizeof(unsigned long long) * azint_bins, *stream));
|
||||
cuda_err(cudaMemsetAsync(gpu_count, 0, sizeof(uint32_t) * azint_bins, *stream));
|
||||
|
||||
if (shared_needed < shared_size) {
|
||||
@@ -178,11 +191,15 @@ void AzIntEngineGPU::Run(const ImagePreprocessorBuffer &image, AzimuthalIntegrat
|
||||
cuda_err(cudaGetLastError());
|
||||
}
|
||||
|
||||
cudaMemcpyAsync(azint_sum.data(), gpu_sum, sizeof(float) * azint_bins, cudaMemcpyDeviceToHost, *stream);
|
||||
cudaMemcpyAsync(azint_sum2.data(), gpu_sum2, sizeof(float) * azint_bins, cudaMemcpyDeviceToHost, *stream);
|
||||
cudaMemcpyAsync(fixed_sum.data(), gpu_sum, sizeof(unsigned long long) * azint_bins, cudaMemcpyDeviceToHost, *stream);
|
||||
cudaMemcpyAsync(fixed_sum2.data(), gpu_sum2, sizeof(unsigned long long) * azint_bins, cudaMemcpyDeviceToHost, *stream);
|
||||
cudaMemcpyAsync(azint_count.data(), gpu_count, sizeof(uint32_t) * azint_bins, cudaMemcpyDeviceToHost, *stream);
|
||||
cuda_err(cudaStreamSynchronize(*stream));
|
||||
|
||||
for (int b = 0; b < azint_bins; b++) {
|
||||
azint_sum[b] = static_cast<float>(static_cast<double>(static_cast<long long>(fixed_sum[b])) / AZINT_FIXED);
|
||||
azint_sum2[b] = static_cast<float>(static_cast<double>(static_cast<long long>(fixed_sum2[b])) / AZINT_FIXED);
|
||||
}
|
||||
profile.Clear(integration);
|
||||
profile.Add(azint_sum, azint_sum2, azint_count);
|
||||
}
|
||||
@@ -19,11 +19,14 @@ class AzIntEngineGPU : public AzIntEngine {
|
||||
std::shared_ptr<CudaDevicePtr<float>> gpu_azint_correction;
|
||||
std::shared_ptr<CudaDevicePtr<uint16_t>> gpu_pixel_to_bin;
|
||||
|
||||
CudaDevicePtr<float> gpu_sum;
|
||||
CudaDevicePtr<float> gpu_sum2;
|
||||
// Fixed point (AZINT_FIXED in the .cu), so the sums do not depend on the order the atomics arrive in.
|
||||
CudaDevicePtr<unsigned long long> gpu_sum;
|
||||
CudaDevicePtr<unsigned long long> gpu_sum2;
|
||||
CudaDevicePtr<uint32_t> gpu_count;
|
||||
CudaRegisteredVector<float> cpu_sum_reg;
|
||||
CudaRegisteredVector<float> cpu_sum2_reg;
|
||||
std::vector<unsigned long long> fixed_sum;
|
||||
std::vector<unsigned long long> fixed_sum2;
|
||||
CudaRegisteredVector<unsigned long long> cpu_sum_reg;
|
||||
CudaRegisteredVector<unsigned long long> cpu_sum2_reg;
|
||||
CudaRegisteredVector<uint32_t> cpu_count_reg;
|
||||
public:
|
||||
AzIntEngineGPU(const AzimuthalIntegrationMapping& integration, std::shared_ptr<CudaStream> stream);
|
||||
|
||||
@@ -12,12 +12,28 @@ inline void cuda_err(cudaError_t val) {
|
||||
throw JFJochException(JFJochExceptionCategory::GPUCUDAError, cudaGetErrorString(val));
|
||||
}
|
||||
|
||||
// Fixed-point scale of the corrected (profile) sums. A corrected value is a pixel times a float
|
||||
// correction, so unlike the raw sums it has no exact integer form, and a float atomicAdd adds the
|
||||
// values in whatever order the threads and blocks happen to arrive - the profile, and the background
|
||||
// estimate read from it, then moved in their last bits between two runs of the same command. Each
|
||||
// value is quantised on its own, and the sum of the quanta is integer addition, which is associative:
|
||||
// the result no longer depends on the order, nor on how the grid was sized for this GPU. 2^12 puts
|
||||
// the quantum at 2.4e-4 of a count, and leaves room for a ring summing to ~2e15 in either sum.
|
||||
constexpr float CORR_FIXED = 4096.0f; // 2^12
|
||||
|
||||
// Signed, carried as two's complement like the raw sums.
|
||||
__device__ __forceinline__ unsigned long long corr_fixed(float v) {
|
||||
return static_cast<unsigned long long>(static_cast<long long>(llrintf(v * CORR_FIXED)));
|
||||
}
|
||||
|
||||
// One thread's merged contribution to a single ring, pushed to the block's shared accumulators.
|
||||
__device__ __forceinline__ void flush_ring(unsigned long long *s_sum, unsigned long long *s_sum2,
|
||||
uint32_t *s_count, float *s_sum_corr, float *s_sum2_corr,
|
||||
uint32_t *s_count, unsigned long long *s_sum_corr,
|
||||
unsigned long long *s_sum2_corr,
|
||||
bool accumulate_corrected, int b,
|
||||
unsigned long long r_sum, unsigned long long r_sum2,
|
||||
uint32_t r_count, float r_sum_corr, float r_sum2_corr) {
|
||||
uint32_t r_count, unsigned long long r_sum_corr,
|
||||
unsigned long long r_sum2_corr) {
|
||||
if (r_count == 0) return; // also covers the initial "no ring yet"
|
||||
atomicAdd(&s_sum[b], r_sum);
|
||||
atomicAdd(&s_sum2[b], r_sum2);
|
||||
@@ -66,9 +82,9 @@ __device__ __forceinline__ void count_value(uint32_t *hist, int2 *overflow, uint
|
||||
}
|
||||
|
||||
// One ring reduction, staging per-ring sums in shared memory (fast path). Shared layout:
|
||||
// [ sum(float) | sum2(float) | count(uint32) | sum_corr(float) | sum2_corr(float) ] x nbins
|
||||
// [ sum | sum2 | sum_corr | sum2_corr | count(uint32) ] x nbins
|
||||
// The corrected arrays exist only when accumulate_corrected is true (the plain first pass); on the
|
||||
// sigma-clip passes only the first three are launched/used.
|
||||
// sigma-clip passes they are left out and the count follows sum2.
|
||||
__global__ void reduce_rings_shared(
|
||||
const uint16_t *__restrict__ pixel_to_bin,
|
||||
const float *__restrict__ corrections,
|
||||
@@ -78,7 +94,7 @@ __global__ void reduce_rings_shared(
|
||||
float clip_k,
|
||||
bool accumulate_corrected,
|
||||
unsigned long long *__restrict__ sum, unsigned long long *__restrict__ sum2, uint32_t *__restrict__ count,
|
||||
float *__restrict__ sum_corr, float *__restrict__ sum2_corr,
|
||||
unsigned long long *__restrict__ sum_corr, unsigned long long *__restrict__ sum2_corr,
|
||||
uint32_t *__restrict__ hist, int2 *__restrict__ overflow, uint32_t *__restrict__ overflow_n,
|
||||
uint32_t overflow_cap,
|
||||
size_t npix, int nbins) {
|
||||
@@ -95,22 +111,22 @@ __global__ void reduce_rings_shared(
|
||||
// accumulators the threshold moved in its last bits between runs, and because detection compares
|
||||
// an integer pixel value against it, a threshold crossing an integer flipped every pixel of that
|
||||
// value in the ring at once.
|
||||
// The CORRECTED sums stay float: they are a pixel value times a float correction, so there is no
|
||||
// exact integer form. They feed the reported azimuthal profile, not the detection decision.
|
||||
// The CORRECTED sums have no exact integer form, so they are summed in fixed point (CORR_FIXED).
|
||||
extern __shared__ unsigned long long sh[];
|
||||
unsigned long long *s_sum = sh; // signed value carried as two's complement
|
||||
unsigned long long *s_sum2 = &s_sum[nbins];
|
||||
uint32_t *s_count = reinterpret_cast<uint32_t *>(&s_sum2[nbins]);
|
||||
float *s_sum_corr = reinterpret_cast<float *>(&s_count[nbins]);
|
||||
float *s_sum2_corr = &s_sum_corr[nbins];
|
||||
unsigned long long *s_sum_corr = &s_sum2[nbins];
|
||||
unsigned long long *s_sum2_corr = &s_sum_corr[nbins];
|
||||
uint32_t *s_count = reinterpret_cast<uint32_t *>(accumulate_corrected ? &s_sum2_corr[nbins]
|
||||
: &s_sum2[nbins]);
|
||||
|
||||
for (int i = threadIdx.x; i < nbins; i += blockDim.x) {
|
||||
s_sum[i] = 0;
|
||||
s_sum2[i] = 0;
|
||||
s_count[i] = 0;
|
||||
if (accumulate_corrected) {
|
||||
s_sum_corr[i] = 0.0f;
|
||||
s_sum2_corr[i] = 0.0f;
|
||||
s_sum_corr[i] = 0;
|
||||
s_sum2_corr[i] = 0;
|
||||
}
|
||||
}
|
||||
__syncthreads();
|
||||
@@ -142,7 +158,7 @@ __global__ void reduce_rings_shared(
|
||||
int r_b = -1;
|
||||
unsigned long long r_sum = 0, r_sum2 = 0;
|
||||
uint32_t r_count = 0;
|
||||
float r_sum_corr = 0.0f, r_sum2_corr = 0.0f;
|
||||
unsigned long long r_sum_corr = 0, r_sum2_corr = 0;
|
||||
|
||||
#pragma unroll
|
||||
for (int k = 0; k < 4; k++) {
|
||||
@@ -160,7 +176,7 @@ __global__ void reduce_rings_shared(
|
||||
r_b, r_sum, r_sum2, r_count, r_sum_corr, r_sum2_corr);
|
||||
r_b = b;
|
||||
r_sum = 0; r_sum2 = 0; r_count = 0;
|
||||
r_sum_corr = 0.0f; r_sum2_corr = 0.0f;
|
||||
r_sum_corr = 0; r_sum2_corr = 0;
|
||||
}
|
||||
// Unsigned, so a run that overflows wraps exactly as the shared accumulator would have:
|
||||
// addition mod 2^64 is associative, which is what keeps the regrouping bit-identical.
|
||||
@@ -169,8 +185,8 @@ __global__ void reduce_rings_shared(
|
||||
r_count += 1u;
|
||||
if (accumulate_corrected) {
|
||||
const float cv = fv * cq[k];
|
||||
r_sum_corr += cv;
|
||||
r_sum2_corr += cv * cv;
|
||||
r_sum_corr += corr_fixed(cv);
|
||||
r_sum2_corr += corr_fixed(cv * cv);
|
||||
}
|
||||
}
|
||||
flush_ring(s_sum, s_sum2, s_count, s_sum_corr, s_sum2_corr, accumulate_corrected,
|
||||
@@ -192,8 +208,8 @@ __global__ void reduce_rings_shared(
|
||||
atomicAdd(&s_count[b], 1u);
|
||||
if (accumulate_corrected) {
|
||||
const float cv = fv * corrections[idx];
|
||||
atomicAdd(&s_sum_corr[b], cv);
|
||||
atomicAdd(&s_sum2_corr[b], cv * cv);
|
||||
atomicAdd(&s_sum_corr[b], corr_fixed(cv));
|
||||
atomicAdd(&s_sum2_corr[b], corr_fixed(cv * cv));
|
||||
}
|
||||
}
|
||||
__syncthreads();
|
||||
@@ -220,7 +236,7 @@ __global__ void reduce_rings_global(
|
||||
float clip_k,
|
||||
bool accumulate_corrected,
|
||||
unsigned long long *__restrict__ sum, unsigned long long *__restrict__ sum2, uint32_t *__restrict__ count,
|
||||
float *__restrict__ sum_corr, float *__restrict__ sum2_corr,
|
||||
unsigned long long *__restrict__ sum_corr, unsigned long long *__restrict__ sum2_corr,
|
||||
size_t npix, int nbins) {
|
||||
|
||||
for (size_t idx = blockIdx.x * blockDim.x + threadIdx.x; idx < npix; idx += blockDim.x * gridDim.x) {
|
||||
@@ -235,8 +251,8 @@ __global__ void reduce_rings_global(
|
||||
atomicAdd(&count[b], 1u);
|
||||
if (accumulate_corrected) {
|
||||
const float cv = fv * corrections[idx];
|
||||
atomicAdd(&sum_corr[b], cv);
|
||||
atomicAdd(&sum2_corr[b], cv * cv);
|
||||
atomicAdd(&sum_corr[b], corr_fixed(cv));
|
||||
atomicAdd(&sum2_corr[b], corr_fixed(cv * cv));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -411,7 +427,7 @@ AdaptiveSpotFinderGPU::AdaptiveSpotFinderGPU(const AzimuthalIntegrationMapping &
|
||||
// measured no better (181 vs 175 us/launch).
|
||||
flag_blocks = 4 * prop.multiProcessorCount;
|
||||
|
||||
shared_plain = static_cast<size_t>(nbins) * (2 * sizeof(unsigned long long) + 2 * sizeof(float) + sizeof(uint32_t));
|
||||
shared_plain = static_cast<size_t>(nbins) * (4 * sizeof(unsigned long long) + sizeof(uint32_t));
|
||||
shared_clip = static_cast<size_t>(nbins) * (2 * sizeof(unsigned long long) + sizeof(uint32_t));
|
||||
use_shared = (shared_plain < prop.sharedMemPerBlock);
|
||||
|
||||
@@ -534,8 +550,8 @@ void AdaptiveSpotFinderGPU::Detect(const ImagePreprocessorBuffer &image,
|
||||
cuda_err(cudaMemsetAsync(gpu_count, 0, sizeof(uint32_t) * nbins, *stream));
|
||||
cuda_err(cudaMemsetAsync(gpu_mean, 0, sizeof(float) * nbins, *stream));
|
||||
cuda_err(cudaMemsetAsync(gpu_sigma, 0, sizeof(float) * nbins, *stream));
|
||||
cuda_err(cudaMemsetAsync(gpu_sum_corr, 0, sizeof(float) * nbins, *stream));
|
||||
cuda_err(cudaMemsetAsync(gpu_sum2_corr, 0, sizeof(float) * nbins, *stream));
|
||||
cuda_err(cudaMemsetAsync(gpu_sum_corr, 0, sizeof(unsigned long long) * nbins, *stream));
|
||||
cuda_err(cudaMemsetAsync(gpu_sum2_corr, 0, sizeof(unsigned long long) * nbins, *stream));
|
||||
if (use_shared) {
|
||||
cuda_err(cudaMemsetAsync(gpu_hist, 0, sizeof(uint32_t) * nbins * HIST_VALUES, *stream));
|
||||
cuda_err(cudaMemsetAsync(gpu_overflow_n, 0, sizeof(uint32_t), *stream));
|
||||
@@ -546,8 +562,8 @@ void AdaptiveSpotFinderGPU::Detect(const ImagePreprocessorBuffer &image,
|
||||
|
||||
// Snapshot the plain corrected profile (and its pixel count) before the raw accumulators are
|
||||
// re-zeroed for the sigma-clip passes.
|
||||
cuda_err(cudaMemcpyAsync(prof_sum.data(), gpu_sum_corr, sizeof(float) * nbins, cudaMemcpyDeviceToHost, *stream));
|
||||
cuda_err(cudaMemcpyAsync(prof_sum2.data(), gpu_sum2_corr, sizeof(float) * nbins, cudaMemcpyDeviceToHost, *stream));
|
||||
cuda_err(cudaMemcpyAsync(prof_sum.data(), gpu_sum_corr, sizeof(unsigned long long) * nbins, cudaMemcpyDeviceToHost, *stream));
|
||||
cuda_err(cudaMemcpyAsync(prof_sum2.data(), gpu_sum2_corr, sizeof(unsigned long long) * nbins, cudaMemcpyDeviceToHost, *stream));
|
||||
cuda_err(cudaMemcpyAsync(prof_count.data(), gpu_count, sizeof(uint32_t) * nbins, cudaMemcpyDeviceToHost, *stream));
|
||||
|
||||
for (int pass = 0; pass < 2; ++pass) {
|
||||
@@ -569,8 +585,13 @@ void AdaptiveSpotFinderGPU::Detect(const ImagePreprocessorBuffer &image,
|
||||
ComputeThresholds(settings);
|
||||
|
||||
// The profile is a byproduct even when the frame has no valid pixels for detection.
|
||||
std::vector<float> sum_corr(nbins), sum2_corr(nbins);
|
||||
for (int b = 0; b < nbins; ++b) {
|
||||
sum_corr[b] = static_cast<float>(static_cast<double>(static_cast<long long>(prof_sum[b])) / CORR_FIXED);
|
||||
sum2_corr[b] = static_cast<float>(static_cast<double>(static_cast<long long>(prof_sum2[b])) / CORR_FIXED);
|
||||
}
|
||||
last_profile.Clear(mapping);
|
||||
last_profile.Add(prof_sum, prof_sum2, prof_count);
|
||||
last_profile.Add(sum_corr, sum2_corr, prof_count);
|
||||
|
||||
if (host_thr.empty()) {
|
||||
// Nothing valid to threshold against: leave no strong pixels for the extractor to build on.
|
||||
|
||||
@@ -76,8 +76,9 @@ class AdaptiveSpotFinderGPU : public ImageSpotFinderGPU {
|
||||
uint32_t overflow_cap = 0;
|
||||
|
||||
// Corrected per-ring accumulators (plain first pass only) -> azimuthal-integration profile.
|
||||
CudaDevicePtr<float> gpu_sum_corr;
|
||||
CudaDevicePtr<float> gpu_sum2_corr;
|
||||
// Fixed point (CORR_FIXED in the .cu), so the sums do not depend on the order the atomics arrive in.
|
||||
CudaDevicePtr<unsigned long long> gpu_sum_corr;
|
||||
CudaDevicePtr<unsigned long long> gpu_sum2_corr;
|
||||
|
||||
// Per-ring detection threshold (host-computed, uploaded) and the ring-threshold mask that is
|
||||
// intersected into the inherited bit buffer.
|
||||
@@ -90,8 +91,8 @@ class AdaptiveSpotFinderGPU : public ImageSpotFinderGPU {
|
||||
std::vector<uint32_t> host_count; // clipped raw count }
|
||||
std::vector<float> host_thr; // per-ring threshold (empty -> frame had no valid pixels)
|
||||
std::vector<float> host_bkg; // clipped per-ring mean, NaN where the ring is too sparse to trust
|
||||
std::vector<float> prof_sum; // plain corrected sum } azimuthal-integration profile
|
||||
std::vector<float> prof_sum2; // plain corrected sum^2 }
|
||||
std::vector<unsigned long long> prof_sum; // plain corrected sum } azimuthal-integration profile
|
||||
std::vector<unsigned long long> prof_sum2; // plain corrected sum^2 } (fixed point)
|
||||
std::vector<uint32_t> prof_count; // plain pixel count }
|
||||
|
||||
// Every per-ring array above is a device-to-host copy once per frame. A D2H copy into PAGEABLE
|
||||
@@ -102,8 +103,8 @@ class AdaptiveSpotFinderGPU : public ImageSpotFinderGPU {
|
||||
CudaRegisteredVector<unsigned long long> host_sum_reg;
|
||||
CudaRegisteredVector<unsigned long long> host_sum2_reg;
|
||||
CudaRegisteredVector<uint32_t> host_count_reg;
|
||||
CudaRegisteredVector<float> prof_sum_reg;
|
||||
CudaRegisteredVector<float> prof_sum2_reg;
|
||||
CudaRegisteredVector<unsigned long long> prof_sum_reg;
|
||||
CudaRegisteredVector<unsigned long long> prof_sum2_reg;
|
||||
CudaRegisteredVector<uint32_t> prof_count_reg;
|
||||
|
||||
AzimuthalIntegrationProfile last_profile; // filled every Run(), retrievable via GetProfile()
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
|
||||
#include "../common/AzimuthalIntegrationMapping.h"
|
||||
#include "../common/AzimuthalIntegrationProfile.h"
|
||||
@@ -188,7 +189,17 @@ TEST_CASE("AzIntEngineGPU_MatchesCPU", "[AdaptiveSpotFinderGPU]") {
|
||||
|
||||
AzimuthalIntegrationProfile cpu_profile(mapping), gpu_profile(mapping);
|
||||
AzIntEngineCPU(mapping).Run(buffer, cpu_profile);
|
||||
AzIntEngineGPU(mapping, std::make_shared<CudaStream>()).Run(buffer, gpu_profile);
|
||||
AzIntEngineGPU gpu_engine(mapping, std::make_shared<CudaStream>());
|
||||
gpu_engine.Run(buffer, gpu_profile);
|
||||
|
||||
// The GPU sums are fixed point, so a re-run on the same image repeats them exactly.
|
||||
for (int repeat = 0; repeat < 20; repeat++) {
|
||||
AzimuthalIntegrationProfile again(mapping);
|
||||
gpu_engine.Run(buffer, again);
|
||||
const auto a = again.GetResult(), g = gpu_profile.GetResult();
|
||||
REQUIRE(a.size() == g.size());
|
||||
REQUIRE(std::memcmp(a.data(), g.data(), a.size() * sizeof(float)) == 0);
|
||||
}
|
||||
|
||||
REQUIRE(gpu_profile.GetPixelCount() == cpu_profile.GetPixelCount());
|
||||
const auto ref = cpu_profile.GetResult();
|
||||
@@ -235,12 +246,20 @@ TEST_CASE("AdaptiveSpotFinderGPU_RunToRunReproducible", "[AdaptiveSpotFinderGPU]
|
||||
// close. Repeat enough times to give a scheduling-dependent threshold a chance to show itself:
|
||||
// the effect it used to have was ~1 changed observation in a million, so a handful of repeats on
|
||||
// a quiet background would not have caught it.
|
||||
// The azimuthal profile computed in the same pass has to repeat bit for bit as well: its
|
||||
// corrected sums are fixed point for that reason. Compared as bytes, since empty rings are NaN.
|
||||
const auto first = gpu.Run(buffer, settings);
|
||||
REQUIRE(first.size() > 0);
|
||||
const auto first_profile = gpu.GetProfile().GetResult();
|
||||
const auto first_std = gpu.GetProfile().GetStd();
|
||||
for (int repeat = 0; repeat < 50; repeat++) {
|
||||
const auto again = gpu.Run(buffer, settings);
|
||||
REQUIRE(again.size() == first.size());
|
||||
REQUIRE(SortedCoords(again) == SortedCoords(first));
|
||||
const auto profile = gpu.GetProfile().GetResult();
|
||||
const auto std_dev = gpu.GetProfile().GetStd();
|
||||
REQUIRE(std::memcmp(profile.data(), first_profile.data(), profile.size() * sizeof(float)) == 0);
|
||||
REQUIRE(std::memcmp(std_dev.data(), first_std.data(), std_dev.size() * sizeof(float)) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user