Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
zommiommy committed Aug 5, 2024
2 parents eb7b668 + cdc25d3 commit bc63ca6
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 5 deletions.
173 changes: 173 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
name: CI

on:
push:
branches:
- main
- master
#tags:
# - '*'
pull_request:
workflow_dispatch:

permissions:
contents: read

jobs:
# If it compiles on ubuntu x86_64, then we can start compiling on other
# platforms, otherwise we want to break early
canary_build:
runs-on: ubuntu-latest
#if: "startsWith(github.ref, 'refs/tags/')"
container:
image: quay.io/pypa/manylinux2014_x86_64
env:
RUSTFLAGS: "" # for some reason it has -D warnings by default
steps:
# setup env
- uses: actions/checkout@v4
- name: Install Openssl
run: apk add openssl-dev py3-pip
- uses: korandoru/setup-zig@v1
with:
zig-version: 0.13.0
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: nightly-2024-06-13
components: rustfmt
cache-workspaces: bindings/python/
- name: Install Maturin
run: python3 -m pip install 'maturin'
# Check
- name: Check that the package compiles
run: cargo check --manifest-path=bindings/python/Cargo.toml
# actually build
- name: Build the package
run: python3 build.py
working-directory: bindings/python/
# upload on pypi
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-linux-x86_64
path: dist

linux:
runs-on: ${{ matrix.platform.runner }}
if: "startsWith(github.ref, 'refs/tags/')"
needs: [canary_build]
strategy:
matrix:
platform:
- runner: ubuntu-latest
target: x86_64
- runner: ubuntu-latest
target: x86
- runner: ubuntu-latest
target: aarch64
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.platform.target }}
args: --release --out dist --find-interpreter
sccache: 'true'
manylinux: musllinux_1_1
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-linux-${{ matrix.platform.target }}
path: dist

windows:
runs-on: ${{ matrix.platform.runner }}
needs: [canary_build]
if: "startsWith(github.ref, 'refs/tags/')"
strategy:
matrix:
platform:
- runner: windows-latest
target: x64
- runner: windows-latest
target: x86
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.10'
architecture: ${{ matrix.platform.target }}
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.platform.target }}
args: --release --out dist --find-interpreter
sccache: 'true'
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-windows-${{ matrix.platform.target }}
path: dist

macos:
runs-on: ${{ matrix.platform.runner }}
needs: [canary_build]
if: "startsWith(github.ref, 'refs/tags/')"
strategy:
matrix:
platform:
- runner: macos-latest
target: x86_64
- runner: macos-14
target: aarch64
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.platform.target }}
args: --release --out dist --find-interpreter
sccache: 'true'
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-macos-${{ matrix.platform.target }}
path: dist

sdist:
runs-on: ubuntu-latest
needs: [canary_build]
if: "startsWith(github.ref, 'refs/tags/')"
steps:
- uses: actions/checkout@v4
- name: Build sdist
uses: PyO3/maturin-action@v1
with:
command: sdist
args: --out dist
- name: Upload sdist
uses: actions/upload-artifact@v4
with:
name: wheels-sdist
path: dist

release:
name: Release
runs-on: ubuntu-latest
if: "startsWith(github.ref, 'refs/tags/')"
needs: [linux, windows, macos, sdist]
permissions:
id-token: write
steps:
- uses: actions/download-artifact@v4
- name: Publish to PyPI
uses: PyO3/maturin-action@v1
with:
command: upload
args: --non-interactive --skip-existing wheels-*/*
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Nix shells folders
.cargo_home
.rustup

# Generated by Cargo
# will have compiled files and executables
target
Expand Down
1 change: 1 addition & 0 deletions graph/csr/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(internal_features)]
#![feature(core_intrinsics)]
use std::hash::{Hash, Hasher};
use std::intrinsics::unlikely;
Expand Down
2 changes: 1 addition & 1 deletion graph/express_measures/src/dot.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::types::*;
use crate::validation::*;
use core::fmt::Debug;
use core::intrinsics::unlikely;
use core::ops::Mul;
use rayon::prelude::*;
use std::iter::Sum;
use core::intrinsics::unlikely;

#[inline(always)]
/// Returns the dot product between the two provided vectors computed sequentially.
Expand Down
1 change: 1 addition & 0 deletions graph/express_measures/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(internal_features)]
#![feature(core_intrinsics)]
mod cosine_similarity;
mod element_wise_operations;
Expand Down
94 changes: 90 additions & 4 deletions graph/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,45 @@ use std::collections::{BTreeSet, BTreeMap};
use std::io::{BufWriter, Write};
use std::fs::File;

#[derive(Clone, Debug)]
pub struct Edge{
pub src: String,
pub dst: String,
pub edge_type: Option<String>,
pub weight: Option<WeightT>,
};

impl PartialEq for Edge {
fn eq(&self, other: &Self) -> bool {
(self.src == other.src) && (self.dst == other.dst) && (self.edge_type == other.edge_type) && (self.weight.zip(other.weight).map(|(a, b)| a == b).unwrap_or(true))
}
}

impl Eq for Edge {}

impl PartialOrd for Edge {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(
self.src.partial_cmp(&other.src)?
.then(self.dst.partial_cmp(&other.dst)?)
.then(self.edge_type.partial_cmp(&other.edge_type)?)
.then(self.weight.zip(other.weight).map(|(a, b)| a.total_cmp(&b)).unwrap_or(core::cmp::Ordering::Equal))
)
}
}

impl Ord for Edge {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.src.cmp(&other.src)
.then(self.dst.cmp(&other.dst))
.then(self.edge_type.cmp(&other.edge_type))
.then(self.weight.zip(other.weight).map(|(a, b)| a.total_cmp(&b)).unwrap_or(core::cmp::Ordering::Equal))
}
}

#[derive(Clone, Debug)]
pub struct GraphBuilder {
pub(crate) edges: BTreeSet<EdgeQuadruple>,
pub(crate) edges: BTreeSet<Edge>,
pub(crate) nodes: BTreeMap<String, Option<Vec<String>>>,

pub(crate) has_node_types: bool,
Expand Down Expand Up @@ -105,7 +141,7 @@ impl GraphBuilder {
if edge_type.is_some() {
self.has_edge_types = true;
}
self.edges.insert(EdgeQuadruple(src, dst, edge_type, weight.unwrap_or(self.default_weight)));
self.edges.insert(Edge{src, dst, edge_type, weight});
Ok(())
}

Expand All @@ -128,7 +164,7 @@ impl GraphBuilder {
return Err(format!("The weight {} is not a finite numnber!", w));
}
}
self.edges.remove(&EdgeQuadruple(src, dst, edge_type, weight.unwrap_or(self.default_weight)));
self.edges.remove(&Edge{src, dst, edge_type, weight});
Ok(())
}

Expand All @@ -155,6 +191,16 @@ impl GraphBuilder {
Ok(())
}

/// Get a sorted iterator over the edges of the graph
pub fn iter_edges(&self) -> impl Iterator<Item=Edge> + '_ {
self.edges.iter().cloned()
}

/// Get a sorted iterator over the nodes of the graph
pub fn iter_nodes(&self) -> impl Iterator<Item=(String, Option<Vec<String>>)> + '_ {
self.nodes.iter().map(|(k, v)| (k.clone(), v.clone()))
}

/// Consume the edges and nodes to create a new graph.
pub fn build(&mut self) -> Result<Graph> {
let nodes = core::mem::replace(&mut self.nodes, BTreeMap::new());
Expand All @@ -168,7 +214,7 @@ impl GraphBuilder {

let edges_iterator = ItersWrapper::Sequential::<_, _, ParEmpty<_>>(
edges.into_iter().enumerate().map(|(idx, x)|
Result::Ok((idx, (x.0, x.1, x.2, x.3)))
Result::Ok((idx, (x.src, x.dst, x.edge_type, x.weight.unwrap_or(self.default_weight))))
)
);

Expand Down Expand Up @@ -210,6 +256,46 @@ impl GraphBuilder {
}
}

impl core::iter::Extend<Edge> for GraphBuilder {
fn extend<T: IntoIterator<Item=Edge>>(&mut self, iter: T) {
for edge in iter {
let _ = self.add_edge(edge.src, edge.dst, edge.edge_type, edge.weight);
}
}
}

impl core::iter::Extend<(String, Option<Vec<String>>)> for GraphBuilder {
fn extend<T: IntoIterator<Item=(String, Option<Vec<String>>)>>(&mut self, iter: T) {
for edge in iter {
let _ = self.add_node(edge.0, edge.1);
}
}
}

impl core::ops::AddAssign<Self> for GraphBuilder {
fn add_assign(&mut self, other: Self) {
assert_eq!(self.directed, other.directed, "The graphs must have the same directedness to be added!");

self.extend(other.iter_edges());
self.extend(other.iter_nodes());

self.name = format!("{} | {}", self.name, other.name);
}
}

impl core::iter::Sum for GraphBuilder {
fn sum<I: Iterator<Item=Self>>(mut iter: I) -> Self {
let first = iter.next();
if first.is_none() {
return Self::new(None, None);
}
let mut res = first.unwrap();
for i in iter {
res += i;
}
res
}
}

#[derive(Debug)]
pub struct GraphCSVBuilder {
Expand Down
40 changes: 40 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Nix shell for building the python wheels ready for publication
{ pkgs ? import <nixpkgs> {} }:

let
rustupToolchain = "nightly-2024-06-13";
in
pkgs.mkShell {
buildInputs = with pkgs; [
python311

rustup
maturin

stdenv
pkg-config
openssl
which
gcc
binutils

# All the C libraries that a manylinux_1 wheel might depend on:
ncurses
xorg.libX11
xorg.libXext
xorg.libXrender
xorg.libICE
xorg.libSM
glib
];

RUST_BACKTRACE = 1;
# use nightly bcz all the features we need are in nightly
RUSTUP_TOOLCHAIN = rustupToolchain;
# make everything self-contained to this folder
CARGO_HOME = toString ./.cargo_home;
RUSTUP_HOME = toString ./.rustup;

CXX = "zig c++ -target x86_64-linux-gnu.2.16";
CC = "zig cc -target x86_64-linux-gnu.2.16";
}

0 comments on commit bc63ca6

Please sign in to comment.