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