an example of parallel calculation of metrics

Jay Norwood via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 1 12:15:10 PDT 2015


On Thursday, 1 October 2015 at 18:08:31 UTC, Ali Çehreli wrote:
> However, if you prove to yourself that the result tuple and 
> your struct have the same memory layout, you can cast the tuple 
> slice to struct slice after calling amap:

After re-reading your explanation, I see that the problem is only 
that the results needs to be a Tuple.  It works with named tuple 
members in this example as the result and array of struct as the 
input.  I'll re-check if the multi-member result also works with 
named members.  I'll update the issue report.

import std.meta;
import std.stdio;

// define some input measurement sample tuples and output metric 
tuples

struct TI {long L1I_MISS; long L1D_MISS; }
alias TO = Tuple!(long, "raw");

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

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

void main(string[] argv)
{
	auto samples = iota(100);
	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.L1D_MISS= 100+i; m.L1I_MISS=100-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);

	TO rv1 = met_l1_miss( meas[1]);

	writeln("measurements:", meas[1]);
	writeln("rv1:", rv1);
	writeln("results:", results[1]);

}



More information about the Digitalmars-d-learn mailing list