NewLISP multi-core programming and newLISP kernel Programming

Source: Internet
Author: User

NewLISP multi-core programming and newLISP kernel Programming

NewLISP implements the Cilk API. It can run multiple processes simultaneously on a multi-processor or multi-core CPU architecture, thus saving the running time in parallel.

The spawn and sync functions can be implemented quickly. The following is an example from the newLISP official documentation. I have added a line of code.

#!/usr/bin/newlisp; calculate primes in a range(define (primes from to)  (local (plist)      (for (i from to)          (if (= 1 (length (factor i)))              (push i plist -1)))      plist)); start child processes(set 'start (time-of-day))(spawn 'p1 (primes 1 1000000))(spawn 'p2 (primes 1000001 2000000))(spawn 'p3 (primes 2000001 3000000))(spawn 'p4 (primes 3000001 4000000)); wait for a maximum of 60 seconds for all tasks to finish(sync 60000) ; returns true if all finished in time; p1, p2, p3 and p4 now each contain a lists of primes(println "time spawn: " (- (time-of-day) start))(println "time simple: " (time  (primes 1 4000000)))(println p1)(exit)


The running result is:

time spawn: 5068time simple: 8614.983(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107  109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223  227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 


Now let's explain the above Code:

1. local

(Local is a function similar to (let) used to initialize variables. The difference is that (local) initializes all variables to nil, so the initial value of plist is nil.

2. spawn Function

syntax: (spawn sym exp [true])

Sym is used to save the created sub-process ID

Exp is the expression run after the sub-process starts. Here we pass the expression composed of the function primes.

True is required when the parent and child processes communicate through the send/receive function. Generally, this parameter is not required.


The spawn function will return immediately, so you need to use the sync function to wait for the sub-process to return. However, after the sync function returns, the computing results of the sub-process are saved to sym.

3. sync Function

syntax: (sync int-timeout [func-inlet])syntax: (sync)

Int-timeout specifies the number of milliseconds to wait, as shown in the preceding figure: 60000 milliseconds, that is, 60 seconds. If the specified time arrives and the sub-process ends, sync returns true. If the sub-process does not end at that time, sync returns nil.

If the process is not completed or not, after sync returns, the computation result of the sub-process is saved to the pid returned by the previous spawn.

Func-inlet is a function or lambda expression that can be executed when sync returns. The parameter is the sub-process id.


4. Use sync to track the progress

The timeout value received by sync is only an estimate. You can call it multiple times in a loop and specify a short timeout value.

Modify the sync Code as follows:

; print a dot after each 1 seconds of waiting(until (sync 1000) (print "."))

The running result is:

.....time spawn: 5147

5. Failed handling

Some sub-processes may fail to end on time due to a problem. You can set a reasonable timeout value in sync, and then use abort To close non-terminated sub-processes.

(spawn 'p1 (primes 1 1000000))(spawn 'p2 (primes 1000001 2000000))(spawn 'p3 (primes 2000001 3000000))(spawn 'p4 (primes 3000001 4000000)); wait for one minute, then abort and; report unfinished PIDs(if (not (sync 60000))    (begin        (println "aborting unfinished: " (sync))        (abort))    (println "all finished successfully"))

Note: Here (sync) returns the child process ID that has not been completed.


6. Recursive use

This is especially suitable for my needs. I have a job tree that uses the breadth-first algorithm to recursively traverse the tree and create a process for each node to execute the job until all nodes are executed, to execute the task of the root node.

Because of the complexity, I will not attach my code here. I will only demonstrate the fibannaci algorithm in the official documentation.

#!/usr/bin/newlisp(define (fibo n)  (local (f1 f2)    (if(< n 2) 1       (begin          (spawn 'f1 (fibo (- n 1)))          (spawn 'f2 (fibo (- n 2)))          (sync 10000)          (+ f1 f2)))))(println (fibo 12))(exit)

The execution result is

233




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.