[Chapel Merge] Initial Py_buffer support for Chapel 1D arrays

Branch: refs/heads/main
Revision: fc9a2910047eaf885118ac9d507611558912da9b
Author: jabraham17
Link: Initial Py_buffer support for Chapel 1D arrays by jabraham17 · Pull Request #26637 · chapel-lang/chapel · GitHub
Log Message:
Initial Py_buffer support for Chapel 1D arrays (#26637)

Add initial support for passing Chapel arrays to Python as Py_buffer's
and for reading Python objects that support the Py_buffer API.

This enables the following use case

use Python;
proc main() {
  var interp = new Interpreter();
  var mod = new Module(interp, "buffer");
  var doit_numpy = new Function(mod, "doit_numpy");
  var doit_torch = new Function(mod, "doit_torch");
  var doit_numpy_plus_torch = new Function(mod, "doit_numpy_plus_torch");

  var arr = [1, 2, 3, 4, 5];
  var pyArr = new Array(interp, arr);

  writeln("Calling numpy version: ", arr);
  doit_numpy(NoneType, pyArr);
  writeln("Back in Chapel: ", arr);

  arr = [1, 2, 3, 4, 5];
  writeln("Calling torch version: ", arr);
  doit_torch(NoneType, pyArr);
  writeln("Back in Chapel: ", arr);


  arr = [1, 2, 3, 4, 5];
  writeln("Calling numpy+torch version: ", arr);
  doit_numpy_plus_torch(NoneType, pyArr);
  writeln("Back in Chapel: ", arr);
}
import numpy as np
import torch

def doit_numpy(arr):
    x = np.asarray(arr)
    x[:] = 100

def doit_torch(arr):
    t = torch.frombuffer(arr, dtype=torch.int64)
    t[:] = 100

def doit_numpy_plus_torch(arr):
    t = torch.from_numpy(np.asarray(arr))
    t[:] = 100
  • st test/library/packages/Python
  • st test/library/packages/Python with gasnet

[Reviewed by @lydia-duncan]

Compare: Comparing f09f2514f7826b01705de95ca97c775ef0e2d92f...fa59a185a22f9b8de472b5e9682eeac62988ec46 · chapel-lang/chapel · GitHub

Diff:
M modules/packages/Python.chpl
M modules/packages/PythonHelper/ArrayTypes.c
M modules/packages/PythonHelper/ArrayTypes.h
M modules/packages/PythonHelper/ChapelPythonHelper.h
A test/library/packages/Python/correctness/arrays/PRETEST
A test/library/packages/Python/correctness/arrays/SUPPRESSIF
A test/library/packages/Python/correctness/arrays/gradient.chpl
A test/library/packages/Python/correctness/arrays/gradient.good
M test/library/packages/Python/correctness/arrays/numpyInterop.chpl
M test/library/packages/Python/correctness/arrays/numpyInterop.good
D test/library/packages/Python/correctness/arrays/numpyInterop.skipif
A test/library/packages/Python/correctness/arrays/usePythonArray.chpl
A test/library/packages/Python/correctness/arrays/usePythonArray.good
A test/library/packages/Python/correctness/arrays/usePythonArray.py
https://github.com/chapel-lang/chapel/pull/26637.diff