Tim wrote:

Sorry, complicated_computation() and f() are simplified too much. They do take more inputs. Among the inputs to complicated_computation(), some is passed from the main() to f() by address since it is a big array, some is passed by value, some are created inside f() before the call to complicated_computation().
so actually (although not exactly) the code is like:
I think I'm agreeing with Terry.  But, to add more detail:

int main(int argc, char ** argv) { int size;
     double *feature = new double[1000];
    // compute values of elements of "feature"
    // some operations
The array "feature" can be computed by the master and then broadcast, or it could be computed redundantly by each process.

f(size, feature); // some operations delete [] feature; return 0; } void f(int size, double *feature) { vector<double> coeff; // read from a file into elements of coeff

Similarly, coeff can be read in by the master and then broadcast, or it could be read redundantly by each process, or each process could read only the portion that it will need.


    MyClass myobj;
double * array = new double [coeff.size()]; for (int i = 0; i < coeff.size(); i++) // need to speed up by MPI. { array[i] = myobj.complicated_computation(size, coeff[i], feature); // time consuming }
Each process loops only over the iterations that correspond to its rank. Then, the master gathers all results.

// some operations using all elements in array delete [] array;
    }
Once the slaves have finished their computations and sent their results to the master, they may exit. The slaves will be launched at the same time as the master, but presumably have less to do than the master does before the "parallel loop" starts. If you don't want slaves consuming excessive CPU time while they wait for the master, fix that problem later once you have the basic code working.

Reply via email to