I'm having trouble with my program and not sure what I'm doing to cause an infinite loop.
2D SORSupposed to
decompose the work among threads -- keep in mind load balancing, cache performance
decide how to synchronize the threads so all threads finish an iteration before any start the next iteration
decide how to synchronize the threads so all threads stop if the max. change at any spot during an iteration is less than the threshold
Code
#include <math.h>
#include <stdio.h>
#include <limits.h>
#include <time.h>
#include <pthread.h>
#include <stdlib.h>
#define MAXTHREADS 32 /* max. # threads */
/* Prototypes that mostly use global variables */
void * thread_main(void *);
void InitializeData();
void barrier();
pthread_mutex_t update_lock;
pthread_mutex_t barrier_lock; /* mutex for the barrier */
pthread_cond_t all_here; /* condition variable for barrier */
int count=0; /* counter for barrier */
/* Global Variables */
int n, t;
double threshold;
double **val, **new; /* pointers to the 2D array of values */
/* val contains the current values with new used
to calculate the next iterations values */
double delta = 0.0;
double deltaNew = 0.0;
/* Command line args: matrix size, number of threads, threshold */
int main(int argc, char * argv[])
{
/* thread ids and attributes */
pthread_t tid[MAXTHREADS];
pthread_attr_t attr;
long i, j;
long startTime, endTime,seqTime,parTime;
float myThreshold;
/* set global thread attributes */
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
/* initial mutex and condition variable */
pthread_mutex_init(&update_lock, NULL);
pthread_mutex_init(&barrier_lock, NULL);
pthread_cond_init(&all_here, NULL);
/* read command line arguments */
if (argc != 4) {
printf("usage: %s <matrix size> <number of threads> <threshold>\n",
argv[0]);
exit(1);
} // end if
sscanf(argv[1], "%d", &n);
sscanf(argv[2], "%d", &t);
sscanf(argv[3], "%f", &myThreshold);
threshold = (double) myThreshold;
/* Initial val and new 2D arrays with 0s and 1s. */
InitializeData();
printf("InitializeData done\n");
for(i=0; i<t; i++) {
pthread_create(&tid[i], &attr, thread_main, (void *) i);
} // end for
for (i=0; i < t; i++) {
pthread_join(tid[i], NULL);
} // end for
printf("maximum difference: %e\n", delta);
} // end main
============================================== Code Here Below (Thread_Main)================================
[COLOR=red]void* thread_main(void * arg) {
long id=(long) arg;
double average, maxDelta;
double thisDelta;
double **temp;
int i, j;
/* YOUR CODE GOES HERE
Infinite Loop Below, Not working correctly */
do {
maxDelta = 0.0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
average = (val[i-1][j] + val[i][j+1] +
val[i+1][j] + val[i][j-1])/4;
thisDelta = fabs(average - val[i][j]);
if (maxDelta < thisDelta) {
maxDelta = thisDelta;
} // end if
// store into new array
new[i][j] = average;
} // end for j
} // end for i
temp = new; /* prepare for next iteration */
new = val;
val = temp;
printf("maxDelta = %8.6f\n", maxDelta);
} while (maxDelta > threshold); // end do-while
delta = maxDelta;
} // end thread_main[/COLOR]
void InitializeData() {
int i, j;
new = (double **) malloc((n+2)*sizeof(double *));
val = (double **) malloc((n+2)*sizeof(double *));
for (i = 0; i < n+2; i++) {
new[i] = (double *) malloc((n+2)*sizeof(double));
val[i] = (double *) malloc((n+2)*sizeof(double));
} // end for i
/* initialize to 0.0 except to 1.0 along the left boundary */
for (i = 0; i < n+2; i++) {
val[i][0] = 1.0;
new[i][0] = 1.0;
} // end for i
for (i = 0; i < n+2; i++) {
for (j = 1; j < n+2; j++) {
val[i][j] = 0.0;
new[i][j] = 0.0;
} // end for j
} // end for i
} // end InitializeData
void barrier(long id) {
pthread_mutex_lock(&barrier_lock);
count++;
// printf("count %d, id %d\n", count, id);
if (count == t) {
count = 0;
pthread_cond_broadcast(&all_here);
} else {
while(pthread_cond_wait(&all_here, &barrier_lock) != 0);
} // end if
pthread_mutex_unlock(&barrier_lock);
} // end barrier
This post was edited by Trev on Nov 9 2013 08:57pm