2021-03-25 06:49:28 +01:00
|
|
|
/*
|
|
|
|
* =======================================================================================
|
|
|
|
*
|
|
|
|
* Author: Jan Eitzinger (je), jan.eitzinger@fau.de
|
|
|
|
* Copyright (c) 2021 RRZE, University Erlangen-Nuremberg
|
|
|
|
*
|
|
|
|
* This file is part of MD-Bench.
|
|
|
|
*
|
|
|
|
* MD-Bench is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MD-Bench is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
* with MD-Bench. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
* =======================================================================================
|
|
|
|
*/
|
2021-11-11 15:23:46 +01:00
|
|
|
#include <math.h>
|
2021-11-09 08:37:37 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2021-11-10 16:02:05 +01:00
|
|
|
#include <stddef.h>
|
2021-12-03 08:13:43 +01:00
|
|
|
#include <cuda_profiler_api.h>
|
2021-11-08 20:32:12 +01:00
|
|
|
#include <cuda_runtime.h>
|
2021-11-10 16:02:05 +01:00
|
|
|
#include <device_launch_parameters.h>
|
|
|
|
|
2021-11-11 14:49:29 +01:00
|
|
|
extern "C" {
|
|
|
|
#include <likwid-marker.h>
|
2021-03-25 06:49:28 +01:00
|
|
|
|
2021-11-11 14:49:29 +01:00
|
|
|
#include <timing.h>
|
|
|
|
#include <neighbor.h>
|
|
|
|
#include <parameter.h>
|
|
|
|
#include <atom.h>
|
2021-12-18 13:02:04 +01:00
|
|
|
#include <allocate.h>
|
2021-11-11 20:14:30 +01:00
|
|
|
}
|
|
|
|
|
2021-11-10 16:02:05 +01:00
|
|
|
// cuda kernel
|
|
|
|
__global__ void calc_force(
|
2022-06-23 22:25:55 +02:00
|
|
|
Atom a,
|
2021-11-11 20:14:30 +01:00
|
|
|
MD_FLOAT cutforcesq, MD_FLOAT sigma6, MD_FLOAT epsilon,
|
2021-11-14 10:02:23 +01:00
|
|
|
int Nlocal, int neigh_maxneighs, int *neigh_neighbors, int *neigh_numneigh) {
|
2021-11-10 16:02:05 +01:00
|
|
|
|
2021-11-14 10:02:23 +01:00
|
|
|
const int i = blockIdx.x * blockDim.x + threadIdx.x;
|
|
|
|
if( i >= Nlocal ) {
|
2021-11-10 16:02:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-23 22:25:55 +02:00
|
|
|
Atom *atom = &a;
|
2021-11-11 15:23:46 +01:00
|
|
|
|
2022-01-31 20:27:59 +01:00
|
|
|
const int numneighs = neigh_numneigh[i];
|
2021-11-14 10:02:23 +01:00
|
|
|
|
|
|
|
MD_FLOAT xtmp = atom_x(i);
|
|
|
|
MD_FLOAT ytmp = atom_y(i);
|
|
|
|
MD_FLOAT ztmp = atom_z(i);
|
|
|
|
|
|
|
|
MD_FLOAT fix = 0;
|
|
|
|
MD_FLOAT fiy = 0;
|
|
|
|
MD_FLOAT fiz = 0;
|
|
|
|
|
|
|
|
for(int k = 0; k < numneighs; k++) {
|
2022-01-31 20:27:59 +01:00
|
|
|
int j = neigh_neighbors[atom->Nlocal * k + i];
|
2021-11-14 10:02:23 +01:00
|
|
|
MD_FLOAT delx = xtmp - atom_x(j);
|
|
|
|
MD_FLOAT dely = ytmp - atom_y(j);
|
|
|
|
MD_FLOAT delz = ztmp - atom_z(j);
|
|
|
|
MD_FLOAT rsq = delx * delx + dely * dely + delz * delz;
|
2021-11-10 16:02:05 +01:00
|
|
|
|
2021-11-11 20:14:30 +01:00
|
|
|
#ifdef EXPLICIT_TYPES
|
2021-11-14 10:02:23 +01:00
|
|
|
const int type_j = atom->type[j];
|
|
|
|
const int type_ij = type_i * atom->ntypes + type_j;
|
|
|
|
const MD_FLOAT cutforcesq = atom->cutforcesq[type_ij];
|
|
|
|
const MD_FLOAT sigma6 = atom->sigma6[type_ij];
|
|
|
|
const MD_FLOAT epsilon = atom->epsilon[type_ij];
|
2021-11-11 20:14:30 +01:00
|
|
|
#endif
|
2021-11-10 16:02:05 +01:00
|
|
|
|
2021-11-14 10:02:23 +01:00
|
|
|
if(rsq < cutforcesq) {
|
|
|
|
MD_FLOAT sr2 = 1.0 / rsq;
|
|
|
|
MD_FLOAT sr6 = sr2 * sr2 * sr2 * sigma6;
|
|
|
|
MD_FLOAT force = 48.0 * sr6 * (sr6 - 0.5) * sr2 * epsilon;
|
|
|
|
fix += delx * force;
|
|
|
|
fiy += dely * force;
|
|
|
|
fiz += delz * force;
|
|
|
|
}
|
2021-11-10 16:02:05 +01:00
|
|
|
}
|
2021-11-14 10:02:23 +01:00
|
|
|
|
2022-01-01 16:09:21 +01:00
|
|
|
atom_fx(i) = fix;
|
|
|
|
atom_fy(i) = fiy;
|
|
|
|
atom_fz(i) = fiz;
|
2021-11-10 16:02:05 +01:00
|
|
|
}
|
|
|
|
|
2022-06-23 22:25:55 +02:00
|
|
|
__global__ void kernel_initial_integrate(MD_FLOAT dtforce, MD_FLOAT dt, int Nlocal, Atom a) {
|
2022-01-01 18:18:12 +01:00
|
|
|
|
|
|
|
const int i = blockIdx.x * blockDim.x + threadIdx.x;
|
|
|
|
if( i >= Nlocal ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-23 22:25:55 +02:00
|
|
|
Atom *atom = &a;
|
2022-01-01 18:18:12 +01:00
|
|
|
|
|
|
|
atom_vx(i) += dtforce * atom_fx(i);
|
|
|
|
atom_vy(i) += dtforce * atom_fy(i);
|
|
|
|
atom_vz(i) += dtforce * atom_fz(i);
|
|
|
|
atom_x(i) = atom_x(i) + dt * atom_vx(i);
|
|
|
|
atom_y(i) = atom_y(i) + dt * atom_vy(i);
|
|
|
|
atom_z(i) = atom_z(i) + dt * atom_vz(i);
|
|
|
|
}
|
|
|
|
|
2022-06-23 22:25:55 +02:00
|
|
|
__global__ void kernel_final_integrate(MD_FLOAT dtforce, int Nlocal, Atom a) {
|
2022-01-01 18:18:12 +01:00
|
|
|
|
|
|
|
const int i = blockIdx.x * blockDim.x + threadIdx.x;
|
|
|
|
if( i >= Nlocal ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-23 22:25:55 +02:00
|
|
|
Atom *atom = &a;
|
2022-01-01 18:18:12 +01:00
|
|
|
|
|
|
|
atom_vx(i) += dtforce * atom_fx(i);
|
|
|
|
atom_vy(i) += dtforce * atom_fy(i);
|
|
|
|
atom_vz(i) += dtforce * atom_fz(i);
|
|
|
|
}
|
|
|
|
|
2021-11-11 14:49:29 +01:00
|
|
|
extern "C" {
|
|
|
|
|
2022-06-23 18:54:56 +02:00
|
|
|
|
2021-12-25 13:52:33 +01:00
|
|
|
|
2022-06-26 16:25:59 +02:00
|
|
|
void cuda_final_integrate(bool doReneighbour, Parameter *param, Atom *atom, Atom *c_atom, const int num_threads_per_block) {
|
2022-01-01 18:18:12 +01:00
|
|
|
|
|
|
|
const int Nlocal = atom->Nlocal;
|
|
|
|
const int num_blocks = ceil((float)Nlocal / (float)num_threads_per_block);
|
|
|
|
|
2022-06-23 22:25:55 +02:00
|
|
|
kernel_final_integrate <<< num_blocks, num_threads_per_block >>> (param->dtforce, Nlocal, *c_atom);
|
2022-01-01 18:18:12 +01:00
|
|
|
|
|
|
|
checkCUDAError( "PeekAtLastError FinalIntegrate", cudaPeekAtLastError() );
|
|
|
|
checkCUDAError( "DeviceSync FinalIntegrate", cudaDeviceSynchronize() );
|
|
|
|
|
|
|
|
if(doReneighbour) {
|
2022-06-23 19:36:59 +02:00
|
|
|
checkCUDAError( "FinalIntegrate: velocity memcpy", cudaMemcpy(atom->vx, c_atom->vx, sizeof(MD_FLOAT) * atom->Nlocal * 3, cudaMemcpyDeviceToHost) );
|
2022-01-01 18:18:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-26 16:25:59 +02:00
|
|
|
void cuda_initial_integrate(bool doReneighbour, Parameter *param, Atom *atom, Atom *c_atom, const int num_threads_per_block) {
|
2022-01-01 18:18:12 +01:00
|
|
|
|
|
|
|
const int Nlocal = atom->Nlocal;
|
|
|
|
const int num_blocks = ceil((float)Nlocal / (float)num_threads_per_block);
|
|
|
|
|
2022-06-23 22:25:55 +02:00
|
|
|
kernel_initial_integrate <<< num_blocks, num_threads_per_block >>> (param->dtforce, param->dt, Nlocal, *c_atom);
|
2022-01-01 18:18:12 +01:00
|
|
|
|
|
|
|
checkCUDAError( "PeekAtLastError InitialIntegrate", cudaPeekAtLastError() );
|
|
|
|
checkCUDAError( "DeviceSync InitialIntegrate", cudaDeviceSynchronize() );
|
|
|
|
|
2022-01-24 18:04:50 +01:00
|
|
|
if(doReneighbour) {
|
2022-06-23 19:36:59 +02:00
|
|
|
checkCUDAError( "InitialIntegrate: velocity memcpy", cudaMemcpy(atom->vx, c_atom->vx, sizeof(MD_FLOAT) * atom->Nlocal * 3, cudaMemcpyDeviceToHost) );
|
2022-01-24 18:04:50 +01:00
|
|
|
}
|
2022-01-01 18:18:12 +01:00
|
|
|
}
|
|
|
|
|
2021-10-26 09:11:17 +02:00
|
|
|
double computeForce(
|
2021-12-25 13:52:33 +01:00
|
|
|
bool reneighbourHappenend,
|
2021-10-26 09:11:17 +02:00
|
|
|
Parameter *param,
|
|
|
|
Atom *atom,
|
2022-06-23 18:54:56 +02:00
|
|
|
Neighbor *neighbor,
|
|
|
|
Atom *c_atom,
|
2022-06-26 16:25:59 +02:00
|
|
|
Neighbor *c_neighbor,
|
|
|
|
int num_threads_per_block
|
2021-10-26 09:11:17 +02:00
|
|
|
)
|
|
|
|
{
|
2021-03-25 06:49:28 +01:00
|
|
|
int Nlocal = atom->Nlocal;
|
2021-10-26 09:11:17 +02:00
|
|
|
#ifndef EXPLICIT_TYPES
|
2021-03-25 06:49:28 +01:00
|
|
|
MD_FLOAT cutforcesq = param->cutforce * param->cutforce;
|
2021-11-11 20:14:30 +01:00
|
|
|
MD_FLOAT sigma6 = param->sigma6;
|
|
|
|
MD_FLOAT epsilon = param->epsilon;
|
2021-10-26 09:11:17 +02:00
|
|
|
#endif
|
2021-03-25 06:49:28 +01:00
|
|
|
|
2021-11-25 08:09:20 +01:00
|
|
|
/*
|
|
|
|
int nDevices;
|
|
|
|
cudaGetDeviceCount(&nDevices);
|
2021-12-25 13:52:33 +01:00
|
|
|
size_t free, total;
|
2021-11-25 08:09:20 +01:00
|
|
|
for(int i = 0; i < nDevices; ++i) {
|
2021-12-25 13:52:33 +01:00
|
|
|
cudaMemGetInfo( &free, &total );
|
2021-11-25 08:09:20 +01:00
|
|
|
cudaDeviceProp prop;
|
|
|
|
cudaGetDeviceProperties(&prop, i);
|
2021-12-25 13:52:33 +01:00
|
|
|
printf("DEVICE %d/%d NAME: %s\r\n with %ld MB/%ld MB memory used", i + 1, nDevices, prop.name, free / 1024 / 1024, total / 1024 / 1024);
|
2021-11-25 08:09:20 +01:00
|
|
|
}
|
2021-12-25 13:52:33 +01:00
|
|
|
*/
|
2021-12-01 17:16:32 +01:00
|
|
|
|
|
|
|
|
2021-11-11 20:14:30 +01:00
|
|
|
// HINT: Run with cuda-memcheck ./MDBench-NVCC in case of error
|
|
|
|
|
2022-06-23 19:36:59 +02:00
|
|
|
// checkCUDAError( "c_atom->fx memset", cudaMemset(c_atom->fx, 0, sizeof(MD_FLOAT) * Nlocal * 3) );
|
2021-12-28 16:48:26 +01:00
|
|
|
|
2022-01-01 18:18:12 +01:00
|
|
|
cudaProfilerStart();
|
2021-12-25 15:36:08 +01:00
|
|
|
|
2021-11-15 19:39:09 +01:00
|
|
|
const int num_blocks = ceil((float)Nlocal / (float)num_threads_per_block);
|
2021-11-11 20:14:30 +01:00
|
|
|
|
2021-11-11 14:49:29 +01:00
|
|
|
double S = getTimeStamp();
|
2021-06-30 13:44:02 +02:00
|
|
|
LIKWID_MARKER_START("force");
|
2021-10-12 22:39:54 +02:00
|
|
|
|
2022-06-23 22:25:55 +02:00
|
|
|
calc_force <<< num_blocks, num_threads_per_block >>> (*c_atom, cutforcesq, sigma6, epsilon, Nlocal, neighbor->maxneighs, c_neighbor->neighbors, c_neighbor->numneigh);
|
2021-10-26 09:11:17 +02:00
|
|
|
|
2022-01-01 18:18:12 +01:00
|
|
|
checkCUDAError( "PeekAtLastError ComputeForce", cudaPeekAtLastError() );
|
|
|
|
checkCUDAError( "DeviceSync ComputeForce", cudaDeviceSynchronize() );
|
2021-10-12 22:39:54 +02:00
|
|
|
|
2021-12-03 08:13:43 +01:00
|
|
|
cudaProfilerStop();
|
|
|
|
|
2021-06-30 13:44:02 +02:00
|
|
|
LIKWID_MARKER_STOP("force");
|
2021-11-11 14:49:29 +01:00
|
|
|
double E = getTimeStamp();
|
2021-10-11 16:57:02 +02:00
|
|
|
|
2021-11-11 14:49:29 +01:00
|
|
|
return E-S;
|
2021-03-25 06:49:28 +01:00
|
|
|
}
|
2022-06-26 18:37:09 +02:00
|
|
|
}
|