;;;; -*- Scheme -*- ;; ;; npot example for HLSIM ;; ;; David DeRoure June 12 1997, revised Sep 11 1997 ;; dder@martigny.ai.mit.edu ;; ;; This file should be compiled with (cps "npot1") then (cbf "npot1"). ;; It uses definitions in npot1d.scm. See npot1s.scm for the simulation ;; setup. See the HLSIM documentation for further details. ;; ;; Several "source" processors each broadcast a message carrying their ;; processor id and a potential. The messages are propagated by the ;; other processors, decreasing the potential at each hop, until the ;; potential is zero. The color of each processor represents the ;; sum of the potentials it has received from all sources. ;; ;; NB The initial potential, and the ids of the source processors, are ;; not in this file - they are set in the simulation (see npot1s.scm). (declare (usual-integrations)) ;; Every processor has an id (not the processor number, as that would ;; be cheating) (define processor-id (random 1000000)) ;; To keep track of maximum recorded potentials from each source, a ;; table is maintained. Messages received with potentials the same ;; as, or lower than those in the table will not be propagated. (define potential-table (make-table)) ;; This processor should originate messages if its processor ;; number is in the list of source processors. This list ;; is kept in a slot in the simulation, so that the numbers can ;; be set outside this file (in the simulation setup, npot1s.scm). (define (source?) (member (processor-number) (simulation.get (the-simulation) 'source-processors))) ;; The initial color of this processor depends whether it's a source. ;; (If sources are also propagators, they will often lose their color ;; during the simulation.) (define my-color (if (source?) "cyan" "blue")) ;; The loop for propagators. ;; ;; When a processor receives a message, it compares the potential ;; in the message with the highest potential it has recorded from that ;; source. If the potential in the message is greater, the processor ;; updates its record of the highest potential received from that source, ;; and broadcasts a message with a potential one unit less (still carrying ;; the id of the source processor); the color of the processor changes to ;; represent the sum of the recorded potentials. If the potential in the ;; received message is less than or equal to the highest recorded potential ;; from the source processor, no action is taken. ;; ;; When a processor detects a collision, it flashes red and waits for ;; a short period of time. The delay is random to avoid the "pulsating" ;; effect that otherwise occurs. ;; ;; The potential is mapped to color using potential->color, which is defined ;; in npot1d.scm (define primitive-message-event (make-event)) (define (propagator-loop) (select (global-timeout 'done) (primitive-message-event => (lambda (message) (event.clear! primitive-message-event) (if (eq? message 'collision) (begin (color-me "red") (wait (make-timeout-event (random 300))) (color-me my-color)) (if (and (> (message-potential message) 0) (new-maximum? message potential-table)) (begin (broadcast (make-message (message-source message) (-1+ (message-potential message)))) (set! my-color (potential->color (total-potential potential-table))) (color-me my-color)))) (propagator-loop))))) ;; Run the loop (define global-timeout (make-timeout-event 5000)) (color-me my-color) (if (source?) (broadcast (make-message processor-id (simulation.get (the-simulation) 'start-potential))) (propagator-loop)) ;; end of npot1.scm