fix valgrind errors

This commit is contained in:
Michael Zhang 2023-10-07 20:37:18 -05:00
parent dd1d3fa504
commit 750876761d
6 changed files with 62 additions and 20 deletions

View file

@ -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"
}

4
Dockerfile Normal file
View file

@ -0,0 +1,4 @@
FROM ubuntu:22.04
RUN apt update -y && apt install -y --no-install-recommends \
git make gcc valgrind

View file

@ -1,7 +1,7 @@
.PHONY: all watch-openmp clean .PHONY: all watch-openmp clean
CFLAGS := -std=c11 -fopenmp=libomp -I/opt/homebrew/opt/libomp/include CFLAGS := -std=c11 -fopenmp -I/opt/homebrew/opt/libomp/include -g
LDFLAGS := -std=c11 -fopenmp=libomp -L/opt/homebrew/opt/libomp/lib LDFLAGS := -std=c11 -fopenmp -L/opt/homebrew/opt/libomp/lib -g
RUST_SOURCES := $(shell find . -name "*.rs") RUST_SOURCES := $(shell find . -name "*.rs")
all: lc_openmp lc_pthreads all: lc_openmp lc_pthreads

View file

@ -5,6 +5,12 @@
#include "common.h" #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. * @brief Output the seconds elapsed while execution.
* *
@ -24,8 +30,10 @@ struct data *read_data(char *path) {
if (!file) if (!file)
perror("failed to open file"); perror("failed to open file");
fscanf(file, "%u", &result->rows); if (!fscanf(file, "%u", &result->rows))
fscanf(file, "%u", &result->dimensions); perror("failed to read num rows");
if (!fscanf(file, "%u", &result->dimensions))
perror("failed to read num dimensions");
// Allocate // Allocate
result->buf = malloc(result->dimensions * result->rows * sizeof(FLOAT)); result->buf = malloc(result->dimensions * result->rows * sizeof(FLOAT));
@ -33,7 +41,8 @@ struct data *read_data(char *path) {
// Read into buffer // Read into buffer
for (uint32_t j = 0; j < result->rows; j++) { for (uint32_t j = 0; j < result->rows; j++) {
for (uint32_t i = 0; i < result->dimensions; i++) { 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) if (!file)
perror("failed to open file"); perror("failed to open file");
fscanf(file, "%u", &result->rows); if (!fscanf(file, "%u", &result->rows))
perror("failed to read num rows");
// Allocate // Allocate
result->buf = malloc(result->rows * sizeof(FLOAT)); result->buf = malloc(result->rows * sizeof(FLOAT));
// Read into buffer // Read into buffer
for (uint32_t i = 0; i < result->rows; i++) { 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); fclose(file);

View file

@ -21,11 +21,7 @@ void print_time(double const seconds);
* *
* @return The number of seconds. * @return The number of seconds.
*/ */
inline double monotonic_seconds() { double monotonic_seconds();
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec + ts.tv_nsec * 1e-9;
}
struct data { struct data {
uint32_t rows, dimensions; uint32_t rows, dimensions;

View file

@ -2,6 +2,7 @@
#include <omp.h> #include <omp.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "common.h" #include "common.h"
@ -10,7 +11,7 @@ int main(int argc, char **argv) {
int outer_iterations = atoi(argv[3]); int outer_iterations = atoi(argv[3]);
int thread_count = atoi(argv[4]); 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 data *data = read_data(data_file_name);
struct labels *label = read_labels(label_file_name); struct labels *label = read_labels(label_file_name);
@ -18,14 +19,14 @@ int main(int argc, char **argv) {
thread_count); thread_count);
FLOAT *w = calloc(data->dimensions, sizeof(FLOAT)); 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++) { for (int iter = 0; iter < outer_iterations; iter++) {
double start_time = monotonic_seconds(); double start_time = monotonic_seconds();
FLOAT *new_w = calloc(data->dimensions, sizeof(FLOAT));
#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++) {
FLOAT *ouais = calloc(data->rows, sizeof(FLOAT));
#pragma omp parallel for default(shared) #pragma omp parallel for default(shared)
for (int j = 0; j < data->rows; j++) { 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]; 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; FLOAT numer = 0, denom = 0;
@ -47,11 +48,10 @@ int main(int argc, char **argv) {
#pragma omp parallel for default(shared) reduction(+ : numer, denom) #pragma omp parallel for default(shared) reduction(+ : numer, denom)
for (int j = 0; j < data->rows; j++) { for (int j = 0; j < data->rows; j++) {
FLOAT xij = data->buf[data->dimensions * i + j]; FLOAT xij = data->buf[data->dimensions * i + j];
numer = xij * ouais[j]; numer = xij * ouais[data->dimensions * i + j];
denom = xij * xij; denom = xij * xij;
} }
free(ouais);
new_w[i] = numer / denom; new_w[i] = numer / denom;
} }
@ -61,14 +61,19 @@ int main(int argc, char **argv) {
} }
printf("\n"); printf("\n");
free(w); memcpy(w, new_w, sizeof(w));
w = new_w;
double end_time = monotonic_seconds(); double end_time = monotonic_seconds();
print_time(end_time - start_time); print_time(end_time - start_time);
} }
free(ouais);
free(w); free(w);
free(new_w);
free(data->buf);
free(label->buf);
free(data);
free(label);
return 0; return 0;
} }