newLISP multi-process and multi-core CPUs

Source: Internet
Author: User

newLISP implements the Cilk API, which allows multiple simultaneous processes to be executed on multiprocessor or multi-core CPU architectures, achieving parallel time savings.

Using the Spawn and sync functions can be implemented quickly. Here is an example of an official document from newLISP:

#!/usr/bin/newlisp; Calculate primes in a range (define (primes from to) (  Local (plist) (for      (I-from) (          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 ' P 3 (primes 2000001 3000000)) (Spawn ' P4 (primes 3000001 4000000)); Wait for a maximum of $ seconds for all tasks to finish (sync 60000); Returns true if all finished in time;  P1, p2, p3 and P4 now all contain a lists of primes (println "Time Spawn:" (-(Time-of-day) Start)) (println "Time Simple: (Time  (primes 1 4000000)) (println p1) (exit)


The operating result is:

Time Spawn:5068time simple:8614.983 (2 3 5 7 One-time-all-in-one-in-a------------------------101 103 107
   109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223  


Now let's explain the code above:

1. Local

(Local is a function that is similar to (let), used to initialize a variable, except that (local) initializes the variable to nil, so the initial value of plist is nil

2. Spawn function

Syntax: (Spawn sym exp [true])

Sym used to save the created child process ID

Exp is an expression that runs after a child process is started, and here we pass an expression consisting of a function primes

True is required when a parent-child process communicates through the Send/receive function, which is generally not required


The Spawn function returns immediately, so waiting for the child process to return requires the use of the sync function. Note, however, that when the sync function returns, the result of the child process calculation is saved to sym.

3. Sync function

Syntax: (sync int-timeout [Func-inlet]) syntax: (sync)

INT-TIMEOUT Specifies the number of milliseconds to wait, such as 60000 milliseconds above, or 60 seconds. If the specified time arrives and the child process ends, sync returns True if the child process is not finished then sync returns nil.

Regardless of whether the child process is complete, when sync returns, the child process calculation results are saved to the PID that was previously returned by Spawn.

Func-inlet is a function or lambda expression that can be executed when sync returns, and the parameter is the child process ID.


4. Use sync to track progress

Sync receives a timeout of just one estimate, which can be called multiple times within a loop and specify a shorter timeout to



newLISP multi-process and multi-core CPUs

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.