@Override public void invoke(InputStream[] fis, OutputStream[] fos, String[] args) { /** inputs: none. */ /** args: (int) number of samples, (int) initial offset. */ int numSamples = Integer.parseInt(args[0]); int offset = Integer.parseInt(args[1]); final HaltonSequence haltonsequence = new HaltonSequence(offset); long numInside = 0L; long numOutside = 0L; for (long i = 0; i < numSamples; ++i) { // generate points in a unit square final double[] point = haltonsequence.nextPoint(); // count points inside/outside of the inscribed circle of the square final double x = point[0] - 0.5; final double y = point[1] - 0.5; if (x * x + y * y > 0.25) { numOutside++; } else { numInside++; } } /** output: single file containing (long) numInside, (long) numOutside. */ try { DataOutputStream out = new DataOutputStream(fos[0]); out.writeLong(numInside); out.writeLong(numOutside); out.close(); } catch (IOException ioe) { throw new RuntimeException(ioe); } }
/** * Map method. * * @param offset samples starting from the (offset+1)th sample. * @param size the number of samples for this map * @param out output {ture->numInside, false->numOutside} * @param reporter */ public void map( LongWritable offset, LongWritable size, OutputCollector<BooleanWritable, LongWritable> out, Reporter reporter) throws IOException { final HaltonSequence haltonsequence = new HaltonSequence(offset.get()); long numInside = 0L; long numOutside = 0L; for (long i = 0; i < size.get(); ) { // generate points in a unit square final double[] point = haltonsequence.nextPoint(); // count points inside/outside of the inscribed circle of the square final double x = point[0] - 0.5; final double y = point[1] - 0.5; if (x * x + y * y > 0.25) { numOutside++; } else { numInside++; } // report status i++; if (i % 1000 == 0) { reporter.setStatus("Generated " + i + " samples."); } } // output map results out.collect(new BooleanWritable(true), new LongWritable(numInside)); out.collect(new BooleanWritable(false), new LongWritable(numOutside)); }