Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created November 26, 2011 19:26
Show Gist options
  • Save Raynos/1396177 to your computer and use it in GitHub Desktop.
Save Raynos/1396177 to your computer and use it in GitHub Desktop.
void * thread_start(void *arg) {
RELAX_DATA* data;
THREAD_ARG* thread_arg;
int column_count;
int dim;
int thread_num;
int threads;
int precision;
clock_t before;
clock_t after;
long loopcounter = 0;
pthread_barrier_t* barrier;
/* unpack arg */
thread_arg = (THREAD_ARG*) arg;
data = thread_arg->data;
/* unpack data */
dim = data->dim;
thread_num = thread_arg->thread_num;
/* printf("started thread %i \n", thread_num); */
threads = data->threads;
precision = data->precision;
barrier = data->barrier;
while (data->finished == 0) {
int barrier_result;
double** temp;
long innerloopcounter;
innerloopcounter=0;
loopcounter++;
before = clock();
/* for each column. Only run columns the thread_num
is assigned to */
for (column_count = thread_num; column_count < dim - 1; column_count+=threads) {
int row_count = 1;
innerloopcounter++;
for (;row_count < dim - 1; row_count++) {
double new_value;
double old_value;
double** source = data->source;
/* calculate and set new value */
old_value = source[row_count][column_count];
new_value = (source[row_count + 1][column_count] +
source[row_count - 1][column_count] +
source[row_count][column_count - 1] +
source[row_count][column_count + 1]) / 4;
data->target[row_count][column_count] = new_value;
/* if the change in value > precision keep looping */
if (fabs(old_value - new_value) > precision) {
data->changed = 1;
}
}
}
after = clock();
/*printf("thread %i computed for %i clocks doing %li loops\n",
thread_num, (int) (after - before), innerloopcounter); */
before = clock();
/* barrier */
barrier_result = pthread_barrier_wait(barrier);
/* for only one thread swap the source & target */
if (barrier_result == PTHREAD_BARRIER_SERIAL_THREAD) {
if (data->changed == 0) {
/* printf("finished %i \n", thread_num); */
/* finished */
data->finished = 1;
}
temp = data->source;
data->source = data->target;
data->target = temp;
data->changed = 0;
}
pthread_barrier_wait(barrier);
after = clock();
/* printf("thread %i wasted %i clocks playing with barriers \n",
thread_num, (int) (after - before)); */
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment