-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathfingerprintdb_cuda.cpp
104 lines (88 loc) · 3.39 KB
/
fingerprintdb_cuda.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* -------------------------------------------------------------------------
* Implements gpusim::FingerprintDB *NON*-CUDA functionality
* This is in its own file so that cuda and Qt don't have to intermix
*
* Copyright Schrodinger LLC, All Rights Reserved.
--------------------------------------------------------------------------- */
#include "fingerprintdb_cuda.h"
#include <QDebug>
#include <QString>
#include <QThreadPool>
#include <QtConcurrent/QtConcurrentMap>
#include "calculation_functors.h"
using namespace gpusim;
using std::vector;
void FingerprintDB::search_cpu(const Fingerprint& query, const QString& dbkey,
unsigned int max_return_count,
float similarity_cutoff,
std::vector<char*>& results_smiles,
std::vector<char*>& results_ids,
std::vector<float>& results_scores,
unsigned long& approximate_result_count) const
{
if (dbkey != m_dbkey) {
qDebug() << "Key check failed, returning empty results";
return;
}
const int total = count();
vector<int> indices(total);
std::iota(indices.begin(), indices.end(), 0);
vector<float> scores(total);
// TODO: CPU searching broken for >1GB, needs to be fixed below
// TODO: CPU searching broken, not giving approximate total count back
// Scoring parallelizes well, but bottleneck is now sorting
QtConcurrent::blockingMap(
indices,
TanimotoFunctorCPU(query, m_fp_intsize, m_storage[0]->m_data, scores));
top_results_bubble_sort(indices, scores, max_return_count);
// Push top return_count results to CPU results vectors to be returned
for (unsigned int i = 0; i < max_return_count; i++) {
results_smiles.push_back(m_smiles[indices[i]]);
results_ids.push_back(m_ids[indices[i]]);
results_scores.push_back(scores[i]);
}
}
std::vector<int>
FingerprintDB::fold_data(const std::vector<int>& unfolded) const
{
vector<int> folded(unfolded.size() / m_fold_factor);
std::fill(folded.begin(), folded.end(), 0);
vector<int> indices(unfolded.size() / m_fp_intsize);
std::iota(indices.begin(), indices.end(), 0);
QtConcurrent::blockingMap(
indices, FoldFingerprintFunctorCPU(m_fold_factor, m_fp_intsize,
unfolded, folded));
return folded;
}
namespace gpusim
{
static inline void swap(vector<int>& indices, vector<float>& scores,
const int idx1, const int idx2)
{
int temp = indices[idx1];
indices[idx1] = indices[idx2];
indices[idx2] = temp;
float tempf = scores[idx1];
scores[idx1] = scores[idx2];
scores[idx2] = tempf;
}
/**
* @internal
* This performs a partial bubble sort, concluding after the top N scores
* have been sorted.
* NOTE: Resulting vectors are *UNSORTED* beyond N positions
* This version of bubble sort is only O(N*len(scores)), where N is small
*/
void top_results_bubble_sort(vector<int>& indices, vector<float>& scores,
int number_required)
{
const int count = indices.size();
for (int i = 0; i < number_required; i++) {
for (int j = (count - 1); j > i; j--) {
if (scores[j] > scores[j - 1]) {
swap(indices, scores, j, j - 1);
}
}
}
}
} // End namespace gpusim