0% found this document useful (0 votes)
15 views

Compute Fibonnaci in Prolog and Using Parallel Computation

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Compute Fibonnaci in Prolog and Using Parallel Computation

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Problem 1: Fibonnaci in Prolog and parallel computation.

Tested on Windows 10 Home (Latest Edition, x64) using SWI-Prolog (Latest Edition). I have 4
physical cores and hyperthreading enable.

M = 4 * 1.3 = 5.2
N = 3000

THREADS Execution time [Relative] Speedup [Relative] Efficiency


[milliseconds] S(n) = T(1) / T(n) E(n) = S(n) / M
1 321 1 0.192307692
2 226 1.420353982 0.273144997
3 176 1.823863636 0.350743007
4 118 2.720338983 0.523142112
5 108 2.972222222 0.571581197
6 126 2.547619048 0.48992674
7 148 2.168918919 0.417099792
8 116 2.767241379 0.532161804
9 117 2.743589744 0.527613412
10 102 3.147058824 0.60520362
11 119 2.697478992 0.51874596
12 108 2.972222222 0.571581197
13 112 2.866071429 0.551167582
14 106 3.028301887 0.582365747
15 116 2.767241379 0.532161804
16 105 3.057142857 0.587912088

TABLE 1. Performance parameters for


Problem 1
F(1000) =
434664570963780983380972905483160364181752881805341183830
520934425829740230415891445903668345575357107020591524656
968529873354576721830545614595578218627409341207413885538
39523117273229343745719962537990724519
F(2000) =
422467676561192147344716124184867733861526690217496837529
069111260390121695541950536334680582118417566866031502394
202181561670490508193924378742126025920275595053997499274
293177639241560414545136950375932622604631331036492513882
847396994056478597452442291141760742508781062191414897154
510698220520115848459127914441629827360100706854234572232
329600908819382967594612527945180669470051923826153666787
543040164637067794
F(3000) =
410613032811188241819363176371960361434211937126877044080
312407593047527412243034520966697493447900786647724488599
177028619470849348366423752241359662696155787401349469308
559467478754982144167917997828121308964978541772895802495
725266448355421566231375033670698754724776203487425062762
665276232311985391316635716545560778233354951240570858377
376394769370800605574542492263350445805508278925754365718
693382035065546850500976885319222420148436277017254848384
919004016431071589553417141866166776265221415220109871326
899351423543017288099748619778878121650093477280546798645
912700295736115394043836651988865453767798802400690814051
1 %%Formula: (((B + A) ^ n - (B - A) ^ n) * B) / (2 * B) ^ n * A
2
3 forLoop(N, Index, _) :-
4 Index > N,
5 writeln("Done"),
6 !.
7 forLoop(N, Index, Increment) :-
8 binetCheck(Index, Fib),
9 %% betterBinet(Index, Fib),
10
11 %% writeln(Fib),
12
13 NewIndex is Index + Increment,
14 assert(fib(Index, Fib)),
15
16 forLoop(N, NewIndex, Increment).
17
18 binetCheck(N, Fib) :-
19 nth_integer_root_and_remainder(2, 50000000000000000, A, _),
20 B is 100000000,
21 SUS is (((B + A) ^ N - (B - A) ^ N) * B),
22 JOS is (2 * B) ^ N * A,
23 Fib is SUS // JOS.
24
25 %%Formula: (((2 * B) * ((B + A) ^ n)) + (A * B)) / (4 * A * B)
26 betterBinet(N, Fib) :-
27 nth_integer_root_and_remainder(2, 50000, A, _),
28 B is 100,
29 SUS is ((2 * B) * ((B + A) ^ N)) + (A * B),
30 JOS is (4 * A * B),
31 Fib is SUS // JOS.
32
33
34 timing(T, N, NO_THREADS) :-
35 statistics(walltime, [TimeSinceStart | [TimeSinceLastCall]]),
36
37 howManyThreads(N, NO_THREADS, NO_THREADS),
38
39 statistics(walltime, [NewTimeSinceStart | [ExecutionTime]]),
40
41 listing(fib(_, _)),
42
43 retractall(fib(_,_)),
44
45 write('Execution took '),
46 write(ExecutionTime),
47 write(' ms.'),
48 nl.
49
50
51 howManyThreads(N, NO_THREADS, 1) :-
52 thread_create(forLoop(N, 1, NO_THREADS), Id1, []),
53 thread_join(Id1, _),
54 !.
55
56 howManyThreads(N, NO_THREADS, 2) :-
57 thread_create(forLoop(N, 1, NO_THREADS), Id1, []),
58 thread_create(forLoop(N, 2, NO_THREADS), Id2, []),
59 thread_join(Id1, _),
60 thread_join(Id2, _),
61 !.
62
63 howManyThreads(N, NO_THREADS, 3) :-
64 thread_create(forLoop(N, 1, NO_THREADS), Id1, []),
65 thread_create(forLoop(N, 2, NO_THREADS), Id2, []),
66 thread_create(forLoop(N, 3, NO_THREADS), Id3, []),
67 thread_join(Id1, _),
68 thread_join(Id2, _),
69 thread_join(Id3, _),
70 !.
71
72 howManyThreads(N, NO_THREADS, 4) :-
73 thread_create(forLoop(N, 1, NO_THREADS), Id1, []),
74 thread_create(forLoop(N, 2, NO_THREADS), Id2, []),
75 thread_create(forLoop(N, 3, NO_THREADS), Id3, []),
76 thread_create(forLoop(N, 4, NO_THREADS), Id4, []),
77 thread_join(Id1, _),
78 thread_join(Id2, _),
79 thread_join(Id3, _),
80 thread_join(Id4, _),
81 !.
82
83 howManyThreads(N, NO_THREADS, 5) :-
84 thread_create(forLoop(N, 1, NO_THREADS), Id1, []),
85 thread_create(forLoop(N, 2, NO_THREADS), Id2, []),
86 thread_create(forLoop(N, 3, NO_THREADS), Id3, []),
87 thread_create(forLoop(N, 4, NO_THREADS), Id4, []),
88 thread_create(forLoop(N, 5, NO_THREADS), Id5, []),
89 thread_join(Id1, _),
90 thread_join(Id2, _),
91 thread_join(Id3, _),
92 thread_join(Id4, _),
93 thread_join(Id5, _),
94 !.
95
96 howManyThreads(N, NO_THREADS, 6) :-
97 thread_create(forLoop(N, 1, NO_THREADS), Id1, []),
98 thread_create(forLoop(N, 2, NO_THREADS), Id2, []),
99 thread_create(forLoop(N, 3, NO_THREADS), Id3, []),
100 thread_create(forLoop(N, 4, NO_THREADS), Id4, []),
101 thread_create(forLoop(N, 5, NO_THREADS), Id5, []),
102 thread_create(forLoop(N, 6, NO_THREADS), Id6, []),
103 thread_join(Id1, _),
104 thread_join(Id2, _),
105 thread_join(Id3, _),
106 thread_join(Id4, _),
107 thread_join(Id5, _),
108 thread_join(Id6, _),
109 !.
110
111 howManyThreads(N, NO_THREADS, 7) :-
112 thread_create(forLoop(N, 1, NO_THREADS), Id1, []),
113 thread_create(forLoop(N, 2, NO_THREADS), Id2, []),
114 thread_create(forLoop(N, 3, NO_THREADS), Id3, []),
115 thread_create(forLoop(N, 4, NO_THREADS), Id4, []),
116 thread_create(forLoop(N, 5, NO_THREADS), Id5, []),
117 thread_create(forLoop(N, 6, NO_THREADS), Id6, []),
118 thread_create(forLoop(N, 7, NO_THREADS), Id7, []),
119 thread_join(Id1, _),
120 thread_join(Id2, _),
121 thread_join(Id3, _),
122 thread_join(Id4, _),
123 thread_join(Id5, _),
124 thread_join(Id6, _),
125 thread_join(Id7, _),
126 !.
127
128 howManyThreads(N, NO_THREADS, 8) :-
129 thread_create(forLoop(N, 1, NO_THREADS), Id1, []),
130 thread_create(forLoop(N, 2, NO_THREADS), Id2, []),
131 thread_create(forLoop(N, 3, NO_THREADS), Id3, []),
132 thread_create(forLoop(N, 4, NO_THREADS), Id4, []),
133 thread_create(forLoop(N, 5, NO_THREADS), Id5, []),
134 thread_create(forLoop(N, 6, NO_THREADS), Id6, []),
135 thread_create(forLoop(N, 7, NO_THREADS), Id7, []),
136 thread_create(forLoop(N, 8, NO_THREADS), Id8, []),
137 thread_join(Id1, _),
138 thread_join(Id2, _),
139 thread_join(Id3, _),
140 thread_join(Id4, _),
141 thread_join(Id5, _),
142 thread_join(Id6, _),
143 thread_join(Id7, _),
144 thread_join(Id8, _),
145 !.
146
147 howManyThreads(N, NO_THREADS, 9) :-
148 thread_create(forLoop(N, 1, NO_THREADS), Id1, []),
149 thread_create(forLoop(N, 2, NO_THREADS), Id2, []),
150 thread_create(forLoop(N, 3, NO_THREADS), Id3, []),
151 thread_create(forLoop(N, 4, NO_THREADS), Id4, []),
152 thread_create(forLoop(N, 5, NO_THREADS), Id5, []),
153 thread_create(forLoop(N, 6, NO_THREADS), Id6, []),
154 thread_create(forLoop(N, 7, NO_THREADS), Id7, []),
155 thread_create(forLoop(N, 8, NO_THREADS), Id8, []),
156 thread_create(forLoop(N, 9, NO_THREADS), Id9, []),
157 thread_join(Id1, _),
158 thread_join(Id2, _),
159 thread_join(Id3, _),
160 thread_join(Id4, _),
161 thread_join(Id5, _),
162 thread_join(Id6, _),
163 thread_join(Id7, _),
164 thread_join(Id8, _),
165 thread_join(Id9, _),
166 !.
167
168 howManyThreads(N, NO_THREADS, 10) :-
169 thread_create(forLoop(N, 1, NO_THREADS), Id1, []),
170 thread_create(forLoop(N, 2, NO_THREADS), Id2, []),
171 thread_create(forLoop(N, 3, NO_THREADS), Id3, []),
172 thread_create(forLoop(N, 4, NO_THREADS), Id4, []),
173 thread_create(forLoop(N, 5, NO_THREADS), Id5, []),
174 thread_create(forLoop(N, 6, NO_THREADS), Id6, []),
175 thread_create(forLoop(N, 7, NO_THREADS), Id7, []),
176 thread_create(forLoop(N, 8, NO_THREADS), Id8, []),
177 thread_create(forLoop(N, 9, NO_THREADS), Id9, []),
178 thread_create(forLoop(N, 10, NO_THREADS), Id10, []),
179 thread_join(Id1, _),
180 thread_join(Id2, _),
181 thread_join(Id3, _),
182 thread_join(Id4, _),
183 thread_join(Id5, _),
184 thread_join(Id6, _),
185 thread_join(Id7, _),
186 thread_join(Id8, _),
187 thread_join(Id9, _),
188 thread_join(Id10, _),
189 !.
190
191 howManyThreads(N, NO_THREADS, 11) :-
192 thread_create(forLoop(N, 1, NO_THREADS), Id1, []),
193 thread_create(forLoop(N, 2, NO_THREADS), Id2, []),
194 thread_create(forLoop(N, 3, NO_THREADS), Id3, []),
195 thread_create(forLoop(N, 4, NO_THREADS), Id4, []),
196 thread_create(forLoop(N, 5, NO_THREADS), Id5, []),
197 thread_create(forLoop(N, 6, NO_THREADS), Id6, []),
198 thread_create(forLoop(N, 7, NO_THREADS), Id7, []),
199 thread_create(forLoop(N, 8, NO_THREADS), Id8, []),
200 thread_create(forLoop(N, 9, NO_THREADS), Id9, []),
201 thread_create(forLoop(N, 10, NO_THREADS), Id10, []),
202 thread_create(forLoop(N, 11, NO_THREADS), Id11, []),
203 thread_join(Id1, _),
204 thread_join(Id2, _),
205 thread_join(Id3, _),
206 thread_join(Id4, _),
207 thread_join(Id5, _),
208 thread_join(Id6, _),
209 thread_join(Id7, _),
210 thread_join(Id8, _),
211 thread_join(Id9, _),
212 thread_join(Id10, _),
213 thread_join(Id11, _),
214 !.
215
216 howManyThreads(N, NO_THREADS, 12) :-
217 thread_create(forLoop(N, 1, NO_THREADS), Id1, []),
218 thread_create(forLoop(N, 2, NO_THREADS), Id2, []),
219 thread_create(forLoop(N, 3, NO_THREADS), Id3, []),
220 thread_create(forLoop(N, 4, NO_THREADS), Id4, []),
221 thread_create(forLoop(N, 5, NO_THREADS), Id5, []),
222 thread_create(forLoop(N, 6, NO_THREADS), Id6, []),
223 thread_create(forLoop(N, 7, NO_THREADS), Id7, []),
224 thread_create(forLoop(N, 8, NO_THREADS), Id8, []),
225 thread_create(forLoop(N, 9, NO_THREADS), Id9, []),
226 thread_create(forLoop(N, 10, NO_THREADS), Id10, []),
227 thread_create(forLoop(N, 11, NO_THREADS), Id11, []),
228 thread_create(forLoop(N, 12, NO_THREADS), Id12, []),
229 thread_join(Id1, _),
230 thread_join(Id2, _),
231 thread_join(Id3, _),
232 thread_join(Id4, _),
233 thread_join(Id5, _),
234 thread_join(Id6, _),
235 thread_join(Id7, _),
236 thread_join(Id8, _),
237 thread_join(Id9, _),
238 thread_join(Id10, _),
239 thread_join(Id11, _),
240 thread_join(Id12, _),
241 !.
242
243 howManyThreads(N, NO_THREADS, 13) :-
244 thread_create(forLoop(N, 1, NO_THREADS), Id1, []),
245 thread_create(forLoop(N, 2, NO_THREADS), Id2, []),
246 thread_create(forLoop(N, 3, NO_THREADS), Id3, []),
247 thread_create(forLoop(N, 4, NO_THREADS), Id4, []),
248 thread_create(forLoop(N, 5, NO_THREADS), Id5, []),
249 thread_create(forLoop(N, 6, NO_THREADS), Id6, []),
250 thread_create(forLoop(N, 7, NO_THREADS), Id7, []),
251 thread_create(forLoop(N, 8, NO_THREADS), Id8, []),
252 thread_create(forLoop(N, 9, NO_THREADS), Id9, []),
253 thread_create(forLoop(N, 10, NO_THREADS), Id10, []),
254 thread_create(forLoop(N, 11, NO_THREADS), Id11, []),
255 thread_create(forLoop(N, 12, NO_THREADS), Id12, []),
256 thread_create(forLoop(N, 13, NO_THREADS), Id13, []),
257 thread_join(Id1, _),
258 thread_join(Id2, _),
259 thread_join(Id3, _),
260 thread_join(Id4, _),
261 thread_join(Id5, _),
262 thread_join(Id6, _),
263 thread_join(Id7, _),
264 thread_join(Id8, _),
265 thread_join(Id9, _),
266 thread_join(Id10, _),
267 thread_join(Id11, _),
268 thread_join(Id12, _),
269 thread_join(Id13, _),
270 !.
271
272 howManyThreads(N, NO_THREADS, 14) :-
273 thread_create(forLoop(N, 1, NO_THREADS), Id1, []),
274 thread_create(forLoop(N, 2, NO_THREADS), Id2, []),
275 thread_create(forLoop(N, 3, NO_THREADS), Id3, []),
276 thread_create(forLoop(N, 4, NO_THREADS), Id4, []),
277 thread_create(forLoop(N, 5, NO_THREADS), Id5, []),
278 thread_create(forLoop(N, 6, NO_THREADS), Id6, []),
279 thread_create(forLoop(N, 7, NO_THREADS), Id7, []),
280 thread_create(forLoop(N, 8, NO_THREADS), Id8, []),
281 thread_create(forLoop(N, 9, NO_THREADS), Id9, []),
282 thread_create(forLoop(N, 10, NO_THREADS), Id10, []),
283 thread_create(forLoop(N, 11, NO_THREADS), Id11, []),
284 thread_create(forLoop(N, 12, NO_THREADS), Id12, []),
285 thread_create(forLoop(N, 13, NO_THREADS), Id13, []),
286 thread_create(forLoop(N, 14, NO_THREADS), Id14, []),
287 thread_join(Id1, _),
288 thread_join(Id2, _),
289 thread_join(Id3, _),
290 thread_join(Id4, _),
291 thread_join(Id5, _),
292 thread_join(Id6, _),
293 thread_join(Id7, _),
294 thread_join(Id8, _),
295 thread_join(Id9, _),
296 thread_join(Id10, _),
297 thread_join(Id11, _),
298 thread_join(Id12, _),
299 thread_join(Id13, _),
300 thread_join(Id14, _),
301 !.
302
303 howManyThreads(N, NO_THREADS, 15) :-
304 thread_create(forLoop(N, 1, NO_THREADS), Id1, []),
305 thread_create(forLoop(N, 2, NO_THREADS), Id2, []),
306 thread_create(forLoop(N, 3, NO_THREADS), Id3, []),
307 thread_create(forLoop(N, 4, NO_THREADS), Id4, []),
308 thread_create(forLoop(N, 5, NO_THREADS), Id5, []),
309 thread_create(forLoop(N, 6, NO_THREADS), Id6, []),
310 thread_create(forLoop(N, 7, NO_THREADS), Id7, []),
311 thread_create(forLoop(N, 8, NO_THREADS), Id8, []),
312 thread_create(forLoop(N, 9, NO_THREADS), Id9, []),
313 thread_create(forLoop(N, 10, NO_THREADS), Id10, []),
314 thread_create(forLoop(N, 11, NO_THREADS), Id11, []),
315 thread_create(forLoop(N, 12, NO_THREADS), Id12, []),
316 thread_create(forLoop(N, 13, NO_THREADS), Id13, []),
317 thread_create(forLoop(N, 14, NO_THREADS), Id14, []),
318 thread_create(forLoop(N, 15, NO_THREADS), Id15, []),
319 thread_join(Id1, _),
320 thread_join(Id2, _),
321 thread_join(Id3, _),
322 thread_join(Id4, _),
323 thread_join(Id5, _),
324 thread_join(Id6, _),
325 thread_join(Id7, _),
326 thread_join(Id8, _),
327 thread_join(Id9, _),
328 thread_join(Id10, _),
329 thread_join(Id11, _),
330 thread_join(Id12, _),
331 thread_join(Id13, _),
332 thread_join(Id14, _),
333 thread_join(Id15, _),
334 !.
335
336 howManyThreads(N, NO_THREADS, 16) :-
337 thread_create(forLoop(N, 1, NO_THREADS), Id1, []),
338 thread_create(forLoop(N, 2, NO_THREADS), Id2, []),
339 thread_create(forLoop(N, 3, NO_THREADS), Id3, []),
340 thread_create(forLoop(N, 4, NO_THREADS), Id4, []),
341 thread_create(forLoop(N, 5, NO_THREADS), Id5, []),
342 thread_create(forLoop(N, 6, NO_THREADS), Id6, []),
343 thread_create(forLoop(N, 7, NO_THREADS), Id7, []),
344 thread_create(forLoop(N, 8, NO_THREADS), Id8, []),
345 thread_create(forLoop(N, 9, NO_THREADS), Id9, []),
346 thread_create(forLoop(N, 10, NO_THREADS), Id10, []),
347 thread_create(forLoop(N, 11, NO_THREADS), Id11, []),
348 thread_create(forLoop(N, 12, NO_THREADS), Id12, []),
349 thread_create(forLoop(N, 13, NO_THREADS), Id13, []),
350 thread_create(forLoop(N, 14, NO_THREADS), Id14, []),
351 thread_create(forLoop(N, 15, NO_THREADS), Id15, []),
352 thread_create(forLoop(N, 16, NO_THREADS), Id16, []),
353 thread_join(Id1, _),
354 thread_join(Id2, _),
355 thread_join(Id3, _),
356 thread_join(Id4, _),
357 thread_join(Id5, _),
358 thread_join(Id6, _),
359 thread_join(Id7, _),
360 thread_join(Id8, _),
361 thread_join(Id9, _),
362 thread_join(Id10, _),
363 thread_join(Id11, _),
364 thread_join(Id12, _),
365 thread_join(Id13, _),
366 thread_join(Id14, _),
367 thread_join(Id15, _),
368 thread_join(Id16, _),
369 !.

You might also like