Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:15:48

0001 #include <cstddef>
0002 #include <cstdint>
0003 #include <random>
0004 #include <vector>
0005 
0006 #define CATCH_CONFIG_MAIN
0007 #include <catch.hpp>
0008 
0009 #include "HeterogeneousCore/CUDAUtilities/interface/requireDevices.h"
0010 #include "HeterogeneousTest/CUDAOpaque/interface/DeviceAdditionOpaque.h"
0011 
0012 TEST_CASE("HeterogeneousTest/CUDAOpaque test", "[cudaTestOpaqueAdditionOpaque]") {
0013   cms::cudatest::requireDevices();
0014 
0015   // random number generator with a gaussian distribution
0016   std::random_device rd{};
0017   std::default_random_engine rand{rd()};
0018   std::normal_distribution<float> dist{0., 1.};
0019 
0020   // tolerance
0021   constexpr float epsilon = 0.000001;
0022 
0023   // buffer size
0024   constexpr size_t size = 1024 * 1024;
0025 
0026   // allocate input and output host buffers
0027   std::vector<float> in1(size);
0028   std::vector<float> in2(size);
0029   std::vector<float> out(size);
0030 
0031   // fill the input buffers with random data, and the output buffer with zeros
0032   for (size_t i = 0; i < size; ++i) {
0033     in1[i] = dist(rand);
0034     in2[i] = dist(rand);
0035     out[i] = 0.;
0036   }
0037 
0038   SECTION("Test add_vectors_f") {
0039     // launch the 1-dimensional kernel for vector addition
0040     REQUIRE_NOTHROW(cms::cudatest::opaque_add_vectors_f(in1.data(), in2.data(), out.data(), size));
0041 
0042     // check the results
0043     for (size_t i = 0; i < size; ++i) {
0044       float sum = in1[i] + in2[i];
0045       CHECK_THAT(out[i], Catch::Matchers::WithinAbs(sum, epsilon));
0046     }
0047   }
0048 }