d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Successive Over-relaxation Program > Getting Infinite Loop
Add Reply New Topic New Poll
Member
Posts: 18,969
Joined: Aug 16 2007
Gold: 16,089.87
Nov 9 2013 08:56pm
I'm having trouble with my program and not sure what I'm doing to cause an infinite loop.
2D SOR

Supposed 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
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 9 2013 09:48pm
Have you ran this thru a debugger yet? Are your doubles (maxDelta and Threshhold) working correctly as your loop counters?

Member
Posts: 18,969
Joined: Aug 16 2007
Gold: 16,089.87
Nov 9 2013 09:51pm
Quote (Eep @ Nov 9 2013 10:48pm)
Have you ran this thru a debugger yet? Are your doubles (maxDelta and Threshhold) working correctly as your loop counters?


I haven't, I'm assuming that part of code that I filled in has a large error of some sort. It doesn't give me any errors but I don't really know how to debug it.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 9 2013 10:00pm
Quote (Trev @ Nov 9 2013 11:51pm)
I haven't, I'm assuming that part of code that I filled in has a large error of some sort. It doesn't give me any errors but I don't really know how to debug it.


i recommend logging. i imagine there's a logging framework that tells you which thread it's running on as well. log4j does it for java, dunno about any specific C frameworks.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll