multithreading - C# serial For --> parallel For: results are jumbled -


this may basic question (and have admit, i'm not experienced parallel programming).

i've written single threaded c# program during array p of parameters created, each parameter p[i] evaluated function f, , pair ( p[i], f( p[i] ) ) of each evaluation placed heap (which gets sorted function value). roughly, looks this:

class agent {   private double[] parameter;   private double value;    public agent( double[] par, double val ) { this.parameter = par; this.val = value; } }  class work {    public void optimise() {      // return array of parameters, each 1 of double array     double[][] parameter = getparameters();     agent[] results = new agent[ parameter.length ];      ( int idx = 0; idx < parameter.length; idx++ )       results[ idx ] = new agent( parameter[idx], cost_function( parameter[ idx ] ) );    }    private double cost_function( double[] par ) {      // evaluate par, result     return result;    } } 

since evaluation of cost_function rather lengthy, , parameter long, thought paralleising that, portioning contents of parameter segments, , using parallel.for on each segment. rather naively, changed routine optimise this:

  public void optimise() {      // select appropriate number of threads, e.g.     int number_of_threads = environment.processorcount;      // return array of parameters, each 1 of double array     double[][] parameter = getparameters();      // split array of parameters #number_of_threads many      // segments (of equal size)     double[][][] par_segments = distribute_parameter( parameter );      agent[][] results = new agent[ number_of_threads ];       // process each segment in individual thread     parallel.for( 0, number_of_threads, idx => {        results[ idx ] = new agent[ par_segments[ idx ].length ];       ( int agent = 0; agent < par_segments[ idx ].length; agent++ )         results[ idx ][ agent ] =            new agent( par_segments[ idx ][ agent ], cost_function( par_segments[ idx ][ agent ] );     } );   } 

my naive expectation was, that, each segment (ie each idx), interior of handled consistently, in particular, in creation of each new agent( p, cost_function( p ) ), 2 p in expression identical, , resulting agent indeed contain corresponding pair of parameter , function value. instead new agent( p1, cost_function( p2 ) ), , i'm don't think p2 neccessarily part of original parameter array.

the parallel.for routine works fine if lock last statement in it, of course rather makes parallelisation useless. there better way ensure results matrix filled consistent parameter/value pairs doesn't require changes original cost_function routine?

if so, there online resource explains this? , final question: book on topic?

thanks in advance! best, rob

it's hard without complete code te grasp it, think mean this:

var result = parameter in parameters.asparallel()              select new agent(parameter, cost_function( parameter)); 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -