33#include < sstream>
44#include < string>
55#include < fstream>
6+ #include < cmath>
67
78#include " SonicCMS/Core/interface/SonicEDProducer.h"
89#include " SonicCMS/TensorRT/interface/TRTClientFPGA.h"
@@ -22,61 +23,23 @@ class HcalProducerFPGA : public SonicEDProducer<Client>
2223 // needed because base class has dependent scope
2324 using typename SonicEDProducer<Client>::Input;
2425 using typename SonicEDProducer<Client>::Output;
25- // std::vector<float> fInVals; => debug
26- // std::vector<float> fPrVals;
27- // int fCount;
2826 explicit HcalProducerFPGA (edm::ParameterSet const & cfg) : SonicEDProducer<Client>(cfg), topN_(cfg.getParameter<unsigned >(" topN" )) {
2927 // for debugging
3028 this ->setDebugName (" HcalProducerFPGA" );
31- /* Validation on temporary files
32- std::ifstream lFIn("tb_input_features.dat");
33- std::ifstream lFPr("tb_output_predictions.dat");
34- std::string iline;
35- std::string pline;
36- bool hit_end = false;
37- bool valid_data = true;
38- for (int istream = 0; istream < 16000; istream++) {
39- if (valid_data && !hit_end){
40- if(std::getline(lFIn,iline) && std::getline(lFPr,pline)) {
41- char* cstr=const_cast<char*>(iline.c_str());
42- char* current;
43- std::vector<float> in;
44- current=strtok(cstr," ");
45- while(current!=NULL){
46- fInVals.push_back(atof(current));
47- current=strtok(NULL," ");
48- }
49- cstr=const_cast<char*>(pline.c_str());
50- current=strtok(cstr," ");
51- while(current!=NULL){
52- fPrVals.push_back(atof(current));
53- current=strtok(NULL," ");
54- }
55- }
56- }
57- }
58- fCount = 0;
59- */
60-
6129 }
6230 void acquire (edm::Event const & iEvent, edm::EventSetup const & iSetup, Input& iInput) override {
6331 auto ninput = client_.ninput ();
6432 auto batchSize = client_.batchSize ();
6533 iInput = Input (ninput*batchSize, 0 .f );
6634 // make some random channels
67- for (unsigned ib = 0 ; ib < batchSize; ib++) {
68- // Current Hcal setup takes 11 inputs but it is sent in chunks of 16 to the FPGA
69- iInput[ib*ninput+0 ] = f_to_ui<16 ,6 >(1 );
70- iInput[ib*ninput+1 ] = f_to_ui<16 ,6 >(2 );
71- iInput[ib*ninput+3 ] = f_to_ui<16 ,6 >(int (rand () % 30 )-15 );
72- iInput[ib*ninput+4 ] = f_to_ui<16 ,6 >(int (rand () % 36 )-36 );
73- for (unsigned i0 = 5 ; i0 < 18 ; i0++) iInput[ib*ninput+i0] = f_to_ui<16 ,8 >(1 );
74- for (unsigned i0 = 18 ; i0 < ninput; i0++) iInput[ib*ninput+i0] = f_to_ui<16 ,8 >(0 );
75- /* for debug
76- for(unsigned i0 = 0; i0 < 11; i0++) iInput[ib*ninput+i0] = f_to_ui<32,14>(fInVals[i0+11*fCount]);
77- for(unsigned i0 = 11; i0 < ninput; i0++) iInput[ib*ninput+i0] = 0;
78- fCount++;
79- */
35+ for (unsigned ib = 0 ; ib < batchSize; ib++) {
36+ // Current Hcal setup takes 11 inputs but it is sent in chunks of 16 to the FPGA
37+ iInput[ib*ninput+0 ] = f_to_ui<16 ,6 >(1 );
38+ iInput[ib*ninput+1 ] = f_to_ui<16 ,6 >(2 );
39+ iInput[ib*ninput+3 ] = f_to_ui<16 ,6 >(int (rand () % 30 )-15 );
40+ iInput[ib*ninput+4 ] = f_to_ui<16 ,6 >(int (rand () % 36 )-36 );
41+ for (unsigned i0 = 5 ; i0 < 18 ; i0++) iInput[ib*ninput+i0] = f_to_ui<16 ,8 >(1 );
42+ for (unsigned i0 = 18 ; i0 < ninput; i0++) iInput[ib*ninput+i0] = f_to_ui<16 ,8 >(0 );
8043 }
8144 }
8245 void produce (edm::Event& iEvent, edm::EventSetup const & iSetup, Output const & iOutput) override {
@@ -119,34 +82,33 @@ class HcalProducerFPGA : public SonicEDProducer<Client>
11982
12083 private:
12184 using SonicEDProducer<Client>::client_;
122- template <unsigned int B, unsigned int I>
123- unsigned short f_to_ui (float f) {
124- bool isPos = f > 0 .;
125- short tmpIs = int (fabs (f));
126- unsigned short tmpI = tmpIs;
127- if (not isPos) {
128- // unsigned int comp = ((unsigned int)((1<<(sizeof(unsigned int)*8-I+1))-1)<<I);
129- unsigned short comp = ((unsigned short )((1 <<(sizeof (unsigned short )*4 -I+1 ))-1 )<<I);
130- tmpI = -tmpIs;
131- tmpI = tmpI-comp;
132- }
133- float tmpF = fabs (f) - float (tmpIs);
134- unsigned short fracs = tmpF*float (1 <<(B-I));
135- unsigned short val = (tmpI << (B-I)) + fracs;
136- return val;
137- }
138- template <unsigned int B, unsigned int I>
139- float ui_to_f (const unsigned short ui) {
140- unsigned short i = ui >> (B-I);
141- unsigned short mask = (1 << (B-I))-1 ;
142- unsigned short dec = ui & mask;
143- float lDec = float (dec)/float (1 << (B-I));
144- return float (i)+lDec;
145- }
146- uint32_t merge (unsigned short iA,unsigned short iB) {
147- uint32_t result = (uint32_t ) iA << 16 | iB;
148- return result;
149- }
85+ template <unsigned int B, unsigned int I>
86+ unsigned short f_to_ui (float f) {
87+ bool isPos = f > 0 .;
88+ short tmpIs = int (std::abs (f));
89+ unsigned short tmpI = tmpIs;
90+ if (not isPos) {
91+ unsigned short comp = ((unsigned short )((1 <<(sizeof (unsigned short )*4 -I+1 ))-1 )<<I);
92+ tmpI = -tmpIs;
93+ tmpI = tmpI-comp;
94+ }
95+ float tmpF = std::abs (f) - float (tmpIs);
96+ unsigned short fracs = tmpF*float (1 <<(B-I));
97+ unsigned short val = (tmpI << (B-I)) + fracs;
98+ return val;
99+ }
100+ template <unsigned int B, unsigned int I>
101+ float ui_to_f (const unsigned short ui) {
102+ unsigned short i = ui >> (B-I);
103+ unsigned short mask = (1 << (B-I))-1 ;
104+ unsigned short dec = ui & mask;
105+ float lDec = float (dec)/float (1 << (B-I));
106+ return float (i)+lDec;
107+ }
108+ uint32_t merge (unsigned short iA,unsigned short iB) {
109+ uint32_t result = (uint32_t ) iA << 16 | iB;
110+ return result;
111+ }
150112 unsigned topN_;
151113};
152114
0 commit comments