From 550ed02ded9b6682320c742d2f76ce4e5e2e0dee Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Mon, 9 Oct 2023 03:14:23 -0500 Subject: [PATCH] update --- assignments/01/Makefile | 8 +++---- assignments/01/lc_openmp.c | 44 ++++++++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/assignments/01/Makefile b/assignments/01/Makefile index 3fe2f05..89bcfa6 100644 --- a/assignments/01/Makefile +++ b/assignments/01/Makefile @@ -3,8 +3,8 @@ CFLAGS := -std=c11 -fopenmp \ -I/opt/homebrew/opt/libomp/include \ -I/usr/lib/gcc/aarch64-linux-gnu/11/include \ - -O3 -LDFLAGS := -std=c11 -fopenmp -L/opt/homebrew/opt/libomp/lib -O3 + -O3 -g +LDFLAGS := -std=c11 -fopenmp -L/opt/homebrew/opt/libomp/lib -O3 RUST_SOURCES := $(shell find . -name "*.rs") all: lc_openmp lc_pthreads @@ -23,7 +23,7 @@ zhan4854.tar.gz: common.c common.h lc_openmp.c lc_pthreads.c Makefile rm -r zhan4854 lc_openmp: lc_openmp.o common.o - $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ -lm lc_pthreads: lc_pthreads.o common.o $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ @@ -46,7 +46,7 @@ run-openmp-small: lc_openmp dataset/small/train_data.txt ./lc_openmp dataset/small/train_data.txt dataset/small/train_label.txt 10 2 dataset/small/test_data.txt dataset/small/test_label.txt run-openmp-mnist: lc_openmp dataset/mnist/train_data.txt - ./lc_openmp dataset/mnist/train_data.txt dataset/mnist/train_label.txt 10 2 dataset/mnist/test_data.txt dataset/mnist/test_label.txt + ./lc_openmp dataset/mnist/train_data.txt dataset/mnist/train_label.txt 10 8 dataset/mnist/test_data.txt dataset/mnist/test_label.txt rust: $(RUST_SOURCES) cargo run -- ${BASE_PATH}/dataset/{small_data.csv,small_label.csv} 10 2 diff --git a/assignments/01/lc_openmp.c b/assignments/01/lc_openmp.c index 577b04e..35e9371 100644 --- a/assignments/01/lc_openmp.c +++ b/assignments/01/lc_openmp.c @@ -31,13 +31,16 @@ int main(int argc, char **argv) { printf("Running %d iteration(s) with %d thread(s).\n", outer_iterations, thread_count); + double program_start_time = monotonic_seconds(); FLOAT *w = calloc(data->dimensions, sizeof(FLOAT)); FLOAT *new_w = calloc(data->dimensions, sizeof(FLOAT)); FLOAT *ouais = calloc(data->dimensions * data->rows, sizeof(FLOAT)); + // FLOAT *loss_matrix = calloc(data->rows, sizeof(FLOAT)); + double total_compute_time = 0; for (int iter = 0; iter < outer_iterations; iter++) { - double start_time = monotonic_seconds(); + double iter_start_time = monotonic_seconds(); #pragma omp parallel for default(shared) for (int i = 0; i < data->dimensions; i++) { @@ -66,26 +69,49 @@ int main(int argc, char **argv) { denom += xij * xij; } - if (denom == 0) { + if (denom == 0) new_w[i] = 0; - // printf("wtf? %f\n", numer); - } else + else new_w[i] = numer / denom; } - double end_time = monotonic_seconds(); - print_time(end_time - start_time); + double iter_end_time = monotonic_seconds(); + total_compute_time += iter_end_time - iter_start_time; + printf("Iter duration (no print): %0.04fs\n", + iter_end_time - iter_start_time); - printf("Done.\nw = ["); + // Update w + // printf("w = ["); for (int idx = 0; idx < data->dimensions; idx++) { w[idx] = new_w[idx]; - printf("%.3f ", w[idx]); + // printf("%.3f ", w[idx]); } - printf("]\n"); + // printf("]\n"); + + // Compute loss + FLOAT loss_sum = 0; +#pragma omp parallel for default(shared) + for (int j = 0; j < data->rows; j++) { + FLOAT loss_value = 0; + for (int i = 0; i < data->dimensions; i++) { + loss_value += data->buf[data->rows * i + j] * w[i]; + } + + loss_value -= label->buf[j]; + loss_sum += loss_value * loss_value; + } + FLOAT loss = sqrt(loss_sum); + printf("Loss: %0.04f\n", loss); // memcpy(w, new_w, data->dimensions * sizeof(FLOAT)); } + double program_end_time = monotonic_seconds(); + printf("Program time (compute): %0.04fs\n", total_compute_time); + printf("Program time (total): %0.04fs\n", + program_end_time - program_start_time); + + // free(loss_matrix); free(ouais); free(new_w); free(data->buf);