config const capacity = 4; config const P = 2; config const C = 2; config const N = 1000; const TERM = -1.0; class BoundedBuffer { /* FILL IN FIELDS */ proc BoundedBuffer() { /* IMPLEMENT */ } proc produce( item: real ) : void { /* IMPLEMENT */ } proc consume( ) : real { /* IMPLEMENT */ } } /* Test the buffer */ // consumer task procedure proc consumer(b: BoundedBuffer, id: int) { // keep consuming until it gets a TERM element var count = 0; do { writeln(id, " consuming "); const data = b.consume(); writeln(id, " consumed ", data); count += 1; } while (data!=TERM); return count-1; } // producer task procedure proc producer(b: BoundedBuffer, id: int) { // produce my strided share of N elements writeln("producer ", id, " in charge of ", 0..#N align id); var count = 0; for i in 0..#N by P align id { writeln(id, " producing ", i); b.produce(i); count += 1; } return count; } // produce an element that indicates that the consumer should exit proc signalExit(b: BoundedBuffer) { b.produce(TERM); } var P_count: [0..#P] int; var C_count: [0..#C] int; var theBuffer = new BoundedBuffer(); cobegin { { // spawn P producers coforall prodID in 0..#P do P_count[prodID] = producer(theBuffer, prodID); // when producers are all done, produce C exit signals for consID in 0..#C do signalExit(theBuffer); } // spawn C consumers coforall consID in 0..#C do C_count[consID] = consumer(theBuffer, consID); } writeln("== TOTALS =="); writeln("P_count=", P_count); writeln("C_count=", C_count);