Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add randn #15

Merged
merged 6 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
os: [ubuntu-latest]
# BUG
# fix bash script running on windows then uncomment
# os: [ubuntu-latest, windows-latest]
python-version: ["3.10", "3.11", "3.12"]

steps:
Expand Down Expand Up @@ -44,10 +47,13 @@ jobs:
run: |
make typecheck

- name: Test with pytest
run: |
make tests
# TODO
# add binaries to cache and make them available for library. Then uncomment

# - name: Test with pytest
# run: |
# make tests

- name: Test package build
- name: Test package build without pre-installed AF binaries
run: |
make build
bash ./scripts/build_package_without_binaries.sh
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

.eggs/
.mypy_cache
arrayfire_python_wrapper.egg-info/
arrayfire_binary_python_wrapper.egg-info/
build/
dist/

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ install :
pip install --upgrade pip
pip install pip-tools
pip-compile requirements.txt -o final_requirements.txt --allow-unsafe --rebuild --verbose
pip install -e . -r final_requirements.txt
pip install -r final_requirements.txt

.PHONY : build
build :
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# arrayfire-python-wrapper
# arrayfire-binary-python-wrapper

Arrayfire python C library wrapper

Expand Down
4 changes: 2 additions & 2 deletions arrayfire_wrapper/_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ def _load_backend_libs(self) -> None:
for backend_type in BackendType:
self._load_backend_lib(backend_type)

if self._backend_type:
if hasattr(self, "_backend_type"):
if VERBOSE_LOADS:
print(f"Setting {backend_type.name} as backend.")
break

if not self._backend_type and not self._clib:
if not (hasattr(self, "_backend_type") and hasattr(self, "_clib")):
raise RuntimeError(
"Could not load any ArrayFire libraries.\n"
"Please look at https://github.com/arrayfire/arrayfire-python/wiki for more information."
Expand Down
2 changes: 2 additions & 0 deletions arrayfire_wrapper/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@

__all__ += [
"AFRandomEngineHandle",
"randn",
"create_random_engine",
"random_engine_get_seed",
"random_engine_get_type",
"random_engine_set_seed",
"random_engine_set_type",
"random_normal",
"random_uniform",
"randu",
"release_random_engine",
Expand Down
4 changes: 4 additions & 0 deletions arrayfire_wrapper/lib/create_and_modify_array/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

__all__ += [
"AFRandomEngineHandle",
"randn",
"create_random_engine",
"random_engine_get_seed",
"random_engine_get_type",
"random_engine_set_seed",
"random_engine_set_type",
"random_normal",
"random_uniform",
"randu",
"release_random_engine",
Expand All @@ -27,10 +29,12 @@
from .create_array.random_number_generation import (
AFRandomEngineHandle,
create_random_engine,
randn,
random_engine_get_seed,
random_engine_get_type,
random_engine_set_seed,
random_engine_set_type,
random_normal,
random_uniform,
randu,
release_random_engine,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ def __init__(self, chunk: int | slice):


class ParallelRange(_IndexSequence):

"""
Class used to parallelize for loop.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@

__all__ += [
"create_random_engine",
"randn",
"random_engine_get_seed",
"random_engine_get_type",
"random_engine_set_seed",
"random_engine_set_type",
"random_normal",
"random_uniform",
"randu",
"release_random_engine",
]

from .random_number_generation import (
create_random_engine,
randn,
random_engine_get_seed,
random_engine_get_type,
random_engine_set_seed,
random_engine_set_type,
random_normal,
random_uniform,
randu,
release_random_engine,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,23 @@ def random_uniform(shape: tuple[int, ...], dtype: Dtype, engine: AFRandomEngineH
c_shape = CShape(*shape)
call_from_clib(random_uniform.__name__, ctypes.pointer(out), 4, c_shape.c_array, dtype.c_api_value, engine)
return out


def randn(shape: tuple[int, ...], dtype: Dtype, /) -> AFArray:
"""
source: https://arrayfire.org/docs/group__random__func__randn.htm#ga4925856392881453e4356a581c761ab9
"""
out = AFArray.create_null_pointer()
c_shape = CShape(*shape)
call_from_clib(randn.__name__, ctypes.pointer(out), 4, c_shape.c_array, dtype.c_api_value)
return out


def random_normal(shape: tuple[int, ...], dtype: Dtype, engine: AFRandomEngineHandle, /) -> AFArray:
"""
source: https://arrayfire.org/docs/group__random__func__randu.htm#ga2ca76d970cfac076f9006755582a4a4c
"""
out = AFArray.create_null_pointer()
c_shape = CShape(*shape)
call_from_clib(random_normal.__name__, ctypes.pointer(out), 4, c_shape.c_array, dtype.c_api_value, engine)
return out
2 changes: 1 addition & 1 deletion arrayfire_wrapper/version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

_MAJOR = "0"
_MINOR = "5"
_MINOR = "6"
# On main and in a nightly release the patch should be one ahead of the last
# released build.
_PATCH = "0"
Expand Down
14 changes: 7 additions & 7 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# Testing-related packages

# Checks style, syntax, etc.
flake8>=6.1.0
flake8~=7.0.0

# Static type checking
mypy==1.7.1
mypy==1.8.0

# Check import order style
isort>=5.13.2
isort~=5.13.2

# Automatic code formatting
black>=23.12.0
black~=24.1.1

# Allows generation of coverage reports with pytest.
pytest>=7.4.3
pytest>=8.0.0
pytest-cov>=4.1.0
pytest-mock>=3.11.1
pytest-mock>=3.12.0

# Allows codecov to generate coverage reports
coverage[toml]>=6.4
coverage[toml]>=7.4.1
codecov>=2.1.12

# Package building
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ requires = [
build-backend = "setuptools.build_meta"

[tool.setuptools.dynamic]
version = { attr = "arrayfire_wrapper.__version__" }
version = { attr = "arrayfire_wrapper.VERSION" }

[project]
name = "arrayfire-python-wrapper"
name = "arrayfire-binary-python-wrapper"
dynamic = ["version"]
requires-python = ">=3.10"
authors = [
Expand Down
16 changes: 16 additions & 0 deletions scripts/build_package_without_binaries.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# Run the Python script and capture the output and error
output=$(python -m build 2>&1)

# Define the expected error message
expected_error="Could not load any ArrayFire libraries."

# Check if the output contains the expected error message
if echo "$output" | grep -q "$expected_error"; then
echo "Expected error received."
exit 0 # Exit with success as the error is expected
else
echo "Unexpected output: $output"
exit 1 # Exit with failure as the output was not expected
fi