Java Code Examples for org.apache.spark.api.java.JavaRDD#fold()

The following examples show how to use org.apache.spark.api.java.JavaRDD#fold() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RDDAggregateUtils.java    From systemds with Apache License 2.0 5 votes vote down vote up
public static MatrixBlock sumStable( JavaRDD<MatrixBlock> in )
{
	//stable sum of all blocks with correction block per function instance
	if( TREE_AGGREGATION ) {
		return in.treeReduce( 
				new SumSingleBlockFunction(true) );	
	}
	else { //DEFAULT
		//reduce-all aggregate via fold instead of reduce to allow 
		//for update in-place w/o deep copy of left-hand-side blocks
		return in.fold(
				new MatrixBlock(), 
				new SumSingleBlockFunction(false));
	}
}
 
Example 2
Source File: RDDAggregateUtils.java    From systemds with Apache License 2.0 5 votes vote down vote up
/**
 * Single block aggregation over rdds with corrections for numerical stability.
 * 
 * @param in matrix as {@code JavaRDD<MatrixBlock>}
 * @param aop aggregate operator
 * @return matrix block
 */
public static MatrixBlock aggStable( JavaRDD<MatrixBlock> in, AggregateOperator aop )
{
	//stable aggregate of all blocks with correction block per function instance
	
	//reduce-all aggregate via fold instead of reduce to allow 
	//for update in-place w/o deep copy of left-hand-side blocks
	return in.fold(
			new MatrixBlock(),
			new AggregateSingleBlockFunction(aop) );
}
 
Example 3
Source File: RDDAggregateUtils.java    From systemds with Apache License 2.0 5 votes vote down vote up
/**
 * Single block aggregation over rdds with corrections for numerical stability.
 *
 * @param in tensor as {@code JavaRDD<TensorBlock>}
 * @param aop aggregate operator
 * @return tensor block
 */
public static TensorBlock aggStableTensor(JavaRDD<TensorBlock> in, AggregateOperator aop )
{
	//stable aggregate of all blocks with correction block per function instance

	//reduce-all aggregate via fold instead of reduce to allow
	//for update in-place w/o deep copy of left-hand-side blocks
	return in.fold(
			new TensorBlock(),
			new AggregateSingleTensorBlockFunction(aop) );
}
 
Example 4
Source File: JavaStandaloneIgniteRDDSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testReadDataFromIgnite() throws Exception {
    JavaSparkContext sc = new JavaSparkContext("local[*]", "test");

    try {
        JavaIgniteContext<String, Integer> ic = new JavaIgniteContext<>(sc, new IgniteConfigProvider());

        Ignite ignite = Ignition.ignite("grid-0");

        IgniteCache<String, Integer> cache = ignite.cache(ENTITY_CACHE_NAME);

        for (int i = 0; i < KEYS_CNT; i++)
            cache.put(String.valueOf(i), i);

        JavaRDD<Integer> values = ic.fromCache(ENTITY_CACHE_NAME).map(STR_INT_PAIR_TO_INT_F);

        int sum = values.fold(0, SUM_F);

        int expSum = (KEYS_CNT * KEYS_CNT + KEYS_CNT) / 2 - KEYS_CNT;

        assertEquals(expSum, sum);
    }
    finally {
        sc.stop();
    }
}
 
Example 5
Source File: JavaEmbeddedIgniteRDDSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testReadDataFromIgnite() throws Exception {
    JavaSparkContext sc = createContext();

    JavaIgniteContext<String, Integer> ic = null;

    try {
        ic = new JavaIgniteContext<>(sc, new IgniteConfigProvider(), false);

        Ignite ignite = ic.ignite();

        IgniteCache<String, Integer> cache = ignite.cache(PARTITIONED_CACHE_NAME);

        for (int i = 0; i < KEYS_CNT; i++)
            cache.put(String.valueOf(i), i);

        JavaRDD<Integer> values = ic.fromCache(PARTITIONED_CACHE_NAME).map(STR_INT_PAIR_TO_INT_F);

        int sum = values.fold(0, SUM_F);

        int expSum = (KEYS_CNT * KEYS_CNT + KEYS_CNT) / 2 - KEYS_CNT;

        assertEquals(expSum, sum);
    }
    finally {
        if (ic != null)
            ic.close(true);

        sc.stop();
    }
}
 
Example 6
Source File: JavaStandaloneIgniteRDDSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testReadDataFromIgnite() throws Exception {
    JavaSparkContext sc = new JavaSparkContext("local[*]", "test");

    try {
        JavaIgniteContext<String, Integer> ic = new JavaIgniteContext<>(sc, new IgniteConfigProvider());

        Ignite ignite = Ignition.ignite("grid-0");

        IgniteCache<String, Integer> cache = ignite.cache(ENTITY_CACHE_NAME);

        for (int i = 0; i < KEYS_CNT; i++)
            cache.put(String.valueOf(i), i);

        JavaRDD<Integer> values = ic.fromCache(ENTITY_CACHE_NAME).map(STR_INT_PAIR_TO_INT_F);

        int sum = values.fold(0, SUM_F);

        int expSum = (KEYS_CNT * KEYS_CNT + KEYS_CNT) / 2 - KEYS_CNT;

        assertEquals(expSum, sum);
    }
    finally {
        sc.stop();
    }
}
 
Example 7
Source File: JavaEmbeddedIgniteRDDSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testReadDataFromIgnite() throws Exception {
    JavaSparkContext sc = createContext();

    JavaIgniteContext<String, Integer> ic = null;

    try {
        ic = new JavaIgniteContext<>(sc, new IgniteConfigProvider(), false);

        Ignite ignite = ic.ignite();

        IgniteCache<String, Integer> cache = ignite.cache(PARTITIONED_CACHE_NAME);

        for (int i = 0; i < KEYS_CNT; i++)
            cache.put(String.valueOf(i), i);

        JavaRDD<Integer> values = ic.fromCache(PARTITIONED_CACHE_NAME).map(STR_INT_PAIR_TO_INT_F);

        int sum = values.fold(0, SUM_F);

        int expSum = (KEYS_CNT * KEYS_CNT + KEYS_CNT) / 2 - KEYS_CNT;

        assertEquals(expSum, sum);
    }
    finally {
        if (ic != null)
            ic.close(true);

        sc.stop();
    }
}
 
Example 8
Source File: RDDAggregateUtils.java    From systemds with Apache License 2.0 5 votes vote down vote up
public static MatrixBlock sumStable( JavaRDD<MatrixBlock> in )
{
	//stable sum of all blocks with correction block per function instance
	if( TREE_AGGREGATION ) {
		return in.treeReduce( 
				new SumSingleBlockFunction(true) );	
	}
	else { //DEFAULT
		//reduce-all aggregate via fold instead of reduce to allow 
		//for update in-place w/o deep copy of left-hand-side blocks
		return in.fold(
				new MatrixBlock(), 
				new SumSingleBlockFunction(false));
	}
}
 
Example 9
Source File: RDDAggregateUtils.java    From systemds with Apache License 2.0 5 votes vote down vote up
/**
 * Single block aggregation over rdds with corrections for numerical stability.
 * 
 * @param in matrix as {@code JavaRDD<MatrixBlock>}
 * @param aop aggregate operator
 * @return matrix block
 */
public static MatrixBlock aggStable( JavaRDD<MatrixBlock> in, AggregateOperator aop )
{
	//stable aggregate of all blocks with correction block per function instance
	
	//reduce-all aggregate via fold instead of reduce to allow 
	//for update in-place w/o deep copy of left-hand-side blocks
	return in.fold(
			new MatrixBlock(),
			new AggregateSingleBlockFunction(aop) );
}
 
Example 10
Source File: RDDAggregateUtils.java    From systemds with Apache License 2.0 5 votes vote down vote up
/**
 * Single block aggregation over rdds with corrections for numerical stability.
 *
 * @param in tensor as {@code JavaRDD<TensorBlock>}
 * @param aop aggregate operator
 * @return tensor block
 */
public static TensorBlock aggStableTensor(JavaRDD<TensorBlock> in, AggregateOperator aop )
{
	//stable aggregate of all blocks with correction block per function instance

	//reduce-all aggregate via fold instead of reduce to allow
	//for update in-place w/o deep copy of left-hand-side blocks
	return in.fold(
			new TensorBlock(),
			new AggregateSingleTensorBlockFunction(aop) );
}
 
Example 11
Source File: AddTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd() {
    List<INDArray> list = new ArrayList<>();
    for (int i = 0; i < 5; i++)
        list.add(Nd4j.ones(5));
    JavaRDD<INDArray> rdd = sc.parallelize(list);
    INDArray sum = rdd.fold(Nd4j.zeros(5), new Add());
    assertEquals(25, sum.sum(Integer.MAX_VALUE).getDouble(0), 1e-1);
}