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
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

View file

@ -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);

View file

@ -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;

View file

@ -2,6 +2,7 @@
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}