Skip to content

Instantly share code, notes, and snippets.

@Bike
Created June 29, 2023 15:39
Show Gist options
  • Save Bike/cefdadd69f6066c8d3e9648c0fb379fb to your computer and use it in GitHub Desktop.
Save Bike/cefdadd69f6066c8d3e9648c0fb379fb to your computer and use it in GitHub Desktop.
autovectorization test
#include <iostream>
#include <cmath>
// clang++15 vtest.cc -O2 -ffast-math -Rpass=loop-vectorize -mavx2 -o vtest
struct Stretch {
float kb;
float r0;
int I1;
int I2;
};
float stretch_energy(Stretch* stretch_begin,
Stretch* stretch_end,
float* pos) {
float Energy = 0.0;
for (auto stretch = stretch_begin; stretch < stretch_end; ++stretch) {
int I1 = stretch->I1, I2 = stretch->I2;
float x1 = pos[I1+0], y1 = pos[I1+1], z1 = pos[I1+2];
float x2 = pos[I2+0], y2 = pos[I2+1], z2 = pos[I2+2];
float xdist = x1 - x2, ydist = y1 - y2, zdist = z1 - z2;
float dist = std::sqrt(xdist*xdist+ydist*ydist+zdist*zdist);
float StretchDeviation = dist - stretch->r0;
Energy += stretch->kb * StretchDeviation * StretchDeviation;
}
return Energy;
}
int main(int argc, const char* argv[]) {
float pos[12] = {0.0, 19.0,3.0, 10.0, 7.0, 80.0,
20.0, 15.0,17.0, 25.0, 44.0, 23.0 };
Stretch stretch[] = { {10.0, 2.0, 0, 3}, {20.0, 3.0, 6, 9} };
float energy = stretch_energy(&stretch[0], &stretch[2], pos);
std::cout << "Energy = " << energy << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment