Concurrently sum the rows of a matrix into subtotals and then sequential add the subtotals.
#include <fstream.hfa> #include <thread.hfa>thread Adder { int * row, cols, & subtotal; // communication }; void ?{}( Adder & adder, int row[], int cols, int & subtotal ) { adder.[row, cols] = [row, cols]; // expression disallowed in multi-member access &adder.subtotal = &subtotal; }void main( Adder & adder ) with( adder ) { // thread starts here subtotal = 0; for ( c; cols ) { subtotal += row[c]; } } int main() { const int rows = 10, cols = 1000; int matrix[rows][cols], subtotals[rows], total = 0;processor p; // add kernel thread for ( r; rows ) { // initialize for ( c; cols ) { matrix[r][c] = 1; } } Adder * adders[rows]; for ( r; rows ) { // start threads to sum rows adders[r] = &(*malloc()){ matrix[r], cols, subtotals[r] }; // adders[r] = new( matrix[r], cols, subtotals[r] ); } for ( r; rows ) { // wait for threads to finish delete( adders[r] ); total += subtotals[r]; // total subtotals } sout | total; }
In-place concurrent quick-sort: threads are created to partition to a specific depth, then sequential recursive-calls are use to sort each partition.
thread Quicksort { int * values; // communication variables int low, high, depth; }; void ?{}( Quicksort & qs, int values[], int size, int depth ) { qs.values = values; qs.low = 0; qs.high = size; qs.depth = depth; }void main( Quicksort & qs ) { // thread starts here // nested routines: information hiding void ?{}( Quicksort & qs, int values[], int low, int high, int depth ) { qs.values = values; qs.low = low; qs.high = high; qs.depth = depth; } void sort( int values[], int low, int high, int depth ) { int left, right; // index to left/right-hand side of the values int pivot; // pivot value of values int swap; // temporary // partition while 2 or more elements in the array if ( low < high ) { pivot = values[low + ( high - low ) / 2]; left = low; right = high; // partition: move values less < pivot before the pivot and values > pivot after the pivot do { while ( values[left] < pivot ) left += 1; // changed values[left] < pivot while ( pivot < values[right] ) right -= 1; if ( left <= right ) { swap = values[left]; // interchange values values[left] = values[right]; values[right] = swap; left += 1; right -= 1; } } while ( left <= right ); // restrict number of tasks to slightly greater than number of processors if ( depth > 0 ) { depth -= 1; Quicksort rqs = { values, low, right, depth }; // concurrently sort upper half sort( values, left, high, depth ); // concurrently sort lower half } else { sort( values, low, right, 0 ); // sequentially sort lower half sort( values, left, high, 0 ); // sequentially sort upper half } } } with( qs ) { sort( values, low, high, depth ); } } int main() { // read size, depth, unsorted valuesprocessor processors[ (1 << depth) - 1 ]; // create 2^depth-1 kernel threadsQuicksort QS = { values, size - 1, depth }; // sort values }
Comparison of C∀
C∀ | Go |
---|---|
#include <fstream.hfa> #include <thread.hfa> struct Msg { int i, j; }; thread Gortn { int i; float f; Msg m; }; void mem1( Gortn & mutex gortn, int i ) { gortn.i = i; } void mem2( Gortn & mutex gortn, float f ) { gortn.f = f; } void mem3( Gortn & mutex gortn, Msg m ) { gortn.m = m; } void ^?{}( Gortn & mutex ) {} void main( Gortn & gortn ) with( gortn ) { // thread starts for () { |
package main import "fmt" func main() { type Msg struct{ i, j int } ch1 := make( chan int ) ch2 := make( chan float32 ) ch3 := make( chan Msg ) hand := make( chan string ) shake := make( chan string ) gortn := func() { // thread starts var i int; var f float32; var m Msg L: for { select { // wait for message case |