From f2f3bace2c3499a5b4558f559c43477f10562da3 Mon Sep 17 00:00:00 2001 From: albertgr Date: Thu, 13 Apr 2023 23:44:34 +0200 Subject: [PATCH] =?UTF-8?q?Commit=203=20concept=20fork=20-----------------?= =?UTF-8?q?----------------------------------------=20-=20Funci=C3=B3n=20q?= =?UTF-8?q?ue=20completa=20n=C3=BAmero=20threads=20y=20n=C3=BAmero=20padre?= =?UTF-8?q?=20en=20vector.=20---------------------------------------------?= =?UTF-8?q?------------?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Lib/EncoderLib/EncCu.cpp | 233 +++++++++++++++++++------------- source/Lib/EncoderLib/EncCu.h | 28 +++- 2 files changed, 169 insertions(+), 92 deletions(-) diff --git a/source/Lib/EncoderLib/EncCu.cpp b/source/Lib/EncoderLib/EncCu.cpp index 50a8f42..b3bcd4f 100644 --- a/source/Lib/EncoderLib/EncCu.cpp +++ b/source/Lib/EncoderLib/EncCu.cpp @@ -78,17 +78,35 @@ //END ALBERTO //ALBERTO CONCEPT FORK -struct vectorModeCostStruct { - int mode; - double cost; -}; -bool debugModeForTesting = true; -const int VECTOR_SIZE = 341; // NODOS DEL TREE -const int VECTOR_BYTES = VECTOR_SIZE * sizeof(vectorModeCostStruct); +//bool debugModeForTesting = true; +//const int THREADS_NUMBER = 4; +//int nodeId = 0; + sem_t* sem; vectorModeCostStruct* vector; -int nodeId = 0; std::string posNode; + +void toStringVector() +{ + for (int i = 0; i < VECTOR_SIZE; i++) { + std::cout << vector[i].mode; + std::cout << vector[i].cost << std::endl; + } +} +void toStringVector(int Level) +{ + int end = 0; + for(int i=Level-1; i>-1; i--) + { + end += pow(4,i); + } + + for (int i = 0; i < end; i++) { + std::cout << vector[i].posPadre << ", "; + std::cout << vector[i].threadNumber << std::endl; + } +} + //END ALBERTO CONCEPT FORK // ==================================================================================================================== @@ -176,7 +194,6 @@ void EncCu::create( EncCfg* encCfg ) m_CurrCtx = 0; } - void EncCu::destroy() { unsigned numWidths = gp_sizeIdxInfo->numWidths(); @@ -292,23 +309,94 @@ void EncCu::init( EncLib* pcEncLib, const SPS& sps ) //ALBERTO CONCEPT FORK // Crear memoria compartida para el vector y el semáforo + createAndInitSemaphore(); + + // Inicializar vector de estructuras con valores iniciales de -1 + //Estamos en el nivel 0 + initStructVector(); + + toStringVector(5); //IMPRIMIR 5 NIVELES + + //Comenzamos en el nodo 0 (único) del nivel 0 de 4 + posNode.append("0"); + +} + +void EncCu::createAndInitSemaphore() +{ int fd = shm_open("/myshm", O_CREAT | O_RDWR, 0666); ftruncate(fd, VECTOR_BYTES + sizeof(sem_t)); vector = (vectorModeCostStruct*) mmap(NULL, VECTOR_BYTES, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); sem = (sem_t*) (vector + VECTOR_SIZE); sem_init(sem,1,1); - - // Inicializar vector de estructuras con valores iniciales de -1 - for (int i = 0; i < VECTOR_SIZE; i++) { - vector[i].mode = -1; - vector[i].cost = -1; - } +} - //Comenzamos en el nodo 0 (único) del nivel 0 de 4 - posNode.append("0"); - //END ALBERTO CONCEPT FORK +void EncCu::initStructVector() +{ + vector[0].posPadre=-1; + vector[0].threadNumber=1; + for (int i = 0; i < VECTOR_SIZE; i++) + { + vector[i].mode=-1; + vector[i].cost=-1; + int posPadreActual = i; + if(posPadreActual==(LEVEL0-1)) //Estamos en el nivel 0, el padre realiza el reparto de threads + { + int salto = 1; + int threadNumber = 1; + int start = posPadreActual+salto; + int end = posPadreActual+salto+4; + for(int j=start; j=(LEVEL0) && posPadreActual<=(LEVEL1)) //Estamos en el nivel 1 + { + updateFatherAndThreadNumbers(0,1,posPadreActual,i); + } + else if(posPadreActual>=(LEVEL1+LEVEL0) && posPadreActual<=(LEVEL2+LEVEL1)) //Estamos en el nivel 2 + { + updateFatherAndThreadNumbers(1,2,posPadreActual,i); + } + else if(posPadreActual>=(LEVEL2+LEVEL1+LEVEL0) && posPadreActual<=(LEVEL3+LEVEL2+LEVEL1)) //Estamos en el nivel 3 + { + int posPadreEnNivel = i-LEVEL2-LEVEL1-LEVEL0; + int salto = (LEVEL3-posPadreEnNivel)+4*posPadreEnNivel; //Saltos sobre los hermanos derechos del padre + Saltos sobre hijos izquierdos no del padre + int threadNumber = vector[i].threadNumber; + int start = posPadreActual+salto; + int end = posPadreActual+salto+4; + for(int j=start; j-1;j--) + { + LevelLow += levelValues[j]; + } + int posPadreEnNivel = i-LevelLow; + int salto = (LevelUp-posPadreEnNivel)+4*posPadreEnNivel; //Saltos sobre los hermanos derechos del padre + Saltos sobre hijos izquierdos no del padre + int threadNumber = vector[i].threadNumber; + int start = posPadreActual+salto; + int end = posPadreActual+salto+4; + for(int j=start; j &prevQP, const EnumArray &currQP) { - if(debugModeForTesting) + /*if(debugModeForTesting) { debugModeForTesting = false; vector[4].mode=1; @@ -415,7 +503,7 @@ void EncCu::compressCtu(CodingStructure &cs, const UnitArea &area, const unsigne else { std::cout << vector[4].mode << "== 1?, " << vector[4].cost << "==6?" << std::endl; - } + }*/ //ALBERTO CONCEPT FORK for (int i = 0; i < 1; i++) { pid_t pid = fork(); @@ -842,6 +930,7 @@ void EncCu::xCompressCU( CodingStructure*& tempCS, CodingStructure*& bestCS, Par memcpy(tempCS->prevPLT.curPLT[i], curLastPLT[i], curLastPLTSize[comID] * sizeof(Pel)); } EncTestMode currTestMode = m_modeCtrl->currTestMode(); + //continue; currTestMode.maxCostAllowed = maxCostAllowed; if (pps.getUseDQP() && partitioner.isSepTree(*tempCS) && isChroma( partitioner.chType )) @@ -1240,12 +1329,6 @@ void EncCu::updateLambda(Slice *slice, const int dQP, #endif // SHARP_LUMA_DELTA_QP || ENABLE_QPA_SUB_CTU //ALBERTO CONCEPT FORK -int LEVEL0 = 1; -int LEVEL1 = 4; -int LEVEL2 = 16; -int LEVEL3 = 64; -int LEVEL4 = 256; -int levelValues [5] = {LEVEL0,LEVEL1,LEVEL2,LEVEL3,LEVEL4}; int EncCu::determinePosNodeInVector(std::string posNode) { int index = -1; @@ -1274,59 +1357,8 @@ int EncCu::determinePosNodeInVector(std::string posNode) index += levelValues[i]; } } - /*for(int i=0; i b.length()) { - return false; - } - for (int i = 0; i < a.length(); i++) { - if (a[i] > b[i]) { - return false; - } - } - return true; -} - -int EncCu::determinePosNodeInVectorRec(std::string posNode, std::string posSearch, int index, int counter) -{ - if(posNode == posSearch) - { - return counter; - } - else - { - if(index < 4 && digits_below_or_equal(posSearch,posNode)) - { - posSearch.append(std::to_string(index)); - index++; - counter += pow(4,posSearch.length()); - return determinePosNodeInVectorRec(posNode,posSearch,index, counter); - } - else - { - index = 0; - counter++; - return determinePosNodeInVectorRec(posNode,posSearch,index, counter); - } - } - -}*/ //END ALBERTO CONCEPT FORK void EncCu::xCheckModeSplit(CodingStructure *&tempCS, CodingStructure *&bestCS, Partitioner &partitioner, const EncTestMode& encTestMode, const ModeType modeTypeParent, bool &skipInterPass, double *splitRdCostBest ) @@ -1449,15 +1481,13 @@ void EncCu::xCheckModeSplit(CodingStructure *&tempCS, CodingStructure *&bestCS, m_pcInterSearch->savePrevUniMvInfo(tempCS->area.Y(), tmpUniMvInfo, isUniMvInfoSaved); } //ALBERTO CONCEPT FORK - int actualNodeId = 0; + int actualNodeId = 0; //CU COUNTER + std::string original = posNode; //END ALBERTO CONCEPT FORK do { - //ALBERTO CONCEPT FORK - posNode.append(std::to_string(actualNodeId)); - //END ALBERTO CONCEPT FORK - const auto &subCUArea = partitioner.currArea(); + const auto &subCUArea = partitioner.currArea(); if( tempCS->picture->Y().contains( subCUArea.lumaPos() ) ) { const unsigned wIdx = gp_sizeIdxInfo->idxFrom( subCUArea.lwidth () ); @@ -1471,15 +1501,25 @@ void EncCu::xCheckModeSplit(CodingStructure *&tempCS, CodingStructure *&bestCS, tempSubCS->bestParent = bestSubCS->bestParent = bestCS; double newMaxCostAllowed = isLuma(partitioner.chType) ? std::min(encTestMode.maxCostAllowed, bestCS->cost - m_pcRdCost->calcRdCost(tempCS->fracBits, tempCS->dist)) : MAX_DOUBLE; newMaxCostAllowed = std::max(0.0, newMaxCostAllowed); - xCompressCU(tempSubCS, bestSubCS, partitioner, newMaxCostAllowed);//LLAMADA RECURSIVA //ALBERTO CONCEPT FORK - int idexPosNode = determinePosNodeInVector("00000"); - if(vector[idexPosNode].mode==-1 && vector[idexPosNode].cost == -1) + posNode.append(std::to_string(actualNodeId)); //INTRODUCIMOS SU ID DENTRO DEL PADRE + std::cout << "CONTROL --> código bucle split" << posNode << " : (" << tempSubCS->area.lheight() <<","<< tempSubCS->area.lwidth() <<") " << std::endl; + if(posNode == "0311") { - //vector[idexPosNode]->mode=3; - //vector[idexPosNode]->cost=bestSubCS->cost; + toStringVector(); + } + //END ALBERTO CONCEPT FORK + xCompressCU(tempSubCS, bestSubCS, partitioner, newMaxCostAllowed);//LLAMADA RECURSIVA + //ALBERTO CONCEPT FORK + if(tempCS->area.lheight()>=8 && tempCS->area.lwidth()>= 8) + { + int idexPosNode = determinePosNodeInVector(posNode); + if(vector[idexPosNode].mode==-1 && vector[idexPosNode].cost == -1) + { + vector[idexPosNode].mode=encTestMode.type; + vector[idexPosNode].cost=tempSubCS->cost; + } } - //END ALBERTO CONCEPT FORK tempSubCS->bestParent = bestSubCS->bestParent = nullptr; @@ -1496,6 +1536,8 @@ void EncCu::xCheckModeSplit(CodingStructure *&tempCS, CodingStructure *&bestCS, { tempCS->motionLut = oldMotionLut; } + posNode = original; + actualNodeId = 0; return; } @@ -1537,14 +1579,24 @@ void EncCu::xCheckModeSplit(CodingStructure *&tempCS, CodingStructure *&bestCS, { tempCS->motionLut = oldMotionLut; } + posNode = original; + actualNodeId = 0; return; } } } + //ALBERTO CONCEPT FORK - actualNodeId++; + actualNodeId++;//NOS PONEMOS EN EL PRIMER NODO DE LA PARTICIÓN (AVANZAMOS NODO) + posNode.pop_back(); //LIMPIAMOS NODO ACTUAL DEL VECTOR DEL CÓDIGO (STRING) //END ALBERTO CONCEPT FORK - } while( partitioner.nextPart( *tempCS ) ); + + } while( partitioner.nextPart( *tempCS ) );//AVANZAMOS A LA SIGIENTE CU GENERADA EN TEMPCS + + //ALBERTO CONCEPT FORK + posNode = original; + actualNodeId = 0; + //END ALBERTO CONCEPT FORK partitioner.exitCurrSplit(); @@ -2151,7 +2203,6 @@ bool EncCu::xCheckRDCostIntra(CodingStructure *&tempCS, CodingStructure *&bestCS return foundZeroRootCbf; } - void EncCu::xCheckPLT(CodingStructure *&tempCS, CodingStructure *&bestCS, Partitioner &partitioner, const EncTestMode& encTestMode) { if (((partitioner.currArea().lumaSize().width * partitioner.currArea().lumaSize().height <= 16) && (isLuma(partitioner.chType)) ) diff --git a/source/Lib/EncoderLib/EncCu.h b/source/Lib/EncoderLib/EncCu.h index b44e7a4..5e37be2 100644 --- a/source/Lib/EncoderLib/EncCu.h +++ b/source/Lib/EncoderLib/EncCu.h @@ -228,6 +228,22 @@ public: }; #endif +struct vectorModeCostStruct { + int mode; + double cost; + int threadNumber; + int posPadre; +}; +//ALBERTO CONCEPT FORK +const int VECTOR_SIZE = 341; // TREE NODES +const int VECTOR_BYTES = VECTOR_SIZE * sizeof(vectorModeCostStruct); // VECTOR SIZE +const int LEVEL0 = 1; +const int LEVEL1 = 4; +const int LEVEL2 = 16; +const int LEVEL3 = 64; +const int LEVEL4 = 256; +const int levelValues [5] = {LEVEL0,LEVEL1,LEVEL2,LEVEL3,LEVEL4}; +//END ALBERTO CONCEPT FORK class EncCu : DecCu { @@ -236,6 +252,7 @@ private: static int iter; static int seconds; //END ALBERTO + bool m_bestModeUpdated; struct CtxPair { @@ -306,6 +323,15 @@ public: /// copy parameters from encoder class void init ( EncLib* pcEncLib, const SPS& sps ); + + //ALBERTO CONCEPT FORK + void createAndInitSemaphore(); + + void initStructVector(); + + void updateFatherAndThreadNumbers(int LevelLowIndex, int LevelUpIndex, int posPadreActual, int i); + //END ALBERTO CONCEPT FORK + void setDecCuReshaperInEncCU(EncReshape* pcReshape, ChromaFormat chromaFormatIdc) { initDecCuReshaper((Reshape*) pcReshape, chromaFormatIdc); @@ -317,7 +343,7 @@ public: void destroy (); - //ALBERT CONCEPT FORK + //ALBERTO CONCEPT FORK void compressCtuForks(CodingStructure &cs, const UnitArea &area, const unsigned ctuRsAddr, const EnumArray &prevQP, const EnumArray &currQP); //END ALBERTO CONCEPT FORK -- 2.24.1