我正在運行聚合物的蒙特卡羅模擬。系統當前狀態的整個配置由名為 的物件給出Grid。這是我的定義Grid:
class Grid{
public:
std::vector <Polymer> PolymersInGrid; // all the polymers in the grid
int x; // length of x-edge of grid
int y; // length of y-edge of grid
int z; // length of z-edge of grid
double kT; // energy factor
double Emm_n ; // monomer-solvent when Not aligned
double Emm_a ; // monomer-solvent when Aligned
double Ems; // monomer-solvent interaction
double Energy; // energy of grid
std::map <std::vector <int>, Particle> OccupancyMap; // a map that gives the particle given the location
Grid(int xlen, int ylen, int zlen, double kT_, double Emm_a_, double Emm_n_, double Ems_): x (xlen), y (ylen), z (zlen), kT (kT_), Emm_n(Emm_n_), Emm_a (Emm_a_), Ems (Ems_) { // Constructor of class
// this->instantiateOccupancyMap();
};
// Destructor of class
~Grid(){
};
// assignment operator that allows for a correct transfer of properties. Important to functioning of program.
Grid& operator=(Grid other){
std::swap(PolymersInGrid, other.PolymersInGrid);
std::swap(Energy, other.Energy);
std::swap(OccupancyMap, other.OccupancyMap);
return *this;
}
.
.
.
}
如果需要,Polymer我可以深入了解物件的詳細資訊。Particle
在我的驅動程式代碼中,這就是我要做的:定義最大迭代次數。
- 定義一個完整的 Grid
G。 G創建被呼叫的副本G_。- 我正在擾亂
G_. G_如果根據 Metropolis 標準接受擾動,我分配G_給G(G=G_)。- 重復步驟 1-4,直到達到最大迭代次數。
這是我的驅動程式代碼:
auto start = std::chrono::high_resolution_clock::now();
Grid G_ (G);
int acceptance_count = 0;
for (int i{1}; i< (Nmov 1); i ){
// choose a move
G_ = MoveChooser(G, v);
if ( MetropolisAcceptance (G.Energy, G_.Energy, G.kT) ) {
// accepted
// replace old config with new config
acceptance_count ;
std::cout << "Number of acceptances is " << acceptance_count << std::endl;
G = G_;
}
else {
// continue;
}
if (i % dfreq == 0){
G.dumpPositionsOfPolymers (i, dfile) ;
G.dumpEnergyOfGrid(i, efile, call) ;
}
// G.PolymersInGrid.at(0).printChainCoords();
}
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds> (stop-start);
std::cout << "\n\nTime taken for simulation: " << duration.count() << " milliseconds" << std::endl;
This is the interesting part: if I run the simulation using condition that do not have lots of "acceptances" (low temperatures, bad solvent), the simulation runs pretty fast. However, if there are a large number of acceptances, the simulation gets incredibly slow. My hypothesis is that my assignment operator = is slowing down my simulation. I ran some tests:
number of acceptances = 25365, wall-clock time = 717770 milliseconds (!)
number of acceptances = 2165, wall-clock time = 64412 milliseconds
number of acceptances = 3000, wall-clock time = 75550 milliseconds
And the trend continues. Could anyone advise me on how to possibly make this more efficient? Is there a way to bypass the slowdown I am experiencing, I think, due to the = operator?
I would really appreciate any advice you have for me!
uj5u.com熱心網友回復:
您當然可以做的一件事來提高性能是強制移動_G而不是應對G:
G = std::move(G_);
畢竟,在這個階段你不再需要G_了。
旁注。您不需要復制所有成員資料的事實operator=表明您的設計Grid遠非完美,但是,如果程式很小并且您確定您可以控制一切,請保留它。無論如何,而不是 using operator=,您應該定義和使用具有有意義名稱的成員函式,例如 " fast_and_dirty_swap" 等 :-) 然后您可以定義operator=@Jarod42 建議的方式,即 using = default。
我在 C 11 之前使用的另一種方法是對指標進行操作。在這種情況下,一個將有兩個Grids,一個是“真實的”,一個被視為緩沖區或沙箱,并且在接受時會簡單地交換指標,因此填充的“緩沖區”MoveChooser將成為真實的,當前的Grid。
偽代碼:
- 創建兩個緩沖區
previous和current,每個緩沖區都能夠存盤模擬狀態 - 初始化
current - 創建兩個指標,
p_prev = &previous,p_curr = ¤rt - 盡可能多的步驟
- 計算下一個狀態
*p_curr并將其存盤在其中*p_prev(例如monte_carlo_step(p_curr, p_prev) - 交換指標:現在當前系統狀態是 at
p_curr和之前的 atp_prev。
- 計算下一個狀態
- 分析存盤在
*p_curr
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/411693.html
標籤:
