Skip to content

Commit f71d95b

Browse files
committed
Small refactoring
1 parent 0c68645 commit f71d95b

File tree

9 files changed

+89
-62
lines changed

9 files changed

+89
-62
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ target_link_libraries(sparsehc-dm ${LIBS})
2222
add_definitions(-std=c++11)
2323

2424
file(GLOB SHARED_SOURCES src/averagecluster.cpp src/cluster.cpp src/common.cpp src/completecluster.cpp src/dendrogram.cpp src/inmatrix.cpp src/matrix.cpp src/singlecluster.cpp)
25-
ADD_LIBRARY(sparsehc_dm SHARED sparsehc-dm_python.cpp ${SHARED_SOURCES})
25+
ADD_LIBRARY(sparsehc_dm SHARED python/sparsehc-dm_python.cpp ${SHARED_SOURCES})
2626
target_link_libraries(sparsehc_dm ${LIBS})
27-
set_target_properties(sparsehc_dm PROPERTIES PREFIX "")
27+
set_target_properties(sparsehc_dm PROPERTIES PREFIX "" SUFFIX ".so")
28+

README

Lines changed: 0 additions & 32 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ for i in range(0,N-1):
2121
Z=linkage(D, method='complete',preserve_input=False)
2222
```
2323
###Instalation
24-
####Prerequisits: boost graph and stxxl library
24+
####Prerequisites: boost graph and stxxl library
2525
```
2626
sudo apt-get install libboost-graph-dev libstxxl-dev libstxxl1
2727
```

examples/benchmark.sh

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
#!/bin/bash
2-
touch fastcluster_load.txt sparsehc-dm_load.txt sparsehc-dm_load_pyRMSD.txt
2+
33
for Nk in 10 20 40 80
44
do
5-
/usr/bin/time -v python fastcluster_load.py $Nk 2>&1 | grep 'finished\|Maximum' >> fastcluster_load.txt
5+
/usr/bin/time -v python fastcluster_load.py $Nk 2>&1 | grep 'finished\|Maximum' >> fastcluster_perf.txt
66
done
77

8-
for Nk in 10 20 40 80 100 200 400
8+
for Nk in 10 20 40
99
do
10-
/usr/bin/time -v python sparsehc-dm_load.py $Nk 2>&1 | grep 'finished\|Maximum' >> sparsehc-dm_load.txt
10+
./cpptraj_cluster.sh $Nk 2>&1 >> cpptraj_perf.txt
1111
done
1212

13-
for Nk in 10 20 40 80 100 200 400
13+
for Nk in 10 20 40 80 100 200 #400 700
1414
do
15-
/usr/bin/time -v python sparsehc-dm_load_pyRMSD.py $Nk 2>&1 | grep 'finished\|Maximum' >> sparsehc-dm_load_pyRMSD.txt
16-
done
15+
/usr/bin/time -v python sparsehc-dm_load.py $Nk 2>&1 | grep 'finished\|Maximum' >> sparsehc-dm_perf.txt
16+
done
17+

examples/cpptraj_cluster.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
Nk=$1
3+
PARMFILE=aMD-148l-all_1.pdb
4+
TRAJINFILE=aMD-148l-first${Nk}k.nc
5+
6+
EPSILON=30.0
7+
8+
cat <<EOF | /usr/bin/time -v cpptraj.OMP
9+
parm ${PARMFILE}
10+
trajin ${TRAJINFILE}
11+
12+
cluster hieragglo epsilon $EPSILON complete rms :1-9999@CA out frame_clstr.dat summary clstr-summary.dat \
13+
info info.dat
14+
15+
go
16+
EOF
17+
cd ..

examples/sparsehc-dm_load.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import random
55
import time
66
import mdtraj as md
7+
import numpy
78

89
N=int(sys.argv[1])
910
traj_filename='aMD-148l-first{}k.nc'.format(N)
@@ -14,15 +15,21 @@
1415

1516
start=time.time()
1617
traj=md.load(traj_filename,top=top_filename, atom_indices=atoms_to_keep)
18+
19+
m=sparsehc_dm.InMatrix()
1720
finishedLoad=time.time()
1821
print ("finished loading ({}k): {}".format(N,finishedLoad-start))
1922

20-
m=sparsehc_dm.InMatrix()
2123
N=traj.n_frames
24+
rmsds=list()
2225
for i in range(0,N-1):
23-
rmsds=md.rmsd(traj, traj, i)[i+1:]
26+
rmsds=md.rmsd(traj, traj, i)[i+1:].tolist()
2427
sparsehc_dm.push(m,rmsds,i)
25-
28+
#if( (i+1)%20==0):
29+
#sparsehc_dm.push(m,rmsds,i-19,N)
30+
#rmsds=list()
31+
32+
2633
#for i in range(0,N-1):
2734
#rmsds=md.rmsd(traj, traj, i)
2835
#for j in range(i+1,N):

python/setup.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from distutils.core import setup
2+
from distutils.extension import Extension
3+
setup(name="sparsehc-dm",
4+
ext_modules=[
5+
Extension("sparsehc-dm",["sparsehc-dm_python.cpp",],
6+
library_dirs=["/usr/local/lib",],
7+
libraries=["",],
8+
include_dirs=[".","../src"],
9+
depends=[]),
10+
]
11+
)

sparsehc-dm_python.cpp renamed to python/sparsehc-dm_python.cpp

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <memory>
22
#include <boost/python.hpp>
33
#include <boost/python/numeric.hpp>
4+
#include <boost/python/stl_iterator.hpp>
45
#include "common.h"
56
#include "inmatrix.h"
67
#include "averagecluster.h"
@@ -40,23 +41,37 @@ boost::python::list linkage(InMatrix& mat, const std::string& method) {
4041

4142
}
4243

43-
void pushList(InMatrix& mat,const boost::python::list& list, int i)
44+
/*TODO: improve pushList() performance by introducing a buffer to first copy values
45+
from Python::list and then push them to InMatrix in a separate thread.
46+
*/
47+
template<typename T>
48+
void pushList(InMatrix& mat,const T& list, unsigned i/*,const unsigned rowLength*/)
4449
{
45-
Element e;
46-
for(unsigned j=0; j<boost::python::len(list); ++j) {
47-
float v=boost::python::extract<float>(list[j]);
48-
e.update(i,j+i+1,v);
49-
mat.push(e);
50-
}
51-
}
52-
void pushNp(InMatrix& mat,const boost::python::numeric::array& list, int i)
53-
{
54-
Element e;
55-
for(unsigned j=0; j<boost::python::len(list); ++j) {
56-
float v=boost::python::extract<float>(list[j]);
57-
e.update(i,j+i+1,v);
58-
mat.push(e);
50+
/*const unsigned n=boost::python::len(list);
51+
std::vector<float> vec;
52+
vec.resize(n);*/
53+
unsigned j=i+1;
54+
std::for_each(boost::python::stl_input_iterator<float>(list),
55+
boost::python::stl_input_iterator<float>(),
56+
[&mat,&i,&j](const float& v) {
57+
mat.push(Element(i,j++,v));
58+
});
59+
/*std::copy(boost::python::stl_input_iterator<float>(list),
60+
boost::python::stl_input_iterator<float>(),vec.begin());
61+
62+
auto it=boost::python::stl_input_iterator<float>(list);
63+
while(it!=boost::python::stl_input_iterator<float>()) {
64+
for(unsigned j=i+1; j<rowLength; j++) {
65+
//std::cout<<"i="<<i<<" j="<<j<<" n="<<n<<std::endl;
66+
vec.emplace_back(i,j,*it++);
67+
}
68+
++i;
5969
}
70+
{ vec.emplace_back(i,i+1+j++,v);}
71+
mat.push(Element(i,i+1+j++,v));
72+
for(auto& e:vec) {
73+
mat.push(std::move(e));
74+
}*/
6075
}
6176
void (InMatrix::*push)(unsigned, unsigned, float) = &InMatrix::push;
6277
void (InMatrix::*pushElement)(Element) = &InMatrix::push;
@@ -69,6 +84,8 @@ BOOST_PYTHON_MODULE(sparsehc_dm)
6984
class_<InMatrix,boost::noncopyable>("InMatrix")
7085
.def("push",push);
7186
def("linkage", linkage);
72-
def("push", pushNp);
87+
//boost::python::list
88+
//boost::python::numeric::array
89+
def("push", pushList<boost::python::list>);
7390

7491
}

src/inmatrix.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ struct InMatrix: public Matrix {
4747
push(Element(row,col,value));
4848
}
4949
void push(Element e) {
50-
threshold=threshold<e.value?nextafterf(e.value,e.value+100.0f):threshold;
50+
threshold=threshold<e.value?e.value:threshold;
5151
sorter.push(std::move(e));
5252
}
53+
/*void push(float v) {
54+
threshold=threshold<v?v:threshold;
55+
sorter.push(std::move(e));
56+
}*/
5357

5458
/*void pushvec(std::vector<float> v,unsigned i) {
5559
for(unsigned j=0; j<v.size(); ++j) {
@@ -60,6 +64,7 @@ struct InMatrix: public Matrix {
6064
}
6165
}*/
6266
void sort() {
67+
threshold=nextafterf(threshold,threshold+100.0f);
6368
sorter.sort();
6469
numPoints=(1L+sqrt(1L+sorter.size()*8L))/2L;
6570
}

0 commit comments

Comments
 (0)