diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..8d96444 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,26 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-dockerfile +{ + "name": "Existing Dockerfile", + "build": { + // Sets the run context to one level up instead of the .devcontainer folder. + "context": "..", + // Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename. + "dockerfile": "../Dockerfile" + } + + // Features to add to the dev container. More info: https://containers.dev/features. + // "features": {}, + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + + // Uncomment the next line to run commands after the container is created. + // "postCreateCommand": "cat /etc/os-release", + + // Configure tool-specific properties. + // "customizations": {}, + + // Uncomment to connect as an existing user other than the container default. More info: https://aka.ms/dev-containers-non-root. + // "remoteUser": "devcontainer" +} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ece80b7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM ubuntu:22.04 + +RUN apt update -y && apt install -y --no-install-recommends \ + git make gcc valgrind \ No newline at end of file diff --git a/assignments/01/Makefile b/assignments/01/Makefile index 7c07104..19d8561 100644 --- a/assignments/01/Makefile +++ b/assignments/01/Makefile @@ -1,7 +1,7 @@ .PHONY: all watch-openmp clean -CFLAGS := -std=c11 -fopenmp=libomp -I/opt/homebrew/opt/libomp/include -LDFLAGS := -std=c11 -fopenmp=libomp -L/opt/homebrew/opt/libomp/lib +CFLAGS := -std=c11 -fopenmp -I/opt/homebrew/opt/libomp/include -g +LDFLAGS := -std=c11 -fopenmp -L/opt/homebrew/opt/libomp/lib -g RUST_SOURCES := $(shell find . -name "*.rs") all: lc_openmp lc_pthreads diff --git a/assignments/01/common.c b/assignments/01/common.c index 2b4a785..e698e1b 100644 --- a/assignments/01/common.c +++ b/assignments/01/common.c @@ -5,6 +5,12 @@ #include "common.h" +double monotonic_seconds() { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec + ts.tv_nsec * 1e-9; +} + /** * @brief Output the seconds elapsed while execution. * @@ -24,8 +30,10 @@ struct data *read_data(char *path) { if (!file) perror("failed to open file"); - fscanf(file, "%u", &result->rows); - fscanf(file, "%u", &result->dimensions); + if (!fscanf(file, "%u", &result->rows)) + perror("failed to read num rows"); + if (!fscanf(file, "%u", &result->dimensions)) + perror("failed to read num dimensions"); // Allocate result->buf = malloc(result->dimensions * result->rows * sizeof(FLOAT)); @@ -33,7 +41,8 @@ struct data *read_data(char *path) { // Read into buffer for (uint32_t j = 0; j < result->rows; j++) { for (uint32_t i = 0; i < result->dimensions; i++) { - fscanf(file, FLOAT_FORMAT, &result->buf[result->rows * i + j]); + if (!fscanf(file, FLOAT_FORMAT, &result->buf[result->rows * i + j])) + perror("failed to read data"); } } @@ -53,14 +62,16 @@ struct labels *read_labels(char *path) { if (!file) perror("failed to open file"); - fscanf(file, "%u", &result->rows); + if (!fscanf(file, "%u", &result->rows)) + perror("failed to read num rows"); // Allocate result->buf = malloc(result->rows * sizeof(FLOAT)); // Read into buffer for (uint32_t i = 0; i < result->rows; i++) { - fscanf(file, FLOAT_FORMAT, &result->buf[i]); + if (!fscanf(file, FLOAT_FORMAT, &result->buf[i])) + perror("failed to read label"); } fclose(file); diff --git a/assignments/01/common.h b/assignments/01/common.h index 76c1652..1245941 100644 --- a/assignments/01/common.h +++ b/assignments/01/common.h @@ -21,11 +21,7 @@ void print_time(double const seconds); * * @return The number of seconds. */ -inline double monotonic_seconds() { - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - return ts.tv_sec + ts.tv_nsec * 1e-9; -} +double monotonic_seconds(); struct data { uint32_t rows, dimensions; diff --git a/assignments/01/lc_openmp.c b/assignments/01/lc_openmp.c index 5fae705..3468557 100644 --- a/assignments/01/lc_openmp.c +++ b/assignments/01/lc_openmp.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "common.h" @@ -10,7 +11,7 @@ int main(int argc, char **argv) { int outer_iterations = atoi(argv[3]); int thread_count = atoi(argv[4]); - omp_set_num_threads(8); + omp_set_num_threads(thread_count); struct data *data = read_data(data_file_name); struct labels *label = read_labels(label_file_name); @@ -18,14 +19,14 @@ int main(int argc, char **argv) { 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(); - 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++) { @@ -39,7 +40,7 @@ int main(int argc, char **argv) { x_ni_w_ni = data->buf[data->rows * i2 + j] * w[i2]; } - ouais[j] = label->buf[j] - x_ni_w_ni; + ouais[data->dimensions * i + j] = label->buf[j] - x_ni_w_ni; } FLOAT numer = 0, denom = 0; @@ -47,11 +48,10 @@ int main(int argc, char **argv) { #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]; + numer = xij * ouais[data->dimensions * i + j]; denom = xij * xij; } - free(ouais); new_w[i] = numer / denom; } @@ -61,14 +61,19 @@ int main(int argc, char **argv) { } printf("\n"); - free(w); - w = new_w; + 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; } \ No newline at end of file