Skip to content

Commit

Permalink
Merge pull request dmritrekker#15 from tfmoraes/cython_compile
Browse files Browse the repository at this point in the history
compile just using cython and add github-ci
  • Loading branch information
baranaydogan authored May 17, 2023
2 parents eff722f + 264e353 commit 29ea2d8
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 43 deletions.
141 changes: 141 additions & 0 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
name: Build

on: [push, pull_request]

jobs:
build_wheels:
name: Build wheel for cp${{ matrix.python }}-${{ matrix.platform_id }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
# Windows 64-bit
- os: windows-latest
python: 38
platform_id: win_amd64
- os: windows-latest
python: 39
platform_id: win_amd64
- os: windows-latest
python: 310
platform_id: win_amd64
- os: windows-latest
python: 311
platform_id: win_amd64

# Linux 64-bit
- os: ubuntu-latest
python: 38
platform_id: manylinux_x86_64
- os: ubuntu-latest
python: 39
platform_id: manylinux_x86_64
- os: ubuntu-latest
python: 310
platform_id: manylinux_x86_64
- os: ubuntu-latest
python: 311
platform_id: manylinux_x86_64

# macOS on Intel 64-bit
- os: macos-latest
python: 38
arch: x86_64
platform_id: macosx_x86_64
- os: macos-latest
python: 39
arch: x86_64
platform_id: macosx_x86_64
- os: macos-latest
python: 310
arch: x86_64
platform_id: macosx_x86_64
- os: macos-latest
python: 311
arch: x86_64
platform_id: macosx_x86_64

# macOS on Apple M1 64-bit
- os: macos-latest
python: 38
arch: arm64
platform_id: macosx_arm64
- os: macos-latest
python: 39
arch: arm64
platform_id: macosx_arm64
- os: macos-latest
python: 310
arch: arm64
platform_id: macosx_arm64
- os: macos-latest
python: 311
arch: arm64
platform_id: macosx_arm64

steps:
- uses: actions/checkout@v3
with:
submodules: true

- uses: actions/setup-python@v4
name: Install Python host for cibuildwheel
with:
python-version: '3.9'

# Visual Studio
- name: Set up MSVC x64
if: matrix.platform_id == 'win_amd64'
uses: ilammy/msvc-dev-cmd@v1

- name: Installing Nasm win_amd64
if: matrix.platform_id == 'win_amd64'
run: |
choco install --no-progress -y nasm
- name: Installing Nasm win32
if: matrix.platform_id == 'win32'
run: |
choco install --no-progress -y --x86 nasm
- name: Set Path win32
if: matrix.platform_id == 'win32'
shell: bash
run: |
echo "C:\Program Files (x86)\NASM" >> $GITHUB_PATH
- name: Set Path win_amd64
if: matrix.platform_id == 'win_amd64'
shell: bash
run: |
echo "C:\Program Files\NASM" >> $GITHUB_PATH
- name: Install cibuildwheel
run: python -m pip install cibuildwheel==2.11.4

- name: Build wheels
# to supply options, put them in 'env', like:
env:
CIBW_MANYLINUX_X86_64_IMAGE: manylinux2014
CIBW_MANYLINUX_I686_IMAGE: manylinux2014
CIBW_MANYLINUX_AARCH64_IMAGE: manylinux2014
CIBW_BUILD: cp${{ matrix.python }}-${{ matrix.platform_id }}
CIBW_ARCHS_LINUX: auto aarch64

CIBW_ARCHS_MACOS: ${{ matrix.arch }}
CIBW_ENVIRONMENT_MACOS: >
CMAKE_OSX_ARCHITECTURES=${{ matrix.arch }}
CIBW_TEST_COMMAND: python -c "import Trekker"

# Include latest Python beta
CIBW_PRERELEASE_PYTHONS: True

CIBW_BEFORE_BUILD: pip install cython wheel

run: python -m cibuildwheel --output-dir wheelhouse src/python/

- uses: actions/upload-artifact@v2
with:
path: ./wheelhouse/*.whl
13 changes: 8 additions & 5 deletions src/config/multithreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ void MT::MTRUN(size_t range, int numberOfThreads, std::function<void(MTTASK mtta
progress = 0;


std::shared_ptr<std::thread[]> task (new std::thread[numberOfThreads]);
std::vector<std::thread> task;
task.resize(numberOfThreads);

std::unique_lock<std::mutex> lk(exit_mx);

Expand Down Expand Up @@ -360,8 +361,8 @@ void MT::MTRUN(size_t range, int chunkSize, int numberOfThreads, std::function<v
float chkPrgrs = float(chunkSize)/float(range)*100;
progress = 0;


std::shared_ptr<std::thread[]> task (new std::thread[numberOfThreads]);
std::vector<std::thread> task;
task.resize(numberOfThreads);

std::unique_lock<std::mutex> lk(exit_mx);

Expand Down Expand Up @@ -435,7 +436,8 @@ void MT::MTRUN(size_t range, int numberOfThreads, std::string message, std::func
float chkPrgrs = float(chunkSize)/float(range)*100;
progress = 0;

std::shared_ptr<std::thread[]> task (new std::thread[numberOfThreads]);
std::vector<std::thread> task;
task.resize(numberOfThreads);

std::unique_lock<std::mutex> lk(exit_mx);

Expand Down Expand Up @@ -508,7 +510,8 @@ void MT::MTRUN(size_t range, int chunkSize, int numberOfThreads, std::string mes
float chkPrgrs = float(chunkSize)/float(range)*100;
progress = 0;

std::shared_ptr<std::thread[]> task (new std::thread[numberOfThreads]);
std::vector<std::thread> task;
task.resize(numberOfThreads);

std::unique_lock<std::mutex> lk(exit_mx);

Expand Down
1 change: 1 addition & 0 deletions src/config/multithreader.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <thread>
#include <functional>
#include <mutex>
Expand Down
2 changes: 1 addition & 1 deletion src/math/doRandomThings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RandomDoer::RandomDoer() {


#ifdef BUILD_FOR_WINDOWS
#if defined(BUILD_FOR_WINDOWS) || defined(BUILD_FOR_MACOS)
std::random_device rd;
unsigned long randSeed = rd();
#else
Expand Down
File renamed without changes.
File renamed without changes.
103 changes: 66 additions & 37 deletions src/python/setup.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,89 @@
from setuptools import setup
from setuptools.extension import Extension
import glob
import os
import platform
import shutil
from pathlib import Path

from Cython.Build import cythonize
from Cython.Distutils import build_ext
from setuptools import setup
from setuptools.extension import Extension

from pathlib import Path
import shutil
import os
import platform
import glob

class TrekkerBuildExt(build_ext):
def run(self):
build_ext.run(self)
target_dir = Path(self.build_lib)
os.makedirs(str(target_dir / 'Trekker'))
for file in glob.glob(str(Path('cython/*'))):
shutil.copy(file, str(target_dir / 'Trekker'))
shutil.copyfile(str(Path('build/cython') / 'Trekker.cpp'), str(target_dir / 'Trekker/Trekker.cpp'))
os.makedirs(str(target_dir / "Trekker"))
for file in glob.glob(str(Path("cython/*"))):
shutil.copy(file, str(target_dir / "Trekker"))
shutil.copyfile(
str(Path("build/cython") / "Trekker.cpp"),
str(target_dir / "Trekker/Trekker.cpp"),
)


LIB_DIRS = ["cython/"]
INC_DIRS = [
"cython/",
"../",
"../nifticlib-2.0.0/znzlib",
"../nifticlib-2.0.0/niftilib",
"../base",
"../math",
"../image",
"../config",
"../tracker",
]

LIB_DIRS = ['cython/']
INC_DIRS = ['cython/']
cpp_sources = [str(i) for i in (Path("../").glob("**/*.cpp"))]
c_sources = [str(i) for i in (Path("../").glob("**/*.c"))]
sources = cpp_sources + c_sources
sources = [
source
for source in sources
if not source.endswith("cmd.cpp") and not source.lower().endswith("trekker.cpp")
]
sources = sources + ["cython/Trekker.pxd", "cython/Trekker.pyx", "../trekker.cpp"]

if (platform.system()=="Linux"):
LIBS = ["Trekker","z","pthread"]
if (platform.system()=="Windows"):
LIBS = ["Trekker"]
print(f"\n\n\n{sources=}\n\n\n")

if platform.system() == "Windows":
LIBS = []
EXTRA_COMPILE_ARGS = ["/DBUILD_FOR_WINDOWS"]
elif platform.system() == "Darwin":
LIBS = ["z"]
EXTRA_COMPILE_ARGS = ["-std=c++11","-D BUILD_FOR_MACOS", "-D HAVE_ZLIB"]
else:
LIBS = ["z"]
EXTRA_COMPILE_ARGS = ["-std=c++11", "-D HAVE_ZLIB"]

with open("README.md", "r") as fh:
long_description = fh.read()

setup(
name='Trekker',
description='Python package for parallel transport tractography (PTT)',
name="Trekker",
description="Python package for parallel transport tractography (PTT)",
long_description=long_description,
long_description_content_type="text/markdown",
version='0.9',
setup_requires=[
'setuptools>=18.0',
'cython>=0.28.4',
],
version="0.9",
setup_requires=["setuptools>=18.0", "cython>=0.28.4", "wheel"],
ext_modules=cythonize(
[
Extension(
'Trekker',
sources=['cython/Trekker.pxd','cython/Trekker.pyx'],
[
Extension(
"Trekker",
sources=sources,
libraries=LIBS,
library_dirs=LIB_DIRS,
include_dirs=INC_DIRS,
language="c++",)
],
language_level=3,
build_dir="build",
compiler_directives=dict(always_allow_keywords=True),
),
language="c++",
extra_compile_args=EXTRA_COMPILE_ARGS,
)
],
language_level=3,
build_dir="build",
compiler_directives=dict(always_allow_keywords=True),
),
cmdclass=dict(build_ext=TrekkerBuildExt),
author="Dogu Baran Aydogan",
author_email="[email protected]",
Expand All @@ -64,7 +93,7 @@ def run(self):
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
license='BSD 2-Clause License',
platforms=['linux','win'],
python_requires=">=3.6",
license="BSD 2-Clause License",
platforms=["linux", "win"],
)
5 changes: 5 additions & 0 deletions src/trekker.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef TREKKER_H_
#define TREKKER_H_

#include <string>
#include <vector>

Expand Down Expand Up @@ -178,3 +181,5 @@ class Trekker {
bool timeUp;

};

#endif

0 comments on commit 29ea2d8

Please sign in to comment.