-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel.lisp
More file actions
71 lines (51 loc) · 1.96 KB
/
parallel.lisp
File metadata and controls
71 lines (51 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
;; Author: Seth M. Fuller
;; Date: 10/22/2018
;; File: multiplyMatricesParallel.lisp
;; Import packages
(ql:quickload :array-operations)
(ql:quickload :lparallel)
;; Create the worker threads
(setf lparallel:*kernel* (lparallel:make-kernel 8))
;; Create a matrix with random values
(defun InitializeMatrix (rows columns)
"Creates a matrix of size rows x columns"
(aops:generate (lambda () (random 10)) '(200 200))
)
(defun MultiplyRow (matrix1RowIndex matrix2ColumnIndex)
"Multiply the matrix1 row at the specified index by
the matrix2 column at the specified index"
(setq sum 0)
(loop for i from 0 below (length (aops:sub matrix1 matrix1RowIndex))
do (setf sum (+ sum (* (aref (aops:sub matrix1 matrix1RowIndex) i) (aref (aops:sub matrix2 matrix2ColumnIndex) i)))))
(setf (aref newMatrix matrix1RowIndex matrix2ColumnIndex) sum)
)
;; Multiply two matrices together
(defun MultiplyMatrices ()
"Multiplies two matrices"
;; Get the dimensions of matrix 1
(destructuring-bind (n m) (array-dimensions matrix1)
(setq matrix1Rows n)
(setq matrix1Columns m)
)
;; Get the dimensions of matrix 2
(destructuring-bind (n m) (array-dimensions matrix2)
(setq matrix2Columns m)
(setq matrix2Rows n)
)
;; Determine dimensions of new matrix
(defvar newMatrixRows matrix2Columns)
(defvar newMatrixColumns matrix1Rows)
;; Create an empty new matrix will with nil values
(defvar newMatrix (aops:generate (lambda () ()) '(200 200)))
;; Loop through ever row and multiply
(time (lparallel:pdotimes (i matrix1Rows)
(dotimes (j matrix2Columns)
(MultiplyRow i j))))
;; Go https://lispcookbook.github.io/cl-cookbook/process.html to find more info on parallel functions
)
;; Create two globally scoped matrices
(defvar matrix1 (InitializeMatrix 1 2))
(defvar matrix2 (InitializeMatrix 2 1))
;; Multiply two matricies in parallel
(MultiplyMatrices)
(EXIT)