csci5451/assignments/01/lc_openmp.c

79 lines
No EOL
2 KiB
C

#include <math.h>
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
int main(int argc, char **argv) {
char *data_file_name = argv[1], *label_file_name = argv[2];
int outer_iterations = atoi(argv[3]);
int thread_count = atoi(argv[4]);
omp_set_num_threads(thread_count);
struct data *data = read_data(data_file_name);
struct labels *label = read_labels(label_file_name);
printf("Running %d iteration(s) with %d thread(s).\n", outer_iterations,
thread_count);
FLOAT *w = calloc(data->dimensions, sizeof(FLOAT));
FLOAT *new_w = calloc(data->dimensions, sizeof(FLOAT));
FLOAT *ouais = calloc(data->dimensions * data->rows, sizeof(FLOAT));
for (int iter = 0; iter < outer_iterations; iter++) {
double start_time = monotonic_seconds();
#pragma omp parallel for default(shared)
for (int i = 0; i < data->dimensions; i++) {
#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[data->dimensions * i + 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[data->dimensions * i + j];
denom = xij * xij;
}
new_w[i] = numer / denom;
}
printf("Done.\n");
for (int idx = 0; idx < data->dimensions; idx++) {
printf("%.3f ", new_w[idx]);
}
printf("\n");
memcpy(w, new_w, sizeof(w));
double end_time = monotonic_seconds();
print_time(end_time - start_time);
}
free(ouais);
free(w);
free(new_w);
free(data->buf);
free(label->buf);
free(data);
free(label);
return 0;
}