-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathfingerprintdb_cuda.h
162 lines (131 loc) · 5.23 KB
/
fingerprintdb_cuda.h
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* -------------------------------------------------------------------------
* Declares gpusim::FingerprintDB CUDA enabled similarity scoring
*
* Copyright Schrodinger LLC, All Rights Reserved.
--------------------------------------------------------------------------- */
#ifndef FINGERPRINTDB_CUDA
#define FINGERPRINTDB_CUDA
#include <memory>
#include <utility>
#include <vector>
#include <QObject>
#include <QString>
#include "types.h"
class QByteArray;
class QSize;
namespace gpusim
{
class FingerprintDB;
class FingerprintDBPriv;
struct StorageResultObject;
typedef std::pair<char*, char*> ResultData;
typedef std::pair<float, ResultData> SortableResult;
unsigned int get_gpu_count();
unsigned int get_next_gpu(size_t required_memory);
class FingerprintDBStorage
{
public:
friend class FingerprintDB;
FingerprintDBStorage(FingerprintDB* parent, std::vector<char>& fp_data,
int index_offset, int fp_bitcount);
unsigned int getOffsetIndex(unsigned int without_offset);
private:
FingerprintDB* m_parent;
std::vector<int> m_data;
std::shared_ptr<FingerprintDBPriv> m_priv; // Used to conceal cuda types
const unsigned int m_index_offset;
const int m_count;
int m_gpu_device;
};
class FingerprintDB : public QObject
{
friend class FingerprintDBStorage;
public:
FingerprintDB(int fp_bitcount, int fp_count, const QString& dbkey,
std::vector<std::vector<char>>& data,
std::vector<char*>& smiles_vector,
std::vector<char*>& ids_vector);
/**
* @brief
* Copy fingerprint memory up to the GPU, folding to a smaller size
* if necessary
*
* @param fold_factor: Minimum factor to fold fingerprints by, might need
* fold by a bigger factor to get even folding
*/
void copyToGPU(unsigned int fold_factor);
// Total number of fingerprints in DB
unsigned int count() const { return m_total_count; };
/*
* @brief
* This function takes an index in the range of all molecules, and finds
* which storage block that index is inside, and returns that storage block
* and the local index inside that block
*/
void getStorageAndLocalIndex(unsigned int offset_index,
FingerprintDBStorage** storage,
unsigned int* local_index) const;
/**
* @brief
* Get an indexed fingerprint that's currently stored in the database
*
* @param index: index of fingerprint to return
* @return: Fingerprint data
*/
Fingerprint getFingerprint(unsigned int index) const;
/**
* @brief
* Search the database for top ten most similar fingerprints to query
*
* @param query: Fingerprint to find closest matches to
* @param results_smiles: Vector to store smiles of results
* @param results_ids: Vector to store IDs of results
* @param results_scores: Vector to store scores of results
* @param return_count: Maximum number of results to return
* @param similarity_cutoff: Minimum similarity score to return
*/
void search(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;
void 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;
char* getSmiles(int index) const { return m_smiles[index]; }
char* getID(int index) const { return m_ids[index]; }
size_t getFingerprintDataSize() const { return m_total_data_size; };
int getFingerprintBitcount() const
{
return m_fp_intsize * sizeof(int) * 8;
}
void search_storage(const Fingerprint& query,
const std::shared_ptr<FingerprintDBStorage>& storage,
StorageResultObject* results, unsigned int return_count,
float similarity_cutoff) const;
protected:
// INTERNAL: A CPU implementation of tanimoto similarity for validation
float tanimoto_similarity_cpu(const Fingerprint& fp1,
const Fingerprint& fp2) const;
std::vector<int> fold_data(const std::vector<int>& unfolded) const;
std::vector<std::shared_ptr<FingerprintDBStorage>> m_storage;
int m_total_count, m_fp_intsize, m_fold_factor;
size_t m_total_data_size;
std::vector<char*> m_smiles;
std::vector<char*> m_ids;
QString m_dbkey;
};
size_t get_available_gpu_memory();
/**
* @brief
* Used for CPU sorting to just get top results in O(number_required*N)
*/
void top_results_bubble_sort(std::vector<int>& indices,
std::vector<float>& scores, int number_required);
std::vector<int> fold_fingerprint(std::vector<int>&, const int);
} // namespace gpusim
#endif