|
| 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) |
0 commit comments