Skip to content

Instantly share code, notes, and snippets.

@MeinLiX
Last active November 20, 2021 16:54
Show Gist options
  • Save MeinLiX/d2f9e982fa1d5a4645fbd1caf652fc50 to your computer and use it in GitHub Desktop.
Save MeinLiX/d2f9e982fa1d5a4645fbd1caf652fc50 to your computer and use it in GitHub Desktop.
Prime count solutions for PARCS
Після команди створення проекту, потрібно зайти на аккаунт клауда та підключити білінг до створеного проекту.
gcloud auth login;
gcloud projects create my-parcs;
gcloud projects list;
gcloud config set project my-parcs;
gcloud config set compute/zone europe-north1-a;
gcloud config set compute/region europe-north1;
gcloud compute firewall-rules create allow-all --direction=INGRESS --priority=1000 --network=default --action=ALLOW --rules=all --source-ranges=0.0.0.0/0;
gcloud compute instances create-with-container master --container-image=registry.hub.docker.com/hummer12007/parcs-node --container-env PARCS_ARGS="master";
gcloud compute instances create-with-container worker1 worker2 worker3 --container-image=registry.hub.docker.com/hummer12007/parcs-node --container-env PARCS_ARGS="worker 10.166.0.2"
from Pyro4 import expose
class Solver:
def __init__(self, workers=None, input_file_name=None, output_file_name=None):
self.input_file_name = input_file_name
self.output_file_name = output_file_name
self.workers = workers
print("Inited")
def solve(self):
print("Job Started")
print("Workers %d" % len(self.workers))
n_d = self.read_input()
n = int(n_d[0])
d = int(n_d[1])
start = n-d
step = (2*d) / len(self.workers)
mapped = []
lastElementI = len(self.workers) - 1
for i in xrange(0, lastElementI):
mapped.append(self.workers[i].mymap(
start + (i * step), start + (i * step + step)))
mapped.append(self.workers[lastElementI].mymap(
start + (lastElementI * step), n+d))
print('Map finished: ', mapped)
reduced = self.myreduce(mapped)
print("Reduce finished: " + str(reduced))
self.write_output(reduced)
print("Job Finished")
@staticmethod
@expose
def mymap(a, b):
res = 0
for i in xrange(a, b):
if Solver.isPrime(i):
res += 1
return res
@staticmethod
@expose
def myreduce(mapped):
output = 0
for x in mapped:
output += x.value
return output
def read_input(self):
f = open(self.input_file_name, 'r')
line = f.readline()
f.close()
return line.split(" ")
def write_output(self, output):
f = open(self.output_file_name, 'w')
f.write("Near prime count solution\n")
f.write("Calculate with " + str(len(self.workers)) + " nodes.\n")
f.write("Result:\n")
f.write(str(output))
f.write('\n')
f.close()
@staticmethod
@expose
def isPrime(a):
if (a == 0 or a == 1):
return 0
if (a == 2 or a == 3 or a == 5 or a == 7 or a == 11 or a == 13 or a == 17 or a == 19 or a == 23 or a == 29):
return 1
if (a % 2 == 0 or a % 3 == 0 or a % 5 == 0 or a % 7 == 0 or a % 11 == 0 or a % 13 == 0 or a % 17 == 0 or a % 19 == 0 or a % 23 == 0 or a % 29 == 0):
return 0
bound = a**0.5
i1 = 31
i2 = 37
i3 = 41
i4 = 43
i5 = 47
i6 = 49
i7 = 53
i8 = 59
while (i8 <= bound and a % i1 and a % i2 and a % i3 and a % i4 and a % i5 and a % i6 and a % i7 and a % i8):
i1 += 30
i2 += 30
i3 += 30
i4 += 30
i5 += 30
i6 += 30
i7 += 30
i8 += 30
if (i8 <= bound or
i1 <= bound and a % i1 == 0 or
i2 <= bound and a % i2 == 0 or
i3 <= bound and a % i3 == 0 or
i4 <= bound and a % i4 == 0 or
i5 <= bound and a % i5 == 0 or
i6 <= bound and a % i6 == 0 or
i7 <= bound and a % i7 == 0):
return 0
return 1
from Pyro4 import expose
class Solver:
def __init__(self, workers=None, input_file_name=None, output_file_name=None):
self.input_file_name = input_file_name
self.output_file_name = output_file_name
self.workers = workers
print("Inited")
def solve(self):
print("Job Started")
print("Workers %d" % len(self.workers))
n = self.read_input()
mapped = []
for i in xrange(0, len(self.workers)):
mapped.append(self.workers[i].mymap(i, len(self.workers), n))
print('Map finished: ', mapped)
reduced = self.myreduce(mapped)
print("Reduce finished: " + str(reduced))
self.write_output(reduced)
print("Job Finished")
@staticmethod
@expose
def mymap(number, count, n):
res = 0
for i in range(number, n, count):
if Solver.isPrime(i):
res += 1
return res
@staticmethod
@expose
def myreduce(mapped):
output = 0
for x in mapped:
output += x.value
return output
def read_input(self):
f = open(self.input_file_name, 'r')
line = f.readline()
f.close()
return int(line)
def write_output(self, output):
f = open(self.output_file_name, 'w')
f.write("Prime count modify cluster optimize solution\n")
f.write("Calculate with " + str(len(self.workers)) + " nodes.\n")
f.write("Result:\n")
f.write(str(output))
f.write('\n')
f.close()
@staticmethod
@expose
def isPrime(a):
if (a == 0 or a == 1):
return 0
if (a == 2 or a == 3 or a == 5 or a == 7 or a == 11 or a == 13 or a == 17 or a == 19 or a == 23 or a == 29):
return 1
if (a % 2 == 0 or a % 3 == 0 or a % 5 == 0 or a % 7 == 0 or a % 11 == 0 or a % 13 == 0 or a % 17 == 0 or a % 19 == 0 or a % 23 == 0 or a % 29 == 0):
return 0
bound = a**0.5
i1 = 31
i2 = 37
i3 = 41
i4 = 43
i5 = 47
i6 = 49
i7 = 53
i8 = 59
while (i8 <= bound and a % i1 and a % i2 and a % i3 and a % i4 and a % i5 and a % i6 and a % i7 and a % i8):
i1 += 30
i2 += 30
i3 += 30
i4 += 30
i5 += 30
i6 += 30
i7 += 30
i8 += 30
if (i8 <= bound or
i1 <= bound and a % i1 == 0 or
i2 <= bound and a % i2 == 0 or
i3 <= bound and a % i3 == 0 or
i4 <= bound and a % i4 == 0 or
i5 <= bound and a % i5 == 0 or
i6 <= bound and a % i6 == 0 or
i7 <= bound and a % i7 == 0):
return 0
return 1
from Pyro4 import expose
class Solver:
def __init__(self, workers=None, input_file_name=None, output_file_name=None):
self.input_file_name = input_file_name
self.output_file_name = output_file_name
self.workers = workers
print("Inited")
def solve(self):
print("Job Started")
print("Workers %d" % len(self.workers))
n = self.read_input()
step = n / len(self.workers)
mapped = []
lastElementI = len(self.workers) - 1
for i in xrange(0, lastElementI):
mapped.append(self.workers[i].mymap(i * step, i * step + step))
mapped.append(self.workers[lastElementI].mymap(lastElementI * step, n))
print('Map finished: ', mapped)
reduced = self.myreduce(mapped)
print("Reduce finished: " + str(reduced))
self.write_output(reduced)
print("Job Finished")
@staticmethod
@expose
def mymap(a, b):
res = 0
for i in xrange(a, b):
if Solver.isPrime(i):
res += 1
return res
@staticmethod
@expose
def myreduce(mapped):
output = 0
for x in mapped:
output += x.value
return output
def read_input(self):
f = open(self.input_file_name, 'r')
line = f.readline()
f.close()
return int(line)
def write_output(self, output):
f = open(self.output_file_name, 'w')
f.write("Prime count modify solution\n")
f.write("Calculate with " + str(len(self.workers)) + " nodes.\n")
f.write("Result:\n")
f.write(str(output))
f.write('\n')
f.close()
@staticmethod
@expose
def isPrime(a):
if (a == 0 or a == 1):
return 0
if (a == 2 or a == 3 or a == 5 or a == 7 or a == 11 or a == 13 or a == 17 or a == 19 or a == 23 or a == 29):
return 1
if (a % 2 == 0 or a % 3 == 0 or a % 5 == 0 or a % 7 == 0 or a % 11 == 0 or a % 13 == 0 or a % 17 == 0 or a % 19 == 0 or a % 23 == 0 or a % 29 == 0):
return 0
bound = a**0.5
i1 = 31
i2 = 37
i3 = 41
i4 = 43
i5 = 47
i6 = 49
i7 = 53
i8 = 59
while (i8 <= bound and a % i1 and a % i2 and a % i3 and a % i4 and a % i5 and a % i6 and a % i7 and a % i8):
i1 += 30
i2 += 30
i3 += 30
i4 += 30
i5 += 30
i6 += 30
i7 += 30
i8 += 30
if (i8 <= bound or
i1 <= bound and a % i1 == 0 or
i2 <= bound and a % i2 == 0 or
i3 <= bound and a % i3 == 0 or
i4 <= bound and a % i4 == 0 or
i5 <= bound and a % i5 == 0 or
i6 <= bound and a % i6 == 0 or
i7 <= bound and a % i7 == 0):
return 0
return 1
from Pyro4 import expose
class Solver:
def __init__(self, workers=None, input_file_name=None, output_file_name=None):
self.input_file_name = input_file_name
self.output_file_name = output_file_name
self.workers = workers
print("Inited")
def solve(self):
print("Job Started")
print("Workers %d" % len(self.workers))
n = self.read_input()
step = n / len(self.workers)
mapped = []
lastElementI = len(self.workers) - 1
for i in xrange(0, lastElementI):
mapped.append(self.workers[i].mymap(i * step, i * step + step))
mapped.append(self.workers[lastElementI].mymap(lastElementI * step, n))
print('Map finished: ', mapped)
reduced = self.myreduce(mapped)
print("Reduce finished: " + str(reduced))
self.write_output(reduced)
print("Job Finished")
@staticmethod
@expose
def mymap(a, b):
res = 0
for i in xrange(a, b):
if Solver.isPrime(i):
res += 1
return res
@staticmethod
@expose
def myreduce(mapped):
output = 0
for x in mapped:
output += x.value
return output
def read_input(self):
f = open(self.input_file_name, 'r')
line = f.readline()
f.close()
return int(line)
def write_output(self, output):
f = open(self.output_file_name, 'w')
f.write("Prime count solution\n")
f.write("Calculate with " + str(len(self.workers)) + " nodes.\n")
f.write("Result:\n")
f.write(str(output))
f.write('\n')
f.close()
@staticmethod
@expose
def isPrime(n):
if n == 2 or n == 3:
return True
if n % 2 == 0 or n < 2:
return False
for i in range(3, int(n**0.5) + 1, 2):
if n % i == 0:
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment