All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.actelion.research.mapReduceGeneric.examples.CalcPi Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
/*
 *     Orbit, a versatile image analysis software for biological image-based quantification.
 *     Copyright (C) 2009 - 2016 Actelion Pharmaceuticals Ltd., Gewerbestrasse 16, CH-4123 Allschwil, Switzerland.
 *
 *     This program 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.
 *
 *     This program 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 this program.  If not, see .
 *
 */

package com.actelion.research.mapReduceGeneric.examples;

import com.actelion.research.mapReduceGeneric.IMapReduce;
import com.actelion.research.mapReduceGeneric.executors.IMapReduceExecutor;
import com.actelion.research.mapReduceGeneric.executors.MapReduceExecutorLocalMultiCore;
import com.actelion.research.mapReduceGeneric.utils.Helpers;
import com.actelion.research.mapReduceGeneric.utils.KeyValue;

import java.util.*;

/**
 * Calculates Pi using a IMapReduceExecutor. Try to use either the Local or LocalMultiCore executor to see the difference.
 */
public class CalcPi implements IMapReduce {
    private Random random = new Random();


    public List> map(Long element) {
        long count = 0;
        long inside = 0;
        for (long l = 0; l < element; l++) {
            count++;
            double x = random.nextDouble();
            double y = random.nextDouble();
            if (Math.hypot(x, y) < 1) inside++;
        }
        double approx = 4d * (inside / (double) count);
        List> result = new ArrayList>();
        result.add(new KeyValue("pi", approx));
        return result;
    }

    public Double reduce(String key, List valueList) {
        double approx = 0d;
        for (double d : valueList) {
            approx += d;
        }
        approx /= valueList.size();
        return approx;
    }

    public Collection parseParams(String s) {
        return Helpers.parseParamsLong(s);
    }

    public String serializeParam(Long element) {
        return Helpers.serializeParamLong(element);
    }


    public static void main(String[] args) throws Exception {
        List input = new ArrayList();
        for (int i = 0; i < 100; i++) input.add(1000000L);
        //IMapReduceExecutor executor = new MapReduceExecutorLocal();
        IMapReduceExecutor executor = new MapReduceExecutorLocalMultiCore();
        Map result = executor.execute(input, new CalcPi());
        for (String s : result.keySet()) {
            System.out.println(s + ": " + result.get(s));
        }

    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy