1 /* =============================================================================
5 * =============================================================================
7 * For the license of ssca2, please see ssca2/COPYRIGHT
9 * ------------------------------------------------------------------------
11 * Unless otherwise noted, the following license applies to STAMP files:
13 * Copyright (c) 2007, Stanford University
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions are
20 * * Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
23 * * Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
28 * * Neither the name of Stanford University nor the names of its
29 * contributors may be used to endorse or promote products derived
30 * from this software without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
33 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
42 * THE POSSIBILITY OF SUCH DAMAGE.
44 * =============================================================================
47 public class ComputeGraph {
49 public GraphSDG SDGdataPtr;
51 public int[] global_p;
52 public int global_maxNumVertices;
53 public int global_outVertexListSize;
54 public int[] global_impliedEdgeList;
55 public int[][] global_auxArr;
57 public ComputeGraph() {
59 global_maxNumVertices = 0;
60 global_outVertexListSize = 0;
61 global_impliedEdgeList = null;
65 public int NOSHARE(int x) {
70 /* =============================================================================
72 * =============================================================================
75 prefix_sums (int myId, int numThread, int[] result, int[] input, int arraySize)
79 p = new int[NOSHARE(numThread)];
83 Barrier.enterBarrier();
90 int r = arraySize / numThread;
93 if (myId == (numThread - 1)) {
97 for (int j = start; j < end; j++) {
98 result[j] = input[j-1] + result[j-1];
101 p[NOSHARE(myId)] = result[end-1];
103 Barrier.enterBarrier();
106 for (int j = 1; j < numThread; j++) {
107 p[NOSHARE(j)] += p[NOSHARE(j-1)];
111 Barrier.enterBarrier();
114 int add_value = p[NOSHARE(myId-1)];
115 for (j = start-1; j < end; j++) {
116 result[j] += add_value;
120 Barrier.enterBarrier();
128 /* =============================================================================
130 * =============================================================================
133 computeGraph (int myId, int numThread, Globals glb, ComputeGraph computeGraphArgs)
136 Barrier.enterBarrier();
138 //Graph GPtr = computeGraphArgs.GPtr;
139 //GraphSDG SDGdataPtr = computeGraphArgs.SDGdata;
142 int maxNumVertices = 0;
143 int numEdgesPlaced = computeGraphArgs.SDGdataPtr.numEdgesPlaced;
146 * First determine the number of vertices by scanning the tuple
149 LocalStartStop lss = new LocalStartStop();
150 CreatePartition.createPartition(0, numEdgesPlaced, myId, numThread, lss);
152 for (int i = lss.i_start; i < lss.i_stop; i++) {
153 if (computeGraphArgs.SDGdataPtr.startVertex[i] > maxNumVertices) {
154 maxNumVertices = computeGraphArgs.SDGdataPtr.startVertex[i];
159 int tmp_maxNumVertices = global_maxNumVertices;
160 int new_maxNumVertices = CreatePartition.MAX( tmp_maxNumVertices, maxNumVertices) + 1;
161 global_maxNumVertices = new_maxNumVertices;
164 Barrier.enterBarrier();
166 maxNumVertices = global_maxNumVertices;
170 computeGraphArgs.GPtr.numVertices = maxNumVertices;
171 computeGraphArgs.GPtr.numEdges = numEdgesPlaced;
172 computeGraphArgs.GPtr.intWeight = computeGraphArgs.SDGdataPtr.intWeight;
173 computeGraphArgs.GPtr.strWeight = computeGraphArgs.SDGdataPtr.strWeight;
175 for (int i = 0; i < numEdgesPlaced; i++) {
176 if (computeGraphArgs.GPtr.intWeight[numEdgesPlaced-i-1] < 0) {
177 computeGraphArgs.GPtr.numStrEdges = -(computeGraphArgs.GPtr.intWeight[numEdgesPlaced-i-1]) + 1;
178 computeGraphArgs.GPtr.numIntEdges = numEdgesPlaced - computeGraphArgs.GPtr.numStrEdges;
183 computeGraphArgs.GPtr.outDegree = new int[computeGraphArgs.GPtr.numVertices];
185 computeGraphArgs.GPtr.outVertexIndex = new int[computeGraphArgs.GPtr.numVertices];
188 Barrier.enterBarrier();
190 CreatePartition.createPartition(0, computeGraphArgs.GPtr.numVertices, myId, numThread, lss);
192 for (i = lss.i_start; i < lss.i_stop; i++) {
193 computeGraphArgs.GPtr.outDegree[i] = 0;
194 computeGraphArgs.GPtr.outVertexIndex[i] = 0;
197 int outVertexListSize = 0;
199 Barrier.enterBarrier();
203 for (int i = lss.i_start; i < lss.i_stop; i++) {
205 if ((outVertexListSize == 0) && (k != 0)) {
207 for (int j = 0; j < numEdgesPlaced; j++) {
208 if (k == computeGraphArgs.SDGdataPtr.startVertex[j]) {
218 if ((outVertexListSize == 0) && (k == 0)) {
222 for (int j = i0; j < numEdgesPlaced; j++) {
223 if (i == computeGraphArgs.GPtr.numVertices-1) {
226 if ((i != computeGraphArgs.SDGdataPtr.startVertex[j])) {
227 if ((j > 0) && (i == computeGraphArgs.SDGdataPtr.startVertex[j-1])) {
230 computeGraphArgs.GPtr.outDegree[i]++;
231 for (int t = (i0+1); t < j; t++) {
232 if (computeGraphArgs.SDGdataPtr.endVertex[t] !=
233 computeGraphArgs.SDGdataPtr.endVertex[t-1])
236 computeGraphArgs.GPtr.outDegree[i] = computeGraphArgs.GPtr.outDegree[i]+1;
246 if (i == computeGraphArgs.GPtr.numVertices-1) {
247 if (numEdgesPlaced-i0 >= 0) {
249 computeGraphArgs.GPtr.outDegree[i]++;
250 for (int t = (i0+1); t < numEdgesPlaced; t++) {
251 if (computeGraphArgs.SDGdataPtr.endVertex[t] != computeGraphArgs.SDGdataPtr.endVertex[t-1]) {
253 computeGraphArgs.GPtr.outDegree[i]++;
261 Barrier.enterBarrier();
263 prefix_sums(myId, numThread, computeGraphArgs.GPtr.outVertexIndex, computeGraphArgs.GPtr.outDegree, computeGraphArgs.GPtr.numVertices);
265 Barrier.enterBarrier();
268 global_outVertexListSize = global_outVertexListSize + outVertexListSize;
271 Barrier.enterBarrier();
273 outVertexListSize = global_outVertexListSize;
276 computeGraphArgs.GPtr.numDirectedEdges = outVertexListSize;
277 computeGraphArgs.GPtr.outVertexList = new int[outVertexListSize];
278 computeGraphArgs.GPtr.paralEdgeIndex = new int[outVertexListSize];
279 computeGraphArgs.GPtr.outVertexList[0] = computeGraphArgs.SDGdataPtr.endVertex[0];
282 Barrier.enterBarrier();
285 * Evaluate outVertexList
290 for (int i = lss.i_start; i < lss.i_stop; i++) {
293 while ((i0 == -1) && (k != 0)) {
294 for (int j = 0; j < numEdgesPlaced; j++) {
295 if (k == computeGraphArgs.SDGdataPtr.startVertex[j]) {
303 if ((i0 == -1) && (k == 0)) {
307 for (int j = i0; j < numEdgesPlaced; j++) {
308 if (i == computeGraphArgs.GPtr.numVertices-1) {
311 if (i != computeGraphArgs.SDGdataPtr.startVertex[j]) {
312 if ((j > 0) && (i == computeGraphArgs.SDGdataPtr.startVertex[j-1])) {
314 int ii = (computeGraphArgs.GPtr.outVertexIndex[i]);
316 computeGraphArgs.GPtr.paralEdgeIndex[ii] = i0;
317 computeGraphArgs.GPtr.outVertexList[ii] = computeGraphArgs.SDGdataPtr.endVertex[ i0];
319 for (int t = (i0+1); t < j; t++) {
320 if (computeGraphArgs.SDGdataPtr.endVertex[t] !=
321 computeGraphArgs.SDGdataPtr.endVertex[t-1])
323 computeGraphArgs.GPtr.paralEdgeIndex[ii+r] = t;
324 computeGraphArgs.GPtr.outVertexList[ii+r] = computeGraphArgs.SDGdataPtr.endVertex[t];
336 if (i == computeGraphArgs.GPtr.numVertices-1) {
338 if (numEdgesPlaced-i0 >= 0) {
339 int ii = computeGraphArgs.GPtr.outVertexIndex[i];
340 computeGraphArgs.GPtr.paralEdgeIndex[ii+r] = i0;
341 computeGraphArgs.GPtr.outVertexList[ii+r] = computeGraphArgs.SDGdataPtr.endVertex[i0];
343 for (int t = i0+1; t < numEdgesPlaced; t++) {
344 if (computeGraphArgs.SDGdataPtr.endVertex[t] != computeGraphArgs.SDGdataPtr.endVertex[t-1]) {
345 computeGraphArgs.GPtr.paralEdgeIndex[ii+r] = t;
346 computeGraphArgs.GPtr.outVertexList[ii+r] = computeGraphArgs.SDGdataPtr.endVertex[t];
355 Barrier.enterBarrier();
358 computeGraphArgs.SDGdataPtr.startVertex = null;
359 computeGraphArgs.SDGdataPtr.endVertex = null;
360 computeGraphArgs.GPtr.inDegree = new int[computeGraphArgs.GPtr.numVertices];
361 computeGraphArgs.GPtr.inVertexIndex = new int[computeGraphArgs.GPtr.numVertices];
364 Barrier.enterBarrier();
366 for (int i = lss.i_start; i < lss.i_stop; i++) {
367 computeGraphArgs.GPtr.inDegree[i] = 0;
368 computeGraphArgs.GPtr.inVertexIndex[i] = 0;
371 /* A temp. array to store the inplied edges */
372 int[] impliedEdgeList;
375 impliedEdgeList = new int[computeGraphArgs.GPtr.numVertices * glb.MAX_CLUSTER_SIZE];
376 global_impliedEdgeList = impliedEdgeList;
379 Barrier.enterBarrier();
381 impliedEdgeList = global_impliedEdgeList;
383 CreatePartition.createPartition(0,
384 (computeGraphArgs.GPtr.numVertices * glb.MAX_CLUSTER_SIZE),
389 for (int i = lss.i_start; i < lss.i_stop; i++) {
390 impliedEdgeList[i] = 0;
394 * An auxiliary array to store implied edges, in case we overshoot
400 auxArr = new int[computeGraphArgs.GPtr.numVertices][glb.MAX_CLUSTER_SIZE];
401 global_auxArr = auxArr;
404 Barrier.enterBarrier();
406 auxArr = global_auxArr;
408 CreatePartition.createPartition(0, computeGraphArgs.GPtr.numVertices, myId, numThread, lss);
410 for (int i = lss.i_start; i < lss.i_stop; i++) {
411 /* Inspect adjacency list of vertex i */
412 for (j = computeGraphArgs.GPtr.outVertexIndex[i];
413 j < (computeGraphArgs.GPtr.outVertexIndex[i] + computeGraphArgs.GPtr.outDegree[i]);
416 int v = (computeGraphArgs.GPtr.outVertexList[j]);
418 for (k = computeGraphArgs.GPtr.outVertexIndex[v];
419 k < (computeGraphArgs.GPtr.outVertexIndex[v] + computeGraphArgs.GPtr.outDegree[v]);
422 if (computeGraphArgs.GPtr.outVertexList[k] == i) {
426 if (k == computeGraphArgs.GPtr.outVertexIndex[v]+computeGraphArgs.GPtr.outDegree[v]) {
428 /* Add i to the impliedEdgeList of v */
429 int inDegree = computeGraphArgs.GPtr.inDegree[v];
430 computeGraphArgs.GPtr.inDegree[v] = (inDegree + 1);
431 if ( inDegree < glb.MAX_CLUSTER_SIZE) {
432 impliedEdgeList[v*glb.MAX_CLUSTER_SIZE+inDegree] = i
434 /* Use auxiliary array to store the implied edge */
435 /* Create an array if it's not present already */
437 if ((inDegree % glb.MAX_CLUSTER_SIZE) == 0) {
438 a = new int[glb.MAX_CLUSTER_SIZE];
443 a[inDegree % MAX_CLUSTER_SIZE] = i;
450 Barrier.enterBarrier();
452 prefix_sums(myId, numThread, computeGraphArgs.GPtr.inVertexIndex, computeGraphArgs.GPtr.inDegree, computeGraphArgs.GPtr.numVertices);
455 computeGraphArgs.GPtr.numUndirectedEdges = computeGraphArgs.GPtr.inVertexIndex[computeGraphArgs.GPtr.numVertices-1]
456 + computeGraphArgs.GPtr.inDegree[computeGraphArgs.GPtr.numVertices-1];
457 computeGraphArgs.GPtr.inVertexList = new int[computeGraphArgs.GPtr.numUndirectedEdges];
460 Barrier.enterBarrier();
463 * Create the inVertex List
466 for (int i = lss.i_start; i < lss.i_stop; i++) {
467 for (int j = computeGraphArgs.GPtr.inVertexIndex[i];
468 j < (computeGraphArgs.GPtr.inVertexIndex[i] + computeGraphArgs.GPtr.inDegree[i]);
471 if ((j - computeGraphArgs.GPtr.inVertexIndex[i]) < glb.MAX_CLUSTER_SIZE) {
472 computeGraphArgs.GPtr.inVertexList[j] =
473 impliedEdgeList[i*glb.MAX_CLUSTER_SIZE+j-computeGraphArgs.GPtr.inVertexIndex[i]];
475 computeGraphArgs.GPtr.inVertexList[j] =
476 auxArr[i][(j-computeGraphArgs.GPtr.inVertexIndex[i]) % glb.MAX_CLUSTER_SIZE];
481 Barrier.enterBarrier();
484 impliedEdgeList = null;
487 for (i = i_start; i < i_stop; i++) {
488 if (computeGraphArgs.GPtr.inDegree[i] > MAX_CLUSTER_SIZE) {
493 Barrier.enterBarrier();
502 /* =============================================================================
504 * End of computeGraph.java
506 * =============================================================================