|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +/// \file TPCFastTransformPOD.cxx |
| 13 | +/// \brief Implementation of POD correction map |
| 14 | +/// |
| 15 | +/// \author ruben.shahoayn@cern.ch |
| 16 | + |
| 17 | +#include "TPCFastTransformPOD.h" |
| 18 | + |
| 19 | +namespace GPUCA_NAMESPACE |
| 20 | +{ |
| 21 | +namespace gpu |
| 22 | +{ |
| 23 | +#if !defined(GPUCA_GPUCODE) |
| 24 | + |
| 25 | +size_t TPCFastTransformPOD::estimateSize(const TPCFastTransform& src) |
| 26 | +{ |
| 27 | + // estimate size of own buffer |
| 28 | + const auto& origCorr = src.getCorrection(); |
| 29 | + const size_t selfSizeFix = sizeof(TPCFastTransformPOD); |
| 30 | + size_t nextDynOffs = alignOffset(selfSizeFix); |
| 31 | + nextDynOffs = alignOffset(nextDynOffs + origCorr.mNumberOfScenarios * sizeof(size_t)); // spline scenarios start here |
| 32 | + // space for splines |
| 33 | + for (int isc = 0; isc < origCorr.mNumberOfScenarios; isc++) { |
| 34 | + const auto& spline = origCorr.mScenarioPtr[isc]; |
| 35 | + nextDynOffs = alignOffset(nextDynOffs + sizeof(spline)); |
| 36 | + } |
| 37 | + // space for splines data |
| 38 | + for (int is = 0; is < 3; is++) { |
| 39 | + for (int slice = 0; slice < origCorr.mGeo.getNumberOfSlices(); slice++) { |
| 40 | + for (int row = 0; row < NROWS; row++) { |
| 41 | + const auto& spline = origCorr.getSpline(slice, row); |
| 42 | + int nPar = spline.getNumberOfParameters(); |
| 43 | + if (is == 1) { |
| 44 | + nPar = nPar / 3; |
| 45 | + } |
| 46 | + if (is == 2) { |
| 47 | + nPar = nPar * 2 / 3; |
| 48 | + } |
| 49 | + nextDynOffs += nPar * sizeof(float); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + nextDynOffs = alignOffset(nextDynOffs); |
| 54 | + return nextDynOffs; |
| 55 | +} |
| 56 | + |
| 57 | +const TPCFastTransformPOD& TPCFastTransformPOD::create(char* buff, size_t buffSize, const TPCFastTransform& src) |
| 58 | +{ |
| 59 | + // instantiate objec to already created buffer of the right size |
| 60 | + assert(buffSize > sizeof(TPCFastTransformPOD)); |
| 61 | + const auto& origCorr = src.getCorrection(); |
| 62 | + auto& podMap = getNonConst(buff); |
| 63 | + |
| 64 | + // copy fixed size data --- start |
| 65 | + podMap.mNumberOfScenarios = origCorr.mNumberOfScenarios; |
| 66 | + std::memcpy(&podMap.mGeo, &origCorr.mGeo, sizeof(TPCFastTransformGeo)); // copy geometry (fixed size) |
| 67 | + for (int row = 0; row < NROWS; row++) { |
| 68 | + podMap.mRowInfo[row] = origCorr.getRowInfo(row); // dataOffsetBytes will be modified later |
| 69 | + } |
| 70 | + for (int slice = 0; slice < TPCFastTransformGeo::getNumberOfSlices(); slice++) { |
| 71 | + podMap.mSliceInfo[slice] = origCorr.getSliceInfo(slice); |
| 72 | + for (int row = 0; row < NROWS; row++) { |
| 73 | + podMap.mSliceRowInfo[NROWS * slice + row] = origCorr.getSliceRowInfo(slice, row); |
| 74 | + } |
| 75 | + } |
| 76 | + podMap.mInterpolationSafetyMargin = origCorr.fInterpolationSafetyMargin; |
| 77 | + podMap.mTimeStamp = origCorr.mTimeStamp; |
| 78 | + // copy fixed size data --- end |
| 79 | + |
| 80 | + size_t nextDynOffs = alignOffset(sizeof(TPCFastTransformPOD)); |
| 81 | + // copy slice scenarios |
| 82 | + podMap.mOffsScenariosOffsets = nextDynOffs; // spline scenarios offsets start here |
| 83 | + LOGP(debug, "Set mOffsScenariosOffsets = {}", podMap.mOffsScenariosOffsets); |
| 84 | + nextDynOffs = alignOffset(nextDynOffs + podMap.mNumberOfScenarios * sizeof(size_t)); // spline scenarios start here |
| 85 | + |
| 86 | + // copy spline objects |
| 87 | + size_t* scenOffs = reinterpret_cast<size_t*>(buff + podMap.mOffsScenariosOffsets); |
| 88 | + for (int isc = 0; isc < origCorr.mNumberOfScenarios; isc++) { |
| 89 | + scenOffs[isc] = nextDynOffs; |
| 90 | + const auto& spline = origCorr.mScenarioPtr[isc]; |
| 91 | + if (buffSize < nextDynOffs + sizeof(spline)) { |
| 92 | + throw std::runtime_error(fmt::format("attempt to copy {} bytes for spline for scenario {} to {}, overflowing the buffer of size {}", sizeof(spline), isc, nextDynOffs + sizeof(spline), buffSize)); |
| 93 | + } |
| 94 | + std::memcpy(buff + scenOffs[isc], &spline, sizeof(spline)); |
| 95 | + nextDynOffs = alignOffset(nextDynOffs + sizeof(spline)); |
| 96 | + LOGP(debug, "Copy {} bytes for spline scenario {} (ptr:{}) to offsset {}", sizeof(spline), isc, (void*)&spline, scenOffs[isc]); |
| 97 | + } |
| 98 | + |
| 99 | + // copy splines data |
| 100 | + for (int is = 0; is < 3; is++) { |
| 101 | + float* data = reinterpret_cast<float*>(buff + nextDynOffs); |
| 102 | + LOGP(debug, "splinID={} start offset {} -> {}", is, nextDynOffs, (void*)data); |
| 103 | + for (int slice = 0; slice < origCorr.mGeo.getNumberOfSlices(); slice++) { |
| 104 | + podMap.mSplineDataOffsets[slice][is] = nextDynOffs; |
| 105 | + size_t rowDataOffs = 0; |
| 106 | + for (int row = 0; row < NROWS; row++) { |
| 107 | + const auto& spline = origCorr.getSpline(slice, row); |
| 108 | + const float* dataOr = origCorr.getSplineData(slice, row, is); |
| 109 | + int nPar = spline.getNumberOfParameters(); |
| 110 | + if (is == 1) { |
| 111 | + nPar = nPar / 3; |
| 112 | + } |
| 113 | + if (is == 2) { |
| 114 | + nPar = nPar * 2 / 3; |
| 115 | + } |
| 116 | + LOGP(debug, "Copying {} floats for spline{} of slice:{} row:{} to offset {}", nPar, is, slice, row, nextDynOffs); |
| 117 | + size_t nbcopy = nPar * sizeof(float); |
| 118 | + if (buffSize < nextDynOffs + nbcopy) { |
| 119 | + throw std::runtime_error(fmt::format("attempt to copy {} bytes of data for spline{} of slice{}/row{} to {}, overflowing the buffer of size {}", nbcopy, is, slice, row, nextDynOffs, buffSize)); |
| 120 | + } |
| 121 | + std::memcpy(data, dataOr, nbcopy); |
| 122 | + podMap.getRowInfo(row).dataOffsetBytes[is] = rowDataOffs; |
| 123 | + rowDataOffs += nbcopy; |
| 124 | + data += nPar; |
| 125 | + nextDynOffs += nbcopy; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + podMap.mTotalSize = alignOffset(nextDynOffs); |
| 130 | + if (buffSize != podMap.mTotalSize) { |
| 131 | + throw std::runtime_error(fmt::format("Estimated buffer size {} differs from filled one {}", buffSize, podMap.mTotalSize)); |
| 132 | + } |
| 133 | + return podMap; |
| 134 | +} |
| 135 | +#endif |
| 136 | + |
| 137 | +} // namespace gpu |
| 138 | +} // namespace GPUCA_NAMESPACE |
0 commit comments