This commit is contained in:
Michael Zhang 2023-10-09 03:14:23 -05:00
parent 29d791e1e6
commit 550ed02ded
2 changed files with 39 additions and 13 deletions

View file

@ -3,8 +3,8 @@
CFLAGS := -std=c11 -fopenmp \ CFLAGS := -std=c11 -fopenmp \
-I/opt/homebrew/opt/libomp/include \ -I/opt/homebrew/opt/libomp/include \
-I/usr/lib/gcc/aarch64-linux-gnu/11/include \ -I/usr/lib/gcc/aarch64-linux-gnu/11/include \
-O3 -O3 -g
LDFLAGS := -std=c11 -fopenmp -L/opt/homebrew/opt/libomp/lib -O3 LDFLAGS := -std=c11 -fopenmp -L/opt/homebrew/opt/libomp/lib -O3
RUST_SOURCES := $(shell find . -name "*.rs") RUST_SOURCES := $(shell find . -name "*.rs")
all: lc_openmp lc_pthreads 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 rm -r zhan4854
lc_openmp: lc_openmp.o common.o lc_openmp: lc_openmp.o common.o
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ -lm
lc_pthreads: lc_pthreads.o common.o lc_pthreads: lc_pthreads.o common.o
$(CC) $(CFLAGS) $(LDFLAGS) -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 ./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 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) rust: $(RUST_SOURCES)
cargo run -- ${BASE_PATH}/dataset/{small_data.csv,small_label.csv} 10 2 cargo run -- ${BASE_PATH}/dataset/{small_data.csv,small_label.csv} 10 2

View file

@ -31,13 +31,16 @@ int main(int argc, char **argv) {
printf("Running %d iteration(s) with %d thread(s).\n", outer_iterations, printf("Running %d iteration(s) with %d thread(s).\n", outer_iterations,
thread_count); thread_count);
double program_start_time = monotonic_seconds();
FLOAT *w = calloc(data->dimensions, sizeof(FLOAT)); FLOAT *w = calloc(data->dimensions, sizeof(FLOAT));
FLOAT *new_w = calloc(data->dimensions, sizeof(FLOAT)); FLOAT *new_w = calloc(data->dimensions, sizeof(FLOAT));
FLOAT *ouais = calloc(data->dimensions * data->rows, 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++) { 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) #pragma omp parallel for default(shared)
for (int i = 0; i < data->dimensions; i++) { for (int i = 0; i < data->dimensions; i++) {
@ -66,26 +69,49 @@ int main(int argc, char **argv) {
denom += xij * xij; denom += xij * xij;
} }
if (denom == 0) { if (denom == 0)
new_w[i] = 0; new_w[i] = 0;
// printf("wtf? %f\n", numer); else
} else
new_w[i] = numer / denom; new_w[i] = numer / denom;
} }
double end_time = monotonic_seconds(); double iter_end_time = monotonic_seconds();
print_time(end_time - start_time); 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++) { for (int idx = 0; idx < data->dimensions; idx++) {
w[idx] = new_w[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)); // 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(ouais);
free(new_w); free(new_w);
free(data->buf); free(data->buf);