Welcome to the CHICKEN Scheme pasting service

MPI example added by arthurmaciel on Tue Apr 9 03:52:39 2013

;; Simple master/worker example
;; Can be run as follows: mpirun -np 4 csi -s master-worker.scm
;; where -np # indicates the number of processes

(use srfi-4 mpi)

(MPI:init)

;; MPI uses objects called communicators to define how processes
;; communicate with each other.  Almost all MPI routines require a
;; communicator as an argument.  

;; `MPI:get-comm-world' returns the communicator object which can send
;; messages to all running MPI processes
(define comm-world  (MPI:get-comm-world))

;; `MPI:comm-size' returns the number of running MPI processes
;;  (including the current one)
(define size        (MPI:comm-size comm-world))

;; `MPI:comm-rank' returns the rank of the calling MPI process
(define myrank      (MPI:comm-rank comm-world))

;; We assign rank 0 to be the master process, and the rest will be
;; worker processes
(cond ((zero? myrank)
       (printf "[~a/~a]: I am the master\n" myrank size)
       (let recur ((i 1))
	 (if (< i size)
	     (begin
	       ;; Send Hello message to process of rank i
	       (MPI:send (string->blob (sprintf "Hello ~a..." i)) 
			 i 0 comm-world)
	       
	       ;; Wait for a response from process of rank i
	       (let ((m (blob->string (MPI:receive i MPI:any-tag comm-world))))
		 (printf "[~a/~a]: received: ~a~%" myrank size m))

	       (recur (+ 1 i))))))
      (else
       (printf "[~a/~a]: I am a worker\n" myrank size)
       ;; Wait for a message from the master (process 0)
       (let ((msg (blob->string (MPI:receive 0 MPI:any-tag comm-world))))
	 (printf "[~a/~a]: received: ~a\n" myrank size msg)
	 ;; Send a response back to the master
	 (MPI:send (string->blob (sprintf "Processor ~a reporting!" myrank))
		   0 0 comm-world))))

(MPI:finalize)

Your annotation:

Enter a new annotation:

Your nick:
The title of your paste:
Your paste (mandatory) :
What's the procedure to access a character in a string by index in R5RS?
Visually impaired? Let me spell it for you (wav file) download WAV