|
| 1 | +import numpy as np |
| 2 | +from numba import cuda |
| 3 | + |
| 4 | +import mpi4py |
| 5 | + |
| 6 | +mpi4py.rc.initialize = False |
| 7 | +mpi4py.rc.finalize = False |
| 8 | +from mpi4py import MPI |
| 9 | + |
| 10 | +from matmul.utils import create_block, read_config |
| 11 | +import argparse |
| 12 | +import importlib |
| 13 | + |
| 14 | +from line_profiler import profile |
| 15 | + |
| 16 | +@profile |
| 17 | +def main_cpu(params: dict): |
| 18 | + SIZE = params["size"] |
| 19 | + md = importlib.import_module("matmul") |
| 20 | + routine = getattr(md, params["function"]["routine"]) |
| 21 | + bs = params["function"]["block_size"] |
| 22 | + |
| 23 | + # Initialise MPI with multithreading enabled and share work among processes |
| 24 | + status = MPI.Init_thread(MPI.THREAD_FUNNELED) |
| 25 | + if status != MPI.THREAD_FUNNELED: |
| 26 | + print("Unable to provide required thread level") |
| 27 | + |
| 28 | + comm = MPI.COMM_WORLD |
| 29 | + rank = comm.Get_rank() |
| 30 | + npes = comm.Get_size() |
| 31 | + |
| 32 | + rest = SIZE%npes |
| 33 | + n_loc = SIZE//npes + (rank < rest) |
| 34 | + |
| 35 | + workloads = np.array([SIZE//npes + (i<rest) for i in range(npes)], dtype=int) |
| 36 | + |
| 37 | + row_offset = np.cumsum(workloads)[rank-1] if rank > 0 else 0 |
| 38 | + |
| 39 | + # initialise matrices somehow |
| 40 | + A = np.arange(1, SIZE*n_loc + 1, dtype=np.float64).reshape((n_loc,SIZE)) + (row_offset * SIZE) |
| 41 | + B = np.zeros((n_loc,SIZE), dtype=np.float64) |
| 42 | + C = np.zeros((n_loc,SIZE), dtype=np.float64) |
| 43 | + for i in range(n_loc): |
| 44 | + B[i, i+row_offset] = 1 |
| 45 | + |
| 46 | + # Compute quantities for Allgatherv and allocate required memory |
| 47 | + ncols = workloads[0] |
| 48 | + rcvcounts = workloads*ncols |
| 49 | + displacements = np.cumsum(rcvcounts) - rcvcounts |
| 50 | + |
| 51 | + B_block = np.empty((n_loc,ncols), dtype=np.float64) |
| 52 | + B_col = np.empty((SIZE,ncols), dtype=np.float64) |
| 53 | + |
| 54 | + t_tot = 0 |
| 55 | + start = 0 |
| 56 | + for i in range(npes): |
| 57 | + # Recompute stuff for Algatherv at some point if needed (because of different workloads) |
| 58 | + if i == rest: |
| 59 | + ncols = workloads[i] |
| 60 | + rcvcounts = workloads*ncols |
| 61 | + displacements = np.cumsum(rcvcounts) - rcvcounts |
| 62 | + |
| 63 | + B_block = np.empty((n_loc,ncols), dtype=np.float64) |
| 64 | + B_col = np.empty((SIZE,ncols), dtype=np.float64) |
| 65 | + |
| 66 | + # create a contiguous block from B to communicate |
| 67 | + create_block(B, B_block, start, ncols) |
| 68 | + # gather all pieces of B from other processes |
| 69 | + comm.Allgatherv([B_block, MPI.DOUBLE], [B_col, rcvcounts,displacements, MPI.DOUBLE]) |
| 70 | + |
| 71 | + t1 = MPI.Wtime() |
| 72 | + # multiply |
| 73 | + routine(A,B_col,C[:,start:start+ncols],bs) |
| 74 | + t2 = MPI.Wtime() |
| 75 | + t_tot += (t2-t1) |
| 76 | + |
| 77 | + start += ncols |
| 78 | + |
| 79 | + print(t_tot) |
| 80 | + |
| 81 | + if params["print"]: |
| 82 | + if rank == 0: |
| 83 | + print(C) |
| 84 | + for i in range(1,npes): |
| 85 | + block = np.zeros((workloads[i], SIZE)) |
| 86 | + block = comm.recv(source=i,tag=i) |
| 87 | + print(block) |
| 88 | + else: |
| 89 | + comm.send(C,dest=0,tag=rank) |
| 90 | + |
| 91 | + |
| 92 | + MPI.Finalize() |
| 93 | + |
| 94 | +@profile |
| 95 | +def main_gpu(params: dict): |
| 96 | + SIZE = params["size"] |
| 97 | + md = importlib.import_module("matmul") |
| 98 | + routine = getattr(md, params["function"]["routine"]) |
| 99 | + bs = params["function"]["block_size"] |
| 100 | + |
| 101 | + # Initialise MPI with multithreading enabled and share work among processes |
| 102 | + status = MPI.Init_thread(MPI.THREAD_FUNNELED) |
| 103 | + if status != MPI.THREAD_FUNNELED: |
| 104 | + print("Unable to provide required thread level") |
| 105 | + |
| 106 | + comm = MPI.COMM_WORLD |
| 107 | + rank = comm.Get_rank() |
| 108 | + npes = comm.Get_size() |
| 109 | + |
| 110 | + rest = SIZE%npes |
| 111 | + n_loc = SIZE//npes + (rank < rest) |
| 112 | + |
| 113 | + workloads = np.array([SIZE//npes + (i<rest) for i in range(npes)], dtype=int) |
| 114 | + |
| 115 | + row_offset = np.cumsum(workloads)[rank-1] if rank > 0 else 0 |
| 116 | + |
| 117 | + # initialise matrices somehow |
| 118 | + A = np.arange(1, SIZE*n_loc + 1, dtype=np.float64).reshape((n_loc,SIZE)) + (row_offset * SIZE) |
| 119 | + B = np.zeros((n_loc,SIZE), dtype=np.float64) |
| 120 | + C = np.zeros((n_loc,SIZE), dtype=np.float64) |
| 121 | + for i in range(n_loc): |
| 122 | + B[i, i+row_offset] = 1 |
| 123 | + |
| 124 | + # Compute quantities for Allgatherv and allocate required memory |
| 125 | + ncols = workloads[0] |
| 126 | + rcvcounts = workloads*ncols |
| 127 | + displacements = np.cumsum(rcvcounts) - rcvcounts |
| 128 | + |
| 129 | + B_block = np.empty((n_loc,ncols), dtype=np.float64) |
| 130 | + B_col = np.empty((SIZE,ncols), dtype=np.float64) |
| 131 | + |
| 132 | + # Select a GPU and move arrays to device |
| 133 | + num_devices = len(cuda.gpus) |
| 134 | + cuda.select_device(rank%num_devices) |
| 135 | + a_d = cuda.to_device(A) |
| 136 | + c_d = cuda.to_device(C) |
| 137 | + |
| 138 | + nthreads = bs |
| 139 | + blocks_per_grid = ((n_loc + nthreads-1)//nthreads,(SIZE + nthreads-1)//nthreads) |
| 140 | + threads_per_block = (nthreads, nthreads) |
| 141 | + |
| 142 | + t_tot = 0 |
| 143 | + start = 0 |
| 144 | + for i in range(npes): |
| 145 | + # Recompute stuff for Algatherv at some point if needed (because of different workloads) |
| 146 | + if i == rest: |
| 147 | + ncols = workloads[i] |
| 148 | + rcvcounts = workloads*ncols |
| 149 | + displacements = np.cumsum(rcvcounts) - rcvcounts |
| 150 | + |
| 151 | + B_block = np.empty((n_loc,ncols), dtype=np.float64) |
| 152 | + B_col = np.empty((SIZE,ncols), dtype=np.float64) |
| 153 | + |
| 154 | + # create a contiguous block from B to communicate |
| 155 | + create_block(B, B_block, start, ncols) |
| 156 | + # gather all pieces of B from other processes |
| 157 | + comm.Allgatherv([B_block, MPI.DOUBLE], [B_col, rcvcounts,displacements, MPI.DOUBLE]) |
| 158 | + |
| 159 | + # move slice of B to device |
| 160 | + b_d = cuda.to_device(B_col) |
| 161 | + |
| 162 | + t1 = cuda.event(timing=True) |
| 163 | + t2 = cuda.event(timing=True) |
| 164 | + t1.record() |
| 165 | + # multiply |
| 166 | + routine[blocks_per_grid, threads_per_block](a_d,b_d,c_d[:,start:start+ncols]) |
| 167 | + t2.record() |
| 168 | + t2.synchronize() |
| 169 | + |
| 170 | + t_tot += (cuda.event_elapsed_time(t1,t2)/1000) |
| 171 | + |
| 172 | + start += ncols |
| 173 | + # move final result back to host |
| 174 | + C = c_d.copy_to_host() |
| 175 | + |
| 176 | + print(t_tot) |
| 177 | + |
| 178 | + if params["print"]: |
| 179 | + if rank == 0: |
| 180 | + print(C) |
| 181 | + for i in range(1,npes): |
| 182 | + block = np.zeros((workloads[i], SIZE)) |
| 183 | + block = comm.recv(source=i,tag=i) |
| 184 | + print(block) |
| 185 | + else: |
| 186 | + comm.send(C,dest=0,tag=rank) |
| 187 | + |
| 188 | + |
| 189 | + MPI.Finalize() |
| 190 | + |
| 191 | + |
| 192 | +cpu_routines = ['matmul', |
| 193 | + 'matmul_numba_serial', |
| 194 | + 'matmul_numba_cpu', |
| 195 | + 'matmul_numba_block_serial', |
| 196 | + 'matmul_numba_block_cpu'] |
| 197 | + |
| 198 | +gpu_routines = ['matmul_numba_gpu', |
| 199 | + 'matmul_numba_block_gpu'] |
| 200 | + |
| 201 | +if __name__=="__main__": |
| 202 | + parser = argparse.ArgumentParser() |
| 203 | + parser.add_argument("--config", type=str, help="Path to the config yaml file") |
| 204 | + parser = parser.parse_args() |
| 205 | + |
| 206 | + if not parser.config: |
| 207 | + raise RuntimeError("Please specify a yaml config file with `--config <filename>`.") |
| 208 | + params = read_config(parser.config) |
| 209 | + routine = params["function"]["routine"] |
| 210 | + |
| 211 | + if params["device"] == "cpu" : |
| 212 | + if not routine in cpu_routines: |
| 213 | + raise ValueError(f"Specified routine '{routine}' is incompatible with device 'cpu'. Compatible routines are {cpu_routines}.") |
| 214 | + main_cpu(params) |
| 215 | + elif params["device"] == "gpu" : |
| 216 | + if not routine in gpu_routines: |
| 217 | + raise ValueError(f"Specified routine '{routine}' is incompatible with device 'gpu'. Compatible routines are {gpu_routines}.") |
| 218 | + main_gpu(params) |
| 219 | + else: |
| 220 | + raise ValueError(f"Parameter `device` can be either 'cpu' or 'gpu', instead got {params['device']}.") |
0 commit comments