Bug 275997 - devel/py-joblib: Add BSD support to CPU count check
Summary: devel/py-joblib: Add BSD support to CPU count check
Status: Closed FIXED
Alias: None
Product: Ports & Packages
Classification: Unclassified
Component: Individual Port(s) (show other bugs)
Version: Latest
Hardware: Any Any
: --- Affects Some People
Assignee: Steven Kreuzer
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-12-29 13:59 UTC by Jason W. Bacon
Modified: 2024-01-05 16:52 UTC (History)
0 users

See Also:
bugzilla: maintainer-feedback? (skreuzer)


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jason W. Bacon freebsd_committer freebsd_triage 2023-12-29 13:59:28 UTC
--- /usr/ports/devel/py-joblib/files/patch-joblib_externals_loky_backend_context.py     1969-12-31 18:00:00.000000000 -0600
+++ /usr/ports/wip/py-joblib/files/patch-joblib_externals_loky_backend_context.py       2023-12-29 07:58:36.334877000 -0600
@@ -0,0 +1,18 @@
+--- joblib/externals/loky/backend/context.py.orig      2023-06-29 15:14:21 UTC
++++ joblib/externals/loky/backend/context.py
+@@ -274,6 +274,15 @@ def _count_physical_cores():
+             )
+             cpu_info = cpu_info.stdout
+             cpu_count_physical = int(cpu_info)
++        # Maybe also openbsd, dragonfly, etc?
++        elif sys.platform.startswith('freebsd') or sys.platform.startswith('netbsd'):
++            cpu_info = subprocess.run(
++                "sysctl -n hw.ncpu".split(),
++                capture_output=True,
++                text=True,
++            )
++            cpu_info = cpu_info.stdout
++            cpu_count_physical = int(cpu_info)
+         else:
+             raise NotImplementedError(f"unsupported platform: {sys.platform}")
+
Comment 1 Steven Kreuzer freebsd_committer freebsd_triage 2024-01-02 03:21:49 UTC
lgtm. please feel free to commit. thanks!
Comment 2 Jason W. Bacon freebsd_committer freebsd_triage 2024-01-03 02:22:28 UTC
Upstream expects the physical core count, not hyperthread count (which is reported by sysconf() and sysctl hw.ncpu).

The following should do it efficiently:

--- /usr/ports/devel/py-joblib/Makefile 2023-08-30 11:33:24.037724000 -0500
+++ /usr/ports/wip/py-joblib/Makefile   2024-01-02 20:20:40.975573000 -0600
@@ -11,6 +11,7 @@
 LICENSE=       BSD3CLAUSE
 LICENSE_FILE=  ${WRKSRC}/LICENSE.txt
 
+RUN_DEPENDS+=  ${PYTHON_PKGNAMEPREFIX}sysctl>0:devel/py-sysctl@${PY_FLAVOR}
 TEST_DEPENDS=  ${PYNUMPY}
 #TEST_DEPENDS+=        ${PYTHON_PKGNAMEPREFIX}threadpoolctl>0:devel/py-threadpoolctl@${PY_FLAVOR}
 
diff -ruN --exclude=CVS --exclude=.svn /usr/ports/devel/py-joblib/files/patch-joblib_externals_loky_backend_context.py /usr/ports/wip/py-joblib/files/patch-joblib_externals_loky_backend_context.py
--- /usr/ports/devel/py-joblib/files/patch-joblib_externals_loky_backend_context.py     1969-12-31 18:00:00.000000000 -0600
+++ /usr/ports/wip/py-joblib/files/patch-joblib_externals_loky_backend_context.py       2024-01-02 20:16:46.256155000 -0600
@@ -0,0 +1,25 @@
+--- joblib/externals/loky/backend/context.py.orig      2023-06-29 15:14:21 UTC
++++ joblib/externals/loky/backend/context.py
+@@ -11,6 +11,7 @@
+ #
+ import os
+ import sys
++import sysctl
+ import math
+ import subprocess
+ import traceback
+@@ -274,6 +275,14 @@ def _count_physical_cores():
+             )
+             cpu_info = cpu_info.stdout
+             cpu_count_physical = int(cpu_info)
++        elif sys.platform.startswith('freebsd'):
++            cpu_count_physical = int(sysctl.filter("kern.smp.cores")[0].value);
+         else:
+             raise NotImplementedError(f"unsupported platform: {sys.platform}")
+
Comment 3 Jason W. Bacon freebsd_committer freebsd_triage 2024-01-04 14:59:28 UTC
FYI, there is apparently no portable sysctl interface for python.  The one in FreeBSD ports is FreeBSD-specific.  There are separate projects for Linux and NetBSD with completely different APIs.

We can use the FreeBSD py-sysctl, but we'll have maintain a patch in the py-joblib port rather than upstream it.

I think it's best if the upstream code uses subprocesses for better portability.  I don't think it should impair performance that much.  And if someone is really concerned about performance, they won't be using an interpreted language anyway.
Comment 4 commit-hook freebsd_committer freebsd_triage 2024-01-05 16:52:24 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/ports/commit/?id=74265132881f61bd8686ceb3c65f3e9fdec88974

commit 74265132881f61bd8686ceb3c65f3e9fdec88974
Author:     Jason W. Bacon <jwb@FreeBSD.org>
AuthorDate: 2024-01-05 16:50:06 +0000
Commit:     Jason W. Bacon <jwb@FreeBSD.org>
CommitDate: 2024-01-05 16:51:44 +0000

    devel/py-joblib: Update to 1.3.2_1

    Patch to allow autodeection of hardware cores on FreeBSD
    Pull request submitted to upstream

    PR:             275997
    Approved by:    skreuzer

 devel/py-joblib/Makefile                           |  1 +
 ...-joblib_externals_loky_backend_context.py (new) | 39 ++++++++++++++++++++++
 2 files changed, 40 insertions(+)