def checkmat(m, name): """If run on one processor it will save m. If run on multiple processors it will load the one-proc m and compare it to the argument and complain if they don't match. """ import pyCombBLAS as pcb if pcb._nprocs() == 1: m.save("checkfile_%s" % (name)) else: one = pcb.pySpParMat() one.load("checkfile_%s" % (name)) test = pcb.EWiseApply(m, one, pcb.equal_to()) if test.Count(pcb.bind2nd(pcb.equal_to(), 1)) != test.getnee(): if pcb.root(): print "%s failed." % (name)
def checkvect(v, name): """If run on one processor it will save v. If run on multiple processors it will load the one-proc v and compare it to the argument and complain if they don't match. """ import pyCombBLAS as pcb if pcb._nprocs() == 1: saveVect(v, "checkfile_%s" % (name)) else: one = loadDenseVect("checkfile_%s" % (name), len(v)) if len(one) != len(v): print "%s failed. length_1 = %d, lengh_p = %d" % (name, len(one), len(v)) return one.EWiseApply(v, pcb.equal_to()) if one.Count(pcb.bind2nd(pcb.equal_to(), 1)) != v.getnee(): if pcb.root(): print "%s failed." % (name)
def degree(self): """ calculates the degree of each vertex of the passed HyGraph instance. Input Arguments: self: a HyGraph instance Output Argument: ret: a ParVec instance with each element containing the degree of the corresponding vertex. SEE ALSO: sum """ if self.nedge() == 0: return ParVec.zeros(self.nvert()) ret = self._spm.Reduce(pcb.pySpParMat.Column(),pcb.plus(), pcb.ifthenelse(pcb.bind2nd(pcb.not_equal_to(), 0), pcb.set(1), pcb.set(0))) return ParVec.toParVec(ret)
def npin(self): """ calculates the cardinality of each edge of the passed HyGraph instance. Input Arguments: self: a HyGraph instance Output Argument: ret: a ParVec instance with each element containing the cardinality of the corresponding edge. SEE ALSO: rank, antirank """ if self.nedge() == 0: return ParVec.zeros(self.nedge()) ret = self._spm.Reduce(pcb.pySpParMat.Row(),pcb.plus(), pcb.ifthenelse(pcb.bind2nd(pcb.not_equal_to(), 0), pcb.set(1), pcb.set(0))) return ParVec.toParVec(ret)
def k2validate(G, root, parents): ret = 1; # assume valid nrowG = G.getnrow(); # calculate level in the tree for each vertex; root is at level 0 # about the same calculation as bfsTree, but tracks levels too parents2 = pcb.pyDenseParVec(nrowG, -1); fringe = pcb.pySpParVec(nrowG); parents2[root] = root; fringe[root] = root; levels = pcb.pyDenseParVec(nrowG, -1); levels[root] = 0; level = 1; while fringe.getnee() > 0: fringe.setNumToInd(); G.SpMV_SelMax_inplace(fringe); pcb.EWiseMult_inplacefirst(fringe, parents2, True, -1); #fringe.printall(); parents2.ApplyMasked(pcb.set(0), fringe); parents2.add(fringe); levels.ApplyMasked(pcb.set(level), fringe); level += 1; # spec test #1 # Not implemented # spec test #2 # tree edges should be between verts whose levels differ by 1 #print "starting spec test#2" # root = element of parents that points to itself ##tmp1 = parents.copy() ##tmp1 -= pcb.pyDenseParVec.range(nrowG,0) ##root = tmp1.FindInds_NotEqual(0); #treeEdges = ((parents <> -1) & (parents <> root); tmp1 = parents.copy(); tmp1[root] = -1; treeEdges = tmp1.FindInds(pcb.bind2nd(pcb.not_equal_to(), -1)); #treeI = parents[treeEdges] treeI = parents.SubsRef(treeEdges); #treeJ = 1..nrowG[treeEdges] treeJ = pcb.pyDenseParVec.range(nrowG,0).SubsRef(treeEdges); #if any(levels[treeI]-levels[treeJ] <> -1): tmp1 = levels.SubsRef(treeI); tmp1 -= levels.SubsRef(treeJ); tmp2 = tmp1.FindInds(pcb.bind2nd(pcb.not_equal_to(), -1)); if tmp2.getnee(): print "spec test #2 failed." ret = -1; # spec test #3 # Not implemented # spec test #4 # Not implemented # spec test #5 # Not implemented del G, parents, parents2, fringe, levels, tmp1, tmp2, treeEdges, treeI, treeJ return ret
#Cands = A.FindIndsOfColsWithSumGreaterThan(4); #numAvailableCands = Cands.length() #if (numAvailableCands < numCands): # if (pcb.root()): # print "Not enough vertices in the graph qualify as candidates. Only %d have enough degree."%(numAvailableCands) # numCands = numAvailableCands #Cands.RandPerm(); #First64 = pcb.pyDenseParVec.range(numCands, 0); #Cands = Cands.SubsRef(First64); Cands = degrees.FindInds(pcb.bind2nd(pcb.greater(), 2)) Cands.RandPerm() Firsts = pcb.pyDenseParVec.range(numCands, 0) Cands = Cands[Firsts] #Cands = Cands.SubsRef(Firsts) #if (pcb.root()): # print "The candidates are:" #Cands.printall() if (pcb.root()): print "Starting vertices generated." ###############################################
def k2Validate(G, start, parents): good = True [valid, levels] = G.isBfsTree(start, parents) # isBfsTree implements Graph500 tests 1 and 2 if not valid: if kdt.master(): print "isBfsTree detected failure of Graph500 test %d" % abs(ret) return False # Spec test #3: # every input edge has vertices whose levels differ by no more than 1 edgeMax = kdt.SpParVec.toSpParVec(G._spm.SpMV_SelMax(levels.toSpParVecAll()._spv)) edgeMin = -kdt.SpParVec.toSpParVec(G._spm.SpMV_SelMax((-levels).toSpParVecAll()._spv)) if ((edgeMax-edgeMin) > 1).any(): if kdt.master(): print "At least one graph edge has endpoints whose levels differ by more than one" good = False # Spec test #4: # the BFS tree spans a connected component's vertices (== all edges # either have both endpoints in the tree or not in the tree, or # source is not in tree and destination is the root) # set not-in-tree vertices' levels to -2 import pyCombBLAS as pcb levels._dpv.Apply(pcb.ifthenelse(pcb.bind2nd(pcb.equal_to(),-1), pcb.set(-2), pcb.identity())) edgeMax = kdt.SpParVec.toSpParVec(G._spm.SpMV_SelMax(levels.toSpParVecAll()._spv)) edgeMin = -kdt.SpParVec.toSpParVec(G._spm.SpMV_SelMax((-levels).toSpParVecAll()._spv)) if ((edgeMax-edgeMin) > 1).any(): if kdt.master(): print "The tree does not span exactly the connected component, root=%d" good = False # Spec test #5: # a vertex and its parent are joined by an edge of the original graph, # except for the root, which has no parent in the tree Gnv = G.nvert(); Gne = G.nedge() [Gi, Gj, ign] = G.toParVec() del ign # non-root tree vertices == NRT Vs NRTVs = (levels!=-2) & (parents!=kdt.ParVec.range(Gnv)) nNRTVs = NRTVs.nnz() TGi = kdt.ParVec.range(nNRTVs) TGj1 = kdt.ParVec.range(Gnv)[NRTVs] TGj2 = parents[NRTVs] M = max(Gne, Gnv) #FIX: really should use SpParMats here, as don't need spm and spmT tmpG1 = kdt.HyGraph(TGi, TGj1, 1, M, Gnv) tmpG2 = kdt.HyGraph(TGi, TGj2, 1, M, Gnv) tmpG1._spm += tmpG2._spm tmpG1._spmT += tmpG2._spmT del tmpG2 tmpG3 = kdt.HyGraph(Gi, Gj, 1, M, Gnv) tmpG4 = kdt.DiGraph() tmpG4._spm = tmpG1._spm.SpMM(tmpG3._spmT) #!? not tmp3._spmT ? maxIncid = tmpG4.max(kdt.DiGraph.Out)[kdt.ParVec.range(Gnv) < nNRTVs] if (maxIncid != 2).any(): if kdt.master(): print "At least one vertex and its parent are not joined by an original edge" good = False return good