Skip to content

Bonded force to gpu - #51

Merged
goodstudyqaq merged 11 commits into
feature/qgpufrom
feature/qgpu-bonded-force
Nov 20, 2025
Merged

goodstudyqaq merged 11 commits into
feature/qgpufrom
feature/qgpu-bonded-force

Conversation

@goodstudyqaq

Copy link
Copy Markdown
Collaborator

No description provided.

@goodstudyqaq
goodstudyqaq requested a review from Copilot November 17, 2025 14:47
@goodstudyqaq goodstudyqaq changed the title finish angle force to gpu Bonded force to gpu Nov 17, 2025

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements GPU-accelerated angle force calculations using CUDA, migrating the computational workload from CPU to GPU. The implementation adds a new CUDA kernel for parallel angle force computation while maintaining the existing CPU code path.

Key Changes:

  • Added CUDA kernel implementation for angle force calculations with device-side energy accumulation and force updates
  • Simplified Makefile to automatically discover and compile CUDA source files
  • Integrated new GPU functions into the existing system (currently commented out pending full testing)

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 11 comments.

File Description
src/core/cuda/src/CudaAngleForce.cu Implements GPU kernel for angle force calculations with memory management and host interface
src/core/cuda/include/CudaAngleForce.h Declares kernel and host function prototypes for CUDA angle force interface
src/core/system.cu Adds include for CUDA angle force header; GPU code path currently commented out
src/core/Makefile Refactored to use pattern rules and automatic source discovery for simplified builds

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/core/cuda/src/CudaAngleForce.cu Resolved
Comment thread src/core/cuda/src/CudaAngleForce.cu Outdated
Comment on lines +3 to +8
bool is_initialized = false;
angle_t *d_angles = nullptr;
coord_t *d_coords = nullptr;
cangle_t *d_cangles = nullptr;
dvel_t *d_dvelocities = nullptr;
double *d_energy_sum = nullptr;

Copilot AI Nov 17, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Global variables with file scope should be declared static to avoid potential symbol conflicts during linking. These variables (is_initialized, d_angles, d_coords, d_cangles, d_dvelocities, d_energy_sum) should be declared as:

static bool is_initialized = false;
static angle_t *d_angles = nullptr;
static coord_t *d_coords = nullptr;
static cangle_t *d_cangles = nullptr;
static dvel_t *d_dvelocities = nullptr;
static double *d_energy_sum = nullptr;
Suggested change
bool is_initialized = false;
angle_t *d_angles = nullptr;
coord_t *d_coords = nullptr;
cangle_t *d_cangles = nullptr;
dvel_t *d_dvelocities = nullptr;
double *d_energy_sum = nullptr;
static bool is_initialized = false;
static angle_t *d_angles = nullptr;
static coord_t *d_coords = nullptr;
static cangle_t *d_cangles = nullptr;
static dvel_t *d_dvelocities = nullptr;
static double *d_energy_sum = nullptr;

Copilot uses AI. Check for mistakes.
Comment thread src/core/cuda/src/CudaAngleForce.cu Outdated
Comment on lines +92 to +114
cudaMalloc((void **)&d_angles, n_angles * sizeof(angle_t));
cudaMalloc((void **)&d_coords, n_atoms * sizeof(coord_t));
cudaMalloc((void **)&d_cangles, n_cangles * sizeof(cangle_t));
cudaMalloc((void **)&d_dvelocities, n_atoms * sizeof(dvel_t));
cudaMalloc((void **)&d_energy_sum, sizeof(double));
}

// copy data to device
cudaMemcpy(d_angles, angles, n_angles * sizeof(angle_t), cudaMemcpyHostToDevice);
cudaMemcpy(d_coords, coords, n_atoms * sizeof(coord_t), cudaMemcpyHostToDevice);
cudaMemcpy(d_cangles, cangles, n_cangles * sizeof(cangle_t), cudaMemcpyHostToDevice);
cudaMemcpy(d_dvelocities, dvelocities, n_atoms * sizeof(dvel_t), cudaMemcpyHostToDevice);

double h_energy_sum = 0.0;
cudaMemcpy(d_energy_sum, &h_energy_sum, sizeof(double), cudaMemcpyHostToDevice);

// launch kernel
calc_angle_forces_kernel<<<numBlocks, blockSize>>>(start, end, d_angles, d_coords, d_cangles, d_dvelocities, d_energy_sum);
cudaDeviceSynchronize();

// copy results back to host
cudaMemcpy(&h_energy_sum, d_energy_sum, sizeof(double), cudaMemcpyDeviceToHost);
cudaMemcpy(dvelocities, d_dvelocities, n_atoms * sizeof(dvel_t), cudaMemcpyDeviceToHost);

Copilot AI Nov 17, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing CUDA error checking. All CUDA API calls should be checked for errors to catch issues during development and runtime. Add error checking after each cudaMalloc, cudaMemcpy, and cudaDeviceSynchronize call. For example:

cudaError_t err = cudaMalloc((void **)&d_angles, n_angles * sizeof(angle_t));
if (err != cudaSuccess) {
    fprintf(stderr, "CUDA malloc failed: %s\n", cudaGetErrorString(err));
    // handle error
}
Suggested change
cudaMalloc((void **)&d_angles, n_angles * sizeof(angle_t));
cudaMalloc((void **)&d_coords, n_atoms * sizeof(coord_t));
cudaMalloc((void **)&d_cangles, n_cangles * sizeof(cangle_t));
cudaMalloc((void **)&d_dvelocities, n_atoms * sizeof(dvel_t));
cudaMalloc((void **)&d_energy_sum, sizeof(double));
}
// copy data to device
cudaMemcpy(d_angles, angles, n_angles * sizeof(angle_t), cudaMemcpyHostToDevice);
cudaMemcpy(d_coords, coords, n_atoms * sizeof(coord_t), cudaMemcpyHostToDevice);
cudaMemcpy(d_cangles, cangles, n_cangles * sizeof(cangle_t), cudaMemcpyHostToDevice);
cudaMemcpy(d_dvelocities, dvelocities, n_atoms * sizeof(dvel_t), cudaMemcpyHostToDevice);
double h_energy_sum = 0.0;
cudaMemcpy(d_energy_sum, &h_energy_sum, sizeof(double), cudaMemcpyHostToDevice);
// launch kernel
calc_angle_forces_kernel<<<numBlocks, blockSize>>>(start, end, d_angles, d_coords, d_cangles, d_dvelocities, d_energy_sum);
cudaDeviceSynchronize();
// copy results back to host
cudaMemcpy(&h_energy_sum, d_energy_sum, sizeof(double), cudaMemcpyDeviceToHost);
cudaMemcpy(dvelocities, d_dvelocities, n_atoms * sizeof(dvel_t), cudaMemcpyDeviceToHost);
cudaError_t err;
err = cudaMalloc((void **)&d_angles, n_angles * sizeof(angle_t));
if (err != cudaSuccess) {
fprintf(stderr, "CUDA malloc failed for d_angles: %s\n", cudaGetErrorString(err));
return 0.0;
}
err = cudaMalloc((void **)&d_coords, n_atoms * sizeof(coord_t));
if (err != cudaSuccess) {
fprintf(stderr, "CUDA malloc failed for d_coords: %s\n", cudaGetErrorString(err));
return 0.0;
}
err = cudaMalloc((void **)&d_cangles, n_cangles * sizeof(cangle_t));
if (err != cudaSuccess) {
fprintf(stderr, "CUDA malloc failed for d_cangles: %s\n", cudaGetErrorString(err));
return 0.0;
}
err = cudaMalloc((void **)&d_dvelocities, n_atoms * sizeof(dvel_t));
if (err != cudaSuccess) {
fprintf(stderr, "CUDA malloc failed for d_dvelocities: %s\n", cudaGetErrorString(err));
return 0.0;
}
err = cudaMalloc((void **)&d_energy_sum, sizeof(double));
if (err != cudaSuccess) {
fprintf(stderr, "CUDA malloc failed for d_energy_sum: %s\n", cudaGetErrorString(err));
return 0.0;
}
}
// copy data to device
err = cudaMemcpy(d_angles, angles, n_angles * sizeof(angle_t), cudaMemcpyHostToDevice);
if (err != cudaSuccess) {
fprintf(stderr, "CUDA memcpy failed for d_angles: %s\n", cudaGetErrorString(err));
return 0.0;
}
err = cudaMemcpy(d_coords, coords, n_atoms * sizeof(coord_t), cudaMemcpyHostToDevice);
if (err != cudaSuccess) {
fprintf(stderr, "CUDA memcpy failed for d_coords: %s\n", cudaGetErrorString(err));
return 0.0;
}
err = cudaMemcpy(d_cangles, cangles, n_cangles * sizeof(cangle_t), cudaMemcpyHostToDevice);
if (err != cudaSuccess) {
fprintf(stderr, "CUDA memcpy failed for d_cangles: %s\n", cudaGetErrorString(err));
return 0.0;
}
err = cudaMemcpy(d_dvelocities, dvelocities, n_atoms * sizeof(dvel_t), cudaMemcpyHostToDevice);
if (err != cudaSuccess) {
fprintf(stderr, "CUDA memcpy failed for d_dvelocities: %s\n", cudaGetErrorString(err));
return 0.0;
}
double h_energy_sum = 0.0;
err = cudaMemcpy(d_energy_sum, &h_energy_sum, sizeof(double), cudaMemcpyHostToDevice);
if (err != cudaSuccess) {
fprintf(stderr, "CUDA memcpy failed for d_energy_sum: %s\n", cudaGetErrorString(err));
return 0.0;
}
// launch kernel
calc_angle_forces_kernel<<<numBlocks, blockSize>>>(start, end, d_angles, d_coords, d_cangles, d_dvelocities, d_energy_sum);
err = cudaDeviceSynchronize();
if (err != cudaSuccess) {
fprintf(stderr, "CUDA device synchronize failed: %s\n", cudaGetErrorString(err));
return 0.0;
}
// copy results back to host
err = cudaMemcpy(&h_energy_sum, d_energy_sum, sizeof(double), cudaMemcpyDeviceToHost);
if (err != cudaSuccess) {
fprintf(stderr, "CUDA memcpy failed for h_energy_sum: %s\n", cudaGetErrorString(err));
return 0.0;
}
err = cudaMemcpy(dvelocities, d_dvelocities, n_atoms * sizeof(dvel_t), cudaMemcpyDeviceToHost);
if (err != cudaSuccess) {
fprintf(stderr, "CUDA memcpy failed for dvelocities: %s\n", cudaGetErrorString(err));
return 0.0;
}

Copilot uses AI. Check for mistakes.
Comment thread src/core/system.cu Outdated
Comment on lines +1017 to +1023
// if (run_gpu) {
// E_bond_p.Uangle = calc_angle_forces_host(0, n_angles_solute);
// E_bond_w.Uangle = calc_angle_forces_host(n_angles_solute, n_angles);
// } else {
E_bond_p.Uangle = calc_angle_forces(0, n_angles_solute);
E_bond_w.Uangle = calc_angle_forces(n_angles_solute, n_angles);
// }

Copilot AI Nov 17, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Commented-out code should be removed. If GPU functionality is not yet ready to be enabled, consider using a preprocessor directive like #ifdef USE_GPU_ANGLE_FORCES or removing the comments entirely. Leaving commented code in production reduces readability.

Suggested change
// if (run_gpu) {
// E_bond_p.Uangle = calc_angle_forces_host(0, n_angles_solute);
// E_bond_w.Uangle = calc_angle_forces_host(n_angles_solute, n_angles);
// } else {
E_bond_p.Uangle = calc_angle_forces(0, n_angles_solute);
E_bond_w.Uangle = calc_angle_forces(n_angles_solute, n_angles);
// }
#ifdef USE_GPU_ANGLE_FORCES
E_bond_p.Uangle = calc_angle_forces_host(0, n_angles_solute);
E_bond_w.Uangle = calc_angle_forces_host(n_angles_solute, n_angles);
#else
E_bond_p.Uangle = calc_angle_forces(0, n_angles_solute);
E_bond_w.Uangle = calc_angle_forces(n_angles_solute, n_angles);
#endif

Copilot uses AI. Check for mistakes.
Comment thread src/core/cuda/src/CudaAngleForce.cu Outdated
Comment on lines +97 to +102
}

// copy data to device
cudaMemcpy(d_angles, angles, n_angles * sizeof(angle_t), cudaMemcpyHostToDevice);
cudaMemcpy(d_coords, coords, n_atoms * sizeof(coord_t), cudaMemcpyHostToDevice);
cudaMemcpy(d_cangles, cangles, n_cangles * sizeof(cangle_t), cudaMemcpyHostToDevice);

Copilot AI Nov 17, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential performance bottleneck: Copying all coordinates and angle data to the GPU on every call is inefficient. Since n_atoms, n_angles, and n_cangles appear to be constant, these arrays should ideally be transferred once during initialization and only updated when necessary, not on every force calculation. Only coords (if it changes) and energy_sum initialization need to be transferred each iteration.

Suggested change
}
// copy data to device
cudaMemcpy(d_angles, angles, n_angles * sizeof(angle_t), cudaMemcpyHostToDevice);
cudaMemcpy(d_coords, coords, n_atoms * sizeof(coord_t), cudaMemcpyHostToDevice);
cudaMemcpy(d_cangles, cangles, n_cangles * sizeof(cangle_t), cudaMemcpyHostToDevice);
// Copy constant data to device only once
cudaMemcpy(d_angles, angles, n_angles * sizeof(angle_t), cudaMemcpyHostToDevice);
cudaMemcpy(d_cangles, cangles, n_cangles * sizeof(cangle_t), cudaMemcpyHostToDevice);
}
// Copy per-iteration data to device
cudaMemcpy(d_coords, coords, n_atoms * sizeof(coord_t), cudaMemcpyHostToDevice);

Copilot uses AI. Check for mistakes.
Comment thread src/core/cuda/include/CudaAngleForce.h Outdated
Comment on lines +1 to +4
#include "system.h"
__global__ void calc_angle_forces_kernel(int start, int end, angle_t *angles, coord_t *coords, cangle_t *cangles, dvel_t *dvelocities, double *energy_sum);

double calc_angle_forces_host(int start, int end); No newline at end of file

Copilot AI Nov 17, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing include guards. Header files should have include guards to prevent multiple inclusion errors. Add:

#ifndef CUDA_ANGLE_FORCE_H
#define CUDA_ANGLE_FORCE_H
// ... existing content ...
#endif
Suggested change
#include "system.h"
__global__ void calc_angle_forces_kernel(int start, int end, angle_t *angles, coord_t *coords, cangle_t *cangles, dvel_t *dvelocities, double *energy_sum);
double calc_angle_forces_host(int start, int end);
#ifndef CUDA_ANGLE_FORCE_H
#define CUDA_ANGLE_FORCE_H
#include "system.h"
__global__ void calc_angle_forces_kernel(int start, int end, angle_t *angles, coord_t *coords, cangle_t *cangles, dvel_t *dvelocities, double *energy_sum);
double calc_angle_forces_host(int start, int end);
#endif

Copilot uses AI. Check for mistakes.
Comment thread src/core/cuda/src/CudaAngleForce.cu Outdated
Comment on lines +90 to +96
if (!is_initialized) {
is_initialized = true;
cudaMalloc((void **)&d_angles, n_angles * sizeof(angle_t));
cudaMalloc((void **)&d_coords, n_atoms * sizeof(coord_t));
cudaMalloc((void **)&d_cangles, n_cangles * sizeof(cangle_t));
cudaMalloc((void **)&d_dvelocities, n_atoms * sizeof(dvel_t));
cudaMalloc((void **)&d_energy_sum, sizeof(double));

Copilot AI Nov 17, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memory leak: CUDA device memory is never freed. The allocated memory (d_angles, d_coords, d_cangles, d_dvelocities, d_energy_sum) should be freed when no longer needed, typically in a cleanup function or destructor. Consider adding a cleanup function like:

void cleanup_angle_forces() {
    if (is_initialized) {
        cudaFree(d_angles);
        cudaFree(d_coords);
        cudaFree(d_cangles);
        cudaFree(d_dvelocities);
        cudaFree(d_energy_sum);
        is_initialized = false;
    }
}

Copilot uses AI. Check for mistakes.
cudaMemcpy(d_angles, angles, n_angles * sizeof(angle_t), cudaMemcpyHostToDevice);
cudaMemcpy(d_coords, coords, n_atoms * sizeof(coord_t), cudaMemcpyHostToDevice);
cudaMemcpy(d_cangles, cangles, n_cangles * sizeof(cangle_t), cudaMemcpyHostToDevice);
cudaMemcpy(d_dvelocities, dvelocities, n_atoms * sizeof(dvel_t), cudaMemcpyHostToDevice);

Copilot AI Nov 17, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inefficient memory transfer pattern. The dvelocities array is copied to the device and then immediately copied back, but the kernel only accumulates values using atomicAdd. If dvelocities already contains values on the host that need to be preserved, this is correct. However, if it starts at zero for each force calculation, you can skip the H2D copy on line 103 and just initialize d_dvelocities to zero on the device using cudaMemset:

cudaMemset(d_dvelocities, 0, n_atoms * sizeof(dvel_t));

This would be more efficient than copying from host.

Suggested change
cudaMemcpy(d_dvelocities, dvelocities, n_atoms * sizeof(dvel_t), cudaMemcpyHostToDevice);
cudaMemset(d_dvelocities, 0, n_atoms * sizeof(dvel_t));

Copilot uses AI. Check for mistakes.
Comment on lines +90 to +91
if (!is_initialized) {
is_initialized = true;

Copilot AI Nov 17, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thread safety issue: The is_initialized flag is not thread-safe. If calc_angle_forces_host could be called from multiple threads concurrently, there's a race condition where multiple threads might try to initialize the CUDA memory simultaneously. Consider using proper synchronization (e.g., std::call_once or a mutex) if multi-threading is a possibility.

Copilot uses AI. Check for mistakes.
Comment on lines +70 to +80
atomicAdd(&dvelocities[i].x, dv * di.x);
atomicAdd(&dvelocities[i].y, dv * di.y);
atomicAdd(&dvelocities[i].z, dv * di.z);

atomicAdd(&dvelocities[k].x, dv * dk.x);
atomicAdd(&dvelocities[k].y, dv * dk.y);
atomicAdd(&dvelocities[k].z, dv * dk.z);

atomicAdd(&dvelocities[j].x, -dv * (di.x + dk.x));
atomicAdd(&dvelocities[j].y, -dv * (di.y + dk.y));
atomicAdd(&dvelocities[j].z, -dv * (di.z + dk.z));

Copilot AI Nov 17, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Atomic operations may cause performance degradation. The kernel uses atomicAdd for updating dvelocities on three different atoms per angle. For systems with many angles sharing the same atoms, this can lead to high contention and reduced performance. Consider using a reduction pattern or thread-local accumulation if performance becomes an issue.

Copilot uses AI. Check for mistakes.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/core/cuda/src/CudaImproper2Force.cu Outdated
#include "utils.h"

namespace CudaImproper2Force {
bool initialized = false;

Copilot AI Nov 18, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent initialization flag naming across bonded force modules. This file uses initialized while CudaAngleForce, CudaBondForce, and CudaTorsionForce use is_initialized. Rename to is_initialized for consistency.

Copilot uses AI. Check for mistakes.
Comment thread src/core/cuda/src/CudaImproper2Force.cu Resolved
Comment on lines +85 to +87
if (!is_initialized) {
is_initialized = true;
check_cudaMalloc((void**)&d_angles, n_angles * sizeof(angle_t));

Copilot AI Nov 18, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialization flag is set before memory allocation, while CudaBondForce, CudaTorsionForce, and CudaImproper2Force set it after. If check_cudaMalloc fails (e.g., out of memory), the flag will incorrectly indicate initialization completed. Move line 86 to after line 91 for consistency and correctness.

Copilot uses AI. Check for mistakes.
Comment thread src/core/cuda/src/CudaBondForce.cu Resolved
Comment on lines +57 to +60
cudaMemcpy(d_bonds, bonds, sizeof(bond_t) * n_bonds, cudaMemcpyHostToDevice);
cudaMemcpy(d_coords, coords, sizeof(coord_t) * n_atoms, cudaMemcpyHostToDevice);
cudaMemcpy(d_cbonds, cbonds, sizeof(cbond_t) * n_cbonds, cudaMemcpyHostToDevice);
cudaMemcpy(d_dvelocities, dvelocities, sizeof(dvel_t) * n_atoms, cudaMemcpyHostToDevice);

Copilot AI Nov 18, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Full array copies occur on every function call. Consider copying only constant data (bonds, cbonds, coords) once during initialization and only copying mutable data (dvelocities) per call. The coords array could potentially be updated less frequently if it only changes between timesteps rather than between force component calculations. This pattern should be evaluated across all four bonded force modules.

Copilot uses AI. Check for mistakes.
@goodstudyqaq
goodstudyqaq merged commit 67d722c into feature/qgpu Nov 20, 2025
@ChielJespers
ChielJespers deleted the feature/qgpu-bonded-force branch March 2, 2026 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants