|
| 1 | +from multiprocessing import Pool |
| 2 | +import pyopencl as ocl |
| 3 | +import pyopencl.array |
| 4 | +import numpy as np |
| 5 | +import time |
| 6 | +from random import randint |
| 7 | + |
| 8 | +with open('../../gpu/opencl.cl') as f: |
| 9 | + cl_prg = ''.join(f.readlines()) |
| 10 | + |
| 11 | +class Matrix: |
| 12 | + |
| 13 | + def __init__(self): |
| 14 | + self.data = [[]] |
| 15 | + self.rows = 0 |
| 16 | + self.cols = 0 |
| 17 | + |
| 18 | + def print(self): |
| 19 | + for row in self.data: |
| 20 | + print(' '.join(str(c) for c in row) + ';\n') |
| 21 | + print(f'{self.rows}x{self.cols}') |
| 22 | + |
| 23 | + @staticmethod |
| 24 | + def __gen__(col): |
| 25 | + return [randint(0, 1000) for _ in range(col)] |
| 26 | + |
| 27 | + def resize(self, row , col): |
| 28 | + if self.rows != row or self.cols != col: |
| 29 | + with Pool() as pool: |
| 30 | + self.data = pool.map(self.__gen__, map(lambda r: col, range(row))) |
| 31 | + self.rows = row |
| 32 | + self.cols = col |
| 33 | + |
| 34 | + |
| 35 | +class Matrices: |
| 36 | + |
| 37 | + def __init__(self): |
| 38 | + self.a = Matrix() |
| 39 | + self.b = Matrix() |
| 40 | + |
| 41 | + def resize(self, np, m): |
| 42 | + self.a.resize(np, m) |
| 43 | + self.b.resize(m, np) |
| 44 | + |
| 45 | + |
| 46 | +class Stopwatch: |
| 47 | + |
| 48 | + def __init__(self): |
| 49 | + self.msgs = [] |
| 50 | + self.start = time.perf_counter() |
| 51 | + |
| 52 | + def lap(self, msg: str): |
| 53 | + t = time.perf_counter() |
| 54 | + duration = t - self.start |
| 55 | + self.start = t |
| 56 | + self.msgs.append(f'{msg}: {duration:.4f}s') |
| 57 | + |
| 58 | + def print(self): |
| 59 | + print('\n'.join(self.msgs)) |
| 60 | + |
| 61 | + |
| 62 | +def __row__(row, b): |
| 63 | + cl = [] |
| 64 | + for column in range(len(b[0])): |
| 65 | + s = 0 |
| 66 | + for i, otr in enumerate(b): |
| 67 | + s += row[i] * otr[column] |
| 68 | + cl.append(s) |
| 69 | + return cl |
| 70 | + |
| 71 | + |
| 72 | +def single(m: Matrices) -> [[int]]: |
| 73 | + """Multiplies using single core""" |
| 74 | + start = time.perf_counter() |
| 75 | + n = [] |
| 76 | + for row in m.a.data: |
| 77 | + n.append(__row__(row, m.b.data)) |
| 78 | + duration = time.perf_counter() - start |
| 79 | + print(f'Single-core: {duration:.4f}s') |
| 80 | + return n |
| 81 | + |
| 82 | + |
| 83 | +def multiple(m: Matrices) -> [[int]]: |
| 84 | + """Multiplies using multiple-core""" |
| 85 | + start = time.perf_counter() |
| 86 | + with Pool() as p: |
| 87 | + result = p.starmap(__row__, map(lambda row: (row, m.b.data), m.a.data)) |
| 88 | + duration = time.perf_counter() - start |
| 89 | + print(f'Multi-core: {duration:.4f}s') |
| 90 | + return result |
| 91 | + |
| 92 | + |
| 93 | +def opencl(m: Matrices, dev: ocl.Device) -> [[int]]: |
| 94 | + """Multiplies using OpenCL""" |
| 95 | + ctx = ocl.Context(devices=(dev,)) |
| 96 | + sw = Stopwatch() |
| 97 | + with ocl.CommandQueue(ctx) as q: |
| 98 | + a = ocl.array.to_device(q, np.array(m.a.data)) |
| 99 | + b = ocl.array.to_device(q, np.array(m.b.data)) |
| 100 | + s = ocl.array.Array(q, m.a.rows ** 2, np.int32) |
| 101 | + sw.lap('->GPU') |
| 102 | + |
| 103 | + prg = ocl.Program(ctx, cl_prg).build() |
| 104 | + prg.multiply(q, s.shape, None, |
| 105 | + np.int32(m.a.rows), np.int32(m.a.cols), np.int32(m.b.cols), |
| 106 | + a.data, b.data, s.data) |
| 107 | + s = s.reshape(m.a.rows, m.b.cols) |
| 108 | + q.finish() |
| 109 | + sw.lap('GPU compute') |
| 110 | + |
| 111 | + result = s.map_to_host().tolist() |
| 112 | + sw.lap('->CPU') |
| 113 | + sw.print() |
| 114 | + return result |
0 commit comments