csci5451/assignments/01/lc_openmp.c

74 lines
1.8 KiB
C
Raw Normal View History

2023-10-08 00:49:58 +00:00
#include <math.h>
#include <omp.h>
2023-10-06 06:21:15 +00:00
#include <stdio.h>
2023-10-08 00:49:58 +00:00
#include <stdlib.h>
2023-10-06 06:21:15 +00:00
2023-09-23 05:04:06 +00:00
#include "common.h"
2023-10-06 06:21:15 +00:00
int main(int argc, char **argv) {
char *data_file_name = argv[1], *label_file_name = argv[2];
2023-10-08 00:49:58 +00:00
int outer_iterations = atoi(argv[3]);
int thread_count = atoi(argv[4]);
omp_set_num_threads(8);
2023-10-06 06:21:15 +00:00
struct data *data = read_data(data_file_name);
struct labels *label = read_labels(label_file_name);
2023-10-08 00:49:58 +00:00
printf("Running %d iteration(s) with %d thread(s).\n", outer_iterations,
thread_count);
FLOAT *w = calloc(data->dimensions, sizeof(FLOAT));
for (int iter = 0; iter < outer_iterations; iter++) {
double start_time = monotonic_seconds();
FLOAT *new_w = calloc(data->dimensions, sizeof(FLOAT));
#pragma omp parallel for default(shared)
for (int i = 0; i < data->dimensions; i++) {
FLOAT *ouais = calloc(data->rows, sizeof(FLOAT));
#pragma omp parallel for default(shared)
for (int j = 0; j < data->rows; j++) {
FLOAT x_ni_w_ni = 0;
#pragma omp parallel for default(shared) reduction(+ : x_ni_w_ni)
for (int i2 = 0; i2 < data->dimensions; i2++) {
if (i2 == i)
continue;
x_ni_w_ni = data->buf[data->rows * i2 + j] * w[i2];
}
ouais[j] = label->buf[j] - x_ni_w_ni;
}
FLOAT numer = 0, denom = 0;
#pragma omp parallel for default(shared) reduction(+ : numer, denom)
for (int j = 0; j < data->rows; j++) {
FLOAT xij = data->buf[data->dimensions * i + j];
numer = xij * ouais[j];
denom = xij * xij;
}
free(ouais);
new_w[i] = numer / denom;
}
printf("Done.\n");
for (int idx = 0; idx < data->dimensions; idx++) {
printf("%.3f ", new_w[idx]);
}
printf("\n");
free(w);
w = new_w;
double end_time = monotonic_seconds();
print_time(end_time - start_time);
}
free(w);
2023-10-06 06:21:15 +00:00
return 0;
}