-
Notifications
You must be signed in to change notification settings - Fork 0
/
gauss_eliminate.c
184 lines (142 loc) · 5.98 KB
/
gauss_eliminate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* Gaussian elimination code.
* * Author: Forest Oden, 10/26/2015
* *
* * Compile as follows:
* * gcc -o gauss_eliminate gauss_eliminate.c compute_gold.c -fopenmp -std=c99 -O3 -lm
* */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include <math.h>
#include <omp.h>
#include "gauss_eliminate.h"
#define MIN_NUMBER 2
#define MAX_NUMBER 50
extern int compute_gold(float*, const float*, unsigned int);
Matrix allocate_matrix(int num_rows, int num_columns, int init);
void gauss_eliminate_using_openmp(const Matrix, Matrix);
int perform_simple_check(const Matrix);
void print_matrix(const Matrix);
float get_random_number(int, int);
int check_results(float *, float *, unsigned int, float);
int
main(int argc, char** argv) {
if(argc > 1){
printf("Error. This program accepts no arguments. \n");
exit(0);
}
/* Allocate and initialize the matrices. */
Matrix A; /* The N x N input matrix. */
Matrix U; /* The upper triangular matrix to be computed. */
srand(time(NULL));
A = allocate_matrix(MATRIX_SIZE, MATRIX_SIZE, 1); /* Create a random N x N matrix. */
U = allocate_matrix(MATRIX_SIZE, MATRIX_SIZE, 0); /* Create a random N x 1 vector. */
/* Gaussian elimination using the reference code. */
Matrix reference = allocate_matrix(MATRIX_SIZE, MATRIX_SIZE, 0);
struct timeval start, stop;
gettimeofday(&start, NULL);
printf("Performing gaussian elimination using the reference code. \n");
int status = compute_gold(reference.elements, A.elements, A.num_rows);
gettimeofday(&stop, NULL);
printf("CPU run time = %0.2f s. \n", (float)(stop.tv_sec - start.tv_sec + (stop.tv_usec - start.tv_usec)/(float)1000000));
if(status == 0){
printf("Failed to convert given matrix to upper triangular. Try again. Exiting. \n");
exit(0);
}
status = perform_simple_check(reference); // Check that the principal diagonal elements are 1
if(status == 0){
printf("The upper triangular matrix is incorrect. Exiting. \n");
exit(0);
}
printf("Gaussian elimination using the reference code was successful. \n");
/* WRITE THIS CODE: Perform the Gaussian elimination using the multi-threaded OpenMP version.
* * The resulting upper triangular matrix should be returned in U
* * */
gettimeofday(&start, NULL);
printf("Performing gaussian elimination using OpenMP code. \n");
gauss_eliminate_using_openmp(A, U);
gettimeofday(&stop, NULL);
printf("CPU run time = %0.2f s. \n", (float) (stop.tv_sec - start.tv_sec + (stop.tv_usec - start.tv_usec)/(float)1000000));
/* check if the OpenMP result is equivalent to the expected solution. */
int size = MATRIX_SIZE*MATRIX_SIZE;
int res = check_results(reference.elements, U.elements, size, 0.001f);
printf("Test %s\n", (1 == res) ? "PASSED" : "FAILED");
free(A.elements); A.elements = NULL;
free(U.elements); U.elements = NULL;
free(reference.elements); reference.elements = NULL;
return 0;
}
void
gauss_eliminate_using_openmp(const Matrix A, Matrix U) /* Write code to perform gaussian elimination using OpenMP. */
{
/*Find num_elements A.NUM_COLUMNS * A.NUM_ROWS
* // parallel for copy contents A->U
* // Parallel for loop on each row on U Matrix
* //Inner for loop run by each thread to reduce current row
* //Set principal diagonal entry in U to be 1
* //Eliminate */
int num_elements = MATRIX_SIZE;
int thread_count = 2;
int i,j,k;
printf("Beginning Parallel Gaussian Eliminater parallel code with %d elements.\n", num_elements);
#pragma omp parallel num_threads(thread_count) shared(num_elements, U) private (i,j,k)
{
#pragma omp for schedule(dynamic)
for (k = 0; k < num_elements; k++){ /* Perform Gaussian elimination in place on the U matrix. */
for (j = (k + 1); j < num_elements; j++){ /* Reduce the current row. */
/*if (U.elements[num_elements*k + k] == 0){
printf("Numerical instability detected. The principal diagonal element is zero. \n");
return;
}*/
/* Division step. */
U.elements[num_elements * k + j] = (float)(U.elements[num_elements * k + j] / U.elements[num_elements * k + k]);
}
U.elements[num_elements * k + k] = 1; /* Set the principal diagonal entry in U to be 1. */
for (i = (k+1); i < num_elements; i++){
for (j = (k+1); j < num_elements; j++)
/* Elimnation step. */
U.elements[num_elements * i + j] = U.elements[num_elements * i + j] -\
(U.elements[num_elements * i + k] * U.elements[num_elements * k + j]);
U.elements[num_elements * i + k] = 0;
}
}
}
}
int
check_results(float *A, float *B, unsigned int size, float tolerance) /* Check if refernce results match multi threaded results. */
{
for(int i = 0; i < size; i++)
if(fabsf(A[i] - B[i]) > tolerance)
return 0;
return 1;
}
/* Allocate a matrix of dimensions height*width.
* * If init == 0, initialize to all zeroes.
* * If init == 1, perform random initialization.
* * */
Matrix
allocate_matrix(int num_rows, int num_columns, int init){
Matrix M;
M.num_columns = M.pitch = num_columns;
M.num_rows = num_rows;
int size = M.num_rows * M.num_columns;
M.elements = (float*) malloc(size*sizeof(float));
for(unsigned int i = 0; i < size; i++){
if(init == 0) M.elements[i] = 0;
else
M.elements[i] = get_random_number(MIN_NUMBER, MAX_NUMBER);
}
return M;
}
float
get_random_number(int min, int max){ /* Returns a random FP number between min and max values. */
return (float)floor((double)(min + (max - min + 1)*((float)rand()/(float)RAND_MAX)));
}
int
perform_simple_check(const Matrix M){ /* Check for upper triangular matrix, that is, the principal diagonal elements are 1. */
for(unsigned int i = 0; i < M.num_rows; i++)
if((fabs(M.elements[M.num_rows*i + i] - 1.0)) > 0.001) return 0;
return 1;
}