Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:53:32

0001 #include <algorithm>
0002 #include <cassert>
0003 #include <cmath>
0004 #include <cstdint>
0005 #include <cstdint>
0006 #include <iomanip>
0007 #include <iostream>
0008 #include <memory>
0009 #include <numeric>
0010 
0011 #include "HeterogeneousCore/CUDAUtilities/interface/device_unique_ptr.h"
0012 #include "HeterogeneousCore/CUDAUtilities/interface/cudaCheck.h"
0013 #include "DataFormats/GeometrySurface/interface/GloballyPositioned.h"
0014 #include "DataFormats/GeometrySurface/interface/SOARotation.h"
0015 #include "DataFormats/GeometrySurface/interface/TkRotation.h"
0016 #include "HeterogeneousCore/CUDAUtilities/interface/requireDevices.h"
0017 
0018 void toGlobalWrapper(SOAFrame<float> const *frame,
0019                      float const *xl,
0020                      float const *yl,
0021                      float *x,
0022                      float *y,
0023                      float *z,
0024                      float const *le,
0025                      float *ge,
0026                      uint32_t n);
0027 
0028 int main(void) {
0029   cms::cudatest::requireDevices();
0030 
0031   typedef float T;
0032   typedef TkRotation<T> Rotation;
0033   typedef SOARotation<T> SRotation;
0034   typedef GloballyPositioned<T> Frame;
0035   typedef SOAFrame<T> SFrame;
0036   typedef typename Frame::PositionType Position;
0037   typedef typename Frame::GlobalVector GlobalVector;
0038   typedef typename Frame::GlobalPoint GlobalPoint;
0039   typedef typename Frame::LocalVector LocalVector;
0040   typedef typename Frame::LocalPoint LocalPoint;
0041 
0042   constexpr uint32_t size = 10000;
0043   constexpr uint32_t size32 = size * sizeof(float);
0044 
0045   float xl[size], yl[size];
0046   float x[size], y[size], z[size];
0047 
0048   // errors
0049   float le[3 * size];
0050   float ge[6 * size];
0051 
0052   auto d_xl = cms::cuda::make_device_unique<float[]>(size, nullptr);
0053   auto d_yl = cms::cuda::make_device_unique<float[]>(size, nullptr);
0054 
0055   auto d_x = cms::cuda::make_device_unique<float[]>(size, nullptr);
0056   auto d_y = cms::cuda::make_device_unique<float[]>(size, nullptr);
0057   auto d_z = cms::cuda::make_device_unique<float[]>(size, nullptr);
0058 
0059   auto d_le = cms::cuda::make_device_unique<float[]>(3 * size, nullptr);
0060   auto d_ge = cms::cuda::make_device_unique<float[]>(6 * size, nullptr);
0061 
0062   double a = 0.01;
0063   double ca = std::cos(a);
0064   double sa = std::sin(a);
0065 
0066   Rotation r1(ca, sa, 0, -sa, ca, 0, 0, 0, 1);
0067   Frame f1(Position(2, 3, 4), r1);
0068   std::cout << "f1.position() " << f1.position() << std::endl;
0069   std::cout << "f1.rotation() " << '\n' << f1.rotation() << std::endl;
0070 
0071   SFrame sf1(f1.position().x(), f1.position().y(), f1.position().z(), f1.rotation());
0072 
0073   auto d_sf = cms::cuda::make_device_unique<char[]>(sizeof(SFrame), nullptr);
0074   cudaCheck(cudaMemcpy(d_sf.get(), &sf1, sizeof(SFrame), cudaMemcpyHostToDevice));
0075 
0076   for (auto i = 0U; i < size; ++i) {
0077     xl[i] = yl[i] = 0.1f * float(i) - float(size / 2);
0078     le[3 * i] = 0.01f;
0079     le[3 * i + 2] = (i > size / 2) ? 1.f : 0.04f;
0080     le[2 * i + 1] = 0.;
0081   }
0082   std::random_shuffle(xl, xl + size);
0083   std::random_shuffle(yl, yl + size);
0084 
0085   cudaCheck(cudaMemcpy(d_xl.get(), xl, size32, cudaMemcpyHostToDevice));
0086   cudaCheck(cudaMemcpy(d_yl.get(), yl, size32, cudaMemcpyHostToDevice));
0087   cudaCheck(cudaMemcpy(d_le.get(), le, 3 * size32, cudaMemcpyHostToDevice));
0088 
0089   toGlobalWrapper((SFrame const *)(d_sf.get()),
0090                   d_xl.get(),
0091                   d_yl.get(),
0092                   d_x.get(),
0093                   d_y.get(),
0094                   d_z.get(),
0095                   d_le.get(),
0096                   d_ge.get(),
0097                   size);
0098   cudaCheck(cudaMemcpy(x, d_x.get(), size32, cudaMemcpyDeviceToHost));
0099   cudaCheck(cudaMemcpy(y, d_y.get(), size32, cudaMemcpyDeviceToHost));
0100   cudaCheck(cudaMemcpy(z, d_z.get(), size32, cudaMemcpyDeviceToHost));
0101   cudaCheck(cudaMemcpy(ge, d_ge.get(), 6 * size32, cudaMemcpyDeviceToHost));
0102 
0103   float eps = 0.;
0104   for (auto i = 0U; i < size; ++i) {
0105     auto gp = f1.toGlobal(LocalPoint(xl[i], yl[i]));
0106     eps = std::max(eps, std::abs(x[i] - gp.x()));
0107     eps = std::max(eps, std::abs(y[i] - gp.y()));
0108     eps = std::max(eps, std::abs(z[i] - gp.z()));
0109   }
0110 
0111   std::cout << "max eps " << eps << std::endl;
0112 
0113   return 0;
0114 }