more progress

This commit is contained in:
Michael Zhang 2023-10-06 02:21:15 -04:00
parent 231275b325
commit 2aebe9b4c2
15 changed files with 638 additions and 102 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
.DS_Store
/target

327
Cargo.lock generated Normal file
View file

@ -0,0 +1,327 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anstream"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87"
[[package]]
name = "anstyle-parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
name = "anyhow"
version = "1.0.75"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d04704f56c2cde07f43e8e2c154b43f216dc5c92fc98ada720177362f953b956"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e231faeaca65ebd1ea3c737966bf858971cd38c3849107aa3ea7de90a804e45"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0862016ff20d69b84ef8247369fabf5c008a7417002411897d40ee1f4532b873"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "crossbeam-deque"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"memoffset",
"scopeguard",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294"
dependencies = [
"cfg-if",
]
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "itertools"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
dependencies = [
"either",
]
[[package]]
name = "memoffset"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c"
dependencies = [
"autocfg",
]
[[package]]
name = "proc-macro2"
version = "1.0.67"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rayon"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]]
name = "rust"
version = "0.1.0"
dependencies = [
"anyhow",
"clap",
"itertools",
"rayon",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "2.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7303ef2c05cd654186cb250d29049a24840ca25d2747c25c0381c8d9e2f582e8"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"

3
Cargo.toml Normal file
View file

@ -0,0 +1,3 @@
[workspace]
resolver = "2"
members = ["assignments/01/rust"]

1
assignments/01/.envrc Normal file
View file

@ -0,0 +1 @@
export BASE_PATH=$PWD

View file

@ -1,3 +1,5 @@
lc_openmp
lc_pthreads
*.o
dataset

View file

@ -5,10 +5,10 @@ CFLAGS :=
all: lc_openmp lc_pthreads
lc_openmp: lc_openmp.o common.o
gcc -o $@ $<
gcc -o $@ $^
lc_pthreads: lc_pthreads.o common.o
gcc -o $@ $<
gcc -o $@ $^
%.o: %.c
gcc -o $@ -c $<

View file

@ -1,5 +1,6 @@
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "common.h"
@ -18,3 +19,56 @@ double monotonic_seconds() {
void print_time(double const seconds) {
printf("Execution time: %0.04fs\n", seconds);
}
struct data *read_data(char *path) {
struct data *result = malloc(sizeof(struct data));
// Open file
FILE *file;
{
file = fopen(path, "r");
if (!file)
perror("failed to open file");
fscanf(file, "%u", &result->rows);
fscanf(file, "%u", &result->dimensions);
// Allocate
result->buf = malloc(result->dimensions * result->rows * sizeof(double));
// Read into buffer
for (uint32_t i = 0; i < result->dimensions; i++) {
fscanf(file, "%lf", &result->buf[i]);
}
fclose(file);
}
return result;
}
struct labels *read_labels(char *path) {
struct labels *result = malloc(sizeof(struct labels));
// Open file
FILE *file;
{
file = fopen(path, "r");
if (!file)
perror("failed to open file");
fscanf(file, "%u", &result->rows);
// Allocate
result->buf = malloc(result->rows * sizeof(double));
// Read into buffer
for (uint32_t i = 0; i < result->rows; i++) {
fscanf(file, "%lf", &result->buf[i]);
}
fclose(file);
}
return result;
}

View file

@ -1,6 +1,8 @@
#ifndef COMMON_H_
#define COMMON_H_
#include <stdint.h>
/**
* @brief Output the seconds elapsed while execution.
*
@ -17,4 +19,17 @@ void print_time(double const seconds);
*/
inline double monotonic_seconds();
struct data {
uint32_t rows, dimensions;
double *buf;
};
struct labels {
uint32_t rows;
double *buf;
};
struct data *read_data(char *path);
struct labels *read_labels(char *path);
#endif

View file

@ -1,3 +1,13 @@
#include <stdio.h>
#include "common.h"
int main() { return 0; }
int main(int argc, char **argv) {
char *data_file_name = argv[1], *label_file_name = argv[2];
struct data *data = read_data(data_file_name);
struct labels *label = read_labels(label_file_name);
printf("Read.\n");
return 0;
}

View file

@ -1,96 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "crossbeam-deque"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"memoffset",
"scopeguard",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294"
dependencies = [
"cfg-if",
]
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "memoffset"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c"
dependencies = [
"autocfg",
]
[[package]]
name = "rayon"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]]
name = "rust"
version = "0.1.0"
dependencies = [
"rayon",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"

View file

@ -4,4 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0.75"
clap = { version = "4.4.5", features = ["derive"] }
itertools = "0.11.0"
rayon = "1.8.0"

View file

@ -0,0 +1,38 @@
use std::{marker::PhantomData, ops::Index};
pub struct Span<T, F, U = usize>(Vec<T>, F, PhantomData<U>);
impl<T, F, U> Index<U> for Span<T, F, U>
where
F: Fn(U) -> usize,
{
type Output = T;
fn index(&self, index: U) -> &Self::Output {
let index = (self.1)(index);
&self.0[index]
}
}
#[test]
fn test() {
let w = (0..100).collect::<Vec<_>>();
// Exclude #4
let w_i_inv: Span<_, _, usize> = Span(w, fuck::<4>, PhantomData::default());
for i in 0..99 {
let n = w_i_inv[i];
println!("idx {i} is {n}");
}
panic!()
}
#[inline]
fn fuck<const n: usize>(i: usize) -> usize {
if i < n {
i
} else {
i + 1
}
}

View file

@ -1,3 +1,177 @@
fn main() {
println!("Hello, world!");
mod lolspan;
use std::{
fs::File,
io::{BufRead, BufReader},
path::PathBuf,
};
use anyhow::Result;
use clap::Parser;
use itertools::Itertools;
use rayon::prelude::{
IndexedParallelIterator, IntoParallelIterator, IntoParallelRefIterator,
ParallelBridge, ParallelIterator,
};
use crate::lolspan::Span;
#[derive(Parser)]
struct Opt {
data_file: PathBuf,
label_file: PathBuf,
outer_iterations: usize,
num_threads: usize,
}
struct Data {
rows: usize,
dimensions: usize,
buf: Vec<f64>,
}
struct Label {
num_points: usize,
buf: Vec<f64>,
}
fn main() -> Result<()> {
let opt = Opt::parse();
let data = read_data(&opt)?;
let label = read_label(&opt)?;
let mut w = (0..data.dimensions).map(|_| 0.0).collect::<Vec<_>>();
for _ in 0..opt.outer_iterations {
let w1 = w.clone();
let new_w = w1
.into_par_iter()
.enumerate()
.map(|(i, w_i)| {
let x_i_start = data.rows * i;
let x_i_end = data.rows * (i + 1);
let x_i = &data.buf[x_i_start..x_i_end];
// X = n x m
// y = n x 1
// w = 1 x m
//
// X_ni = n x (m - 1)
// w_ni = (m - 1) x 1
// X_ni_w_ni = n x 1
let missing_i = i;
let x_ni_w_ni = (0..data.rows)
.par_bridge()
.map(|j| {
(0..data.dimensions)
.filter(|i| *i != missing_i)
.par_bridge()
.map(|i| data.buf[data.rows * i + j] * w[i])
.reduce(|| 0.0, |a, b| a + b)
})
.collect::<Vec<_>>();
let y = label.buf.as_slice();
let sub = y
.par_iter()
.zip(x_ni_w_ni.par_iter())
.map(|(y, x)| y - x)
.collect::<Vec<_>>();
// combine
let numer = x_i
.par_iter()
.zip(sub.par_iter())
.map(|(x, y)| x * y)
.reduce(|| 0.0, |x, y| x + y);
let denom = x_i
.par_iter()
.zip(x_i.par_iter())
.map(|(a, b)| a * b)
.reduce(|| 0.0, |x, y| x + y);
return numer / denom;
})
.collect::<Vec<_>>();
w = new_w;
println!("w is {w:?}");
}
println!("Hello, world!");
Ok(())
}
fn read_data(opt: &Opt) -> Result<Data> {
let file = File::open(&opt.data_file)?;
let mut reader = BufReader::new(file);
// Read first line
let mut first_line = String::new();
reader.read_line(&mut first_line)?;
let parts = first_line.trim().split(' ').collect::<Vec<_>>();
let rows = parts[0].parse()?;
let dimensions = parts[1].parse()?;
// Read rest of the data
let mut buf = vec![0.0; rows * dimensions];
let mut line = String::new();
let mut j = 0;
loop {
let size = reader.read_line(&mut line)?;
if size == 0 {
break;
}
let parts = line.trim().split(' ').collect::<Vec<_>>();
debug_assert_eq!(parts.len(), dimensions);
let row = parts.into_iter().map(|s| s.parse::<f64>());
for (i, n) in row.enumerate() {
let coord = rows * i + j;
buf[coord] = n?;
}
line.clear();
j += 1;
}
Ok(Data {
rows,
dimensions,
buf,
})
}
fn read_label(opt: &Opt) -> Result<Label> {
let file = File::open(&opt.label_file)?;
let mut reader = BufReader::new(file);
// Read first line
let mut first_line = String::new();
reader.read_line(&mut first_line)?;
let num_points = first_line.trim().parse()?;
// Read rest of the data
let mut labels = Vec::with_capacity(num_points);
let mut line = String::new();
loop {
let size = reader.read_line(&mut line)?;
if size == 0 {
break;
}
let label = line.trim().parse::<f64>()?;
labels.push(label);
line.clear();
}
Ok(Label {
num_points,
buf: labels,
})
}

View file

@ -0,0 +1,4 @@
max_width = 80
tab_spaces = 2
wrap_comments = true
fn_single_line = true