an example of parallel calculation of metrics

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


This compiles and appears to execute correctly, but if I 
uncomment the taskPool line I get a compile error message about 
wrong buffer type.  Am I breaking some rule for 
std.parallelism.amap?

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 TR = Tuple!(double,"per_sec", double, "per_cycle", 
long,"raw");
alias TI = Tuple!(long, "proc_cyc", long, "DATA_RD", long, 
"DATA_WR", long, "INST_FETCH", long, "L1I_MISS", long, "L1I_HIT", 
long,"L1D_HIT", long, "L1D_MISS");
alias TO = Tuple!(TR,"L1_MISS", TR, "L1_HIT", TR,"DATA_ACC", 
TR,"ALL_ACC");
const double CYC_PER_SEC = 1_600_000_000;

// various metric definitions
// using Tuples with defined names for each member, and use the 
names here in the metrics.
TR met_l1_miss ( ref TI m){ TR rv; with(rv) with(m) { raw = 
L1I_MISS+L1D_MISS; per_cycle = cast(double)raw/proc_cyc; per_sec 
= per_cycle*CYC_PER_SEC;} return rv; }
TR met_l1_hit ( ref TI m){ TR rv; with(rv) with(m) { raw = 
L1I_HIT+L1D_HIT; per_cycle = cast(double)raw/proc_cyc; per_sec = 
per_cycle*CYC_PER_SEC;} return rv; }
TR met_data_acc ( ref TI m){ TR rv; with(rv) with(m) { raw = 
DATA_RD+DATA_WR; per_cycle = cast(double)raw/proc_cyc; per_sec = 
per_cycle*CYC_PER_SEC;} return rv; }
TR met_all_acc( ref TI m){ TR rv; with(rv) with(m) { raw = 
DATA_RD+DATA_WR+INST_FETCH; per_cycle = cast(double)raw/proc_cyc; 
per_sec = per_cycle*CYC_PER_SEC;} return rv; }

// a convenience to use all the metrics above as a list
alias Metrics = 
AliasSeq!(met_l1_miss,met_l1_hit,met_data_acc,met_all_acc);

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

	// Initialize some values for the measured samples
	foreach(i, ref m; meas){
		with(m){ proc_cyc = 1_000_000+i*2; DATA_RD = 1000+i; DATA_WR= 
2000+i; INST_FETCH=proc_cyc/2;
		        L1I_HIT= INST_FETCH-100; L1I_MISS=100;
				L1D_HIT= DATA_RD+DATA_WR - 200; L1D_MISS=200;}
	}

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

	TR rv1 = met_l1_miss( meas[0]);
	TR rv2 = met_l1_hit( meas[0]);
	TR rv3 = met_data_acc( meas[0]);
	TR rv4 = met_all_acc( meas[0]);

	// how long did this take
	long exec_ms = sw.peek().msecs;
	writeln("measurements:", meas[0]);
	writeln("rv1:", rv1);
	writeln("rv2:", rv2);
	writeln("rv3:", rv3);
	writeln("rv4:", rv4);
	writeln("results:", results[1]);
	writeln("time:", exec_ms);

}



More information about the Digitalmars-d-learn mailing list