Permalink
Browse files

Add lec 2 demos.

  • Loading branch information...
0 parents commit b34beb747773c6ea95e8863652854f0498959c78 @inducer inducer committed Sep 14, 2012
Showing with 508 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +33 −0 helloWorld.c
  3. +13 −0 helloWorld2.c
  4. +26 −0 helloWorld3.c
  5. +22 −0 helloWorld4.c
  6. +24 −0 helloWorld5.c
  7. +52 −0 race2.c
  8. +53 −0 race3.c
  9. +54 −0 race4.c
  10. +31 −0 raceCondition.c
  11. +19 −0 red.c
  12. +20 −0 testFor.c
  13. +51 −0 testFor2.c
  14. +59 −0 testFor3.c
  15. +20 −0 testFor_a.c
  16. +25 −0 testLock.c
6 .gitignore
@@ -0,0 +1,6 @@
+a.out
+.*.swp
+*~
+*-demo.c
+*-demo.cl
+*.o
33 helloWorld.c
@@ -0,0 +1,33 @@
+
+
+ #include <omp.h>
+ #include <stdio.h>
+ #include <stdlib.h>
+
+int main(int argc, char *argv[]) {
+
+ int nThreads;
+
+ if (2 != argc){
+ printf("Must enter integer for number threads on command line\n");
+ exit(-1);
+ }
+ else{
+ nThreads = atoi(argv[1]);
+ printf("Using %d threads\n",nThreads);
+ }
+
+#ifdef _OPENMP
+ int my_threadNum = omp_get_thread_num();
+ int numThreads = omp_get_num_threads();
+#else
+ int my_threadNum = 0;
+ int numThreads = 1;
+#endif
+
+
+#pragma omp parallel num_threads(nThreads)
+ printf("Hello world from thread %d\n",my_threadNum);
+
+ return 0;
+ }
13 helloWorld2.c
@@ -0,0 +1,13 @@
+
+
+ #include <omp.h>
+ #include <stdio.h>
+
+ int main() {
+
+ #pragma omp parallel
+ printf("Hello world from thread %d of %d\n",
+ omp_get_thread_num(),omp_get_num_threads());
+
+ return 0;
+ }
26 helloWorld3.c
@@ -0,0 +1,26 @@
+
+
+ #include <omp.h>
+ #include <stdio.h>
+ #include <stdlib.h>
+
+int main(int argc, char *argv[]) {
+
+ int nThreads;
+
+ if (argc != 2){
+ printf("Must enter integer for number threads on command line\n");
+ fflush(stdout);
+ exit(-1);
+ }
+ else {
+ nThreads = atoi(argv[1]);
+ printf("Using %d threads\n",nThreads);
+ }
+
+#pragma omp parallel num_threads(nThreads)
+ printf("Hello world from thread %d of %d\n",
+ omp_get_thread_num(),omp_get_num_threads());
+
+ return 0;
+}
22 helloWorld4.c
@@ -0,0 +1,22 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <omp.h>
+
+int main(int argc, char *argv[]) {
+
+ //#ifdef _OPENMP
+#if 1
+ int my_threadNum = omp_get_thread_num();
+ int numThreads = omp_get_num_threads();
+#else
+ int my_threadNum = 0;
+ int numThreads = 1;
+#endif
+
+
+#pragma omp parallel
+ printf("Hello world from thread %d of %d\n",my_threadNum, numThreads);
+
+ return 0;
+}
24 helloWorld5.c
@@ -0,0 +1,24 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <omp.h>
+
+int main(int argc, char *argv[]) {
+
+
+
+#pragma omp parallel
+ {
+#ifdef _OPENMP
+ int my_threadNum = omp_get_thread_num();
+ int numThreads = omp_get_num_threads();
+#else
+ int my_threadNum = 0;
+ int numThreads = 1;
+#endif
+
+ printf("Hello world from thread %d of %d\n",my_threadNum, numThreads);
+ }
+
+ return 0;
+}
52 race2.c
@@ -0,0 +1,52 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "timing.h"
+#include <omp.h>
+#include <math.h>
+
+ int main(int argc, char *argv[]) {
+
+ double sum = 0;
+ int i,j;
+ size_t n;
+ timestamp_type time1, time2;
+
+ if (argc != 2){
+ printf("Must enter integer for array size on command line\n");
+ exit(-1);
+ }
+ else {
+ n = atoi(argv[1]);
+ printf("Using array size %ld \n",n);
+ }
+
+ double *x = (double *) malloc(sizeof(double) * n);
+ if (!x) { perror("alloc x"); abort(); }
+ double *y = (double *) malloc(sizeof(double) * n);
+ if (!y) { perror("alloc y"); abort(); }
+
+ for (i=0;i<n;i++){
+ x[i] = i;
+ y[i] = 2*i;
+ }
+
+
+ get_timestamp(&time1);
+
+#pragma omp parallel for
+ for(i=0;i<n;i++){
+ sum += x[i] * y[i];
+ }
+
+ get_timestamp(&time2);
+ double elapsed = timestamp_diff_in_seconds(time1,time2);
+ printf("%f s\n", elapsed);
+
+ printf("sum = %15.7e\n",sum);
+
+ return 0;
+
+ }
+
+
53 race3.c
@@ -0,0 +1,53 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "timing.h"
+#include <omp.h>
+#include <math.h>
+
+ int main(int argc, char *argv[]) {
+
+ double sum = 0;
+ int i,j;
+ size_t n;
+ timestamp_type time1, time2;
+
+ if (argc != 2){
+ printf("Must enter integer for array size on command line\n");
+ exit(-1);
+ }
+ else {
+ n = atoi(argv[1]);
+ printf("Using array size %ld and %d threads\n",n,omp_get_num_threads());
+ }
+
+ double *x = (double *) malloc(sizeof(double) * n);
+ if (!x) { perror("alloc x"); abort(); }
+ double *y = (double *) malloc(sizeof(double) * n);
+ if (!y) { perror("alloc y"); abort(); }
+
+ for (i=0;i<n;i++){
+ x[i] = i;
+ y[i] = 2*i;
+ }
+
+
+ get_timestamp(&time1);
+
+#pragma omp parallel for default(none) shared(x,y,n) \
+ reduction(+:sum)
+ for(i=0;i<n;i++){
+ sum += x[i] * y[i];
+ }
+
+ get_timestamp(&time2);
+ double elapsed = timestamp_diff_in_seconds(time1,time2);
+ printf("%f s\n", elapsed);
+
+ printf("sum = %15.7e\n",sum);
+
+ return 0;
+
+ }
+
+
54 race4.c
@@ -0,0 +1,54 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "timing.h"
+#include <omp.h>
+#include <math.h>
+
+ int main(int argc, char *argv[]) {
+
+ double sum = 0;
+ int i,j;
+ size_t n;
+ timestamp_type time1, time2;
+
+ if (argc != 2){
+ printf("Must enter integer for array size on command line\n");
+ exit(-1);
+ }
+ else {
+ n = atoi(argv[1]);
+ printf("Using array size %ld and %d threads\n",n,omp_get_num_threads());
+ }
+
+ double *x = (double *) malloc(sizeof(double) * n);
+ if (!x) { perror("alloc x"); abort(); }
+ double *y = (double *) malloc(sizeof(double) * n);
+ if (!y) { perror("alloc y"); abort(); }
+
+ for (i=0;i<n;i++){
+ x[i] = i;
+ y[i] = 2*i;
+ }
+
+
+ get_timestamp(&time1);
+
+#pragma omp parallel for default(none) shared(x,y,n,sum)
+ for(i=0;i<n;i++){
+ double a = x[i] * y[i];
+#pragma omp atomic
+ sum += a;
+ }
+
+ get_timestamp(&time2);
+ double elapsed = timestamp_diff_in_seconds(time1,time2);
+ printf("%f s\n", elapsed);
+
+ printf("sum = %15.7e\n",sum);
+
+ return 0;
+
+ }
+
+
31 raceCondition.c
@@ -0,0 +1,31 @@
+
+ #include <stdio.h>
+ #include <omp.h>
+
+ int main(){
+
+ int x = 2;
+
+#pragma omp parallel shared(x) num_threads(2)
+ {
+ if (1 == omp_get_thread_num()){
+ x = 5;
+ }
+ else {
+ printf("1: Thread %d has x = %d\n",omp_get_thread_num(),x);
+ }
+
+ #pragma omp barrier
+
+ if (0 == omp_get_thread_num()) {
+ printf("2: thread %d has x = %d\n",omp_get_thread_num(),x);
+ }
+ else {
+ printf("3: thread %d has x = %x\n",omp_get_thread_num(),x);
+ }
+
+ }
+ return 0;
+ }
+
+
19 red.c
@@ -0,0 +1,19 @@
+#include <stdio.h>
+
+
+int main(){
+
+int sum = 0;
+int a[100],i;
+
+#pragma omp parallel for shared(sum)
+for (i=0;i<100;i++) a[i]=i;
+
+#pragma omp parallel for \
+ reduction(+:sum)
+ for(i=0;i<100;i++)
+ sum += a[i];
+
+printf("sum of first 100 numbers %d\n",sum);
+
+}
20 testFor.c
@@ -0,0 +1,20 @@
+
+ #include <stdio.h>
+ #include <omp.h>
+
+ #define N 20
+
+ int main(){
+
+ int sum = 0;
+ int a[N],i;
+
+#pragma omp parallel for schedule(dynamic,2)
+ for(i=0;i<N;i++){
+ a[i] = i;
+ printf(" iterate i=%3d by thread %3d\n",i,omp_get_thread_num());
+ }
+
+ return 0;
+
+ }
51 testFor2.c
@@ -0,0 +1,51 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "timing.h"
+#include <omp.h>
+#include <math.h>
+
+ int main(int argc, char *argv[]) {
+
+ int sum = 0;
+ int i,j;
+ size_t n;
+ timestamp_type time1, time2;
+
+ if (argc != 2){
+ printf("Must enter integer for array size on command line\n");
+ exit(-1);
+ }
+ else {
+ n = atoi(argv[1]);
+ printf("Using array size %ld \n",n);
+ }
+
+ double *x = (double *) malloc(sizeof(double) * n);
+ if (!x) { perror("alloc x"); abort(); }
+ double *y = (double *) malloc(sizeof(double) * n);
+ if (!y) { perror("alloc y"); abort(); }
+ double *z = (double *) malloc(sizeof(double) * n);
+ if (!z) { perror("alloc z"); abort(); }
+
+
+ for (i=0;i<n;i++){
+ x[i] = i;
+ y[i] = 2*i;
+ }
+
+
+ get_timestamp(&time1);
+
+#pragma omp parallel for
+ for(i=0;i<n;i++){
+ z[i] = x[i] + y[i];
+ }
+
+ get_timestamp(&time2);
+ double elapsed = timestamp_diff_in_seconds(time1,time2);
+ printf("%f s\n", elapsed);
+
+ return 0;
+
+ }
59 testFor3.c
@@ -0,0 +1,59 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "timing.h"
+#include <omp.h>
+#include <math.h>
+
+ int main(int argc, char *argv[]) {
+
+ int sum = 0;
+ int i,j;
+ size_t n;
+ timestamp_type time1, time2;
+
+ if (argc != 2){
+ printf("Must enter integer for array size on command line\n");
+ exit(-1);
+ }
+ else {
+ n = atoi(argv[1]);
+ printf("Using array size %ld \n",n);
+ }
+
+ double *x = (double *) malloc(sizeof(double) * n);
+ if (!x) { perror("alloc x"); abort(); }
+ double *y = (double *) malloc(sizeof(double) * n);
+ if (!y) { perror("alloc y"); abort(); }
+ double *z = (double *) malloc(sizeof(double) * n);
+ if (!z) { perror("alloc z"); abort(); }
+
+
+ for (i=0;i<n;i++){
+ x[i] = i;
+ y[i] = 2*i;
+ }
+
+
+ get_timestamp(&time1);
+
+#pragma omp parallel for default(none) private(j) shared(x,y,z,n)
+ for(i=0;i<n;i++){
+ double a = x[i];
+ double b = y[i];
+
+ for ( j = 0; j < 20; ++j){
+ a = sin(a) + b;
+ b = cos(b) + a;
+ }
+
+ z[i] = a + b;
+ }
+
+ get_timestamp(&time2);
+ double elapsed = timestamp_diff_in_seconds(time1,time2);
+ printf("%f s\n", elapsed);
+
+ return 0;
+
+ }
20 testFor_a.c
@@ -0,0 +1,20 @@
+
+ #include <stdio.h>
+ #include <omp.h>
+
+ #define N 20
+
+ int main(){
+
+ int sum = 0;
+ int a[N],i;
+
+#pragma omp parallel for schedule (dynamic)
+ for(i=0;i<N;i++){
+ a[i] = i;
+ printf(" iterate i=%3d by thread %3d\n",i,omp_get_thread_num());
+ }
+
+ return 0;
+
+ }
25 testLock.c
@@ -0,0 +1,25 @@
+
+#include <stdio.h>
+#include <omp.h>
+
+omp_lock_t my_lock;
+
+int main() {
+ omp_init_lock(&my_lock);
+
+ #pragma omp parallel
+ {
+ int tid = omp_get_thread_num( );
+ int i, j;
+
+ for (i = 0; i < 5; ++i) {
+ omp_set_lock(&my_lock);
+ printf("Thread %d - starting locked region\n", tid);
+
+ printf("Thread %d - ending locked region\n", tid);
+ omp_unset_lock(&my_lock);
+ }
+ }
+
+ omp_destroy_lock(&my_lock);
+}

0 comments on commit b34beb7

Please sign in to comment.