#include "hello.h" #include "tbb/tick_count.h" int compute::execute(const int& tag, hello_context& ctx) const { // This code turns a hostname like "n5" into a number like "5" // Serializing entire string in a more proper way takes some extra work char hostname[256]; gethostname(hostname, 256); ctx.hostid.put(tag, (unsigned)atoi(&hostname[1])); ctx.threadid.put(tag, (unsigned)pthread_self()); return CnC::CNC_Success; } int main(int argc, char *argv[]) { int N, j, nthreads; if (argc < 2) { printf("Usage %s [numThreads]\n", argv[0]); return(1); } N = atoi(argv[1]); char hostname[256]; gethostname(hostname, 256); printf("CnC process started on host %s with N = %d\n", hostname, N); if (argc > 2) { nthreads = atoi(argv[2]); printf("Setting number of threads per host to %d\n", nthreads); CnC::debug::set_num_threads(nthreads); } hello_context ctx; tbb::tick_count t0 = tbb::tick_count::now(); for (j = 0; j < N; j++) { //execute is run in parallel ASAP after this line ctx.tagvalue.put(j); } ctx.wait(); tbb::tick_count t1 = tbb::tick_count::now(); printf("Size of ctx.tagvalue tag collection is %d\n", ctx.tagvalue.size()); int count = 0; unsigned threadid; unsigned hostid; printf("%3s %8s %10s %3s\n", "Tag", "j", "threadid", "hostid"); CnC::tag_collection::const_iterator itr; for (itr = ctx.tagvalue.begin(); itr != ctx.tagvalue.end(); itr++) { ctx.threadid.get(*itr, threadid); ctx.hostid.get(*itr, hostid); count++; printf("%03d %8d %10u %03u\n", count, *itr, threadid, hostid); } printf("total elapsed sec = %0.2f\n", (t1-t0).seconds()); return 0; }