Skip to content

Commit

Permalink
Merge pull request conda-forge#1544 from h-vetinari/cuda_enabled
Browse files Browse the repository at this point in the history
set CF_CUDA_ENABLED=True if `compiler('cuda')` is detected in meta.yaml
  • Loading branch information
isuruf committed Nov 16, 2021
2 parents 5d1d3ac + 779ab07 commit 569b092
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
14 changes: 14 additions & 0 deletions conda_smithy/configure_feedstock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from itertools import product, chain
import logging
import os
import re
import subprocess
import textwrap
import yaml
Expand Down Expand Up @@ -588,6 +589,19 @@ def _render_ci_provider(
if ver:
os.environ["DEFAULT_LINUX_VERSION"] = ver

# detect if `compiler('cuda')` is used in meta.yaml,
# and set appropriate environment variable
with open(
os.path.join(forge_dir, forge_config["recipe_dir"], "meta.yaml")
) as f:
meta_lines = f.readlines()
# looking for `compiler('cuda')` with both quote variants;
# do not match if there is a `#` somewhere before on the line
pat = re.compile(r"^[^\#]*compiler\((\"cuda\"|\'cuda\')\).*")
for ml in meta_lines:
if pat.match(ml):
os.environ["CF_CUDA_ENABLED"] = "True"

config = conda_build.config.get_or_merge_config(
None,
exclusive_config_file=forge_config["exclusive_config_file"],
Expand Down
26 changes: 26 additions & 0 deletions news/add-CF_CUDA_ENABLED.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
**Added:**

* Conda smithy will now detect if a recipe uses ``compiler('cuda')``
and set the ``CF_CUDA_ENABLED`` environment variable to ``True`` if
so. This can for example be useful to distinguish different options
for builds with or without GPUs in ``conda_build_config.yaml``.

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
33 changes: 33 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,39 @@ def choco_recipe(config_yaml, request):
)


@pytest.fixture(scope="function")
def cuda_enabled_recipe(config_yaml, request):
with open(os.path.join(config_yaml, "recipe", "meta.yaml"), "w") as fh:
fh.write(
"""
package:
name: py-test
version: 1.0.0
build:
skip: True # [os.environ.get("CF_CUDA_ENABLED") != "True"]
requirements:
build:
- {{ compiler('c') }}
- {{ compiler('cuda') }}
host:
- python
run:
- python
about:
home: home
"""
)
return RecipeConfigPair(
str(config_yaml),
_load_forge_config(
config_yaml,
exclusive_config_file=os.path.join(
config_yaml, "recipe", "default_config.yaml"
),
),
)


@pytest.fixture(scope="function")
def jinja_env(request):
tmplt_dir = os.path.join(conda_forge_content, "templates")
Expand Down
31 changes: 31 additions & 0 deletions tests/test_configure_feedstock.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,3 +815,34 @@ def test_cos7_env_render(py_recipe, jinja_env):
else:
if "DEFAULT_LINUX_VERSION" in os.environ:
del os.environ["DEFAULT_LINUX_VERSION"]


def test_cuda_enabled_render(cuda_enabled_recipe, jinja_env):
forge_config = copy.deepcopy(cuda_enabled_recipe.config)
has_env = "CF_CUDA_ENABLED" in os.environ
if has_env:
old_val = os.environ["CF_CUDA_ENABLED"]
del os.environ["CF_CUDA_ENABLED"]

try:
assert "CF_CUDA_ENABLED" not in os.environ
cnfgr_fdstk.render_azure(
jinja_env=jinja_env,
forge_config=forge_config,
forge_dir=cuda_enabled_recipe.recipe,
)
assert os.environ["CF_CUDA_ENABLED"] == "True"

# this configuration should be run
assert forge_config["azure"]["enabled"]
matrix_dir = os.path.join(cuda_enabled_recipe.recipe, ".ci_support")
assert os.path.isdir(matrix_dir)
# single matrix entry - readme is generated later in main function
assert len(os.listdir(matrix_dir)) == 6

finally:
if has_env:
os.environ["CF_CUDA_ENABLED"] = old_val
else:
if "CF_CUDA_ENABLED" in os.environ:
del os.environ["CF_CUDA_ENABLED"]

0 comments on commit 569b092

Please sign in to comment.