package edu.rice.hj.example.comp322.assignments.hw2; import edu.rice.hj.api.SuspendableException; import static edu.rice.hj.Module1.asyncNb; import static edu.rice.hj.Module1.finish; /** * This file is a skeleton for your GeneralizedReduce implementation. You can assume that each client includes a call to * perf.doWork(1) for each call to the combine() method. This abstraction assumes that each call to combine() takes 1 * unit of time, and ignores the cost all other computations. You should aim for maximum parallelism with respect to * this abstraction. All code should include basic documentation for each method in this class. *

* Though you will need to extend this file for your homework submission, do not alter the declarations below in any * way, other than filling in the "..." code regions. * * @author Vivek Sarkar (vsarkar@rice.edu) */ public class GeneralizedReduceSol { private final GeneralizedReduceApp app; /** *

Constructor for GeneralizedReduceSol.

* * @param a a {@link edu.rice.hj.example.comp322.assignments.hw2.GeneralizedReduceApp} object. */ public GeneralizedReduceSol(final GeneralizedReduceApp a) { app = a; } /** *

run.

* * @param inArray an array of T objects. * @return a T object. */ public T run(final T[] inArray) throws SuspendableException { for (int strideOut = 1; strideOut < inArray.length; strideOut *= 2) { final int size = (inArray.length + (2 * strideOut) - 1) / (2 * strideOut); final int stride = strideOut; finish(() -> { for (int indexOut = 0; indexOut < size; indexOut++) { final int index = indexOut; asyncNb(() -> { final int index1 = 2 * index * stride; final int index2 = (2 * index + 1) * stride; if (index2 < inArray.length) { inArray[index1] = app.combine(inArray[index1], inArray[index2]); } }); } }); } return app.combine(app.init(), inArray[0]); } }