简体   繁体   English

C++中的多线程分子模拟

[英]Multi-threading molecular simulations in C++

I am developing a molecular dynamics simulation code in C++, which essentially takes atom positions and other properties as input and simulates their motion under Newton's laws of motion.我正在用 C++ 开发一个分子动力学模拟代码,它基本上以原子位置和其他属性作为输入,并在牛顿运动定律下模拟它们的运动。 The core algorithm uses what's called the Velocity Verlet scheme and looks like:核心算法使用所谓的 Velocity Verlet 方案,如下所示:

//  iterate through time (k=[1,#steps])
double Dt = 0.002; // time step
double Ttot = 1.0; // total time
double halfDt = Dt/2.0;

for (int k = 1; k*Dt <= Ttot; k++){
    for (int i = 0; i < number_particles; i++)
        vHalf[i] = p[i].velocity + F[i]*halfDt; // step 1

    for (int i = 0; i < number_particles; i++)
        p[i].position += vHalf[i]*Dt; // step 2

    for (int i = 0; i < number_particles; i++)
        F[i] = Force(p,i); // recalculate force on all particle i's

    for (int i = 0; i < number_particles; i++)
        p[i].velocity = vHalf[i] + F[i]*halfDt; // step 3
}

Where p is an array of class objects which store things like particle position, velocity, mass, etc. and Force is a function that calculates the net force on a particle using something like Lennard-Jones potential.其中p是一个类对象数组,用于存储诸如粒子位置、速度、质量等内容,而Force是一个函数,它使用诸如 Lennard-Jones 势之类的东西来计算粒子上的净力。

My question regards the time required to complete the calculation;我的问题是关于完成计算所需的时间; all of my subroutines are optimized in terms of crunching numbers (eg using x*x*x to raise to the third power instead of pow(x,3) ), but the main issue is the time loop will often be performed for millions of iterations and there are typically close to a million particles.我的所有子程序都在处理数字方面进行了优化(例如使用x*x*x提升到三次方而不是pow(x,3) ),但主要问题是时间循环通常会执行数百万次迭代并且通常有接近一百万个粒子。 Is there any way to implement this algorithm using multi-threading?有没有办法使用多线程来实现这个算法? From my understanding, multi-threading essentially opens another stream of data to and from a CPU core, which would allow me to run two different simulations at the same time;根据我的理解,多线程本质上打开了另一个进出 CPU 内核的数据流,这将允许我同时运行两个不同的模拟; I would like to use multi-threading to make just one of these simulations run faster我想使用多线程来使这些模拟之一运行得更快

I'd recommend using OpenMP .我建议使用OpenMP

Your specific use case is trivially parallelizable.您的特定用例可以简单地并行化。 Prallelization should be as simple as:颗粒化应该很简单:

double Dt = 0.002; // time step
double Ttot = 1.0; // total time
double halfDt = Dt/2.0;

for (int k = 1; k*Dt <= Ttot; k++){

    #pragma omp parallel for
    for (int i = 0; i < number_particles; i++)
        vHalf[i] = p[i].velocity + F[i]*halfDt; // step 1
        p[i].position += vHalf[i]*Dt; // step 2

    #pragma omp parallel for
    for (int i = 0; i < number_particles; i++)
        F[i] = Force(p,i); // recalculate force on all particle i's    
        p[i].velocity = vHalf[i] + F[i]*halfDt; // step 3
}

Most popular compilers and platforms have support for OpenMP.大多数流行的编译器和平台都支持 OpenMP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM