|
| 1 | +/*********************************************************************************************************************** |
| 2 | +* * |
| 3 | +* libscopehal * |
| 4 | +* * |
| 5 | +* Copyright (c) 2012-2025 Andrew D. Zonenberg and contributors * |
| 6 | +* All rights reserved. * |
| 7 | +* * |
| 8 | +* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the * |
| 9 | +* following conditions are met: * |
| 10 | +* * |
| 11 | +* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the * |
| 12 | +* following disclaimer. * |
| 13 | +* * |
| 14 | +* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the * |
| 15 | +* following disclaimer in the documentation and/or other materials provided with the distribution. * |
| 16 | +* * |
| 17 | +* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products * |
| 18 | +* derived from this software without specific prior written permission. * |
| 19 | +* * |
| 20 | +* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * |
| 21 | +* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * |
| 22 | +* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * |
| 23 | +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR * |
| 24 | +* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * |
| 25 | +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * |
| 26 | +* POSSIBILITY OF SUCH DAMAGE. * |
| 27 | +* * |
| 28 | +***********************************************************************************************************************/ |
| 29 | + |
| 30 | +/** |
| 31 | + @file |
| 32 | + @author Andrew D. Zonenberg |
| 33 | + @brief Unit test for FindZeroCrossings() and similar |
| 34 | + */ |
| 35 | +#ifdef _CATCH2_V3 |
| 36 | +#include <catch2/catch_all.hpp> |
| 37 | +#else |
| 38 | +#include <catch2/catch.hpp> |
| 39 | +#endif |
| 40 | + |
| 41 | +#include "../../lib/scopehal/scopehal.h" |
| 42 | +#include "../../lib/scopehal/LevelCrossingDetector.h" |
| 43 | +#include "../../lib/scopehal/TestWaveformSource.h" |
| 44 | +#include "../../lib/scopeprotocols/scopeprotocols.h" |
| 45 | +#include "Primitives.h" |
| 46 | + |
| 47 | +using namespace std; |
| 48 | + |
| 49 | +TEST_CASE("Primitive_FindZeroCrossings") |
| 50 | +{ |
| 51 | + //Create a queue and command buffer |
| 52 | + shared_ptr<QueueHandle> queue(g_vkQueueManager->GetComputeQueue("Primitive_FindZeroCrossings.queue")); |
| 53 | + vk::CommandPoolCreateInfo poolInfo( |
| 54 | + vk::CommandPoolCreateFlagBits::eTransient | vk::CommandPoolCreateFlagBits::eResetCommandBuffer, |
| 55 | + queue->m_family ); |
| 56 | + vk::raii::CommandPool pool(*g_vkComputeDevice, poolInfo); |
| 57 | + |
| 58 | + vk::CommandBufferAllocateInfo bufinfo(*pool, vk::CommandBufferLevel::ePrimary, 1); |
| 59 | + vk::raii::CommandBuffer cmdBuf(std::move(vk::raii::CommandBuffers(*g_vkComputeDevice, bufinfo).front())); |
| 60 | + |
| 61 | + const size_t depth = 50000000; |
| 62 | + |
| 63 | + //Deterministic PRNG for repeatable testing |
| 64 | + minstd_rand rng; |
| 65 | + rng.seed(0); |
| 66 | + TestWaveformSource source(rng); |
| 67 | + |
| 68 | + SECTION("UniformAnalogWaveform") |
| 69 | + { |
| 70 | + //Input waveforms |
| 71 | + auto wfm = dynamic_cast<UniformAnalogWaveform*>( |
| 72 | + source.GenerateNoisySinewave(1.0, 0.0, 200000, 20000, depth, 0.1)); |
| 73 | + wfm->MarkModifiedFromCpu(); |
| 74 | + |
| 75 | + //Find the reference zero crossings using the base function |
| 76 | + float threshold = 0.05; |
| 77 | + double start = GetTime(); |
| 78 | + vector<int64_t> edges; |
| 79 | + Filter::FindZeroCrossings(wfm, threshold, edges); |
| 80 | + double dt = GetTime() - start; |
| 81 | + LogNotice("CPU: %.3f ms, %zu edges, %zu samples\n", dt*1000, edges.size(), depth); |
| 82 | + |
| 83 | + { |
| 84 | + LogNotice("First few ref timestamps:\n"); |
| 85 | + LogIndenter li; |
| 86 | + for(size_t i=0; i<5; i++) |
| 87 | + LogNotice("%" PRIi64 "\n", edges[i]); |
| 88 | + } |
| 89 | + |
| 90 | + //Do the GPU version |
| 91 | + //Run twice, second time for score, so we don't count deferred init or allocations in the benchmark |
| 92 | + LevelCrossingDetector ldet; |
| 93 | + ldet.FindZeroCrossings(wfm, threshold, cmdBuf, queue); |
| 94 | + start = GetTime(); |
| 95 | + ldet.FindZeroCrossings(wfm, threshold, cmdBuf, queue); |
| 96 | + dt = GetTime() - start; |
| 97 | + LogNotice("GPU: %.3f ms, TBD\n", dt*1000); |
| 98 | + |
| 99 | + /* |
| 100 | + //Initial sanity check: we should have the same number of data bits as we generated, |
| 101 | + //and all sizes should be consistent |
| 102 | + REQUIRE(nsamples == samples.m_offsets.size()); |
| 103 | + REQUIRE(nsamples == samples.m_durations.size()); |
| 104 | + REQUIRE(nsamples == samples.m_samples.size()); |
| 105 | +
|
| 106 | + //Check each of the bits |
| 107 | + for(size_t i=0; i<nsamples; i++) |
| 108 | + { |
| 109 | + REQUIRE(samples.m_offsets[i] == samples_expected.m_offsets[i]); |
| 110 | + REQUIRE(samples.m_durations[i] == samples_expected.m_durations[i]); |
| 111 | + REQUIRE(samples.m_samples[i] == samples_expected.m_samples[i]); |
| 112 | + } |
| 113 | + */ |
| 114 | + |
| 115 | + //done, clean up |
| 116 | + delete wfm; |
| 117 | + } |
| 118 | +} |
0 commit comments