Est-ce que Java paralléliser automatiquement les fonctions récursives?

manolo:

Je parallélisation une fonction récursive et je mesurais pauvre speedup. Lors du débogage du code, j'ai remarqué que dans la version séquentielle tous les cœurs fonctionnent.

J'ai reproduit ce comportement dans un exemple minimal, et encore une fois, tous mes noyaux ont une charge de travail d'environ 90%. J'utilise Java 8 (OpenJDK).

Java est fait automatiquement le parallélisme à mon insu? Comment Java est en train de faire cela?

import java.util.Random;
import java.util.ArrayList;

class Node
{
    float value;
    ArrayList<Node> children;

    public Node()
    {
        children = new ArrayList<Node>();
    }

    public Node(float value)
    {
        this.value = value;
    }

    public int count()
    {
        int count = 1;

        if (children != null)
            for (Node c : children)
                count += c.count();

        return count;
    }
}

public class ProofOfConcept {
    final static int N_NODES = 10000000;
    final static int MAX_CHILDREN = 6;

    final static Random RAND = new Random();

    static Node generateTree(int nNodes)
    {
        if (nNodes > 1)
        {
            Node result = new Node();
            int nChildren = 1 + RAND.nextInt(Math.min(MAX_CHILDREN, nNodes) - 1);
            int nNodesPerChild = (nNodes - 1) / nChildren;

            for (int i = 0; i < nChildren; ++i)
            {
                Node t = generateTree(nNodesPerChild);
                result.children.add(t);
            }

            return result;
        }
        else
            return new Node(RAND.nextFloat());
    }

    public static void main(String[] args)
    {
        Node t = generateTree(N_NODES);
        System.out.println(t.count());
    }
}

EDIT: Ceci est aussi vraiment bizarre pour moi. Je joins une capture d' écran de htop; comme vous pouvez le voir, nous avons le processus principal et huit fils (un pour chacun de mes cœurs logiques).

htop

EDIT 2: It seems like GC is doing its job in parallel. For those who do not understand why GC is triggered if apparently no objects are being freed, you should read the following reference:

When a garbage collection is triggered by an allocation failure, but the garbage collection does not free enough space, the Garbage Collector expands the storage heap. During heap expansion, the Garbage Collector takes storage from the maximum amount of storage reserved for the heap (the amount specified by the -Xmx option), and adds it to the active part of the heap (which began as the size specified by the -Xms option). Heap expansion does not increase the amount of storage required for the JVM, because the maximum amount of storage specified by the -Xmx option has already been allocated to the JVM at startup. If the value of the -Xms option provides sufficient storage in the active part of the heap for your applications, the Garbage Collector does not have to carry out heap expansion at all.

Karol Dowbecki :

Non, Java ne fait pas par magie votre code parallèle.

Si vous voyez l'utilisation de 90% sur l'ensemble de base, il est soit le système d'exploitation, d'autres processus ou faire un travail de fond JVM. Il pourrait être JVM en utilisant GC parallèle en utilisant tous les noyaux à ordures Collect.

Je suppose que tu aimes

Origine http://43.154.161.224:23101/article/api/json?id=201233&siteId=1
conseillé
Classement