an example of parallel calculation of metrics

Jay Norwood via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 1 16:03:46 PDT 2015


This is another attempt with the metric parallel processing. This 
uses the results only to return an int value, which could be used 
later as an error return value.  The metric value locations are 
now allocated as a part of the input measurement values tuple.

The Tuple vs struct definitions seem to have a big difference in 
default output formatting.


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

// define some input measurement sample tuples and output metric 
tuples
alias TR = Tuple!(long,"raw",double, "per_cycle");
//struct TR {long raw; double per_cycle;}
alias TO = Tuple!(TR, "l1_miss", TR, "l1_access" );
//struct TO {TR l1_miss; TR l1_access; };
alias TI = Tuple!(long, "L1I_MISS",long, "L1D_MISS", long, 
"L1D_READ", long, "L1D_WRITE", long, "cycles", TO, "res");

// various metric definitions
// using Tuples with defined names for each member, and use the 
names here in the metrics.
long met_l1_miss ( ref TI m){  return m.L1I_MISS + m.L1D_MISS; }
long met_l1_access ( ref TI m){  return  m.L1D_READ + 
m.L1D_WRITE; }

int met_all (ref TI m) {

	with (m.res){
	 l1_miss.raw = met_l1_miss(m);
	 l1_access.raw = met_l1_access(m);
	 l1_miss.per_cycle =  (m.cycles == 0)? double.nan : l1_miss.raw 
/ cast(double)m.cycles;
	 l1_access.per_cycle = (m.cycles == 0)? double.nan : 
l1_access.raw / cast(double)m.cycles;
	}
	return 0;
}

// a convenience to use all the metrics above as a list
alias Metrics = AliasSeq!(met_all);

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

	// Initialize some values for the measured samples
	foreach(i, ref m; meas){
		m.L1D_MISS= 100+i; m.L1I_MISS=100-i;
		m.L1D_READ= 200+i; m.L1D_WRITE=200-i;
		m.cycles= 10+i;
	}

     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);


	writeln("measurements:", meas[1]);
	foreach(ref m; meas){
		writeln(m.res);
	}

}




More information about the Digitalmars-d-learn mailing list