-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME
More file actions
31 lines (24 loc) · 1.65 KB
/
Copy pathREADME
File metadata and controls
31 lines (24 loc) · 1.65 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
// Copyright (C) 2021 Cocioran Stefan 331 CA
- tema1_par -
The memory for initial generation was allocated here because calloc/malloc is
not thread-safe and the thread function (run_genetic_algorithm) arguments were
passed as a structure in order to avoid the usage of global variables. Threads
are created and the execution of their function begins. When the execution finishes,
the resources used are freed.
- genetic_algorithm_par
First of all, before starting the actual execution of the algorithm, the setting
of the initial generation is parallelizded, being equally divided between threads.
I tried to parallelize almost every "for" loop I could, using "start" and "end"
indexes calculated by the formulas presented in the course/laboratory. The barrier
was used multiple times to ensure that there will be no race condition and the
individuals' genetic data was computed properly before proceeding with further
calculations and data manipulation (fitness computions, mutations, crossovers,
copying of individuals).
The compare function used by "qsort" had a "for" loop which was iterating through
the chromosomes of an individual. I observed that there is the exact same loop in
the "compute_fitness" function. In order to not iterate twice through the same
elements, the chromosome incrementation instructions that should have been done
here were moved. The value of "first_count" and "second_count" variables were stored
in an additional field of the "individual" structure.
The fitness compution function receives two extra parameters, the number of threads
and the thread id so that it can be parallelized using the same principle and formulas.