_PARALLEL C EXTENSIONS_ by Barr E. Bauer [LISTING ONE] /* Loop parallelism sum reduction critical block within loop nest * B. E. Bauer 1992 -- compiled using the following on the IRIS: * parallel: cc -O2 -o example1p -mp example1.c * serial: cc -O2 -o example1 example1.c */ #include #define MAX 1536 double a[MAX][MAX], b[MAX][MAX]; main() { int i, j, k, k1; double sum=0.0, temp; /* shared arrays filled; values not important, just order */ for (i=0; i #define MAX 1536 double a[MAX][MAX], b[MAX][MAX]; main() { int i, j, k, k1; double sum=0.0, temp, st; /* st = local code subtotal */ /* shared arrays filled; values not important, just order */ for (i=0; i #define MAXCONSUMERS 3 #define MAXPRODUCTS 10000 #define STOP_TOKEN -1 #define NULL_ITEM (node_t *)NULL /* the product is defined here */ struct node { int productid; struct node *next; }; typedef struct node node_t; /* the typedef is convenient */ node_t *root = NULL_ITEM; /* linked list root */ int max_depth = 0; /* ----- create a new "product" -------------------------------- */ node_t *make_product(node_t *item, int prodid) { item = (node_t *)malloc(sizeof(node_t)); if (item != NULL_ITEM) { item->productid = prodid; item->next = NULL_ITEM; } else printf("problems with production, boss...\n"); return (item); } /* ----- add a "product" to the end of the list ---------------- */ node_t *ship_product(node_t *new) { int cur_depth = 0; node_t *list = root; if (root == NULL_ITEM) root = new; else { while (list->next != NULL_ITEM) { list = list->next; cur_depth++; } cur_depth++; list->next = new; } if (cur_depth > max_depth) max_depth = cur_depth; } /* ----- pop the product off the beginning of the list --------- */ node_t *receive_product() { node_t *item = root; if (root != NULL_ITEM) root = root->next; return(item); } /* ----- consume the product by freeing its memory ------------- */ void consume_product(node_t *item) { if (item != NULL_ITEM) free (item); } /* ----- the producer ------------------------------------------ */ void producer(int cnt) { node_t *temp; int i; /* make "products" cnt times (limits simulation) */ for (i=0; i<=cnt; i++) { if ((temp = make_product(temp,i)) == NULL_ITEM) break; #pragma critical { ship_product(temp); } } /* load on stop tokens */ for (i=0; iproductid != STOP_TOKEN) { if (item->productid) ++consumed; printf("consumer %d consuming product %d\n", myid, item->productid); } else { consuming = 0; printf("consumer %d ran out of products\n", myid); } consume_product(item); } } printf("consumer %d consumed %d products\n", myid, consumed); return(consumed); } main() { long total, sum[MAXCONSUMERS]; int i; printf("business starts with %d products\n",MAXPRODUCTS); #pragma parallel shared(sum) { #pragma independent { producer(MAXPRODUCTS); /* start producer */ } #pragma independent { sum[0] = consumer(1); /* start consumer 1 */ } #pragma independent { sum[1] = consumer(2); /* start consumer 2 */ } #pragma independent { sum[2] = consumer(3); /* start consumer 3 */ } } for (i=0, total = 0L; i