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

Initial Py_buffer support for Chapel 1D arrays #26637

Merged
merged 6 commits into from
Feb 4, 2025

Conversation

jabraham17
Copy link
Member

@jabraham17 jabraham17 commented Jan 31, 2025

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]

Signed-off-by: Jade Abraham <jade.abraham@hpe.com>
Signed-off-by: Jade Abraham <jade.abraham@hpe.com>
Signed-off-by: Jade Abraham <jade.abraham@hpe.com>
Signed-off-by: Jade Abraham <jade.abraham@hpe.com>
Signed-off-by: Jade Abraham <jade.abraham@hpe.com>
Signed-off-by: Jade Abraham <jade.abraham@hpe.com>
@jabraham17 jabraham17 force-pushed the initial-pybuffer-support branch from 7de01ba to fa59a18 Compare February 3, 2025 23:02
@jabraham17 jabraham17 marked this pull request as ready for review February 3, 2025 23:05
@jabraham17 jabraham17 merged commit fc9a291 into chapel-lang:main Feb 4, 2025
8 checks passed
@jabraham17 jabraham17 deleted the initial-pybuffer-support branch February 4, 2025 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants