commit 57ef9aa4ebdfa14db2a04997c0e16281df97b2a3 Author: PawelDopierala Date: Tue Nov 26 16:53:02 2024 +0100 add files diff --git a/computeC b/computeC new file mode 100644 index 0000000..9e27036 Binary files /dev/null and b/computeC differ diff --git a/create_servers.py b/create_servers.py new file mode 100644 index 0000000..4a5c49d --- /dev/null +++ b/create_servers.py @@ -0,0 +1,44 @@ +from hcloud import Client +from hcloud.images.domain import Image +from hcloud.server_types.domain import ServerType +from hcloud.locations.domain import Location + +TOKEN = "V5gkzZ13coCVPKWkQbmbyGPyxDdsTjiubwVtx35jH7mix8A32JqM5CWJtqoLjtFK" + +client = Client( + token=TOKEN +) +PREFIX = "PD" + +YOUR_LOCAL_SSH_PUBKEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCzIOEX3AABgr3ZKLFcvZYkKr10JJjD0+AXlXoJ5CfpNw56SVj17Gh/Qbag4QeB8eJYaJxHV+TPhIOtHNxDJFlcVXUKPCBDErlIFYIJ8t0uhqvssd9r6XCZxvDjTC20FNayG+Zr1adB3BYe501I3JB9KiGZxg9juh/lY/1jZyqjjPzt8vLE1M2j6C7rbLAlIOub37B8OC7SGsy/nz4Ub5kS3Jxh9Trmr6vtuXa5WnASIaqX+3jl9hBhsg55j5WKUrGM1BOEKmwYxFJEH7bY0NjmcNAeaw1mm5bxERSW6EFXX3hEk+kx5zuFbO1LXDBh3HP5FyjwOXDKY70aLfVVpdGj" + +ssh_key = client.ssh_keys.create(name=f"{PREFIX}-pzc-ssh-key", public_key=YOUR_LOCAL_SSH_PUBKEY) +print(f"Klucz {ssh_key.data_model.name} został dodany: {ssh_key.data_model.public_key}") + +cloud_init_db=r'''#cloud-config +runcmd: + - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - + - add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" + - apt-get update -y + - apt-get install -y docker-ce docker-ce-cli containerd.io + - curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose + - chmod +x /usr/local/bin/docker-compose + - systemctl start docker + - systemctl enable docker + - cd /root/ && docker-compose up -d +''' + +db_server = client.servers.create( + name=f"{PREFIX}-db", + server_type=ServerType("cpx11"), + image=Image(name="ubuntu-22.04"), + ssh_keys=[ssh_key], + location=Location("hel1"), + user_data=cloud_init_db, +) + +db_server.action.wait_until_finished() +print(f"Tworzenie serwera db: {db_server.action.complete}") + +db_server = client.servers.get_by_name(f"{PREFIX}-db") +print(f"Serwer: {db_server.data_model.name}\n\tpubliczne IP: {db_server.data_model.public_net.ipv4.ip}") diff --git a/main.py b/main.py new file mode 100644 index 0000000..ea93f80 --- /dev/null +++ b/main.py @@ -0,0 +1,29 @@ +from fastapi import FastAPI +from pydantic import BaseModel +import subprocess +import uvicorn + +app = FastAPI() + + +class ComputeInput(BaseModel): + a: str + b: str + sigma: str + + +@app.post("/compute") +async def compute(data: ComputeInput): + a = data.a + b = data.b + sigma = data.sigma + + result = subprocess.run( + ['./computeC', a, b, sigma], + capture_output=True, + text=True + ) + return result.stdout.strip() + +if __name__ == "__main__": + uvicorn.run(app, host="0.0.0.0", port=5000) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7561b50 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +fastapi==0.100.0 +uvicorn[standard]==0.23.2 +pydantic==1.10.9 \ No newline at end of file diff --git a/run.py b/run.py new file mode 100644 index 0000000..5bc95a0 --- /dev/null +++ b/run.py @@ -0,0 +1,30 @@ +import subprocess +from concurrent.futures import ThreadPoolExecutor + +MODULO = 999999937 + +# Funkcja do wywołania ./computeC +def run_computeC(a, b, sigma): + result = subprocess.run(["./computeC", str(a), str(b), str(sigma)], + capture_output=True, text=True) + if result.returncode != 0: + raise RuntimeError(f"Error running computeC: {result.stderr}") + return int(result.stdout.strip()) % MODULO + +# Główna funkcja +def parallel_calculate_C_sigma(a_start, b_end, sigma, num_chunks): + chunk_size = (b_end - a_start + 1) // num_chunks + ranges = [(a_start + i * chunk_size, min(a_start + (i + 1) * chunk_size - 1, b_end)) + for i in range(num_chunks)] + + with ThreadPoolExecutor() as executor: + futures = [executor.submit(run_computeC, a, b, sigma) for a, b in ranges] + results = [f.result() for f in futures] + + return sum(results) % MODULO + +# Parametry +a_start, b_end, sigma = 1, 10000, 42 +num_chunks = 32 # Liczba podziałów równoległych +result = parallel_calculate_C_sigma(a_start, b_end, sigma, num_chunks) +print(f"C_sigma({a_start}, {b_end}) = {result}")