#pragma once #include #include #include #include #include "../Utils/RationalNum.h" typedef enum links { RR_link, WR_link, WW_link } LinkType; #define MAX_LOOP_DIM 8 #define MAX_CHAIN_LEN 8 namespace Distribution { class Array; template class Arrays; template class Cycle; template class GraphCSR { private: vType numVerts; vType numEdges; vType lastNumOfV; std::vector neighbors; std::vector edges; std::vector weights; std::vector linkType; std::vector attributes; std::vector localIdx; std::vector globalIdx; //for finding std::vector color; vType *activeV; vType *activeE; std::pair *activeArcs; uint64_t usedMem; std::vector> treesQuality; int activeCounter; vType findFrom; bool hardLinksWasUp; int maxChainLen; int maxLoopDim; uint64_t maxAvailMemory; std::map>>> cacheLinks; int countRequestsToAdd, countMissToAdd; private: GraphCSR(const std::vector &neighbors, const std::vector &edges, const std::vector &weights, const std::vector &localIdx, const std::vector &globalIdx, const std::vector &attributes, const std::vector &linkType, const bool hardLinksWasUp) : neighbors(neighbors), edges(edges), weights(weights), localIdx(localIdx), globalIdx(globalIdx), attributes(attributes), linkType(linkType), hardLinksWasUp(hardLinksWasUp) { numVerts = (int)neighbors.size() - 1; numEdges = (int)edges.size(); } vType GetLocalVNum(const vType &V, bool &ifNew); void AddEdgeToGraph(const vType &V1, const vType &V2, const wType &W, const attrType &attr, const bool &ifNew, const uint8_t linkType); void IncreaseWeight(const int &idx, const int &idxRev, const wType &W); int CheckExist(const vType &V1, const vType &V2, const attrType &attr, const bool &ifNew, const uint8_t &linkType); //old algorithm without sort in the fly //TODO: need to update void FindLoop(std::vector> &cycles, const vType V, const vType VPrev); void FindLoop(std::vector, Cycle>> &cycles, const vType V, const vType VPrev, const std::vector &numbers); void RemoveDuplicates(std::vector> &cycles); bool findLink(const vType v1, std::pair &inGraphAttr1, const vType v2, std::pair &inGraphAttr2); std::pair findLinkWithTempate2(const vType v1, int &templV, Array *&templ, const Arrays &allArrays, std::set wasDone); int findDimNumLink(const vType v, const Array *to, const Arrays &allArrays, std::set &wasDone) const; bool checkFirstCoefOfNode(vType node); bool getOptimalBoundsForNode(vType nodeFrom, vType nodeTo, int &needBound, std::pair &bounds); bool hasLinkWithTempate(const vType root, const Arrays &allArrays, bool, int newValue = 0); public: GraphCSR() { ClearGraphCSR(); } GraphCSR(const GraphCSR &G) : GraphCSR(G.neighbors, G.edges, G.weights, G.localIdx, G.globalIdx, G.attributes, G.linkType, G.hardLinksWasUp) { this->lastNumOfV = G.lastNumOfV; this->maxLoopDim = G.maxLoopDim; this->maxChainLen = G.maxChainLen; this->maxAvailMemory = G.maxAvailMemory; this->countRequestsToAdd = G.countRequestsToAdd; this->countMissToAdd = G.countMissToAdd; this->treesQuality = G.treesQuality; } void cleanCacheLinks() { cacheLinks.clear(); } void ClearGraphCSR() { lastNumOfV = numVerts = numEdges = 0; hardLinksWasUp = false; neighbors.resize(0); edges.resize(0); weights.resize(0); linkType.resize(0); localIdx.resize(0); globalIdx.resize(0); attributes.resize(0); treesQuality.resize(0); maxLoopDim = MAX_LOOP_DIM; maxChainLen = MAX_CHAIN_LEN; maxAvailMemory = 0; countRequestsToAdd = 0; countMissToAdd = 0; } std::set FindTrees(std::vector &inTree, std::vector> &vertByTrees); bool SaveGraphToFile(FILE *file); bool LoadGraphFromFile(FILE *file); int AddToGraph(const vType &V1, const vType &V2, const wType &W, const attrType &attr, const uint8_t linkType); void GetAllSimpleLoops(std::vector>> &cycles, bool needPrint, bool useSavedQ); int SortLoopsBySize(std::vector> &cycles, bool needPrint); int SortLoopsByWeight(std::vector> &cycles, bool needPrint); int GetConflictCycles(const std::vector> &cycles, const Arrays &allArrays, std::vector> &indexOfConflict, bool needPrint); void RemoveMultipleArcsByWeights(); void RemoveMultipleArcsOptimal(); void RemoveVerticesByWeight(); int CreateGraphWiz(const char *fileName, const std::vector> &toDelArcs, const Arrays &allArrays, const bool onlyTree); void RemovedEdges(const std::vector> &toDelArcs, const Arrays &allArrays); void HighlightLinks(); int GetAlignRuleForArray(Array *inputArray, const Arrays &allArrays, std::vector>> &assignedArrays); int GetAlignRuleWithTemplate(Array *inputArray, const Arrays &allArrays, std::vector>> &rules, const uint64_t regionId); void FindLinksBetweenArrays(const Arrays &allArrays, const Array *from, const Array *to, std::vector &links) const; void FindLinkWithMaxDim(const vType from, const Arrays &allArrays, std::pair &result, std::set &wasDone); int FindAllArraysTrees(std::map &trees, const Arrays &allArrays); vType GetNumberOfV() const { return numVerts; } vType GetNumberOfE() const { return numEdges; } void SetMaxLoopDim(const int newMaxLoopDim) { maxLoopDim = newMaxLoopDim; } void SetMaxChainLen(const int newMaxChainLen) { maxChainLen = newMaxChainLen; } int GetMaxLoopDim() const { return maxLoopDim; } int GetMaxChainLen() const { return maxChainLen; } void SetMaxAvailMemory(const uint64_t memSize) { maxAvailMemory = memSize; } void ChangeQuality(const int newMaxLoopDim, const int newMaxChainLen) { SetMaxLoopDim(newMaxLoopDim); SetMaxChainLen(newMaxChainLen); } int getCountOfReq() const { return countRequestsToAdd; } int getCountOfMiss() const { return countMissToAdd; } std::vector GetAllAttributes(const int vert) const; int CountOfConnected(const vType startV) const; int CountOfConnectedForArray(const vType startV) const; std::vector> CreateMaximumSpanningTree(); std::pair MakeConnected(const vType startV, std::vector &inSet) const; double CalculateSumOfWeights() const { double ret = 0; for (int z = 0; z < weights.size(); ++z) ret += weights[z] / 2.0; return ret; } void RemoveAllEdgesFromGraph(const std::map>>& toDel, const Arrays& allArrays); }; std::pair Fx(const std::pair &x, const std::pair &F); }