Main Page | Namespace List | Class List | File List | Namespace Members | Class Members | Related Pages | Examples

prime.py

This example generates prime numbers.

How To Run

Once run ...

prime.py master

Everywhere else run ...

prime.py
#!/usr/bin/python # Copyright 2004 Andrew Wilkinson <aw@cs.york.ac.uk>. # # This file is part of PyLinda (http://www-users.cs.york.ac.uk/~aw/pylinda) # # PyLinda is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # # PyLinda is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with PyLinda; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import linda import math import sys linda.connect() LIMIT = 10000 GRAIN = 250 def is_prime(): primes = {} while 1: t = ts._in(("next task", int)) start = t[1] if start >= LIMIT: ts._out(("next task", start)) return 0 print start ts._out(("next task", start + GRAIN)) for me in range(start, start + GRAIN): i = 2 while i <= math.sqrt(me): if not primes.has_key(i): ok = ts._rd(("prime", i, bool))[2] primes[i] = ok else: ok = primes[i] if ok and (me % i == 0): ts._out(("prime", me, False)) break i = i + 1 if i > math.sqrt(me): ts._out(("prime", me, True)) import sys def main(): ts._out(("next task", 1)) ts._rd(("next task", LIMIT+1 )) linda.universe._in(("ts", linda.TupleSpace)) print "finished" linda.kernel.message_noreturn("kill_server") if __name__ == "__main__": if len(sys.argv) > 1 and sys.argv[1] == "master": ts = linda.TupleSpace() linda.universe._out(("ts", ts)) else: ts = linda.universe._rd(("ts", linda.TupleSpace))[1] print ts if len(sys.argv) > 1 and sys.argv[1] == "master": main() else: is_prime()
00001 #!/usr/bin/python 00002 00003 # Copyright 2004 Andrew Wilkinson <aw@cs.york.ac.uk>. 00004 # 00005 # This file is part of PyLinda (http://www-users.cs.york.ac.uk/~aw/pylinda) 00006 # 00007 # PyLinda is free software; you can redistribute it and/or modify 00008 # it under the terms of the GNU Lesser General Public License as published by 00009 # the Free Software Foundation; either version 2.1 of the License, or 00010 # (at your option) any later version. 00011 # 00012 # PyLinda is distributed in the hope that it will be useful, 00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 # GNU Lesser General Public License for more details. 00016 # 00017 # You should have received a copy of the GNU Lesser General Public License 00018 # along with PyLinda; if not, write to the Free Software 00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 00021 import linda 00022 import math 00023 00024 import sys 00025 00026 linda.connect() 00027 00028 LIMIT = 10000 00029 GRAIN = 250 00030 00031 def is_prime(): 00032 primes = {} 00033 while 1: 00034 t = ts._in(("next task", int)) 00035 start = t[1] 00036 if start >= LIMIT: 00037 ts._out(("next task", start)) 00038 return 0 00039 print start 00040 ts._out(("next task", start + GRAIN)) 00041 00042 for me in range(start, start + GRAIN): 00043 i = 2 00044 while i <= math.sqrt(me): 00045 if not primes.has_key(i): 00046 ok = ts._rd(("prime", i, bool))[2] 00047 primes[i] = ok 00048 else: 00049 ok = primes[i] 00050 if ok and (me % i == 0): 00051 ts._out(("prime", me, False)) 00052 break 00053 i = i + 1 00054 if i > math.sqrt(me): 00055 ts._out(("prime", me, True)) 00056 00057 00058 import sys 00059 def main(): 00060 ts._out(("next task", 1)) 00061 00062 ts._rd(("next task", LIMIT+1 )) 00063 00064 linda.universe._in(("ts", linda.TupleSpace)) 00065 print "finished" 00066 00067 linda.kernel.message_noreturn("kill_server") 00068 00069 if __name__ == "__main__": 00070 if len(sys.argv) > 1 and sys.argv[1] == "master": 00071 ts = linda.TupleSpace() 00072 linda.universe._out(("ts", ts)) 00073 else: 00074 ts = linda.universe._rd(("ts", linda.TupleSpace))[1] 00075 00076 print ts 00077 00078 if len(sys.argv) > 1 and sys.argv[1] == "master": 00079 main() 00080 else: 00081 is_prime() 00082 00083

PyLinda is © Copyright 2004 Andrew Wilkinson.

Generated on Thu Jan 13 14:12:30 2005 for PyLinda by doxygen 1.3.7