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

Parallel superpose #22

Open
patham9 opened this issue Nov 29, 2023 · 2 comments
Open

Parallel superpose #22

patham9 opened this issue Nov 29, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@patham9
Copy link
Collaborator

patham9 commented Nov 29, 2023

An attempt I tried a few weeks ago which worked out and I forgot to document (OS-process-based ambivalence to let one easily make use of CPU resources)

(import (chicken process) (chicken string) (chicken file posix) amb)

(define-syntax hyperpose
  (syntax-rules ()
    ((_ arg1 arg2)
     (receive (pipefd0 pipefd1) (create-pipe)
              (let ((pid (process-fork)))
                   (if (eq? pid 0)
                       (begin (write arg1 (open-output-file* pipefd1))
                              (exit 0)) ;child is done
                       (let* ((res1 arg2) ;get to work before waiting for child to return result
                              (res2 (read (open-input-file* pipefd0))))
                             (amb res1 res2))))))))

;example:
(define (test x) (list 2 x (+ 2 x)))
(display (amb-collect (hyperpose (test 1) (test 2))))
(newline)

This is just an example, forking has some time overhead so use only if you have computationally expensive branches which should be able to be explored with different CPU cores!

@patham9 patham9 changed the title Hyperpose (OS-process-based ambivalence to let one easily make use of CPU resources) Concurrent superpose Nov 29, 2023
@patham9
Copy link
Collaborator Author

patham9 commented Nov 29, 2023

For mettamorph.scm the repeated amb import is not needed and the arguments should be wrapped into auto-list1:

(import (chicken process) (chicken string) (chicken file posix))

(define-syntax hyperpose
  (syntax-rules ()
    ((_ arg1 arg2)
     (receive (pipefd0 pipefd1) (create-pipe)
              (let ((pid (process-fork)))
                   (if (eq? pid 0)
                       (begin (write (auto-list1 arg1) (open-output-file* pipefd1))
                              (exit 0)) ;child is done
                       (let* ((res1 (auto-list1 arg2)) ;get to work before waiting for child to return result
                              (res2 (read (open-input-file* pipefd0))))
                             (amb res1 res2))))))))

TODO accept list (tuple) as argument which can have more than 2 items.

@patham9 patham9 added the enhancement New feature or request label Nov 29, 2023
@patham9 patham9 changed the title Concurrent superpose Parallel superpose Nov 29, 2023
@patham9
Copy link
Collaborator Author

patham9 commented Dec 15, 2023

An initial useful version is in master now 69ae8bf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant