an example of parallel calculation of metrics

Jay Norwood via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Sep 30 15:24:23 PDT 2015


This is something I'm playing with for work. We do this a lot, 
capture counter events for some number of on-chip performance 
counters, compute some metrics, display the outputs. This seems 
ideal for the application.

import std.algorithm, std.parallelism, std.range;
import std.stdio;
import std.datetime;
import std.typecons;
import std.meta;

// define some input measurement sample tuples and output metric 
tuples
alias TI = Tuple!(long, long, long, long, long);
alias TO = Tuple!(long, long, long, long);

// various metric definitions
// the Tuples could also define names for each member and use the 
names here in the metrics.
long met1( TI m){ return m[0] + m[1] + m[2]; }
long met2( TI m){ return m[1] + m[2] + m[3]; }
long met3( TI m){ return m[0] - m[1] + m[2]; }
long met4( TI m){ return m[0] + m[1] - m[2]; }

// a convenience to use all the metrics above as a list
alias Metrics = AliasSeq!(met1,met2,met3,met4);

void main(string[] argv)
{
	auto samples = iota(1_000);
	auto meas = new TI[samples.length];
	auto results = new TO[samples.length];

	// Initialize some values for the measured samples
	foreach(i, ref m; meas){
		m[0] = i;
		m[1] = i+1;
		m[2] = i+2;
		m[3] = i+3;
		m[4] = i+4;
	}

	std.datetime.StopWatch sw;
	sw.start();

     ref TI getTerm(int i)
     {
         return meas[i];
     }

	// compute the metric results for the above measured sample 
values in parallel
	taskPool.amap!(Metrics)(std.algorithm.map!getTerm(samples),results);

	// how long did this take
	long exec_ms = sw.peek().msecs;
	writeln("results:", results);
	writeln("time:", exec_ms);

}




More information about the Digitalmars-d-learn mailing list