From bd6b58fbe142493ead2eea48250e7bdde0be6e0b Mon Sep 17 00:00:00 2001 From: lain Date: Thu, 28 Sep 2023 22:55:19 -0700 Subject: [PATCH] Add unit test for Add filter. Bump scopehal submodule commit. --- lib | 2 +- tests/Filters/CMakeLists.txt | 1 + tests/Filters/Filter_Add.cpp | 160 +++++++++++++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 tests/Filters/Filter_Add.cpp diff --git a/lib b/lib index d35429757..bb3cdcd56 160000 --- a/lib +++ b/lib @@ -1 +1 @@ -Subproject commit d35429757173cfd0470db717c17dcdfe7464f994 +Subproject commit bb3cdcd566f5da369152b41195bd74a2a7aae372 diff --git a/tests/Filters/CMakeLists.txt b/tests/Filters/CMakeLists.txt index 2bcec4e38..f81b28876 100644 --- a/tests/Filters/CMakeLists.txt +++ b/tests/Filters/CMakeLists.txt @@ -1,6 +1,7 @@ add_executable(Filters main.cpp + Filter_Add.cpp Filter_DeEmbed.cpp Filter_FIR.cpp Filter_FFT.cpp diff --git a/tests/Filters/Filter_Add.cpp b/tests/Filters/Filter_Add.cpp new file mode 100644 index 000000000..b7d22f958 --- /dev/null +++ b/tests/Filters/Filter_Add.cpp @@ -0,0 +1,160 @@ +/*********************************************************************************************************************** +* * +* glscopeclient * +* * +* Copyright (c) 2012-2023 Andrew D. Zonenberg and contributors * +* All rights reserved. * +* * +* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the * +* following conditions are met: * +* * +* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the * +* following disclaimer. * +* * +* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the * +* following disclaimer in the documentation and/or other materials provided with the distribution. * +* * +* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products * +* derived from this software without specific prior written permission. * +* * +* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * +* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * +* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR * +* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * +* POSSIBILITY OF SUCH DAMAGE. * +* * +***********************************************************************************************************************/ + +/** + @file + @author Andrew D. Zonenberg + @brief Unit test for Add filter + */ +#ifdef _CATCH2_V3 +#include +#else +#include +#endif + +#include "../../lib/scopehal/scopehal.h" +#include "../../lib/scopehal/TestWaveformSource.h" +#include "../../lib/scopeprotocols/scopeprotocols.h" +#include "Filters.h" + +using namespace std; + +void VerifyAdditionResult(UniformAnalogWaveform* pa, UniformAnalogWaveform* pb, UniformAnalogWaveform* padd); +void AddCpu(UniformAnalogWaveform* pout, UniformAnalogWaveform* pa, UniformAnalogWaveform* pb); + +TEST_CASE("Filter_Add") +{ + auto filter = dynamic_cast(Filter::CreateFilter("Add", "#ffffff")); + REQUIRE(filter != NULL); + filter->AddRef(); + + //Create a queue and command buffer + shared_ptr queue(g_vkQueueManager->GetComputeQueue("Filter_Add.queue")); + vk::CommandPoolCreateInfo poolInfo( + vk::CommandPoolCreateFlagBits::eTransient | vk::CommandPoolCreateFlagBits::eResetCommandBuffer, + queue->m_family ); + vk::raii::CommandPool pool(*g_vkComputeDevice, poolInfo); + + vk::CommandBufferAllocateInfo bufinfo(*pool, vk::CommandBufferLevel::ePrimary, 1); + vk::raii::CommandBuffer cmdbuf(move(vk::raii::CommandBuffers(*g_vkComputeDevice, bufinfo).front())); + + //Create two empty input waveforms + const size_t depth = 10000000; + UniformAnalogWaveform ua; + UniformAnalogWaveform ub; + UniformAnalogWaveform ubase; + ubase.Resize(depth); + + //Set up filter configuration + g_scope->GetOscilloscopeChannel(0)->SetData(&ua, 0); + g_scope->GetOscilloscopeChannel(1)->SetData(&ub, 0); + filter->SetInput("a", g_scope->GetOscilloscopeChannel(0)); + filter->SetInput("b", g_scope->GetOscilloscopeChannel(1)); + + const size_t niter = 5; + for(size_t i=0; iRefresh(cmdbuf, queue); + + //Baseline on the CPU + double start = GetTime(); + AddCpu(&ubase, &ua, &ub); + double tbase = GetTime() - start; + LogVerbose("CPU: %.2f ms\n", tbase * 1000); + + VerifyAdditionResult(&ua, &ub, &ubase); + + start = GetTime(); + filter->Refresh(cmdbuf, queue); + double dt = GetTime() - start; + LogVerbose("GPU: %.2f ms, %.2fx speedup\n", dt * 1000, tbase / dt); + + VerifyAdditionResult(&ua, &ub, dynamic_cast(filter->GetData(0))); + } + } + + g_scope->GetOscilloscopeChannel(0)->Detach(0); + g_scope->GetOscilloscopeChannel(1)->Detach(0); + + filter->Release(); +} + +void AddCpu(UniformAnalogWaveform* pout, UniformAnalogWaveform* pa, UniformAnalogWaveform* pb) +{ + REQUIRE(pout != nullptr); + REQUIRE(pa != nullptr); + REQUIRE(pb != nullptr); + REQUIRE(pout->size() == pa->size()); + REQUIRE(pa->size() == pb->size()); + + pout->PrepareForCpuAccess(); + pa->PrepareForCpuAccess(); + pb->PrepareForCpuAccess(); + + float* out = pout->m_samples.GetCpuPointer(); + + size_t len = pa->size(); + + for(size_t i=0; im_samples[i] + pb->m_samples[i]; + + pout->MarkModifiedFromCpu(); +} + +void VerifyAdditionResult(UniformAnalogWaveform* pa, UniformAnalogWaveform* pb, UniformAnalogWaveform* padd) +{ + REQUIRE(padd != nullptr); + REQUIRE(padd->size() == min(pa->size(), pb->size()) ); + + pa->PrepareForCpuAccess(); + pb->PrepareForCpuAccess(); + padd->PrepareForCpuAccess(); + + size_t len = padd->size(); + + for(size_t i=0; im_samples[i] + pb->m_samples[i]; + REQUIRE(fabs(padd->m_samples[i] - expected) < 1e-6); + } +}