You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An N-body simulation is a simulation of a dynamical system of particles, usually under the influence of physical forces, such as gravity. This nbody sample code is written to SYCL standards for Intel® CPUs and GPUs.
2
+
An N-body simulation is a simulation of a dynamical system of particles, usually under the influence of physical forces, such as gravity. This `Nbody` sample code uses SYCL* standards for CPUs and GPUs.
3
3
4
-
| Property | Description
5
-
|:--- |:---
6
-
| What you will learn | How to offload the computation to GPU using Intel® oneAPI DPC++ Compiler
7
-
| Time to complete | 15 minutes
4
+
| Property | Description
5
+
|:--- |:---
6
+
| What you will learn | How to offload complex computations to GPU
7
+
| Time to complete | 15 minutes
8
8
9
9
## Purpose
10
-
Nbody sample code simulates 16000 particles and for ten integration steps. Each particle's position, velocity and acceleration parameters are dependent on other (N-1) particles. This algorithm is highly data parallel and a perfect candidate to offload to GPU. The code demonstrates how to deal with multiple device kernels, which can be enqueued into a DPC++ queue for execution and how to handle parallel reductions.
10
+
By default, the `Nbody` sample code simulates 16,000 particles over ten integration steps. The position, velocity, and acceleration parameters of each particle are dependent on other (N-1) particles.
11
11
12
-
## Prerequisites
13
-
14
-
| Optimized for | Description
15
-
|:--- |:---
16
-
| OS | Linux* Ubuntu* 18.04 <br>Windows* 10
17
-
| Hardware | Skylake with GEN9 or newer
18
-
| Software | Intel® oneAPI DPC++ Compiler;
12
+
This algorithm is highly data parallel, so the code a good candidate to offload to GPU. The code demonstrates how to deal with multiple device kernels, which can be enqueued into a SYCL queue for execution, and the code demonstrates how to handle parallel reductions.
19
13
14
+
## Prerequisites
15
+
| Optimized for | Description
16
+
|:--- |:---
17
+
| OS | Ubuntu* 18.04 <br> Windows* 10
18
+
| Hardware | Skylake with GEN9 or newer
19
+
| Software | Intel® oneAPI DPC++ Compiler
20
20
21
21
## Key Implementation Details
22
22
The basic SYCL* compliant implementation explained in the code includes device selector, buffer, accessor, kernel, and command groups.
23
23
24
-
## Building the Program for CPU and GPU
24
+
## Set Environment Variables
25
+
When working with the command-line interface (CLI), you should configure the oneAPI toolkits using environment variables. Set up your CLI environment by sourcing the `setvars` script every time you open a new terminal window. This practice ensures that your compiler, libraries, and tools are ready for development.
25
26
27
+
## Build the `Nbody` Program for CPU and GPU
26
28
> **Note**: If you have not already done so, set up your CLI
27
-
> environment by sourcing the `setvars` script located in
28
-
> the root of your oneAPI installation.
29
+
> environment by sourcing the `setvars` script in the root of your oneAPI installation.
29
30
>
30
-
> Linux:
31
+
> Linux*:
31
32
> - For system wide installations: `. /opt/intel/oneapi/setvars.sh`
32
-
> - For private installations: `. ~/intel/oneapi/setvars.sh`
33
+
> - For private installations: ` . ~/intel/oneapi/setvars.sh`
34
+
> - For non-POSIX shells, like csh, use the following command: `bash -c 'source <install-dir>/setvars.sh ; exec csh'`
> - Windows PowerShell*, use the following command: `cmd.exe "/K" '"C:\Program Files (x86)\Intel\oneAPI\setvars.bat" && powershell'`
36
39
>
37
-
>For more information on environment variables, see Use the setvars Script for [Linux or macOS](https://www.intel.com/content/www/us/en/develop/documentation/oneapi-programming-guide/top/oneapi-development-environment-setup/use-the-setvars-script-with-linux-or-macos.html), or [Windows](https://www.intel.com/content/www/us/en/develop/documentation/oneapi-programming-guide/top/oneapi-development-environment-setup/use-the-setvars-script-with-windows.html).
38
-
39
-
### Running Samples in Intel® DevCloud
40
-
If running a sample in the Intel® DevCloud, you must specify the compute node (CPU, GPU, FPGA) and whether to run in batch or interactive mode. For more information, see the Intel® oneAPI Base Toolkit [Get Started Guide](https://devcloud.intel.com/oneapi/get_started/), and see [Run the Nbody sample on the Intel® DevCloud](#run-nbody-on-devcloud) below.
40
+
> For more information on configuring environment variables, see [Use the setvars Script with Linux* or macOS*](https://www.intel.com/content/www/us/en/develop/documentation/oneapi-programming-guide/top/oneapi-development-environment-setup/use-the-setvars-script-with-linux-or-macos.html) or [Use the setvars Script with Windows*](https://www.intel.com/content/www/us/en/develop/documentation/oneapi-programming-guide/top/oneapi-development-environment-setup/use-the-setvars-script-with-windows.html).
41
41
42
+
### Include Files
43
+
The include folder is at `%ONEAPI_ROOT%\dev-utilities\latest\include` on your development system. You might need to use some of the resources from this location to build the sample.
42
44
43
-
### Using Visual Studio Code* (Optional)
44
-
45
-
You can use Visual Studio Code (VS Code) extensions to set your environment, create launch configurations,
46
-
and browse and download samples.
45
+
### Use Visual Studio Code* (VS Code) (Optional)
46
+
You can use Visual Studio Code* (VS Code) extensions to set your environment,
47
+
create launch configurations, and browse and download samples.
47
48
48
49
The basic steps to build and run a sample using VS Code include:
49
-
- Download a sample using the extension **Code Sample Browser for Intel® oneAPI Toolkits**.
50
-
- Configure the oneAPI environment with the extension **Environment Configurator for Intel oneAPI Toolkits**.
51
-
- Open a Terminal in VS Code (**Terminal>New Terminal**).
52
-
- Run the sample in the VS Code terminal using the instructions below.
50
+
1. Configure the oneAPI environment with the extension **Environment Configurator for Intel® oneAPI Toolkits**.
51
+
2. Download a sample using the extension **Code Sample Browser for Intel® oneAPI Toolkits**.
52
+
3. Open a terminal in VS Code (**Terminal > New Terminal**).
53
+
4. Run the sample in the VS Code terminal using the instructions below.
53
54
54
-
To learn more about the extensions and how to configure the oneAPI environment, see
55
-
[Using Visual Studio Code with Intel® oneAPI Toolkits](https://software.intel.com/content/www/us/en/develop/documentation/using-vs-code-with-intel-oneapi/top.html).
56
-
57
-
After learning how to use the extensions for Intel oneAPI Toolkits, return to this readme for instructions on how to build and run a sample.
58
-
59
-
### Include Files
60
-
The include folder is located at `%ONEAPI_ROOT%\dev-utilities\latest\include` on your development system.
55
+
To learn more about the extensions and how to configure the oneAPI environment, see the
56
+
[Using Visual Studio Code with Intel® oneAPI Toolkits User Guide](https://www.intel.com/content/www/us/en/develop/documentation/using-vs-code-with-intel-oneapi/top.html).
61
57
62
58
### On Linux*
63
-
Perform the following steps:
64
-
1. Build the program using the following `cmake` commands.
65
-
```
66
-
$ mkdir build
67
-
$ cd build
68
-
$ cmake ..
69
-
$ make
70
-
```
71
-
72
-
2. Run the program
73
-
```
74
-
make run
75
-
```
76
-
77
-
3. Clean the program
78
-
```
79
-
make clean
80
-
```
81
-
59
+
1. Build the program.
60
+
```
61
+
mkdir build
62
+
cd build
63
+
cmake ..
64
+
make
65
+
```
82
66
If an error occurs, you can get more details by running `make` with
83
67
the `VERBOSE=1` argument:
84
68
```
85
69
make VERBOSE=1
86
70
```
87
-
### Troubleshooting
88
-
If you receive an error message, troubleshoot the problem using the Diagnostics Utility for Intel® oneAPI Toolkits, which provides system checks to find missing
89
-
dependencies and permissions errors. See [Diagnostics Utility for Intel® oneAPI Toolkits User Guide](https://www.intel.com/content/www/us/en/develop/documentation/diagnostic-utility-user-guide/top.html).
90
-
91
-
### On Windows* Using Visual Studio* Version 2017 or Newer
92
-
- Build the program using VS2017 or VS2019.
93
-
- Right-click on the solution file and open using either VS2017 or VS2019 IDE.
94
-
- Right-click on the project in Solution Explorer and select Rebuild.
95
-
- From the top menu, select Debug -> Start without Debugging.
96
-
97
-
- Build the program using MSBuild
98
-
- Open "x64 Native Tools Command Prompt for VS2017" or "x64 Native Tools Command Prompt for VS2019"
99
-
- Run the following command: `MSBuild Hello_World_GPU.sln /t:Rebuild /p:Configuration="Release"`
100
71
72
+
### On Windows*
73
+
**Using Visual Studio**
74
+
75
+
Build the program using **Visual Studio 2017** or newer.
76
+
1. Change to the sample directory.
77
+
2. Right-click on the solution file and open the solution in the IDE.
78
+
3. From the top menu, select **Debug** > **Start without Debugging**.
79
+
80
+
**Using MSBuild**
81
+
82
+
1. Open "x64 Native Tools Command Prompt for VS2017" or "x64 Native Tools Command Prompt for VS2019" or whatever is appropriate for your Visual Studio* version.
83
+
2. Change to the sample directory.
84
+
3. Run the following command: `MSBuild Nbody.sln /t:Rebuild /p:Configuration="Release"`
85
+
86
+
#### Troubleshooting
87
+
If you receive an error message, troubleshoot the problem using the **Diagnostics Utility for Intel® oneAPI Toolkits**. The diagnostic utility provides configuration and system checks to help find missing dependencies, permissions errors, and other issues. See the [Diagnostics Utility for Intel® oneAPI Toolkits User Guide](https://www.intel.com/content/www/us/en/develop/documentation/diagnostic-utility-user-guide/top.html) for more information on using the utility.
88
+
89
+
## Run the `Nbody` Program
90
+
### On Linux
91
+
1. Run the program.
92
+
```
93
+
make run
94
+
```
95
+
2. Clean the program. (Optional)
96
+
```
97
+
make clean
98
+
```
99
+
### On Windows
100
+
1. Change to the output directory.
101
+
2. Run the executable.
102
+
```
103
+
Nbody.exe
104
+
```
105
+
106
+
### Run 'Nbody' Sample in Intel® DevCloud (Optional)
107
+
When running a sample in the Intel® DevCloud, you must specify the compute node (CPU, GPU, FPGA) and whether to run in batch or interactive mode. For more information, see the Intel® oneAPI Base Toolkit [Get Started Guide](https://devcloud.intel.com/oneapi/get_started/).
108
+
109
+
#### Build and Run Samples in Batch Mode (Optional)
110
+
You can submit build and run jobs through a Portable Bash Script (PBS). A job is a script that submitted to PBS through the `qsub` utility. By default, the `qsub` utility does not inherit the current environment variables or your current working directory, so you might need to submit jobs to configure the environment variables. To indicate the correct working directory, you can use either absolute paths or pass the `-d \<dir\>` option to `qsub`.
111
+
112
+
If you choose to use scripts, jobs terminate with writing a couple of files to the disk:
113
+
-`<script_name>.sh.eXXXX` is the job stderr
114
+
-`<script_name>.sh.oXXXX` is the job stdout
115
+
116
+
Here `XXXX` is the job ID, which gets printed to the screen after each `qsub` command.
4. Change directories to the Nbody sample directory.
151
-
```
152
-
cd ~/oneAPI-samples/DirectProgramming/DPC++/N-bodyMethods/Nbody
153
-
```
154
-
#### Build and run the sample in batch mode
155
-
The following describes the process of submitting build and run jobs to PBS.
156
-
A job is a script that is submitted to PBS through the qsub utility. By default, the qsub utility does not inherit the current environment variables or your current working directory. For this reason, it is necessary to submit jobs as scripts that handle the setup of the environment variables. In order to address the working directory issue, you can either use absolute paths or pass the `-d \<dir\>` option to qsub to set the working directory.
157
-
158
-
#### Create the Job Scripts
159
-
1. Create a build.sh script with your preferred text editor:
Jobs submitted in batch mode are placed in a queue waiting for the necessary resources (compute nodes) to become available. The jobs will be executed on a first come basis on the first available node(s) having the requested property or label.
190
-
1. Build the sample on a gpu node.
191
-
192
-
```
193
-
qsub -l nodes=1:gpu:ppn=2 -d . build.sh
194
-
```
156
+
You can modify the `NBody` sample simulation parameters in `GSimulation.cpp`. Configurable parameters include:
195
157
196
-
Note: -l nodes=1:gpu:ppn=2 (lower case L) is used to assign one full GPU node to the job.
197
-
Note: The -d . is used to configure the current folder as the working directory for the task.
158
+
|Parameter | Defaults
159
+
|:--- |:---
160
+
|`set_npart` | Default number of particles is **16000**
161
+
|`set_nsteps` | Default number of integration steps is **10**
162
+
|`set_tstep` | Default time delta is **0.1**
163
+
|`set_sfreq` | Default sample frequency is **1**
198
164
199
-
2. In order to inspect the job progress, use the qstat utility.
165
+
### Example Output on Linux
200
166
```
201
-
watch -n 1 qstat -n -1
202
-
```
203
-
Note: The watch -n 1 command is used to run qstat -n -1 and display its results every second. If no results are displayed, the job has completed.
204
-
205
-
3. After the build job completes successfully, run the sample on a gpu node:
206
-
```
207
-
qsub -l nodes=1:gpu:ppn=2 -d . run.sh
208
-
```
209
-
4. When a job terminates, a couple of files are written to the disk:
210
-
211
-
`<script_name>.sh.eXXXX`, which is the job stderr
212
-
213
-
`<script_name>.sh.oXXXX`, which is the job stdout
214
-
215
-
Here XXXX is the job ID, which gets printed to the screen after each qsub command.
216
-
217
-
5. Inspect the output of the sample.
218
-
```
219
-
cat run.sh.oXXXX
167
+
===============================
168
+
Initialize Gravity Simulation
169
+
Target Device: Intel(R) Gen9
170
+
nPart = 16000; nSteps = 10; dt = 0.1
171
+
------------------------------------------------
172
+
s dt kenergy time (s) GFLOPS
173
+
------------------------------------------------
174
+
1 0.1 26.405 0.28029 26.488
175
+
2 0.2 313.77 0.066867 111.03
176
+
3 0.3 926.56 0.065832 112.78
177
+
4 0.4 1866.4 0.066153 112.23
178
+
5 0.5 3135.6 0.065607 113.16
179
+
6 0.6 4737.6 0.066544 111.57
180
+
7 0.7 6676.6 0.066403 111.81
181
+
8 0.8 8957.7 0.066365 111.87
182
+
9 0.9 11587 0.066617 111.45
183
+
10 1 14572 0.06637 111.86
184
+
185
+
# Total Time (s) : 0.87714
186
+
# Average Performance : 112.09 +- 0.56002
187
+
===============================
188
+
Built target run
220
189
```
221
-
You should see output similar to this:
222
-
190
+
### Example Output on Intel® DevCloud
223
191
```
224
192
Scanning dependencies of target run
225
193
===============================
@@ -244,16 +212,6 @@ Scanning dependencies of target run
244
212
===============================
245
213
Built target run
246
214
```
247
-
248
-
6. Remove the stdout and stderr files, and clean-up the project files.
249
-
```
250
-
rm build.sh.*; rm run.sh.*; make clean
251
-
```
252
-
7. Disconnect from the Intel DevCloud.
253
-
```
254
-
exit
255
-
```
256
-
257
215
## License
258
216
Code samples are licensed under the MIT license. See
259
217
[License.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/License.txt) for details.
0 commit comments