This commit is contained in:
Michael Zhang 2023-09-23 00:04:06 -05:00
parent 07f514c657
commit f6d401f865
10 changed files with 121 additions and 4 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.DS_Store

View file

@ -1,2 +1,3 @@
lc_openmp
lc_pthreads
lc_pthreads
*.o

View file

@ -1,9 +1,14 @@
.PHONY: all
CFLAGS :=
all: lc_openmp lc_pthreads
lc_openmp: lc_openmp.c
lc_openmp: lc_openmp.o common.o
gcc -o $@ $<
lc_pthreads: lc_pthreads.c
gcc -o $@ $<
lc_pthreads: lc_pthreads.o common.o
gcc -o $@ $<
%.o: %.c
gcc -o $@ -c $<

20
assignments/01/common.c Normal file
View file

@ -0,0 +1,20 @@
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <time.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.
*
* @param seconds Seconds spent on execution, excluding IO.
*/
void print_time(double const seconds) {
printf("Execution time: %0.04fs\n", seconds);
}

20
assignments/01/common.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef COMMON_H_
#define COMMON_H_
/**
* @brief Output the seconds elapsed while execution.
*
* @param seconds Seconds spent on execution, excluding IO.
*/
void print_time(double const seconds);
/**
* @brief Return the number of seconds since an unspecified time (e.g., Unix
* epoch). This is accomplished with a high-resolution monotonic timer,
* suitable for performance timing.
*
* @return The number of seconds.
*/
inline double monotonic_seconds();
#endif

View file

@ -0,0 +1,20 @@
import random
import click
def evaluate(w, p):
result = sum(map(lambda s: s[0] * s[1], zip(w, p)))
return result
@click.command()
@click.option('--dimensions', default=2, help='Number of dimensions')
@click.option('--count', default=2000, help='How many points to generate')
def generate_test_data(dimensions: int, count: int):
actual_w = [random.uniform(0.0, 1.0) for _ in range(dimensions)]
for _ in range(count):
point = [random.uniform(0.0, 1.0) for _ in range(dimensions)]
y = evaluate(actual_w, point)
print(point, y)
if __name__ == '__main__':
generate_test_data()

View file

@ -1 +1,3 @@
#include "common.h"
int main() { return 0; }

View file

@ -1 +1,3 @@
#include "common.h"
int main() { return 0; }

31
assignments/01/poetry.lock generated Normal file
View file

@ -0,0 +1,31 @@
# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand.
[[package]]
name = "click"
version = "8.1.7"
description = "Composable command line interface toolkit"
optional = false
python-versions = ">=3.7"
files = [
{file = "click-8.1.7-py3-none-any.whl", hash = "sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28"},
{file = "click-8.1.7.tar.gz", hash = "sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de"},
]
[package.dependencies]
colorama = {version = "*", markers = "platform_system == \"Windows\""}
[[package]]
name = "colorama"
version = "0.4.6"
description = "Cross-platform colored terminal text."
optional = false
python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7"
files = [
{file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"},
{file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"},
]
[metadata]
lock-version = "2.0"
python-versions = "^3.11"
content-hash = "628b2f91879695db7c6062f64a17355697ff1237691b4f4f3105875e3b3eb929"

View file

@ -0,0 +1,15 @@
[tool.poetry]
name = "01"
version = "0.1.0"
description = ""
authors = ["Michael Zhang <mail@mzhang.io>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.11"
click = "^8.1.7"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"