diff --git a/image_analysis/indexing/FFTIndexer.cpp b/image_analysis/indexing/FFTIndexer.cpp index 08fb35f77..dfb1efa15 100644 --- a/image_analysis/indexing/FFTIndexer.cpp +++ b/image_analysis/indexing/FFTIndexer.cpp @@ -425,6 +425,13 @@ std::optional FFTIndexer::DegeneratePlaneNormal(const std::vector // and this costs exactly one extra pass. std::optional FFTIndexer::SearchCap(const std::vector &coord, size_t nspots, const Coord &axis, float half_angle_deg) { + // An axis that is not a direction normalizes to NaN, and a grid of NaN directions is not a worse + // answer but a crash: every row it produces is a NaN length, and a NaN key makes the sort in + // FilterFFTResults walk off its index array. There is nothing to search about such an axis. + const float axis_length = axis.Length(); + if (!std::isfinite(axis_length) || (axis_length <= 0.0f)) + return {}; + const Coord u = axis.Normalize(); Coord seed = std::fabs(u.x) < 0.9f ? Coord(1.0f, 0.0f, 0.0f) : Coord(0.0f, 1.0f, 0.0f); const Coord e1 = (u % seed).Normalize(); @@ -435,41 +442,45 @@ std::optional FFTIndexer::SearchCap(const std::vector &coord, size std::vector saved; saved.swap(direction_vectors); - direction_vectors.reserve(saved.size()); - for (size_t i = 0; i < saved.size(); i++) { - // Same spiral as SetupDirectionVectors, with z restricted to the cap instead of the half-sphere. - const double z = 1.0 - (1.0 - cos_half) * static_cast(i) / static_cast(saved.size() - 1); - const double r = std::sqrt(std::max(0.0, 1.0 - z * z)); - const double theta = golden_angle * static_cast(i); - direction_vectors.emplace_back(u * static_cast(z) - + e1 * static_cast(r * std::cos(theta)) - + e2 * static_cast(r * std::sin(theta))); - } - DirectionsChanged(); // The search borrows a member the object keeps using afterwards, and an indexer outlives a failed // transform - the broker's pool logs the error and hands the same indexer the next image - so the - // grid has to go back even when ExecuteFFT throws. + // grid has to go back however this function leaves. Everything between the swap and the restore is + // inside the try for that reason: the fill can run out of memory, the upload can fail, and the + // .at() below throws on exactly the corrupted result row this search has to survive. Leaving the + // cap grid installed poisons every later image on that indexer. + std::optional found; try { + direction_vectors.reserve(saved.size()); + for (size_t i = 0; i < saved.size(); i++) { + // Same spiral as SetupDirectionVectors, with z restricted to the cap instead of the half-sphere. + const double z = 1.0 - (1.0 - cos_half) * static_cast(i) / static_cast(saved.size() - 1); + const double r = std::sqrt(std::max(0.0, 1.0 - z * z)); + const double theta = golden_angle * static_cast(i); + direction_vectors.emplace_back(u * static_cast(z) + + e1 * static_cast(r * std::cos(theta)) + + e2 * static_cast(r * std::sin(theta))); + } + DirectionsChanged(); + ExecuteFFT(coord, nspots); + + int best = 0; + for (int i = 1; i < nDirections; i++) + if (result_fft[i].magnitude > result_fft[best].magnitude) + best = i; + const FFTResult peak = result_fft[best]; + if (peak.length >= min_length_A) + found = direction_vectors.at(peak.direction) * peak.length; } catch (...) { direction_vectors.swap(saved); DirectionsChanged(); throw; } - int best = 0; - for (int i = 1; i < nDirections; i++) - if (result_fft[i].magnitude > result_fft[best].magnitude) - best = i; - const FFTResult peak = result_fft[best]; - const Coord found = direction_vectors.at(peak.direction) * peak.length; - direction_vectors.swap(saved); DirectionsChanged(); - if (peak.length < min_length_A) - return {}; return found; } diff --git a/image_analysis/indexing/FFTIndexerGPU.cu b/image_analysis/indexing/FFTIndexerGPU.cu index ce0959a90..a769801f0 100644 --- a/image_analysis/indexing/FFTIndexerGPU.cu +++ b/image_analysis/indexing/FFTIndexerGPU.cu @@ -192,9 +192,12 @@ void FFTIndexerGPU::DirectionsChanged() { dir_y[i] = direction_vectors.at(i).y; dir_z[i] = direction_vectors.at(i).z; } - cudaMemcpy(d_dir_x, dir_x.data(), nDirections * sizeof(float), cudaMemcpyHostToDevice); - cudaMemcpy(d_dir_y, dir_y.data(), nDirections * sizeof(float), cudaMemcpyHostToDevice); - cudaMemcpy(d_dir_z, dir_z.data(), nDirections * sizeof(float), cudaMemcpyHostToDevice); + // Checked like every other CUDA call here: a silently failed upload leaves the device holding the + // PREVIOUS grid, so the directions the host reads the results against are not the ones the kernel + // used - wrong rows reported as good ones, and nothing says so. + cuda_err(cudaMemcpy(d_dir_x, dir_x.data(), nDirections * sizeof(float), cudaMemcpyHostToDevice)); + cuda_err(cudaMemcpy(d_dir_y, dir_y.data(), nDirections * sizeof(float), cudaMemcpyHostToDevice)); + cuda_err(cudaMemcpy(d_dir_z, dir_z.data(), nDirections * sizeof(float), cudaMemcpyHostToDevice)); } void FFTIndexerGPU::ExecuteFFT(const std::vector &coord, size_t nspots) { @@ -207,9 +210,9 @@ void FFTIndexerGPU::ExecuteFFT(const std::vector &coord, size_t nspots) { spot_z[i] = coord[i].z; } - cudaMemcpyAsync(d_spot_x, spot_x, nspots * sizeof(float), cudaMemcpyHostToDevice, stream); - cudaMemcpyAsync(d_spot_y, spot_y, nspots * sizeof(float), cudaMemcpyHostToDevice, stream); - cudaMemcpyAsync(d_spot_z, spot_z, nspots * sizeof(float), cudaMemcpyHostToDevice, stream); + cuda_err(cudaMemcpyAsync(d_spot_x, spot_x, nspots * sizeof(float), cudaMemcpyHostToDevice, stream)); + cuda_err(cudaMemcpyAsync(d_spot_y, spot_y, nspots * sizeof(float), cudaMemcpyHostToDevice, stream)); + cuda_err(cudaMemcpyAsync(d_spot_z, spot_z, nspots * sizeof(float), cudaMemcpyHostToDevice, stream)); // Shared-memory bins where they fit (they do at any sane sizing - 12.6 kB at the defaults), the // thread-per-direction kernel where they do not.