Skip to content

Commit

Permalink
Use ctypes instead of cython
Browse files Browse the repository at this point in the history
  • Loading branch information
ayan-b committed Jun 23, 2020
1 parent cd28559 commit 50f047f
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 34 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ notifications:
email: false

install:
- pip3 install .
- python setup.py build_ext --inplace
- pip3 install .
script:
- pip3 install pytest pytest-cov
- pytest --cov=gdist
Expand All @@ -13,6 +13,8 @@ jobs:
- name: "Python 3.8 on Xenial Linux"
language: python
python: 3.8
before_install: pip3 install codecov
after_success: codecov
- name: "Python 3.7.4 on macOS"
os: osx
osx_image: xcode11.2
Expand All @@ -22,8 +24,9 @@ jobs:
language: shell
before_install:
- choco install python --version 3.8.0
- python -m pip install --upgrade pip
- python -m pip install -U pip
- pip install --user wheel
- ./create_dll.bat
env: PATH=/c/Python38:/c/Python38/Scripts:$PATH
- stage: lint
language: python
Expand Down
43 changes: 43 additions & 0 deletions create_dll.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
echo on

if NOT DEFINED VCINSTALLDIR (
if exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" (
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64
echo "USING VISUAL STUDIO 17"
)
)

if NOT DEFINED VCINSTALLDIR (
if exist "C:\Program Files (x86)\Microsoft Visual Studio 15.0\VC\vcvarsall.bat" (
call "C:\Program Files (x86)\Microsoft Visual Studio 15.0\VC\vcvarsall.bat" amd64
echo "USING VISUAL STUDIO 15"
)
)

if NOT DEFINED VCINSTALLDIR (
if exist "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" (
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
echo "USING VISUAL STUDIO 14"
)
)

if NOT DEFINED VCINSTALLDIR (
if exist "C:\Program Files (x86)\Microsoft Visual Studio 13.0\VC\vcvarsall.bat" (
call "C:\Program Files (x86)\Microsoft Visual Studio 13.0\VC\vcvarsall.bat" amd64
echo "USING VISUAL STUDIO 13"
)
)

if NOT DEFINED VCINSTALLDIR (
if exist "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" (
call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" amd64
echo "USING VISUAL STUDIO 12"
)
)

if NOT DEFINED VCINSTALLDIR (
echo "No compatible visual studio found! run vcvarsall.bat first!"
)

cl.exe /LD /DDLL_EXPORTS /DNDEBUG gdist_c_api.cpp
ls
4 changes: 3 additions & 1 deletion gdist.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import ctypes
import glob
import os
import sys

import numpy as np
import scipy.sparse


if sys.platform == 'win32':
libfile = glob.glob('build/*/gdist*.pyd')[0]
libfile = glob.glob('gdist_c_api.dll')[0]
libfile = os.path.abspath(libfile)
lib = ctypes.windll.LoadLibrary(libfile)
elif sys.platform == 'darwin':
try:
Expand Down
21 changes: 3 additions & 18 deletions gdist_c_api.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
#include <iostream>
#include <fstream>
#include <inttypes.h>

#include "geodesic_library/geodesic_algorithm_exact.h"


#if defined(_WIN32)
# if defined(DLL_EXPORTS)
# define DLL_EXPORT_API __declspec(dllexport)
# else
# define DLL_EXPORT_API __declspec(dllimport)
# endif
#else
# define DLL_EXPORT_API
#endif
#include "gdist_c_api.h"


void compute_gdist_impl(
Expand Down Expand Up @@ -108,7 +93,7 @@ double* local_gdist_matrix_impl(


extern "C" {
DLL_EXPORT_API void compute_gdist(
void compute_gdist(
unsigned number_of_vertices,
unsigned number_of_triangles,
double *vertices,
Expand All @@ -134,7 +119,7 @@ extern "C" {
);
}

DLL_EXPORT_API double* local_gdist_matrix(
double* local_gdist_matrix(
unsigned number_of_vertices,
unsigned number_of_triangles,
double *vertices,
Expand Down
63 changes: 63 additions & 0 deletions gdist_c_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <iostream>
#include <fstream>
#include <inttypes.h>

#include "geodesic_library/geodesic_algorithm_exact.h"


#if defined(_WIN32)
# if defined(DLL_EXPORTS)
# define DLL_EXPORT_API __declspec(dllexport)
# else
# define DLL_EXPORT_API __declspec(dllimport)
# endif
#else
# define DLL_EXPORT_API
#endif


void compute_gdist_impl(
unsigned number_of_vertices,
unsigned number_of_triangles,
double *vertices,
int *triangles,
unsigned number_of_source_indices,
unsigned number_of_target_indices,
unsigned *source_indices_array,
unsigned *target_indices_array,
double *distance,
double distance_limit
);

double* local_gdist_matrix_impl(
unsigned number_of_vertices,
unsigned number_of_triangles,
double *vertices,
unsigned *triangles,
unsigned *sparse_matrix_size,
double max_distance
);

extern "C" {
DLL_EXPORT_API void compute_gdist(
unsigned number_of_vertices,
unsigned number_of_triangles,
double *vertices,
int *triangles,
unsigned number_of_source_indices,
unsigned number_of_target_indices,
unsigned *source_indices_array,
unsigned *target_indices_array,
double *distance,
double distance_limit
);

DLL_EXPORT_API double* local_gdist_matrix(
unsigned number_of_vertices,
unsigned number_of_triangles,
double *vertices,
unsigned *triangles,
unsigned *sparse_matrix_size,
double max_distance
);
};
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build-system]
requires = ["setuptools", "wheel", "numpy"]
23 changes: 14 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,23 @@

import os
import shutil
import sys
import setuptools

GEODESIC_NAME = "gdist"

GEODESIC_MODULE = [
setuptools.Extension(
name=GEODESIC_NAME, # Name of extension
sources=["gdist_c_api.cpp"],
language="c++",
extra_compile_args=['--std=c++11'],
extra_link_args=['--std=c++11'],
)
]
GEODESIC_MODULE = []

if sys.platform == 'darwin' or sys.platform == 'linux':
GEODESIC_MODULE = [
setuptools.Extension(
name=GEODESIC_NAME, # Name of extension
sources=["gdist_c_api.cpp"],
language="c++",
extra_compile_args=['--std=c++11'],
extra_link_args=['--std=c++11'],
)
]

INCLUDE_DIRS = [
# numpy.get_include(), # NumPy dtypes
Expand All @@ -72,6 +76,7 @@
name="tvb-" + GEODESIC_NAME,
version='2.0.2',
scripts=['gdist.py'],
py_modules=['gdist'],
ext_modules=GEODESIC_MODULE,
include_dirs=INCLUDE_DIRS,
install_requires=INSTALL_REQUIREMENTS,
Expand Down
8 changes: 4 additions & 4 deletions tests/test_gdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def test_flat_triangular_mesh(self):
distance = gdist.compute_gdist(
vertices,
triangles,
source_indices=source,
target_indices=target
source,
target
)
np.testing.assert_array_almost_equal(distance, [0.2])

Expand All @@ -27,8 +27,8 @@ def test_hedgehog_mesh(self):
distance = gdist.compute_gdist(
vertices,
triangles,
source_indices=source,
target_indices=target
source,
target
)
np.testing.assert_array_almost_equal(distance, [1.40522])

Expand Down

0 comments on commit 50f047f

Please sign in to comment.