From a082040333d29d62260ec55933c69be7331f0606 Mon Sep 17 00:00:00 2001 From: cs123n Date: Sun, 29 May 2022 08:00:18 +0800 Subject: [PATCH] add readme --- .gitignore | 1 + input/func.py | 5 +++-- main.py | 1 + readme.md | 18 ++++++++++++++++++ requirements.txt | 1 + test/smooth.py | 35 ----------------------------------- test/test.py | 33 --------------------------------- test/test1.py | 20 -------------------- 8 files changed, 24 insertions(+), 90 deletions(-) create mode 100644 readme.md create mode 100644 requirements.txt delete mode 100644 test/smooth.py delete mode 100644 test/test.py delete mode 100644 test/test1.py diff --git a/.gitignore b/.gitignore index 0d20b64..d601571 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.pyc +test diff --git a/input/func.py b/input/func.py index 3b17b01..e0e9012 100644 --- a/input/func.py +++ b/input/func.py @@ -1,7 +1,8 @@ import torch as th def origin_func(x, y): - return - 2 * th.exp(x + y) #* (th.sin(x) * th.sin(y) - th.cos(x) * th.cos(y)) + # return - 2 * th.exp(x + y) * (th.cos(x) * th.cos(y) - th.sin(x) * th.sin(y)) + return - 2 * th.exp(x + y) * th.cos(x + y) def target_func(x, y): - return th.exp(x + y) #* th.cos(x) * th.sin(y) + return th.exp(x + y) * th.cos(x) * th.sin(y) diff --git a/main.py b/main.py index 2094670..c41f196 100644 --- a/main.py +++ b/main.py @@ -42,6 +42,7 @@ if rank == 0: print(u.view(w, w)[:8, :8]) + # if rank == 3: # print(u.view(w, w)[0:8, 0:8]) diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..74d1e67 --- /dev/null +++ b/readme.md @@ -0,0 +1,18 @@ +# MultiGrid Method + +Hi, everyone! This repo provides a parallel version of 2-D multigrid method based on Pytorch. + +Install necessary python packages: +```bash +pip install -r requirements.txt +``` + +Run single node version: +```bash +python main.py -n 64 -p 1 +``` + +Run multi-node version: +```bash +torchrun --nproc_per_node 4 main.py -n 64 -p 2 +``` \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..713f0b5 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +torch>=1.10.0 diff --git a/test/smooth.py b/test/smooth.py deleted file mode 100644 index 7584b11..0000000 --- a/test/smooth.py +++ /dev/null @@ -1,35 +0,0 @@ -import torch as th -import math - - -def separate(A): - return th.diag(A), -th.tril(A, -1), -th.triu(A, 1) - -def smooth(A, w=2.0/3, type='jacobi'): - D, L, U = separate(A) - if type == 'jacobi': - Di = (1 / D) - T, c_ = Di * (L + U), lambda b: Di * b - else: - T, c_ = None, None - - def method(x, b, m): - c = c_(b) - for _ in range(m): - x = w * (T @ x + c) + (1 - w) * x - return x - return method - - -if __name__ == "__main__": - from grid import tridiag - - n = 4 - A, b = tridiag(-1, 2, -1, n, device='cpu'), th.randn(n) - x = th.inverse(A) @ b - - smooth_method = smooth(A, w=2.0/3) - x_p = smooth_method(th.zeros(n), b, 10) - # print(x) - - diff --git a/test/test.py b/test/test.py deleted file mode 100644 index 4e3a65d..0000000 --- a/test/test.py +++ /dev/null @@ -1,33 +0,0 @@ -import torch as th -import argparse -import time - -from grid import laplace, condition -from multigrid import MultiGrid, FullMultiGrid -from input.func import origin_func, target_func -from smooth import smooth - - -if __name__ == "__main__": - parser = argparse.ArgumentParser() - # parser.add_argument('--n', '-n', type=int, default=16) - parser.add_argument('--device', type=str, default='cpu') - args = parser.parse_args() - - device = th.device(args.device) - e_list = [] - for n in (8, 16, 32, 64, 128): - - A = laplace(n, device)[n] - MG_method = MultiGrid(n, device) - b_method = condition(n, device) - b, u_t = b_method(origin_func, target_func) # condition(origin_func) - - u = th.zeros((n-1)**2, device=device) - for _ in range(n): - u = MG_method(u, b, n=n) - e_list.append(th.norm(u - u_t) / n) - - o_list = [th.log2(e_list[i]) - th.log2(e_list[i+1]) for i in range(len(e_list)-1)] - print(e_list) - print(o_list) \ No newline at end of file diff --git a/test/test1.py b/test/test1.py deleted file mode 100644 index 6232d81..0000000 --- a/test/test1.py +++ /dev/null @@ -1,20 +0,0 @@ -import torch as th -import torch.nn.functional as F -import torch.distributed as dist -import sys -import os - - -rank = os.environ.get("LOCAL_RANK") -rank = 0 if rank is None else int(rank) -dist.init_process_group(backend='gloo') - -s_list = th.tensor([1., 2.]) -s_list = [item.reshape(1, 1) for item in s_list] -s = th.zeros(1) -if rank == 0: - dist.scatter(s, s_list) -else: - dist.scatter(s) - -print(s)