Example usage for com.google.common.util.concurrent AtomicDoubleArray length

List of usage examples for com.google.common.util.concurrent AtomicDoubleArray length

Introduction

In this page you can find the example usage for com.google.common.util.concurrent AtomicDoubleArray length.

Prototype

public final int length() 

Source Link

Document

Returns the length of the array.

Usage

From source file:com.twitter.graphjet.demo.PageRankCassovaryDemo.java

public static void main(String[] argv) throws Exception {
    final PageRankCassovaryDemoArgs args = new PageRankCassovaryDemoArgs();
    CmdLineParser parser = new CmdLineParser(args, ParserProperties.defaults().withUsageWidth(90));

    try {/*  w ww  . j  a va 2  s  .  c om*/
        parser.parseArgument(argv);
    } catch (CmdLineException e) {
        System.err.println(e.getMessage());
        parser.printUsage(System.err);
        return;
    }

    DirectedGraph<Node> cgraph = ListOfEdgesGraphReader.forIntIds(args.inputDir, args.inputFile,
            new NodeNumberer.IntIdentity(), false, false, '\t', StoredGraphDir.OnlyOut(), true)
            .toSharedArrayBasedDirectedGraph(scala.Option.apply(null));

    // Wrap the Cassovary graph.
    CassovaryOutIndexedDirectedGraph graph = new CassovaryOutIndexedDirectedGraph(cgraph);

    // Extract the nodes and the max node id.
    LongOpenHashSet nodes = new LongOpenHashSet();
    long maxNodeId = 0;

    scala.collection.Iterator<Node> iter = cgraph.iterator();
    while (iter.hasNext()) {
        Node n = iter.next();
        nodes.add(n.id());
        if (n.id() > maxNodeId) {
            maxNodeId = n.id();
        }
    }

    System.out.println("Verifying loaded graph...");
    long startTime = System.currentTimeMillis();
    AtomicLong graphEdgeCounter = new AtomicLong();
    nodes.forEach(v -> graphEdgeCounter.addAndGet(graph.getOutDegree(v)));
    System.out.println(
            graphEdgeCounter.get() + " edges traversed in " + (System.currentTimeMillis() - startTime) + "ms");

    double prVector[] = null;
    long total = 0;
    for (int i = 0; i < args.trials; i++) {
        startTime = System.currentTimeMillis();
        System.out.print("Trial " + i + ": Running PageRank for " + args.iterations + " iterations - ");

        long endTime;
        if (args.threads == 1) {
            System.out.print("single-threaded: ");
            PageRank pr = new PageRank(graph, nodes, maxNodeId, 0.85, args.iterations, 1e-15);
            pr.run();
            prVector = pr.getPageRankVector();
            endTime = System.currentTimeMillis();
        } else {
            System.out.print(String.format("multi-threaded (%d threads): ", args.threads));
            MultiThreadedPageRank pr = new MultiThreadedPageRank(graph, new LongArrayList(nodes), maxNodeId,
                    0.85, args.iterations, 1e-15, args.threads);
            pr.run();
            endTime = System.currentTimeMillis();
            AtomicDoubleArray prValues = pr.getPageRankVector();
            // We need to convert the AtomicDoubleArray into an ordinary double array.
            // No need to do this more than once.
            if (prVector == null) {
                prVector = new double[prValues.length()];
                for (int n = 0; n < prValues.length(); n++) {
                    prVector[n] = prValues.get(n);
                }
            }
        }

        System.out.println("Complete! Elapsed time = " + (endTime - startTime) + " ms");
        total += endTime - startTime;
    }
    System.out.println("Averaged over " + args.trials + " trials: " + total / args.trials + " ms");

    // Extract the top k.
    if (args.k != 0) {
        TopNodes top = new TopNodes(args.k);
        LongIterator nodeIter = nodes.iterator();
        while (nodeIter.hasNext()) {
            long nodeId = nodeIter.nextLong();
            top.offer(nodeId, prVector[(int) nodeId]);
        }

        for (NodeValueEntry entry : top.getNodes()) {
            System.out.println(entry.getNode() + " " + entry.getValue());
        }
    }
}

From source file:com.twitter.graphjet.demo.PageRankGraphJetDemo.java

public static void main(String[] argv) throws Exception {
    final PageRankGraphJetDemoArgs args = new PageRankGraphJetDemoArgs();
    CmdLineParser parser = new CmdLineParser(args, ParserProperties.defaults().withUsageWidth(90));

    try {//  w w w. j  a v a 2  s  .c o m
        parser.parseArgument(argv);
    } catch (CmdLineException e) {
        System.err.println(e.getMessage());
        parser.printUsage(System.err);
        return;
    }

    String graphPath = args.inputFile;

    OutIndexedPowerLawMultiSegmentDirectedGraph graph = new OutIndexedPowerLawMultiSegmentDirectedGraph(
            args.maxSegments, args.maxEdgesPerSegment, args.numNodes, args.expectedMaxDegree,
            args.powerLawExponent, new IdentityEdgeTypeMask(), new NullStatsReceiver());

    final LongOpenHashSet nodes = new LongOpenHashSet(); // Note, *not* thread safe.
    final AtomicLong fileEdgeCounter = new AtomicLong();
    final AtomicLong maxNodeId = new AtomicLong();

    System.out.println("Loading graph from file...");
    long loadStart = System.currentTimeMillis();

    Files.walk(Paths.get(graphPath)).forEach(filePath -> {
        if (Files.isRegularFile(filePath)) {
            try {
                InputStream inputStream = Files.newInputStream(filePath);
                GZIPInputStream gzip = new GZIPInputStream(inputStream);
                BufferedReader br = new BufferedReader(new InputStreamReader(gzip));
                String line;
                while ((line = br.readLine()) != null) {
                    if (line.startsWith("#"))
                        continue;

                    String[] tokens = line.split("\\s+");
                    if (tokens.length > 1) {
                        final long from = Long.parseLong(tokens[0]);
                        final long to = Long.parseLong(tokens[1]);
                        graph.addEdge(from, to, EDGE_TYPE);
                        fileEdgeCounter.incrementAndGet();

                        // Print logging output every 10 million edges.
                        if (fileEdgeCounter.get() % 10000000 == 0) {
                            System.out.println(String.format("%d million edges read, elapsed time %.2f seconds",
                                    fileEdgeCounter.get() / 1000000,
                                    (System.currentTimeMillis() - loadStart) / 1000.0));
                        }

                        // Note, LongOpenHashSet not thread safe so we need to synchronize manually.
                        synchronized (nodes) {
                            if (!nodes.contains(from)) {
                                nodes.add(from);
                            }
                            if (!nodes.contains(to)) {
                                nodes.add(to);
                            }
                        }

                        maxNodeId.getAndUpdate(x -> Math.max(x, from));
                        maxNodeId.getAndUpdate(x -> Math.max(x, to));
                    }
                }
            } catch (Exception e) {
                // Catch all exceptions and quit.
                e.printStackTrace();
                System.exit(-1);
            }
        }
    });

    long loadEnd = System.currentTimeMillis();
    System.out.println(String.format("Read %d vertices, %d edges loaded in %d ms", nodes.size(),
            fileEdgeCounter.get(), (loadEnd - loadStart)));
    System.out.println(String.format("Average: %.0f edges per second",
            fileEdgeCounter.get() / ((float) (loadEnd - loadStart)) * 1000));

    System.out.println("Verifying loaded graph...");
    long startTime = System.currentTimeMillis();
    AtomicLong graphEdgeCounter = new AtomicLong();
    nodes.forEach(v -> graphEdgeCounter.addAndGet(graph.getOutDegree(v)));
    System.out.println(
            graphEdgeCounter.get() + " edges traversed in " + (System.currentTimeMillis() - startTime) + "ms");

    if (fileEdgeCounter.get() != graphEdgeCounter.get()) {
        System.err.println(String.format("Error, edge counts don't match! Expected: %d, Actual: %d",
                fileEdgeCounter.get(), graphEdgeCounter.get()));
        System.exit(-1);
    }

    double prVector[] = null;
    long total = 0;
    for (int i = 0; i < args.trials; i++) {
        startTime = System.currentTimeMillis();
        System.out.print("Trial " + i + ": Running PageRank for " + args.iterations + " iterations... ");

        long endTime;
        if (args.threads == 1) {
            System.out.print("single-threaded: ");
            PageRank pr = new PageRank(graph, nodes, maxNodeId.get(), 0.85, args.iterations, 1e-15);
            pr.run();
            prVector = pr.getPageRankVector();
            endTime = System.currentTimeMillis();
        } else {
            System.out.print(String.format("multi-threaded (%d threads): ", args.threads));
            MultiThreadedPageRank pr = new MultiThreadedPageRank(graph, new LongArrayList(nodes),
                    maxNodeId.get(), 0.85, args.iterations, 1e-15, args.threads);
            pr.run();
            endTime = System.currentTimeMillis();
            com.google.common.util.concurrent.AtomicDoubleArray prValues = pr.getPageRankVector();
            // We need to convert the AtomicDoubleArray into an ordinary double array.
            // No need to do this more than once.
            if (prVector == null) {
                prVector = new double[prValues.length()];
                for (int n = 0; n < prValues.length(); n++) {
                    prVector[n] = prValues.get(n);
                }
            }
        }

        System.out.println("Complete! Elapsed time = " + (endTime - startTime) + " ms");
        total += endTime - startTime;
    }
    System.out.println("Averaged over " + args.trials + " trials: " + total / args.trials + " ms");

    // Extract the top k.
    if (args.k != 0) {
        TopNodes top = new TopNodes(args.k);
        it.unimi.dsi.fastutil.longs.LongIterator nodeIter = nodes.iterator();
        while (nodeIter.hasNext()) {
            long nodeId = nodeIter.nextLong();
            top.offer(nodeId, prVector[(int) nodeId]);
        }

        for (NodeValueEntry entry : top.getNodes()) {
            System.out.println(entry.getNode() + " " + entry.getValue());
        }
    }
}

From source file:com.analog.lyric.dimple.parameters.ParameterListN.java

protected ParameterListN(ParameterListN<Key> that) {
    AtomicDoubleArray thoseValues = that._values;
    int size = thoseValues.length();
    _values = new AtomicDoubleArray(size);
    for (int i = 0; i < size; ++i) {
        _values.set(i, thoseValues.get(i));
    }// w ww . j  a  va 2s. c  o  m

    AtomicIntegerArray thatFixedMask = that._fixedMask;
    int fixedMaskSize = thatFixedMask.length();
    _fixedMask = new AtomicIntegerArray(fixedMaskSize);
    for (int i = 0; i < fixedMaskSize; ++i) {
        _fixedMask.set(i, thatFixedMask.get(i));
    }
}

From source file:com.twitter.graphjet.algorithms.MultiThreadedPageRank.java

private double computeL1Norm(AtomicDoubleArray a, AtomicDoubleArray b) {
    double ret = 0.0;
    for (int i = 0; i < a.length(); ++i) {
        ret += Math.abs(a.get(i) - b.get(i));
    }/* w  w w. j a va 2 s  .  c  o m*/
    return ret;
}