Package cc.redberry.transformation.ec

Source Code of cc.redberry.transformation.ec.EqualCollectInputPort

/*
* Redberry: symbolic tensor computations.
*
* Copyright (c) 2010-2012:
*   Stanislav Poslavsky   <stvlpos@mail.ru>
*   Bolotin Dmitriy       <bolotin.dmitriy@gmail.com>
*
* This file is part of Redberry.
*
* Redberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Redberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Redberry. If not, see <http://www.gnu.org/licenses/>.
*/
package cc.redberry.transformation.ec;

import cc.redberry.concurrent.ConcurrentGrowingList;
import cc.redberry.concurrent.InputPort;
import cc.redberry.concurrent.OutputPortUnsafe;
import cc.redberry.core.context.CC;
import cc.redberry.core.indexmapping.IndexMappingBuffer;
import cc.redberry.core.indexmapping.IndexMappingBufferTester;
import cc.redberry.core.indexmapping.IndexMappings;
import cc.redberry.core.indices.IndicesUtils;
import cc.redberry.core.tensor.Product;
import cc.redberry.core.tensor.Sum;
import cc.redberry.core.tensor.Tensor;
import cc.redberry.core.tensor.TensorNumber;
import cc.redberry.transformation.concurrent.CollectIP;
import cc.redberry.transformation.concurrent.CollectIPFactory;
import java.util.concurrent.ConcurrentHashMap;

/**
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
*/
public class EqualCollectInputPort implements InputPort<Split> {
    private final ConcurrentHashMap<Integer, ConcurrentGrowingList<FactorNode>> nodes = new ConcurrentHashMap<>();
    private final CollectIPFactory cipFactory;
    private final ThreadLocal<ConcurrentGrowingList<FactorNode>> threadLocalList = new ThreadLocal<ConcurrentGrowingList<FactorNode>>() {
        @Override
        protected ConcurrentGrowingList<FactorNode> initialValue() {
            return new ConcurrentGrowingList<>();
        }
    };

    public EqualCollectInputPort(CollectIPFactory cipFactory) {
        this.cipFactory = cipFactory;
    }

    @Override
    public void put(Split split) throws InterruptedException {
        int hash = split.factor.hashCode();

        //List with factors with the same hashes
        ConcurrentGrowingList<FactorNode> growingList;

        //Chached instance
        ConcurrentGrowingList<FactorNode> newList = threadLocalList.get();
        growingList = nodes.putIfAbsent(hash, newList);

        if (growingList == null) //If new list was putted successfuly
        {
            growingList = newList;
            threadLocalList.remove();
        }

        ConcurrentGrowingList<FactorNode>.GrowingIterator iterator = growingList.iterator();

        FactorNode newFactorNode = null;
        FactorNode current;
        while (true) {
            current = iterator.next();
            if (current == null) {
                if (newFactorNode == null)
                    newFactorNode = new FactorNode(cipFactory.create(), split.factor);
                if ((current = iterator.set(newFactorNode)) == null) {
                    newFactorNode.cip.put(split.summand);
                    break;
                }
            }

            Boolean parity = compare(split.factor, current.factor);
            if (parity == null)
                continue;
            if (parity.booleanValue() == false)
                current.cip.put(split.summand);
            else
                current.cip.put(new Product(TensorNumber.createMINUSONE(), split.summand));
            break;
        }
    }

    public Tensor result() throws InterruptedException {
        Sum result = new Sum();
        FactorNode node;
        for (ConcurrentGrowingList<FactorNode> list : nodes.values()) {
            ConcurrentGrowingList<FactorNode>.GrowingIterator gi = list.iterator();
            while ((node = gi.next()) != null)
                result.add(new Product(node.cip.result(), node.factor));
        }
        return result.equivalent();
    }

    private static Boolean compare(Tensor from, Tensor to) {
        int[] fromIndices = from.getIndices().getFreeIndices().getAllIndices().copy();
        for (int i = 0; i < fromIndices.length; ++i)
            fromIndices[i] = IndicesUtils.getNameWithType(fromIndices[i]);
        OutputPortUnsafe<IndexMappingBuffer> provider = IndexMappings.createPort(new IndexMappingBufferTester(fromIndices, CC.withMetric()), from, to);
        IndexMappingBuffer buffer;
        if ((buffer = provider.take()) != null) {
            buffer.removeContracted();
            assert buffer.isEmpty();
            return buffer.getSignum();
        }
        return null;
    }

    private final static class FactorNode {
        final CollectIP cip;
        final Tensor factor;

        public FactorNode(CollectIP cip, Tensor factor) {
            this.cip = cip;
            this.factor = factor;
        }
    }
}
TOP

Related Classes of cc.redberry.transformation.ec.EqualCollectInputPort

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.