pthread_test_array.c0000644000201400001440000000344211056047477014327 0ustar martinusers/* * pthread_test.c -- a NOT CORRECTLY SYNCHRONIZED program computing the sum over all thread ids. * With transactions it should be correctly synchronized. */ #include #include #include #include #define MAX_THREAD 100 typedef struct { int id; } parm; int gvar[100]; void *hello(void *arg) { parm *p=(parm *)arg; int tmp = p->id; int int_tmp = 5; printf ("Thread reads %d.\n", tmp); /* Here goes the magic: read global value, perform computation on temporary, store value to global variable. Without TM we observe lost updates. */ #pragma tm atomic { int tmp2 = gvar[50]; usleep ((int) (10.0*rand()/(10+1.0))/100); int_tmp = 3; gvar[50] = tmp + tmp2; } return (NULL); } int main(int argc, char* argv[]) { int n,i; pthread_t *threads; pthread_attr_t pthread_custom_attr; parm *p; gvar[50] = 0; if (argc != 2) { printf ("Usage: %s n\n where n is no. of threads\n",argv[0]); return 1; } n=atoi(argv[1]); if ((n < 1) || (n > MAX_THREAD)) { printf ("The no of thread should between 1 and %d.\n",MAX_THREAD); return 1; } threads=(pthread_t *)malloc(n*sizeof(*threads)); pthread_attr_init(&pthread_custom_attr); p=(parm *)malloc(sizeof(parm)*n); /* Start up thread */ for (i=0; i #include #include #include #define MAX_THREAD 100 typedef struct { int id; int *global; } parm; int gvar; /* Function executed by each thread. */ void *hello(void *arg) { parm *p=(parm *)arg; int tmp = p->id; printf ("Thread reads %d.\n", tmp); /* Here goes the magic: read global value, perform computation on temporary, store value to global variable. Without TM we observe lost updates. */ #pragma tm atomic { int tmp2 = *(p->global); usleep ((int) (10.0*rand()/(10+1.0))/100); *(p->global) = tmp + tmp2; } return (NULL); } int main(int argc, char* argv[]) { int n,i; pthread_t *threads; pthread_attr_t pthread_custom_attr; parm *p; gvar = 0; if (argc != 2) { printf ("Usage: %s n\n where n is number of threads\n",argv[0]); return 1; } n=atoi(argv[1]); if ((n < 1) || (n > MAX_THREAD)) { printf ("Thread number bound by 1 and %d.\n",MAX_THREAD); return 1; } threads=(pthread_t *)malloc(n*sizeof(*threads)); pthread_attr_init(&pthread_custom_attr); p=(parm *)malloc(sizeof(parm)*n); /* Start up thread */ for (i=0; i #include #include #include #define MAX_THREAD 100 typedef struct { int id; int *global; } parm; void *hello(void *arg) { parm *p=(parm *)arg; int tmp = p->id; printf ("Thread reads %d.\n", tmp); /* Here goes the magic: read global value, perform computation on temporary, store value to global variable. Without TM we observe lost updates. */ #pragma tm atomic { int tmp2 = *(p->global); usleep ((int) (10.0*rand()/(10+1.0))/100); *(p->global) = tmp + tmp2; } return (NULL); } int main(int argc, char* argv[]) { int gvar; int n,i; pthread_t *threads; pthread_attr_t pthread_custom_attr; parm *p; gvar = 0; if (argc != 2) { printf ("Usage: %s n\n where n is no. of threads\n",argv[0]); return 1; } n=atoi(argv[1]); if ((n < 1) || (n > MAX_THREAD)) { printf ("The no of thread should between 1 and %d.\n",MAX_THREAD); return 1; } threads=(pthread_t *)malloc(n*sizeof(*threads)); pthread_attr_init(&pthread_custom_attr); p=(parm *)malloc(sizeof(parm)*n); /* Start up thread */ for (i=0; i #include #include #include #define MAX_THREAD 100 typedef struct { int id; } parm; int gvar; void *hello(void *arg) { parm *p=(parm *)arg; int tmp = p->id; printf ("Thread reads %d.\n", tmp); /* Here goes the magic: read global value, perform computation on temporary, store value to global variable. Without TM we observe lost updates. */ #pragma tm atomic { int tmp2 = gvar; usleep ((int) (10.0*rand()/(10+1.0))/100); gvar = tmp + tmp2; } return (NULL); } int main(int argc, char* argv[]) { int n,i; pthread_t *threads; pthread_attr_t pthread_custom_attr; parm *p; gvar = 0; if (argc != 2) { printf ("Usage: %s n\n where n is no. of threads\n",argv[0]); return 1; } n=atoi(argv[1]); if ((n < 1) || (n > MAX_THREAD)) { printf ("The no of thread should between 1 and %d.\n",MAX_THREAD); return 1; } threads=(pthread_t *)malloc(n*sizeof(*threads)); pthread_attr_init(&pthread_custom_attr); p=(parm *)malloc(sizeof(parm)*n); /* Start up thread */ for (i=0; i #include #include #include int a[100]; int main() { a[50] = 0; int num_threads = omp_get_max_threads (); int check = num_threads * (num_threads - 1) / 2; #pragma omp parallel shared (a) { int tmp = omp_get_thread_num () ; printf ("thread %d reads its number\n", tmp); #pragma tm atomic { int tmp2 = a[50]; usleep ((int) (10.0*rand()/(10+1.0))/100); a[50] = tmp + tmp2; } } printf ("%d\n", a[50]); if (a[50] == check) printf ("check passed\n"); else printf ("check failed (%d)\n", check); return 0; } openmp_test_globalvariable.c0000644000201400001440000000132411056047343016013 0ustar martinusers/* This is a NOT correctly synchronised version of an OpenMP example. */ /* With transactions it should work fine. */ #include #include #include #include int a; int main() { a = 0; int num_threads = omp_get_max_threads (); int check = num_threads * (num_threads - 1) / 2; #pragma omp parallel shared (a) { int tmp = omp_get_thread_num () ; printf ("thread %d reads its number\n", tmp); #pragma tm atomic { int tmp2 = a; usleep ((int) (10.0*rand()/(10+1.0))/100); a = tmp + tmp2; } } printf ("%d\n", a); if (a == check) printf ("check passed\n"); else printf ("check failed (%d)\n", check); return 0; } openmp_test_stackarray.c0000644000201400001440000000136011056047343015211 0ustar martinusers/* This is a NOT correctly synchronised version of an OpenMP example. */ /* With transactions it should work fine. */ #include #include #include #include int main() { int a[100]; a[50] = 0; int num_threads = omp_get_max_threads (); int check = num_threads * (num_threads - 1) / 2; #pragma omp parallel shared (a) { int tmp = omp_get_thread_num () ; printf ("thread %d reads its number\n", tmp); #pragma tm atomic { int tmp2 = a[50]; usleep ((int) (10.0*rand()/(10+1.0))/100); a[50] = tmp + tmp2; } } printf ("%d\n", a[50]); if (a[50] == check) printf ("check passed\n"); else printf ("check failed (%d)\n", check); return 0; } openmp_test_stackvariable.c0000644000201400001440000000132711056047343015663 0ustar martinusers/* This is a NOT correctly synchronised version of an OpenMP example. */ /* With transactions it should work fine. */ #include #include #include #include int main() { int a; a = 0; int num_threads = omp_get_max_threads (); int check = num_threads * (num_threads - 1) / 2; #pragma omp parallel shared (a) { int tmp = omp_get_thread_num () ; printf ("thread %d reads its number\n", tmp); #pragma tm atomic { int tmp2 = a; usleep ((int) (10.0*rand()/(10+1.0))/100); a = tmp + tmp2; } } printf ("%d\n", a); if (a == check) printf ("check passed\n"); else printf ("check failed (%d)\n", check); return 0; }