Skip to content

Commit 97c9e69

Browse files
krzeszewPosiadacz07Marina-Zubovabdmoore1
authored
Genetic Algorithms on GPU using Intel Distribution of Python numba-dpex code sample (oneapi-src#2296)
* Genetic Algorithms on GPU using Intel Distribution of Python numba-dpex code sample * Update code sample documentation and README * Update readme * Apply suggestions from code review - first batch Co-authored-by: Marina Zubova <[email protected]> Co-authored-by: Ben Moore <[email protected]> * Apply PR comments - second batch * PR changes - update README.md * Update AI-and-Analytics/Features-and-Functionality/IntelPython_GPU_numba-dpex_Genetic_Algorithm/README.md Co-authored-by: Marina Zubova <[email protected]> --------- Co-authored-by: Urszula Guminska <[email protected]> Co-authored-by: Marina Zubova <[email protected]> Co-authored-by: Ben Moore <[email protected]>
1 parent 04a1b59 commit 97c9e69

File tree

10 files changed

+1737
-0
lines changed

10 files changed

+1737
-0
lines changed

AI-and-Analytics/Features-and-Functionality/IntelPython_GPU_numba-dpex_Genetic_Algorithm/IntelPython_GPU_numba-dpex_Genetic_Algorithm.ipynb

Lines changed: 775 additions & 0 deletions
Large diffs are not rendered by default.

AI-and-Analytics/Features-and-Functionality/IntelPython_GPU_numba-dpex_Genetic_Algorithm/IntelPython_GPU_numba-dpex_Genetic_Algorithm.py

Lines changed: 556 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright Intel Corporation
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# `Genetic Algorithms on GPU using Intel® Distribution for Python* numba-dpex` Sample
2+
3+
The `Genetic Algorithms on GPU using Intel® Distribution for Python* numba-dpex` sample shows how to implement a general genetic algorithm (GA) and offload computation to a GPU using numba-dpex.
4+
5+
| Property | Description
6+
| :--- | :---
7+
| Category | Code Optimization
8+
| What you will learn | How to implement the genetic algorithm using the Data-parallel Extension for Numba* (numba-dpex)?
9+
| Time to complete | 8 minutes
10+
11+
>**Note**: This sample is validated on Intel® Distribution for Python* Offline Installer and AI Tools Offline Installer. For the full list of validated platforms refer to [Platform Validation](https://github.com/oneapi-src/oneAPI-samples/tree/master?tab=readme-ov-file#platform-validation).
12+
13+
## Purpose
14+
15+
In this sample, you will create and run the general genetic algorithm and optimize it to run on GPU using the Intel® Distribution for Python* numba-dpex. You will learn what are selection, crossover, and mutation, and how to adjust those methods from general genetic algorithm to a specific optimization problem which is the Traveling Salesman Problem.
16+
17+
## Prerequisites
18+
19+
| Optimized for | Description
20+
|:--- |:---
21+
| OS | Ubuntu* 20.04
22+
| Hardware | GPU
23+
| Software | Intel® Distribution for Python*
24+
25+
## Key Implementation Details
26+
27+
This sample code is implemented for GPUs using Python. The sample assumes you have numba-dpex installed inside a Conda environment, similar to what is installed with the Intel® Distribution for Python*.
28+
29+
The sample tutorial contains one Jupyter Notebook and one Python script. You can use either.
30+
31+
## Environment Setup
32+
You will need to download and install the following toolkits to use the sample.
33+
<!-- Use numbered steps instead of subheadings -->
34+
35+
**1. Get Intel® Distribution for Python***
36+
37+
If you have not already, install Intel® Distribution for Python* via [Installer](https://www.intel.com/content/www/us/en/developer/tools/oneapi/distribution-python-download.html?operatingsystem=linux&linux-install-type=offline).
38+
39+
**2. Activate the Intel® Distribution for Python\* base environment**
40+
<!-- this step is from AI Tools GSG, please don't modify unless GSG is updated -->
41+
If the default path is used during the installation of Intel® Distribution for Python*:
42+
```
43+
source $HOME/intelpython3/bin/activate
44+
```
45+
If a non-default path is used:
46+
```
47+
source <custom_path>/bin/activate
48+
```
49+
50+
**3. Clone the GitHub repository**
51+
<!-- for oneapi-samples: git clone https://github.com/oneapi-src/oneAPI-samples.git
52+
cd oneAPI-samples/AI-and-Analytics/<samples-folder>/<individual-sample-folder> -->
53+
<!-- for migrated samples - provide git clone command for individual repo and cd to sample dir -->
54+
```
55+
git clone https://github.com/oneapi-src/oneAPI-samples.git
56+
cd oneAPI-samples/AI-and-Analytics/Features-and-Functionality/IntelPython_GPU_numba-dpex_Genetic_Algorithm
57+
```
58+
59+
**4. Install dependencies**
60+
<!-- It is required to have requirement.txt file in sample dir. It should list additional libraries, such as matplotlib, ipykernel etc. -->
61+
>**Note**: Before running the following commands, make sure your Conda environment is activated
62+
63+
```
64+
pip install -r requirements.txt
65+
pip install notebook
66+
```
67+
For Jupyter Notebook, refer to [Installing Jupyter](https://jupyter.org/install) for detailed installation instructions.
68+
69+
## Run the Sample
70+
>**Note**: Before running the sample, make sure [Environment Setup](https://github.com/oneapi-src/oneAPI-samples/tree/master/AI-and-Analytics/Features-and-Functionality/IntelPython_GPU_numba-dpex_Genetic_Algorithm#environment-setup) is completed.
71+
72+
### Intel® Distribution for Python* Offline Installer (Validated)
73+
74+
**1. Register Conda kernel to Jupyter Notebook kernel**
75+
76+
If the default path is used during the installation of Intel® Distribution for Python*:
77+
```
78+
$HOME/intelpython3/bin/python -m ipykernel install --user --name=base
79+
```
80+
If a non-default path is used:
81+
```
82+
<custom_path>/bin/python -m ipykernel install --user --name=base
83+
```
84+
**2. Launch Jupyter Notebook**
85+
<!-- add other flags to jupyter notebook command if needed, such as port 8888 or allow-root -->
86+
```
87+
jupyter notebook --ip=0.0.0.0
88+
```
89+
**3. Follow the instructions to open the URL with the token in your browser**
90+
91+
**4. Select the Notebook**
92+
<!-- add sample file name -->
93+
```
94+
IntelPython_GPU_numba-dpex_Genetic_Algorithm.ipynb
95+
```
96+
**5. Change the kernel to `base`**
97+
<!-- specify relevant kernel name(s), for example `pytorch` -->
98+
**6. Run every cell in the Notebook in sequence**
99+
100+
## Example Output
101+
102+
If successful, the sample displays `[CODE_SAMPLE_COMPLETED_SUCCESSFULLY]` at the end of execution. The sample will print out the runtimes and charts of relative performance with numba-dpex and without any optimizations as the baseline. Additionally, sample will print the best and worst path found in the Traveling Salesman Problem.
103+
104+
## Related Samples
105+
106+
* [Get Started with the Intel® Distribution for Python*](https://www.intel.com/content/www/us/en/developer/articles/technical/get-started-with-intel-distribution-for-python.html)
107+
108+
## License
109+
110+
Code samples are licensed under the MIT license. See
111+
[License.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/License.txt)
112+
for details.
113+
114+
Third party program Licenses can be found here:
115+
[third-party-programs.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/third-party-programs.txt)
116+
117+
*Other names and brands may be claimed as the property of others. [Trademarks](https://www.intel.com/content/www/us/en/legal/trademarks.html)
89.5 KB
Loading
60.6 KB
Loading
91.5 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ipykernel
2+
matplotlib
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"guid": "DB33884A-2DDE-4657-9F95-E6E573403F61",
3+
"name": "Genetic Algorithms on GPU using Intel® Distribution of Python numba-dpex",
4+
"categories": ["Toolkit/oneAPI AI And Analytics/Getting Started"],
5+
"description": "This sample shows how to implement general genetic algorithm (GA) and offload computation to GPU using numba-dpex.",
6+
"builder": ["cli"],
7+
"languages": [{"python":{}}],
8+
"dependencies": ["intelpython"],
9+
"os":["linux"],
10+
"targetDevice": ["GPU"],
11+
"ciTests": {
12+
"linux": [
13+
{
14+
"env": [
15+
"source /intel/oneapi/intelpython/bin/activate",
16+
"conda activate base",
17+
"pip install -r requirements.txt"
18+
],
19+
"id": "idp_ga_numba_dpex_py",
20+
"steps": [
21+
"python IntelPython_GPU_numba-dpex_Genetic_Algorithm.py"
22+
]
23+
}
24+
]
25+
},
26+
"expertise": "Code Optimization"
27+
}

0 commit comments

Comments
 (0)