commit 13ba7fca071e4d6e14771c4ca03759897d3118ba
parent 31b1032c750af2de7be5faef39a5a3edb053036d
Author: Andreas Olsson <photoguy.apo@gmail.com>
Date: Tue, 10 Jan 2017 08:41:45 +0100
Add files via upload
Added pmapp, and changed name on the function pmap to pmapf.
Diffstat:
5 files changed, 108 insertions(+), 26 deletions(-)
diff --git a/main.rkt b/main.rkt
@@ -1,19 +1,4 @@
#lang racket
-;pmap is a parallel concurrent map function.
-;its inspired of Clojures pmap.
-
-(require racket/future)
-
-(provide pmap)
-
-(define (transpose lists) ; collumns to rows!
- (apply map list lists))
-
-(define (pmap func . lists) ; pmap
- (map touch
- (for/list ([a (transpose lists)])
- (future (lambda () (apply func a)))
- )))
-
-
+(require "pmapf.rkt" "pmapp.rkt")
+(provide (all-from-out "pmapf.rkt" "pmapp.rkt"))
diff --git a/pmap.scrbl b/pmap.scrbl
@@ -3,25 +3,52 @@
@require[@for-label[pmap
racket/base
- racket/future]]
+ racket/future
+ racket/place
+ racket/runtime-path]]
@title{pmap}
@author{APOS80}
@defmodule[pmap]
-@section{General behavior}
+@section{pmapf}
-pmap works as map but applies the function to every item in the list/lists in parallel using futures.
+pmapf works as map but applies the function to every item in the list/lists in parallel using futures.
It's restrictions is the same as for futures and map in general in Racket.
-@section{Use}
+
@racketblock[
- (pmap function list/lists)
-
- >(map + '(1 2 3) '(1 2 3))
+ ;Example:
+
+ >(pmapf + '(1 2 3) '(1 2 3))
>'(2 4 6)
]
If the function applied is to simple pmap might perform worse than map because of the
-overhead a future generates.
-\ No newline at end of file
+overhead a future generate.
+
+
+@section{pmapp}
+
+pmapp works almost as map and applies the function to every item in the list/lists in parallel using places.
+Places has some restrictions and that impacts on the implementation in several ways, READ ON!
+
+First:
+On creation of a place a rkt file is loaded into the new place, that means the uses of the place
+is determined of the file and cant be changed by anyone else then the creator. For now it includes racket base, flonum and fixnum.
+
+Second:
+A procedure can NOT be passed to a place! Then how do you pass a function, as a list!
+
+
+@racketblock[
+ ;Example:
+
+ >(pmapp '(lambda (x y)(+ x y) ) '(1 2 3) '(1 2 3))
+ >'(2 4 6)
+
+ >(pmapp '(lambda (x y)(fl+ x y) ) '(1.0 2.0 3.0) '(1.0 2.0 3.0))
+ >'(2.0 4.0 6.0)
+ ]
+
diff --git a/pmapf.rkt b/pmapf.rkt
@@ -0,0 +1,19 @@
+#lang racket
+
+;pmap is a parallel concurrent map function.
+;its inspired of Clojures pmap.
+
+(require racket/future)
+
+(provide pmapf)
+
+(define (transpose lists) ; collumns to rows!
+ (apply map list lists))
+
+(define (pmapf func . lists) ; pmap
+ (map touch
+ (for/list ([a (transpose lists)])
+ (future (lambda () (apply func a)))
+ )))
+
+
diff --git a/pmapp.rkt b/pmapp.rkt
@@ -0,0 +1,31 @@
+#lang racket
+
+(require racket/place)
+(require racket/runtime-path)
+
+(define-runtime-path worker "pmapp_worker.rkt")
+;(define pa (build-path (current-directory) "pmapp_worker.rkt"))
+
+(provide pmapp)
+
+(define (transpose lists) ; collumns to rows!
+ (apply map list lists))
+
+(define (pmapp func . args) ;start places, give work, collect results, stop places.
+
+ (define jlist (transpose args))
+ (display jlist)
+
+ (let* ([pls (for/list ([i (in-range (length jlist))])
+ (dynamic-place worker 'pmapp-worker ))]
+ [wpls (for/list ([j pls][w jlist])
+ (place-channel-put j (append (list func) w)))]
+ [rlist (for/list ([v pls]) (place-channel-get v))]
+ [stop (map place-wait pls)])
+ rlist
+ ))
+
+
+
+
+
diff --git a/pmapp_worker.rkt b/pmapp_worker.rkt
@@ -0,0 +1,21 @@
+#lang racket
+
+(define worker-namespace
+ (parameterize ((current-namespace (make-base-namespace)))
+ (namespace-require 'racket)
+ (namespace-require 'racket/flonum)
+ (namespace-require 'racket/fixnum)
+ (current-namespace)))
+
+(eval 'force worker-namespace) ; -> #<procedure:force>
+
+;(eval '(apply (lambda (x y)(* x y)) '(2 3)) worker-namespace)
+
+(provide pmapp-worker)
+
+(define (pmapp-worker place-ch)
+ (let (
+ [v (eval (place-channel-get place-ch) worker-namespace)])
+ (place-channel-put place-ch v)
+
+ ))