From 4c1bc6a6c1e8c82ef0c278b60d14bb0346e11928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Onur=20=C3=9Clgen?= Date: Thu, 19 Oct 2023 13:56:47 +0100 Subject: [PATCH] Enable CUDA for NMI gradient unit test --- niftyreg_build_version.txt | 2 +- reg-test/reg_test_nmi_gradient.cpp | 35 ++++++++++++++++-------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/niftyreg_build_version.txt b/niftyreg_build_version.txt index 4772052f..51b40081 100644 --- a/niftyreg_build_version.txt +++ b/niftyreg_build_version.txt @@ -1 +1 @@ -344 +345 diff --git a/reg-test/reg_test_nmi_gradient.cpp b/reg-test/reg_test_nmi_gradient.cpp index 0f5e19cf..f19ac9bd 100644 --- a/reg-test/reg_test_nmi_gradient.cpp +++ b/reg-test/reg_test_nmi_gradient.cpp @@ -1,6 +1,5 @@ -// OpenCL and CUDA are not supported for this test yet +// OpenCL is not supported for this test yet #undef _USE_OPENCL -#undef _USE_CUDA #include "reg_test_common.h" @@ -82,9 +81,11 @@ class NMIGradientTest { // Create the content unique_ptr content{ contentCreator->Create(reference, floating) }; // Add some displacements to the deformation field to avoid grid effect - float *defPtr = static_cast(content->GetDeformationField()->data); - for (size_t index = 0; index < content->GetDeformationField()->nvox; ++index) + nifti_image *defField = content->Content::GetDeformationField(); + float *defPtr = static_cast(defField->data); + for (size_t index = 0; index < defField->nvox; ++index) defPtr[index] += 0.1f; + content->UpdateDeformationField(); // Compute the warped image given the current transformation unique_ptr compute{ platform->CreateCompute(*content) }; compute->ResampleImage(1, padding); @@ -104,21 +105,23 @@ class NMIGradientTest { // Create an image to store the expected gradient values NiftiImage expectedGradientImage(content->GetDeformationField(), NiftiImage::Copy::Image); // Apply perturbations to each value in the deformation field - float *gradPtr = static_cast(expectedGradientImage->data); - const float delta = 0.00001; - for (unsigned index = 0; index < expectedGradientImage.nVoxels(); ++index) { - float current_value = defPtr[index]; - // compute the NMI when removing delta(s) - defPtr[index] = current_value - delta; + float *gradPtr = static_cast(expectedGradientImage->data); + constexpr float delta = 0.00001f; + for (auto index = 0; index < expectedGradientImage.nVoxels(); ++index) { + const float orgDefValue = defPtr[index]; + // Compute the NMI when removing delta(s) + defPtr[index] = orgDefValue - delta; + content->UpdateDeformationField(); compute->ResampleImage(1, padding); - const double nmi_pre = measure_nmi->GetSimilarityMeasureValue(); - // compute the NMI when adding delta(s) - defPtr[index] = current_value + delta; + const double nmiPre = measure_nmi->GetSimilarityMeasureValue(); + // Compute the NMI when adding delta(s) + defPtr[index] = orgDefValue + delta; + content->UpdateDeformationField(); compute->ResampleImage(1, padding); - const double nmi_post = measure_nmi->GetSimilarityMeasureValue(); + const double nmiPost = measure_nmi->GetSimilarityMeasureValue(); // Compute the difference - gradPtr[index] = -(nmi_post - nmi_pre) / (2. * delta); - defPtr[index] = current_value; + gradPtr[index] = float(-(nmiPost - nmiPre) / (2.0 * delta)); + defPtr[index] = orgDefValue; } testCases.push_back({ testName + " "s + platform->GetName(), std::move(gradientImage), std::move(expectedGradientImage) }); }